This repository was archived by the owner on Jun 9, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
feat: add stream helper
#395
Merged
Merged
Changes from 5 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
ff8d224
feat!: update to node v12 (oldest supported version by AWS)
Skn0tt 0daf834
feat: add stream() helper
Skn0tt 1dccb79
fix: ci
Skn0tt f950624
feat: bump to v14
Skn0tt 07060f8
feat: export stream from entrypoint
Skn0tt eb1baed
feat: support all kinds of stream
Skn0tt 8e2caa3
feat: add web stream example to doc comment
Skn0tt 77c299e
feat: propagate headers
Skn0tt 467c69d
fix: use promise version of pipelining
Skn0tt 0b7f387
fix: support node v14
Skn0tt 61211e5
Update src/lib/stream.ts
Skn0tt d6b033b
Update src/lib/stream.ts
Skn0tt 722bc60
Merge branch 'main' into stream-helper
eduardoboucas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -66,6 +66,6 @@ | |
| "typescript": "^4.4.4" | ||
| }, | ||
| "engines": { | ||
| "node": ">=8.3.0" | ||
| "node": ">=14.0.0" | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import { pipeline } from 'stream' | ||
|
|
||
| import type { Handler, HandlerEvent, HandlerContext, HandlerResponse, StreamingHandler } from '../function/index.js' | ||
|
|
||
| declare global { | ||
| // eslint-disable-next-line @typescript-eslint/no-namespace | ||
| namespace awslambda { | ||
| function streamifyResponse( | ||
| handler: ( | ||
| event: HandlerEvent, | ||
| responseStream: NodeJS.WritableStream, | ||
| context: HandlerContext, | ||
| ) => Promise<HandlerResponse>, | ||
| ): Handler | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Enables streaming responses. | ||
| * | ||
| * @example | ||
| * ``` | ||
| * const { Readable } = require('stream'); | ||
| * | ||
| * export const handler = stream(async (event, context) => { | ||
| * const stream = Readable.from(Buffer.from(JSON.stringify(event))) | ||
| * return { | ||
| * statusCode: 200, | ||
| * body: stream, | ||
| * } | ||
| * }) | ||
| * ``` | ||
| * | ||
| * @param handler | ||
| * @see https://ntl.fyi/streaming-func | ||
| */ | ||
| const stream = (handler: StreamingHandler): Handler => | ||
| awslambda.streamifyResponse(async (event, responseStream, context) => { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the plan for making this with in local development? Should we add this to https://github.com/ashiina/lambda-local?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's probably the best way forward, yes. I think we already got one PR in there. |
||
| const { body, ...rest } = await handler(event, context) | ||
|
|
||
| if (typeof body === 'undefined' || typeof body === 'string') { | ||
| return { | ||
| body, | ||
| ...rest, | ||
| } | ||
| } | ||
|
|
||
| pipeline(body, responseStream) | ||
|
|
||
| return { | ||
| ...rest, | ||
| } | ||
| }) | ||
|
|
||
| export { stream } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| export { builder } from './lib/builder.js' | ||
| export { schedule } from './lib/schedule.js' | ||
| export { stream } from './lib/stream.js' | ||
| export * from './function/index.js' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.