Skip to content

fix: simplify streaming of server writes #137

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 15, 2024
Merged
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
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