Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for @aws-sdk/client-s3 #277

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 52 additions & 32 deletions lib/Open/directory.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,35 @@ var signature = Buffer.alloc(4);
signature.writeUInt32LE(0x06054b50,0);

function getCrxHeader(source) {
var sourceStream = source.stream(0).pipe(PullStream());

return sourceStream.pull(4).then(function(data) {
var signature = data.readUInt32LE(0);
if (signature === 0x34327243) {
var crxHeader;
return sourceStream.pull(12).then(function(data) {
crxHeader = binary.parse(data)
.word32lu('version')
.word32lu('pubKeyLength')
.word32lu('signatureLength')
.vars;
}).then(function() {
return sourceStream.pull(crxHeader.pubKeyLength +crxHeader.signatureLength);
}).then(function(data) {
crxHeader.publicKey = data.slice(0,crxHeader.pubKeyLength);
crxHeader.signature = data.slice(crxHeader.pubKeyLength);
crxHeader.size = 16 + crxHeader.pubKeyLength +crxHeader.signatureLength;
return crxHeader;
const res = PullStream();

return source.stream(0)
.catch(function(e) {
res.emit('error', e);
})
.then(function(stream) {
stream.pipe(res);
return res.pull(4).then(function(data) {
var signature = data.readUInt32LE(0);
if (signature === 0x34327243) {
var crxHeader;
return res.pull(12).then(function(data) {
crxHeader = binary.parse(data)
.word32lu('version')
.word32lu('pubKeyLength')
.word32lu('signatureLength')
.vars;
}).then(function() {
return res.pull(crxHeader.pubKeyLength + crxHeader.signatureLength);
}).then(function(data) {
crxHeader.publicKey = data.slice(0,crxHeader.pubKeyLength);
crxHeader.signature = data.slice(crxHeader.pubKeyLength);
crxHeader.size = 16 + crxHeader.pubKeyLength + crxHeader.signatureLength;
return crxHeader;
});
}
});
}
});
})
}

// Zip64 File Format Notes: https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT
Expand All @@ -51,9 +58,14 @@ function getZip64CentralDirectory(source, zip64CDL) {
}

var dir64 = PullStream();
source.stream(d64loc.offsetToStartOfCentralDirectory).pipe(dir64);

return dir64.pull(56)
return source.stream(d64loc.offsetToStartOfCentralDirectory)
.then(function(stream) {
stream.pipe(dir64);
return dir64.pull(56)
})
.catch(function(error) {
return dir64.destroy(error);
})
}

// Zip64 File Format Notes: https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT
Expand Down Expand Up @@ -94,12 +106,15 @@ module.exports = function centralDirectory(source, options) {
.then(function(size) {
sourceSize = size;

source.stream(Math.max(0,size-tailSize))
.on('error', function (error) { endDir.emit('error', error) })
.pipe(endDir);

return source.stream(Math.max(0,size-tailSize))
})
.then(function(stream) {
stream.pipe(endDir);
return endDir.pull(signature);
})
.catch(function(error) {
endDir.emit('error', error);
})
.then(function() {
return Promise.props({directory: endDir.pull(22), crxHeader: crxHeader});
})
Expand Down Expand Up @@ -130,9 +145,11 @@ module.exports = function centralDirectory(source, options) {
const zip64CDLOffset = sourceSize - (tailSize - endDir.match + zip64CDLSize)
const zip64CDLStream = PullStream();

source.stream(zip64CDLOffset).pipe(zip64CDLStream);

return zip64CDLStream.pull(zip64CDLSize)
return source.stream(zip64CDLOffset)
.then(function(stream) {
stream.pipe(zip64CDLStream);
return zip64CDLStream.pull(zip64CDLSize);
})
.then(function (d) { return getZip64CentralDirectory(source, d) })
.then(function (dir64record) {
vars = parseZip64DirRecord(dir64record)
Expand All @@ -147,7 +164,10 @@ module.exports = function centralDirectory(source, options) {
});
})
.then(function() {
source.stream(vars.offsetToStartOfCentralDirectory).pipe(records);
return source.stream(vars.offsetToStartOfCentralDirectory);
})
.then(function(stream) {
stream.pipe(records);

vars.extract = function(opts) {
if (!opts || !opts.path) throw new Error('PATH_MISSING');
Expand Down
34 changes: 21 additions & 13 deletions lib/Open/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module.exports = {
stream: function(offset, length) {
var stream = Stream.PassThrough();
stream.end(buffer.slice(offset, length));
return stream;
return Promise.resolve(stream);
},
size: function() {
return Promise.resolve(buffer.length);
Expand All @@ -24,7 +24,7 @@ module.exports = {
file: function(filename, options) {
var source = {
stream: function(offset,length) {
return fs.createReadStream(filename,{start: offset, end: length && offset+length});
return Promise.resolve(fs.createReadStream(filename,{start: offset, end: length && offset+length}));
},
size: function() {
return new Promise(function(resolve,reject) {
Expand All @@ -48,11 +48,11 @@ module.exports = {
params.headers = params.headers || {};

var source = {
stream : function(offset,length) {
stream: function(offset,length) {
var options = Object.create(params);
options.headers = Object.create(params.headers);
options.headers.range = 'bytes='+offset+'-' + (length ? length : '');
return request(options);
return Promise.resolve(request(options));
},
size: function() {
return new Promise(function(resolve,reject) {
Expand All @@ -71,24 +71,32 @@ module.exports = {
return directory(source, options);
},

s3 : function(client,params, options) {
s3: function(client, params, options) {
// detect aws-sdk v2 or aws-sdk v3
const isAwsSdkV3 = Object.prototype.hasOwnProperty.call(client, 'send');

var source = {
size: function() {
return new Promise(function(resolve,reject) {
client.headObject(params, function(err,d) {
if (err)
reject(err);
else
resolve(d.ContentLength);
if (isAwsSdkV3) {
return client.headObject(params).then(function (res) {
Copy link

@zandaqo zandaqo Jun 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But AWS SDK V3 doesn't have headObject function? S3Client doesn't have the said methods, S3 class does, though, it doesn't have the send method, hence, the PR still breaks.

Copy link

@ChrisZieba ChrisZieba Jun 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, yea I gave this a try and it doesn't seem to work out. What if you did something like

try {
   return client.headObject(params).promise()
} catch(e) {
   if (e.msg === 'something') {
        return client.headObject(params).then()
   }
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alvin-nt thanks for opening this PR, do you have the bandwidth to add this tweak or should I give it a shot?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi. Apologies for the long reply. Due to life circumstances, I could not continue this PR yet. If you may, please give a shot to fix my code.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be enough to change isAwsSdkV3 to this:

            const isAwsSdkV3 = typeof client.send === "function";

Here, at least, it gives true with v3 and false with v2

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, Maybe we can do something like:

    if (isAwsSdkV3) {
          return client.send(new HeadObjectCommand(params)).then(function (res) {
            return res.ContentLength;
          });
    }

return res.ContentLength;
});
}
return client.headObject(params).promise().then(function (res) {
return res.ContentLength;
});
},
stream: function(offset,length) {
stream: function(offset, length) {
var d = {};
for (var key in params)
d[key] = params[key];
d.Range = 'bytes='+offset+'-' + (length ? length : '');
return client.getObject(d).createReadStream();
if (isAwsSdkV3) {
return client.getObject(d).then(function (res) {
return res.Body;
})
}
return Promise.resolve(client.getObject(d).createReadStream());
}
};

Expand Down
41 changes: 24 additions & 17 deletions lib/Open/unzip.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,19 @@ if (!Stream.Writable || !Stream.Writable.prototype.destroy)

module.exports = function unzip(source,offset,_password, directoryVars) {
var file = PullStream(),
entry = Stream.PassThrough();
entry = Stream.PassThrough(),
req;

var req = source.stream(offset);
req.pipe(file).on('error', function(e) {
entry.emit('error', e);
});

entry.vars = file.pull(30)
entry.vars = source.stream(offset)
.catch(function(e) {
entry.emit('error', e);
})
.then(function(srcStream) {
req = srcStream.pipe(file).on('error', function(e) {
entry.emit('error', e);
});
return file.pull(30)
})
.then(function(data) {
var vars = binary.parse(data)
.word32lu('signature')
Expand Down Expand Up @@ -106,16 +111,18 @@ module.exports = function unzip(source,offset,_password, directoryVars) {
.on('error',function(err) { entry.emit('error',err);})
.pipe(entry)
.on('finish', function() {
if(req.destroy)
req.destroy()
else if (req.abort)
req.abort();
else if (req.close)
req.close();
else if (req.push)
req.push();
else
console.log('warning - unable to close stream');
if (req) {
if(req.destroy)
req.destroy()
else if (req.abort)
req.abort();
else if (req.close)
req.close();
else if (req.push)
req.push();
else
console.log('warning - unable to close stream');
}
});
})
.catch(function(e) {
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,17 @@
"setimmediate": "~1.0.4"
},
"devDependencies": {
"aws-sdk": "^2.77.0",
"dirdiff": ">= 0.0.1 < 1",
"iconv-lite": "^0.4.24",
"request": "^2.88.0",
"stream-buffers": ">= 0.2.5 < 1",
"tap": ">= 0.3.0 < 1",
"temp": ">= 0.4.0 < 1"
},
"optionalDependencies": {
"aws-sdk": "^2.77.0",
"@aws-sdk/client-s3": "^3.304.0"
},
"directories": {
"example": "examples",
"test": "test"
Expand Down
2 changes: 1 addition & 1 deletion test/openCustom.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ test("get content of a single file entry out of a zip", function (t) {

var customSource = {
stream: function(offset,length) {
return fs.createReadStream(archive, {start: offset, end: length && offset+length});
return Promise.resolve(fs.createReadStream(archive, {start: offset, end: length && offset+length}));
},
size: function() {
return new Promise(function(resolve, reject) {
Expand Down