Skip to content

Commit

Permalink
Use Transform for size calculation
Browse files Browse the repository at this point in the history
This prevents a race condition
  • Loading branch information
garrettjstevens committed Jul 2, 2024
1 parent 622f63f commit 35f2635
Showing 1 changed file with 39 additions and 6 deletions.
45 changes: 39 additions & 6 deletions packages/apollo-cli/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ import EventEmitter from 'node:events'
import * as fs from 'node:fs'
import * as os from 'node:os'
import * as path from 'node:path'
import {
Transform,
TransformCallback,
TransformOptions,
pipeline,
} from 'node:stream'

import { SingleBar } from 'cli-progress'
import { Agent, RequestInit, Response, fetch } from 'undici'
Expand Down Expand Up @@ -424,6 +430,31 @@ export async function submitAssembly(
return response
}

interface ProgressTransformOptions extends TransformOptions {
progressBar: SingleBar
}

class ProgressTransform extends Transform {
private size = 0

private progressBar: SingleBar

constructor(opts: ProgressTransformOptions) {
super(opts)
this.progressBar = opts.progressBar
}

_transform(
chunk: any,

Check failure on line 448 in packages/apollo-cli/src/utils.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
_encoding: BufferEncoding,
callback: TransformCallback,
): void {
this.size += chunk.length
this.progressBar.update(this.size)
callback(null, chunk)
}
}

export async function uploadFile(
address: string,
accessToken: string,
Expand All @@ -434,15 +465,17 @@ export async function uploadFile(
const { size } = await filehandle.stat()
const stream = filehandle.createReadStream()
const progressBar = new SingleBar({ etaBuffer: 100_000_000 })
let sizeProcesed = 0
stream.on('data', (chunk) => {
sizeProcesed += chunk.length
progressBar.update(sizeProcesed)
return chunk
const progressTransform = new ProgressTransform({ progressBar })
const body = pipeline(stream, progressTransform, (err) => {
if (err) {
progressBar.stop()
console.error('Error processing file.', err)
throw Error()

Check failure on line 473 in packages/apollo-cli/src/utils.ts

View workflow job for this annotation

GitHub Actions / Lint

Pass a message to the `Error` constructor

Check failure on line 473 in packages/apollo-cli/src/utils.ts

View workflow job for this annotation

GitHub Actions / Lint

Use `new` when throwing an error

Check failure on line 473 in packages/apollo-cli/src/utils.ts

View workflow job for this annotation

GitHub Actions / Lint

Use `new Error()` instead of `Error()`
}
})
const init: RequestInit = {
method: 'POST',
body: stream,
body,
duplex: 'half',
headers: {
Authorization: `Bearer ${accessToken}`,
Expand Down

0 comments on commit 35f2635

Please sign in to comment.