Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(interactions): use pako instead of fflate in RN #10278

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/interactions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@
"@aws-sdk/client-lex-runtime-service": "3.6.1",
"@aws-sdk/client-lex-runtime-v2": "3.31.0",
"base-64": "1.0.0",
"fflate": "0.7.3"
"fflate": "0.7.3",
"pako": "2.0.4"
},
"jest": {
"globals": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,15 @@
* CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
import { strFromU8 } from 'fflate';
import { base64ToArrayBuffer, gzipDecompress } from './utils';
import { base64ToArrayBuffer, gzipDecompressToString } from './utils';

export const unGzipBase64AsJson = async (gzipBase64: string | undefined) => {
if (typeof gzipBase64 === 'undefined') return undefined;

try {
const decodedArrayBuffer = base64ToArrayBuffer(gzipBase64);

const decompressedData: Uint8Array = await gzipDecompress(
decodedArrayBuffer
);

const objString = strFromU8(decompressedData, true);
const objString: string = await gzipDecompressToString(decodedArrayBuffer);

return JSON.parse(objString);
} catch (error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/

import { decode } from 'base-64';
import { gunzipSync } from 'fflate';
import { ungzip } from 'pako';

export const convert = async (stream: object): Promise<Uint8Array> => {
if (!(stream instanceof Blob)) {
Expand Down Expand Up @@ -42,6 +42,15 @@ export const base64ToArrayBuffer = (base64: string): Uint8Array => {
return Uint8Array.from(binaryString, c => c.charCodeAt(0));
};

export const gzipDecompress = async (data: Uint8Array): Promise<Uint8Array> => {
return new Promise(resolve => resolve(gunzipSync(data)));
export const gzipDecompressToString = async (
data: Uint8Array
): Promise<string> => {
return new Promise((resolve, reject) => {
try {
const result: string = ungzip(data, { to: 'string' });
resolve(result);
} catch (error) {
reject('unable to decompress' + error);
}
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* and limitations under the License.
*/

import { gunzip } from 'fflate';
import { gunzip, strFromU8 } from 'fflate';

export const convert = async (stream: object): Promise<Uint8Array> => {
if (stream instanceof Blob || stream instanceof ReadableStream) {
Expand All @@ -27,11 +27,13 @@ export const base64ToArrayBuffer = (base64: string): Uint8Array => {
return Uint8Array.from(window.atob(base64), c => c.charCodeAt(0));
};

export const gzipDecompress = async (data: Uint8Array): Promise<Uint8Array> => {
export const gzipDecompressToString = async (
data: Uint8Array
): Promise<string> => {
return await new Promise((resolve, reject) => {
gunzip(data, (err, resp) => {
if (err) reject(err);
else resolve(resp);
else resolve(strFromU8(resp));
});
});
};