@marko/run 0.2.5 → 0.2.7

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.
@@ -18,6 +18,10 @@ var __copyProps = (to, from, except, desc) => {
18
18
  return to;
19
19
  };
20
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.
21
25
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
22
26
  mod
23
27
  ));
@@ -132,40 +136,11 @@ function getSetCookie_fallback(headers) {
132
136
  }
133
137
  return value;
134
138
  }
135
- var HRMClientIdCookieName = "hmr-client-id";
136
- function getHMRClientId(req) {
137
- if (req.headers.cookie) {
138
- const cookie = req.headers.cookie.split(/;\s+/).find((c) => c.startsWith(HRMClientIdCookieName));
139
- if (cookie) {
140
- return cookie.slice(HRMClientIdCookieName.length + 1);
141
- }
142
- }
143
- }
144
139
  function createMiddleware(fetch2, options = {}) {
145
140
  const {
146
141
  origin = process.env.ORIGIN,
147
- trustProxy = process.env.TRUST_PROXY === "1",
148
- devServer
142
+ trustProxy = process.env.TRUST_PROXY === "1"
149
143
  } = options;
150
- let hmrCallbacks;
151
- if (process.env.NODE_ENV !== "production" && devServer) {
152
- hmrCallbacks = [];
153
- devServer.ws.on("connection", (ws, req) => {
154
- if (hmrCallbacks == null ? void 0 : hmrCallbacks.length) {
155
- const id = getHMRClientId(req);
156
- const now = Date.now();
157
- const nextHMRCallbacks = [];
158
- for (const entry of hmrCallbacks) {
159
- if (entry.id === id) {
160
- entry.callback(ws);
161
- } else if (entry.expires > now) {
162
- nextHMRCallbacks.push(entry);
163
- }
164
- }
165
- hmrCallbacks = nextHMRCallbacks;
166
- }
167
- });
168
- }
169
144
  return async (req, res, next) => {
170
145
  var _a;
171
146
  const controller = new AbortController();
@@ -177,50 +152,51 @@ function createMiddleware(fetch2, options = {}) {
177
152
  req.socket.on("error", onErrorOrClose);
178
153
  res.on("error", onErrorOrClose);
179
154
  res.on("close", onErrorOrClose);
155
+ signal.addEventListener("abort", onSignalAborted);
180
156
  function onErrorOrClose(err) {
181
157
  req.off("error", onErrorOrClose);
182
158
  req.socket.off("error", onErrorOrClose);
183
159
  res.off("error", onErrorOrClose);
184
160
  res.off("close", onErrorOrClose);
185
161
  if (err) {
162
+ signal.removeEventListener("abort", onSignalAborted);
186
163
  controller.abort(err);
187
164
  }
188
165
  }
189
- let hmrId;
190
- if (process.env.NODE_ENV !== "production" && hmrCallbacks && ((_a = req.headers.accept) == null ? void 0 : _a.includes("text/html"))) {
191
- const expires = Date.now() + 1e3;
192
- hmrId = Math.floor(Math.random() * expires).toString(36);
193
- hmrCallbacks.push({
194
- id: hmrId,
195
- expires,
196
- callback(ws) {
197
- if (signal.aborted) {
198
- sendError();
199
- } else {
200
- signal.addEventListener("abort", sendError);
201
- }
202
- function sendError() {
203
- const { message, stack = "" } = signal.reason;
204
- ws.send(
205
- JSON.stringify({
206
- type: "error",
207
- err: { message, stack }
208
- })
209
- );
210
- }
211
- }
212
- });
213
- } else {
214
- signal.addEventListener("abort", () => {
166
+ function onSignalAborted() {
167
+ if (next) {
168
+ next(signal.reason);
169
+ } else {
215
170
  if (!res.destroyed && res.socket) {
216
171
  res.socket.destroySoon();
217
172
  }
173
+ console.error(signal.reason);
174
+ }
175
+ }
176
+ let setDevClientId;
177
+ if (process.env.NODE_ENV !== "production" && globalThis.__marko_run_dev__ && ((_a = req.headers.accept) == null ? void 0 : _a.includes("text/html"))) {
178
+ setDevClientId = globalThis.__marko_run_dev__.onClient((ws) => {
179
+ if (signal.aborted) {
180
+ sendError();
181
+ } else {
182
+ signal.addEventListener("abort", sendError);
183
+ }
184
+ function sendError() {
185
+ const { message, stack = "" } = signal.reason;
186
+ ws.send(
187
+ JSON.stringify({
188
+ type: "error",
189
+ err: { message, stack }
190
+ })
191
+ );
192
+ }
218
193
  });
219
194
  }
220
195
  const request = new Request(url, {
221
196
  method: req.method,
222
197
  headers,
223
198
  body: req.method === "GET" || req.method === "HEAD" ? void 0 : req,
199
+ // @ts-expect-error: Node requires this for streams
224
200
  duplex: "half",
225
201
  signal
226
202
  });
@@ -235,11 +211,8 @@ function createMiddleware(fetch2, options = {}) {
235
211
  }
236
212
  return;
237
213
  }
238
- if (process.env.NODE_ENV !== "production" && hmrId) {
239
- response.headers.append(
240
- "set-cookie",
241
- `${HRMClientIdCookieName}=${hmrId}; Path=/; Max-Age=100; HttpOnly`
242
- );
214
+ if (process.env.NODE_ENV !== "production" && setDevClientId) {
215
+ setDevClientId(response);
243
216
  }
244
217
  res.statusCode = response.status;
245
218
  setResponseHeaders(response, res);
@@ -2,14 +2,13 @@
2
2
  import "./polyfill";
3
3
  import type { Fetch } from "../runtime";
4
4
  import type { IncomingMessage, ServerResponse, OutgoingMessage } from "http";
5
- import type { ViteDevServer } from "vite";
6
5
  export interface NodePlatformInfo {
7
6
  ip: string;
8
7
  request: IncomingMessage;
9
8
  response: ServerResponse;
10
9
  }
11
10
  /** Connect/Express style request listener/middleware */
12
- export type NodeMiddleware = (req: IncomingMessage, res: ServerResponse, next?: () => void) => void;
11
+ export type NodeMiddleware = (req: IncomingMessage, res: ServerResponse, next?: (error?: Error) => void) => void;
13
12
  /** Adapter options */
14
13
  export interface NodeAdapterOptions {
15
14
  /**
@@ -32,7 +31,6 @@ export interface NodeAdapterOptions {
32
31
  * is set to `1`, otherwise false.
33
32
  */
34
33
  trustProxy?: boolean;
35
- devServer?: ViteDevServer;
36
34
  }
37
35
  export declare function getOrigin(req: IncomingMessage, trustProxy?: boolean): string;
38
36
  export declare function setResponseHeaders(response: Response, res: OutgoingMessage): void;
@@ -93,40 +93,11 @@ function getSetCookie_fallback(headers) {
93
93
  }
94
94
  return value;
95
95
  }
96
- var HRMClientIdCookieName = "hmr-client-id";
97
- function getHMRClientId(req) {
98
- if (req.headers.cookie) {
99
- const cookie = req.headers.cookie.split(/;\s+/).find((c) => c.startsWith(HRMClientIdCookieName));
100
- if (cookie) {
101
- return cookie.slice(HRMClientIdCookieName.length + 1);
102
- }
103
- }
104
- }
105
96
  function createMiddleware(fetch2, options = {}) {
106
97
  const {
107
98
  origin = process.env.ORIGIN,
108
- trustProxy = process.env.TRUST_PROXY === "1",
109
- devServer
99
+ trustProxy = process.env.TRUST_PROXY === "1"
110
100
  } = options;
111
- let hmrCallbacks;
112
- if (process.env.NODE_ENV !== "production" && devServer) {
113
- hmrCallbacks = [];
114
- devServer.ws.on("connection", (ws, req) => {
115
- if (hmrCallbacks == null ? void 0 : hmrCallbacks.length) {
116
- const id = getHMRClientId(req);
117
- const now = Date.now();
118
- const nextHMRCallbacks = [];
119
- for (const entry of hmrCallbacks) {
120
- if (entry.id === id) {
121
- entry.callback(ws);
122
- } else if (entry.expires > now) {
123
- nextHMRCallbacks.push(entry);
124
- }
125
- }
126
- hmrCallbacks = nextHMRCallbacks;
127
- }
128
- });
129
- }
130
101
  return async (req, res, next) => {
131
102
  var _a;
132
103
  const controller = new AbortController();
@@ -138,50 +109,51 @@ function createMiddleware(fetch2, options = {}) {
138
109
  req.socket.on("error", onErrorOrClose);
139
110
  res.on("error", onErrorOrClose);
140
111
  res.on("close", onErrorOrClose);
112
+ signal.addEventListener("abort", onSignalAborted);
141
113
  function onErrorOrClose(err) {
142
114
  req.off("error", onErrorOrClose);
143
115
  req.socket.off("error", onErrorOrClose);
144
116
  res.off("error", onErrorOrClose);
145
117
  res.off("close", onErrorOrClose);
146
118
  if (err) {
119
+ signal.removeEventListener("abort", onSignalAborted);
147
120
  controller.abort(err);
148
121
  }
149
122
  }
150
- let hmrId;
151
- if (process.env.NODE_ENV !== "production" && hmrCallbacks && ((_a = req.headers.accept) == null ? void 0 : _a.includes("text/html"))) {
152
- const expires = Date.now() + 1e3;
153
- hmrId = Math.floor(Math.random() * expires).toString(36);
154
- hmrCallbacks.push({
155
- id: hmrId,
156
- expires,
157
- callback(ws) {
158
- if (signal.aborted) {
159
- sendError();
160
- } else {
161
- signal.addEventListener("abort", sendError);
162
- }
163
- function sendError() {
164
- const { message, stack = "" } = signal.reason;
165
- ws.send(
166
- JSON.stringify({
167
- type: "error",
168
- err: { message, stack }
169
- })
170
- );
171
- }
172
- }
173
- });
174
- } else {
175
- signal.addEventListener("abort", () => {
123
+ function onSignalAborted() {
124
+ if (next) {
125
+ next(signal.reason);
126
+ } else {
176
127
  if (!res.destroyed && res.socket) {
177
128
  res.socket.destroySoon();
178
129
  }
130
+ console.error(signal.reason);
131
+ }
132
+ }
133
+ let setDevClientId;
134
+ if (process.env.NODE_ENV !== "production" && globalThis.__marko_run_dev__ && ((_a = req.headers.accept) == null ? void 0 : _a.includes("text/html"))) {
135
+ setDevClientId = globalThis.__marko_run_dev__.onClient((ws) => {
136
+ if (signal.aborted) {
137
+ sendError();
138
+ } else {
139
+ signal.addEventListener("abort", sendError);
140
+ }
141
+ function sendError() {
142
+ const { message, stack = "" } = signal.reason;
143
+ ws.send(
144
+ JSON.stringify({
145
+ type: "error",
146
+ err: { message, stack }
147
+ })
148
+ );
149
+ }
179
150
  });
180
151
  }
181
152
  const request = new Request(url, {
182
153
  method: req.method,
183
154
  headers,
184
155
  body: req.method === "GET" || req.method === "HEAD" ? void 0 : req,
156
+ // @ts-expect-error: Node requires this for streams
185
157
  duplex: "half",
186
158
  signal
187
159
  });
@@ -196,11 +168,8 @@ function createMiddleware(fetch2, options = {}) {
196
168
  }
197
169
  return;
198
170
  }
199
- if (process.env.NODE_ENV !== "production" && hmrId) {
200
- response.headers.append(
201
- "set-cookie",
202
- `${HRMClientIdCookieName}=${hmrId}; Path=/; Max-Age=100; HttpOnly`
203
- );
171
+ if (process.env.NODE_ENV !== "production" && setDevClientId) {
172
+ setDevClientId(response);
204
173
  }
205
174
  res.statusCode = response.status;
206
175
  setResponseHeaders(response, res);