microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/networkInspector/requestBodyFormatters/utils.ts
40lines · modeblame
4bb0956eRedMickey5 years ago | 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. | |
| 3 | | |
09f6024fHeniker4 years ago | 4 | /* eslint-disable */ |
| 5 | /* eslint-enable prettier/prettier*/ | |
| 6 | | |
4bb0956eRedMickey5 years ago | 7 | import { TextDecoder } from "util"; |
| 8 | import { Base64 } from "js-base64"; | |
| 9 | | |
| 10 | /** | |
| 11 | * @preserve | |
| 12 | * Start region: the code is borrowed from https://github.com/facebook/flipper/blob/v0.79.1/desktop/plugins/network/chunks.tsx#L12-L28 | |
| 13 | * | |
| 14 | * Copyright (c) Facebook, Inc. and its affiliates. | |
| 15 | * | |
| 16 | * This source code is licensed under the MIT license found in the | |
| 17 | * LICENSE file in the root directory of this source tree. | |
| 18 | * | |
| 19 | * @format | |
| 20 | */ | |
| 21 | | |
| 22 | export function combineBase64Chunks(chunks: string[]): string { | |
| 23 | const byteArray = chunks.map( | |
| 24 | b64Chunk => Uint8Array.from(Base64.atob(b64Chunk), c => c.charCodeAt(0)).buffer, | |
| 25 | ); | |
| 26 | const size = byteArray.map(b => b.byteLength).reduce((prev, curr) => prev + curr, 0); | |
| 27 | const buffer = new Uint8Array(size); | |
| 28 | let offset = 0; | |
| 29 | for (let i = 0; i < byteArray.length; i++) { | |
| 30 | buffer.set(new Uint8Array(byteArray[i]), offset); | |
| 31 | offset += byteArray[i].byteLength; | |
| 32 | } | |
| 33 | const data = new TextDecoder("utf-8").decode(buffer); | |
| 34 | return data; | |
| 35 | } | |
| 36 | | |
| 37 | /** | |
| 38 | * @preserve | |
| 39 | * End region: https://github.com/facebook/flipper/blob/v0.79.1/desktop/plugins/network/chunks.tsx#L12-L28 | |
| 40 | */ |