MCPcopy Create free account
hub / github.com/denoland/std / #decodeThree

Method #decodeThree

cbor/sequence_decoder_stream.ts:305–357  ·  view source on GitHub ↗
(
    aI: number,
  )

Source from the content-addressed store, hash-verified

303 }
304
305 async #decodeThree(
306 aI: number,
307 ): Promise<string | [CborTextDecodedStream, lock: Promise<unknown>]> {
308 if (aI < 24) return new TextDecoder().decode(await this.#read(aI, true));
309 if (aI <= 27) {
310 const bytes = arrayToNumber(
311 (await this.#read(2 ** (aI - 24), true)).buffer,
312 true,
313 );
314 // Strings can't be as long as Uint8Arrays so a lower bound is set before switching to a stream.
315 if (bytes > 2 ** 16) {
316 let releaseLock: ReleaseLock = () => {};
317 const lock = new Promise((x) => releaseLock = x);
318 return [
319 new CborTextDecodedStream(
320 async function* (gen) {
321 const decoder = new TextDecoder();
322 for await (const chunk of gen) {
323 yield decoder.decode(chunk, { stream: true });
324 }
325 }(this.#readGen(BigInt(bytes))),
326 releaseLock,
327 ),
328 lock,
329 ];
330 } else {
331 return new TextDecoder().decode(
332 await this.#read(Number(bytes), true),
333 );
334 }
335 }
336 if (aI === 31) {
337 let releaseLock: ReleaseLock = () => {};
338 const lock = new Promise((x) => releaseLock = x);
339 return [
340 new CborTextDecodedStream(
341 async function* (gen) {
342 for await (const x of gen) {
343 if (typeof x === "string") yield x;
344 else if (x instanceof CborTextDecodedStream) {
345 for await (const y of x) yield y;
346 } else throw new TypeError("Unexpected type in CBOR text string");
347 }
348 }(this.#readIndefinite(true, "")),
349 releaseLock,
350 ),
351 lock,
352 ];
353 }
354 throw new RangeError(
355 `Cannot decode value (0b011_${aI.toString(2).padStart(5, "0")})`,
356 );
357 }
358
359 async #decodeFour(
360 aI: number,

Callers 1

#decodeMethod · 0.95

Calls 5

#readMethod · 0.95
#readGenMethod · 0.95
#readIndefiniteMethod · 0.95
arrayToNumberFunction · 0.90
toStringMethod · 0.45

Tested by

no test coverage detected