API reference

@wellknown-id/mailbox

A mailbox that keeps ciphertext under ids its writers choose, and answers anyone who knows an id: wellknown.id's relay runs one, and a site that accepts delegation may run its own. It can't read what it holds, tell whose it is or link one id to another, logs nothing, and throws away what's extinct. Mount mailboxHandler in a server of your own, or run createMailboxServer (or the wellknown-mailbox command).

Interfaces

MailboxStorage

What a mailbox keeps, as text by kind and id. Synchronous, so a request reads and writes without interleaving.

Methods

delete()
delete(kind, id): void;
Parameters
ParameterType
kindKind
idstring
Returns

void

get()
get(kind, id): string | undefined;
Parameters
ParameterType
kindKind
idstring
Returns

string | undefined

ids()
ids(kind): Iterable<string>;

Every id held of a kind (for counting what's held at start, and for garbage collection).

Parameters
ParameterType
kindKind
Returns

Iterable<string>

put()
put(
   kind, 
   id, 
   text
): void;
Parameters
ParameterType
kindKind
idstring
textstring
Returns

void

size()
size(kind, id): number;

The bytes held under an id, 0 for none.

Parameters
ParameterType
kindKind
idstring
Returns

number

Type Aliases

GcOptions

type GcOptions = object;

When garbage collection runs: every intervalMs (0: never by the clock), and after every everyRequests requests (0: never).

Properties

PropertyType
batchnumber
everyRequestsnumber
intervalMsnumber

Kind

type Kind = "store" | "box" | "drop";

What a mailbox keeps: blobs (store), records boxes (box) and drop boxes (drop).


MailboxHandler

type MailboxHandler = (req, res) => void & object;

A Node HTTP request handler answering the mailbox's API, with garbage collection on demand and a way to stop.

Type Declaration

NameTypeDescription
close()() => voidStops the clock that runs garbage collection, and the write buckets' sweep.
gc()() => Promise<number>Runs garbage collection now (or joins the run under way); resolves with how many ids it threw away.

MailboxOptions

type MailboxOptions = object;

How a mailbox is made: where it keeps things, who may use it from a web page, and its limits.

Properties

PropertyTypeDescription
freeBytes?() => numberFree bytes on the disk the storage writes to (Infinity, for storage that isn't a disk).
gc?Partial<GcOptions>When garbage collection runs (GC_DEFAULTS by default).
limits?StoreLimitsIts limits (STORE_LIMITS by default).
now?() => numberThe time, in milliseconds; tests move it.
origins?string[]Web pages that may use it (CORS), by origin.
prefix?stringThe path it answers under: '/relay' on wellknown.id's relay.
storageMailboxStorageWhere it keeps what it's given (fileStorage, memoryStorage, or your own).

ServerOptions

type ServerOptions = Partial<Omit<MailboxOptions, "storage">> & object;

A mailbox server's options: the mailbox's own (MailboxOptions, but storage), and a directory to keep files in.

Type Declaration

NameTypeDescription
dir?stringA directory to keep files in; in memory without one.

StoreLimits

type StoreLimits = typeof STORE_LIMITS;

A mailbox's limits (STORE_LIMITS has the defaults and what each is for).

Variables

GC_DEFAULTS

const GC_DEFAULTS: GcOptions;

Garbage collection by default: hourly, and after every 1000 requests, 200 ids at a time.


KINDS

const KINDS: readonly Kind[];

Every kind, in the order storage lists them.


STORE_LIMITS

const STORE_LIMITS: object;

The default limits: sizes per blob, records entry and drop-box item, what's held in all, and writes per client.

Type Declaration

NameTypeDefault value
blobBytesnumber-
dropDaysnumber30
entriesPerBoxnumber5000
entryBytesnumber-
itemBytesnumber-
itemsPerDropnumber2000
maxClientsnumber100_000
maxLifetimeDaysnumber400
maxTotalBytesnumber-
minFreeBytesnumber-
writesBurstnumber120
writesPerSecondnumber2

Functions

clientOf()

function clientOf(req): string;

Which client a request is from, for its bucket only: the address the nearest proxy saw (the rightmost X-Forwarded-For).

Parameters

ParameterType
reqIncomingMessage

Returns

string


createMailboxServer()

function createMailboxServer(opts?): Server<typeof IncomingMessage, typeof ServerResponse> & object;

A server answering the mailbox's API (and GET {prefix}/health), not yet listening.

Parameters

ParameterType
optsServerOptions

Returns

Server<typeof IncomingMessage, typeof ServerResponse> & object


fileStorage()

function fileStorage(dir): MailboxStorage;

Storage in files under dir: {dir}/{kind}/{id}.json, each written whole (to a temporary file, then renamed).

Parameters

ParameterType
dirstring

Returns

MailboxStorage


mailboxHandler()

function mailboxHandler(opts): MailboxHandler;

The mailbox's HTTP handler.

Parameters

ParameterType
optsMailboxOptions

Returns

MailboxHandler


memoryStorage()

function memoryStorage(): MailboxStorage;

Storage in memory: gone when the process ends.

Returns

MailboxStorage


writeBuckets()

function writeBuckets(limits, now?): object;

Token buckets by client, in memory only; full buckets are forgotten.

Parameters

ParameterType
limitsPick<StoreLimits, "writesBurst" | "writesPerSecond" | "maxClients">
now() => number

Returns

NameTypeDescription
close()() => void-
size()() => number-
take()(client) => booleanTakes a token from client's bucket; false when it's empty.