import type { ScopedDb } from '@protobase/query' import type { Capability } from '@protobase/schema' import type { Caller } from '../api-keys/resolve-caller' import type { CallsAccess } from '../calls/calls-access' import type { DecryptField } from '../encryption/decrypt-field-types' import type { ChannelSender } from '../channels/channel-sender' import type { LiveRefresh } from '../live/refresh/live-refresh' import type { PersonalMail } from '../personal-mail/personal-mailer' import type { Session } from '../types' import type { SocketEvents } from './function-socket' import type { FunctionRecords } from './records' /** What every function gets: its name and the raw database. */ export type PublicFunctionContext = { /** The function's name, as in `/api/functions/`. */ name: string /** * The database, with no access rules or row filters, and the tenant scope of `.tenant()` resources neither: privileged * work only. The tables whose rows belong to one organization, `member`, `invitation` and `personal_access_token`, it * keeps to the caller's (`ScopedDb`); a public function's to none, so it reads none of their rows and writes none. * `db.unscoped('')` reads them all. */ db: ScopedDb /** Broadcasts to the channel of a record, as the server, to whoever is on it (see `/api/channels`). */ channels: ChannelSender /** The caller, through the app's own `authenticate`: rejects with a 401 problem when the request carries no valid token. */ caller: () => Promise /** * Answers a WebSocket upgrade with a socket: return its response. A request that is no upgrade is a 426 problem. While * the socket is open, the caller of a protected function, and of a public one once `caller()` resolved, is checked * again every minute: their credential, the function's roles, and the same user, tenant, organization and roles. The * socket closes with 1008 once they would be refused. The function's own checks, such as access to one record, are * not repeated unless it passes them as `recheck`. `maxMessageBytes` lowers the largest message the socket takes. */ acceptWebSocket: (events: SocketEvents) => Response /** Admits a member to a call's media, over the socket the function accepts: see `CallsAccess`. */ calls: CallsAccess /** Tells a person's open pages to read again once their access changed: `live.refresh({ user })`, see `LiveRefresh`. */ live: LiveRefresh } /** Who called a function, and the app's resources as them. */ export type FunctionCaller = { session: Session /** The app's resources as this caller, under the same access rules, row filters and tenant scope as the REST API. */ records: FunctionRecords } /** What a function that needs a signed-in caller gets: the caller resolved before it runs. */ export type FunctionContext = PublicFunctionContext & FunctionCaller & { /** * The cleartext of an `.encrypted()` field of one record, or `null` when it is empty: privileged like `db`, with no * access rules or tenant scope. Each read is audited as a reveal, naming this function and its caller, and fails when * the audit does. Not on a public function's context: a public function that needs a value enqueues a job. */ decryptField: DecryptField /** * Mail from the caller's own mailbox, through the mail provider they linked (`auth.personalMail`), attachments and * all: `personalMail.send({ to, subject, text, html, attachments })`, or an email template's `send(to, data, * context.personalMail, { attachments })`. It goes out from the From address record `sender_address_id` names, through * that record's mailbox connection (`send({ sender_address_id, ... })`), else from the caller's default address, else * their mailbox's own: an id that is not one of the caller's is refused with a 404 problem, and an address the mailbox * cannot send as (`sender_address_failed`), one still `pending` or one on no connection (`sender_address_detached`) * with a 409, sending nothing. `sender(address?)` answers the record an address stands for, with its id, state and own * signature, `senders()` every address the caller may choose, and `refresh()` lists the send-as addresses of each * mailbox whose provider can. Rejects with a `PersonalMailError`, sending nothing, when they linked no * mailbox (`not_linked`) or the link no longer works (`link_expired`); thrown on, it answers as a 409 problem with * that `code`. Only someone signed in as themselves sends from their mailbox: staff signed in as them, or an outside * app acting for them, is refused with a 403 problem. Not on a public function's context. */ personalMail: PersonalMail } /** What a function gets at `/api/sb/functions/v1/`, where supabase-js calls it: the caller its keys and token make. */ export type KeyFunctionContext = { name: string /** The database as a public function's: no organization's rows of the tables that hold them. */ db: ScopedDb /** The caller of the `apikey` header and bearer token: rejects with a 401 problem when they admit nobody. */ caller: () => Promise /** Tells a person's open pages to read again once their access changed: `live.refresh({ user })`, see `LiveRefresh`. */ live: LiveRefresh } /** A Web-standard handler. Its request's path is the part after `/api/functions/`: `/` or `/more/parts`. */ export type FunctionHandler = (request: Request, context: FunctionContext) => Response | Promise export type PublicFunctionHandler = (request: Request, context: PublicFunctionContext) => Response | Promise /** Any app with a Hono-style `fetch`, such as `new Hono()`: its `c.env` is the function's context. */ export type FunctionApp = { fetch: (request: Request, env: Context) => Response | Promise } /** `hono/cors` options: an origin, a list of them, or a function of the request's origin. */ export type CorsOptions = { origin: string | string[] | ((origin: string) => string | undefined | null) allowMethods?: string[] allowHeaders?: string[] exposeHeaders?: string[] maxAge?: number credentials?: boolean } type SharedOptions = { /** Answers CORS preflights before authentication and adds the CORS headers to every response, problems included. */ cors?: CorsOptions /** * Its queries through `db` may run up to 30 seconds instead of the database's own limit, on a connection kept for * such work: for imports, reports and other bulk work. Its `records` keep the API's limit. */ longQueries?: boolean /** * Also answers at `/api/sb/functions/v1/`, as Supabase's `verify_jwt` does, where the app's publishable key alone * admits a caller. For functions moved from Supabase; not together with `roles` or `capability`. */ supabase?: boolean } /** A function anyone may call, such as a webhook; it can still ask for a caller with `context.caller()`. */ export type PublicFunctionOptions = SharedOptions & { public: true } export type ProtectedFunctionOptions = SharedOptions & { public?: false /** The caller needs one of these roles, otherwise the call is a 403 problem. */ roles?: string[] /** * The caller's roles must grant all of this capability (`roles.holds`), such as `vms.update`, otherwise the call is a * 403 problem. Needs the app's `roles`; not together with `roles` here. */ capability?: Capability } export type FunctionOptions = PublicFunctionOptions | ProtectedFunctionOptions /** A function as `createAdmin` serves it: its options and what answers its requests. */ export type ApiFunction = | { kind: 'function'; options: PublicFunctionOptions; handler: PublicFunctionHandler | FunctionApp } | { kind: 'function'; options: ProtectedFunctionOptions; handler: FunctionHandler | FunctionApp } /** What a function file may default-export: `defineFunction(...)`, a bare handler or a Hono app (both protected). */ export type FunctionSource = ApiFunction | FunctionHandler | FunctionApp /** * A function of your own, served at `/api/functions/`. Without options it needs a signed-in caller, through the * app's `authenticate`, and with the app's `roles` one whose roles hold `*`; `{ roles }` or `{ capability }` opens it * to more, `{ public: true }` to anyone, as for a webhook. `{ supabase: true }` serves it at `/api/sb/functions/v1/` too. */ export function defineFunction(handler: FunctionHandler | FunctionApp): ApiFunction export function defineFunction(options: PublicFunctionOptions, handler: PublicFunctionHandler | FunctionApp): ApiFunction export function defineFunction(options: ProtectedFunctionOptions, handler: FunctionHandler | FunctionApp): ApiFunction export function defineFunction(first: FunctionOptions | FunctionHandler | FunctionApp, handler?: unknown): ApiFunction { if (handler === undefined) return { kind: 'function', options: {}, handler: first as FunctionHandler | FunctionApp } return { kind: 'function', options: first, handler } as ApiFunction } const isApp = (value: unknown): value is FunctionApp => typeof value === 'object' && value !== null && typeof (value as FunctionApp).fetch === 'function' const isApiFunction = (value: unknown): value is ApiFunction => typeof value === 'object' && value !== null && (value as ApiFunction).kind === 'function' /** A function's name is its URL segment: letters, digits, `-` and `_`, starting with a letter or digit. */ export const functionNamePattern = /^[A-Za-z0-9][A-Za-z0-9_-]*$/ /** The function a source stands for; anything else is an error naming it. */ export const toApiFunction = (name: string, source: unknown): ApiFunction => { if (!functionNamePattern.test(name)) throw new Error(`The function name "${name}" is not a URL segment: use letters, digits, - and _`) if (isApiFunction(source)) { if (typeof source.handler !== 'function' && !isApp(source.handler)) throw new Error(`The function "${name}" has no handler: pass a (request, context) function or a Hono app`) return source } if (typeof source === 'function' || isApp(source)) return defineFunction(source as FunctionHandler | FunctionApp) throw new Error(`The function "${name}" must default-export defineFunction(...), a (request, context) handler or a Hono app`) } /** Calls a function's handler or app with its request and context; anything but a Response is an error. */ export const invokeFunction = async (name: string, fn: ApiFunction, request: Request, context: PublicFunctionContext | FunctionContext | KeyFunctionContext) => { const { handler } = fn const response: unknown = isApp(handler) ? await handler.fetch(request, context as FunctionContext) : await (handler as FunctionHandler)(request, context as FunctionContext) if (!(response instanceof Response)) throw new Error(`The function "${name}" returned no Response`) return response }