@catmint/adapter-node 0.0.0-prealpha.12 → 0.0.0-prealpha.15

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.
@@ -1 +1 @@
1
- {"version":3,"file":"server-template.d.ts","sourceRoot":"","sources":["../src/server-template.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAElD,UAAU,aAAa;IACrB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,CAAC;IACpB,YAAY,EAAE,UAAU,GAAG,MAAM,CAAC;CACnC;AAED;;;;;;;;;GASG;AACH,wBAAgB,mBAAmB,CACjC,SAAS,EAAE,WAAW,EACtB,OAAO,EAAE,aAAa,GACrB,MAAM,CA8jBR"}
1
+ {"version":3,"file":"server-template.d.ts","sourceRoot":"","sources":["../src/server-template.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAElD,UAAU,aAAa;IACrB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,CAAC;IACpB,YAAY,EAAE,UAAU,GAAG,MAAM,CAAC;CACnC;AAED;;;;;;;;;GASG;AACH,wBAAgB,mBAAmB,CACjC,SAAS,EAAE,WAAW,EACtB,OAAO,EAAE,aAAa,GACrB,MAAM,CAgnBR"}
@@ -269,6 +269,30 @@ ${options.headerPreset === "baseline"
269
269
  const pathname = url.pathname;
270
270
  const method = (req.method || "GET").toUpperCase();
271
271
 
272
+ // Establish the request-scoped AsyncLocalStorage context.
273
+ // This makes cookies(), headers(), getPlatform() etc. available
274
+ // to server functions, middleware, API endpoints, and RSC rendering.
275
+ const __webRequest = nodeReqToWebRequest(req);
276
+ const __store = ssrEntry.createRequestStore(__webRequest, { req, res });
277
+ await ssrEntry.runWithRequestContext(__store, async () => {
278
+
279
+ // Helper: flush pending Set-Cookie headers and accumulated response
280
+ // headers from the request store onto the Node.js response object.
281
+ function __flushPendingHeaders() {
282
+ // Apply accumulated response headers
283
+ __store.responseHeaders.forEach(function(value, key) {
284
+ res.setHeader(key, value);
285
+ });
286
+ // Apply pending Set-Cookie headers
287
+ var cookies = [];
288
+ __store.pendingCookies.forEach(function(pending) {
289
+ cookies.push(pending.serialized);
290
+ });
291
+ if (cookies.length > 0) {
292
+ res.setHeader("Set-Cookie", cookies);
293
+ }
294
+ }
295
+
272
296
  try {
273
297
  // 1. Serve client assets from dist/client/ (immutable for hashed assets)
274
298
  // These have hashed filenames and never conflict with app routes.
@@ -308,13 +332,14 @@ ${options.headerPreset === "baseline"
308
332
  var mwResult = await ssrEntry.executeMiddleware(middlewarePath, webRequest);
309
333
 
310
334
  if (mwResult.shortCircuit) {
311
- // Middleware short-circuited — return its response directly
312
- res.writeHead(mwResult.shortCircuit.status,
313
- Object.fromEntries(mwResult.shortCircuit.headers));
314
- var scBody = await mwResult.shortCircuit.text();
315
- res.end(scBody);
316
- return;
317
- }
335
+ // Middleware short-circuited — return its response directly
336
+ res.writeHead(mwResult.shortCircuit.status,
337
+ Object.fromEntries(mwResult.shortCircuit.headers));
338
+ __flushPendingHeaders();
339
+ var scBody = await mwResult.shortCircuit.text();
340
+ res.end(scBody);
341
+ return;
342
+ }
318
343
 
319
344
  // Middleware passed — capture headers to merge into final response
320
345
  if (mwResult.headers && typeof mwResult.headers.forEach === "function") {
@@ -323,6 +348,7 @@ ${options.headerPreset === "baseline"
323
348
  } catch (err) {
324
349
  console.error("Middleware error:", err);
325
350
  if (!res.headersSent) {
351
+ __flushPendingHeaders();
326
352
  res.writeHead(500);
327
353
  res.end("Internal Server Error");
328
354
  }
@@ -344,6 +370,7 @@ ${options.headerPreset === "baseline"
344
370
  try {
345
371
  // Enforce POST method for server function calls
346
372
  if (method !== "POST") {
373
+ __flushPendingHeaders();
347
374
  sendJson(res, 405, { error: "Method " + method + " not allowed, expected POST" });
348
375
  return;
349
376
  }
@@ -351,6 +378,7 @@ ${options.headerPreset === "baseline"
351
378
  // Enforce Content-Type: application/json
352
379
  const ct = (req.headers["content-type"] || "").toLowerCase();
353
380
  if (ct && !ct.startsWith("application/json")) {
381
+ __flushPendingHeaders();
354
382
  sendJson(res, 415, { error: "Unsupported Content-Type, expected application/json" });
355
383
  return;
356
384
  }
@@ -360,6 +388,7 @@ ${options.headerPreset === "baseline"
360
388
  if (origin) {
361
389
  const expected = \`http://\${HOST}:\${PORT}\`;
362
390
  if (origin !== expected && origin !== \`https://\${HOST}:\${PORT}\`) {
391
+ __flushPendingHeaders();
363
392
  sendJson(res, 403, { error: "Origin not allowed" });
364
393
  return;
365
394
  }
@@ -370,6 +399,7 @@ ${options.headerPreset === "baseline"
370
399
  try {
371
400
  parsed = body.length > 0 ? JSON.parse(body.toString("utf-8")) : undefined;
372
401
  } catch {
402
+ __flushPendingHeaders();
373
403
  sendJson(res, 400, { error: "Invalid JSON body" });
374
404
  return;
375
405
  }
@@ -378,6 +408,7 @@ ${options.headerPreset === "baseline"
378
408
  // Check if the result IS a ClientSafeError (returned, not thrown)
379
409
  if (__isClientSafeErrorLike(result.result)) {
380
410
  applyMiddlewareHeaders();
411
+ __flushPendingHeaders();
381
412
  sendJson(res, 200, {
382
413
  __clientSafeError: true,
383
414
  error: result.result.message,
@@ -387,9 +418,11 @@ ${options.headerPreset === "baseline"
387
418
  return;
388
419
  }
389
420
  applyMiddlewareHeaders();
421
+ __flushPendingHeaders();
390
422
  sendJson(res, 200, result.result);
391
423
  return;
392
424
  }
425
+ __flushPendingHeaders();
393
426
  sendJson(res, 404, { error: "Server function not found" });
394
427
  } catch (err) {
395
428
  // Always log full error server-side
@@ -397,12 +430,14 @@ ${options.headerPreset === "baseline"
397
430
 
398
431
  // RedirectError — send redirect envelope
399
432
  if (__isRedirectErrorLike(err)) {
433
+ __flushPendingHeaders();
400
434
  sendJson(res, 200, { __redirect: true, url: err.url, status: err.status });
401
435
  return;
402
436
  }
403
437
 
404
438
  // ClientSafeError — developer opted in, expose to client
405
439
  if (__isClientSafeErrorLike(err)) {
440
+ __flushPendingHeaders();
406
441
  sendJson(res, err.statusCode, { error: err.message, data: err.data });
407
442
  return;
408
443
  }
@@ -410,6 +445,7 @@ ${options.headerPreset === "baseline"
410
445
  // Default: sanitize — generic message with correlation hash
411
446
  const ref = __errorRef(err);
412
447
  console.error("[catmint] Error reference [ref: " + ref + "] — see above for full error");
448
+ __flushPendingHeaders();
413
449
  sendJson(res, 500, { error: "Internal Server Error [ref: " + ref + "]" });
414
450
  }
415
451
  return;
@@ -419,6 +455,7 @@ ${options.headerPreset === "baseline"
419
455
  if (pathname === "/__catmint/rsc" && method === "GET" && rscEntry.render) {
420
456
  const targetPath = url.searchParams.get("path");
421
457
  if (!targetPath) {
458
+ __flushPendingHeaders();
422
459
  res.setHeader("Content-Type", "application/json");
423
460
  res.writeHead(400);
424
461
  res.end(JSON.stringify({ error: "Missing ?path= parameter" }));
@@ -428,6 +465,7 @@ ${options.headerPreset === "baseline"
428
465
  const rscResult = await rscEntry.render(targetPath);
429
466
  if (rscResult) {
430
467
  applyMiddlewareHeaders();
468
+ __flushPendingHeaders();
431
469
  res.writeHead(200, {
432
470
  "Content-Type": "text/x-component; charset=utf-8",
433
471
  "Cache-Control": "no-cache, no-store, must-revalidate",
@@ -435,12 +473,14 @@ ${options.headerPreset === "baseline"
435
473
  await pipeWebStreamToResponse(rscResult.stream, res);
436
474
  return;
437
475
  }
476
+ __flushPendingHeaders();
438
477
  res.setHeader("Content-Type", "application/json");
439
478
  res.writeHead(404);
440
479
  res.end(JSON.stringify({ error: "No matching route" }));
441
480
  } catch (err) {
442
481
  console.error("RSC navigation error:", err);
443
482
  if (!res.headersSent) {
483
+ __flushPendingHeaders();
444
484
  res.setHeader("Content-Type", "application/json");
445
485
  res.writeHead(500);
446
486
  res.end(JSON.stringify({ error: "RSC navigation error" }));
@@ -476,6 +516,7 @@ ${options.headerPreset === "baseline"
476
516
  var epHeaders = Object.fromEntries(result.response.headers);
477
517
  res.writeHead(result.response.status, epHeaders);
478
518
  applyMiddlewareHeaders();
519
+ __flushPendingHeaders();
479
520
  if (result.response.body) {
480
521
  await pipeWebStreamToResponse(result.response.body, res);
481
522
  } else {
@@ -485,12 +526,14 @@ ${options.headerPreset === "baseline"
485
526
  }
486
527
 
487
528
  // Endpoint exists but method not handled
529
+ __flushPendingHeaders();
488
530
  res.writeHead(405, { "Content-Type": "text/plain" });
489
531
  res.end(\`Method \${method} not allowed\`);
490
532
  return;
491
533
  } catch (err) {
492
534
  console.error("Endpoint error:", err);
493
535
  if (!res.headersSent) {
536
+ __flushPendingHeaders();
494
537
  sendText(res, 500, "Internal Server Error");
495
538
  }
496
539
  return;
@@ -513,6 +556,7 @@ ${options.headerPreset === "baseline"
513
556
  if (prStats.isFile()) {
514
557
  const content = await readFile(prFilePath);
515
558
  applyMiddlewareHeaders();
559
+ __flushPendingHeaders();
516
560
  res.setHeader("Content-Type", "text/html; charset=utf-8");
517
561
  res.setHeader("Content-Length", content.byteLength);
518
562
  res.setHeader("Cache-Control", "public, max-age=0, must-revalidate");
@@ -532,6 +576,7 @@ ${options.headerPreset === "baseline"
532
576
  if (rscResult) {
533
577
  const htmlStream = await ssrEntry.renderToHtml(rscResult.stream, rscResult.headConfig);
534
578
  applyMiddlewareHeaders();
579
+ __flushPendingHeaders();
535
580
  res.writeHead(200, { "Content-Type": "text/html; charset=utf-8" });
536
581
  await pipeWebStreamToResponse(htmlStream, res);
537
582
  return;
@@ -539,6 +584,7 @@ ${options.headerPreset === "baseline"
539
584
  } catch (err) {
540
585
  console.error("RSC render error:", err);
541
586
  if (!res.headersSent) {
587
+ __flushPendingHeaders();
542
588
  await sendStatusPage(res, 500, pathname);
543
589
  }
544
590
  return;
@@ -553,11 +599,15 @@ ${options.headerPreset === "baseline"
553
599
  }
554
600
 
555
601
  // 10. 404
602
+ __flushPendingHeaders();
556
603
  await sendStatusPage(res, 404, pathname);
557
604
  } catch (err) {
558
605
  console.error("Request error:", err);
606
+ __flushPendingHeaders();
559
607
  await sendStatusPage(res, 500, pathname);
560
608
  }
609
+
610
+ }); // end runWithRequestContext
561
611
  });
562
612
 
563
613
  server.listen(PORT, HOST, () => {
@@ -1 +1 @@
1
- {"version":3,"file":"server-template.js","sourceRoot":"","sources":["../src/server-template.ts"],"names":[],"mappings":"AAAA,gEAAgE;AAWhE;;;;;;;;;GASG;AACH,MAAM,UAAU,mBAAmB,CACjC,SAAsB,EACtB,OAAsB;IAEtB,OAAO;;;;;;;;;;;;mEAY0D,OAAO,CAAC,IAAI;mCAC5C,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,IAAI,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;0BA4IrC,OAAO,CAAC,WAAW;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAgG3C,OAAO,CAAC,YAAY,KAAK,UAAU;QACjC,CAAC,CAAC;;;;CAIL;QACG,CAAC,CAAC,EACN;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA4TC,CAAC;AACF,CAAC"}
1
+ {"version":3,"file":"server-template.js","sourceRoot":"","sources":["../src/server-template.ts"],"names":[],"mappings":"AAAA,gEAAgE;AAWhE;;;;;;;;;GASG;AACH,MAAM,UAAU,mBAAmB,CACjC,SAAsB,EACtB,OAAsB;IAEtB,OAAO;;;;;;;;;;;;mEAY0D,OAAO,CAAC,IAAI;mCAC5C,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,IAAI,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;0BA4IrC,OAAO,CAAC,WAAW;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAgG3C,OAAO,CAAC,YAAY,KAAK,UAAU;QACjC,CAAC,CAAC;;;;CAIL;QACG,CAAC,CAAC,EACN;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA8WC,CAAC;AACF,CAAC"}
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@catmint/adapter-node",
3
- "version": "0.0.0-prealpha.12",
3
+ "version": "0.0.0-prealpha.15",
4
4
  "type": "module",
5
5
  "private": false,
6
- "license": "MIT",
6
+ "license": "GPL-2.0",
7
7
  "files": [
8
8
  "dist"
9
9
  ],
@@ -14,7 +14,7 @@
14
14
  }
15
15
  },
16
16
  "dependencies": {
17
- "catmint": "0.0.0-prealpha.12"
17
+ "catmint": "0.0.0-prealpha.15"
18
18
  },
19
19
  "devDependencies": {
20
20
  "typescript": "^5.7.0"