function asUpgradeResponse
Since 0.2.0
asUpgradeResponse(
response: unknown,
binding: string
): DurableObjectUpgradeResponse

Narrows a Durable Object stub's response to one carrying a socket.

A stub answering an ordinary Response — a 404 from a DO class with no upgrade path, a 500 from one that threw, a 101 from a runtime that does not populate webSocket — fails here, naming the binding, rather than surfacing later as a TypeError on undefined.accept() with nothing to point at.

Examples

Example 1

const response = await stub.fetch('https://do/connect', {
  headers: { Upgrade: 'websocket' },
});
const socket = asUpgradeResponse(response, 'REALTIME').webSocket;
socket.accept();

Parameters

response: unknown

The stub's response

binding: string

The Durable Object binding name, for the error message

Return Type

The same response, narrowed

Throws

CloudflareUnsupportedError

When the response carries no webSocket

Usage

import { asUpgradeResponse } from "cloudflare-plugin/src/index.ts";