better code
Massimo Melina committed
Mar 5, 2026 at 00:25 UTC
274f34b316d29ccdd7634e407d95e46dce3defc6
5 files changed
+8
-12
src/ThrottledStream.ts
+1
-1
@@ -74,7 +74,7 @@ export class ThrottleGroup {
74
75
updateLimit(kBs: number) {
76
if (kBs < 0)
77
- throw new Error('invalid bytesPerSecond')
77
+ throw Error('invalid bytesPerSecond')
78
kBs *= 1000
79
return this.bucket = new TokenBucket({
80
bucketSize: kBs,
src/stat.ts
+1
-1
@@ -15,7 +15,7 @@ export function getStatWorker(key: string) {
15
const requests = new Map<string, PendingPromise<Stats>>()
16
worker.on('message', (msg: any) => { // request finished, good or bad
17
const k = msg.path
18
- requests.get(k)?.resolve(msg.error ? Promise.reject(new Error(msg.error))
18
+ requests.get(k)?.resolve(msg.error ? Promise.reject(Error(msg.error))
19
: Object.setPrototypeOf(msg.result, Stats.prototype) )
20
requests.delete(k)
21
})
src/update.ts
+2
-6
@@ -171,7 +171,7 @@ export async function update(tagOrUrl: string='') {
171
catch {}
172
renameSync(bin, oldBin)
173
console.log("launching new version in background", newBinFile)
174
- launch(newBin, ['--updating', binFile, '--cwd .'], { sync: true }) // sync necessary to work on Mac by double-click
174
+ spawnSync(cmdEscape(newBin), ['--updating', binFile, '--cwd .'], { shell: true, stdio: [0,1,2] }) // sync necessary to work on Mac by double-click
175
})
176
console.log("quitting")
177
setTimeout(() => process.exit()) // give time to return (and caller to complete, eg: rest api to reply)
@@ -182,10 +182,6 @@ export async function update(tagOrUrl: string='') {
182
}
183
}
184
185
-function launch(cmd: string, pars: string[]=[], options?: { sync: boolean } & Parameters<typeof spawn>[2]) {
186
- return (options?.sync ? spawnSync : spawn)(cmdEscape(cmd), pars, { detached: true, shell: true, stdio: [0,1,2], ...options })
187
-}
188
-
185
if (argv.updating) { // we were launched with a temporary name, restore original name to avoid breaking references
186
const bin = process.execPath
187
const dest = join(dirname(bin), argv.updating)
@@ -195,7 +191,7 @@ if (argv.updating) { // we were launched with a temporary name, restore original
191
// if you change anything, be sure to test launching both double-clicking and in a terminal
192
if (IS_WINDOWS) // windows-only; this method on Mac works only once, and without the console
193
onProcessExit(() =>
198
- launch(dest, ['--updated', '--cwd .']) ) // launch+sync here would cause the old process to stay open, locking ports
194
+ spawn(cmdEscape(dest), ['--updated', '--cwd .'], { detached: true, shell: true, stdio: [0,1,2] }) ) // launch+sync here would cause the old process to stay open, locking ports
195
else if (process.stdin.isTTY && process.stdout.isTTY) // keep interactive terminal users attached to the restarted process
196
spawnSync(dest, ['--updated', '--cwd', process.cwd()], { stdio: [0, 1, 2] })
197
else
src/util-http.ts
+3
-3
@@ -82,15 +82,15 @@ export function httpStream(url: string, { body, proxy, jar, noRedirect, httpThro
82
else delete hostJar[k]
83
}
84
if (!res.statusCode || httpThrow && res.statusCode >= 400)
85
- return reject(new Error(String(res.statusCode), { cause: res }))
85
+ return reject(Error(String(res.statusCode), { cause: res }))
86
let r = res.headers.location
87
if (r && !noRedirect) {
88
const dest = new URL(r, url) // rewrite in case r is just a path, and thus relative to the current url
89
r = dest.toString()
90
const src = new URL(url)
91
const sameOrigin = src.protocol === dest.protocol && src.host === dest.host
92
- return redirected.includes(r) ? reject(new Error('endless http redirection'))
93
- : redirected.length > 20 ? reject(new Error('excessive http redirection'))
92
+ return redirected.includes(r) ? reject(Error('endless http redirection'))
93
+ : redirected.length > 20 ? reject(Error('excessive http redirection'))
94
: resolve(httpStream(r, {
95
httpThrow, jar, proxy,
96
..._.pick(options, ['agent', 'rejectUnauthorized', 'timeout']),
tests/test.ts
+1
-1
@@ -906,7 +906,7 @@ async function readEventStreamOnce(url: string, { baseUrl, ...requestOptions }:
906
const data = await new Promise<string>((resolve, reject) => {
907
const timer = setTimeout(() => {
908
res.destroy()
909
- reject(new Error('event stream timeout'))
909
+ reject(Error('event stream timeout'))
910
}, 2000)
911
res.once('data', chunk => {
912
clearTimeout(timer)