@modern-js/plugin-data-loader 2.5.0 → 2.5.1-alpha.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/cjs/cli/generate-client.js +4 -0
- package/dist/cjs/cli/loader.js +3 -0
- package/dist/cjs/server/index.js +21 -1
- package/dist/esm/cli/loader.js +6 -0
- package/dist/esm/server/index.js +18 -2
- package/dist/esm-node/cli/loader.js +3 -0
- package/dist/esm-node/server/index.js +17 -1
- package/dist/js/modern/cli/create-request.js +66 -0
- package/dist/js/modern/cli/generate-client.js +42 -0
- package/dist/js/modern/cli/loader.js +40 -0
- package/dist/js/modern/common/constants.js +6 -0
- package/dist/js/modern/server/index.js +237 -0
- package/dist/js/node/cli/create-request.js +90 -0
- package/dist/js/node/cli/generate-client.js +71 -0
- package/dist/js/node/cli/loader.js +61 -0
- package/dist/js/node/common/constants.js +30 -0
- package/dist/js/node/server/index.js +261 -0
- package/dist/js/treeshaking/cli/create-request.js +186 -0
- package/dist/js/treeshaking/cli/generate-client.js +45 -0
- package/dist/js/treeshaking/cli/loader.js +154 -0
- package/dist/js/treeshaking/common/constants.js +3 -0
- package/dist/js/treeshaking/server/index.js +578 -0
- package/package.json +9 -9
|
@@ -17,6 +17,10 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
17
17
|
return to;
|
|
18
18
|
};
|
|
19
19
|
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
20
24
|
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
21
25
|
mod
|
|
22
26
|
));
|
package/dist/cjs/cli/loader.js
CHANGED
|
@@ -28,6 +28,9 @@ async function loader(source) {
|
|
|
28
28
|
if (target === "node") {
|
|
29
29
|
return source;
|
|
30
30
|
}
|
|
31
|
+
if (target === "webworker") {
|
|
32
|
+
return source;
|
|
33
|
+
}
|
|
31
34
|
const options = this.getOptions();
|
|
32
35
|
const code = (0, import_generate_client.generateClient)({
|
|
33
36
|
mapFile: options.mapFile,
|
package/dist/cjs/server/index.js
CHANGED
|
@@ -17,6 +17,10 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
17
17
|
return to;
|
|
18
18
|
};
|
|
19
19
|
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
20
24
|
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
21
25
|
mod
|
|
22
26
|
));
|
|
@@ -37,6 +41,20 @@ var import_utils = require("@modern-js/utils");
|
|
|
37
41
|
var import_constants = require("../common/constants");
|
|
38
42
|
(0, import_node.installGlobals)();
|
|
39
43
|
const redirectStatusCodes = /* @__PURE__ */ new Set([301, 302, 303, 307, 308]);
|
|
44
|
+
function sortByUrlPath(entries) {
|
|
45
|
+
entries.sort(function(a, b) {
|
|
46
|
+
const length1 = a.urlPath.length;
|
|
47
|
+
const length2 = b.urlPath.length;
|
|
48
|
+
if (length1 < length2) {
|
|
49
|
+
return 1;
|
|
50
|
+
}
|
|
51
|
+
if (length1 > length2) {
|
|
52
|
+
return -1;
|
|
53
|
+
}
|
|
54
|
+
return 0;
|
|
55
|
+
});
|
|
56
|
+
return entries;
|
|
57
|
+
}
|
|
40
58
|
function isRedirectResponse(response) {
|
|
41
59
|
return redirectStatusCodes.has(response.status);
|
|
42
60
|
}
|
|
@@ -59,6 +77,7 @@ const callRouteLoader = async ({
|
|
|
59
77
|
loader,
|
|
60
78
|
params,
|
|
61
79
|
request,
|
|
80
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
62
81
|
loadContext
|
|
63
82
|
}) => {
|
|
64
83
|
if (!loader) {
|
|
@@ -130,7 +149,8 @@ const getPathWithoutEntry = (pathname, entryPath) => {
|
|
|
130
149
|
return pathname.replace(entryPath, "");
|
|
131
150
|
};
|
|
132
151
|
const matchEntry = (pathname, entries) => {
|
|
133
|
-
|
|
152
|
+
const newEntries = sortByUrlPath(entries);
|
|
153
|
+
return newEntries.find((entry) => pathname.startsWith(entry.urlPath));
|
|
134
154
|
};
|
|
135
155
|
const handleRequest = async ({
|
|
136
156
|
context,
|
package/dist/esm/cli/loader.js
CHANGED
package/dist/esm/server/index.js
CHANGED
|
@@ -233,6 +233,20 @@ var redirectStatusCodes = /* @__PURE__ */ new Set([
|
|
|
233
233
|
307,
|
|
234
234
|
308
|
|
235
235
|
]);
|
|
236
|
+
function sortByUrlPath(entries) {
|
|
237
|
+
entries.sort(function(a, b) {
|
|
238
|
+
var length1 = a.urlPath.length;
|
|
239
|
+
var length2 = b.urlPath.length;
|
|
240
|
+
if (length1 < length2) {
|
|
241
|
+
return 1;
|
|
242
|
+
}
|
|
243
|
+
if (length1 > length2) {
|
|
244
|
+
return -1;
|
|
245
|
+
}
|
|
246
|
+
return 0;
|
|
247
|
+
});
|
|
248
|
+
return entries;
|
|
249
|
+
}
|
|
236
250
|
function isRedirectResponse(response) {
|
|
237
251
|
return redirectStatusCodes.has(response.status);
|
|
238
252
|
}
|
|
@@ -254,7 +268,8 @@ var json = function(data) {
|
|
|
254
268
|
};
|
|
255
269
|
var callRouteLoader = function() {
|
|
256
270
|
var _ref = _asyncToGenerator(function(param) {
|
|
257
|
-
var routeId, loader, params, request,
|
|
271
|
+
var routeId, loader, params, request, // eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
272
|
+
loadContext, result, error;
|
|
258
273
|
return __generator(this, function(_state) {
|
|
259
274
|
switch(_state.label){
|
|
260
275
|
case 0:
|
|
@@ -431,7 +446,8 @@ var getPathWithoutEntry = function(pathname, entryPath) {
|
|
|
431
446
|
return pathname.replace(entryPath, "");
|
|
432
447
|
};
|
|
433
448
|
var matchEntry = function(pathname, entries) {
|
|
434
|
-
|
|
449
|
+
var newEntries = sortByUrlPath(entries);
|
|
450
|
+
return newEntries.find(function(entry) {
|
|
435
451
|
return pathname.startsWith(entry.urlPath);
|
|
436
452
|
});
|
|
437
453
|
};
|
|
@@ -11,6 +11,20 @@ import { MAIN_ENTRY_NAME, SERVER_BUNDLE_DIRECTORY } from "@modern-js/utils";
|
|
|
11
11
|
import { LOADER_ID_PARAM } from "../common/constants";
|
|
12
12
|
installGlobals();
|
|
13
13
|
const redirectStatusCodes = /* @__PURE__ */ new Set([301, 302, 303, 307, 308]);
|
|
14
|
+
function sortByUrlPath(entries) {
|
|
15
|
+
entries.sort(function(a, b) {
|
|
16
|
+
const length1 = a.urlPath.length;
|
|
17
|
+
const length2 = b.urlPath.length;
|
|
18
|
+
if (length1 < length2) {
|
|
19
|
+
return 1;
|
|
20
|
+
}
|
|
21
|
+
if (length1 > length2) {
|
|
22
|
+
return -1;
|
|
23
|
+
}
|
|
24
|
+
return 0;
|
|
25
|
+
});
|
|
26
|
+
return entries;
|
|
27
|
+
}
|
|
14
28
|
function isRedirectResponse(response) {
|
|
15
29
|
return redirectStatusCodes.has(response.status);
|
|
16
30
|
}
|
|
@@ -33,6 +47,7 @@ const callRouteLoader = async ({
|
|
|
33
47
|
loader,
|
|
34
48
|
params,
|
|
35
49
|
request,
|
|
50
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
36
51
|
loadContext
|
|
37
52
|
}) => {
|
|
38
53
|
if (!loader) {
|
|
@@ -104,7 +119,8 @@ const getPathWithoutEntry = (pathname, entryPath) => {
|
|
|
104
119
|
return pathname.replace(entryPath, "");
|
|
105
120
|
};
|
|
106
121
|
const matchEntry = (pathname, entries) => {
|
|
107
|
-
|
|
122
|
+
const newEntries = sortByUrlPath(entries);
|
|
123
|
+
return newEntries.find((entry) => pathname.startsWith(entry.urlPath));
|
|
108
124
|
};
|
|
109
125
|
const handleRequest = async ({
|
|
110
126
|
context,
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
var __async = (__this, __arguments, generator) => {
|
|
2
|
+
return new Promise((resolve, reject) => {
|
|
3
|
+
var fulfilled = (value) => {
|
|
4
|
+
try {
|
|
5
|
+
step(generator.next(value));
|
|
6
|
+
} catch (e) {
|
|
7
|
+
reject(e);
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
var rejected = (value) => {
|
|
11
|
+
try {
|
|
12
|
+
step(generator.throw(value));
|
|
13
|
+
} catch (e) {
|
|
14
|
+
reject(e);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
18
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
import { compile } from "path-to-regexp";
|
|
22
|
+
import { redirect } from "react-router-dom";
|
|
23
|
+
import { LOADER_ID_PARAM, DIRECT_PARAM } from "../common/constants";
|
|
24
|
+
const getRequestUrl = ({
|
|
25
|
+
params,
|
|
26
|
+
request,
|
|
27
|
+
routeId
|
|
28
|
+
}) => {
|
|
29
|
+
const url = new URL(request.url);
|
|
30
|
+
const toPath = compile(url.pathname, {
|
|
31
|
+
encode: encodeURIComponent
|
|
32
|
+
});
|
|
33
|
+
const newPathName = toPath(params);
|
|
34
|
+
url.pathname = newPathName;
|
|
35
|
+
url.searchParams.append(LOADER_ID_PARAM, routeId);
|
|
36
|
+
url.searchParams.append(DIRECT_PARAM, "true");
|
|
37
|
+
return url;
|
|
38
|
+
};
|
|
39
|
+
const handleRedirectResponse = (res) => {
|
|
40
|
+
const { headers } = res;
|
|
41
|
+
const location = headers.get("X-Modernjs-Redirect");
|
|
42
|
+
if (location) {
|
|
43
|
+
return redirect(location);
|
|
44
|
+
}
|
|
45
|
+
return res;
|
|
46
|
+
};
|
|
47
|
+
const createRequest = (routeId, method = "get") => {
|
|
48
|
+
return (_0) => __async(void 0, [_0], function* ({
|
|
49
|
+
params,
|
|
50
|
+
request
|
|
51
|
+
}) {
|
|
52
|
+
const url = getRequestUrl({ params, request, routeId });
|
|
53
|
+
const res = yield fetch(url, {
|
|
54
|
+
method,
|
|
55
|
+
signal: request.signal
|
|
56
|
+
});
|
|
57
|
+
if (!res.ok) {
|
|
58
|
+
throw res;
|
|
59
|
+
}
|
|
60
|
+
return handleRedirectResponse(res);
|
|
61
|
+
});
|
|
62
|
+
};
|
|
63
|
+
export {
|
|
64
|
+
createRequest,
|
|
65
|
+
getRequestUrl
|
|
66
|
+
};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import path from "path";
|
|
2
|
+
const generateClient = ({
|
|
3
|
+
mapFile,
|
|
4
|
+
loaderId
|
|
5
|
+
}) => {
|
|
6
|
+
delete require.cache[mapFile];
|
|
7
|
+
const loadersMap = require(mapFile);
|
|
8
|
+
let requestCode = ``;
|
|
9
|
+
let exportsCode = ``;
|
|
10
|
+
const requestCreatorPath = path.join(__dirname, "./create-request").replace("/node/cli/", "/treeshaking/cli/").replace(/\\/g, "/");
|
|
11
|
+
const importCode = `
|
|
12
|
+
import { createRequest } from '${requestCreatorPath}';
|
|
13
|
+
`;
|
|
14
|
+
if (!loaderId) {
|
|
15
|
+
requestCode = Object.keys(loadersMap).map((loaderId2) => {
|
|
16
|
+
const { routeId } = loadersMap[loaderId2];
|
|
17
|
+
return `
|
|
18
|
+
const ${loaderId2} = createRequest('${routeId}');
|
|
19
|
+
`;
|
|
20
|
+
}).join("");
|
|
21
|
+
exportsCode = `export {`;
|
|
22
|
+
for (const loader of Object.keys(loadersMap)) {
|
|
23
|
+
exportsCode += `${loader},`;
|
|
24
|
+
}
|
|
25
|
+
exportsCode += "}";
|
|
26
|
+
} else {
|
|
27
|
+
const loader = loadersMap[loaderId];
|
|
28
|
+
requestCode = `
|
|
29
|
+
const loader = createRequest('${loader.routeId}');
|
|
30
|
+
`;
|
|
31
|
+
exportsCode = `export default loader;`;
|
|
32
|
+
}
|
|
33
|
+
const generatedCode = `
|
|
34
|
+
${importCode}
|
|
35
|
+
${requestCode}
|
|
36
|
+
${exportsCode}
|
|
37
|
+
`;
|
|
38
|
+
return generatedCode;
|
|
39
|
+
};
|
|
40
|
+
export {
|
|
41
|
+
generateClient
|
|
42
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
var __async = (__this, __arguments, generator) => {
|
|
2
|
+
return new Promise((resolve, reject) => {
|
|
3
|
+
var fulfilled = (value) => {
|
|
4
|
+
try {
|
|
5
|
+
step(generator.next(value));
|
|
6
|
+
} catch (e) {
|
|
7
|
+
reject(e);
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
var rejected = (value) => {
|
|
11
|
+
try {
|
|
12
|
+
step(generator.throw(value));
|
|
13
|
+
} catch (e) {
|
|
14
|
+
reject(e);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
18
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
import { generateClient } from "./generate-client";
|
|
22
|
+
function loader(source) {
|
|
23
|
+
return __async(this, null, function* () {
|
|
24
|
+
var _a;
|
|
25
|
+
this.cacheable();
|
|
26
|
+
const target = (_a = this._compiler) == null ? void 0 : _a.options.target;
|
|
27
|
+
if (target === "node") {
|
|
28
|
+
return source;
|
|
29
|
+
}
|
|
30
|
+
const options = this.getOptions();
|
|
31
|
+
const code = generateClient({
|
|
32
|
+
mapFile: options.mapFile,
|
|
33
|
+
loaderId: options.loaderId
|
|
34
|
+
});
|
|
35
|
+
return code;
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
export {
|
|
39
|
+
loader as default
|
|
40
|
+
};
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defProps = Object.defineProperties;
|
|
3
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __spreadValues = (a, b) => {
|
|
9
|
+
for (var prop in b || (b = {}))
|
|
10
|
+
if (__hasOwnProp.call(b, prop))
|
|
11
|
+
__defNormalProp(a, prop, b[prop]);
|
|
12
|
+
if (__getOwnPropSymbols)
|
|
13
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
+
if (__propIsEnum.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
}
|
|
17
|
+
return a;
|
|
18
|
+
};
|
|
19
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
+
var __async = (__this, __arguments, generator) => {
|
|
21
|
+
return new Promise((resolve, reject) => {
|
|
22
|
+
var fulfilled = (value) => {
|
|
23
|
+
try {
|
|
24
|
+
step(generator.next(value));
|
|
25
|
+
} catch (e) {
|
|
26
|
+
reject(e);
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
var rejected = (value) => {
|
|
30
|
+
try {
|
|
31
|
+
step(generator.throw(value));
|
|
32
|
+
} catch (e) {
|
|
33
|
+
reject(e);
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
37
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
38
|
+
});
|
|
39
|
+
};
|
|
40
|
+
import path from "path";
|
|
41
|
+
import {
|
|
42
|
+
installGlobals,
|
|
43
|
+
writeReadableStreamToWritable,
|
|
44
|
+
Response as NodeResponse
|
|
45
|
+
} from "@remix-run/node";
|
|
46
|
+
import {
|
|
47
|
+
matchRoutes
|
|
48
|
+
} from "react-router-dom";
|
|
49
|
+
import { MAIN_ENTRY_NAME, SERVER_BUNDLE_DIRECTORY } from "@modern-js/utils";
|
|
50
|
+
import { LOADER_ID_PARAM } from "../common/constants";
|
|
51
|
+
installGlobals();
|
|
52
|
+
const redirectStatusCodes = /* @__PURE__ */ new Set([301, 302, 303, 307, 308]);
|
|
53
|
+
function isRedirectResponse(response) {
|
|
54
|
+
return redirectStatusCodes.has(response.status);
|
|
55
|
+
}
|
|
56
|
+
function isResponse(value) {
|
|
57
|
+
return value != null && typeof value.status === "number" && typeof value.statusText === "string" && typeof value.headers === "object" && typeof value.body !== "undefined";
|
|
58
|
+
}
|
|
59
|
+
const json = (data, init = {}) => {
|
|
60
|
+
const responseInit = typeof init === "number" ? { status: init } : init;
|
|
61
|
+
const headers = new Headers(responseInit.headers);
|
|
62
|
+
if (!headers.has("Content-Type")) {
|
|
63
|
+
headers.set("Content-Type", "application/json; charset=utf-8");
|
|
64
|
+
}
|
|
65
|
+
return new NodeResponse(JSON.stringify(data), __spreadProps(__spreadValues({}, responseInit), {
|
|
66
|
+
headers
|
|
67
|
+
}));
|
|
68
|
+
};
|
|
69
|
+
const callRouteLoader = (_0) => __async(void 0, [_0], function* ({
|
|
70
|
+
routeId,
|
|
71
|
+
loader,
|
|
72
|
+
params,
|
|
73
|
+
request,
|
|
74
|
+
loadContext
|
|
75
|
+
}) {
|
|
76
|
+
if (!loader) {
|
|
77
|
+
throw new Error(
|
|
78
|
+
`You made a ${request.method} request to ${request.url} but did not provide a default component or \`loader\` for route "${routeId}", so there is no way to handle the request.`
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
let result;
|
|
82
|
+
try {
|
|
83
|
+
result = yield loader({
|
|
84
|
+
request,
|
|
85
|
+
params
|
|
86
|
+
});
|
|
87
|
+
} catch (error) {
|
|
88
|
+
if (!isResponse(error)) {
|
|
89
|
+
throw error;
|
|
90
|
+
}
|
|
91
|
+
result = error;
|
|
92
|
+
}
|
|
93
|
+
if (result === void 0) {
|
|
94
|
+
throw new Error(
|
|
95
|
+
`You defined a loader for route "${routeId}" but didn't return anything from your \`loader\` function. Please return a value or \`null\`.`
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
return isResponse(result) ? result : json(result);
|
|
99
|
+
});
|
|
100
|
+
const createLoaderHeaders = (requestHeaders) => {
|
|
101
|
+
const headers = new Headers();
|
|
102
|
+
for (const [key, values] of Object.entries(requestHeaders)) {
|
|
103
|
+
if (values) {
|
|
104
|
+
if (Array.isArray(values)) {
|
|
105
|
+
for (const value of values) {
|
|
106
|
+
headers.append(key, value);
|
|
107
|
+
}
|
|
108
|
+
} else {
|
|
109
|
+
headers.set(key, values);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return headers;
|
|
114
|
+
};
|
|
115
|
+
const createLoaderRequest = (context) => {
|
|
116
|
+
const origin = `${context.protocol}://${context.host}`;
|
|
117
|
+
const url = new URL(context.url, origin);
|
|
118
|
+
const controller = new AbortController();
|
|
119
|
+
const init = {
|
|
120
|
+
method: context.method,
|
|
121
|
+
headers: createLoaderHeaders(context.headers),
|
|
122
|
+
signal: controller.signal
|
|
123
|
+
};
|
|
124
|
+
return new Request(url.href, init);
|
|
125
|
+
};
|
|
126
|
+
const sendLoaderResponse = (res, nodeResponse) => __async(void 0, null, function* () {
|
|
127
|
+
res.statusMessage = nodeResponse.statusText;
|
|
128
|
+
res.statusCode = nodeResponse.status;
|
|
129
|
+
for (const [key, value] of nodeResponse.headers.entries()) {
|
|
130
|
+
res.setHeader(key, value);
|
|
131
|
+
}
|
|
132
|
+
if (nodeResponse.body) {
|
|
133
|
+
yield writeReadableStreamToWritable(nodeResponse.body, res);
|
|
134
|
+
} else {
|
|
135
|
+
res.end();
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
const getPathWithoutEntry = (pathname, entryPath) => {
|
|
139
|
+
if (entryPath === "/") {
|
|
140
|
+
return pathname;
|
|
141
|
+
}
|
|
142
|
+
return pathname.replace(entryPath, "");
|
|
143
|
+
};
|
|
144
|
+
const matchEntry = (pathname, entries) => {
|
|
145
|
+
return entries.find((entry) => pathname.startsWith(entry.urlPath));
|
|
146
|
+
};
|
|
147
|
+
const handleRequest = (_0) => __async(void 0, [_0], function* ({
|
|
148
|
+
context,
|
|
149
|
+
serverRoutes,
|
|
150
|
+
distDir
|
|
151
|
+
}) {
|
|
152
|
+
const { method, query } = context;
|
|
153
|
+
const routeId = query[LOADER_ID_PARAM];
|
|
154
|
+
if (!routeId || method.toLowerCase() !== "get") {
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
const entry = matchEntry(context.path, serverRoutes);
|
|
158
|
+
if (!entry) {
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
const routesPath = path.join(
|
|
162
|
+
distDir,
|
|
163
|
+
SERVER_BUNDLE_DIRECTORY,
|
|
164
|
+
`${entry.entryName || MAIN_ENTRY_NAME}-server-loaders`
|
|
165
|
+
);
|
|
166
|
+
const { routes } = yield import(routesPath);
|
|
167
|
+
if (!routes) {
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
const { res } = context;
|
|
171
|
+
const pathname = getPathWithoutEntry(context.path, entry.urlPath);
|
|
172
|
+
const matches = matchRoutes(routes, pathname);
|
|
173
|
+
if (!matches) {
|
|
174
|
+
res.statusCode = 403;
|
|
175
|
+
res.end(`Route ${pathname} was not matched`);
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
const match = matches == null ? void 0 : matches.find((match2) => match2.route.id === routeId);
|
|
179
|
+
if (!match) {
|
|
180
|
+
res.statusCode = 403;
|
|
181
|
+
res.end(`Route ${routeId} does not match URL ${context.path}`);
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
const request = createLoaderRequest(context);
|
|
185
|
+
let response;
|
|
186
|
+
try {
|
|
187
|
+
response = yield callRouteLoader({
|
|
188
|
+
loader: match.route.loader,
|
|
189
|
+
routeId: match.route.id,
|
|
190
|
+
params: match.params,
|
|
191
|
+
request,
|
|
192
|
+
loadContext: {}
|
|
193
|
+
});
|
|
194
|
+
if (isRedirectResponse(response)) {
|
|
195
|
+
const headers = new Headers(response.headers);
|
|
196
|
+
headers.set("X-Modernjs-Redirect", headers.get("Location"));
|
|
197
|
+
headers.delete("Location");
|
|
198
|
+
response = new NodeResponse(null, {
|
|
199
|
+
status: 204,
|
|
200
|
+
headers
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
} catch (error) {
|
|
204
|
+
const message = String(error);
|
|
205
|
+
response = new NodeResponse(message, {
|
|
206
|
+
status: 500,
|
|
207
|
+
headers: {
|
|
208
|
+
"Content-Type": "text/plain"
|
|
209
|
+
}
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
sendLoaderResponse(res, response);
|
|
213
|
+
});
|
|
214
|
+
var server_default = () => ({
|
|
215
|
+
name: "@modern-js/plugin-data-loader",
|
|
216
|
+
setup: () => ({
|
|
217
|
+
preparebeforeRouteHandler({
|
|
218
|
+
serverRoutes,
|
|
219
|
+
distDir
|
|
220
|
+
}) {
|
|
221
|
+
return (context) => __async(this, null, function* () {
|
|
222
|
+
return handleRequest({
|
|
223
|
+
serverRoutes,
|
|
224
|
+
distDir,
|
|
225
|
+
context
|
|
226
|
+
});
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
})
|
|
230
|
+
});
|
|
231
|
+
export {
|
|
232
|
+
server_default as default,
|
|
233
|
+
getPathWithoutEntry,
|
|
234
|
+
handleRequest,
|
|
235
|
+
isRedirectResponse,
|
|
236
|
+
isResponse
|
|
237
|
+
};
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
var __async = (__this, __arguments, generator) => {
|
|
19
|
+
return new Promise((resolve, reject) => {
|
|
20
|
+
var fulfilled = (value) => {
|
|
21
|
+
try {
|
|
22
|
+
step(generator.next(value));
|
|
23
|
+
} catch (e) {
|
|
24
|
+
reject(e);
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
var rejected = (value) => {
|
|
28
|
+
try {
|
|
29
|
+
step(generator.throw(value));
|
|
30
|
+
} catch (e) {
|
|
31
|
+
reject(e);
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
35
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
36
|
+
});
|
|
37
|
+
};
|
|
38
|
+
var create_request_exports = {};
|
|
39
|
+
__export(create_request_exports, {
|
|
40
|
+
createRequest: () => createRequest,
|
|
41
|
+
getRequestUrl: () => getRequestUrl
|
|
42
|
+
});
|
|
43
|
+
module.exports = __toCommonJS(create_request_exports);
|
|
44
|
+
var import_path_to_regexp = require("path-to-regexp");
|
|
45
|
+
var import_react_router_dom = require("react-router-dom");
|
|
46
|
+
var import_constants = require("../common/constants");
|
|
47
|
+
const getRequestUrl = ({
|
|
48
|
+
params,
|
|
49
|
+
request,
|
|
50
|
+
routeId
|
|
51
|
+
}) => {
|
|
52
|
+
const url = new URL(request.url);
|
|
53
|
+
const toPath = (0, import_path_to_regexp.compile)(url.pathname, {
|
|
54
|
+
encode: encodeURIComponent
|
|
55
|
+
});
|
|
56
|
+
const newPathName = toPath(params);
|
|
57
|
+
url.pathname = newPathName;
|
|
58
|
+
url.searchParams.append(import_constants.LOADER_ID_PARAM, routeId);
|
|
59
|
+
url.searchParams.append(import_constants.DIRECT_PARAM, "true");
|
|
60
|
+
return url;
|
|
61
|
+
};
|
|
62
|
+
const handleRedirectResponse = (res) => {
|
|
63
|
+
const { headers } = res;
|
|
64
|
+
const location = headers.get("X-Modernjs-Redirect");
|
|
65
|
+
if (location) {
|
|
66
|
+
return (0, import_react_router_dom.redirect)(location);
|
|
67
|
+
}
|
|
68
|
+
return res;
|
|
69
|
+
};
|
|
70
|
+
const createRequest = (routeId, method = "get") => {
|
|
71
|
+
return (_0) => __async(void 0, [_0], function* ({
|
|
72
|
+
params,
|
|
73
|
+
request
|
|
74
|
+
}) {
|
|
75
|
+
const url = getRequestUrl({ params, request, routeId });
|
|
76
|
+
const res = yield fetch(url, {
|
|
77
|
+
method,
|
|
78
|
+
signal: request.signal
|
|
79
|
+
});
|
|
80
|
+
if (!res.ok) {
|
|
81
|
+
throw res;
|
|
82
|
+
}
|
|
83
|
+
return handleRedirectResponse(res);
|
|
84
|
+
});
|
|
85
|
+
};
|
|
86
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
87
|
+
0 && (module.exports = {
|
|
88
|
+
createRequest,
|
|
89
|
+
getRequestUrl
|
|
90
|
+
});
|