@openfairygui/backend 0.6.2 → 0.7.0

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.
Files changed (48) hide show
  1. package/dist/contracts-Bx8HUr7I.d.cts +756 -0
  2. package/dist/contracts-CMQiPEyz.d.mts +756 -0
  3. package/dist/diagnostics-CIe7S-RW.cjs +670 -0
  4. package/dist/diagnostics-CZ12H1Mx.d.cts +1679 -0
  5. package/dist/diagnostics-CpJ8cAP6.d.mts +1679 -0
  6. package/dist/diagnostics-DTnLXQGs.mjs +641 -0
  7. package/dist/docs.cjs +11506 -5902
  8. package/dist/docs.d.cts +5 -185
  9. package/dist/docs.d.mts +5 -185
  10. package/dist/docs.mjs +11504 -5901
  11. package/dist/index.cjs +16 -2703
  12. package/dist/index.d.cts +5 -2362
  13. package/dist/index.d.mts +5 -2362
  14. package/dist/index.mjs +4 -2691
  15. package/dist/node.cjs +367 -2779
  16. package/dist/node.d.cts +4 -724
  17. package/dist/node.d.mts +4 -724
  18. package/dist/node.mjs +359 -2751
  19. package/dist/rolldown-runtime-BJqNVkSG.cjs +28 -0
  20. package/dist/runtime-DYqG77ak.cjs +2249 -0
  21. package/dist/runtime-gStewf1p.mjs +2189 -0
  22. package/package.json +4 -5
  23. package/src/contracts.ts +0 -58
  24. package/src/diagnostics.ts +0 -188
  25. package/src/docs.ts +0 -109
  26. package/src/generated/contracts.ts +0 -24165
  27. package/src/generated/docs.ts +0 -12
  28. package/src/index.ts +0 -99
  29. package/src/node.ts +0 -317
  30. package/src/path-policy.ts +0 -123
  31. package/src/runtime/capabilities.ts +0 -131
  32. package/src/runtime/contracts.ts +0 -672
  33. package/src/runtime.ts +0 -231
  34. package/src/services/artifact-service.ts +0 -19
  35. package/src/services/authoring-service.ts +0 -229
  36. package/src/services/cache-service.ts +0 -81
  37. package/src/services/capture-project.ts +0 -75
  38. package/src/services/context.ts +0 -150
  39. package/src/services/event-service.ts +0 -86
  40. package/src/services/persistence-service.ts +0 -550
  41. package/src/services/read-service.ts +0 -274
  42. package/src/services/runtime-service.ts +0 -342
  43. package/src/services/session-operation-queue.ts +0 -21
  44. package/src/services/session-project-writer.ts +0 -89
  45. package/src/services/session-registry.ts +0 -70
  46. package/src/services/session-utils.ts +0 -45
  47. package/src/services/transaction-preview.ts +0 -116
  48. package/src/storage.ts +0 -232
@@ -1,150 +0,0 @@
1
- import {
2
- BACKEND_CAPABILITY_SCHEMA_VERSION,
3
- BACKEND_CONTRACT_VERSION,
4
- type BackendDiagnostic,
5
- type BackendMessage,
6
- type BackendResponseMeta,
7
- type BackendStage,
8
- } from '../contracts.js';
9
- import { enrichBackendDiagnostic } from '../diagnostics.js';
10
- import type { SessionRegistry } from './session-registry.js';
11
- import type {
12
- BackendCapabilities,
13
- BackendError,
14
- BackendFailure,
15
- BackendFileSystem,
16
- BackendHostAdapter,
17
- BackendSessionLock,
18
- BackendSessionSnapshot,
19
- BackendSuccess,
20
- } from '../runtime.js';
21
-
22
- export interface BackendSessionState {
23
- readonly sessionId: string;
24
- readonly fairyPath: string;
25
- readonly canonicalProjectPath: string;
26
- readonly canonicalPathKey: string;
27
- readonly lockFilePath: string;
28
- sessionLock: BackendSessionLock | null;
29
- fileSystem?: BackendFileSystem;
30
- project: import('@openfairygui/core/uam').UamProject;
31
- readDiagnostics: import('@openfairygui/core').ProjectDiagnostic[];
32
- readComplete: boolean;
33
- uamFidelity: 'full' | 'unsupported';
34
- revision: number;
35
- lastSavedRevision: number;
36
- /** Package-controlled files deferred until a successful replacement project write. */
37
- pendingStaleSourceFiles: Map<string, import('@openfairygui/core/project-io').ProjectSourceFile>;
38
- /** Empty resource directories deferred until a successful replacement project write. */
39
- pendingStaleResourceFolders: Map<string, import('@openfairygui/core/project-io').ProjectResourceFolder>;
40
- /** Removed package-branch and root-branch directories deferred until controlled files are replaced. */
41
- pendingStaleBranchDirectories: Map<string, import('@openfairygui/core/project-io').ProjectBranchDirectory>;
42
- dirty: boolean;
43
- lockHeld: boolean;
44
- closed: boolean;
45
- }
46
-
47
- export interface BackendContext {
48
- fileSystem?: BackendFileSystem;
49
- host?: BackendHostAdapter;
50
- allowedProjectRoots?: readonly string[];
51
- capabilities: BackendCapabilities;
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;
73
- }
74
-
75
- function diagnosticFromError(error: BackendError): BackendDiagnostic {
76
- return {
77
- code: error.code,
78
- message: error.message,
79
- severity: 'error',
80
- };
81
- }
82
-
83
- function randomId(): string {
84
- return `${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 10)}`;
85
- }
86
-
87
- export function createMeta(
88
- stage: BackendStage,
89
- startedAt: number,
90
- options?: {
91
- requestId?: string;
92
- sessionId?: string;
93
- revision?: number;
94
- warnings?: BackendMessage[];
95
- diagnostics?: BackendDiagnostic[];
96
- },
97
- ): BackendResponseMeta {
98
- return {
99
- requestId: options?.requestId ?? randomId(),
100
- sessionId: options?.sessionId,
101
- revision: options?.revision,
102
- durationMs: Math.max(0, Date.now() - startedAt),
103
- warnings: options?.warnings ?? [],
104
- diagnostics: (options?.diagnostics ?? []).map((entry) => enrichBackendDiagnostic(entry, options?.sessionId)),
105
- stage,
106
- contractVersion: BACKEND_CONTRACT_VERSION,
107
- capabilitySchemaVersion: BACKEND_CAPABILITY_SCHEMA_VERSION,
108
- };
109
- }
110
-
111
- export function success<T>(
112
- stage: BackendStage,
113
- startedAt: number,
114
- data: T,
115
- options?: {
116
- requestId?: string;
117
- sessionId?: string;
118
- revision?: number;
119
- warnings?: BackendMessage[];
120
- diagnostics?: BackendDiagnostic[];
121
- },
122
- ): BackendSuccess<T> {
123
- return {
124
- ok: true,
125
- meta: createMeta(stage, startedAt, options),
126
- data,
127
- };
128
- }
129
-
130
- export function failure<E extends BackendError>(
131
- stage: BackendStage,
132
- startedAt: number,
133
- error: E,
134
- session?: BackendSessionSnapshot,
135
- options?: {
136
- requestId?: string;
137
- sessionId?: string;
138
- revision?: number;
139
- warnings?: BackendMessage[];
140
- diagnostics?: BackendDiagnostic[];
141
- },
142
- ): BackendFailure<E> {
143
- const diagnostics = options?.diagnostics ?? [diagnosticFromError(error)];
144
- return {
145
- ok: false,
146
- meta: createMeta(stage, startedAt, { ...options, diagnostics }),
147
- error,
148
- session,
149
- };
150
- }
@@ -1,86 +0,0 @@
1
- import { failure, success, type BackendSessionState } from './context.js';
2
- import { enrichBackendDiagnostic } from '../diagnostics.js';
3
- import { createSessionNotFoundError } from './session-utils.js';
4
- import type {
5
- BackendEvent,
6
- BackendResult,
7
- EventCursorInvalidError,
8
- GetEventsInput,
9
- GetEventsSnapshot,
10
- SessionNotFoundError,
11
- } from '../runtime.js';
12
-
13
- const DEFAULT_EVENT_RETENTION_LIMIT = 1000;
14
-
15
- export class EventService {
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) {}
19
-
20
- public emit(event: Omit<BackendEvent, 'sequence' | 'timestamp' | 'diagnostics'> & {
21
- diagnostics?: BackendEvent['diagnostics'];
22
- }): BackendEvent {
23
- const emitted: BackendEvent = {
24
- ...event,
25
- sequence: ++this.sequence,
26
- timestamp: new Date().toISOString(),
27
- diagnostics: (event.diagnostics ?? []).map((entry) => enrichBackendDiagnostic(entry, event.sessionId)),
28
- };
29
- const sessionId = event.sessionId;
30
- if (!sessionId) return emitted;
31
- const events = this.eventsBySession.get(sessionId) ?? [];
32
- events.push(emitted);
33
- while (events.length > DEFAULT_EVENT_RETENTION_LIMIT) events.shift();
34
- this.eventsBySession.set(sessionId, events);
35
- return emitted;
36
- }
37
-
38
- public getEvents(input: GetEventsInput): BackendResult<GetEventsSnapshot, SessionNotFoundError | EventCursorInvalidError> {
39
- const startedAt = Date.now();
40
- const session = this.getSession(input.sessionId);
41
- if (!session || session.closed) {
42
- return failure('runtime', startedAt, createSessionNotFoundError(input.sessionId));
43
- }
44
- const events = this.eventsBySession.get(input.sessionId) ?? [];
45
- const oldestSequence = events[0]?.sequence ?? (events.length === 0 ? 1 : 0);
46
- const currentSequence = events.at(-1)?.sequence ?? 0;
47
- const after = input.after === undefined ? 0 : Number(input.after);
48
- if (!Number.isInteger(after) || after < 0) {
49
- return failure('runtime', startedAt, {
50
- code: 'event_cursor_invalid',
51
- message: `Invalid event cursor: ${input.after}`,
52
- sessionId: input.sessionId,
53
- after: String(input.after),
54
- });
55
- }
56
- if (events.length > 0 && after !== 0 && after < oldestSequence - 1) {
57
- return failure('runtime', startedAt, {
58
- code: 'event_cursor_invalid',
59
- message: `Event cursor has expired: ${after}`,
60
- sessionId: input.sessionId,
61
- after: String(input.after),
62
- });
63
- }
64
- if (after > currentSequence) {
65
- return failure('runtime', startedAt, {
66
- code: 'event_cursor_invalid',
67
- message: `Unknown event cursor: ${after}`,
68
- sessionId: input.sessionId,
69
- after: String(input.after),
70
- });
71
- }
72
-
73
- const filtered = events.filter((event) => event.sequence > after);
74
- const limit = input.limit === undefined ? filtered.length : Math.max(0, input.limit);
75
- return success('runtime', startedAt, {
76
- events: filtered.slice(0, limit).map((event) => structuredClone(event)),
77
- oldestSequence,
78
- currentSequence,
79
- cursorExpired: false,
80
- }, { sessionId: session.sessionId, revision: session.revision });
81
- }
82
-
83
- public removeSession(sessionId: string): void {
84
- this.eventsBySession.delete(sessionId);
85
- }
86
- }