@analogjs/router 3.0.0-beta.39 → 3.0.0-beta.40

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,8 +35,9 @@ function assertNodeContext(event) {
35
35
  }
36
36
  /**
37
37
  * A Node request/response pair for a bare `Request`, carrying what Analog
38
- * reads: method, URL, headers and protocol. The same approach as h3's web
39
- * adapter; it needs `node:http` (Node, Bun, Deno).
38
+ * reads: method, URL, headers and protocol, plus the `Request` and its abort
39
+ * `signal`. The same approach as h3's web adapter; it needs `node:http` (Node,
40
+ * Bun, Deno).
40
41
  */
41
42
  function nodeContext(request, url) {
42
43
  const socket = new Socket();
@@ -46,6 +47,10 @@ function nodeContext(request, url) {
46
47
  req.method = request.method;
47
48
  req.url = path;
48
49
  Object.assign(req, { originalUrl: path });
50
+ Object.defineProperties(req, {
51
+ signal: { value: request.signal },
52
+ request: { value: request }
53
+ });
49
54
  req.headers = {
50
55
  host: url.host,
51
56
  ...Object.fromEntries(request.headers)
@@ -210,7 +215,7 @@ function isServerFnOriginAllowed(headers, allowedOrigins = []) {
210
215
  */
211
216
  async function dispatchServerFn(id, rawInput, event, options = {}) {
212
217
  const { parent, providers = [], method, allowedOrigins = [] } = options;
213
- const node = assertNodeContext(event);
218
+ const node = event.node?.req || !(event.req instanceof Request) ? assertNodeContext(event) : nodeContext(event.req, new URL(event.req.url));
214
219
  const headers = node.req.headers ?? {};
215
220
  if (method) {
216
221
  if (!isServerFnOriginAllowed(headers, [...allowedOrigins, ...parent?.get(SERVER_FN_ALLOWED_ORIGINS, []) ?? []])) return {
@@ -1466,21 +1471,16 @@ function createServerFnEventHandler(appInjector) {
1466
1471
  async function handleServerFnRequest(event, appInjector) {
1467
1472
  const id = getRouterParam(event, "id") ?? "";
1468
1473
  if (!event.node?.req) return handleServerFnFetch(id, event.req, await appInjector);
1469
- const node = assertNodeContext(event);
1470
1474
  let input;
1471
1475
  if (event.method !== "GET") try {
1472
1476
  input = await readBody(event);
1473
1477
  } catch {
1474
- node.res.statusCode = 400;
1475
- return { message: "Malformed request body" };
1478
+ return malformedBodyResponse();
1476
1479
  }
1477
- const { status, body, headers } = await dispatchServerFn(id, input, event, {
1480
+ return serverFnResponse(await dispatchServerFn(id, input, event, {
1478
1481
  parent: await appInjector,
1479
1482
  method: event.method
1480
- });
1481
- node.res.statusCode = status;
1482
- if (headers) for (const [key, value] of Object.entries(headers)) node.res.setHeader(key, value);
1483
- return body;
1483
+ }));
1484
1484
  }
1485
1485
  /** h3 v2 on fetch-native hosts (workerd, Bun, Deno) has a `Request` only. */
1486
1486
  async function handleServerFnFetch(id, request, parent) {
@@ -1494,13 +1494,15 @@ async function callServerFn(id, request, context, parent) {
1494
1494
  try {
1495
1495
  input = text ? JSON.parse(text) : void 0;
1496
1496
  } catch {
1497
- return Response.json({ message: "Malformed request body" }, { status: 400 });
1497
+ return malformedBodyResponse();
1498
1498
  }
1499
1499
  }
1500
- const { status, body, headers } = await dispatchServerFn(id, input, { node: context }, {
1500
+ return serverFnResponse(await dispatchServerFn(id, input, { node: context }, {
1501
1501
  parent,
1502
1502
  method: request.method
1503
- });
1503
+ }));
1504
+ }
1505
+ function serverFnResponse({ status, body, headers }) {
1504
1506
  const responseHeaders = new Headers();
1505
1507
  for (const [key, value] of Object.entries(headers ?? {})) for (const item of [value].flat()) responseHeaders.append(key, item);
1506
1508
  if (isNullBodyStatus(status)) return new Response(null, {
@@ -1512,6 +1514,9 @@ async function callServerFn(id, request, context, parent) {
1512
1514
  headers: responseHeaders
1513
1515
  });
1514
1516
  }
1517
+ function malformedBodyResponse() {
1518
+ return Response.json({ message: "Malformed request body" }, { status: 400 });
1519
+ }
1515
1520
  function isNullBodyStatus(status) {
1516
1521
  return status === 204 || status === 205 || status === 304;
1517
1522
  }