@b9g/platform 0.1.11 → 0.1.13
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 +1 -1
- package/package.json +22 -38
- package/src/config.d.ts +15 -163
- package/src/config.js +18 -630
- package/src/globals.d.ts +119 -0
- package/src/index.d.ts +294 -25
- package/src/index.js +466 -126
- package/src/runtime.d.ts +423 -22
- package/src/runtime.js +693 -250
- package/src/shovel-config.d.ts +10 -0
- package/chunk-P57PW2II.js +0 -11
- package/src/cookie-store.d.ts +0 -80
- package/src/cookie-store.js +0 -233
- package/src/single-threaded.d.ts +0 -59
- package/src/single-threaded.js +0 -114
- package/src/worker-pool.d.ts +0 -93
- package/src/worker-pool.js +0 -390
package/src/runtime.d.ts
CHANGED
|
@@ -1,22 +1,209 @@
|
|
|
1
|
+
/// <reference path="./globals.d.ts" />
|
|
2
|
+
/// <reference path="./shovel-config.d.ts" />
|
|
1
3
|
/**
|
|
2
4
|
* ServiceWorker Runtime - Complete ServiceWorker API Implementation
|
|
3
5
|
*
|
|
4
6
|
* This module provides the complete ServiceWorker runtime environment for Shovel:
|
|
7
|
+
* - Cookie Store API (RequestCookieStore for per-request cookie management)
|
|
5
8
|
* - Base Event Classes (ExtendableEvent, FetchEvent, InstallEvent, ActivateEvent)
|
|
6
9
|
* - ServiceWorker API Type Shims (Client, Clients, ServiceWorkerRegistration, etc.)
|
|
7
10
|
* - ServiceWorkerGlobals (installs ServiceWorker globals onto any JavaScript runtime)
|
|
8
11
|
*
|
|
9
12
|
* Based on: https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API#interfaces
|
|
10
13
|
*/
|
|
11
|
-
import {
|
|
12
|
-
import
|
|
14
|
+
import { CustomDirectoryStorage } from "@b9g/filesystem";
|
|
15
|
+
import { CustomCacheStorage, Cache } from "@b9g/cache";
|
|
16
|
+
declare global {
|
|
17
|
+
interface CookieListItem {
|
|
18
|
+
domain?: string;
|
|
19
|
+
path?: string;
|
|
20
|
+
expires?: number;
|
|
21
|
+
secure?: boolean;
|
|
22
|
+
sameSite?: CookieSameSite;
|
|
23
|
+
partitioned?: boolean;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
export type CookieSameSite = globalThis.CookieSameSite;
|
|
27
|
+
export type CookieInit = globalThis.CookieInit;
|
|
28
|
+
export type CookieStoreGetOptions = globalThis.CookieStoreGetOptions;
|
|
29
|
+
export type CookieStoreDeleteOptions = globalThis.CookieStoreDeleteOptions;
|
|
30
|
+
export type CookieListItem = globalThis.CookieListItem;
|
|
31
|
+
export type CookieList = CookieListItem[];
|
|
32
|
+
/**
|
|
33
|
+
* Parse Cookie header value into key-value pairs
|
|
34
|
+
* Cookie: name=value; name2=value2
|
|
35
|
+
*/
|
|
36
|
+
export declare function parseCookieHeader(cookieHeader: string): Map<string, string>;
|
|
37
|
+
/**
|
|
38
|
+
* Serialize cookie into Set-Cookie header value
|
|
39
|
+
*/
|
|
40
|
+
export declare function serializeCookie(cookie: CookieInit): string;
|
|
41
|
+
/**
|
|
42
|
+
* Parse Set-Cookie header into CookieListItem
|
|
43
|
+
*/
|
|
44
|
+
export declare function parseSetCookieHeader(setCookieHeader: string): CookieListItem;
|
|
45
|
+
/**
|
|
46
|
+
* RequestCookieStore - Cookie Store implementation for ServiceWorker contexts
|
|
47
|
+
*
|
|
48
|
+
* This implementation:
|
|
49
|
+
* - Reads cookies from the incoming Request's Cookie header
|
|
50
|
+
* - Tracks changes (set/delete operations)
|
|
51
|
+
* - Exports changes as Set-Cookie headers for the Response
|
|
52
|
+
*
|
|
53
|
+
* It follows the Cookie Store API spec but is designed for server-side
|
|
54
|
+
* request handling rather than browser contexts.
|
|
55
|
+
*/
|
|
56
|
+
export declare class RequestCookieStore extends EventTarget {
|
|
57
|
+
#private;
|
|
58
|
+
onchange: ((this: RequestCookieStore, ev: Event) => any) | null;
|
|
59
|
+
constructor(request?: Request);
|
|
60
|
+
get(nameOrOptions: string | CookieStoreGetOptions): Promise<CookieListItem | null>;
|
|
61
|
+
getAll(nameOrOptions?: string | CookieStoreGetOptions): Promise<CookieList>;
|
|
62
|
+
set(nameOrOptions: string | CookieInit, value?: string): Promise<void>;
|
|
63
|
+
delete(nameOrOptions: string | CookieStoreDeleteOptions): Promise<void>;
|
|
64
|
+
/**
|
|
65
|
+
* Get Set-Cookie headers for all changes
|
|
66
|
+
* This should be called when constructing the Response
|
|
67
|
+
*/
|
|
68
|
+
getSetCookieHeaders(): string[];
|
|
69
|
+
hasChanges(): boolean;
|
|
70
|
+
clearChanges(): void;
|
|
71
|
+
}
|
|
72
|
+
import type { DirectoryStorage } from "@b9g/filesystem";
|
|
73
|
+
import { type Logger } from "@logtape/logtape";
|
|
74
|
+
/**
|
|
75
|
+
* Logger storage interface for accessing loggers by category path.
|
|
76
|
+
* Uses array signature (matching LogTape) to allow future options parameter.
|
|
77
|
+
*/
|
|
78
|
+
export interface LoggerStorage {
|
|
79
|
+
/**
|
|
80
|
+
* Get a logger by category path (sync)
|
|
81
|
+
* @example const logger = self.loggers.get(["app"])
|
|
82
|
+
* @example const logger = self.loggers.get(["app", "db"])
|
|
83
|
+
*/
|
|
84
|
+
get(categories: string[]): Logger;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Factory function type for creating loggers (sync).
|
|
88
|
+
*/
|
|
89
|
+
export type LoggerFactory = (categories: string[]) => Logger;
|
|
90
|
+
/**
|
|
91
|
+
* Custom logger storage implementation that wraps a factory function
|
|
92
|
+
*/
|
|
93
|
+
export declare class CustomLoggerStorage implements LoggerStorage {
|
|
94
|
+
#private;
|
|
95
|
+
constructor(factory: LoggerFactory);
|
|
96
|
+
get(categories: string[]): Logger;
|
|
97
|
+
}
|
|
98
|
+
import { Database } from "@b9g/zen";
|
|
99
|
+
/**
|
|
100
|
+
* Database configuration from shovel.json.
|
|
101
|
+
* Uses the unified impl pattern like directories and caches.
|
|
102
|
+
*/
|
|
103
|
+
export interface DatabaseConfig {
|
|
104
|
+
/** Reified implementation (driver class from build-time code generation) */
|
|
105
|
+
impl?: new (url: string, options?: any) => any;
|
|
106
|
+
/** Database connection URL */
|
|
107
|
+
url?: string;
|
|
108
|
+
/** Additional driver-specific options */
|
|
109
|
+
[key: string]: unknown;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Upgrade event passed to onUpgrade callback during database.open().
|
|
113
|
+
*/
|
|
114
|
+
export interface DatabaseUpgradeEvent {
|
|
115
|
+
/** The database being upgraded */
|
|
116
|
+
db: Database;
|
|
117
|
+
/** Previous database version (0 if new) */
|
|
118
|
+
oldVersion: number;
|
|
119
|
+
/** Target version being opened */
|
|
120
|
+
newVersion: number;
|
|
121
|
+
/** Register a promise that must complete before open() resolves */
|
|
122
|
+
waitUntil(promise: Promise<unknown>): void;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* DatabaseStorage interface - provides access to named database instances.
|
|
126
|
+
*
|
|
127
|
+
* @example
|
|
128
|
+
* ```typescript
|
|
129
|
+
* // In activate handler - open database with migrations
|
|
130
|
+
* self.addEventListener("activate", (event) => {
|
|
131
|
+
* event.waitUntil(
|
|
132
|
+
* self.databases.open("main", 2, (e) => {
|
|
133
|
+
* e.waitUntil(runMigrations(e));
|
|
134
|
+
* })
|
|
135
|
+
* );
|
|
136
|
+
* });
|
|
137
|
+
*
|
|
138
|
+
* // In fetch handler - get the opened database (sync)
|
|
139
|
+
* self.addEventListener("fetch", (event) => {
|
|
140
|
+
* const db = self.databases.get("main");
|
|
141
|
+
* const users = await db.all(User)`WHERE active = ${true}`;
|
|
142
|
+
* });
|
|
143
|
+
* ```
|
|
144
|
+
*/
|
|
145
|
+
export interface DatabaseStorage {
|
|
146
|
+
/**
|
|
147
|
+
* Open a database at a specific version.
|
|
148
|
+
* Imports the driver, creates the Database, runs migrations if needed.
|
|
149
|
+
* Caches the opened instance for later get() calls.
|
|
150
|
+
*/
|
|
151
|
+
open(name: string, version: number, onUpgrade?: (event: DatabaseUpgradeEvent) => void): Promise<Database>;
|
|
152
|
+
/**
|
|
153
|
+
* Get an already-opened database.
|
|
154
|
+
* Throws if the database hasn't been opened yet.
|
|
155
|
+
* This is synchronous for fast access in request handlers.
|
|
156
|
+
*/
|
|
157
|
+
get(name: string): Database;
|
|
158
|
+
/** Close a specific database */
|
|
159
|
+
close(name: string): Promise<void>;
|
|
160
|
+
/** Close all databases */
|
|
161
|
+
closeAll(): Promise<void>;
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Factory function type for creating database drivers.
|
|
165
|
+
* Returns the Database instance and a close function.
|
|
166
|
+
*/
|
|
167
|
+
export type DatabaseFactory = (name: string) => Promise<{
|
|
168
|
+
db: Database;
|
|
169
|
+
close: () => Promise<void>;
|
|
170
|
+
}>;
|
|
171
|
+
/**
|
|
172
|
+
* CustomDatabaseStorage implements DatabaseStorage.
|
|
173
|
+
*/
|
|
174
|
+
export declare class CustomDatabaseStorage implements DatabaseStorage {
|
|
175
|
+
#private;
|
|
176
|
+
constructor(factory: DatabaseFactory);
|
|
177
|
+
open(name: string, version: number, onUpgrade?: (event: DatabaseUpgradeEvent) => void): Promise<Database>;
|
|
178
|
+
get(name: string): Database;
|
|
179
|
+
close(name: string): Promise<void>;
|
|
180
|
+
closeAll(): Promise<void>;
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Create a DatabaseFactory from declarative config.
|
|
184
|
+
*
|
|
185
|
+
* This dynamically imports the driver module at runtime, following
|
|
186
|
+
* the same pattern as createDirectoryFactory and createCacheFactory.
|
|
187
|
+
*
|
|
188
|
+
* @param configs - The databases config from shovel.json
|
|
189
|
+
* @returns An async factory function that creates databases by name
|
|
190
|
+
*
|
|
191
|
+
* @example
|
|
192
|
+
* ```typescript
|
|
193
|
+
* // In platform adapter:
|
|
194
|
+
* const factory = createDatabaseFactory(config.databases);
|
|
195
|
+
* return new CustomDatabaseStorage(factory);
|
|
196
|
+
* ```
|
|
197
|
+
*/
|
|
198
|
+
export declare function createDatabaseFactory(configs: Record<string, DatabaseConfig>): DatabaseFactory;
|
|
13
199
|
/** Symbol for ending dispatch phase (internal use only) */
|
|
14
200
|
declare const kEndDispatchPhase: unique symbol;
|
|
15
201
|
/** Symbol for checking if extensions are allowed (internal use only) */
|
|
16
202
|
declare const kCanExtend: unique symbol;
|
|
17
203
|
/**
|
|
18
|
-
* ExtendableEvent
|
|
19
|
-
*
|
|
204
|
+
* Shovel's ExtendableEvent implementation following ServiceWorker spec.
|
|
205
|
+
*
|
|
206
|
+
* Standard constructor: new ShovelExtendableEvent(type) or new ShovelExtendableEvent(type, options)
|
|
20
207
|
*
|
|
21
208
|
* Per spec, waitUntil() can be called:
|
|
22
209
|
* 1. Synchronously during event dispatch, OR
|
|
@@ -24,7 +211,7 @@ declare const kCanExtend: unique symbol;
|
|
|
24
211
|
*
|
|
25
212
|
* See: https://github.com/w3c/ServiceWorker/issues/771
|
|
26
213
|
*/
|
|
27
|
-
export declare class
|
|
214
|
+
export declare class ShovelExtendableEvent extends Event implements ExtendableEvent {
|
|
28
215
|
#private;
|
|
29
216
|
constructor(type: string, eventInitDict?: EventInit);
|
|
30
217
|
waitUntil(promise: Promise<any>): void;
|
|
@@ -35,27 +222,48 @@ export declare class ExtendableEvent extends Event {
|
|
|
35
222
|
[kCanExtend](): boolean;
|
|
36
223
|
}
|
|
37
224
|
/**
|
|
38
|
-
*
|
|
225
|
+
* Options for ShovelFetchEvent constructor (non-standard Shovel extension)
|
|
39
226
|
*/
|
|
40
|
-
export
|
|
227
|
+
export interface ShovelFetchEventInit extends EventInit {
|
|
228
|
+
/**
|
|
229
|
+
* Platform-provided callback for extending request lifetime.
|
|
230
|
+
* Called automatically when waitUntil() is invoked.
|
|
231
|
+
* (e.g., Cloudflare's ctx.waitUntil)
|
|
232
|
+
*/
|
|
233
|
+
platformWaitUntil?: (promise: Promise<unknown>) => void;
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Shovel's FetchEvent implementation.
|
|
237
|
+
*
|
|
238
|
+
* Platforms can subclass this to add platform-specific properties (e.g., env bindings).
|
|
239
|
+
* The platformWaitUntil hook allows platforms to extend request lifetime properly.
|
|
240
|
+
*/
|
|
241
|
+
export declare class ShovelFetchEvent extends ShovelExtendableEvent implements FetchEvent {
|
|
41
242
|
#private;
|
|
42
243
|
readonly request: Request;
|
|
43
244
|
readonly cookieStore: RequestCookieStore;
|
|
44
|
-
|
|
245
|
+
readonly clientId: string;
|
|
246
|
+
readonly handled: Promise<undefined>;
|
|
247
|
+
readonly preloadResponse: Promise<any>;
|
|
248
|
+
readonly resultingClientId: string;
|
|
249
|
+
constructor(request: Request, options?: ShovelFetchEventInit);
|
|
250
|
+
waitUntil(promise: Promise<any>): void;
|
|
45
251
|
respondWith(response: Response | Promise<Response>): void;
|
|
46
252
|
getResponse(): Promise<Response> | null;
|
|
47
253
|
hasResponded(): boolean;
|
|
254
|
+
/** The URL of the request (convenience property) */
|
|
255
|
+
get url(): string;
|
|
48
256
|
}
|
|
49
257
|
/**
|
|
50
|
-
*
|
|
258
|
+
* Shovel's InstallEvent implementation
|
|
51
259
|
*/
|
|
52
|
-
export declare class
|
|
260
|
+
export declare class ShovelInstallEvent extends ShovelExtendableEvent {
|
|
53
261
|
constructor(eventInitDict?: EventInit);
|
|
54
262
|
}
|
|
55
263
|
/**
|
|
56
|
-
*
|
|
264
|
+
* Shovel's ActivateEvent implementation
|
|
57
265
|
*/
|
|
58
|
-
export declare class
|
|
266
|
+
export declare class ShovelActivateEvent extends ShovelExtendableEvent {
|
|
59
267
|
constructor(eventInitDict?: EventInit);
|
|
60
268
|
}
|
|
61
269
|
/**
|
|
@@ -111,7 +319,7 @@ export interface ExtendableMessageEventInit extends EventInit {
|
|
|
111
319
|
source?: Client | ServiceWorker | MessagePort | null;
|
|
112
320
|
ports?: MessagePort[];
|
|
113
321
|
}
|
|
114
|
-
export declare class ExtendableMessageEvent extends
|
|
322
|
+
export declare class ExtendableMessageEvent extends ShovelExtendableEvent {
|
|
115
323
|
readonly data: any;
|
|
116
324
|
readonly origin: string;
|
|
117
325
|
readonly lastEventId: string;
|
|
@@ -178,8 +386,13 @@ export declare class ShovelServiceWorkerRegistration extends EventTarget impleme
|
|
|
178
386
|
activate(): Promise<void>;
|
|
179
387
|
/**
|
|
180
388
|
* Handle a fetch request (Shovel extension)
|
|
389
|
+
*
|
|
390
|
+
* Platforms create a ShovelFetchEvent (or subclass) with platform-specific
|
|
391
|
+
* properties and hooks, then pass it to this method for dispatching.
|
|
392
|
+
*
|
|
393
|
+
* @param event - The fetch event to handle (created by platform adapter)
|
|
181
394
|
*/
|
|
182
|
-
handleRequest(
|
|
395
|
+
handleRequest(event: ShovelFetchEvent): Promise<Response>;
|
|
183
396
|
/**
|
|
184
397
|
* Check if ready to handle requests (Shovel extension)
|
|
185
398
|
*/
|
|
@@ -268,7 +481,7 @@ export interface NotificationEventInit extends EventInit {
|
|
|
268
481
|
notification: Notification;
|
|
269
482
|
reply?: string | null;
|
|
270
483
|
}
|
|
271
|
-
export declare class NotificationEvent extends
|
|
484
|
+
export declare class NotificationEvent extends ShovelExtendableEvent {
|
|
272
485
|
readonly action: string;
|
|
273
486
|
readonly notification: Notification;
|
|
274
487
|
readonly reply: string | null;
|
|
@@ -280,7 +493,7 @@ export declare class NotificationEvent extends ExtendableEvent {
|
|
|
280
493
|
export interface PushEventInit extends EventInit {
|
|
281
494
|
data?: PushMessageData | null;
|
|
282
495
|
}
|
|
283
|
-
export declare class PushEvent extends
|
|
496
|
+
export declare class PushEvent extends ShovelExtendableEvent {
|
|
284
497
|
readonly data: PushMessageData | null;
|
|
285
498
|
constructor(type: string, eventInitDict?: PushEventInit);
|
|
286
499
|
}
|
|
@@ -304,7 +517,7 @@ export interface SyncEventInit extends EventInit {
|
|
|
304
517
|
tag: string;
|
|
305
518
|
lastChance?: boolean;
|
|
306
519
|
}
|
|
307
|
-
export declare class SyncEvent extends
|
|
520
|
+
export declare class SyncEvent extends ShovelExtendableEvent {
|
|
308
521
|
readonly tag: string;
|
|
309
522
|
readonly lastChance: boolean;
|
|
310
523
|
constructor(type: string, eventInitDict: SyncEventInit);
|
|
@@ -333,8 +546,12 @@ interface NotificationOptions {
|
|
|
333
546
|
export interface ServiceWorkerGlobalsOptions {
|
|
334
547
|
/** ServiceWorker registration instance */
|
|
335
548
|
registration: ServiceWorkerRegistration;
|
|
336
|
-
/**
|
|
337
|
-
|
|
549
|
+
/** Directory storage (file system access) - REQUIRED */
|
|
550
|
+
directories: DirectoryStorage;
|
|
551
|
+
/** Database storage - OPTIONAL */
|
|
552
|
+
databases?: DatabaseStorage;
|
|
553
|
+
/** Logger storage (logging access) - REQUIRED */
|
|
554
|
+
loggers: LoggerStorage;
|
|
338
555
|
/** Cache storage (required by ServiceWorkerGlobalScope) - REQUIRED */
|
|
339
556
|
caches: CacheStorage;
|
|
340
557
|
/** Development mode flag */
|
|
@@ -355,7 +572,7 @@ export declare class DedicatedWorkerGlobalScope extends WorkerGlobalScope {
|
|
|
355
572
|
/**
|
|
356
573
|
* ServiceWorkerGlobals - Installs ServiceWorker globals onto globalThis
|
|
357
574
|
*
|
|
358
|
-
* This class holds ServiceWorker API implementations (caches,
|
|
575
|
+
* This class holds ServiceWorker API implementations (caches, directories, clients, etc.)
|
|
359
576
|
* and patches them onto globalThis via install(). It maintains the browser invariant
|
|
360
577
|
* that self === globalThis while providing ServiceWorker APIs.
|
|
361
578
|
*
|
|
@@ -366,7 +583,9 @@ export declare class ServiceWorkerGlobals implements ServiceWorkerGlobalScope {
|
|
|
366
583
|
readonly self: any;
|
|
367
584
|
readonly registration: ServiceWorkerRegistration;
|
|
368
585
|
readonly caches: CacheStorage;
|
|
369
|
-
readonly
|
|
586
|
+
readonly directories: DirectoryStorage;
|
|
587
|
+
readonly databases?: DatabaseStorage;
|
|
588
|
+
readonly loggers: LoggerStorage;
|
|
370
589
|
readonly clients: Clients;
|
|
371
590
|
get cookieStore(): any;
|
|
372
591
|
readonly serviceWorker: any;
|
|
@@ -415,7 +634,8 @@ export declare class ServiceWorkerGlobals implements ServiceWorkerGlobalScope {
|
|
|
415
634
|
*/
|
|
416
635
|
skipWaiting(): Promise<void>;
|
|
417
636
|
/**
|
|
418
|
-
* Event target delegation to registration
|
|
637
|
+
* Event target delegation - ServiceWorker events go to registration,
|
|
638
|
+
* other events (like "message" for worker threads) go to native handler
|
|
419
639
|
*/
|
|
420
640
|
addEventListener(type: string, listener: EventListenerOrEventListenerObject | null, options?: boolean | AddEventListenerOptions): void;
|
|
421
641
|
removeEventListener(type: string, listener: EventListenerOrEventListenerObject | null, options?: boolean | EventListenerOptions): void;
|
|
@@ -431,4 +651,185 @@ export declare class ServiceWorkerGlobals implements ServiceWorkerGlobalScope {
|
|
|
431
651
|
*/
|
|
432
652
|
restore(): void;
|
|
433
653
|
}
|
|
654
|
+
/** Cache provider configuration */
|
|
655
|
+
export interface CacheConfig {
|
|
656
|
+
/** Reified implementation (class or factory from build-time code generation) */
|
|
657
|
+
impl?: (new (name: string, options?: any) => Cache) | ((name: string, options?: any) => Cache);
|
|
658
|
+
/** Additional options passed to the constructor */
|
|
659
|
+
[key: string]: unknown;
|
|
660
|
+
}
|
|
661
|
+
/** Directory (filesystem) provider configuration */
|
|
662
|
+
export interface DirectoryConfig {
|
|
663
|
+
/** Reified implementation (class or factory from build-time code generation) */
|
|
664
|
+
impl?: (new (name: string, options?: any) => FileSystemDirectoryHandle) | ((name: string, options?: any) => FileSystemDirectoryHandle);
|
|
665
|
+
/** Custom path for filesystem directories */
|
|
666
|
+
path?: string;
|
|
667
|
+
/** Additional options passed to the constructor */
|
|
668
|
+
[key: string]: unknown;
|
|
669
|
+
}
|
|
670
|
+
/** Shovel application configuration (from shovel.json) */
|
|
671
|
+
export interface ShovelConfig {
|
|
672
|
+
port?: number;
|
|
673
|
+
host?: string;
|
|
674
|
+
workers?: number;
|
|
675
|
+
platform?: string;
|
|
676
|
+
logging?: LoggingConfig;
|
|
677
|
+
caches?: Record<string, CacheConfig>;
|
|
678
|
+
directories?: Record<string, DirectoryConfig>;
|
|
679
|
+
databases?: Record<string, DatabaseConfig>;
|
|
680
|
+
}
|
|
681
|
+
/**
|
|
682
|
+
* Creates a directory factory function for CustomDirectoryStorage.
|
|
683
|
+
*
|
|
684
|
+
* Configs must have impl pre-imported (from generated config module).
|
|
685
|
+
* Paths are expected to be already resolved at build time by the path syntax parser.
|
|
686
|
+
* Runtime paths (like [tmpdir]) are evaluated as expressions in the generated config.
|
|
687
|
+
*/
|
|
688
|
+
export declare function createDirectoryFactory(configs: Record<string, DirectoryConfig>): (name: string) => Promise<FileSystemDirectoryHandle>;
|
|
689
|
+
export interface CacheFactoryOptions {
|
|
690
|
+
/** Cache configurations with pre-imported CacheClass (from generated config module) */
|
|
691
|
+
configs: Record<string, CacheConfig>;
|
|
692
|
+
/** If true, use PostMessageCache (for workers communicating with main thread) */
|
|
693
|
+
usePostMessage?: boolean;
|
|
694
|
+
}
|
|
695
|
+
/**
|
|
696
|
+
* Creates a cache factory function for CustomCacheStorage.
|
|
697
|
+
* Configs must have impl pre-imported (from generated config module).
|
|
698
|
+
*/
|
|
699
|
+
export declare function createCacheFactory(options: CacheFactoryOptions): (name: string) => Promise<Cache>;
|
|
700
|
+
/**
|
|
701
|
+
* Worker message types for the message loop
|
|
702
|
+
*/
|
|
703
|
+
export interface WorkerRequestMessage {
|
|
704
|
+
type: "request";
|
|
705
|
+
request: {
|
|
706
|
+
url: string;
|
|
707
|
+
method: string;
|
|
708
|
+
headers: Record<string, string>;
|
|
709
|
+
body?: ArrayBuffer | null;
|
|
710
|
+
};
|
|
711
|
+
requestID: number;
|
|
712
|
+
}
|
|
713
|
+
export interface WorkerResponseMessage {
|
|
714
|
+
type: "response";
|
|
715
|
+
response: {
|
|
716
|
+
status: number;
|
|
717
|
+
statusText: string;
|
|
718
|
+
headers: Record<string, string>;
|
|
719
|
+
body: ArrayBuffer;
|
|
720
|
+
};
|
|
721
|
+
requestID: number;
|
|
722
|
+
}
|
|
723
|
+
export interface WorkerErrorMessage {
|
|
724
|
+
type: "error";
|
|
725
|
+
error: string;
|
|
726
|
+
stack?: string;
|
|
727
|
+
requestID?: number;
|
|
728
|
+
}
|
|
729
|
+
/**
|
|
730
|
+
* Options for initializing the worker runtime
|
|
731
|
+
*/
|
|
732
|
+
export interface InitWorkerRuntimeOptions {
|
|
733
|
+
/** Shovel configuration (from shovel:config) */
|
|
734
|
+
config: ShovelConfig;
|
|
735
|
+
}
|
|
736
|
+
/**
|
|
737
|
+
* Result from initializing the worker runtime
|
|
738
|
+
*/
|
|
739
|
+
export interface InitWorkerRuntimeResult {
|
|
740
|
+
/** The ServiceWorker registration instance */
|
|
741
|
+
registration: ShovelServiceWorkerRegistration;
|
|
742
|
+
/** The installed ServiceWorkerGlobals scope */
|
|
743
|
+
scope: ServiceWorkerGlobals;
|
|
744
|
+
/** Cache storage instance */
|
|
745
|
+
caches: CustomCacheStorage;
|
|
746
|
+
/** Directory storage instance */
|
|
747
|
+
directories: CustomDirectoryStorage;
|
|
748
|
+
/** Database storage instance (if configured) */
|
|
749
|
+
databases?: CustomDatabaseStorage;
|
|
750
|
+
/** Logger storage instance */
|
|
751
|
+
loggers: CustomLoggerStorage;
|
|
752
|
+
}
|
|
753
|
+
/**
|
|
754
|
+
* Initialize the worker runtime environment.
|
|
755
|
+
* Sets up ServiceWorkerGlobals, caches, directories, and logging.
|
|
756
|
+
*
|
|
757
|
+
* This should be called at the top of a worker entry point before importing user code.
|
|
758
|
+
*
|
|
759
|
+
* @example
|
|
760
|
+
* ```typescript
|
|
761
|
+
* import {config} from "shovel:config";
|
|
762
|
+
* import {initWorkerRuntime, startWorkerMessageLoop} from "@b9g/platform/runtime";
|
|
763
|
+
*
|
|
764
|
+
* const {registration} = await initWorkerRuntime({config});
|
|
765
|
+
*
|
|
766
|
+
* // Import user code (registers event handlers)
|
|
767
|
+
* import "./server.js";
|
|
768
|
+
*
|
|
769
|
+
* // Run lifecycle and start message loop
|
|
770
|
+
* await registration.install();
|
|
771
|
+
* await registration.activate();
|
|
772
|
+
* startWorkerMessageLoop(registration);
|
|
773
|
+
* ```
|
|
774
|
+
*/
|
|
775
|
+
export declare function initWorkerRuntime(options: InitWorkerRuntimeOptions): Promise<InitWorkerRuntimeResult>;
|
|
776
|
+
/**
|
|
777
|
+
* Options for the worker message loop
|
|
778
|
+
*/
|
|
779
|
+
export interface WorkerMessageLoopOptions {
|
|
780
|
+
registration: ShovelServiceWorkerRegistration;
|
|
781
|
+
databases?: CustomDatabaseStorage;
|
|
782
|
+
caches?: CacheStorage;
|
|
783
|
+
}
|
|
784
|
+
/**
|
|
785
|
+
* Start the worker message loop for handling requests.
|
|
786
|
+
* This function sets up message handling for request/response communication
|
|
787
|
+
* with the main thread via postMessage.
|
|
788
|
+
*
|
|
789
|
+
* @param options - The registration and resources to manage
|
|
790
|
+
*/
|
|
791
|
+
export declare function startWorkerMessageLoop(options: ShovelServiceWorkerRegistration | WorkerMessageLoopOptions): void;
|
|
792
|
+
/** Log level for filtering */
|
|
793
|
+
export type LogLevel = "debug" | "info" | "warning" | "error";
|
|
794
|
+
/** Sink configuration */
|
|
795
|
+
export interface SinkConfig {
|
|
796
|
+
/** Reified implementation (factory function from build-time code generation) */
|
|
797
|
+
impl?: (...args: any[]) => unknown;
|
|
798
|
+
/** Additional options passed to the factory (path, maxSize, etc.) */
|
|
799
|
+
[key: string]: unknown;
|
|
800
|
+
}
|
|
801
|
+
/** Logger configuration - matches LogTape's logger config structure */
|
|
802
|
+
export interface LoggerConfig {
|
|
803
|
+
/** Category as string or array for hierarchy. e.g. "myapp" or ["myapp", "db"] */
|
|
804
|
+
category: string | string[];
|
|
805
|
+
/** Log level for this category. Inherits from parent if not specified. */
|
|
806
|
+
level?: LogLevel;
|
|
807
|
+
/** Sink names to add. Inherits from parent by default. */
|
|
808
|
+
sinks?: string[];
|
|
809
|
+
/** Set to "override" to replace parent sinks instead of inherit */
|
|
810
|
+
parentSinks?: "override";
|
|
811
|
+
}
|
|
812
|
+
export interface LoggingConfig {
|
|
813
|
+
/** Named sinks. "console" is always available implicitly. */
|
|
814
|
+
sinks?: Record<string, SinkConfig>;
|
|
815
|
+
/** Logger configurations. Shovel provides defaults for ["shovel", ...] categories. */
|
|
816
|
+
loggers?: LoggerConfig[];
|
|
817
|
+
}
|
|
818
|
+
/** Processed logging config with all defaults applied */
|
|
819
|
+
export interface ProcessedLoggingConfig {
|
|
820
|
+
sinks: Record<string, SinkConfig>;
|
|
821
|
+
loggers: LoggerConfig[];
|
|
822
|
+
}
|
|
823
|
+
/**
|
|
824
|
+
* Configure LogTape logging based on Shovel config.
|
|
825
|
+
* Call this in both main thread and workers.
|
|
826
|
+
*
|
|
827
|
+
* Uses LogTape-aligned config structure:
|
|
828
|
+
* - Named sinks (console is implicit)
|
|
829
|
+
* - Loggers array with category hierarchy support
|
|
830
|
+
* - Shovel provides default loggers for ["shovel", ...] categories
|
|
831
|
+
*
|
|
832
|
+
* @param loggingConfig - The logging configuration
|
|
833
|
+
*/
|
|
834
|
+
export declare function configureLogging(loggingConfig: LoggingConfig): Promise<void>;
|
|
434
835
|
export {};
|