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 368 : if (typeof window.debugging === "undefined") {
31 368 : try {
32 : // Sometimes this throws a SecurityError such as during testing
33 368 : Object.defineProperty(window, "debugging", {
34 399 : get: function() { return window.sessionStorage.debugging || window.localStorage.debugging },
35 3 : set: function(x) { window.sessionStorage.debugging = x }
36 368 : });
37 67 : } catch { }
38 368 : }
39 :
40 296 : export function in_array(array: unknown[], val: unknown): boolean {
41 296 : const length = array.length;
42 289 : for (let i = 0; i < length; i++) {
43 289 : if (val === array[i])
44 26 : return true;
45 289 : }
46 296 : return false;
47 296 : }
48 :
49 399 : export function is_function(x: unknown): x is (...args: unknown[]) => unknown {
50 399 : return typeof x === 'function';
51 399 : }
52 :
53 368 : export function is_object(x: unknown): x is object {
54 368 : return x !== null && typeof x === 'object';
55 368 : }
56 :
57 366 : export function is_plain_object(x: unknown): boolean {
58 366 : return is_object(x) && Object.prototype.toString.call(x) === '[object Object]';
59 366 : }
60 :
61 399 : export function invoke_functions<F extends (...args: never[]) => void>(functions: F[], self: ThisType<F>, args: Parameters<F>): void {
62 399 : const length = functions?.length ?? 0;
63 399 : for (let i = 0; i < length; i++) {
64 399 : if (functions[i])
65 399 : functions[i].apply(self, args);
66 399 : }
67 399 : }
68 :
69 30 : export function iterate_data(data: StrOrBytes, callback: (chunk: StrOrBytes) => void, batch: number = 64 * 1024): void {
70 30 : if (typeof data === 'string') {
71 30 : for (let i = 0; i < data.length; i += batch) {
72 30 : callback(data.substring(i, i + batch));
73 30 : }
74 2 : } else if (data) {
75 2 : for (let i = 0; i < data.byteLength; i += batch) {
76 2 : const n = Math.min(data.byteLength - i, batch);
77 2 : callback(new Uint8Array(data.buffer, i, n));
78 2 : }
79 2 : }
80 30 : }
81 :
82 363 : export function join_data(buffers: StrOrBytes[], binary: boolean): StrOrBytes {
83 363 : if (!binary)
84 363 : return buffers.join("");
85 :
86 66 : let total = 0;
87 66 : const length = buffers.length;
88 66 : for (let i = 0; i < length; i++)
89 66 : total += buffers[i].length;
90 :
91 66 : const data = new Uint8Array(total);
92 66 : for (let j = 0, i = 0; i < length; i++) {
93 66 : data.set(buffers[i] as Uint8Array, j);
94 66 : j += buffers[i].length;
95 66 : }
96 :
97 66 : return data;
98 363 : }
|