| 1 |
/* |
| 2 |
* Copyright (C) 2014 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
/* eslint-disable indent,no-empty */ |
| 7 |
|
| 8 |
import { base64_encode, base64_decode } from './cockpit/_internal/base64'; |
| 9 |
import { Channel } from './cockpit/_internal/channel'; |
| 10 |
import { |
| 11 |
in_array, is_function, is_object, is_plain_object, invoke_functions, iterate_data, join_data |
| 12 |
} from './cockpit/_internal/common'; |
| 13 |
import { Deferred, later_invoke } from './cockpit/_internal/deferred'; |
| 14 |
import { event_mixin } from './cockpit/_internal/event-mixin'; |
| 15 |
import { transport_origin, calculate_application, calculate_url } from './cockpit/_internal/location-utils'; |
| 16 |
import { Location } from 'cockpit/_internal/location'; |
| 17 |
import { ensure_transport, transport_globals } from './cockpit/_internal/transport'; |
| 18 |
import { FsInfoClient } from "./cockpit/fsinfo"; |
| 19 |
import { fetch_info } from './cockpit/_internal/info'; |
| 20 |
|
| 21 |
function factory() { |
| 22 |
const cockpit = { }; |
| 23 |
event_mixin(cockpit, { }); |
| 24 |
|
| 25 |
cockpit.init = async () => { |
| 26 |
await new Promise(resolve => ensure_transport(resolve)); |
| 27 |
Object.assign(cockpit.info, await fetch_info()); |
| 28 |
}; |
| 29 |
|
| 30 |
cockpit.channel = function channel(options) { |
| 31 |
return new Channel(options); |
| 32 |
}; |
| 33 |
|
| 34 |
cockpit.event_target = function event_target(obj) { |
| 35 |
event_mixin(obj, { }); |
| 36 |
return obj; |
| 37 |
}; |
| 38 |
|
| 39 |
/* obsolete backwards compatible shim */ |
| 40 |
cockpit.extend = Object.assign; |
| 41 |
|
| 42 |
/* These can be filled in by loading ../manifests.js */ |
| 43 |
cockpit.manifests = { }; |
| 44 |
|
| 45 |
/* ------------------------------------------------------------ |
| 46 |
* Text Encoding |
| 47 |
*/ |
| 48 |
|
| 49 |
cockpit.base64_encode = base64_encode; |
| 50 |
cockpit.base64_decode = base64_decode; |
| 51 |
|
| 52 |
cockpit.kill = function kill(host, group) { |
| 53 |
const options = { }; |
| 54 |
if (host) |
| 55 |
options.host = host; |
| 56 |
if (group) |
| 57 |
options.group = group; |
| 58 |
cockpit.transport.control("kill", options); |
| 59 |
}; |
| 60 |
|
| 61 |
/* Not public API ... yet? */ |
| 62 |
cockpit.hint = function hint(name, options) { |
| 63 |
if (!transport_globals.default_transport) |
| 64 |
return; |
| 65 |
if (!options) |
| 66 |
options = transport_globals.default_host; |
| 67 |
if (typeof options == "string") |
| 68 |
options = { host: options }; |
| 69 |
options.hint = name; |
| 70 |
cockpit.transport.control("hint", options); |
| 71 |
}; |
| 72 |
|
| 73 |
cockpit.transport = { |
| 74 |
wait: ensure_transport, |
| 75 |
inject: function inject(message, out) { |
| 76 |
if (!transport_globals.default_transport) |
| 77 |
return false; |
| 78 |
if (out === undefined || out) |
| 79 |
return transport_globals.default_transport.send_data(message); |
| 80 |
else |
| 81 |
return transport_globals.default_transport.dispatch_data({ data: message }); |
| 82 |
}, |
| 83 |
filter: function filter(callback, out) { |
| 84 |
if (out) { |
| 85 |
console.error("'out' filters are no longer supported"); |
| 86 |
} else { |
| 87 |
transport_globals.incoming_filters.push(callback); |
| 88 |
} |
| 89 |
}, |
| 90 |
close: function close(problem) { |
| 91 |
if (transport_globals.default_transport) |
| 92 |
transport_globals.default_transport.close(problem ? { problem } : undefined); |
| 93 |
transport_globals.default_transport = null; |
| 94 |
this.options = { }; |
| 95 |
}, |
| 96 |
origin: transport_origin, |
| 97 |
options: { }, |
| 98 |
uri: calculate_url, |
| 99 |
control: function(command, options) { |
| 100 |
options = { ...options, command }; |
| 101 |
ensure_transport(function(transport) { |
| 102 |
transport.send_control(options); |
| 103 |
}); |
| 104 |
}, |
| 105 |
application: function () { |
| 106 |
if (!transport_globals.default_transport || window.mock) |
| 107 |
return calculate_application(); |
| 108 |
return transport_globals.default_transport.application; |
| 109 |
}, |
| 110 |
}; |
| 111 |
|
| 112 |
cockpit.resolve = function resolve(result) { |
| 113 |
console.warn("cockpit.resolve() is deprecated. Use Promise.resolve()"); |
| 114 |
return Promise.resolve(result); |
| 115 |
}; |
| 116 |
|
| 117 |
cockpit.reject = function reject(ex) { |
| 118 |
console.warn("cockpit.reject() is deprecated. Use Promise.reject()"); |
| 119 |
return Promise.reject(ex); |
| 120 |
}; |
| 121 |
|
| 122 |
cockpit.defer = function() { |
| 123 |
return new Deferred(); |
| 124 |
}; |
| 125 |
|
| 126 |
/* --------------------------------------------------------------------- |
| 127 |
* Utilities |
| 128 |
*/ |
| 129 |
|
| 130 |
const fmt_re = /\$\{([^}]+)\}|\$([a-zA-Z0-9_]+)/g; |
| 131 |
cockpit.format = function format(fmt, args) { |
| 132 |
if (arguments.length != 2 || !is_object(args) || args === null) |
| 133 |
args = Array.prototype.slice.call(arguments, 1); |
| 134 |
|
| 135 |
function replace(m, x, y) { |
| 136 |
const value = args[x || y]; |
| 137 |
|
| 138 |
/* Special-case 0 (also catches 0.0). All other falsy values return |
| 139 |
* the empty string. |
| 140 |
*/ |
| 141 |
if (value === 0) |
| 142 |
return '0'; |
| 143 |
|
| 144 |
return value || ''; |
| 145 |
} |
| 146 |
|
| 147 |
return fmt.replace(fmt_re, replace); |
| 148 |
}; |
| 149 |
|
| 150 |
cockpit.format_number = function format_number(number, precision) { |
| 151 |
/* We show given number of digits of precision (default 3), but avoid scientific notation. |
| 152 |
* We also show integers without digits after the comma. |
| 153 |
* |
| 154 |
* We want to localise the decimal separator, but we never want to |
| 155 |
* show thousands separators (to avoid ambiguity). For this |
| 156 |
* reason, for integers and large enough numbers, we use |
| 157 |
* non-localised conversions (and in both cases, show no |
| 158 |
* fractional part). |
| 159 |
*/ |
| 160 |
if (precision === undefined) |
| 161 |
precision = 3; |
| 162 |
const lang = cockpit.language === undefined ? undefined : cockpit.language.replace('_', '-'); |
| 163 |
const smallestValue = 10 ** (-precision); |
| 164 |
|
| 165 |
if (!number && number !== 0) |
| 166 |
return ""; |
| 167 |
else if (number % 1 === 0) |
| 168 |
return number.toString(); |
| 169 |
else if (number > 0 && number <= smallestValue) |
| 170 |
return smallestValue.toLocaleString(lang); |
| 171 |
else if (number < 0 && number >= -smallestValue) |
| 172 |
return (-smallestValue).toLocaleString(lang); |
| 173 |
else if (number > 999 || number < -999) |
| 174 |
return number.toFixed(0); |
| 175 |
else |
| 176 |
return number.toLocaleString(lang, { |
| 177 |
maximumSignificantDigits: precision, |
| 178 |
minimumSignificantDigits: precision, |
| 179 |
}); |
| 180 |
}; |
| 181 |
|
| 182 |
let deprecated_format_warned = false; |
| 183 |
function format_units(suffixes, number, second_arg, third_arg) { |
| 184 |
let options = second_arg; |
| 185 |
let factor = options?.base2 ? 1024 : 1000; |
| 186 |
|
| 187 |
// compat API: we used to accept 'factor' as a separate second arg |
| 188 |
if (third_arg || (second_arg && !is_object(second_arg))) { |
| 189 |
if (!deprecated_format_warned) { |
| 190 |
console.warn(`cockpit.format_{bytes,bits}[_per_sec](..., ${second_arg}, ${third_arg}) is deprecated.`); |
| 191 |
deprecated_format_warned = true; |
| 192 |
} |
| 193 |
|
| 194 |
factor = second_arg || 1000; |
| 195 |
options = third_arg; |
| 196 |
// double backwards compat: "options" argument position used to be a boolean flag "separate" |
| 197 |
if (!is_object(options)) |
| 198 |
options = { separate: options }; |
| 199 |
} |
| 200 |
|
| 201 |
let suffix = null; |
| 202 |
|
| 203 |
/* Find that factor string */ |
| 204 |
if (!number && number !== 0) { |
| 205 |
suffix = null; |
| 206 |
} else if (typeof (factor) === "string") { |
| 207 |
/* Prefer larger factors */ |
| 208 |
const keys = []; |
| 209 |
for (const key in suffixes) |
| 210 |
keys.push(key); |
| 211 |
keys.sort().reverse(); |
| 212 |
for (let y = 0; y < keys.length; y++) { |
| 213 |
for (let x = 0; x < suffixes[keys[y]].length; x++) { |
| 214 |
if (factor == suffixes[keys[y]][x]) { |
| 215 |
number = number / Math.pow(keys[y], x); |
| 216 |
suffix = factor; |
| 217 |
break; |
| 218 |
} |
| 219 |
} |
| 220 |
if (suffix) |
| 221 |
break; |
| 222 |
} |
| 223 |
|
| 224 |
/* @factor is a number */ |
| 225 |
} else if (factor in suffixes) { |
| 226 |
let divisor = 1; |
| 227 |
for (let i = 0; i < suffixes[factor].length; i++) { |
| 228 |
const quotient = number / divisor; |
| 229 |
if (quotient < factor) { |
| 230 |
number = quotient; |
| 231 |
suffix = suffixes[factor][i]; |
| 232 |
break; |
| 233 |
} |
| 234 |
divisor *= factor; |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
const string_representation = cockpit.format_number(number, options?.precision); |
| 239 |
let ret; |
| 240 |
|
| 241 |
if (string_representation && suffix) |
| 242 |
ret = [string_representation, suffix]; |
| 243 |
else |
| 244 |
ret = [string_representation]; |
| 245 |
|
| 246 |
if (!options?.separate) |
| 247 |
ret = ret.join(" "); |
| 248 |
|
| 249 |
return ret; |
| 250 |
} |
| 251 |
|
| 252 |
const byte_suffixes = { |
| 253 |
1000: ["B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB"], |
| 254 |
1024: ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB"] |
| 255 |
}; |
| 256 |
|
| 257 |
cockpit.format_bytes = function format_bytes(number, ...args) { |
| 258 |
return format_units(byte_suffixes, number, ...args); |
| 259 |
}; |
| 260 |
|
| 261 |
const byte_sec_suffixes = { |
| 262 |
1000: ["B/s", "kB/s", "MB/s", "GB/s", "TB/s", "PB/s", "EB/s", "ZB/s"], |
| 263 |
1024: ["B/s", "KiB/s", "MiB/s", "GiB/s", "TiB/s", "PiB/s", "EiB/s", "ZiB/s"] |
| 264 |
}; |
| 265 |
|
| 266 |
cockpit.format_bytes_per_sec = function format_bytes_per_sec(number, ...args) { |
| 267 |
return format_units(byte_sec_suffixes, number, ...args); |
| 268 |
}; |
| 269 |
|
| 270 |
// https://en.wikipedia.org/wiki/Data-rate_units#Decimal_multiples_of_bits |
| 271 |
const bit_suffixes = { |
| 272 |
1000: ["bps", "kbps", "Mbps", "Gbps", "Tbps", "Pbps", "Ebps", "Zbps"] |
| 273 |
}; |
| 274 |
|
| 275 |
cockpit.format_bits_per_sec = function format_bits_per_sec(number, ...args) { |
| 276 |
return format_units(bit_suffixes, number, ...args); |
| 277 |
}; |
| 278 |
|
| 279 |
/* --------------------------------------------------------------------- |
| 280 |
* Storage Helper. |
| 281 |
* |
| 282 |
* Use application to prefix data stored in browser storage |
| 283 |
* with helpers for compatibility. |
| 284 |
*/ |
| 285 |
function StorageHelper(storageName) { |
| 286 |
const self = this; |
| 287 |
let storage; |
| 288 |
|
| 289 |
try { |
| 290 |
storage = window[storageName]; |
| 291 |
} catch (e) { } |
| 292 |
|
| 293 |
self.prefixedKey = function (key) { |
| 294 |
return cockpit.transport.application() + ":" + key; |
| 295 |
}; |
| 296 |
|
| 297 |
self.getItem = function (key, both) { |
| 298 |
let value = storage.getItem(self.prefixedKey(key)); |
| 299 |
if (!value && both) |
| 300 |
value = storage.getItem(key); |
| 301 |
return value; |
| 302 |
}; |
| 303 |
|
| 304 |
self.setItem = function (key, value, both) { |
| 305 |
storage.setItem(self.prefixedKey(key), value); |
| 306 |
if (both) |
| 307 |
storage.setItem(key, value); |
| 308 |
}; |
| 309 |
|
| 310 |
self.removeItem = function(key, both) { |
| 311 |
storage.removeItem(self.prefixedKey(key)); |
| 312 |
if (both) |
| 313 |
storage.removeItem(key); |
| 314 |
}; |
| 315 |
|
| 316 |
/* Instead of clearing, purge anything that isn't prefixed with an application |
| 317 |
* and anything prefixed with our application. |
| 318 |
*/ |
| 319 |
self.clear = function(full) { |
| 320 |
let i = 0; |
| 321 |
while (i < storage.length) { |
| 322 |
const k = storage.key(i); |
| 323 |
if (full && k.indexOf("cockpit") !== 0) |
| 324 |
storage.removeItem(k); |
| 325 |
else if (k.indexOf(cockpit.transport.application()) === 0) |
| 326 |
storage.removeItem(k); |
| 327 |
else |
| 328 |
i++; |
| 329 |
} |
| 330 |
}; |
| 331 |
} |
| 332 |
|
| 333 |
cockpit.localStorage = new StorageHelper("localStorage"); |
| 334 |
cockpit.sessionStorage = new StorageHelper("sessionStorage"); |
| 335 |
|
| 336 |
/* --------------------------------------------------------------------- |
| 337 |
* Shared data cache. |
| 338 |
* |
| 339 |
* We cannot use sessionStorage when keeping lots of data in memory and |
| 340 |
* sharing it between frames. It has a rather paltry limit on the amount |
| 341 |
* of data it can hold ... so we use window properties instead. |
| 342 |
*/ |
| 343 |
|
| 344 |
function lookup_storage(win) { |
| 345 |
let storage; |
| 346 |
if (win.parent && win.parent !== win) |
| 347 |
storage = lookup_storage(win.parent); |
| 348 |
if (!storage) { |
| 349 |
try { |
| 350 |
storage = win["cv1-storage"]; |
| 351 |
if (!storage) |
| 352 |
win["cv1-storage"] = storage = { }; |
| 353 |
} catch (ex) { } |
| 354 |
} |
| 355 |
return storage; |
| 356 |
} |
| 357 |
|
| 358 |
function StorageCache(org_key, provider, consumer) { |
| 359 |
const self = this; |
| 360 |
const key = cockpit.transport.application() + ":" + org_key; |
| 361 |
|
| 362 |
/* For triggering events and ownership */ |
| 363 |
const trigger = window.sessionStorage; |
| 364 |
let last; |
| 365 |
|
| 366 |
const storage = lookup_storage(window); |
| 367 |
|
| 368 |
let claimed = false; |
| 369 |
let source; |
| 370 |
|
| 371 |
function callback() { |
| 372 |
/* Only run the callback if we have a result */ |
| 373 |
if (storage[key] !== undefined) { |
| 374 |
const value = storage[key]; |
| 375 |
window.setTimeout(function() { |
| 376 |
if (consumer(value, org_key) === false) |
| 377 |
self.close(); |
| 378 |
}); |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
function result(value) { |
| 383 |
if (source && !claimed) |
| 384 |
claimed = true; |
| 385 |
if (!claimed) |
| 386 |
return; |
| 387 |
|
| 388 |
// use a random number to avoid races by separate instances |
| 389 |
const version = Math.floor(Math.random() * 10000000) + 1; |
| 390 |
|
| 391 |
/* Event for the local window */ |
| 392 |
const ev = document.createEvent("StorageEvent"); |
| 393 |
ev.initStorageEvent("storage", false, false, key, null, |
| 394 |
version, window.location, trigger); |
| 395 |
|
| 396 |
storage[key] = value; |
| 397 |
trigger.setItem(key, version); |
| 398 |
ev.self = self; |
| 399 |
window.dispatchEvent(ev); |
| 400 |
} |
| 401 |
|
| 402 |
self.claim = function claim() { |
| 403 |
if (source) |
| 404 |
return; |
| 405 |
|
| 406 |
/* In case we're unclaimed during the callback */ |
| 407 |
const claiming = { close: function() { } }; |
| 408 |
source = claiming; |
| 409 |
|
| 410 |
const changed = provider(result, org_key); |
| 411 |
if (source === claiming) |
| 412 |
source = changed; |
| 413 |
else |
| 414 |
changed.close(); |
| 415 |
}; |
| 416 |
|
| 417 |
function unclaim() { |
| 418 |
if (source?.close) |
| 419 |
source.close(); |
| 420 |
source = null; |
| 421 |
|
| 422 |
if (!claimed) |
| 423 |
return; |
| 424 |
|
| 425 |
claimed = false; |
| 426 |
|
| 427 |
let current_value = trigger.getItem(key); |
| 428 |
if (current_value) |
| 429 |
current_value = parseInt(current_value, 10); |
| 430 |
else |
| 431 |
current_value = null; |
| 432 |
|
| 433 |
if (last && last === current_value) { |
| 434 |
const ev = document.createEvent("StorageEvent"); |
| 435 |
const version = trigger[key]; |
| 436 |
ev.initStorageEvent("storage", false, false, key, version, |
| 437 |
null, window.location, trigger); |
| 438 |
delete storage[key]; |
| 439 |
trigger.removeItem(key); |
| 440 |
ev.self = self; |
| 441 |
window.dispatchEvent(ev); |
| 442 |
} |
| 443 |
} |
| 444 |
|
| 445 |
function changed(event) { |
| 446 |
if (event.key !== key) |
| 447 |
return; |
| 448 |
|
| 449 |
/* check where the event came from |
| 450 |
- it came from someone else: |
| 451 |
if it notifies their unclaim (new value null) and we haven't already claimed, do so |
| 452 |
- it came from ourselves: |
| 453 |
if the new value doesn't match the actual value in the cache, and |
| 454 |
we tried to claim (from null to a number), cancel our claim |
| 455 |
*/ |
| 456 |
if (event.self !== self) { |
| 457 |
if (!event.newValue && !claimed) { |
| 458 |
self.claim(); |
| 459 |
return; |
| 460 |
} |
| 461 |
} else if (claimed && !event.oldValue && (event.newValue !== trigger.getItem(key))) { |
| 462 |
unclaim(); |
| 463 |
} |
| 464 |
|
| 465 |
let new_value = null; |
| 466 |
if (event.newValue) |
| 467 |
new_value = parseInt(event.newValue, 10); |
| 468 |
if (last !== new_value) { |
| 469 |
last = new_value; |
| 470 |
callback(); |
| 471 |
} |
| 472 |
} |
| 473 |
|
| 474 |
self.close = function() { |
| 475 |
window.removeEventListener("storage", changed, true); |
| 476 |
unclaim(); |
| 477 |
}; |
| 478 |
|
| 479 |
window.addEventListener("storage", changed, true); |
| 480 |
|
| 481 |
/* Always clear this data on unload */ |
| 482 |
window.addEventListener("beforeunload", function() { |
| 483 |
self.close(); |
| 484 |
}); |
| 485 |
window.addEventListener("unload", function() { |
| 486 |
self.close(); |
| 487 |
}); |
| 488 |
|
| 489 |
if (trigger.getItem(key)) |
| 490 |
callback(); |
| 491 |
else |
| 492 |
self.claim(); |
| 493 |
} |
| 494 |
|
| 495 |
cockpit.cache = function cache(key, provider, consumer) { |
| 496 |
return new StorageCache(key, provider, consumer); |
| 497 |
}; |
| 498 |
|
| 499 |
/* --------------------------------------------------------------------- |
| 500 |
* Metrics |
| 501 |
* |
| 502 |
* Implements the cockpit.series and cockpit.grid. Part of the metrics |
| 503 |
* implementations that do not require jquery. |
| 504 |
*/ |
| 505 |
|
| 506 |
function SeriesSink(interval, identifier, fetch_callback) { |
| 507 |
const self = this; |
| 508 |
|
| 509 |
self.interval = interval; |
| 510 |
self.limit = identifier ? 64 * 1024 : 1024; |
| 511 |
|
| 512 |
/* |
| 513 |
* The cache sits on a window, either our own or a parent |
| 514 |
* window whichever we can access properly. |
| 515 |
* |
| 516 |
* Entries in the index are: |
| 517 |
* |
| 518 |
* { beg: N, items: [], mapping: { }, next: item } |
| 519 |
*/ |
| 520 |
const index = setup_index(identifier); |
| 521 |
|
| 522 |
/* |
| 523 |
* A linked list through the index, that we use for expiry |
| 524 |
* of the cache. |
| 525 |
*/ |
| 526 |
let count = 0; |
| 527 |
let head = null; |
| 528 |
let tail = null; |
| 529 |
|
| 530 |
function setup_index(id) { |
| 531 |
if (!id) |
| 532 |
return []; |
| 533 |
|
| 534 |
/* Try and find a good place to cache data */ |
| 535 |
const storage = lookup_storage(window); |
| 536 |
|
| 537 |
let index = storage[id]; |
| 538 |
if (!index) |
| 539 |
storage[id] = index = []; |
| 540 |
return index; |
| 541 |
} |
| 542 |
|
| 543 |
function search(idx, beg) { |
| 544 |
let low = 0; |
| 545 |
let high = idx.length - 1; |
| 546 |
|
| 547 |
while (low <= high) { |
| 548 |
const mid = (low + high) / 2 | 0; |
| 549 |
const val = idx[mid].beg; |
| 550 |
if (val < beg) |
| 551 |
low = mid + 1; |
| 552 |
else if (val > beg) |
| 553 |
high = mid - 1; |
| 554 |
else |
| 555 |
return mid; /* key found */ |
| 556 |
} |
| 557 |
return low; |
| 558 |
} |
| 559 |
|
| 560 |
function fetch(beg, end, for_walking) { |
| 561 |
if (fetch_callback) { |
| 562 |
if (!for_walking) { |
| 563 |
/* Stash some fake data synchronously so that we don't ask |
| 564 |
* again for the same range while they are still fetching |
| 565 |
* it asynchronously. |
| 566 |
*/ |
| 567 |
stash(beg, new Array(end - beg), { }); |
| 568 |
} |
| 569 |
fetch_callback(beg, end, for_walking); |
| 570 |
} |
| 571 |
} |
| 572 |
|
| 573 |
self.load = function load(beg, end, for_walking) { |
| 574 |
if (end <= beg) |
| 575 |
return; |
| 576 |
|
| 577 |
const at = search(index, beg); |
| 578 |
|
| 579 |
const len = index.length; |
| 580 |
let last = beg; |
| 581 |
|
| 582 |
/* We do this in two phases: First, we walk the index to |
| 583 |
* process what we already have and at the same time make |
| 584 |
* notes about what we need to fetch. Then we go over the |
| 585 |
* notes and actually fetch what we need. That way, the |
| 586 |
* fetch callbacks in the second phase can modify the |
| 587 |
* index data structure without disturbing the walk in the |
| 588 |
* first phase. |
| 589 |
*/ |
| 590 |
|
| 591 |
const fetches = []; |
| 592 |
|
| 593 |
/* Data relevant to this range can be at the found index, or earlier */ |
| 594 |
for (let i = at > 0 ? at - 1 : at; i < len; i++) { |
| 595 |
const entry = index[i]; |
| 596 |
const en = entry.items.length; |
| 597 |
if (!en) |
| 598 |
continue; |
| 599 |
|
| 600 |
const eb = entry.beg; |
| 601 |
const b = Math.max(eb, beg); |
| 602 |
const e = Math.min(eb + en, end); |
| 603 |
|
| 604 |
if (b < e) { |
| 605 |
if (b > last) |
| 606 |
fetches.push([last, b]); |
| 607 |
process(b, entry.items.slice(b - eb, e - eb), entry.mapping); |
| 608 |
last = e; |
| 609 |
} else if (i >= at) { |
| 610 |
break; /* no further intersections */ |
| 611 |
} |
| 612 |
} |
| 613 |
|
| 614 |
for (let i = 0; i < fetches.length; i++) |
| 615 |
fetch(fetches[i][0], fetches[i][1], for_walking); |
| 616 |
|
| 617 |
if (last != end) |
| 618 |
fetch(last, end, for_walking); |
| 619 |
}; |
| 620 |
|
| 621 |
function stash(beg, items, mapping) { |
| 622 |
if (!items.length) |
| 623 |
return; |
| 624 |
|
| 625 |
let at = search(index, beg); |
| 626 |
|
| 627 |
const end = beg + items.length; |
| 628 |
|
| 629 |
const len = index.length; |
| 630 |
let i; |
| 631 |
for (i = at > 0 ? at - 1 : at; i < len; i++) { |
| 632 |
const entry = index[i]; |
| 633 |
const en = entry.items.length; |
| 634 |
if (!en) |
| 635 |
continue; |
| 636 |
|
| 637 |
const eb = entry.beg; |
| 638 |
const b = Math.max(eb, beg); |
| 639 |
const e = Math.min(eb + en, end); |
| 640 |
|
| 641 |
/* |
| 642 |
* We truncate blocks that intersect with this one |
| 643 |
* |
| 644 |
* We could adjust them, but in general the loaders are |
| 645 |
* intelligent enough to only load the required data, so |
| 646 |
* not doing this optimization yet. |
| 647 |
*/ |
| 648 |
|
| 649 |
if (b < e) { |
| 650 |
const num = e - b; |
| 651 |
entry.items.splice(b - eb, num); |
| 652 |
count -= num; |
| 653 |
if (b - eb === 0) |
| 654 |
entry.beg += (e - eb); |
| 655 |
} else if (i >= at) { |
| 656 |
break; /* no further intersections */ |
| 657 |
} |
| 658 |
} |
| 659 |
|
| 660 |
/* Insert our item into the array */ |
| 661 |
const entry = { beg, items, mapping }; |
| 662 |
if (!head) |
| 663 |
head = entry; |
| 664 |
if (tail) |
| 665 |
tail.next = entry; |
| 666 |
tail = entry; |
| 667 |
count += items.length; |
| 668 |
index.splice(at, 0, entry); |
| 669 |
|
| 670 |
/* Remove any items with zero length around insertion point */ |
| 671 |
for (at--; at <= i; at++) { |
| 672 |
const entry = index[at]; |
| 673 |
if (entry && !entry.items.length) { |
| 674 |
index.splice(at, 1); |
| 675 |
at--; |
| 676 |
} |
| 677 |
} |
| 678 |
|
| 679 |
/* If our index has gotten too big, expire entries */ |
| 680 |
while (head && count > self.limit) { |
| 681 |
count -= head.items.length; |
| 682 |
head.items = []; |
| 683 |
head.mapping = null; |
| 684 |
head = head.next || null; |
| 685 |
} |
| 686 |
|
| 687 |
/* Remove any entries with zero length at beginning */ |
| 688 |
const newlen = index.length; |
| 689 |
for (i = 0; i < newlen; i++) { |
| 690 |
if (index[i].items.length > 0) |
| 691 |
break; |
| 692 |
} |
| 693 |
index.splice(0, i); |
| 694 |
} |
| 695 |
|
| 696 |
/* |
| 697 |
* Used to populate grids, the keys are grid ids and |
| 698 |
* the values are objects: { grid, rows, notify } |
| 699 |
* |
| 700 |
* The rows field is an object indexed by paths |
| 701 |
* container aliases, and the values are: [ row, path ] |
| 702 |
*/ |
| 703 |
const registered = { }; |
| 704 |
|
| 705 |
/* An undocumented function called by DataGrid */ |
| 706 |
self._register = function _register(grid, id) { |
| 707 |
if (grid.interval != interval) |
| 708 |
throw Error("mismatched metric interval between grid and sink"); |
| 709 |
let gdata = registered[id]; |
| 710 |
if (!gdata) { |
| 711 |
gdata = registered[id] = { grid, links: [] }; |
| 712 |
gdata.links.remove = function remove() { |
| 713 |
delete registered[id]; |
| 714 |
}; |
| 715 |
} |
| 716 |
return gdata.links; |
| 717 |
}; |
| 718 |
|
| 719 |
function process(beg, items, mapping) { |
| 720 |
const end = beg + items.length; |
| 721 |
|
| 722 |
for (const id in registered) { |
| 723 |
const gdata = registered[id]; |
| 724 |
const grid = gdata.grid; |
| 725 |
|
| 726 |
const b = Math.max(beg, grid.beg); |
| 727 |
const e = Math.min(end, grid.end); |
| 728 |
|
| 729 |
/* Does this grid overlap the bounds of item? */ |
| 730 |
if (b < e) { |
| 731 |
/* Where in the items to take from */ |
| 732 |
const f = b - beg; |
| 733 |
|
| 734 |
/* Where and how many to place */ |
| 735 |
const t = b - grid.beg; |
| 736 |
|
| 737 |
/* How many to process */ |
| 738 |
const n = e - b; |
| 739 |
|
| 740 |
for (let i = 0; i < n; i++) { |
| 741 |
const klen = gdata.links.length; |
| 742 |
for (let k = 0; k < klen; k++) { |
| 743 |
const path = gdata.links[k][0]; |
| 744 |
const row = gdata.links[k][1]; |
| 745 |
|
| 746 |
/* Calculate the data field to fill in */ |
| 747 |
let data = items[f + i]; |
| 748 |
let map = mapping; |
| 749 |
const jlen = path.length; |
| 750 |
for (let j = 0; data !== undefined && j < jlen; j++) { |
| 751 |
if (!data) { |
| 752 |
data = undefined; |
| 753 |
} else if (map !== undefined && map !== null) { |
| 754 |
map = map[path[j]]; |
| 755 |
if (map) |
| 756 |
data = data[map[""]]; |
| 757 |
else |
| 758 |
data = data[path[j]]; |
| 759 |
} else { |
| 760 |
data = data[path[j]]; |
| 761 |
} |
| 762 |
} |
| 763 |
|
| 764 |
row[t + i] = data; |
| 765 |
} |
| 766 |
} |
| 767 |
|
| 768 |
/* Notify the grid, so it can call any functions */ |
| 769 |
grid.notify(t, n); |
| 770 |
} |
| 771 |
} |
| 772 |
} |
| 773 |
|
| 774 |
self.input = function input(beg, items, mapping) { |
| 775 |
process(beg, items, mapping); |
| 776 |
stash(beg, items, mapping); |
| 777 |
}; |
| 778 |
|
| 779 |
self.close = function () { |
| 780 |
for (const id in registered) { |
| 781 |
const grid = registered[id]; |
| 782 |
if (grid?.grid) |
| 783 |
grid.grid.remove_sink(self); |
| 784 |
} |
| 785 |
}; |
| 786 |
} |
| 787 |
|
| 788 |
cockpit.series = function series(interval, cache, fetch) { |
| 789 |
return new SeriesSink(interval, cache, fetch); |
| 790 |
}; |
| 791 |
|
| 792 |
let unique = 1; |
| 793 |
|
| 794 |
function SeriesGrid(interval, beg, end) { |
| 795 |
const self = this; |
| 796 |
|
| 797 |
/* We can trigger events */ |
| 798 |
event_mixin(self, { }); |
| 799 |
|
| 800 |
const rows = []; |
| 801 |
|
| 802 |
self.interval = interval; |
| 803 |
self.beg = 0; |
| 804 |
self.end = 0; |
| 805 |
|
| 806 |
/* |
| 807 |
* Used to populate table data, the values are: |
| 808 |
* [ callback, row ] |
| 809 |
*/ |
| 810 |
const callbacks = []; |
| 811 |
|
| 812 |
const sinks = []; |
| 813 |
|
| 814 |
let suppress = 0; |
| 815 |
|
| 816 |
const id = "g1-" + unique; |
| 817 |
unique += 1; |
| 818 |
|
| 819 |
/* Used while walking */ |
| 820 |
let walking = null; |
| 821 |
let offset = null; |
| 822 |
|
| 823 |
self.notify = function notify(x, n) { |
| 824 |
if (suppress) |
| 825 |
return; |
| 826 |
if (x + n > self.end - self.beg) |
| 827 |
n = (self.end - self.beg) - x; |
| 828 |
if (n <= 0) |
| 829 |
return; |
| 830 |
const jlen = callbacks.length; |
| 831 |
for (let j = 0; j < jlen; j++) { |
| 832 |
const callback = callbacks[j][0]; |
| 833 |
const row = callbacks[j][1]; |
| 834 |
callback.call(self, row, x, n); |
| 835 |
} |
| 836 |
|
| 837 |
self.dispatchEvent("notify", x, n); |
| 838 |
}; |
| 839 |
|
| 840 |
self.add = function add(/* sink, path */) { |
| 841 |
const row = []; |
| 842 |
rows.push(row); |
| 843 |
|
| 844 |
/* Called as add(sink, path) */ |
| 845 |
if (is_object(arguments[0])) { |
| 846 |
const sink = arguments[0].series || arguments[0]; |
| 847 |
|
| 848 |
/* The path argument can be an array, or a dot separated string */ |
| 849 |
let path = arguments[1]; |
| 850 |
if (!path) |
| 851 |
path = []; |
| 852 |
else if (typeof (path) === "string") |
| 853 |
path = path.split("."); |
| 854 |
|
| 855 |
const links = sink._register(self, id); |
| 856 |
if (!links.length) |
| 857 |
sinks.push({ sink, links }); |
| 858 |
links.push([path, row]); |
| 859 |
|
| 860 |
/* Called as add(callback) */ |
| 861 |
} else if (is_function(arguments[0])) { |
| 862 |
const cb = [arguments[0], row]; |
| 863 |
if (arguments[1] === true) |
| 864 |
callbacks.unshift(cb); |
| 865 |
else |
| 866 |
callbacks.push(cb); |
| 867 |
|
| 868 |
/* Not called as add() */ |
| 869 |
} else if (arguments.length !== 0) { |
| 870 |
throw Error("invalid args to grid.add()"); |
| 871 |
} |
| 872 |
|
| 873 |
return row; |
| 874 |
}; |
| 875 |
|
| 876 |
self.remove = function remove(row) { |
| 877 |
/* Remove from the sinks */ |
| 878 |
let ilen = sinks.length; |
| 879 |
for (let i = 0; i < ilen; i++) { |
| 880 |
const jlen = sinks[i].links.length; |
| 881 |
for (let j = 0; j < jlen; j++) { |
| 882 |
if (sinks[i].links[j][1] === row) { |
| 883 |
sinks[i].links.splice(j, 1); |
| 884 |
break; |
| 885 |
} |
| 886 |
} |
| 887 |
} |
| 888 |
|
| 889 |
/* Remove from our list of rows */ |
| 890 |
ilen = rows.length; |
| 891 |
for (let i = 0; i < ilen; i++) { |
| 892 |
if (rows[i] === row) { |
| 893 |
rows.splice(i, 1); |
| 894 |
break; |
| 895 |
} |
| 896 |
} |
| 897 |
}; |
| 898 |
|
| 899 |
self.remove_sink = function remove_sink(sink) { |
| 900 |
const len = sinks.length; |
| 901 |
for (let i = 0; i < len; i++) { |
| 902 |
if (sinks[i].sink === sink) { |
| 903 |
sinks[i].links.remove(); |
| 904 |
sinks.splice(i, 1); |
| 905 |
break; |
| 906 |
} |
| 907 |
} |
| 908 |
}; |
| 909 |
|
| 910 |
self.sync = function sync(for_walking) { |
| 911 |
/* Suppress notifications */ |
| 912 |
suppress++; |
| 913 |
|
| 914 |
/* Ask all sinks to load data */ |
| 915 |
const len = sinks.length; |
| 916 |
for (let i = 0; i < len; i++) { |
| 917 |
const sink = sinks[i].sink; |
| 918 |
sink.load(self.beg, self.end, for_walking); |
| 919 |
} |
| 920 |
|
| 921 |
suppress--; |
| 922 |
|
| 923 |
/* Notify for all rows */ |
| 924 |
self.notify(0, self.end - self.beg); |
| 925 |
}; |
| 926 |
|
| 927 |
function move_internal(beg, end, for_walking) { |
| 928 |
if (end === undefined) |
| 929 |
end = beg + (self.end - self.beg); |
| 930 |
|
| 931 |
if (end < beg) |
| 932 |
beg = end; |
| 933 |
|
| 934 |
self.beg = beg; |
| 935 |
self.end = end; |
| 936 |
|
| 937 |
if (!rows.length) |
| 938 |
return; |
| 939 |
|
| 940 |
rows.forEach(function(row) { |
| 941 |
row.length = 0; |
| 942 |
}); |
| 943 |
|
| 944 |
self.sync(for_walking); |
| 945 |
} |
| 946 |
|
| 947 |
function stop_walking() { |
| 948 |
window.clearInterval(walking); |
| 949 |
walking = null; |
| 950 |
offset = null; |
| 951 |
} |
| 952 |
|
| 953 |
function is_negative(n) { |
| 954 |
return ((n = +n) || 1 / n) < 0; |
| 955 |
} |
| 956 |
|
| 957 |
self.move = function move(beg, end) { |
| 958 |
stop_walking(); |
| 959 |
/* Some code paths use now twice. |
| 960 |
* They should use the same value. |
| 961 |
*/ |
| 962 |
let now = null; |
| 963 |
|
| 964 |
/* Treat negative numbers relative to now */ |
| 965 |
if (beg === undefined) { |
| 966 |
beg = 0; |
| 967 |
} else if (is_negative(beg)) { |
| 968 |
now = Date.now(); |
| 969 |
beg = Math.floor(now / self.interval) + beg; |
| 970 |
} |
| 971 |
if (end !== undefined && is_negative(end)) { |
| 972 |
if (now === null) |
| 973 |
now = Date.now(); |
| 974 |
end = Math.floor(now / self.interval) + end; |
| 975 |
} |
| 976 |
|
| 977 |
move_internal(beg, end, false); |
| 978 |
}; |
| 979 |
|
| 980 |
self.walk = function walk() { |
| 981 |
/* Don't overflow 32 signed bits with the interval since |
| 982 |
* many browsers will mishandle it. This means that plots |
| 983 |
* that would make about one step every month don't walk |
| 984 |
* at all, but I guess that is ok. |
| 985 |
* |
| 986 |
* For example, |
| 987 |
* https://developer.mozilla.org/en-US/docs/Web/API/setTimeout |
| 988 |
* says: |
| 989 |
* |
| 990 |
* Browsers including Internet Explorer, Chrome, |
| 991 |
* Safari, and Firefox store the delay as a 32-bit |
| 992 |
* signed Integer internally. This causes an Integer |
| 993 |
* overflow when using delays larger than 2147483647, |
| 994 |
* resulting in the timeout being executed immediately. |
| 995 |
*/ |
| 996 |
|
| 997 |
const start = Date.now(); |
| 998 |
if (self.interval > 2000000000) |
| 999 |
return; |
| 1000 |
|
| 1001 |
stop_walking(); |
| 1002 |
offset = start - self.beg * self.interval; |
| 1003 |
walking = window.setInterval(function() { |
| 1004 |
const now = Date.now(); |
| 1005 |
move_internal(Math.floor((now - offset) / self.interval), undefined, true); |
| 1006 |
}, self.interval); |
| 1007 |
}; |
| 1008 |
|
| 1009 |
self.close = function close() { |
| 1010 |
stop_walking(); |
| 1011 |
while (sinks.length) |
| 1012 |
(sinks.pop()).links.remove(); |
| 1013 |
}; |
| 1014 |
|
| 1015 |
self.move(beg, end); |
| 1016 |
} |
| 1017 |
|
| 1018 |
cockpit.grid = function grid(interval, beg, end) { |
| 1019 |
return new SeriesGrid(interval, beg, end); |
| 1020 |
}; |
| 1021 |
|
| 1022 |
/* -------------------------------------------------------------------- |
| 1023 |
* Basic utilities. |
| 1024 |
*/ |
| 1025 |
|
| 1026 |
function BasicError(problem, message) { |
| 1027 |
this.problem = problem; |
| 1028 |
this.message = message || cockpit.message(problem); |
| 1029 |
this.toString = function() { |
| 1030 |
return this.message; |
| 1031 |
}; |
| 1032 |
} |
| 1033 |
|
| 1034 |
cockpit.logout = function logout(reload, reason) { |
| 1035 |
/* fully clear session storage */ |
| 1036 |
cockpit.sessionStorage.clear(true); |
| 1037 |
|
| 1038 |
/* Only clean application data from localStorage, |
| 1039 |
* except for login-data. Clear that completely */ |
| 1040 |
cockpit.localStorage.removeItem('login-data', true); |
| 1041 |
cockpit.localStorage.clear(false); |
| 1042 |
|
| 1043 |
if (reload !== false) |
| 1044 |
transport_globals.reload_after_disconnect = true; |
| 1045 |
ensure_transport(function(transport) { |
| 1046 |
if (!transport.send_control({ command: "logout", disconnect: true })) |
| 1047 |
window.location.reload(transport_globals.reload_after_disconnect); |
| 1048 |
}); |
| 1049 |
window.sessionStorage.setItem("logout-intent", "explicit"); |
| 1050 |
if (reason) |
| 1051 |
window.sessionStorage.setItem("logout-reason", reason); |
| 1052 |
}; |
| 1053 |
|
| 1054 |
/* Not public API ... yet? */ |
| 1055 |
cockpit.drop_privileges = function drop_privileges() { |
| 1056 |
console.warn("cockpit.drop_privileges() is deprecated"); |
| 1057 |
ensure_transport(function(transport) { |
| 1058 |
transport.send_control({ command: "logout", disconnect: false }); |
| 1059 |
}); |
| 1060 |
}; |
| 1061 |
|
| 1062 |
/* --------------------------------------------------------------------- |
| 1063 |
* User and system information |
| 1064 |
*/ |
| 1065 |
|
| 1066 |
cockpit.info = { }; |
| 1067 |
event_mixin(cockpit.info, { }); |
| 1068 |
|
| 1069 |
transport_globals.init_callback = function(options) { |
| 1070 |
if (options.system) { |
| 1071 |
cockpit.info.ws = options.system; |
| 1072 |
Object.assign(cockpit.info, options.system); |
| 1073 |
} |
| 1074 |
if (options.system) |
| 1075 |
cockpit.info.dispatchEvent("changed"); |
| 1076 |
|
| 1077 |
cockpit.transport.options = options; |
| 1078 |
cockpit.transport.csrf_token = options["csrf-token"]; |
| 1079 |
cockpit.transport.host = transport_globals.default_host; |
| 1080 |
}; |
| 1081 |
|
| 1082 |
let the_user = null; |
| 1083 |
cockpit.user = function () { |
| 1084 |
if (!the_user) { |
| 1085 |
const dbus = cockpit.dbus(null, { bus: "internal" }); |
| 1086 |
return dbus.call("/user", "org.freedesktop.DBus.Properties", "GetAll", |
| 1087 |
["cockpit.User"], { type: "s" }) |
| 1088 |
.then(([user]) => { |
| 1089 |
the_user = { |
| 1090 |
id: user.Id.v, |
| 1091 |
gid: user.Gid?.v, |
| 1092 |
name: user.Name.v, |
| 1093 |
full_name: user.Full.v, |
| 1094 |
groups: user.Groups.v, |
| 1095 |
home: user.Home.v, |
| 1096 |
shell: user.Shell.v |
| 1097 |
}; |
| 1098 |
Object.freeze(the_user); |
| 1099 |
return the_user; |
| 1100 |
}) |
| 1101 |
.finally(() => dbus.close()); |
| 1102 |
} else { |
| 1103 |
return Promise.resolve(the_user); |
| 1104 |
} |
| 1105 |
}; |
| 1106 |
|
| 1107 |
/* ------------------------------------------------------------------------ |
| 1108 |
* Override for broken browser behavior |
| 1109 |
*/ |
| 1110 |
|
| 1111 |
document.addEventListener("click", function(ev) { |
| 1112 |
if (ev.target.classList && in_array(ev.target.classList, 'disabled')) |
| 1113 |
ev.stopPropagation(); |
| 1114 |
}, true); |
| 1115 |
|
| 1116 |
/* ------------------------------------------------------------------------ |
| 1117 |
* Cockpit location |
| 1118 |
*/ |
| 1119 |
|
| 1120 |
let last_loc = null; |
| 1121 |
|
| 1122 |
Object.defineProperty(cockpit, "location", { |
| 1123 |
enumerable: true, |
| 1124 |
get: function() { |
| 1125 |
if (!last_loc || last_loc.href !== window.location.hash.slice(1)) |
| 1126 |
last_loc = new Location(); |
| 1127 |
return last_loc; |
| 1128 |
}, |
| 1129 |
set: function(v) { |
| 1130 |
cockpit.location.go(v); |
| 1131 |
} |
| 1132 |
}); |
| 1133 |
|
| 1134 |
window.addEventListener("hashchange", function() { |
| 1135 |
if (last_loc) |
| 1136 |
last_loc.invalidate(); |
| 1137 |
last_loc = null; |
| 1138 |
const hash = window.location.hash.slice(1); |
| 1139 |
cockpit.hint("location", { hash }); |
| 1140 |
cockpit.dispatchEvent("locationchanged"); |
| 1141 |
}); |
| 1142 |
|
| 1143 |
/* ------------------------------------------------------------------------ |
| 1144 |
* Cockpit jump |
| 1145 |
*/ |
| 1146 |
|
| 1147 |
cockpit.jump = function jump(path, host) { |
| 1148 |
if (Array.isArray(path)) |
| 1149 |
path = "/" + path.map(encodeURIComponent).join("/") |
| 1150 |
.replaceAll("%40", "@") |
| 1151 |
.replaceAll("%3D", "=") |
| 1152 |
.replaceAll("%2B", "+"); |
| 1153 |
else |
| 1154 |
path = "" + path; |
| 1155 |
|
| 1156 |
/* When host is not given (undefined), use current transport's host. If |
| 1157 |
* it is null, use localhost. |
| 1158 |
*/ |
| 1159 |
if (host === undefined) |
| 1160 |
host = cockpit.transport.host; |
| 1161 |
|
| 1162 |
const options = { command: "jump", location: path, host }; |
| 1163 |
cockpit.transport.inject("\n" + JSON.stringify(options)); |
| 1164 |
}; |
| 1165 |
|
| 1166 |
/* --------------------------------------------------------------------- |
| 1167 |
* Cockpit Page Visibility |
| 1168 |
*/ |
| 1169 |
|
| 1170 |
(function() { |
| 1171 |
let hiddenHint = false; |
| 1172 |
|
| 1173 |
function visibility_change() { |
| 1174 |
let value = document.hidden; |
| 1175 |
if (value === false) |
| 1176 |
value = hiddenHint; |
| 1177 |
if (cockpit.hidden !== value) { |
| 1178 |
cockpit.hidden = value; |
| 1179 |
cockpit.dispatchEvent("visibilitychange"); |
| 1180 |
} |
| 1181 |
} |
| 1182 |
|
| 1183 |
document.addEventListener("visibilitychange", visibility_change); |
| 1184 |
|
| 1185 |
/* |
| 1186 |
* Wait for changes in visibility of just our iframe. These are delivered |
| 1187 |
* via a hint message from the parent. For now we are the only handler of |
| 1188 |
* hint messages, so this is implemented rather simply on purpose. |
| 1189 |
*/ |
| 1190 |
transport_globals.process_hints = function(data) { |
| 1191 |
if ("hidden" in data) { |
| 1192 |
hiddenHint = data.hidden; |
| 1193 |
visibility_change(); |
| 1194 |
} |
| 1195 |
}; |
| 1196 |
|
| 1197 |
/* The first time */ |
| 1198 |
visibility_change(); |
| 1199 |
}()); |
| 1200 |
|
| 1201 |
/* --------------------------------------------------------------------- |
| 1202 |
* Spawning |
| 1203 |
*/ |
| 1204 |
|
| 1205 |
function ProcessError(options, name) { |
| 1206 |
this.problem = options.problem || null; |
| 1207 |
this.exit_status = options["exit-status"]; |
| 1208 |
if (this.exit_status === undefined) |
| 1209 |
this.exit_status = null; |
| 1210 |
this.exit_signal = options["exit-signal"]; |
| 1211 |
if (this.exit_signal === undefined) |
| 1212 |
this.exit_signal = null; |
| 1213 |
this.message = options.message; |
| 1214 |
|
| 1215 |
if (this.message === undefined) { |
| 1216 |
if (this.problem) |
| 1217 |
this.message = cockpit.message(options.problem); |
| 1218 |
else if (this.exit_signal !== null) |
| 1219 |
this.message = cockpit.format(_("$0 killed with signal $1"), name, this.exit_signal); |
| 1220 |
else if (this.exit_status !== null) |
| 1221 |
this.message = cockpit.format(_("$0 exited with code $1"), name, this.exit_status); |
| 1222 |
else |
| 1223 |
this.message = cockpit.format(_("$0 failed"), name); |
| 1224 |
} else { |
| 1225 |
this.message = this.message.trim(); |
| 1226 |
} |
| 1227 |
|
| 1228 |
this.toString = function() { |
| 1229 |
return this.message; |
| 1230 |
}; |
| 1231 |
} |
| 1232 |
|
| 1233 |
cockpit.ProcessError = ProcessError; |
| 1234 |
|
| 1235 |
function spawn_debug() { |
| 1236 |
if (window.debugging == "all" || window.debugging?.includes("spawn")) |
| 1237 |
console.debug.apply(console, arguments); |
| 1238 |
} |
| 1239 |
|
| 1240 |
/* public */ |
| 1241 |
cockpit.spawn = function(command, options) { |
| 1242 |
const dfd = cockpit.defer(); |
| 1243 |
|
| 1244 |
const args = { payload: "stream", spawn: [] }; |
| 1245 |
if (command instanceof Array) { |
| 1246 |
for (let i = 0; i < command.length; i++) |
| 1247 |
args.spawn.push(String(command[i])); |
| 1248 |
} else { |
| 1249 |
args.spawn.push(String(command)); |
| 1250 |
} |
| 1251 |
if (options !== undefined) |
| 1252 |
Object.assign(args, options); |
| 1253 |
|
| 1254 |
spawn_debug("process spawn:", JSON.stringify(args.spawn)); |
| 1255 |
|
| 1256 |
const name = args.spawn[0] || "process"; |
| 1257 |
const channel = cockpit.channel(args); |
| 1258 |
|
| 1259 |
/* Callback that wants a stream response, see below */ |
| 1260 |
const buffer = channel.buffer(null); |
| 1261 |
|
| 1262 |
channel.addEventListener("close", function(event, options) { |
| 1263 |
const data = buffer.squash(); |
| 1264 |
spawn_debug("process closed:", JSON.stringify(options)); |
| 1265 |
if (data) |
| 1266 |
spawn_debug("process output:", data); |
| 1267 |
if (options.message !== undefined) |
| 1268 |
spawn_debug("process error:", options.message); |
| 1269 |
|
| 1270 |
if (options.problem) |
| 1271 |
dfd.reject(new ProcessError(options, name)); |
| 1272 |
else if (options["exit-status"] || options["exit-signal"]) |
| 1273 |
dfd.reject(new ProcessError(options, name), data); |
| 1274 |
else if (options.message !== undefined) |
| 1275 |
dfd.resolve(data, options.message); |
| 1276 |
else |
| 1277 |
dfd.resolve(data); |
| 1278 |
}); |
| 1279 |
|
| 1280 |
const ret = dfd.promise; |
| 1281 |
ret.stream = function(callback) { |
| 1282 |
buffer.callback = callback.bind(ret); |
| 1283 |
return this; |
| 1284 |
}; |
| 1285 |
|
| 1286 |
ret.input = function(message, stream) { |
| 1287 |
if (message !== null && message !== undefined) { |
| 1288 |
spawn_debug("process input:", message); |
| 1289 |
iterate_data(message, function(data) { |
| 1290 |
channel.send(data); |
| 1291 |
}); |
| 1292 |
} |
| 1293 |
if (!stream) |
| 1294 |
channel.control({ command: "done" }); |
| 1295 |
return this; |
| 1296 |
}; |
| 1297 |
|
| 1298 |
ret.close = function(problem) { |
| 1299 |
spawn_debug("process closing:", problem); |
| 1300 |
if (channel.valid) |
| 1301 |
channel.close(problem); |
| 1302 |
return this; |
| 1303 |
}; |
| 1304 |
|
| 1305 |
return ret; |
| 1306 |
}; |
| 1307 |
|
| 1308 |
/* public */ |
| 1309 |
cockpit.script = function(script, args, options) { |
| 1310 |
if (!options && is_plain_object(args)) { |
| 1311 |
options = args; |
| 1312 |
args = []; |
| 1313 |
} |
| 1314 |
const command = ["/bin/sh", "-c", script, "--"]; |
| 1315 |
command.push.apply(command, args); |
| 1316 |
return cockpit.spawn(command, options); |
| 1317 |
}; |
| 1318 |
|
| 1319 |
function dbus_debug() { |
| 1320 |
if (window.debugging == "all" || window.debugging?.includes("dbus")) |
| 1321 |
console.debug.apply(console, arguments); |
| 1322 |
} |
| 1323 |
|
| 1324 |
function DBusError(arg, arg1) { |
| 1325 |
if (typeof (arg) == "string") { |
| 1326 |
this.problem = arg; |
| 1327 |
this.name = null; |
| 1328 |
this.message = arg1 || cockpit.message(arg); |
| 1329 |
} else { |
| 1330 |
this.problem = null; |
| 1331 |
this.name = arg[0]; |
| 1332 |
this.message = arg[1][0] || arg[0]; |
| 1333 |
} |
| 1334 |
this.toString = function() { |
| 1335 |
return this.message; |
| 1336 |
}; |
| 1337 |
} |
| 1338 |
|
| 1339 |
function DBusCache() { |
| 1340 |
const self = this; |
| 1341 |
|
| 1342 |
let callbacks = []; |
| 1343 |
self.data = { }; |
| 1344 |
self.meta = { }; |
| 1345 |
|
| 1346 |
self.connect = function connect(path, iface, callback, first) { |
| 1347 |
const cb = [path, iface, callback]; |
| 1348 |
if (first) |
| 1349 |
callbacks.unshift(cb); |
| 1350 |
else |
| 1351 |
callbacks.push(cb); |
| 1352 |
return { |
| 1353 |
remove: function remove() { |
| 1354 |
const length = callbacks.length; |
| 1355 |
for (let i = 0; i < length; i++) { |
| 1356 |
const cb = callbacks[i]; |
| 1357 |
if (cb[0] === path && cb[1] === iface && cb[2] === callback) { |
| 1358 |
delete cb[i]; |
| 1359 |
break; |
| 1360 |
} |
| 1361 |
} |
| 1362 |
} |
| 1363 |
}; |
| 1364 |
}; |
| 1365 |
|
| 1366 |
function emit(path, iface, props) { |
| 1367 |
const copy = callbacks.slice(); |
| 1368 |
const length = copy.length; |
| 1369 |
for (let i = 0; i < length; i++) { |
| 1370 |
const cb = copy[i]; |
| 1371 |
if ((!cb[0] || cb[0] === path) && |
| 1372 |
(!cb[1] || cb[1] === iface)) { |
| 1373 |
cb[2](props, path); |
| 1374 |
} |
| 1375 |
} |
| 1376 |
} |
| 1377 |
|
| 1378 |
self.update = function update(path, iface, props) { |
| 1379 |
if (!self.data[path]) |
| 1380 |
self.data[path] = { }; |
| 1381 |
if (!self.data[path][iface]) |
| 1382 |
self.data[path][iface] = props; |
| 1383 |
else |
| 1384 |
props = Object.assign(self.data[path][iface], props); |
| 1385 |
emit(path, iface, props); |
| 1386 |
}; |
| 1387 |
|
| 1388 |
self.remove = function remove(path, iface) { |
| 1389 |
if (self.data[path]) { |
| 1390 |
delete self.data[path][iface]; |
| 1391 |
emit(path, iface, null); |
| 1392 |
} |
| 1393 |
}; |
| 1394 |
|
| 1395 |
self.lookup = function lookup(path, iface) { |
| 1396 |
if (self.data[path]) |
| 1397 |
return self.data[path][iface]; |
| 1398 |
return undefined; |
| 1399 |
}; |
| 1400 |
|
| 1401 |
self.each = function each(iface, callback) { |
| 1402 |
for (const path in self.data) { |
| 1403 |
for (const ifa in self.data[path]) { |
| 1404 |
if (ifa == iface) |
| 1405 |
callback(self.data[path][iface], path); |
| 1406 |
} |
| 1407 |
} |
| 1408 |
}; |
| 1409 |
|
| 1410 |
self.close = function close() { |
| 1411 |
self.data = { }; |
| 1412 |
const copy = callbacks; |
| 1413 |
callbacks = []; |
| 1414 |
const length = copy.length; |
| 1415 |
for (let i = 0; i < length; i++) |
| 1416 |
copy[i].callback(); |
| 1417 |
}; |
| 1418 |
} |
| 1419 |
|
| 1420 |
function DBusProxy(client, cache, iface, path, options) { |
| 1421 |
const self = this; |
| 1422 |
event_mixin(self, { }); |
| 1423 |
|
| 1424 |
let valid = false; |
| 1425 |
let defined = false; |
| 1426 |
const waits = cockpit.defer(); |
| 1427 |
|
| 1428 |
/* No enumeration on these properties */ |
| 1429 |
Object.defineProperties(self, { |
| 1430 |
client: { value: client, enumerable: false, writable: false }, |
| 1431 |
path: { value: path, enumerable: false, writable: false }, |
| 1432 |
iface: { value: iface, enumerable: false, writable: false }, |
| 1433 |
valid: { get: function() { return valid }, enumerable: false }, |
| 1434 |
wait: { |
| 1435 |
enumerable: false, |
| 1436 |
writable: false, |
| 1437 |
value: function(func) { |
| 1438 |
if (func) |
| 1439 |
waits.promise.always(func); |
| 1440 |
return waits.promise; |
| 1441 |
} |
| 1442 |
}, |
| 1443 |
call: { |
| 1444 |
value: function(name, args, options) { return client.call(path, iface, name, args, options) }, |
| 1445 |
enumerable: false, |
| 1446 |
writable: false |
| 1447 |
}, |
| 1448 |
data: { value: { }, enumerable: false } |
| 1449 |
}); |
| 1450 |
|
| 1451 |
if (!options) |
| 1452 |
options = { }; |
| 1453 |
|
| 1454 |
function define() { |
| 1455 |
if (!cache.meta[iface]) |
| 1456 |
return; |
| 1457 |
|
| 1458 |
const meta = cache.meta[iface]; |
| 1459 |
defined = true; |
| 1460 |
|
| 1461 |
Object.keys(meta.methods || { }).forEach(function(name) { |
| 1462 |
if (name[0].toLowerCase() == name[0]) |
| 1463 |
return; /* Only map upper case */ |
| 1464 |
|
| 1465 |
/* Again, make sure these don't show up in enumerations */ |
| 1466 |
Object.defineProperty(self, name, { |
| 1467 |
enumerable: false, |
| 1468 |
value: function() { |
| 1469 |
const dfd = cockpit.defer(); |
| 1470 |
client.call(path, iface, name, Array.prototype.slice.call(arguments)) |
| 1471 |
.done(function(reply) { dfd.resolve.apply(dfd, reply) }) |
| 1472 |
.fail(function(ex) { dfd.reject(ex) }); |
| 1473 |
return dfd.promise; |
| 1474 |
} |
| 1475 |
}); |
| 1476 |
}); |
| 1477 |
|
| 1478 |
Object.keys(meta.properties || { }).forEach(function(name) { |
| 1479 |
if (name[0].toLowerCase() == name[0]) |
| 1480 |
return; /* Only map upper case */ |
| 1481 |
|
| 1482 |
const config = { |
| 1483 |
enumerable: true, |
| 1484 |
get: function() { return self.data[name] }, |
| 1485 |
set: function(v) { throw Error(name + "is not writable") } |
| 1486 |
}; |
| 1487 |
|
| 1488 |
const prop = meta.properties[name]; |
| 1489 |
if (prop.flags && prop.flags.indexOf('w') !== -1) { |
| 1490 |
config.set = function(v) { |
| 1491 |
client.call(path, "org.freedesktop.DBus.Properties", "Set", |
| 1492 |
[iface, name, cockpit.variant(prop.type, v)]) |
| 1493 |
.fail(function(ex) { |
| 1494 |
console.log("Couldn't set " + iface + " " + name + |
| 1495 |
" at " + path + ": " + ex); |
| 1496 |
}); |
| 1497 |
}; |
| 1498 |
} |
| 1499 |
|
| 1500 |
/* Again, make sure these don't show up in enumerations */ |
| 1501 |
Object.defineProperty(self, name, config); |
| 1502 |
}); |
| 1503 |
} |
| 1504 |
|
| 1505 |
function update(props) { |
| 1506 |
if (props) { |
| 1507 |
Object.assign(self.data, props); |
| 1508 |
if (!defined) |
| 1509 |
define(); |
| 1510 |
valid = true; |
| 1511 |
} else { |
| 1512 |
valid = false; |
| 1513 |
} |
| 1514 |
self.dispatchEvent("changed", props); |
| 1515 |
} |
| 1516 |
|
| 1517 |
cache.connect(path, iface, update, true); |
| 1518 |
update(cache.lookup(path, iface)); |
| 1519 |
|
| 1520 |
function signal(path, iface, name, args) { |
| 1521 |
self.dispatchEvent("signal", name, args); |
| 1522 |
if (name[0].toLowerCase() != name[0]) { |
| 1523 |
args = args.slice(); |
| 1524 |
args.unshift(name); |
| 1525 |
self.dispatchEvent.apply(self, args); |
| 1526 |
} |
| 1527 |
} |
| 1528 |
|
| 1529 |
client.subscribe({ path, interface: iface }, signal, options.subscribe !== false); |
| 1530 |
|
| 1531 |
function waited(ex) { |
| 1532 |
// The client.watch call below will be successful for |
| 1533 |
// non-existing interfaces, but "valid" will be false in |
| 1534 |
// that case (as it should be). When that happens, our |
| 1535 |
// argument is not an Error object. So we create the |
| 1536 |
// "not-found" DBusError ourselves. |
| 1537 |
|
| 1538 |
if (valid) |
| 1539 |
waits.resolve(); |
| 1540 |
else if (ex instanceof DBusError) |
| 1541 |
waits.reject(ex); |
| 1542 |
else |
| 1543 |
waits.reject(new DBusError("not-found")); |
| 1544 |
} |
| 1545 |
|
| 1546 |
/* If watching then do a proper watch, otherwise object is done */ |
| 1547 |
if (options.watch !== false) |
| 1548 |
client.watch({ path, interface: iface }).always(waited); |
| 1549 |
else |
| 1550 |
waited(); |
| 1551 |
} |
| 1552 |
|
| 1553 |
function DBusProxies(client, cache, iface, path_namespace, options) { |
| 1554 |
const self = this; |
| 1555 |
event_mixin(self, { }); |
| 1556 |
|
| 1557 |
self.client = client; |
| 1558 |
self.iface = iface; |
| 1559 |
self.path_namespace = path_namespace; |
| 1560 |
|
| 1561 |
let waits; |
| 1562 |
|
| 1563 |
self.wait = function(func) { |
| 1564 |
if (func) |
| 1565 |
waits.always(func); |
| 1566 |
return waits; |
| 1567 |
}; |
| 1568 |
|
| 1569 |
Object.defineProperties(self, { |
| 1570 |
client: { enumerable: false, writable: false }, |
| 1571 |
iface: { enumerable: false, writable: false }, |
| 1572 |
path_namespace: { enumerable: false, writable: false }, |
| 1573 |
wait: { enumerable: false, writable: false }, |
| 1574 |
}); |
| 1575 |
|
| 1576 |
/* Subscribe to signals once for all proxies */ |
| 1577 |
const match = { interface: iface, path_namespace }; |
| 1578 |
|
| 1579 |
/* Callbacks added by proxies */ |
| 1580 |
client.subscribe(match); |
| 1581 |
|
| 1582 |
/* Watch for property changes */ |
| 1583 |
if (options.watch !== false) { |
| 1584 |
waits = client.watch(match); |
| 1585 |
} else { |
| 1586 |
waits = cockpit.defer().resolve().promise; |
| 1587 |
} |
| 1588 |
|
| 1589 |
/* Already added watch/subscribe, tell proxies not to */ |
| 1590 |
options = { watch: false, subscribe: false, ...options }; |
| 1591 |
|
| 1592 |
function update(props, path) { |
| 1593 |
let proxy = self[path]; |
| 1594 |
if (path) { |
| 1595 |
if (!props && proxy) { |
| 1596 |
delete self[path]; |
| 1597 |
self.dispatchEvent("removed", proxy); |
| 1598 |
} else if (props) { |
| 1599 |
if (!proxy) { |
| 1600 |
proxy = self[path] = client.proxy(iface, path, options); |
| 1601 |
self.dispatchEvent("added", proxy); |
| 1602 |
} |
| 1603 |
self.dispatchEvent("changed", proxy); |
| 1604 |
} |
| 1605 |
} |
| 1606 |
} |
| 1607 |
|
| 1608 |
cache.connect(null, iface, update, false); |
| 1609 |
cache.each(iface, update); |
| 1610 |
} |
| 1611 |
|
| 1612 |
function DBusClient(name, options) { |
| 1613 |
const self = this; |
| 1614 |
event_mixin(self, { }); |
| 1615 |
|
| 1616 |
const args = { }; |
| 1617 |
let track = false; |
| 1618 |
let owner = null; |
| 1619 |
|
| 1620 |
if (options) { |
| 1621 |
if (options.track) |
| 1622 |
track = true; |
| 1623 |
|
| 1624 |
delete options.track; |
| 1625 |
Object.assign(args, options); |
| 1626 |
} |
| 1627 |
args.payload = "dbus-json3"; |
| 1628 |
if (name) |
| 1629 |
args.name = name; |
| 1630 |
self.options = options; |
| 1631 |
self.unique_name = null; |
| 1632 |
|
| 1633 |
dbus_debug("dbus open: ", args); |
| 1634 |
|
| 1635 |
let channel = cockpit.channel(args); |
| 1636 |
const subscribers = { }; |
| 1637 |
let calls = { }; |
| 1638 |
let cache; |
| 1639 |
|
| 1640 |
/* The problem we closed with */ |
| 1641 |
let closed; |
| 1642 |
|
| 1643 |
self.constructors = { "*": DBusProxy }; |
| 1644 |
|
| 1645 |
/* Allows waiting on the channel if necessary */ |
| 1646 |
self.wait = channel.wait; |
| 1647 |
|
| 1648 |
function ensure_cache() { |
| 1649 |
if (!cache) |
| 1650 |
cache = new DBusCache(); |
| 1651 |
} |
| 1652 |
|
| 1653 |
function send(payload) { |
| 1654 |
if (channel?.valid) { |
| 1655 |
dbus_debug("dbus:", payload); |
| 1656 |
channel.send(payload); |
| 1657 |
return true; |
| 1658 |
} |
| 1659 |
return false; |
| 1660 |
} |
| 1661 |
|
| 1662 |
function matches(signal, match) { |
| 1663 |
if (match.path && signal[0] !== match.path) |
| 1664 |
return false; |
| 1665 |
if (match.path_namespace && signal[0].indexOf(match.path_namespace) !== 0) |
| 1666 |
return false; |
| 1667 |
if (match.interface && signal[1] !== match.interface) |
| 1668 |
return false; |
| 1669 |
if (match.member && signal[2] !== match.member) |
| 1670 |
return false; |
| 1671 |
if (match.arg0 && (!signal[3] || signal[3][0] !== match.arg0)) |
| 1672 |
return false; |
| 1673 |
return true; |
| 1674 |
} |
| 1675 |
|
| 1676 |
function on_message(event, payload) { |
| 1677 |
dbus_debug("dbus:", payload); |
| 1678 |
let msg; |
| 1679 |
try { |
| 1680 |
msg = JSON.parse(payload); |
| 1681 |
} catch (ex) { |
| 1682 |
console.warn("received invalid dbus json message:", ex); |
| 1683 |
} |
| 1684 |
if (msg === undefined) { |
| 1685 |
channel.close({ problem: "protocol-error" }); |
| 1686 |
return; |
| 1687 |
} |
| 1688 |
const dfd = (msg.id !== undefined) ? calls[msg.id] : undefined; |
| 1689 |
if (msg.reply) { |
| 1690 |
if (dfd) { |
| 1691 |
const options = { }; |
| 1692 |
if (msg.type) |
| 1693 |
options.type = msg.type; |
| 1694 |
if (msg.flags) |
| 1695 |
options.flags = msg.flags; |
| 1696 |
dfd.resolve(msg.reply[0] || [], options); |
| 1697 |
delete calls[msg.id]; |
| 1698 |
} |
| 1699 |
return; |
| 1700 |
} else if (msg.error) { |
| 1701 |
if (dfd) { |
| 1702 |
dfd.reject(new DBusError(msg.error)); |
| 1703 |
delete calls[msg.id]; |
| 1704 |
} |
| 1705 |
return; |
| 1706 |
} |
| 1707 |
|
| 1708 |
/* |
| 1709 |
* The above promise resolutions or failures are triggered via |
| 1710 |
* later_invoke(). In order to preserve ordering guarantees we |
| 1711 |
* also have to process other events that way too. |
| 1712 |
*/ |
| 1713 |
later_invoke(function() { |
| 1714 |
if (msg.signal) { |
| 1715 |
for (const id in subscribers) { |
| 1716 |
const subscription = subscribers[id]; |
| 1717 |
if (subscription.callback) { |
| 1718 |
if (matches(msg.signal, subscription.match)) |
| 1719 |
subscription.callback.apply(self, msg.signal); |
| 1720 |
} |
| 1721 |
} |
| 1722 |
} else if (msg.notify) { |
| 1723 |
notify(msg.notify); |
| 1724 |
} else if (msg.meta) { |
| 1725 |
meta(msg.meta); |
| 1726 |
} else if (msg.owner !== undefined) { |
| 1727 |
self.dispatchEvent("owner", msg.owner); |
| 1728 |
|
| 1729 |
/* |
| 1730 |
* We won't get this signal with the same |
| 1731 |
* owner twice so if we've seen an owner |
| 1732 |
* before that means it has changed. |
| 1733 |
*/ |
| 1734 |
if (track && owner) |
| 1735 |
self.close(); |
| 1736 |
|
| 1737 |
owner = msg.owner; |
| 1738 |
} else { |
| 1739 |
dbus_debug("received unexpected dbus json message:", payload); |
| 1740 |
} |
| 1741 |
}); |
| 1742 |
} |
| 1743 |
|
| 1744 |
function meta(data) { |
| 1745 |
ensure_cache(); |
| 1746 |
Object.assign(cache.meta, data); |
| 1747 |
self.dispatchEvent("meta", data); |
| 1748 |
} |
| 1749 |
|
| 1750 |
function notify(data) { |
| 1751 |
ensure_cache(); |
| 1752 |
for (const path in data) { |
| 1753 |
for (const iface in data[path]) { |
| 1754 |
const props = data[path][iface]; |
| 1755 |
if (!props) |
| 1756 |
cache.remove(path, iface); |
| 1757 |
else |
| 1758 |
cache.update(path, iface, props); |
| 1759 |
} |
| 1760 |
} |
| 1761 |
self.dispatchEvent("notify", data); |
| 1762 |
} |
| 1763 |
|
| 1764 |
this.notify = notify; |
| 1765 |
|
| 1766 |
function close_perform(options) { |
| 1767 |
closed = options.problem || "disconnected"; |
| 1768 |
const outstanding = calls; |
| 1769 |
calls = { }; |
| 1770 |
for (const id in outstanding) { |
| 1771 |
outstanding[id].reject(new DBusError(closed, options.message)); |
| 1772 |
} |
| 1773 |
self.dispatchEvent("close", options); |
| 1774 |
} |
| 1775 |
|
| 1776 |
this.close = function close(options) { |
| 1777 |
if (typeof options == "string") |
| 1778 |
options = { problem: options }; |
| 1779 |
if (!options) |
| 1780 |
options = { }; |
| 1781 |
if (channel) |
| 1782 |
channel.close(options); |
| 1783 |
else |
| 1784 |
close_perform(options); |
| 1785 |
}; |
| 1786 |
|
| 1787 |
function on_ready(event, message) { |
| 1788 |
dbus_debug("dbus ready:", options); |
| 1789 |
self.unique_name = message["unique-name"]; |
| 1790 |
} |
| 1791 |
|
| 1792 |
function on_close(event, options) { |
| 1793 |
dbus_debug("dbus close:", options); |
| 1794 |
channel.removeEventListener("ready", on_ready); |
| 1795 |
channel.removeEventListener("message", on_message); |
| 1796 |
channel.removeEventListener("close", on_close); |
| 1797 |
channel = null; |
| 1798 |
close_perform(options); |
| 1799 |
} |
| 1800 |
|
| 1801 |
channel.addEventListener("ready", on_ready); |
| 1802 |
channel.addEventListener("message", on_message); |
| 1803 |
channel.addEventListener("close", on_close); |
| 1804 |
|
| 1805 |
let last_cookie = 1; |
| 1806 |
|
| 1807 |
this.call = function call(path, iface, method, args, options) { |
| 1808 |
const dfd = cockpit.defer(); |
| 1809 |
const id = String(last_cookie); |
| 1810 |
last_cookie++; |
| 1811 |
const method_call = { |
| 1812 |
...options, |
| 1813 |
call: [path, iface, method, args || []], |
| 1814 |
id |
| 1815 |
}; |
| 1816 |
|
| 1817 |
const msg = JSON.stringify(method_call); |
| 1818 |
if (send(msg)) |
| 1819 |
calls[id] = dfd; |
| 1820 |
else |
| 1821 |
dfd.reject(new DBusError(closed)); |
| 1822 |
|
| 1823 |
return dfd.promise; |
| 1824 |
}; |
| 1825 |
|
| 1826 |
self.signal = function signal(path, iface, member, args, options) { |
| 1827 |
if (!channel || !channel.valid) |
| 1828 |
return; |
| 1829 |
|
| 1830 |
const message = { ...options, signal: [path, iface, member, args || []] }; |
| 1831 |
|
| 1832 |
send(JSON.stringify(message)); |
| 1833 |
}; |
| 1834 |
|
| 1835 |
this.subscribe = function subscribe(match, callback, rule) { |
| 1836 |
const subscription = { |
| 1837 |
match: { ...match }, |
| 1838 |
callback |
| 1839 |
}; |
| 1840 |
|
| 1841 |
if (rule !== false) |
| 1842 |
send(JSON.stringify({ "add-match": subscription.match })); |
| 1843 |
|
| 1844 |
let id; |
| 1845 |
if (callback) { |
| 1846 |
id = String(last_cookie); |
| 1847 |
last_cookie++; |
| 1848 |
subscribers[id] = subscription; |
| 1849 |
} |
| 1850 |
|
| 1851 |
return { |
| 1852 |
remove: function() { |
| 1853 |
let prev; |
| 1854 |
if (id) { |
| 1855 |
prev = subscribers[id]; |
| 1856 |
if (prev) |
| 1857 |
delete subscribers[id]; |
| 1858 |
} |
| 1859 |
if (rule !== false && prev) |
| 1860 |
send(JSON.stringify({ "remove-match": prev.match })); |
| 1861 |
} |
| 1862 |
}; |
| 1863 |
}; |
| 1864 |
|
| 1865 |
self.watch = function watch(path) { |
| 1866 |
const match = is_plain_object(path) ? { ...path } : { path: String(path) }; |
| 1867 |
|
| 1868 |
const id = String(last_cookie); |
| 1869 |
last_cookie++; |
| 1870 |
const dfd = cockpit.defer(); |
| 1871 |
|
| 1872 |
const msg = JSON.stringify({ watch: match, id }); |
| 1873 |
if (send(msg)) |
| 1874 |
calls[id] = dfd; |
| 1875 |
else |
| 1876 |
dfd.reject(new DBusError(closed)); |
| 1877 |
|
| 1878 |
const ret = dfd.promise; |
| 1879 |
ret.remove = function remove() { |
| 1880 |
if (id in calls) { |
| 1881 |
dfd.reject(new DBusError("cancelled")); |
| 1882 |
delete calls[id]; |
| 1883 |
} |
| 1884 |
send(JSON.stringify({ unwatch: match })); |
| 1885 |
}; |
| 1886 |
return ret; |
| 1887 |
}; |
| 1888 |
|
| 1889 |
self.proxy = function proxy(iface, path, options) { |
| 1890 |
if (!iface) |
| 1891 |
iface = name; |
| 1892 |
iface = String(iface); |
| 1893 |
if (!path) |
| 1894 |
path = "/" + iface.replaceAll(".", "/"); |
| 1895 |
let Constructor = self.constructors[iface]; |
| 1896 |
if (!Constructor) |
| 1897 |
Constructor = self.constructors["*"]; |
| 1898 |
if (!options) |
| 1899 |
options = { }; |
| 1900 |
ensure_cache(); |
| 1901 |
return new Constructor(self, cache, iface, String(path), options); |
| 1902 |
}; |
| 1903 |
|
| 1904 |
self.proxies = function proxies(iface, path_namespace, options) { |
| 1905 |
if (!iface) |
| 1906 |
iface = name; |
| 1907 |
if (!path_namespace) |
| 1908 |
path_namespace = "/"; |
| 1909 |
if (!options) |
| 1910 |
options = { }; |
| 1911 |
ensure_cache(); |
| 1912 |
return new DBusProxies(self, cache, String(iface), String(path_namespace), options); |
| 1913 |
}; |
| 1914 |
} |
| 1915 |
|
| 1916 |
/* Well known buses */ |
| 1917 |
const shared_dbus = { |
| 1918 |
internal: null, |
| 1919 |
session: null, |
| 1920 |
system: null, |
| 1921 |
}; |
| 1922 |
|
| 1923 |
/* public */ |
| 1924 |
cockpit.dbus = function dbus(name, options) { |
| 1925 |
if (!options) |
| 1926 |
options = { bus: "system" }; |
| 1927 |
|
| 1928 |
/* |
| 1929 |
* Figure out if this we should use a shared bus. |
| 1930 |
* |
| 1931 |
* This is only the case if a null name *and* the |
| 1932 |
* options are just a simple { "bus": "xxxx" } |
| 1933 |
*/ |
| 1934 |
const keys = Object.keys(options); |
| 1935 |
const bus = options.bus; |
| 1936 |
const shared = !name && keys.length == 1 && bus in shared_dbus; |
| 1937 |
|
| 1938 |
if (shared && shared_dbus[bus]) |
| 1939 |
return shared_dbus[bus]; |
| 1940 |
|
| 1941 |
const client = new DBusClient(name, options); |
| 1942 |
|
| 1943 |
/* |
| 1944 |
* Store the shared bus for next time. Override the |
| 1945 |
* close function to only work when a problem is |
| 1946 |
* indicated. |
| 1947 |
*/ |
| 1948 |
if (shared) { |
| 1949 |
const old_close = client.close; |
| 1950 |
client.close = function() { |
| 1951 |
if (arguments.length > 0) |
| 1952 |
old_close.apply(client, arguments); |
| 1953 |
}; |
| 1954 |
client.addEventListener("close", function() { |
| 1955 |
if (shared_dbus[bus] == client) |
| 1956 |
shared_dbus[bus] = null; |
| 1957 |
}); |
| 1958 |
shared_dbus[bus] = client; |
| 1959 |
} |
| 1960 |
|
| 1961 |
return client; |
| 1962 |
}; |
| 1963 |
|
| 1964 |
cockpit.variant = function variant(type, value) { |
| 1965 |
return { v: value, t: type }; |
| 1966 |
}; |
| 1967 |
|
| 1968 |
cockpit.byte_array = function byte_array(string) { |
| 1969 |
console.warn("cockpit.byte_array() is deprecated, use window.btoa"); |
| 1970 |
return window.btoa(string); |
| 1971 |
}; |
| 1972 |
|
| 1973 |
/* File access |
| 1974 |
*/ |
| 1975 |
|
| 1976 |
cockpit.file = function file(path, options) { |
| 1977 |
options = options || { }; |
| 1978 |
const binary = options.binary; |
| 1979 |
|
| 1980 |
const self = { |
| 1981 |
path, |
| 1982 |
read, |
| 1983 |
replace, |
| 1984 |
modify, |
| 1985 |
|
| 1986 |
watch, |
| 1987 |
|
| 1988 |
close |
| 1989 |
}; |
| 1990 |
|
| 1991 |
const base_channel_options = { ...options }; |
| 1992 |
delete base_channel_options.syntax; |
| 1993 |
|
| 1994 |
function parse(str) { |
| 1995 |
if (options.syntax?.parse) |
| 1996 |
return options.syntax.parse(str); |
| 1997 |
else |
| 1998 |
return str; |
| 1999 |
} |
| 2000 |
|
| 2001 |
function stringify(obj) { |
| 2002 |
if (options.syntax?.stringify) |
| 2003 |
return options.syntax.stringify(obj); |
| 2004 |
else |
| 2005 |
return obj; |
| 2006 |
} |
| 2007 |
|
| 2008 |
let read_promise = null; |
| 2009 |
let read_channel; |
| 2010 |
|
| 2011 |
function read() { |
| 2012 |
if (read_promise) |
| 2013 |
return read_promise; |
| 2014 |
|
| 2015 |
const dfd = cockpit.defer(); |
| 2016 |
const opts = { |
| 2017 |
...base_channel_options, |
| 2018 |
payload: "fsread1", |
| 2019 |
path |
| 2020 |
}; |
| 2021 |
|
| 2022 |
function try_read() { |
| 2023 |
read_channel = cockpit.channel(opts); |
| 2024 |
const content_parts = []; |
| 2025 |
read_channel.addEventListener("message", function (event, message) { |
| 2026 |
content_parts.push(message); |
| 2027 |
}); |
| 2028 |
read_channel.addEventListener("close", function (event, message) { |
| 2029 |
read_channel = null; |
| 2030 |
|
| 2031 |
if (message.problem == "change-conflict") { |
| 2032 |
try_read(); |
| 2033 |
return; |
| 2034 |
} |
| 2035 |
|
| 2036 |
read_promise = null; |
| 2037 |
|
| 2038 |
if (message.problem) { |
| 2039 |
const error = new BasicError(message.problem, message.message); |
| 2040 |
fire_watch_callbacks(null, null, error); |
| 2041 |
dfd.reject(error); |
| 2042 |
return; |
| 2043 |
} |
| 2044 |
|
| 2045 |
let content; |
| 2046 |
if (message.tag == "-") |
| 2047 |
content = null; |
| 2048 |
else { |
| 2049 |
try { |
| 2050 |
content = parse(join_data(content_parts, binary)); |
| 2051 |
} catch (e) { |
| 2052 |
fire_watch_callbacks(null, null, e); |
| 2053 |
dfd.reject(e); |
| 2054 |
return; |
| 2055 |
} |
| 2056 |
} |
| 2057 |
|
| 2058 |
fire_watch_callbacks(content, message.tag); |
| 2059 |
dfd.resolve(content, message.tag); |
| 2060 |
}); |
| 2061 |
} |
| 2062 |
|
| 2063 |
try_read(); |
| 2064 |
|
| 2065 |
read_promise = dfd.promise; |
| 2066 |
return read_promise; |
| 2067 |
} |
| 2068 |
|
| 2069 |
let replace_channel = null; |
| 2070 |
|
| 2071 |
function replace(new_content, expected_tag) { |
| 2072 |
const dfd = cockpit.defer(); |
| 2073 |
|
| 2074 |
let file_content; |
| 2075 |
try { |
| 2076 |
file_content = (new_content === null) ? null : stringify(new_content); |
| 2077 |
} catch (e) { |
| 2078 |
dfd.reject(e); |
| 2079 |
return dfd.promise; |
| 2080 |
} |
| 2081 |
|
| 2082 |
if (replace_channel) |
| 2083 |
replace_channel.close("abort"); |
| 2084 |
|
| 2085 |
const opts = { |
| 2086 |
...base_channel_options, |
| 2087 |
payload: "fsreplace1", |
| 2088 |
path, |
| 2089 |
tag: expected_tag |
| 2090 |
}; |
| 2091 |
replace_channel = cockpit.channel(opts); |
| 2092 |
|
| 2093 |
replace_channel.addEventListener("close", function (event, message) { |
| 2094 |
replace_channel = null; |
| 2095 |
if (message.problem) { |
| 2096 |
dfd.reject(new BasicError(message.problem, message.message)); |
| 2097 |
} else { |
| 2098 |
fire_watch_callbacks(new_content, message.tag); |
| 2099 |
dfd.resolve(message.tag); |
| 2100 |
} |
| 2101 |
}); |
| 2102 |
|
| 2103 |
// null means 'erase this file', which is what will happen if |
| 2104 |
// we send no data. the empty string means "write an empty |
| 2105 |
// file", and in order to do that, we need to explicitly send |
| 2106 |
// an empty frame (or we'll delete the file). iterate_data() |
| 2107 |
// doesn't call us if the string is empty, so we handle it. |
| 2108 |
if (file_content !== null) { |
| 2109 |
if (file_content.length === 0 || file_content.byteLength === 0) { |
| 2110 |
replace_channel.send(file_content); |
| 2111 |
} else { |
| 2112 |
iterate_data(file_content, data => { |
| 2113 |
replace_channel.send(data); |
| 2114 |
}); |
| 2115 |
} |
| 2116 |
} |
| 2117 |
|
| 2118 |
replace_channel.control({ command: "done" }); |
| 2119 |
return dfd.promise; |
| 2120 |
} |
| 2121 |
|
| 2122 |
function modify(callback, initial_content, initial_tag) { |
| 2123 |
const dfd = cockpit.defer(); |
| 2124 |
|
| 2125 |
function update(content, tag) { |
| 2126 |
let new_content = callback(content); |
| 2127 |
if (new_content === undefined) |
| 2128 |
new_content = content; |
| 2129 |
replace(new_content, tag) |
| 2130 |
.done(function (new_tag) { |
| 2131 |
dfd.resolve(new_content, new_tag); |
| 2132 |
}) |
| 2133 |
.fail(function (error) { |
| 2134 |
if (error.problem == "change-conflict") |
| 2135 |
read_then_update(); |
| 2136 |
else |
| 2137 |
dfd.reject(error); |
| 2138 |
}); |
| 2139 |
} |
| 2140 |
|
| 2141 |
function read_then_update() { |
| 2142 |
read() |
| 2143 |
.done(update) |
| 2144 |
.fail(function (error) { |
| 2145 |
dfd.reject(error); |
| 2146 |
}); |
| 2147 |
} |
| 2148 |
|
| 2149 |
if (initial_content === undefined) |
| 2150 |
read_then_update(); |
| 2151 |
else |
| 2152 |
update(initial_content, initial_tag); |
| 2153 |
|
| 2154 |
return dfd.promise; |
| 2155 |
} |
| 2156 |
|
| 2157 |
const watch_callbacks = []; |
| 2158 |
let n_watch_callbacks = 0; |
| 2159 |
|
| 2160 |
let watch_channel = null; |
| 2161 |
let watch_tag; |
| 2162 |
|
| 2163 |
function ensure_watch_channel(options) { |
| 2164 |
if (n_watch_callbacks > 0) { |
| 2165 |
if (watch_channel) |
| 2166 |
return; |
| 2167 |
|
| 2168 |
watch_channel = new FsInfoClient(path, ["tag"], { superuser: base_channel_options.superuser }); |
| 2169 |
watch_channel.on('change', (state) => { |
| 2170 |
if (state.error) { |
| 2171 |
// Behave like fsread1, not-found is not a fatal error |
| 2172 |
if (state.error.problem === "not-found") { |
| 2173 |
fire_watch_callbacks(null, "-"); |
| 2174 |
} else { |
| 2175 |
const error = new BasicError(state.error.problem, state.error.message); |
| 2176 |
fire_watch_callbacks(null, null, error); |
| 2177 |
} |
| 2178 |
} else if (state.info && state.info.tag) { |
| 2179 |
// otherwise, the file is present with the given tag |
| 2180 |
if (state.info.tag !== watch_tag) { |
| 2181 |
// cockpit.file.watch() defaults to reading |
| 2182 |
if (options?.read === false) |
| 2183 |
fire_watch_callbacks(null, state.info.tag); |
| 2184 |
else |
| 2185 |
read(); |
| 2186 |
} |
| 2187 |
} |
| 2188 |
}); |
| 2189 |
|
| 2190 |
// fallback when running against bridge < 310 |
| 2191 |
watch_channel.on('close', ex => { |
| 2192 |
if (ex.problem === 'not-supported') { |
| 2193 |
const opts = { |
| 2194 |
payload: "fswatch1", |
| 2195 |
path, |
| 2196 |
superuser: base_channel_options.superuser, |
| 2197 |
}; |
| 2198 |
watch_channel = cockpit.channel(opts); |
| 2199 |
watch_channel.addEventListener("message", (event, message_string) => { |
| 2200 |
let message; |
| 2201 |
try { |
| 2202 |
message = JSON.parse(message_string); |
| 2203 |
} catch (e) { |
| 2204 |
message = null; |
| 2205 |
} |
| 2206 |
if (message && message.path == path && message.tag && message.tag != watch_tag) { |
| 2207 |
if (options && options.read !== undefined && !options.read) |
| 2208 |
fire_watch_callbacks(null, message.tag); |
| 2209 |
else |
| 2210 |
read(); |
| 2211 |
} |
| 2212 |
}); |
| 2213 |
// trigger initial watch event |
| 2214 |
read(); |
| 2215 |
} |
| 2216 |
}); |
| 2217 |
} else { |
| 2218 |
if (watch_channel) { |
| 2219 |
watch_channel.close(); |
| 2220 |
watch_channel = null; |
| 2221 |
} |
| 2222 |
} |
| 2223 |
} |
| 2224 |
|
| 2225 |
function fire_watch_callbacks(/* content, tag, error */) { |
| 2226 |
watch_tag = arguments[1] || null; |
| 2227 |
invoke_functions(watch_callbacks, self, arguments); |
| 2228 |
} |
| 2229 |
|
| 2230 |
function watch(callback, options) { |
| 2231 |
if (callback) |
| 2232 |
watch_callbacks.push(callback); |
| 2233 |
n_watch_callbacks += 1; |
| 2234 |
ensure_watch_channel(options); |
| 2235 |
|
| 2236 |
watch_tag = null; |
| 2237 |
|
| 2238 |
return { |
| 2239 |
remove: function () { |
| 2240 |
if (callback) { |
| 2241 |
const index = watch_callbacks.indexOf(callback); |
| 2242 |
if (index > -1) |
| 2243 |
watch_callbacks[index] = null; |
| 2244 |
} |
| 2245 |
n_watch_callbacks -= 1; |
| 2246 |
ensure_watch_channel(options); |
| 2247 |
} |
| 2248 |
}; |
| 2249 |
} |
| 2250 |
|
| 2251 |
function close() { |
| 2252 |
if (read_channel) |
| 2253 |
read_channel.close("cancelled"); |
| 2254 |
if (replace_channel) |
| 2255 |
replace_channel.close("cancelled"); |
| 2256 |
if (watch_channel) |
| 2257 |
watch_channel.close(); |
| 2258 |
} |
| 2259 |
|
| 2260 |
return self; |
| 2261 |
}; |
| 2262 |
|
| 2263 |
/* --------------------------------------------------------------------- |
| 2264 |
* Localization |
| 2265 |
*/ |
| 2266 |
|
| 2267 |
let po_data = { }; |
| 2268 |
let po_plural; |
| 2269 |
|
| 2270 |
cockpit.language = "en"; |
| 2271 |
cockpit.language_direction = "ltr"; |
| 2272 |
const test_l10n = window.localStorage.test_l10n; |
| 2273 |
|
| 2274 |
cockpit.locale = function locale(po) { |
| 2275 |
let lang = cockpit.language; |
| 2276 |
let lang_dir = cockpit.language_direction; |
| 2277 |
let header; |
| 2278 |
|
| 2279 |
if (po) { |
| 2280 |
Object.assign(po_data, po); |
| 2281 |
header = po[""]; |
| 2282 |
} else if (po === null) { |
| 2283 |
po_data = { }; |
| 2284 |
} |
| 2285 |
|
| 2286 |
if (header) { |
| 2287 |
if (header["plural-forms"]) |
| 2288 |
po_plural = header["plural-forms"]; |
| 2289 |
if (header.language) |
| 2290 |
lang = header.language; |
| 2291 |
if (header["language-direction"]) |
| 2292 |
lang_dir = header["language-direction"]; |
| 2293 |
} |
| 2294 |
|
| 2295 |
cockpit.language = lang; |
| 2296 |
cockpit.language_direction = lang_dir; |
| 2297 |
}; |
| 2298 |
|
| 2299 |
cockpit.translate = function translate(/* ... */) { |
| 2300 |
let what; |
| 2301 |
|
| 2302 |
/* Called without arguments, entire document */ |
| 2303 |
if (arguments.length === 0) |
| 2304 |
what = [document]; |
| 2305 |
|
| 2306 |
/* Called with a single array like argument */ |
| 2307 |
else if (arguments.length === 1 && arguments[0].length) |
| 2308 |
what = arguments[0]; |
| 2309 |
|
| 2310 |
/* Called with 1 or more element arguments */ |
| 2311 |
else |
| 2312 |
what = arguments; |
| 2313 |
|
| 2314 |
/* Translate all the things */ |
| 2315 |
const wlen = what.length; |
| 2316 |
for (let w = 0; w < wlen; w++) { |
| 2317 |
/* The list of things to translate */ |
| 2318 |
let list = null; |
| 2319 |
if (what[w].querySelectorAll) |
| 2320 |
list = what[w].querySelectorAll("[translate]"); |
| 2321 |
if (!list) |
| 2322 |
continue; |
| 2323 |
|
| 2324 |
/* Each element */ |
| 2325 |
for (let i = 0; i < list.length; i++) { |
| 2326 |
const el = list[i]; |
| 2327 |
|
| 2328 |
let val = el.getAttribute("translate") || "yes"; |
| 2329 |
if (val == "no") |
| 2330 |
continue; |
| 2331 |
|
| 2332 |
/* Each thing to translate */ |
| 2333 |
const tasks = val.split(" "); |
| 2334 |
val = el.getAttribute("translate-context") || el.getAttribute("context"); |
| 2335 |
for (let t = 0; t < tasks.length; t++) { |
| 2336 |
if (tasks[t] == "yes" || tasks[t] == "translate") |
| 2337 |
el.textContent = cockpit.gettext(val, el.textContent); |
| 2338 |
else if (tasks[t]) |
| 2339 |
el.setAttribute(tasks[t], cockpit.gettext(val, el.getAttribute(tasks[t]) || "")); |
| 2340 |
} |
| 2341 |
|
| 2342 |
/* Mark this thing as translated */ |
| 2343 |
el.removeAttribute("translate"); |
| 2344 |
} |
| 2345 |
} |
| 2346 |
}; |
| 2347 |
|
| 2348 |
cockpit.gettext = function gettext(context, string) { |
| 2349 |
/* Missing first parameter */ |
| 2350 |
if (arguments.length == 1) { |
| 2351 |
string = context; |
| 2352 |
context = undefined; |
| 2353 |
} |
| 2354 |
|
| 2355 |
const key = context ? context + '\u0004' + string : string; |
| 2356 |
if (po_data) { |
| 2357 |
const translated = po_data[key]; |
| 2358 |
if (translated?.[1]) |
| 2359 |
string = translated[1]; |
| 2360 |
} |
| 2361 |
|
| 2362 |
if (test_l10n === 'true') |
| 2363 |
return "»" + string + "«"; |
| 2364 |
|
| 2365 |
return string; |
| 2366 |
}; |
| 2367 |
|
| 2368 |
function imply(val) { |
| 2369 |
return (val === true ? 1 : val || 0); |
| 2370 |
} |
| 2371 |
|
| 2372 |
cockpit.ngettext = function ngettext(context, string1, stringN, num) { |
| 2373 |
/* Missing first parameter */ |
| 2374 |
if (arguments.length == 3) { |
| 2375 |
num = stringN; |
| 2376 |
stringN = string1; |
| 2377 |
string1 = context; |
| 2378 |
context = undefined; |
| 2379 |
} |
| 2380 |
|
| 2381 |
const key = context ? context + '\u0004' + string1 : string1; |
| 2382 |
if (po_data && po_plural) { |
| 2383 |
const translated = po_data[key]; |
| 2384 |
if (translated) { |
| 2385 |
const i = imply(po_plural(num)) + 1; |
| 2386 |
if (translated[i]) |
| 2387 |
return translated[i]; |
| 2388 |
} |
| 2389 |
} |
| 2390 |
if (num == 1) |
| 2391 |
return string1; |
| 2392 |
return stringN; |
| 2393 |
}; |
| 2394 |
|
| 2395 |
cockpit.noop = function noop(arg0, arg1) { |
| 2396 |
return arguments[arguments.length - 1]; |
| 2397 |
}; |
| 2398 |
|
| 2399 |
/* Only for _() calls here in the cockpit code */ |
| 2400 |
const _ = cockpit.gettext; |
| 2401 |
|
| 2402 |
cockpit.message = function message(arg) { |
| 2403 |
if (arg.message) |
| 2404 |
return arg.message; |
| 2405 |
|
| 2406 |
let problem = null; |
| 2407 |
if (arg.problem) |
| 2408 |
problem = arg.problem; |
| 2409 |
else |
| 2410 |
problem = arg + ""; |
| 2411 |
if (problem == "terminated") |
| 2412 |
return _("Your session has been terminated."); |
| 2413 |
else if (problem == "no-session") |
| 2414 |
return _("Your session has expired. Please log in again."); |
| 2415 |
else if (problem == "access-denied") |
| 2416 |
return _("Not permitted to perform this action."); |
| 2417 |
else if (problem == "authentication-failed") |
| 2418 |
return _("Login failed"); |
| 2419 |
else if (problem == "authentication-not-supported") |
| 2420 |
return _("The server refused to authenticate using any supported methods."); |
| 2421 |
else if (problem == "unknown-hostkey") |
| 2422 |
return _("Untrusted host"); |
| 2423 |
else if (problem == "unknown-host") |
| 2424 |
return _("Untrusted host"); |
| 2425 |
else if (problem == "invalid-hostkey") |
| 2426 |
return _("Host key is incorrect"); |
| 2427 |
else if (problem == "internal-error") |
| 2428 |
return _("Internal error"); |
| 2429 |
else if (problem == "timeout") |
| 2430 |
return _("Connection has timed out."); |
| 2431 |
else if (problem == "no-cockpit") |
| 2432 |
return _("Cockpit is not installed on the system."); |
| 2433 |
else if (problem == "no-forwarding") |
| 2434 |
return _("Cannot forward login credentials"); |
| 2435 |
else if (problem == "disconnected") |
| 2436 |
return _("Server has closed the connection."); |
| 2437 |
else if (problem == "not-supported") |
| 2438 |
return _("Cockpit is not compatible with the software on the system."); |
| 2439 |
else if (problem == "no-host") |
| 2440 |
return _("Cockpit could not contact the given host."); |
| 2441 |
else if (problem == "too-large") |
| 2442 |
return _("Too much data"); |
| 2443 |
else |
| 2444 |
return problem; |
| 2445 |
}; |
| 2446 |
|
| 2447 |
function HttpError(arg0, arg1, message) { |
| 2448 |
this.status = parseInt(arg0, 10); |
| 2449 |
this.reason = arg1; |
| 2450 |
this.message = message || arg1; |
| 2451 |
this.problem = null; |
| 2452 |
|
| 2453 |
this.valueOf = function() { |
| 2454 |
return this.status; |
| 2455 |
}; |
| 2456 |
this.toString = function() { |
| 2457 |
return this.status + " " + this.message; |
| 2458 |
}; |
| 2459 |
} |
| 2460 |
|
| 2461 |
function http_debug() { |
| 2462 |
if (window.debugging == "all" || window.debugging?.includes("http")) |
| 2463 |
console.debug.apply(console, arguments); |
| 2464 |
} |
| 2465 |
|
| 2466 |
function find_header(headers, name) { |
| 2467 |
if (!headers) |
| 2468 |
return undefined; |
| 2469 |
name = name.toLowerCase(); |
| 2470 |
for (const head in headers) { |
| 2471 |
if (head.toLowerCase() == name) |
| 2472 |
return headers[head]; |
| 2473 |
} |
| 2474 |
return undefined; |
| 2475 |
} |
| 2476 |
|
| 2477 |
function HttpClient(endpoint, options) { |
| 2478 |
const self = this; |
| 2479 |
|
| 2480 |
self.options = options; |
| 2481 |
options.payload = "http-stream2"; |
| 2482 |
|
| 2483 |
const active_requests = []; |
| 2484 |
|
| 2485 |
if (endpoint !== undefined) { |
| 2486 |
if (endpoint.indexOf && endpoint.indexOf("/") === 0) { |
| 2487 |
options.unix = endpoint; |
| 2488 |
} else { |
| 2489 |
const port = parseInt(endpoint, 10); |
| 2490 |
if (!isNaN(port)) |
| 2491 |
options.port = port; |
| 2492 |
else |
| 2493 |
throw Error("The endpoint must be either a unix path or port number"); |
| 2494 |
} |
| 2495 |
} |
| 2496 |
|
| 2497 |
if (options.address) { |
| 2498 |
if (!options.capabilities) |
| 2499 |
options.capabilities = []; |
| 2500 |
options.capabilities.push("address"); |
| 2501 |
} |
| 2502 |
|
| 2503 |
function param(obj) { |
| 2504 |
return Object.keys(obj).map(function(k) { |
| 2505 |
return encodeURIComponent(k) + '=' + encodeURIComponent(obj[k]); |
| 2506 |
}) |
| 2507 |
.join('&') |
| 2508 |
.split('%20') |
| 2509 |
.join('+'); /* split/join because phantomjs */ |
| 2510 |
} |
| 2511 |
|
| 2512 |
self.request = function request(req) { |
| 2513 |
const dfd = cockpit.defer(); |
| 2514 |
const ret = dfd.promise; |
| 2515 |
|
| 2516 |
if (!req.path) |
| 2517 |
req.path = "/"; |
| 2518 |
if (!req.method) |
| 2519 |
req.method = "GET"; |
| 2520 |
if (req.params) { |
| 2521 |
if (req.path.indexOf("?") === -1) |
| 2522 |
req.path += "?" + param(req.params); |
| 2523 |
else |
| 2524 |
req.path += "&" + param(req.params); |
| 2525 |
} |
| 2526 |
delete req.params; |
| 2527 |
|
| 2528 |
const input = req.body; |
| 2529 |
delete req.body; |
| 2530 |
|
| 2531 |
const headers = req.headers; |
| 2532 |
delete req.headers; |
| 2533 |
|
| 2534 |
Object.assign(req, options); |
| 2535 |
|
| 2536 |
/* Combine the headers */ |
| 2537 |
if (options.headers && headers) |
| 2538 |
req.headers = { ...options.headers, ...headers }; |
| 2539 |
else if (options.headers) |
| 2540 |
req.headers = options.headers; |
| 2541 |
else |
| 2542 |
req.headers = headers; |
| 2543 |
|
| 2544 |
http_debug("http request:", JSON.stringify(req)); |
| 2545 |
|
| 2546 |
/* We need a channel for the request */ |
| 2547 |
const channel = cockpit.channel(req); |
| 2548 |
|
| 2549 |
if (input !== undefined) { |
| 2550 |
if (input !== "") { |
| 2551 |
http_debug("http input:", input); |
| 2552 |
iterate_data(input, function(data) { |
| 2553 |
channel.send(data); |
| 2554 |
}); |
| 2555 |
} |
| 2556 |
http_debug("http", req.method, req.path, "request sent, channel done"); |
| 2557 |
channel.control({ command: "done" }); |
| 2558 |
} |
| 2559 |
|
| 2560 |
/* Callbacks that want to stream or get headers */ |
| 2561 |
let streamer = null; |
| 2562 |
let responders = null; |
| 2563 |
|
| 2564 |
let resp = null; |
| 2565 |
|
| 2566 |
const buffer = channel.buffer(function(data) { |
| 2567 |
/* Fire any streamers */ |
| 2568 |
if (resp && resp.status >= 200 && resp.status <= 299 && streamer) |
| 2569 |
return streamer.call(ret, data); |
| 2570 |
return 0; |
| 2571 |
}); |
| 2572 |
|
| 2573 |
function on_control(event, options) { |
| 2574 |
/* Anyone looking for response details? */ |
| 2575 |
if (options.command == "response") { |
| 2576 |
resp = options; |
| 2577 |
if (responders) { |
| 2578 |
resp.headers = resp.headers || { }; |
| 2579 |
invoke_functions(responders, ret, [resp.status, resp.headers]); |
| 2580 |
} |
| 2581 |
} |
| 2582 |
} |
| 2583 |
|
| 2584 |
function on_close(event, options) { |
| 2585 |
const pos = active_requests.indexOf(ret); |
| 2586 |
if (pos >= 0) |
| 2587 |
active_requests.splice(pos, 1); |
| 2588 |
|
| 2589 |
if (options.problem) { |
| 2590 |
http_debug("http problem: ", options.problem); |
| 2591 |
dfd.reject(new BasicError(options.problem, options.message)); |
| 2592 |
} else { |
| 2593 |
const body = buffer.squash(); |
| 2594 |
|
| 2595 |
/* An error, fail here */ |
| 2596 |
if (resp && (resp.status < 200 || resp.status > 299)) { |
| 2597 |
let message; |
| 2598 |
const type = find_header(resp.headers, "Content-Type"); |
| 2599 |
if (type && !channel.binary) { |
| 2600 |
if (type.indexOf("text/plain") === 0) |
| 2601 |
message = body; |
| 2602 |
} |
| 2603 |
http_debug("http", req.method, req.path, "failed:", resp.status, resp.reason); |
| 2604 |
dfd.reject(new HttpError(resp.status, resp.reason, message), body); |
| 2605 |
} else { |
| 2606 |
if (resp) |
| 2607 |
http_debug("http", req.method, req.path, "succeeded:", resp.status); |
| 2608 |
else |
| 2609 |
http_debug("http", req.method, req.path, "failed without response"); |
| 2610 |
dfd.resolve(body); |
| 2611 |
} |
| 2612 |
} |
| 2613 |
|
| 2614 |
channel.removeEventListener("control", on_control); |
| 2615 |
channel.removeEventListener("close", on_close); |
| 2616 |
} |
| 2617 |
|
| 2618 |
channel.addEventListener("control", on_control); |
| 2619 |
channel.addEventListener("close", on_close); |
| 2620 |
|
| 2621 |
ret.stream = function(callback) { |
| 2622 |
streamer = callback; |
| 2623 |
return ret; |
| 2624 |
}; |
| 2625 |
ret.response = function(callback) { |
| 2626 |
if (responders === null) |
| 2627 |
responders = []; |
| 2628 |
responders.push(callback); |
| 2629 |
return ret; |
| 2630 |
}; |
| 2631 |
ret.input = function(message, stream) { |
| 2632 |
if (message !== null && message !== undefined) { |
| 2633 |
http_debug("http input:", message); |
| 2634 |
iterate_data(message, function(data) { |
| 2635 |
channel.send(data); |
| 2636 |
}); |
| 2637 |
} |
| 2638 |
if (!stream) { |
| 2639 |
http_debug("http done"); |
| 2640 |
channel.control({ command: "done" }); |
| 2641 |
} |
| 2642 |
return ret; |
| 2643 |
}; |
| 2644 |
ret.close = function(problem) { |
| 2645 |
http_debug("http closing:", problem); |
| 2646 |
channel.close(problem); |
| 2647 |
return ret; |
| 2648 |
}; |
| 2649 |
|
| 2650 |
active_requests.push(ret); |
| 2651 |
return ret; |
| 2652 |
}; |
| 2653 |
|
| 2654 |
self.get = function get(path, params, headers) { |
| 2655 |
return self.request({ |
| 2656 |
method: "GET", |
| 2657 |
params, |
| 2658 |
path, |
| 2659 |
body: "", |
| 2660 |
headers |
| 2661 |
}); |
| 2662 |
}; |
| 2663 |
|
| 2664 |
self.post = function post(path, body, headers) { |
| 2665 |
headers = headers || { }; |
| 2666 |
|
| 2667 |
if (is_plain_object(body) || Array.isArray(body)) { |
| 2668 |
body = JSON.stringify(body); |
| 2669 |
if (find_header(headers, "Content-Type") === undefined) |
| 2670 |
headers["Content-Type"] = "application/json"; |
| 2671 |
} else if (body === undefined || body === null) { |
| 2672 |
body = ""; |
| 2673 |
} else if (typeof body !== "string") { |
| 2674 |
body = String(body); |
| 2675 |
} |
| 2676 |
|
| 2677 |
return self.request({ |
| 2678 |
method: "POST", |
| 2679 |
path, |
| 2680 |
body, |
| 2681 |
headers |
| 2682 |
}); |
| 2683 |
}; |
| 2684 |
|
| 2685 |
self.close = function close(problem) { |
| 2686 |
const reqs = active_requests.slice(); |
| 2687 |
for (let i = 0; i < reqs.length; i++) |
| 2688 |
reqs[i].close(problem); |
| 2689 |
}; |
| 2690 |
} |
| 2691 |
|
| 2692 |
/* public */ |
| 2693 |
cockpit.http = function(endpoint, options) { |
| 2694 |
if (is_plain_object(endpoint) && options === undefined) { |
| 2695 |
options = endpoint; |
| 2696 |
endpoint = undefined; |
| 2697 |
} |
| 2698 |
return new HttpClient(endpoint, options || { }); |
| 2699 |
}; |
| 2700 |
|
| 2701 |
/* --------------------------------------------------------------------- |
| 2702 |
* Permission |
| 2703 |
*/ |
| 2704 |
|
| 2705 |
function check_superuser() { |
| 2706 |
return new Promise((resolve, reject) => { |
| 2707 |
const ch = cockpit.channel({ payload: "null", superuser: "require" }); |
| 2708 |
ch.wait() |
| 2709 |
.then(() => resolve(true)) |
| 2710 |
.catch(() => resolve(false)) |
| 2711 |
.always(() => ch.close()); |
| 2712 |
}); |
| 2713 |
} |
| 2714 |
|
| 2715 |
function Permission(options) { |
| 2716 |
const self = this; |
| 2717 |
event_mixin(self, { }); |
| 2718 |
|
| 2719 |
const api = cockpit.dbus(null, { bus: "internal" }).proxy("cockpit.Superuser", "/superuser"); |
| 2720 |
api.addEventListener("changed", maybe_reload); |
| 2721 |
|
| 2722 |
function maybe_reload() { |
| 2723 |
if (api.valid && self.allowed !== null) { |
| 2724 |
if (self.allowed != (api.Current != "none")) |
| 2725 |
window.location.reload(true); |
| 2726 |
} |
| 2727 |
} |
| 2728 |
|
| 2729 |
self.allowed = null; |
| 2730 |
self.user = options ? options.user : null; // pre-fill for unit tests |
| 2731 |
self.is_superuser = options ? options._is_superuser : null; // pre-fill for unit tests |
| 2732 |
|
| 2733 |
let group = null; |
| 2734 |
let admin = false; |
| 2735 |
|
| 2736 |
if (options) |
| 2737 |
group = options.group; |
| 2738 |
|
| 2739 |
if (options?.admin) |
| 2740 |
admin = true; |
| 2741 |
|
| 2742 |
function decide(user) { |
| 2743 |
if (user.id === 0) |
| 2744 |
return true; |
| 2745 |
|
| 2746 |
if (group) |
| 2747 |
return !!(user.groups || []).includes(group); |
| 2748 |
|
| 2749 |
if (admin) |
| 2750 |
return self.is_superuser; |
| 2751 |
|
| 2752 |
if (user.id === undefined) |
| 2753 |
return null; |
| 2754 |
|
| 2755 |
return false; |
| 2756 |
} |
| 2757 |
|
| 2758 |
if (self.user && self.is_superuser !== null) { |
| 2759 |
self.allowed = decide(self.user); |
| 2760 |
} else { |
| 2761 |
Promise.all([cockpit.user(), check_superuser()]) |
| 2762 |
.then(([user, is_superuser]) => { |
| 2763 |
self.user = user; |
| 2764 |
self.is_superuser = is_superuser; |
| 2765 |
const allowed = decide(user); |
| 2766 |
if (self.allowed !== allowed) { |
| 2767 |
self.allowed = allowed; |
| 2768 |
maybe_reload(); |
| 2769 |
self.dispatchEvent("changed"); |
| 2770 |
} |
| 2771 |
}); |
| 2772 |
} |
| 2773 |
|
| 2774 |
self.close = function close() { |
| 2775 |
/* no-op for now */ |
| 2776 |
}; |
| 2777 |
} |
| 2778 |
|
| 2779 |
cockpit.permission = function permission(arg) { |
| 2780 |
return new Permission(arg); |
| 2781 |
}; |
| 2782 |
|
| 2783 |
/* --------------------------------------------------------------------- |
| 2784 |
* Metrics |
| 2785 |
* |
| 2786 |
*/ |
| 2787 |
|
| 2788 |
function MetricsChannel(interval, options_list, cache) { |
| 2789 |
const self = this; |
| 2790 |
event_mixin(self, { }); |
| 2791 |
|
| 2792 |
if (options_list.length === undefined) |
| 2793 |
options_list = [options_list]; |
| 2794 |
|
| 2795 |
const channels = []; |
| 2796 |
let following = false; |
| 2797 |
|
| 2798 |
self.series = cockpit.series(interval, cache, fetch_for_series); |
| 2799 |
self.archives = null; |
| 2800 |
self.meta = null; |
| 2801 |
|
| 2802 |
function fetch_for_series(beg, end, for_walking) { |
| 2803 |
if (!for_walking) |
| 2804 |
self.fetch(beg, end); |
| 2805 |
else |
| 2806 |
self.follow(); |
| 2807 |
} |
| 2808 |
|
| 2809 |
function transfer(options_list, callback, is_archive) { |
| 2810 |
if (options_list.length === 0) |
| 2811 |
return; |
| 2812 |
|
| 2813 |
if (!is_archive) { |
| 2814 |
if (following) |
| 2815 |
return; |
| 2816 |
following = true; |
| 2817 |
} |
| 2818 |
|
| 2819 |
const options = { |
| 2820 |
payload: "metrics1", |
| 2821 |
interval, |
| 2822 |
source: "internal", |
| 2823 |
...options_list[0] |
| 2824 |
}; |
| 2825 |
|
| 2826 |
delete options.archive_source; |
| 2827 |
|
| 2828 |
const channel = cockpit.channel(options); |
| 2829 |
channels.push(channel); |
| 2830 |
|
| 2831 |
let meta = null; |
| 2832 |
let last = null; |
| 2833 |
let beg; |
| 2834 |
|
| 2835 |
channel.addEventListener("close", function(ev, close_options) { |
| 2836 |
if (!is_archive) |
| 2837 |
following = false; |
| 2838 |
|
| 2839 |
if (options_list.length > 1 && |
| 2840 |
(close_options.problem == "not-supported" || close_options.problem == "not-found")) { |
| 2841 |
transfer(options_list.slice(1), callback); |
| 2842 |
} else if (close_options.problem) { |
| 2843 |
if (close_options.problem != "terminated" && |
| 2844 |
close_options.problem != "disconnected" && |
| 2845 |
close_options.problem != "authentication-failed" && |
| 2846 |
(close_options.problem != "not-found" || !is_archive) && |
| 2847 |
(close_options.problem != "not-supported" || !is_archive)) { |
| 2848 |
console.warn("metrics channel failed: " + close_options.problem); |
| 2849 |
} |
| 2850 |
} else if (is_archive) { |
| 2851 |
if (!self.archives) { |
| 2852 |
self.archives = true; |
| 2853 |
self.dispatchEvent('changed'); |
| 2854 |
} |
| 2855 |
} |
| 2856 |
}); |
| 2857 |
|
| 2858 |
channel.addEventListener("message", function(ev, payload) { |
| 2859 |
const message = JSON.parse(payload); |
| 2860 |
|
| 2861 |
/* A meta message? */ |
| 2862 |
const message_len = message.length; |
| 2863 |
if (message_len === undefined) { |
| 2864 |
meta = message; |
| 2865 |
let timestamp = 0; |
| 2866 |
if (meta.now && meta.timestamp) |
| 2867 |
timestamp = meta.timestamp + (Date.now() - meta.now); |
| 2868 |
beg = Math.floor(timestamp / interval); |
| 2869 |
callback(beg, meta, null, options_list[0]); |
| 2870 |
|
| 2871 |
/* Trigger to outside interest that meta changed */ |
| 2872 |
self.meta = meta; |
| 2873 |
self.dispatchEvent('changed'); |
| 2874 |
|
| 2875 |
/* A data message */ |
| 2876 |
} else if (meta) { |
| 2877 |
/* Data decompression */ |
| 2878 |
for (let i = 0; i < message_len; i++) { |
| 2879 |
const data = message[i]; |
| 2880 |
if (last) { |
| 2881 |
for (let j = 0; j < last.length; j++) { |
| 2882 |
const dataj = data[j]; |
| 2883 |
if (dataj === null || dataj === undefined) { |
| 2884 |
data[j] = last[j]; |
| 2885 |
} else { |
| 2886 |
const dataj_len = dataj.length; |
| 2887 |
if (dataj_len !== undefined) { |
| 2888 |
const lastj = last[j]; |
| 2889 |
const lastj_len = last[j].length; |
| 2890 |
let k; |
| 2891 |
for (k = 0; k < dataj_len; k++) { |
| 2892 |
if (dataj[k] === null) |
| 2893 |
dataj[k] = lastj[k]; |
| 2894 |
} |
| 2895 |
for (; k < lastj_len; k++) |
| 2896 |
dataj[k] = lastj[k]; |
| 2897 |
} |
| 2898 |
} |
| 2899 |
} |
| 2900 |
} |
| 2901 |
last = data; |
| 2902 |
} |
| 2903 |
|
| 2904 |
/* Return the data */ |
| 2905 |
callback(beg, meta, message, options_list[0]); |
| 2906 |
|
| 2907 |
/* Bump timestamp for the next message */ |
| 2908 |
beg += message_len; |
| 2909 |
meta.timestamp += (interval * message_len); |
| 2910 |
} |
| 2911 |
}); |
| 2912 |
} |
| 2913 |
|
| 2914 |
function drain(beg, meta, message, options) { |
| 2915 |
/* Generate a mapping object if necessary */ |
| 2916 |
let mapping = meta.mapping; |
| 2917 |
if (!mapping) { |
| 2918 |
mapping = { }; |
| 2919 |
meta.metrics.forEach(function(metric, i) { |
| 2920 |
const map = { "": i }; |
| 2921 |
const name = options.metrics_path_names?.[i] ?? metric.name; |
| 2922 |
mapping[name] = map; |
| 2923 |
if (metric.instances) { |
| 2924 |
metric.instances.forEach(function(instance, i) { |
| 2925 |
if (instance === "") |
| 2926 |
instance = "/"; |
| 2927 |
map[instance] = { "": i }; |
| 2928 |
}); |
| 2929 |
} |
| 2930 |
}); |
| 2931 |
meta.mapping = mapping; |
| 2932 |
} |
| 2933 |
|
| 2934 |
if (message) |
| 2935 |
self.series.input(beg, message, mapping); |
| 2936 |
} |
| 2937 |
|
| 2938 |
self.fetch = function fetch(beg, end) { |
| 2939 |
const timestamp = beg * interval - Date.now(); |
| 2940 |
const limit = end - beg; |
| 2941 |
|
| 2942 |
const archive_options_list = []; |
| 2943 |
for (let i = 0; i < options_list.length; i++) { |
| 2944 |
if (options_list[i].archive_source) { |
| 2945 |
archive_options_list.push({ |
| 2946 |
...options_list[i], |
| 2947 |
source: options_list[i].archive_source, |
| 2948 |
timestamp, |
| 2949 |
limit |
| 2950 |
}); |
| 2951 |
} |
| 2952 |
} |
| 2953 |
|
| 2954 |
transfer(archive_options_list, drain, true); |
| 2955 |
}; |
| 2956 |
|
| 2957 |
self.follow = function follow() { |
| 2958 |
transfer(options_list, drain); |
| 2959 |
}; |
| 2960 |
|
| 2961 |
self.close = function close(options) { |
| 2962 |
const len = channels.length; |
| 2963 |
if (self.series) |
| 2964 |
self.series.close(); |
| 2965 |
|
| 2966 |
for (let i = 0; i < len; i++) |
| 2967 |
channels[i].close(options); |
| 2968 |
}; |
| 2969 |
} |
| 2970 |
|
| 2971 |
cockpit.metrics = function metrics(interval, options) { |
| 2972 |
return new MetricsChannel(interval, options); |
| 2973 |
}; |
| 2974 |
|
| 2975 |
/* --------------------------------------------------------------------- |
| 2976 |
* Ooops handling. |
| 2977 |
* |
| 2978 |
* If we're embedded, send oops to parent frame. Since everything |
| 2979 |
* could be broken at this point, just do it manually, without |
| 2980 |
* involving cockpit.transport or any of that logic. |
| 2981 |
*/ |
| 2982 |
|
| 2983 |
cockpit.oops = function oops() { |
| 2984 |
if (window.parent !== window && window.name.indexOf("cockpit1:") === 0) |
| 2985 |
window.parent.postMessage("\n{ \"command\": \"oops\" }", transport_origin); |
| 2986 |
}; |
| 2987 |
|
| 2988 |
const old_onerror = window.onerror; |
| 2989 |
window.onerror = function(msg, url, line) { |
| 2990 |
// Errors with url == "" are not logged apparently, so let's |
| 2991 |
// not show the "Oops" for them either. |
| 2992 |
if (url != "") |
| 2993 |
cockpit.oops(); |
| 2994 |
if (old_onerror) |
| 2995 |
return old_onerror(msg, url, line); |
| 2996 |
return false; |
| 2997 |
}; |
| 2998 |
|
| 2999 |
cockpit.assert = (predicate, message) => { |
| 3000 |
if (!predicate) { |
| 3001 |
throw new Error(`Assertion failed: ${message}`); |
| 3002 |
} |
| 3003 |
}; |
| 3004 |
|
| 3005 |
return cockpit; |
| 3006 |
} |
| 3007 |
|
| 3008 |
const cockpit = factory(); |
| 3009 |
export default cockpit; |
| 3010 |
|
| 3011 |
// Register cockpit object as global, so that it can be used without ES6 modules |
| 3012 |
// we need to do that here instead of in pkg/base1/cockpit.js, so that po.js can access cockpit already |
| 3013 |
window.cockpit = cockpit; |