Skip to content

Commit

Permalink
checking withDO
Browse files Browse the repository at this point in the history
  • Loading branch information
kwhitley committed May 1, 2024
1 parent 4587d5b commit bcc0aa4
Showing 1 changed file with 12 additions and 26 deletions.
38 changes: 12 additions & 26 deletions src/withDurables.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,17 @@
import { StatusError } from 'itty-router'
import { proxyDurable } from './proxy-durable'
const withDO = (request: any, env: any) => {

Check failure on line 1 in src/withDurables.ts

View workflow job for this annotation

GitHub Actions / build

'withDO' is assigned a value but never used
request.DO = new Proxy(request.proxy ?? request, {
get: (target: Request, prop: string, receiver) => {

Check failure on line 3 in src/withDurables.ts

View workflow job for this annotation

GitHub Actions / build

'receiver' is defined but never used
// @ts-ignore
if (target[prop] != undefined) return target[prop]

// returns true if binding appears to be a durable binding
const isDurable = (binding) => typeof binding.idFromName === 'function'
const DO = env[prop], isDO = DO?.idFromName
if (!isDO) return undefined

export const withDurables = (options = {}) => (request, env) => {
const {
parse = false,
classes = {},
} = options
request.durables = request.durables || {}

for (const [key, binding] of Object.entries(env)) {
if (isDurable(binding)) {
const proxied = proxyDurable(binding, {
name: key,
class: classes[key], // pass in class key by default,
headers: Object.fromEntries(request.headers),
env,
parse,
})

try {
request[key] = request.durables[key] = proxied
} catch (err) {
throw new StatusError(500, `Could not set Durable binding "${key}" on Request`)
return (id: any) =>
typeof id == 'string'
? DO.get(DO.idFromName(id))
: DO.get(id)
}
}
}
)
}

0 comments on commit bcc0aa4

Please sign in to comment.