This repository has been archived by the owner on Oct 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 243
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: streaming examples closes #1014
- Loading branch information
Showing
8 changed files
with
397 additions
and
182 deletions.
There are no files selected for viewing
This file contains 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,3 @@ | ||
|
||
# sst | ||
.sst |
This file contains 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,19 @@ | ||
import { APIGatewayProxyEventV2 } from "aws-lambda"; | ||
import { streamifyResponse, ResponseStream } from "lambda-stream"; | ||
|
||
export const handler = streamifyResponse(myHandler); | ||
|
||
async function myHandler( | ||
_event: APIGatewayProxyEventV2, | ||
responseStream: ResponseStream | ||
): Promise<void> { | ||
return new Promise((resolve, _reject) => { | ||
responseStream.setContentType('text/plain') | ||
responseStream.write('Hello') | ||
setTimeout(() => { | ||
responseStream.write(' World') | ||
responseStream.end() | ||
resolve() | ||
}, 3000) | ||
}) | ||
} |
This file contains 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,19 @@ | ||
{ | ||
"name": "aws-lambda-stream", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"lambda-stream": "^0.5.0", | ||
"sst": "latest" | ||
}, | ||
"devDependencies": { | ||
"@types/aws-lambda": "8.10.145" | ||
} | ||
} |
This file contains 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,14 @@ | ||
/* This file is auto-generated by SST. Do not edit. */ | ||
/* tslint:disable */ | ||
/* eslint-disable */ | ||
import "sst" | ||
export {} | ||
declare module "sst" { | ||
export interface Resource { | ||
"MyFunction": { | ||
"name": string | ||
"type": "sst.aws.Function" | ||
"url": string | ||
} | ||
} | ||
} |
This file contains 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,80 @@ | ||
/// <reference path="./.sst/platform/config.d.ts" /> | ||
|
||
/** | ||
* ## AWS Lambda streaming | ||
* | ||
* An example on how to enable streaming for Lambda functions. | ||
* | ||
* ```ts title="sst.config.ts" | ||
* { | ||
* streaming: true | ||
* } | ||
* ``` | ||
* | ||
* While `sst dev` doesn't support streaming, you can use the | ||
* [`lambda-stream`](https://github.com/astuyve/lambda-stream) package to test locally. | ||
* | ||
* ```bash | ||
* npm install lambda-stream | ||
* ``` | ||
* | ||
* Then, you can use the `streamifyResponse` function to wrap your handler: | ||
* | ||
* ```ts title="index.ts" | ||
* import { APIGatewayProxyEventV2 } from "aws-lambda"; | ||
* import { streamifyResponse, ResponseStream } from "lambda-stream"; | ||
* | ||
* export const handler = streamifyResponse(myHandler); | ||
* | ||
* async function myHandler( | ||
* _event: APIGatewayProxyEventV2, | ||
* responseStream: ResponseStream | ||
* ): Promise<void> { | ||
* return new Promise((resolve, _reject) => { | ||
* responseStream.setContentType('text/plain') | ||
* responseStream.write('Hello') | ||
* setTimeout(() => { | ||
* responseStream.write(' World') | ||
* responseStream.end() | ||
* resolve() | ||
* }, 3000) | ||
* }) | ||
* } | ||
* ``` | ||
* | ||
* When deployed, this will use the `awslambda.streamifyResponse`. | ||
* | ||
* :::note | ||
* Streaming is currently not supported in `sst dev`. | ||
* ::: | ||
* | ||
* To test this in your terminal, use the `curl` command with the `--no-buffer` option. | ||
* | ||
* ```bash "--no-buffer" | ||
* curl --no-buffer https://u3dyblk457ghskwbmzrbylpxoi0ayrbb.lambda-url.us-east-1.on.aws | ||
* ``` | ||
* | ||
* Here we are using a Function URL directly because API Gateway doesn't support streaming. | ||
* | ||
*/ | ||
export default $config({ | ||
app(input) { | ||
return { | ||
name: "aws-lambda-stream", | ||
removal: input?.stage === "production" ? "retain" : "remove", | ||
home: "aws", | ||
}; | ||
}, | ||
async run() { | ||
const fn = new sst.aws.Function("MyFunction", { | ||
url: true, | ||
streaming: true, | ||
timeout: "15 minutes", | ||
handler: "index.handler", | ||
}); | ||
|
||
return { | ||
url: fn.url, | ||
}; | ||
}, | ||
}); |
This file contains 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 @@ | ||
{} |
Oops, something went wrong.