Skip to content

Commit f9e520f

Browse files
committed
update blob-storage
Signed-off-by: karthik2804 <[email protected]>
1 parent 9f22a26 commit f9e520f

File tree

10 files changed

+88
-108
lines changed

10 files changed

+88
-108
lines changed

examples/blob-storage/backblaze/.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/
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
KNITWIT_SOURCE=./config/knitwit.json

examples/blob-storage/backblaze/README.md

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,6 @@ This example showcases how to connect to and send messages using Backblaze B2 wi
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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"version": 1,
3+
"project": {
4+
"worlds": [
5+
"spin-http"
6+
]
7+
},
8+
"packages": {}
9+
}

examples/blob-storage/backblaze/knitwit.json

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

examples/blob-storage/backblaze/package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,24 @@
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/backblaze.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/backblaze.wasm",
88
"test": "echo \"Error: no test specified\" && exit 1"
99
},
1010
"keywords": [],
1111
"author": "",
1212
"license": "ISC",
1313
"devDependencies": {
14-
"@types/uuid": "^10.0.0",
1514
"mkdirp": "^3.0.1",
1615
"ts-loader": "^9.4.1",
1716
"typescript": "^4.8.4",
1817
"webpack": "^5.74.0",
19-
"webpack-cli": "^4.10.0"
18+
"webpack-cli": "^4.10.0",
19+
"@fermyon/knitwit": "0.3.0"
2020
},
2121
"dependencies": {
22+
"@fermyon/spin-sdk": "^3.0.0-rc1",
23+
"itty-router": "^5.0.18",
2224
"@aws-sdk/client-s3": "^3.600.0",
23-
"@fermyon/spin-sdk": "^2.0.0",
2425
"uuid": "^10.0.0"
2526
}
2627
}

examples/blob-storage/backblaze/spin.toml

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

1313
[component.backblaze]
14-
source = "target/backblaze.wasm"
14+
source = "dist/backblaze.wasm"
1515
exclude_files = ["**/node_modules"]
16-
allowed_outbound_hosts = ['https://*:*']
16+
allowed_outbound_hosts = ["https://*.backblazeb2.com"]
1717
[component.backblaze.build]
18-
command = "npm run build"
19-
watch = ["src/**/*.ts", "package.json"]
18+
command = ["npm install", "npm run build"]
19+
watch = ["src/**/*.ts"]
Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,46 @@
1+
// https://itty.dev/itty-router/routers/autorouter
2+
import { AutoRouter } from 'itty-router';
13
import {
2-
CreateBucketCommand,
3-
PutObjectCommand,
4-
S3Client,
4+
CreateBucketCommand,
5+
PutObjectCommand,
6+
S3Client,
57
} from '@aws-sdk/client-s3';
6-
import { ResponseBuilder } from '@fermyon/spin-sdk';
78
import { v4 as uuid } from 'uuid';
89

910
const s3 = new S3Client({
10-
endpoint: '<Backblaze b2 endpoint>',
11-
region: '<>',
12-
credentials: {
13-
accessKeyId: '<>',
14-
secretAccessKey: '<>',
15-
},
11+
endpoint: '<Backblaze b2 endpoint>',
12+
region: '<>',
13+
credentials: {
14+
accessKeyId: '<>',
15+
secretAccessKey: '<>',
16+
},
1617
});
18+
let router = AutoRouter();
1719

18-
export async function handler(_req: Request, res: ResponseBuilder) {
19-
try {
20-
let bucketName = 'spin-sdk-bucket-' + uuid();
21-
let keyName = 'hello_world.txt';
22-
await s3.send(new CreateBucketCommand({ Bucket: bucketName }));
20+
// Route ordering matters, the first route that matches will be used
21+
// Any route that does not return will be treated as a middleware
22+
// Any unmatched route will return a 404
23+
router
24+
.get("/", async () => {
25+
try {
26+
let bucketName = 'spin-sdk-bucket-' + uuid();
27+
let keyName = 'hello_world.txt';
28+
await s3.send(new CreateBucketCommand({ Bucket: bucketName }));
2329

24-
await s3.send(
25-
new PutObjectCommand({
26-
Bucket: bucketName,
27-
Key: keyName,
28-
Body: 'Hello World!',
29-
}),
30-
);
31-
res.send(`Successfully uploaded data to ${bucketName}/${keyName}`);
32-
} catch (e: any) {
33-
res.status(500);
34-
res.send(`error: ${e}`);
35-
}
36-
}
30+
await s3.send(
31+
new PutObjectCommand({
32+
Bucket: bucketName,
33+
Key: keyName,
34+
Body: 'Hello World!',
35+
}),
36+
);
37+
return new Response(`Successfully uploaded data to ${bucketName}/${keyName}`);
38+
} catch (e: any) {
39+
return new Response(`error: ${e}`, { status: 500 });
40+
}
41+
})
42+
43+
//@ts-ignore
44+
addEventListener('fetch', async (event: FetchEvent) => {
45+
event.respondWith(router.fetch(event.request));
46+
});

examples/blob-storage/backblaze/src/spin.ts

Lines changed: 0 additions & 21 deletions
This file was deleted.
Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,35 @@
11
const path = require('path');
2-
const SpinSdkPlugin = require('@fermyon/spin-sdk/plugins/webpack');
2+
const SpinSdkPlugin = require("@fermyon/spin-sdk/plugins/webpack")
33

44
module.exports = {
5-
entry: './src/spin.ts',
6-
experiments: {
7-
outputModule: true,
8-
},
9-
module: {
10-
rules: [
11-
{
12-
test: /\.tsx?$/,
13-
use: 'ts-loader',
14-
exclude: /node_modules/,
15-
},
5+
entry: './src/index.ts',
6+
experiments: {
7+
outputModule: true,
8+
},
9+
module: {
10+
rules: [
11+
{
12+
test: /\.tsx?$/,
13+
use: 'ts-loader',
14+
exclude: /node_modules/,
15+
},
16+
],
17+
},
18+
resolve: {
19+
extensions: ['.tsx', '.ts', '.js'],
20+
},
21+
output: {
22+
path: path.resolve(__dirname, './build'),
23+
filename: 'bundle.js',
24+
module: true,
25+
library: {
26+
type: "module",
27+
}
28+
},
29+
plugins: [
30+
new SpinSdkPlugin()
1631
],
17-
},
18-
resolve: {
19-
extensions: ['.tsx', '.ts', '.js'],
20-
},
21-
output: {
22-
path: path.resolve(__dirname, './'),
23-
filename: 'dist.js',
24-
module: true,
25-
library: {
26-
type: 'module',
32+
optimization: {
33+
minimize: false
2734
},
28-
},
29-
plugins: [new SpinSdkPlugin()],
30-
optimization: {
31-
minimize: false,
32-
},
33-
};
35+
};

0 commit comments

Comments
 (0)