Skip to content
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
5 changes: 3 additions & 2 deletions packages/helix-shared-storage/src/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,9 @@ class Bucket {
*/
async store(key, res) {
const { log } = this;
const body = await res.buffer();
const zipped = await gzip(body);
const buffer = await res.buffer();
const contentEncoding = res.headers.get('content-encoding');
const zipped = contentEncoding === 'gzip' ? buffer : await gzip(buffer);

const input = {
Body: zipped,
Expand Down
49 changes: 49 additions & 0 deletions packages/helix-shared-storage/test/storage.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,55 @@ describe('Storage test', () => {
assert.deepEqual(reqs.r2, req);
});

it('can store gzipped object', async () => {
const reqs = { s3: {}, r2: {} };
nock('https://helix-code-bus.s3.fake.amazonaws.com')
.put('/foo?x-id=PutObject')
.reply(function cb(uri) {
reqs.s3[uri] = {
body: Buffer.concat(this.req.requestBodyBuffers),
headers: Object.fromEntries(Object.entries(this.req.headers)
.filter(([key]) => TEST_HEADERS.indexOf(key) >= 0)),
};
return [201];
});
nock(`https://helix-code-bus.${CLOUDFLARE_ACCOUNT_ID}.r2.cloudflarestorage.com`)
.put('/foo?x-id=PutObject')
.reply(function cb(uri) {
reqs.r2[uri] = {
body: Buffer.concat(this.req.requestBodyBuffers),
headers: Object.fromEntries(Object.entries(this.req.headers)
.filter(([key]) => TEST_HEADERS.indexOf(key) >= 0)),
};
return [201];
});

const bus = storage.codeBus();

const zipped = await gzip('hello, world.');
const data = new Response(zipped, {
headers: {
'content-type': 'text/plain',
'content-encoding': 'gzip',
myid: '1234',
},
});
await bus.store('/foo', data);

const req = {
'/foo?x-id=PutObject': {
body: await gzip(Buffer.from('hello, world.', 'utf-8')),
headers: {
'content-encoding': 'gzip',
'content-type': 'text/plain',
'x-amz-meta-myid': '1234',
},
},
};
assert.deepEqual(reqs.s3, req);
assert.deepEqual(reqs.r2, req);
});

it('can remove object', async () => {
const reqs = { s3: {}, r2: {} };
nock('https://helix-code-bus.s3.fake.amazonaws.com')
Expand Down