Skip to content

Commit

Permalink
fix: simplify streaming of server writes (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardoboucas authored Jan 15, 2024
1 parent 3fdde97 commit ae5bdf9
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import http from 'node:http'
import { tmpdir } from 'node:os'
import { dirname, join, relative, resolve, sep } from 'node:path'
import { platform } from 'node:process'
import stream from 'node:stream'
import { promisify } from 'node:util'

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

// TODO: Replace with `promises` import of `node:stream` once we can drop
// support for Node 14.
const pipeline = promisify(stream.pipeline)

interface BlobsServerOptions {
/**
* Whether debug-level information should be logged, such as internal errors
Expand Down Expand Up @@ -271,12 +277,7 @@ export class BlobsServer {
const tempDataPath = join(tempDirectory, relativeDataPath)

await fs.mkdir(dirname(tempDataPath), { recursive: true })

await new Promise((resolve, reject) => {
req.pipe(createWriteStream(tempDataPath))
req.on('end', resolve)
req.on('error', reject)
})
await pipeline(req, createWriteStream(tempDataPath))

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

0 comments on commit ae5bdf9

Please sign in to comment.