diff --git a/lib/Open/index.js b/lib/Open/index.js index ab0da12..81f95d5 100644 --- a/lib/Open/index.js +++ b/lib/Open/index.js @@ -1,7 +1,6 @@ const fs = require('graceful-fs'); const directory = require('./directory'); const Stream = require('stream'); -const { GetObjectCommand, HeadObjectCommand } = require('@aws-sdk/client-s3'); module.exports = { buffer: function(buffer, options) { @@ -95,6 +94,7 @@ module.exports = { return directory(source, options); }, s3_v3: function (client, params, options) { + const { GetObjectCommand, HeadObjectCommand } = require('@aws-sdk/client-s3'); const source = { size: async () => { const head = await client.send( @@ -104,7 +104,11 @@ module.exports = { }) ); - return head.ContentLength ?? 0; + if(!head.ContentLength) { + return 0; + } + + return head.ContentLength; }, stream: (offset, length) => { const stream = Stream.PassThrough(); diff --git a/package.json b/package.json index ab009c1..47aedc9 100644 --- a/package.json +++ b/package.json @@ -32,6 +32,7 @@ "devDependencies": { "@eslint/js": "^9.2.0", "aws-sdk": "^2.1636.0", + "@aws-sdk/client-s3": "^3.0.0", "dirdiff": ">= 0.0.1 < 1", "eslint": "^9.2.0", "globals": "^15.2.0", diff --git a/test/openS3_v3.js b/test/openS3_v3.js new file mode 100644 index 0000000..6cb720b --- /dev/null +++ b/test/openS3_v3.js @@ -0,0 +1,25 @@ +const test = require('tap').test; +const fs = require('fs'); +const path = require('path'); +const unzip = require('../unzip'); + +const version = +process.version.replace('v', '').split('.')[0]; + +test("get content of a single file entry out of a zip", { skip: version < 16 }, function(t) { + const { S3Client } = require('@aws-sdk/client-s3'); + const client = new S3Client({ region: 'us-east-1' }); + + return unzip.Open.s3_v3(client, { Bucket: 'unzipper', Key: 'archive.zip' }) + .then(function(d) { + const file = d.files.filter(function(file) { + return file.path == 'file.txt'; + })[0]; + + return file.buffer() + .then(function(str) { + const fileStr = fs.readFileSync(path.join(__dirname, '../testData/compressed-standard/inflated/file.txt'), 'utf8'); + t.equal(str.toString(), fileStr); + t.end(); + }); + }); +});