@edgeone/opennextjs-pages 0.0.7 → 0.0.8
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/dist/build/functions/middleware/compat/globals.js +119 -0
- package/dist/build/functions/middleware/compat/index.js +27 -0
- package/dist/build/functions/middleware/compat/turbopack-runtime.js +662 -0
- package/dist/build/functions/middleware/compat/webpack-runtime.js +266 -0
- package/dist/build/functions/middleware/compiler.js +3580 -0
- package/dist/build/functions/middleware/middleware.js +58 -0
- package/dist/build/functions/middleware/polyfills/async-local-storage.js +100 -0
- package/dist/build/functions/middleware/polyfills/buffer.js +214 -0
- package/dist/build/functions/middleware/polyfills/crypto.js +117 -0
- package/dist/build/functions/middleware/polyfills/index.js +200 -0
- package/dist/build/functions/middleware/polyfills/process.js +101 -0
- package/dist/build/functions/middleware/wrapper.js +353 -0
- package/dist/index.js +2 -0
- package/dist/run/handlers/tags-handler.cjs +1 -1
- package/dist/types.js +6 -0
- package/package.json +1 -1
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
|
|
2
|
+
var require = await (async () => {
|
|
3
|
+
var { createRequire } = await import("node:module");
|
|
4
|
+
return createRequire(import.meta.url);
|
|
5
|
+
})();
|
|
6
|
+
|
|
7
|
+
import "../../../esm-chunks/chunk-6BT4RYQJ.js";
|
|
8
|
+
|
|
9
|
+
// src/build/functions/middleware/middleware.ts
|
|
10
|
+
import { compile } from "./compiler.js";
|
|
11
|
+
import { join } from "node:path";
|
|
12
|
+
import { existsSync, writeFileSync, mkdirSync, readFileSync } from "node:fs";
|
|
13
|
+
var compileMiddleware = async (ctx) => {
|
|
14
|
+
const possiblePaths = [
|
|
15
|
+
// 1. Next.js 15+: distDir/server/middleware.js (直接在 .next 目录下)
|
|
16
|
+
join(ctx.distDir, "server/middleware.js"),
|
|
17
|
+
// 2. Next.js 15+: standaloneDir/.next/server/middleware.js
|
|
18
|
+
join(ctx.standaloneDir, ".next/server/middleware.js"),
|
|
19
|
+
// 3. 旧版: standaloneDir/.next/server/src/middleware.js
|
|
20
|
+
join(ctx.standaloneDir, ".next/server/src/middleware.js"),
|
|
21
|
+
// 4. 旧版: distDir/server/src/middleware.js
|
|
22
|
+
join(ctx.distDir, "server/src/middleware.js"),
|
|
23
|
+
// 5. 相对于当前工作目录
|
|
24
|
+
join(process.cwd(), ".next/server/middleware.js"),
|
|
25
|
+
join(process.cwd(), ".next/server/src/middleware.js")
|
|
26
|
+
];
|
|
27
|
+
let middlewareFilePath = "";
|
|
28
|
+
for (const path of possiblePaths) {
|
|
29
|
+
if (existsSync(path)) {
|
|
30
|
+
middlewareFilePath = path;
|
|
31
|
+
break;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
if (!middlewareFilePath) {
|
|
35
|
+
console.error("[Middleware Compiler] Could not find middleware.js in any of the expected locations:");
|
|
36
|
+
possiblePaths.forEach((p) => console.error(" -", p));
|
|
37
|
+
throw new Error("middleware.js not found");
|
|
38
|
+
}
|
|
39
|
+
const result = await compile(middlewareFilePath, {
|
|
40
|
+
env: { DEBUG: "true" }
|
|
41
|
+
});
|
|
42
|
+
const outputDir = join(ctx.distDir, "functions");
|
|
43
|
+
mkdirSync(outputDir, { recursive: true });
|
|
44
|
+
const outputPath = join(outputDir, "compiled-middleware.js");
|
|
45
|
+
writeFileSync(outputPath, result.code || "", "utf-8");
|
|
46
|
+
const edgeFunctionPath = join(process.cwd(), ".edgeone/edge-functions/index.js");
|
|
47
|
+
if (existsSync(edgeFunctionPath)) {
|
|
48
|
+
let edgeFunctionCode = readFileSync(edgeFunctionPath, "utf-8");
|
|
49
|
+
if (edgeFunctionCode.includes(`'__MIDDLEWARE_BUNDLE_CODE__'`)) {
|
|
50
|
+
edgeFunctionCode = edgeFunctionCode.replace(`'__MIDDLEWARE_BUNDLE_CODE__'`, () => result.code || "");
|
|
51
|
+
writeFileSync(edgeFunctionPath, edgeFunctionCode, "utf-8");
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return result;
|
|
55
|
+
};
|
|
56
|
+
export {
|
|
57
|
+
compileMiddleware
|
|
58
|
+
};
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
|
|
2
|
+
var require = await (async () => {
|
|
3
|
+
var { createRequire } = await import("node:module");
|
|
4
|
+
return createRequire(import.meta.url);
|
|
5
|
+
})();
|
|
6
|
+
|
|
7
|
+
import "../../../../esm-chunks/chunk-6BT4RYQJ.js";
|
|
8
|
+
|
|
9
|
+
// src/build/functions/middleware/polyfills/async-local-storage.ts
|
|
10
|
+
var asyncLocalStoragePolyfill = `
|
|
11
|
+
// === AsyncLocalStorage Polyfill ===
|
|
12
|
+
const AsyncLocalStorage = (function() {
|
|
13
|
+
// \u4F7F\u7528 WeakMap \u5B58\u50A8\u5F02\u6B65\u4E0A\u4E0B\u6587
|
|
14
|
+
const asyncContexts = new WeakMap();
|
|
15
|
+
let currentContext = null;
|
|
16
|
+
|
|
17
|
+
class AsyncLocalStoragePolyfill {
|
|
18
|
+
constructor() {
|
|
19
|
+
this._enabled = true;
|
|
20
|
+
this._store = undefined;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
disable() {
|
|
24
|
+
this._enabled = false;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
getStore() {
|
|
28
|
+
if (!this._enabled) return undefined;
|
|
29
|
+
return this._store;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
run(store, callback, ...args) {
|
|
33
|
+
if (!this._enabled) {
|
|
34
|
+
return callback(...args);
|
|
35
|
+
}
|
|
36
|
+
const previousStore = this._store;
|
|
37
|
+
this._store = store;
|
|
38
|
+
try {
|
|
39
|
+
return callback(...args);
|
|
40
|
+
} finally {
|
|
41
|
+
this._store = previousStore;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
exit(callback, ...args) {
|
|
46
|
+
if (!this._enabled) {
|
|
47
|
+
return callback(...args);
|
|
48
|
+
}
|
|
49
|
+
const previousStore = this._store;
|
|
50
|
+
this._store = undefined;
|
|
51
|
+
try {
|
|
52
|
+
return callback(...args);
|
|
53
|
+
} finally {
|
|
54
|
+
this._store = previousStore;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
enterWith(store) {
|
|
59
|
+
if (!this._enabled) return;
|
|
60
|
+
this._store = store;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
static bind(fn) {
|
|
64
|
+
return fn;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
static snapshot() {
|
|
68
|
+
return (fn, ...args) => fn(...args);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return AsyncLocalStoragePolyfill;
|
|
73
|
+
})();
|
|
74
|
+
|
|
75
|
+
// \u6A21\u62DF node:async_hooks \u6A21\u5757
|
|
76
|
+
const async_hooks = {
|
|
77
|
+
AsyncLocalStorage,
|
|
78
|
+
createHook: () => ({
|
|
79
|
+
enable: () => {},
|
|
80
|
+
disable: () => {}
|
|
81
|
+
}),
|
|
82
|
+
executionAsyncId: () => 0,
|
|
83
|
+
triggerAsyncId: () => 0,
|
|
84
|
+
executionAsyncResource: () => ({}),
|
|
85
|
+
AsyncResource: class AsyncResource {
|
|
86
|
+
constructor(type) { this.type = type; }
|
|
87
|
+
runInAsyncScope(fn, thisArg, ...args) { return fn.call(thisArg, ...args); }
|
|
88
|
+
emitDestroy() { return this; }
|
|
89
|
+
asyncId() { return 0; }
|
|
90
|
+
triggerAsyncId() { return 0; }
|
|
91
|
+
bind(fn) { return fn; }
|
|
92
|
+
static bind(fn) { return fn; }
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
globalThis.AsyncLocalStorage = AsyncLocalStorage;
|
|
97
|
+
`;
|
|
98
|
+
export {
|
|
99
|
+
asyncLocalStoragePolyfill
|
|
100
|
+
};
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
|
|
2
|
+
var require = await (async () => {
|
|
3
|
+
var { createRequire } = await import("node:module");
|
|
4
|
+
return createRequire(import.meta.url);
|
|
5
|
+
})();
|
|
6
|
+
|
|
7
|
+
import "../../../../esm-chunks/chunk-6BT4RYQJ.js";
|
|
8
|
+
|
|
9
|
+
// src/build/functions/middleware/polyfills/buffer.ts
|
|
10
|
+
var bufferPolyfill = `
|
|
11
|
+
// === Buffer Polyfill ===
|
|
12
|
+
const Buffer = (function() {
|
|
13
|
+
class BufferPolyfill extends Uint8Array {
|
|
14
|
+
static isBuffer(obj) {
|
|
15
|
+
return obj instanceof BufferPolyfill || obj instanceof Uint8Array;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
static from(value, encodingOrOffset, length) {
|
|
19
|
+
if (typeof value === 'string') {
|
|
20
|
+
const encoding = encodingOrOffset || 'utf8';
|
|
21
|
+
if (encoding === 'base64') {
|
|
22
|
+
const binaryString = atob(value);
|
|
23
|
+
const bytes = new Uint8Array(binaryString.length);
|
|
24
|
+
for (let i = 0; i < binaryString.length; i++) {
|
|
25
|
+
bytes[i] = binaryString.charCodeAt(i);
|
|
26
|
+
}
|
|
27
|
+
return new BufferPolyfill(bytes);
|
|
28
|
+
} else if (encoding === 'hex') {
|
|
29
|
+
const bytes = new Uint8Array(value.length / 2);
|
|
30
|
+
for (let i = 0; i < value.length; i += 2) {
|
|
31
|
+
bytes[i / 2] = parseInt(value.substr(i, 2), 16);
|
|
32
|
+
}
|
|
33
|
+
return new BufferPolyfill(bytes);
|
|
34
|
+
} else {
|
|
35
|
+
// utf8
|
|
36
|
+
const encoder = new TextEncoder();
|
|
37
|
+
return new BufferPolyfill(encoder.encode(value));
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
if (Array.isArray(value) || value instanceof Uint8Array) {
|
|
41
|
+
return new BufferPolyfill(value);
|
|
42
|
+
}
|
|
43
|
+
if (value instanceof ArrayBuffer) {
|
|
44
|
+
return new BufferPolyfill(new Uint8Array(value, encodingOrOffset, length));
|
|
45
|
+
}
|
|
46
|
+
throw new TypeError('Invalid argument type for Buffer.from');
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
static alloc(size, fill, encoding) {
|
|
50
|
+
const buf = new BufferPolyfill(size);
|
|
51
|
+
if (fill !== undefined) {
|
|
52
|
+
if (typeof fill === 'number') {
|
|
53
|
+
buf.fill(fill);
|
|
54
|
+
} else if (typeof fill === 'string') {
|
|
55
|
+
const fillBuf = BufferPolyfill.from(fill, encoding);
|
|
56
|
+
for (let i = 0; i < size; i++) {
|
|
57
|
+
buf[i] = fillBuf[i % fillBuf.length];
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return buf;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
static allocUnsafe(size) {
|
|
65
|
+
return new BufferPolyfill(size);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
static concat(list, totalLength) {
|
|
69
|
+
if (totalLength === undefined) {
|
|
70
|
+
totalLength = list.reduce((acc, buf) => acc + buf.length, 0);
|
|
71
|
+
}
|
|
72
|
+
const result = new BufferPolyfill(totalLength);
|
|
73
|
+
let offset = 0;
|
|
74
|
+
for (const buf of list) {
|
|
75
|
+
result.set(buf, offset);
|
|
76
|
+
offset += buf.length;
|
|
77
|
+
}
|
|
78
|
+
return result;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
static byteLength(string, encoding) {
|
|
82
|
+
if (typeof string !== 'string') {
|
|
83
|
+
return string.length || string.byteLength || 0;
|
|
84
|
+
}
|
|
85
|
+
if (encoding === 'base64') {
|
|
86
|
+
return Math.ceil(string.length * 3 / 4);
|
|
87
|
+
}
|
|
88
|
+
return new TextEncoder().encode(string).length;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
toString(encoding = 'utf8', start = 0, end = this.length) {
|
|
92
|
+
const slice = this.subarray(start, end);
|
|
93
|
+
if (encoding === 'base64') {
|
|
94
|
+
let binary = '';
|
|
95
|
+
for (let i = 0; i < slice.length; i++) {
|
|
96
|
+
binary += String.fromCharCode(slice[i]);
|
|
97
|
+
}
|
|
98
|
+
return btoa(binary);
|
|
99
|
+
} else if (encoding === 'hex') {
|
|
100
|
+
return Array.from(slice)
|
|
101
|
+
.map(b => b.toString(16).padStart(2, '0'))
|
|
102
|
+
.join('');
|
|
103
|
+
} else {
|
|
104
|
+
// utf8
|
|
105
|
+
return new TextDecoder().decode(slice);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
write(string, offset = 0, length, encoding = 'utf8') {
|
|
110
|
+
const buf = BufferPolyfill.from(string, encoding);
|
|
111
|
+
const bytesToWrite = Math.min(buf.length, length || this.length - offset);
|
|
112
|
+
this.set(buf.subarray(0, bytesToWrite), offset);
|
|
113
|
+
return bytesToWrite;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
copy(target, targetStart = 0, sourceStart = 0, sourceEnd = this.length) {
|
|
117
|
+
const slice = this.subarray(sourceStart, sourceEnd);
|
|
118
|
+
target.set(slice, targetStart);
|
|
119
|
+
return slice.length;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
slice(start, end) {
|
|
123
|
+
return new BufferPolyfill(this.subarray(start, end));
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
equals(otherBuffer) {
|
|
127
|
+
if (this.length !== otherBuffer.length) return false;
|
|
128
|
+
for (let i = 0; i < this.length; i++) {
|
|
129
|
+
if (this[i] !== otherBuffer[i]) return false;
|
|
130
|
+
}
|
|
131
|
+
return true;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
compare(otherBuffer) {
|
|
135
|
+
const len = Math.min(this.length, otherBuffer.length);
|
|
136
|
+
for (let i = 0; i < len; i++) {
|
|
137
|
+
if (this[i] < otherBuffer[i]) return -1;
|
|
138
|
+
if (this[i] > otherBuffer[i]) return 1;
|
|
139
|
+
}
|
|
140
|
+
if (this.length < otherBuffer.length) return -1;
|
|
141
|
+
if (this.length > otherBuffer.length) return 1;
|
|
142
|
+
return 0;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
indexOf(value, byteOffset = 0, encoding) {
|
|
146
|
+
if (typeof value === 'string') {
|
|
147
|
+
value = BufferPolyfill.from(value, encoding);
|
|
148
|
+
}
|
|
149
|
+
if (typeof value === 'number') {
|
|
150
|
+
for (let i = byteOffset; i < this.length; i++) {
|
|
151
|
+
if (this[i] === value) return i;
|
|
152
|
+
}
|
|
153
|
+
return -1;
|
|
154
|
+
}
|
|
155
|
+
outer: for (let i = byteOffset; i <= this.length - value.length; i++) {
|
|
156
|
+
for (let j = 0; j < value.length; j++) {
|
|
157
|
+
if (this[i + j] !== value[j]) continue outer;
|
|
158
|
+
}
|
|
159
|
+
return i;
|
|
160
|
+
}
|
|
161
|
+
return -1;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
includes(value, byteOffset, encoding) {
|
|
165
|
+
return this.indexOf(value, byteOffset, encoding) !== -1;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// \u8BFB\u5199\u65B9\u6CD5
|
|
169
|
+
readUInt8(offset = 0) { return this[offset]; }
|
|
170
|
+
readUInt16BE(offset = 0) { return (this[offset] << 8) | this[offset + 1]; }
|
|
171
|
+
readUInt16LE(offset = 0) { return this[offset] | (this[offset + 1] << 8); }
|
|
172
|
+
readUInt32BE(offset = 0) {
|
|
173
|
+
return (this[offset] * 0x1000000) + ((this[offset + 1] << 16) | (this[offset + 2] << 8) | this[offset + 3]);
|
|
174
|
+
}
|
|
175
|
+
readUInt32LE(offset = 0) {
|
|
176
|
+
return ((this[offset + 3] * 0x1000000) + ((this[offset + 2] << 16) | (this[offset + 1] << 8) | this[offset])) >>> 0;
|
|
177
|
+
}
|
|
178
|
+
readInt8(offset = 0) { return this[offset] > 127 ? this[offset] - 256 : this[offset]; }
|
|
179
|
+
readInt16BE(offset = 0) { const val = this.readUInt16BE(offset); return val > 0x7FFF ? val - 0x10000 : val; }
|
|
180
|
+
readInt16LE(offset = 0) { const val = this.readUInt16LE(offset); return val > 0x7FFF ? val - 0x10000 : val; }
|
|
181
|
+
readInt32BE(offset = 0) { return (this[offset] << 24) | (this[offset + 1] << 16) | (this[offset + 2] << 8) | this[offset + 3]; }
|
|
182
|
+
readInt32LE(offset = 0) { return this[offset] | (this[offset + 1] << 8) | (this[offset + 2] << 16) | (this[offset + 3] << 24); }
|
|
183
|
+
|
|
184
|
+
writeUInt8(value, offset = 0) { this[offset] = value & 0xFF; return offset + 1; }
|
|
185
|
+
writeUInt16BE(value, offset = 0) { this[offset] = (value >> 8) & 0xFF; this[offset + 1] = value & 0xFF; return offset + 2; }
|
|
186
|
+
writeUInt16LE(value, offset = 0) { this[offset] = value & 0xFF; this[offset + 1] = (value >> 8) & 0xFF; return offset + 2; }
|
|
187
|
+
writeUInt32BE(value, offset = 0) {
|
|
188
|
+
this[offset] = (value >>> 24) & 0xFF;
|
|
189
|
+
this[offset + 1] = (value >>> 16) & 0xFF;
|
|
190
|
+
this[offset + 2] = (value >>> 8) & 0xFF;
|
|
191
|
+
this[offset + 3] = value & 0xFF;
|
|
192
|
+
return offset + 4;
|
|
193
|
+
}
|
|
194
|
+
writeUInt32LE(value, offset = 0) {
|
|
195
|
+
this[offset] = value & 0xFF;
|
|
196
|
+
this[offset + 1] = (value >>> 8) & 0xFF;
|
|
197
|
+
this[offset + 2] = (value >>> 16) & 0xFF;
|
|
198
|
+
this[offset + 3] = (value >>> 24) & 0xFF;
|
|
199
|
+
return offset + 4;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
toJSON() {
|
|
203
|
+
return { type: 'Buffer', data: Array.from(this) };
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
return BufferPolyfill;
|
|
208
|
+
})();
|
|
209
|
+
|
|
210
|
+
globalThis.Buffer = Buffer;
|
|
211
|
+
`;
|
|
212
|
+
export {
|
|
213
|
+
bufferPolyfill
|
|
214
|
+
};
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
|
|
2
|
+
var require = await (async () => {
|
|
3
|
+
var { createRequire } = await import("node:module");
|
|
4
|
+
return createRequire(import.meta.url);
|
|
5
|
+
})();
|
|
6
|
+
|
|
7
|
+
import "../../../../esm-chunks/chunk-6BT4RYQJ.js";
|
|
8
|
+
|
|
9
|
+
// src/build/functions/middleware/polyfills/crypto.ts
|
|
10
|
+
var cryptoPolyfill = `
|
|
11
|
+
// === Crypto Polyfill ===
|
|
12
|
+
const crypto = globalThis.crypto || {};
|
|
13
|
+
|
|
14
|
+
// \u786E\u4FDD getRandomValues \u53EF\u7528
|
|
15
|
+
if (!crypto.getRandomValues) {
|
|
16
|
+
crypto.getRandomValues = (array) => {
|
|
17
|
+
for (let i = 0; i < array.length; i++) {
|
|
18
|
+
array[i] = Math.floor(Math.random() * 256);
|
|
19
|
+
}
|
|
20
|
+
return array;
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// \u6DFB\u52A0 randomBytes \u65B9\u6CD5\uFF08Node.js \u98CE\u683C\uFF09
|
|
25
|
+
crypto.randomBytes = (size) => {
|
|
26
|
+
const bytes = new Uint8Array(size);
|
|
27
|
+
crypto.getRandomValues(bytes);
|
|
28
|
+
return Buffer.from(bytes);
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
// \u6DFB\u52A0 randomUUID \u65B9\u6CD5
|
|
32
|
+
if (!crypto.randomUUID) {
|
|
33
|
+
crypto.randomUUID = () => {
|
|
34
|
+
return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c =>
|
|
35
|
+
(c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
|
|
36
|
+
);
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// \u6DFB\u52A0 createHash \u65B9\u6CD5\uFF08\u7B80\u5316\u7248\uFF09
|
|
41
|
+
crypto.createHash = (algorithm) => {
|
|
42
|
+
const data = [];
|
|
43
|
+
return {
|
|
44
|
+
update(chunk, encoding) {
|
|
45
|
+
if (typeof chunk === 'string') {
|
|
46
|
+
chunk = new TextEncoder().encode(chunk);
|
|
47
|
+
}
|
|
48
|
+
data.push(chunk);
|
|
49
|
+
return this;
|
|
50
|
+
},
|
|
51
|
+
async digest(encoding) {
|
|
52
|
+
const buffer = Buffer.concat(data);
|
|
53
|
+
const hashBuffer = await crypto.subtle.digest(
|
|
54
|
+
algorithm.toUpperCase().replace('SHA', 'SHA-'),
|
|
55
|
+
buffer
|
|
56
|
+
);
|
|
57
|
+
const result = Buffer.from(hashBuffer);
|
|
58
|
+
if (encoding === 'hex') {
|
|
59
|
+
return result.toString('hex');
|
|
60
|
+
} else if (encoding === 'base64') {
|
|
61
|
+
return result.toString('base64');
|
|
62
|
+
}
|
|
63
|
+
return result;
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
// \u6DFB\u52A0 createHmac \u65B9\u6CD5\uFF08\u7B80\u5316\u7248\uFF09
|
|
69
|
+
crypto.createHmac = (algorithm, key) => {
|
|
70
|
+
const data = [];
|
|
71
|
+
return {
|
|
72
|
+
update(chunk, encoding) {
|
|
73
|
+
if (typeof chunk === 'string') {
|
|
74
|
+
chunk = new TextEncoder().encode(chunk);
|
|
75
|
+
}
|
|
76
|
+
data.push(chunk);
|
|
77
|
+
return this;
|
|
78
|
+
},
|
|
79
|
+
async digest(encoding) {
|
|
80
|
+
const keyData = typeof key === 'string' ? new TextEncoder().encode(key) : key;
|
|
81
|
+
const cryptoKey = await crypto.subtle.importKey(
|
|
82
|
+
'raw',
|
|
83
|
+
keyData,
|
|
84
|
+
{ name: 'HMAC', hash: algorithm.toUpperCase().replace('SHA', 'SHA-') },
|
|
85
|
+
false,
|
|
86
|
+
['sign']
|
|
87
|
+
);
|
|
88
|
+
const buffer = Buffer.concat(data);
|
|
89
|
+
const signature = await crypto.subtle.sign('HMAC', cryptoKey, buffer);
|
|
90
|
+
const result = Buffer.from(signature);
|
|
91
|
+
if (encoding === 'hex') {
|
|
92
|
+
return result.toString('hex');
|
|
93
|
+
} else if (encoding === 'base64') {
|
|
94
|
+
return result.toString('base64');
|
|
95
|
+
}
|
|
96
|
+
return result;
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
// \u65F6\u95F4\u5B89\u5168\u6BD4\u8F83
|
|
102
|
+
crypto.timingSafeEqual = (a, b) => {
|
|
103
|
+
if (a.length !== b.length) {
|
|
104
|
+
return false;
|
|
105
|
+
}
|
|
106
|
+
let result = 0;
|
|
107
|
+
for (let i = 0; i < a.length; i++) {
|
|
108
|
+
result |= a[i] ^ b[i];
|
|
109
|
+
}
|
|
110
|
+
return result === 0;
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
globalThis.crypto = crypto;
|
|
114
|
+
`;
|
|
115
|
+
export {
|
|
116
|
+
cryptoPolyfill
|
|
117
|
+
};
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
|
|
2
|
+
var require = await (async () => {
|
|
3
|
+
var { createRequire } = await import("node:module");
|
|
4
|
+
return createRequire(import.meta.url);
|
|
5
|
+
})();
|
|
6
|
+
|
|
7
|
+
import "../../../../esm-chunks/chunk-6BT4RYQJ.js";
|
|
8
|
+
|
|
9
|
+
// src/build/functions/middleware/polyfills/index.ts
|
|
10
|
+
import { bufferPolyfill } from "./buffer.js";
|
|
11
|
+
import { processPolyfill } from "./process.js";
|
|
12
|
+
import { asyncLocalStoragePolyfill } from "./async-local-storage.js";
|
|
13
|
+
import { cryptoPolyfill } from "./crypto.js";
|
|
14
|
+
var headersPolyfill = `
|
|
15
|
+
// === Headers Polyfill for EdgeOne ===
|
|
16
|
+
// EdgeOne Headers \u6784\u9020\u51FD\u6570\u4E0D\u63A5\u53D7 undefined \u53C2\u6570\uFF0C\u9700\u8981\u5305\u88C5\u5904\u7406
|
|
17
|
+
(function() {
|
|
18
|
+
const OriginalHeaders = globalThis.Headers;
|
|
19
|
+
|
|
20
|
+
// \u68C0\u67E5\u662F\u5426\u9700\u8981 polyfill
|
|
21
|
+
let needsPatch = false;
|
|
22
|
+
try {
|
|
23
|
+
new OriginalHeaders(undefined);
|
|
24
|
+
} catch (e) {
|
|
25
|
+
needsPatch = true;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (needsPatch) {
|
|
29
|
+
// \u4F7F\u7528 Proxy \u5305\u88C5 Headers \u6784\u9020\u51FD\u6570
|
|
30
|
+
globalThis.Headers = new Proxy(OriginalHeaders, {
|
|
31
|
+
construct(target, args) {
|
|
32
|
+
// \u5982\u679C\u7B2C\u4E00\u4E2A\u53C2\u6570\u662F undefined \u6216 null\uFF0C\u4F20\u5165\u7A7A\u5BF9\u8C61
|
|
33
|
+
if (args[0] === undefined || args[0] === null) {
|
|
34
|
+
return new target({});
|
|
35
|
+
}
|
|
36
|
+
return new target(...args);
|
|
37
|
+
},
|
|
38
|
+
get(target, prop, receiver) {
|
|
39
|
+
return Reflect.get(target, prop, receiver);
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
})();
|
|
44
|
+
`;
|
|
45
|
+
var responsePolyfill = `
|
|
46
|
+
// === Response Polyfill for EdgeOne ===
|
|
47
|
+
// \u786E\u4FDD Response \u6784\u9020\u51FD\u6570\u80FD\u6B63\u786E\u5904\u7406\u5404\u79CD\u53C2\u6570
|
|
48
|
+
(function() {
|
|
49
|
+
const OriginalResponse = globalThis.Response;
|
|
50
|
+
|
|
51
|
+
// \u6E05\u7406 ResponseInit \u53C2\u6570\uFF0C\u53EA\u4FDD\u7559 EdgeOne \u652F\u6301\u7684\u5C5E\u6027
|
|
52
|
+
function cleanResponseInit(init) {
|
|
53
|
+
if (init === undefined || init === null) {
|
|
54
|
+
return {};
|
|
55
|
+
}
|
|
56
|
+
if (typeof init !== 'object') {
|
|
57
|
+
return init;
|
|
58
|
+
}
|
|
59
|
+
// \u53EA\u4FDD\u7559 EdgeOne \u652F\u6301\u7684\u5C5E\u6027: status, statusText, headers
|
|
60
|
+
const cleanInit = {};
|
|
61
|
+
if (init.status !== undefined) cleanInit.status = init.status;
|
|
62
|
+
if (init.statusText !== undefined) cleanInit.statusText = init.statusText;
|
|
63
|
+
if (init.headers !== undefined) cleanInit.headers = init.headers;
|
|
64
|
+
return cleanInit;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// \u5305\u88C5 Response.redirect \u9759\u6001\u65B9\u6CD5
|
|
68
|
+
// EdgeOne \u7684 Response.redirect \u53EA\u63A5\u53D7\u5B57\u7B26\u4E32\uFF0C\u4E0D\u63A5\u53D7 URL \u5BF9\u8C61
|
|
69
|
+
const originalRedirect = OriginalResponse.redirect;
|
|
70
|
+
const patchedRedirect = function(url, status) {
|
|
71
|
+
// \u5982\u679C url \u662F URL \u5BF9\u8C61\uFF0C\u8F6C\u6362\u4E3A\u5B57\u7B26\u4E32
|
|
72
|
+
const urlString = (url && typeof url === 'object' && url.toString) ? url.toString() : url;
|
|
73
|
+
return originalRedirect.call(OriginalResponse, urlString, status);
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
// \u521B\u5EFA cookies \u5BF9\u8C61\u7684\u5DE5\u5382\u51FD\u6570
|
|
77
|
+
function createResponseCookies(response) {
|
|
78
|
+
const cookieStore = new Map();
|
|
79
|
+
return {
|
|
80
|
+
get: (name) => cookieStore.get(name),
|
|
81
|
+
getAll: () => Array.from(cookieStore.values()),
|
|
82
|
+
has: (name) => cookieStore.has(name),
|
|
83
|
+
set: (nameOrOptions, value, options) => {
|
|
84
|
+
let cookieName, cookieValue, cookieOptions;
|
|
85
|
+
if (typeof nameOrOptions === 'object') {
|
|
86
|
+
cookieName = nameOrOptions.name;
|
|
87
|
+
cookieValue = nameOrOptions.value;
|
|
88
|
+
cookieOptions = nameOrOptions;
|
|
89
|
+
} else {
|
|
90
|
+
cookieName = nameOrOptions;
|
|
91
|
+
cookieValue = value;
|
|
92
|
+
cookieOptions = options || {};
|
|
93
|
+
}
|
|
94
|
+
cookieStore.set(cookieName, { name: cookieName, value: cookieValue, ...cookieOptions });
|
|
95
|
+
// \u540C\u6B65\u5230 Set-Cookie header
|
|
96
|
+
const cookieParts = [cookieName + '=' + encodeURIComponent(cookieValue)];
|
|
97
|
+
if (cookieOptions.path) cookieParts.push('Path=' + cookieOptions.path);
|
|
98
|
+
if (cookieOptions.domain) cookieParts.push('Domain=' + cookieOptions.domain);
|
|
99
|
+
if (cookieOptions.maxAge) cookieParts.push('Max-Age=' + cookieOptions.maxAge);
|
|
100
|
+
if (cookieOptions.expires) cookieParts.push('Expires=' + cookieOptions.expires.toUTCString());
|
|
101
|
+
if (cookieOptions.httpOnly) cookieParts.push('HttpOnly');
|
|
102
|
+
if (cookieOptions.secure) cookieParts.push('Secure');
|
|
103
|
+
if (cookieOptions.sameSite) cookieParts.push('SameSite=' + cookieOptions.sameSite);
|
|
104
|
+
response.headers.append('Set-Cookie', cookieParts.join('; '));
|
|
105
|
+
},
|
|
106
|
+
delete: (name) => {
|
|
107
|
+
cookieStore.delete(name);
|
|
108
|
+
response.headers.append('Set-Cookie', name + '=; Max-Age=0; Path=/');
|
|
109
|
+
},
|
|
110
|
+
clear: () => cookieStore.clear(),
|
|
111
|
+
[Symbol.iterator]: () => cookieStore.values(),
|
|
112
|
+
size: cookieStore.size
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// \u4F7F\u7528 Proxy \u5305\u88C5 Response \u6784\u9020\u51FD\u6570
|
|
117
|
+
globalThis.Response = new Proxy(OriginalResponse, {
|
|
118
|
+
construct(target, args) {
|
|
119
|
+
// args[0] = body, args[1] = init
|
|
120
|
+
const body = args[0];
|
|
121
|
+
const init = cleanResponseInit(args[1]);
|
|
122
|
+
const response = new target(body, init);
|
|
123
|
+
|
|
124
|
+
// \u4E3A response \u6DFB\u52A0 cookies \u5C5E\u6027\uFF08NextResponse \u517C\u5BB9\uFF09
|
|
125
|
+
if (!response.cookies) {
|
|
126
|
+
Object.defineProperty(response, 'cookies', {
|
|
127
|
+
value: createResponseCookies(response),
|
|
128
|
+
writable: true,
|
|
129
|
+
enumerable: true
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return response;
|
|
134
|
+
},
|
|
135
|
+
get(target, prop, receiver) {
|
|
136
|
+
// \u62E6\u622A redirect \u9759\u6001\u65B9\u6CD5
|
|
137
|
+
if (prop === 'redirect') {
|
|
138
|
+
return patchedRedirect;
|
|
139
|
+
}
|
|
140
|
+
// \u5176\u4ED6\u9759\u6001\u65B9\u6CD5\u76F4\u63A5\u4ECE\u539F\u59CB Response \u83B7\u53D6
|
|
141
|
+
return Reflect.get(target, prop, receiver);
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
})();
|
|
145
|
+
`;
|
|
146
|
+
function getPolyfillsCode() {
|
|
147
|
+
return `
|
|
148
|
+
// ============================================================
|
|
149
|
+
// Node.js Polyfills for Edge Runtime
|
|
150
|
+
// ============================================================
|
|
151
|
+
|
|
152
|
+
${headersPolyfill}
|
|
153
|
+
|
|
154
|
+
${responsePolyfill}
|
|
155
|
+
|
|
156
|
+
${bufferPolyfill}
|
|
157
|
+
|
|
158
|
+
${processPolyfill}
|
|
159
|
+
|
|
160
|
+
${asyncLocalStoragePolyfill}
|
|
161
|
+
|
|
162
|
+
${cryptoPolyfill}
|
|
163
|
+
|
|
164
|
+
// === Additional Globals ===
|
|
165
|
+
if (typeof globalThis.self === 'undefined') {
|
|
166
|
+
globalThis.self = globalThis;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// === Node.js \u8DEF\u5F84\u76F8\u5173\u5168\u5C40\u53D8\u91CF ===
|
|
170
|
+
// \u8FB9\u7F18\u73AF\u5883\u4E0D\u652F\u6301 __dirname \u548C __filename\uFF0C\u63D0\u4F9B\u6A21\u62DF\u503C
|
|
171
|
+
if (typeof __dirname === 'undefined') {
|
|
172
|
+
globalThis.__dirname = '/';
|
|
173
|
+
}
|
|
174
|
+
if (typeof __filename === 'undefined') {
|
|
175
|
+
globalThis.__filename = '/index.js';
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// === module \u548C exports \u517C\u5BB9 ===
|
|
179
|
+
// \u67D0\u4E9B CommonJS \u4EE3\u7801\u53EF\u80FD\u5F15\u7528\u8FD9\u4E9B\u53D8\u91CF
|
|
180
|
+
if (typeof module === 'undefined') {
|
|
181
|
+
globalThis.module = { exports: {} };
|
|
182
|
+
}
|
|
183
|
+
if (typeof exports === 'undefined') {
|
|
184
|
+
globalThis.exports = globalThis.module.exports;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// \u6A21\u62DF node:buffer \u6A21\u5757
|
|
188
|
+
const nodeBuffer = { Buffer };
|
|
189
|
+
|
|
190
|
+
// \u6A21\u62DF node:async_hooks \u6A21\u5757\u5BFC\u51FA
|
|
191
|
+
const nodeAsyncHooks = async_hooks;
|
|
192
|
+
`;
|
|
193
|
+
}
|
|
194
|
+
export {
|
|
195
|
+
asyncLocalStoragePolyfill,
|
|
196
|
+
bufferPolyfill,
|
|
197
|
+
cryptoPolyfill,
|
|
198
|
+
getPolyfillsCode,
|
|
199
|
+
processPolyfill
|
|
200
|
+
};
|