Skip to content

Commit

Permalink
feat: add in zstd support
Browse files Browse the repository at this point in the history
  • Loading branch information
willfarrell committed Sep 1, 2024
1 parent f86f9ee commit df1ca12
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 4 deletions.
56 changes: 56 additions & 0 deletions packages/http-content-encoding/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,69 @@ import httpContentEncoding from '../index.js'

import { createReadableStream, streamToBuffer } from '@datastream/core'
import { brotliCompressSync, gzipSync, deflateSync } from 'node:zlib'
import ZstdStream from 'zstd-codec/lib/zstd-stream.js'

const zstdAsync = async (data) => {
const init = new Promise((resolve) => {
ZstdStream.run((streams) => {
resolve(streams.ZstdCompressTransform)
})
})

const ZstdCompressStream = await init
return streamToBuffer(
createReadableStream(data).pipe(new ZstdCompressStream())
)
}

const context = {
getRemainingTimeInMillis: () => 1000
}

const compressibleBody = JSON.stringify(new Array(100).fill(0))

test('It should encode string using zstd', async (t) => {
const body = compressibleBody
const handler = middy((event, context) => ({ statusCode: 200, body })).use(
httpContentEncoding()
)

const event = {}

const response = await handler(event, {
...context,
preferredEncoding: 'zstd'
})
deepEqual(response, {
statusCode: 200,
body: (await zstdAsync(body)).toString('base64'),
headers: { 'Content-Encoding': 'zstd' },
isBase64Encoded: true
})
})

test('It should encode stream using br', async (t) => {
const body = compressibleBody
const handler = middy((event, context) => ({
statusCode: 200,
body: createReadableStream(body)
})).use(httpContentEncoding())

const event = {}

const response = await handler(event, {
...context,
preferredEncoding: 'zstd'
})
response.body = await streamToBuffer(response.body)
response.body = response.body.toString('base64')
deepEqual(response, {
statusCode: 200,
body: (await zstdAsync(body)).toString('base64'),
headers: { 'Content-Encoding': 'zstd' }
})
})

test('It should encode string using br', async (t) => {
const body = compressibleBody
const handler = middy((event, context) => ({ statusCode: 200, body })).use(
Expand Down
16 changes: 13 additions & 3 deletions packages/http-content-encoding/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,28 @@ import {
createGzip as gzipCompressStream,
createDeflate as deflateCompressStream
} from 'node:zlib'

import ZstdStream from 'zstd-codec/lib/zstd-stream.js'
import { normalizeHttpResponse } from '@middy/util'

const contentEncodingStreams = {
br: brotliCompressStream,
zstd: async (options) => {
const init = new Promise((resolve) => {
ZstdStream.run((streams) => {
resolve(streams.ZstdCompressTransform)
})
})

const ZstdCompressStream = await init
return new ZstdCompressStream(options)
},
gzip: gzipCompressStream,
deflate: deflateCompressStream
}

const defaults = {
br: undefined,
// zstd: undefined,
zstd: undefined, // compression_level
gzip: undefined,
deflate: undefined,
overridePreferredEncoding: []
Expand Down Expand Up @@ -53,7 +63,7 @@ const httpContentEncodingMiddleware = (opts) => {
return
}

let contentEncodingStream = contentEncodingStreams[preferredEncoding](
let contentEncodingStream = await contentEncodingStreams[preferredEncoding](
options[preferredEncoding]
)
let contentEncoding = preferredEncoding
Expand Down
3 changes: 2 additions & 1 deletion packages/http-content-encoding/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@
"url": "https://github.com/sponsors/willfarrell"
},
"dependencies": {
"@middy/util": "5.4.7"
"@middy/util": "5.4.7",
"zstd-codec": "0.1.5"
},
"devDependencies": {
"@datastream/core": "0.0.38",
Expand Down

0 comments on commit df1ca12

Please sign in to comment.