fix(core): Add a helper function to convert binary streams to buffers (no-changelog) (#5641)

This commit is contained in:
agobrech 2023-03-08 14:32:27 +01:00 committed by GitHub
parent c81656d149
commit 0d49ad8b93
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 7 deletions

View file

@ -1,4 +1,3 @@
import concatStream from 'concat-stream';
import { readFile, stat } from 'fs/promises';
import type { BinaryMetadata, IBinaryData, INodeExecutionData } from 'n8n-workflow';
import prettyBytes from 'pretty-bytes';
@ -6,6 +5,7 @@ import type { Readable } from 'stream';
import { BINARY_ENCODING } from '../Constants';
import type { IBinaryDataConfig, IBinaryDataManager } from '../Interfaces';
import { BinaryDataFileSystem } from './FileSystem';
import { binaryToBuffer } from './utils';
export class BinaryDataManager {
static instance: BinaryDataManager | undefined;
@ -104,11 +104,7 @@ export class BinaryDataManager {
fileSize,
});
} else {
// Else fallback to storing this data in memory.
const buffer = await new Promise<Buffer>((resolve) => {
if (Buffer.isBuffer(input)) resolve(input);
else input.pipe(concatStream(resolve));
});
const buffer = await binaryToBuffer(input);
binaryData.data = buffer.toString(BINARY_ENCODING);
binaryData.fileSize = prettyBytes(buffer.length);
}

View file

@ -0,0 +1,8 @@
import concatStream from 'concat-stream';
import type { Readable } from 'stream';
export const binaryToBuffer = async (body: Buffer | Readable) =>
new Promise<Buffer>((resolve) => {
if (Buffer.isBuffer(body)) resolve(body);
else body.pipe(concatStream(resolve));
});

View file

@ -149,7 +149,6 @@ const createFormDataObject = (data: Record<string, unknown>) => {
});
return formData;
};
function searchForHeader(headers: IDataObject, headerName: string) {
if (headers === undefined) {
return undefined;