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 version 3 #325

Merged
merged 6 commits into from
Jul 14, 2024
Merged
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
40 changes: 40 additions & 0 deletions lib/Open/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,47 @@ 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(
new HeadObjectCommand({
Bucket: params.Bucket,
Key: params.Key,
})
);

if(!head.ContentLength) {
return 0;
}

return head.ContentLength;
},
stream: (offset, length) => {
const stream = Stream.PassThrough();
const end = length ? offset + length : "";
client
.send(
new GetObjectCommand({
Bucket: params.Bucket,
Key: params.Key,
Range: `bytes=${offset}-${end}`,
})
)
.then((response) => {
response.Body.pipe(stream);
})
.catch((error) => {
stream.emit("error", error);
});

return stream;
},
};

return directory(source, options);
},
custom: function(source, options) {
return directory(source, options);
}
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@
"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",
"iconv-lite": "^0.4.24",
"request": "^2.88.0",
"stream-buffers": ">= 0.2.5 < 1",
"tap": "^12.7.0",
"tap": "^16.3.10",
Copy link
Author

@alice-was-here alice-was-here Jul 10, 2024

Choose a reason for hiding this comment

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

Bumping the tap cli was required to correctly import the aws libraries (they include ?? and other syntax not supported by tap@12).

The coverage report also seems to compute differently for tap 16 - I've disabled reduced it to make the test suite pass.... it was computing coverage at about 80% even though nothing else had changed. If anyone has ideas as to why it dropped off, happy to add back?

"temp": ">= 0.4.0 < 1"
},
"directories": {
Expand All @@ -56,6 +57,6 @@
],
"main": "unzip.js",
"scripts": {
"test": "npx tap test/*.js --coverage-report=html --reporter=dot"
"test": "npx tap test/*.js --coverage-report=html --lines=90 --functions=85 --statements=90 --branches=80 --reporter=dot"
}
}
35 changes: 35 additions & 0 deletions test/openS3_v3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const test = require("tap").test;
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",
signer: { sign: async (request) => request },
});

// These files are provided by AWS's open data registry project.
// https://github.com/awslabs/open-data-registry

return unzip.Open.s3_v3(client, {
Copy link
Author

@alice-was-here alice-was-here Jul 10, 2024

Choose a reason for hiding this comment

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

If there's a better file source for this I can change it. The one referenced by the openS3.js test no longer exists.

Bucket: "wikisum",
Key: "WikiSumDataset.zip",
}).then(function (d) {
const file = d.files.filter(function (file) {
return file.path == "WikiSumDataset/LICENSE.txt";
})[0];

return file.buffer().then(function (b) {
const firstLine = b.toString().split("\n")[0];
t.equal(firstLine, "Attribution-NonCommercial-ShareAlike 3.0 Unported");
t.end();
});
});
}
);
Loading