@modern-js/plugin-data-loader 2.5.0-alpha.0 → 2.6.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.
@@ -24,7 +24,6 @@ module.exports = __toCommonJS(create_request_exports);
24
24
  var import_path_to_regexp = require("path-to-regexp");
25
25
  var import_react_router_dom = require("react-router-dom");
26
26
  var import_constants = require("../common/constants");
27
- var import_data = require("./data");
28
27
  const getRequestUrl = ({
29
28
  params,
30
29
  request,
@@ -44,14 +43,7 @@ const handleRedirectResponse = (res) => {
44
43
  const { headers } = res;
45
44
  const location = headers.get("X-Modernjs-Redirect");
46
45
  if (location) {
47
- throw (0, import_react_router_dom.redirect)(location);
48
- }
49
- return res;
50
- };
51
- const handleDeferredResponse = async (res) => {
52
- var _a;
53
- if (((_a = res.headers.get("Content-Type")) == null ? void 0 : _a.match(/text\/modernjs-deferred/)) && res.body) {
54
- return await (0, import_data.parseDeferredReadableStream)(res.body);
46
+ return (0, import_react_router_dom.redirect)(location);
55
47
  }
56
48
  return res;
57
49
  };
@@ -61,17 +53,14 @@ const createRequest = (routeId, method = "get") => {
61
53
  request
62
54
  }) => {
63
55
  const url = getRequestUrl({ params, request, routeId });
64
- let res;
65
- res = await fetch(url, {
56
+ const res = await fetch(url, {
66
57
  method,
67
58
  signal: request.signal
68
59
  });
69
60
  if (!res.ok) {
70
61
  throw res;
71
62
  }
72
- res = handleRedirectResponse(res);
73
- res = await handleDeferredResponse(res);
74
- return res;
63
+ return handleRedirectResponse(res);
75
64
  };
76
65
  };
77
66
  // Annotate the CommonJS export names for ESM import in node:
@@ -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
  ));
@@ -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,
@@ -17,18 +17,193 @@ 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
  ));
23
27
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
24
28
  var server_exports = {};
25
29
  __export(server_exports, {
26
- default: () => server_default
30
+ default: () => server_default,
31
+ getPathWithoutEntry: () => getPathWithoutEntry,
32
+ handleRequest: () => handleRequest,
33
+ isRedirectResponse: () => isRedirectResponse,
34
+ isResponse: () => isResponse
27
35
  });
28
36
  module.exports = __toCommonJS(server_exports);
29
37
  var import_path = __toESM(require("path"));
38
+ var import_node = require("@remix-run/node");
39
+ var import_react_router_dom = require("react-router-dom");
30
40
  var import_utils = require("@modern-js/utils");
31
- var import_runtime = require("../runtime");
41
+ var import_constants = require("../common/constants");
42
+ (0, import_node.installGlobals)();
43
+ const redirectStatusCodes = /* @__PURE__ */ new Set([301, 302, 303, 307, 308]);
44
+ function isRedirectResponse(response) {
45
+ return redirectStatusCodes.has(response.status);
46
+ }
47
+ function isResponse(value) {
48
+ return value != null && typeof value.status === "number" && typeof value.statusText === "string" && typeof value.headers === "object" && typeof value.body !== "undefined";
49
+ }
50
+ const json = (data, init = {}) => {
51
+ const responseInit = typeof init === "number" ? { status: init } : init;
52
+ const headers = new Headers(responseInit.headers);
53
+ if (!headers.has("Content-Type")) {
54
+ headers.set("Content-Type", "application/json; charset=utf-8");
55
+ }
56
+ return new import_node.Response(JSON.stringify(data), {
57
+ ...responseInit,
58
+ headers
59
+ });
60
+ };
61
+ const callRouteLoader = async ({
62
+ routeId,
63
+ loader,
64
+ params,
65
+ request,
66
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
67
+ loadContext
68
+ }) => {
69
+ if (!loader) {
70
+ throw new Error(
71
+ `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.`
72
+ );
73
+ }
74
+ let result;
75
+ try {
76
+ result = await loader({
77
+ request,
78
+ params
79
+ });
80
+ } catch (error) {
81
+ if (!isResponse(error)) {
82
+ throw error;
83
+ }
84
+ result = error;
85
+ }
86
+ if (result === void 0) {
87
+ throw new Error(
88
+ `You defined a loader for route "${routeId}" but didn't return anything from your \`loader\` function. Please return a value or \`null\`.`
89
+ );
90
+ }
91
+ return isResponse(result) ? result : json(result);
92
+ };
93
+ const createLoaderHeaders = (requestHeaders) => {
94
+ const headers = new Headers();
95
+ for (const [key, values] of Object.entries(requestHeaders)) {
96
+ if (values) {
97
+ if (Array.isArray(values)) {
98
+ for (const value of values) {
99
+ headers.append(key, value);
100
+ }
101
+ } else {
102
+ headers.set(key, values);
103
+ }
104
+ }
105
+ }
106
+ return headers;
107
+ };
108
+ const createLoaderRequest = (context) => {
109
+ const origin = `${context.protocol}://${context.host}`;
110
+ const url = new URL(context.url, origin);
111
+ const controller = new AbortController();
112
+ const init = {
113
+ method: context.method,
114
+ headers: createLoaderHeaders(context.headers),
115
+ signal: controller.signal
116
+ };
117
+ return new Request(url.href, init);
118
+ };
119
+ const sendLoaderResponse = async (res, nodeResponse) => {
120
+ res.statusMessage = nodeResponse.statusText;
121
+ res.statusCode = nodeResponse.status;
122
+ for (const [key, value] of nodeResponse.headers.entries()) {
123
+ res.setHeader(key, value);
124
+ }
125
+ if (nodeResponse.body) {
126
+ await (0, import_node.writeReadableStreamToWritable)(nodeResponse.body, res);
127
+ } else {
128
+ res.end();
129
+ }
130
+ };
131
+ const getPathWithoutEntry = (pathname, entryPath) => {
132
+ if (entryPath === "/") {
133
+ return pathname;
134
+ }
135
+ return pathname.replace(entryPath, "");
136
+ };
137
+ const matchEntry = (pathname, entries) => {
138
+ return entries.find((entry) => pathname.startsWith(entry.urlPath));
139
+ };
140
+ const handleRequest = async ({
141
+ context,
142
+ serverRoutes,
143
+ distDir
144
+ }) => {
145
+ const { method, query } = context;
146
+ const routeId = query[import_constants.LOADER_ID_PARAM];
147
+ if (!routeId || method.toLowerCase() !== "get") {
148
+ return;
149
+ }
150
+ const entry = matchEntry(context.path, serverRoutes);
151
+ if (!entry) {
152
+ return;
153
+ }
154
+ const routesPath = import_path.default.join(
155
+ distDir,
156
+ import_utils.SERVER_BUNDLE_DIRECTORY,
157
+ `${entry.entryName || import_utils.MAIN_ENTRY_NAME}-server-loaders`
158
+ );
159
+ const { routes } = await Promise.resolve().then(() => __toESM(require(routesPath)));
160
+ if (!routes) {
161
+ return;
162
+ }
163
+ const { res } = context;
164
+ const pathname = getPathWithoutEntry(context.path, entry.urlPath);
165
+ const matches = (0, import_react_router_dom.matchRoutes)(routes, pathname);
166
+ if (!matches) {
167
+ res.statusCode = 403;
168
+ res.end(`Route ${pathname} was not matched`);
169
+ return;
170
+ }
171
+ const match = matches == null ? void 0 : matches.find((match2) => match2.route.id === routeId);
172
+ if (!match) {
173
+ res.statusCode = 403;
174
+ res.end(`Route ${routeId} does not match URL ${context.path}`);
175
+ return;
176
+ }
177
+ const request = createLoaderRequest(context);
178
+ let response;
179
+ try {
180
+ response = await callRouteLoader({
181
+ loader: match.route.loader,
182
+ routeId: match.route.id,
183
+ params: match.params,
184
+ request,
185
+ loadContext: {}
186
+ });
187
+ if (isRedirectResponse(response)) {
188
+ const headers = new Headers(response.headers);
189
+ headers.set("X-Modernjs-Redirect", headers.get("Location"));
190
+ headers.delete("Location");
191
+ response = new import_node.Response(null, {
192
+ status: 204,
193
+ headers
194
+ });
195
+ }
196
+ } catch (error) {
197
+ const message = String(error);
198
+ response = new import_node.Response(message, {
199
+ status: 500,
200
+ headers: {
201
+ "Content-Type": "text/plain"
202
+ }
203
+ });
204
+ }
205
+ sendLoaderResponse(res, response);
206
+ };
32
207
  var server_default = () => ({
33
208
  name: "@modern-js/plugin-data-loader",
34
209
  setup: () => ({
@@ -37,28 +212,19 @@ var server_default = () => ({
37
212
  distDir
38
213
  }) {
39
214
  return async (context) => {
40
- const entry = (0, import_runtime.matchEntry)(context.path, serverRoutes);
41
- if (!entry) {
42
- throw new Error("Route is not matched");
43
- }
44
- const routesPath = import_path.default.join(
45
- distDir,
46
- import_utils.SERVER_BUNDLE_DIRECTORY,
47
- `${entry.entryName || import_utils.MAIN_ENTRY_NAME}-server-loaders`
48
- );
49
- const { routes, handleRequest } = await Promise.resolve().then(() => __toESM(require(routesPath)));
50
- if (!routes) {
51
- throw new Error("Missing route info");
52
- ;
53
- }
54
215
  return handleRequest({
55
216
  serverRoutes,
56
- context,
57
- routes
217
+ distDir,
218
+ context
58
219
  });
59
220
  };
60
221
  }
61
222
  })
62
223
  });
63
224
  // Annotate the CommonJS export names for ESM import in node:
64
- 0 && (module.exports = {});
225
+ 0 && (module.exports = {
226
+ getPathWithoutEntry,
227
+ handleRequest,
228
+ isRedirectResponse,
229
+ isResponse
230
+ });
@@ -125,7 +125,6 @@ var __generator = this && this.__generator || function(thisArg, body) {
125
125
  import { compile } from "path-to-regexp";
126
126
  import { redirect } from "react-router-dom";
127
127
  import { LOADER_ID_PARAM, DIRECT_PARAM } from "../common/constants";
128
- import { parseDeferredReadableStream } from "./data";
129
128
  var getRequestUrl = function(param) {
130
129
  var params = param.params, request = param.request, routeId = param.routeId;
131
130
  var url = new URL(request.url);
@@ -142,41 +141,10 @@ var handleRedirectResponse = function(res) {
142
141
  var headers = res.headers;
143
142
  var location = headers.get("X-Modernjs-Redirect");
144
143
  if (location) {
145
- throw redirect(location);
144
+ return redirect(location);
146
145
  }
147
146
  return res;
148
147
  };
149
- var handleDeferredResponse = function() {
150
- var _ref = _asyncToGenerator(function(res) {
151
- var _res_headers_get;
152
- return __generator(this, function(_state) {
153
- switch(_state.label){
154
- case 0:
155
- if (!(((_res_headers_get = res.headers.get("Content-Type")) === null || _res_headers_get === void 0 ? void 0 : _res_headers_get.match(/text\/modernjs-deferred/)) && res.body)) return [
156
- 3,
157
- 2
158
- ];
159
- return [
160
- 4,
161
- parseDeferredReadableStream(res.body)
162
- ];
163
- case 1:
164
- return [
165
- 2,
166
- _state.sent()
167
- ];
168
- case 2:
169
- return [
170
- 2,
171
- res
172
- ];
173
- }
174
- });
175
- });
176
- return function handleDeferredResponse(res) {
177
- return _ref.apply(this, arguments);
178
- };
179
- }();
180
148
  var createRequest = function(routeId) {
181
149
  var method = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : "get";
182
150
  return function() {
@@ -203,16 +171,9 @@ var createRequest = function(routeId) {
203
171
  if (!res.ok) {
204
172
  throw res;
205
173
  }
206
- res = handleRedirectResponse(res);
207
- return [
208
- 4,
209
- handleDeferredResponse(res)
210
- ];
211
- case 2:
212
- res = _state.sent();
213
174
  return [
214
175
  2,
215
- res
176
+ handleRedirectResponse(res)
216
177
  ];
217
178
  }
218
179
  });
@@ -138,6 +138,12 @@ function _loader() {
138
138
  source
139
139
  ];
140
140
  }
141
+ if (target === "webworker") {
142
+ return [
143
+ 2,
144
+ source
145
+ ];
146
+ }
141
147
  options = this.getOptions();
142
148
  code = generateClient({
143
149
  mapFile: options.mapFile,