fix(core & function nodes): Update function nodes to work with binary-data-mode 'filesystem'. (#3845)

* Initial Fix

* Self-Review #1

* Lint

* Added support for FunctionItem. Minor updates.

* Self-review

* review comments. Added testing.

* Self Review

* Fixed memory handling on data manager use.

* Fixes for unnecessary memory leaks.
This commit is contained in:
Rhys Williams
2022-09-11 16:42:09 +02:00
committed by GitHub
parent b450e977a3
commit f6064ef278
7 changed files with 267 additions and 4 deletions

View File

@@ -75,22 +75,70 @@ return item;`,
};
for (let itemIndex = 0; itemIndex < length; itemIndex++) {
const mode = this.getMode();
try {
item = items[itemIndex];
item.index = itemIndex;
// Copy the items as they may get changed in the functions
item = JSON.parse(JSON.stringify(item));
// Define the global objects for the custom function
const sandbox = {
/** @deprecated for removal - replaced by getBinaryDataAsync() */
getBinaryData: (): IBinaryKeyData | undefined => {
if (mode === 'manual') {
this.sendMessageToUI(
'getBinaryData(...) is deprecated and will be removed in a future version. Please consider switching to getBinaryDataAsync(...) instead.',
);
}
return item.binary;
},
/** @deprecated for removal - replaced by setBinaryDataAsync() */
setBinaryData: async (data: IBinaryKeyData) => {
if (mode === 'manual') {
this.sendMessageToUI(
'setBinaryData(...) is deprecated and will be removed in a future version. Please consider switching to setBinaryDataAsync(...) instead.',
);
}
item.binary = data;
},
getNodeParameter: this.getNodeParameter,
getWorkflowStaticData: this.getWorkflowStaticData,
helpers: this.helpers,
item: item.json,
setBinaryData: (data: IBinaryKeyData) => {
getBinaryDataAsync: async (): Promise<IBinaryKeyData | undefined> => {
// Fetch Binary Data, if available. Cannot check item with `if (item?.index)`, as index may be 0.
if (item?.binary && item?.index !== undefined && item?.index !== null) {
for (const binaryPropertyName of Object.keys(item.binary)) {
item.binary[binaryPropertyName].data = (
await this.helpers.getBinaryDataBuffer(item.index, binaryPropertyName)
)?.toString('base64');
}
}
// Retrun Data
return item.binary;
},
setBinaryDataAsync: async (data: IBinaryKeyData) => {
// Ensure data is present
if (!data) {
throw new NodeOperationError(
this.getNode(),
'No data was provided to setBinaryDataAsync (data: IBinaryKeyData).',
);
}
// Set Binary Data
for (const binaryPropertyName of Object.keys(data)) {
const binaryItem = data[binaryPropertyName];
data[binaryPropertyName] = await this.helpers.setBinaryDataBuffer(
binaryItem,
Buffer.from(binaryItem.data, 'base64'),
);
}
// Set Item Reference
item.binary = data;
},
};
@@ -99,8 +147,6 @@ return item;`,
const dataProxy = this.getWorkflowDataProxy(itemIndex);
Object.assign(sandbox, dataProxy);
const mode = this.getMode();
const options = {
console: mode === 'manual' ? 'redirect' : 'inherit',
sandbox,