@modern-js/server-core 2.67.2 → 2.67.3

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.
@@ -35,6 +35,7 @@ __export(node_exports, {
35
35
  });
36
36
  module.exports = __toCommonJS(node_exports);
37
37
  var import_node_http = require("node:http");
38
+ var import_cloneable_readable = __toESM(require("cloneable-readable"));
38
39
  var import_helper = require("./helper");
39
40
  var import_install = require("./polyfills/install");
40
41
  var import_stream = require("./polyfills/stream");
@@ -61,13 +62,36 @@ const createWebRequest = (req, res, body) => {
61
62
  };
62
63
  res.on("close", () => controller.abort("res closed"));
63
64
  const url = `http://${req.headers.host}${req.url}`;
64
- const fullUrl = new URL(url);
65
- if (body || !(method === "GET" || method === "HEAD") && fullUrl.searchParams.has("__loader") || fullUrl.searchParams.has("__pass_body") || req.headers["x-mf-micro"] || req.headers["x-rsc-action"] || req.headers["x-parse-through-body"]) {
66
- init.body = body !== null && body !== void 0 ? body : (0, import_stream.createReadableStreamFromReadable)(req);
65
+ const needsRequestBody = body || !(method === "GET" || method === "HEAD");
66
+ const cloneableReq = needsRequestBody ? (0, import_cloneable_readable.default)(req) : null;
67
+ if (needsRequestBody) {
68
+ if (body) {
69
+ init.body = body;
70
+ } else {
71
+ const stream = cloneableReq.clone();
72
+ init.body = (0, import_stream.createReadableStreamFromReadable)(stream);
73
+ }
67
74
  init.duplex = "half";
68
75
  }
69
- const request = new Request(url, init);
70
- return request;
76
+ const originalRequest = new Request(url, init);
77
+ if (needsRequestBody) {
78
+ return new Proxy(originalRequest, {
79
+ get(target, prop) {
80
+ if ([
81
+ "json",
82
+ "text",
83
+ "blob",
84
+ "arrayBuffer",
85
+ "formData",
86
+ "body"
87
+ ].includes(prop)) {
88
+ cloneableReq.resume();
89
+ }
90
+ return target[prop];
91
+ }
92
+ });
93
+ }
94
+ return originalRequest;
71
95
  };
72
96
  const sendResponse = async (response, res) => {
73
97
  var _response_headers_get;
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var context_exports = {};
20
+ __export(context_exports, {
21
+ run: () => run,
22
+ useHonoContext: () => useHonoContext
23
+ });
24
+ module.exports = __toCommonJS(context_exports);
25
+ var import_storage = require("./utils/storage");
26
+ const { run, useHonoContext } = (0, import_storage.createStorage)();
27
+ // Annotate the CommonJS export names for ESM import in node:
28
+ 0 && (module.exports = {
29
+ run,
30
+ useHonoContext
31
+ });
package/dist/cjs/index.js CHANGED
@@ -21,15 +21,19 @@ var src_exports = {};
21
21
  __export(src_exports, {
22
22
  AGGRED_DIR: () => import_constants.AGGRED_DIR,
23
23
  ErrorDigest: () => import_utils.ErrorDigest,
24
+ Hono: () => import_hono.Hono,
24
25
  createErrorHtml: () => import_utils.createErrorHtml,
25
26
  createServerBase: () => import_serverBase.createServerBase,
26
27
  getLoaderCtx: () => import_helper.getLoaderCtx,
27
- onError: () => import_utils.onError
28
+ onError: () => import_utils.onError,
29
+ useHonoContext: () => import_context.useHonoContext
28
30
  });
29
31
  module.exports = __toCommonJS(src_exports);
30
32
  var import_utils = require("./utils");
31
33
  var import_constants = require("./constants");
32
34
  var import_serverBase = require("./serverBase");
35
+ var import_context = require("./context");
36
+ var import_hono = require("hono");
33
37
  var import_helper = require("./helper");
34
38
  __reExport(src_exports, require("./plugins"), module.exports);
35
39
  __reExport(src_exports, require("./types/plugins"), module.exports);
@@ -41,10 +45,12 @@ __reExport(src_exports, require("./types/requestHandler"), module.exports);
41
45
  0 && (module.exports = {
42
46
  AGGRED_DIR,
43
47
  ErrorDigest,
48
+ Hono,
44
49
  createErrorHtml,
45
50
  createServerBase,
46
51
  getLoaderCtx,
47
52
  onError,
53
+ useHonoContext,
48
54
  ...require("./plugins"),
49
55
  ...require("./types/plugins"),
50
56
  ...require("./types/render"),
@@ -26,6 +26,7 @@ var import_class_private_method_get = require("@swc/helpers/_/_class_private_met
26
26
  var import_class_private_method_init = require("@swc/helpers/_/_class_private_method_init");
27
27
  var import_server = require("@modern-js/plugin-v2/server");
28
28
  var import_hono = require("hono");
29
+ var import_context = require("./context");
29
30
  var import_hooks = require("./plugins/compat/hooks");
30
31
  var import_utils = require("./utils");
31
32
  var _applyMiddlewares = /* @__PURE__ */ new WeakSet();
@@ -99,6 +100,7 @@ class ServerBase {
99
100
  this.serverContext = null;
100
101
  this.options = options;
101
102
  this.app = new import_hono.Hono();
103
+ this.app.use("*", import_context.run);
102
104
  }
103
105
  }
104
106
  function applyMiddlewares() {
@@ -0,0 +1,74 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var storage_exports = {};
30
+ __export(storage_exports, {
31
+ createStorage: () => createStorage
32
+ });
33
+ module.exports = __toCommonJS(storage_exports);
34
+ var ah = __toESM(require("async_hooks"));
35
+ const createStorage = () => {
36
+ let storage;
37
+ if (typeof ah.AsyncLocalStorage !== "undefined") {
38
+ storage = new ah.AsyncLocalStorage();
39
+ }
40
+ const run = (context, cb) => {
41
+ if (!storage) {
42
+ throw new Error(`Unable to use async_hook, please confirm the node version >= 12.17
43
+ `);
44
+ }
45
+ return new Promise((resolve, reject) => {
46
+ storage.run(context, () => {
47
+ try {
48
+ return resolve(cb());
49
+ } catch (error) {
50
+ return reject(error);
51
+ }
52
+ });
53
+ });
54
+ };
55
+ const useHonoContext = () => {
56
+ if (!storage) {
57
+ throw new Error(`Unable to use async_hook, please confirm the node version >= 12.17
58
+ `);
59
+ }
60
+ const context = storage.getStore();
61
+ if (!context) {
62
+ throw new Error(`Can't call useContext out of server scope`);
63
+ }
64
+ return context;
65
+ };
66
+ return {
67
+ run,
68
+ useHonoContext
69
+ };
70
+ };
71
+ // Annotate the CommonJS export names for ESM import in node:
72
+ 0 && (module.exports = {
73
+ createStorage
74
+ });
@@ -4,6 +4,7 @@ import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
4
4
  import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
5
5
  import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
6
6
  import { ServerResponse } from "node:http";
7
+ import cloneable from "cloneable-readable";
7
8
  import { isResFinalized } from "./helper";
8
9
  import { installGlobals } from "./polyfills/install";
9
10
  import { createReadableStreamFromReadable, writeReadableStreamToWritable } from "./polyfills/stream";
@@ -32,13 +33,36 @@ var createWebRequest = function(req, res, body) {
32
33
  return controller.abort("res closed");
33
34
  });
34
35
  var url = "http://".concat(req.headers.host).concat(req.url);
35
- var fullUrl = new URL(url);
36
- if (body || !(method === "GET" || method === "HEAD") && fullUrl.searchParams.has("__loader") || fullUrl.searchParams.has("__pass_body") || req.headers["x-mf-micro"] || req.headers["x-rsc-action"] || req.headers["x-parse-through-body"]) {
37
- init.body = body !== null && body !== void 0 ? body : createReadableStreamFromReadable(req);
36
+ var needsRequestBody = body || !(method === "GET" || method === "HEAD");
37
+ var cloneableReq = needsRequestBody ? cloneable(req) : null;
38
+ if (needsRequestBody) {
39
+ if (body) {
40
+ init.body = body;
41
+ } else {
42
+ var stream = cloneableReq.clone();
43
+ init.body = createReadableStreamFromReadable(stream);
44
+ }
38
45
  init.duplex = "half";
39
46
  }
40
- var request = new Request(url, init);
41
- return request;
47
+ var originalRequest = new Request(url, init);
48
+ if (needsRequestBody) {
49
+ return new Proxy(originalRequest, {
50
+ get: function get(target, prop) {
51
+ if ([
52
+ "json",
53
+ "text",
54
+ "blob",
55
+ "arrayBuffer",
56
+ "formData",
57
+ "body"
58
+ ].includes(prop)) {
59
+ cloneableReq.resume();
60
+ }
61
+ return target[prop];
62
+ }
63
+ });
64
+ }
65
+ return originalRequest;
42
66
  };
43
67
  var sendResponse = function() {
44
68
  var _ref = _async_to_generator(function(response, res) {
@@ -0,0 +1,6 @@
1
+ import { createStorage } from "./utils/storage";
2
+ var _createStorage = createStorage(), run = _createStorage.run, useHonoContext = _createStorage.useHonoContext;
3
+ export {
4
+ run,
5
+ useHonoContext
6
+ };
package/dist/esm/index.js CHANGED
@@ -1,6 +1,8 @@
1
1
  import { createErrorHtml, onError, ErrorDigest } from "./utils";
2
2
  import { AGGRED_DIR } from "./constants";
3
3
  import { createServerBase } from "./serverBase";
4
+ import { useHonoContext } from "./context";
5
+ import { Hono } from "hono";
4
6
  import { getLoaderCtx } from "./helper";
5
7
  export * from "./plugins";
6
8
  export * from "./types/plugins";
@@ -11,8 +13,10 @@ export * from "./types/requestHandler";
11
13
  export {
12
14
  AGGRED_DIR,
13
15
  ErrorDigest,
16
+ Hono,
14
17
  createErrorHtml,
15
18
  createServerBase,
16
19
  getLoaderCtx,
17
- onError
20
+ onError,
21
+ useHonoContext
18
22
  };
@@ -7,6 +7,7 @@ import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
7
7
  import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
8
8
  import { server } from "@modern-js/plugin-v2/server";
9
9
  import { Hono } from "hono";
10
+ import { run } from "./context";
10
11
  import { handleSetupResult } from "./plugins/compat/hooks";
11
12
  import { loadConfig } from "./utils";
12
13
  var _applyMiddlewares = /* @__PURE__ */ new WeakSet();
@@ -19,6 +20,7 @@ var ServerBase = /* @__PURE__ */ function() {
19
20
  this.serverContext = null;
20
21
  this.options = options;
21
22
  this.app = new Hono();
23
+ this.app.use("*", run);
22
24
  }
23
25
  var _proto = ServerBase2.prototype;
24
26
  _proto.init = function init() {
@@ -0,0 +1,38 @@
1
+ import * as ah from "async_hooks";
2
+ var createStorage = function() {
3
+ var storage;
4
+ if (typeof ah.AsyncLocalStorage !== "undefined") {
5
+ storage = new ah.AsyncLocalStorage();
6
+ }
7
+ var run = function(context, cb) {
8
+ if (!storage) {
9
+ throw new Error("Unable to use async_hook, please confirm the node version >= 12.17\n ");
10
+ }
11
+ return new Promise(function(resolve, reject) {
12
+ storage.run(context, function() {
13
+ try {
14
+ return resolve(cb());
15
+ } catch (error) {
16
+ return reject(error);
17
+ }
18
+ });
19
+ });
20
+ };
21
+ var useHonoContext = function() {
22
+ if (!storage) {
23
+ throw new Error("Unable to use async_hook, please confirm the node version >= 12.17\n ");
24
+ }
25
+ var context = storage.getStore();
26
+ if (!context) {
27
+ throw new Error("Can't call useContext out of server scope");
28
+ }
29
+ return context;
30
+ };
31
+ return {
32
+ run,
33
+ useHonoContext
34
+ };
35
+ };
36
+ export {
37
+ createStorage
38
+ };
@@ -1,4 +1,5 @@
1
1
  import { ServerResponse } from "node:http";
2
+ import cloneable from "cloneable-readable";
2
3
  import { isResFinalized } from "./helper";
3
4
  import { installGlobals } from "./polyfills/install";
4
5
  import { createReadableStreamFromReadable, writeReadableStreamToWritable } from "./polyfills/stream";
@@ -25,13 +26,36 @@ const createWebRequest = (req, res, body) => {
25
26
  };
26
27
  res.on("close", () => controller.abort("res closed"));
27
28
  const url = `http://${req.headers.host}${req.url}`;
28
- const fullUrl = new URL(url);
29
- if (body || !(method === "GET" || method === "HEAD") && fullUrl.searchParams.has("__loader") || fullUrl.searchParams.has("__pass_body") || req.headers["x-mf-micro"] || req.headers["x-rsc-action"] || req.headers["x-parse-through-body"]) {
30
- init.body = body !== null && body !== void 0 ? body : createReadableStreamFromReadable(req);
29
+ const needsRequestBody = body || !(method === "GET" || method === "HEAD");
30
+ const cloneableReq = needsRequestBody ? cloneable(req) : null;
31
+ if (needsRequestBody) {
32
+ if (body) {
33
+ init.body = body;
34
+ } else {
35
+ const stream = cloneableReq.clone();
36
+ init.body = createReadableStreamFromReadable(stream);
37
+ }
31
38
  init.duplex = "half";
32
39
  }
33
- const request = new Request(url, init);
34
- return request;
40
+ const originalRequest = new Request(url, init);
41
+ if (needsRequestBody) {
42
+ return new Proxy(originalRequest, {
43
+ get(target, prop) {
44
+ if ([
45
+ "json",
46
+ "text",
47
+ "blob",
48
+ "arrayBuffer",
49
+ "formData",
50
+ "body"
51
+ ].includes(prop)) {
52
+ cloneableReq.resume();
53
+ }
54
+ return target[prop];
55
+ }
56
+ });
57
+ }
58
+ return originalRequest;
35
59
  };
36
60
  const sendResponse = async (response, res) => {
37
61
  var _response_headers_get;
@@ -0,0 +1,6 @@
1
+ import { createStorage } from "./utils/storage";
2
+ const { run, useHonoContext } = createStorage();
3
+ export {
4
+ run,
5
+ useHonoContext
6
+ };
@@ -1,6 +1,8 @@
1
1
  import { createErrorHtml, onError, ErrorDigest } from "./utils";
2
2
  import { AGGRED_DIR } from "./constants";
3
3
  import { createServerBase } from "./serverBase";
4
+ import { useHonoContext } from "./context";
5
+ import { Hono } from "hono";
4
6
  import { getLoaderCtx } from "./helper";
5
7
  export * from "./plugins";
6
8
  export * from "./types/plugins";
@@ -11,8 +13,10 @@ export * from "./types/requestHandler";
11
13
  export {
12
14
  AGGRED_DIR,
13
15
  ErrorDigest,
16
+ Hono,
14
17
  createErrorHtml,
15
18
  createServerBase,
16
19
  getLoaderCtx,
17
- onError
20
+ onError,
21
+ useHonoContext
18
22
  };
@@ -2,6 +2,7 @@ import { _ as _class_private_method_get } from "@swc/helpers/_/_class_private_me
2
2
  import { _ as _class_private_method_init } from "@swc/helpers/_/_class_private_method_init";
3
3
  import { server } from "@modern-js/plugin-v2/server";
4
4
  import { Hono } from "hono";
5
+ import { run } from "./context";
5
6
  import { handleSetupResult } from "./plugins/compat/hooks";
6
7
  import { loadConfig } from "./utils";
7
8
  var _applyMiddlewares = /* @__PURE__ */ new WeakSet();
@@ -75,6 +76,7 @@ class ServerBase {
75
76
  this.serverContext = null;
76
77
  this.options = options;
77
78
  this.app = new Hono();
79
+ this.app.use("*", run);
78
80
  }
79
81
  }
80
82
  function applyMiddlewares() {
@@ -0,0 +1,40 @@
1
+ import * as ah from "async_hooks";
2
+ const createStorage = () => {
3
+ let storage;
4
+ if (typeof ah.AsyncLocalStorage !== "undefined") {
5
+ storage = new ah.AsyncLocalStorage();
6
+ }
7
+ const run = (context, cb) => {
8
+ if (!storage) {
9
+ throw new Error(`Unable to use async_hook, please confirm the node version >= 12.17
10
+ `);
11
+ }
12
+ return new Promise((resolve, reject) => {
13
+ storage.run(context, () => {
14
+ try {
15
+ return resolve(cb());
16
+ } catch (error) {
17
+ return reject(error);
18
+ }
19
+ });
20
+ });
21
+ };
22
+ const useHonoContext = () => {
23
+ if (!storage) {
24
+ throw new Error(`Unable to use async_hook, please confirm the node version >= 12.17
25
+ `);
26
+ }
27
+ const context = storage.getStore();
28
+ if (!context) {
29
+ throw new Error(`Can't call useContext out of server scope`);
30
+ }
31
+ return context;
32
+ };
33
+ return {
34
+ run,
35
+ useHonoContext
36
+ };
37
+ };
38
+ export {
39
+ createStorage
40
+ };
@@ -0,0 +1,3 @@
1
+ import type { Context } from 'hono';
2
+ declare const run: <O>(context: Context<any, any, {}>, cb: () => O | Promise<O>) => Promise<O>, useHonoContext: () => Context<any, any, {}>;
3
+ export { run, useHonoContext };
@@ -2,6 +2,8 @@ export { createErrorHtml, onError, ErrorDigest } from './utils';
2
2
  export { AGGRED_DIR } from './constants';
3
3
  export type { ServerBase, ServerBaseOptions } from './serverBase';
4
4
  export { createServerBase } from './serverBase';
5
+ export { useHonoContext } from './context';
6
+ export { Hono, type MiddlewareHandler } from 'hono';
5
7
  export type { Middleware, Context, Next, HonoRequest as InternalRequest, ServerEnv, ServerManifest, ServerLoaderBundle, } from './types';
6
8
  export { getLoaderCtx } from './helper';
7
9
  export * from './plugins';
@@ -44,6 +44,7 @@ export type Middleware = {
44
44
  before?: Array<Middleware['name']>;
45
45
  order?: MiddlewareOrder;
46
46
  };
47
+ export type ServerMiddleware = Middleware;
47
48
  export interface GetRenderHandlerOptions {
48
49
  pwd: string;
49
50
  routes: ServerRoute[];
@@ -1,3 +1,3 @@
1
1
  export * from './new';
2
2
  export * from './old';
3
- export type { ServerConfig, CacheConfig, OnFallback, FallbackReason, GetRenderHandlerOptions, FileChangeEvent, FallbackInput, WebServerStartInput, APIServerStartInput, } from './base';
3
+ export type { ServerConfig, CacheConfig, OnFallback, FallbackReason, GetRenderHandlerOptions, FileChangeEvent, FallbackInput, WebServerStartInput, APIServerStartInput, ServerMiddleware, } from './base';
@@ -0,0 +1,5 @@
1
+ declare const createStorage: <T>() => {
2
+ run: <O>(context: T, cb: () => O | Promise<O>) => Promise<O>;
3
+ useHonoContext: () => T;
4
+ };
5
+ export { createStorage };
package/package.json CHANGED
@@ -15,7 +15,7 @@
15
15
  "modern",
16
16
  "modern.js"
17
17
  ],
18
- "version": "2.67.2",
18
+ "version": "2.67.3",
19
19
  "jsnext:source": "./src/index.ts",
20
20
  "types": "./dist/types/index.d.ts",
21
21
  "main": "./dist/cjs/index.js",
@@ -50,25 +50,27 @@
50
50
  "@web-std/fetch": "^4.2.1",
51
51
  "@web-std/file": "^3.0.3",
52
52
  "@web-std/stream": "^1.0.3",
53
+ "cloneable-readable": "^3.0.0",
53
54
  "flatted": "^3.2.9",
54
55
  "hono": "^3.12.2",
55
56
  "ts-deepmerge": "7.0.2",
56
- "@modern-js/plugin": "2.67.2",
57
- "@modern-js/runtime-utils": "2.67.2",
58
- "@modern-js/plugin-v2": "2.67.2",
59
- "@modern-js/utils": "2.67.2"
57
+ "@modern-js/plugin": "2.67.3",
58
+ "@modern-js/plugin-v2": "2.67.3",
59
+ "@modern-js/runtime-utils": "2.67.3",
60
+ "@modern-js/utils": "2.67.3"
60
61
  },
61
62
  "devDependencies": {
63
+ "@types/cloneable-readable": "^2.0.3",
62
64
  "@types/jest": "^29",
63
65
  "@types/merge-deep": "^3.0.0",
64
66
  "@types/node": "^14",
65
- "http-proxy-middleware": "^2.0.4",
67
+ "http-proxy-middleware": "^2.0.9",
66
68
  "jest": "^29",
67
69
  "ts-jest": "^29.1.0",
68
70
  "typescript": "^5",
69
- "@modern-js/types": "2.67.2",
70
- "@scripts/build": "2.66.0",
71
- "@scripts/jest-config": "2.66.0"
71
+ "@modern-js/types": "2.67.3",
72
+ "@scripts/jest-config": "2.66.0",
73
+ "@scripts/build": "2.66.0"
72
74
  },
73
75
  "sideEffects": false,
74
76
  "publishConfig": {