Skip to content

Commit cb6a784

Browse files
authored
(WIP) Update templates/examples for SDK v3 (#319)
* bump templates to use v3.0.0-rc1 and depend on itty router directly * update common pattern examples * update github examples * update upstash examples * update drizzle example and update tsconfig to be ES2020 * fix outbound url for upstash * update aws examples * update blob-storage * update serverlessdb examples * update spin host api examples * update spin SDK package and checkin package-lock for examples * address review comments Signed-off-by: karthik2804 <[email protected]>
1 parent d0e9dbe commit cb6a784

File tree

221 files changed

+76469
-1671
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

221 files changed

+76469
-1671
lines changed

examples/aws/s3/.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ node_modules
22
dist
33
target
44
.spin/
5-
dist.js
5+
build/

examples/aws/s3/.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
KNITWIT_SOURCE=./config/knitwit.json

examples/aws/s3/README.md

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,6 @@ This example showcases how to connect to and send messages using Amazon S3 with
44

55
## Prerequisites
66
- `spin >=2.6.0`
7-
-
8-
9-
## Install Dependencies
10-
Install the necessary npm packages:
11-
12-
```bash
13-
npm install
14-
```
157

168
## Setup the Example
179

@@ -30,18 +22,15 @@ npm install
3022

3123
```typescript
3224
const client = new S3Client({
33-
region: "<>",
25+
region: "us-west-2",
3426
credentials: {
35-
accessKeyId: "<>>",
27+
accessKeyId: "<>",
3628
secretAccessKey: "<>",
3729
sessionToken: "<>"
3830
},
3931
});
40-
41-
const params = {
42-
Bucket: "<>"
43-
};
4432
```
33+
*note*: The example assumes that the bucket is in the `us-west-2` region. If the object is in some other region modify the configuration of the client and also edit the `allowed_outbound_hosts` in the `spin.toml`.
4534

4635
## Building and Running the Example
4736

@@ -50,4 +39,7 @@ spin build
5039
spin up
5140
```
5241

53-
Use e.g. `curl -v http://127.0.0.1:3000/` to test the endpoint.
42+
### Testing the different endpoints
43+
44+
- `curl -v http://127.0.0.1:3000/list/<bucket name>` to list the objects stored in the given bucket.
45+
- `curl -v http://127.0.0.1:3000/stream/<bucket name>/<object key>` to stream the object in the given bucket.
File renamed without changes.

examples/aws/s3/package-lock.json

Lines changed: 5302 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/aws/s3/package.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "",
55
"main": "index.js",
66
"scripts": {
7-
"build": "npx webpack --mode=production && npx mkdirp target && npx j2w -i dist.js -n spin-http -o target/s3.wasm",
7+
"build": "knitwit --out-dir build/wit/knitwit --out-world combined && npx webpack --mode=production && npx mkdirp dist && npx j2w -i build/bundle.js -d build/wit/knitwit -n combined -o dist/s3.wasm",
88
"test": "echo \"Error: no test specified\" && exit 1"
99
},
1010
"keywords": [],
@@ -15,10 +15,12 @@
1515
"ts-loader": "^9.4.1",
1616
"typescript": "^4.8.4",
1717
"webpack": "^5.74.0",
18-
"webpack-cli": "^4.10.0"
18+
"webpack-cli": "^4.10.0",
19+
"@fermyon/knitwit": "0.3.0"
1920
},
2021
"dependencies": {
21-
"@aws-sdk/client-s3": "^3.600.0",
22-
"@fermyon/spin-sdk": "^2.0.0"
22+
"@fermyon/spin-sdk": "^3.0.0",
23+
"itty-router": "^5.0.18",
24+
"@aws-sdk/client-s3": "^3.600.0"
2325
}
2426
}

examples/aws/s3/spin.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ route = "/..."
1111
component = "s3"
1212

1313
[component.s3]
14-
source = "target/s3.wasm"
14+
source = "dist/s3.wasm"
1515
exclude_files = ["**/node_modules"]
16-
allowed_outbound_hosts = ["https://*:*"]
16+
allowed_outbound_hosts = ["https://*.s3.us-west-2.amazonaws.com"]
1717
[component.s3.build]
18-
command = "npm run build"
19-
watch = ["src/**/*.ts", "package.json"]
18+
command = ["npm install", "npm run build"]
19+
watch = ["src/**/*.ts"]

examples/aws/s3/src/index.ts

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,43 @@
1-
import { ResponseBuilder } from '@fermyon/spin-sdk';
2-
import { S3Client, ListObjectsV2Command } from '@aws-sdk/client-s3';
1+
// https://itty.dev/itty-router/routers/autorouter
2+
import { AutoRouter } from 'itty-router';
3+
import { S3Client, ListObjectsV2Command, GetObjectCommand } from '@aws-sdk/client-s3';
34

45
const client = new S3Client({
5-
region: '<>',
6-
credentials: {
7-
accessKeyId: '<>>',
8-
secretAccessKey: '<>',
9-
sessionToken: '<>',
10-
},
6+
region: 'us-west-2',
7+
credentials: {
8+
accessKeyId: '<>',
9+
secretAccessKey: '<>',
10+
sessionToken: '<>',
11+
},
1112
});
13+
let router = AutoRouter();
1214

13-
const params = {
14-
Bucket: '<>',
15-
};
15+
// Route ordering matters, the first route that matches will be used
16+
// Any route that does not return will be treated as a middleware
17+
// Any unmatched route will return a 404
18+
router
19+
.get("/list/:bucket", async ({ bucket }) => {
20+
let command = new ListObjectsV2Command({ Bucket: bucket });
21+
try {
22+
let data = await client.send(command);
23+
return new Response(JSON.stringify(data.Contents, null, 2));
24+
} catch (e: any) {
25+
return new Response(`error : ${e.message}`, { status: 500 });
26+
}
27+
})
28+
.get("/stream/:bucket/:file", async ({ bucket, file }) => {
29+
let command = new GetObjectCommand({ Bucket: bucket, Key: file });
30+
try {
31+
const data = await client.send(command);
32+
return new Response(data.Body as ReadableStream, {
33+
status: 200,
34+
});
35+
} catch (e: any) {
36+
return new Response(`error : ${e.message}`, { status: 500 });
37+
}
38+
})
1639

17-
export async function handler(_req: Request, res: ResponseBuilder) {
18-
const command = new ListObjectsV2Command(params);
19-
try {
20-
let data = await client.send(command);
21-
res.send(JSON.stringify(data.Contents, null, 2));
22-
} catch (e: any) {
23-
res.status(500);
24-
res.send(`error : ${e.message}`);
25-
}
26-
}
40+
//@ts-ignore
41+
addEventListener('fetch', async (event: FetchEvent) => {
42+
event.respondWith(router.fetch(event.request));
43+
});

examples/aws/s3/src/spin.ts

Lines changed: 0 additions & 19 deletions
This file was deleted.

examples/aws/s3/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"jsx": "react",
88
"skipLibCheck": true,
99
"lib": [
10-
"ES2015",
10+
"ES2020",
1111
"WebWorker"
1212
],
1313
"allowJs": true,

0 commit comments

Comments
 (0)