@zerolith/client/port
Import from @zerolith/client/port. Generated from the source; every signature below is what the published types declare.
Functions
portTransport
function portTransport(port: PortLike): TransportA Transport that sends each request over the port as a message and
resolves with the response that comes back under the same id. Aborting
the request's signal rejects the call; the message already sent is
answered and ignored.
Source: packages/client/src/port.ts:78
servePort
function servePort(port: PortLike, handler: (request: Request) => Response | Promise<Response>): voidAnswers the requests portTransport sends by building a Request,
calling handler, and posting the response back. A handler that throws
answers with a 500 problem so the client sees a ProblemError.
Source: packages/client/src/port.ts:127
Interfaces
PortLike
interface PortLikeThe port shapes supported: a DOM MessagePort, Node's, or Electron's MessagePortMain.
postMessage(message: unknown): void: Sends a message to the other end;portTransportposts eachPortRequestthrough it andservePorteachPortResponse.start?(): void: Starts delivering messages; called, when present, right after the listener is attached, since a DOMMessagePortand Electron'sMessagePortMainqueue messages until it runs.addEventListener?(type: "message", listener: (event: { data: unknown }) => void): void: Receives messages theEventTargetway, as a DOMMessagePortdoes; used in preference toonwhen both exist.on?(type: "message", listener: (event: { data: unknown }) => void): void: Receives messages theEventEmitterway, as Electron'sMessagePortMaindoes; used whenaddEventListeneris absent, and a port with neither throws aTypeError.
Source: packages/client/src/port.ts:9
PortRequest
interface PortRequestWhat crosses the port for a request.
kind: "zerolith:request": Tags the message as a request, soservePortignores other traffic on the port.id: number: Pairs the request with its response; eachportTransportnumbers its requests from 1.url: string: The request's absolute URL, from whichservePortbuilds theRequestits handler receives.method: string: The request method asRequest.methodreports it, such asPOST.headers: [string, string][]: The request headers as[name, value]pairs with lowercase names, as iterating aHeadersyields them.body: string | null: The body as UTF-8 text, ornullfor aGETorHEADrequest or an empty body.
Source: packages/client/src/port.ts:22
PortResponse
interface PortResponseWhat crosses the port for a response.
kind: "zerolith:response": Tags the message as a response, soportTransportignores other traffic on the port.id: number: Theidof the request this answers; an answer with no pending request, such as one whose call was aborted, is dropped.status: number: The status code of the handler's response, or 500 when the handler threw.statusText: string: The status text of the handler's response, empty when none was set.headers: [string, string][]: The response headers as[name, value]pairs with lowercase names, as iterating aHeadersyields them.body: string | null: The body as UTF-8 text, ornullwhen it is empty, as for a 204.
Source: packages/client/src/port.ts:39