Skip to content

Commit

Permalink
Merge branch 'main' into cf-metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
neonphog committed Jul 11, 2024
2 parents 6aa92aa + 72db08f commit a44ce50
Show file tree
Hide file tree
Showing 14 changed files with 639 additions and 516 deletions.
2 changes: 1 addition & 1 deletion ts/sbd-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.0.0",
"private": true,
"scripts": {
"deploy": "wrangler deploy --minify",
"deploy": "wrangler deploy",
"dev": "wrangler dev",
"start": "wrangler dev",
"test:fmt": "prettier -w src",
Expand Down
18 changes: 0 additions & 18 deletions ts/sbd-server/src/b64.ts

This file was deleted.

124 changes: 124 additions & 0 deletions ts/sbd-server/src/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import { fromUint8Array, toUint8Array } from 'js-base64';
import * as ed from '@noble/ed25519';
import { sha512 } from '@noble/hashes/sha512';
ed.etc.sha512Sync = (...m) => sha512(ed.etc.concatBytes(...m));
export { ed };

/**
* How long to wait ahead of "now" to batch up message sends.
* Note, we're setting this to zero which will try to send queued messages
* as fast as possible. This doesn't mean messages won't be queued/batched,
* since there will be a delay between requesting an alarm and when it
* is actually invoked + however long it takes to actually run.
*/
export const BATCH_DUR_MS = 0;

/**
* How many nanoseconds of rate limiting quota should be burned by a single
* byte sent through the system. Higher numbers mean slower rate limiting.
*/
export const LIMIT_NANOS_PER_BYTE = 8000;

/**
* Milliseconds connections are allowed to remain idle before being closed.
*/
export const LIMIT_IDLE_MILLIS = 10000;

/**
* Max message size.
*/
export const MAX_MESSAGE_BYTES = 20000;

/**
* Cloudflare worker environment objects.
*/
export interface EnvExplicit {
SBD_COORDINATION: KVNamespace;
SIGNAL: DurableObjectNamespace;
RATE_LIMIT: DurableObjectNamespace;
}

/**
* Cloudflare worker environment variables.
*/
export interface EnvVars {
[index: string]: string;
}

/**
* Combined Cloudflare Env type.
*/
export type Env = EnvExplicit & EnvVars;

/**
* Mixin to allow errors with status codes.
*/
export interface AddStatus {
status: number;
}

/**
* Error type with a status code.
*/
export type StatusError = Error & AddStatus;

/**
* Adds a 'status' property to ts Error type.
* If not specified will be set to 500.
* Allows altering the http or ws error status for responses.
* In the case of a websocket error, the http status code
* will be added to 4000 for user-specified error codes.
*/
export function err(e: string, s?: number): StatusError {
const out: any = new Error(e);
out.status = s || 500;
return out;
}

/**
* Seconds since epoch timestamp.
*/
export function timestamp(): number {
return (Date.now() / 1000) | 0;
}

/**
* Pull pubKey string and bytes from the url path.
*/
export function parsePubKey(path: string): {
pubKeyStr: string;
pubKeyBytes: Uint8Array;
} {
const parts: Array<string> = path.split('/');

if (parts.length !== 2) {
throw err('expected single pubKey item on path', 400);
}

const pubKeyStr = parts[1];

const pubKeyBytes = fromB64Url(parts[1]);

if (pubKeyBytes.length !== 32) {
throw err('invalid pubKey length', 400);
}

return { pubKeyStr, pubKeyBytes };
}

/**
* Convert to base64url representation.
*/
export function toB64Url(s: Uint8Array): string {
return fromUint8Array(s)
.replace(/\=/g, '')
.replace(/\+/g, '-')
.replace(/\//g, '_');
}

/**
* Convert from base64url representation.
*/
export function fromB64Url(s: string): Uint8Array {
return toUint8Array(s.replace(/\-/g, '+').replace(/\_/g, '/'));
}
7 changes: 0 additions & 7 deletions ts/sbd-server/src/ed.ts

This file was deleted.

18 changes: 0 additions & 18 deletions ts/sbd-server/src/err.ts

This file was deleted.

Loading

0 comments on commit a44ce50

Please sign in to comment.