@centralu/darwin-arm64 0.1.0-beta.12 → 0.1.0-beta.14

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.
@@ -0,0 +1,290 @@
1
+ // Centralu's remote installer, step 2 (docs/plans/remote-hub.md §10.2). The hub sends this file over
2
+ // ssh and runs it on the pinned Node that step 1 placed; it never runs on the hub. It imports Node's
3
+ // own modules only, and reads one argument: the base64 of a JSON object the hub wrote
4
+ // ({ version, node, platform, hub, packages: [{ name, tarball, integrity }], activate? }), or for the
5
+ // update's later steps ({ action: 'pointers', current, previous } and { action: 'prune' }; plan §10.5).
6
+ //
7
+ // It prints one line for the hub to read, `CENTRALU-INSTALL done <json>` or
8
+ // `CENTRALU-INSTALL fail <code> <detail>`, and nothing that matters on its exit code (exit codes do
9
+ // not survive the way back from Windows; tunnel.ts). The sentences for the person are the hub's.
10
+ import { spawnSync } from 'node:child_process'
11
+ import { createHash, randomBytes } from 'node:crypto'
12
+ import { chmodSync, existsSync, mkdirSync, readdirSync, readFileSync, realpathSync, renameSync, rmSync, writeFileSync } from 'node:fs'
13
+ import { homedir } from 'node:os'
14
+ import { join } from 'node:path'
15
+ import { pathToFileURL } from 'node:url'
16
+
17
+ const WORD = /^[0-9A-Za-z][0-9A-Za-z.+-]{0,63}$/
18
+
19
+ class Refusal extends Error {
20
+ constructor(code, detail = '') {
21
+ super(`${code} ${detail}`.trim())
22
+ this.code = code
23
+ this.detail = detail
24
+ }
25
+ }
26
+
27
+ /** The data folder: `CC_DATA_DIR`, else `~/.centralu`, the rule `serve` and the host use */
28
+ export function remoteLayout(dataDir) {
29
+ const root = join(dataDir, 'remote')
30
+ return { root, current: join(root, 'current'), previous: join(root, 'previous'), versions: join(root, 'versions'), node: join(root, 'node'), bin: join(root, 'bin'), lock: join(root, 'install.lock') }
31
+ }
32
+
33
+ /** `current` / `previous`: "<centralu version> <node version>", two plain words, or null */
34
+ export function readPointer(file) {
35
+ try {
36
+ const m = /^\s*(\S+)[ \t]+(\S+)\s*$/.exec(readFileSync(file, 'utf8'))
37
+ return m && WORD.test(m[1]) && WORD.test(m[2]) ? { version: m[1], node: m[2] } : null
38
+ } catch {
39
+ return null
40
+ }
41
+ }
42
+
43
+ /**
44
+ * Replaces `file` by a rename over it, so a reader (the launcher, `serve --connection`) sees the old
45
+ * line or the new one and never half of one. Atomic on posix, and measured atomic on Windows
46
+ * (`renameSync` over an existing file replaces it; plan §10.1)
47
+ */
48
+ export function writeAtomic(file, text) {
49
+ const tmp = `${file}.${process.pid}-${randomBytes(4).toString('hex')}.tmp`
50
+ writeFileSync(tmp, text)
51
+ renameSync(tmp, file)
52
+ }
53
+
54
+ /** Points `current` at `next`, the old `current` becoming `previous` (plan §10.5 step 3) */
55
+ export function switchTo(l, next) {
56
+ const cur = readPointer(l.current)
57
+ if (cur && cur.version === next.version && cur.node === next.node) return { current: next, previous: readPointer(l.previous) }
58
+ if (cur) writeAtomic(l.previous, `${cur.version} ${cur.node}\n`)
59
+ writeAtomic(l.current, `${next.version} ${next.node}\n`)
60
+ return { current: next, previous: cur ?? readPointer(l.previous) }
61
+ }
62
+
63
+ /**
64
+ * Removes every version that is neither `current` nor `previous`, and every Node neither uses. A
65
+ * folder that cannot go (a Windows program still running from it) stays and is named, never fatal
66
+ */
67
+ export function prune(l) {
68
+ const keep = [readPointer(l.current), readPointer(l.previous)].filter(Boolean)
69
+ const removed = []
70
+ const left = []
71
+ const sweep = (dir, kept) => {
72
+ let names = []
73
+ try {
74
+ names = readdirSync(dir)
75
+ } catch {
76
+ return
77
+ }
78
+ for (const name of names) {
79
+ if (kept.has(name) || name.startsWith('.')) continue
80
+ try {
81
+ rmSync(join(dir, name), { recursive: true, force: true })
82
+ removed.push(name)
83
+ } catch {
84
+ left.push(name)
85
+ }
86
+ }
87
+ }
88
+ sweep(l.versions, new Set(keep.map((p) => p.version)))
89
+ sweep(l.node, new Set(keep.map((p) => `v${p.node}`)))
90
+ return { removed, left }
91
+ }
92
+
93
+ /** One installer at a time per data folder; a lock whose process is gone is taken over */
94
+ function lock(l) {
95
+ for (let i = 0; i < 2; i++) {
96
+ try {
97
+ mkdirSync(l.lock)
98
+ writeFileSync(join(l.lock, 'pid'), String(process.pid))
99
+ return () => rmSync(l.lock, { recursive: true, force: true })
100
+ } catch (err) {
101
+ if (err.code !== 'EEXIST') throw err
102
+ const pid = Number(readFileSafe(join(l.lock, 'pid')))
103
+ if (pid > 0 && alive(pid)) throw new Refusal('busy', String(pid))
104
+ rmSync(l.lock, { recursive: true, force: true })
105
+ }
106
+ }
107
+ throw new Refusal('busy')
108
+ }
109
+
110
+ function readFileSafe(file) {
111
+ try {
112
+ return readFileSync(file, 'utf8')
113
+ } catch {
114
+ return ''
115
+ }
116
+ }
117
+
118
+ function alive(pid) {
119
+ try {
120
+ process.kill(pid, 0)
121
+ return true
122
+ } catch (err) {
123
+ return err.code === 'EPERM'
124
+ }
125
+ }
126
+
127
+ /** The system's tar. On Windows the one in System32 (bsdtar): a GNU tar on PATH reads `C:` as a host */
128
+ function tarProgram() {
129
+ return process.platform === 'win32' ? join(process.env.SystemRoot || 'C:\\Windows', 'System32', 'tar.exe') : 'tar'
130
+ }
131
+
132
+ /** The tarball's bytes, refused unless their sha512 is the integrity the hub read from the signed metadata */
133
+ export async function fetchChecked(pkg) {
134
+ let res
135
+ try {
136
+ res = await fetch(pkg.tarball, { signal: AbortSignal.timeout(300_000) })
137
+ } catch (err) {
138
+ throw new Refusal('download', `${pkg.name}: ${err.cause?.message ?? err.message}`)
139
+ }
140
+ if (!res.ok) throw new Refusal('download', `${pkg.name}: HTTP ${res.status}`)
141
+ const bytes = Buffer.from(await res.arrayBuffer())
142
+ const have = `sha512-${createHash('sha512').update(bytes).digest('base64')}`
143
+ if (have !== pkg.integrity) throw new Refusal('integrity', pkg.name)
144
+ return bytes
145
+ }
146
+
147
+ /** Where the host sits in the platform package, and what of the package a remote never runs (plan S6) */
148
+ function platformParts(platform) {
149
+ if (platform === 'win32-x64') return { host: join('Centralu', 'resources', 'host', 'main.mjs'), drop: [join('Centralu', 'centralu.exe')] }
150
+ return { host: join('host', 'main.mjs'), drop: ['Centralu.AppImage'] }
151
+ }
152
+
153
+ function sameInstall(dir, p) {
154
+ try {
155
+ const was = JSON.parse(readFileSync(join(dir, 'install.json'), 'utf8'))
156
+ return was.version === p.version && p.packages.every((pkg) => was.packages?.some((w) => w.name === pkg.name && w.integrity === pkg.integrity))
157
+ } catch {
158
+ return false
159
+ }
160
+ }
161
+
162
+ /** Installs `p.version` beside what is there, switches `current` to it, and keeps `previous` */
163
+ export async function install(p, { dataDir = process.env.CC_DATA_DIR || join(homedir(), '.centralu'), platform = process.platform } = {}) {
164
+ if (!WORD.test(p.version) || !WORD.test(p.node)) throw new Refusal('params', 'version')
165
+ const l = remoteLayout(dataDir)
166
+ mkdirSync(l.root, { recursive: true })
167
+ const unlock = lock(l)
168
+ try {
169
+ const dir = join(l.versions, p.version)
170
+ const parts = platformParts(p.platform)
171
+ if (!sameInstall(dir, p)) {
172
+ // Everything lands in a folder of its own first; the version's folder appears by one rename
173
+ const partial = join(l.root, `.partial-${p.version}-${process.pid}`)
174
+ rmSync(partial, { recursive: true, force: true })
175
+ try {
176
+ for (const [i, pkg] of p.packages.entries()) {
177
+ const bytes = await fetchChecked(pkg)
178
+ const file = join(partial, `${i}.tgz`)
179
+ const dest = join(partial, 'node_modules', ...pkg.name.split('/'))
180
+ mkdirSync(dest, { recursive: true })
181
+ writeFileSync(file, bytes)
182
+ const r = spawnSync(tarProgram(), ['-xzf', file, '-C', dest, '--strip-components', '1'], { encoding: 'utf8', windowsHide: true })
183
+ if (r.status !== 0) throw new Refusal('unpack', `${pkg.name}: ${(r.stderr || r.error?.message || '').trim().split('\n').at(-1)}`)
184
+ rmSync(file, { force: true })
185
+ }
186
+ const pkgDir = join(partial, 'node_modules', '@centralu', p.platform)
187
+ if (!existsSync(join(pkgDir, parts.host))) throw new Refusal('no_host', p.platform)
188
+ for (const d of parts.drop) rmSync(join(pkgDir, d), { force: true })
189
+ writeFileSync(
190
+ join(partial, 'install.json'),
191
+ JSON.stringify({ version: p.version, node: p.node, platform: p.platform, packages: p.packages.map(({ name, integrity }) => ({ name, integrity })), installedAt: new Date().toISOString(), by: p.hub ?? null }, null, 2) + '\n',
192
+ )
193
+ mkdirSync(l.versions, { recursive: true })
194
+ rmSync(dir, { recursive: true, force: true })
195
+ renameSync(partial, dir)
196
+ } finally {
197
+ rmSync(partial, { recursive: true, force: true })
198
+ }
199
+ }
200
+ // An update installs beside and switches later, once the running host is stopped (plan §10.5):
201
+ // nothing is pointed at the new version and nothing is removed yet. Without a `current`, no
202
+ // launcher either, so the lookup still finds the Centralu that runs now
203
+ if (p.activate === false) {
204
+ return { installed: { version: p.version, node: p.node }, current: readPointer(l.current), previous: readPointer(l.previous), removed: [], left: [] }
205
+ }
206
+ const launcher = await ensureManagedLauncher(l, dir, p.version, platform)
207
+ const pointers = switchTo(l, { version: p.version, node: p.node })
208
+ return { ...pointers, ...prune(l), launcher }
209
+ } finally {
210
+ unlock()
211
+ }
212
+ }
213
+
214
+ function launcherPath(l, platform) {
215
+ return join(l.bin, platform === 'win32' ? 'centralu.cmd' : 'centralu')
216
+ }
217
+
218
+ /** The launcher reads `current` at every start and never changes (S8): written once, never over */
219
+ async function ensureManagedLauncher(l, dir, version, platform) {
220
+ const launcher = launcherPath(l, platform)
221
+ if (!existsSync(launcher)) {
222
+ const serve = await import(pathToFileURL(join(dir, 'node_modules', 'centralu', 'bin', 'serve.mjs')).href)
223
+ if (typeof serve.managedLauncherScript !== 'function') throw new Refusal('no_launcher', version)
224
+ mkdirSync(l.bin, { recursive: true })
225
+ writeAtomic(launcher, serve.managedLauncherScript(platform))
226
+ if (platform !== 'win32') chmodSync(launcher, 0o755)
227
+ }
228
+ return launcher
229
+ }
230
+
231
+ const pointerOf = (v) => (v && WORD.test(v.version) && WORD.test(v.node) ? { version: v.version, node: v.node } : null)
232
+
233
+ /**
234
+ * Writes both pointers as given (plan §10.5 steps 3 and 5, and rollback): `previous` first, then
235
+ * `current`, each by rename; a null `previous` removes that file. `current` must name a version and a
236
+ * Node that are both there. A null `current` puts the machine back to having no managed version: the
237
+ * pointers and the launcher go, so the lookup finds the Centralu it ran before (an npm install).
238
+ * Nothing is removed from `versions/` or `node/`
239
+ */
240
+ export async function setPointers(p, { dataDir = process.env.CC_DATA_DIR || join(homedir(), '.centralu'), platform = process.platform } = {}) {
241
+ const l = remoteLayout(dataDir)
242
+ mkdirSync(l.root, { recursive: true })
243
+ const unlock = lock(l)
244
+ try {
245
+ const current = pointerOf(p.current)
246
+ const previous = pointerOf(p.previous)
247
+ if (!current) {
248
+ for (const f of [l.previous, l.current, launcherPath(l, platform)]) rmSync(f, { force: true })
249
+ return { current: null, previous: null }
250
+ }
251
+ const dir = join(l.versions, current.version)
252
+ const node = join(l.node, `v${current.node}`)
253
+ if (!existsSync(join(dir, 'install.json')) || !existsSync(node)) throw new Refusal('missing', `${current.version} ${current.node}`)
254
+ await ensureManagedLauncher(l, dir, current.version, platform)
255
+ if (previous) writeAtomic(l.previous, `${previous.version} ${previous.node}\n`)
256
+ else rmSync(l.previous, { force: true })
257
+ writeAtomic(l.current, `${current.version} ${current.node}\n`)
258
+ return { current, previous }
259
+ } finally {
260
+ unlock()
261
+ }
262
+ }
263
+
264
+ /** Plan §10.5 step 6, on its own: what is neither `current` nor `previous` goes */
265
+ export function pruneNow({ dataDir = process.env.CC_DATA_DIR || join(homedir(), '.centralu') } = {}) {
266
+ const l = remoteLayout(dataDir)
267
+ mkdirSync(l.root, { recursive: true })
268
+ const unlock = lock(l)
269
+ try {
270
+ return { current: readPointer(l.current), previous: readPointer(l.previous), ...prune(l) }
271
+ } finally {
272
+ unlock()
273
+ }
274
+ }
275
+
276
+ async function main() {
277
+ try {
278
+ const p = JSON.parse(Buffer.from(process.argv[2] ?? '', 'base64').toString('utf8'))
279
+ const r = p.action === 'pointers' ? await setPointers(p) : p.action === 'prune' ? pruneNow() : await install(p)
280
+ process.stdout.write(`CENTRALU-INSTALL done ${JSON.stringify(r)}\n`)
281
+ } catch (err) {
282
+ const code = err instanceof Refusal ? err.code : 'error'
283
+ const detail = err instanceof Refusal ? err.detail : err.message
284
+ process.stdout.write(`CENTRALU-INSTALL fail ${code} ${String(detail).replace(/\s+/g, ' ')}\n`)
285
+ process.exitCode = 1
286
+ }
287
+ }
288
+
289
+ // Run as a program (not imported by a test). Node names its main module by its real path: compare that
290
+ if (process.argv[1] && import.meta.url === pathToFileURL(realpathSync(process.argv[1])).href) await main()
@@ -0,0 +1,21 @@
1
+ {
2
+ "node": {
3
+ "version": "24.21.0",
4
+ "signedBy": "5BE8A3F6C8A5C01D106C0AD820B1A390B168D356",
5
+ "releaseKeys": "481637f813e912c4aa3622d7964ab426c97b8e8d",
6
+ "archives": {
7
+ "linux-arm64": {
8
+ "file": "node-v24.21.0-linux-arm64.tar.gz",
9
+ "sha256": "724282c3b43aec998aa9527380465b45d229e021b58035f5f4f63095eabfe5d5"
10
+ },
11
+ "linux-x64": {
12
+ "file": "node-v24.21.0-linux-x64.tar.gz",
13
+ "sha256": "6e1db87ef58b8819e5d5402eff1536491b18edd8eb7bee5ef7897876e88dc5ff"
14
+ },
15
+ "win32-x64": {
16
+ "file": "node-v24.21.0-win-x64.zip",
17
+ "sha256": "158f7685b44de51f6c0df1d153526cbcd3e1bc739a8dfc607721cef75de9e541"
18
+ }
19
+ }
20
+ }
21
+ }
@@ -1,7 +1,7 @@
1
1
  {
2
- "commit": "6d18a3e",
2
+ "commit": "1a563a8",
3
3
  "protocolVersion": 1,
4
- "builtAt": "2026-10-08T05:46:15.346Z",
4
+ "builtAt": "2026-10-09T05:25:26.987Z",
5
5
  "runtime": "system-node",
6
6
  "platform": "darwin",
7
7
  "arch": "arm64"