0.1.0-alpha.7

@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): Transport

A 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>): void

Answers 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 PortLike

The port shapes supported: a DOM MessagePort, Node's, or Electron's MessagePortMain.

  • postMessage(message: unknown): void: Sends a message to the other end; portTransport posts each PortRequest through it and servePort each PortResponse.
  • start?(): void: Starts delivering messages; called, when present, right after the listener is attached, since a DOM MessagePort and Electron's MessagePortMain queue messages until it runs.
  • addEventListener?(type: "message", listener: (event: { data: unknown }) => void): void: Receives messages the EventTarget way, as a DOM MessagePort does; used in preference to on when both exist.
  • on?(type: "message", listener: (event: { data: unknown }) => void): void: Receives messages the EventEmitter way, as Electron's MessagePortMain does; used when addEventListener is absent, and a port with neither throws a TypeError.

Source: packages/client/src/port.ts:9

PortRequest

interface PortRequest

What crosses the port for a request.

  • kind: "zerolith:request": Tags the message as a request, so servePort ignores other traffic on the port.
  • id: number: Pairs the request with its response; each portTransport numbers its requests from 1.
  • url: string: The request's absolute URL, from which servePort builds the Request its handler receives.
  • method: string: The request method as Request.method reports it, such as POST.
  • headers: [string, string][]: The request headers as [name, value] pairs with lowercase names, as iterating a Headers yields them.
  • body: string | null: The body as UTF-8 text, or null for a GET or HEAD request or an empty body.

Source: packages/client/src/port.ts:22

PortResponse

interface PortResponse

What crosses the port for a response.

  • kind: "zerolith:response": Tags the message as a response, so portTransport ignores other traffic on the port.
  • id: number: The id of 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 a Headers yields them.
  • body: string | null: The body as UTF-8 text, or null when it is empty, as for a 204.

Source: packages/client/src/port.ts:39