@sproutboat/runtime 0.7.1 → 0.9.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/CHANGELOG.md +36 -0
- package/README.md +41 -0
- package/package.json +1 -1
- package/src/native-fetch-prelude.js +58 -0
- package/src/r2-object.test.js +28 -2
- package/src/transport-broker.js +21 -0
- package/src/transport-embedded.js +813 -54
- package/src/wrap.ts +27 -6
package/src/wrap.ts
CHANGED
|
@@ -24,7 +24,10 @@ export const preludePath = new URL("./native-fetch-prelude.js", import.meta.url)
|
|
|
24
24
|
*/
|
|
25
25
|
export type Transport = "broker" | "embedded";
|
|
26
26
|
export const transportPath = (transport: Transport): URL =>
|
|
27
|
-
new URL(
|
|
27
|
+
new URL(
|
|
28
|
+
transport === "embedded" ? "./transport-embedded.js" : "./transport-broker.js",
|
|
29
|
+
import.meta.url,
|
|
30
|
+
);
|
|
28
31
|
|
|
29
32
|
/** Where the prelude expects its transport spliced in. */
|
|
30
33
|
export const TRANSPORT_MARKER =
|
|
@@ -188,24 +191,37 @@ export function wrapNativeFetchHandler(
|
|
|
188
191
|
// #15 — and enforces the outbound allowlist itself, with no broker to do it.
|
|
189
192
|
`globalThis.__sbOutbound = ${JSON.stringify(bindings.outbound)};\n` +
|
|
190
193
|
(assets ? `globalThis.__sbAssets = ${JSON.stringify(assets)};\n` : "");
|
|
191
|
-
|
|
194
|
+
// The native transfer hook runs before JavaScript receives its Request, so
|
|
195
|
+
// it needs the same default data-directory name as the embedded transport.
|
|
196
|
+
const nativeAppName = `Porffor.c\`const char* sb_standalone_app_name(void) { return ${JSON.stringify(appName)}; }\`;\n`;
|
|
197
|
+
const wire = hasBindings(bindings)
|
|
198
|
+
? `__sbInstallBindings(env, ${JSON.stringify(bindings)});\n`
|
|
199
|
+
: "";
|
|
192
200
|
const registerDO = bindings.do.length
|
|
193
201
|
? `__sbRegisterDO({ ${bindings.do.map((d) => `${d.className}: ${d.className}`).join(", ")} });\n`
|
|
194
202
|
: "";
|
|
195
203
|
// Cron / queue / alarm timers, for a transport that has no broker to deliver
|
|
196
204
|
// them. The broker transport defines this as a no-op, so the emitted module
|
|
197
205
|
// is the same either way.
|
|
198
|
-
const triggers = hasBindings(bindings)
|
|
206
|
+
const triggers = hasBindings(bindings)
|
|
207
|
+
? `__sbStartLocalTriggers(__sbHandlers, ${JSON.stringify(bindings)});\n`
|
|
208
|
+
: "";
|
|
199
209
|
|
|
200
210
|
return (
|
|
201
|
-
`${prelude}\n${compat}${env}${wire}` +
|
|
211
|
+
`${prelude}\n${nativeAppName}${compat}${env}${wire}` +
|
|
202
212
|
`${neutralised}\n` +
|
|
203
213
|
`${registerDO}${triggers}` +
|
|
204
214
|
`export default {\n port: ${port},\n fetch(request) { return __sbEntry(__sbHandlers, request); }\n};\n`
|
|
205
215
|
);
|
|
206
216
|
}
|
|
207
217
|
|
|
208
|
-
type VarsJson =
|
|
218
|
+
type VarsJson =
|
|
219
|
+
| string
|
|
220
|
+
| number
|
|
221
|
+
| boolean
|
|
222
|
+
| null
|
|
223
|
+
| { readonly [key: string]: VarsJson }
|
|
224
|
+
| VarsJson[];
|
|
209
225
|
function isVarsObject(value: VarsJson): value is { readonly [key: string]: VarsJson } {
|
|
210
226
|
return value !== null && Object(value) === value && !Array.isArray(value);
|
|
211
227
|
}
|
|
@@ -260,7 +276,12 @@ export function readBindingsFromEnv(): Bindings {
|
|
|
260
276
|
const ratelimiters: Array<{ binding: string; limit: number; period: number }> = [];
|
|
261
277
|
if (Array.isArray(parsed.ratelimiters)) {
|
|
262
278
|
for (const entry of parsed.ratelimiters) {
|
|
263
|
-
if (
|
|
279
|
+
if (
|
|
280
|
+
isVarsObject(entry) &&
|
|
281
|
+
isVarsString(entry.binding) &&
|
|
282
|
+
isCount(entry.limit) &&
|
|
283
|
+
isCount(entry.period)
|
|
284
|
+
) {
|
|
264
285
|
ratelimiters.push({ binding: entry.binding, limit: entry.limit, period: entry.period });
|
|
265
286
|
}
|
|
266
287
|
}
|