@modern-js/plugin-data-loader 2.4.1-beta.0 → 2.5.0-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/{js/node → cjs}/cli/create-request.js +17 -26
- package/dist/cjs/cli/data.js +162 -0
- package/dist/{js/node → cjs}/cli/generate-client.js +1 -1
- package/dist/{js/node → cjs}/cli/loader.js +12 -34
- package/dist/{js/node → cjs}/common/constants.js +0 -0
- package/dist/cjs/runtime/index.js +159 -0
- package/dist/cjs/runtime/response.js +102 -0
- package/dist/cjs/server/index.js +64 -0
- package/dist/{js/treeshaking → esm}/cli/create-request.js +41 -2
- package/dist/esm/cli/data.js +820 -0
- package/dist/{js/treeshaking → esm}/cli/generate-client.js +1 -1
- package/dist/{js/treeshaking → esm}/cli/loader.js +0 -0
- package/dist/{js/treeshaking → esm}/common/constants.js +0 -0
- package/dist/{js/treeshaking/server → esm/runtime}/index.js +66 -222
- package/dist/esm/runtime/response.js +284 -0
- package/dist/esm/server/index.js +176 -0
- package/dist/{js/modern → esm-node}/cli/create-request.js +17 -26
- package/dist/esm-node/cli/data.js +142 -0
- package/dist/{js/modern → esm-node}/cli/generate-client.js +1 -1
- package/dist/esm-node/cli/loader.js +18 -0
- package/dist/{js/modern → esm-node}/common/constants.js +0 -0
- package/dist/esm-node/runtime/index.js +143 -0
- package/dist/esm-node/runtime/response.js +79 -0
- package/dist/esm-node/server/index.js +40 -0
- package/dist/types/cli/create-request.d.ts +2 -1
- package/dist/types/cli/data.d.ts +6 -0
- package/dist/types/runtime/index.d.ts +15 -0
- package/dist/types/runtime/response.d.ts +2 -0
- package/dist/types/server/index.d.ts +0 -15
- package/package.json +24 -14
- package/dist/js/modern/cli/loader.js +0 -40
- package/dist/js/modern/server/index.js +0 -237
- package/dist/js/node/server/index.js +0 -261
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* modified from https://github.com/remix-run/remix/blob/main/packages/remix-react/data.ts
|
|
3
|
+
* license at https://github.com/remix-run/remix/blob/main/LICENSE.md
|
|
4
|
+
*/
|
|
5
|
+
import { UNSAFE_DeferredData as DeferredData } from '@modern-js/utils/remix-router';
|
|
6
|
+
export declare function parseDeferredReadableStream(stream: ReadableStream<Uint8Array>): Promise<DeferredData>;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { ModernServerContext, NestedRoute, ServerRoute } from '@modern-js/types';
|
|
2
|
+
import { Response as NodeResponse } from '@remix-run/node';
|
|
3
|
+
export type ServerContext = Pick<ModernServerContext, 'req' | 'res' | 'params' | 'headers' | 'method' | 'url' | 'host' | 'protocol' | 'origin' | 'href' | 'path' | 'query'>;
|
|
4
|
+
export declare function isRedirectResponse(status: number): boolean;
|
|
5
|
+
export declare function isResponse(value: any): value is NodeResponse;
|
|
6
|
+
export declare const matchEntry: (pathname: string, entries: ServerRoute[]) => ServerRoute | undefined;
|
|
7
|
+
export declare const handleRequest: ({
|
|
8
|
+
context,
|
|
9
|
+
serverRoutes,
|
|
10
|
+
routes
|
|
11
|
+
}: {
|
|
12
|
+
context: ServerContext;
|
|
13
|
+
serverRoutes: ServerRoute[];
|
|
14
|
+
routes: NestedRoute[];
|
|
15
|
+
}) => Promise<void>;
|
|
@@ -1,19 +1,4 @@
|
|
|
1
1
|
import type { ServerPlugin } from '@modern-js/server-core';
|
|
2
|
-
import type { ModernServerContext, ServerRoute } from '@modern-js/types';
|
|
3
|
-
import { Response as NodeResponse } from '@remix-run/node';
|
|
4
|
-
export type ServerContext = Pick<ModernServerContext, 'req' | 'res' | 'params' | 'headers' | 'method' | 'url' | 'host' | 'protocol' | 'origin' | 'href' | 'path' | 'query'>;
|
|
5
|
-
export declare function isRedirectResponse(response: NodeResponse): boolean;
|
|
6
|
-
export declare function isResponse(value: any): value is NodeResponse;
|
|
7
|
-
export declare const getPathWithoutEntry: (pathname: string, entryPath: string) => string;
|
|
8
|
-
export declare const handleRequest: ({
|
|
9
|
-
context,
|
|
10
|
-
serverRoutes,
|
|
11
|
-
distDir
|
|
12
|
-
}: {
|
|
13
|
-
context: ServerContext;
|
|
14
|
-
serverRoutes: ServerRoute[];
|
|
15
|
-
distDir: string;
|
|
16
|
-
}) => Promise<void>;
|
|
17
2
|
|
|
18
3
|
declare const _default: () => ServerPlugin;
|
|
19
4
|
|
package/package.json
CHANGED
|
@@ -11,20 +11,26 @@
|
|
|
11
11
|
"modern",
|
|
12
12
|
"modern.js"
|
|
13
13
|
],
|
|
14
|
-
"version": "2.
|
|
14
|
+
"version": "2.5.0-alpha.0",
|
|
15
|
+
"engines": {
|
|
16
|
+
"node": ">=14.17.6"
|
|
17
|
+
},
|
|
15
18
|
"jsnext:source": "./src/index.ts",
|
|
16
19
|
"types": "./dist/types/index.d.ts",
|
|
17
|
-
"main": "./dist/
|
|
18
|
-
"module": "./dist/
|
|
19
|
-
"jsnext:modern": "./dist/js/modern/index.js",
|
|
20
|
+
"main": "./dist/cjs/index.js",
|
|
21
|
+
"module": "./dist/esm/index.js",
|
|
20
22
|
"exports": {
|
|
21
23
|
"./loader": {
|
|
22
24
|
"jsnext:source": "./src/cli/loader.ts",
|
|
23
|
-
"default": "./dist/
|
|
25
|
+
"default": "./dist/cjs/cli/loader.js"
|
|
24
26
|
},
|
|
25
27
|
"./server": {
|
|
26
28
|
"jsnext:source": "./src/server/index.ts",
|
|
27
|
-
"default": "./dist/
|
|
29
|
+
"default": "./dist/cjs/server/index.js"
|
|
30
|
+
},
|
|
31
|
+
"./runtime": {
|
|
32
|
+
"jsnext:source": "./src/runtime/index.ts",
|
|
33
|
+
"default": "./dist/esm/runtime/index.js"
|
|
28
34
|
}
|
|
29
35
|
},
|
|
30
36
|
"typesVersions": {
|
|
@@ -34,16 +40,19 @@
|
|
|
34
40
|
],
|
|
35
41
|
"server": [
|
|
36
42
|
"./dist/types/server/index.d.ts"
|
|
43
|
+
],
|
|
44
|
+
"runtime": [
|
|
45
|
+
"./dist/types/runtime/index.d.ts"
|
|
37
46
|
]
|
|
38
47
|
}
|
|
39
48
|
},
|
|
40
49
|
"dependencies": {
|
|
41
50
|
"@babel/core": "^7.18.0",
|
|
42
51
|
"@babel/runtime": "^7.18.0",
|
|
43
|
-
"@remix-run/node": "^1.
|
|
52
|
+
"@remix-run/node": "^1.12.0",
|
|
44
53
|
"path-to-regexp": "^6.2.0",
|
|
45
|
-
"react-router-dom": "^6.
|
|
46
|
-
"@modern-js/utils": "2.
|
|
54
|
+
"react-router-dom": "^6.8.1",
|
|
55
|
+
"@modern-js/utils": "2.5.0-alpha.0"
|
|
47
56
|
},
|
|
48
57
|
"devDependencies": {
|
|
49
58
|
"@types/babel__core": "^7.1.15",
|
|
@@ -58,11 +67,12 @@
|
|
|
58
67
|
"typescript": "^4",
|
|
59
68
|
"webpack": "^5.75.0",
|
|
60
69
|
"webpack-chain": "^6.5.1",
|
|
61
|
-
"@modern-js/core": "2.
|
|
62
|
-
"@modern-js/
|
|
63
|
-
"@
|
|
64
|
-
"@
|
|
65
|
-
"@
|
|
70
|
+
"@modern-js/core": "2.5.0",
|
|
71
|
+
"@modern-js/types": "2.5.0",
|
|
72
|
+
"@scripts/build": "2.5.0",
|
|
73
|
+
"@modern-js/server-core": "2.5.0",
|
|
74
|
+
"@modern-js/plugin-module-babel": "2.5.0",
|
|
75
|
+
"@scripts/jest-config": "2.5.0"
|
|
66
76
|
},
|
|
67
77
|
"sideEffects": false,
|
|
68
78
|
"publishConfig": {
|
|
@@ -1,40 +0,0 @@
|
|
|
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
|
-
};
|
|
@@ -1,237 +0,0 @@
|
|
|
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
|
-
};
|
|
@@ -1,261 +0,0 @@
|
|
|
1
|
-
var __create = Object.create;
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __defProps = Object.defineProperties;
|
|
4
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
-
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
6
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
-
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
8
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
9
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
10
|
-
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
11
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
12
|
-
var __spreadValues = (a, b) => {
|
|
13
|
-
for (var prop in b || (b = {}))
|
|
14
|
-
if (__hasOwnProp.call(b, prop))
|
|
15
|
-
__defNormalProp(a, prop, b[prop]);
|
|
16
|
-
if (__getOwnPropSymbols)
|
|
17
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
|
18
|
-
if (__propIsEnum.call(b, prop))
|
|
19
|
-
__defNormalProp(a, prop, b[prop]);
|
|
20
|
-
}
|
|
21
|
-
return a;
|
|
22
|
-
};
|
|
23
|
-
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
24
|
-
var __export = (target, all) => {
|
|
25
|
-
for (var name in all)
|
|
26
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
27
|
-
};
|
|
28
|
-
var __copyProps = (to, from, except, desc) => {
|
|
29
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
30
|
-
for (let key of __getOwnPropNames(from))
|
|
31
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
32
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
33
|
-
}
|
|
34
|
-
return to;
|
|
35
|
-
};
|
|
36
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
37
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
38
|
-
mod
|
|
39
|
-
));
|
|
40
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
41
|
-
var __async = (__this, __arguments, generator) => {
|
|
42
|
-
return new Promise((resolve, reject) => {
|
|
43
|
-
var fulfilled = (value) => {
|
|
44
|
-
try {
|
|
45
|
-
step(generator.next(value));
|
|
46
|
-
} catch (e) {
|
|
47
|
-
reject(e);
|
|
48
|
-
}
|
|
49
|
-
};
|
|
50
|
-
var rejected = (value) => {
|
|
51
|
-
try {
|
|
52
|
-
step(generator.throw(value));
|
|
53
|
-
} catch (e) {
|
|
54
|
-
reject(e);
|
|
55
|
-
}
|
|
56
|
-
};
|
|
57
|
-
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
58
|
-
step((generator = generator.apply(__this, __arguments)).next());
|
|
59
|
-
});
|
|
60
|
-
};
|
|
61
|
-
var server_exports = {};
|
|
62
|
-
__export(server_exports, {
|
|
63
|
-
default: () => server_default,
|
|
64
|
-
getPathWithoutEntry: () => getPathWithoutEntry,
|
|
65
|
-
handleRequest: () => handleRequest,
|
|
66
|
-
isRedirectResponse: () => isRedirectResponse,
|
|
67
|
-
isResponse: () => isResponse
|
|
68
|
-
});
|
|
69
|
-
module.exports = __toCommonJS(server_exports);
|
|
70
|
-
var import_path = __toESM(require("path"));
|
|
71
|
-
var import_node = require("@remix-run/node");
|
|
72
|
-
var import_react_router_dom = require("react-router-dom");
|
|
73
|
-
var import_utils = require("@modern-js/utils");
|
|
74
|
-
var import_constants = require("../common/constants");
|
|
75
|
-
(0, import_node.installGlobals)();
|
|
76
|
-
const redirectStatusCodes = /* @__PURE__ */ new Set([301, 302, 303, 307, 308]);
|
|
77
|
-
function isRedirectResponse(response) {
|
|
78
|
-
return redirectStatusCodes.has(response.status);
|
|
79
|
-
}
|
|
80
|
-
function isResponse(value) {
|
|
81
|
-
return value != null && typeof value.status === "number" && typeof value.statusText === "string" && typeof value.headers === "object" && typeof value.body !== "undefined";
|
|
82
|
-
}
|
|
83
|
-
const json = (data, init = {}) => {
|
|
84
|
-
const responseInit = typeof init === "number" ? { status: init } : init;
|
|
85
|
-
const headers = new Headers(responseInit.headers);
|
|
86
|
-
if (!headers.has("Content-Type")) {
|
|
87
|
-
headers.set("Content-Type", "application/json; charset=utf-8");
|
|
88
|
-
}
|
|
89
|
-
return new import_node.Response(JSON.stringify(data), __spreadProps(__spreadValues({}, responseInit), {
|
|
90
|
-
headers
|
|
91
|
-
}));
|
|
92
|
-
};
|
|
93
|
-
const callRouteLoader = (_0) => __async(void 0, [_0], function* ({
|
|
94
|
-
routeId,
|
|
95
|
-
loader,
|
|
96
|
-
params,
|
|
97
|
-
request,
|
|
98
|
-
loadContext
|
|
99
|
-
}) {
|
|
100
|
-
if (!loader) {
|
|
101
|
-
throw new Error(
|
|
102
|
-
`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.`
|
|
103
|
-
);
|
|
104
|
-
}
|
|
105
|
-
let result;
|
|
106
|
-
try {
|
|
107
|
-
result = yield loader({
|
|
108
|
-
request,
|
|
109
|
-
params
|
|
110
|
-
});
|
|
111
|
-
} catch (error) {
|
|
112
|
-
if (!isResponse(error)) {
|
|
113
|
-
throw error;
|
|
114
|
-
}
|
|
115
|
-
result = error;
|
|
116
|
-
}
|
|
117
|
-
if (result === void 0) {
|
|
118
|
-
throw new Error(
|
|
119
|
-
`You defined a loader for route "${routeId}" but didn't return anything from your \`loader\` function. Please return a value or \`null\`.`
|
|
120
|
-
);
|
|
121
|
-
}
|
|
122
|
-
return isResponse(result) ? result : json(result);
|
|
123
|
-
});
|
|
124
|
-
const createLoaderHeaders = (requestHeaders) => {
|
|
125
|
-
const headers = new Headers();
|
|
126
|
-
for (const [key, values] of Object.entries(requestHeaders)) {
|
|
127
|
-
if (values) {
|
|
128
|
-
if (Array.isArray(values)) {
|
|
129
|
-
for (const value of values) {
|
|
130
|
-
headers.append(key, value);
|
|
131
|
-
}
|
|
132
|
-
} else {
|
|
133
|
-
headers.set(key, values);
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
return headers;
|
|
138
|
-
};
|
|
139
|
-
const createLoaderRequest = (context) => {
|
|
140
|
-
const origin = `${context.protocol}://${context.host}`;
|
|
141
|
-
const url = new URL(context.url, origin);
|
|
142
|
-
const controller = new AbortController();
|
|
143
|
-
const init = {
|
|
144
|
-
method: context.method,
|
|
145
|
-
headers: createLoaderHeaders(context.headers),
|
|
146
|
-
signal: controller.signal
|
|
147
|
-
};
|
|
148
|
-
return new Request(url.href, init);
|
|
149
|
-
};
|
|
150
|
-
const sendLoaderResponse = (res, nodeResponse) => __async(void 0, null, function* () {
|
|
151
|
-
res.statusMessage = nodeResponse.statusText;
|
|
152
|
-
res.statusCode = nodeResponse.status;
|
|
153
|
-
for (const [key, value] of nodeResponse.headers.entries()) {
|
|
154
|
-
res.setHeader(key, value);
|
|
155
|
-
}
|
|
156
|
-
if (nodeResponse.body) {
|
|
157
|
-
yield (0, import_node.writeReadableStreamToWritable)(nodeResponse.body, res);
|
|
158
|
-
} else {
|
|
159
|
-
res.end();
|
|
160
|
-
}
|
|
161
|
-
});
|
|
162
|
-
const getPathWithoutEntry = (pathname, entryPath) => {
|
|
163
|
-
if (entryPath === "/") {
|
|
164
|
-
return pathname;
|
|
165
|
-
}
|
|
166
|
-
return pathname.replace(entryPath, "");
|
|
167
|
-
};
|
|
168
|
-
const matchEntry = (pathname, entries) => {
|
|
169
|
-
return entries.find((entry) => pathname.startsWith(entry.urlPath));
|
|
170
|
-
};
|
|
171
|
-
const handleRequest = (_0) => __async(void 0, [_0], function* ({
|
|
172
|
-
context,
|
|
173
|
-
serverRoutes,
|
|
174
|
-
distDir
|
|
175
|
-
}) {
|
|
176
|
-
const { method, query } = context;
|
|
177
|
-
const routeId = query[import_constants.LOADER_ID_PARAM];
|
|
178
|
-
if (!routeId || method.toLowerCase() !== "get") {
|
|
179
|
-
return;
|
|
180
|
-
}
|
|
181
|
-
const entry = matchEntry(context.path, serverRoutes);
|
|
182
|
-
if (!entry) {
|
|
183
|
-
return;
|
|
184
|
-
}
|
|
185
|
-
const routesPath = import_path.default.join(
|
|
186
|
-
distDir,
|
|
187
|
-
import_utils.SERVER_BUNDLE_DIRECTORY,
|
|
188
|
-
`${entry.entryName || import_utils.MAIN_ENTRY_NAME}-server-loaders`
|
|
189
|
-
);
|
|
190
|
-
const { routes } = yield Promise.resolve().then(() => __toESM(require(routesPath)));
|
|
191
|
-
if (!routes) {
|
|
192
|
-
return;
|
|
193
|
-
}
|
|
194
|
-
const { res } = context;
|
|
195
|
-
const pathname = getPathWithoutEntry(context.path, entry.urlPath);
|
|
196
|
-
const matches = (0, import_react_router_dom.matchRoutes)(routes, pathname);
|
|
197
|
-
if (!matches) {
|
|
198
|
-
res.statusCode = 403;
|
|
199
|
-
res.end(`Route ${pathname} was not matched`);
|
|
200
|
-
return;
|
|
201
|
-
}
|
|
202
|
-
const match = matches == null ? void 0 : matches.find((match2) => match2.route.id === routeId);
|
|
203
|
-
if (!match) {
|
|
204
|
-
res.statusCode = 403;
|
|
205
|
-
res.end(`Route ${routeId} does not match URL ${context.path}`);
|
|
206
|
-
return;
|
|
207
|
-
}
|
|
208
|
-
const request = createLoaderRequest(context);
|
|
209
|
-
let response;
|
|
210
|
-
try {
|
|
211
|
-
response = yield callRouteLoader({
|
|
212
|
-
loader: match.route.loader,
|
|
213
|
-
routeId: match.route.id,
|
|
214
|
-
params: match.params,
|
|
215
|
-
request,
|
|
216
|
-
loadContext: {}
|
|
217
|
-
});
|
|
218
|
-
if (isRedirectResponse(response)) {
|
|
219
|
-
const headers = new Headers(response.headers);
|
|
220
|
-
headers.set("X-Modernjs-Redirect", headers.get("Location"));
|
|
221
|
-
headers.delete("Location");
|
|
222
|
-
response = new import_node.Response(null, {
|
|
223
|
-
status: 204,
|
|
224
|
-
headers
|
|
225
|
-
});
|
|
226
|
-
}
|
|
227
|
-
} catch (error) {
|
|
228
|
-
const message = String(error);
|
|
229
|
-
response = new import_node.Response(message, {
|
|
230
|
-
status: 500,
|
|
231
|
-
headers: {
|
|
232
|
-
"Content-Type": "text/plain"
|
|
233
|
-
}
|
|
234
|
-
});
|
|
235
|
-
}
|
|
236
|
-
sendLoaderResponse(res, response);
|
|
237
|
-
});
|
|
238
|
-
var server_default = () => ({
|
|
239
|
-
name: "@modern-js/plugin-data-loader",
|
|
240
|
-
setup: () => ({
|
|
241
|
-
preparebeforeRouteHandler({
|
|
242
|
-
serverRoutes,
|
|
243
|
-
distDir
|
|
244
|
-
}) {
|
|
245
|
-
return (context) => __async(this, null, function* () {
|
|
246
|
-
return handleRequest({
|
|
247
|
-
serverRoutes,
|
|
248
|
-
distDir,
|
|
249
|
-
context
|
|
250
|
-
});
|
|
251
|
-
});
|
|
252
|
-
}
|
|
253
|
-
})
|
|
254
|
-
});
|
|
255
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
256
|
-
0 && (module.exports = {
|
|
257
|
-
getPathWithoutEntry,
|
|
258
|
-
handleRequest,
|
|
259
|
-
isRedirectResponse,
|
|
260
|
-
isResponse
|
|
261
|
-
});
|