Skip to content

Commit ae5bdf9

Browse files
fix: simplify streaming of server writes (#137)
1 parent 3fdde97 commit ae5bdf9

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

Diff for: src/server.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import http from 'node:http'
44
import { tmpdir } from 'node:os'
55
import { dirname, join, relative, resolve, sep } from 'node:path'
66
import { platform } from 'node:process'
7+
import stream from 'node:stream'
8+
import { promisify } from 'node:util'
79

810
import { ListResponse } from './backend/list.ts'
911
import { decodeMetadata, encodeMetadata, METADATA_HEADER_INTERNAL } from './metadata.ts'
@@ -21,6 +23,10 @@ export enum Operation {
2123
SET = 'set',
2224
}
2325

26+
// TODO: Replace with `promises` import of `node:stream` once we can drop
27+
// support for Node 14.
28+
const pipeline = promisify(stream.pipeline)
29+
2430
interface BlobsServerOptions {
2531
/**
2632
* Whether debug-level information should be logged, such as internal errors
@@ -271,12 +277,7 @@ export class BlobsServer {
271277
const tempDataPath = join(tempDirectory, relativeDataPath)
272278

273279
await fs.mkdir(dirname(tempDataPath), { recursive: true })
274-
275-
await new Promise((resolve, reject) => {
276-
req.pipe(createWriteStream(tempDataPath))
277-
req.on('end', resolve)
278-
req.on('error', reject)
279-
})
280+
await pipeline(req, createWriteStream(tempDataPath))
280281

281282
await fs.mkdir(dirname(dataPath), { recursive: true })
282283
await fs.copyFile(tempDataPath, dataPath)

0 commit comments

Comments
 (0)