@vite-hub/runtime 0.1.0 → 0.3.1
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 +151 -117
- package/dist/durability.d.ts +107 -0
- package/dist/durability.js +168 -0
- package/dist/error-diagnostics-BKICpwZp.js +85 -0
- package/dist/error-diagnostics-BsJr9VdW.d.ts +15 -0
- package/dist/{errors-CbqHQTvn.js → errors-BEomN3fX.js} +1 -1
- package/dist/{execution-budget-D8SwJE8_.d.ts → execution-budget-BPXbISEt.d.ts} +9 -6
- package/dist/index-Bcl-Hr-a.d.ts +678 -0
- package/dist/index.d.ts +5 -4
- package/dist/index.js +5 -4
- package/dist/internal/drain.d.ts +17 -0
- package/dist/internal/drain.js +75 -0
- package/dist/internal/execution-budget.d.ts +1 -1
- package/dist/internal/grant.js +2 -2
- package/dist/internal/h3.d.ts +42 -0
- package/dist/internal/h3.js +181 -0
- package/dist/{node.d.ts → internal/node.d.ts} +2 -2
- package/dist/{node.js → internal/node.js} +4 -4
- package/dist/internal/public-url.d.ts +2 -0
- package/dist/internal/public-url.js +2 -0
- package/dist/internal/run-scope.d.ts +11 -0
- package/dist/internal/run-scope.js +30 -0
- package/dist/internal/runtime-type.d.ts +9 -2
- package/dist/internal/runtime-type.js +3 -2
- package/dist/{shutdown.d.ts → internal/shutdown.d.ts} +1 -1
- package/dist/{shutdown.js → internal/shutdown.js} +1 -1
- package/dist/internal/test-mode-setup.d.ts +7 -0
- package/dist/internal/test-mode-setup.js +114 -0
- package/dist/internal/test-mode.d.ts +32 -0
- package/dist/internal/test-mode.js +9 -0
- package/dist/operations.d.ts +167 -0
- package/dist/operations.js +406 -0
- package/dist/public-url-1WeOw54Z.d.ts +34 -0
- package/dist/public-url-B6x0HMgY.js +140 -0
- package/dist/{src-jy-gyPPu.js → request-context-CP9NhgLL.js} +635 -551
- package/dist/runtime-type-BftydMAf.js +88 -0
- package/package.json +61 -26
- package/dist/drain.d.ts +0 -1
- package/dist/drain.js +0 -69
- package/dist/error-diagnostics-Bkym4e6y.d.ts +0 -12
- package/dist/h3.d.ts +0 -49
- package/dist/h3.js +0 -404
- package/dist/index-Rl4DbP8B.d.ts +0 -574
- package/dist/runtime-type-CgfU6SWl.js +0 -52
package/dist/h3.js
DELETED
|
@@ -1,404 +0,0 @@
|
|
|
1
|
-
import { i as runtimeErrorDiagnostics } from "./runtime-type-CgfU6SWl.js";
|
|
2
|
-
import { n as createRuntimeContext } from "./src-jy-gyPPu.js";
|
|
3
|
-
import { AsyncLocalStorage } from "node:async_hooks";
|
|
4
|
-
//#region ../internal/dist/runtime/cloudflare-env.js
|
|
5
|
-
let activeEnv;
|
|
6
|
-
const activeEnvStorageKey = Symbol.for("vitehub.cloudflare.env-storage");
|
|
7
|
-
const runtimeGlobal = globalThis;
|
|
8
|
-
const activeEnvStorage = runtimeGlobal[activeEnvStorageKey] ||= new AsyncLocalStorage();
|
|
9
|
-
function getActiveCloudflareEnv() {
|
|
10
|
-
const scoped = activeEnvStorage.getStore();
|
|
11
|
-
if (scoped) return scoped.env;
|
|
12
|
-
const host = globalThis;
|
|
13
|
-
return Object.hasOwn(host, "__env__") && host.__env__ !== void 0 ? host.__env__ : activeEnv;
|
|
14
|
-
}
|
|
15
|
-
function ownValue(value, key) {
|
|
16
|
-
if (value === null || typeof value !== "object" && typeof value !== "function" || !Object.hasOwn(value, key)) return void 0;
|
|
17
|
-
return Reflect.get(value, key);
|
|
18
|
-
}
|
|
19
|
-
function ownObject(value) {
|
|
20
|
-
return value !== null && (typeof value === "object" || typeof value === "function") ? value : void 0;
|
|
21
|
-
}
|
|
22
|
-
/** The objects where H3 1, H3 2, and Nitro hosts put Cloudflare `env` and `context`, in lookup order. */
|
|
23
|
-
function cloudflareOwners(target) {
|
|
24
|
-
const context = ownObject(ownValue(target, "context"));
|
|
25
|
-
const platform = ownObject(ownValue(context, "_platform"));
|
|
26
|
-
const req = ownObject(ownValue(target, "req"));
|
|
27
|
-
const nodeReq = ownObject(ownValue(ownObject(ownValue(target, "node")), "req"));
|
|
28
|
-
return [
|
|
29
|
-
ownObject(ownValue(context, "cloudflare")),
|
|
30
|
-
ownObject(ownValue(platform, "cloudflare")),
|
|
31
|
-
ownObject(ownValue(ownObject(ownValue(req, "runtime")), "cloudflare")),
|
|
32
|
-
ownObject(ownValue(ownObject(ownValue(nodeReq, "runtime")), "cloudflare"))
|
|
33
|
-
];
|
|
34
|
-
}
|
|
35
|
-
function getCloudflareEnv(event, options = {}) {
|
|
36
|
-
const target = ownObject(event);
|
|
37
|
-
for (const candidate of [ownValue(target, "env"), ...cloudflareOwners(target).map((owner) => ownValue(owner, "env"))]) if (candidate) return candidate;
|
|
38
|
-
return options.fallback === false ? void 0 : getActiveCloudflareEnv();
|
|
39
|
-
}
|
|
40
|
-
/** Return the Cloudflare Worker execution context of a request event, if the host provides one. */
|
|
41
|
-
function getCloudflareExecutionContext(event) {
|
|
42
|
-
for (const owner of cloudflareOwners(ownObject(event))) {
|
|
43
|
-
const context = ownObject(ownValue(owner, "context"));
|
|
44
|
-
if (context) return context;
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
function resolveWaitUntil(event, options = {}) {
|
|
48
|
-
const target = ownObject(event);
|
|
49
|
-
const context = ownObject(ownValue(target, "context"));
|
|
50
|
-
const cloudflare = ownObject(ownValue(context, "cloudflare"));
|
|
51
|
-
const cloudflareContext = ownObject(ownValue(cloudflare, "context"));
|
|
52
|
-
const platformCloudflare = ownObject(ownValue(ownObject(ownValue(context, "_platform")), "cloudflare"));
|
|
53
|
-
const platformCloudflareContext = ownObject(ownValue(platformCloudflare, "context"));
|
|
54
|
-
const req = ownObject(ownValue(target, "req"));
|
|
55
|
-
const reqCloudflare = ownObject(ownValue(ownObject(ownValue(req, "runtime")), "cloudflare"));
|
|
56
|
-
const reqCloudflareContext = ownObject(ownValue(reqCloudflare, "context"));
|
|
57
|
-
const nodeReq = ownObject(ownValue(ownObject(ownValue(target, "node")), "req"));
|
|
58
|
-
const nodeCloudflare = ownObject(ownValue(ownObject(ownValue(nodeReq, "runtime")), "cloudflare"));
|
|
59
|
-
const hosts = [
|
|
60
|
-
context,
|
|
61
|
-
cloudflare,
|
|
62
|
-
cloudflareContext,
|
|
63
|
-
platformCloudflare,
|
|
64
|
-
platformCloudflareContext,
|
|
65
|
-
req,
|
|
66
|
-
reqCloudflare,
|
|
67
|
-
reqCloudflareContext,
|
|
68
|
-
nodeReq,
|
|
69
|
-
nodeCloudflare,
|
|
70
|
-
ownObject(ownValue(nodeCloudflare, "context"))
|
|
71
|
-
];
|
|
72
|
-
const owners = options.preferHost ? [...hosts, target] : [target, ...hosts];
|
|
73
|
-
for (const owner of owners) {
|
|
74
|
-
const waitUntil = ownValue(owner, "waitUntil");
|
|
75
|
-
if (typeof waitUntil === "function") return waitUntil.bind(owner);
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
//#endregion
|
|
79
|
-
//#region ../internal/dist/runtime/h3-v1-request.js
|
|
80
|
-
const payloadMethods = /* @__PURE__ */ new Set([
|
|
81
|
-
"DELETE",
|
|
82
|
-
"PATCH",
|
|
83
|
-
"POST",
|
|
84
|
-
"PUT"
|
|
85
|
-
]);
|
|
86
|
-
const rawBodySymbol = Symbol.for("h3RawBody");
|
|
87
|
-
const nodeEventRequests = /* @__PURE__ */ new WeakMap();
|
|
88
|
-
/** Return true for an event with an own `node.req` object, such as an H3 1 event. */
|
|
89
|
-
function isH3V1Event(input) {
|
|
90
|
-
if (!Object.hasOwn(input, "node")) return false;
|
|
91
|
-
const node = Reflect.get(input, "node");
|
|
92
|
-
return isObjectLike$1(node) && isObjectLike$1(Reflect.get(node, "req"));
|
|
93
|
-
}
|
|
94
|
-
function isObjectLike$1(value) {
|
|
95
|
-
return value !== null && (typeof value === "object" || typeof value === "function");
|
|
96
|
-
}
|
|
97
|
-
function isWebRequest(value) {
|
|
98
|
-
if (!isObjectLike$1(value)) return false;
|
|
99
|
-
const headers = Reflect.get(value, "headers");
|
|
100
|
-
return typeof Reflect.get(value, "url") === "string" && typeof Reflect.get(value, "method") === "string" && isObjectLike$1(headers) && typeof Reflect.get(headers, "get") === "function";
|
|
101
|
-
}
|
|
102
|
-
function firstHeader(value) {
|
|
103
|
-
return Array.isArray(value) ? value[0] : value;
|
|
104
|
-
}
|
|
105
|
-
function readNodeRequestHeaders(request) {
|
|
106
|
-
const headers = new Headers();
|
|
107
|
-
for (const [name, value] of Object.entries(request.headers)) if (Array.isArray(value)) for (const item of value) headers.append(name, item);
|
|
108
|
-
else if (value !== void 0) headers.set(name, value);
|
|
109
|
-
return headers;
|
|
110
|
-
}
|
|
111
|
-
/** Rebuild the public URL. H3 1 removes the app base path from `url` and keeps it in `originalUrl`. */
|
|
112
|
-
function readNodeRequestURL(request) {
|
|
113
|
-
const host = firstHeader(request.headers.host) || "localhost";
|
|
114
|
-
const encrypted = request.socket?.encrypted || request.connection?.encrypted;
|
|
115
|
-
const protocol = firstHeader(request.headers["x-forwarded-proto"]) === "https" || encrypted ? "https" : "http";
|
|
116
|
-
const path = (request.originalUrl || request.url || "/").replace(/^[/\\]+/, "/");
|
|
117
|
-
return new URL(path, `${protocol}://${host}`);
|
|
118
|
-
}
|
|
119
|
-
function toBodyStream(value) {
|
|
120
|
-
if (value instanceof ReadableStream) return value;
|
|
121
|
-
if (value instanceof Uint8Array) return new ReadableStream({ start(controller) {
|
|
122
|
-
controller.enqueue(value);
|
|
123
|
-
controller.close();
|
|
124
|
-
} });
|
|
125
|
-
if (typeof value === "string") return new Blob([value]).stream();
|
|
126
|
-
if (value instanceof URLSearchParams) return new Blob([value.toString()]).stream();
|
|
127
|
-
if (value instanceof Object && Object.getPrototypeOf(value) === Object.prototype) return new Blob([JSON.stringify(value)]).stream();
|
|
128
|
-
}
|
|
129
|
-
async function openNodeRequestBody(event) {
|
|
130
|
-
const request = event.node.req;
|
|
131
|
-
const stored = event._requestBody ?? request[rawBodySymbol] ?? request.rawBody ?? request.body;
|
|
132
|
-
if (stored !== void 0 && stored !== null) {
|
|
133
|
-
const stream = toBodyStream(await stored);
|
|
134
|
-
if (stream) {
|
|
135
|
-
const reader = stream.getReader();
|
|
136
|
-
return () => reader.read();
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
const iterator = request[Symbol.asyncIterator]?.();
|
|
140
|
-
if (!iterator) return void 0;
|
|
141
|
-
const chunks = [];
|
|
142
|
-
const collected = [];
|
|
143
|
-
let total = 0;
|
|
144
|
-
let ended = false;
|
|
145
|
-
let failed = false;
|
|
146
|
-
let failure;
|
|
147
|
-
let waiting;
|
|
148
|
-
const produce = async () => {
|
|
149
|
-
try {
|
|
150
|
-
for await (const chunk of { [Symbol.asyncIterator]: () => iterator }) {
|
|
151
|
-
let bytes;
|
|
152
|
-
if (typeof chunk === "string") bytes = new TextEncoder().encode(chunk);
|
|
153
|
-
else if (chunk instanceof Uint8Array) bytes = chunk;
|
|
154
|
-
else throw new TypeError("[vitehub] Request body chunks must be strings or bytes.");
|
|
155
|
-
collected.push(bytes);
|
|
156
|
-
total += bytes.byteLength;
|
|
157
|
-
if (waiting) {
|
|
158
|
-
waiting.resolve({ value: bytes });
|
|
159
|
-
waiting = void 0;
|
|
160
|
-
} else chunks.push(bytes);
|
|
161
|
-
}
|
|
162
|
-
ended = true;
|
|
163
|
-
waiting?.resolve({ done: true });
|
|
164
|
-
waiting = void 0;
|
|
165
|
-
} catch (error) {
|
|
166
|
-
failed = true;
|
|
167
|
-
failure = error;
|
|
168
|
-
ended = true;
|
|
169
|
-
waiting?.reject(error);
|
|
170
|
-
waiting = void 0;
|
|
171
|
-
throw error;
|
|
172
|
-
}
|
|
173
|
-
const bytes = new Uint8Array(total);
|
|
174
|
-
let offset = 0;
|
|
175
|
-
for (const chunk of collected) {
|
|
176
|
-
bytes.set(chunk, offset);
|
|
177
|
-
offset += chunk.byteLength;
|
|
178
|
-
}
|
|
179
|
-
return bytes;
|
|
180
|
-
};
|
|
181
|
-
let body;
|
|
182
|
-
const start = () => body ??= produce();
|
|
183
|
-
event._requestBody = { then: (resolve, reject) => start().then(resolve, reject) };
|
|
184
|
-
return async () => {
|
|
185
|
-
start();
|
|
186
|
-
body?.catch(() => {});
|
|
187
|
-
if (failed) throw failure;
|
|
188
|
-
if (chunks.length) return { value: chunks.shift() };
|
|
189
|
-
if (ended) return { done: true };
|
|
190
|
-
return new Promise((resolve, reject) => {
|
|
191
|
-
waiting = {
|
|
192
|
-
resolve,
|
|
193
|
-
reject
|
|
194
|
-
};
|
|
195
|
-
});
|
|
196
|
-
};
|
|
197
|
-
}
|
|
198
|
-
/** Read the body lazily, from a value a host already read or from the Node request stream. */
|
|
199
|
-
function readNodeRequestBody(event) {
|
|
200
|
-
let reader;
|
|
201
|
-
return new ReadableStream({ async pull(controller) {
|
|
202
|
-
const chunk = await (await (reader ??= openNodeRequestBody(event)))?.();
|
|
203
|
-
if (!chunk || chunk.done) {
|
|
204
|
-
controller.close();
|
|
205
|
-
return;
|
|
206
|
-
}
|
|
207
|
-
const value = chunk.value;
|
|
208
|
-
if (typeof value === "string") controller.enqueue(new TextEncoder().encode(value));
|
|
209
|
-
else if (value instanceof Uint8Array) controller.enqueue(value);
|
|
210
|
-
else controller.error(/* @__PURE__ */ new TypeError("[vitehub] Request body chunks must be strings or bytes."));
|
|
211
|
-
} }, { highWaterMark: 0 });
|
|
212
|
-
}
|
|
213
|
-
/** Return one Web Request per H3 1 event. The body is read only when a caller reads the Request body. */
|
|
214
|
-
function toH3V1Request(event) {
|
|
215
|
-
const cached = nodeEventRequests.get(event);
|
|
216
|
-
if (cached) return cached;
|
|
217
|
-
if (isWebRequest(event.web?.request)) return event.web.request;
|
|
218
|
-
const request = event.node.req;
|
|
219
|
-
const method = (request.method || "GET").toUpperCase();
|
|
220
|
-
const init = {
|
|
221
|
-
headers: readNodeRequestHeaders(request),
|
|
222
|
-
method
|
|
223
|
-
};
|
|
224
|
-
if (payloadMethods.has(method)) {
|
|
225
|
-
init.body = readNodeRequestBody(event);
|
|
226
|
-
init.duplex = "half";
|
|
227
|
-
}
|
|
228
|
-
const webRequest = new Request(readNodeRequestURL(request), init);
|
|
229
|
-
nodeEventRequests.set(event, webRequest);
|
|
230
|
-
return webRequest;
|
|
231
|
-
}
|
|
232
|
-
function headerValues(value) {
|
|
233
|
-
if (value === void 0) return [];
|
|
234
|
-
return Array.isArray(value) ? value : [String(value)];
|
|
235
|
-
}
|
|
236
|
-
/**
|
|
237
|
-
* Return a `Headers` view of a Node response. Writes go to the Node response, so H3 1 sends them.
|
|
238
|
-
* Reads return what the Node response holds now.
|
|
239
|
-
*/
|
|
240
|
-
function createH3V1ResponseHeaders(response) {
|
|
241
|
-
const snapshot = () => {
|
|
242
|
-
const headers = new Headers();
|
|
243
|
-
for (const [name, value] of Object.entries(response.getHeaders())) for (const item of headerValues(value)) headers.append(name, item);
|
|
244
|
-
return headers;
|
|
245
|
-
};
|
|
246
|
-
const view = new Headers();
|
|
247
|
-
return Object.assign(view, {
|
|
248
|
-
append(name, value) {
|
|
249
|
-
const current = headerValues(response.getHeader(name));
|
|
250
|
-
response.setHeader(name, current.length ? [...current, value] : value);
|
|
251
|
-
},
|
|
252
|
-
delete(name) {
|
|
253
|
-
response.removeHeader(name);
|
|
254
|
-
},
|
|
255
|
-
entries: () => snapshot().entries(),
|
|
256
|
-
forEach(callback, thisArg) {
|
|
257
|
-
snapshot().forEach((value, key) => callback.call(thisArg, value, key, view));
|
|
258
|
-
},
|
|
259
|
-
get: (name) => snapshot().get(name),
|
|
260
|
-
getSetCookie: () => headerValues(response.getHeader("set-cookie")),
|
|
261
|
-
has: (name) => response.getHeader(name) !== void 0,
|
|
262
|
-
keys: () => snapshot().keys(),
|
|
263
|
-
set(name, value) {
|
|
264
|
-
response.setHeader(name, value);
|
|
265
|
-
},
|
|
266
|
-
values: () => snapshot().values(),
|
|
267
|
-
[Symbol.iterator]: () => snapshot()[Symbol.iterator]()
|
|
268
|
-
});
|
|
269
|
-
}
|
|
270
|
-
//#endregion
|
|
271
|
-
//#region src/h3.ts
|
|
272
|
-
function detectRuntime(cloudflare) {
|
|
273
|
-
if (cloudflare) return "cloudflare-agents";
|
|
274
|
-
if ("Deno" in globalThis) return "deno";
|
|
275
|
-
if (typeof process !== "undefined") {
|
|
276
|
-
if (process.env.VERCEL) return "vercel";
|
|
277
|
-
if (process.env.NODE_ENV === "development") return "vite";
|
|
278
|
-
}
|
|
279
|
-
return "unknown";
|
|
280
|
-
}
|
|
281
|
-
/**
|
|
282
|
-
* Return true for the Request of an H3 2 event. Hosts pass their own Request classes, such as srvx's Node
|
|
283
|
-
* request or a Request from another realm, so check the fields that ViteHub reads instead of the prototype.
|
|
284
|
-
*/
|
|
285
|
-
function isHostRequest(value) {
|
|
286
|
-
if (!isObjectLike(value)) return false;
|
|
287
|
-
const headers = Reflect.get(value, "headers");
|
|
288
|
-
return typeof Reflect.get(value, "url") === "string" && typeof Reflect.get(value, "method") === "string" && isObjectLike(headers) && typeof Reflect.get(headers, "get") === "function";
|
|
289
|
-
}
|
|
290
|
-
function isObjectLike(value) {
|
|
291
|
-
return value !== null && (typeof value === "object" || typeof value === "function");
|
|
292
|
-
}
|
|
293
|
-
/** Map Standard Context response fields to the response that H3 sends. H3 sends the value the handler returns as the body. */
|
|
294
|
-
function preparedResponse(target) {
|
|
295
|
-
return {
|
|
296
|
-
get body() {},
|
|
297
|
-
set body(_value) {
|
|
298
|
-
throw runtimeErrorDiagnostics.RUNTIME_R0016({ message: "[vitehub] H3 does not read `~standard.res.body`. Return the body or a Response from the handler." });
|
|
299
|
-
},
|
|
300
|
-
get headers() {
|
|
301
|
-
return target.headers;
|
|
302
|
-
},
|
|
303
|
-
set headers(value) {
|
|
304
|
-
for (const name of Array.from(target.headers.keys())) target.headers.delete(name);
|
|
305
|
-
for (const [name, item] of value ?? []) target.headers.append(name, item);
|
|
306
|
-
},
|
|
307
|
-
get status() {
|
|
308
|
-
return target.status;
|
|
309
|
-
},
|
|
310
|
-
set status(value) {
|
|
311
|
-
target.status = value;
|
|
312
|
-
}
|
|
313
|
-
};
|
|
314
|
-
}
|
|
315
|
-
function h3V2ClientAddress(request) {
|
|
316
|
-
const ip = Reflect.get(request, "ip");
|
|
317
|
-
return typeof ip === "string" && ip ? ip : void 0;
|
|
318
|
-
}
|
|
319
|
-
function createH3StandardProps(event) {
|
|
320
|
-
let readRequest;
|
|
321
|
-
let res;
|
|
322
|
-
let clientAddress;
|
|
323
|
-
const req = Object.hasOwn(event, "req") ? Reflect.get(event, "req") : void 0;
|
|
324
|
-
if (isHostRequest(req)) {
|
|
325
|
-
const response = Reflect.get(event, "res");
|
|
326
|
-
const headers = isObjectLike(response) ? Reflect.get(response, "headers") : void 0;
|
|
327
|
-
if (!isObjectLike(headers) || typeof Reflect.get(headers, "set") !== "function") return void 0;
|
|
328
|
-
res = preparedResponse(response);
|
|
329
|
-
readRequest = () => req;
|
|
330
|
-
clientAddress = h3V2ClientAddress(req);
|
|
331
|
-
} else if (isH3V1Event(event)) {
|
|
332
|
-
const response = event.node.res;
|
|
333
|
-
if (!response) return void 0;
|
|
334
|
-
res = preparedResponse({
|
|
335
|
-
headers: createH3V1ResponseHeaders(response),
|
|
336
|
-
get status() {
|
|
337
|
-
return response.statusCode;
|
|
338
|
-
},
|
|
339
|
-
set status(value) {
|
|
340
|
-
if (value !== void 0) response.statusCode = value;
|
|
341
|
-
}
|
|
342
|
-
});
|
|
343
|
-
readRequest = () => toH3V1Request(event);
|
|
344
|
-
clientAddress = event.node.req.socket?.remoteAddress || event.node.req.connection?.remoteAddress || void 0;
|
|
345
|
-
} else return;
|
|
346
|
-
const env = getCloudflareEnv(event, { fallback: false });
|
|
347
|
-
const context = getCloudflareExecutionContext(event);
|
|
348
|
-
const waitUntil = resolveWaitUntil(event, { preferHost: true });
|
|
349
|
-
const runtime = {
|
|
350
|
-
...env ? { cloudflare: {
|
|
351
|
-
env,
|
|
352
|
-
...context ? { context } : {}
|
|
353
|
-
} } : {},
|
|
354
|
-
...clientAddress ? { clientAddress } : {}
|
|
355
|
-
};
|
|
356
|
-
return {
|
|
357
|
-
vendor: "h3",
|
|
358
|
-
get req() {
|
|
359
|
-
return readRequest();
|
|
360
|
-
},
|
|
361
|
-
res,
|
|
362
|
-
runtime,
|
|
363
|
-
...waitUntil ? { waitUntil } : {}
|
|
364
|
-
};
|
|
365
|
-
}
|
|
366
|
-
/**
|
|
367
|
-
* Set `event["~standard"]` on an H3 1 or H3 2 event. A value that the host set stays.
|
|
368
|
-
* ViteHub calls this for each request in a Nitro `request` hook. Call it yourself only for an event that
|
|
369
|
-
* Nitro did not create, such as in a test. Throws `RUNTIME_R0015` when the event has no request.
|
|
370
|
-
*/
|
|
371
|
-
function installStandardContext(event) {
|
|
372
|
-
if ("~standard" in event) return;
|
|
373
|
-
const props = createH3StandardProps(event);
|
|
374
|
-
if (!props) throw runtimeErrorDiagnostics.RUNTIME_R0015({ message: "[vitehub] The event has no request, so ViteHub cannot set `~standard`. Pass the event of an H3 or Nitro route." });
|
|
375
|
-
Object.defineProperty(event, "~standard", {
|
|
376
|
-
configurable: true,
|
|
377
|
-
enumerable: false,
|
|
378
|
-
value: props
|
|
379
|
-
});
|
|
380
|
-
}
|
|
381
|
-
function getRuntimeContext(event, options = {}) {
|
|
382
|
-
if (!event["~standard"]) try {
|
|
383
|
-
installStandardContext(event);
|
|
384
|
-
} catch (error) {
|
|
385
|
-
if (options.request === void 0 || error?.name !== "RUNTIME_R0015") throw error;
|
|
386
|
-
}
|
|
387
|
-
const standard = event["~standard"];
|
|
388
|
-
const request = options.request ?? standard?.req;
|
|
389
|
-
if (!request) throw runtimeErrorDiagnostics.RUNTIME_R0015({ message: "[vitehub] The event has no request, so ViteHub cannot create a runtime context." });
|
|
390
|
-
const waitUntil = options.waitUntil?.bind(options) ?? options.vercel?.waitUntil?.bind(options.vercel) ?? standard?.waitUntil;
|
|
391
|
-
const cloudflare = options.cloudflare ?? standard?.runtime?.cloudflare;
|
|
392
|
-
const runtime = options.runtime ?? detectRuntime(!!cloudflare);
|
|
393
|
-
return createRuntimeContext({
|
|
394
|
-
request,
|
|
395
|
-
...options,
|
|
396
|
-
...cloudflare ? { cloudflare } : {},
|
|
397
|
-
event,
|
|
398
|
-
runtime,
|
|
399
|
-
...runtime === "vercel" && waitUntil ? { vercel: { waitUntil } } : {},
|
|
400
|
-
...waitUntil ? { waitUntil } : {}
|
|
401
|
-
});
|
|
402
|
-
}
|
|
403
|
-
//#endregion
|
|
404
|
-
export { getRuntimeContext, installStandardContext };
|