@hydranium/protocol 1.0.0-next.22 → 1.0.0-next.220
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +39 -3
- package/lib/abstract-logger.d.ts +5 -0
- package/lib/abstract-logger.d.ts.map +1 -1
- package/lib/abstract-logger.js +7 -0
- package/lib/abstract-logger.js.map +1 -1
- package/lib/client/data-connection.d.ts +157 -0
- package/lib/client/data-connection.d.ts.map +1 -0
- package/lib/client/data-connection.js +237 -0
- package/lib/client/data-connection.js.map +1 -0
- package/lib/client/data-events.d.ts +13 -1
- package/lib/client/data-events.d.ts.map +1 -1
- package/lib/client/data-events.js +21 -0
- package/lib/client/data-events.js.map +1 -1
- package/lib/client/data-port.d.ts +24 -22
- package/lib/client/data-port.d.ts.map +1 -1
- package/lib/client/data-session.d.ts +471 -81
- package/lib/client/data-session.d.ts.map +1 -1
- package/lib/client/data-session.js +738 -108
- package/lib/client/data-session.js.map +1 -1
- package/lib/client/index.d.ts +14 -9
- package/lib/client/index.d.ts.map +1 -1
- package/lib/client/index.js +14 -9
- package/lib/client/index.js.map +1 -1
- package/lib/client/message-relay.d.ts +9 -3
- package/lib/client/message-relay.d.ts.map +1 -1
- package/lib/client/message-relay.js +11 -5
- package/lib/client/message-relay.js.map +1 -1
- package/lib/client/post-message-transport.d.ts +64 -3
- package/lib/client/post-message-transport.d.ts.map +1 -1
- package/lib/client/post-message-transport.js +175 -1
- package/lib/client/post-message-transport.js.map +1 -1
- package/lib/client/rpc-connection.d.ts +149 -0
- package/lib/client/rpc-connection.d.ts.map +1 -0
- package/lib/client/rpc-connection.js +202 -0
- package/lib/client/rpc-connection.js.map +1 -0
- package/lib/client-ids.d.ts +45 -0
- package/lib/client-ids.d.ts.map +1 -0
- package/lib/client-ids.js +48 -0
- package/lib/client-ids.js.map +1 -0
- package/lib/clock.d.ts +38 -0
- package/lib/clock.d.ts.map +1 -1
- package/lib/clock.js +36 -1
- package/lib/clock.js.map +1 -1
- package/lib/console-logger.d.ts +23 -0
- package/lib/console-logger.d.ts.map +1 -0
- package/lib/console-logger.js +39 -0
- package/lib/console-logger.js.map +1 -0
- package/lib/data/data-protocol-methods.d.ts +4 -4
- package/lib/data/data-protocol-methods.d.ts.map +1 -1
- package/lib/data/data-protocol-methods.js +12 -1
- package/lib/data/data-protocol-methods.js.map +1 -1
- package/lib/data/data-server-protocol.d.ts +132 -41
- package/lib/data/data-server-protocol.d.ts.map +1 -1
- package/lib/data/events.d.ts +117 -21
- package/lib/data/events.d.ts.map +1 -1
- package/lib/data/requests.d.ts +69 -11
- package/lib/data/requests.d.ts.map +1 -1
- package/lib/debouncer.d.ts.map +1 -1
- package/lib/debouncer.js.map +1 -1
- package/lib/errors.d.ts +187 -29
- package/lib/errors.d.ts.map +1 -1
- package/lib/errors.js +270 -29
- package/lib/errors.js.map +1 -1
- package/lib/glsp-request-model-args.d.ts +16 -0
- package/lib/glsp-request-model-args.d.ts.map +1 -0
- package/lib/glsp-request-model-args.js +19 -0
- package/lib/glsp-request-model-args.js.map +1 -0
- package/lib/glsp-save-model-actions.d.ts +50 -0
- package/lib/glsp-save-model-actions.d.ts.map +1 -0
- package/lib/glsp-save-model-actions.js +28 -0
- package/lib/glsp-save-model-actions.js.map +1 -0
- package/lib/index.d.ts +7 -0
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +10 -0
- package/lib/index.js.map +1 -1
- package/lib/latency-collector.d.ts +8 -4
- package/lib/latency-collector.d.ts.map +1 -1
- package/lib/latency-collector.js.map +1 -1
- package/lib/logger.d.ts +22 -1
- package/lib/logger.d.ts.map +1 -1
- package/lib/logger.js +31 -3
- package/lib/logger.js.map +1 -1
- package/lib/messages/index.d.ts +29 -0
- package/lib/messages/index.d.ts.map +1 -0
- package/lib/messages/index.js +59 -0
- package/lib/messages/index.js.map +1 -0
- package/lib/messages/primitives.d.ts +188 -0
- package/lib/messages/primitives.d.ts.map +1 -0
- package/lib/messages/primitives.js +161 -0
- package/lib/messages/primitives.js.map +1 -0
- package/lib/model-server.d.ts +60 -13
- package/lib/model-server.d.ts.map +1 -1
- package/lib/model-server.js +4 -2
- package/lib/model-server.js.map +1 -1
- package/lib/model-service/base-version.d.ts +64 -0
- package/lib/model-service/base-version.d.ts.map +1 -0
- package/lib/model-service/base-version.js +43 -0
- package/lib/model-service/base-version.js.map +1 -0
- package/lib/model-service/index.d.ts +1 -1
- package/lib/model-service/index.d.ts.map +1 -1
- package/lib/model-service/index.js +4 -5
- package/lib/model-service/index.js.map +1 -1
- package/lib/model-service/reference-candidate.d.ts +5 -3
- package/lib/model-service/reference-candidate.d.ts.map +1 -1
- package/lib/{model-service/args.js → node/index.d.ts} +2 -3
- package/lib/node/index.d.ts.map +1 -0
- package/lib/node/index.js +29 -0
- package/lib/node/index.js.map +1 -0
- package/lib/node/process-memory.d.ts +66 -0
- package/lib/node/process-memory.d.ts.map +1 -0
- package/lib/node/process-memory.js +291 -0
- package/lib/node/process-memory.js.map +1 -0
- package/lib/noop-logger.d.ts.map +1 -1
- package/lib/noop-logger.js.map +1 -1
- package/lib/observable-value.js.map +1 -1
- package/lib/patch-merge.d.ts +35 -32
- package/lib/patch-merge.d.ts.map +1 -1
- package/lib/patch-merge.js +67 -23
- package/lib/patch-merge.js.map +1 -1
- package/lib/profile-session.d.ts +8 -4
- package/lib/profile-session.d.ts.map +1 -1
- package/lib/profile-session.js.map +1 -1
- package/lib/random-uuid.d.ts +14 -0
- package/lib/random-uuid.d.ts.map +1 -0
- package/lib/random-uuid.js +24 -0
- package/lib/random-uuid.js.map +1 -0
- package/lib/reconcile-write.d.ts +65 -0
- package/lib/reconcile-write.d.ts.map +1 -0
- package/lib/reconcile-write.js +67 -0
- package/lib/reconcile-write.js.map +1 -0
- package/lib/rpc/bind-rpc-methods.d.ts +33 -3
- package/lib/rpc/bind-rpc-methods.d.ts.map +1 -1
- package/lib/rpc/bind-rpc-methods.js +32 -3
- package/lib/rpc/bind-rpc-methods.js.map +1 -1
- package/lib/rpc/create-rpc-proxy.d.ts +10 -0
- package/lib/rpc/create-rpc-proxy.d.ts.map +1 -1
- package/lib/rpc/create-rpc-proxy.js +12 -2
- package/lib/rpc/create-rpc-proxy.js.map +1 -1
- package/lib/rpc/index.d.ts +1 -0
- package/lib/rpc/index.d.ts.map +1 -1
- package/lib/rpc/index.js +1 -0
- package/lib/rpc/index.js.map +1 -1
- package/lib/rpc/send-by-method-name.d.ts +76 -0
- package/lib/rpc/send-by-method-name.d.ts.map +1 -0
- package/lib/rpc/send-by-method-name.js +120 -0
- package/lib/rpc/send-by-method-name.js.map +1 -0
- package/lib/rpc/wire-prefix.js.map +1 -1
- package/lib/testing/catalogue-audit.d.ts +80 -0
- package/lib/testing/catalogue-audit.d.ts.map +1 -0
- package/lib/testing/catalogue-audit.js +94 -0
- package/lib/testing/catalogue-audit.js.map +1 -0
- package/lib/testing/data-doubles.d.ts +39 -15
- package/lib/testing/data-doubles.d.ts.map +1 -1
- package/lib/testing/data-doubles.js +57 -10
- package/lib/testing/data-doubles.js.map +1 -1
- package/lib/testing/fake-clock.d.ts +9 -1
- package/lib/testing/fake-clock.d.ts.map +1 -1
- package/lib/testing/fake-clock.js +54 -45
- package/lib/testing/fake-clock.js.map +1 -1
- package/lib/testing/index.d.ts +1 -0
- package/lib/testing/index.d.ts.map +1 -1
- package/lib/testing/index.js +5 -2
- package/lib/testing/index.js.map +1 -1
- package/lib/testing/node/duplex-connection.d.ts.map +1 -1
- package/lib/testing/node/duplex-connection.js +3 -2
- package/lib/testing/node/duplex-connection.js.map +1 -1
- package/lib/testing/node/duplex-stream.js.map +1 -1
- package/lib/testing/node/index.d.ts +1 -0
- package/lib/testing/node/index.d.ts.map +1 -1
- package/lib/testing/node/index.js +2 -2
- package/lib/testing/node/index.js.map +1 -1
- package/lib/testing/node/message-port-pair.d.ts +25 -0
- package/lib/testing/node/message-port-pair.d.ts.map +1 -0
- package/lib/testing/node/message-port-pair.js +26 -0
- package/lib/testing/node/message-port-pair.js.map +1 -0
- package/lib/testing/wait-for.js.map +1 -1
- package/lib/tracer.d.ts.map +1 -1
- package/lib/tracer.js.map +1 -1
- package/lib/transfer-diagnostic.d.ts +33 -0
- package/lib/transfer-diagnostic.d.ts.map +1 -1
- package/lib/transfer-diagnostic.js +23 -0
- package/lib/transfer-diagnostic.js.map +1 -1
- package/lib/transfer-document.d.ts +70 -32
- package/lib/transfer-document.d.ts.map +1 -1
- package/lib/transfer-document.js +17 -9
- package/lib/transfer-document.js.map +1 -1
- package/lib/uri.d.ts.map +1 -1
- package/lib/uri.js.map +1 -1
- package/lib/util.d.ts +8 -0
- package/lib/util.d.ts.map +1 -1
- package/lib/util.js +32 -0
- package/lib/util.js.map +1 -1
- package/package.json +29 -37
- package/src/abstract-logger.ts +8 -0
- package/src/client/data-connection.ts +315 -0
- package/src/client/data-events.ts +33 -1
- package/src/client/data-port.ts +25 -23
- package/src/client/data-session.ts +946 -126
- package/src/client/index.ts +14 -9
- package/src/client/message-relay.ts +29 -7
- package/src/client/post-message-transport.ts +219 -4
- package/src/client/rpc-connection.ts +268 -0
- package/src/client-ids.ts +49 -0
- package/src/clock.ts +56 -0
- package/src/console-logger.ts +39 -0
- package/src/data/data-protocol-methods.ts +13 -4
- package/src/data/data-server-protocol.ts +157 -41
- package/src/data/events.ts +123 -21
- package/src/data/requests.ts +74 -11
- package/src/errors.ts +322 -36
- package/src/glsp-request-model-args.ts +16 -0
- package/src/glsp-save-model-actions.ts +59 -0
- package/src/index.ts +10 -0
- package/src/latency-collector.ts +8 -3
- package/src/logger.ts +28 -2
- package/src/messages/index.ts +36 -0
- package/src/messages/primitives.ts +271 -0
- package/src/model-server.ts +63 -18
- package/src/model-service/base-version.ts +72 -0
- package/src/model-service/index.ts +4 -5
- package/src/model-service/reference-candidate.ts +5 -3
- package/src/node/index.ts +14 -0
- package/src/node/process-memory.ts +299 -0
- package/src/patch-merge.ts +97 -42
- package/src/profile-session.ts +9 -4
- package/src/random-uuid.ts +21 -0
- package/src/reconcile-write.ts +124 -0
- package/src/rpc/README.md +2 -3
- package/src/rpc/bind-rpc-methods.ts +59 -4
- package/src/rpc/create-rpc-proxy.ts +20 -2
- package/src/rpc/index.ts +1 -0
- package/src/rpc/send-by-method-name.ts +140 -0
- package/src/testing/catalogue-audit.ts +111 -0
- package/src/testing/data-doubles.ts +141 -25
- package/src/testing/fake-clock.ts +62 -47
- package/src/testing/index.ts +5 -2
- package/src/testing/node/duplex-connection.ts +3 -2
- package/src/testing/node/index.ts +2 -2
- package/src/testing/node/message-port-pair.ts +40 -0
- package/src/transfer-diagnostic.ts +40 -0
- package/src/transfer-document.ts +87 -34
- package/src/util.ts +33 -0
- package/lib/model-service/args.d.ts +0 -64
- package/lib/model-service/args.d.ts.map +0 -1
- package/lib/model-service/args.js.map +0 -1
- package/src/model-service/args.ts +0 -67
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
/********************************************************************************
|
|
2
|
+
* Copyright (c) 2026 CrossBreeze, EclipseSource and others.
|
|
3
|
+
*
|
|
4
|
+
* This program and the accompanying materials are made available under the
|
|
5
|
+
* terms of the MIT License which is available in the project root.
|
|
6
|
+
*
|
|
7
|
+
* SPDX-License-Identifier: MIT
|
|
8
|
+
********************************************************************************/
|
|
9
|
+
|
|
10
|
+
/*
|
|
11
|
+
* Node-only process- and pod-level memory introspection plus heap-snapshot
|
|
12
|
+
* writing. Everything here is generic OS-level code (V8 stats, cgroup v2/v1,
|
|
13
|
+
* `/proc`); the only host-specific part is naming the processes, which a head
|
|
14
|
+
* supplies through a `ProcessClassifier`.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import * as fs from 'node:fs';
|
|
18
|
+
import * as os from 'node:os';
|
|
19
|
+
import * as path from 'node:path';
|
|
20
|
+
import * as v8 from 'node:v8';
|
|
21
|
+
import { Format } from '../logger';
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Format a one-shot memory snapshot of the CURRENT process (heap, rss, external, V8 limit).
|
|
25
|
+
* Suited to a process that hosts no Langium documents (e.g. a host backend); for the
|
|
26
|
+
* language-server process with document counts, see `formatServerState`.
|
|
27
|
+
*/
|
|
28
|
+
export function formatProcessMemory(label: string): string {
|
|
29
|
+
const mem = process.memoryUsage();
|
|
30
|
+
const heap = v8.getHeapStatistics();
|
|
31
|
+
const heapPercent = Math.round((mem.heapUsed / heap.heap_size_limit) * 100);
|
|
32
|
+
return [
|
|
33
|
+
`${label}:`,
|
|
34
|
+
` heap ${Format.bytes(mem.heapUsed)} used / ${Format.bytes(mem.heapTotal)} total / ` +
|
|
35
|
+
`${Format.bytes(heap.heap_size_limit)} limit (${heapPercent}% of limit)`,
|
|
36
|
+
` rss ${Format.bytes(mem.rss)}, external ${Format.bytes(mem.external)}, arrayBuffers ${Format.bytes(mem.arrayBuffers)}`,
|
|
37
|
+
` uptime ${Math.round(process.uptime())}s, pid ${process.pid}`
|
|
38
|
+
].join('\n');
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Compose an absolute artifact file path of the form `<prefix>[-<label>].<ext>` inside
|
|
43
|
+
* {@link directory} (e.g. the workspace folder, so it lands on the persistent volume in cloud and
|
|
44
|
+
* is visible in the file explorer), falling back to the OS temp dir when the directory is missing
|
|
45
|
+
* or does not exist. The {@link prefix} carries the origin-first stem (e.g. `server-cpu`) and, when
|
|
46
|
+
* a non-empty {@link label} is given (a named capture window), it is sanitised (non-word characters
|
|
47
|
+
* → `_`, truncated to 40 chars) and appended as `-<label>`; an empty label yields the bare
|
|
48
|
+
* `<prefix>.<ext>` so the manifest `kind` equals the filename stem. No timestamp — a fresh session
|
|
49
|
+
* directory per run keeps names stable and collision-free. Shared by the heap-snapshot writer and
|
|
50
|
+
* the sampled-profile capture so heap / CPU / allocation artefacts land identically, differing only
|
|
51
|
+
* by extension.
|
|
52
|
+
*/
|
|
53
|
+
export function snapshotFilePath(directory: string | undefined, label: string, prefix: string, ext: string): string {
|
|
54
|
+
const safeLabel = label ? label.replace(/[^\w.-]+/g, '_').slice(0, 40) : '';
|
|
55
|
+
const dir = directory && fs.existsSync(directory) ? directory : os.tmpdir();
|
|
56
|
+
const stem = safeLabel ? `${prefix}-${safeLabel}` : prefix;
|
|
57
|
+
return path.join(dir, `${stem}.${ext}`);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Write a V8 heap snapshot of the CURRENT process to {@link directory}, falling back to the OS temp
|
|
62
|
+
* dir when it is missing (see {@link snapshotFilePath}). `v8.writeHeapSnapshot` runs a full GC first and
|
|
63
|
+
* briefly pauses the process; it also transiently inflates RSS while serialising. Returns the
|
|
64
|
+
* absolute file path. The {@link prefix} (default `server-heap`, the origin-first stem) and
|
|
65
|
+
* {@link label} are folded into the name.
|
|
66
|
+
*/
|
|
67
|
+
export function writeHeapSnapshotToDir(directory: string | undefined, label: string, prefix = 'server-heap'): string {
|
|
68
|
+
const filePath = snapshotFilePath(directory, label, prefix, 'heapsnapshot');
|
|
69
|
+
v8.writeHeapSnapshot(filePath);
|
|
70
|
+
return filePath;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const CGROUP_ROOT = '/sys/fs/cgroup';
|
|
74
|
+
|
|
75
|
+
function readNumber(file: string): number | undefined {
|
|
76
|
+
try {
|
|
77
|
+
const text = fs.readFileSync(file, 'utf8').trim();
|
|
78
|
+
if (text === 'max') {
|
|
79
|
+
return Number.POSITIVE_INFINITY;
|
|
80
|
+
}
|
|
81
|
+
const value = Number(text);
|
|
82
|
+
return Number.isFinite(value) ? value : undefined;
|
|
83
|
+
} catch {
|
|
84
|
+
return undefined;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Parse a `key value` per-line file (cgroup memory.stat) into a map. */
|
|
89
|
+
function readKeyedBytes(file: string): Record<string, number> {
|
|
90
|
+
const out: Record<string, number> = {};
|
|
91
|
+
try {
|
|
92
|
+
for (const line of fs.readFileSync(file, 'utf8').split('\n')) {
|
|
93
|
+
const [key, value] = line.trim().split(/\s+/);
|
|
94
|
+
if (key && value !== undefined) {
|
|
95
|
+
const num = Number(value);
|
|
96
|
+
if (Number.isFinite(num)) {
|
|
97
|
+
out[key] = num;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
} catch {
|
|
102
|
+
// file absent or unreadable; caller handles the empty map.
|
|
103
|
+
}
|
|
104
|
+
return out;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
interface ProcInfo {
|
|
108
|
+
pid: number;
|
|
109
|
+
rssBytes: number;
|
|
110
|
+
label: string;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
interface RawProc extends ProcInfo {
|
|
114
|
+
ppid: number;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** A readable role for a process, whether it belongs to the app, and whether exactly one is expected. */
|
|
118
|
+
export interface ProcessRole {
|
|
119
|
+
/** Human-readable role shown in the snapshot. */
|
|
120
|
+
role: string;
|
|
121
|
+
/** Whether this process is part of the application (vs. tooling/debugger), counted in the app total. */
|
|
122
|
+
isApp: boolean;
|
|
123
|
+
/**
|
|
124
|
+
* Set when at most one instance of this role should exist; `formatPodMemory` warns when more than
|
|
125
|
+
* one is found (e.g. a duplicated/ghost language server).
|
|
126
|
+
*/
|
|
127
|
+
singletonExpected?: boolean;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** Map a raw command line to a {@link ProcessRole}. A head supplies this to label its own processes. */
|
|
131
|
+
export type ProcessClassifier = (cmdline: string) => ProcessRole;
|
|
132
|
+
|
|
133
|
+
/** Generic fallback: every process is shown under the app heading, labelled by its (truncated) command line. */
|
|
134
|
+
const defaultProcessClassifier: ProcessClassifier = cmdline => ({ role: cmdline ? cmdline.slice(0, 60) : 'process', isApp: true });
|
|
135
|
+
|
|
136
|
+
/** Read /proc for every visible process: pid, ppid, RSS, and a command label. */
|
|
137
|
+
function readAllProcs(): RawProc[] {
|
|
138
|
+
const procs: RawProc[] = [];
|
|
139
|
+
let entries: string[] = [];
|
|
140
|
+
try {
|
|
141
|
+
entries = fs.readdirSync('/proc');
|
|
142
|
+
} catch {
|
|
143
|
+
return procs;
|
|
144
|
+
}
|
|
145
|
+
for (const entry of entries) {
|
|
146
|
+
if (!/^\d+$/.test(entry)) {
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
let status = '';
|
|
150
|
+
try {
|
|
151
|
+
status = fs.readFileSync(`/proc/${entry}/status`, 'utf8');
|
|
152
|
+
} catch {
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
const rssMatch = status.match(/^VmRSS:\s+(\d+)\s+kB/m);
|
|
156
|
+
if (!rssMatch) {
|
|
157
|
+
continue;
|
|
158
|
+
}
|
|
159
|
+
const ppid = Number(status.match(/^PPid:\s+(\d+)/m)?.[1] ?? 0);
|
|
160
|
+
let label = '';
|
|
161
|
+
try {
|
|
162
|
+
label = fs.readFileSync(`/proc/${entry}/cmdline`, 'utf8').replace(/\0/g, ' ').trim();
|
|
163
|
+
} catch {
|
|
164
|
+
// fall back to the process name from status below.
|
|
165
|
+
}
|
|
166
|
+
if (!label) {
|
|
167
|
+
label = status.match(/^Name:\s+(.+)$/m)?.[1] ?? `pid ${entry}`;
|
|
168
|
+
}
|
|
169
|
+
procs.push({ pid: Number(entry), ppid, rssBytes: Number(rssMatch[1]) * 1024, label });
|
|
170
|
+
}
|
|
171
|
+
return procs;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Sum and list per-process RSS. When {@link rootPid} is given, restrict to that process and its
|
|
176
|
+
* descendants (the app's own process tree), which keeps the figure meaningful in local dev where
|
|
177
|
+
* /proc otherwise shows the whole machine. In a container's PID namespace the tree is effectively
|
|
178
|
+
* all the pod's processes anyway.
|
|
179
|
+
*/
|
|
180
|
+
function collectProcessRss(rootPid?: number): { total: number; processes: ProcInfo[]; scoped: boolean } {
|
|
181
|
+
const all = readAllProcs();
|
|
182
|
+
let selected: RawProc[] = all;
|
|
183
|
+
let scoped = false;
|
|
184
|
+
if (rootPid !== undefined) {
|
|
185
|
+
const childrenByPpid = new Map<number, RawProc[]>();
|
|
186
|
+
for (const proc of all) {
|
|
187
|
+
(childrenByPpid.get(proc.ppid) ?? childrenByPpid.set(proc.ppid, []).get(proc.ppid)!).push(proc);
|
|
188
|
+
}
|
|
189
|
+
const tree: RawProc[] = [];
|
|
190
|
+
const queue = all.filter(proc => proc.pid === rootPid);
|
|
191
|
+
while (queue.length > 0) {
|
|
192
|
+
const proc = queue.shift()!;
|
|
193
|
+
tree.push(proc);
|
|
194
|
+
queue.push(...(childrenByPpid.get(proc.pid) ?? []));
|
|
195
|
+
}
|
|
196
|
+
if (tree.length > 0) {
|
|
197
|
+
selected = tree;
|
|
198
|
+
scoped = true;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
const total = selected.reduce((sum, proc) => sum + proc.rssBytes, 0);
|
|
202
|
+
selected.sort((a, b) => b.rssBytes - a.rssBytes);
|
|
203
|
+
return { total, processes: selected, scoped };
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/** Options for {@link formatPodMemory}. */
|
|
207
|
+
export interface PodMemoryOptions {
|
|
208
|
+
/** Map a process command line to a readable role; lets a head label its own processes. */
|
|
209
|
+
classify?: ProcessClassifier;
|
|
210
|
+
/** Heading for the in-app process group (default `Application`). */
|
|
211
|
+
appLabel?: string;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Format a pod/container-level memory snapshot read from the cgroup this process belongs to
|
|
216
|
+
* (cgroup v2 `memory.current`/`peak`/`max`/`stat`, falling back to cgroup v1). This is the figure
|
|
217
|
+
* the Kubernetes OOM-killer watches — it covers every process in the pod plus page cache and kernel
|
|
218
|
+
* memory, so it is higher than any single `process.memoryUsage().rss`. Also sums per-process RSS so
|
|
219
|
+
* the per-process split is visible. Explains itself when no cgroup memory controller is present
|
|
220
|
+
* (e.g. local dev outside a container).
|
|
221
|
+
*/
|
|
222
|
+
export function formatPodMemory(options: PodMemoryOptions = {}): string {
|
|
223
|
+
const classify = options.classify ?? defaultProcessClassifier;
|
|
224
|
+
const appLabel = options.appLabel ?? 'Application';
|
|
225
|
+
const lines: string[] = ['Pod memory snapshot:'];
|
|
226
|
+
|
|
227
|
+
const v2Current = path.join(CGROUP_ROOT, 'memory.current');
|
|
228
|
+
const v1Usage = path.join(CGROUP_ROOT, 'memory', 'memory.usage_in_bytes');
|
|
229
|
+
|
|
230
|
+
if (fs.existsSync(v2Current)) {
|
|
231
|
+
const current = readNumber(v2Current);
|
|
232
|
+
const peak = readNumber(path.join(CGROUP_ROOT, 'memory.peak'));
|
|
233
|
+
const max = readNumber(path.join(CGROUP_ROOT, 'memory.max'));
|
|
234
|
+
const stat = readKeyedBytes(path.join(CGROUP_ROOT, 'memory.stat'));
|
|
235
|
+
lines.push(' cgroup v2');
|
|
236
|
+
lines.push(` current ${current !== undefined ? Format.bytes(current) : 'n/a'}`);
|
|
237
|
+
lines.push(` peak ${peak !== undefined ? Format.bytes(peak) : 'n/a (kernel too old for memory.peak)'}`);
|
|
238
|
+
lines.push(` limit ${max === Number.POSITIVE_INFINITY ? 'unlimited' : max !== undefined ? Format.bytes(max) : 'n/a'}`);
|
|
239
|
+
if (Object.keys(stat).length > 0) {
|
|
240
|
+
lines.push(
|
|
241
|
+
` breakdown anon ${Format.bytes(stat.anon ?? 0)} (heaps/stacks), file ${Format.bytes(stat.file ?? 0)} (page cache), ` +
|
|
242
|
+
`kernel ${Format.bytes(stat.kernel ?? stat.slab ?? 0)}, sock ${Format.bytes(stat.sock ?? 0)}`
|
|
243
|
+
);
|
|
244
|
+
}
|
|
245
|
+
} else if (fs.existsSync(v1Usage)) {
|
|
246
|
+
const v1 = path.join(CGROUP_ROOT, 'memory');
|
|
247
|
+
const current = readNumber(v1Usage);
|
|
248
|
+
const peak = readNumber(path.join(v1, 'memory.max_usage_in_bytes'));
|
|
249
|
+
const limit = readNumber(path.join(v1, 'memory.limit_in_bytes'));
|
|
250
|
+
const stat = readKeyedBytes(path.join(v1, 'memory.stat'));
|
|
251
|
+
lines.push(' cgroup v1');
|
|
252
|
+
lines.push(` current ${current !== undefined ? Format.bytes(current) : 'n/a'}`);
|
|
253
|
+
lines.push(` peak ${peak !== undefined ? Format.bytes(peak) : 'n/a'}`);
|
|
254
|
+
// v1 reports a huge sentinel for "unlimited"; treat anything >= 2^60 as unlimited.
|
|
255
|
+
lines.push(` limit ${limit !== undefined && limit < 2 ** 60 ? Format.bytes(limit) : 'unlimited'}`);
|
|
256
|
+
if (Object.keys(stat).length > 0) {
|
|
257
|
+
lines.push(
|
|
258
|
+
` breakdown rss ${Format.bytes(stat.rss ?? 0)}, cache ${Format.bytes(stat.cache ?? 0)} (page cache), ` +
|
|
259
|
+
`kernel ${Format.bytes(stat.kernel ?? 0)}`
|
|
260
|
+
);
|
|
261
|
+
}
|
|
262
|
+
} else {
|
|
263
|
+
lines.push(' cgroup no memory controller under /sys/fs/cgroup (not running in a container?)');
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
// Root at this process so local dev reports only the app's tree, not the whole machine.
|
|
267
|
+
const { processes } = collectProcessRss(process.pid);
|
|
268
|
+
if (processes.length > 0) {
|
|
269
|
+
const classified = processes.map(proc => ({ ...proc, ...classify(proc.label) }));
|
|
270
|
+
const appProcs = classified.filter(proc => proc.isApp);
|
|
271
|
+
const otherProcs = classified.filter(proc => !proc.isApp);
|
|
272
|
+
const appTotal = appProcs.reduce((sum, proc) => sum + proc.rssBytes, 0);
|
|
273
|
+
|
|
274
|
+
lines.push('');
|
|
275
|
+
lines.push(` ${appLabel}: ${Format.bytes(appTotal)} across ${appProcs.length} process(es)`);
|
|
276
|
+
for (const proc of appProcs) {
|
|
277
|
+
lines.push(` ${Format.bytes(proc.rssBytes).padStart(10)} ${proc.role} (pid ${proc.pid})`);
|
|
278
|
+
}
|
|
279
|
+
const singletonCounts = new Map<string, number>();
|
|
280
|
+
for (const proc of appProcs) {
|
|
281
|
+
if (proc.singletonExpected) {
|
|
282
|
+
singletonCounts.set(proc.role, (singletonCounts.get(proc.role) ?? 0) + 1);
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
for (const [role, count] of singletonCounts) {
|
|
286
|
+
if (count > 1) {
|
|
287
|
+
lines.push(` WARNING: ${count} instances of "${role}" running - likely ghost process(es); expected 1`);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
if (otherProcs.length > 0) {
|
|
291
|
+
const otherTotal = otherProcs.reduce((sum, proc) => sum + proc.rssBytes, 0);
|
|
292
|
+
lines.push(` Other (tooling/debugger, not counted above): ${Format.bytes(otherTotal)} across ${otherProcs.length} process(es)`);
|
|
293
|
+
}
|
|
294
|
+
lines.push(' Note: in a real pod the cgroup figure above is the OOM ceiling; this per-process');
|
|
295
|
+
lines.push(' split is for attribution. Local dev may include debugger processes (excluded here).');
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
return lines.join('\n');
|
|
299
|
+
}
|
package/src/patch-merge.ts
CHANGED
|
@@ -7,34 +7,43 @@
|
|
|
7
7
|
* SPDX-License-Identifier: MIT
|
|
8
8
|
********************************************************************************/
|
|
9
9
|
|
|
10
|
-
import {
|
|
10
|
+
import {
|
|
11
|
+
_areEquals,
|
|
12
|
+
applyPatch,
|
|
13
|
+
compare,
|
|
14
|
+
deepClone,
|
|
15
|
+
getValueByPointer,
|
|
16
|
+
type Operation as JsonPatchOperation,
|
|
17
|
+
unescapePathComponent
|
|
18
|
+
} from 'fast-json-patch';
|
|
11
19
|
|
|
12
20
|
/**
|
|
13
|
-
* Augment a user-intent JSON patch (computed `
|
|
21
|
+
* Augment a user-intent JSON patch (computed `base → ours`) with
|
|
14
22
|
* `test` ops so a strict `applyPatch` against freshly-fetched server state
|
|
15
23
|
* fails loudly when a foreign writer changed a path the user also changed.
|
|
16
24
|
*
|
|
17
25
|
* `fast-json-patch`'s `validateOperation: true` validates op structure and
|
|
18
26
|
* path resolvability but NOT the pre-existing value — a plain `replace` /
|
|
19
27
|
* `remove` silently overwrites whatever the foreign writer put there. For each
|
|
20
|
-
* `replace` / `remove` op we prepend a `test` op carrying the
|
|
28
|
+
* `replace` / `remove` op we prepend a `test` op carrying the base value at
|
|
21
29
|
* that path, so a same-path divergence becomes a `TEST_OPERATION_FAILED` throw.
|
|
22
30
|
* The caller treats that as a real field-level conflict (drop + refetch) rather
|
|
23
31
|
* than silently clobbering the foreign edit.
|
|
24
32
|
*
|
|
25
|
-
* `add` ops
|
|
26
|
-
* to test
|
|
33
|
+
* `add` ops get no `test` op: the path is new, so there is no base value
|
|
34
|
+
* to test. {@link reconcileByPatchReplay} checks them against theirs
|
|
35
|
+
* instead; a caller applying this patch itself does not get that check.
|
|
27
36
|
*
|
|
28
37
|
* Used behind the framework's `ConflictError` contract by every reconcile
|
|
29
38
|
* path — the GLSP recording command's undo/redo and forward-write, via
|
|
30
39
|
* {@link ReconcilingConflictResolver}, and an adopter's form-widget save — so
|
|
31
40
|
* they share one collision-detection rule.
|
|
32
41
|
*/
|
|
33
|
-
export function augmentWithTestOps(
|
|
42
|
+
export function augmentWithTestOps(base: object, userPatch: ReadonlyArray<JsonPatchOperation>): JsonPatchOperation[] {
|
|
34
43
|
const augmented: JsonPatchOperation[] = [];
|
|
35
44
|
for (const op of userPatch) {
|
|
36
45
|
if (op.op === 'replace' || op.op === 'remove') {
|
|
37
|
-
augmented.push({ op: 'test', path: op.path, value: getValueByPointer(
|
|
46
|
+
augmented.push({ op: 'test', path: op.path, value: getValueByPointer(base, op.path) });
|
|
38
47
|
}
|
|
39
48
|
augmented.push(op);
|
|
40
49
|
}
|
|
@@ -42,39 +51,40 @@ export function augmentWithTestOps(baseline: object, userPatch: ReadonlyArray<Js
|
|
|
42
51
|
}
|
|
43
52
|
|
|
44
53
|
/**
|
|
45
|
-
* Outcome of {@link reconcileByPatchReplay}. The caller persists
|
|
46
|
-
*
|
|
47
|
-
*
|
|
54
|
+
* Outcome of {@link reconcileByPatchReplay}. The caller persists `merged` and
|
|
55
|
+
* takes it as the new base, drops + surfaces the `theirs` root on `conflict`,
|
|
56
|
+
* catches up with the server on `no-op`, and decides its own fallback (e.g.
|
|
57
|
+
* force-retry) on `unavailable`.
|
|
48
58
|
*/
|
|
49
59
|
export type ReconcileOutcome<T> =
|
|
50
60
|
| {
|
|
51
61
|
/** The replay succeeded: the foreign writer touched no path the user did. */
|
|
52
62
|
status: 'merged';
|
|
53
63
|
/**
|
|
54
|
-
* A
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
* had.
|
|
64
|
+
* A new root built on theirs, carrying both intents. It is NOT the
|
|
65
|
+
* caller's `ours` root — take this value as the new base, or the next
|
|
66
|
+
* write diffs against state the server never had.
|
|
58
67
|
*/
|
|
59
68
|
merged: T;
|
|
60
69
|
}
|
|
61
70
|
| {
|
|
62
71
|
/**
|
|
63
|
-
* The user's root
|
|
64
|
-
*
|
|
65
|
-
*
|
|
72
|
+
* The user's root equalled the base, so the gate fired on another writer's
|
|
73
|
+
* change and the caller is behind the server (`ConflictError.actualVersion`).
|
|
74
|
+
* Nothing was refetched. Catch up from the update at that version or a later
|
|
75
|
+
* one, or refetch; forcing the write overwrites the other writer's change.
|
|
66
76
|
*/
|
|
67
77
|
status: 'no-op';
|
|
68
78
|
}
|
|
69
79
|
| {
|
|
70
|
-
/**
|
|
80
|
+
/** User and foreign writer changed one path, or both added the same element or key. */
|
|
71
81
|
status: 'conflict';
|
|
72
82
|
/**
|
|
73
83
|
* The server's current root, refetched and unmodified — the user's
|
|
74
84
|
* intent was NOT applied to it. Surface it and drop the write; treating
|
|
75
85
|
* it as a merge result silently discards what the user typed.
|
|
76
86
|
*/
|
|
77
|
-
|
|
87
|
+
theirs: T;
|
|
78
88
|
}
|
|
79
89
|
| {
|
|
80
90
|
/**
|
|
@@ -87,39 +97,84 @@ export type ReconcileOutcome<T> =
|
|
|
87
97
|
|
|
88
98
|
/**
|
|
89
99
|
* Shared three-way reconcile for a `ConflictError`: diff the user's intent
|
|
90
|
-
* (`
|
|
100
|
+
* (`base → ours`), refetch the server's current root (theirs), and replay the
|
|
91
101
|
* intent on top under strict, {@link augmentWithTestOps}-guarded `applyPatch`.
|
|
92
102
|
*
|
|
93
|
-
* - `no-op` — the user's root equals the
|
|
94
|
-
*
|
|
103
|
+
* - `no-op` — the user's root equals the base: nothing to replay, and no
|
|
104
|
+
* refetch; the caller's document is behind the server's.
|
|
95
105
|
* - `unavailable` — the refetch produced nothing; caller falls back.
|
|
96
106
|
* - `merged` — the foreign writer touched only paths the user did not; the
|
|
97
107
|
* merged root carries both intents.
|
|
98
|
-
* - `conflict` — a same-path divergence tripped a `test` op
|
|
99
|
-
* write and
|
|
108
|
+
* - `conflict` — a same-path divergence tripped a `test` op, or an `add`
|
|
109
|
+
* collides with one the foreign writer made; caller drops the write and
|
|
110
|
+
* surfaces `theirs`.
|
|
100
111
|
*
|
|
101
112
|
* I/O is the caller's: `refetch` supplies the current root, and applying the
|
|
102
|
-
* `merged` result (update vs save,
|
|
113
|
+
* `merged` result (update vs save, new base, UI refresh) stays at the call
|
|
103
114
|
* site so form and GLSP paths keep their own persistence semantics.
|
|
104
115
|
*/
|
|
105
116
|
export async function reconcileByPatchReplay<T extends object>(
|
|
106
|
-
|
|
107
|
-
|
|
117
|
+
base: T,
|
|
118
|
+
ours: T,
|
|
108
119
|
refetch: () => Promise<T | undefined>
|
|
109
120
|
): Promise<ReconcileOutcome<T>> {
|
|
110
|
-
const userPatch = compare(
|
|
121
|
+
const userPatch = compare(base, ours);
|
|
111
122
|
if (userPatch.length === 0) {
|
|
112
123
|
return { status: 'no-op' };
|
|
113
124
|
}
|
|
114
|
-
const
|
|
115
|
-
if (!
|
|
125
|
+
const theirs = await refetch();
|
|
126
|
+
if (!theirs) {
|
|
116
127
|
return { status: 'unavailable' };
|
|
117
128
|
}
|
|
129
|
+
if (addsCollide(base, theirs, userPatch)) {
|
|
130
|
+
return { status: 'conflict', theirs };
|
|
131
|
+
}
|
|
118
132
|
try {
|
|
119
|
-
const merged = applyPatch(deepClone(
|
|
133
|
+
const merged = applyPatch(deepClone(theirs), augmentWithTestOps(base, userPatch), true).newDocument;
|
|
120
134
|
return { status: 'merged', merged };
|
|
121
135
|
} catch {
|
|
122
|
-
return { status: 'conflict',
|
|
136
|
+
return { status: 'conflict', theirs };
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Whether an `add` of `userPatch` collides with one the foreign writer made:
|
|
142
|
+
* it inserts a value into an array that `theirs` holds more often than
|
|
143
|
+
* `base` did, or it adds an object key that `theirs` already holds.
|
|
144
|
+
*
|
|
145
|
+
* Replayed, the first inserts the value a second time and the second replaces
|
|
146
|
+
* the foreign value. Identical values conflict rather than being skipped:
|
|
147
|
+
* skipping drops one of two additions that merely happen to be equal.
|
|
148
|
+
*/
|
|
149
|
+
function addsCollide(base: object, theirs: object, userPatch: readonly JsonPatchOperation[]): boolean {
|
|
150
|
+
// Through JSON, as `compare` clones the add values: an undefined-valued key
|
|
151
|
+
// in `theirs` would otherwise make an equal element count as another.
|
|
152
|
+
const comparable = JSON.parse(JSON.stringify(theirs)) as object;
|
|
153
|
+
return userPatch.some(op => {
|
|
154
|
+
if (op.op !== 'add') {
|
|
155
|
+
return false;
|
|
156
|
+
}
|
|
157
|
+
const cut = op.path.lastIndexOf('/');
|
|
158
|
+
const before = valueAt(base, op.path.slice(0, cut));
|
|
159
|
+
const now = valueAt(comparable, op.path.slice(0, cut));
|
|
160
|
+
if (Array.isArray(before)) {
|
|
161
|
+
return Array.isArray(now) && occurrences(now, op.value) > occurrences(before, op.value);
|
|
162
|
+
}
|
|
163
|
+
return typeof now === 'object' && now !== null && Object.hasOwn(now, unescapePathComponent(op.path.slice(cut + 1)));
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** How many elements of `array` deeply equal `value`, whatever their key order. */
|
|
168
|
+
function occurrences(array: readonly unknown[], value: unknown): number {
|
|
169
|
+
return array.filter(element => _areEquals(element, value)).length;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** The value at `pointer`, or `undefined` when a step of it is missing. */
|
|
173
|
+
function valueAt(document: object, pointer: string): unknown {
|
|
174
|
+
try {
|
|
175
|
+
return getValueByPointer(document, pointer);
|
|
176
|
+
} catch {
|
|
177
|
+
return undefined;
|
|
123
178
|
}
|
|
124
179
|
}
|
|
125
180
|
|
|
@@ -132,12 +187,12 @@ export async function reconcileByPatchReplay<T extends object>(
|
|
|
132
187
|
*/
|
|
133
188
|
export interface ConflictResolver {
|
|
134
189
|
/**
|
|
135
|
-
* Reconcile the user's `
|
|
136
|
-
* server state (`refetch`), returning a {@link ReconcileOutcome} the caller
|
|
137
|
-
* acts on (persist `merged`, drop on `conflict`,
|
|
138
|
-
* `unavailable`).
|
|
190
|
+
* Reconcile the user's `base → ours` intent against the current
|
|
191
|
+
* server state (theirs, from `refetch`), returning a {@link ReconcileOutcome} the caller
|
|
192
|
+
* acts on (persist `merged`, drop on `conflict`, catch up on `no-op`, fall
|
|
193
|
+
* back on `unavailable`).
|
|
139
194
|
*/
|
|
140
|
-
resolve<T extends object>(
|
|
195
|
+
resolve<T extends object>(base: T, ours: T, refetch: () => Promise<T | undefined>): Promise<ReconcileOutcome<T>>;
|
|
141
196
|
}
|
|
142
197
|
|
|
143
198
|
/**
|
|
@@ -147,19 +202,19 @@ export interface ConflictResolver {
|
|
|
147
202
|
* clobbered.
|
|
148
203
|
*/
|
|
149
204
|
export class ReconcilingConflictResolver implements ConflictResolver {
|
|
150
|
-
resolve<T extends object>(
|
|
151
|
-
return reconcileByPatchReplay(
|
|
205
|
+
resolve<T extends object>(base: T, ours: T, refetch: () => Promise<T | undefined>): Promise<ReconcileOutcome<T>> {
|
|
206
|
+
return reconcileByPatchReplay(base, ours, refetch);
|
|
152
207
|
}
|
|
153
208
|
}
|
|
154
209
|
|
|
155
210
|
/**
|
|
156
|
-
* Last-writer-wins {@link ConflictResolver}: always reports the `
|
|
211
|
+
* Last-writer-wins {@link ConflictResolver}: always reports the `ours`
|
|
157
212
|
* state as merged, without refetching or guarding. Suitable for single-client
|
|
158
213
|
* tools or always-regenerated artifacts where a concurrent foreign edit may be
|
|
159
214
|
* overwritten. A foreign edit to any field is clobbered.
|
|
160
215
|
*/
|
|
161
216
|
export class ForceConflictResolver implements ConflictResolver {
|
|
162
|
-
async resolve<T extends object>(
|
|
163
|
-
return { status: 'merged', merged:
|
|
217
|
+
async resolve<T extends object>(_base: T, ours: T, _refetch: () => Promise<T | undefined>): Promise<ReconcileOutcome<T>> {
|
|
218
|
+
return { status: 'merged', merged: ours };
|
|
164
219
|
}
|
|
165
220
|
}
|
package/src/profile-session.ts
CHANGED
|
@@ -62,7 +62,12 @@ export interface ProfileSession {
|
|
|
62
62
|
records(): readonly ProfileRecord[];
|
|
63
63
|
}
|
|
64
64
|
|
|
65
|
-
|
|
65
|
+
/**
|
|
66
|
+
* One in-flight {@link ProfileSession.scope} call. Held on the `protected`
|
|
67
|
+
* {@link DefaultProfileSession.stack} and named by
|
|
68
|
+
* {@link DefaultProfileSession.closeFrame}, so a subclass has to name it too.
|
|
69
|
+
*/
|
|
70
|
+
export interface ProfileScopeFrame {
|
|
66
71
|
readonly id: string;
|
|
67
72
|
/** Session-stopwatch reading when this scope began. */
|
|
68
73
|
readonly start: number;
|
|
@@ -78,7 +83,7 @@ interface ScopeFrame {
|
|
|
78
83
|
export class DefaultProfileSession implements ProfileSession {
|
|
79
84
|
/** Single monotonic timeline for the whole session; per-scope readings are deltas off it. */
|
|
80
85
|
protected readonly sessionStopwatch: Stopwatch;
|
|
81
|
-
protected readonly stack:
|
|
86
|
+
protected readonly stack: ProfileScopeFrame[] = [];
|
|
82
87
|
protected readonly entries = new Map<string, number[]>();
|
|
83
88
|
|
|
84
89
|
constructor(
|
|
@@ -92,7 +97,7 @@ export class DefaultProfileSession implements ProfileSession {
|
|
|
92
97
|
scope<T>(id: string, fn: () => Promise<T>): Promise<T>;
|
|
93
98
|
scope<T>(id: string, fn: () => T): T;
|
|
94
99
|
scope<T>(id: string, fn: () => T | Promise<T>): T | Promise<T> {
|
|
95
|
-
const frame:
|
|
100
|
+
const frame: ProfileScopeFrame = { id, start: this.sessionStopwatch.elapsedMs, childMs: 0 };
|
|
96
101
|
this.stack.push(frame);
|
|
97
102
|
let result: T | Promise<T>;
|
|
98
103
|
try {
|
|
@@ -118,7 +123,7 @@ export class DefaultProfileSession implements ProfileSession {
|
|
|
118
123
|
}
|
|
119
124
|
|
|
120
125
|
/** Pop `frame`, charge its full duration to the parent, and record its self-time. */
|
|
121
|
-
protected closeFrame(frame:
|
|
126
|
+
protected closeFrame(frame: ProfileScopeFrame): void {
|
|
122
127
|
this.stack.pop();
|
|
123
128
|
const duration = this.sessionStopwatch.elapsedMs - frame.start;
|
|
124
129
|
const parent = this.stack[this.stack.length - 1];
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/********************************************************************************
|
|
2
|
+
* Copyright (c) 2026 CrossBreeze, EclipseSource and others.
|
|
3
|
+
*
|
|
4
|
+
* This program and the accompanying materials are made available under the
|
|
5
|
+
* terms of the MIT License which is available in the project root.
|
|
6
|
+
*
|
|
7
|
+
* SPDX-License-Identifier: MIT
|
|
8
|
+
********************************************************************************/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* A random v4 UUID. Not `crypto.randomUUID`: a browser offers it only in a
|
|
12
|
+
* secure context, and a client may be served over plain HTTP.
|
|
13
|
+
*/
|
|
14
|
+
export function randomUuid(): string {
|
|
15
|
+
const bytes = globalThis.crypto.getRandomValues(new Uint8Array(16));
|
|
16
|
+
// Version 4, RFC 4122 variant.
|
|
17
|
+
bytes[6] = (bytes[6] & 0x0f) | 0x40;
|
|
18
|
+
bytes[8] = (bytes[8] & 0x3f) | 0x80;
|
|
19
|
+
const hex = Array.from(bytes, byte => byte.toString(16).padStart(2, '0')).join('');
|
|
20
|
+
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
|
|
21
|
+
}
|