@edgeone/opennextjs-pages 0.0.9 → 0.1.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/dist/build/functions/middleware/compiler.js +234 -3
- package/dist/build/functions/middleware/middleware.js +2 -3
- package/dist/build/functions/middleware/wrapper.js +16 -28
- package/dist/build/functions/server.js +2 -0
- package/dist/build/routes.js +102 -1
- package/dist/index.js +4 -3
- package/dist/run/handlers/tags-handler.cjs +1 -1
- package/package.json +1 -1
|
@@ -2356,10 +2356,17 @@ function transformForEdgeOneRuntime(code) {
|
|
|
2356
2356
|
const patchedConstructor = `class ${className} extends ${parentClass}{constructor(${paramName}){super(${paramName}.input,${paramName}.init),this.sourcePage=${paramName}.page;var __k="__edgeone_nextInternal",__self=this;if(this[__k]){Object.defineProperty(this,"nextUrl",{get:function(){return __self[__k]?__self[__k].nextUrl:undefined},enumerable:true,configurable:true});Object.defineProperty(this,"cookies",{get:function(){return __self[__k]?__self[__k].cookies:undefined},enumerable:true,configurable:true})}}`;
|
|
2357
2357
|
return patchedConstructor;
|
|
2358
2358
|
});
|
|
2359
|
-
const baseClassInternalAssignPattern = /this\[(\w+)\]\s*=\s*\{\s*cookies\s*:\s*new\s
|
|
2360
|
-
transformed = transformed.replace(baseClassInternalAssignPattern, (match, internalVar,
|
|
2359
|
+
const baseClassInternalAssignPattern = /this\[(\w+)\]\s*=\s*\{\s*cookies\s*:\s*new\s+([\w.]+)\.RequestCookies\s*\(\s*this\.headers\s*\)\s*,\s*nextUrl\s*:\s*(\w+)\s*,\s*url\s*:\s*\3\.toString\s*\(\s*\)\s*\}/g;
|
|
2360
|
+
transformed = transformed.replace(baseClassInternalAssignPattern, (match, internalVar, cookiesModule, nextUrlVar) => {
|
|
2361
2361
|
transformCount++;
|
|
2362
|
-
const patchedAssignment =
|
|
2362
|
+
const patchedAssignment = `Object.defineProperty(this,${internalVar},{value:{cookies:new ${cookiesModule}.RequestCookies(this.headers),nextUrl:${nextUrlVar},url:${nextUrlVar}.toString()},writable:true,enumerable:true,configurable:true});var __self=this;Object.defineProperty(this,"nextUrl",{get:function(){return __self[${internalVar}]?__self[${internalVar}].nextUrl:undefined},enumerable:true,configurable:true});Object.defineProperty(this,"cookies",{get:function(){return __self[${internalVar}]?__self[${internalVar}].cookies:undefined},enumerable:true,configurable:true})`;
|
|
2363
|
+
return patchedAssignment;
|
|
2364
|
+
});
|
|
2365
|
+
const baseClassInternalAssignPatternMultiline = /this\[(\w+)\]\s*=\s*\{\s*cookies\s*:\s*new\s+[\w.]+\.RequestCookies\s*\(\s*this\.headers\s*\)\s*,\s*geo\s*:\s*[^,]+,\s*ip\s*:\s*[^,]+,\s*nextUrl\s*,\s*url\s*:\s*[^}]+\}/g;
|
|
2366
|
+
transformed = transformed.replace(baseClassInternalAssignPatternMultiline, (match, internalVar) => {
|
|
2367
|
+
transformCount++;
|
|
2368
|
+
const objContent = match.replace(/^this\[\w+\]\s*=\s*/, "");
|
|
2369
|
+
const patchedAssignment = `Object.defineProperty(this,${internalVar},{value:${objContent},writable:true,enumerable:true,configurable:true});var __self=this;Object.defineProperty(this,"nextUrl",{get:function(){return __self[${internalVar}]?__self[${internalVar}].nextUrl:undefined},enumerable:true,configurable:true});Object.defineProperty(this,"cookies",{get:function(){return __self[${internalVar}]?__self[${internalVar}].cookies:undefined},enumerable:true,configurable:true});Object.defineProperty(this,"geo",{get:function(){return __self[${internalVar}]?__self[${internalVar}].geo:undefined},enumerable:true,configurable:true});Object.defineProperty(this,"ip",{get:function(){return __self[${internalVar}]?__self[${internalVar}].ip:undefined},enumerable:true,configurable:true})`;
|
|
2363
2370
|
return patchedAssignment;
|
|
2364
2371
|
});
|
|
2365
2372
|
const responseInternalAssignPattern = /this\[(\w+)\]\s*=\s*\{\s*cookies\s*:\s*(\w+)\s*,\s*url\s*:\s*([^}]+)\}/g;
|
|
@@ -2961,6 +2968,230 @@ function getExternalMock(id) {
|
|
|
2961
2968
|
};
|
|
2962
2969
|
}
|
|
2963
2970
|
|
|
2971
|
+
// OpenTelemetry API mock - Next.js 16+ \u4F7F\u7528 OpenTelemetry \u8FDB\u884C\u8FFD\u8E2A
|
|
2972
|
+
if (id.includes('opentelemetry') || id.includes('@opentelemetry/api')) {
|
|
2973
|
+
// createContextKey - \u521B\u5EFA\u4E0A\u4E0B\u6587\u952E
|
|
2974
|
+
const createContextKey = (name) => Symbol.for(name);
|
|
2975
|
+
|
|
2976
|
+
// ROOT_CONTEXT - \u6839\u4E0A\u4E0B\u6587
|
|
2977
|
+
class Context {
|
|
2978
|
+
constructor(parentContext) {
|
|
2979
|
+
this._currentContext = parentContext ? new Map(parentContext._currentContext) : new Map();
|
|
2980
|
+
}
|
|
2981
|
+
getValue(key) { return this._currentContext.get(key); }
|
|
2982
|
+
setValue(key, value) {
|
|
2983
|
+
const ctx = new Context(this);
|
|
2984
|
+
ctx._currentContext.set(key, value);
|
|
2985
|
+
return ctx;
|
|
2986
|
+
}
|
|
2987
|
+
deleteValue(key) {
|
|
2988
|
+
const ctx = new Context(this);
|
|
2989
|
+
ctx._currentContext.delete(key);
|
|
2990
|
+
return ctx;
|
|
2991
|
+
}
|
|
2992
|
+
}
|
|
2993
|
+
const ROOT_CONTEXT = new Context();
|
|
2994
|
+
|
|
2995
|
+
// NoopContextManager - \u7A7A\u64CD\u4F5C\u4E0A\u4E0B\u6587\u7BA1\u7406\u5668
|
|
2996
|
+
class NoopContextManager {
|
|
2997
|
+
active() { return ROOT_CONTEXT; }
|
|
2998
|
+
with(context, fn, thisArg, ...args) { return fn.call(thisArg, ...args); }
|
|
2999
|
+
bind(context, target) { return target; }
|
|
3000
|
+
enable() { return this; }
|
|
3001
|
+
disable() { return this; }
|
|
3002
|
+
}
|
|
3003
|
+
|
|
3004
|
+
// NoopSpan - \u7A7A\u64CD\u4F5C Span
|
|
3005
|
+
class NoopSpan {
|
|
3006
|
+
constructor() {}
|
|
3007
|
+
spanContext() { return { traceId: '', spanId: '', traceFlags: 0 }; }
|
|
3008
|
+
setAttribute() { return this; }
|
|
3009
|
+
setAttributes() { return this; }
|
|
3010
|
+
addEvent() { return this; }
|
|
3011
|
+
setStatus() { return this; }
|
|
3012
|
+
updateName() { return this; }
|
|
3013
|
+
end() {}
|
|
3014
|
+
isRecording() { return false; }
|
|
3015
|
+
recordException() {}
|
|
3016
|
+
}
|
|
3017
|
+
|
|
3018
|
+
// NoopTracer - \u7A7A\u64CD\u4F5C\u8FFD\u8E2A\u5668
|
|
3019
|
+
class NoopTracer {
|
|
3020
|
+
startSpan() { return new NoopSpan(); }
|
|
3021
|
+
startActiveSpan(name, ...args) {
|
|
3022
|
+
const span = new NoopSpan();
|
|
3023
|
+
const fn = args[args.length - 1];
|
|
3024
|
+
if (typeof fn === 'function') {
|
|
3025
|
+
return fn(span);
|
|
3026
|
+
}
|
|
3027
|
+
return span;
|
|
3028
|
+
}
|
|
3029
|
+
}
|
|
3030
|
+
|
|
3031
|
+
// NoopTracerProvider - \u7A7A\u64CD\u4F5C\u8FFD\u8E2A\u5668\u63D0\u4F9B\u8005
|
|
3032
|
+
class NoopTracerProvider {
|
|
3033
|
+
getTracer() { return new NoopTracer(); }
|
|
3034
|
+
}
|
|
3035
|
+
|
|
3036
|
+
// NoopMeter - \u7A7A\u64CD\u4F5C\u8BA1\u91CF\u5668
|
|
3037
|
+
class NoopMeter {
|
|
3038
|
+
createCounter() { return { add: () => {} }; }
|
|
3039
|
+
createHistogram() { return { record: () => {} }; }
|
|
3040
|
+
createUpDownCounter() { return { add: () => {} }; }
|
|
3041
|
+
createObservableGauge() { return { addCallback: () => {} }; }
|
|
3042
|
+
createObservableCounter() { return { addCallback: () => {} }; }
|
|
3043
|
+
createObservableUpDownCounter() { return { addCallback: () => {} }; }
|
|
3044
|
+
}
|
|
3045
|
+
|
|
3046
|
+
// NoopMeterProvider - \u7A7A\u64CD\u4F5C\u8BA1\u91CF\u5668\u63D0\u4F9B\u8005
|
|
3047
|
+
class NoopMeterProvider {
|
|
3048
|
+
getMeter() { return new NoopMeter(); }
|
|
3049
|
+
}
|
|
3050
|
+
|
|
3051
|
+
// DiagLogLevel \u679A\u4E3E
|
|
3052
|
+
const DiagLogLevel = {
|
|
3053
|
+
NONE: 0,
|
|
3054
|
+
ERROR: 30,
|
|
3055
|
+
WARN: 50,
|
|
3056
|
+
INFO: 60,
|
|
3057
|
+
DEBUG: 70,
|
|
3058
|
+
VERBOSE: 80,
|
|
3059
|
+
ALL: 9999
|
|
3060
|
+
};
|
|
3061
|
+
|
|
3062
|
+
// DiagConsoleLogger - \u63A7\u5236\u53F0\u8BCA\u65AD\u65E5\u5FD7\u8BB0\u5F55\u5668
|
|
3063
|
+
class DiagConsoleLogger {
|
|
3064
|
+
error(...args) { console.error('[OTel]', ...args); }
|
|
3065
|
+
warn(...args) { console.warn('[OTel]', ...args); }
|
|
3066
|
+
info(...args) { console.info('[OTel]', ...args); }
|
|
3067
|
+
debug(...args) { console.debug('[OTel]', ...args); }
|
|
3068
|
+
verbose(...args) { console.log('[OTel]', ...args); }
|
|
3069
|
+
}
|
|
3070
|
+
|
|
3071
|
+
// API \u5355\u4F8B
|
|
3072
|
+
const contextAPI = {
|
|
3073
|
+
active: () => ROOT_CONTEXT,
|
|
3074
|
+
with: (ctx, fn, thisArg, ...args) => fn.call(thisArg, ...args),
|
|
3075
|
+
bind: (ctx, target) => target,
|
|
3076
|
+
setGlobalContextManager: () => true,
|
|
3077
|
+
disable: () => {}
|
|
3078
|
+
};
|
|
3079
|
+
|
|
3080
|
+
const traceAPI = {
|
|
3081
|
+
getTracer: () => new NoopTracer(),
|
|
3082
|
+
getTracerProvider: () => new NoopTracerProvider(),
|
|
3083
|
+
setGlobalTracerProvider: () => new NoopTracerProvider(),
|
|
3084
|
+
getSpan: () => undefined,
|
|
3085
|
+
getActiveSpan: () => undefined,
|
|
3086
|
+
getSpanContext: () => undefined, // \u83B7\u53D6 span \u4E0A\u4E0B\u6587
|
|
3087
|
+
setSpan: (ctx) => ctx,
|
|
3088
|
+
setSpanContext: (ctx) => ctx, // \u8BBE\u7F6E span \u4E0A\u4E0B\u6587
|
|
3089
|
+
deleteSpan: (ctx) => ctx,
|
|
3090
|
+
isSpanContextValid: () => false,
|
|
3091
|
+
wrapSpanContext: (spanContext) => new NoopSpan()
|
|
3092
|
+
};
|
|
3093
|
+
|
|
3094
|
+
const metricsAPI = {
|
|
3095
|
+
getMeter: () => new NoopMeter(),
|
|
3096
|
+
getMeterProvider: () => new NoopMeterProvider(),
|
|
3097
|
+
setGlobalMeterProvider: () => new NoopMeterProvider()
|
|
3098
|
+
};
|
|
3099
|
+
|
|
3100
|
+
const propagationAPI = {
|
|
3101
|
+
inject: () => {},
|
|
3102
|
+
extract: (ctx) => ctx,
|
|
3103
|
+
fields: () => [],
|
|
3104
|
+
setGlobalPropagator: () => true,
|
|
3105
|
+
createBaggage: () => ({
|
|
3106
|
+
getEntry: () => undefined,
|
|
3107
|
+
getAllEntries: () => [],
|
|
3108
|
+
setEntry: () => ({}),
|
|
3109
|
+
removeEntry: () => ({}),
|
|
3110
|
+
clear: () => ({})
|
|
3111
|
+
}),
|
|
3112
|
+
getBaggage: () => undefined,
|
|
3113
|
+
setBaggage: (ctx) => ctx,
|
|
3114
|
+
deleteBaggage: (ctx) => ctx,
|
|
3115
|
+
getActiveBaggage: () => undefined
|
|
3116
|
+
};
|
|
3117
|
+
|
|
3118
|
+
const diagAPI = {
|
|
3119
|
+
setLogger: () => {},
|
|
3120
|
+
disable: () => {},
|
|
3121
|
+
createComponentLogger: () => new DiagConsoleLogger(),
|
|
3122
|
+
verbose: () => {},
|
|
3123
|
+
debug: () => {},
|
|
3124
|
+
info: () => {},
|
|
3125
|
+
warn: () => {},
|
|
3126
|
+
error: () => {}
|
|
3127
|
+
};
|
|
3128
|
+
|
|
3129
|
+
// \u5B8C\u6574\u7684 OpenTelemetry API mock
|
|
3130
|
+
const result = {
|
|
3131
|
+
// \u4E0A\u4E0B\u6587
|
|
3132
|
+
createContextKey,
|
|
3133
|
+
ROOT_CONTEXT,
|
|
3134
|
+
Context,
|
|
3135
|
+
context: contextAPI,
|
|
3136
|
+
ContextAPI: { getInstance: () => contextAPI },
|
|
3137
|
+
NoopContextManager,
|
|
3138
|
+
|
|
3139
|
+
// \u8FFD\u8E2A
|
|
3140
|
+
trace: traceAPI,
|
|
3141
|
+
TraceAPI: { getInstance: () => traceAPI },
|
|
3142
|
+
SpanKind: { INTERNAL: 0, SERVER: 1, CLIENT: 2, PRODUCER: 3, CONSUMER: 4 },
|
|
3143
|
+
SpanStatusCode: { UNSET: 0, OK: 1, ERROR: 2 },
|
|
3144
|
+
TraceFlags: { NONE: 0, SAMPLED: 1 },
|
|
3145
|
+
isSpanContextValid: () => false,
|
|
3146
|
+
isValidTraceId: () => false,
|
|
3147
|
+
isValidSpanId: () => false,
|
|
3148
|
+
INVALID_SPANID: '',
|
|
3149
|
+
INVALID_TRACEID: '',
|
|
3150
|
+
INVALID_SPAN_CONTEXT: { traceId: '', spanId: '', traceFlags: 0 },
|
|
3151
|
+
NoopTracer,
|
|
3152
|
+
NoopTracerProvider,
|
|
3153
|
+
NoopSpan,
|
|
3154
|
+
|
|
3155
|
+
// \u8BA1\u91CF
|
|
3156
|
+
metrics: metricsAPI,
|
|
3157
|
+
MetricsAPI: { getInstance: () => metricsAPI },
|
|
3158
|
+
ValueType: { INT: 0, DOUBLE: 1 },
|
|
3159
|
+
NoopMeter,
|
|
3160
|
+
NoopMeterProvider,
|
|
3161
|
+
|
|
3162
|
+
// \u4F20\u64AD
|
|
3163
|
+
propagation: propagationAPI,
|
|
3164
|
+
PropagationAPI: { getInstance: () => propagationAPI },
|
|
3165
|
+
|
|
3166
|
+
// \u8BCA\u65AD
|
|
3167
|
+
diag: diagAPI,
|
|
3168
|
+
DiagAPI: { getInstance: () => diagAPI },
|
|
3169
|
+
DiagLogLevel,
|
|
3170
|
+
DiagConsoleLogger,
|
|
3171
|
+
|
|
3172
|
+
// Baggage
|
|
3173
|
+
baggageEntryMetadataFromString: () => ({ toString: () => '' }),
|
|
3174
|
+
createBaggage: () => ({
|
|
3175
|
+
getEntry: () => undefined,
|
|
3176
|
+
getAllEntries: () => [],
|
|
3177
|
+
setEntry: () => ({}),
|
|
3178
|
+
removeEntry: () => ({}),
|
|
3179
|
+
clear: () => ({})
|
|
3180
|
+
}),
|
|
3181
|
+
|
|
3182
|
+
// \u9ED8\u8BA4\u5BFC\u51FA
|
|
3183
|
+
default: {
|
|
3184
|
+
context: contextAPI,
|
|
3185
|
+
trace: traceAPI,
|
|
3186
|
+
metrics: metricsAPI,
|
|
3187
|
+
propagation: propagationAPI,
|
|
3188
|
+
diag: diagAPI
|
|
3189
|
+
}
|
|
3190
|
+
};
|
|
3191
|
+
Object.defineProperty(result, '__esModule', { value: true });
|
|
3192
|
+
return result;
|
|
3193
|
+
}
|
|
3194
|
+
|
|
2964
3195
|
// \u901A\u7528 mock
|
|
2965
3196
|
const result = {};
|
|
2966
3197
|
Object.defineProperty(result, '__esModule', { value: true });
|
|
@@ -32,9 +32,8 @@ var compileMiddleware = async (ctx) => {
|
|
|
32
32
|
}
|
|
33
33
|
}
|
|
34
34
|
if (!middlewareFilePath) {
|
|
35
|
-
console.
|
|
36
|
-
|
|
37
|
-
throw new Error("middleware.js not found");
|
|
35
|
+
console.log("[Middleware Compiler] No middleware.js found, skipping middleware compilation.");
|
|
36
|
+
return null;
|
|
38
37
|
}
|
|
39
38
|
const result = await compile(middlewareFilePath, {
|
|
40
39
|
env: { DEBUG: "true" }
|
|
@@ -292,48 +292,36 @@ async function executeMiddleware({request}) {
|
|
|
292
292
|
// \u83B7\u53D6 middleware \u51FD\u6570
|
|
293
293
|
const middlewareFn = await getMiddleware();
|
|
294
294
|
|
|
295
|
-
// \
|
|
296
|
-
//
|
|
297
|
-
const
|
|
295
|
+
// \u5C06\u539F\u59CB Headers \u8F6C\u6362\u4E3A\u666E\u901A\u5BF9\u8C61\u683C\u5F0F
|
|
296
|
+
// Next.js adapter \u5185\u90E8\u7684 fromNodeOutgoingHttpHeaders \u671F\u671B\u5BF9\u8C61\u683C\u5F0F
|
|
297
|
+
const headersObject = {};
|
|
298
|
+
request.headers.forEach((value, key) => {
|
|
299
|
+
headersObject[key] = value;
|
|
300
|
+
});
|
|
298
301
|
|
|
299
|
-
|
|
302
|
+
// \u6784\u9020 Next.js middleware adapter \u671F\u671B\u7684\u53C2\u6570\u683C\u5F0F
|
|
303
|
+
// \u53C2\u8003 Next.js \u6E90\u7801\u4E2D\u7684 adapter \u51FD\u6570
|
|
304
|
+
const middlewareParams = {
|
|
300
305
|
request: {
|
|
301
306
|
url: request.url,
|
|
302
307
|
method: request.method,
|
|
303
|
-
headers:
|
|
308
|
+
headers: headersObject,
|
|
304
309
|
body: request.body,
|
|
305
310
|
nextConfig: {
|
|
306
311
|
basePath: '',
|
|
307
312
|
i18n: null,
|
|
308
313
|
trailingSlash: false
|
|
309
314
|
},
|
|
310
|
-
|
|
311
|
-
|
|
315
|
+
geo: {},
|
|
316
|
+
ip: request.headers.get('x-forwarded-for') || request.headers.get('x-real-ip') || '',
|
|
317
|
+
signal: request.signal || null
|
|
312
318
|
},
|
|
313
319
|
page: '/',
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
// \u6DFB\u52A0 rewrite \u65B9\u6CD5 - \u7F16\u8BD1\u540E\u7684 middleware \u9700\u8981\u8C03\u7528\u6B64\u65B9\u6CD5
|
|
317
|
-
rewrite: (url) => {
|
|
318
|
-
const headers = new Headers();
|
|
319
|
-
headers.set("x-middleware-rewrite", typeof url === 'string' ? url : url.toString());
|
|
320
|
-
return new Response(null, { headers });
|
|
321
|
-
},
|
|
322
|
-
// \u6DFB\u52A0 redirect \u65B9\u6CD5
|
|
323
|
-
redirect: (url, status = 307) => {
|
|
324
|
-
return new Response(null, {
|
|
325
|
-
status,
|
|
326
|
-
headers: { Location: typeof url === 'string' ? url : url.toString() }
|
|
327
|
-
});
|
|
328
|
-
},
|
|
329
|
-
// \u6DFB\u52A0 next \u65B9\u6CD5
|
|
330
|
-
next: () => {
|
|
331
|
-
return new Response(null, { headers: { 'x-middleware-next': '1' } });
|
|
332
|
-
},
|
|
333
|
-
IncrementalCache: null
|
|
320
|
+
// \u6DFB\u52A0 waitUntil \u65B9\u6CD5
|
|
321
|
+
waitUntil: (promise) => {}
|
|
334
322
|
};
|
|
335
323
|
|
|
336
|
-
const result = await middlewareFn(
|
|
324
|
+
const result = await middlewareFn(middlewareParams);
|
|
337
325
|
|
|
338
326
|
// Webpack \u6A21\u5F0F\u8FD4\u56DE\u7684\u662F { response: Response, waitUntil: {} } \u683C\u5F0F
|
|
339
327
|
// \u9700\u8981\u63D0\u53D6\u5B9E\u9645\u7684 Response \u5BF9\u8C61
|
|
@@ -100,6 +100,8 @@ var getHandlerFile = async (ctx) => {
|
|
|
100
100
|
};
|
|
101
101
|
if (ctx.relativeAppDir.length !== 0) {
|
|
102
102
|
const template2 = await readFile(join(templatesDir, "handler-monorepo.tmpl.js"), "utf-8");
|
|
103
|
+
console.log("ctx.lambdaWorkingDirectory", ctx.lambdaWorkingDirectory);
|
|
104
|
+
console.log("ctx.nextServerHandler", ctx.nextServerHandler);
|
|
103
105
|
templateVariables["{{cwd}}"] = posixJoin(ctx.lambdaWorkingDirectory);
|
|
104
106
|
templateVariables["{{nextServerHandler}}"] = posixJoin(ctx.nextServerHandler);
|
|
105
107
|
return applyTemplateVariables(template2, templateVariables);
|
package/dist/build/routes.js
CHANGED
|
@@ -9,6 +9,105 @@ import "../esm-chunks/chunk-6BT4RYQJ.js";
|
|
|
9
9
|
// src/build/routes.ts
|
|
10
10
|
import * as fs from "fs";
|
|
11
11
|
import * as path from "path";
|
|
12
|
+
function isRE2Compatible(regexSource) {
|
|
13
|
+
const unsupported = [
|
|
14
|
+
/\(\?[=!<]/,
|
|
15
|
+
// 断言(前瞻、后顾)
|
|
16
|
+
/\(\?>/,
|
|
17
|
+
// 原子组
|
|
18
|
+
/\\(\d+)/,
|
|
19
|
+
// 反向引用 \1 \2
|
|
20
|
+
/\(\?\(/
|
|
21
|
+
// 条件表达式
|
|
22
|
+
];
|
|
23
|
+
return !unsupported.some((r) => r.test(regexSource));
|
|
24
|
+
}
|
|
25
|
+
async function getMiddlewareConfig(ctx) {
|
|
26
|
+
try {
|
|
27
|
+
const manifest = await ctx.getMiddlewareManifest();
|
|
28
|
+
if (manifest && manifest.middleware && manifest.middleware["/"]) {
|
|
29
|
+
const middlewareInfo = manifest.middleware["/"];
|
|
30
|
+
const matchers = middlewareInfo.matchers || [];
|
|
31
|
+
const normalizedMatchers = matchers.map((m) => ({
|
|
32
|
+
source: m.originalSource || "/:path*",
|
|
33
|
+
regex: m.regexp
|
|
34
|
+
}));
|
|
35
|
+
const hasIncompatibleRegex = normalizedMatchers.some(
|
|
36
|
+
(item) => item.regex && !isRE2Compatible(item.regex)
|
|
37
|
+
);
|
|
38
|
+
if (hasIncompatibleRegex) {
|
|
39
|
+
return {
|
|
40
|
+
runtime: "edge",
|
|
41
|
+
matcher: [{ source: "/:path*" }]
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
return {
|
|
45
|
+
runtime: "edge",
|
|
46
|
+
matcher: normalizedMatchers.map((item) => ({ source: item.source }))
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
const possibleFunctionsConfigPaths = [
|
|
50
|
+
path.join(process.cwd(), ".next/server/functions-config-manifest.json"),
|
|
51
|
+
path.join(ctx.distDir, "server/functions-config-manifest.json")
|
|
52
|
+
];
|
|
53
|
+
let functionsConfigPath = "";
|
|
54
|
+
for (const p of possibleFunctionsConfigPaths) {
|
|
55
|
+
if (fs.existsSync(p)) {
|
|
56
|
+
functionsConfigPath = p;
|
|
57
|
+
break;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
if (functionsConfigPath) {
|
|
61
|
+
const functionsConfig = JSON.parse(fs.readFileSync(functionsConfigPath, "utf-8"));
|
|
62
|
+
const middlewareConfig = functionsConfig?.functions?.["/_middleware"];
|
|
63
|
+
if (middlewareConfig && middlewareConfig.matchers) {
|
|
64
|
+
const matchers = middlewareConfig.matchers;
|
|
65
|
+
const normalizedMatchers = matchers.map((m) => ({
|
|
66
|
+
source: m.originalSource || "/:path*",
|
|
67
|
+
regex: m.regexp
|
|
68
|
+
}));
|
|
69
|
+
const hasIncompatibleRegex = normalizedMatchers.some(
|
|
70
|
+
(item) => item.regex && !isRE2Compatible(item.regex)
|
|
71
|
+
);
|
|
72
|
+
if (hasIncompatibleRegex) {
|
|
73
|
+
return {
|
|
74
|
+
runtime: middlewareConfig.runtime || "edge",
|
|
75
|
+
matcher: [{ source: "/:path*" }]
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
return {
|
|
79
|
+
runtime: middlewareConfig.runtime || "edge",
|
|
80
|
+
matcher: normalizedMatchers.map((item) => ({ source: item.source }))
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return null;
|
|
85
|
+
} catch (error) {
|
|
86
|
+
console.log("[Middleware] Failed to read middleware config:", error);
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
function updateEdgeFunctionsMetaJson(middlewareConfig) {
|
|
91
|
+
const metaJsonPath = path.join(process.cwd(), ".edgeone/edge-functions/meta.json");
|
|
92
|
+
let meta = { routes: [] };
|
|
93
|
+
if (fs.existsSync(metaJsonPath)) {
|
|
94
|
+
try {
|
|
95
|
+
const content = fs.readFileSync(metaJsonPath, "utf-8");
|
|
96
|
+
meta = JSON.parse(content);
|
|
97
|
+
} catch (error) {
|
|
98
|
+
console.log("[Middleware] Failed to parse existing meta.json, creating new one");
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
if (middlewareConfig) {
|
|
102
|
+
meta.middleware = middlewareConfig;
|
|
103
|
+
console.log("[Middleware] Updated meta.json with middleware config:", JSON.stringify(middlewareConfig.matcher));
|
|
104
|
+
}
|
|
105
|
+
const edgeFunctionsDir = path.dirname(metaJsonPath);
|
|
106
|
+
if (!fs.existsSync(edgeFunctionsDir)) {
|
|
107
|
+
fs.mkdirSync(edgeFunctionsDir, { recursive: true });
|
|
108
|
+
}
|
|
109
|
+
fs.writeFileSync(metaJsonPath, JSON.stringify(meta, null, 2), "utf-8");
|
|
110
|
+
}
|
|
12
111
|
var convertNextRoutePattern = (path2) => {
|
|
13
112
|
if (!path2.includes("[")) {
|
|
14
113
|
return path2;
|
|
@@ -97,7 +196,7 @@ var createRouteMeta = async (ctx) => {
|
|
|
97
196
|
path: path2,
|
|
98
197
|
...config
|
|
99
198
|
}));
|
|
100
|
-
const serverHandlerDir = ctx.
|
|
199
|
+
const serverHandlerDir = ctx.serverHandlerRootDir;
|
|
101
200
|
if (!fs.existsSync(serverHandlerDir)) {
|
|
102
201
|
fs.mkdirSync(serverHandlerDir, { recursive: true });
|
|
103
202
|
}
|
|
@@ -110,6 +209,8 @@ var createRouteMeta = async (ctx) => {
|
|
|
110
209
|
JSON.stringify(metaData, null, 2),
|
|
111
210
|
"utf-8"
|
|
112
211
|
);
|
|
212
|
+
const middlewareConfig = await getMiddlewareConfig(ctx);
|
|
213
|
+
updateEdgeFunctionsMetaJson(middlewareConfig);
|
|
113
214
|
};
|
|
114
215
|
export {
|
|
115
216
|
convertNextRoutePattern,
|
package/dist/index.js
CHANGED
|
@@ -36,13 +36,14 @@ var onBuild = async (options) => {
|
|
|
36
36
|
// copyStaticAssets(ctx),
|
|
37
37
|
copyStaticContent(ctx),
|
|
38
38
|
copyPrerenderedContent(ctx),
|
|
39
|
-
createServerHandler(ctx)
|
|
40
|
-
createRouteMeta(ctx)
|
|
39
|
+
createServerHandler(ctx)
|
|
41
40
|
]);
|
|
41
|
+
await createRouteMeta(ctx);
|
|
42
42
|
};
|
|
43
43
|
var onPostBuild = async (options) => {
|
|
44
|
+
console.log("onPostBuild");
|
|
44
45
|
const ctx = new PluginContext(options);
|
|
45
|
-
compileMiddleware(ctx);
|
|
46
|
+
await compileMiddleware(ctx);
|
|
46
47
|
};
|
|
47
48
|
export {
|
|
48
49
|
onBuild,
|
|
@@ -28,7 +28,7 @@ module.exports = __toCommonJS(tags_handler_exports);
|
|
|
28
28
|
|
|
29
29
|
// package.json
|
|
30
30
|
var name = "@edgeone/opennextjs-pages";
|
|
31
|
-
var version = "0.0
|
|
31
|
+
var version = "0.1.0";
|
|
32
32
|
|
|
33
33
|
// src/run/handlers/tags-handler.cts
|
|
34
34
|
var import_request_context = require("./request-context.cjs");
|