@spooky-sync/core 0.0.1-canary.17 → 0.0.1-canary.18
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/dist/index.d.ts +4 -353
- package/dist/index.js +3 -71
- package/dist/otel/index.d.ts +21 -0
- package/dist/otel/index.js +86 -0
- package/dist/types.d.ts +359 -0
- package/package.json +5 -1
- package/skills/spooky-core/references/config.md +2 -2
- package/src/otel/index.ts +124 -0
- package/src/services/logger/index.ts +4 -108
- package/src/spooky.ts +1 -1
- package/src/types.ts +8 -3
- package/tsdown.config.ts +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,132 +1,9 @@
|
|
|
1
|
+
import { C as Logger$1, S as UpdateOptions, T as EventSystem, _ as RunOptions, a as MutationEvent, b as SpookyQueryResultPromise, c as PinoTransmit, d as QueryHash, f as QueryState, g as RecordVersionDiff, h as RecordVersionArray, i as MutationCallback, l as QueryConfig, m as QueryUpdateCallback, n as EventSubscriptionOptions, o as MutationEventType, p as QueryTimeToLive, r as Level, s as PersistenceClient, t as DebounceOptions, u as QueryConfigRecord, v as SpookyConfig, w as EventDefinition, x as StoreType, y as SpookyQueryResult } from "./types.js";
|
|
1
2
|
import * as surrealdb0 from "surrealdb";
|
|
2
3
|
import { Duration, RecordId, Surreal, SurrealTransaction } from "surrealdb";
|
|
3
|
-
import { AccessDefinition, BackendNames, BackendRoutes, BucketNames, ColumnSchema, GetTable, QueryBuilder, QueryOptions,
|
|
4
|
-
import {
|
|
4
|
+
import { AccessDefinition, BackendNames, BackendRoutes, BucketNames, ColumnSchema, GetTable, QueryBuilder, QueryOptions, RoutePayload, SchemaStructure, TableModel, TableNames, TypeNameToTypeMap } from "@spooky-sync/query-builder";
|
|
5
|
+
import { Logger } from "pino";
|
|
5
6
|
|
|
6
|
-
//#region src/events/index.d.ts
|
|
7
|
-
/**
|
|
8
|
-
* Utility type to define the payload structure of an event.
|
|
9
|
-
* If the payload type P is never, it defines payload as undefined.
|
|
10
|
-
*/
|
|
11
|
-
type EventPayloadDefinition<P> = [P] extends [never] ? {
|
|
12
|
-
payload: undefined;
|
|
13
|
-
} : {
|
|
14
|
-
payload: P;
|
|
15
|
-
};
|
|
16
|
-
/**
|
|
17
|
-
* Defines the structure of an event with a specific type and payload.
|
|
18
|
-
* @template T The string literal type of the event.
|
|
19
|
-
* @template P The type of the event payload.
|
|
20
|
-
*/
|
|
21
|
-
type EventDefinition<T extends string, P> = {
|
|
22
|
-
type: T;
|
|
23
|
-
} & EventPayloadDefinition<P>;
|
|
24
|
-
/**
|
|
25
|
-
* A map of event types to their definitions.
|
|
26
|
-
* Keys are event names, values are EventDefinitions.
|
|
27
|
-
*/
|
|
28
|
-
type EventTypeMap = Record<string, EventDefinition<any, unknown> | EventDefinition<any, never>>;
|
|
29
|
-
/**
|
|
30
|
-
* Options for pushing/emitting events.
|
|
31
|
-
*/
|
|
32
|
-
interface PushEventOptions {
|
|
33
|
-
/** Configuration for debouncing the event. */
|
|
34
|
-
debounced?: {
|
|
35
|
-
key: string;
|
|
36
|
-
delay: number;
|
|
37
|
-
};
|
|
38
|
-
}
|
|
39
|
-
/**
|
|
40
|
-
* Extracts the full Event object type from the map for a given key.
|
|
41
|
-
*/
|
|
42
|
-
type Event<E extends EventTypeMap, T extends EventType<E>> = E[T];
|
|
43
|
-
/**
|
|
44
|
-
* Extracts the payload type from the map for a given key.
|
|
45
|
-
*/
|
|
46
|
-
type EventPayload<E extends EventTypeMap, T extends EventType<E>> = E[T]['payload'];
|
|
47
|
-
/**
|
|
48
|
-
* Array of available event type keys.
|
|
49
|
-
*/
|
|
50
|
-
type EventTypes<E extends EventTypeMap> = (keyof E)[];
|
|
51
|
-
/**
|
|
52
|
-
* Represents a valid key (event name) from the EventTypeMap.
|
|
53
|
-
*/
|
|
54
|
-
type EventType<E extends EventTypeMap> = keyof E;
|
|
55
|
-
/**
|
|
56
|
-
* Function signature for an event handler.
|
|
57
|
-
*/
|
|
58
|
-
type EventHandler<E extends EventTypeMap, T extends EventType<E>> = (event: Event<E, T>) => void;
|
|
59
|
-
/**
|
|
60
|
-
* Options when subscribing to an event.
|
|
61
|
-
*/
|
|
62
|
-
type EventSubscriptionOptions$1 = {
|
|
63
|
-
/** If true, the handler will be called immediately with the last emitted event of this type (if any). */
|
|
64
|
-
immediately?: boolean;
|
|
65
|
-
/** If true, the subscription will be automatically removed after the first event is handled. */
|
|
66
|
-
once?: boolean;
|
|
67
|
-
};
|
|
68
|
-
/**
|
|
69
|
-
* A type-safe event system that handles subscription, emission (including debouncing), and buffering of events.
|
|
70
|
-
* @template E The EventTypeMap defining all supported events.
|
|
71
|
-
*/
|
|
72
|
-
declare class EventSystem<E extends EventTypeMap> {
|
|
73
|
-
private _eventTypes;
|
|
74
|
-
private subscriberId;
|
|
75
|
-
private isProcessing;
|
|
76
|
-
private buffer;
|
|
77
|
-
private subscribers;
|
|
78
|
-
private subscribersTypeMap;
|
|
79
|
-
private lastEvents;
|
|
80
|
-
private debouncedEvents;
|
|
81
|
-
constructor(_eventTypes: EventTypes<E>);
|
|
82
|
-
get eventTypes(): EventTypes<E>;
|
|
83
|
-
/**
|
|
84
|
-
* Subscribes a handler to a specific event type.
|
|
85
|
-
* @param type The event type to subscribe to.
|
|
86
|
-
* @param handler The function to call when the event occurs.
|
|
87
|
-
* @param options Subscription options (once, immediately).
|
|
88
|
-
* @returns A subscription ID that can be used to unsubscribe.
|
|
89
|
-
*/
|
|
90
|
-
subscribe<T extends EventType<E>>(type: T, handler: EventHandler<E, T>, options?: EventSubscriptionOptions$1): number;
|
|
91
|
-
/**
|
|
92
|
-
* Subscribes a handler to multiple event types.
|
|
93
|
-
* @param types An array of event types to subscribe to.
|
|
94
|
-
* @param handler The function to call when any of the events occur.
|
|
95
|
-
* @param options Subscription options.
|
|
96
|
-
* @returns An array of subscription IDs.
|
|
97
|
-
*/
|
|
98
|
-
subscribeMany<T extends EventType<E>>(types: T[], handler: EventHandler<E, T>, options?: EventSubscriptionOptions$1): number[];
|
|
99
|
-
/**
|
|
100
|
-
* Unsubscribes a specific subscription by ID.
|
|
101
|
-
* @param id The subscription ID returned by subscribe().
|
|
102
|
-
* @returns True if the subscription was found and removed, false otherwise.
|
|
103
|
-
*/
|
|
104
|
-
unsubscribe(id: number): boolean;
|
|
105
|
-
/**
|
|
106
|
-
* Emits an event with the given type and payload.
|
|
107
|
-
* @param type The type of event to emit.
|
|
108
|
-
* @param payload The data associated with the event.
|
|
109
|
-
*/
|
|
110
|
-
emit<T extends EventType<E>, P extends EventPayload<E, T>>(type: T, payload: P): void;
|
|
111
|
-
/**
|
|
112
|
-
* Adds a fully constructed event object to the system.
|
|
113
|
-
* Similar to emit, but takes the full event object directly.
|
|
114
|
-
* Supports debouncing if options are provided.
|
|
115
|
-
* @param event The event object.
|
|
116
|
-
* @param options Options for the event push (e.g., debouncing).
|
|
117
|
-
*/
|
|
118
|
-
addEvent<T extends EventType<E>>(event: Event<E, T>, options?: PushEventOptions): void;
|
|
119
|
-
private handleDebouncedEvent;
|
|
120
|
-
private scheduleProcessing;
|
|
121
|
-
private processEvents;
|
|
122
|
-
private dequeue;
|
|
123
|
-
private setLastEvent;
|
|
124
|
-
private broadcastEvent;
|
|
125
|
-
}
|
|
126
|
-
//#endregion
|
|
127
|
-
//#region src/services/logger/index.d.ts
|
|
128
|
-
type Logger$1 = Logger;
|
|
129
|
-
//#endregion
|
|
130
7
|
//#region src/services/database/events/index.d.ts
|
|
131
8
|
declare const DatabaseEventTypes: {
|
|
132
9
|
readonly LocalQuery: "DATABASE_LOCAL_QUERY";
|
|
@@ -194,33 +71,6 @@ declare class RemoteDatabaseService extends AbstractDatabaseService {
|
|
|
194
71
|
invalidate(): Promise<void>;
|
|
195
72
|
}
|
|
196
73
|
//#endregion
|
|
197
|
-
//#region src/modules/sync/queue/queue-up.d.ts
|
|
198
|
-
type CreateEvent = {
|
|
199
|
-
type: 'create';
|
|
200
|
-
mutation_id: RecordId;
|
|
201
|
-
record_id: RecordId;
|
|
202
|
-
data: Record<string, unknown>;
|
|
203
|
-
record?: Record<string, unknown>;
|
|
204
|
-
tableName?: string;
|
|
205
|
-
options?: PushEventOptions;
|
|
206
|
-
};
|
|
207
|
-
type UpdateEvent = {
|
|
208
|
-
type: 'update';
|
|
209
|
-
mutation_id: RecordId;
|
|
210
|
-
record_id: RecordId;
|
|
211
|
-
data: Record<string, unknown>;
|
|
212
|
-
record?: Record<string, unknown>;
|
|
213
|
-
beforeRecord?: Record<string, unknown>;
|
|
214
|
-
options?: PushEventOptions;
|
|
215
|
-
};
|
|
216
|
-
type DeleteEvent = {
|
|
217
|
-
type: 'delete';
|
|
218
|
-
mutation_id: RecordId;
|
|
219
|
-
record_id: RecordId;
|
|
220
|
-
options?: PushEventOptions;
|
|
221
|
-
};
|
|
222
|
-
type UpEvent = CreateEvent | UpdateEvent | DeleteEvent;
|
|
223
|
-
//#endregion
|
|
224
74
|
//#region src/services/stream-processor/wasm-types.d.ts
|
|
225
75
|
interface WasmStreamUpdate {
|
|
226
76
|
query_id: string;
|
|
@@ -297,205 +147,6 @@ declare class StreamProcessorService {
|
|
|
297
147
|
private normalizeValue;
|
|
298
148
|
}
|
|
299
149
|
//#endregion
|
|
300
|
-
//#region src/types.d.ts
|
|
301
|
-
/**
|
|
302
|
-
* The type of storage backend to use for the local database.
|
|
303
|
-
* - 'memory': In-memory storage (transient).
|
|
304
|
-
* - 'indexeddb': IndexedDB storage (persistent).
|
|
305
|
-
*/
|
|
306
|
-
type StoreType = 'memory' | 'indexeddb';
|
|
307
|
-
/**
|
|
308
|
-
* Interface for a custom persistence client.
|
|
309
|
-
* Allows providing a custom storage mechanism for the local database.
|
|
310
|
-
*/
|
|
311
|
-
interface PersistenceClient {
|
|
312
|
-
/**
|
|
313
|
-
* Sets a value in the storage.
|
|
314
|
-
* @param key The key to set.
|
|
315
|
-
* @param value The value to store.
|
|
316
|
-
*/
|
|
317
|
-
set<T>(key: string, value: T): Promise<void>;
|
|
318
|
-
/**
|
|
319
|
-
* Gets a value from the storage.
|
|
320
|
-
* @param key The key to retrieve.
|
|
321
|
-
* @returns The stored value or null if not found.
|
|
322
|
-
*/
|
|
323
|
-
get<T>(key: string): Promise<T | null>;
|
|
324
|
-
/**
|
|
325
|
-
* Removes a value from the storage.
|
|
326
|
-
* @param key The key to remove.
|
|
327
|
-
*/
|
|
328
|
-
remove(key: string): Promise<void>;
|
|
329
|
-
}
|
|
330
|
-
/**
|
|
331
|
-
* Supported Time-To-Live (TTL) values for cached queries.
|
|
332
|
-
* Format: number + unit (m=minutes, h=hours, d=days).
|
|
333
|
-
*/
|
|
334
|
-
type QueryTimeToLive = '1m' | '5m' | '10m' | '15m' | '20m' | '25m' | '30m' | '1h' | '2h' | '3h' | '4h' | '5h' | '6h' | '7h' | '8h' | '9h' | '10h' | '11h' | '12h' | '1d';
|
|
335
|
-
/**
|
|
336
|
-
* Result object returned when a query is registered or executed.
|
|
337
|
-
*/
|
|
338
|
-
interface SpookyQueryResult {
|
|
339
|
-
/** The unique hash identifier for the query. */
|
|
340
|
-
hash: string;
|
|
341
|
-
}
|
|
342
|
-
type SpookyQueryResultPromise = Promise<SpookyQueryResult>;
|
|
343
|
-
interface EventSubscriptionOptions {
|
|
344
|
-
priority?: number;
|
|
345
|
-
}
|
|
346
|
-
/**
|
|
347
|
-
* Configuration options for the Spooky client.
|
|
348
|
-
* @template S The schema structure type.
|
|
349
|
-
*/
|
|
350
|
-
interface SpookyConfig<S extends SchemaStructure> {
|
|
351
|
-
/** Database connection configuration. */
|
|
352
|
-
database: {
|
|
353
|
-
/** The SurrealDB endpoint URL. */
|
|
354
|
-
endpoint?: string;
|
|
355
|
-
/** The namespace to use. */
|
|
356
|
-
namespace: string;
|
|
357
|
-
/** The database name. */
|
|
358
|
-
database: string;
|
|
359
|
-
/** The local store type implementation. */
|
|
360
|
-
store?: StoreType;
|
|
361
|
-
/** Authentication token. */
|
|
362
|
-
token?: string;
|
|
363
|
-
};
|
|
364
|
-
/** Unique client identifier. If not provided, one will be generated. */
|
|
365
|
-
clientId?: string;
|
|
366
|
-
/** The schema definition. */
|
|
367
|
-
schema: S;
|
|
368
|
-
/** The compiled SURQL schema string. */
|
|
369
|
-
schemaSurql: string;
|
|
370
|
-
/** Logging level. */
|
|
371
|
-
logLevel: Level$1;
|
|
372
|
-
/**
|
|
373
|
-
* Persistence client to use.
|
|
374
|
-
* Can be a custom implementation, 'surrealdb' (default), or 'localstorage'.
|
|
375
|
-
*/
|
|
376
|
-
persistenceClient?: PersistenceClient | 'surrealdb' | 'localstorage';
|
|
377
|
-
/** OpenTelemetry collector endpoint for telemetry data. */
|
|
378
|
-
otelEndpoint?: string;
|
|
379
|
-
/**
|
|
380
|
-
* Debounce time in milliseconds for stream updates.
|
|
381
|
-
* Defaults to 100ms.
|
|
382
|
-
*/
|
|
383
|
-
streamDebounceTime?: number;
|
|
384
|
-
}
|
|
385
|
-
type QueryHash = string;
|
|
386
|
-
type RecordVersionArray = Array<[string, number]>;
|
|
387
|
-
/**
|
|
388
|
-
* Represents the difference between two record version sets.
|
|
389
|
-
* Used for synchronizing local and remote states.
|
|
390
|
-
*/
|
|
391
|
-
interface RecordVersionDiff {
|
|
392
|
-
/** List of records added. */
|
|
393
|
-
added: Array<{
|
|
394
|
-
id: RecordId$1<string>;
|
|
395
|
-
version: number;
|
|
396
|
-
}>;
|
|
397
|
-
/** List of records updated. */
|
|
398
|
-
updated: Array<{
|
|
399
|
-
id: RecordId$1<string>;
|
|
400
|
-
version: number;
|
|
401
|
-
}>;
|
|
402
|
-
/** List of record IDs removed. */
|
|
403
|
-
removed: RecordId$1<string>[];
|
|
404
|
-
}
|
|
405
|
-
/**
|
|
406
|
-
* Configuration for a specific query instance.
|
|
407
|
-
* Stores metadata about the query's state, parameters, and versioning.
|
|
408
|
-
*/
|
|
409
|
-
interface QueryConfig {
|
|
410
|
-
/** The unique ID of the query config record. */
|
|
411
|
-
id: RecordId$1<string>;
|
|
412
|
-
/** The SURQL query string. */
|
|
413
|
-
surql: string;
|
|
414
|
-
/** Parameters used in the query. */
|
|
415
|
-
params: Record<string, any>;
|
|
416
|
-
/** The version array representing the local state of results. */
|
|
417
|
-
localArray: RecordVersionArray;
|
|
418
|
-
/** The version array representing the remote (server) state of results. */
|
|
419
|
-
remoteArray: RecordVersionArray;
|
|
420
|
-
/** Time-To-Live for this query. */
|
|
421
|
-
ttl: QueryTimeToLive;
|
|
422
|
-
/** Timestamp when the query was last accessed/active. */
|
|
423
|
-
lastActiveAt: Date;
|
|
424
|
-
/** The name of the table this query targets (if applicable). */
|
|
425
|
-
tableName: string;
|
|
426
|
-
}
|
|
427
|
-
type QueryConfigRecord = QueryConfig & {
|
|
428
|
-
id: string;
|
|
429
|
-
};
|
|
430
|
-
/**
|
|
431
|
-
* Internal state of a live query.
|
|
432
|
-
*/
|
|
433
|
-
interface QueryState {
|
|
434
|
-
/** The configuration for this query. */
|
|
435
|
-
config: QueryConfig;
|
|
436
|
-
/** The current cached records for this query. */
|
|
437
|
-
records: Record<string, any>[];
|
|
438
|
-
/** Timer for TTL expiration. */
|
|
439
|
-
ttlTimer: NodeJS.Timeout | null;
|
|
440
|
-
/** TTL duration in milliseconds. */
|
|
441
|
-
ttlDurationMs: number;
|
|
442
|
-
/** Number of times the query has been updated. */
|
|
443
|
-
updateCount: number;
|
|
444
|
-
}
|
|
445
|
-
type QueryUpdateCallback = (records: Record<string, any>[]) => void;
|
|
446
|
-
type MutationCallback = (mutations: UpEvent[]) => void;
|
|
447
|
-
type MutationEventType = 'create' | 'update' | 'delete';
|
|
448
|
-
/**
|
|
449
|
-
* Represents a mutation event (create, update, delete) to be synchronized.
|
|
450
|
-
*/
|
|
451
|
-
interface MutationEvent {
|
|
452
|
-
/** Example: 'create', 'update', or 'delete'. */
|
|
453
|
-
type: MutationEventType;
|
|
454
|
-
/** unique id of the mutation */
|
|
455
|
-
mutation_id: RecordId$1<string>;
|
|
456
|
-
/** The ID of the record being mutated. */
|
|
457
|
-
record_id: RecordId$1<string>;
|
|
458
|
-
/** The data payload for create/update operations. */
|
|
459
|
-
data?: any;
|
|
460
|
-
/** The full record data (optional context). */
|
|
461
|
-
record?: any;
|
|
462
|
-
/** Options for the mutation event (e.g., debounce settings). */
|
|
463
|
-
options?: PushEventOptions;
|
|
464
|
-
/** Timestamp when the event was created. */
|
|
465
|
-
createdAt: Date;
|
|
466
|
-
}
|
|
467
|
-
/**
|
|
468
|
-
* Options for run operations.
|
|
469
|
-
*/
|
|
470
|
-
interface RunOptions {
|
|
471
|
-
assignedTo?: string;
|
|
472
|
-
max_retries?: number;
|
|
473
|
-
retry_strategy?: 'linear' | 'exponential';
|
|
474
|
-
}
|
|
475
|
-
/**
|
|
476
|
-
* Options for update operations.
|
|
477
|
-
*/
|
|
478
|
-
interface UpdateOptions {
|
|
479
|
-
/**
|
|
480
|
-
* Debounce configuration for the update.
|
|
481
|
-
* If boolean, enables default debounce behavior.
|
|
482
|
-
*/
|
|
483
|
-
debounced?: boolean | DebounceOptions;
|
|
484
|
-
}
|
|
485
|
-
/**
|
|
486
|
-
* Configuration options for debouncing updates.
|
|
487
|
-
*/
|
|
488
|
-
interface DebounceOptions {
|
|
489
|
-
/**
|
|
490
|
-
* The key to use for debouncing.
|
|
491
|
-
* - 'recordId': Debounce based on the specific record ID. WARNING: IT WILL ONLY ACCEPT THE LATEST CHANGE AND DOES *NOT* MERGE THE PREVIOUS ONCES. IF YOU ARE UNSURE JUST USE 'recordId_x_fields'.
|
|
492
|
-
* - 'recordId_x_fields': Debounce based on record ID and specific fields.
|
|
493
|
-
*/
|
|
494
|
-
key?: 'recordId' | 'recordId_x_fields';
|
|
495
|
-
/** The debounce delay in milliseconds. */
|
|
496
|
-
delay?: number;
|
|
497
|
-
}
|
|
498
|
-
//#endregion
|
|
499
150
|
//#region src/modules/auth/events/index.d.ts
|
|
500
151
|
declare const AuthEventTypes: {
|
|
501
152
|
readonly AuthStateChanged: "AUTH_STATE_CHANGED";
|
|
@@ -608,4 +259,4 @@ declare function fileToUint8Array(file: File | Blob): Promise<Uint8Array>;
|
|
|
608
259
|
*/
|
|
609
260
|
|
|
610
261
|
//#endregion
|
|
611
|
-
export { AuthEventSystem, AuthEventTypeMap, AuthEventTypes, AuthService, BucketHandle, DebounceOptions, EventSubscriptionOptions,
|
|
262
|
+
export { AuthEventSystem, AuthEventTypeMap, AuthEventTypes, AuthService, BucketHandle, DebounceOptions, EventSubscriptionOptions, Level, MutationCallback, MutationEvent, MutationEventType, PersistenceClient, PinoTransmit, QueryConfig, QueryConfigRecord, QueryHash, QueryState, QueryTimeToLive, QueryUpdateCallback, RecordVersionArray, RecordVersionDiff, RunOptions, SpookyClient, SpookyConfig, SpookyQueryResult, SpookyQueryResultPromise, StoreType, UpdateOptions, createAuthEventSystem, fileToUint8Array };
|
package/dist/index.js
CHANGED
|
@@ -1110,82 +1110,14 @@ var RemoteDatabaseService = class extends AbstractDatabaseService {
|
|
|
1110
1110
|
|
|
1111
1111
|
//#endregion
|
|
1112
1112
|
//#region src/services/logger/index.ts
|
|
1113
|
-
function
|
|
1114
|
-
switch (level) {
|
|
1115
|
-
case "trace": return 1;
|
|
1116
|
-
case "debug": return 5;
|
|
1117
|
-
case "info": return 9;
|
|
1118
|
-
case "warn": return 13;
|
|
1119
|
-
case "error": return 17;
|
|
1120
|
-
case "fatal": return 21;
|
|
1121
|
-
default: return 9;
|
|
1122
|
-
}
|
|
1123
|
-
}
|
|
1124
|
-
async function loadOtelModules(otelEndpoint) {
|
|
1125
|
-
const [{ LoggerProvider, BatchLogRecordProcessor }, { OTLPLogExporter }, { resourceFromAttributes }, { ATTR_SERVICE_NAME }] = await Promise.all([
|
|
1126
|
-
import("@opentelemetry/sdk-logs"),
|
|
1127
|
-
import("@opentelemetry/exporter-logs-otlp-proto"),
|
|
1128
|
-
import("@opentelemetry/resources"),
|
|
1129
|
-
import("@opentelemetry/semantic-conventions")
|
|
1130
|
-
]);
|
|
1131
|
-
const loggerProvider = new LoggerProvider({
|
|
1132
|
-
resource: resourceFromAttributes({ [ATTR_SERVICE_NAME]: "spooky-client" }),
|
|
1133
|
-
processors: [new BatchLogRecordProcessor(new OTLPLogExporter({ url: otelEndpoint }))]
|
|
1134
|
-
});
|
|
1135
|
-
const otelLoggerCache = {};
|
|
1136
|
-
return (category) => {
|
|
1137
|
-
if (!otelLoggerCache[category]) otelLoggerCache[category] = loggerProvider.getLogger(category);
|
|
1138
|
-
return otelLoggerCache[category];
|
|
1139
|
-
};
|
|
1140
|
-
}
|
|
1141
|
-
function createLogger(level = "info", otelEndpoint) {
|
|
1113
|
+
function createLogger(level = "info", transmit) {
|
|
1142
1114
|
const browserConfig = {
|
|
1143
1115
|
asObject: true,
|
|
1144
1116
|
write: (o) => {
|
|
1145
1117
|
console.log(JSON.stringify(o));
|
|
1146
1118
|
}
|
|
1147
1119
|
};
|
|
1148
|
-
if (
|
|
1149
|
-
const otelReady = loadOtelModules(otelEndpoint);
|
|
1150
|
-
browserConfig.transmit = {
|
|
1151
|
-
level,
|
|
1152
|
-
send: (levelLabel, logEvent) => {
|
|
1153
|
-
otelReady.then((getOtelLogger) => {
|
|
1154
|
-
try {
|
|
1155
|
-
const messages = [...logEvent.messages];
|
|
1156
|
-
const severityNumber = mapLevelToSeverityNumber(levelLabel);
|
|
1157
|
-
let body = "";
|
|
1158
|
-
const msg = messages.pop();
|
|
1159
|
-
if (typeof msg === "string") body = msg;
|
|
1160
|
-
else if (msg) body = JSON.stringify(msg);
|
|
1161
|
-
let category = "spooky-client::unknown";
|
|
1162
|
-
const attributes = {};
|
|
1163
|
-
for (const msg of messages) if (typeof msg === "object") {
|
|
1164
|
-
if (msg.Category) {
|
|
1165
|
-
category = msg.Category;
|
|
1166
|
-
delete msg.Category;
|
|
1167
|
-
}
|
|
1168
|
-
Object.assign(attributes, msg);
|
|
1169
|
-
}
|
|
1170
|
-
getOtelLogger(category).emit({
|
|
1171
|
-
severityNumber,
|
|
1172
|
-
severityText: levelLabel.toUpperCase(),
|
|
1173
|
-
body,
|
|
1174
|
-
attributes: {
|
|
1175
|
-
...logEvent.bindings[0],
|
|
1176
|
-
...attributes
|
|
1177
|
-
},
|
|
1178
|
-
timestamp: new Date(logEvent.ts)
|
|
1179
|
-
});
|
|
1180
|
-
} catch (e) {
|
|
1181
|
-
console.warn("Failed to transmit log to OTEL endpoint", e);
|
|
1182
|
-
}
|
|
1183
|
-
}).catch((e) => {
|
|
1184
|
-
console.warn("Failed to load OpenTelemetry modules", e);
|
|
1185
|
-
});
|
|
1186
|
-
}
|
|
1187
|
-
};
|
|
1188
|
-
}
|
|
1120
|
+
if (transmit) browserConfig.transmit = transmit;
|
|
1189
1121
|
return pino({
|
|
1190
1122
|
level,
|
|
1191
1123
|
browser: browserConfig
|
|
@@ -3069,7 +3001,7 @@ var SpookyClient = class {
|
|
|
3069
3001
|
}
|
|
3070
3002
|
constructor(config) {
|
|
3071
3003
|
this.config = config;
|
|
3072
|
-
const logger = createLogger(config.logLevel ?? "info", config.
|
|
3004
|
+
const logger = createLogger(config.logLevel ?? "info", config.otelTransmit);
|
|
3073
3005
|
this.logger = logger.child({ service: "SpookyClient" });
|
|
3074
3006
|
this.logger.info({
|
|
3075
3007
|
config: {
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { c as PinoTransmit } from "../types.js";
|
|
2
|
+
import { Level } from "pino";
|
|
3
|
+
|
|
4
|
+
//#region src/otel/index.d.ts
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Creates a pino browser transmit object that forwards logs to an OpenTelemetry collector.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* import { createOtelTransmit } from '@spooky-sync/core/otel';
|
|
12
|
+
*
|
|
13
|
+
* new SpookyClient({
|
|
14
|
+
* // ...
|
|
15
|
+
* otelTransmit: createOtelTransmit('http://localhost:4318/v1/logs'),
|
|
16
|
+
* });
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
declare function createOtelTransmit(endpoint: string, level?: Level): PinoTransmit;
|
|
20
|
+
//#endregion
|
|
21
|
+
export { createOtelTransmit };
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
//#region src/otel/index.ts
|
|
2
|
+
function mapLevelToSeverityNumber(level) {
|
|
3
|
+
switch (level) {
|
|
4
|
+
case "trace": return 1;
|
|
5
|
+
case "debug": return 5;
|
|
6
|
+
case "info": return 9;
|
|
7
|
+
case "warn": return 13;
|
|
8
|
+
case "error": return 17;
|
|
9
|
+
case "fatal": return 21;
|
|
10
|
+
default: return 9;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
async function loadOtelModules(otelEndpoint) {
|
|
14
|
+
const [{ LoggerProvider, BatchLogRecordProcessor }, { OTLPLogExporter }, { resourceFromAttributes }, { ATTR_SERVICE_NAME }] = await Promise.all([
|
|
15
|
+
import("@opentelemetry/sdk-logs"),
|
|
16
|
+
import("@opentelemetry/exporter-logs-otlp-proto"),
|
|
17
|
+
import("@opentelemetry/resources"),
|
|
18
|
+
import("@opentelemetry/semantic-conventions")
|
|
19
|
+
]);
|
|
20
|
+
const loggerProvider = new LoggerProvider({
|
|
21
|
+
resource: resourceFromAttributes({ [ATTR_SERVICE_NAME]: "spooky-client" }),
|
|
22
|
+
processors: [new BatchLogRecordProcessor(new OTLPLogExporter({ url: otelEndpoint }))]
|
|
23
|
+
});
|
|
24
|
+
const otelLoggerCache = {};
|
|
25
|
+
return (category) => {
|
|
26
|
+
if (!otelLoggerCache[category]) otelLoggerCache[category] = loggerProvider.getLogger(category);
|
|
27
|
+
return otelLoggerCache[category];
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Creates a pino browser transmit object that forwards logs to an OpenTelemetry collector.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```ts
|
|
35
|
+
* import { createOtelTransmit } from '@spooky-sync/core/otel';
|
|
36
|
+
*
|
|
37
|
+
* new SpookyClient({
|
|
38
|
+
* // ...
|
|
39
|
+
* otelTransmit: createOtelTransmit('http://localhost:4318/v1/logs'),
|
|
40
|
+
* });
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
function createOtelTransmit(endpoint, level = "info") {
|
|
44
|
+
const otelReady = loadOtelModules(endpoint);
|
|
45
|
+
return {
|
|
46
|
+
level,
|
|
47
|
+
send: (levelLabel, logEvent) => {
|
|
48
|
+
otelReady.then((getOtelLogger) => {
|
|
49
|
+
try {
|
|
50
|
+
const messages = [...logEvent.messages];
|
|
51
|
+
const severityNumber = mapLevelToSeverityNumber(levelLabel);
|
|
52
|
+
let body = "";
|
|
53
|
+
const msg = messages.pop();
|
|
54
|
+
if (typeof msg === "string") body = msg;
|
|
55
|
+
else if (msg) body = JSON.stringify(msg);
|
|
56
|
+
let category = "spooky-client::unknown";
|
|
57
|
+
const attributes = {};
|
|
58
|
+
for (const msg of messages) if (typeof msg === "object") {
|
|
59
|
+
if (msg.Category) {
|
|
60
|
+
category = msg.Category;
|
|
61
|
+
delete msg.Category;
|
|
62
|
+
}
|
|
63
|
+
Object.assign(attributes, msg);
|
|
64
|
+
}
|
|
65
|
+
getOtelLogger(category).emit({
|
|
66
|
+
severityNumber,
|
|
67
|
+
severityText: levelLabel.toUpperCase(),
|
|
68
|
+
body,
|
|
69
|
+
attributes: {
|
|
70
|
+
...logEvent.bindings[0],
|
|
71
|
+
...attributes
|
|
72
|
+
},
|
|
73
|
+
timestamp: new Date(logEvent.ts)
|
|
74
|
+
});
|
|
75
|
+
} catch (e) {
|
|
76
|
+
console.warn("Failed to transmit log to OTEL endpoint", e);
|
|
77
|
+
}
|
|
78
|
+
}).catch((e) => {
|
|
79
|
+
console.warn("Failed to load OpenTelemetry modules", e);
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
//#endregion
|
|
86
|
+
export { createOtelTransmit };
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,359 @@
|
|
|
1
|
+
import { RecordId } from "surrealdb";
|
|
2
|
+
import { RecordId as RecordId$1, SchemaStructure } from "@spooky-sync/query-builder";
|
|
3
|
+
import { Level, Level as Level$1, Logger, LoggerOptions } from "pino";
|
|
4
|
+
|
|
5
|
+
//#region src/events/index.d.ts
|
|
6
|
+
/**
|
|
7
|
+
* Utility type to define the payload structure of an event.
|
|
8
|
+
* If the payload type P is never, it defines payload as undefined.
|
|
9
|
+
*/
|
|
10
|
+
type EventPayloadDefinition<P> = [P] extends [never] ? {
|
|
11
|
+
payload: undefined;
|
|
12
|
+
} : {
|
|
13
|
+
payload: P;
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Defines the structure of an event with a specific type and payload.
|
|
17
|
+
* @template T The string literal type of the event.
|
|
18
|
+
* @template P The type of the event payload.
|
|
19
|
+
*/
|
|
20
|
+
type EventDefinition<T extends string, P> = {
|
|
21
|
+
type: T;
|
|
22
|
+
} & EventPayloadDefinition<P>;
|
|
23
|
+
/**
|
|
24
|
+
* A map of event types to their definitions.
|
|
25
|
+
* Keys are event names, values are EventDefinitions.
|
|
26
|
+
*/
|
|
27
|
+
type EventTypeMap = Record<string, EventDefinition<any, unknown> | EventDefinition<any, never>>;
|
|
28
|
+
/**
|
|
29
|
+
* Options for pushing/emitting events.
|
|
30
|
+
*/
|
|
31
|
+
interface PushEventOptions {
|
|
32
|
+
/** Configuration for debouncing the event. */
|
|
33
|
+
debounced?: {
|
|
34
|
+
key: string;
|
|
35
|
+
delay: number;
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Extracts the full Event object type from the map for a given key.
|
|
40
|
+
*/
|
|
41
|
+
type Event<E extends EventTypeMap, T extends EventType<E>> = E[T];
|
|
42
|
+
/**
|
|
43
|
+
* Extracts the payload type from the map for a given key.
|
|
44
|
+
*/
|
|
45
|
+
type EventPayload<E extends EventTypeMap, T extends EventType<E>> = E[T]['payload'];
|
|
46
|
+
/**
|
|
47
|
+
* Array of available event type keys.
|
|
48
|
+
*/
|
|
49
|
+
type EventTypes<E extends EventTypeMap> = (keyof E)[];
|
|
50
|
+
/**
|
|
51
|
+
* Represents a valid key (event name) from the EventTypeMap.
|
|
52
|
+
*/
|
|
53
|
+
type EventType<E extends EventTypeMap> = keyof E;
|
|
54
|
+
/**
|
|
55
|
+
* Function signature for an event handler.
|
|
56
|
+
*/
|
|
57
|
+
type EventHandler<E extends EventTypeMap, T extends EventType<E>> = (event: Event<E, T>) => void;
|
|
58
|
+
/**
|
|
59
|
+
* Options when subscribing to an event.
|
|
60
|
+
*/
|
|
61
|
+
type EventSubscriptionOptions$1 = {
|
|
62
|
+
/** If true, the handler will be called immediately with the last emitted event of this type (if any). */
|
|
63
|
+
immediately?: boolean;
|
|
64
|
+
/** If true, the subscription will be automatically removed after the first event is handled. */
|
|
65
|
+
once?: boolean;
|
|
66
|
+
};
|
|
67
|
+
/**
|
|
68
|
+
* A type-safe event system that handles subscription, emission (including debouncing), and buffering of events.
|
|
69
|
+
* @template E The EventTypeMap defining all supported events.
|
|
70
|
+
*/
|
|
71
|
+
declare class EventSystem<E extends EventTypeMap> {
|
|
72
|
+
private _eventTypes;
|
|
73
|
+
private subscriberId;
|
|
74
|
+
private isProcessing;
|
|
75
|
+
private buffer;
|
|
76
|
+
private subscribers;
|
|
77
|
+
private subscribersTypeMap;
|
|
78
|
+
private lastEvents;
|
|
79
|
+
private debouncedEvents;
|
|
80
|
+
constructor(_eventTypes: EventTypes<E>);
|
|
81
|
+
get eventTypes(): EventTypes<E>;
|
|
82
|
+
/**
|
|
83
|
+
* Subscribes a handler to a specific event type.
|
|
84
|
+
* @param type The event type to subscribe to.
|
|
85
|
+
* @param handler The function to call when the event occurs.
|
|
86
|
+
* @param options Subscription options (once, immediately).
|
|
87
|
+
* @returns A subscription ID that can be used to unsubscribe.
|
|
88
|
+
*/
|
|
89
|
+
subscribe<T extends EventType<E>>(type: T, handler: EventHandler<E, T>, options?: EventSubscriptionOptions$1): number;
|
|
90
|
+
/**
|
|
91
|
+
* Subscribes a handler to multiple event types.
|
|
92
|
+
* @param types An array of event types to subscribe to.
|
|
93
|
+
* @param handler The function to call when any of the events occur.
|
|
94
|
+
* @param options Subscription options.
|
|
95
|
+
* @returns An array of subscription IDs.
|
|
96
|
+
*/
|
|
97
|
+
subscribeMany<T extends EventType<E>>(types: T[], handler: EventHandler<E, T>, options?: EventSubscriptionOptions$1): number[];
|
|
98
|
+
/**
|
|
99
|
+
* Unsubscribes a specific subscription by ID.
|
|
100
|
+
* @param id The subscription ID returned by subscribe().
|
|
101
|
+
* @returns True if the subscription was found and removed, false otherwise.
|
|
102
|
+
*/
|
|
103
|
+
unsubscribe(id: number): boolean;
|
|
104
|
+
/**
|
|
105
|
+
* Emits an event with the given type and payload.
|
|
106
|
+
* @param type The type of event to emit.
|
|
107
|
+
* @param payload The data associated with the event.
|
|
108
|
+
*/
|
|
109
|
+
emit<T extends EventType<E>, P extends EventPayload<E, T>>(type: T, payload: P): void;
|
|
110
|
+
/**
|
|
111
|
+
* Adds a fully constructed event object to the system.
|
|
112
|
+
* Similar to emit, but takes the full event object directly.
|
|
113
|
+
* Supports debouncing if options are provided.
|
|
114
|
+
* @param event The event object.
|
|
115
|
+
* @param options Options for the event push (e.g., debouncing).
|
|
116
|
+
*/
|
|
117
|
+
addEvent<T extends EventType<E>>(event: Event<E, T>, options?: PushEventOptions): void;
|
|
118
|
+
private handleDebouncedEvent;
|
|
119
|
+
private scheduleProcessing;
|
|
120
|
+
private processEvents;
|
|
121
|
+
private dequeue;
|
|
122
|
+
private setLastEvent;
|
|
123
|
+
private broadcastEvent;
|
|
124
|
+
}
|
|
125
|
+
//#endregion
|
|
126
|
+
//#region src/services/logger/index.d.ts
|
|
127
|
+
type Logger$1 = Logger;
|
|
128
|
+
//#endregion
|
|
129
|
+
//#region src/modules/sync/queue/queue-up.d.ts
|
|
130
|
+
type CreateEvent = {
|
|
131
|
+
type: 'create';
|
|
132
|
+
mutation_id: RecordId;
|
|
133
|
+
record_id: RecordId;
|
|
134
|
+
data: Record<string, unknown>;
|
|
135
|
+
record?: Record<string, unknown>;
|
|
136
|
+
tableName?: string;
|
|
137
|
+
options?: PushEventOptions;
|
|
138
|
+
};
|
|
139
|
+
type UpdateEvent = {
|
|
140
|
+
type: 'update';
|
|
141
|
+
mutation_id: RecordId;
|
|
142
|
+
record_id: RecordId;
|
|
143
|
+
data: Record<string, unknown>;
|
|
144
|
+
record?: Record<string, unknown>;
|
|
145
|
+
beforeRecord?: Record<string, unknown>;
|
|
146
|
+
options?: PushEventOptions;
|
|
147
|
+
};
|
|
148
|
+
type DeleteEvent = {
|
|
149
|
+
type: 'delete';
|
|
150
|
+
mutation_id: RecordId;
|
|
151
|
+
record_id: RecordId;
|
|
152
|
+
options?: PushEventOptions;
|
|
153
|
+
};
|
|
154
|
+
type UpEvent = CreateEvent | UpdateEvent | DeleteEvent;
|
|
155
|
+
//#endregion
|
|
156
|
+
//#region src/types.d.ts
|
|
157
|
+
/**
|
|
158
|
+
* A pino browser transmit object for forwarding logs to an external sink (e.g. OpenTelemetry).
|
|
159
|
+
*/
|
|
160
|
+
type PinoTransmit = NonNullable<NonNullable<LoggerOptions['browser']>['transmit']>;
|
|
161
|
+
/**
|
|
162
|
+
* The type of storage backend to use for the local database.
|
|
163
|
+
* - 'memory': In-memory storage (transient).
|
|
164
|
+
* - 'indexeddb': IndexedDB storage (persistent).
|
|
165
|
+
*/
|
|
166
|
+
type StoreType = 'memory' | 'indexeddb';
|
|
167
|
+
/**
|
|
168
|
+
* Interface for a custom persistence client.
|
|
169
|
+
* Allows providing a custom storage mechanism for the local database.
|
|
170
|
+
*/
|
|
171
|
+
interface PersistenceClient {
|
|
172
|
+
/**
|
|
173
|
+
* Sets a value in the storage.
|
|
174
|
+
* @param key The key to set.
|
|
175
|
+
* @param value The value to store.
|
|
176
|
+
*/
|
|
177
|
+
set<T>(key: string, value: T): Promise<void>;
|
|
178
|
+
/**
|
|
179
|
+
* Gets a value from the storage.
|
|
180
|
+
* @param key The key to retrieve.
|
|
181
|
+
* @returns The stored value or null if not found.
|
|
182
|
+
*/
|
|
183
|
+
get<T>(key: string): Promise<T | null>;
|
|
184
|
+
/**
|
|
185
|
+
* Removes a value from the storage.
|
|
186
|
+
* @param key The key to remove.
|
|
187
|
+
*/
|
|
188
|
+
remove(key: string): Promise<void>;
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Supported Time-To-Live (TTL) values for cached queries.
|
|
192
|
+
* Format: number + unit (m=minutes, h=hours, d=days).
|
|
193
|
+
*/
|
|
194
|
+
type QueryTimeToLive = '1m' | '5m' | '10m' | '15m' | '20m' | '25m' | '30m' | '1h' | '2h' | '3h' | '4h' | '5h' | '6h' | '7h' | '8h' | '9h' | '10h' | '11h' | '12h' | '1d';
|
|
195
|
+
/**
|
|
196
|
+
* Result object returned when a query is registered or executed.
|
|
197
|
+
*/
|
|
198
|
+
interface SpookyQueryResult {
|
|
199
|
+
/** The unique hash identifier for the query. */
|
|
200
|
+
hash: string;
|
|
201
|
+
}
|
|
202
|
+
type SpookyQueryResultPromise = Promise<SpookyQueryResult>;
|
|
203
|
+
interface EventSubscriptionOptions {
|
|
204
|
+
priority?: number;
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Configuration options for the Spooky client.
|
|
208
|
+
* @template S The schema structure type.
|
|
209
|
+
*/
|
|
210
|
+
interface SpookyConfig<S extends SchemaStructure> {
|
|
211
|
+
/** Database connection configuration. */
|
|
212
|
+
database: {
|
|
213
|
+
/** The SurrealDB endpoint URL. */
|
|
214
|
+
endpoint?: string;
|
|
215
|
+
/** The namespace to use. */
|
|
216
|
+
namespace: string;
|
|
217
|
+
/** The database name. */
|
|
218
|
+
database: string;
|
|
219
|
+
/** The local store type implementation. */
|
|
220
|
+
store?: StoreType;
|
|
221
|
+
/** Authentication token. */
|
|
222
|
+
token?: string;
|
|
223
|
+
};
|
|
224
|
+
/** Unique client identifier. If not provided, one will be generated. */
|
|
225
|
+
clientId?: string;
|
|
226
|
+
/** The schema definition. */
|
|
227
|
+
schema: S;
|
|
228
|
+
/** The compiled SURQL schema string. */
|
|
229
|
+
schemaSurql: string;
|
|
230
|
+
/** Logging level. */
|
|
231
|
+
logLevel: Level;
|
|
232
|
+
/**
|
|
233
|
+
* Persistence client to use.
|
|
234
|
+
* Can be a custom implementation, 'surrealdb' (default), or 'localstorage'.
|
|
235
|
+
*/
|
|
236
|
+
persistenceClient?: PersistenceClient | 'surrealdb' | 'localstorage';
|
|
237
|
+
/** A pino browser transmit object for forwarding logs (e.g. via @spooky-sync/core/otel). */
|
|
238
|
+
otelTransmit?: PinoTransmit;
|
|
239
|
+
/**
|
|
240
|
+
* Debounce time in milliseconds for stream updates.
|
|
241
|
+
* Defaults to 100ms.
|
|
242
|
+
*/
|
|
243
|
+
streamDebounceTime?: number;
|
|
244
|
+
}
|
|
245
|
+
type QueryHash = string;
|
|
246
|
+
type RecordVersionArray = Array<[string, number]>;
|
|
247
|
+
/**
|
|
248
|
+
* Represents the difference between two record version sets.
|
|
249
|
+
* Used for synchronizing local and remote states.
|
|
250
|
+
*/
|
|
251
|
+
interface RecordVersionDiff {
|
|
252
|
+
/** List of records added. */
|
|
253
|
+
added: Array<{
|
|
254
|
+
id: RecordId$1<string>;
|
|
255
|
+
version: number;
|
|
256
|
+
}>;
|
|
257
|
+
/** List of records updated. */
|
|
258
|
+
updated: Array<{
|
|
259
|
+
id: RecordId$1<string>;
|
|
260
|
+
version: number;
|
|
261
|
+
}>;
|
|
262
|
+
/** List of record IDs removed. */
|
|
263
|
+
removed: RecordId$1<string>[];
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* Configuration for a specific query instance.
|
|
267
|
+
* Stores metadata about the query's state, parameters, and versioning.
|
|
268
|
+
*/
|
|
269
|
+
interface QueryConfig {
|
|
270
|
+
/** The unique ID of the query config record. */
|
|
271
|
+
id: RecordId$1<string>;
|
|
272
|
+
/** The SURQL query string. */
|
|
273
|
+
surql: string;
|
|
274
|
+
/** Parameters used in the query. */
|
|
275
|
+
params: Record<string, any>;
|
|
276
|
+
/** The version array representing the local state of results. */
|
|
277
|
+
localArray: RecordVersionArray;
|
|
278
|
+
/** The version array representing the remote (server) state of results. */
|
|
279
|
+
remoteArray: RecordVersionArray;
|
|
280
|
+
/** Time-To-Live for this query. */
|
|
281
|
+
ttl: QueryTimeToLive;
|
|
282
|
+
/** Timestamp when the query was last accessed/active. */
|
|
283
|
+
lastActiveAt: Date;
|
|
284
|
+
/** The name of the table this query targets (if applicable). */
|
|
285
|
+
tableName: string;
|
|
286
|
+
}
|
|
287
|
+
type QueryConfigRecord = QueryConfig & {
|
|
288
|
+
id: string;
|
|
289
|
+
};
|
|
290
|
+
/**
|
|
291
|
+
* Internal state of a live query.
|
|
292
|
+
*/
|
|
293
|
+
interface QueryState {
|
|
294
|
+
/** The configuration for this query. */
|
|
295
|
+
config: QueryConfig;
|
|
296
|
+
/** The current cached records for this query. */
|
|
297
|
+
records: Record<string, any>[];
|
|
298
|
+
/** Timer for TTL expiration. */
|
|
299
|
+
ttlTimer: NodeJS.Timeout | null;
|
|
300
|
+
/** TTL duration in milliseconds. */
|
|
301
|
+
ttlDurationMs: number;
|
|
302
|
+
/** Number of times the query has been updated. */
|
|
303
|
+
updateCount: number;
|
|
304
|
+
}
|
|
305
|
+
type QueryUpdateCallback = (records: Record<string, any>[]) => void;
|
|
306
|
+
type MutationCallback = (mutations: UpEvent[]) => void;
|
|
307
|
+
type MutationEventType = 'create' | 'update' | 'delete';
|
|
308
|
+
/**
|
|
309
|
+
* Represents a mutation event (create, update, delete) to be synchronized.
|
|
310
|
+
*/
|
|
311
|
+
interface MutationEvent {
|
|
312
|
+
/** Example: 'create', 'update', or 'delete'. */
|
|
313
|
+
type: MutationEventType;
|
|
314
|
+
/** unique id of the mutation */
|
|
315
|
+
mutation_id: RecordId$1<string>;
|
|
316
|
+
/** The ID of the record being mutated. */
|
|
317
|
+
record_id: RecordId$1<string>;
|
|
318
|
+
/** The data payload for create/update operations. */
|
|
319
|
+
data?: any;
|
|
320
|
+
/** The full record data (optional context). */
|
|
321
|
+
record?: any;
|
|
322
|
+
/** Options for the mutation event (e.g., debounce settings). */
|
|
323
|
+
options?: PushEventOptions;
|
|
324
|
+
/** Timestamp when the event was created. */
|
|
325
|
+
createdAt: Date;
|
|
326
|
+
}
|
|
327
|
+
/**
|
|
328
|
+
* Options for run operations.
|
|
329
|
+
*/
|
|
330
|
+
interface RunOptions {
|
|
331
|
+
assignedTo?: string;
|
|
332
|
+
max_retries?: number;
|
|
333
|
+
retry_strategy?: 'linear' | 'exponential';
|
|
334
|
+
}
|
|
335
|
+
/**
|
|
336
|
+
* Options for update operations.
|
|
337
|
+
*/
|
|
338
|
+
interface UpdateOptions {
|
|
339
|
+
/**
|
|
340
|
+
* Debounce configuration for the update.
|
|
341
|
+
* If boolean, enables default debounce behavior.
|
|
342
|
+
*/
|
|
343
|
+
debounced?: boolean | DebounceOptions;
|
|
344
|
+
}
|
|
345
|
+
/**
|
|
346
|
+
* Configuration options for debouncing updates.
|
|
347
|
+
*/
|
|
348
|
+
interface DebounceOptions {
|
|
349
|
+
/**
|
|
350
|
+
* The key to use for debouncing.
|
|
351
|
+
* - 'recordId': Debounce based on the specific record ID. WARNING: IT WILL ONLY ACCEPT THE LATEST CHANGE AND DOES *NOT* MERGE THE PREVIOUS ONCES. IF YOU ARE UNSURE JUST USE 'recordId_x_fields'.
|
|
352
|
+
* - 'recordId_x_fields': Debounce based on record ID and specific fields.
|
|
353
|
+
*/
|
|
354
|
+
key?: 'recordId' | 'recordId_x_fields';
|
|
355
|
+
/** The debounce delay in milliseconds. */
|
|
356
|
+
delay?: number;
|
|
357
|
+
}
|
|
358
|
+
//#endregion
|
|
359
|
+
export { Logger$1 as C, UpdateOptions as S, EventSystem as T, RunOptions as _, MutationEvent as a, SpookyQueryResultPromise as b, PinoTransmit as c, QueryHash as d, QueryState as f, RecordVersionDiff as g, RecordVersionArray as h, MutationCallback as i, QueryConfig as l, QueryUpdateCallback as m, EventSubscriptionOptions as n, MutationEventType as o, QueryTimeToLive as p, Level$1 as r, PersistenceClient as s, DebounceOptions as t, QueryConfigRecord as u, SpookyConfig as v, EventDefinition as w, StoreType as x, SpookyQueryResult as y };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@spooky-sync/core",
|
|
3
|
-
"version": "0.0.1-canary.
|
|
3
|
+
"version": "0.0.1-canary.18",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -8,6 +8,10 @@
|
|
|
8
8
|
".": {
|
|
9
9
|
"types": "./dist/index.d.ts",
|
|
10
10
|
"import": "./dist/index.js"
|
|
11
|
+
},
|
|
12
|
+
"./otel": {
|
|
13
|
+
"types": "./dist/otel/index.d.ts",
|
|
14
|
+
"import": "./dist/otel/index.js"
|
|
11
15
|
}
|
|
12
16
|
},
|
|
13
17
|
"scripts": {
|
|
@@ -29,8 +29,8 @@ interface SpookyConfig<S extends SchemaStructure> {
|
|
|
29
29
|
* - Custom: Provide an object implementing PersistenceClient
|
|
30
30
|
*/
|
|
31
31
|
persistenceClient?: PersistenceClient | 'surrealdb' | 'localstorage';
|
|
32
|
-
/**
|
|
33
|
-
|
|
32
|
+
/** A pino browser transmit object for forwarding logs (e.g. via @spooky-sync/core/otel) */
|
|
33
|
+
otelTransmit?: PinoTransmit;
|
|
34
34
|
/** Debounce time in ms for stream updates (default: 100) */
|
|
35
35
|
streamDebounceTime?: number;
|
|
36
36
|
}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { Level } from 'pino';
|
|
2
|
+
import { PinoTransmit } from '../types';
|
|
3
|
+
|
|
4
|
+
// Map pino levels to OTEL severity numbers
|
|
5
|
+
// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/logs/data-model.md#severity-fields
|
|
6
|
+
function mapLevelToSeverityNumber(level: string): number {
|
|
7
|
+
switch (level) {
|
|
8
|
+
case 'trace':
|
|
9
|
+
return 1;
|
|
10
|
+
case 'debug':
|
|
11
|
+
return 5;
|
|
12
|
+
case 'info':
|
|
13
|
+
return 9;
|
|
14
|
+
case 'warn':
|
|
15
|
+
return 13;
|
|
16
|
+
case 'error':
|
|
17
|
+
return 17;
|
|
18
|
+
case 'fatal':
|
|
19
|
+
return 21;
|
|
20
|
+
default:
|
|
21
|
+
return 9;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
async function loadOtelModules(otelEndpoint: string) {
|
|
26
|
+
const [{ LoggerProvider, BatchLogRecordProcessor }, { OTLPLogExporter }, { resourceFromAttributes }, { ATTR_SERVICE_NAME }] =
|
|
27
|
+
await Promise.all([
|
|
28
|
+
import('@opentelemetry/sdk-logs'),
|
|
29
|
+
import('@opentelemetry/exporter-logs-otlp-proto'),
|
|
30
|
+
import('@opentelemetry/resources'),
|
|
31
|
+
import('@opentelemetry/semantic-conventions'),
|
|
32
|
+
]);
|
|
33
|
+
|
|
34
|
+
const resource = resourceFromAttributes({
|
|
35
|
+
[ATTR_SERVICE_NAME]: 'spooky-client',
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
const exporter = new OTLPLogExporter({
|
|
39
|
+
url: otelEndpoint,
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
const loggerProvider = new LoggerProvider({
|
|
43
|
+
resource,
|
|
44
|
+
processors: [new BatchLogRecordProcessor(exporter)],
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
const otelLoggerCache: Record<string, ReturnType<typeof loggerProvider.getLogger>> = {};
|
|
48
|
+
|
|
49
|
+
return (category: string) => {
|
|
50
|
+
if (!otelLoggerCache[category]) {
|
|
51
|
+
otelLoggerCache[category] = loggerProvider.getLogger(category);
|
|
52
|
+
}
|
|
53
|
+
return otelLoggerCache[category];
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Creates a pino browser transmit object that forwards logs to an OpenTelemetry collector.
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* ```ts
|
|
62
|
+
* import { createOtelTransmit } from '@spooky-sync/core/otel';
|
|
63
|
+
*
|
|
64
|
+
* new SpookyClient({
|
|
65
|
+
* // ...
|
|
66
|
+
* otelTransmit: createOtelTransmit('http://localhost:4318/v1/logs'),
|
|
67
|
+
* });
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
export function createOtelTransmit(endpoint: string, level: Level = 'info'): PinoTransmit {
|
|
71
|
+
// Start loading OTel modules eagerly (don't await — we're synchronous)
|
|
72
|
+
const otelReady = loadOtelModules(endpoint);
|
|
73
|
+
|
|
74
|
+
return {
|
|
75
|
+
level: level,
|
|
76
|
+
send: (levelLabel: string, logEvent: any) => {
|
|
77
|
+
otelReady.then((getOtelLogger) => {
|
|
78
|
+
try {
|
|
79
|
+
const messages = [...logEvent.messages];
|
|
80
|
+
const severityNumber = mapLevelToSeverityNumber(levelLabel);
|
|
81
|
+
|
|
82
|
+
// Construct the message body
|
|
83
|
+
let body = '';
|
|
84
|
+
const msg = messages.pop();
|
|
85
|
+
|
|
86
|
+
if (typeof msg === 'string') {
|
|
87
|
+
body = msg;
|
|
88
|
+
} else if (msg) {
|
|
89
|
+
body = JSON.stringify(msg);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
let category = 'spooky-client::unknown';
|
|
93
|
+
|
|
94
|
+
const attributes = {};
|
|
95
|
+
for (const msg of messages) {
|
|
96
|
+
if (typeof msg === 'object') {
|
|
97
|
+
if (msg.Category) {
|
|
98
|
+
category = msg.Category;
|
|
99
|
+
delete msg.Category;
|
|
100
|
+
}
|
|
101
|
+
Object.assign(attributes, msg);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// Emit to OTEL SDK
|
|
106
|
+
getOtelLogger(category).emit({
|
|
107
|
+
severityNumber: severityNumber,
|
|
108
|
+
severityText: levelLabel.toUpperCase(),
|
|
109
|
+
body: body,
|
|
110
|
+
attributes: {
|
|
111
|
+
...logEvent.bindings[0],
|
|
112
|
+
...attributes,
|
|
113
|
+
},
|
|
114
|
+
timestamp: new Date(logEvent.ts),
|
|
115
|
+
});
|
|
116
|
+
} catch (e) {
|
|
117
|
+
console.warn('Failed to transmit log to OTEL endpoint', e);
|
|
118
|
+
}
|
|
119
|
+
}).catch((e) => {
|
|
120
|
+
console.warn('Failed to load OpenTelemetry modules', e);
|
|
121
|
+
});
|
|
122
|
+
},
|
|
123
|
+
};
|
|
124
|
+
}
|
|
@@ -1,61 +1,9 @@
|
|
|
1
1
|
import pino, { Level, type Logger as PinoLogger, type LoggerOptions } from 'pino';
|
|
2
|
+
import { PinoTransmit } from '../../types';
|
|
2
3
|
|
|
3
4
|
export type Logger = PinoLogger;
|
|
4
5
|
|
|
5
|
-
|
|
6
|
-
// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/logs/data-model.md#severity-fields
|
|
7
|
-
function mapLevelToSeverityNumber(level: string): number {
|
|
8
|
-
switch (level) {
|
|
9
|
-
case 'trace':
|
|
10
|
-
return 1;
|
|
11
|
-
case 'debug':
|
|
12
|
-
return 5;
|
|
13
|
-
case 'info':
|
|
14
|
-
return 9;
|
|
15
|
-
case 'warn':
|
|
16
|
-
return 13;
|
|
17
|
-
case 'error':
|
|
18
|
-
return 17;
|
|
19
|
-
case 'fatal':
|
|
20
|
-
return 21;
|
|
21
|
-
default:
|
|
22
|
-
return 9;
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
async function loadOtelModules(otelEndpoint: string) {
|
|
27
|
-
const [{ LoggerProvider, BatchLogRecordProcessor }, { OTLPLogExporter }, { resourceFromAttributes }, { ATTR_SERVICE_NAME }] =
|
|
28
|
-
await Promise.all([
|
|
29
|
-
import('@opentelemetry/sdk-logs'),
|
|
30
|
-
import('@opentelemetry/exporter-logs-otlp-proto'),
|
|
31
|
-
import('@opentelemetry/resources'),
|
|
32
|
-
import('@opentelemetry/semantic-conventions'),
|
|
33
|
-
]);
|
|
34
|
-
|
|
35
|
-
const resource = resourceFromAttributes({
|
|
36
|
-
[ATTR_SERVICE_NAME]: 'spooky-client',
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
const exporter = new OTLPLogExporter({
|
|
40
|
-
url: otelEndpoint,
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
const loggerProvider = new LoggerProvider({
|
|
44
|
-
resource,
|
|
45
|
-
processors: [new BatchLogRecordProcessor(exporter)],
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
const otelLoggerCache: Record<string, ReturnType<typeof loggerProvider.getLogger>> = {};
|
|
49
|
-
|
|
50
|
-
return (category: string) => {
|
|
51
|
-
if (!otelLoggerCache[category]) {
|
|
52
|
-
otelLoggerCache[category] = loggerProvider.getLogger(category);
|
|
53
|
-
}
|
|
54
|
-
return otelLoggerCache[category];
|
|
55
|
-
};
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
export function createLogger(level: Level = 'info', otelEndpoint?: string): Logger {
|
|
6
|
+
export function createLogger(level: Level = 'info', transmit?: PinoTransmit): Logger {
|
|
59
7
|
const browserConfig: LoggerOptions['browser'] = {
|
|
60
8
|
asObject: true,
|
|
61
9
|
write: (o: any) => {
|
|
@@ -63,60 +11,8 @@ export function createLogger(level: Level = 'info', otelEndpoint?: string): Logg
|
|
|
63
11
|
},
|
|
64
12
|
};
|
|
65
13
|
|
|
66
|
-
if (
|
|
67
|
-
|
|
68
|
-
const otelReady = loadOtelModules(otelEndpoint);
|
|
69
|
-
|
|
70
|
-
browserConfig.transmit = {
|
|
71
|
-
level: level,
|
|
72
|
-
send: (levelLabel: string, logEvent: any) => {
|
|
73
|
-
otelReady.then((getOtelLogger) => {
|
|
74
|
-
try {
|
|
75
|
-
const messages = [...logEvent.messages];
|
|
76
|
-
const severityNumber = mapLevelToSeverityNumber(levelLabel);
|
|
77
|
-
|
|
78
|
-
// Construct the message body
|
|
79
|
-
let body = '';
|
|
80
|
-
const msg = messages.pop();
|
|
81
|
-
|
|
82
|
-
if (typeof msg === 'string') {
|
|
83
|
-
body = msg;
|
|
84
|
-
} else if (msg) {
|
|
85
|
-
body = JSON.stringify(msg);
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
let category = 'spooky-client::unknown';
|
|
89
|
-
|
|
90
|
-
const attributes = {};
|
|
91
|
-
for (const msg of messages) {
|
|
92
|
-
if (typeof msg === 'object') {
|
|
93
|
-
if (msg.Category) {
|
|
94
|
-
category = msg.Category;
|
|
95
|
-
delete msg.Category;
|
|
96
|
-
}
|
|
97
|
-
Object.assign(attributes, msg);
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
// Emit to OTEL SDK
|
|
102
|
-
getOtelLogger(category).emit({
|
|
103
|
-
severityNumber: severityNumber,
|
|
104
|
-
severityText: levelLabel.toUpperCase(),
|
|
105
|
-
body: body,
|
|
106
|
-
attributes: {
|
|
107
|
-
...logEvent.bindings[0],
|
|
108
|
-
...attributes,
|
|
109
|
-
},
|
|
110
|
-
timestamp: new Date(logEvent.ts),
|
|
111
|
-
});
|
|
112
|
-
} catch (e) {
|
|
113
|
-
console.warn('Failed to transmit log to OTEL endpoint', e);
|
|
114
|
-
}
|
|
115
|
-
}).catch((e) => {
|
|
116
|
-
console.warn('Failed to load OpenTelemetry modules', e);
|
|
117
|
-
});
|
|
118
|
-
},
|
|
119
|
-
};
|
|
14
|
+
if (transmit) {
|
|
15
|
+
browserConfig.transmit = transmit;
|
|
120
16
|
}
|
|
121
17
|
|
|
122
18
|
return pino({
|
package/src/spooky.ts
CHANGED
|
@@ -113,7 +113,7 @@ export class SpookyClient<S extends SchemaStructure> {
|
|
|
113
113
|
}
|
|
114
114
|
|
|
115
115
|
constructor(private config: SpookyConfig<S>) {
|
|
116
|
-
const logger = createLogger(config.logLevel ?? 'info', config.
|
|
116
|
+
const logger = createLogger(config.logLevel ?? 'info', config.otelTransmit);
|
|
117
117
|
this.logger = logger.child({ service: 'SpookyClient' });
|
|
118
118
|
|
|
119
119
|
this.logger.info(
|
package/src/types.ts
CHANGED
|
@@ -1,10 +1,15 @@
|
|
|
1
1
|
import { RecordId, SchemaStructure } from '@spooky-sync/query-builder';
|
|
2
|
-
import { Level } from 'pino';
|
|
2
|
+
import { Level, type LoggerOptions } from 'pino';
|
|
3
3
|
import { PushEventOptions } from './events/index';
|
|
4
4
|
import { UpEvent } from './modules/sync/index';
|
|
5
5
|
|
|
6
6
|
export type { Level } from 'pino';
|
|
7
7
|
|
|
8
|
+
/**
|
|
9
|
+
* A pino browser transmit object for forwarding logs to an external sink (e.g. OpenTelemetry).
|
|
10
|
+
*/
|
|
11
|
+
export type PinoTransmit = NonNullable<NonNullable<LoggerOptions['browser']>['transmit']>;
|
|
12
|
+
|
|
8
13
|
/**
|
|
9
14
|
* The type of storage backend to use for the local database.
|
|
10
15
|
* - 'memory': In-memory storage (transient).
|
|
@@ -107,8 +112,8 @@ export interface SpookyConfig<S extends SchemaStructure> {
|
|
|
107
112
|
* Can be a custom implementation, 'surrealdb' (default), or 'localstorage'.
|
|
108
113
|
*/
|
|
109
114
|
persistenceClient?: PersistenceClient | 'surrealdb' | 'localstorage';
|
|
110
|
-
/**
|
|
111
|
-
|
|
115
|
+
/** A pino browser transmit object for forwarding logs (e.g. via @spooky-sync/core/otel). */
|
|
116
|
+
otelTransmit?: PinoTransmit;
|
|
112
117
|
/**
|
|
113
118
|
* Debounce time in milliseconds for stream updates.
|
|
114
119
|
* Defaults to 100ms.
|