@vite-hub/rate-limit 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 +18 -14
- package/dist/cli-szm60zN5.d.ts +163 -0
- package/dist/cli.d.ts +1 -1
- package/dist/cli.js +12 -26
- package/dist/{cloudflare-DUDRACVB.js → cloudflare-otBvOPv4.js} +5 -4
- package/dist/{counters-pKORpgGd.d.ts → counters-DXpF67J_.d.ts} +3 -3
- package/dist/{counters-wdEMxDEY.js → counters-DgVdHwdL.js} +13 -6
- package/dist/dev-peer-CHafxbtj.js +238 -0
- package/dist/drivers/cloudflare.d.ts +1 -1
- package/dist/drivers/cloudflare.js +1 -1
- package/dist/drivers/kv.d.ts +2 -0
- package/dist/drivers/kv.js +92 -0
- package/dist/drivers/memory.d.ts +1 -1
- package/dist/drivers/memory.js +5 -5
- package/dist/durability.d.ts +12 -0
- package/dist/durability.js +35 -0
- package/dist/error-diagnostics-MgAnhRTP.js +234 -0
- package/dist/index.d.ts +16 -7
- package/dist/index.js +108 -38
- package/dist/{inspect-D-FzyM_x.js → inspect-B6gTdgy9.js} +7 -1
- package/dist/kv-C1wpLulE.d.ts +29 -0
- package/dist/{limiter-DBrMI4vi.js → kv-provider-C1S6dRTR.js} +16 -9
- package/dist/{operations-tNUdj3G4.d.ts → operation-definition-8gOe-ubD.d.ts} +37 -15
- package/dist/operations-BYRa7wpx.js +1355 -0
- package/dist/{operations-Bu-xulNd.js → operations-CLI9w3rO.js} +9 -14
- package/dist/operations.d.ts +9 -4
- package/dist/operations.js +51 -23
- package/dist/runtime.d.ts +21 -3
- package/dist/runtime.js +2 -2
- package/dist/state-BU-zcLNY.js +89 -0
- package/dist/{types-CQEJpgtz.d.ts → types-CMyR1JeW.d.ts} +43 -23
- package/dist/vite.d.ts +131 -21
- package/dist/vite.js +349 -2479
- package/package.json +21 -17
- package/dist/cli-Cniq99iX.d.ts +0 -85
- package/dist/error-diagnostics-CTAC_bbr.js +0 -56
- package/dist/h3-v1-request-qoGwJr5-.js +0 -235
- package/dist/operations-B09NnEjT.js +0 -1438
- package/dist/state-Ct7zR8fD.js +0 -43
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vite-hub/rate-limit",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"description": "Provider-neutral Rate Limit primitives and Vite deployment integration for ViteHub.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cloudflare",
|
|
@@ -33,25 +33,25 @@
|
|
|
33
33
|
"import": "./dist/index.js",
|
|
34
34
|
"default": "./dist/index.js"
|
|
35
35
|
},
|
|
36
|
-
"./cli": {
|
|
37
|
-
"types": "./dist/cli.d.ts",
|
|
38
|
-
"import": "./dist/cli.js",
|
|
39
|
-
"default": "./dist/cli.js"
|
|
40
|
-
},
|
|
41
36
|
"./drivers/cloudflare": {
|
|
42
37
|
"types": "./dist/drivers/cloudflare.d.ts",
|
|
43
38
|
"import": "./dist/drivers/cloudflare.js",
|
|
44
39
|
"default": "./dist/drivers/cloudflare.js"
|
|
45
40
|
},
|
|
41
|
+
"./drivers/kv": {
|
|
42
|
+
"types": "./dist/drivers/kv.d.ts",
|
|
43
|
+
"import": "./dist/drivers/kv.js",
|
|
44
|
+
"default": "./dist/drivers/kv.js"
|
|
45
|
+
},
|
|
46
46
|
"./drivers/memory": {
|
|
47
47
|
"types": "./dist/drivers/memory.d.ts",
|
|
48
48
|
"import": "./dist/drivers/memory.js",
|
|
49
49
|
"default": "./dist/drivers/memory.js"
|
|
50
50
|
},
|
|
51
|
-
"./
|
|
52
|
-
"types": "./dist/
|
|
53
|
-
"import": "./dist/
|
|
54
|
-
"default": "./dist/
|
|
51
|
+
"./durability": {
|
|
52
|
+
"types": "./dist/durability.d.ts",
|
|
53
|
+
"import": "./dist/durability.js",
|
|
54
|
+
"default": "./dist/durability.js"
|
|
55
55
|
},
|
|
56
56
|
"./runtime": {
|
|
57
57
|
"types": "./dist/runtime.d.ts",
|
|
@@ -66,21 +66,25 @@
|
|
|
66
66
|
"./package.json": "./package.json"
|
|
67
67
|
},
|
|
68
68
|
"dependencies": {
|
|
69
|
-
"
|
|
70
|
-
"
|
|
69
|
+
"@valibot/to-json-schema": "^1.8.0",
|
|
70
|
+
"@vite-hub/runtime": "0.3.1",
|
|
71
|
+
"birpc": "^4.2.0",
|
|
72
|
+
"h3": "^2.0.8",
|
|
73
|
+
"nostics": "1.4.0",
|
|
71
74
|
"valibot": "^1.5.0"
|
|
72
75
|
},
|
|
73
76
|
"devDependencies": {
|
|
74
77
|
"@types/node": "^24.19.1",
|
|
75
|
-
"
|
|
78
|
+
"@vite-hub/internal": "0.0.1",
|
|
79
|
+
"nitro": "^3.0.261103-rc",
|
|
76
80
|
"publint": "^0.3.25",
|
|
77
81
|
"typescript": "^6.0.3",
|
|
78
82
|
"vite": "^8.2.2",
|
|
79
83
|
"vite-plus": "^0.3.3",
|
|
80
|
-
"vitest": "^4.1.11"
|
|
81
|
-
"@vite-hub/internal": "0.0.1"
|
|
84
|
+
"vitest": "^4.1.11"
|
|
82
85
|
},
|
|
83
86
|
"peerDependencies": {
|
|
87
|
+
"nitro": "3.0.261102-rc || ^3.0.261103-rc",
|
|
84
88
|
"vite": ">=8.0.0"
|
|
85
89
|
},
|
|
86
90
|
"peerDependenciesMeta": {
|
|
@@ -89,12 +93,12 @@
|
|
|
89
93
|
}
|
|
90
94
|
},
|
|
91
95
|
"engines": {
|
|
92
|
-
"node": ">=
|
|
96
|
+
"node": ">=22.18.0"
|
|
93
97
|
},
|
|
94
98
|
"scripts": {
|
|
95
99
|
"build": "vp pack",
|
|
96
100
|
"dev": "vp pack --watch",
|
|
97
101
|
"typecheck": "vp run --cache -t @vite-hub/rate-limit#build && node ../../test/package-typecheck.mjs --noEmit -p ./tsconfig.json",
|
|
98
|
-
"test": "vp run -t @vite-hub/rate-limit#build && vp test"
|
|
102
|
+
"test": "vp run --cache -t @vite-hub/rate-limit#build && vp test"
|
|
99
103
|
}
|
|
100
104
|
}
|
package/dist/cli-Cniq99iX.d.ts
DELETED
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
import { Diagnostic } from "nostics";
|
|
2
|
-
//#region ../internal/src/provision.d.ts
|
|
3
|
-
type ProvisionProvider = "cloudflare" | "vercel";
|
|
4
|
-
interface ProvisionLogger {
|
|
5
|
-
log: (message: string) => void;
|
|
6
|
-
warn: (message: string) => void;
|
|
7
|
-
}
|
|
8
|
-
interface ProvisionContext {
|
|
9
|
-
env: Record<string, string | undefined>;
|
|
10
|
-
fetch: typeof globalThis.fetch;
|
|
11
|
-
logger: ProvisionLogger;
|
|
12
|
-
markPlanUnchecked?: () => void;
|
|
13
|
-
}
|
|
14
|
-
interface ProvisionState {
|
|
15
|
-
cloudflare?: Record<string, Record<string, string>>;
|
|
16
|
-
vercel?: Record<string, Record<string, string>>;
|
|
17
|
-
}
|
|
18
|
-
interface ProvisionResult {
|
|
19
|
-
ids?: ProvisionState;
|
|
20
|
-
}
|
|
21
|
-
interface ProvisionAction {
|
|
22
|
-
kind: string;
|
|
23
|
-
name: string;
|
|
24
|
-
exists: boolean;
|
|
25
|
-
pending?: boolean;
|
|
26
|
-
apply: () => Promise<ProvisionResult>;
|
|
27
|
-
}
|
|
28
|
-
interface ProvisionStep {
|
|
29
|
-
id: string;
|
|
30
|
-
provider: ProvisionProvider;
|
|
31
|
-
plan: (context: ProvisionContext) => Promise<ProvisionAction[]>;
|
|
32
|
-
}
|
|
33
|
-
//#endregion
|
|
34
|
-
//#region ../internal/src/cli.d.ts
|
|
35
|
-
interface ViteHubCliStreams {
|
|
36
|
-
stderr: {
|
|
37
|
-
write: (chunk: string | Uint8Array, encoding?: BufferEncoding | StdoutWriteCallback, callback?: StdoutWriteCallback) => unknown;
|
|
38
|
-
};
|
|
39
|
-
stdout: {
|
|
40
|
-
write: (chunk: string | Uint8Array) => unknown;
|
|
41
|
-
};
|
|
42
|
-
}
|
|
43
|
-
interface ViteHubCliSpawnResult {
|
|
44
|
-
exitCode: number | null;
|
|
45
|
-
signal?: NodeJS.Signals | null;
|
|
46
|
-
}
|
|
47
|
-
interface ViteHubCliSpawnOptions {
|
|
48
|
-
cwd?: string;
|
|
49
|
-
env?: NodeJS.ProcessEnv;
|
|
50
|
-
stderr?: "inherit" | "pipe";
|
|
51
|
-
stdout?: "inherit" | "pipe";
|
|
52
|
-
}
|
|
53
|
-
type ViteHubCliSpawn = (command: string, args: string[], options?: ViteHubCliSpawnOptions) => Promise<ViteHubCliSpawnResult>;
|
|
54
|
-
interface ViteHubCliContext extends ViteHubCliStreams {
|
|
55
|
-
cwd: string;
|
|
56
|
-
env: NodeJS.ProcessEnv;
|
|
57
|
-
/** Replaces `fetch` for commands that call a running app, so tests need no server. */
|
|
58
|
-
fetch?: typeof fetch;
|
|
59
|
-
rootDir: string;
|
|
60
|
-
spawn: ViteHubCliSpawn;
|
|
61
|
-
}
|
|
62
|
-
interface ViteHubCliFeature {
|
|
63
|
-
description?: string;
|
|
64
|
-
name: string;
|
|
65
|
-
run: (args: string[], context: ViteHubCliContext) => Promise<number | void> | number | void;
|
|
66
|
-
usage?: string;
|
|
67
|
-
}
|
|
68
|
-
interface ViteHubCliCommandNamespace {
|
|
69
|
-
/** The feature that `vitehub <name>` runs when no feature is named. */
|
|
70
|
-
defaultFeature?: string;
|
|
71
|
-
description?: string;
|
|
72
|
-
features: ViteHubCliFeature[];
|
|
73
|
-
name: string;
|
|
74
|
-
}
|
|
75
|
-
interface ViteHubCliContributor {
|
|
76
|
-
namespaces: ViteHubCliCommandNamespace[];
|
|
77
|
-
provision?: ProvisionStep[];
|
|
78
|
-
}
|
|
79
|
-
type ViteHubCliContributorFactory = () => ViteHubCliContributor | undefined | Promise<ViteHubCliContributor | undefined>;
|
|
80
|
-
interface ViteHubCliPluginMetadata {
|
|
81
|
-
cli?: ViteHubCliContributor | ViteHubCliContributorFactory;
|
|
82
|
-
}
|
|
83
|
-
type StdoutWriteCallback = (error?: Error | null) => void;
|
|
84
|
-
//#endregion
|
|
85
|
-
export { ViteHubCliStreams as i, ViteHubCliContributor as n, ViteHubCliPluginMetadata as r, ViteHubCliContext as t };
|
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import { defineDiagnostics } from "nostics";
|
|
2
|
-
//#region src/error-diagnostics.ts
|
|
3
|
-
const dynamicError = { why: ({ message }) => message === void 0 ? "" : String(message) };
|
|
4
|
-
const rateLimitErrorDiagnostics = /*#__PURE__*/ defineDiagnostics({
|
|
5
|
-
docsBase: () => "https://vitehub.dev/docs/reference/diagnostics",
|
|
6
|
-
codes: {
|
|
7
|
-
RATE_LIMIT_R0001: dynamicError,
|
|
8
|
-
RATE_LIMIT_R0002: dynamicError,
|
|
9
|
-
RATE_LIMIT_R0003: dynamicError,
|
|
10
|
-
RATE_LIMIT_R0004: dynamicError,
|
|
11
|
-
RATE_LIMIT_R0005: dynamicError,
|
|
12
|
-
RATE_LIMIT_R0006: dynamicError,
|
|
13
|
-
RATE_LIMIT_R0007: dynamicError,
|
|
14
|
-
RATE_LIMIT_R0008: dynamicError,
|
|
15
|
-
RATE_LIMIT_R0009: dynamicError,
|
|
16
|
-
RATE_LIMIT_R0010: dynamicError,
|
|
17
|
-
RATE_LIMIT_R0011: dynamicError,
|
|
18
|
-
RATE_LIMIT_R0012: dynamicError,
|
|
19
|
-
RATE_LIMIT_R0013: dynamicError,
|
|
20
|
-
RATE_LIMIT_R0015: dynamicError,
|
|
21
|
-
RATE_LIMIT_R0016: dynamicError,
|
|
22
|
-
RATE_LIMIT_R0019: dynamicError,
|
|
23
|
-
RATE_LIMIT_R0020: dynamicError,
|
|
24
|
-
RATE_LIMIT_R0021: dynamicError,
|
|
25
|
-
RATE_LIMIT_R0022: dynamicError,
|
|
26
|
-
RATE_LIMIT_R0023: dynamicError,
|
|
27
|
-
RATE_LIMIT_R0024: dynamicError,
|
|
28
|
-
RATE_LIMIT_R0025: dynamicError,
|
|
29
|
-
RATE_LIMIT_R0026: dynamicError,
|
|
30
|
-
RATE_LIMIT_R0027: dynamicError,
|
|
31
|
-
RATE_LIMIT_R0028: dynamicError,
|
|
32
|
-
RATE_LIMIT_R0029: dynamicError,
|
|
33
|
-
RATE_LIMIT_R0030: dynamicError,
|
|
34
|
-
RATE_LIMIT_R0031: dynamicError,
|
|
35
|
-
RATE_LIMIT_R0032: dynamicError,
|
|
36
|
-
RATE_LIMIT_R0033: dynamicError,
|
|
37
|
-
RATE_LIMIT_R0034: dynamicError,
|
|
38
|
-
RATE_LIMIT_R0035: dynamicError,
|
|
39
|
-
RATE_LIMIT_R0036: dynamicError,
|
|
40
|
-
RATE_LIMIT_R0037: dynamicError,
|
|
41
|
-
RATE_LIMIT_R0038: dynamicError,
|
|
42
|
-
RATE_LIMIT_R0039: dynamicError,
|
|
43
|
-
RATE_LIMIT_R0040: dynamicError,
|
|
44
|
-
RATE_LIMIT_R0041: dynamicError,
|
|
45
|
-
RATE_LIMIT_R0042: dynamicError,
|
|
46
|
-
RATE_LIMIT_R0043: dynamicError,
|
|
47
|
-
RATE_LIMIT_R0044: dynamicError,
|
|
48
|
-
RATE_LIMIT_R0045: dynamicError,
|
|
49
|
-
RATE_LIMIT_B0001: dynamicError,
|
|
50
|
-
RATE_LIMIT_B0002: dynamicError,
|
|
51
|
-
RATE_LIMIT_B0003: dynamicError,
|
|
52
|
-
RATE_LIMIT_B0004: dynamicError
|
|
53
|
-
}
|
|
54
|
-
});
|
|
55
|
-
//#endregion
|
|
56
|
-
export { rateLimitErrorDiagnostics as t };
|
|
@@ -1,235 +0,0 @@
|
|
|
1
|
-
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
|
-
//#region ../internal/src/runtime/cloudflare-env.ts
|
|
3
|
-
let activeEnv;
|
|
4
|
-
const activeEnvStorageKey = Symbol.for("vitehub.cloudflare.env-storage");
|
|
5
|
-
const runtimeGlobal = globalThis;
|
|
6
|
-
const activeEnvStorage = runtimeGlobal[activeEnvStorageKey] ||= new AsyncLocalStorage();
|
|
7
|
-
function getActiveCloudflareEnv() {
|
|
8
|
-
const scoped = activeEnvStorage.getStore();
|
|
9
|
-
if (scoped) return scoped.env;
|
|
10
|
-
const host = globalThis;
|
|
11
|
-
return Object.hasOwn(host, "__env__") && host.__env__ !== void 0 ? host.__env__ : activeEnv;
|
|
12
|
-
}
|
|
13
|
-
function ownValue(value, key) {
|
|
14
|
-
if (value === null || typeof value !== "object" && typeof value !== "function" || !Object.hasOwn(value, key)) return void 0;
|
|
15
|
-
return Reflect.get(value, key);
|
|
16
|
-
}
|
|
17
|
-
function ownObject(value) {
|
|
18
|
-
return value !== null && (typeof value === "object" || typeof value === "function") ? value : void 0;
|
|
19
|
-
}
|
|
20
|
-
/** The objects where H3 1, H3 2, and Nitro hosts put Cloudflare `env` and `context`, in lookup order. */
|
|
21
|
-
function cloudflareOwners(target) {
|
|
22
|
-
const context = ownObject(ownValue(target, "context"));
|
|
23
|
-
const platform = ownObject(ownValue(context, "_platform"));
|
|
24
|
-
const req = ownObject(ownValue(target, "req"));
|
|
25
|
-
const nodeReq = ownObject(ownValue(ownObject(ownValue(target, "node")), "req"));
|
|
26
|
-
return [
|
|
27
|
-
ownObject(ownValue(context, "cloudflare")),
|
|
28
|
-
ownObject(ownValue(platform, "cloudflare")),
|
|
29
|
-
ownObject(ownValue(ownObject(ownValue(req, "runtime")), "cloudflare")),
|
|
30
|
-
ownObject(ownValue(ownObject(ownValue(nodeReq, "runtime")), "cloudflare"))
|
|
31
|
-
];
|
|
32
|
-
}
|
|
33
|
-
function getCloudflareEnv(event, options = {}) {
|
|
34
|
-
const target = ownObject(event);
|
|
35
|
-
for (const candidate of [ownValue(target, "env"), ...cloudflareOwners(target).map((owner) => ownValue(owner, "env"))]) if (candidate) return candidate;
|
|
36
|
-
return options.fallback === false ? void 0 : getActiveCloudflareEnv();
|
|
37
|
-
}
|
|
38
|
-
function resolveWaitUntil(event, options = {}) {
|
|
39
|
-
const target = ownObject(event);
|
|
40
|
-
const context = ownObject(ownValue(target, "context"));
|
|
41
|
-
const cloudflare = ownObject(ownValue(context, "cloudflare"));
|
|
42
|
-
const cloudflareContext = ownObject(ownValue(cloudflare, "context"));
|
|
43
|
-
const platformCloudflare = ownObject(ownValue(ownObject(ownValue(context, "_platform")), "cloudflare"));
|
|
44
|
-
const platformCloudflareContext = ownObject(ownValue(platformCloudflare, "context"));
|
|
45
|
-
const req = ownObject(ownValue(target, "req"));
|
|
46
|
-
const reqCloudflare = ownObject(ownValue(ownObject(ownValue(req, "runtime")), "cloudflare"));
|
|
47
|
-
const reqCloudflareContext = ownObject(ownValue(reqCloudflare, "context"));
|
|
48
|
-
const nodeReq = ownObject(ownValue(ownObject(ownValue(target, "node")), "req"));
|
|
49
|
-
const nodeCloudflare = ownObject(ownValue(ownObject(ownValue(nodeReq, "runtime")), "cloudflare"));
|
|
50
|
-
const hosts = [
|
|
51
|
-
context,
|
|
52
|
-
cloudflare,
|
|
53
|
-
cloudflareContext,
|
|
54
|
-
platformCloudflare,
|
|
55
|
-
platformCloudflareContext,
|
|
56
|
-
req,
|
|
57
|
-
reqCloudflare,
|
|
58
|
-
reqCloudflareContext,
|
|
59
|
-
nodeReq,
|
|
60
|
-
nodeCloudflare,
|
|
61
|
-
ownObject(ownValue(nodeCloudflare, "context"))
|
|
62
|
-
];
|
|
63
|
-
const owners = options.preferHost ? [...hosts, target] : [target, ...hosts];
|
|
64
|
-
for (const owner of owners) {
|
|
65
|
-
const waitUntil = ownValue(owner, "waitUntil");
|
|
66
|
-
if (typeof waitUntil === "function") return waitUntil.bind(owner);
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
//#endregion
|
|
70
|
-
//#region ../internal/src/runtime/h3-v1-request.ts
|
|
71
|
-
const payloadMethods = /* @__PURE__ */ new Set([
|
|
72
|
-
"DELETE",
|
|
73
|
-
"PATCH",
|
|
74
|
-
"POST",
|
|
75
|
-
"PUT"
|
|
76
|
-
]);
|
|
77
|
-
const rawBodySymbol = Symbol.for("h3RawBody");
|
|
78
|
-
const nodeEventRequests = /* @__PURE__ */ new WeakMap();
|
|
79
|
-
/** Return true for an event with an own `node.req` object, such as an H3 1 event. */
|
|
80
|
-
function isH3V1Event(input) {
|
|
81
|
-
if (!Object.hasOwn(input, "node")) return false;
|
|
82
|
-
const node = Reflect.get(input, "node");
|
|
83
|
-
return isObjectLike(node) && isObjectLike(Reflect.get(node, "req"));
|
|
84
|
-
}
|
|
85
|
-
function isObjectLike(value) {
|
|
86
|
-
return value !== null && (typeof value === "object" || typeof value === "function");
|
|
87
|
-
}
|
|
88
|
-
function isWebRequest(value) {
|
|
89
|
-
if (!isObjectLike(value)) return false;
|
|
90
|
-
const headers = Reflect.get(value, "headers");
|
|
91
|
-
return typeof Reflect.get(value, "url") === "string" && typeof Reflect.get(value, "method") === "string" && isObjectLike(headers) && typeof Reflect.get(headers, "get") === "function";
|
|
92
|
-
}
|
|
93
|
-
function firstHeader(value) {
|
|
94
|
-
return Array.isArray(value) ? value[0] : value;
|
|
95
|
-
}
|
|
96
|
-
function readNodeRequestHeaders(request) {
|
|
97
|
-
const headers = new Headers();
|
|
98
|
-
for (const [name, value] of Object.entries(request.headers)) if (Array.isArray(value)) for (const item of value) headers.append(name, item);
|
|
99
|
-
else if (value !== void 0) headers.set(name, value);
|
|
100
|
-
return headers;
|
|
101
|
-
}
|
|
102
|
-
/** Rebuild the public URL. H3 1 removes the app base path from `url` and keeps it in `originalUrl`. */
|
|
103
|
-
function readNodeRequestURL(request) {
|
|
104
|
-
const host = firstHeader(request.headers.host) || "localhost";
|
|
105
|
-
const encrypted = request.socket?.encrypted || request.connection?.encrypted;
|
|
106
|
-
const protocol = firstHeader(request.headers["x-forwarded-proto"]) === "https" || encrypted ? "https" : "http";
|
|
107
|
-
const path = (request.originalUrl || request.url || "/").replace(/^[/\\]+/, "/");
|
|
108
|
-
return new URL(path, `${protocol}://${host}`);
|
|
109
|
-
}
|
|
110
|
-
function toBodyStream(value) {
|
|
111
|
-
if (value instanceof ReadableStream) return value;
|
|
112
|
-
if (value instanceof Uint8Array) return new ReadableStream({ start(controller) {
|
|
113
|
-
controller.enqueue(value);
|
|
114
|
-
controller.close();
|
|
115
|
-
} });
|
|
116
|
-
if (typeof value === "string") return new Blob([value]).stream();
|
|
117
|
-
if (value instanceof URLSearchParams) return new Blob([value.toString()]).stream();
|
|
118
|
-
if (value instanceof Object && Object.getPrototypeOf(value) === Object.prototype) return new Blob([JSON.stringify(value)]).stream();
|
|
119
|
-
}
|
|
120
|
-
async function openNodeRequestBody(event) {
|
|
121
|
-
const request = event.node.req;
|
|
122
|
-
const stored = event._requestBody ?? request[rawBodySymbol] ?? request.rawBody ?? request.body;
|
|
123
|
-
if (stored !== void 0 && stored !== null) {
|
|
124
|
-
const stream = toBodyStream(await stored);
|
|
125
|
-
if (stream) {
|
|
126
|
-
const reader = stream.getReader();
|
|
127
|
-
return () => reader.read();
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
const iterator = request[Symbol.asyncIterator]?.();
|
|
131
|
-
if (!iterator) return void 0;
|
|
132
|
-
const chunks = [];
|
|
133
|
-
const collected = [];
|
|
134
|
-
let total = 0;
|
|
135
|
-
let ended = false;
|
|
136
|
-
let failed = false;
|
|
137
|
-
let failure;
|
|
138
|
-
let waiting;
|
|
139
|
-
const produce = async () => {
|
|
140
|
-
try {
|
|
141
|
-
for await (const chunk of { [Symbol.asyncIterator]: () => iterator }) {
|
|
142
|
-
let bytes;
|
|
143
|
-
if (typeof chunk === "string") bytes = new TextEncoder().encode(chunk);
|
|
144
|
-
else if (chunk instanceof Uint8Array) bytes = chunk;
|
|
145
|
-
else throw new TypeError("[vitehub] Request body chunks must be strings or bytes.");
|
|
146
|
-
collected.push(bytes);
|
|
147
|
-
total += bytes.byteLength;
|
|
148
|
-
if (waiting) {
|
|
149
|
-
waiting.resolve({ value: bytes });
|
|
150
|
-
waiting = void 0;
|
|
151
|
-
} else chunks.push(bytes);
|
|
152
|
-
}
|
|
153
|
-
ended = true;
|
|
154
|
-
waiting?.resolve({ done: true });
|
|
155
|
-
waiting = void 0;
|
|
156
|
-
} catch (error) {
|
|
157
|
-
failed = true;
|
|
158
|
-
failure = error;
|
|
159
|
-
ended = true;
|
|
160
|
-
waiting?.reject(error);
|
|
161
|
-
waiting = void 0;
|
|
162
|
-
throw error;
|
|
163
|
-
}
|
|
164
|
-
const bytes = new Uint8Array(total);
|
|
165
|
-
let offset = 0;
|
|
166
|
-
for (const chunk of collected) {
|
|
167
|
-
bytes.set(chunk, offset);
|
|
168
|
-
offset += chunk.byteLength;
|
|
169
|
-
}
|
|
170
|
-
return bytes;
|
|
171
|
-
};
|
|
172
|
-
let body;
|
|
173
|
-
const start = () => body ??= produce();
|
|
174
|
-
event._requestBody = { then: (resolve, reject) => start().then(resolve, reject) };
|
|
175
|
-
return async () => {
|
|
176
|
-
start();
|
|
177
|
-
body?.catch(() => {});
|
|
178
|
-
if (failed) throw failure;
|
|
179
|
-
if (chunks.length) return { value: chunks.shift() };
|
|
180
|
-
if (ended) return { done: true };
|
|
181
|
-
return new Promise((resolve, reject) => {
|
|
182
|
-
waiting = {
|
|
183
|
-
resolve,
|
|
184
|
-
reject
|
|
185
|
-
};
|
|
186
|
-
});
|
|
187
|
-
};
|
|
188
|
-
}
|
|
189
|
-
/** Read the body lazily, from a value a host already read or from the Node request stream. */
|
|
190
|
-
function readNodeRequestBody(event) {
|
|
191
|
-
let reader;
|
|
192
|
-
return new ReadableStream({ async pull(controller) {
|
|
193
|
-
const chunk = await (await (reader ??= openNodeRequestBody(event)))?.();
|
|
194
|
-
if (!chunk || chunk.done) {
|
|
195
|
-
controller.close();
|
|
196
|
-
return;
|
|
197
|
-
}
|
|
198
|
-
const value = chunk.value;
|
|
199
|
-
if (typeof value === "string") controller.enqueue(new TextEncoder().encode(value));
|
|
200
|
-
else if (value instanceof Uint8Array) controller.enqueue(value);
|
|
201
|
-
else controller.error(/* @__PURE__ */ new TypeError("[vitehub] Request body chunks must be strings or bytes."));
|
|
202
|
-
} }, { highWaterMark: 0 });
|
|
203
|
-
}
|
|
204
|
-
/** Return one Web Request per H3 1 event. The body is read only when a caller reads the Request body. */
|
|
205
|
-
function toH3V1Request(event) {
|
|
206
|
-
const cached = nodeEventRequests.get(event);
|
|
207
|
-
if (cached) return cached;
|
|
208
|
-
if (isWebRequest(event.web?.request)) return event.web.request;
|
|
209
|
-
const request = event.node.req;
|
|
210
|
-
const method = (request.method || "GET").toUpperCase();
|
|
211
|
-
const init = {
|
|
212
|
-
headers: readNodeRequestHeaders(request),
|
|
213
|
-
method
|
|
214
|
-
};
|
|
215
|
-
if (payloadMethods.has(method)) {
|
|
216
|
-
init.body = readNodeRequestBody(event);
|
|
217
|
-
init.duplex = "half";
|
|
218
|
-
}
|
|
219
|
-
const webRequest = new Request(readNodeRequestURL(request), init);
|
|
220
|
-
nodeEventRequests.set(event, webRequest);
|
|
221
|
-
return webRequest;
|
|
222
|
-
}
|
|
223
|
-
/**
|
|
224
|
-
* Return the Web Request of a request, an H3 2 event with an own `req` request, or an H3 1 event such as Nuxt 4's.
|
|
225
|
-
* H3 1 events have no `url`, so read the URL from the request this returns. Throws a `TypeError` for any other value.
|
|
226
|
-
*/
|
|
227
|
-
function toEventRequest(event) {
|
|
228
|
-
if (isWebRequest(event)) return event;
|
|
229
|
-
const req = Object.hasOwn(event, "req") ? Reflect.get(event, "req") : void 0;
|
|
230
|
-
if (isWebRequest(req)) return req;
|
|
231
|
-
if (isH3V1Event(event)) return toH3V1Request(event);
|
|
232
|
-
throw new TypeError("[vitehub] Expected a Request, an H3 2 event, or an H3 1 event.");
|
|
233
|
-
}
|
|
234
|
-
//#endregion
|
|
235
|
-
export { resolveWaitUntil as i, toEventRequest as n, getCloudflareEnv as r, isH3V1Event as t };
|