Skip to content

Commit

Permalink
Fix msg object duplication (#60)
Browse files Browse the repository at this point in the history
* Updated copy-object.js

* Updated create-bucket.js

* Updated delete-object.js

* Updated list-buckets.js

* Updated list-objects.js

* Updated put-objects.js

* Updated get-object.js

* Updated head-object.js

* Updated put-object.js

* Updated move-object.js

* Updated list-objects-v2.js

* Updated list-objects-versions.js

* Updated CHNAGELOG
  • Loading branch information
rristov60 authored Aug 2, 2023
1 parent 3f82a06 commit 168e3ee
Show file tree
Hide file tree
Showing 13 changed files with 175 additions and 159 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased
Added msg object clones for better msg object handling

## 1.11.5 - 2023-08-01
Updated node.error so it can be catch with catch node

## 1.11.4 - 2023-08-01
Reverted the refactor with await/async

## 1.11.3 - 2023-08-01
Refactored nodes with await/async

## 1.11.2 - 2023-07-24
Added ACL for Copy Object node
Expand Down
29 changes: 15 additions & 14 deletions nodes/copy-object/copy-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ module.exports = function (RED) {
* https://docs.aws.amazon.com/AmazonS3/latest/API/API_CopyObject.html
*/
let payloadConfig = {};
const msgClone = structuredClone(msg);

// Bucket parameter
let bucket = n.bucket != "" ? n.bucket : null;
if (!bucket) {
bucket = msg.bucket ? msg.bucket : null;
bucket = msgClone.bucket ? msgClone.bucket : null;
if (!bucket) {
node.error("No bucket provided!");
return;
Expand All @@ -36,7 +37,7 @@ module.exports = function (RED) {
// key parameter
let key = n.key != "" ? n.key : null;
if (!key) {
key = msg.key ? msg.key : null;
key = msgClone.key ? msgClone.key : null;
if (!key) {
node.error("No object key provided!");
return;
Expand All @@ -47,7 +48,7 @@ module.exports = function (RED) {
// copy source parameter
let copysource = n.copysource != "" ? n.copysource : null;
if (!copysource) {
copysource = msg.copysource ? msg.copysource : null;
copysource = msgClone.copysource ? msgClone.copysource : null;
if (!copysource) {
node.error("No Copy Source provided!");
return;
Expand All @@ -57,7 +58,7 @@ module.exports = function (RED) {
// versionId parameter
let versionid = n.versionid != "" ? n.versionid : null;
if (!versionid) {
versionid = msg.versionid ? msg.versionid : null;
versionid = msgClone.versionid ? msgClone.versionid : null;
}

if (versionid) {
Expand All @@ -71,7 +72,7 @@ module.exports = function (RED) {
// ContentEncoding parameter
let contentencoding = n.contentencoding != "" ? n.contentencoding : null;
if (!contentencoding) {
contentencoding = msg.contentencoding ? msg.contentencoding : null;
contentencoding = msgClone.contentencoding ? msgClone.contentencoding : null;
}
if (contentencoding && !isValidContentEncoding(contentencoding)) {
node.error("Invalid content encoding!");
Expand All @@ -83,7 +84,7 @@ module.exports = function (RED) {

// ACL parameter
if (!acl) {
acl = msg.acl ? msg.acl : null;
acl = msgClone.acl ? msgClone.acl : null;
}
if (acl && !isValidACL(acl)) {
node.error("Invalid ACL permissions value");
Expand Down Expand Up @@ -111,29 +112,29 @@ module.exports = function (RED) {
s3Client.copyObject(payloadConfig, function (err, data) {
if (err) {
node.status({ fill: "red", shape: "dot", text: `Failure` });
node.error(err, msg);
node.error(err, msgClone);
// Replace the payload with null
msg.payload = null;
msgClone.payload = null;
// Append the bucket to
// the message object
msg.bucket = bucket;
msgClone.bucket = bucket;

// Return the complete message object
send(msg);
send(msgClone);

setTimeout(() => {
node.status({});
}, 3000);
} else {
// Replace the payload with
// the returned data
msg.payload = data;
msgClone.payload = data;
// Append the bucket to
// the message object
msg.bucket = bucket;
msgClone.bucket = bucket;

// Return the complete message object
send(msg);
send(msgClone);

// Finalize
if (done) {
Expand All @@ -149,7 +150,7 @@ module.exports = function (RED) {
});
} catch (err) {
// If error occurs
node.error(err, msg);
node.error(err, msgClone);
// Cleanup
if (s3Client !== null) s3Client.destroy();
if (done) done();
Expand Down
20 changes: 11 additions & 9 deletions nodes/create-bucket/create-bucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ module.exports = function (RED) {

this.on("input", async function (msg, send, done) {
let bucket = n.bucket != "" ? n.bucket : null; // Bucket info
const msgClone = structuredClone(msg);

// Checking for correct properties input
if (!bucket) {
bucket = msg.bucket ? msg.bucket : null;
bucket = msgClone.bucket ? msgClone.bucket : null;
if (!bucket) {
node.error("No bucket provided!");
return;
Expand All @@ -44,25 +46,25 @@ module.exports = function (RED) {
s3Client.createBucket({ Bucket: bucket }, function (err, data) {
if (err) {
node.status({ fill: "red", shape: "dot", text: `Failure` });
node.error(err, msg);
node.error(err, msgClone);
// Replace the payload with null
msg.payload = null;
msgClone.payload = null;
// Append the bucket to
// the message object
msg.bucket = bucket;
msgClone.bucket = bucket;

// Return the complete message object
send(msg);
send(msgClone);
} else {
// Replace the payload with
// the returned data
msg.payload = data;
msgClone.payload = data;
// Append the bucket to
// the message object
msg.bucket = bucket;
msgClone.bucket = bucket;

// Return the complete message object
send(msg);
send(msgClone);

node.status({ fill: "green", shape: "dot", text: "Success" });
}
Expand All @@ -80,7 +82,7 @@ module.exports = function (RED) {
});
} catch (err) {
// If error occurs
node.error(err, msg);
node.error(err, msgClone);
// Cleanup
if (s3Client !== null) s3Client.destroy();
if (done) done();
Expand Down
21 changes: 11 additions & 10 deletions nodes/delete-object/delete-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,19 @@ module.exports = function (RED) {
this.on("input", async function (msg, send, done) {
let bucket = n.bucket != "" ? n.bucket : null; // Bucket info
let key = n.key != "" ? n.key : null; // Object key
const msgClone = structuredClone(msg);

// Checking for correct properties input
if (!bucket) {
bucket = msg.bucket ? msg.bucket : null;
bucket = msgClone.bucket ? msgClone.bucket : null;
if (!bucket) {
node.error("No bucket provided!");
return;
}
}

if (!key) {
key = msg.key ? msg.key : null;
key = msgClone.key ? msgClone.key : null;
if (!key) {
node.error("No object key provided!");
return;
Expand All @@ -56,24 +57,24 @@ module.exports = function (RED) {
function (err, data) {
if (err) {
node.status({ fill: "red", shape: "dot", text: `Failure` });
node.error(err, msg);
node.error(err, msgClone);
// Replace the payload with null
msg.payload = null;
msgClone.payload = null;
// Append the delete object
// key to the message object
msg.key = key;
msgClone.key = key;

// Return the complete message object
send(msg);
send(msgClone);
} else {
// Replace the payload with
// the returned data
msg.payload = data;
msgClone.payload = data;
// Append the deleted object
// key to the message object
msg.key = key;
msgClone.key = key;

send(msg);
send(msgClone);
}

node.status({ fill: "green", shape: "dot", text: `Done!` });
Expand All @@ -90,7 +91,7 @@ module.exports = function (RED) {
);
} catch (err) {
// If error occurs
node.error(err, msg);
node.error(err, msgClone);
// Cleanup
if (s3Client !== null) s3Client.destroy();
if (done) done();
Expand Down
29 changes: 15 additions & 14 deletions nodes/get-object/get-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ module.exports = function (RED) {
this.on("input", async function (msg, send, done) {
let bucket = n.bucket != "" ? n.bucket : null;
let key = n.key != "" ? n.key : null;
const msgClone = structuredClone(msg);

let getObjectPayload = {};

// Checking for correct properties input
if (!bucket) {
bucket = msg.bucket ? msg.bucket : null;
bucket = msgClone.bucket ? msgClone.bucket : null;
if (!bucket) {
node.error("No bucket provided!");
return;
Expand All @@ -33,7 +34,7 @@ module.exports = function (RED) {
getObjectPayload.Bucket = bucket;

if (!key) {
key = msg.key ? msg.key : null;
key = msgClone.key ? msgClone.key : null;
if (!key) {
node.error("No object key provided!");
return;
Expand All @@ -44,7 +45,7 @@ module.exports = function (RED) {
// versionId parameter
let versionid = n.versionid != "" ? n.versionid : null;
if (!versionid) {
versionid = msg.versionid ? msg.versionid : null;
versionid = msgClone.versionid ? msgClone.versionid : null;
}
if (versionid) {
getObjectPayload.VersionId = versionid;
Expand All @@ -53,16 +54,16 @@ module.exports = function (RED) {
// stringifyBody parameter
let stringifybody = n.stringifybody ? n.stringifybody : false;
if (!stringifybody) {
stringifybody = msg.stringifybody ? msg.stringifybody : false;
stringifybody = msgClone.stringifybody ? msgClone.stringifybody : false;
}

// stringifyBody base 64 encoding parameter
let stringifybodybase64 = n.stringifybodybase64
? n.stringifybodybase64
: false;
if (!stringifybodybase64) {
stringifybodybase64 = msg.stringifybodybase64
? msg.stringifybodybase64
stringifybodybase64 = msgClone.stringifybodybase64
? msgClone.stringifybodybase64
: false;
}

Expand All @@ -86,15 +87,15 @@ module.exports = function (RED) {
// If an error occured, print the error
if (err) {
node.status({ fill: "red", shape: "dot", text: `Failure` });
node.error(err, msg);
node.error(err, msgClone);
// Replace the payload with null
msg.payload = null;
msgClone.payload = null;
// Append the object
// key to the message object
msg.key = key;
msgClone.key = key;

// Return the complete message object
send(msg);
send(msgClone);
} else {
// if not, return message with the payload
// ********************************************
Expand All @@ -117,13 +118,13 @@ module.exports = function (RED) {

// Replace the payload with
// the returned data
msg.payload = data;
msgClone.payload = data;
// Append the object
// key to the message object
msg.key = key;
msgClone.key = key;

// Return the complete message object
send(msg);
send(msgClone);

node.status({ fill: "green", shape: "dot", text: "Success" });
}
Expand All @@ -140,7 +141,7 @@ module.exports = function (RED) {
});
} catch (err) {
// If error occurs
node.error(err, msg);
node.error(err, msgClone);
// Cleanup
if (s3Client !== null) s3Client.destroy();
if (done) done();
Expand Down
Loading

0 comments on commit 168e3ee

Please sign in to comment.