All files mqtt-plus-source.ts

87.6% Statements 544/621
59.66% Branches 71/119
100% Functions 12/12
87.6% Lines 544/621

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 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 6221x 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 9x 9x 1x 1x 1x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x               6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x   7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 2x 2x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 519x 519x 519x                 519x 519x 7x 7x 7x 7x 7x 7x 260x 260x 260x 260x 260x 260x 260x 260x 260x 260x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 253x       253x       253x 253x 253x 1x 1x 1x 1x 1x 253x 252x 252x 252x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 6x 6x 6x 6x 7x 7x 3x 3x 3x   3x 3x 3x 7x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 2x 7x 7x 7x 7x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x               8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x   8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 267x               267x 267x 260x 260x 267x 267x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 529x 529x 529x 1x 1x 1x 528x 529x 8x 8x 8x 8x 8x 8x 665x 665x 665x 657x 665x 665x 665x 665x 665x 665x 255x 255x 255x 255x 255x 255x   255x 255x 255x 665x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 10x 2x 2x 2x 1x 1x 1x 1x 1x 1x 2x 10x 10x 10x 10x 8x 8x 8x 8x 8x 7x 7x 7x               7x 7x 7x 1x 1x 1x 1x 1x 1x 6x 6x 6x 6x 6x 8x 8x 8x 8x 260x 260x 260x               260x 260x 260x               260x             260x 260x 260x 260x               260x 260x 260x 260x 260x 5x 5x 5x 5x 5x 260x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x       8x 8x 8x 8x 8x 7x 7x 4x 4x 8x 8x 8x    
/*
**  MQTT+ -- MQTT Communication Patterns
**  Copyright (c) 2018-2026 Dr. Ralf S. Engelschall <rse@engelschall.com>
**
**  Permission is hereby granted, free of charge, to any person obtaining
**  a copy of this software and associated documentation files (the
**  "Software"), to deal in the Software without restriction, including
**  without limitation the rights to use, copy, modify, merge, publish,
**  distribute, sublicense, and/or sell copies of the Software, and to
**  permit persons to whom the Software is furnished to do so, subject to
**  the following conditions:
**
**  The above copyright notice and this permission notice shall be included
**  in all copies or substantial portions of the Software.
**
**  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
**  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
**  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
**  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
**  CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
**  TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
**  SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
 
/*  built-in requirements  */
import { Readable }                                       from "node:stream"
 
/*  external requirements  */
import type { IClientPublishOptions,
    IClientSubscribeOptions }                             from "mqtt"
import { nanoid }                                         from "nanoid"
 
/*  internal requirements  */
import { CreditGate, ReadableTee,
    sendBufferAsChunks, sendStreamAsChunks,
    makeMutuallyExclusiveFields }                         from "./mqtt-plus-util"
import { run, Spool, ensureError }                        from "./mqtt-plus-error"
import type { SourceFetchRequest, SourceFetchResponse,
    SourceFetchChunk, SourceFetchCredit }                 from "./mqtt-plus-msg"
import type { APISchema, SourceKeys, Registration }       from "./mqtt-plus-api"
import type { WithInfo, InfoSource }                      from "./mqtt-plus-info"
import { ServiceTrait }                                   from "./mqtt-plus-service"
import type { AuthOption }                                from "./mqtt-plus-auth"
 
/*  Source Fetch Trait  */
export class SourceTrait<T extends APISchema = APISchema> extends ServiceTrait<T> {
    /*  source state  */
    private sourceCreditGates = new Map<string, CreditGate>()
    private sourceControllers = new Map<string, AbortController>()
    private sourceSpools      = new Map<string, Spool>()
 
    /*  destroy source trait  */
    override async destroy () {
        for (const controller of this.sourceControllers.values())
            controller.abort(new Error("source destroyed"))
        for (const gate of this.sourceCreditGates.values())
            gate.abort()
        for (const spool of this.sourceSpools.values())
            await spool.unroll()
        this.sourceSpools.clear()
        this.sourceControllers.clear()
        this.sourceCreditGates.clear()
        await super.destroy()
    }
 
    /*  establish a source (for fetch requests)  */
    async source<K extends SourceKeys<T> & string> (
        name:     K,
        callback: WithInfo<T[K], InfoSource>
    ): Promise<Registration>
    async source<K extends SourceKeys<T> & string> (
        config: {
            name:      K,
            callback:  WithInfo<T[K], InfoSource>,
            options?:  Partial<IClientSubscribeOptions>,
            share?:    string,
            auth?:     AuthOption
        }
    ): Promise<Registration>
    async source<K extends SourceKeys<T> & string> (
        nameOrConfig: K | {
            name:      K,
            callback:  WithInfo<T[K], InfoSource>,
            options?:  Partial<IClientSubscribeOptions>,
            share?:    string,
            auth?:     AuthOption
        },
        ...args:       any[]
    ): Promise<Registration> {
        /*  determine actual parameters  */
        let name:     K
        let callback: WithInfo<T[K], InfoSource>
        let options:  Partial<IClientSubscribeOptions> = {}
        let share     = this.options.share
        let auth:     AuthOption | undefined
        if (typeof nameOrConfig === "object" && nameOrConfig !== null && "name" in nameOrConfig) {
            /*  object-based API  */
            name     = nameOrConfig.name
            callback = nameOrConfig.callback
            options  = nameOrConfig.options ?? {}
            share    = nameOrConfig.share   ?? this.options.share
            auth     = nameOrConfig.auth
        }
        else {
            /*  positional API  */
            name     = nameOrConfig
            callback = args[0]
        }
 
        /*  sanity check callback  */
        if (typeof callback !== "function")
            throw new Error("source: callback argument is required and must be a function")
 
        /*  create a resource spool  */
        const spool = new Spool()
 
        /*  sanity check situation  */
        if (this.onRequest.has(`source-fetch-request:${name}`))
            throw new Error(`source: source "${name}" already registered`)
 
        /*  generate the corresponding MQTT topics for broadcast and direct use  */
        const topicS    = share !== "" ? `$share/${share}/${name}` : name
        const topicReqB = this.options.topicMake(topicS, "source-fetch-request")
        const topicReqD = this.options.topicMake(name,   "source-fetch-request", this.options.id)
 
        /*  remember the registration  */
        this.onRequest.set(`source-fetch-request:${name}`, async (request: SourceFetchRequest, topicName: string) => {
            /*  check receiver  */
            if (request.receiver && request.receiver !== this.options.id)
                return
 
            /*  determine information  */
            const requestId = request.id
            const params    = request.params ?? []
            const sender    = request.sender
            if (sender === undefined || sender === "")
                throw new Error("invalid request: missing sender")
            const receiver  = request.receiver
            const abortController = new AbortController()
            if (this.sourceControllers.has(requestId))
                throw new Error(`source: duplicate request id "${requestId}"`)
            this.sourceControllers.set(requestId, abortController)
            const abortSignal     = abortController.signal
            const info: InfoSource = { sender, signal: abortSignal }
            if (receiver)
                info.receiver = receiver
            if (request.meta)
                info.meta = request.meta
 
            /*  generate corresponding MQTT topic (single topic for all responses)  */
            const responseTopic = this.options.topicMake(name, "source-fetch-response", sender)
            const sourceNameMismatchError = (kind: string, actualName: string) =>
                new Error(`${kind} name mismatch (expected "${name}", got "${actualName}")`)
 
            /*  callback for sending the ack/nak response  */
            const sendResponse = async (error?: string) => {
                const metaStore = this.metaStore(info.meta)
                const response = this.msg.makeSourceFetchResponse(requestId,
                    name, error, this.options.id, sender, metaStore)
                const message = this.codec.encode(response)
                await this.publishToTopic(responseTopic, message, { qos: options.qos ?? 2 })
            }
 
            /*  create a resource spool for request cleanup  */
            const reqSpool = new Spool()
            if (this.sourceSpools.has(requestId))
                throw new Error(`source: duplicate request id "${requestId}"`)
            this.sourceSpools.set(requestId, reqSpool)
            reqSpool.roll(() => { this.sourceSpools.delete(requestId) })
            reqSpool.roll(() => {
                this.onResponse.delete(`source-fetch-credit:${requestId}`)
                this.sourceControllers.delete(requestId)
            })
 
            /*  ensure stream gets destroyed on abort  */
            abortSignal.addEventListener("abort", () => {
                if (info.stream instanceof Readable && !info.stream.destroyed)
                    info.stream.destroy(ensureError(abortSignal.reason))
            }, { once: true })
            const abortPromise = new Promise<never>((_resolve, reject) => {
                const onAbort = () => { reject(ensureError(abortSignal.reason)) }
                if (abortSignal.aborted)
                    onAbort()
                else
                    abortSignal.addEventListener("abort", onAbort, { once: true })
                reqSpool.roll(() => { abortSignal.removeEventListener("abort", onAbort) })
            })
 
            /*  utility functions for timeout management  */
            const sourceTimerId = `source-fetch-send:${requestId}`
            const refreshSourceTimeout = () => {
                if (abortSignal.aborted)
                    return
                this.timerRefresh(sourceTimerId, () => {
                    const error = new Error(`source fetch "${name}" timed out`)
                    abortController.abort(error)
                    const gate = this.sourceCreditGates.get(requestId)
                    if (gate !== undefined) {
                        gate.abort()
                        this.sourceCreditGates.delete(requestId)
                    }
                    reqSpool.unroll()
                })
            }
            const clearSourceTimeout   = () => this.timerClear(sourceTimerId)
            refreshSourceTimeout()
            reqSpool.roll(() => { clearSourceTimeout() })
 
            /*  callback for creating and sending a chunk message  */
            const sendChunk = async (
                chunk: Uint8Array | undefined,
                error: string | undefined,
                final: boolean
            ): Promise<void> => {
                refreshSourceTimeout()
                const chunkMsg = this.msg.makeSourceFetchChunk(requestId,
                    name, chunk, error, final, this.options.id, sender)
                const message = this.codec.encode(chunkMsg)
                await this.publishToTopic(responseTopic, message, { qos: options.qos ?? 2 })
            }
 
            /*  call the handler callback  */
            let ackSent = false
            let creditGate: CreditGate | undefined
            let cancelledByFetcher = false
            try {
                if (topicName !== request.name)
                    throw new Error(`source name mismatch (topic: "${topicName}", payload: "${request.name}")`)
                if (auth)
                    info.authenticated = await this.authenticated(sender, request.auth, auth)
                if (info.authenticated !== undefined && !info.authenticated)
                    throw new Error(`source "${name}" failed authentication`)
 
                /*  handle credit-based flow control (if credit provided in request)  */
                const initialCredit = request.credit
                creditGate = (initialCredit !== undefined && initialCredit > 0)
                    ? new CreditGate(initialCredit) : undefined
                if (creditGate) {
                    this.sourceCreditGates.set(requestId, creditGate)
                    reqSpool.roll(() => {
                        creditGate!.abort()
                        this.sourceCreditGates.delete(requestId)
                    })
                }
 
                /*  register credit/cancel handler (unconditional for cancel support)  */
                this.onResponse.set(`source-fetch-credit:${requestId}`, (creditParsed: SourceFetchCredit) => {
                    if (creditParsed.name !== name) {
                        abortController.abort(sourceNameMismatchError("source credit", creditParsed.name))
                        return
                    }
                    if (creditParsed.sender === undefined || creditParsed.sender === "") {
                        abortController.abort(new Error(`source credit for "${name}" missing sender`))
                        return
                    }
                    if (creditParsed.sender !== sender)
                        return
                    if (creditParsed.credit === 0) {
                        /*  cancel signal from fetcher  */
                        cancelledByFetcher = true
                        abortController.abort(new Error(`source fetch "${name}" cancelled by fetcher`))
                        return
                    }
                    if (creditGate) {
                        creditGate.replenish(creditParsed.credit)
                        refreshSourceTimeout()
                    }
                })
 
                await Promise.race([
                    Promise.resolve(callback(...params, info)),
                    abortPromise
                ])
 
                /*  check for valid data source  */
                if (!(info.stream instanceof Readable) && !(info.buffer instanceof Promise) && !(info.buffer instanceof Uint8Array))
                    throw new Error("handler did not provide data via info.stream or info.buffer fields")
                if (info.stream instanceof Readable && (info.buffer instanceof Promise || info.buffer instanceof Uint8Array))
                    throw new Error("handler has set both info.stream and info.buffer fields")
 
                /*  send ack response  */
                await sendResponse()
                ackSent = true
 
                /*  dispatch according to data type  */
                if (info.stream instanceof Readable)
                    /*  handle Readable stream result  */
                    await sendStreamAsChunks(info.stream, this.options.chunkSize, sendChunk, creditGate, abortSignal)
                else if (info.buffer instanceof Promise || info.buffer instanceof Uint8Array) {
                    /*  handle Buffer result  */
                    const buffer = (info.buffer instanceof Promise)
                        ? await Promise.race([ info.buffer, abortPromise ])
                        : info.buffer
                    await sendBufferAsChunks(buffer, this.options.chunkSize, sendChunk, creditGate, abortSignal)
                }
            }
            catch (err: unknown) {
                /*  cleanup stream resource (if provided by handler)  */
                const error = ensureError(err, `handler for source "${name}" failed`)
                abortController.abort(error)
 
                /*  on explicit fetcher cancellation, abort silently without emitting error responses  */
                if (!cancelledByFetcher) {
                    /*  send error as nak response or as error chunk  */
                    this.error(error)
                    if (ackSent)
                        await sendChunk(undefined, error.message, true).catch(() => {})
                    else
                        await sendResponse(error.message).catch(() => {})
                }
            }
            finally {
                /*  cleanup resources  */
                await reqSpool.unroll()
            }
        })
        spool.roll(() => { this.onRequest.delete(`source-fetch-request:${name}`) })
 
        /*  subscribe to MQTT topics  */
        await this.subscribeTopicAndSpool(spool, topicReqB, options)
        await this.subscribeTopicAndSpool(spool, topicReqD, options)
 
        /*  provide a registration for subsequent destruction  */
        return this.makeRegistration(spool, "source", name, `source-fetch-request:${name}`)
    }
 
    /*  fetch source  */
    async fetch<K extends SourceKeys<T> & string> (
        name:          K,
        ...params:     Parameters<T[K]>
    ): Promise<{
        stream:        Readable,
        buffer:        Promise<Uint8Array>,
        meta:          Promise<Record<string, any> | undefined>
    }>
    async fetch<K extends SourceKeys<T> & string> (
        config: {
            name:      K,
            params:    Parameters<T[K]>,
            receiver?: string,
            options?:  IClientPublishOptions,
            meta?:     Record<string, any>
        }
    ): Promise<{
        stream:        Readable,
        buffer:        Promise<Uint8Array>,
        meta:          Promise<Record<string, any> | undefined>
    }>
    async fetch<K extends SourceKeys<T> & string> (
        nameOrConfig: K | {
            name:      K,
            params:    Parameters<T[K]>,
            receiver?: string,
            options?:  IClientPublishOptions,
            meta?:     Record<string, any>
        },
        ...args:       any[]
    ): Promise<{
        stream:        Readable,
        buffer:        Promise<Uint8Array>,
        meta:          Promise<Record<string, any> | undefined>
    }> {
        /*  determine actual parameters  */
        let name:      K
        let params:    Parameters<T[K]>
        let receiver:  string | undefined
        let options:   IClientPublishOptions = {}
        let meta:      Record<string, any> | undefined
        if (typeof nameOrConfig === "object" && nameOrConfig !== null && "name" in nameOrConfig) {
            /*  object-based API  */
            name     = nameOrConfig.name
            params   = nameOrConfig.params
            receiver = nameOrConfig.receiver
            options  = nameOrConfig.options ?? {}
            meta     = nameOrConfig.meta
        }
        else {
            /*  positional API  */
            name     = nameOrConfig
            params   = args as Parameters<T[K]>
        }
 
        /*  create a resource spool  */
        const spool = new Spool()
 
        /*  generate unique request id  */
        let requestId = nanoid()
        while (
            this.onResponse.has(`source-fetch-response:${requestId}`)
            || this.onResponse.has(`source-fetch-chunk:${requestId}`))
            requestId = nanoid()
 
        /*  subscribe to single response topic (for ack/nak and data chunks)  */
        const responseTopic = this.options.topicMake(name, "source-fetch-response", this.options.id)
        const sourceNameMismatchError = (kind: string, actualName: string) =>
            new Error(`${kind} name mismatch (expected "${name}", got "${actualName}")`)
        await this.subscribeTopicAndSpool(spool, responseTopic, { qos: options.qos ?? 2 })
 
        /*  credit-based flow control state  */
        const chunkCredit  = this.options.chunkCredit
        const maxBufferedBytes = chunkCredit > 0 ? chunkCredit * this.options.chunkSize : 16 * 1024
        let chunksReceived = 0
        let creditGranted  = chunkCredit
        let responderId    = receiver
        let responseAcked  = false
        let streamEnded    = false
 
        /*  lock the responder for this communication  */
        const lockResponder = (kind: string, sender?: string): boolean => {
            if (sender === undefined || sender === "") {
                streamEnded = true
                const error = new Error(`received ${kind} without sender`)
                metaReject(error)
                stream?.destroy(error)
                spool.unroll()
                return false
            }
            if (responderId === undefined)
                responderId = sender
            else if (sender !== responderId)
                return false
            return true
        }
 
        /*  create promise for meta (resolved on first chunk)  */
        let metaResolve!: (value: Record<string, any> | undefined) => void
        let metaReject!:  (reason?: any) => void
        const metaP = new Promise<Record<string, any> | undefined>((resolve, reject) => {
            metaResolve = resolve
            metaReject  = reject
        })
        metaP.catch(() => {}) /*  avoid unhandled promise rejection  */
        spool.roll(() => { metaResolve(undefined) })
 
        /*  define timer  */
        const timerId = `source-fetch-recv:${requestId}`
        let stream: ReadableTee | undefined = undefined
        const refreshTimeout = () => {
            if (streamEnded || (stream && stream.destroyed))
                return
            this.timerRefresh(timerId, () => {
                const error = new Error("communication timeout")
                metaReject(error)
                stream?.destroy(error)
            })
        }
        spool.roll(() => { this.timerClear(timerId) })
 
        /*  establish a readable for buffering received chunks  */
        stream = new ReadableTee({
            highWaterMark: maxBufferedBytes,
            read: (_size) => {
                if (chunkCredit <= 0 || streamEnded)
                    return
                const targetId = responderId
                if (!targetId)
                    return
                const outstanding   = creditGranted - chunksReceived
                const freeBytes     = Math.max(0, maxBufferedBytes - (stream?.readableLength ?? 0))
                const freeChunks    = Math.floor(freeBytes / this.options.chunkSize)
                const creditToGrant = Math.max(0, freeChunks - outstanding)
                if (creditToGrant > 0) {
                    creditGranted += creditToGrant
                    const creditMsg = this.msg.makeSourceFetchCredit(requestId,
                        name, creditToGrant, this.options.id, targetId)
                    const encoded = this.codec.encode(creditMsg)
                    const creditTopic = this.options.topicMake(name, "source-fetch-request", targetId)
                    this.publishToTopic(creditTopic, encoded, { qos: options.qos ?? 2 }).catch((err: Error) => {
                        this.error(err, `sending credit for fetch "${name}" failed`)
                    })
                    refreshTimeout()
                }
            }
        })
        stream.once("error", () => {}) /*  prevent unhandled error exception  */
 
        /*  create promise for eagerly collecting stream chunks  */
        const buffer = stream.buffer
 
        /*  start timeout handler  */
        refreshTimeout()
 
        /*  ensure resources are released if consumer aborts stream early  */
        let cancelled = false
        const cancelAndUnroll = () => {
            if (!cancelled && !streamEnded) {
                cancelled = true
                const targetId = responderId
                if (targetId) {
                    const cancelMsg = this.msg.makeSourceFetchCredit(requestId,
                        name, 0, this.options.id, targetId)
                    const encoded = this.codec.encode(cancelMsg)
                    const cancelTopic = this.options.topicMake(name, "source-fetch-request", targetId)
                    this.publishToTopic(cancelTopic, encoded, { qos: options.qos ?? 2 }).catch(() => {})
                }
            }
            if (!streamEnded)
                metaReject(new Error("stream aborted"))
            spool.unroll()
        }
        stream.once("close", cancelAndUnroll)
        stream.once("error", cancelAndUnroll)
 
        /*  register response dispatch callback (ack/nak)  */
        this.onResponse.set(`source-fetch-response:${requestId}`, (response: SourceFetchResponse) => {
            if (streamEnded)
                return
            if (response.name !== name) {
                streamEnded = true
                const error = sourceNameMismatchError("source response", response.name)
                metaReject(error)
                stream.destroy(error)
                spool.unroll()
                return
            }
            if (!lockResponder("source response", response.sender))
                return
            if (response.error) {
                streamEnded = true
                const error = new Error(response.error)
                metaReject(error)
                stream.destroy(error)
                spool.unroll()
            }
            else {
                responseAcked = true
                metaResolve(response.meta)
                refreshTimeout()
            }
        })
 
        /*  register chunk dispatch callback (data chunks)  */
        this.onResponse.set(`source-fetch-chunk:${requestId}`, (response: SourceFetchChunk) => {
            if (streamEnded)
                return
            if (response.name !== name) {
                streamEnded = true
                const error = sourceNameMismatchError("source chunk", response.name)
                metaReject(error)
                stream.destroy(error)
                spool.unroll()
                return
            }
            if (!lockResponder("source chunk", response.sender))
                return
            if (!responseAcked) {
                streamEnded = true
                const error = new Error("received source chunk before source response acknowledgement")
                metaReject(error)
                stream.destroy(error)
                spool.unroll()
                return
            }
            if (response.error) {
                streamEnded = true
                const error = new Error(response.error)
                metaReject(error)
                stream.destroy(error)
                spool.unroll()
            }
            else {
                refreshTimeout()
                if (response.chunk !== undefined) {
                    if (chunkCredit > 0 && chunksReceived >= creditGranted) {
                        streamEnded = true
                        const error = new Error("flow control violation")
                        metaReject(error)
                        stream.destroy(error)
                        spool.unroll()
                        return
                    }
                    chunksReceived++
                    if (!stream.destroyed)
                        stream.push(response.chunk)
                }
                if (response.final) {
                    streamEnded = true
                    if (!stream.destroyed)
                        stream.push(null)
                    spool.unroll()
                }
            }
        })
        spool.roll(() => {
            this.onResponse.delete(`source-fetch-response:${requestId}`)
            this.onResponse.delete(`source-fetch-chunk:${requestId}`)
        })
 
        /*  generate encoded message  */
        const auth = this.authenticate()
        const metaStore = this.metaStore(meta)
        const credit = chunkCredit > 0 ? chunkCredit : undefined
        const request = this.msg.makeSourceFetchRequest(requestId,
            name, params, this.options.id, receiver, auth, metaStore, credit)
        const message = this.codec.encode(request)
 
        /*  generate corresponding MQTT topic  */
        const topic = this.options.topicMake(name, "source-fetch-request", receiver)
 
        /*  publish message to MQTT topic
            (no spool passed to run() — on failure, stream.destroy() triggers
            cleanup via the stream's "close"/"error" handlers above)  */
        run(`publish fetch request as MQTT message to topic "${topic}"`, () =>
            this.publishToTopic(topic, message, { qos: 2, ...options })
        ).catch((err: unknown) => {
            const error = ensureError(err)
            metaReject(error)
            stream.destroy(error)
        })
 
        /*  produce result  */
        const result = { stream, buffer, meta: metaP }
        makeMutuallyExclusiveFields(result, "stream", "buffer", (field) => {
            if (field === "stream")
                stream.stopCollecting()
            else if (field === "buffer")
                stream.resume() /*  drain readable side  */
        })
        return result
    }
}