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
32 changes: 27 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,35 @@ defaults:
jobs:
build:
runs-on: "ubuntu-latest"
strategy:
matrix:
node: ["22.22.2", "22.x", "24.15.0", "24.x", "26.0.0", "26.x"]
steps:
- uses: "actions/checkout@v3"
- uses: "actions/checkout@v6"

- uses: "actions/setup-node@v3"
- uses: "actions/setup-node@v6"
with:
node-version: "14.x"
node-version: "${{ matrix.node }}"

- run: npm install --ignore-scripts --no-fund
# Generate an ignored lockfile for npm audit, resolving ranges afresh.
- run: npm install --engine-strict --no-fund --package-lock

- run: npm test -- --no-check-coverage --timeout=60
- run: npm audit

- run: npm test

- name: Verify the published dependency tree and CLI
run: |
tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT
npm pack --pack-destination "$tmp"
npm install --prefix "$tmp/consumer" --engine-strict --no-fund --package-lock "$tmp"/*.tgz
npm audit --prefix "$tmp/consumer"
npm ls --prefix "$tmp/consumer" --all
for attempt in 1 2; do
(
cd "$tmp/consumer"
XDG_CACHE_HOME="$tmp/cache" node node_modules/purescript-installer/index.js --purs-ver 0.15.7
test "$(./purs --version)" = 0.15.7
)
done
6 changes: 6 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
node_modules/
.nyc_output/
.test-cache/
coverage/
purs
package-lock.json
5 changes: 2 additions & 3 deletions build-purescript/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
'use strict';

const {basename, join, resolve} = require('path');
const {mkdtemp, stat} = require('fs');
const {mkdtemp, stat, rm} = require('fs');
const osTmpdir = require('os').tmpdir;
const util = require('util');

const feint = require('../feint/index.js');
const isPlainObj = require('is-plain-obj');
const Observable = require('zen-observable');
const once = require('once');
const {rimraf} = require('rimraf');
const spawnStack = require('../spawn-stack/index.js');

const downloadPurescriptSource = require('../download-purescript-source/index.js');
Expand Down Expand Up @@ -102,7 +101,7 @@ module.exports = function buildPurescript(...args) {
}

const spawnOptions = {cwd: null, ...options};
const cleanupSourceDir = (cb = () => {}) => spawnOptions.cwd ? rimraf(spawnOptions.cwd).then(() => cb(), () => cb()) : cb();
const cleanupSourceDir = (cb = () => {}) => spawnOptions.cwd ? rm(spawnOptions.cwd, {recursive: true, force: true}, () => cb()) : cb();

const sendError = once((err, id) => {
if (id) {
Expand Down
13 changes: 9 additions & 4 deletions dl-tar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

const {inspect} = require('util');
const {resolve} = require('path');
const {Transform} = require('stream');
const {Readable, Transform} = require('stream');
const {promises: fs} = require('fs');

const cancelablePump = require('../cancelable-pump/index.js');
const {Unpack} = require('tar');
const isPlainObj = require('is-plain-obj');
const fetch = require('make-fetch-happen');
const {fetch, EnvHttpProxyAgent, RetryAgent} = require('undici');
const Observable = require('zen-observable');

class InternalUnpack extends Unpack {
Expand Down Expand Up @@ -167,6 +167,8 @@ module.exports = function dlTar(...args) {
const absoluteDest = resolve(cwd, dest);
let ended = false;
let cancel;
const controller = new AbortController();
const dispatcher = new RetryAgent(new EnvHttpProxyAgent());

(async () => {
try {
Expand All @@ -187,9 +189,10 @@ module.exports = function dlTar(...args) {
const {baseUrl, headers} = options;
const {href} = new URL(url, baseUrl);

const res = await fetch(href, {headers}).then(response => {
const res = await fetch(href, {headers, dispatcher, signal: controller.signal}).then(async response => {

if (response.ok !== true) {
await response.body.cancel();
throw new Error(`${response.status} ${response.statusText}`);
}

Expand All @@ -201,7 +204,7 @@ module.exports = function dlTar(...args) {
});

const pipe = [
res.body,
Readable.fromWeb(res.body),
new Transform({
transform(chunk, encoding, cb) {
unpackStream.responseBytes += chunk.length;
Expand All @@ -228,6 +231,8 @@ module.exports = function dlTar(...args) {
})();

return function cancelExtract() {
controller.abort();
dispatcher.destroy().catch(() => {});
if (!cancel) {
ended = true;
return;
Expand Down
14 changes: 5 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,38 +1,34 @@
{
"name": "purescript-installer",
"version": "0.3.6",
"version": "0.4.0",
"description": "Installs the PureScript compiler",
"bin": {
"install-purescript": "index.js"
},
"engines": {
"node": "^20.17.0 || >=22.9.0"
"node": "^22.22.2 || ^24.15.0 || >=26.0.0"
},
"dependencies": {
"byline": "^5.0.0",
"cacache": "^20.0.0",
"cacache": "^21.0.2",
"chalk": "^2.4.2",
"env-paths": "^2.2.0",
"execa": "^2.0.3",
"filesize": "^4.1.2",
"is-plain-obj": "^2.0.0",
"log-symbols": "^3.0.0",
"log-update": "^4.0.0",
"make-fetch-happen": "^15.0.6",
"minimist": "^1.2.0",
"ms": "^2.1.2",
"once": "^1.4.0",
"rimraf": "^6.0.0",
"semver": "^7.3.7",
"tar": "^7.5.21",
"undici": "^8.12.4",
"which": "^1.3.1",
"zen-observable": "^0.8.14"
},
"devDependencies": {
"tap": "^21.7.4"
},
"scripts": {
"test": "tap"
"test": "node --test --test-timeout=60000"
},
"repository": {
"type": "git",
Expand Down
145 changes: 145 additions & 0 deletions test/download.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
'use strict';

const {test, before, after} = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs/promises');
const http = require('node:http');
const {once} = require('node:events');
const {join} = require('node:path');
const {tmpdir} = require('node:os');
const {execFile} = require('node:child_process');
const {promisify} = require('node:util');
const tar = require('tar');
const download = require('../dl-tar');

let directory;
let archive;
let server;
let baseUrl;
let attempts = 0;

before(async () => {
directory = await fs.mkdtemp(join(tmpdir(), 'installer-download-'));
await fs.mkdir(join(directory, 'package'));
await fs.writeFile(join(directory, 'package', 'payload'), 'archive payload');
await tar.create({cwd: directory, file: join(directory, 'archive.tgz'), gzip: true}, ['package']);
archive = await fs.readFile(join(directory, 'archive.tgz'));
server = http.createServer((request, response) => {
if (request.url === '/redirect') {
response.writeHead(302, {location: '/archive'}).end();
} else if (request.url === '/retry' && ++attempts === 1) {
response.writeHead(503).end();
} else if (request.url === '/missing') {
response.writeHead(404).end();
} else if (request.url === '/invalid') {
response.end('not an archive');
} else if (request.url === '/pending') {
// Wait for cancellation before sending any headers.
} else if (request.url === '/streaming') {
response.writeHead(200);
response.write(archive.subarray(0, -8));
} else {
response.writeHead(200, {'x-received-header': request.headers['x-test'] || ''});
response.end(archive);
}
});
server.listen(0, '127.0.0.1');
await once(server, 'listening');
baseUrl = `http://127.0.0.1:${server.address().port}`;
});

after(async () => {
server.closeAllConnections();
await new Promise(resolve => server.close(resolve));
await fs.rm(directory, {recursive: true, force: true});
});

function extract(url, destination, options) {
return new Promise((resolve, reject) => {
const progress = [];
download(url, destination, options).subscribe({
next: event => progress.push(event),
error: reject,
complete: () => resolve(progress)
});
});
}

test('redirects, request headers, extraction and progress', async () => {
const destination = join(directory, 'redirect-result');
const progress = await extract('/redirect', destination, {baseUrl, headers: {'x-test': 'preserved'}});
assert.equal(await fs.readFile(join(destination, 'payload'), 'utf8'), 'archive payload');
assert.ok(progress.length > 0);
const last = progress.at(-1).response;
assert.equal(last.url, `${baseUrl}/archive`);
assert.equal(last.headers.get('x-received-header'), 'preserved');
assert.equal(last.bytes, archive.length);
});

test('retries transient HTTP errors', async () => {
const destination = join(directory, 'retry-result');
await extract(`${baseUrl}/retry`, destination);
assert.equal(attempts, 2);
assert.equal(await fs.readFile(join(destination, 'payload'), 'utf8'), 'archive payload');
});

test('reports HTTP and extraction failures', async () => {
await assert.rejects(extract(`${baseUrl}/missing`, join(directory, 'missing-result')), /404 Not Found/);
await assert.rejects(extract(`${baseUrl}/invalid`, join(directory, 'invalid-result')), /Unrecognized archive format/);
});

for (const state of ['pending', 'streaming']) {
test(`unsubscribe closes a ${state} request`, async () => {
const received = once(server, 'request');
let notifications = 0;
let progressed;
const progress = new Promise(resolve => { progressed = resolve; });
const subscription = download(`${baseUrl}/${state}`, join(directory, state)).subscribe({
next: () => { notifications++; progressed(); },
error: () => notifications++,
complete: () => notifications++
});
const [request] = await received;
if (state === 'streaming') await progress;
const beforeCancel = notifications;
const closed = new Promise(resolve => request.socket.once('close', resolve));
subscription.unsubscribe();
await closed;
assert.equal(notifications, beforeCancel);
});
}

test('honors HTTP_PROXY and NO_PROXY', async t => {
let connections = 0;
const proxy = http.createServer((request, response) => {
connections++;
const upstream = http.request(request.url, {headers: request.headers}, incoming => {
response.writeHead(incoming.statusCode, incoming.headers);
incoming.pipe(response);
});
upstream.on('error', error => response.destroy(error));
request.pipe(upstream);
});
proxy.listen(0, '127.0.0.1');
await once(proxy, 'listening');
t.after(async () => {
proxy.closeAllConnections();
await new Promise(resolve => proxy.close(resolve));
});
const proxyUrl = `http://127.0.0.1:${proxy.address().port}`;
for (const bypass of ['', '127.0.0.1']) {
const destination = join(directory, bypass ? 'bypassed' : 'proxied');
const {stderr} = await promisify(execFile)(process.execPath, ['-e', `
const download = require(${JSON.stringify(require.resolve('../dl-tar'))});
download(process.argv[1], process.argv[2]).subscribe({
error(error) { console.error(error); process.exitCode = 1; }
});
`, `${baseUrl}/archive`, destination], {
timeout: 10000,
env: {...process.env, HTTP_PROXY: proxyUrl, http_proxy: proxyUrl, NO_PROXY: bypass, no_proxy: bypass}
});
assert.equal(stderr, '');
assert.equal(await fs.readFile(join(destination, 'payload'), 'utf8'), 'archive payload');
assert.equal(connections, 1, 'proxy used once, then bypassed');
}
});
Loading
Loading