-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ee3eb94
commit 5cae9cd
Showing
67 changed files
with
2,555 additions
and
2,376 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import type { Logging } from '@web3-storage/worker-utils/logging' | ||
import type { SigningAuthority } from '@ucanto/interface' | ||
import type { config } from './config' | ||
|
||
export {} | ||
|
||
declare global { | ||
const ACCOUNTS: KVNamespace | ||
} | ||
|
||
export interface RouteContext { | ||
params: Record<string, string> | ||
log: Logging | ||
keypair: SigningAuthority | ||
config: typeof config | ||
} | ||
|
||
export type Handler = ( | ||
event: FetchEvent, | ||
ctx: RouteContext | ||
) => Promise<Response> | Response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
export const config = loadConfigVariables() | ||
|
||
/** | ||
* Loads configuration variables from the global environment and returns a JS object | ||
* keyed by variable names. | ||
* | ||
*/ | ||
export function loadConfigVariables() { | ||
/** @type Record<string, string> */ | ||
const vars = {} | ||
|
||
/** @type Record<string, unknown> */ | ||
const globals = globalThis | ||
|
||
const required = [ | ||
'ENV', | ||
'DEBUG', | ||
'PRIVATE_KEY', | ||
'SENTRY_DSN', | ||
'POSTMARK_TOKEN', | ||
] | ||
|
||
for (const name of required) { | ||
const val = globals[name] | ||
if (typeof val === 'string' && val.length > 0) { | ||
vars[name] = val | ||
} else { | ||
throw new Error( | ||
`Missing required config variables: ${name}. Check your .env, testing globals or cloudflare vars.` | ||
) | ||
} | ||
} | ||
|
||
return { | ||
DEBUG: boolValue(vars.DEBUG), | ||
ENV: parseRuntimeEnv(vars.ENV), | ||
|
||
PRIVATE_KEY: vars.PRIVATE_KEY, | ||
POSTMARK_TOKEN: vars.POSTMARK_TOKEN, | ||
SENTRY_DSN: vars.SENTRY_DSN, | ||
|
||
// These are injected in esbuild | ||
// @ts-ignore | ||
// eslint-disable-next-line no-undef | ||
BRANCH: ACCOUNT_BRANCH, | ||
// @ts-ignore | ||
// eslint-disable-next-line no-undef | ||
VERSION: ACCOUNT_VERSION, | ||
// @ts-ignore | ||
// eslint-disable-next-line no-undef | ||
COMMITHASH: ACCOUNT_COMMITHASH, | ||
} | ||
} | ||
|
||
/** | ||
* Returns `true` if the string `s` is equal to `"true"` (case-insensitive) or `"1", and false for `"false"`, `"0"` or an empty value. | ||
* | ||
* @param {string} s | ||
* @returns {boolean} | ||
*/ | ||
function boolValue(s) { | ||
return Boolean(s && JSON.parse(String(s).toLowerCase())) | ||
} | ||
|
||
/** | ||
* Validates that `s` is a defined runtime environment name and returns it. | ||
* | ||
* @param {unknown} s | ||
*/ | ||
function parseRuntimeEnv(s) { | ||
switch (s) { | ||
case 'test': | ||
case 'dev': | ||
case 'staging': | ||
case 'production': | ||
return s | ||
default: | ||
throw new Error('invalid runtime environment name: ' + s) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import * as Server from '@ucanto/server' | ||
import * as CAR from '@ucanto/transport/car' | ||
import * as CBOR from '@ucanto/transport/cbor' | ||
import { version } from './routes/version.js' | ||
import { serverCodec } from './ucanto/server-codec.js' | ||
import { service } from './ucanto/service.js' | ||
import { getContext } from './utils/context.js' | ||
|
||
import { corsHeaders, preflight } from '@web3-storage/worker-utils/cors' | ||
import { errorHandler } from '@web3-storage/worker-utils/error' | ||
import { notFound } from '@web3-storage/worker-utils/response' | ||
import { Router } from '@web3-storage/worker-utils/router' | ||
|
||
/** @type Router<import('./bindings.js').RouteContext> */ | ||
const r = new Router({ onNotFound: notFound }) | ||
r.add('options', '*', preflight) | ||
r.add('get', '/version', version) | ||
r.add('post', '/', async (request, env) => { | ||
const server = Server.create({ | ||
id: env.keypair, | ||
encoder: CBOR, | ||
decoder: CAR, | ||
service: service(env), | ||
catch: (/** @type {string | Error} */ err) => { | ||
env.log.error(err) | ||
}, | ||
canIssue: ( | ||
/** @type {{ with: any; can: string; }} */ capability, | ||
/** @type {import("@ucanto/interface").DID<unknown>} */ issuer | ||
) => { | ||
if (capability.with === issuer || issuer === env.keypair.did()) { | ||
return true | ||
} | ||
|
||
if (capability.can === 'identity/validate') { | ||
return true | ||
} | ||
|
||
return false | ||
}, | ||
}) | ||
|
||
const rsp = await server.request({ | ||
body: new Uint8Array(await request.arrayBuffer()), | ||
headers: Object.fromEntries(request.headers.entries()), | ||
}) | ||
return new Response(rsp.body, { headers: rsp.headers }) | ||
}) | ||
|
||
r.add('post', '/raw', async (request, env) => { | ||
const server = Server.create({ | ||
id: env.keypair, | ||
encoder: serverCodec, | ||
decoder: serverCodec, | ||
service: service(env), | ||
catch: (/** @type {string | Error} */ err) => { | ||
env.log.error(err) | ||
}, | ||
}) | ||
|
||
const rsp = await server.request({ | ||
body: new Uint8Array(await request.arrayBuffer()), | ||
headers: Object.fromEntries(request.headers.entries()), | ||
}) | ||
return new Response(rsp.body, { headers: rsp.headers }) | ||
}) | ||
|
||
addEventListener('fetch', (event) => { | ||
const env = getContext(event, {}) | ||
env.log.time('request') | ||
event.respondWith( | ||
r | ||
.handle(event, env) | ||
.then((rsp) => { | ||
env.log.timeEnd('request') | ||
return env.log.end(corsHeaders(event.request, rsp)) | ||
}) | ||
.catch((error) => { | ||
return errorHandler(error, env.log) | ||
}) | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { JSONResponse } from '@web3-storage/worker-utils/response' | ||
|
||
/** | ||
* @param {import('@web3-storage/worker-utils/router').ParsedRequest} event | ||
* @param {import('../bindings.js').RouteContext} env | ||
*/ | ||
export function version(event, env) { | ||
return new JSONResponse({ | ||
version: env.config.VERSION, | ||
commit: env.config.COMMITHASH, | ||
branch: env.config.BRANCH, | ||
did: env.keypair.did(), | ||
}) | ||
} |
Oops, something went wrong.