@nanobpm/urban 0.90.1 → 0.90.2

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/cli.js CHANGED
@@ -433,6 +433,30 @@ export async function main(argv) {
433
433
  return 1;
434
434
  }
435
435
  }
436
+ /**
437
+ * Flush a Node writable stdio stream, resolving once its buffered writes have drained to the OS.
438
+ * `process.stdout`/`stderr` connected to a *pipe* is asynchronous in Node: `console.log` buffers
439
+ * the write and an immediate `process.exit()` abandons anything not yet handed to the OS, so a
440
+ * large reply (> the 64 KB pipe buffer on Linux) is silently truncated. Writing an empty chunk and
441
+ * waiting for its completion callback drains the prior writes first. Deno's stdio has no such
442
+ * `write(chunk, cb)` shape (its `console.log` is synchronous), so its absence is a no-op flush.
443
+ */
444
+ function flushStream(stream) {
445
+ return new Promise((resolve) => {
446
+ if (!stream || typeof stream.write !== "function") {
447
+ resolve();
448
+ return;
449
+ }
450
+ stream.write("", () => resolve());
451
+ });
452
+ }
453
+ /** Drain both stdout and stderr before an explicit `process.exit` so no buffered output is lost. */
454
+ async function flushStdio() {
455
+ const p = processGlobal();
456
+ if (!p)
457
+ return;
458
+ await Promise.all([flushStream(p.stdout), flushStream(p.stderr)]);
459
+ }
436
460
  const proc = processGlobal();
437
461
  const deno = denoGlobal();
438
462
  const importMetaMain = Reflect.get(import.meta, "main");
@@ -450,11 +474,14 @@ if (importMetaMain === true || nodeMain) {
450
474
  else if (deno?.exit)
451
475
  deno.exit(code);
452
476
  };
453
- main(argv).then((code) => {
454
- if (code >= 0)
477
+ main(argv).then(async (code) => {
478
+ if (code >= 0) {
479
+ await flushStdio();
455
480
  exit(code);
456
- }, (err) => {
481
+ }
482
+ }, async (err) => {
457
483
  console.error(errorMessage(err));
484
+ await flushStdio();
458
485
  exit(1);
459
486
  });
460
487
  }
@@ -7,12 +7,23 @@ export interface DenoRuntimeGlobal {
7
7
  exit?: (code: number) => void;
8
8
  addSignalListener?: (sig: string, cb: () => void) => void;
9
9
  }
10
+ /**
11
+ * The subset of a Node writable stdio stream (`process.stdout` / `process.stderr`) we depend on:
12
+ * a `write` that accepts a completion callback fired once the chunk has flushed to the OS. Deno's
13
+ * stdio streams do not expose this shape; callers treat its absence as "already synchronous, no
14
+ * flush needed" (see `flushStdio` in cli.ts).
15
+ */
16
+ export interface WritableStdio {
17
+ write?: (chunk: string, cb: () => void) => void;
18
+ }
10
19
  export interface ProcessRuntimeGlobal {
11
20
  argv?: string[];
12
21
  exit?: (code: number) => void;
13
22
  stdin?: AsyncIterable<Uint8Array | string> & {
14
23
  setEncoding?(encoding: string): void;
15
24
  };
25
+ stdout?: WritableStdio;
26
+ stderr?: WritableStdio;
16
27
  on?: (sig: string, cb: () => void) => void;
17
28
  }
18
29
  export declare function denoGlobal(): DenoRuntimeGlobal | undefined;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nanobpm/urban",
3
- "version": "0.90.1",
3
+ "version": "0.90.2",
4
4
  "description": "Urban: build and run code-first apps on Nano — runtime, derivation toolkit, and CLI in one. Author durable processes with @nanobpm/workflow (defineFlow), a typed datasource, forms, triggers and surfaces from a nano.app.json manifest, on Node or Deno.",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",
package/src/cli.test.ts CHANGED
@@ -1,7 +1,14 @@
1
1
  import { test } from "node:test";
2
2
  import assert from "node:assert/strict";
3
+ import { spawn } from "node:child_process";
4
+ import { mkdtemp, mkdir, writeFile, rm } from "node:fs/promises";
5
+ import { tmpdir } from "node:os";
6
+ import { join } from "node:path";
7
+ import { fileURLToPath } from "node:url";
3
8
  import { cmdData } from "./cli.ts";
4
9
  import { isRecord } from "./runtime/core/guards.ts";
10
+ import { createNodeHost } from "./runtime/adapters/node.ts";
11
+ import { runDataOp } from "./runtime/core/modules/dataops.ts";
5
12
 
6
13
  // The `urban data` gateway's contract (see cmdData's docstring): *every* reply — including an
7
14
  // unreadable request — is a parseable `{ ok: false, error }` envelope on stdout AND the process
@@ -41,3 +48,85 @@ test("urban data exits 0 with a parseable envelope on an unreadable request", as
41
48
  if (typeof reply.error !== "string") throw new TypeError("expected string error");
42
49
  assert.match(reply.error, /^bad request: /);
43
50
  });
51
+
52
+ // Regression guard for nanobpm/nano-ide#549: `urban data`'s success path prints the full reply
53
+ // with `console.log`, then the top-level runner calls `process.exit(code)`. In Node, stdout
54
+ // connected to a *pipe* is asynchronous — an immediate `process.exit` abandons anything still
55
+ // buffered, truncating the reply at the OS pipe-buffer boundary (65536 bytes on Linux) so the
56
+ // console host fails to `JSON.parse` it ("EOF … column 65536"). The fix drains stdout before the
57
+ // explicit exit. This test spawns the real CLI on a datasource whose query result serialises to
58
+ // well over 64 KB, reads the piped stdout, and asserts the parent parses a complete envelope whose
59
+ // byte length matches the untruncated JSON. It FAILS before the fix (truncated at 65536) and
60
+ // passes after.
61
+ test("urban data streams a > 64 KB reply without truncation through an explicit process.exit", async () => {
62
+ const dir = await mkdtemp(join(tmpdir(), "urban-data-large-"));
63
+ try {
64
+ await mkdir(join(dir, "db", "migrations"), { recursive: true });
65
+ await writeFile(
66
+ join(dir, "nano.app.json"),
67
+ JSON.stringify({
68
+ schemaVersion: 1,
69
+ id: "big",
70
+ name: "Big",
71
+ data: {
72
+ default: "app",
73
+ sources: { app: { driver: "sqlite", url: "file:./db/app.db", migrations: "db/migrations" } },
74
+ },
75
+ }),
76
+ );
77
+ await writeFile(
78
+ join(dir, "db", "migrations", "001_big.sql"),
79
+ "CREATE TABLE big (id INTEGER PRIMARY KEY, payload TEXT NOT NULL);",
80
+ );
81
+ // 2000 rows × ~128-char payload → a query reply of roughly 300 KB, comfortably past the 64 KB
82
+ // pipe boundary. A single recursive-CTE INSERT (no inner `;`) so the migration splitter keeps
83
+ // it as one statement.
84
+ await writeFile(
85
+ join(dir, "db", "migrations", "002_seed.sql"),
86
+ "INSERT INTO big (id, payload) WITH RECURSIVE seq(n) AS (SELECT 1 UNION ALL SELECT n + 1 FROM seq WHERE n < 2000) SELECT n, printf('%0128d', n) FROM seq;",
87
+ );
88
+
89
+ const host = createNodeHost({ cwd: dir });
90
+ await runDataOp(host, ".", "nano.app.json", { op: "migrate" });
91
+ const query = { op: "query", sql: "SELECT * FROM big ORDER BY id" } as const;
92
+ const result = await runDataOp(host, ".", "nano.app.json", query);
93
+ const expected = `${JSON.stringify({ ok: true, ...result })}\n`;
94
+ const expectedBytes = Buffer.byteLength(expected);
95
+ assert.ok(
96
+ expectedBytes > 65536,
97
+ `fixture too small (${expectedBytes} bytes) to exercise the pipe-buffer boundary`,
98
+ );
99
+
100
+ const cli = fileURLToPath(new URL("./cli.ts", import.meta.url));
101
+ const { stdout, code, signal } = await new Promise<{
102
+ stdout: Buffer;
103
+ code: number | null;
104
+ signal: NodeJS.Signals | null;
105
+ }>((resolve, reject) => {
106
+ const child = spawn(
107
+ process.execPath,
108
+ ["--experimental-strip-types", cli, "data", "--root", dir],
109
+ { stdio: ["pipe", "pipe", "inherit"] },
110
+ );
111
+ const chunks: Buffer[] = [];
112
+ child.stdout.on("data", (c: Buffer) => chunks.push(c));
113
+ child.on("error", reject);
114
+ child.on("close", (code, signal) => resolve({ stdout: Buffer.concat(chunks), code, signal }));
115
+ child.stdin.end(JSON.stringify(query));
116
+ });
117
+
118
+ assert.equal(signal, null, `CLI killed by signal ${signal}`);
119
+ assert.equal(code, 0, `CLI exited ${code}, expected 0`);
120
+ assert.equal(
121
+ stdout.byteLength,
122
+ expectedBytes,
123
+ `stdout truncated: got ${stdout.byteLength} bytes, expected ${expectedBytes}`,
124
+ );
125
+ const reply: unknown = JSON.parse(stdout.toString("utf8"));
126
+ assert.ok(isRecord(reply));
127
+ assert.equal(reply.ok, true);
128
+ assert.deepEqual(reply, { ok: true, ...result });
129
+ } finally {
130
+ await rm(dir, { recursive: true, force: true });
131
+ }
132
+ });
package/src/cli.ts CHANGED
@@ -22,6 +22,7 @@ import {
22
22
  selectHost,
23
23
  } from "./runtime/index.ts";
24
24
  import { denoGlobal, processGlobal } from "./runtime/adapters/globals.ts";
25
+ import type { WritableStdio } from "./runtime/adapters/globals.ts";
25
26
  import { errorMessage, isRecord } from "./runtime/core/guards.ts";
26
27
  import { scaffold, slugify } from "create-urban-app";
27
28
  import type { DataRequest } from "./runtime/index.ts";
@@ -446,6 +447,31 @@ export async function main(argv: string[]): Promise<number> {
446
447
  }
447
448
  }
448
449
 
450
+ /**
451
+ * Flush a Node writable stdio stream, resolving once its buffered writes have drained to the OS.
452
+ * `process.stdout`/`stderr` connected to a *pipe* is asynchronous in Node: `console.log` buffers
453
+ * the write and an immediate `process.exit()` abandons anything not yet handed to the OS, so a
454
+ * large reply (> the 64 KB pipe buffer on Linux) is silently truncated. Writing an empty chunk and
455
+ * waiting for its completion callback drains the prior writes first. Deno's stdio has no such
456
+ * `write(chunk, cb)` shape (its `console.log` is synchronous), so its absence is a no-op flush.
457
+ */
458
+ function flushStream(stream: WritableStdio | undefined): Promise<void> {
459
+ return new Promise((resolve) => {
460
+ if (!stream || typeof stream.write !== "function") {
461
+ resolve();
462
+ return;
463
+ }
464
+ stream.write("", () => resolve());
465
+ });
466
+ }
467
+
468
+ /** Drain both stdout and stderr before an explicit `process.exit` so no buffered output is lost. */
469
+ async function flushStdio(): Promise<void> {
470
+ const p = processGlobal();
471
+ if (!p) return;
472
+ await Promise.all([flushStream(p.stdout), flushStream(p.stderr)]);
473
+ }
474
+
449
475
  const proc = processGlobal();
450
476
  const deno = denoGlobal();
451
477
  const importMetaMain: unknown = Reflect.get(import.meta, "main");
@@ -462,11 +488,15 @@ if (importMetaMain === true || nodeMain) {
462
488
  else if (deno?.exit) deno.exit(code);
463
489
  };
464
490
  main(argv).then(
465
- (code) => {
466
- if (code >= 0) exit(code);
491
+ async (code) => {
492
+ if (code >= 0) {
493
+ await flushStdio();
494
+ exit(code);
495
+ }
467
496
  },
468
- (err) => {
497
+ async (err) => {
469
498
  console.error(errorMessage(err));
499
+ await flushStdio();
470
500
  exit(1);
471
501
  },
472
502
  );
@@ -8,10 +8,22 @@ export interface DenoRuntimeGlobal {
8
8
  addSignalListener?: (sig: string, cb: () => void) => void;
9
9
  }
10
10
 
11
+ /**
12
+ * The subset of a Node writable stdio stream (`process.stdout` / `process.stderr`) we depend on:
13
+ * a `write` that accepts a completion callback fired once the chunk has flushed to the OS. Deno's
14
+ * stdio streams do not expose this shape; callers treat its absence as "already synchronous, no
15
+ * flush needed" (see `flushStdio` in cli.ts).
16
+ */
17
+ export interface WritableStdio {
18
+ write?: (chunk: string, cb: () => void) => void;
19
+ }
20
+
11
21
  export interface ProcessRuntimeGlobal {
12
22
  argv?: string[];
13
23
  exit?: (code: number) => void;
14
24
  stdin?: AsyncIterable<Uint8Array | string> & { setEncoding?(encoding: string): void };
25
+ stdout?: WritableStdio;
26
+ stderr?: WritableStdio;
15
27
  on?: (sig: string, cb: () => void) => void;
16
28
  }
17
29