@typespec/ts-http-runtime
Version:
Isomorphic client library for making HTTP requests in node.js and browser.
1 lines • 3.6 kB
Source Map (JSON)
{"version":3,"file":"concat-react-native.mjs","sourceRoot":"","sources":["../../../src/util/concat-react-native.mts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAElC,OAAO,EAAE,mBAAmB,EAAE,MAAM,sBAAsB,CAAC;AAc3D;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,MAAM,CAAC,KAAK,UAAU,MAAM,CAAC,OAAgD;IAC3E,MAAM,KAAK,GAA0B,EAAE,CAAC;IACxC,KAAK,MAAM,MAAM,IAAI,OAAO,EAAE,CAAC;QAC7B,MAAM,QAAQ,GAAG,OAAO,MAAM,KAAK,UAAU,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC;QAClE,IAAI,QAAQ,YAAY,IAAI,IAAI,QAAQ,YAAY,UAAU,EAAE,CAAC;YAC/D,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;QACvB,CAAC;aAAM,IAAI,mBAAmB,CAAC,QAAQ,CAAC,EAAE,CAAC;YACzC,qDAAqD;YACrD,KAAK,CAAC,IAAI,CAAC,MAAM,IAAI,QAAQ,CAAC,QAAiB,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC;QAC3D,CAAC;aAAM,CAAC;YACN,MAAM,IAAI,KAAK,CAAC,4BAA4B,OAAO,QAAQ,EAAE,CAAC,CAAC;QACjE,CAAC;IACH,CAAC;IAED,qEAAqE;IACrE,uEAAuE;IACvE,4CAA4C;IAC5C,OAAO,IAAI,IAAI,CAAC,KAA0B,CAAC,CAAC;AAC9C,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nimport { isWebReadableStream } from \"#platform/typeGuards\";\n\n/**\n * Accepted binary data types for concat\n *\n * React Native doesn't natively provide ReadableStream or NodeJS.ReadableStream,\n * but apps may polyfill them. The structural stream type lets callers pass polyfilled\n * stream objects; they are handled at runtime via duck-typing (isWebReadableStream).\n *\n * @internal\n */\nexport type ConcatSource =\n Uint8Array<ArrayBuffer> | Blob | { getReader(): unknown; tee(): unknown };\n\n/**\n * Utility function that concatenates a set of binary inputs into one combined output.\n *\n * React Native runs on the Hermes engine, which implements a subset of web\n * APIs with narrower type contracts than full browsers:\n *\n * - **Blob**: natively supported, but its constructor only accepts\n * `(Blob | string)[]` — NOT the full `BlobPart[]` union. Passing a\n * `Uint8Array` to `new Blob([uint8])` throws at runtime.\n * See https://github.com/facebook/react-native/issues/44125\n *\n * - **Uint8Array**: works as a data type, but cannot be passed to the Blob\n * constructor without a polyfill (e.g. react-native-blob-jsi-helper).\n * See https://github.com/facebook/react-native/issues/41079\n *\n * - **ReadableStream / Response**: not available by default. Apps can polyfill\n * these (e.g. web-streams-polyfill + react-native-fetch-api).\n *\n * This implementation handles all source types and defers to the runtime,\n * so apps with the appropriate polyfills get correct behavior while apps\n * without them get the engine's native error.\n *\n * @param sources - array of sources for the concatenation\n * @returns a `Blob` representing all the concatenated inputs.\n *\n * @internal\n */\nexport async function concat(sources: (ConcatSource | (() => ConcatSource))[]): Promise<Blob> {\n const parts: (Blob | Uint8Array)[] = [];\n for (const source of sources) {\n const resolved = typeof source === \"function\" ? source() : source;\n if (resolved instanceof Blob || resolved instanceof Uint8Array) {\n parts.push(resolved);\n } else if (isWebReadableStream(resolved)) {\n // Requires ReadableStream + Response polyfills in RN\n parts.push(await new Response(resolved as never).blob());\n } else {\n throw new Error(`Unsupported source type: ${typeof resolved}`);\n }\n }\n\n // Hermes's Blob constructor natively accepts only (Blob | string)[].\n // Uint8Array requires a polyfill — the cast lets it through at compile\n // time and defers the check to the runtime.\n return new Blob(parts as (Blob | string)[]);\n}\n"]}