UNPKG

2.54 kBJavaScriptView Raw
1// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2// SPDX-License-Identifier: Apache-2.0
3import { __awaiter, __generator } from "tslib";
4import { decode } from 'base-64';
5import { ungzip } from 'pako';
6export var convert = function (stream) { return __awaiter(void 0, void 0, void 0, function () {
7 return __generator(this, function (_a) {
8 if (!(stream instanceof Blob)) {
9 return [2 /*return*/, Promise.reject('Invalid content type')];
10 }
11 return [2 /*return*/, new Promise(function (resolve, reject) { return __awaiter(void 0, void 0, void 0, function () {
12 var fileReaderInstance_1;
13 return __generator(this, function (_a) {
14 try {
15 fileReaderInstance_1 = new FileReader();
16 fileReaderInstance_1.readAsDataURL(stream);
17 fileReaderInstance_1.onload = function () { return __awaiter(void 0, void 0, void 0, function () {
18 var blobURL, base64Blob, decodedArrayBuffer;
19 return __generator(this, function (_a) {
20 blobURL = fileReaderInstance_1.result;
21 base64Blob = blobURL.split(/,(.*)/s)[1];
22 decodedArrayBuffer = base64ToArrayBuffer(base64Blob);
23 resolve(decodedArrayBuffer);
24 return [2 /*return*/];
25 });
26 }); };
27 }
28 catch (error) {
29 reject('unable to convert blob to arrayBuffer: ' + error);
30 }
31 return [2 /*return*/];
32 });
33 }); })];
34 });
35}); };
36export var base64ToArrayBuffer = function (base64) {
37 var binaryString = decode(base64);
38 return Uint8Array.from(binaryString, function (c) { return c.charCodeAt(0); });
39};
40export var gzipDecompressToString = function (data) { return __awaiter(void 0, void 0, void 0, function () {
41 return __generator(this, function (_a) {
42 return [2 /*return*/, new Promise(function (resolve, reject) {
43 try {
44 var result = ungzip(data, { to: 'string' });
45 resolve(result);
46 }
47 catch (error) {
48 reject('unable to decompress' + error);
49 }
50 })];
51 });
52}); };
53//# sourceMappingURL=utils.native.js.map
\No newline at end of file