Skip to content

Commit

Permalink
feat: fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
astuyve committed May 3, 2023
1 parent 2d1a7ce commit 143e567
Show file tree
Hide file tree
Showing 15 changed files with 297 additions and 173 deletions.
44 changes: 23 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,65 +7,67 @@ In AWS Lambda, it'll simply use the global `awslambda.streamifyResponse`.
This library exposes a `ResponseStream` class, the `streamifyResponse` method, and `isInAWS` method. Types are also included.

## Why?

AWS added Lambda Response Streaming directly into the NodeJS runtime as a global method, instead of providing it with the AWS-SDK.

This makes functions leveraging Lambda Response Streaming difficult to test, run locally, or even write Lambda functions using a typical editor. Magic, runtime-only methods are a drag on developer experience!

So I wrote this library to fill that gap

## End of Life

This library shouldn't exist.

If or when AWS decides to release and support a library which makes AWS Lambda Streaming Responses usable and testable locally, I'll happily archive this project.


## Examples

Works like this:

```typescript
import { APIGatewayProxyEvent } from "aws-lambda";
import { APIGatewayProxyEvent } from 'aws-lambda'
import { streamifyResponse, ResponseStream } from 'lambda-stream'

export const handler = streamifyResponse(myHandler)

async function myHandler (event: APIGatewayProxyEvent, responseStream: ResponseStream): Promise<void> {
console.log('Handler got event:', event)
responseStream.setContentType("text/plain");
responseStream.write("Hello, world!");
responseStream.end();
async function myHandler(
event: APIGatewayProxyEvent,
responseStream: ResponseStream
): Promise<void> {
console.log('Handler got event:', event)
responseStream.setContentType('text/plain')
responseStream.write('Hello, world!')
responseStream.end()
}
```

Or in commonjs:

```javascript
'use strict';
'use strict'
const { streamifyResponse } = require('lambda-stream')

module.exports.hello = streamifyResponse(
async (event, responseStream, context) => {
responseStream.setContentType("text/plain");
responseStream.write("Hello, world!");
responseStream.end();
}
);
async (event, responseStream, context) => {
responseStream.setContentType('text/plain')
responseStream.write('Hello, world!')
responseStream.end()
}
)
```


Pipelining is also supported:

```javascript
const pipeline = require("util").promisify(require("stream").pipeline);
const { Readable } = require('stream');
const pipeline = require('util').promisify(require('stream').pipeline)
const { Readable } = require('stream')
const { streamifyResponse } = require('lambda-stream')


const handler = async (event, responseStream, _context) => {
// As an example, convert event to a readable stream.
requestStream = Readable.from(Buffer.from(JSON.stringify({hello: 'world'})));
// As an example, convert event to a readable stream.
requestStream = Readable.from(Buffer.from(JSON.stringify({ hello: 'world' })))

await pipeline(requestStream, zlib.createGzip(), responseStream);
await pipeline(requestStream, zlib.createGzip(), responseStream)
}

module.exports.gzip = streamifyResponse(handler)
Expand Down
20 changes: 12 additions & 8 deletions dist/ResponseStream.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
/// <reference types="node" />
import { Stream } from 'stream';
import { Stream } from 'stream'
export declare class ResponseStream extends Stream.Writable {
private response;
_contentType?: string;
constructor();
_write(chunk: any, encoding: BufferEncoding, callback: (error?: Error | null) => void): void;
getBufferedData(): Buffer;
setContentType(contentType: string): void;
private response
_contentType?: string
constructor()
_write(
chunk: any,
encoding: BufferEncoding,
callback: (error?: Error | null) => void
): void
getBufferedData(): Buffer
setContentType(contentType: string): void
}
//# sourceMappingURL=ResponseStream.d.ts.map
//# sourceMappingURL=ResponseStream.d.ts.map
44 changes: 22 additions & 22 deletions dist/ResponseStream.js

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

12 changes: 6 additions & 6 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export declare const HANDLER_STREAMING: unique symbol;
export declare const STREAM_RESPONSE = "response";
export declare function isInAWS(handler: any): boolean;
export declare function streamifyResponse(handler: Function): Function;
export { ResponseStream } from './ResponseStream';
//# sourceMappingURL=index.d.ts.map
export declare const HANDLER_STREAMING: unique symbol
export declare const STREAM_RESPONSE = 'response'
export declare function isInAWS(handler: any): boolean
export declare function streamifyResponse(handler: Function): Function
export { ResponseStream } from './ResponseStream'
//# sourceMappingURL=index.d.ts.map
78 changes: 45 additions & 33 deletions dist/index.js

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

4 changes: 2 additions & 2 deletions examples/dist/handler.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export declare const handler: Function;
//# sourceMappingURL=handler.d.ts.map
export declare const handler: Function
//# sourceMappingURL=handler.d.ts.map
Loading

0 comments on commit 143e567

Please sign in to comment.