@b9g/platform 0.1.4 → 0.1.5
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 +94 -28
- package/package.json +44 -86
- package/src/config.d.ts +105 -0
- package/src/config.js +463 -0
- package/src/cookie-store.d.ts +80 -0
- package/src/cookie-store.js +231 -0
- package/src/index.d.ts +189 -9
- package/src/index.js +242 -49
- package/src/runtime.d.ts +426 -0
- package/src/runtime.js +997 -0
- package/src/single-threaded.d.ts +57 -0
- package/src/single-threaded.js +107 -0
- package/src/worker-pool.d.ts +22 -32
- package/src/worker-pool.js +214 -100
- package/src/adapter-registry.d.ts +0 -53
- package/src/adapter-registry.js +0 -71
- package/src/base-platform.d.ts +0 -49
- package/src/base-platform.js +0 -114
- package/src/detection.d.ts +0 -36
- package/src/detection.js +0 -126
- package/src/directory-storage.d.ts +0 -41
- package/src/directory-storage.js +0 -53
- package/src/filesystem.d.ts +0 -16
- package/src/filesystem.js +0 -20
- package/src/registry.d.ts +0 -31
- package/src/registry.js +0 -74
- package/src/service-worker.d.ts +0 -175
- package/src/service-worker.js +0 -245
- package/src/types.d.ts +0 -246
- package/src/types.js +0 -36
- package/src/utils.d.ts +0 -33
- package/src/utils.js +0 -139
- package/src/worker-web.js +0 -119
package/src/index.js
CHANGED
|
@@ -1,66 +1,259 @@
|
|
|
1
1
|
/// <reference types="./index.d.ts" />
|
|
2
2
|
// src/index.ts
|
|
3
|
-
import
|
|
3
|
+
import * as Path from "path";
|
|
4
|
+
import { readFileSync } from "fs";
|
|
5
|
+
import { CustomCacheStorage } from "@b9g/cache";
|
|
6
|
+
import { MemoryCache } from "@b9g/cache/memory";
|
|
4
7
|
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
} from "./service-worker.js";
|
|
8
|
+
ServiceWorkerPool
|
|
9
|
+
} from "./worker-pool.js";
|
|
8
10
|
import {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
} from "./directory-storage.js";
|
|
11
|
+
SingleThreadedRuntime
|
|
12
|
+
} from "./single-threaded.js";
|
|
12
13
|
import {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
14
|
+
ShovelServiceWorkerRegistration,
|
|
15
|
+
ShovelGlobalScope,
|
|
16
|
+
FetchEvent,
|
|
17
|
+
InstallEvent,
|
|
18
|
+
ActivateEvent,
|
|
19
|
+
ExtendableEvent
|
|
20
|
+
} from "./runtime.js";
|
|
18
21
|
import {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
displayPlatformInfo
|
|
26
|
-
} from "./detection.js";
|
|
22
|
+
RequestCookieStore,
|
|
23
|
+
parseCookieHeader,
|
|
24
|
+
serializeCookie,
|
|
25
|
+
parseSetCookieHeader
|
|
26
|
+
} from "./cookie-store.js";
|
|
27
|
+
import { CustomBucketStorage } from "@b9g/filesystem";
|
|
27
28
|
import {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
29
|
+
loadConfig,
|
|
30
|
+
getCacheConfig,
|
|
31
|
+
getBucketConfig,
|
|
32
|
+
parseConfigExpr,
|
|
33
|
+
processConfigValue,
|
|
34
|
+
matchPattern,
|
|
35
|
+
createBucketFactory,
|
|
36
|
+
createCacheFactory
|
|
37
|
+
} from "./config.js";
|
|
38
|
+
function detectRuntime() {
|
|
39
|
+
if (typeof Bun !== "undefined" || process.versions?.bun) {
|
|
40
|
+
return "bun";
|
|
41
|
+
}
|
|
42
|
+
if (typeof Deno !== "undefined") {
|
|
43
|
+
return "deno";
|
|
44
|
+
}
|
|
45
|
+
return "node";
|
|
46
|
+
}
|
|
47
|
+
function detectPlatformFromPackageJSON(cwd) {
|
|
48
|
+
if (!cwd && typeof process === "undefined") {
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
try {
|
|
52
|
+
const pkgPath = Path.join(cwd || process.cwd(), "package.json");
|
|
53
|
+
const pkgContent = readFileSync(pkgPath, "utf8");
|
|
54
|
+
const pkg = JSON.parse(pkgContent);
|
|
55
|
+
const deps = { ...pkg.dependencies, ...pkg.devDependencies };
|
|
56
|
+
return selectPlatformFromDeps(deps);
|
|
57
|
+
} catch {
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
function selectPlatformFromDeps(deps) {
|
|
62
|
+
const hasBun = deps["@b9g/platform-bun"];
|
|
63
|
+
const hasNode = deps["@b9g/platform-node"];
|
|
64
|
+
const hasCloudflare = deps["@b9g/platform-cloudflare"];
|
|
65
|
+
const installedCount = [hasBun, hasNode, hasCloudflare].filter(
|
|
66
|
+
Boolean
|
|
67
|
+
).length;
|
|
68
|
+
if (installedCount === 0)
|
|
69
|
+
return null;
|
|
70
|
+
if (installedCount === 1) {
|
|
71
|
+
if (hasBun)
|
|
72
|
+
return "bun";
|
|
73
|
+
if (hasNode)
|
|
74
|
+
return "node";
|
|
75
|
+
if (hasCloudflare)
|
|
76
|
+
return "cloudflare";
|
|
77
|
+
}
|
|
78
|
+
const runtime = detectRuntime();
|
|
79
|
+
if (runtime === "bun" && hasBun)
|
|
80
|
+
return "bun";
|
|
81
|
+
if (runtime === "node" && hasNode)
|
|
82
|
+
return "node";
|
|
83
|
+
if (hasBun)
|
|
84
|
+
return "bun";
|
|
85
|
+
if (hasNode)
|
|
86
|
+
return "node";
|
|
87
|
+
if (hasCloudflare)
|
|
88
|
+
return "cloudflare";
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
function detectDeploymentPlatform() {
|
|
92
|
+
if (typeof process !== "undefined" && (process.versions?.node || process.versions?.bun)) {
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
if (typeof caches !== "undefined" && typeof addEventListener !== "undefined" && typeof fetch !== "undefined" && // Ensure we're not in a browser
|
|
96
|
+
typeof window === "undefined") {
|
|
97
|
+
return "cloudflare";
|
|
98
|
+
}
|
|
99
|
+
return null;
|
|
100
|
+
}
|
|
101
|
+
function detectDevelopmentPlatform() {
|
|
102
|
+
const pkgPlatform = detectPlatformFromPackageJSON();
|
|
103
|
+
if (pkgPlatform) {
|
|
104
|
+
return pkgPlatform;
|
|
105
|
+
}
|
|
106
|
+
const runtime = detectRuntime();
|
|
107
|
+
switch (runtime) {
|
|
108
|
+
case "bun":
|
|
109
|
+
return "bun";
|
|
110
|
+
case "deno":
|
|
111
|
+
return "deno";
|
|
112
|
+
case "node":
|
|
113
|
+
default:
|
|
114
|
+
return "node";
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
function resolvePlatform(options) {
|
|
118
|
+
if (options.platform) {
|
|
119
|
+
return options.platform;
|
|
120
|
+
}
|
|
121
|
+
if (options.target) {
|
|
122
|
+
return options.target;
|
|
123
|
+
}
|
|
124
|
+
const deploymentPlatform = detectDeploymentPlatform();
|
|
125
|
+
if (deploymentPlatform) {
|
|
126
|
+
return deploymentPlatform;
|
|
127
|
+
}
|
|
128
|
+
return detectDevelopmentPlatform();
|
|
129
|
+
}
|
|
130
|
+
async function createPlatform(platformName, options = {}) {
|
|
131
|
+
switch (platformName) {
|
|
132
|
+
case "node": {
|
|
133
|
+
const modulePath = import.meta.resolve("@b9g/platform-node");
|
|
134
|
+
const NodePlatform = await import(modulePath).then((m) => m.default);
|
|
135
|
+
return new NodePlatform(options);
|
|
136
|
+
}
|
|
137
|
+
case "bun": {
|
|
138
|
+
const modulePath = import.meta.resolve("@b9g/platform-bun");
|
|
139
|
+
const BunPlatform = await import(modulePath).then((m) => m.default);
|
|
140
|
+
return new BunPlatform(options);
|
|
141
|
+
}
|
|
142
|
+
case "cloudflare":
|
|
143
|
+
case "cloudflare-workers":
|
|
144
|
+
case "cf": {
|
|
145
|
+
const modulePath = import.meta.resolve("@b9g/platform-cloudflare");
|
|
146
|
+
const CloudflarePlatform = await import(modulePath).then(
|
|
147
|
+
(m) => m.default
|
|
148
|
+
);
|
|
149
|
+
return new CloudflarePlatform(options);
|
|
150
|
+
}
|
|
151
|
+
default:
|
|
152
|
+
throw new Error(
|
|
153
|
+
`Unknown platform: ${platformName}. Available platforms: node, bun, cloudflare`
|
|
154
|
+
);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
var BasePlatform = class {
|
|
158
|
+
config;
|
|
159
|
+
constructor(config = {}) {
|
|
160
|
+
this.config = config;
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Create cache storage
|
|
164
|
+
* Returns empty CacheStorage - applications create caches on-demand via caches.open()
|
|
165
|
+
*/
|
|
166
|
+
async createCaches() {
|
|
167
|
+
return new CustomCacheStorage(
|
|
168
|
+
(name) => new MemoryCache(name)
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
var DefaultPlatformRegistry = class {
|
|
173
|
+
#platforms;
|
|
174
|
+
constructor() {
|
|
175
|
+
this.#platforms = /* @__PURE__ */ new Map();
|
|
176
|
+
}
|
|
177
|
+
register(name, platform) {
|
|
178
|
+
this.#platforms.set(name, platform);
|
|
179
|
+
}
|
|
180
|
+
get(name) {
|
|
181
|
+
return this.#platforms.get(name);
|
|
182
|
+
}
|
|
183
|
+
list() {
|
|
184
|
+
return Array.from(this.#platforms.keys());
|
|
185
|
+
}
|
|
186
|
+
};
|
|
187
|
+
var platformRegistry = new DefaultPlatformRegistry();
|
|
188
|
+
function getPlatform(name) {
|
|
189
|
+
if (name) {
|
|
190
|
+
const platform2 = platformRegistry.get(name);
|
|
191
|
+
if (!platform2) {
|
|
192
|
+
const available = platformRegistry.list();
|
|
193
|
+
throw new Error(
|
|
194
|
+
`Platform '${name}' not found. Available platforms: ${available.join(", ")}`
|
|
195
|
+
);
|
|
196
|
+
}
|
|
197
|
+
return platform2;
|
|
198
|
+
}
|
|
199
|
+
const platformName = detectDeploymentPlatform() || detectDevelopmentPlatform();
|
|
200
|
+
const platform = platformRegistry.get(platformName);
|
|
201
|
+
if (!platform) {
|
|
202
|
+
throw new Error(
|
|
203
|
+
`Detected platform '${platformName}' not registered. Please register it manually or specify a platform name.`
|
|
204
|
+
);
|
|
205
|
+
}
|
|
206
|
+
return platform;
|
|
207
|
+
}
|
|
208
|
+
async function getPlatformAsync(name) {
|
|
209
|
+
if (name) {
|
|
210
|
+
const platform2 = platformRegistry.get(name);
|
|
211
|
+
if (!platform2) {
|
|
212
|
+
const available = platformRegistry.list();
|
|
213
|
+
throw new Error(
|
|
214
|
+
`Platform '${name}' not found. Available platforms: ${available.join(", ")}`
|
|
215
|
+
);
|
|
216
|
+
}
|
|
217
|
+
return platform2;
|
|
218
|
+
}
|
|
219
|
+
const platformName = detectDeploymentPlatform() || detectDevelopmentPlatform();
|
|
220
|
+
const platform = platformRegistry.get(platformName);
|
|
221
|
+
if (!platform) {
|
|
222
|
+
throw new Error(
|
|
223
|
+
`Detected platform '${platformName}' not registered. Please register it manually using platformRegistry.register().`
|
|
224
|
+
);
|
|
225
|
+
}
|
|
226
|
+
return platform;
|
|
227
|
+
}
|
|
38
228
|
export {
|
|
229
|
+
ActivateEvent,
|
|
39
230
|
BasePlatform,
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
231
|
+
CustomBucketStorage,
|
|
232
|
+
ExtendableEvent,
|
|
233
|
+
FetchEvent,
|
|
234
|
+
InstallEvent,
|
|
235
|
+
RequestCookieStore,
|
|
236
|
+
ServiceWorkerPool,
|
|
237
|
+
ShovelGlobalScope,
|
|
238
|
+
ShovelServiceWorkerRegistration,
|
|
239
|
+
SingleThreadedRuntime,
|
|
240
|
+
createBucketFactory,
|
|
241
|
+
createCacheFactory,
|
|
45
242
|
createPlatform,
|
|
46
|
-
|
|
47
|
-
createServiceWorkerGlobals,
|
|
243
|
+
detectDeploymentPlatform,
|
|
48
244
|
detectDevelopmentPlatform,
|
|
49
|
-
detectPlatform,
|
|
50
|
-
detectPlatforms,
|
|
51
245
|
detectRuntime,
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
getBucket,
|
|
55
|
-
getDirectoryHandle,
|
|
56
|
-
getFileSystemRoot,
|
|
246
|
+
getBucketConfig,
|
|
247
|
+
getCacheConfig,
|
|
57
248
|
getPlatform,
|
|
58
249
|
getPlatformAsync,
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
250
|
+
loadConfig,
|
|
251
|
+
matchPattern,
|
|
252
|
+
parseConfigExpr,
|
|
253
|
+
parseCookieHeader,
|
|
254
|
+
parseSetCookieHeader,
|
|
63
255
|
platformRegistry,
|
|
256
|
+
processConfigValue,
|
|
64
257
|
resolvePlatform,
|
|
65
|
-
|
|
258
|
+
serializeCookie
|
|
66
259
|
};
|