Line data Source code
1 : // SPDX-License-Identifier: LGPL-2.1-or-later
2 : export type JsonValue = null | boolean | number | string | JsonValue[] | { [key: string]: JsonValue };
3 : export type JsonObject = Record<string, JsonValue>;
4 :
5 : export type StrOrBytes = string | Uint8Array;
6 :
7 : declare global {
8 : interface CockpitMockOptions {
9 : url_root?: string;
10 : pathname?: string;
11 : last_transport?: unknown;
12 : url?: string;
13 : }
14 :
15 : interface Window {
16 : mock?: CockpitMockOptions;
17 : debugging?: string;
18 : }
19 : }
20 :
21 : /*
22 : * The debugging property is a global that is used
23 : * by various parts of the code to show/hide debug
24 : * messages in the javascript console.
25 : *
26 : * We support using storage to get/set that property
27 : * so that it carries across the various frames or
28 : * alternatively persists across refreshes.
29 : */
30 127 : if (typeof window.debugging === "undefined") {
31 127 : try {
32 : // Sometimes this throws a SecurityError such as during testing
33 127 : Object.defineProperty(window, "debugging", {
34 138 : get: function() { return window.sessionStorage.debugging || window.localStorage.debugging },
35 3 : set: function(x) { window.sessionStorage.debugging = x }
36 127 : });
37 19 : } catch { }
38 127 : }
39 :
40 98 : export function in_array(array: unknown[], val: unknown): boolean {
41 98 : const length = array.length;
42 92 : for (let i = 0; i < length; i++) {
43 92 : if (val === array[i])
44 10 : return true;
45 92 : }
46 98 : return false;
47 98 : }
48 :
49 138 : export function is_function(x: unknown): x is (...args: unknown[]) => unknown {
50 138 : return typeof x === 'function';
51 138 : }
52 :
53 127 : export function is_object(x: unknown): x is object {
54 127 : return x !== null && typeof x === 'object';
55 127 : }
56 :
57 126 : export function is_plain_object(x: unknown): boolean {
58 126 : return is_object(x) && Object.prototype.toString.call(x) === '[object Object]';
59 126 : }
60 :
61 138 : export function invoke_functions<F extends (...args: never[]) => void>(functions: F[], self: ThisType<F>, args: Parameters<F>): void {
62 138 : const length = functions?.length ?? 0;
63 138 : for (let i = 0; i < length; i++) {
64 138 : if (functions[i])
65 138 : functions[i].apply(self, args);
66 138 : }
67 138 : }
68 :
69 18 : export function iterate_data(data: StrOrBytes, callback: (chunk: StrOrBytes) => void, batch: number = 64 * 1024): void {
70 18 : if (typeof data === 'string') {
71 18 : for (let i = 0; i < data.length; i += batch) {
72 18 : callback(data.substring(i, i + batch));
73 18 : }
74 1 : } else if (data) {
75 1 : for (let i = 0; i < data.byteLength; i += batch) {
76 1 : const n = Math.min(data.byteLength - i, batch);
77 1 : callback(new Uint8Array(data.buffer, i, n));
78 1 : }
79 1 : }
80 18 : }
81 :
82 123 : export function join_data(buffers: StrOrBytes[], binary: boolean): StrOrBytes {
83 123 : if (!binary)
84 123 : return buffers.join("");
85 :
86 19 : let total = 0;
87 19 : const length = buffers.length;
88 19 : for (let i = 0; i < length; i++)
89 19 : total += buffers[i].length;
90 :
91 19 : const data = new Uint8Array(total);
92 19 : for (let j = 0, i = 0; i < length; i++) {
93 19 : data.set(buffers[i] as Uint8Array, j);
94 19 : j += buffers[i].length;
95 19 : }
96 :
97 19 : return data;
98 123 : }
|