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 | 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 14x 14x 14x 1x 14x 14x 14x 14x 14x 14x 14x 4x 2x 2x 2x 14x 14x 1x 529x 266x 6x 6x 6x 260x 260x 266x 529x 529x 1x 808x 808x 808x 1x 6x 6x 1x 6x 6x 6x 6x 6x 6x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 260x 260x 260x 260x 260x 260x 260x 260x 260x 6x 6x 1x 1x 6x 6x 6x 6x 6x 6x 6x 6x 6x 39x 39x 39x 39x 39x 39x 261x 261x 261x 261x 261x 255x 255x 255x 255x 261x 261x 39x 6x 6x 6x 4x 4x 4x 4x 6x 1x 1x 1x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 1x 16x 16x 16x 15x 16x 16x 16x 16x 16x 16x 9x 9x 1x 9x 8x 8x 8x 9x 16x 16x 16x 16x | /* ** 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 { Buffer } from "node:buffer" import { Readable } from "node:stream" import type { ReadableOptions } from "node:stream" /* external requirements */ import PLazyAPI from "p-lazy" /* workaround for ESM-only module "p-lazy" which, when used in the context of MQTT+'s CJS build (e.g. inside test suite), exports via "default" */ export const PLazy = ((PLazyAPI as { default?: typeof PLazyAPI }).default ?? PLazyAPI) as typeof PLazyAPI /* credit-based flow control gate for chunk producers */ export class CreditGate { /* internal state */ private remaining: number private waiters: Array<(aborted: boolean) => void> = [] private aborted = false /* initialization */ constructor (initialCredit: number) { this.remaining = initialCredit } /* acquire one unit of credit (wait if exhausted) */ async acquire (abortSignal?: AbortSignal): Promise<void> { if (this.aborted) throw new Error("credit gate aborted") if (abortSignal?.aborted) throw abortSignal.reason ?? new Error("aborted") if (this.remaining > 0) /* directly take a remaining credit */ this.remaining-- else /* wait for credit to be replenished */ await new Promise<void>((resolve, reject) => { const onAbort = () => { const idx = this.waiters.indexOf(waiter) if (idx !== -1) this.waiters.splice(idx, 1) reject(abortSignal?.reason ?? new Error("aborted")) } if (abortSignal) abortSignal.addEventListener("abort", onAbort, { once: true }) const waiter = (aborted: boolean) => { if (abortSignal) abortSignal.removeEventListener("abort", onAbort) if (aborted) { reject(new Error("credit gate aborted")) return } this.remaining-- resolve() } this.waiters.push(waiter) }) } /* replenish credit (called when credit message received) */ replenish (amount: number): void { this.remaining += amount while (this.waiters.length > 0 && this.remaining > 0) this.waiters.shift()!(false) } /* release any waiting producer (for cleanup on error/abort) */ abort (): void { this.aborted = true while (this.waiters.length > 0) this.waiters.shift()!(true) } } /* reusable encoder instance */ const textEncoder = new TextEncoder() /* concatenate elements of a Uint8Array array */ function uint8ArrayConcat (arrays: Uint8Array[]) { const totalLength = arrays.reduce((acc, value) => acc + value.byteLength, 0) const result = new Uint8Array(totalLength) let offset = 0 for (const a of arrays) { result.set(a, offset) offset += a.byteLength } return result } /* utility function for converting a chunk to a buffer */ function chunkToBuffer (chunk: unknown): Uint8Array { let buffer: Uint8Array if (chunk instanceof Buffer) buffer = new Uint8Array(chunk.buffer, chunk.byteOffset, chunk.byteLength) else if (chunk instanceof Uint8Array) buffer = chunk else if (typeof chunk === "string") buffer = textEncoder.encode(chunk) else throw new Error("invalid chunk type: expected Buffer, Uint8Array, or string") return buffer } /* Readable stream that eagerly tees pushed data into an internal buffer without consuming the readable side */ export class ReadableTee extends Readable { private _chunks: Uint8Array[] = [] private _collecting = true private _onRead?: (size: number) => void private _resolve!: (value: Uint8Array) => void private _reject!: (reason: Error) => void readonly buffer: Promise<Uint8Array> constructor (opts?: ReadableOptions & { read?: (size: number) => void }) { super(opts) this._onRead = opts?.read this.buffer = new Promise<Uint8Array>((resolve, reject) => { this._resolve = resolve this._reject = reject }) this.on("error", (err: Error) => { if (this._collecting) { this._collecting = false this._reject(err) } }) } override push (chunk: any, encoding?: BufferEncoding): boolean { if (this._collecting) { if (chunk === null) { this._collecting = false this._resolve(uint8ArrayConcat(this._chunks)) } else if (chunk !== undefined) this._chunks.push(chunkToBuffer(chunk)) } return super.push(chunk, encoding) } override read (size?: number): any { this._onRead?.(size ?? 0) return super.read(size) } get collecting (): boolean { return this._collecting } stopCollecting (): void { if (!this._collecting) return this._reject(new Error("collecting stopped")) this._collecting = false this._chunks.length = 0 } } /* callback type for sending chunks */ type SendChunkCallback = ( chunk: Uint8Array | undefined, error: string | undefined, final: boolean ) => Promise<void> /* utility function for sending a buffer as chunks */ export async function sendBufferAsChunks ( buffer: Uint8Array, chunkSize: number, sendChunk: SendChunkCallback, creditGate?: CreditGate, abortSignal?: AbortSignal ): Promise<void> { if (buffer.byteLength === 0) await sendChunk(undefined, undefined, true) else { for (let i = 0; i < buffer.byteLength; i += chunkSize) { if (abortSignal?.aborted) throw abortSignal.reason ?? new Error("aborted") const size = Math.min(buffer.byteLength - i, chunkSize) const chunk = buffer.subarray(i, i + size) const final = (i + size >= buffer.byteLength) if (creditGate) await creditGate.acquire(abortSignal) await sendChunk(chunk, undefined, final) } } } /* utility function for sending a Readable stream as chunks */ export async function sendStreamAsChunks ( readable: Readable, chunkSize: number, sendChunk: SendChunkCallback, creditGate?: CreditGate, abortSignal?: AbortSignal ): Promise<void> { let pending: Uint8Array | undefined for await (const raw of readable) { if (abortSignal?.aborted) throw abortSignal.reason ?? new Error("aborted") const buffer = chunkToBuffer(raw) if (buffer.byteLength === 0) continue for (let i = 0; i < buffer.byteLength; i += chunkSize) { if (abortSignal?.aborted) throw abortSignal.reason ?? new Error("aborted") const size = Math.min(buffer.byteLength - i, chunkSize) const chunk = buffer.subarray(i, i + size) if (pending !== undefined) { if (creditGate) await creditGate.acquire(abortSignal) await sendChunk(pending, undefined, false) } pending = chunk } } if (abortSignal?.aborted) throw abortSignal.reason ?? new Error("aborted") if (pending !== undefined) { if (creditGate) await creditGate.acquire(abortSignal) await sendChunk(pending, undefined, true) } else await sendChunk(undefined, undefined, true) } /* utility function for making two object fields mutually exclusive */ export function makeMutuallyExclusiveFields<T extends object>( obj: T, f1Name: keyof T & string, f2Name: keyof T & string, onConsumed?: (field: string) => void ): void { if (!(typeof obj === "object" && obj !== null)) throw new Error("invalid object") let consumed: "f1" | "f2" | undefined const f1Value = obj[f1Name] const f2Value = obj[f2Name] Object.defineProperty(obj, f1Name, { get: () => { if (consumed === "f2") throw new Error(`field "${f1Name}" is mutually exclusive with ` + `field "${f2Name}" and "${f2Name}" was already consumed`) if (!consumed) onConsumed?.(f1Name) consumed = "f1" return f1Value }, enumerable: true, configurable: true }) Object.defineProperty(obj, f2Name, { get: () => { if (consumed === "f1") throw new Error(`field "${f2Name}" is mutually exclusive with ` + `field "${f1Name}" and "${f1Name}" was already consumed`) if (!consumed) onConsumed?.(f2Name) consumed = "f2" return f2Value }, enumerable: true, configurable: true }) } |