@nano-step/nano-brain 2026.7.1102 → 2026.7.1103

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/README.md CHANGED
@@ -8,6 +8,7 @@ Agent-oriented memory and code intelligence over MCP. AI agents don't read docs
8
8
  [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE)
9
9
  [![GitHub](https://img.shields.io/badge/GitHub-nano--step%2Fnano--brain-181717?logo=github)](https://github.com/nano-step/nano-brain)
10
10
  [![npm](https://img.shields.io/badge/npm-@nano--step%2Fnano--brain-CC3533?logo=npm)](https://www.npmjs.com/package/@nano-step/nano-brain)
11
+ [![npm downloads](https://img.shields.io/npm/dw/@nano-step/nano-brain?logo=npm&label=downloads)](https://www.npmjs.com/package/@nano-step/nano-brain)
11
12
  [![Docker](https://img.shields.io/badge/Docker-2496ED?logo=docker&logoColor=white)](https://hub.docker.com/r/nano-step/nano-brain)
12
13
  [![Discord](https://img.shields.io/badge/Discord-5865F2?logo=discord&logoColor=white)](https://discord.gg/nano-brain)
13
14
 
@@ -31,27 +31,61 @@ function getPlatformKey() {
31
31
  return `${platform}-${arch}`;
32
32
  }
33
33
 
34
+ // Remove a partial download without throwing. A socket error can fire before
35
+ // createWriteStream has opened the file, or after a redirect branch already
36
+ // removed it — an unguarded fs.unlinkSync then throws ENOENT *inside* the
37
+ // event callback, which is uncaught and crashes the whole postinstall (#592).
38
+ function safeUnlink(p) {
39
+ // No existsSync pre-check: the try/catch already swallows ENOENT, and a
40
+ // check-then-unlink is a TOCTOU race. Best-effort cleanup.
41
+ try {
42
+ fs.unlinkSync(p);
43
+ } catch (_) {
44
+ /* best-effort cleanup */
45
+ }
46
+ }
47
+
48
+ // file.close() is asynchronous; the fd is only released when its callback
49
+ // fires. Every unlink / recursive-redirect / reject is therefore deferred into
50
+ // file.close(() => …) so the partial file is gone (and not re-opened by a
51
+ // racing redirect) before we act — otherwise Windows unlink fails with EPERM
52
+ // while the fd is open, and a raw request error would leak the write stream.
34
53
  function download(url, dest) {
35
54
  return new Promise((resolve, reject) => {
36
55
  const file = fs.createWriteStream(dest);
37
56
  https.get(url, (res) => {
38
57
  if (res.statusCode === 301 || res.statusCode === 302) {
39
- file.close();
40
- fs.unlinkSync(dest);
41
- return download(res.headers.location, dest).then(resolve).catch(reject);
58
+ file.close(() => {
59
+ safeUnlink(dest);
60
+ download(res.headers.location, dest).then(resolve).catch(reject);
61
+ });
62
+ return;
42
63
  }
43
64
  if (res.statusCode !== 200) {
44
- file.close();
45
- fs.unlinkSync(dest);
46
- return reject(new Error(`Download failed: HTTP ${res.statusCode}`));
65
+ file.close(() => {
66
+ safeUnlink(dest);
67
+ reject(new Error(`Download failed: HTTP ${res.statusCode}`));
68
+ });
69
+ return;
47
70
  }
48
71
  res.pipe(file);
72
+ // pipe() does NOT forward source 'error' events; without this a socket
73
+ // reset mid-transfer emits an unhandled 'error' on res and crashes the
74
+ // postinstall (#592) — mirror downloadWithHash's handler.
75
+ res.on("error", (err) => {
76
+ file.close(() => {
77
+ safeUnlink(dest);
78
+ reject(err);
79
+ });
80
+ });
49
81
  file.on("finish", () => {
50
82
  file.close(resolve);
51
83
  });
52
84
  }).on("error", (err) => {
53
- fs.unlinkSync(dest);
54
- reject(err);
85
+ file.close(() => {
86
+ safeUnlink(dest);
87
+ reject(err);
88
+ });
55
89
  });
56
90
  });
57
91
  }
@@ -62,14 +96,18 @@ function downloadWithHash(url, dest) {
62
96
  const hash = crypto.createHash("sha256");
63
97
  https.get(url, (res) => {
64
98
  if (res.statusCode === 301 || res.statusCode === 302) {
65
- file.close();
66
- fs.unlinkSync(dest);
67
- return downloadWithHash(res.headers.location, dest).then(resolve).catch(reject);
99
+ file.close(() => {
100
+ safeUnlink(dest);
101
+ downloadWithHash(res.headers.location, dest).then(resolve).catch(reject);
102
+ });
103
+ return;
68
104
  }
69
105
  if (res.statusCode !== 200) {
70
- file.close();
71
- fs.unlinkSync(dest);
72
- return reject(new Error(`Download failed: HTTP ${res.statusCode}`));
106
+ file.close(() => {
107
+ safeUnlink(dest);
108
+ reject(new Error(`Download failed: HTTP ${res.statusCode}`));
109
+ });
110
+ return;
73
111
  }
74
112
  res.on("data", (chunk) => hash.update(chunk));
75
113
  res.pipe(file);
@@ -77,13 +115,16 @@ function downloadWithHash(url, dest) {
77
115
  file.close(() => resolve(hash.digest("hex")));
78
116
  });
79
117
  res.on("error", (err) => {
80
- file.close();
81
- if (fs.existsSync(dest)) fs.unlinkSync(dest);
82
- reject(err);
118
+ file.close(() => {
119
+ safeUnlink(dest);
120
+ reject(err);
121
+ });
83
122
  });
84
123
  }).on("error", (err) => {
85
- if (fs.existsSync(dest)) fs.unlinkSync(dest);
86
- reject(err);
124
+ file.close(() => {
125
+ safeUnlink(dest);
126
+ reject(err);
127
+ });
87
128
  });
88
129
  });
89
130
  }
@@ -110,9 +151,7 @@ async function verifySHA256(tag, assetName, binPath, computedHex) {
110
151
  console.warn(`⚠ SHA256SUMS not available for ${tag} (${err.message}); skipping integrity verification.`);
111
152
  return;
112
153
  } finally {
113
- if (fs.existsSync(sumsPath)) {
114
- try { fs.unlinkSync(sumsPath); } catch (_) {}
115
- }
154
+ safeUnlink(sumsPath);
116
155
  }
117
156
 
118
157
  const expectedHex = parseSHA256Line(sumsContent, assetName);
@@ -122,7 +161,10 @@ async function verifySHA256(tag, assetName, binPath, computedHex) {
122
161
  }
123
162
 
124
163
  if (expectedHex.toLowerCase() !== computedHex.toLowerCase()) {
125
- if (fs.existsSync(binPath)) fs.unlinkSync(binPath);
164
+ // safeUnlink so an unrelated fs error (e.g. EACCES) can't turn a SHA
165
+ // mismatch into a non-"SECURITY:" rejection that main() would treat as a
166
+ // retryable download failure, bypassing the hard-fail fail-safe.
167
+ safeUnlink(binPath);
126
168
  throw new Error(
127
169
  `SECURITY: SHA-256 mismatch for ${assetName}\n` +
128
170
  ` expected: ${expectedHex}\n` +
@@ -305,4 +347,4 @@ if (require.main === module) {
305
347
  main();
306
348
  }
307
349
 
308
- module.exports = { parseSHA256Line, downloadWithHash, verifySHA256, tryAutoLink };
350
+ module.exports = { parseSHA256Line, download, downloadWithHash, verifySHA256, tryAutoLink, safeUnlink };
@@ -6,7 +6,34 @@ const fs = require("node:fs");
6
6
  const path = require("node:path");
7
7
  const os = require("node:os");
8
8
  const crypto = require("node:crypto");
9
- const { parseSHA256Line, tryAutoLink } = require("./postinstall");
9
+ const { parseSHA256Line, tryAutoLink, safeUnlink, download, downloadWithHash } = require("./postinstall");
10
+
11
+ // #592 regression: a request/socket error must REJECT the promise (so main()'s
12
+ // per-tag retry loop continues), never throw synchronously or crash the
13
+ // process via an unguarded unlink. Point at a closed port → ECONNREFUSED.
14
+ test("download: rejects (not throws) on connection error, no lingering file", async () => {
15
+ const dest = path.join(os.tmpdir(), `nb-dl-refused-${process.pid}-${Date.now()}`);
16
+ await assert.rejects(() => download("https://127.0.0.1:1/x", dest));
17
+ assert.strictEqual(fs.existsSync(dest), false);
18
+ });
19
+
20
+ test("downloadWithHash: rejects (not throws) on connection error, no lingering file", async () => {
21
+ const dest = path.join(os.tmpdir(), `nb-dlh-refused-${process.pid}-${Date.now()}`);
22
+ await assert.rejects(() => downloadWithHash("https://127.0.0.1:1/x", dest));
23
+ assert.strictEqual(fs.existsSync(dest), false);
24
+ });
25
+
26
+ test("safeUnlink: does not throw on a nonexistent path (#592 regression)", () => {
27
+ const missing = path.join(os.tmpdir(), `nb-safeunlink-missing-${process.pid}-${Date.now()}`);
28
+ assert.doesNotThrow(() => safeUnlink(missing));
29
+ });
30
+
31
+ test("safeUnlink: removes an existing file", () => {
32
+ const p = path.join(os.tmpdir(), `nb-safeunlink-${process.pid}-${Date.now()}`);
33
+ fs.writeFileSync(p, "x");
34
+ safeUnlink(p);
35
+ assert.strictEqual(fs.existsSync(p), false);
36
+ });
10
37
 
11
38
  test("parseSHA256Line: returns hash for matching filename in single-line content", () => {
12
39
  const content = "abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789 nano-brain-linux-amd64\n";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nano-step/nano-brain",
3
- "version": "2026.7.1102",
3
+ "version": "2026.7.1103",
4
4
  "description": "Persistent memory and code intelligence for AI coding agents",
5
5
  "bin": {
6
6
  "nano-brain": "npm/run.js"