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

Feat/content type #5

Merged
merged 4 commits into from
May 23, 2023
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
2 changes: 2 additions & 0 deletions dist/ResponseStream.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import { Stream } from 'stream';
export declare class ResponseStream extends Stream.Writable {
private response;
_contentType?: string;
_isBase64Encoded?: boolean;
constructor();
_write(chunk: any, encoding: BufferEncoding, callback: (error?: Error | null) => void): void;
getBufferedData(): Buffer;
setContentType(contentType: string): void;
setIsBase64Encoded(isBase64Encoded: boolean): void;
}
//# sourceMappingURL=ResponseStream.d.ts.map
2 changes: 1 addition & 1 deletion dist/ResponseStream.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions dist/ResponseStream.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/ResponseStream.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions examples/src/binary-base64-handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { APIGatewayProxyEvent } from 'aws-lambda'
import { ResponseStream, streamifyResponse } from 'lambda-stream'
import { Readable } from 'stream'
import { pipeline } from 'stream/promises'

export const binaryBase64Handler = streamifyResponse(myHandler)

async function myHandler(
event: APIGatewayProxyEvent,
responseStream: ResponseStream
): Promise<void> {
const source = Readable.from(Buffer.from('hello world'))
responseStream.setContentType('binary/octet-stream')
responseStream.setIsBase64Encoded(true)
await pipeline(source, responseStream)
}
5 changes: 5 additions & 0 deletions src/ResponseStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Stream } from 'stream'
export class ResponseStream extends Stream.Writable {
private response: Buffer[]
_contentType?: string
_isBase64Encoded?: boolean

constructor() {
super()
Expand All @@ -26,4 +27,8 @@ export class ResponseStream extends Stream.Writable {
setContentType(contentType: string) {
this._contentType = contentType
}

setIsBase64Encoded(isBase64Encoded: boolean) {
this._isBase64Encoded = isBase64Encoded
}
}
14 changes: 12 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,18 @@ export function streamifyResponse(handler: Function): Function {
apply: async function (target, _, argList) {
const responseStream: ResponseStream = patchArgs(argList)
await target(...argList)
// Todo - honor content type
return responseStream.getBufferedData().toString()
return {
statusCode: 200,
headers: {
'content-type': responseStream._contentType || 'application/json',
},
...(responseStream._isBase64Encoded
? { isBase64Encoded: responseStream._isBase64Encoded }
: {}),
body: responseStream._isBase64Encoded
? responseStream.getBufferedData().toString('base64')
: responseStream.getBufferedData().toString(),
}
},
})
}
Expand Down
9 changes: 9 additions & 0 deletions test/binary-pipeline.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { binaryBase64Handler } from '../examples/src/binary-base64-handler'

describe('simple handler', () => {
it('awaits and returns the response', async () => {
const resp = await binaryBase64Handler('hello')
const decodedMessage = Buffer.from(resp.body, 'base64').toString()
expect(decodedMessage).toEqual('hello world')
})
})
2 changes: 1 addition & 1 deletion test/pipeline.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const { gzip } = require('../examples/example.js')
describe('simple handler', () => {
it('awaits and returns the response', async () => {
const resp = await gzip('hello')
const parsed = JSON.parse(resp)
const parsed = JSON.parse(resp.body)
expect(parsed).toMatchObject({ hello: 'world' })
})
})
2 changes: 1 addition & 1 deletion test/simpleHandler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import { handler } from '../examples/src/handler'
describe('simple handler', () => {
it('awaits and returns the response', async () => {
const resp = await handler('hello')
expect(resp).toEqual('Hello, world!')
expect(resp.body).toEqual('Hello, world!')
})
})