CF Worker: better detection for binding type

Signed-off-by: ItalyPaleAle <43508+ItalyPaleAle@users.noreply.github.com>
This commit is contained in:
ItalyPaleAle 2022-12-29 00:24:03 +00:00
parent c23fc9e30d
commit 4e17e42818
5 changed files with 21 additions and 25 deletions

View File

@ -1,12 +1,12 @@
{
"name": "dapr-cfworkers-client",
"version": "20221219",
"version": "20221228",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "dapr-cfworkers-client",
"version": "20221219",
"version": "20221228",
"license": "Apache2",
"dependencies": {
"itty-router": "^2.6.6",

View File

@ -2,7 +2,7 @@
"private": true,
"name": "dapr-cfworkers-client",
"description": "Client code for Dapr to interact with Cloudflare Workers",
"version": "20221219",
"version": "20221228",
"main": "worker.ts",
"scripts": {
"build": "esbuild --bundle --minify --outfile=../workers/code/worker.js --format=esm --platform=browser --sourcemap worker.ts",

View File

@ -40,24 +40,20 @@ const router = Router()
continue
}
const obj = env[all[i]]
if (!obj || typeof obj != 'object') {
if (!obj || typeof obj != 'object' || !obj.constructor) {
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])
switch (obj.constructor.name) {
case 'KVNamespace':
kv.push(all[i])
break
case 'Queue':
queues.push(all[i])
break
case 'R2Bucket':
// Note that we currently don't support R2 yet
r2.push(all[i])
break
}
}
@ -174,7 +170,7 @@ async function setupKVRequest(
return { errorRes: new Response('Bad request', { status: 400 }) }
}
const namespace = env[req.params.namespace] as KVNamespace<string>
if (!namespace || typeof namespace.getWithMetadata != 'function') {
if (typeof namespace != 'object' || namespace?.constructor?.name != 'KVNamespace') {
return {
errorRes: new Response(
`Worker is not bound to KV '${req.params.kv}'`,
@ -200,7 +196,7 @@ async function setupQueueRequest(
return { errorRes: new Response('Bad request', { status: 400 }) }
}
const queue = env[req.params.queue] as Queue<string>
if (!queue || typeof queue.send != 'function') {
if (typeof queue != 'object' || queue?.constructor?.name != 'Queue') {
return {
errorRes: new Response(
`Worker is not bound to queue '${req.params.queue}'`,

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long