@b9g/platform-bun 0.1.2 → 0.1.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@b9g/platform-bun",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "description": "Bun platform adapter for Shovel with hot reloading and built-in TypeScript/JSX support",
5
5
  "keywords": [
6
6
  "shovel",
@@ -12,12 +12,12 @@
12
12
  "jsx"
13
13
  ],
14
14
  "dependencies": {
15
- "@b9g/platform": "^0.1.1",
16
- "@b9g/cache": "^0.1.1",
17
- "@b9g/assets": "^0.1.1"
15
+ "@b9g/platform": "^0.1.4",
16
+ "@b9g/cache": "^0.1.3",
17
+ "@b9g/assets": "^0.1.4"
18
18
  },
19
19
  "devDependencies": {
20
- "@b9g/libuild": "^0.1.10",
20
+ "@b9g/libuild": "^0.1.11",
21
21
  "bun-types": "latest"
22
22
  },
23
23
  "type": "module",
package/src/platform.d.ts CHANGED
@@ -23,6 +23,8 @@ export interface BunPlatformOptions extends PlatformConfig {
23
23
  export declare class BunPlatform extends BasePlatform {
24
24
  readonly name = "bun";
25
25
  private options;
26
+ private workerPool?;
27
+ private cacheStorage?;
26
28
  constructor(options?: BunPlatformOptions);
27
29
  /**
28
30
  * Build artifacts filesystem (install-time only)
@@ -42,8 +44,13 @@ export declare class BunPlatform extends BasePlatform {
42
44
  createServer(handler: Handler, options?: ServerOptions): Server;
43
45
  /**
44
46
  * Load and run a ServiceWorker-style entrypoint with Bun
47
+ * Uses native Web Workers with the common WorkerPool
45
48
  */
46
49
  loadServiceWorker(entrypoint: string, options?: ServiceWorkerOptions): Promise<ServiceWorkerInstance>;
50
+ /**
51
+ * Reload workers for hot reloading (called by CLI)
52
+ */
53
+ reloadWorkers(version?: number | string): Promise<void>;
47
54
  /**
48
55
  * Dispose of platform resources
49
56
  */
package/src/platform.js CHANGED
@@ -1,17 +1,17 @@
1
1
  /// <reference types="./platform.d.ts" />
2
2
  // src/platform.ts
3
3
  import {
4
- BasePlatform,
5
- ServiceWorkerRuntime,
6
- createServiceWorkerGlobals,
7
- createBucketStorage
4
+ BasePlatform
8
5
  } from "@b9g/platform";
6
+ import { WorkerPool } from "@b9g/platform/worker-pool";
9
7
  import { CustomCacheStorage, PostMessageCache } from "@b9g/cache";
10
8
  import { FileSystemRegistry, MemoryBucket, LocalBucket } from "@b9g/filesystem";
11
9
  import * as Path from "path";
12
10
  var BunPlatform = class extends BasePlatform {
13
11
  name = "bun";
14
12
  options;
13
+ workerPool;
14
+ cacheStorage;
15
15
  constructor(options = {}) {
16
16
  super(options);
17
17
  this.options = {
@@ -58,19 +58,19 @@ var BunPlatform = class extends BasePlatform {
58
58
  */
59
59
  async createCaches(config) {
60
60
  const { MemoryCache } = await import("@b9g/cache");
61
- const { isMainThread } = await import("worker_threads");
61
+ const isWorkerThread = typeof self !== "undefined" && typeof window === "undefined";
62
62
  return new CustomCacheStorage((name) => {
63
- if (isMainThread) {
63
+ if (!isWorkerThread) {
64
64
  return new MemoryCache(name, {
65
65
  maxEntries: 1e3,
66
- maxSize: 50 * 1024 * 1024
67
- // 50MB
66
+ maxAge: 60 * 60 * 1e3
67
+ // 1 hour
68
68
  });
69
69
  } else {
70
70
  return new PostMessageCache(name, {
71
71
  maxEntries: 1e3,
72
- maxSize: 50 * 1024 * 1024
73
- // 50MB
72
+ maxAge: 60 * 60 * 1e3
73
+ // 1 hour
74
74
  });
75
75
  }
76
76
  });
@@ -107,62 +107,77 @@ var BunPlatform = class extends BasePlatform {
107
107
  }
108
108
  /**
109
109
  * Load and run a ServiceWorker-style entrypoint with Bun
110
+ * Uses native Web Workers with the common WorkerPool
110
111
  */
111
112
  async loadServiceWorker(entrypoint, options = {}) {
112
- const runtime = new ServiceWorkerRuntime();
113
113
  const entryPath = Path.resolve(this.options.cwd, entrypoint);
114
- const caches = await this.createCaches(options.caches);
115
- const distPath = Path.resolve(this.options.cwd, "dist");
116
- const buckets = createBucketStorage(distPath);
114
+ if (!this.cacheStorage) {
115
+ this.cacheStorage = await this.createCaches(options.caches);
116
+ }
117
+ if (this.workerPool) {
118
+ await this.workerPool.terminate();
119
+ }
120
+ const workerCount = options.workerCount || 1;
121
+ const poolOptions = {
122
+ workerCount,
123
+ requestTimeout: 3e4,
124
+ hotReload: this.options.hotReload,
125
+ cwd: this.options.cwd
126
+ };
127
+ this.workerPool = new WorkerPool(
128
+ this.cacheStorage,
129
+ poolOptions,
130
+ entryPath
131
+ );
132
+ await this.workerPool.init();
133
+ const version = Date.now();
134
+ await this.workerPool.reloadWorkers(version);
117
135
  const instance = {
118
- runtime,
119
- handleRequest: (request) => runtime.handleRequest(request),
120
- install: () => runtime.install(),
121
- activate: () => runtime.activate(),
122
- collectStaticRoutes: (outDir, baseUrl) => runtime.collectStaticRoutes(outDir, baseUrl),
136
+ runtime: this.workerPool,
137
+ handleRequest: async (request) => {
138
+ if (!this.workerPool) {
139
+ throw new Error("WorkerPool not initialized");
140
+ }
141
+ return this.workerPool.handleRequest(request);
142
+ },
143
+ install: async () => {
144
+ console.info("[Bun] ServiceWorker installed via native Web Workers");
145
+ },
146
+ activate: async () => {
147
+ console.info("[Bun] ServiceWorker activated via native Web Workers");
148
+ },
149
+ collectStaticRoutes: async () => {
150
+ return [];
151
+ },
123
152
  get ready() {
124
- return runtime.ready;
153
+ return this.workerPool?.ready ?? false;
125
154
  },
126
155
  dispose: async () => {
127
- runtime.reset();
156
+ if (this.workerPool) {
157
+ await this.workerPool.terminate();
158
+ this.workerPool = void 0;
159
+ }
160
+ console.info("[Bun] ServiceWorker disposed");
128
161
  }
129
162
  };
130
- if (this.options.hotReload && options.hotReload !== false) {
131
- console.info("[Bun] Hot reloading enabled - native TypeScript support");
132
- const loadModule = async () => {
133
- try {
134
- runtime.reset();
135
- createServiceWorkerGlobals(runtime, { caches, buckets });
136
- globalThis.self = runtime;
137
- globalThis.addEventListener = runtime.addEventListener.bind(runtime);
138
- globalThis.removeEventListener = runtime.removeEventListener.bind(runtime);
139
- globalThis.dispatchEvent = runtime.dispatchEvent.bind(runtime);
140
- const moduleUrl = `${entryPath}?t=${Date.now()}`;
141
- await import(moduleUrl);
142
- await runtime.install();
143
- await runtime.activate();
144
- console.info("[Bun] ServiceWorker loaded successfully");
145
- } catch (error) {
146
- console.error("[Bun] Failed to load ServiceWorker:", error);
147
- }
148
- };
149
- await loadModule();
150
- } else {
151
- createServiceWorkerGlobals(runtime, { caches, buckets });
152
- globalThis.self = runtime;
153
- globalThis.addEventListener = runtime.addEventListener.bind(runtime);
154
- globalThis.removeEventListener = runtime.removeEventListener.bind(runtime);
155
- globalThis.dispatchEvent = runtime.dispatchEvent.bind(runtime);
156
- await import(entryPath);
157
- await runtime.install();
158
- await runtime.activate();
159
- }
160
163
  return instance;
161
164
  }
165
+ /**
166
+ * Reload workers for hot reloading (called by CLI)
167
+ */
168
+ async reloadWorkers(version) {
169
+ if (this.workerPool) {
170
+ await this.workerPool.reloadWorkers(version);
171
+ }
172
+ }
162
173
  /**
163
174
  * Dispose of platform resources
164
175
  */
165
176
  async dispose() {
177
+ if (this.workerPool) {
178
+ await this.workerPool.terminate();
179
+ this.workerPool = void 0;
180
+ }
166
181
  }
167
182
  };
168
183
  function createBunPlatform(options) {