-
-
Notifications
You must be signed in to change notification settings - Fork 313
Expand file tree
/
Copy pathruntime-instance.ts
More file actions
62 lines (54 loc) · 1.6 KB
/
Copy pathruntime-instance.ts
File metadata and controls
62 lines (54 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { randomUUID } from 'node:crypto';
const DEFAULT_WORKSPACE_KEY = 'default';
export interface RuntimeInstance {
instanceId: string;
pid: number;
workspaceKey: string;
}
let configuredWorkspaceKey: string | null = null;
let runtimeInstance: RuntimeInstance | null = null;
export function configureRuntimeWorkspaceKey(workspaceKey: string): void {
const normalized = workspaceKey.trim();
if (!normalized) {
throw new Error('Workspace key cannot be empty');
}
configuredWorkspaceKey = normalized;
if (runtimeInstance) {
runtimeInstance = { ...runtimeInstance, workspaceKey: normalized };
}
}
export function getRuntimeInstance(): RuntimeInstance {
const workspaceKey = configuredWorkspaceKey;
if (!workspaceKey) {
throw new Error('Runtime workspace key has not been configured');
}
runtimeInstance ??= {
instanceId: randomUUID(),
pid: process.pid,
workspaceKey,
};
return runtimeInstance;
}
export function getRuntimeInstanceIfConfigured(): RuntimeInstance | null {
if (runtimeInstance) {
return runtimeInstance;
}
if (!configuredWorkspaceKey) {
return null;
}
return getRuntimeInstance();
}
export function setRuntimeInstanceForTests(
instance:
| (Omit<RuntimeInstance, 'workspaceKey'> & Partial<Pick<RuntimeInstance, 'workspaceKey'>>)
| null,
): void {
runtimeInstance = instance
? {
instanceId: instance.instanceId,
pid: instance.pid,
workspaceKey: instance.workspaceKey ?? configuredWorkspaceKey ?? DEFAULT_WORKSPACE_KEY,
}
: null;
configuredWorkspaceKey = runtimeInstance?.workspaceKey ?? null;
}