@ereo/core 0.2.36 → 0.2.38
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/app.d.ts +4 -0
- package/dist/app.d.ts.map +1 -1
- package/dist/context.d.ts.map +1 -1
- package/dist/index.js +78 -11
- package/dist/plugin.d.ts +1 -0
- package/dist/plugin.d.ts.map +1 -1
- package/package.json +1 -1
package/dist/app.d.ts
CHANGED
|
@@ -24,6 +24,8 @@ export declare class EreoApp implements Application {
|
|
|
24
24
|
private pluginRegistry;
|
|
25
25
|
private middlewares;
|
|
26
26
|
private routeMatcher;
|
|
27
|
+
private static readonly METHOD_OVERRIDE_HEADER;
|
|
28
|
+
private static readonly METHOD_OVERRIDE_ALLOWED;
|
|
27
29
|
constructor(options?: ApplicationOptions);
|
|
28
30
|
/**
|
|
29
31
|
* Deep merge configuration objects.
|
|
@@ -62,6 +64,8 @@ export declare class EreoApp implements Application {
|
|
|
62
64
|
* Handle a route request.
|
|
63
65
|
*/
|
|
64
66
|
private handleRoute;
|
|
67
|
+
private normalizeBasePath;
|
|
68
|
+
private getEffectiveMethod;
|
|
65
69
|
/**
|
|
66
70
|
* Safely serialize data as a JSON response.
|
|
67
71
|
*/
|
package/dist/app.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"app.d.ts","sourceRoot":"","sources":["../src/app.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EACV,WAAW,EACX,kBAAkB,EAClB,eAAe,EACf,MAAM,EACN,KAAK,EACL,UAAU,EACV,iBAAiB,EAClB,MAAM,SAAS,CAAC;AAGjB,OAAO,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAsB1C;;GAEG;AACH,wBAAgB,SAAS,CAAC,OAAO,GAAE,kBAAuB,GAAG,OAAO,CAEnE;AAED;;GAEG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,eAAe,GAAG,eAAe,CAErE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AE,MAAM,GAAG,OAAO;IAIzB;;OAEG;IACH,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO;IAI5B;;OAEG;IACH,uBAAuB,IAAI,MAAM,GAAG,IAAI;IAuBxC;;OAEG;IACH,eAAe,CAAC,QAAQ,EAAE,QAAQ,GAAG,QAAQ;CA8B9C;AAED;;GAEG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,cAAc,CAExE;AAQD,wBAAgB,aAAa,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,cAAc,GAAG,IAAI,CAE7E;AAED,wBAAgB,UAAU,CAAC,OAAO,EAAE,OAAO,GAAG,cAAc,GAAG,SAAS,CAEvE"}
|
package/dist/index.js
CHANGED
|
@@ -28,7 +28,11 @@ class RequestContext {
|
|
|
28
28
|
cookieMap = new Map;
|
|
29
29
|
setCookieHeaders = [];
|
|
30
30
|
constructor(request) {
|
|
31
|
-
|
|
31
|
+
try {
|
|
32
|
+
this.url = new URL(request.url);
|
|
33
|
+
} catch {
|
|
34
|
+
this.url = new URL("http://localhost/");
|
|
35
|
+
}
|
|
32
36
|
this.env = typeof process !== "undefined" ? process.env : {};
|
|
33
37
|
this.responseHeaders = new Headers;
|
|
34
38
|
const cookieHeader = request.headers.get("Cookie");
|
|
@@ -97,7 +101,8 @@ class RequestContext {
|
|
|
97
101
|
parts.push(`Path=${options?.path ?? "/"}`);
|
|
98
102
|
if (options?.domain)
|
|
99
103
|
parts.push(`Domain=${options.domain}`);
|
|
100
|
-
|
|
104
|
+
if (options?.httpOnly !== false)
|
|
105
|
+
parts.push("HttpOnly");
|
|
101
106
|
this.setCookieHeaders.push(parts.join("; "));
|
|
102
107
|
},
|
|
103
108
|
has: (name) => {
|
|
@@ -182,10 +187,10 @@ class PluginRegistry {
|
|
|
182
187
|
console.warn(`Plugin "${plugin.name}" is already registered, skipping duplicate`);
|
|
183
188
|
return;
|
|
184
189
|
}
|
|
185
|
-
this.plugins.push(plugin);
|
|
186
190
|
if (plugin.setup) {
|
|
187
191
|
await plugin.setup(this.context);
|
|
188
192
|
}
|
|
193
|
+
this.plugins.push(plugin);
|
|
189
194
|
}
|
|
190
195
|
async registerAll(plugins) {
|
|
191
196
|
for (const plugin of plugins) {
|
|
@@ -257,11 +262,20 @@ class PluginRegistry {
|
|
|
257
262
|
}
|
|
258
263
|
}
|
|
259
264
|
async buildEnd() {
|
|
265
|
+
let firstError = null;
|
|
260
266
|
for (const plugin of this.plugins) {
|
|
261
267
|
if (plugin.buildEnd) {
|
|
262
|
-
|
|
268
|
+
try {
|
|
269
|
+
await plugin.buildEnd();
|
|
270
|
+
} catch (error) {
|
|
271
|
+
console.error(`Plugin "${plugin.name}" buildEnd failed:`, error);
|
|
272
|
+
if (!firstError)
|
|
273
|
+
firstError = error;
|
|
274
|
+
}
|
|
263
275
|
}
|
|
264
276
|
}
|
|
277
|
+
if (firstError)
|
|
278
|
+
throw firstError;
|
|
265
279
|
}
|
|
266
280
|
}
|
|
267
281
|
function definePlugin(plugin) {
|
|
@@ -326,11 +340,20 @@ function composePlugins(name, plugins) {
|
|
|
326
340
|
}
|
|
327
341
|
},
|
|
328
342
|
async buildEnd() {
|
|
343
|
+
let firstError = null;
|
|
329
344
|
for (const plugin of plugins) {
|
|
330
345
|
if (plugin.buildEnd) {
|
|
331
|
-
|
|
346
|
+
try {
|
|
347
|
+
await plugin.buildEnd();
|
|
348
|
+
} catch (error) {
|
|
349
|
+
console.error(`Composed plugin "${plugin.name}" buildEnd failed:`, error);
|
|
350
|
+
if (!firstError)
|
|
351
|
+
firstError = error;
|
|
352
|
+
}
|
|
332
353
|
}
|
|
333
354
|
}
|
|
355
|
+
if (firstError)
|
|
356
|
+
throw firstError;
|
|
334
357
|
}
|
|
335
358
|
};
|
|
336
359
|
}
|
|
@@ -373,6 +396,8 @@ class EreoApp {
|
|
|
373
396
|
pluginRegistry;
|
|
374
397
|
middlewares = [];
|
|
375
398
|
routeMatcher = null;
|
|
399
|
+
static METHOD_OVERRIDE_HEADER = "_method";
|
|
400
|
+
static METHOD_OVERRIDE_ALLOWED = new Set(["PUT", "PATCH", "DELETE"]);
|
|
376
401
|
constructor(options = {}) {
|
|
377
402
|
this.config = this.mergeConfig(defaultConfig, options.config || {});
|
|
378
403
|
const mode = this.config.server?.development ? "development" : "production";
|
|
@@ -418,15 +443,24 @@ class EreoApp {
|
|
|
418
443
|
});
|
|
419
444
|
return context.applyToResponse(response);
|
|
420
445
|
} catch (error) {
|
|
421
|
-
|
|
446
|
+
const errorResponse = this.handleError(error, context);
|
|
447
|
+
return context.applyToResponse(errorResponse);
|
|
422
448
|
}
|
|
423
449
|
}
|
|
424
450
|
async runMiddleware(request, context, final) {
|
|
425
451
|
let index = 0;
|
|
426
452
|
const next = async () => {
|
|
427
453
|
if (index < this.middlewares.length) {
|
|
428
|
-
const
|
|
429
|
-
|
|
454
|
+
const currentIndex = index++;
|
|
455
|
+
const middleware = this.middlewares[currentIndex];
|
|
456
|
+
let called = false;
|
|
457
|
+
return middleware(request, context, async () => {
|
|
458
|
+
if (called) {
|
|
459
|
+
throw new Error("next() called multiple times in middleware");
|
|
460
|
+
}
|
|
461
|
+
called = true;
|
|
462
|
+
return next();
|
|
463
|
+
});
|
|
430
464
|
}
|
|
431
465
|
return final();
|
|
432
466
|
};
|
|
@@ -435,8 +469,13 @@ class EreoApp {
|
|
|
435
469
|
async handleRoute(request, context) {
|
|
436
470
|
const url = new URL(request.url);
|
|
437
471
|
let pathname = url.pathname;
|
|
438
|
-
|
|
439
|
-
|
|
472
|
+
const effectiveMethod = await this.getEffectiveMethod(request);
|
|
473
|
+
const normalizedBasePath = this.normalizeBasePath(this.config.basePath);
|
|
474
|
+
if (normalizedBasePath && (pathname === normalizedBasePath || pathname.startsWith(normalizedBasePath + "/"))) {
|
|
475
|
+
pathname = pathname.slice(normalizedBasePath.length) || "/";
|
|
476
|
+
if (!pathname.startsWith("/")) {
|
|
477
|
+
pathname = "/" + pathname;
|
|
478
|
+
}
|
|
440
479
|
}
|
|
441
480
|
if (!this.routeMatcher) {
|
|
442
481
|
return new Response("Router not configured", { status: 500 });
|
|
@@ -449,7 +488,7 @@ class EreoApp {
|
|
|
449
488
|
return new Response("Route module not loaded", { status: 500 });
|
|
450
489
|
}
|
|
451
490
|
const module = match.route.module;
|
|
452
|
-
if (
|
|
491
|
+
if (effectiveMethod !== "GET" && effectiveMethod !== "HEAD") {
|
|
453
492
|
if (module.action) {
|
|
454
493
|
const actionData = await module.action({
|
|
455
494
|
request,
|
|
@@ -480,6 +519,34 @@ class EreoApp {
|
|
|
480
519
|
}
|
|
481
520
|
return this.jsonResponse({ loaderData, params: match.params });
|
|
482
521
|
}
|
|
522
|
+
normalizeBasePath(basePath) {
|
|
523
|
+
if (!basePath || basePath === "/") {
|
|
524
|
+
return "";
|
|
525
|
+
}
|
|
526
|
+
return basePath.replace(/\/{2,}/g, "/").replace(/\/+$/, "");
|
|
527
|
+
}
|
|
528
|
+
async getEffectiveMethod(request) {
|
|
529
|
+
const method = request.method.toUpperCase();
|
|
530
|
+
if (method !== "POST") {
|
|
531
|
+
return method;
|
|
532
|
+
}
|
|
533
|
+
const contentType = request.headers.get("Content-Type") || "";
|
|
534
|
+
const isFormSubmission = contentType.includes("application/x-www-form-urlencoded") || contentType.includes("multipart/form-data");
|
|
535
|
+
if (!isFormSubmission) {
|
|
536
|
+
return method;
|
|
537
|
+
}
|
|
538
|
+
try {
|
|
539
|
+
const formData = await request.clone().formData();
|
|
540
|
+
const override = formData.get(EreoApp.METHOD_OVERRIDE_HEADER);
|
|
541
|
+
if (typeof override !== "string") {
|
|
542
|
+
return method;
|
|
543
|
+
}
|
|
544
|
+
const normalized = override.toUpperCase();
|
|
545
|
+
return EreoApp.METHOD_OVERRIDE_ALLOWED.has(normalized) ? normalized : method;
|
|
546
|
+
} catch {
|
|
547
|
+
return method;
|
|
548
|
+
}
|
|
549
|
+
}
|
|
483
550
|
jsonResponse(data, status = 200) {
|
|
484
551
|
try {
|
|
485
552
|
return new Response(JSON.stringify(data), {
|
package/dist/plugin.d.ts
CHANGED
package/dist/plugin.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../src/plugin.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,MAAM,EAAiB,SAAS,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAEjF;;GAEG;AACH,qBAAa,cAAc;IACzB,OAAO,CAAC,OAAO,CAAgB;IAC/B,OAAO,CAAC,OAAO,CAAgB;gBAEnB,MAAM,EAAE,eAAe,EAAE,IAAI,EAAE,aAAa,GAAG,YAAY,EAAE,IAAI,EAAE,MAAM;IAIrF;;OAEG;IACG,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;
|
|
1
|
+
{"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../src/plugin.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,MAAM,EAAiB,SAAS,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAEjF;;GAEG;AACH,qBAAa,cAAc;IACzB,OAAO,CAAC,OAAO,CAAgB;IAC/B,OAAO,CAAC,OAAO,CAAgB;gBAEnB,MAAM,EAAE,eAAe,EAAE,IAAI,EAAE,aAAa,GAAG,YAAY,EAAE,IAAI,EAAE,MAAM;IAIrF;;OAEG;IACG,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAqB7C;;OAEG;IACG,WAAW,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAMnD;;OAEG;IACH,UAAU,IAAI,SAAS,MAAM,EAAE;IAI/B;;OAEG;IACH,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS;IAI3C;;OAEG;IACG,SAAS,CAAC,IAA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