From cf62df3caef2ebcc1c8e4b04e59a1d8a912f05d9 Mon Sep 17 00:00:00 2001 From: karthik2804 Date: Thu, 29 Aug 2024 11:26:17 +0200 Subject: [PATCH 1/2] add templates for redis Signed-off-by: karthik2804 --- templates/redis-js/content/.gitignore | 5 +++ templates/redis-js/content/knitwit.json | 9 +++++ templates/redis-js/content/package.json | 23 ++++++++++++ templates/redis-js/content/spin.toml | 21 +++++++++++ templates/redis-js/content/src/index.js | 11 ++++++ templates/redis-js/content/webpack.config.js | 23 ++++++++++++ .../metadata/snippets/application-trigger.txt | 2 ++ .../redis-js/metadata/snippets/component.txt | 10 ++++++ .../redis-js/metadata/spin-template.toml | 19 ++++++++++ templates/redis-ts/content/.gitignore | 5 +++ templates/redis-ts/content/knitwit.json | 9 +++++ templates/redis-ts/content/package.json | 25 +++++++++++++ templates/redis-ts/content/spin.toml | 21 +++++++++++ templates/redis-ts/content/src/index.ts | 9 +++++ templates/redis-ts/content/tsconfig.json | 18 ++++++++++ templates/redis-ts/content/webpack.config.js | 35 +++++++++++++++++++ .../metadata/snippets/application-trigger.txt | 2 ++ .../redis-ts/metadata/snippets/component.txt | 10 ++++++ .../redis-ts/metadata/spin-template.toml | 19 ++++++++++ 19 files changed, 276 insertions(+) create mode 100644 templates/redis-js/content/.gitignore create mode 100644 templates/redis-js/content/knitwit.json create mode 100644 templates/redis-js/content/package.json create mode 100644 templates/redis-js/content/spin.toml create mode 100644 templates/redis-js/content/src/index.js create mode 100644 templates/redis-js/content/webpack.config.js create mode 100644 templates/redis-js/metadata/snippets/application-trigger.txt create mode 100644 templates/redis-js/metadata/snippets/component.txt create mode 100644 templates/redis-js/metadata/spin-template.toml create mode 100644 templates/redis-ts/content/.gitignore create mode 100644 templates/redis-ts/content/knitwit.json create mode 100644 templates/redis-ts/content/package.json create mode 100644 templates/redis-ts/content/spin.toml create mode 100644 templates/redis-ts/content/src/index.ts create mode 100644 templates/redis-ts/content/tsconfig.json create mode 100644 templates/redis-ts/content/webpack.config.js create mode 100644 templates/redis-ts/metadata/snippets/application-trigger.txt create mode 100644 templates/redis-ts/metadata/snippets/component.txt create mode 100644 templates/redis-ts/metadata/spin-template.toml diff --git a/templates/redis-js/content/.gitignore b/templates/redis-js/content/.gitignore new file mode 100644 index 00000000..1ba44682 --- /dev/null +++ b/templates/redis-js/content/.gitignore @@ -0,0 +1,5 @@ +node_modules +dist +target +.spin/ +dist.js \ No newline at end of file diff --git a/templates/redis-js/content/knitwit.json b/templates/redis-js/content/knitwit.json new file mode 100644 index 00000000..774a0ba3 --- /dev/null +++ b/templates/redis-js/content/knitwit.json @@ -0,0 +1,9 @@ +{ + "version": 1, + "project": { + "worlds": [ + "spin-redis" + ] + }, + "packages": {} +} \ No newline at end of file diff --git a/templates/redis-js/content/package.json b/templates/redis-js/content/package.json new file mode 100644 index 00000000..2ce5e3d5 --- /dev/null +++ b/templates/redis-js/content/package.json @@ -0,0 +1,23 @@ +{ + "name": "{{project-name | kebab_case}}", + "version": "1.0.0", + "description": "{{project-description}}", + "main": "index.js", + "scripts": { + "build": "npx webpack --mode=production && npx mkdirp target && npx j2w -i dist.js -d combined-wit -n combined -o target/{{project-name | kebab_case}}.wasm", + "test": "echo \"Error: no test specified\" && exit 1", + "postinstall": "knitwit" + }, + "keywords": [], + "author": "", + "license": "ISC", + "devDependencies": { + "mkdirp": "^3.0.1", + "webpack": "^5.74.0", + "webpack-cli": "^4.10.0", + "@fermyon/knitwit": "https://github.com/fermyon/knitwit" + }, + "dependencies": { + "@fermyon/spin-sdk": "^2.0.0" + } +} \ No newline at end of file diff --git a/templates/redis-js/content/spin.toml b/templates/redis-js/content/spin.toml new file mode 100644 index 00000000..723b5507 --- /dev/null +++ b/templates/redis-js/content/spin.toml @@ -0,0 +1,21 @@ +spin_manifest_version = 2 + +[application] +name = "{{project-name | kebab_case}}" +version = "0.1.0" +authors = ["{{authors}}"] +description = "{{project-description}}" + +[application.trigger.redis] +address = "{{redis-address}}" + +[[trigger.redis]] +channel = "{{redis-channel}}" +component = "{{project-name | kebab_case}}" + +[component.{{project-name | kebab_case}}] +source = "target/{{project-name | kebab_case}}.wasm" +exclude_files = ["**/node_modules"] +[component.{{project-name | kebab_case}}.build] +command = "npm run build" +watch = ["src/**/*.ts", "package.json"] diff --git a/templates/redis-js/content/src/index.js b/templates/redis-js/content/src/index.js new file mode 100644 index 00000000..d38c79f6 --- /dev/null +++ b/templates/redis-js/content/src/index.js @@ -0,0 +1,11 @@ +import { RedisHandler } from "@fermyon/spin-sdk"; + +const decoder = new TextDecoder(); + +export class MyRedisHandler extends RedisHandler { + async handleMessage(msg) { + console.log("Received message:", decoder.decode(msg)); + } +} + +export const inboundRedis = new MyRedisHandler(); \ No newline at end of file diff --git a/templates/redis-js/content/webpack.config.js b/templates/redis-js/content/webpack.config.js new file mode 100644 index 00000000..f9c536db --- /dev/null +++ b/templates/redis-js/content/webpack.config.js @@ -0,0 +1,23 @@ +const path = require('path'); +const SpinSdkPlugin = require("@fermyon/spin-sdk/plugins/webpack") + +module.exports = { + entry: './src/index.js', + experiments: { + outputModule: true, + }, + output: { + path: path.resolve(__dirname, './'), + filename: 'dist.js', + module: true, + library: { + type: "module", + } + }, + plugins: [ + new SpinSdkPlugin() + ], + optimization: { + minimize: false + }, +}; \ No newline at end of file diff --git a/templates/redis-js/metadata/snippets/application-trigger.txt b/templates/redis-js/metadata/snippets/application-trigger.txt new file mode 100644 index 00000000..aea47909 --- /dev/null +++ b/templates/redis-js/metadata/snippets/application-trigger.txt @@ -0,0 +1,2 @@ +[application.trigger.redis] +address = "{{redis-address}}" \ No newline at end of file diff --git a/templates/redis-js/metadata/snippets/component.txt b/templates/redis-js/metadata/snippets/component.txt new file mode 100644 index 00000000..a70eaa0d --- /dev/null +++ b/templates/redis-js/metadata/snippets/component.txt @@ -0,0 +1,10 @@ +[[trigger.redis]] +channel = "{{redis-channel}}" +component = "{{project-name | kebab_case}}" + +[component.{{project-name | kebab_case}}] +source = "{{ output-path }}/target/{{project-name | kebab_case}}.wasm" +allowed_outbound_hosts = [] +[component.{{project-name | kebab_case}}.build] +command = "npm run build" +workdir = "{{ output-path }}" \ No newline at end of file diff --git a/templates/redis-js/metadata/spin-template.toml b/templates/redis-js/metadata/spin-template.toml new file mode 100644 index 00000000..365e0534 --- /dev/null +++ b/templates/redis-js/metadata/spin-template.toml @@ -0,0 +1,19 @@ +manifest_version = "1" +id = "redis-js" +description = "Redis message handler using JavaScript" +tags = ["redis", "js"] + +[add_component] +skip_files = ["spin.toml"] +[add_component.snippets] +component = "component.txt" +application_trigger = "application-trigger.txt" +[add_component.conditions.address_exists] +condition = { manifest_entry_exists = "application.trigger.redis" } +skip_parameters = ["redis-address"] +skip_snippets = ["application_trigger"] + +[parameters] +project-description = { type = "string", prompt = "Description", default = "" } +redis-address = { type = "string", prompt = "Redis address", default = "redis://localhost:6379" } +redis-channel = { type = "string", prompt = "Redis channel" } \ No newline at end of file diff --git a/templates/redis-ts/content/.gitignore b/templates/redis-ts/content/.gitignore new file mode 100644 index 00000000..1ba44682 --- /dev/null +++ b/templates/redis-ts/content/.gitignore @@ -0,0 +1,5 @@ +node_modules +dist +target +.spin/ +dist.js \ No newline at end of file diff --git a/templates/redis-ts/content/knitwit.json b/templates/redis-ts/content/knitwit.json new file mode 100644 index 00000000..774a0ba3 --- /dev/null +++ b/templates/redis-ts/content/knitwit.json @@ -0,0 +1,9 @@ +{ + "version": 1, + "project": { + "worlds": [ + "spin-redis" + ] + }, + "packages": {} +} \ No newline at end of file diff --git a/templates/redis-ts/content/package.json b/templates/redis-ts/content/package.json new file mode 100644 index 00000000..ef11751a --- /dev/null +++ b/templates/redis-ts/content/package.json @@ -0,0 +1,25 @@ +{ + "name": "{{project-name | kebab_case}}", + "version": "1.0.0", + "description": "{{project-description}}", + "main": "index.js", + "scripts": { + "build": "npx webpack --mode=production && npx mkdirp target && npx j2w -i dist.js -d combined-wit -n combined -o target/{{project-name | kebab_case}}.wasm", + "test": "echo \"Error: no test specified\" && exit 1", + "postinstall": "knitwit" + }, + "keywords": [], + "author": "", + "license": "ISC", + "devDependencies": { + "mkdirp": "^3.0.1", + "ts-loader": "^9.4.1", + "typescript": "^4.8.4", + "webpack": "^5.74.0", + "webpack-cli": "^4.10.0", + "@fermyon/knitwit": "https://github.com/fermyon/knitwit" + }, + "dependencies": { + "@fermyon/spin-sdk": "^2.0.0" + } +} \ No newline at end of file diff --git a/templates/redis-ts/content/spin.toml b/templates/redis-ts/content/spin.toml new file mode 100644 index 00000000..723b5507 --- /dev/null +++ b/templates/redis-ts/content/spin.toml @@ -0,0 +1,21 @@ +spin_manifest_version = 2 + +[application] +name = "{{project-name | kebab_case}}" +version = "0.1.0" +authors = ["{{authors}}"] +description = "{{project-description}}" + +[application.trigger.redis] +address = "{{redis-address}}" + +[[trigger.redis]] +channel = "{{redis-channel}}" +component = "{{project-name | kebab_case}}" + +[component.{{project-name | kebab_case}}] +source = "target/{{project-name | kebab_case}}.wasm" +exclude_files = ["**/node_modules"] +[component.{{project-name | kebab_case}}.build] +command = "npm run build" +watch = ["src/**/*.ts", "package.json"] diff --git a/templates/redis-ts/content/src/index.ts b/templates/redis-ts/content/src/index.ts new file mode 100644 index 00000000..8a78fa0f --- /dev/null +++ b/templates/redis-ts/content/src/index.ts @@ -0,0 +1,9 @@ +import { RedisHandler } from "@fermyon/spin-sdk"; + +export class MyRedisHandler extends RedisHandler { + async handleMessage(msg: Uint8Array): Promise { + console.log("Received message:", msg); + } +} + +export const inboundRedis = new MyRedisHandler() \ No newline at end of file diff --git a/templates/redis-ts/content/tsconfig.json b/templates/redis-ts/content/tsconfig.json new file mode 100644 index 00000000..851fe99f --- /dev/null +++ b/templates/redis-ts/content/tsconfig.json @@ -0,0 +1,18 @@ +{ + "compilerOptions": { + "outDir": "./dist/", + "noImplicitAny": true, + "module": "es6", + "target": "es2020", + "jsx": "react", + "skipLibCheck": true, + "lib": [ + "ES2015", + "WebWorker" + ], + "allowJs": true, + "strict": true, + "noImplicitReturns": true, + "moduleResolution": "node" + } +} \ No newline at end of file diff --git a/templates/redis-ts/content/webpack.config.js b/templates/redis-ts/content/webpack.config.js new file mode 100644 index 00000000..80e4bdb9 --- /dev/null +++ b/templates/redis-ts/content/webpack.config.js @@ -0,0 +1,35 @@ +const path = require('path'); +const SpinSdkPlugin = require("@fermyon/spin-sdk/plugins/webpack") + +module.exports = { + entry: './src/index.ts', + experiments: { + outputModule: true, + }, + module: { + rules: [ + { + test: /\.tsx?$/, + use: 'ts-loader', + exclude: /node_modules/, + }, + ], + }, + resolve: { + extensions: ['.tsx', '.ts', '.js'], + }, + output: { + path: path.resolve(__dirname, './'), + filename: 'dist.js', + module: true, + library: { + type: "module", + } + }, + plugins: [ + new SpinSdkPlugin() + ], + optimization: { + minimize: false + }, +}; \ No newline at end of file diff --git a/templates/redis-ts/metadata/snippets/application-trigger.txt b/templates/redis-ts/metadata/snippets/application-trigger.txt new file mode 100644 index 00000000..aea47909 --- /dev/null +++ b/templates/redis-ts/metadata/snippets/application-trigger.txt @@ -0,0 +1,2 @@ +[application.trigger.redis] +address = "{{redis-address}}" \ No newline at end of file diff --git a/templates/redis-ts/metadata/snippets/component.txt b/templates/redis-ts/metadata/snippets/component.txt new file mode 100644 index 00000000..a70eaa0d --- /dev/null +++ b/templates/redis-ts/metadata/snippets/component.txt @@ -0,0 +1,10 @@ +[[trigger.redis]] +channel = "{{redis-channel}}" +component = "{{project-name | kebab_case}}" + +[component.{{project-name | kebab_case}}] +source = "{{ output-path }}/target/{{project-name | kebab_case}}.wasm" +allowed_outbound_hosts = [] +[component.{{project-name | kebab_case}}.build] +command = "npm run build" +workdir = "{{ output-path }}" \ No newline at end of file diff --git a/templates/redis-ts/metadata/spin-template.toml b/templates/redis-ts/metadata/spin-template.toml new file mode 100644 index 00000000..734a22f6 --- /dev/null +++ b/templates/redis-ts/metadata/spin-template.toml @@ -0,0 +1,19 @@ +manifest_version = "1" +id = "redis-ts" +description = "Redis message handler using TypeScript" +tags = ["redis", "ts"] + +[add_component] +skip_files = ["spin.toml"] +[add_component.snippets] +component = "component.txt" +application_trigger = "application-trigger.txt" +[add_component.conditions.address_exists] +condition = { manifest_entry_exists = "application.trigger.redis" } +skip_parameters = ["redis-address"] +skip_snippets = ["application_trigger"] + +[parameters] +project-description = { type = "string", prompt = "Description", default = "" } +redis-address = { type = "string", prompt = "Redis address", default = "redis://localhost:6379" } +redis-channel = { type = "string", prompt = "Redis channel" } \ No newline at end of file From 69156981d789c2f7225fefc20b88a3140e95816f Mon Sep 17 00:00:00 2001 From: karthik2804 Date: Tue, 3 Sep 2024 10:08:36 +0200 Subject: [PATCH 2/2] bump spin_sdk dep Signed-off-by: karthik2804 --- templates/http-js/content/package.json | 2 +- templates/http-ts/content/package.json | 2 +- templates/redis-js/content/package.json | 2 +- templates/redis-ts/content/package.json | 2 +- templates/redis-ts/content/src/index.ts | 4 +++- 5 files changed, 7 insertions(+), 5 deletions(-) diff --git a/templates/http-js/content/package.json b/templates/http-js/content/package.json index 2ce5e3d5..d3e0012c 100644 --- a/templates/http-js/content/package.json +++ b/templates/http-js/content/package.json @@ -18,6 +18,6 @@ "@fermyon/knitwit": "https://github.com/fermyon/knitwit" }, "dependencies": { - "@fermyon/spin-sdk": "^2.0.0" + "@fermyon/spin-sdk": "^2.1.0" } } \ No newline at end of file diff --git a/templates/http-ts/content/package.json b/templates/http-ts/content/package.json index ef11751a..97d686cd 100644 --- a/templates/http-ts/content/package.json +++ b/templates/http-ts/content/package.json @@ -20,6 +20,6 @@ "@fermyon/knitwit": "https://github.com/fermyon/knitwit" }, "dependencies": { - "@fermyon/spin-sdk": "^2.0.0" + "@fermyon/spin-sdk": "^2.1.0" } } \ No newline at end of file diff --git a/templates/redis-js/content/package.json b/templates/redis-js/content/package.json index 2ce5e3d5..d3e0012c 100644 --- a/templates/redis-js/content/package.json +++ b/templates/redis-js/content/package.json @@ -18,6 +18,6 @@ "@fermyon/knitwit": "https://github.com/fermyon/knitwit" }, "dependencies": { - "@fermyon/spin-sdk": "^2.0.0" + "@fermyon/spin-sdk": "^2.1.0" } } \ No newline at end of file diff --git a/templates/redis-ts/content/package.json b/templates/redis-ts/content/package.json index ef11751a..97d686cd 100644 --- a/templates/redis-ts/content/package.json +++ b/templates/redis-ts/content/package.json @@ -20,6 +20,6 @@ "@fermyon/knitwit": "https://github.com/fermyon/knitwit" }, "dependencies": { - "@fermyon/spin-sdk": "^2.0.0" + "@fermyon/spin-sdk": "^2.1.0" } } \ No newline at end of file diff --git a/templates/redis-ts/content/src/index.ts b/templates/redis-ts/content/src/index.ts index 8a78fa0f..c4242a7f 100644 --- a/templates/redis-ts/content/src/index.ts +++ b/templates/redis-ts/content/src/index.ts @@ -1,8 +1,10 @@ import { RedisHandler } from "@fermyon/spin-sdk"; +const decoder = new TextDecoder(); + export class MyRedisHandler extends RedisHandler { async handleMessage(msg: Uint8Array): Promise { - console.log("Received message:", msg); + console.log("Received message:", decoder.decode(msg)); } }