-
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.
Co-authored-by: Oli Evans <oli.evans@gmail.com>
- Loading branch information
1 parent
104480f
commit 49c389f
Showing
22 changed files
with
1,911 additions
and
1,248 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
/* eslint-disable unicorn/prefer-module */ | ||
// @ts-nocheck | ||
export const { Buffer } = require('buffer') | ||
export const process = require('process/browser') |
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 './utils/logging' | ||
|
||
export {} | ||
|
||
declare global { | ||
const _PRIVATE_KEY: string | ||
const BRANCH: string | ||
const VERSION: string | ||
const COMMITHASH: string | ||
const ENV: string | ||
const DEBUG: string | ||
} | ||
|
||
export interface RouteContext { | ||
params: Record<string, string> | ||
log: Logging | ||
} | ||
|
||
export interface 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,3 @@ | ||
/* eslint-disable no-undef */ | ||
|
||
export const PRIVATE_KEY = _PRIVATE_KEY |
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 |
---|---|---|
@@ -1,16 +1,22 @@ | ||
/** | ||
* | ||
* @param {Request} request | ||
* @returns | ||
*/ | ||
async function handleRequest(request) { | ||
return new Response(JSON.stringify({ msg: 'hello world!' }), { | ||
headers: { | ||
'content-type': 'application/json;charset=UTF-8', | ||
}, | ||
}) | ||
} | ||
import { Router } from './utils/router.js' | ||
import { getContext } from './utils/context.js' | ||
import { HTTPError } from './utils/errors.js' | ||
import { cors, postCors } from './utils/cors.js' | ||
// import { Service } from './service.js' | ||
import { version } from './routes/version.js' | ||
import { notFound } from './utils/responses.js' | ||
|
||
addEventListener('fetch', (/** @type {FetchEvent} */ event) => { | ||
return event.respondWith(handleRequest(event.request)) | ||
const r = new Router(getContext, { | ||
onError(req, err, ctx) { | ||
return HTTPError.respond(err, ctx) | ||
}, | ||
}) | ||
|
||
// CORS | ||
r.add('options', '*', cors) | ||
|
||
// Version | ||
r.add('get', '/version', version, [postCors]) | ||
|
||
r.add('all', '*', notFound) | ||
addEventListener('fetch', r.listen.bind(r)) |
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,12 @@ | ||
import { JSONResponse } from '../utils/responses.js' | ||
|
||
/** | ||
* @param {FetchEvent} event | ||
*/ | ||
export function version(event) { | ||
return new JSONResponse({ | ||
version: VERSION, | ||
commit: COMMITHASH, | ||
branch: BRANCH, | ||
}) | ||
} |
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,68 @@ | ||
import { UcanChain } from 'ucan-storage/ucan-chain' | ||
import { KeyPair } from 'ucan-storage/keypair' | ||
|
||
/** | ||
* @type {import('ucan-storage/types').CapabilitySemantics<any>} | ||
*/ | ||
export const accessSemantics = { | ||
tryParsing(cap) { | ||
return cap | ||
}, | ||
|
||
tryDelegating(parentCap, childCap) { | ||
return childCap | ||
}, | ||
} | ||
|
||
export class Service { | ||
/** | ||
* @param {KeyPair} keypair | ||
*/ | ||
constructor(keypair) { | ||
this.keypair = keypair | ||
} | ||
|
||
/** | ||
* @param {string} key | ||
*/ | ||
static async fromPrivateKey(key) { | ||
const kp = await KeyPair.fromExportedKey(key) | ||
return new Service(kp) | ||
} | ||
|
||
static async create() { | ||
return new Service(await KeyPair.create()) | ||
} | ||
|
||
/** | ||
* Validates UCAN for capability | ||
* | ||
* @param {string} encodedUcan | ||
*/ | ||
async validate(encodedUcan) { | ||
const token = await UcanChain.fromToken(encodedUcan, {}) | ||
|
||
if (token.audience() !== this.did()) { | ||
throw new Error('Invalid UCAN: Audience does not match this service.') | ||
} | ||
|
||
const caps = token.caps(accessSemantics) | ||
|
||
if (caps.length > 1) { | ||
throw new Error('Invocation ucan should have only 1 cap.') | ||
} | ||
|
||
const cap = caps[0] | ||
const root = cap.root | ||
|
||
if (root.issuer() !== this.did()) { | ||
throw new Error('Invalid UCAN: Root issuer does not match this service.') | ||
} | ||
|
||
return cap | ||
} | ||
|
||
did() { | ||
return this.keypair.did() | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,35 @@ | ||
import { Logging } from './logging.js' | ||
// import Toucan from 'toucan-js' | ||
// import pkg from '../../package.json' | ||
|
||
// const sentryOptions = { | ||
// dsn: secrets.sentry, | ||
// allowedHeaders: ['user-agent', 'x-client'], | ||
// allowedSearchParams: /(.*)/, | ||
// debug: false, | ||
// environment: ENV, | ||
// rewriteFrames: { | ||
// root: '/', | ||
// }, | ||
// release: VERSION, | ||
// pkg, | ||
// } | ||
|
||
/** | ||
* Obtains a route context object. | ||
* | ||
* @param {FetchEvent} event | ||
* @param {Record<string, string>} params - Parameters from the URL | ||
* @returns {Promise<import('../bindings').RouteContext>} | ||
*/ | ||
export async function getContext(event, params) { | ||
// const sentry = new Toucan({ | ||
// event, | ||
// ...sentryOptions, | ||
// }) | ||
const log = new Logging(event, { | ||
debug: DEBUG === 'true', | ||
}) | ||
|
||
return { params, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/** | ||
* @param {FetchEvent} event | ||
*/ | ||
export function cors(event) { | ||
const headers = event.request.headers | ||
// Make sure the necessary headers are present for this to be a valid pre-flight request | ||
if ( | ||
headers.get('Origin') !== null && | ||
headers.get('Access-Control-Request-Method') !== null && | ||
headers.get('Access-Control-Request-Headers') !== null | ||
) { | ||
// Handle CORS pre-flight request. | ||
/** @type {Record<string, string>} */ | ||
const respHeaders = { | ||
'Content-Length': '0', | ||
'Access-Control-Allow-Origin': headers.get('origin') || '*', | ||
'Access-Control-Allow-Methods': 'GET,POST,DELETE,PATCH,OPTIONS', | ||
'Access-Control-Max-Age': '86400', | ||
// Allow all future content Request headers to go back to browser | ||
// such as Authorization (Bearer) or X-Client-Name-Version | ||
'Access-Control-Allow-Headers': | ||
headers.get('Access-Control-Request-Headers') || '', | ||
} | ||
|
||
return new Response(undefined, { | ||
status: 204, | ||
headers: respHeaders, | ||
}) | ||
} else { | ||
return new Response('Non CORS options request not allowed', { | ||
status: 405, | ||
statusText: 'Method Not Allowed', | ||
}) | ||
} | ||
} | ||
|
||
/** | ||
* @param {Request} req | ||
* @param {Response} rsp | ||
*/ | ||
export function postCors(req, rsp) { | ||
const origin = req.headers.get('origin') | ||
if (origin) { | ||
rsp.headers.set('Access-Control-Allow-Origin', origin) | ||
rsp.headers.set( | ||
'Access-Control-Allow-Methods', | ||
'GET,POST,DELETE,PATCH,OPTIONS' | ||
) | ||
rsp.headers.set('Vary', 'Origin') | ||
} else { | ||
rsp.headers.set('Access-Control-Allow-Origin', '*') | ||
} | ||
return rsp | ||
} |
Oops, something went wrong.