Skip to content

Commit c3213d9

Browse files
committed
refactor: improve npm package code
- Update Node.js version from 20 to 22 in CI - Update pnpm version from 8 to 10 in CI - Replace node-fetch with built-in fetch API - Prefix all Node.js core modules with 'node:' protocol - Extract platform/arch checks into isPlatform() utility function - Use parseArgs() for CLI argument parsing - Extract localhost validation into isLocalhost() function - Remove node-fetch dependency from package.json
1 parent 3ae841f commit c3213d9

File tree

5 files changed

+39
-47
lines changed

5 files changed

+39
-47
lines changed

.github/npm/getBinary.js

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,24 @@
1-
import fetch from 'node-fetch';
2-
import { mkdirSync, chmodSync, existsSync, readFileSync } from 'fs';
3-
import { fileURLToPath } from 'url';
4-
import { dirname, join } from 'path';
5-
import { pipeline } from 'stream/promises';
1+
import { mkdirSync, chmodSync, existsSync, readFileSync } from 'node:fs';
2+
import { fileURLToPath } from 'node:url';
3+
import { dirname, join } from 'node:path';
4+
import { pipeline } from 'node:stream/promises';
65
import * as tar from 'tar';
76

87
const __filename = fileURLToPath(import.meta.url);
98
const __dirname = dirname(__filename);
109

11-
function getPlatform() {
12-
const type = process.platform;
13-
const arch = process.arch;
14-
15-
if (type === 'win32' && arch === 'x64') {
16-
return 'x86_64-pc-windows-msvc';
17-
}
18-
19-
if (type === 'linux' && arch === 'x64') {
20-
return 'x86_64-unknown-linux-musl';
21-
}
22-
23-
if (type === 'linux' && arch === 'arm64') {
24-
return 'aarch64-unknown-linux-musl';
25-
}
26-
27-
if (type === 'darwin' && arch === 'x64') {
28-
return 'x86_64-apple-darwin';
29-
}
10+
function isPlatform(platform, arch) {
11+
return process.platform === platform && process.arch === arch;
12+
}
3013

31-
if (type === 'darwin' && arch === 'arm64') {
32-
return 'aarch64-apple-darwin';
33-
}
14+
function getPlatform() {
15+
if (isPlatform('win32', 'x64')) return 'x86_64-pc-windows-msvc';
16+
if (isPlatform('linux', 'x64')) return 'x86_64-unknown-linux-musl';
17+
if (isPlatform('linux', 'arm64')) return 'aarch64-unknown-linux-musl';
18+
if (isPlatform('darwin', 'x64')) return 'x86_64-apple-darwin';
19+
if (isPlatform('darwin', 'arm64')) return 'aarch64-apple-darwin';
3420

35-
throw new Error(`Unsupported platform: ${type} ${arch}. Please create an issue at https://github.com/coralogix/protofetch/issues`);
21+
throw new Error(`Unsupported platform: ${process.platform} ${process.arch}. Please create an issue at https://github.com/coralogix/protofetch/issues`);
3622
}
3723

3824
function getVersion() {
@@ -63,8 +49,7 @@ async function downloadBinary(options = {}) {
6349
for (let attempt = 1; attempt <= 3; attempt++) {
6450
try {
6551
const response = await fetch(url, {
66-
redirect: 'follow',
67-
timeout: 60000
52+
redirect: 'follow'
6853
});
6954

7055
if (!response.ok) {

.github/npm/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
"postinstall": "node scripts.js install"
1414
},
1515
"dependencies": {
16-
"node-fetch": "^3.3.2",
1716
"tar": "^7.4.3"
1817
},
1918
"keywords": [

.github/npm/run.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env node
2-
import { spawn } from 'child_process';
3-
import path from 'path';
4-
import { fileURLToPath } from 'url';
2+
import { spawn } from 'node:child_process';
3+
import path from 'node:path';
4+
import { fileURLToPath } from 'node:url';
55

66
const __filename = fileURLToPath(import.meta.url);
77
const __dirname = path.dirname(__filename);

.github/npm/scripts.js

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,30 @@
1+
import { parseArgs } from 'node:util';
12
import { downloadBinary } from './getBinary.js';
23

4+
function isLocalhost(hostname) {
5+
return hostname === 'localhost' || hostname === '127.0.0.1';
6+
}
7+
38
if (process.argv.includes('install')) {
4-
// Check for --url argument for testing (only localhost allowed for security)
5-
const urlArg = process.argv.find(arg => arg.startsWith('--url='));
9+
const { values } = parseArgs({
10+
options: {
11+
url: {
12+
type: 'string'
13+
}
14+
},
15+
strict: false
16+
});
17+
618
let url = null;
719

8-
if (urlArg) {
9-
const providedUrl = urlArg.split('=')[1];
20+
if (values.url) {
1021
try {
11-
const parsedUrl = new URL(providedUrl);
12-
// Only allow localhost URLs for testing
13-
if (parsedUrl.hostname === 'localhost' || parsedUrl.hostname === '127.0.0.1') {
14-
url = providedUrl;
15-
} else {
22+
const parsedUrl = new URL(values.url);
23+
if (!isLocalhost(parsedUrl.hostname)) {
1624
console.error('Error: --url parameter only allows localhost URLs for security reasons');
1725
process.exit(1);
1826
}
27+
url = values.url;
1928
} catch (error) {
2029
console.error('Error: Invalid URL provided to --url parameter');
2130
process.exit(1);

.github/workflows/ci.yml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,12 @@ jobs:
179179
- name: Setup Node.js
180180
uses: actions/setup-node@v4
181181
with:
182-
node-version: '20'
182+
node-version: '22'
183183

184184
- name: Install pnpm
185185
uses: pnpm/action-setup@v4
186186
with:
187-
version: 8
187+
version: 10
188188

189189
- name: Download artifacts
190190
uses: actions/download-artifact@v4
@@ -204,8 +204,7 @@ jobs:
204204
shell: bash
205205
run: |
206206
cd artifacts
207-
# Start HTTP server in background
208-
python3 -m http.server 8000 &
207+
python -m http.server 8000 &
209208
echo $! > /tmp/http_server.pid
210209
sleep 2
211210
echo "HTTP server started on port 8000"

0 commit comments

Comments
 (0)