From 596952352a757e955e47a9477da41281f1fb1d63 Mon Sep 17 00:00:00 2001 From: jdalton Date: Mon, 9 Nov 2026 22:26:49 -0500 Subject: [PATCH] Ship platform-specific native packages with a verified release workflow --- .github/workflows/ci.yml | 3 + .github/workflows/platform-release.yml | 161 +++++++++++++++++++++++++ .gitignore | 2 + docs/platform-package-sizes.json | 47 ++++++++ docs/platform-packages.md | 96 +++++++++++++++ package.json | 6 +- publish.yml | 8 +- scripts/native-target.js | 21 ++++ scripts/platform-packages.js | 125 +++++++++++++++++++ scripts/post-install.js | 4 +- scripts/prebuild.js | 19 +++ scripts/publish-platform-packages.js | 56 +++++++++ scripts/test-platform-release.js | 26 ++++ src/utils.ts | 14 ++- test/platform-packages.js | 143 ++++++++++++++++++++++ test/pty-smoke.js | 19 +++ 16 files changed, 741 insertions(+), 9 deletions(-) create mode 100644 .github/workflows/platform-release.yml create mode 100644 docs/platform-package-sizes.json create mode 100644 docs/platform-packages.md create mode 100644 scripts/native-target.js create mode 100644 scripts/platform-packages.js create mode 100644 scripts/publish-platform-packages.js create mode 100644 scripts/test-platform-release.js create mode 100644 test/platform-packages.js create mode 100644 test/pty-smoke.js diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1d8625ae4..a125a2567 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -83,3 +83,6 @@ jobs: - name: Test run: npm test + + - name: Test platform packages and real PTY loading + run: npm run test:platforms diff --git a/.github/workflows/platform-release.yml b/.github/workflows/platform-release.yml new file mode 100644 index 000000000..587950441 --- /dev/null +++ b/.github/workflows/platform-release.yml @@ -0,0 +1,161 @@ +name: Platform package release +on: + workflow_dispatch: + inputs: + version: + description: 'Exact release version (for example 1.2.0-beta.19)' + required: true + type: string + tag: + description: npm distribution tag + type: choice + options: [beta, latest] + default: beta + publish: + description: Publish verified tarballs to npm + type: boolean + default: false +permissions: + contents: read +concurrency: + group: node-pty-npm-release + cancel-in-progress: false +jobs: + build: + name: Build ${{ matrix.target }} + strategy: + fail-fast: false + matrix: + include: + - { os: ubuntu-22.04, target: linux-x64, arch: x64 } + - { os: ubuntu-22.04-arm, target: linux-arm64, arch: arm64 } + - { os: macos-14, target: darwin-arm64, arch: arm64 } + - { os: macos-15-intel, target: darwin-x64, arch: x64 } + - { os: windows-2022, target: win32-x64, arch: x64 } + - { os: windows-11-arm, target: win32-arm64, arch: arm64 } + runs-on: ${{ matrix.os }} + env: + RELEASE_VERSION: ${{ inputs.version }} + ARCH: ${{ matrix.arch }} + npm_config_arch: ${{ matrix.arch }} + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 + with: + node-version: '24.22.1' + - name: Validate release version + run: node -e "require('./scripts/platform-packages').validateVersion(process.env.RELEASE_VERSION)" + - name: Install Linux sysroot + if: runner.os == 'Linux' + shell: bash + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + set -euo pipefail + sudo apt-get update -qq + sudo apt-get install -y gcc-10 g++-10 + echo 'CC=gcc-10' >> "$GITHUB_ENV" + echo 'CXX=g++-10' >> "$GITHUB_ENV" + SYSROOT_PATH=$(node scripts/linux/install-sysroot.js "$ARCH" | grep 'SYSROOT_PATH=' | cut -d= -f2) + echo "SYSROOT_PATH=$SYSROOT_PATH" >> "$GITHUB_ENV" + - run: npm ci + - run: npm test + - run: npm run test:platforms + - name: Verify glibc compatibility + if: runner.os == 'Linux' + shell: bash + run: EXPECTED_GLIBC_VERSION=2.28 EXPECTED_GLIBCXX_VERSION=3.4.25 SEARCH_PATH=build ./scripts/linux/verify-glibc-requirements.sh + - name: Stage native package + shell: bash + run: npm run package:platforms -- platform release "$RELEASE_VERSION" '${{ matrix.target }}' + - uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4 + with: + name: native-${{ matrix.target }} + path: release/${{ matrix.target }}/ + if-no-files-found: error + pack: + needs: build + runs-on: ubuntu-22.04 + env: + RELEASE_VERSION: ${{ inputs.version }} + RELEASE_TAG: ${{ inputs.tag }} + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 + with: + node-version: '24.22.1' + - uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4 + with: + pattern: native-* + path: artifacts + - name: Assemble all platforms + shell: bash + run: | + mkdir release + for source in artifacts/native-*; do + target=${source#artifacts/native-} + mv "$source" "release/$target" + done + chmod +x release/darwin-*/spawn-helper + - run: npm ci --ignore-scripts + - run: npm run build + - name: Pack and verify complete release + run: | + npm run package:platforms -- release release "$RELEASE_VERSION" + npm run publish:platforms -- release/tarballs "$RELEASE_TAG" + - uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4 + with: + name: npm-release + path: release/tarballs/ + if-no-files-found: error + verify-install: + needs: pack + strategy: + fail-fast: false + matrix: + os: [ubuntu-22.04, ubuntu-22.04-arm, macos-14, macos-15-intel, windows-2022, windows-11-arm] + runs-on: ${{ matrix.os }} + env: + RELEASE_TAG: ${{ inputs.tag }} + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 + with: + node-version: '24.22.1' + - uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4 + with: + name: npm-release + path: tarballs + - name: Install tarballs without scripts and spawn a real PTY + shell: bash + run: npm run test:release -- tarballs "$RELEASE_TAG" + publish: + needs: verify-install + if: inputs.publish && github.repository == 'microsoft/node-pty' && github.ref == 'refs/heads/main' + runs-on: ubuntu-22.04 + environment: npm + permissions: + contents: read + id-token: write + env: + RELEASE_TAG: ${{ inputs.tag }} + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 + with: + node-version: '24.22.1' + registry-url: https://registry.npmjs.org + - uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4 + with: + name: npm-release + path: tarballs + - name: Publish native packages, then node-pty + run: npm run publish:platforms -- tarballs "$RELEASE_TAG" --publish diff --git a/.gitignore b/.gitignore index 7de13cd9c..805fd5fd1 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,5 @@ fixtures/space folder/ yarn.lock prebuilds/ _codeql_detected_source_root + +release/ diff --git a/docs/platform-package-sizes.json b/docs/platform-package-sizes.json new file mode 100644 index 000000000..480306ec8 --- /dev/null +++ b/docs/platform-package-sizes.json @@ -0,0 +1,47 @@ +{ + "input": "node-pty@1.2.0-beta.18 native prebuilds; current branch JS wrapper; package-file sizes excluding dependencies and filesystem allocation", + "packages": [ + { + "name": "node-pty", + "version": "1.2.0-beta.19", + "tarballBytes": 1227892, + "unpackedBytes": 2533645 + }, + { + "name": "@node-pty/darwin-arm64", + "version": "1.2.0-beta.19", + "tarballBytes": 23758, + "unpackedBytes": 141207 + }, + { + "name": "@node-pty/darwin-x64", + "version": "1.2.0-beta.19", + "tarballBytes": 23666, + "unpackedBytes": 71719 + }, + { + "name": "@node-pty/linux-arm64", + "version": "1.2.0-beta.19", + "tarballBytes": 27050, + "unpackedBytes": 72924 + }, + { + "name": "@node-pty/linux-x64", + "version": "1.2.0-beta.19", + "tarballBytes": 28980, + "unpackedBytes": 75604 + }, + { + "name": "@node-pty/win32-arm64", + "version": "1.2.0-beta.19", + "tarballBytes": 755734, + "unpackedBytes": 1625457 + }, + { + "name": "@node-pty/win32-x64", + "version": "1.2.0-beta.19", + "tarballBytes": 831075, + "unpackedBytes": 1604537 + } + ] +} diff --git a/docs/platform-packages.md b/docs/platform-packages.md new file mode 100644 index 000000000..05cbbf08b --- /dev/null +++ b/docs/platform-packages.md @@ -0,0 +1,96 @@ +# Platform-specific packages + +This implements [node-pty #864](https://github.com/microsoft/node-pty/issues/864) +using the optional-package model from [@lydell/node-pty](https://github.com/lydell/node-pty). +The public `node-pty` entry point and types stay the same. Exact-version optional +dependencies select `@node-pty/-` for Darwin, Linux, and Windows, on ARM64 +and x64. Linux packages declare glibc, matching the existing 2.28 sysroot; the +install script also detects musl for package managers that ignore `libc`. + +Unlike Lydell's prebuilt-only distribution, the wrapper keeps the source and +Windows `third_party` assets needed for node-gyp. Source builds, `--omit=optional`, +and `npm_config_build_from_source=true` remain supported. A forced source build +never removes files from an optional package shared by a package manager. Local +Release/Debug builds and legacy `prebuilds/` directories retain loading priority. + +Native packages contain only runtime `.node` files, macOS `spawn-helper`, and +Windows `conpty/` files. They omit PDB debug symbols and have no install scripts. +The loader resolves the package's absolute directory so helper and DLL paths work +with hoisted dependencies and symlinked package-manager layouts. `--ignore-scripts` +works when the matching optional package is installed. Without optional packages +and without build scripts, native loading cannot work. + +## Release workflow + +Lydell's repository has build and publish scripts; its publish script publishes +native packages before the wrapper. `.github/workflows/platform-release.yml` +implements that order with native builds and real installed-tarball tests: + +1. Dispatch with an explicit version and `beta` or `latest` tag. Publishing is off + by default. Inputs are validated and passed through environment variables. +2. Six native runners build and run node-pty's tests. Linux uses the existing + sysroot and glibc checks. Platform tests exercise real PTY spawning through an + isolated optional package. Native headers must match their advertised target. +3. A packaging job checks that all six packages have matching versions and target + constraints. It generates the wrapper with exact optional dependency versions, + then creates seven tarballs and a SHA-512 release manifest. +4. Six runners install the actual matching tarball and wrapper with install scripts + disabled, then spawn a PTY. Publication depends on all six passing. +5. An opt-in job on upstream `main`, protected by the `npm` environment, checks + every tarball hash and publishes the six native packages before `node-pty`. + Publication stops on any failure. Reruns skip versions only when npm reports + identical tarball integrity; differing published bytes abort the release. + +Azure's existing build/test/scan pipeline remains, but its publish switch is +removed to prevent it from publishing a monolithic wrapper independently. Its +checks are separate from the new GitHub release workflow; existing organizational +release/security gates must also be configured for the new `npm` environment. + +### Maintainer setup + +The `@node-pty` scope and six package names must be owned by the maintainers. +Configure npm trusted publishing for all seven packages to this repository, +`platform-release.yml`, and environment `npm`. New package bootstrap and protected +environment approvals require maintainer access. No npm packages, credentials, +or environments are created by this PR. The workflow requests OIDC only in the +publish job; dry runs and fork tests cannot publish. Download the `npm-release` +artifact to inspect the exact tarballs before enabling publication. + +### Local commands + +After `npm ci`, build one platform package: + +```sh +npm run package:platforms -- platform release 1.2.0-beta.19 darwin-arm64 +``` + +Collect all six platform directories under `release/`, then: + +```sh +npm run package:platforms -- release release 1.2.0-beta.19 +npm run publish:platforms -- release/tarballs beta +npm run test:release -- release/tarballs beta +``` + +The publish command is a dry run unless `--publish` is explicitly supplied. The +packed smoke test uses the current platform. `npm run test:platforms` covers +version/target rejection, incomplete releases, package metadata, integrity and +publish ordering, source fallback, and real native loading. npm is used for the +installed-tarball tests; pnpm/Yarn and a real npm publication need separate release +validation. + +## Size example + +Using the six native builds from node-pty 1.2.0-beta.18 and this branch's wrapper, +the macOS ARM64 installation contains 2,674,852 bytes across its two packages, +versus the beta's 26,907,504 bytes (90.1% less). The two tarballs total 1,251,650 +bytes versus 7,152,900 bytes (82.5% less). These are package-file sums, excluding +shared dependencies and filesystem allocation. The result includes omitting PDB +symbols as well as omitting other platforms; it is not solely a platform-filtering +measurement. The wrapper retains about 2.5 MB including source-build assets. + +The exact local release was dry-run validated and its wrapper/native tarballs +were installed with scripts disabled and used to spawn a real PTY on macOS ARM64. +The six-platform workflow still needs upstream execution and npm/environment setup. + +[Per-package measurements](platform-package-sizes.json) include all six targets. diff --git a/package.json b/package.json index 08e2d0687..38b699400 100644 --- a/package.json +++ b/package.json @@ -57,7 +57,11 @@ "test": "cross-env NODE_ENV=test mocha -R spec --exit lib/*.test.js", "posttest": "npm run lint", "prepare": "npm run build", - "prepublishOnly": "npm run build" + "prepublishOnly": "npm run build", + "package:platforms": "node scripts/platform-packages.js", + "test:platforms": "mocha --exit test/platform-packages.js", + "publish:platforms": "node scripts/publish-platform-packages.js", + "test:release": "node scripts/test-platform-release.js" }, "dependencies": { "node-addon-api": "^7.1.0" diff --git a/publish.yml b/publish.yml index 7c89a7964..71c73e6f0 100644 --- a/publish.yml +++ b/publish.yml @@ -16,10 +16,6 @@ resources: endpoint: Monaco parameters: - - name: publishPackage - displayName: 🚀 Publish node-pty - type: boolean - default: false - name: releaseQuality displayName: Quality type: string @@ -91,7 +87,9 @@ extends: - script: npm run lint displayName: 'Lint' - publishPackage: ${{ parameters.publishPackage }} + # Publishing moved to .github/workflows/platform-release.yml so all + # native packages are published before the wrapper. Keep Azure checks. + publishPackage: false ${{ if eq(variables['releaseQuality'], 'stable') }}: tag: latest publishRequiresApproval: true diff --git a/scripts/native-target.js b/scripts/native-target.js new file mode 100644 index 000000000..850b8d689 --- /dev/null +++ b/scripts/native-target.js @@ -0,0 +1,21 @@ +// Copyright (c) Microsoft Corporation. Licensed under the MIT License. +// Check the compiler artifact before labeling a package with a platform/CPU. +// The native loader still validates the complete image and its signature. +function verifyNativeTarget(bytes, target) { + const [platform, arch] = target.split('-'); + let valid = false; + if (bytes.length >= 64 && (arch === 'arm64' || arch === 'x64')) { + if (platform === 'linux') { + valid = bytes.subarray(0, 4).equals(Buffer.from([0x7f, 0x45, 0x4c, 0x46])) && + bytes[4] === 2 && bytes[5] === 1 && bytes.readUInt16LE(18) === (arch === 'arm64' ? 183 : 62); + } else if (platform === 'darwin') { + valid = bytes.readUInt32LE(0) === 0xfeedfacf && bytes.readUInt32LE(4) === (arch === 'arm64' ? 0x100000c : 0x1000007); + } else if (platform === 'win32') { + const offset = bytes.readUInt32LE(0x3c); + valid = bytes[0] === 0x4d && bytes[1] === 0x5a && offset <= bytes.length - 6 && + bytes.readUInt32LE(offset) === 0x4550 && bytes.readUInt16LE(offset + 4) === (arch === 'arm64' ? 0xaa64 : 0x8664); + } + } + if (!valid) { throw new Error(`Native image does not match ${target}`); } +} +module.exports = { verifyNativeTarget }; diff --git a/scripts/platform-packages.js b/scripts/platform-packages.js new file mode 100644 index 000000000..229adb9ea --- /dev/null +++ b/scripts/platform-packages.js @@ -0,0 +1,125 @@ +// Copyright (c) Microsoft Corporation. Licensed under the MIT License. +const fs = require('fs'); +const path = require('path'); +const crypto = require('crypto'); +const cp = require('child_process'); +const { verifyNativeTarget } = require('./native-target'); + +const TARGETS = ['darwin-arm64', 'darwin-x64', 'linux-arm64', 'linux-x64', 'win32-arm64', 'win32-x64']; +const ROOT = path.resolve(__dirname, '..'); +const SCOPE = '@node-pty'; + +function validateVersion(version) { + if (!/^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-[0-9A-Za-z]+(?:[.-][0-9A-Za-z]+)*)?$/.test(version)) { + throw new Error(`Invalid release version: ${version}`); + } +} +function runtimeFiles(target) { + if (!TARGETS.includes(target)) { throw new Error(`Unknown target: ${target}`); } + return target.startsWith('win32-') + ? ['conpty.node', 'conpty_console_list.node', 'conpty/conpty.dll', 'conpty/OpenConsole.exe'] + : target.startsWith('darwin-') ? ['pty.node', 'spawn-helper'] : ['pty.node']; +} +function copyFile(source, destination) { + if (!fs.lstatSync(source).isFile()) { throw new Error(`Not a regular file: ${source}`); } + fs.mkdirSync(path.dirname(destination), { recursive: true }); + fs.copyFileSync(source, destination, fs.constants.COPYFILE_EXCL); +} +function writeJson(filename, value) { fs.writeFileSync(filename, JSON.stringify(value, null, 2) + '\n', { flag: 'wx' }); } + +function buildPlatform(source, output, target, version) { + validateVersion(version); + const files = runtimeFiles(target); + fs.mkdirSync(output, { recursive: true }); + const [os, cpu] = target.split('-'); + for (const file of files) { + const input = path.join(source, file); + if (file.endsWith('.node')) { verifyNativeTarget(fs.readFileSync(input), target); } + copyFile(input, path.join(output, file)); + } + if (target.startsWith('darwin-')) { fs.chmodSync(path.join(output, 'spawn-helper'), 0o755); } + copyFile(path.join(ROOT, 'LICENSE'), path.join(output, 'LICENSE')); + writeJson(path.join(output, 'package.json'), { + name: `${SCOPE}/${target}`, version, description: `Native binaries for node-pty on ${target}`, + license: 'MIT', repository: require('../package.json').repository, os: [os], cpu: [cpu], + // Match the glibc 2.28 sysroot used by the existing prebuild pipeline. + ...(os === 'linux' ? { libc: ['glibc'] } : {}), + files: [...files, 'LICENSE', 'README.md'] + }); + fs.writeFileSync(path.join(output, 'README.md'), `# ${SCOPE}/${target}\n\nNative binaries for node-pty. Installed automatically as an optional dependency.\n`); +} + +function verifyPlatforms(root, version) { + for (const target of TARGETS) { + const dir = path.join(root, target); + const pkg = JSON.parse(fs.readFileSync(path.join(dir, 'package.json'), 'utf8')); + const [os, cpu] = target.split('-'); + if (pkg.name !== `${SCOPE}/${target}` || pkg.version !== version || + JSON.stringify(pkg.os) !== JSON.stringify([os]) || JSON.stringify(pkg.cpu) !== JSON.stringify([cpu]) || + (os === 'linux' && JSON.stringify(pkg.libc) !== '["glibc"]')) { + throw new Error(`Mismatched platform package: ${target}`); + } + for (const file of runtimeFiles(target)) { + const stat = fs.lstatSync(path.join(dir, file)); + if (!stat.isFile() || !stat.size) { throw new Error(`Missing runtime file: ${target}/${file}`); } + if (file.endsWith('.node')) { verifyNativeTarget(fs.readFileSync(path.join(dir, file)), target); } + } + } +} + +function buildWrapper(root, version) { + validateVersion(version); + verifyPlatforms(root, version); + const output = path.join(root, 'node-pty'); + fs.mkdirSync(output); + // npm's file list selects public JS, typings, and source-build inputs. Source + // builds keep third_party conpty assets, including unsupported architectures. + const packageJson = JSON.parse(fs.readFileSync(path.join(ROOT, 'package.json'), 'utf8')); + for (const name of ['lib', 'src', 'scripts', 'typings', 'third_party']) { + fs.cpSync(path.join(ROOT, name), path.join(output, name), { recursive: true }); + } + for (const name of ['binding.gyp', 'LICENSE', 'README.md']) { copyFile(path.join(ROOT, name), path.join(output, name)); } + packageJson.version = version; + packageJson.files = packageJson.files.filter(file => file !== 'prebuilds/'); + packageJson.optionalDependencies = Object.fromEntries(TARGETS.map(target => [`${SCOPE}/${target}`, version])); + // Publishing a staged package must not compile or change release inputs. + delete packageJson.scripts.prepare; + delete packageJson.scripts.prepublishOnly; + writeJson(path.join(output, 'package.json'), packageJson); +} + +function npm(args, options = {}) { + // npm_execpath avoids shell quoting and npm.cmd behavior on Windows. + const npmPath = process.env.npm_execpath; + if (!npmPath) { throw new Error('Run this script through npm run'); } + const result = cp.spawnSync(process.execPath, [npmPath, ...args], { encoding: 'utf8', ...options }); + if (result.status !== 0) { throw new Error(result.stderr || result.error || `npm ${args[0]} failed`); } + return result.stdout; +} +function packRelease(root, version) { + verifyPlatforms(root, version); + const artifacts = path.join(root, 'tarballs'); + fs.mkdirSync(artifacts); + const packages = []; + for (const target of [...TARGETS, 'node-pty']) { + const dir = path.join(root, target); + const pkg = JSON.parse(fs.readFileSync(path.join(dir, 'package.json'), 'utf8')); + const report = JSON.parse(npm(['pack', '--ignore-scripts', '--json', '--pack-destination', artifacts], { cwd: dir })); + const packed = Array.isArray(report) ? report[0] : Object.values(report)[0]; + const bytes = fs.readFileSync(path.join(artifacts, packed.filename)); + packages.push({ name: pkg.name, version: pkg.version, filename: packed.filename, + integrity: `sha512-${crypto.createHash('sha512').update(bytes).digest('base64')}` }); + } + writeJson(path.join(artifacts, 'release.json'), { version, packages }); +} + +if (require.main === module) { + const [command, root, version, target] = process.argv.slice(2); + if (command === 'platform') { + buildPlatform(path.join(ROOT, 'build/Release'), path.join(path.resolve(root), target), target, version); + } else if (command === 'release') { + buildWrapper(path.resolve(root), version); + packRelease(path.resolve(root), version); + } else { throw new Error('Usage: npm run package:platforms -- platform|release OUTPUT VERSION [TARGET]'); } +} +module.exports = { TARGETS, SCOPE, runtimeFiles, validateVersion, buildPlatform, buildWrapper, verifyPlatforms }; diff --git a/scripts/post-install.js b/scripts/post-install.js index dfb4d2f72..878a0506b 100644 --- a/scripts/post-install.js +++ b/scripts/post-install.js @@ -44,8 +44,8 @@ try { } console.log(`\x1b[32m> Moving conpty.dll...\x1b[0m`); -if (os.platform() !== 'win32') { - console.log(' SKIPPED (not Windows)'); +if (os.platform() !== 'win32' || !fs.existsSync(path.join(RELEASE_DIR, 'conpty.node'))) { + console.log(' SKIPPED (no local Windows build)'); } else { let windowsArch; if (process.env.npm_config_arch) { diff --git a/scripts/prebuild.js b/scripts/prebuild.js index 17f1d980e..ce1494948 100644 --- a/scripts/prebuild.js +++ b/scripts/prebuild.js @@ -24,6 +24,25 @@ if (process.env.npm_config_build_from_source === 'true') { process.exit(1); } +// Optional packages are installed before the parent's install lifecycle. Keep +// source builds and legacy prebuild directories working as before. +try { + if (process.platform === 'linux' && !process.report?.getReport().header.glibcVersionRuntime) { + console.log('> Linux prebuilds require glibc; building from source'); + process.exit(1); + } + const packageDir = path.dirname(require.resolve(`@node-pty/${process.platform}-${process.arch}/package.json`)); + const files = process.platform === 'win32' + ? ['conpty.node', 'conpty_console_list.node', 'conpty/conpty.dll', 'conpty/OpenConsole.exe'] + : process.platform === 'darwin' ? ['pty.node', 'spawn-helper'] : ['pty.node']; + if (files.every(file => fs.existsSync(path.join(packageDir, file)))) { + console.log('> Using platform package'); + process.exit(0); + } +} catch (error) { + if (error.code !== 'MODULE_NOT_FOUND') { throw error; } +} + // Check whether the correct prebuilt files exist console.log('\x1b[32m> Checking prebuilds...\x1b[0m'); if (!fs.existsSync(PREBUILD_DIR)) { diff --git a/scripts/publish-platform-packages.js b/scripts/publish-platform-packages.js new file mode 100644 index 000000000..f79a3138b --- /dev/null +++ b/scripts/publish-platform-packages.js @@ -0,0 +1,56 @@ +// Copyright (c) Microsoft Corporation. Licensed under the MIT License. +const fs = require('fs'); +const path = require('path'); +const crypto = require('crypto'); +const cp = require('child_process'); +const { TARGETS, SCOPE, validateVersion } = require('./platform-packages'); + +function readRelease(root, tag) { + if (!['beta', 'latest'].includes(tag)) { throw new Error('Release tag must be beta or latest'); } + const release = JSON.parse(fs.readFileSync(path.join(root, 'release.json'), 'utf8')); + validateVersion(release.version); + if ((tag === 'latest') === release.version.includes('-')) { throw new Error('Release version does not match tag'); } + const expected = [...TARGETS.map(target => `${SCOPE}/${target}`), 'node-pty']; + if (!Array.isArray(release.packages) || release.packages.length !== expected.length) { throw new Error('Incomplete release'); } + for (let i = 0; i < expected.length; i++) { + const pkg = release.packages[i]; + if (pkg.name !== expected[i] || pkg.version !== release.version || + typeof pkg.filename !== 'string' || !/^[a-z0-9.-]+\.tgz$/.test(pkg.filename)) { + throw new Error('Invalid release package or publish order'); + } + const stat = fs.lstatSync(path.join(root, pkg.filename)); + if (!stat.isFile()) { throw new Error('Release tarball must be a regular file'); } + const digest = crypto.createHash('sha512').update(fs.readFileSync(path.join(root, pkg.filename))).digest('base64'); + if (pkg.integrity !== `sha512-${digest}`) { throw new Error(`Changed release artifact: ${pkg.name}`); } + } + return release; +} +function npm(args) { + if (!process.env.npm_execpath) { throw new Error('Run through npm run publish:platforms'); } + return cp.spawnSync(process.execPath, [process.env.npm_execpath, ...args], { encoding: 'utf8' }); +} +function publish(root, tag, execute = false, run = npm) { + const release = readRelease(root, tag); + // Validate every artifact before any remote mutation. All six native packages + // must be present before publishing the wrapper's exact optional dependencies. + for (const pkg of release.packages) { + if (!execute) { console.log(`Would publish ${pkg.name}@${pkg.version} with tag ${tag}`); continue; } + const existing = run(['view', `${pkg.name}@${pkg.version}`, 'dist.integrity', '--json', '--registry=https://registry.npmjs.org']); + if (existing.status === 0) { + if (JSON.parse(existing.stdout) !== pkg.integrity) { throw new Error(`Published bytes differ: ${pkg.name}`); } + console.log(`Already published with identical bytes: ${pkg.name}`); + continue; + } + if (!existing.stderr.includes('E404')) { throw new Error(existing.stderr || 'Cannot query npm'); } + const result = run(['publish', path.join(root, pkg.filename), '--ignore-scripts', '--access=public', '--provenance', + `--tag=${tag}`, '--registry=https://registry.npmjs.org']); + if (result.status !== 0) { throw new Error(result.stderr || `Publishing ${pkg.name} failed`); } + console.log(result.stdout); + } +} +if (require.main === module) { + const [root, tag, execute] = process.argv.slice(2); + if (execute !== undefined && execute !== '--publish') { throw new Error('Expected --publish or no third argument'); } + publish(path.resolve(root), tag, execute === '--publish'); +} +module.exports = { readRelease, publish }; diff --git a/scripts/test-platform-release.js b/scripts/test-platform-release.js new file mode 100644 index 000000000..ce55122fb --- /dev/null +++ b/scripts/test-platform-release.js @@ -0,0 +1,26 @@ +// Install the exact packed wrapper + matching native tarball with lifecycle +// scripts disabled, then exercise native loading and actual PTY I/O. +const fs = require('fs'); +const path = require('path'); +const os = require('os'); +const cp = require('child_process'); +const { readRelease } = require('./publish-platform-packages'); +const root = path.resolve(process.argv[2]); +const release = readRelease(root, process.argv[3]); +const target = `@node-pty/${process.platform}-${process.arch}`; +const native = release.packages.find(pkg => pkg.name === target); +const wrapper = release.packages.find(pkg => pkg.name === 'node-pty'); +const work = fs.mkdtempSync(path.join(os.tmpdir(), 'node-pty-packed-')); +try { + if (!process.env.npm_execpath) { throw new Error('Run through npm run'); } + const result = cp.spawnSync(process.execPath, [process.env.npm_execpath, 'install', '--ignore-scripts', + '--no-audit', '--no-fund', '--package-lock=false', path.join(root, wrapper.filename), path.join(root, native.filename)], + { cwd: work, encoding: 'utf8' }); + if (result.status !== 0) { throw new Error(result.stderr || result.error || 'Install failed'); } + const installed = path.join(work, 'node_modules/node-pty'); + if (fs.existsSync(path.join(installed, 'build')) || fs.existsSync(path.join(installed, 'prebuilds'))) { + throw new Error('Packed smoke must use only the optional native package'); + } + const smoke = cp.spawnSync(process.execPath, [path.resolve(__dirname, '../test/pty-smoke.js'), installed], { stdio: 'inherit' }); + if (smoke.status !== 0) { throw new Error('Packed PTY smoke failed'); } +} finally { fs.rmSync(work, { recursive: true, force: true }); } diff --git a/src/utils.ts b/src/utils.ts index 1d2d7c933..543d96e5b 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -3,6 +3,8 @@ * Copyright (c) 2018, Microsoft Corporation (MIT License). */ +import * as path from 'path'; + export function assign(target: any, ...sources: any[]): any { sources.forEach(source => Object.keys(source).forEach(key => target[key] = source[key])); return target; @@ -25,5 +27,15 @@ export function loadNativeModule(name: string): {dir: string, module: any} { } } } - throw new Error(`Failed to load native module: ${name}.node, checked: ${dirs.join(', ')}: ${lastError}`); + const packageName = `@node-pty/${process.platform}-${process.arch}`; + try { + if (process.platform === 'linux' && !(process.report?.getReport() as any)?.header?.glibcVersionRuntime) { + throw new Error('Linux prebuilds require glibc; install node-pty with source builds enabled'); + } + const dir = path.dirname(require.resolve(`${packageName}/package.json`)); + return { dir, module: require(path.join(dir, `${name}.node`)) }; + } catch (e) { + lastError = e; + } + throw new Error(`Failed to load native module: ${name}.node, checked: ${dirs.join(', ')}, ${packageName}: ${lastError}`); } diff --git a/test/platform-packages.js b/test/platform-packages.js new file mode 100644 index 000000000..020bddb1d --- /dev/null +++ b/test/platform-packages.js @@ -0,0 +1,143 @@ +// Copyright (c) Microsoft Corporation. Licensed under the MIT License. +const assert = require('assert'); +const fs = require('fs'); +const path = require('path'); +const os = require('os'); +const cp = require('child_process'); +const crypto = require('crypto'); +const { TARGETS, SCOPE, buildPlatform, buildWrapper, validateVersion, verifyPlatforms, runtimeFiles } = require('../scripts/platform-packages'); +const { readRelease, publish } = require('../scripts/publish-platform-packages'); +const target = `${process.platform}-${process.arch}`; +let work; +function child(script, env = process.env) { + return new Promise((resolve, reject) => { + const p = cp.spawn(process.execPath, ['-e', script], { env }); + let output = ''; + p.stdout.on('data', chunk => output += chunk); + p.stderr.on('data', chunk => output += chunk); + p.on('error', reject); + p.on('exit', code => code === 0 ? resolve(output) : reject(new Error(`Exit ${code}: ${output}`))); + }); +} +function fakeRelease() { + const root = path.join(work, 'release'); + fs.mkdirSync(root); + for (const name of TARGETS) { + const source = path.join(work, name); + for (const file of runtimeFiles(name)) { + fs.mkdirSync(path.dirname(path.join(source, file)), { recursive: true }); + const bytes = Buffer.alloc(128); + const [platform, arch] = name.split('-'); + if (platform === 'darwin') { bytes.writeUInt32LE(0xfeedfacf, 0); bytes.writeUInt32LE(arch === 'arm64' ? 0x100000c : 0x1000007, 4); } + if (platform === 'linux') { Buffer.from([0x7f, 0x45, 0x4c, 0x46, 2, 1]).copy(bytes); bytes.writeUInt16LE(arch === 'arm64' ? 183 : 62, 18); } + if (platform === 'win32') { bytes.write('MZ'); bytes.writeUInt32LE(64, 60); bytes.writeUInt32LE(0x4550, 64); bytes.writeUInt16LE(arch === 'arm64' ? 0xaa64 : 0x8664, 68); } + fs.writeFileSync(path.join(source, file), bytes); + } + buildPlatform(source, path.join(root, name), name, '1.2.0-beta.19'); + } + return root; +} +describe('platform packages', function () { + this.timeout(30000); + beforeEach(() => { work = fs.mkdtempSync(path.join(os.tmpdir(), 'node-pty-platform-test-')); }); + afterEach(() => fs.rmSync(work, { recursive: true, force: true })); + it('validates release version and target before staging', () => { + for (const version of ['../../bad', '--tag=latest', '1.2', '', '01.2.3', '1.2.3;echo hi']) { + assert.throws(() => validateVersion(version)); + } + assert.throws(() => buildPlatform(work, path.join(work, 'out'), '../bad', '1.2.3')); + }); + it('builds all package metadata with exact optional versions and no debug symbols', () => { + const root = fakeRelease(); + verifyPlatforms(root, '1.2.0-beta.19'); + buildWrapper(root, '1.2.0-beta.19'); + const wrapper = JSON.parse(fs.readFileSync(path.join(root, 'node-pty/package.json'))); + assert.deepStrictEqual(wrapper.optionalDependencies, Object.fromEntries(TARGETS.map(name => [`${SCOPE}/${name}`, wrapper.version]))); + assert.ok(!wrapper.files.includes('prebuilds/')); + assert.ok(wrapper.files.includes('third_party/')); + assert.ok(!wrapper.scripts.prepare && !wrapper.scripts.prepublishOnly); + for (const name of TARGETS) { + const pkg = JSON.parse(fs.readFileSync(path.join(root, name, 'package.json'))); + assert.deepStrictEqual(pkg.os, [name.split('-')[0]]); + assert.deepStrictEqual(pkg.cpu, [name.split('-')[1]]); + assert.ok(!pkg.scripts); + assert.ok(!pkg.files.some(file => file.endsWith('.pdb'))); + if (name.startsWith('linux-')) { assert.deepStrictEqual(pkg.libc, ['glibc']); } + } + }); + it('refuses incomplete or mismatched releases before creating the wrapper', () => { + const root = fakeRelease(); + fs.unlinkSync(path.join(root, 'darwin-x64/pty.node')); + assert.throws(() => buildWrapper(root, '1.2.0-beta.19')); + assert.ok(!fs.existsSync(path.join(root, 'node-pty'))); + }); + it('checks every tarball and publish order before publishing anything', () => { + const packages = [...TARGETS.map(name => `${SCOPE}/${name}`), 'node-pty'].map((name, i) => { + const filename = `${i}.tgz`; + const bytes = Buffer.from(name); + fs.writeFileSync(path.join(work, filename), bytes); + return { name, version: '1.2.0-beta.19', filename, integrity: 'sha512-' + crypto.createHash('sha512').update(bytes).digest('base64') }; + }); + const release = { version: '1.2.0-beta.19', packages }; + fs.writeFileSync(path.join(work, 'release.json'), JSON.stringify(release)); + assert.strictEqual(readRelease(work, 'beta').packages.length, 7); + assert.throws(() => readRelease(work, 'latest')); + publish(work, 'beta'); // Dry run must not contact npm. + const calls = []; + publish(work, 'beta', true, args => { + calls.push(args); + return args[0] === 'view' ? { status: 1, stderr: 'E404' } : { status: 0, stdout: 'published' }; + }); + assert.deepStrictEqual(calls.filter(args => args[0] === 'publish').map(args => path.basename(args[1])), packages.map(pkg => pkg.filename)); + let attempts = 0; + assert.throws(() => publish(work, 'beta', true, args => { + if (args[0] === 'view') { return { status: 1, stderr: 'E404' }; } + attempts++; + return { status: 1, stderr: 'registry unavailable' }; + }), /registry unavailable/); + assert.strictEqual(attempts, 1); + let views = 0; + publish(work, 'beta', true, args => { + assert.strictEqual(args[0], 'view'); + return { status: 0, stdout: JSON.stringify(packages[views++].integrity) }; + }); + assert.strictEqual(views, 7); + assert.throws(() => publish(work, 'beta', true, () => ({ status: 0, stdout: JSON.stringify('sha512-other') })), /Published bytes differ/); + fs.appendFileSync(path.join(work, '0.tgz'), 'corrupt'); + assert.throws(() => publish(work, 'beta', true), /Changed release artifact/); + release.packages.reverse(); + fs.writeFileSync(path.join(work, 'release.json'), JSON.stringify(release)); + assert.throws(() => readRelease(work, 'beta'), /publish order/); + }); + it('loads a real PTY with only the matching optional package and no install scripts', async () => { + const pkg = path.join(work, 'node-pty'); + fs.cpSync(path.resolve(__dirname, '../lib'), path.join(pkg, 'lib'), { recursive: true }); + fs.cpSync(path.resolve(__dirname, '../scripts'), path.join(pkg, 'scripts'), { recursive: true }); + const native = path.join(pkg, 'node_modules', SCOPE, target); + buildPlatform(process.env.NODE_PTY_TEST_SOURCE || path.resolve(__dirname, '../build/Release'), native, target, '1.2.0-beta.19'); + const result = cp.spawnSync(process.execPath, [path.join(pkg, 'scripts/prebuild.js')], { encoding: 'utf8' }); + assert.strictEqual(result.status, 0, result.stderr); + assert.match(result.stdout, /Using platform package/); + const forced = cp.spawnSync(process.execPath, [path.join(pkg, 'scripts/prebuild.js')], { + env: { ...process.env, npm_config_build_from_source: 'true' }, encoding: 'utf8' + }); + assert.strictEqual(forced.status, 1); + assert.ok(fs.existsSync(path.join(native, runtimeFiles(target)[0]))); + await child(` + const pty = require(${JSON.stringify(path.join(pkg, 'lib'))}); + const term = pty.spawn(process.platform === 'win32' ? 'cmd.exe' : '/bin/sh', + process.platform === 'win32' ? ['/c', 'echo optional-ok'] : ['-c', 'printf optional-ok'], + { env: process.env, cwd: process.cwd(), useConptyDll: true }); + let output = ''; + term.onData(data => output += data); + term.onExit(({ exitCode }) => { if (exitCode !== 0 || !output.includes('optional-ok')) process.exit(1); }); + setTimeout(() => process.exit(2), 10000).unref(); + `); + }); + it('falls back to source builds when optional dependencies are omitted', () => { + const pkg = path.join(work, 'missing'); + fs.cpSync(path.resolve(__dirname, '../scripts'), path.join(pkg, 'scripts'), { recursive: true }); + const result = cp.spawnSync(process.execPath, [path.join(pkg, 'scripts/prebuild.js')], { encoding: 'utf8' }); + assert.strictEqual(result.status, 1); + }); +}); diff --git a/test/pty-smoke.js b/test/pty-smoke.js new file mode 100644 index 000000000..957e2765a --- /dev/null +++ b/test/pty-smoke.js @@ -0,0 +1,19 @@ +// Exercise the public API of a packed/installed package, including companion files. +const assert = require('assert'); +const path = require('path'); +const root = path.resolve(process.argv[2]); +const start = process.hrtime.bigint(); +const pty = require(root); +const loaded = process.hrtime.bigint(); +const term = pty.spawn(process.platform === 'win32' ? 'cmd.exe' : '/bin/sh', + process.platform === 'win32' ? ['/c', 'echo packed-ok'] : ['-c', 'printf packed-ok'], + { cwd: process.cwd(), env: process.env, useConptyDll: true }); +let output = ''; +term.onData(data => output += data); +term.onExit(({ exitCode }) => { + assert.strictEqual(exitCode, 0); + assert.ok(output.includes('packed-ok'), output); + clearTimeout(timeout); + console.log(JSON.stringify({ requireMs: Number(loaded - start) / 1e6, smoke: 'passed' })); +}); +const timeout = setTimeout(() => { console.error('PTY smoke timed out'); process.exit(1); }, 10000);