Skip to content

Commit

Permalink
fix(@aws-amplify/interactions): use pako instead of fflate in RN (#10278
Browse files Browse the repository at this point in the history
)

* fix(interactions): use pako instead of fflate in RN
  • Loading branch information
ashwinkumar6 committed Sep 1, 2022
1 parent 840086d commit 0c804e4
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,28 @@ describe('Interactions', () => {
);
expect.assertions(1);
});

test('send obj text and obj voice messages in wrong format', async () => {
// obj text in wrong format
await expect(
provider.sendMessage('BookTrip', {
content: createBlob(),
options: {
messageType: 'text',
},
})
).rejects.toEqual('invalid content type');

// obj voice in wrong format
await expect(
provider.sendMessage('BookTrip', {
content: 'Hi',
options: {
messageType: 'voice',
},
})
).rejects.toEqual('invalid content type');
});
});

// attach 'onComplete' callback to bot
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,28 @@ describe('Interactions', () => {
);
expect.assertions(1);
});

test('send obj text and obj voice messages in wrong format', async () => {
// obj text in wrong format
await expect(
provider.sendMessage('BookTrip', {
content: createBlob(),
options: {
messageType: 'text',
},
})
).rejects.toEqual('invalid content type');

// obj voice in wrong format
await expect(
provider.sendMessage('BookTrip', {
content: 'Hi',
options: {
messageType: 'voice',
},
})
).rejects.toEqual('invalid content type');
});
});

// Test 'onComplete' API
Expand Down
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
5 changes: 4 additions & 1 deletion packages/interactions/src/Providers/AWSLexProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,10 @@ export class AWSLexProvider extends AbstractInteractionsProvider {
content,
options: { messageType },
} = message;
if (messageType === 'voice' && typeof content === 'object') {
if (messageType === 'voice') {
if (typeof content !== 'object') {
return Promise.reject('invalid content type');
}
const inputStream =
content instanceof Uint8Array ? content : await convert(content);

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));
});
});
};
6 changes: 5 additions & 1 deletion packages/interactions/src/Providers/AWSLexV2Provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,11 @@ export class AWSLexV2Provider extends AbstractInteractionsProvider {
let params: RecognizeUtteranceCommandInput;

// prepare params
if (messageType === 'voice' && typeof content === 'object') {
if (messageType === 'voice') {
if (typeof content !== 'object') {
return Promise.reject('invalid content type');
}

const inputStream =
content instanceof Uint8Array ? content : await convert(content);

Expand Down

0 comments on commit 0c804e4

Please sign in to comment.