Skip to content

Commit

Permalink
fix(MediaManager): use event handlers on writable instead
Browse files Browse the repository at this point in the history
- pipeline() does not resolve when input readable is not finished yet
- however, the transform is designed to finish early (such as when only first 5s video is needed), in this case pipeline() would never resolve
- we switch to only look at the last stream (upload to GCS)
  • Loading branch information
MrOrz committed Feb 28, 2024
1 parent feeb01c commit bfee77b
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions src/MediaManager.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import fetch from 'node-fetch';
import { Bucket, Storage, File } from '@google-cloud/storage';
import { pipeline } from 'stream/promises';
import prepareStream from './lib/prepareStream';
import { defaultGetVariantSettings, DEFAULT_ORIGINAL_VARIANT_NAME } from './lib/variants';
import { getFileIDHash, getImageSearchHashes, base64urlHammingDist } from './lib/hashes';
Expand Down Expand Up @@ -161,14 +160,17 @@ class MediaManager {
// Must not await this promise because we still need to pipe body to another writable stream
// for hash calculation below
const uploadPromise = Promise.all(
variantSettings.map(async ({ transform, contentType }, i) =>
pipeline(
// Initiate separate HTTP stream here, as each variant may consume the stream differently,
// or even ends before reading the whole file.
(await fetch(url)).body,
transform,
tempVariantFiles[i].createWriteStream({ contentType, gzip: 'auto' })
)
variantSettings.map(
async ({ transform, contentType }, i) =>
new Promise(async (resolve, reject) => {
// Initiate separate HTTP stream here, as each variant may consume the stream differently,
// or even ends before reading the whole file.
(await fetch(url)).body
.pipe(transform)
.pipe(tempVariantFiles[i].createWriteStream({ contentType, gzip: 'auto' }))
.on('finish', resolve)
.on('error', reject);
})
)
);

Expand Down

0 comments on commit bfee77b

Please sign in to comment.