Added draft code for the Worker
Signed-off-by: ItalyPaleAle <43508+ItalyPaleAle@users.noreply.github.com>
This commit is contained in:
parent
20285ba6f9
commit
30750ab058
|
@ -0,0 +1,237 @@
|
|||
# Created by https://www.toptal.com/developers/gitignore/api/node,linux,windows,macos,visualstudiocode
|
||||
# Edit at https://www.toptal.com/developers/gitignore?templates=node,linux,windows,macos,visualstudiocode
|
||||
|
||||
### Linux ###
|
||||
*~
|
||||
|
||||
# temporary files which can be created if a process still has a handle open of a deleted file
|
||||
.fuse_hidden*
|
||||
|
||||
# KDE directory preferences
|
||||
.directory
|
||||
|
||||
# Linux trash folder which might appear on any partition or disk
|
||||
.Trash-*
|
||||
|
||||
# .nfs files are created when an open file is removed but is still being accessed
|
||||
.nfs*
|
||||
|
||||
### macOS ###
|
||||
# General
|
||||
.DS_Store
|
||||
.AppleDouble
|
||||
.LSOverride
|
||||
|
||||
# Icon must end with two \r
|
||||
Icon
|
||||
|
||||
|
||||
# Thumbnails
|
||||
._*
|
||||
|
||||
# Files that might appear in the root of a volume
|
||||
.DocumentRevisions-V100
|
||||
.fseventsd
|
||||
.Spotlight-V100
|
||||
.TemporaryItems
|
||||
.Trashes
|
||||
.VolumeIcon.icns
|
||||
.com.apple.timemachine.donotpresent
|
||||
|
||||
# Directories potentially created on remote AFP share
|
||||
.AppleDB
|
||||
.AppleDesktop
|
||||
Network Trash Folder
|
||||
Temporary Items
|
||||
.apdisk
|
||||
|
||||
### macOS Patch ###
|
||||
# iCloud generated files
|
||||
*.icloud
|
||||
|
||||
### Node ###
|
||||
# Logs
|
||||
logs
|
||||
*.log
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
lerna-debug.log*
|
||||
.pnpm-debug.log*
|
||||
|
||||
# Diagnostic reports (https://nodejs.org/api/report.html)
|
||||
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
|
||||
|
||||
# Runtime data
|
||||
pids
|
||||
*.pid
|
||||
*.seed
|
||||
*.pid.lock
|
||||
|
||||
# Directory for instrumented libs generated by jscoverage/JSCover
|
||||
lib-cov
|
||||
|
||||
# Coverage directory used by tools like istanbul
|
||||
coverage
|
||||
*.lcov
|
||||
|
||||
# nyc test coverage
|
||||
.nyc_output
|
||||
|
||||
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
|
||||
.grunt
|
||||
|
||||
# Bower dependency directory (https://bower.io/)
|
||||
bower_components
|
||||
|
||||
# node-waf configuration
|
||||
.lock-wscript
|
||||
|
||||
# Compiled binary addons (https://nodejs.org/api/addons.html)
|
||||
build/Release
|
||||
|
||||
# Dependency directories
|
||||
node_modules/
|
||||
jspm_packages/
|
||||
|
||||
# Snowpack dependency directory (https://snowpack.dev/)
|
||||
web_modules/
|
||||
|
||||
# TypeScript cache
|
||||
*.tsbuildinfo
|
||||
|
||||
# Optional npm cache directory
|
||||
.npm
|
||||
|
||||
# Optional eslint cache
|
||||
.eslintcache
|
||||
|
||||
# Optional stylelint cache
|
||||
.stylelintcache
|
||||
|
||||
# Microbundle cache
|
||||
.rpt2_cache/
|
||||
.rts2_cache_cjs/
|
||||
.rts2_cache_es/
|
||||
.rts2_cache_umd/
|
||||
|
||||
# Optional REPL history
|
||||
.node_repl_history
|
||||
|
||||
# Output of 'npm pack'
|
||||
*.tgz
|
||||
|
||||
# Yarn Integrity file
|
||||
.yarn-integrity
|
||||
|
||||
# dotenv environment variable files
|
||||
.env
|
||||
.env.development.local
|
||||
.env.test.local
|
||||
.env.production.local
|
||||
.env.local
|
||||
|
||||
# parcel-bundler cache (https://parceljs.org/)
|
||||
.cache
|
||||
.parcel-cache
|
||||
|
||||
# Next.js build output
|
||||
.next
|
||||
out
|
||||
|
||||
# Nuxt.js build / generate output
|
||||
.nuxt
|
||||
dist
|
||||
|
||||
# Gatsby files
|
||||
.cache/
|
||||
# Comment in the public line in if your project uses Gatsby and not Next.js
|
||||
# https://nextjs.org/blog/next-9-1#public-directory-support
|
||||
# public
|
||||
|
||||
# vuepress build output
|
||||
.vuepress/dist
|
||||
|
||||
# vuepress v2.x temp and cache directory
|
||||
.temp
|
||||
|
||||
# Docusaurus cache and generated files
|
||||
.docusaurus
|
||||
|
||||
# Serverless directories
|
||||
.serverless/
|
||||
|
||||
# FuseBox cache
|
||||
.fusebox/
|
||||
|
||||
# DynamoDB Local files
|
||||
.dynamodb/
|
||||
|
||||
# TernJS port file
|
||||
.tern-port
|
||||
|
||||
# Stores VSCode versions used for testing VSCode extensions
|
||||
.vscode-test
|
||||
|
||||
# yarn v2
|
||||
.yarn/cache
|
||||
.yarn/unplugged
|
||||
.yarn/build-state.yml
|
||||
.yarn/install-state.gz
|
||||
.pnp.*
|
||||
|
||||
### Node Patch ###
|
||||
# Serverless Webpack directories
|
||||
.webpack/
|
||||
|
||||
# Optional stylelint cache
|
||||
|
||||
# SvelteKit build / generate output
|
||||
.svelte-kit
|
||||
|
||||
### VisualStudioCode ###
|
||||
.vscode/*
|
||||
!.vscode/settings.json
|
||||
!.vscode/tasks.json
|
||||
!.vscode/launch.json
|
||||
!.vscode/extensions.json
|
||||
!.vscode/*.code-snippets
|
||||
|
||||
# Local History for Visual Studio Code
|
||||
.history/
|
||||
|
||||
# Built Visual Studio Code Extensions
|
||||
*.vsix
|
||||
|
||||
### VisualStudioCode Patch ###
|
||||
# Ignore all local history of files
|
||||
.history
|
||||
.ionide
|
||||
|
||||
### Windows ###
|
||||
# Windows thumbnail cache files
|
||||
Thumbs.db
|
||||
Thumbs.db:encryptable
|
||||
ehthumbs.db
|
||||
ehthumbs_vista.db
|
||||
|
||||
# Dump file
|
||||
*.stackdump
|
||||
|
||||
# Folder config file
|
||||
[Dd]esktop.ini
|
||||
|
||||
# Recycle Bin used on file shares
|
||||
$RECYCLE.BIN/
|
||||
|
||||
# Windows Installer files
|
||||
*.cab
|
||||
*.msi
|
||||
*.msix
|
||||
*.msm
|
||||
*.msp
|
||||
|
||||
# Windows shortcuts
|
||||
*.lnk
|
||||
|
||||
# End of https://www.toptal.com/developers/gitignore/api/node,linux,windows,macos,visualstudiocode
|
|
@ -0,0 +1,5 @@
|
|||
trailingComma: 'es5'
|
||||
tabWidth: 4
|
||||
useTabs: false
|
||||
semi: false
|
||||
singleQuote: true
|
|
@ -0,0 +1,20 @@
|
|||
# Dapr connector for Cloudflare Workers
|
||||
|
||||
## Develop locally
|
||||
|
||||
```sh
|
||||
npm install
|
||||
npm run start
|
||||
```
|
||||
|
||||
## Info endpoint
|
||||
|
||||
```sh
|
||||
curl "http://localhost:8787/.well-known/dapr/info"
|
||||
```
|
||||
|
||||
## Publish a message
|
||||
|
||||
```sh
|
||||
curl -X POST -d 'hello world' "http://localhost:8787/publish/daprdemo"
|
||||
```
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,22 @@
|
|||
{
|
||||
"private": true,
|
||||
"name": "dapr-cfworkers-client",
|
||||
"description": "Client code for Dapr to interact with Cloudflare Workers",
|
||||
"version": "20221209",
|
||||
"main": "worker.ts",
|
||||
"scripts": {
|
||||
"start": "wrangler dev --local",
|
||||
"format": "prettier --write ."
|
||||
},
|
||||
"author": "Dapr authors",
|
||||
"license": "Apache2",
|
||||
"devDependencies": {
|
||||
"@cloudflare/workers-types": "^4.20221111.1",
|
||||
"prettier": "^2.8.1",
|
||||
"typescript": "^4.9.4",
|
||||
"wrangler": "^2.6.1"
|
||||
},
|
||||
"dependencies": {
|
||||
"itty-router": "^2.6.6"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
{
|
||||
"compilerOptions": {
|
||||
"target": "es2022",
|
||||
"lib": ["es2022"],
|
||||
"module": "es2022",
|
||||
"moduleResolution": "node",
|
||||
"typeRoots": ["node_modules", "."],
|
||||
"types": ["@cloudflare/workers-types"],
|
||||
"resolveJsonModule": true,
|
||||
"allowJs": true,
|
||||
"noEmit": true,
|
||||
"isolatedModules": true,
|
||||
"allowSyntheticDefaultImports": true,
|
||||
"strict": true
|
||||
}
|
||||
}
|
|
@ -0,0 +1,75 @@
|
|||
import { Router, type Request } from 'itty-router'
|
||||
|
||||
import { version } from './package.json'
|
||||
|
||||
const router = Router()
|
||||
.get(
|
||||
'/.well-known/dapr/info',
|
||||
(request: Request, env: Environment): Response => {
|
||||
// Filter all bindings by type
|
||||
const queues: string[] = []
|
||||
const kv: string[] = []
|
||||
const r2: string[] = []
|
||||
const all = Object.keys(env)
|
||||
for (let i = 0; i < all.length; i++) {
|
||||
if (!all[i]) {continue }
|
||||
const obj = env[all[i]]
|
||||
if (!obj || typeof obj != 'object') { continue }
|
||||
if ((obj as Queue<string>) && typeof (obj as Queue<string>).send == 'function') {
|
||||
queues.push(all[i])
|
||||
} else if ((obj as KVNamespace) && typeof (obj as KVNamespace).getWithMetadata == 'function') {
|
||||
kv.push(all[i])
|
||||
} else if ((obj as R2Bucket) && typeof (obj as R2Bucket).createMultipartUpload == 'function') {
|
||||
r2.push(all[i])
|
||||
}
|
||||
}
|
||||
|
||||
const res = JSON.stringify({
|
||||
version,
|
||||
queues: (queues && queues.length) ? queues: undefined,
|
||||
kv: (kv && kv.length) ? kv: undefined,
|
||||
r2: (r2 && r2.length) ? r2: undefined,
|
||||
})
|
||||
return new Response(res, {
|
||||
headers: {
|
||||
'content-type': 'application/json',
|
||||
},
|
||||
})
|
||||
}
|
||||
)
|
||||
.post(
|
||||
'/publish/:queue',
|
||||
async (request: Request, env: Environment): Promise<Response> => {
|
||||
if (!request?.text || !request.params?.queue) {
|
||||
return new Response('Bad request', { status: 400 })
|
||||
}
|
||||
const queue = env[request.params.queue] as Queue<string>
|
||||
if (!queue || typeof queue.send != 'function') {
|
||||
return new Response(
|
||||
`Not subscribed to queue '${request.params.queue}'`,
|
||||
{ status: 412 }
|
||||
)
|
||||
}
|
||||
let message = await request.text()
|
||||
await queue.send(message)
|
||||
return new Response('', { status: 201 })
|
||||
}
|
||||
)
|
||||
// Catch-all route to handle 404s
|
||||
.all('*', (): Response => {
|
||||
return new Response('Not found', { status: 404 })
|
||||
})
|
||||
|
||||
export default {
|
||||
fetch: router.handle,
|
||||
|
||||
async queue(batch: MessageBatch<string>, env: Environment) {
|
||||
for (let i = 0; i < batch.messages.length; i++) {
|
||||
console.log(`Received message ${JSON.stringify(batch.messages[i])}`)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
type Environment = {
|
||||
readonly [x in string]: Queue<string> | KVNamespace | R2Bucket
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
name = "daprdemo"
|
||||
main = "worker.ts"
|
||||
compatibility_date = "2022-12-09"
|
||||
|
||||
# Worker defines a binding, named "QUEUE", which gives it a capability
|
||||
# to send messages to a Queue, named "daprdemo".
|
||||
[[queues.producers]]
|
||||
queue = "daprdemo"
|
||||
binding = "daprdemo"
|
||||
|
||||
# Worker also receives messages from the Queue, named "daprdemo".
|
||||
[[queues.consumers]]
|
||||
queue = "daprdemo"
|
||||
max_batch_size = 10 # Max messages per batch
|
||||
max_batch_timeout = 1 # Max seconds to wait before batch is full
|
||||
max_retries = 3 # Max retries per batch
|
Loading…
Reference in New Issue