diff --git a/packages/nodes-base/nodes/MoveBinaryData.node.ts b/packages/nodes-base/nodes/MoveBinaryData.node.ts index b715bf3971..1286fab193 100644 --- a/packages/nodes-base/nodes/MoveBinaryData.node.ts +++ b/packages/nodes-base/nodes/MoveBinaryData.node.ts @@ -4,6 +4,10 @@ import { unset, } from 'lodash'; +import { + BINARY_ENCODING, +} from 'n8n-core'; + import { IExecuteFunctions } from 'n8n-core'; import { IBinaryData, @@ -13,6 +17,18 @@ import { INodeTypeDescription, } from 'n8n-workflow'; +import * as iconv from 'iconv-lite'; +iconv.encodingExists('utf8'); +const bomAware: string[] = []; +const encodeDecodeOptions: INodePropertyOptions[] = []; +Object.keys((iconv as any).encodings).forEach(encoding => { + if(!(encoding.startsWith('_') || typeof (iconv as any).encodings[encoding] == 'string')) { // only encodings without direct alias or internals + if((iconv as any).encodings[encoding].bomAware) { + bomAware.push(encoding); + } + encodeDecodeOptions.push({ name: encoding, value: encoding}); + } +}); export class MoveBinaryData implements INodeType { description: INodeTypeDescription = { @@ -189,17 +205,47 @@ export class MoveBinaryData implements INodeType { { displayName: 'Encoding', name: 'encoding', - type: 'string', + type: 'options', + options: encodeDecodeOptions, displayOptions: { show: { '/mode': [ 'binaryToJson', + 'jsonToBinary', ], }, }, default: 'utf8', description: 'Set the encoding of the data stream', }, + { + displayName: 'Strip BOM', + name: 'stripBOM', + displayOptions: { + show: { + '/mode': [ + 'binaryToJson', + ], + encoding: bomAware + }, + }, + type: 'boolean', + default: true, + }, + { + displayName: 'Add BOM', + name: 'addBOM', + displayOptions: { + show: { + '/mode': [ + 'jsonToBinary', + ], + encoding: bomAware + }, + }, + type: 'boolean', + default: false, + }, { displayName: 'File Name', name: 'fileName', @@ -336,19 +382,19 @@ export class MoveBinaryData implements INodeType { continue; } - const encoding = (options.encoding as BufferEncoding) || 'utf8'; + const encoding = (options.encoding as string) || 'utf8'; let convertedValue = value.data; if (setAllData === true) { // Set the full data - convertedValue = Buffer.from(convertedValue, 'base64').toString(encoding); + convertedValue = iconv.decode(Buffer.from(convertedValue, BINARY_ENCODING), encoding, {stripBOM: options.stripBOM as boolean}); newItem.json = JSON.parse(convertedValue); } else { // Does get added to existing data so copy it first newItem.json = JSON.parse(JSON.stringify(item.json)); if (options.keepAsBase64 !== true) { - convertedValue = Buffer.from(convertedValue, 'base64').toString(encoding); + convertedValue = iconv.decode(Buffer.from(convertedValue, BINARY_ENCODING), encoding, {stripBOM: options.stripBOM as boolean}); } if (options.jsonParse) { @@ -372,6 +418,7 @@ export class MoveBinaryData implements INodeType { const convertAllData = this.getNodeParameter('convertAllData', itemIndex) as boolean; const destinationKey = this.getNodeParameter('destinationKey', itemIndex) as string; + const encoding = (options.encoding as string) || 'utf8'; let value: IDataObject | string = item.json; if (convertAllData === false) { const sourceKey = this.getNodeParameter('sourceKey', itemIndex) as string; @@ -396,7 +443,7 @@ export class MoveBinaryData implements INodeType { value = JSON.stringify(value); } - value = Buffer.from(value as string).toString('base64'); + value = iconv.encode(value as string, encoding, {addBOM: options.addBOM as boolean}).toString(BINARY_ENCODING); } const convertedValue: IBinaryData = { @@ -437,4 +484,4 @@ export class MoveBinaryData implements INodeType { return [returnData]; } -} +} \ No newline at end of file diff --git a/packages/nodes-base/package.json b/packages/nodes-base/package.json index b4317f1f93..2d97909a8c 100644 --- a/packages/nodes-base/package.json +++ b/packages/nodes-base/package.json @@ -589,6 +589,7 @@ "get-system-fonts": "^2.0.2", "glob-promise": "^3.4.0", "gm": "^1.23.1", + "iconv-lite": "^0.6.2", "imap-simple": "^4.3.0", "iso-639-1": "^2.1.3", "jsonwebtoken": "^8.5.1", @@ -639,4 +640,4 @@ "json" ] } -} +} \ No newline at end of file