Skip to content
This repository has been archived by the owner on Jan 3, 2024. It is now read-only.

Add support for source tarball builds #212

Closed
wants to merge 1 commit into from
Closed
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
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ A utility to fetch or build patched Node binaries used by [pkg](https://github.c

<em id="fn3">[3]</em>: [mandatory code signing](https://developer.apple.com/documentation/macos-release-notes/macos-big-sur-11_0_1-universal-apps-release-notes) is enforced by Apple.

## Building Binaries

In `--force-build` mode the source is cloned using `git` from [Node.js canonical repository][1]. Eventually, in e.g. network restricted environments it can be provided directly by setting environment variable `PKG_SOURCE_TAR` value to a path to [Node.js source tarball][2]. In such case tarball version must match desired Node.js range otherwise patching will fail. See all available [patches](patches/).

Build directory can be changed by `PKG_BUILD_PATH` environment variable. Built (and fetched) binaries will be persisted in between the builds in `.pkg-cache/` in user's home directory if not overriden by value of `PKG_CACHE_PATH` environment variable.

[1]: https://github.com/nodejs/node
[2]: https://nodejs.org/en/download/releases/

## Security

We do not expect this project to have vulnerabilities of its own. Nonetheless, as this project distributes prebuilt Node.js binaries,
Expand Down
34 changes: 32 additions & 2 deletions lib/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import { hostArch, hostPlatform } from './system';
import { log } from './log';
import patchesJson from '../patches/patches.json';

// Optional PKG_SOURCE_TAR pointing to official Node.js source tarball to avoid
// cloning in e.g. network restricted environments
const tarPath = (process.env.PKG_SOURCE_TAR ?
path.resolve(process.env.PKG_SOURCE_TAR) : '')
const buildPath = path.resolve(
process.env.PKG_BUILD_PATH ||
path.join(os.tmpdir(), `pkg.${crypto.randomBytes(12).toString('hex')}`)
Expand Down Expand Up @@ -60,6 +64,26 @@ function getConfigureArgs(major: number, targetPlatform: string): string[] {
return args;
}

async function tarExtract() {
log.info(`Extracting Node.js source tarball ${tarPath}`)

await fs.mkdirp(nodePath);

// This expects official Node.js tarballs that have node-vX.X.X/ top level
// directory. -C and --strip-components 1 are well adopted and work in GNU and
// on MacOS
const args = [
'-xf',
tarPath,
'-C',
nodePath,
'--strip-components',
'1',
]

await spawn('tar', args, { stdio: 'inherit' });
}

async function gitClone(nodeVersion: string) {
log.info('Cloning Node.js repository from GitHub...');

Expand Down Expand Up @@ -254,8 +278,14 @@ export default async function build(
await fs.remove(buildPath);
await fs.mkdirp(buildPath);

await gitClone(nodeVersion);
await gitResetHard(nodeVersion);
// If tarPath is not set clone Node.js using git; otherwise just extract
// source tarball.
if (tarPath === '') {
await gitClone(nodeVersion);
await gitResetHard(nodeVersion);
} else {
await tarExtract();
}
await applyPatches(nodeVersion);

const output = await compile(nodeVersion, targetArch, targetPlatform);
Expand Down
5 changes: 4 additions & 1 deletion lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,11 @@ export async function spawn(
args?: ReadonlyArray<string>,
options?: SpawnSyncOptions
): Promise<void> {
const { error } = spawnSync(command, args, options);
const { status, error } = spawnSync(command, args, options);
if (error) {
throw error;
}
if (status !== 0) {
throw new Error(`non-zero exit code (${status}) from ${command}`)
}
}