@b9g/platform 0.1.10 → 0.1.12

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/src/runtime.d.ts CHANGED
@@ -2,14 +2,97 @@
2
2
  * ServiceWorker Runtime - Complete ServiceWorker API Implementation
3
3
  *
4
4
  * This module provides the complete ServiceWorker runtime environment for Shovel:
5
+ * - Cookie Store API (RequestCookieStore for per-request cookie management)
5
6
  * - Base Event Classes (ExtendableEvent, FetchEvent, InstallEvent, ActivateEvent)
6
7
  * - ServiceWorker API Type Shims (Client, Clients, ServiceWorkerRegistration, etc.)
7
- * - ShovelGlobalScope (implements ServiceWorkerGlobalScope for any JavaScript runtime)
8
+ * - ServiceWorkerGlobals (installs ServiceWorker globals onto any JavaScript runtime)
8
9
  *
9
10
  * Based on: https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API#interfaces
10
11
  */
11
- import { RequestCookieStore } from "./cookie-store.js";
12
- import type { BucketStorage } from "@b9g/filesystem";
12
+ declare global {
13
+ interface CookieListItem {
14
+ domain?: string;
15
+ path?: string;
16
+ expires?: number;
17
+ secure?: boolean;
18
+ sameSite?: CookieSameSite;
19
+ partitioned?: boolean;
20
+ }
21
+ }
22
+ export type CookieSameSite = globalThis.CookieSameSite;
23
+ export type CookieInit = globalThis.CookieInit;
24
+ export type CookieStoreGetOptions = globalThis.CookieStoreGetOptions;
25
+ export type CookieStoreDeleteOptions = globalThis.CookieStoreDeleteOptions;
26
+ export type CookieListItem = globalThis.CookieListItem;
27
+ export type CookieList = CookieListItem[];
28
+ /**
29
+ * Parse Cookie header value into key-value pairs
30
+ * Cookie: name=value; name2=value2
31
+ */
32
+ export declare function parseCookieHeader(cookieHeader: string): Map<string, string>;
33
+ /**
34
+ * Serialize cookie into Set-Cookie header value
35
+ */
36
+ export declare function serializeCookie(cookie: CookieInit): string;
37
+ /**
38
+ * Parse Set-Cookie header into CookieListItem
39
+ */
40
+ export declare function parseSetCookieHeader(setCookieHeader: string): CookieListItem;
41
+ /**
42
+ * RequestCookieStore - Cookie Store implementation for ServiceWorker contexts
43
+ *
44
+ * This implementation:
45
+ * - Reads cookies from the incoming Request's Cookie header
46
+ * - Tracks changes (set/delete operations)
47
+ * - Exports changes as Set-Cookie headers for the Response
48
+ *
49
+ * It follows the Cookie Store API spec but is designed for server-side
50
+ * request handling rather than browser contexts.
51
+ */
52
+ export declare class RequestCookieStore extends EventTarget {
53
+ #private;
54
+ onchange: ((this: RequestCookieStore, ev: Event) => any) | null;
55
+ constructor(request?: Request);
56
+ get(nameOrOptions: string | CookieStoreGetOptions): Promise<CookieListItem | null>;
57
+ getAll(nameOrOptions?: string | CookieStoreGetOptions): Promise<CookieList>;
58
+ set(nameOrOptions: string | CookieInit, value?: string): Promise<void>;
59
+ delete(nameOrOptions: string | CookieStoreDeleteOptions): Promise<void>;
60
+ /**
61
+ * Get Set-Cookie headers for all changes
62
+ * This should be called when constructing the Response
63
+ */
64
+ getSetCookieHeaders(): string[];
65
+ hasChanges(): boolean;
66
+ clearChanges(): void;
67
+ }
68
+ import type { DirectoryStorage } from "@b9g/filesystem";
69
+ import { type Logger } from "@logtape/logtape";
70
+ /**
71
+ * Logger storage interface for accessing loggers by category path.
72
+ * Unlike CacheStorage/DirectoryStorage which use a registry pattern,
73
+ * LoggerStorage uses variadic categories since logging backends are
74
+ * always LogTape and per-category config is in shovel.config.json.
75
+ */
76
+ export interface LoggerStorage {
77
+ /**
78
+ * Open a logger by category path - returns LogTape's Logger directly
79
+ * @example loggers.open("app") → getLogger(["app"])
80
+ * @example loggers.open("app", "db") → getLogger(["app", "db"])
81
+ */
82
+ open(...categories: string[]): Logger;
83
+ }
84
+ /**
85
+ * Factory function type for creating loggers
86
+ */
87
+ export type LoggerFactory = (...categories: string[]) => Logger;
88
+ /**
89
+ * Custom logger storage implementation that wraps a factory function
90
+ */
91
+ export declare class CustomLoggerStorage implements LoggerStorage {
92
+ #private;
93
+ constructor(factory: LoggerFactory);
94
+ open(...categories: string[]): Logger;
95
+ }
13
96
  /** Symbol for ending dispatch phase (internal use only) */
14
97
  declare const kEndDispatchPhase: unique symbol;
15
98
  /** Symbol for checking if extensions are allowed (internal use only) */
@@ -330,11 +413,13 @@ interface NotificationOptions {
330
413
  timestamp?: number;
331
414
  vibrate?: number[];
332
415
  }
333
- export interface ShovelGlobalScopeOptions {
416
+ export interface ServiceWorkerGlobalsOptions {
334
417
  /** ServiceWorker registration instance */
335
418
  registration: ServiceWorkerRegistration;
336
- /** Bucket storage (file system access) - REQUIRED */
337
- buckets: BucketStorage;
419
+ /** Directory storage (file system access) - REQUIRED */
420
+ directories: DirectoryStorage;
421
+ /** Logger storage (logging access) - REQUIRED */
422
+ loggers: LoggerStorage;
338
423
  /** Cache storage (required by ServiceWorkerGlobalScope) - REQUIRED */
339
424
  caches: CacheStorage;
340
425
  /** Development mode flag */
@@ -353,17 +438,21 @@ export declare class WorkerGlobalScope {
353
438
  export declare class DedicatedWorkerGlobalScope extends WorkerGlobalScope {
354
439
  }
355
440
  /**
356
- * ShovelGlobalScope implements ServiceWorkerGlobalScope
441
+ * ServiceWorkerGlobals - Installs ServiceWorker globals onto globalThis
442
+ *
443
+ * This class holds ServiceWorker API implementations (caches, directories, clients, etc.)
444
+ * and patches them onto globalThis via install(). It maintains the browser invariant
445
+ * that self === globalThis while providing ServiceWorker APIs.
357
446
  *
358
- * This is the `self` object in Shovel ServiceWorker applications.
359
- * It provides all standard ServiceWorker APIs plus Shovel-specific extensions.
447
+ * Use restore() to revert all patches (useful for testing).
360
448
  */
361
- export declare class ShovelGlobalScope implements ServiceWorkerGlobalScope {
449
+ export declare class ServiceWorkerGlobals implements ServiceWorkerGlobalScope {
362
450
  #private;
363
451
  readonly self: any;
364
452
  readonly registration: ServiceWorkerRegistration;
365
453
  readonly caches: CacheStorage;
366
- readonly buckets: BucketStorage;
454
+ readonly directories: DirectoryStorage;
455
+ readonly loggers: LoggerStorage;
367
456
  readonly clients: Clients;
368
457
  get cookieStore(): any;
369
458
  readonly serviceWorker: any;
@@ -405,22 +494,89 @@ export declare class ShovelGlobalScope implements ServiceWorkerGlobalScope {
405
494
  ononline: ((ev: Event) => any) | null;
406
495
  onrejectionhandled: ((ev: PromiseRejectionEvent) => any) | null;
407
496
  onunhandledrejection: ((ev: PromiseRejectionEvent) => any) | null;
408
- constructor(options: ShovelGlobalScopeOptions);
497
+ constructor(options: ServiceWorkerGlobalsOptions);
409
498
  /**
410
499
  * Standard ServiceWorker skipWaiting() implementation
411
500
  * Allows the ServiceWorker to activate immediately
412
501
  */
413
502
  skipWaiting(): Promise<void>;
414
503
  /**
415
- * Event target delegation to registration
504
+ * Event target delegation - ServiceWorker events go to registration,
505
+ * other events (like "message" for worker threads) go to native handler
416
506
  */
417
507
  addEventListener(type: string, listener: EventListenerOrEventListenerObject | null, options?: boolean | AddEventListenerOptions): void;
418
508
  removeEventListener(type: string, listener: EventListenerOrEventListenerObject | null, options?: boolean | EventListenerOptions): void;
419
509
  dispatchEvent(event: Event): boolean;
420
510
  /**
421
511
  * Install this scope as the global scope
422
- * Sets up globalThis with all ServiceWorker globals
512
+ * Patches globalThis with ServiceWorker globals while maintaining self === globalThis
423
513
  */
424
514
  install(): void;
515
+ /**
516
+ * Restore original globals (for testing)
517
+ * Reverts all patched globals to their original values
518
+ */
519
+ restore(): void;
520
+ }
521
+ /** Log level for filtering */
522
+ export type LogLevel = "debug" | "info" | "warning" | "error";
523
+ /** Sink configuration */
524
+ export interface SinkConfig {
525
+ provider: string;
526
+ /** Pre-imported factory function (from build-time code generation) */
527
+ factory?: (options: Record<string, unknown>) => unknown;
528
+ /** Provider-specific options (path, maxSize, etc.) */
529
+ [key: string]: any;
425
530
  }
531
+ /** Per-category logging configuration */
532
+ export interface CategoryLoggingConfig {
533
+ level?: LogLevel;
534
+ sinks?: SinkConfig[];
535
+ }
536
+ export interface LoggingConfig {
537
+ /** Default log level. Defaults to "info" */
538
+ level?: LogLevel;
539
+ /** Default sinks. Defaults to console */
540
+ sinks?: SinkConfig[];
541
+ /** Per-category config (inherits from top-level, can override level and/or sinks) */
542
+ categories?: Record<string, CategoryLoggingConfig>;
543
+ }
544
+ /** Processed logging config with all defaults applied */
545
+ export interface ProcessedLoggingConfig {
546
+ level: LogLevel;
547
+ sinks: SinkConfig[];
548
+ categories: Record<string, CategoryLoggingConfig>;
549
+ }
550
+ /** Cache provider configuration */
551
+ export interface CacheConfig {
552
+ provider?: string;
553
+ [key: string]: unknown;
554
+ }
555
+ /** Directory (filesystem) provider configuration */
556
+ export interface DirectoryConfig {
557
+ provider?: string;
558
+ path?: string;
559
+ [key: string]: unknown;
560
+ }
561
+ /** Shovel application configuration (from shovel.json) */
562
+ export interface ShovelConfig {
563
+ port?: number;
564
+ host?: string;
565
+ workers?: number;
566
+ platform?: string;
567
+ logging?: LoggingConfig;
568
+ caches?: Record<string, CacheConfig>;
569
+ directories?: Record<string, DirectoryConfig>;
570
+ }
571
+ /**
572
+ * Configure LogTape logging based on Shovel config.
573
+ * Call this in both main thread and workers.
574
+ *
575
+ * @param loggingConfig - The logging configuration (sinks defaults to console)
576
+ * @param options - Additional options
577
+ * @param options.reset - Whether to reset existing LogTape config (default: true)
578
+ */
579
+ export declare function configureLogging(loggingConfig: LoggingConfig, options?: {
580
+ reset?: boolean;
581
+ }): Promise<void>;
426
582
  export {};