acryl 0.2.4 → 0.2.6

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/bin.js CHANGED
@@ -1,24 +1,29 @@
1
1
  #!/usr/bin/env node
2
2
  import { spawn } from 'node:child_process'
3
3
  import { createRequire } from 'node:module'
4
- import { dirname, join } from 'node:path'
4
+ import { join } from 'node:path'
5
5
  import { createInterface } from 'node:readline/promises'
6
6
  import { stdin, stderr } from 'node:process'
7
- import { runtimeLauncher, targetFor } from './runtime.js'
7
+ import { forwardStopSignals, runtimeCommand, runtimeLauncher, targetFor } from './runtime.js'
8
8
  import { acquireDesktopInstaller } from './desktop-runtime.js'
9
- import { acquireWebRuntime, fetchReleaseManifest, hasVerifiedWebRuntime, manifestUrl, managedWebRoot, selectWebArtifact } from './web-runtime.js'
9
+ import { acquireWebRuntime, fetchReleaseManifest, hasVerifiedWebRuntime, manifestUrl, managedWebRoot, selectWebArtifact, webRuntimeCommand } from './web-runtime.js'
10
10
 
11
11
  const require = createRequire(import.meta.url)
12
12
  const selectorVersion = require('./package.json').version
13
13
  const args = process.argv.slice(2)
14
14
 
15
- function run(command, commandArgs) {
15
+ function run({ command, args: commandArgs }) {
16
16
  const child = spawn(command, commandArgs, { stdio: 'inherit' })
17
+ const stopForwarding = forwardStopSignals(child)
17
18
  child.once('error', cause => {
19
+ stopForwarding()
18
20
  console.error(`acryl: ${cause.message}`)
19
21
  process.exitCode = 1
20
22
  })
21
- child.once('exit', code => { process.exitCode = code ?? 1 })
23
+ child.once('exit', code => {
24
+ stopForwarding()
25
+ process.exitCode = code ?? 1
26
+ })
22
27
  }
23
28
 
24
29
  async function confirmDownload() {
@@ -65,7 +70,7 @@ async function runWeb(webArgs) {
65
70
  throw new Error('Web runtime acquisition requires confirmation. Re-run with: acryl web --yes')
66
71
  }
67
72
  const result = await acquireWebRuntime({ root: managedWebRoot(), version: selectorVersion, target, manifest })
68
- run(join(result.root, 'runtime', 'bin', process.platform === 'win32' ? 'acryl-web.cmd' : 'acryl-web'), forwarded)
73
+ run(webRuntimeCommand(result.root, forwarded))
69
74
  }
70
75
 
71
76
  if (args[0] === 'web') {
@@ -79,7 +84,7 @@ if (args[0] === 'web') {
79
84
  process.exitCode = 1
80
85
  })
81
86
  } else {
82
- try { run(runtimeLauncher(), args) } catch (cause) {
87
+ try { run(runtimeCommand(runtimeLauncher(), args)) } catch (cause) {
83
88
  console.error(`acryl: ${cause instanceof Error ? cause.message : String(cause)}`)
84
89
  process.exitCode = 1
85
90
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "acryl",
3
- "version": "0.2.4",
3
+ "version": "0.2.6",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "bin": {
@@ -15,10 +15,10 @@
15
15
  "README.md"
16
16
  ],
17
17
  "optionalDependencies": {
18
- "acryl-cli-darwin-arm64": "0.2.4",
19
- "acryl-cli-linux-arm64": "0.2.4",
20
- "acryl-cli-linux-x64": "0.2.4",
21
- "acryl-cli-windows-x64": "0.2.4"
18
+ "acryl-cli-darwin-arm64": "0.2.6",
19
+ "acryl-cli-linux-arm64": "0.2.6",
20
+ "acryl-cli-linux-x64": "0.2.6",
21
+ "acryl-cli-windows-x64": "0.2.6"
22
22
  },
23
23
  "engines": {
24
24
  "node": ">=22.19.0 || >=24.0.0"
@@ -3,6 +3,7 @@ import { readFileSync, readdirSync, statSync } from 'node:fs'
3
3
  import { join, relative } from 'node:path'
4
4
 
5
5
  export const RECEIPT_SCHEMA_VERSION = 1
6
+ export const RELEASE_MANIFEST_NAME = 'acryl-release-manifest.json'
6
7
 
7
8
  // darwin-x64 is intentionally absent: GitHub's macos-14 runners are arm64 and
8
9
  // cannot cross-compile sharp/koffi's native binaries for x64, so the CLI is
@@ -31,13 +32,16 @@ export function sha256(value) {
31
32
  return createHash('sha256').update(value).digest('hex')
32
33
  }
33
34
 
35
+ /** A payload file's name in the receipt: forward slashes on every platform, so a receipt made on one machine validates on another. */
36
+ export function payloadPath(path) { return path.replaceAll('\\', '/') }
37
+
34
38
  /** Hash a prepared runtime tree in a stable, receipt-excluding form. */
35
39
  export function payloadSha256(directory) {
36
40
  const entries = []
37
41
  const visit = current => {
38
42
  for (const entry of readdirSync(current)) {
39
43
  const absolute = join(current, entry)
40
- const path = relative(directory, absolute).replaceAll('\\\\', '/')
44
+ const path = payloadPath(relative(directory, absolute))
41
45
  if (path === 'receipt.json') continue
42
46
  // npm strips .npmignore/.gitignore/.npmrc when packing a vendored
43
47
  // node_modules, so exclude them from the payload hash or the installed
package/runtime.js CHANGED
@@ -35,3 +35,32 @@ export function runtimeLauncher({ require = createRequire(import.meta.url), plat
35
35
  }
36
36
  return join(root, 'runtime', 'bin', platform === 'win32' ? 'acryl.cmd' : 'acryl')
37
37
  }
38
+
39
+ /**
40
+ * How to start the CLI runtime whose launcher `runtimeLauncher` returned. Elsewhere that is the launcher itself. On Windows the launcher is a
41
+ * `.cmd`, which Node starts only through cmd.exe (CVE-2024-27980), where the user's arguments would be parsed again (`%`, `^`, `"`) and Ctrl-C
42
+ * asks "Terminate batch job (Y/N)?". So what the `.cmd` runs (apps/acryl-cli/scripts/acryl-cli-launcher.cmd) is run directly: this Node, with
43
+ * --expose-internals, on the runtime's lib/bin.js.
44
+ * @returns {{ command: string, args: string[] }}
45
+ */
46
+ export function runtimeCommand(launcher, args, { platform = process.platform, node = process.execPath } = {}) {
47
+ if (platform !== 'win32') return { command: launcher, args }
48
+ return { command: node, args: ['--expose-internals', join(dirname(dirname(launcher)), 'lib', 'bin.js'), ...args] }
49
+ }
50
+
51
+ export const STOP_SIGNALS = Object.freeze(['SIGINT', 'SIGTERM', 'SIGHUP'])
52
+
53
+ /**
54
+ * Pass the stop signals this process receives on to the runtime it started, and stay alive until that runtime exits. A supervisor or `kill`
55
+ * signals this process alone, and the runtime (the Web server, holding its port) outlived it. A terminal's Ctrl-C reaches both, so the runtime
56
+ * gets it twice and must take a repeated signal calmly (apps/acryl-web/src/serve.ts does). On Windows nothing is passed on: the console sends
57
+ * Ctrl-C to both processes itself, Node can only end a child there, not signal it, and the child is in a job that ends with this process.
58
+ * @returns {() => void} removes the handlers
59
+ */
60
+ export function forwardStopSignals(child, { platform = process.platform, host = process } = {}) {
61
+ const forward = signal => {
62
+ if (platform !== 'win32' && child.exitCode === null && child.signalCode === null) child.kill(signal)
63
+ }
64
+ for (const signal of STOP_SIGNALS) host.on(signal, forward)
65
+ return () => { for (const signal of STOP_SIGNALS) host.off(signal, forward) }
66
+ }
package/web-runtime.js CHANGED
@@ -10,6 +10,7 @@ import {
10
10
  } from 'node:fs'
11
11
  import { homedir } from 'node:os'
12
12
  import { dirname, join } from 'node:path'
13
+ import { RELEASE_MANIFEST_NAME } from './release-contract.js'
13
14
 
14
15
  const RECEIPT_NAME = '.acryl-web-runtime.json'
15
16
  const DEFAULT_RELEASE = 'https://github.com/acryldev/acryl/releases/download'
@@ -23,7 +24,7 @@ export function managedWebRoot(env = process.env) {
23
24
  }
24
25
 
25
26
  export function manifestUrl(version, env = process.env) {
26
- return env.ACRYL_RELEASE_MANIFEST_URL ?? `${DEFAULT_RELEASE}/v${version}/release-manifest.json`
27
+ return env.ACRYL_RELEASE_MANIFEST_URL ?? `${DEFAULT_RELEASE}/v${version}/${RELEASE_MANIFEST_NAME}`
27
28
  }
28
29
 
29
30
  export function confirmWebAcquisition({ yes, interactive, confirm }) {
@@ -66,12 +67,26 @@ export function hasVerifiedWebRuntime(root, { version, target, sha256 }) {
66
67
  } catch { return false }
67
68
  }
68
69
 
70
+ /**
71
+ * How to start a verified Web runtime under `root`. On Windows its launcher is a `.cmd`, started by Node only through cmd.exe (see
72
+ * `runtimeCommand` in runtime.js), so what the `.cmd` runs is run directly: the runtime's own node.exe on its lib/bin.js.
73
+ * @returns {{ command: string, args: string[] }}
74
+ */
75
+ export function webRuntimeCommand(root, args, { platform = process.platform } = {}) {
76
+ if (platform !== 'win32') return { command: join(root, 'runtime', 'bin', 'acryl-web'), args }
77
+ return { command: join(root, 'runtime', 'bin', 'node.exe'), args: [join(root, 'lib', 'bin.js'), ...args] }
78
+ }
79
+
80
+ // Windows' own tar (bsdtar): the first `tar` on a Windows PATH is often Git's GNU tar, which reads `C:\...` as a remote host.
81
+ const TAR = process.platform === 'win32' ? join(process.env.SystemRoot ?? 'C:\\Windows', 'System32', 'tar.exe') : 'tar'
82
+
69
83
  function defaultExtract(archive, destination) {
70
- execFileSync('tar', ['-xzf', archive, '--no-same-owner', '--no-same-permissions', '--strip-components=1', '-C', destination])
84
+ execFileSync(TAR, ['-xzf', archive, '--no-same-owner', '--no-same-permissions', '--strip-components=1', '-C', destination])
71
85
  }
72
86
 
73
87
  async function defaultDownload(url) {
74
- const response = await fetch(url, { redirect: 'error' })
88
+ // GitHub answers a release download with a redirect to its asset host; the archive is checked against the manifest's SHA-256 either way.
89
+ const response = await fetch(url, { redirect: 'follow' })
75
90
  if (!response.ok) fail(`Web artifact download failed: ${response.status} ${response.statusText}`)
76
91
  return Buffer.from(await response.arrayBuffer())
77
92
  }
@@ -124,7 +139,7 @@ export async function acquireWebRuntime({
124
139
  }
125
140
 
126
141
  export async function fetchReleaseManifest(url = manifestUrl('unknown')) {
127
- const response = await fetch(url, { redirect: 'error' })
142
+ const response = await fetch(url, { redirect: 'follow' })
128
143
  if (!response.ok) fail(`release manifest lookup failed: ${response.status} ${response.statusText}`)
129
144
  try {
130
145
  const manifest = await response.json()