Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -232,12 +232,15 @@
!/test/unit/sea/build/lock.test.mts
!/test/unit/sea/build/publish.test.mts
!/test/unit/sea/build-sea.test.mts
!/test/utils/
!/test/utils/sea.mts
!/test/unit/sea/download-bytes.test.mts
!/test/unit/sea/download-cache.test.mts
!/test/unit/sea/runtime.test.mts
!/test/unit/sea/version.test.mts
!/test/unit/utils/
!/test/unit/utils/agent.test.mts
!/test/unit/utils/quote-powershell.test.mts
!/test/unit/lib/
!/test/unit/lib/native/
!/test/unit/lib/native/cli-harness.test.mts
Expand Down Expand Up @@ -1054,6 +1057,9 @@
!/src/sea/adapters/synp.mts
!/src/sea/bootstrap.mts
!/src/sea/build-sea.mts
!/src/sea/readme-version.mts
!/src/sea/.node-version
!/src/sea/node-version.mts
!/src/sea/licenses/cdxgen-third-party/boolbase-1.0.0-ISC.txt
!/src/sea/licenses/cdxgen-third-party/err-code-2.0.3-MIT.txt
!/src/sea/licenses/cdxgen-third-party/imurmurhash-0.1.4-MIT.txt
Expand Down Expand Up @@ -1179,6 +1185,7 @@
!/src/utils/purl-legacy-npm-names.json
!/src/utils/purl-to-ghsa.mts
!/src/utils/purl.mts
!/src/utils/quote-powershell.mts
!/src/utils/registry-manifest.json
!/src/utils/requirements.mts
!/src/utils/sdk.mts
Expand Down
1 change: 1 addition & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ module.exports = [
// Allow loose one-off scripts.
'scripts/*.mts',
'scripts/test/*.mts',
'test/utils/*.mts',
],
defaultProject: 'tsconfig.json',
tsconfigRootDir: rootPath,
Expand Down
1 change: 1 addition & 0 deletions src/sea/.node-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
27.0.0-alpha.0.1.1
2 changes: 1 addition & 1 deletion src/sea/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ The CLI already runs on Node. SEA keeps that runtime and uses Node's VFS to embe
pnpm build:sea -- --prototype
```

The builder uses Node `v27.0.0-alpha.0.0.0`. It verifies the Node archive against the release `SHASUMS256.txt`. Node 27 is a prerelease, so these builds are experimental.
The builder uses Node <!-- socket-sea-node-version:start -->`v27.0.0-alpha.0.1.1`<!-- socket-sea-node-version:end -->. It verifies the Node archive against the release `SHASUMS256.txt`. Node 27 is a prerelease, so these builds are experimental.

Build on the target OS and architecture. The builder supports `darwin`, `linux`, and `win32`, with `x64` and `arm64` archives where Node publishes them. Cross-target builds fail.

Expand Down
10 changes: 6 additions & 4 deletions src/sea/build-sea.mts
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,22 @@ import { publishArtifactPair } from './build/publish.mts'
import { createSeaCapabilities } from './capabilities.mts'
import { downloadBytes } from './download-bytes.mts'
import { cachedDownload } from './download-cache.mts'
import { NODE_VERSION } from './node-version.mts'
import { updateSeaReadmeVersion } from './readme-version.mts'
import { stageSynp } from './stage-synp.mts'
import { stageUv } from './stage-uv.mts'
import { SEA_VERSION } from './version.mts'
import { quotePowerShell } from '../utils/quote-powershell.mts'

const HERE = path.dirname(fileURLToPath(import.meta.url))
const ROOT = path.resolve(HERE, '../..')
const NODE_VERSION = '27.0.0-alpha.0.0.0'
const MAX_PINNED_ARCHIVE_BYTES = 512 * 1024 * 1024
const MAX_NODE_SUMS_BYTES = 1024 * 1024
// `pnpm build:dist:src` starts by deleting `dist/`. Keep SEA artifacts outside
// that managed distribution tree so an ordinary package build cannot erase
// the output of a preceding SEA build.
const DEFAULT_OUT = path.join(ROOT, 'build/sea')

const TARGETS = [
['darwin', 'arm64', 'darwin-arm64'],
['darwin', 'x64', 'darwin-x64'],
Expand Down Expand Up @@ -638,7 +641,7 @@ async function pinnedNode(target: string): Promise<{
const extracted = path.join(owned, 'unpacked')
await fs.mkdir(extracted)
if (platform === 'win') {
const ps = `Expand-Archive -LiteralPath '${archivePath.replaceAll("'", "''")}' -DestinationPath '${extracted.replaceAll("'", "''")}' -Force`
const ps = `Add-Type -AssemblyName System.IO.Compression.FileSystem; [System.IO.Compression.ZipFile]::ExtractToDirectory(${quotePowerShell(archivePath)}, ${quotePowerShell(extracted)})`
await run('powershell', ['-NoProfile', '-Command', ps])
const extractedRoot = path.join(
extracted,
Expand Down Expand Up @@ -833,6 +836,7 @@ async function main(): Promise<void> {
'No matching supported target; pass --platform and --arch explicitly for cross builds',
)
}
await updateSeaReadmeVersion(path.join(HERE, 'README.md'), NODE_VERSION)
const [, , triple] = target
if (target[0] !== process.platform || target[1] !== process.arch) {
throw new Error(
Expand Down Expand Up @@ -1127,8 +1131,6 @@ async function main(): Promise<void> {
rootFiles,
})
if (process.platform === 'win32') {
const quotePowerShell = (value: string) =>
`'${value.replaceAll("'", "''")}'`
const sourceList = archivePaths
.map(filename => quotePowerShell(path.join(stage, filename)))
.join(', ')
Expand Down
10 changes: 10 additions & 0 deletions src/sea/node-version.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { readFileSync } from 'node:fs'

export const NODE_VERSION = readFileSync(
new URL('.node-version', import.meta.url),
'utf8',
).trim()

if (!NODE_VERSION) {
throw new Error('SEA Node version file is empty: src/sea/.node-version')
}
50 changes: 50 additions & 0 deletions src/sea/readme-version.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import fs from 'node:fs/promises'

export const NODE_VERSION_START = '<!-- socket-sea-node-version:start -->'
export const NODE_VERSION_END = '<!-- socket-sea-node-version:end -->'

export function renderSeaReadme(readme: string, nodeVersion: string): string {
const startIndex = readme.indexOf(NODE_VERSION_START)
const endIndex = readme.indexOf(NODE_VERSION_END)
let throws = startIndex === -1 || endIndex === -1 || startIndex >= endIndex

if (!throws) {
throws =
readme.indexOf(
NODE_VERSION_START,
startIndex + NODE_VERSION_START.length,
) !== -1
}

if (!throws) {
throws =
readme.indexOf(NODE_VERSION_END, endIndex + NODE_VERSION_END.length) !==
-1
}

if (throws) {
throw new Error(
'SEA README must contain one ordered Node version marker pair',
)
}

const versionStart = startIndex + NODE_VERSION_START.length
return [
readme.slice(0, versionStart),
`\`v${nodeVersion}\``,
readme.slice(endIndex),
].join('')
}

export async function updateSeaReadmeVersion(
readmePath: string,
nodeVersion: string,
): Promise<boolean> {
const original = await fs.readFile(readmePath, 'utf8')
const updated = renderSeaReadme(original, nodeVersion)
if (updated === original) {
return false
}
await fs.writeFile(readmePath, updated)
return true
}
4 changes: 4 additions & 0 deletions src/utils/quote-powershell.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/** Quote a value as a PowerShell single-quoted string literal. */
export function quotePowerShell(value: string): string {
return `'${value.replaceAll("'", "''")}'`
}
13 changes: 8 additions & 5 deletions test/unit/lib/terminal/output.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
normalizeBackendOutput,
normalizeBackendPaths,
} from '../../../lib/terminal/output.mts'
import { readSeaNodeVersion } from '../../../utils/sea.mts'

import type { TerminalBackend } from '@termless/core'

Expand All @@ -30,23 +31,25 @@ describe('terminal output normalization', () => {
)
})

it('normalizes only the manifest Node version across split chunks', () => {
it('normalizes only the manifest Node version across split chunks', async () => {
const received: Buffer[] = []
const backend = {
feed(data: Uint8Array) {
received.push(Buffer.from(data))
},
} as unknown as TerminalBackend
const nodeVersion = await readSeaNodeVersion()
const normalizer = normalizeBackendOutput(backend, [
{ search: 'v27.0.0-alpha.0.0.0', replacement: 'v<NODE_VERSION>' },
{ search: `v${nodeVersion}`, replacement: 'v<NODE_VERSION>' },
])
backend.feed(Buffer.from('Node.js v27.0.0-alpha.'))
backend.feed(Buffer.from('0.0.0; package 1.2.3'))
const splitAt = Math.floor(nodeVersion.length / 2)
backend.feed(Buffer.from(`Node.js v${nodeVersion.slice(0, splitAt)}`))
backend.feed(Buffer.from(`${nodeVersion.slice(splitAt)}; package 1.2.3`))
normalizer.flush()
expect(Buffer.concat(received).toString()).toBe(
'Node.js v<NODE_VERSION>; package 1.2.3',
)
expect(normalizer.matched('v27.0.0-alpha.0.0.0')).toBe(true)
expect(normalizer.matched(`v${nodeVersion}`)).toBe(true)
})

it('normalizes the socket-patch executable display token across split chunks', () => {
Expand Down
61 changes: 55 additions & 6 deletions test/unit/sea/build-sea.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,17 @@ import {
sha512IntegrityHex,
} from '../../../src/sea/build-sea.mts'
import { createSeaCapabilities } from '../../../src/sea/capabilities.mts'
import {
NODE_VERSION_END,
NODE_VERSION_START,
renderSeaReadme,
updateSeaReadmeVersion,
} from '../../../src/sea/readme-version.mts'
import { readSeaNodeVersion } from '../../utils/sea.mts'

const execFileAsync = promisify(execFile)
// PowerShell's archive cmdlets can be slow on shared Windows CI runners.
const WINDOWS_ARCHIVE_TEST_TIMEOUT_MS = 90_000
const root = path.resolve(
path.dirname(fileURLToPath(import.meta.url)),
'../../..',
Expand All @@ -40,8 +49,8 @@ describe('SEA build arguments', () => {
})

describe('SEA lockfile metadata', () => {
it('parses a unique exact Node archive checksum entry', () => {
const archive = 'node-v27.0.0-alpha.0.0.0-linux-x64.tar.gz'
it('parses a unique exact Node archive checksum entry', async () => {
const archive = `node-v${await readSeaNodeVersion()}-linux-x64.tar.gz`
const digest = 'a'.repeat(64)
expect(findNodeArchiveSha256(`${digest} ${archive}\n`, archive)).toBe(
digest,
Expand Down Expand Up @@ -86,6 +95,45 @@ describe('SEA lockfile metadata', () => {
})
})

describe('SEA README Node version', () => {
it('renders the pinned version between its hidden markers', async () => {
const nodeVersion = await readSeaNodeVersion()
const source = `The builder uses ${NODE_VERSION_START}old${NODE_VERSION_END}.`

expect(renderSeaReadme(source, nodeVersion)).toBe(
`The builder uses ${NODE_VERSION_START}\`v${nodeVersion}\`${NODE_VERSION_END}.`,
)
})

it('keeps the checked-in README version in sync with the pin', async () => {
const readme = await readFile(path.join(root, 'src/sea/README.md'), 'utf8')

expect(renderSeaReadme(readme, await readSeaNodeVersion())).toBe(readme)
})

it('updates only the marked version and is idempotent', async () => {
const directory = await mkdtemp(path.join(os.tmpdir(), 'sea-readme-test-'))
const readmePath = path.join(directory, 'README.md')
const source = `Keep this. ${NODE_VERSION_START}old${NODE_VERSION_END} Keep this too.`

try {
await writeFile(readmePath, source)

expect(
await updateSeaReadmeVersion(readmePath, await readSeaNodeVersion()),
).toBe(true)
expect(await readFile(readmePath, 'utf8')).toBe(
renderSeaReadme(source, await readSeaNodeVersion()),
)
expect(
await updateSeaReadmeVersion(readmePath, await readSeaNodeVersion()),
).toBe(false)
} finally {
await rm(directory, { recursive: true, force: true })
}
})
})

describe('SEA capability policy', () => {
it('keeps optional tools unavailable unless their payloads are present', () => {
expect(
Expand Down Expand Up @@ -115,7 +163,7 @@ describe('SEA archive paths', () => {
})

it.skipIf(process.platform !== 'win32')(
'keeps nested tools paths in a real Compress-Archive round trip',
'keeps nested tools paths in a real Windows ZIP round trip',
async () => {
const directory = await mkdtemp(path.join(os.tmpdir(), 'sea-zip-test-'))
const stage = path.join(directory, 'stage')
Expand All @@ -130,7 +178,8 @@ describe('SEA archive paths', () => {
'param([string]$Stage, [string]$Archive, [string]$Expanded)',
"$ErrorActionPreference = 'Stop'",
"Compress-Archive -LiteralPath (Join-Path $Stage 'tools') -DestinationPath $Archive -Force",
'Expand-Archive -LiteralPath $Archive -DestinationPath $Expanded -Force',
'Add-Type -AssemblyName System.IO.Compression.FileSystem',
'[System.IO.Compression.ZipFile]::ExtractToDirectory($Archive, $Expanded)',
"if (-not (Test-Path (Join-Path $Expanded 'tools/uv/uv.exe'))) { throw 'Nested uv executable was flattened' }",
].join('\n'),
)
Expand All @@ -148,7 +197,7 @@ describe('SEA archive paths', () => {
archive,
expanded,
],
{ timeout: 30_000 },
{ timeout: WINDOWS_ARCHIVE_TEST_TIMEOUT_MS },
)
expect(
(await stat(path.join(expanded, 'tools/uv/uv.exe'))).isFile(),
Expand All @@ -157,7 +206,7 @@ describe('SEA archive paths', () => {
await rm(directory, { recursive: true, force: true })
}
},
30_000,
WINDOWS_ARCHIVE_TEST_TIMEOUT_MS,
)
})

Expand Down
21 changes: 21 additions & 0 deletions test/unit/utils/quote-powershell.test.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { describe, expect, it } from 'vitest'

import { quotePowerShell } from '../../../src/utils/quote-powershell.mts'

describe('quotePowerShell', () => {
it('wraps a value in a PowerShell single-quoted string', () => {
expect(quotePowerShell('C:\\tools\\node.zip')).toBe("'C:\\tools\\node.zip'")
})

it('doubles single quotes inside the value', () => {
expect(quotePowerShell("C:\\Users\\O'Neil\\node.zip")).toBe(
"'C:\\Users\\O''Neil\\node.zip'",
)
})

it('leaves PowerShell expansion characters literal', () => {
expect(quotePowerShell('$(Write-Host `value`)')).toBe(
"'$(Write-Host `value`)'",
)
})
})
14 changes: 14 additions & 0 deletions test/utils/sea.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { readFile } from 'node:fs/promises'
import path from 'node:path'
import { fileURLToPath } from 'node:url'

const REPOSITORY_ROOT = path.resolve(
path.dirname(fileURLToPath(import.meta.url)),
'../..',
)

export async function readSeaNodeVersion(): Promise<string> {
return (
await readFile(path.join(REPOSITORY_ROOT, 'src/sea/.node-version'), 'utf8')
).trim()
}
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"src/**/*.mts",
"scripts/ci/**/*.mts",
"scripts/lib/**/*.mts",
"scripts/release/**/*.mts"
"scripts/release/**/*.mts",
"scripts/utils/**/*.mts"
],
"exclude": [
"src/**/*.test.mts",
Expand Down
Loading