@haifai/bwhale 0.1.2 → 0.1.4

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.
Files changed (2) hide show
  1. package/bin/bwhale.js +33 -16
  2. package/package.json +1 -1
package/bin/bwhale.js CHANGED
@@ -16,9 +16,10 @@ import { createWriteStream, existsSync, mkdirSync, readFileSync, rmSync, statSyn
16
16
  import { homedir, platform, arch } from 'node:os'
17
17
  import path from 'node:path'
18
18
  import { Readable } from 'node:stream'
19
- import { finished } from 'node:stream/promises'
19
+ import { pipeline } from 'node:stream/promises'
20
+ import { Transform } from 'node:stream'
20
21
 
21
- const REPO = 'Haifai-AI/baby-whale'
22
+ const REPO = 'Haifai-AI/bwhale-dist'
22
23
  const HOME = homedir()
23
24
  const ROOT = process.env.BWHALE_HOME ?? path.join(HOME, '.bwhale')
24
25
  const LATEST_API = `https://api.github.com/repos/${REPO}/releases/latest`
@@ -71,7 +72,7 @@ function die(message) { log(`bwhale: ${message}`); process.exit(1) }
71
72
  function bundleAssetName(version) {
72
73
  const p = SUPPORT?.platformKey() ?? platform()
73
74
  const a = arch() === 'arm64' ? 'arm64' : 'x86_64'
74
- return `baby-whale-${p}-${a}-${version}.zip`
75
+ return `baby-whale-${p}-${a}-${version.replace(/^v/, '')}.zip`
75
76
  }
76
77
 
77
78
  async function latestRelease() {
@@ -89,23 +90,34 @@ async function latestRelease() {
89
90
  async function download(url, dest, size) {
90
91
  mkdirSync(path.dirname(dest), { recursive: true })
91
92
  const partial = `${dest}.part`
93
+ // A finished-but-unrenamed .part from a previous crashed run is reused as-is.
94
+ if (size > 0 && existsSync(partial) && statSync(partial).size === size) {
95
+ log('bwhale: reusing the completed download from the previous run')
96
+ return partial
97
+ }
92
98
  const response = await fetch(url, { redirect: 'follow' })
93
99
  if (!response.ok || response.body === null) die(`download failed with HTTP ${response.status}`)
94
100
  const total = Number(response.headers.get('content-length') ?? size ?? 0)
95
101
  let received = 0
96
- const out = createWriteStream(partial)
97
- process.stderr.write('bwhale: downloading runtime ')
98
- for await (const chunk of Readable.fromWeb(response.body)) {
99
- received += chunk.length
100
- out.write(chunk)
101
- if (total > 0) process.stderr.write(`\rbwhale: downloading runtime ${Math.round((received / total) * 100)}%`)
102
- }
102
+ let lastPercent = -1
103
+ const counter = new Transform({
104
+ transform(chunk, _enc, callback) {
105
+ received += chunk.length
106
+ if (total > 0) {
107
+ const percent = Math.round((received / total) * 100)
108
+ if (percent !== lastPercent) {
109
+ lastPercent = percent
110
+ process.stderr.write(`\rbwhale: downloading runtime ${percent}%`)
111
+ }
112
+ }
113
+ callback(null, chunk)
114
+ },
115
+ })
116
+ // pipeline applies backpressure — naive per-chunk writes buffer the whole
117
+ // file in memory and get the process OOM-killed at these sizes.
118
+ await pipeline(Readable.fromWeb(response.body), counter, createWriteStream(partial))
103
119
  process.stderr.write('\n')
104
- await finished(out)
105
- // integrity: the release asset is the contract; a truncated file must never install
106
120
  if (total > 0 && received !== total) die(`download truncated (${received}/${total} bytes) — retry`)
107
- rmSync(dest, { force: true })
108
- statSync(partial)
109
121
  return partial
110
122
  }
111
123
 
@@ -121,7 +133,7 @@ function unzip(archive, into) {
121
133
  function runtimeDir(version) {
122
134
  const p = SUPPORT?.platformKey() ?? platform()
123
135
  const a = arch() === 'arm64' ? 'arm64' : 'x86_64'
124
- return path.join(ROOT, 'runtime', `baby-whale-${p}-${a}-${version}`)
136
+ return path.join(ROOT, 'runtime', `baby-whale-${p}-${a}-${version.replace(/^v/, '')}`)
125
137
  }
126
138
 
127
139
  function installedMarker() {
@@ -175,7 +187,12 @@ async function cmdStart(argv) {
175
187
  for (const gap of gaps) log(` - ${gap.name}: ${gap.hint}`)
176
188
  if (gaps.some(g => g.name.startsWith('git'))) die('git is required (session history)')
177
189
  }
178
- const entry = await ensureRuntime(process.env.BWHALE_VERSION ?? 'latest')
190
+ let entry
191
+ try {
192
+ entry = await ensureRuntime(process.env.BWHALE_VERSION ?? 'latest')
193
+ } catch (error) {
194
+ die(error instanceof Error ? error.message : String(error))
195
+ }
179
196
  const nodeBin = platform() === 'win32'
180
197
  ? path.join(entry.dir, 'node', 'node.exe')
181
198
  : path.join(entry.dir, 'node', 'bin', 'node')
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@haifai/bwhale",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "description": "Baby Whale — local-first knowledge-work coworker. One command: bwhale. Fetches its runtime once, starts the app, opens your browser. Nothing leaves your machine.",
5
5
  "license": "MIT",
6
6
  "publishConfig": { "access": "public" },