All files / core/blong-gogo/src/adapter/server webhook.ts

44.71% Statements 271/606
84.61% Branches 33/39
100% Functions 6/6
44.71% Lines 271/606

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 6071x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 14x 14x 5x 14x 1x 1x 1x 5x 5x 1x 1x 20x 1x 1x 20x 20x 20x 20x 20x 20x 20x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 9x 9x 9x 9x 9x 9x 9x 9x 9x   9x 9x 9x 9x 3x 3x 9x 9x 9x 6x 6x 6x 6x 6x 6x 6x 9x 9x 1x 1x 1x 1x 1x 1x 5x 5x 9x 9x 9x 3x 2x 1x 3x 3x 2x 1x 1x 9x 1x 1x 2x 9x 9x 1x 1x 1x 1x 2x 3x 3x 3x 3x 3x 3x       3x 3x 3x 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
import {
    adapter,
    type Adapter,
    type Errors,
    type IErrorMap,
    type IMeta,
} from '@feasibleone/blong/types';
import fastify, {type FastifyInstance, type FastifyReply, type FastifyRequest} from 'fastify';
import got, {type HttpsOptions, type Options} from 'got';
import {Duplex, Readable, Writable} from 'stream';
import {v4} from 'uuid';
 
import tls from '../../tls.ts';
 
export interface IConfig {
    tls?: {
        key?: string;
        cert?: string;
        ca?: string | string[];
    };
    handle?: string;
    url?: string;
    /**
     * Start the receiving half.
     *
     * Off by default: a realm may want the posting half alone (a notifier), and a port that listened
     * without being asked would open a socket nothing dials.
     */
    listen?: boolean;
    /** Where the receiving half listens. Port `0` is a real choice — the kernel picks, and the port reports it. */
    server?: {host?: string; port?: number};
    /** The path an incoming request is posted to. */
    path?: string;
    /**
     * The prefix of the receiving hooks: `<hook>.identity`, `<hook>.message`, `<hook>.server`.
     *
     * Defaults to the port's namespace, which is what a port with one handler group wants; a port
     * whose receiving methods are named after the inbound side (`slackIn.identity` beside its own
     * `slack` methods) names it.
     */
    hook?: string;
    /**
     * How the HTTP answer relates to the work.
     *
     * `server` acknowledges from `response` as soon as the payload is understood and lets the
     * destination run on, which is what a platform with a three-second ack needs; `reply` waits for
     * the chain and answers with what the `server` hook shapes, which is what a test wants to observe.
     */
    mode?: 'server' | 'reply';
    /** The acknowledgement body, when `mode` is `server` or no `server` hook answers. */
    response?: IWebhookResponse;
    /** The method an inbound message is dispatched to, e.g. `chat.in.message.receive`. */
    destination?: string;
    /** Largest body accepted, in bytes. */
    bodyLimit?: number;
}
 
/** What an incoming request gives the hooks: the parsed body and the bytes it came from. */
export interface IWebhookRequest {
    method: string;
    path: string;
    query: unknown;
    params: unknown;
    headers: Record<string, unknown>;
    /** The body as it arrived, which is what a platform signature is computed over. */
    rawBody?: Buffer;
    /** The same body, parsed — JSON, or a form as a name/value record. */
    body?: unknown;
}
 
/** What the receiving half answers with. */
export interface IWebhookResponse {
    code?: number;
    body?: unknown;
    headers?: Record<string, string>;
}
 
/** What the `server` hook is handed, once the chain has run. */
export interface IWebhookAnswerContext {
    request: IWebhookRequest;
    identity?: unknown;
    /** `false` when the payload was understood and there is nothing to process — a challenge, an ack. */
    message?: unknown;
    /** What the destination answered, in `reply` mode only. */
    result?: unknown;
}
 
/**
 * The three hooks a receiving port may provide.
 *
 * Each is optional and each missing one degrades rather than fails: no identity means no verification
 * (a simulator), no message means the request itself is the message, and no server means the
 * configured response is the answer.
 */
export interface IWebhookHooks {
    identity?: (request: IWebhookRequest, $meta: IMeta) => Promise<unknown>;
    message?: (request: IWebhookRequest, $meta: IMeta) => Promise<unknown>;
    server?: (
        context: IWebhookAnswerContext,
        $meta: IMeta,
    ) => Promise<IWebhookResponse | undefined>;
    /**
     * The acknowledgement, when it depends on the payload rather than on the port.
     *
     * One path often carries two protocols: Slack's Events API and its Interactivity both post to
     * the same Request URL, and the client that posts a form reads a different answer than the
     * server that posts an event — a submitted modal is dismissed by an empty 200 and greeted with
     * an error by any other body (`@@F-645`). The hook runs on the request alone, before the realm,
     * and `undefined` leaves the configured `response` as the answer (`@@D-689`).
     */
    acknowledge?: (request: IWebhookRequest, $meta: IMeta) => Promise<IWebhookResponse | undefined>;
}
 
/** Everything the answer needs that is not a hook. */
export interface IWebhookBehaviour {
    hook: string;
    mode: 'server' | 'reply';
    response: IWebhookResponse;
    destination?: string;
    /** Reaches the destination — the port's own `dispatch`, or a stub in a test. */
    dispatch?: (message: unknown, $meta: IMeta) => Promise<unknown>;
    /** Mints the trace a request carries, so two deliveries of one event are two traces. */
    traceId?: () => string;
}
 
/**
 * The ids already delivered, and the cap that keeps the memory bounded.
 *
 * A map rather than a set because the cap evicts the oldest entry, and insertion order is what
 * "oldest" means here. A platform retries on its own schedule, so the only thing a memory has to
 * outlive is one retry window (`@@D-676`).
 */
export interface IWebhookMemory {
    seen: Map<string, true>;
    limit: number;
}
 
export const webhookMemory = (limit = 1000): IWebhookMemory => ({seen: new Map(), limit});
 
/**
 * Remember a request id, and answer whether it was already there.
 *
 * `true` means the delivery is a duplicate and must be acknowledged without being processed again.
 * An id-less request is always processed: a platform that names no id gives nothing to compare.
 */
export const webhookRemember = (memory: IWebhookMemory, requestId: string | undefined): boolean => {
    if (!requestId) return false;
    if (memory.seen.has(requestId)) return true;
    memory.seen.set(requestId, true);
    if (memory.seen.size > memory.limit) {
        const oldest = memory.seen.keys().next().value;
        if (oldest !== undefined) memory.seen.delete(oldest);
    }
    return false;
};
 
const traceOf = (behaviour: IWebhookBehaviour): string =>
    (behaviour.traceId ?? (() => v4().replace(/-/g, '')))();
 
const metaOf = (request: IWebhookRequest, method: string, behaviour: IWebhookBehaviour): IMeta =>
    ({
        mtid: 'request',
        method,
        forward: {'x-b3-traceid': traceOf(behaviour)},
        params: request.params,
        request,
    }) as unknown as IMeta;
 
/**
 * Run the receiving chain and produce the HTTP answer.
 *
 * Pure in the sense that matters: every side effect is a hook or the `dispatch` it is handed, so the
 * modes, the duplicate rule and the shapes a `server` hook may return are assertions in
 * `webhook.test.ts` rather than something only a live platform can prove.
 *
 * A `detached` promise is returned in `server` mode rather than awaited: the acknowledgment is the
 * point of that mode, and the caller attaches its own logging so a failure is still recorded.
 */
export const webhookAnswer = async (
    request: IWebhookRequest,
    hooks: IWebhookHooks,
    behaviour: IWebhookBehaviour,
    memory: IWebhookMemory,
): Promise<{response: IWebhookResponse; detached?: Promise<unknown>}> => {
    const identity =
        (await hooks.identity?.(
            request,
            metaOf(request, `${behaviour.hook}.identity`, behaviour),
        )) ?? {};
    // The answer is decided once, before anything runs: it is what a duplicate is answered with
    // too, and a platform whose acknowledgement depends on the payload says so here.
    const ack =
        (await hooks.acknowledge?.(
            request,
            metaOf(request, `${behaviour.hook}.acknowledge`, behaviour),
        )) ?? behaviour.response;
    const requestId = (identity as {requestId?: string}).requestId;
    if (webhookRemember(memory, requestId)) return {response: ack};
    // The identity is `auth` on everything after it, the way a handler reads it: a message hook that
    // builds a download URL, or a destination that records who spoke, needs the account the signature
    // was verified against and not only the fact that it verified.
    const messageMeta = {
        ...metaOf(request, `${behaviour.hook}.message`, behaviour),
        auth: identity,
    };
    const message = (await hooks.message?.(request, messageMeta)) ?? request;
    if (message === false) {
        const shaped = await hooks.server?.(
            {request, identity, message},
            metaOf(request, `${behaviour.hook}.server`, behaviour),
        );
        return {response: shaped ?? ack};
    }
    const destinationMeta = {
        ...messageMeta,
        ...(behaviour.destination && {method: behaviour.destination}),
    };
    if (behaviour.mode === 'server') {
        const detached = behaviour.destination
            ? Promise.resolve(behaviour.dispatch?.(message, destinationMeta))
            : undefined;
        return {response: ack, detached};
    }
    const result = behaviour.destination
        ? await behaviour.dispatch?.(message, destinationMeta)
        : undefined;
    const shaped = await hooks.server?.(
        {request, identity, message, result},
        metaOf(request, `${behaviour.hook}.server`, behaviour),
    );
    return {response: shaped ?? ack};
};
 
/** The body parser a receiving port installs, keeping the bytes beside the value. */
export const webhookBodyParser =
    (kind: 'json' | 'form') =>
    (
        request: FastifyRequest,
        body: Buffer,
        done: (error: Error | null, value?: unknown) => void,
    ): void => {
        (request as FastifyRequest & {rawBody?: Buffer}).rawBody = body;
        if (!body.length) {
            done(null, null);
            return;
        }
        const text = body.toString('utf8');
        try {
            done(
                null,
                kind === 'json' ? JSON.parse(text) : Object.fromEntries(new URLSearchParams(text)),
            );
        } catch (error) {
            // A body that is not what its content type promised is the caller's mistake, so the answer
            // is a 400 rather than the 500 a bare parser error would produce.
            const failure = new Error(`Cannot parse the ${kind} body`, {cause: error});
            (failure as Error & {statusCode?: number}).statusCode = 400;
            done(failure, undefined);
        }
    };
 
const errorMap: IErrorMap = {
    'webhook.http': 'HTTP Error',
};
 
let _errors: Errors<typeof errorMap>;
 
/** The port as the receiving half needs it: its own hook handlers, and the dispatch to the realm. */
interface IWebhookPort {
    config: IConfig & {namespace?: string | string[]};
    findHandler(name: string): unknown;
    handle(params: unknown, $meta: IMeta): Promise<unknown>;
}
 
export default adapter<IConfig>(({utError, local, registry}) => {
    _errors ||= utError.register(errorMap);
    let stream: Duplex | null = null;
    let https: HttpsOptions | undefined;
    let http: FastifyInstance | null = null;
    const memory = webhookMemory();

    return {
        activation: {
            default: {
                type: 'webhook',
                url: 'http://localhost:8080',
            },
        },
        async init(...configs: object[]) {
            await super.init(...configs);
            https = tls(this.config, true) as HttpsOptions | undefined;
            const cfg = this.config as unknown as Record<string, unknown>;
            if (cfg['codec.openapi'])
                await registry.loadApi(
                    this.config.id + '.api',
                    cfg['codec.openapi'] as {namespace: Record<string, string | string[]>},
                    this.configBase ?? '',
                );
        },

        async start(this: Adapter<IConfig>) {
            const result = await super.start();
            const readable = new Readable({
                objectMode: true,
                read() {},
            });
            const self = this as unknown as IWebhookPort;
            const cfg = this.config as IConfig & {namespace?: string | string[]};
            const hook =
                cfg.hook ?? (typeof cfg.namespace === 'string' ? cfg.namespace : 'webhook');
            const response: IWebhookResponse = cfg.response ?? {
                code: 200,
                body: 'EVENT_RECEIVED',
            };
            /**
             * Call one of the port's own hooks.
             *
             * `handle` finds the imported handler by name, so a hook the port does not carry is simply
             * absent — which is how a simulator runs the same receiver with nothing verified.
             */
            const call = async (name: string, params: unknown, $meta: IMeta): Promise<unknown> => {
                if (!self.findHandler(name)) return undefined;
                return self.handle(params, $meta);
            };
            const hooks: IWebhookHooks = {
                identity: (request, $meta) => call(`${hook}.identity`, request, $meta),
                // A port with no message hook receives the request itself: it is the shape a simulator
                // wants, and the shape a platform port replaces with a canonical message.
                message: async (request, $meta) =>
                    (await call(`${hook}.message`, request, $meta)) ?? request,
                server: async (context, $meta) =>
                    (await call(`${hook}.server`, context, $meta)) as IWebhookResponse | undefined,
                acknowledge: async (request, $meta) =>
                    (await call(`${hook}.acknowledge`, request, $meta)) as
                        | IWebhookResponse
                        | undefined,
            };

            local.register(
                {
                    [`${this.config.namespace}Webhook.request`]: async (
                        params: unknown,
                        $meta: unknown,
                    ) =>
                        new Promise((resolve, reject) => {
                            ($meta as Record<string, unknown>)['dispatch'] = (
                                ...packet: unknown[]
                            ) => {
                                (this.dispatch as (...args: unknown[]) => Promise<unknown[]>)(
                                    ...packet,
                                ).then(resolve, reject);
                            };
                            readable.push([params, $meta]);
                        }),
                    [`${this.config.namespace}Webhook.publish`]: async (...params: unknown[]) => {
                        readable.push(params);
                    },
                    /**
                     * The address the port bound, which is how a port configured with `port: 0` is
                     * reached: a test and the README both need to know where it listens, and reading it
                     * back is cheaper than agreeing on a number.
                     */
                    [`${this.config.namespace}Webhook.address`]: async () => ({
                        host: cfg.server?.host,
                        port: cfg.server?.port,
                        path: cfg.path,
                        listening: Boolean(http),
                    }),
                },
                'ports',
                false,
                this.config.pkg,
            );

            stream = Duplex.from({
                writable: new Writable({
                    objectMode: true,
                    write: async (
                        [
                            {
                                path = '',
                                query: searchParams,
                                url = new URL(path, cfg.url),
                                responseType = 'json',
                                method,
                                headers,
                                body,
                                form,
                                json,
                            },
                            $meta,
                        ]: [
                            {
                                path: string;
                                query: string;
                                url: URL;
                                responseType: Options['responseType'];
                                method: Options['method'];
                                headers: Options['headers'];
                                body: Options['body'];
                                form: Options['form'];
                                json: Options['json'];
                            },
                            IMeta,
                        ],
                        _encoding: BufferEncoding,
                        callback: (error?: Error | null) => void,
                    ) => {
                        try {
                            const request = {
                                url,
                                searchParams,
                                https,
                                method: method || 'POST',
                                headers,
                                responseType,
                                body,
                                form,
                                json,
                                throwHttpErrors: false,
                                followRedirect: false,
                                // isStream: false,
                            };
                            // A capture, not a dump: the rung decides what is rendered, and the payload
                            // is built only when the configuration asks for one (`@@T-730`). The verb is
                            // hoisted because both halves name it in their selector.
                            const verb = String(request.method).toUpperCase();
                            this.capture?.http?.({method: verb, phase: 'request'}, () => ({
                                phase: 'request',
                                method: verb,
                                url: String(url),
                                headers: headers as Record<string, unknown>,
                                ...(json != null ? {body: json} : {}),
                            }));
                            {
                                const startedAt = Date.now();
                                const result = await got(request);
                                const {headers, body, statusCode, statusMessage} = result;
                                this.capture?.http?.({method: verb, phase: 'response'}, () => ({
                                    phase: 'response',
                                    method: verb,
                                    url: String(url),
                                    statusCode,
                                    statusMessage,
                                    headers: headers as unknown as Record<string, unknown>,
                                    body,
                                    durationMs: Date.now() - startedAt,
                                }));
                                if (!$meta.trace)
                                    readable.push([result, {...$meta, mtid: 'response'}]);
                            }
                        } catch (error) {
                            callback(_errors['webhook.http'](error!));
                            return;
                        }
                        callback();
                    },
                }),
                readable,
            });

            super.connect(stream);

            if (cfg.listen) {
                const app = fastify({
                    logger: false,
                    bodyLimit: cfg.bodyLimit ?? 1048576,
                });
                app.addContentTypeParser(
                    'application/json',
                    {parseAs: 'buffer'},
                    webhookBodyParser('json'),
                );
                app.addContentTypeParser(
                    'application/x-www-form-urlencoded',
                    {parseAs: 'buffer'},
                    webhookBodyParser('form'),
                );
                app.route({
                    method: 'POST',
                    // The path is written `<hook>`-style, with a parameter in braces
                    // and a configuration reads back as it was written — while the router
                    // matches `:name`. The translation is here rather than in every port, because a port
                    // that had to write router syntax would make the config the router's copy. The
                    // optional marker travels with the name (`{action?}` → `:action?`).
                    url: (cfg.path ?? '/webhook').replace(/\{([A-Za-z0-9_]+)(\?)?\}/g, ':$1$2'),
                    handler: async (request: FastifyRequest, reply: FastifyReply) => {
                        const descriptor: IWebhookRequest = {
                            method: request.method,
                            path: request.url.split('?')[0] ?? request.url,
                            query: request.query,
                            params: request.params,
                            headers: request.headers as Record<string, unknown>,
                            rawBody: (request as FastifyRequest & {rawBody?: Buffer}).rawBody,
                            body: request.body,
                        };
                        const answer = (shaped: IWebhookResponse) => {
                            const {code = 200, body = null, headers} = shaped;
                            if (headers) reply.headers(headers);
                            return reply.code(code).send(body);
                        };
                        try {
                            const {response: shaped, detached} = await webhookAnswer(
                                descriptor,
                                hooks,
                                {
                                    hook,
                                    mode: cfg.mode ?? 'server',
                                    response,
                                    destination: cfg.destination,
                                    dispatch: (message, $meta) =>
                                        (
                                            this.dispatch as unknown as (
                                                params: unknown,
                                                $meta: IMeta,
                                            ) => Promise<unknown>
                                        )(message, $meta),
                                },
                                memory,
                            );
                            // A detached dispatch that fails is logged when it fails; one that never
                            // settles leaves the platform acked and the realm silent, which reads from
                            // outside exactly like a message nobody received (`@@F-668`). The note is at
                            // debug and two minutes out, because a destination that *asks a question* is
                            // still running on purpose — it is a hint for the case that is stuck, not a
                            // verdict on the case that is waiting.
                            const destination = cfg.destination;
                            const watchdog = setTimeout(() => {
                                this.signal?.frameworkWebhookDestinationSlow?.({destination});
                            }, 120_000);
                            (watchdog as unknown as {unref?: () => void}).unref?.();
                            detached
                                ?.catch(error =>
                                    this.signal?.frameworkWebhookDestinationFailed?.({
                                        error:
                                            error instanceof Error
                                                ? error.message
                                                : String(error),
                                    }),
                                )
                                .finally(() => clearTimeout(watchdog));
                            return answer(shaped);
                        } catch (error) {
                            // A handler throws `errors[name]({params}, $meta)`, and with `$meta` the factory
                            // answers an array — the shape the bus unpacks. The status and the type live on
                            // the error inside it, not on the array.
                            const thrown = (Array.isArray(error) ? error[0] : error) as Error & {
                                statusCode?: number;
                                type?: string;
                            };
                            const statusCode = thrown?.statusCode ?? 500;
                            this.signal?.frameworkWebhookHandlerFailed?.({
                                error: thrown?.message ?? String(thrown),
                            });
                            return reply.code(statusCode).send({
                                error: {
                                    ...(thrown?.type && {type: thrown.type}),
                                    message: thrown?.message ?? 'Webhook request failed',
                                },
                            });
                        }
                    },
                });
                await app.listen({
                    host: cfg.server?.host ?? '0.0.0.0',
                    port: cfg.server?.port ?? 0,
                });
                const address = app.server.address();
                if (address && typeof address === 'object') {
                    cfg.server = {
                        host: cfg.server?.host ?? address.address,
                        port: address.port,
                    };
                }
                http = app;
            }

            return result;
        },

        async stop(...params: unknown[]) {
            let result;
            try {
                if (http) {
                    const closing = http;
                    http = null;
                    await closing.close();
                }
                if (stream) {
                    const closing = stream;
                    stream = null;
                    // `Duplex.from` reports a bare destroy as a rejected operation — `AbortError` — which
                    // the port loop logs as a stream error on every ordinary shutdown. Ending both halves
                    // first turns the teardown into a close; the destroy then only releases the handle.
                    closing.push(null);
                    closing.end();
                    setImmediate(() => closing.destroy());
                }
            } finally {
                stream = null;
                result = await super.stop(...params);
            }
            return result;
        },
    };
});