From 6f7421f970b12e870c7d84b0f559b06678e3d42a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E0=A4=95=E0=A4=BE=E0=A4=B0=E0=A4=A4=E0=A5=8B=E0=A4=AB?= =?UTF-8?q?=E0=A5=8D=E0=A4=AB=E0=A5=87=E0=A4=B2=E0=A4=B8=E0=A5=8D=E0=A4=95?= =?UTF-8?q?=E0=A5=8D=E0=A4=B0=E0=A4=BF=E0=A4=AA=E0=A5=8D=E0=A4=9F=E2=84=A2?= Date: Tue, 31 Jan 2023 14:03:31 +0100 Subject: [PATCH] feat(SSH Node): Stream binary data for uploads and downloads (#5305) --- packages/nodes-base/nodes/Ssh/Ssh.node.ts | 32 ++++++++++++++--------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/packages/nodes-base/nodes/Ssh/Ssh.node.ts b/packages/nodes-base/nodes/Ssh/Ssh.node.ts index 6c64dedb86..5d28ba8861 100644 --- a/packages/nodes-base/nodes/Ssh/Ssh.node.ts +++ b/packages/nodes-base/nodes/Ssh/Ssh.node.ts @@ -1,4 +1,5 @@ import type { IExecuteFunctions } from 'n8n-core'; +import { BINARY_ENCODING } from 'n8n-core'; import type { IDataObject, @@ -8,11 +9,13 @@ import type { } from 'n8n-workflow'; import { NodeOperationError } from 'n8n-workflow'; -import { readFile, rm, writeFile } from 'fs/promises'; +import { rm, writeFile } from 'fs/promises'; -import { file } from 'tmp-promise'; +import { file as tmpFile } from 'tmp-promise'; +import type { Config } from 'node-ssh'; import { NodeSSH } from 'node-ssh'; +import type { Readable } from 'stream'; export class Ssh implements INodeType { description: INodeTypeDescription = { @@ -268,16 +271,16 @@ export class Ssh implements INodeType { } else if (authentication === 'privateKey') { const credentials = await this.getCredentials('sshPrivateKey'); - const { path } = await file({ prefix: 'n8n-ssh-' }); + const { path } = await tmpFile({ prefix: 'n8n-ssh-' }); temporaryFiles.push(path); await writeFile(path, credentials.privateKey as string); - const options = { + const options: Config = { host: credentials.host as string, username: credentials.username as string, port: credentials.port as number, privateKey: path, - } as any; + }; if (credentials.passphrase) { options.passphrase = credentials.passphrase as string; @@ -306,7 +309,7 @@ export class Ssh implements INodeType { const dataPropertyNameDownload = this.getNodeParameter('binaryPropertyName', i); const parameterPath = this.getNodeParameter('path', i) as string; - const { path } = await file({ prefix: 'n8n-ssh-' }); + const { path } = await tmpFile({ prefix: 'n8n-ssh-' }); temporaryFiles.push(path); await ssh.getFile(path, parameterPath); @@ -328,10 +331,8 @@ export class Ssh implements INodeType { items[i] = newItem; - const data = await readFile(path); - - items[i].binary![dataPropertyNameDownload] = await this.helpers.prepareBinaryData( - data, + items[i].binary![dataPropertyNameDownload] = await this.helpers.copyBinaryFile( + path, parameterPath, ); } @@ -360,11 +361,16 @@ export class Ssh implements INodeType { ); } - const dataBuffer = await this.helpers.getBinaryDataBuffer(i, propertyNameUpload); + let uploadData: Buffer | Readable; + if (binaryData.id) { + uploadData = this.helpers.getBinaryStream(binaryData.id); + } else { + uploadData = Buffer.from(binaryData.data, BINARY_ENCODING); + } - const { path } = await file({ prefix: 'n8n-ssh-' }); + const { path } = await tmpFile({ prefix: 'n8n-ssh-' }); temporaryFiles.push(path); - await writeFile(path, dataBuffer); + await writeFile(path, uploadData); await ssh.putFile( path,