mirror of
https://github.com/n8n-io/n8n.git
synced 2025-01-11 21:07:28 -08:00
fix(core): Add a helper function to convert binary streams to buffers (no-changelog) (#5641)
This commit is contained in:
parent
c81656d149
commit
0d49ad8b93
|
@ -1,4 +1,3 @@
|
||||||
import concatStream from 'concat-stream';
|
|
||||||
import { readFile, stat } from 'fs/promises';
|
import { readFile, stat } from 'fs/promises';
|
||||||
import type { BinaryMetadata, IBinaryData, INodeExecutionData } from 'n8n-workflow';
|
import type { BinaryMetadata, IBinaryData, INodeExecutionData } from 'n8n-workflow';
|
||||||
import prettyBytes from 'pretty-bytes';
|
import prettyBytes from 'pretty-bytes';
|
||||||
|
@ -6,6 +5,7 @@ import type { Readable } from 'stream';
|
||||||
import { BINARY_ENCODING } from '../Constants';
|
import { BINARY_ENCODING } from '../Constants';
|
||||||
import type { IBinaryDataConfig, IBinaryDataManager } from '../Interfaces';
|
import type { IBinaryDataConfig, IBinaryDataManager } from '../Interfaces';
|
||||||
import { BinaryDataFileSystem } from './FileSystem';
|
import { BinaryDataFileSystem } from './FileSystem';
|
||||||
|
import { binaryToBuffer } from './utils';
|
||||||
|
|
||||||
export class BinaryDataManager {
|
export class BinaryDataManager {
|
||||||
static instance: BinaryDataManager | undefined;
|
static instance: BinaryDataManager | undefined;
|
||||||
|
@ -104,11 +104,7 @@ export class BinaryDataManager {
|
||||||
fileSize,
|
fileSize,
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
// Else fallback to storing this data in memory.
|
const buffer = await binaryToBuffer(input);
|
||||||
const buffer = await new Promise<Buffer>((resolve) => {
|
|
||||||
if (Buffer.isBuffer(input)) resolve(input);
|
|
||||||
else input.pipe(concatStream(resolve));
|
|
||||||
});
|
|
||||||
binaryData.data = buffer.toString(BINARY_ENCODING);
|
binaryData.data = buffer.toString(BINARY_ENCODING);
|
||||||
binaryData.fileSize = prettyBytes(buffer.length);
|
binaryData.fileSize = prettyBytes(buffer.length);
|
||||||
}
|
}
|
||||||
|
|
8
packages/core/src/BinaryDataManager/utils.ts
Normal file
8
packages/core/src/BinaryDataManager/utils.ts
Normal 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));
|
||||||
|
});
|
|
@ -149,7 +149,6 @@ const createFormDataObject = (data: Record<string, unknown>) => {
|
||||||
});
|
});
|
||||||
return formData;
|
return formData;
|
||||||
};
|
};
|
||||||
|
|
||||||
function searchForHeader(headers: IDataObject, headerName: string) {
|
function searchForHeader(headers: IDataObject, headerName: string) {
|
||||||
if (headers === undefined) {
|
if (headers === undefined) {
|
||||||
return undefined;
|
return undefined;
|
||||||
|
|
Loading…
Reference in a new issue