@b9g/platform 0.1.4 → 0.1.6

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.
@@ -0,0 +1,426 @@
1
+ /**
2
+ * ServiceWorker Runtime - Complete ServiceWorker API Implementation
3
+ *
4
+ * This module provides the complete ServiceWorker runtime environment for Shovel:
5
+ * - Base Event Classes (ExtendableEvent, FetchEvent, InstallEvent, ActivateEvent)
6
+ * - ServiceWorker API Type Shims (Client, Clients, ServiceWorkerRegistration, etc.)
7
+ * - ShovelGlobalScope (implements ServiceWorkerGlobalScope for any JavaScript runtime)
8
+ *
9
+ * Based on: https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API#interfaces
10
+ */
11
+ import { RequestCookieStore } from "./cookie-store.js";
12
+ import type { BucketStorage } from "@b9g/filesystem";
13
+ /** Symbol for ending dispatch phase (internal use only) */
14
+ declare const kEndDispatchPhase: unique symbol;
15
+ /** Symbol for checking if extensions are allowed (internal use only) */
16
+ declare const kCanExtend: unique symbol;
17
+ /**
18
+ * ExtendableEvent base class following ServiceWorker spec
19
+ * Standard constructor: new ExtendableEvent(type) or new ExtendableEvent(type, options)
20
+ *
21
+ * Per spec, waitUntil() can be called:
22
+ * 1. Synchronously during event dispatch, OR
23
+ * 2. Asynchronously if there are pending promises from prior waitUntil/respondWith calls
24
+ *
25
+ * See: https://github.com/w3c/ServiceWorker/issues/771
26
+ */
27
+ export declare class ExtendableEvent extends Event {
28
+ #private;
29
+ constructor(type: string, eventInitDict?: EventInit);
30
+ waitUntil(promise: Promise<any>): void;
31
+ getPromises(): Promise<any>[];
32
+ /** @internal Called after synchronous dispatch completes */
33
+ [kEndDispatchPhase](): void;
34
+ /** @internal Check if extensions are still allowed */
35
+ [kCanExtend](): boolean;
36
+ }
37
+ /**
38
+ * ServiceWorker-style fetch event
39
+ */
40
+ export declare class FetchEvent extends ExtendableEvent {
41
+ #private;
42
+ readonly request: Request;
43
+ readonly cookieStore: RequestCookieStore;
44
+ constructor(request: Request, eventInitDict?: EventInit);
45
+ respondWith(response: Response | Promise<Response>): void;
46
+ getResponse(): Promise<Response> | null;
47
+ hasResponded(): boolean;
48
+ }
49
+ /**
50
+ * ServiceWorker-style install event
51
+ */
52
+ export declare class InstallEvent extends ExtendableEvent {
53
+ constructor(eventInitDict?: EventInit);
54
+ }
55
+ /**
56
+ * ServiceWorker-style activate event
57
+ */
58
+ export declare class ActivateEvent extends ExtendableEvent {
59
+ constructor(eventInitDict?: EventInit);
60
+ }
61
+ /**
62
+ * ShovelClient - Internal implementation of Client for Shovel runtime
63
+ * Note: Standard Client has no constructor - instances are created internally
64
+ */
65
+ export declare class ShovelClient implements Client {
66
+ readonly frameType: "auxiliary" | "top-level" | "nested" | "none";
67
+ readonly id: string;
68
+ readonly type: "window" | "worker" | "sharedworker";
69
+ readonly url: string;
70
+ constructor(options: {
71
+ id: string;
72
+ url: string;
73
+ type?: "window" | "worker" | "sharedworker";
74
+ });
75
+ postMessage(message: any, transfer: Transferable[]): void;
76
+ postMessage(message: any, options?: StructuredSerializeOptions): void;
77
+ }
78
+ /**
79
+ * ShovelWindowClient - Internal implementation of WindowClient for Shovel runtime
80
+ * Note: Standard WindowClient has no constructor - instances are created internally
81
+ */
82
+ export declare class ShovelWindowClient extends ShovelClient implements WindowClient {
83
+ readonly focused: boolean;
84
+ readonly visibilityState: DocumentVisibilityState;
85
+ constructor(options: {
86
+ id: string;
87
+ url: string;
88
+ focused?: boolean;
89
+ visibilityState?: DocumentVisibilityState;
90
+ });
91
+ focus(): Promise<WindowClient>;
92
+ navigate(_url: string): Promise<WindowClient | null>;
93
+ }
94
+ /**
95
+ * ShovelClients - Internal implementation of Clients for Shovel runtime
96
+ * Note: Standard Clients has no constructor - instances are created internally
97
+ */
98
+ export declare class ShovelClients implements Clients {
99
+ claim(): Promise<void>;
100
+ get(_id: string): Promise<Client | undefined>;
101
+ matchAll<T extends ClientQueryOptions>(_options?: T): Promise<readonly (T["type"] extends "window" ? WindowClient : Client)[]>;
102
+ openWindow(_url: string): Promise<WindowClient | null>;
103
+ }
104
+ /**
105
+ * ExtendableMessageEvent represents message events with waitUntil support
106
+ */
107
+ export interface ExtendableMessageEventInit extends EventInit {
108
+ data?: any;
109
+ origin?: string;
110
+ lastEventId?: string;
111
+ source?: Client | ServiceWorker | MessagePort | null;
112
+ ports?: MessagePort[];
113
+ }
114
+ export declare class ExtendableMessageEvent extends ExtendableEvent {
115
+ readonly data: any;
116
+ readonly origin: string;
117
+ readonly lastEventId: string;
118
+ readonly source: Client | ServiceWorker | MessagePort | null;
119
+ readonly ports: readonly MessagePort[];
120
+ constructor(type: string, eventInitDict?: ExtendableMessageEventInit);
121
+ }
122
+ /**
123
+ * ShovelServiceWorker - Internal implementation of ServiceWorker for Shovel runtime
124
+ * Note: Standard ServiceWorker has no constructor - instances are created internally
125
+ */
126
+ export declare class ShovelServiceWorker extends EventTarget implements ServiceWorker {
127
+ scriptURL: string;
128
+ state: "parsed" | "installing" | "installed" | "activating" | "activated" | "redundant";
129
+ onstatechange: ((ev: Event) => any) | null;
130
+ onerror: ((ev: Event) => any) | null;
131
+ constructor(scriptURL: string, state?: "parsed" | "installing" | "installed" | "activating" | "activated" | "redundant");
132
+ postMessage(message: any, transfer: Transferable[]): void;
133
+ postMessage(message: any, options?: StructuredSerializeOptions): void;
134
+ _setState(newState: typeof this.state): void;
135
+ }
136
+ /**
137
+ * ShovelNavigationPreloadManager - Internal implementation of NavigationPreloadManager
138
+ * Note: Standard NavigationPreloadManager has no constructor - instances are created internally
139
+ */
140
+ export declare class ShovelNavigationPreloadManager implements NavigationPreloadManager {
141
+ disable(): Promise<void>;
142
+ enable(): Promise<void>;
143
+ getState(): Promise<{
144
+ enabled: boolean;
145
+ headerValue: string;
146
+ }>;
147
+ setHeaderValue(_value: string): Promise<void>;
148
+ }
149
+ /**
150
+ * ShovelServiceWorkerRegistration - Internal implementation of ServiceWorkerRegistration
151
+ * This is also the Shovel ServiceWorker runtime - they are unified into one class
152
+ * Note: Standard ServiceWorkerRegistration has no constructor - instances are created internally
153
+ */
154
+ export declare class ShovelServiceWorkerRegistration extends EventTarget implements ServiceWorkerRegistration {
155
+ readonly scope: string;
156
+ readonly updateViaCache: "imports" | "all" | "none";
157
+ readonly navigationPreload: NavigationPreloadManager;
158
+ _serviceWorker: ShovelServiceWorker;
159
+ readonly cookies: any;
160
+ readonly pushManager: any;
161
+ onupdatefound: ((ev: Event) => any) | null;
162
+ constructor(scope?: string, scriptURL?: string);
163
+ get active(): ServiceWorker | null;
164
+ get installing(): ServiceWorker | null;
165
+ get waiting(): ServiceWorker | null;
166
+ getNotifications(_options?: NotificationOptions): Promise<Notification[]>;
167
+ showNotification(_title: string, _options?: NotificationOptions): Promise<void>;
168
+ sync(): Promise<void>;
169
+ unregister(): Promise<boolean>;
170
+ update(): Promise<ServiceWorkerRegistration>;
171
+ /**
172
+ * Install the ServiceWorker (Shovel extension)
173
+ */
174
+ install(): Promise<void>;
175
+ /**
176
+ * Activate the ServiceWorker (Shovel extension)
177
+ */
178
+ activate(): Promise<void>;
179
+ /**
180
+ * Handle a fetch request (Shovel extension)
181
+ */
182
+ handleRequest(request: Request): Promise<Response>;
183
+ /**
184
+ * Check if ready to handle requests (Shovel extension)
185
+ */
186
+ get ready(): boolean;
187
+ }
188
+ /**
189
+ * ShovelServiceWorkerContainer - Internal implementation of ServiceWorkerContainer
190
+ * This is the registry that manages multiple ServiceWorkerRegistrations by scope
191
+ * Note: Standard ServiceWorkerContainer has no constructor - instances are created internally
192
+ */
193
+ export declare class ShovelServiceWorkerContainer extends EventTarget implements ServiceWorkerContainer {
194
+ #private;
195
+ readonly controller: ServiceWorker | null;
196
+ readonly ready: Promise<ServiceWorkerRegistration>;
197
+ oncontrollerchange: ((ev: Event) => any) | null;
198
+ onmessage: ((ev: MessageEvent) => any) | null;
199
+ onmessageerror: ((ev: MessageEvent) => any) | null;
200
+ constructor();
201
+ /**
202
+ * Get registration for a specific scope
203
+ */
204
+ getRegistration(scope?: string): Promise<ServiceWorkerRegistration | undefined>;
205
+ /**
206
+ * Get all registrations
207
+ */
208
+ getRegistrations(): Promise<ServiceWorkerRegistration[]>;
209
+ /**
210
+ * Register a new ServiceWorker for a specific scope
211
+ */
212
+ register(scriptURL: string | URL, options?: {
213
+ scope?: string;
214
+ type?: "classic" | "module";
215
+ updateViaCache?: "imports" | "all" | "none";
216
+ }): Promise<ServiceWorkerRegistration>;
217
+ /**
218
+ * Unregister a ServiceWorker registration
219
+ */
220
+ unregister(scope: string): Promise<boolean>;
221
+ /**
222
+ * Route a request to the appropriate registration based on scope matching
223
+ */
224
+ handleRequest(request: Request): Promise<Response | null>;
225
+ /**
226
+ * Install and activate all registrations
227
+ */
228
+ installAll(): Promise<void>;
229
+ /**
230
+ * Get list of all scopes
231
+ */
232
+ getScopes(): string[];
233
+ startMessages(): void;
234
+ }
235
+ /**
236
+ * Notification interface for push notifications (server context stubs)
237
+ */
238
+ export declare class Notification extends EventTarget {
239
+ readonly actions: readonly NotificationAction[];
240
+ readonly badge: string;
241
+ readonly body: string;
242
+ readonly data: any;
243
+ readonly dir: "auto" | "ltr" | "rtl";
244
+ readonly icon: string;
245
+ readonly image: string;
246
+ readonly lang: string;
247
+ readonly renotify: boolean;
248
+ readonly requireInteraction: boolean;
249
+ readonly silent: boolean;
250
+ readonly tag: string;
251
+ readonly timestamp: number;
252
+ readonly title: string;
253
+ readonly vibrate: readonly number[];
254
+ onclick: ((ev: Event) => any) | null;
255
+ onclose: ((ev: Event) => any) | null;
256
+ onerror: ((ev: Event) => any) | null;
257
+ onshow: ((ev: Event) => any) | null;
258
+ static permission: "default" | "denied" | "granted";
259
+ constructor(title: string, options?: NotificationOptions);
260
+ close(): void;
261
+ static requestPermission(): Promise<"default" | "denied" | "granted">;
262
+ }
263
+ /**
264
+ * NotificationEvent for notification interactions
265
+ */
266
+ export interface NotificationEventInit extends EventInit {
267
+ action?: string;
268
+ notification: Notification;
269
+ reply?: string | null;
270
+ }
271
+ export declare class NotificationEvent extends ExtendableEvent {
272
+ readonly action: string;
273
+ readonly notification: Notification;
274
+ readonly reply: string | null;
275
+ constructor(type: string, eventInitDict: NotificationEventInit);
276
+ }
277
+ /**
278
+ * PushEvent for push message handling
279
+ */
280
+ export interface PushEventInit extends EventInit {
281
+ data?: PushMessageData | null;
282
+ }
283
+ export declare class PushEvent extends ExtendableEvent {
284
+ readonly data: PushMessageData | null;
285
+ constructor(type: string, eventInitDict?: PushEventInit);
286
+ }
287
+ /**
288
+ * ShovelPushMessageData - Internal implementation of PushMessageData
289
+ * Note: Standard PushMessageData has no constructor - instances are created internally
290
+ */
291
+ export declare class ShovelPushMessageData implements PushMessageData {
292
+ private _data;
293
+ constructor(_data: any);
294
+ arrayBuffer(): ArrayBuffer;
295
+ blob(): Blob;
296
+ bytes(): Uint8Array<ArrayBuffer>;
297
+ json(): any;
298
+ text(): string;
299
+ }
300
+ /**
301
+ * SyncEvent for background sync
302
+ */
303
+ export interface SyncEventInit extends EventInit {
304
+ tag: string;
305
+ lastChance?: boolean;
306
+ }
307
+ export declare class SyncEvent extends ExtendableEvent {
308
+ readonly tag: string;
309
+ readonly lastChance: boolean;
310
+ constructor(type: string, eventInitDict: SyncEventInit);
311
+ }
312
+ interface NotificationAction {
313
+ action: string;
314
+ title: string;
315
+ icon?: string;
316
+ }
317
+ interface NotificationOptions {
318
+ actions?: NotificationAction[];
319
+ badge?: string;
320
+ body?: string;
321
+ data?: any;
322
+ dir?: "auto" | "ltr" | "rtl";
323
+ icon?: string;
324
+ image?: string;
325
+ lang?: string;
326
+ renotify?: boolean;
327
+ requireInteraction?: boolean;
328
+ silent?: boolean;
329
+ tag?: string;
330
+ timestamp?: number;
331
+ vibrate?: number[];
332
+ }
333
+ export interface ShovelGlobalScopeOptions {
334
+ /** ServiceWorker registration instance */
335
+ registration: ServiceWorkerRegistration;
336
+ /** Bucket storage (file system access) - REQUIRED */
337
+ buckets: BucketStorage;
338
+ /** Cache storage (required by ServiceWorkerGlobalScope) - REQUIRED */
339
+ caches: CacheStorage;
340
+ /** Development mode flag */
341
+ isDevelopment?: boolean;
342
+ }
343
+ /**
344
+ * Base class for all worker global scopes
345
+ * Part of the Web Worker standard - used for worker context detection
346
+ */
347
+ export declare class WorkerGlobalScope {
348
+ }
349
+ /**
350
+ * Global scope for dedicated workers
351
+ * Part of the Web Worker standard - extends WorkerGlobalScope
352
+ */
353
+ export declare class DedicatedWorkerGlobalScope extends WorkerGlobalScope {
354
+ }
355
+ /**
356
+ * ShovelGlobalScope implements ServiceWorkerGlobalScope
357
+ *
358
+ * This is the `self` object in Shovel ServiceWorker applications.
359
+ * It provides all standard ServiceWorker APIs plus Shovel-specific extensions.
360
+ */
361
+ export declare class ShovelGlobalScope implements ServiceWorkerGlobalScope {
362
+ #private;
363
+ readonly self: any;
364
+ readonly registration: ServiceWorkerRegistration;
365
+ readonly caches: CacheStorage;
366
+ readonly buckets: BucketStorage;
367
+ readonly clients: Clients;
368
+ get cookieStore(): any;
369
+ readonly serviceWorker: any;
370
+ readonly location: WorkerLocation;
371
+ readonly navigator: WorkerNavigator;
372
+ readonly fonts: FontFaceSet;
373
+ readonly indexedDB: IDBFactory;
374
+ readonly isSecureContext: boolean;
375
+ readonly crossOriginIsolated: boolean;
376
+ readonly origin: string;
377
+ readonly performance: Performance;
378
+ readonly crypto: Crypto;
379
+ importScripts(..._urls: (string | URL)[]): void;
380
+ atob(data: string): string;
381
+ btoa(data: string): string;
382
+ clearInterval(id: number): void;
383
+ clearTimeout(id: number): void;
384
+ createImageBitmap(..._args: any[]): Promise<ImageBitmap>;
385
+ fetch(input: RequestInfo | URL, init?: RequestInit): Promise<Response>;
386
+ queueMicrotask(callback: VoidFunction): void;
387
+ reportError(e: any): void;
388
+ setInterval(handler: TimerHandler, timeout?: number, ...args: any[]): number;
389
+ setTimeout(handler: TimerHandler, timeout?: number, ...args: any[]): number;
390
+ structuredClone<T>(value: T, options?: StructuredSerializeOptions): T;
391
+ onactivate: ((this: ServiceWorkerGlobalScope, ev: globalThis.ExtendableEvent) => any) | null;
392
+ oncookiechange: ((this: ServiceWorkerGlobalScope, ev: Event) => any) | null;
393
+ onfetch: ((this: ServiceWorkerGlobalScope, ev: globalThis.FetchEvent) => any) | null;
394
+ oninstall: ((this: ServiceWorkerGlobalScope, ev: globalThis.ExtendableEvent) => any) | null;
395
+ onmessage: ((this: ServiceWorkerGlobalScope, ev: globalThis.ExtendableMessageEvent) => any) | null;
396
+ onmessageerror: ((this: ServiceWorkerGlobalScope, ev: MessageEvent) => any) | null;
397
+ onnotificationclick: ((this: ServiceWorkerGlobalScope, ev: globalThis.NotificationEvent) => any) | null;
398
+ onnotificationclose: ((this: ServiceWorkerGlobalScope, ev: globalThis.NotificationEvent) => any) | null;
399
+ onpush: ((this: ServiceWorkerGlobalScope, ev: globalThis.PushEvent) => any) | null;
400
+ onpushsubscriptionchange: ((this: ServiceWorkerGlobalScope, ev: Event) => any) | null;
401
+ onsync: ((this: ServiceWorkerGlobalScope, ev: SyncEvent) => any) | null;
402
+ onerror: OnErrorEventHandlerNonNull | null;
403
+ onlanguagechange: ((ev: Event) => any) | null;
404
+ onoffline: ((ev: Event) => any) | null;
405
+ ononline: ((ev: Event) => any) | null;
406
+ onrejectionhandled: ((ev: PromiseRejectionEvent) => any) | null;
407
+ onunhandledrejection: ((ev: PromiseRejectionEvent) => any) | null;
408
+ constructor(options: ShovelGlobalScopeOptions);
409
+ /**
410
+ * Standard ServiceWorker skipWaiting() implementation
411
+ * Allows the ServiceWorker to activate immediately
412
+ */
413
+ skipWaiting(): Promise<void>;
414
+ /**
415
+ * Event target delegation to registration
416
+ */
417
+ addEventListener(type: string, listener: EventListenerOrEventListenerObject | null, options?: boolean | AddEventListenerOptions): void;
418
+ removeEventListener(type: string, listener: EventListenerOrEventListenerObject | null, options?: boolean | EventListenerOptions): void;
419
+ dispatchEvent(event: Event): boolean;
420
+ /**
421
+ * Install this scope as the global scope
422
+ * Sets up globalThis with all ServiceWorker globals
423
+ */
424
+ install(): void;
425
+ }
426
+ export {};