-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathkernel-context.ts
More file actions
103 lines (91 loc) · 2.75 KB
/
kernel-context.ts
File metadata and controls
103 lines (91 loc) · 2.75 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/**
* Kernel Context Implementation
*
* Implements the KernelContext interface from @objectstack/spec/system.
* Provides static environment information available at boot time.
*/
import type { KernelContext } from '@objectstack/spec/system';
import { randomUUID } from 'crypto';
/**
* Creates a KernelContext instance with default values.
*
* @param options - Partial context options to override defaults
* @returns Complete KernelContext instance
*/
export function createKernelContext(options: Partial<KernelContext> = {}): KernelContext {
// Read version from package.json safely
let version = options.version || '0.0.0';
try {
// Using require for package.json is acceptable in Node.js context
// as package.json is not a TypeScript module
// eslint-disable-next-line @typescript-eslint/no-var-requires
const pkg = require('../../package.json');
version = options.version || pkg.version || '0.0.0';
} catch (e) {
// Fallback if package.json cannot be loaded (e.g., in tests)
// Version will use the provided option or default to '0.0.0'
}
return {
instanceId: options.instanceId || randomUUID(),
mode: options.mode || (process.env.NODE_ENV === 'production' ? 'production' : 'development'),
version,
appName: options.appName,
cwd: options.cwd || process.cwd(),
workspaceRoot: options.workspaceRoot,
startTime: options.startTime || Date.now(),
features: options.features || {},
};
}
/**
* KernelContextManager
*
* Manages the kernel context throughout the application lifecycle.
*/
export class KernelContextManager {
private context: KernelContext;
constructor(options: Partial<KernelContext> = {}) {
this.context = createKernelContext(options);
}
/**
* Get the current kernel context.
*/
getContext(): KernelContext {
return { ...this.context };
}
/**
* Get the instance ID.
*/
getInstanceId(): string {
return this.context.instanceId;
}
/**
* Get the runtime mode.
*/
getMode(): KernelContext['mode'] {
return this.context.mode;
}
/**
* Check if a feature flag is enabled.
*/
isFeatureEnabled(feature: string): boolean {
return this.context.features[feature] === true;
}
/**
* Enable a feature flag.
*/
enableFeature(feature: string): void {
this.context.features[feature] = true;
}
/**
* Disable a feature flag.
*/
disableFeature(feature: string): void {
this.context.features[feature] = false;
}
/**
* Get uptime in milliseconds.
*/
getUptime(): number {
return Date.now() - this.context.startTime;
}
}