Skip to content
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

feat(stdlib): remove pipeline from streamToBuffer #687

Merged
merged 1 commit into from
Feb 14, 2021
Merged
Show file tree
Hide file tree
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
26 changes: 16 additions & 10 deletions packages/stdlib/src/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ import { exec as cpExec, spawn as cpSpawn } from "child_process";
import { lstatSync, readdirSync } from "fs";
import { lstat, readdir } from "fs/promises";
import { posix } from "path";
import { pipeline } from "stream";
import { promisify } from "util";

const internalExec = promisify(cpExec);
const internalPipeline = promisify(pipeline);

/**
* Join all arguments together and normalize the resulting path. Arguments must be
Expand Down Expand Up @@ -77,19 +75,27 @@ export function spawn(command, args, opts = {}) {
*
* @since 0.1.0
*
* @param {ReadableStream} stream
* @param {NodeJS.ReadableStream} stream
* @returns {Promise<Buffer>}
*/
export async function streamToBuffer(stream) {
const buffers = [];
await internalPipeline(stream, async function* (transform) {
for await (const chunk of transform) {
if (!stream || typeof stream._read !== "function") {
return Buffer.from([]);
}

return new Promise((resolve, reject) => {
const buffers = [];

stream.on("data", function (chunk) {
buffers.push(chunk);
yield chunk;
}
});
stream.on("end", function () {
resolve(Buffer.concat(buffers));
});
stream.on("error", function (err) {
reject(err);
});
});

return Buffer.concat(buffers);
}

/**
Expand Down
18 changes: 5 additions & 13 deletions packages/store/src/file-cache.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { once } from "events";
import { mkdirSync, createReadStream, createWriteStream } from "fs";
import { createReadStream, createWriteStream, mkdirSync } from "fs";
import { pipeline as pipelineCallbacks, Readable } from "stream";
import { promisify } from "util";
import { isNil, pathJoin, uuid } from "@compas/stdlib";
import { isNil, pathJoin, streamToBuffer, uuid } from "@compas/stdlib";
import { getFileStream } from "./files.js";

const pipeline = promisify(pipelineCallbacks);
Expand Down Expand Up @@ -126,18 +126,10 @@ export class FileCache {
* @param end
*/
async cacheFileInMemory(key, id, start, end) {
const buffers = [];
await pipeline(
await getFileStream(this.minio, this.bucketName, id),
async function* (transform) {
for await (const chunk of transform) {
buffers.push(chunk);
yield chunk;
}
},
);
const stream = await getFileStream(this.minio, this.bucketName, id);
const buffer = await streamToBuffer(stream);

this.memoryCache.set(key, Buffer.concat(buffers));
this.memoryCache.set(key, buffer);

return {
stream: Readable.from(this.memoryCache.get(key).slice(start, end)),
Expand Down