Skip to content

Commit

Permalink
fix: Support empty objects (size 0) (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelwittig authored May 24, 2023
1 parent ae6ab2d commit de4a336
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 11 deletions.
36 changes: 25 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,13 @@ function getObject({Bucket, Key, VersionId, PartNumber, Range}, timeout, cb) {
if (err) {
cb(err);
} else {
if (res.statusCode === 206) {
if (res.statusCode === 200 && body.length === 0) {
const data = {
Body: body,
ContentLength: body.length
};
cb(null, data);
} else if (res.statusCode === 206) {
const data = {
Body: body,
ContentLength: body.length
Expand Down Expand Up @@ -578,19 +584,27 @@ exports.download = ({bucket, key, version}, {partSizeInMegabytes, concurrency, c
partsDownloading[1] = getObject(params, timeout, (err, data) => {
delete partsDownloading[1];
if (err) {
reject(err);
if (err.code === 'InvalidRange') {
resolve({metadata: {lengthInBytes: 0}, body: Buffer.alloc(0)});
} else {
reject(err);
}
} else {
const contentRange = parseContentRange(data.ContentRange);
if (contentRange === undefined) {
reject(new Error(`unexpected S3 content range: ${data.ContentRange}`));
if (data.ContentLength === 0) {
resolve({metadata: {lengthInBytes: 0}, body: Buffer.alloc(0)});
} else {
const metadata = {
lengthInBytes: contentRange.length
};
if ('PartsCount' in data) {
metadata.parts = data.PartsCount;
const contentRange = parseContentRange(data.ContentRange);
if (contentRange === undefined) {
reject(new Error(`unexpected S3 content range: ${data.ContentRange}`));
} else {
const metadata = {
lengthInBytes: contentRange.length
};
if ('PartsCount' in data) {
metadata.parts = data.PartsCount;
}
resolve({metadata, body: data.Body});
}
resolve({metadata, body: data.Body});
}
}
});
Expand Down
71 changes: 71 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,42 @@ describe('index', () => {
);
});
});
it('empty file', (done) => {
nock('https://bucket.s3.eu-west-1.amazonaws.com', {
reqheaders: {
'x-amz-content-sha256': /.*/,
'x-amz-date': /.*/,
authorization: /.*/
}
})
.get('/key')
.query({
versionId: 'version',
partNumber: '1'
})
.reply(206, Buffer.alloc(0), {
'Content-Length': '0',
'Content-Type': 'text/plain'
});
mockfs({
'/tmp': {
}
});
pipeline(
download({bucket:'bucket', key: 'key', version: 'version'}, {concurrency: 4}).readStream(),
fs.createWriteStream('/tmp/test'),
(err) => {
if (err) {
done(err);
} else {
assert.ok(nock.isDone());
const {size} = fs.statSync('/tmp/test');
assert.deepStrictEqual(size, 0);
done();
}
}
);
});
it('happy', (done) => {
const bytes = 1000000;
nockPart(1000000, 1, 1, bytes);
Expand Down Expand Up @@ -645,6 +681,41 @@ describe('index', () => {
}
);
});
it('empty file', (done) => {
nock('https://bucket.s3.eu-west-1.amazonaws.com', {
reqheaders: {
range: 'bytes=0-7999999',
'x-amz-content-sha256': /.*/,
'x-amz-date': /.*/,
authorization: /.*/
}
})
.get('/key')
.query({
versionId: 'version'
})
.reply(416, '<?xml version="1.0" encoding="UTF-8"?>\n<Error><Code>InvalidRange</Code><Message>The requested range is not satisfiable</Message><RangeRequested>bytes=0-7999999</RangeRequested><ActualObjectSize>0</ActualObjectSize><RequestId>X98GTEXW2R90YZ2Y</RequestId><HostId>n/pHIOHiOaojuuH5uQ2JGqASSO3cPPCqrb1fHBJAVEdRu/XDLCR1VMwcVCUSv4DwwfKMxSY9wBQ=</HostId></Error>', {
'Content-Type': 'application/xml'
});
mockfs({
'/tmp': {
}
});
pipeline(
download({bucket:'bucket', key: 'key', version: 'version'}, {partSizeInMegabytes: 8, concurrency: 4}).readStream(),
fs.createWriteStream('/tmp/test'),
(err) => {
if (err) {
done(err);
} else {
assert.ok(nock.isDone());
const {size} = fs.statSync('/tmp/test');
assert.deepStrictEqual(size, 0);
done();
}
}
);
});
it('happy', (done) => {
const bytes = 1000000;
nockRange(0, 7999999, bytes);
Expand Down

0 comments on commit de4a336

Please sign in to comment.