Skip to content

Commit

Permalink
fix(@aws-amplify/interactions): remove Readable dependencies (aws-a…
Browse files Browse the repository at this point in the history
  • Loading branch information
wlee221 authored Jul 23, 2020
1 parent 8c6fb4a commit 5682f22
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 59 deletions.
44 changes: 21 additions & 23 deletions packages/interactions/__tests__/Interactions-unit-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,16 @@ import {
PostContentCommand,
PostTextCommand,
} from '@aws-sdk/client-lex-runtime-service';
import { Readable } from 'stream';

(global as any).Response = () => {};
(global as any).Response.prototype.arrayBuffer = (blob: Blob) => {
return Promise.resolve(new ArrayBuffer(0));
};

// mock stream response
const createReadable = () => {
const stream = new Readable({
read() {},
});
stream.push('test');
stream.push(null);
return stream;
const createBlob = () => {
return new Blob();
};
const typedStream = new Uint8Array([116, 101, 115, 116]); // 'test' in Uint8Array

LexRuntimeServiceClient.prototype.send = jest.fn((command, callback) => {
if (command instanceof PostTextCommand) {
Expand Down Expand Up @@ -48,14 +46,14 @@ LexRuntimeServiceClient.prototype.send = jest.fn((command, callback) => {
m1: 'voice:hi',
m2: 'voice:done',
},
audioStream: createReadable(),
audioStream: createBlob(),
};
return Promise.resolve(result);
} else {
const result = {
message: 'voice:echo:' + command.input.inputStream,
dialogState: 'ElicitSlot',
audioStream: createReadable(),
audioStream: createBlob(),
};
return Promise.resolve(result);
}
Expand All @@ -68,14 +66,14 @@ LexRuntimeServiceClient.prototype.send = jest.fn((command, callback) => {
m1: 'hi',
m2: 'done',
},
audioStream: createReadable(),
audioStream: createBlob(),
};
return Promise.resolve(result);
} else {
const result = {
message: 'echo:' + command.input.inputStream,
dialogState: 'ElicitSlot',
audioStream: createReadable(),
audioStream: createBlob(),
};
return Promise.resolve(result);
}
Expand Down Expand Up @@ -367,7 +365,7 @@ describe('Interactions', () => {
expect(responseVoice).toEqual({
dialogState: 'ElicitSlot',
message: 'voice:echo:voice:hi',
audioStream: typedStream,
audioStream: new Uint8Array(),
});

const responseText = await interactions.send(
Expand All @@ -377,7 +375,7 @@ describe('Interactions', () => {
expect(responseText).toEqual({
dialogState: 'ElicitSlot',
message: 'echo:hi',
audioStream: typedStream,
audioStream: new Uint8Array(),
});
});

Expand Down Expand Up @@ -442,7 +440,7 @@ describe('Interactions', () => {
expect(responseVoice).toEqual({
dialogState: 'ElicitSlot',
message: 'voice:echo:voice:hi',
audioStream: typedStream,
audioStream: new Uint8Array(),
});

const responseText = await interactions.send(
Expand All @@ -452,7 +450,7 @@ describe('Interactions', () => {
expect(responseText).toEqual({
dialogState: 'ElicitSlot',
message: 'echo:hi',
audioStream: typedStream,
audioStream: new Uint8Array(),
});
});

Expand Down Expand Up @@ -514,7 +512,7 @@ describe('Interactions', () => {
m1: 'hi',
m2: 'done',
},
audioStream: typedStream,
audioStream: new Uint8Array(),
});
jest.runAllTimers();
});
Expand Down Expand Up @@ -564,7 +562,7 @@ describe('Interactions', () => {
m1: 'voice:hi',
m2: 'voice:done',
},
audioStream: typedStream,
audioStream: new Uint8Array(),
});
jest.runAllTimers();
});
Expand Down Expand Up @@ -623,7 +621,7 @@ describe('Interactions', () => {
m1: 'hi',
m2: 'done',
},
audioStream: typedStream,
audioStream: new Uint8Array(),
});
jest.runAllTimers();
});
Expand Down Expand Up @@ -673,7 +671,7 @@ describe('Interactions', () => {
m1: 'voice:hi',
m2: 'voice:done',
},
audioStream: typedStream,
audioStream: new Uint8Array(),
});
jest.runAllTimers();
});
Expand Down Expand Up @@ -916,7 +914,7 @@ describe('Interactions', () => {
expect(responseVoice).toEqual({
dialogState: 'ElicitSlot',
message: 'voice:echo:voice:hi',
audioStream: typedStream,
audioStream: new Uint8Array(),
});

const responseText = await interactions.send(
Expand All @@ -926,7 +924,7 @@ describe('Interactions', () => {
expect(responseText).toEqual({
dialogState: 'ElicitSlot',
message: 'echo:hi',
audioStream: typedStream,
audioStream: new Uint8Array(),
});
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,41 +1,12 @@
import { Readable } from 'stream';
export const convert = async (
stream: Readable | ReadableStream | Blob
export const convert = (
stream: Blob | Readable | ReadableStream
): Promise<Uint8Array> => {
if (stream instanceof Readable) {
return readOnNode(stream);
if (stream instanceof Blob || stream instanceof ReadableStream) {
return new Response(stream)
.arrayBuffer()
.then(buffer => new Uint8Array(buffer));
} else {
return readOnBrowser(stream);
throw new Error('Readable is not supported.');
}
};

const readOnBrowser = (stream: ReadableStream | Blob) => {
return new Response(stream)
.arrayBuffer()
.then(buffer => new Uint8Array(buffer));
};

const readOnNode = (stream: Readable): Promise<Uint8Array> => {
if (!stream.isPaused()) stream.pause();
const chunks: Array<string | Buffer> = [];
return new Promise((res, rej) => {
stream.on('readable', () => {
while (true) {
const chunk = stream.read();
if (!chunk) break;
chunks.push(chunk);
}
});
stream.on('end', () => {
const blob = new Blob(chunks);
const fileReader = new FileReader();
fileReader.onload = event => {
const buffer = event.target.result as ArrayBuffer;
return res(new Uint8Array(buffer));
};
fileReader.onerror = rej;
fileReader.readAsArrayBuffer(blob);
});
stream.on('error', rej);
});
};

0 comments on commit 5682f22

Please sign in to comment.