@b9g/platform-bun 0.1.0
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 +49 -0
- package/src/index.d.ts +7 -0
- package/src/index.js +10 -0
- package/src/platform.d.ts +64 -0
- package/src/platform.js +170 -0
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@b9g/platform-bun",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Bun platform adapter for Shovel with hot reloading and built-in TypeScript/JSX support",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"shovel",
|
|
7
|
+
"platform",
|
|
8
|
+
"bun",
|
|
9
|
+
"adapter",
|
|
10
|
+
"hot-reload",
|
|
11
|
+
"typescript",
|
|
12
|
+
"jsx"
|
|
13
|
+
],
|
|
14
|
+
"dependencies": {
|
|
15
|
+
"@b9g/platform": "workspace:*",
|
|
16
|
+
"@b9g/cache": "workspace:*",
|
|
17
|
+
"@b9g/assets": "workspace:*"
|
|
18
|
+
},
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"@b9g/libuild": "^0.1.10",
|
|
21
|
+
"bun-types": "latest"
|
|
22
|
+
},
|
|
23
|
+
"type": "module",
|
|
24
|
+
"types": "src/index.d.ts",
|
|
25
|
+
"module": "src/index.js",
|
|
26
|
+
"exports": {
|
|
27
|
+
".": {
|
|
28
|
+
"types": "./src/index.d.ts",
|
|
29
|
+
"import": "./src/index.js"
|
|
30
|
+
},
|
|
31
|
+
"./platform": {
|
|
32
|
+
"types": "./src/platform.d.ts",
|
|
33
|
+
"import": "./src/platform.js"
|
|
34
|
+
},
|
|
35
|
+
"./platform.js": {
|
|
36
|
+
"types": "./src/platform.d.ts",
|
|
37
|
+
"import": "./src/platform.js"
|
|
38
|
+
},
|
|
39
|
+
"./package.json": "./package.json",
|
|
40
|
+
"./index": {
|
|
41
|
+
"types": "./src/index.d.ts",
|
|
42
|
+
"import": "./src/index.js"
|
|
43
|
+
},
|
|
44
|
+
"./index.js": {
|
|
45
|
+
"types": "./src/index.d.ts",
|
|
46
|
+
"import": "./src/index.js"
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @b9g/platform-bun - Bun platform adapter for Shovel
|
|
3
|
+
*
|
|
4
|
+
* Provides built-in TypeScript/JSX support and simplified server setup for Bun environments.
|
|
5
|
+
*/
|
|
6
|
+
export { BunPlatform, createBunPlatform, type BunPlatformOptions, } from "./platform.js";
|
|
7
|
+
export type { Platform, CacheConfig, StaticConfig, Handler, Server, ServerOptions, ServiceWorkerOptions, ServiceWorkerInstance, } from "@b9g/platform";
|
package/src/index.js
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bun platform implementation - ServiceWorker entrypoint loader for Bun
|
|
3
|
+
*
|
|
4
|
+
* Bun has built-in TypeScript/JSX support, so this is much simpler than Node.js.
|
|
5
|
+
* Uses native imports instead of complex ESBuild VM system.
|
|
6
|
+
*/
|
|
7
|
+
import { BasePlatform, PlatformConfig, CacheConfig, Handler, Server, ServerOptions, ServiceWorkerOptions, ServiceWorkerInstance } from "@b9g/platform";
|
|
8
|
+
import { CustomCacheStorage } from "@b9g/cache";
|
|
9
|
+
export interface BunPlatformOptions extends PlatformConfig {
|
|
10
|
+
/** Enable hot reloading (default: true in development) */
|
|
11
|
+
hotReload?: boolean;
|
|
12
|
+
/** Port for development server (default: 3000) */
|
|
13
|
+
port?: number;
|
|
14
|
+
/** Host for development server (default: localhost) */
|
|
15
|
+
host?: string;
|
|
16
|
+
/** Working directory for file resolution */
|
|
17
|
+
cwd?: string;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Bun platform implementation
|
|
21
|
+
* ServiceWorker entrypoint loader for Bun with native TypeScript/JSX support
|
|
22
|
+
*/
|
|
23
|
+
export declare class BunPlatform extends BasePlatform {
|
|
24
|
+
readonly name = "bun";
|
|
25
|
+
private options;
|
|
26
|
+
private _dist?;
|
|
27
|
+
constructor(options?: BunPlatformOptions);
|
|
28
|
+
/**
|
|
29
|
+
* Build artifacts filesystem (install-time only)
|
|
30
|
+
*/
|
|
31
|
+
get distDir(): FileSystemDirectoryHandle;
|
|
32
|
+
/**
|
|
33
|
+
* Get platform-specific default cache configuration for Bun
|
|
34
|
+
*/
|
|
35
|
+
protected getDefaultCacheConfig(): CacheConfig;
|
|
36
|
+
/**
|
|
37
|
+
* Override cache creation to use PostMessage coordination for Bun
|
|
38
|
+
*/
|
|
39
|
+
createCaches(config?: CacheConfig): Promise<CustomCacheStorage>;
|
|
40
|
+
/**
|
|
41
|
+
* Create HTTP server using Bun.serve
|
|
42
|
+
*/
|
|
43
|
+
createServer(handler: Handler, options?: ServerOptions): Server;
|
|
44
|
+
/**
|
|
45
|
+
* Load and run a ServiceWorker-style entrypoint with Bun
|
|
46
|
+
*/
|
|
47
|
+
loadServiceWorker(entrypoint: string, options?: ServiceWorkerOptions): Promise<ServiceWorkerInstance>;
|
|
48
|
+
/**
|
|
49
|
+
* Get filesystem root for File System Access API
|
|
50
|
+
*/
|
|
51
|
+
getFileSystemRoot(name?: string): Promise<FileSystemDirectoryHandle>;
|
|
52
|
+
/**
|
|
53
|
+
* Dispose of platform resources
|
|
54
|
+
*/
|
|
55
|
+
dispose(): Promise<void>;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Create a Bun platform instance
|
|
59
|
+
*/
|
|
60
|
+
export declare function createBunPlatform(options?: BunPlatformOptions): BunPlatform;
|
|
61
|
+
/**
|
|
62
|
+
* Default export for easy importing
|
|
63
|
+
*/
|
|
64
|
+
export default createBunPlatform;
|
package/src/platform.js
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
/// <reference types="./platform.d.ts" />
|
|
2
|
+
// src/platform.ts
|
|
3
|
+
import {
|
|
4
|
+
BasePlatform,
|
|
5
|
+
ServiceWorkerRuntime,
|
|
6
|
+
createServiceWorkerGlobals,
|
|
7
|
+
createDirectoryStorage
|
|
8
|
+
} from "@b9g/platform";
|
|
9
|
+
import { CustomCacheStorage, PostMessageCache } from "@b9g/cache";
|
|
10
|
+
import { FileSystemRegistry, getFileSystemRoot, MemoryFileSystemAdapter, NodeFileSystemAdapter, BunS3FileSystemAdapter } from "@b9g/filesystem";
|
|
11
|
+
import * as Path from "path";
|
|
12
|
+
var BunPlatform = class extends BasePlatform {
|
|
13
|
+
name = "bun";
|
|
14
|
+
options;
|
|
15
|
+
_dist;
|
|
16
|
+
constructor(options = {}) {
|
|
17
|
+
super(options);
|
|
18
|
+
this.options = {
|
|
19
|
+
hotReload: Bun.env.NODE_ENV !== "production",
|
|
20
|
+
port: 3e3,
|
|
21
|
+
host: "localhost",
|
|
22
|
+
cwd: process.cwd(),
|
|
23
|
+
...options
|
|
24
|
+
};
|
|
25
|
+
FileSystemRegistry.register("memory", new MemoryFileSystemAdapter());
|
|
26
|
+
FileSystemRegistry.register("node", new NodeFileSystemAdapter({
|
|
27
|
+
rootPath: Path.join(this.options.cwd, "dist")
|
|
28
|
+
}));
|
|
29
|
+
try {
|
|
30
|
+
FileSystemRegistry.register("bun-s3", new BunS3FileSystemAdapter(
|
|
31
|
+
// @ts-ignore - Bun's S3Client
|
|
32
|
+
new Bun.S3Client({})
|
|
33
|
+
));
|
|
34
|
+
} catch {
|
|
35
|
+
console.warn("[Bun] S3Client not available, using memory filesystem");
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Build artifacts filesystem (install-time only)
|
|
40
|
+
*/
|
|
41
|
+
get distDir() {
|
|
42
|
+
if (!this._dist) {
|
|
43
|
+
const distPath = Path.resolve(this.options.cwd, "dist");
|
|
44
|
+
this._dist = new NodeFileSystemAdapter({ rootPath: distPath }).getFileSystemRoot("");
|
|
45
|
+
}
|
|
46
|
+
return this._dist;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Get platform-specific default cache configuration for Bun
|
|
50
|
+
*/
|
|
51
|
+
getDefaultCacheConfig() {
|
|
52
|
+
return {
|
|
53
|
+
pages: { type: "memory" },
|
|
54
|
+
// PostMessage cache for coordination
|
|
55
|
+
api: { type: "memory" },
|
|
56
|
+
static: { type: "memory" }
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Override cache creation to use PostMessage coordination for Bun
|
|
61
|
+
*/
|
|
62
|
+
async createCaches(config) {
|
|
63
|
+
return new CustomCacheStorage((name) => {
|
|
64
|
+
return new PostMessageCache(name, {
|
|
65
|
+
maxEntries: 1e3,
|
|
66
|
+
maxSize: 50 * 1024 * 1024
|
|
67
|
+
// 50MB
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Create HTTP server using Bun.serve
|
|
73
|
+
*/
|
|
74
|
+
createServer(handler, options = {}) {
|
|
75
|
+
const port = options.port ?? this.options.port;
|
|
76
|
+
const hostname = options.host ?? this.options.host;
|
|
77
|
+
const server = Bun.serve({
|
|
78
|
+
port,
|
|
79
|
+
hostname,
|
|
80
|
+
async fetch(request) {
|
|
81
|
+
return handler(request);
|
|
82
|
+
},
|
|
83
|
+
development: this.options.hotReload
|
|
84
|
+
});
|
|
85
|
+
return {
|
|
86
|
+
listen: () => {
|
|
87
|
+
console.info(`\u{1F956} Bun server running at http://${hostname}:${port}`);
|
|
88
|
+
return Promise.resolve();
|
|
89
|
+
},
|
|
90
|
+
close: () => {
|
|
91
|
+
server.stop();
|
|
92
|
+
return Promise.resolve();
|
|
93
|
+
},
|
|
94
|
+
address: () => ({ port, host: hostname })
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Load and run a ServiceWorker-style entrypoint with Bun
|
|
99
|
+
*/
|
|
100
|
+
async loadServiceWorker(entrypoint, options = {}) {
|
|
101
|
+
const runtime = new ServiceWorkerRuntime();
|
|
102
|
+
const entryPath = Path.resolve(this.options.cwd, entrypoint);
|
|
103
|
+
const caches = await this.createCaches(options.caches);
|
|
104
|
+
const dirs = createDirectoryStorage(this.distDir);
|
|
105
|
+
const instance = {
|
|
106
|
+
runtime,
|
|
107
|
+
handleRequest: (request) => runtime.handleRequest(request),
|
|
108
|
+
install: () => runtime.install(),
|
|
109
|
+
activate: () => runtime.activate(),
|
|
110
|
+
collectStaticRoutes: (outDir, baseUrl) => runtime.collectStaticRoutes(outDir, baseUrl),
|
|
111
|
+
get ready() {
|
|
112
|
+
return runtime.ready;
|
|
113
|
+
},
|
|
114
|
+
dispose: async () => {
|
|
115
|
+
runtime.reset();
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
if (this.options.hotReload && options.hotReload !== false) {
|
|
119
|
+
console.info("[Bun] Hot reloading enabled - native TypeScript support");
|
|
120
|
+
const loadModule = async () => {
|
|
121
|
+
try {
|
|
122
|
+
runtime.reset();
|
|
123
|
+
createServiceWorkerGlobals(runtime, { caches, dirs });
|
|
124
|
+
globalThis.self = runtime;
|
|
125
|
+
globalThis.addEventListener = runtime.addEventListener.bind(runtime);
|
|
126
|
+
globalThis.removeEventListener = runtime.removeEventListener.bind(runtime);
|
|
127
|
+
globalThis.dispatchEvent = runtime.dispatchEvent.bind(runtime);
|
|
128
|
+
const moduleUrl = `${entryPath}?t=${Date.now()}`;
|
|
129
|
+
await import(moduleUrl);
|
|
130
|
+
await runtime.install();
|
|
131
|
+
await runtime.activate();
|
|
132
|
+
console.info("[Bun] ServiceWorker loaded successfully");
|
|
133
|
+
} catch (error) {
|
|
134
|
+
console.error("[Bun] Failed to load ServiceWorker:", error);
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
await loadModule();
|
|
138
|
+
} else {
|
|
139
|
+
createServiceWorkerGlobals(runtime, { caches, dirs });
|
|
140
|
+
globalThis.self = runtime;
|
|
141
|
+
globalThis.addEventListener = runtime.addEventListener.bind(runtime);
|
|
142
|
+
globalThis.removeEventListener = runtime.removeEventListener.bind(runtime);
|
|
143
|
+
globalThis.dispatchEvent = runtime.dispatchEvent.bind(runtime);
|
|
144
|
+
await import(entryPath);
|
|
145
|
+
await runtime.install();
|
|
146
|
+
await runtime.activate();
|
|
147
|
+
}
|
|
148
|
+
return instance;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Get filesystem root for File System Access API
|
|
152
|
+
*/
|
|
153
|
+
async getFileSystemRoot(name = "default") {
|
|
154
|
+
return await getFileSystemRoot(name);
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Dispose of platform resources
|
|
158
|
+
*/
|
|
159
|
+
async dispose() {
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
function createBunPlatform(options) {
|
|
163
|
+
return new BunPlatform(options);
|
|
164
|
+
}
|
|
165
|
+
var platform_default = createBunPlatform;
|
|
166
|
+
export {
|
|
167
|
+
BunPlatform,
|
|
168
|
+
createBunPlatform,
|
|
169
|
+
platform_default as default
|
|
170
|
+
};
|