@openfairygui/backend 0.5.0 → 0.6.1
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 +3 -3
- package/dist/docs.cjs +190 -611
- package/dist/docs.d.cts +9 -27
- package/dist/docs.d.mts +9 -27
- package/dist/docs.mjs +190 -611
- package/dist/index.cjs +445 -635
- package/dist/index.d.cts +46 -160
- package/dist/index.d.mts +46 -160
- package/dist/index.mjs +445 -636
- package/dist/node.cjs +476 -664
- package/dist/node.d.cts +20 -94
- package/dist/node.d.mts +20 -94
- package/dist/node.mjs +476 -664
- package/package.json +3 -3
- package/src/contracts.ts +2 -2
- package/src/diagnostics.ts +4 -5
- package/src/generated/contracts.ts +208 -837
- package/src/generated/docs.ts +3 -3
- package/src/index.ts +2 -13
- package/src/node.ts +36 -30
- package/src/runtime/capabilities.ts +2 -12
- package/src/runtime/contracts.ts +33 -107
- package/src/runtime.ts +22 -57
- package/src/services/authoring-service.ts +9 -572
- package/src/services/cache-service.ts +32 -10
- package/src/services/context.ts +27 -14
- package/src/services/event-service.ts +10 -8
- package/src/services/persistence-service.ts +550 -0
- package/src/services/read-service.ts +18 -18
- package/src/services/runtime-service.ts +27 -36
- package/src/services/session-operation-queue.ts +21 -0
- package/src/services/session-project-writer.ts +3 -1
- package/src/services/session-registry.ts +70 -0
- package/src/services/session-utils.ts +4 -4
- package/src/services/job-service.ts +0 -238
|
@@ -1,14 +1,21 @@
|
|
|
1
|
-
import { failure, success, type
|
|
1
|
+
import { failure, success, type BackendSessionState } from './context.js';
|
|
2
|
+
import type { EventService } from './event-service.js';
|
|
2
3
|
import { createSessionNotFoundError } from './session-utils.js';
|
|
3
4
|
import type {
|
|
4
5
|
BackendCacheEntry,
|
|
5
6
|
BackendCacheSnapshot,
|
|
6
7
|
BackendResult,
|
|
7
8
|
GetCacheSnapshotInput,
|
|
9
|
+
RefreshCacheInput,
|
|
8
10
|
SessionNotFoundError,
|
|
9
11
|
} from '../runtime.js';
|
|
10
12
|
|
|
11
|
-
|
|
13
|
+
type CacheSession = Readonly<Pick<BackendSessionState,
|
|
14
|
+
'sessionId' | 'canonicalPathKey' | 'revision' | 'lastSavedRevision' | 'dirty' | 'closed'>> & {
|
|
15
|
+
readonly project: { readonly packages: readonly { readonly resources: readonly unknown[] }[] };
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
function createCacheEntry(session: CacheSession, valid: boolean): BackendCacheEntry {
|
|
12
19
|
return {
|
|
13
20
|
canonicalPathKey: session.canonicalPathKey,
|
|
14
21
|
sessionId: session.sessionId,
|
|
@@ -26,34 +33,49 @@ function createCacheEntry(session: BackendSessionState, valid: boolean): Backend
|
|
|
26
33
|
}
|
|
27
34
|
|
|
28
35
|
export class CacheService {
|
|
29
|
-
|
|
36
|
+
private readonly cacheBySession = new Map<string, BackendCacheEntry>();
|
|
37
|
+
public constructor(private readonly getSession: (sessionId: string) => CacheSession | undefined, private readonly events: EventService) {}
|
|
30
38
|
|
|
31
39
|
public getCacheSnapshot(input: GetCacheSnapshotInput): BackendResult<BackendCacheSnapshot, SessionNotFoundError> {
|
|
32
40
|
const startedAt = Date.now();
|
|
33
|
-
const session = this.
|
|
41
|
+
const session = this.getSession(input.sessionId);
|
|
34
42
|
if (!session || session.closed) {
|
|
35
43
|
return failure('read', startedAt, createSessionNotFoundError(input.sessionId));
|
|
36
44
|
}
|
|
37
|
-
const entry = this.
|
|
45
|
+
const entry = this.cacheBySession.get(input.sessionId);
|
|
38
46
|
return success('read', startedAt, {
|
|
39
47
|
cacheRevision: entry?.revision ?? session.revision,
|
|
40
48
|
entries: entry ? [structuredClone(entry)] : [],
|
|
41
49
|
}, { sessionId: session.sessionId, revision: session.revision });
|
|
42
50
|
}
|
|
43
51
|
|
|
44
|
-
public
|
|
52
|
+
public refreshCache(input: RefreshCacheInput): BackendResult<BackendCacheSnapshot, SessionNotFoundError> {
|
|
53
|
+
const startedAt = Date.now();
|
|
54
|
+
const session = this.getSession(input.sessionId);
|
|
55
|
+
if (!session || session.closed) return failure('runtime', startedAt, createSessionNotFoundError(input.sessionId));
|
|
56
|
+
const entry = this.refreshSession(session);
|
|
57
|
+
this.events.emit({
|
|
58
|
+
kind: 'cache.updated', sessionId: session.sessionId, canonicalPathKey: session.canonicalPathKey,
|
|
59
|
+
revision: session.revision, cacheRevision: entry.revision, payload: { reason: input.reason ?? 'manual' },
|
|
60
|
+
});
|
|
61
|
+
return success('runtime', startedAt, {
|
|
62
|
+
cacheRevision: entry.revision, entries: [structuredClone(entry)],
|
|
63
|
+
}, { sessionId: session.sessionId, revision: session.revision });
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
public refreshSession(session: CacheSession): BackendCacheEntry {
|
|
45
67
|
const entry = createCacheEntry(session, true);
|
|
46
|
-
this.
|
|
68
|
+
this.cacheBySession.set(session.sessionId, entry);
|
|
47
69
|
return entry;
|
|
48
70
|
}
|
|
49
71
|
|
|
50
|
-
public invalidateSession(session:
|
|
72
|
+
public invalidateSession(session: CacheSession): BackendCacheEntry {
|
|
51
73
|
const entry = createCacheEntry(session, false);
|
|
52
|
-
this.
|
|
74
|
+
this.cacheBySession.set(session.sessionId, entry);
|
|
53
75
|
return entry;
|
|
54
76
|
}
|
|
55
77
|
|
|
56
78
|
public removeSession(sessionId: string): void {
|
|
57
|
-
this.
|
|
79
|
+
this.cacheBySession.delete(sessionId);
|
|
58
80
|
}
|
|
59
81
|
}
|
package/src/services/context.ts
CHANGED
|
@@ -7,26 +7,24 @@ import {
|
|
|
7
7
|
type BackendStage,
|
|
8
8
|
} from '../contracts.js';
|
|
9
9
|
import { enrichBackendDiagnostic } from '../diagnostics.js';
|
|
10
|
+
import type { SessionRegistry } from './session-registry.js';
|
|
10
11
|
import type {
|
|
11
|
-
BackendCacheEntry,
|
|
12
12
|
BackendCapabilities,
|
|
13
13
|
BackendError,
|
|
14
|
-
BackendEvent,
|
|
15
14
|
BackendFailure,
|
|
16
15
|
BackendFileSystem,
|
|
17
16
|
BackendHostAdapter,
|
|
18
|
-
BackendJobSnapshot,
|
|
19
17
|
BackendSessionLock,
|
|
20
18
|
BackendSessionSnapshot,
|
|
21
19
|
BackendSuccess,
|
|
22
20
|
} from '../runtime.js';
|
|
23
21
|
|
|
24
22
|
export interface BackendSessionState {
|
|
25
|
-
sessionId: string;
|
|
26
|
-
fairyPath: string;
|
|
27
|
-
canonicalProjectPath: string;
|
|
28
|
-
canonicalPathKey: string;
|
|
29
|
-
lockFilePath: string;
|
|
23
|
+
readonly sessionId: string;
|
|
24
|
+
readonly fairyPath: string;
|
|
25
|
+
readonly canonicalProjectPath: string;
|
|
26
|
+
readonly canonicalPathKey: string;
|
|
27
|
+
readonly lockFilePath: string;
|
|
30
28
|
sessionLock: BackendSessionLock | null;
|
|
31
29
|
fileSystem?: BackendFileSystem;
|
|
32
30
|
project: import('@openfairygui/core/uam').UamProject;
|
|
@@ -51,12 +49,27 @@ export interface BackendContext {
|
|
|
51
49
|
host?: BackendHostAdapter;
|
|
52
50
|
allowedProjectRoots?: readonly string[];
|
|
53
51
|
capabilities: BackendCapabilities;
|
|
54
|
-
sessions:
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
52
|
+
sessions: SessionRegistry;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Borrowed data is read-only, including nested arrays and primary resource bytes. */
|
|
56
|
+
export type ReadonlyData<T> = unknown extends T ? T : T extends Uint8Array
|
|
57
|
+
? Readonly<Pick<Uint8Array, 'length' | 'byteLength' | typeof Symbol.iterator>> & { readonly [index: number]: number }
|
|
58
|
+
: { readonly [K in keyof T]: ReadonlyData<T[K]> };
|
|
59
|
+
|
|
60
|
+
export type SessionReadView = ReadonlyData<Pick<BackendSessionState,
|
|
61
|
+
'sessionId' | 'canonicalProjectPath' | 'canonicalPathKey' | 'project' | 'readDiagnostics' | 'readComplete'
|
|
62
|
+
| 'uamFidelity' | 'revision' | 'lastSavedRevision' | 'dirty' | 'lockHeld' | 'closed'>>;
|
|
63
|
+
export type SessionLookup = (sessionId: string) => SessionReadView | undefined;
|
|
64
|
+
|
|
65
|
+
/** Borrow without copying the entire project before a read has checked its response budget. */
|
|
66
|
+
export function readView<T>(value: T): ReadonlyData<T> {
|
|
67
|
+
return value as ReadonlyData<T>;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** A detached copy is writable again; this is the only conversion from a borrowed read view. */
|
|
71
|
+
export function cloneReadData<T>(value: ReadonlyData<T>): T {
|
|
72
|
+
return structuredClone(value) as T;
|
|
60
73
|
}
|
|
61
74
|
|
|
62
75
|
function diagnosticFromError(error: BackendError): BackendDiagnostic {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { failure, success, type
|
|
1
|
+
import { failure, success, type BackendSessionState } from './context.js';
|
|
2
2
|
import { enrichBackendDiagnostic } from '../diagnostics.js';
|
|
3
3
|
import { createSessionNotFoundError } from './session-utils.js';
|
|
4
4
|
import type {
|
|
@@ -13,33 +13,35 @@ import type {
|
|
|
13
13
|
const DEFAULT_EVENT_RETENTION_LIMIT = 1000;
|
|
14
14
|
|
|
15
15
|
export class EventService {
|
|
16
|
-
|
|
16
|
+
private readonly eventsBySession = new Map<string, BackendEvent[]>();
|
|
17
|
+
private sequence = 0;
|
|
18
|
+
public constructor(private readonly getSession: (sessionId: string) => Readonly<Pick<BackendSessionState, 'sessionId' | 'revision' | 'closed'>> | undefined) {}
|
|
17
19
|
|
|
18
20
|
public emit(event: Omit<BackendEvent, 'sequence' | 'timestamp' | 'diagnostics'> & {
|
|
19
21
|
diagnostics?: BackendEvent['diagnostics'];
|
|
20
22
|
}): BackendEvent {
|
|
21
23
|
const emitted: BackendEvent = {
|
|
22
24
|
...event,
|
|
23
|
-
sequence: this.
|
|
25
|
+
sequence: ++this.sequence,
|
|
24
26
|
timestamp: new Date().toISOString(),
|
|
25
27
|
diagnostics: (event.diagnostics ?? []).map((entry) => enrichBackendDiagnostic(entry, event.sessionId)),
|
|
26
28
|
};
|
|
27
29
|
const sessionId = event.sessionId;
|
|
28
30
|
if (!sessionId) return emitted;
|
|
29
|
-
const events = this.
|
|
31
|
+
const events = this.eventsBySession.get(sessionId) ?? [];
|
|
30
32
|
events.push(emitted);
|
|
31
33
|
while (events.length > DEFAULT_EVENT_RETENTION_LIMIT) events.shift();
|
|
32
|
-
this.
|
|
34
|
+
this.eventsBySession.set(sessionId, events);
|
|
33
35
|
return emitted;
|
|
34
36
|
}
|
|
35
37
|
|
|
36
38
|
public getEvents(input: GetEventsInput): BackendResult<GetEventsSnapshot, SessionNotFoundError | EventCursorInvalidError> {
|
|
37
39
|
const startedAt = Date.now();
|
|
38
|
-
const session = this.
|
|
40
|
+
const session = this.getSession(input.sessionId);
|
|
39
41
|
if (!session || session.closed) {
|
|
40
42
|
return failure('runtime', startedAt, createSessionNotFoundError(input.sessionId));
|
|
41
43
|
}
|
|
42
|
-
const events = this.
|
|
44
|
+
const events = this.eventsBySession.get(input.sessionId) ?? [];
|
|
43
45
|
const oldestSequence = events[0]?.sequence ?? (events.length === 0 ? 1 : 0);
|
|
44
46
|
const currentSequence = events.at(-1)?.sequence ?? 0;
|
|
45
47
|
const after = input.after === undefined ? 0 : Number(input.after);
|
|
@@ -79,6 +81,6 @@ export class EventService {
|
|
|
79
81
|
}
|
|
80
82
|
|
|
81
83
|
public removeSession(sessionId: string): void {
|
|
82
|
-
this.
|
|
84
|
+
this.eventsBySession.delete(sessionId);
|
|
83
85
|
}
|
|
84
86
|
}
|