Skip to content
Open
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
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,5 @@ jobs:
with:
node-version: "20"
- run: OPTIQOR_SKIP_POSTINSTALL=1 npm install --ignore-scripts
- run: npm run test:postinstall
- run: npm pack
5 changes: 2 additions & 3 deletions npm/postinstall.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,8 @@ const pipelineP = promisify(pipeline);
console.log('optiqor: ready. Run `optiqor --version` to verify.');
} catch (err) {
console.error('optiqor: failed to install binary:', err.message);
console.error('optiqor: this is non-fatal — build from source if needed:');
console.error('optiqor: build from source if needed:');
console.error(' go install github.com/optiqor/optiqor-cli/cmd/optiqor@latest');
// Exit 0 so npm install does not abort entirely.
process.exit(0);
process.exit(1);
}
})();
58 changes: 58 additions & 0 deletions npm/postinstall.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/usr/bin/env node

const assert = require('assert/strict');
const fs = require('fs');
const path = require('path');
const { spawnSync } = require('child_process');

const root = path.join(__dirname, '..');
const vendorDir = path.join(root, 'vendor');

const runPostinstallWithHttpFailure = () => {
const script = `
const https = require('https');
const { Readable } = require('stream');

https.get = (url, callback) => {
const response = new Readable({
read() {
this.push(null);
},
});
response.statusCode = 404;
response.headers = {};
process.nextTick(() => callback(response));

return {
on() {
return this;
},
};
};

Object.defineProperty(process, 'platform', { value: 'linux' });
Object.defineProperty(process, 'arch', { value: 'x64' });
require('./npm/postinstall.js');
`;

return spawnSync(process.execPath, ['-e', script], {
cwd: root,
encoding: 'utf8',
});
};

fs.rmSync(vendorDir, { recursive: true, force: true });

try {
const result = runPostinstallWithHttpFailure();

assert.equal(
result.status,
1,
`expected failed binary download to exit 1\nstdout:\n${result.stdout}\nstderr:\n${result.stderr}`
);
assert.match(result.stderr, /failed to install binary: HTTP 404/);
assert.doesNotMatch(result.stderr, /non-fatal/);
} finally {
fs.rmSync(vendorDir, { recursive: true, force: true });
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
},
"scripts": {
"postinstall": "node ./npm/postinstall.js",
"test:postinstall": "node ./npm/postinstall.test.js",
"test": "node -e \"require('./npm/index.js')\""
},
"files": [
Expand Down
Loading