Skip to content

Commit 681d746

Browse files
authored
fix: blob datatype issues where blob as arraybufferlike had no bytelength (#182)
* fix: remove unused package from lock * fix: fixed blob data type issue where blob as arraybufferlike had no bytelength in the browser * fix: remove erroneous browser check * fix: we need node 18 to be blob friendly
1 parent 05f8210 commit 681d746

File tree

6 files changed

+22
-102
lines changed

6 files changed

+22
-102
lines changed

.github/workflows/CI.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ jobs:
3232
strategy:
3333
matrix:
3434
os: [ubuntu-latest]
35-
node: ["16"]
35+
node: ["18"]
3636

3737
runs-on: ${{ matrix.os }}
3838

.github/workflows/docs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
strategy:
1313
matrix:
1414
os: [ubuntu-latest]
15-
node: ["16"]
15+
node: ["18"]
1616

1717
runs-on: ${{ matrix.os }}
1818

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
name: Release / Node ${{ matrix.node }}
1616
strategy:
1717
matrix:
18-
node: ["16"]
18+
node: ["18"]
1919

2020
runs-on: ubuntu-latest
2121

package-lock.json

Lines changed: 3 additions & 93 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
"dist",
3030
"src"
3131
],
32+
"engines": {
33+
"node": ">=18.0.0"
34+
},
3235
"main": "dist/main/index.js",
3336
"module": "dist/module/index.js",
3437
"types": "dist/module/index.d.ts",

src/packages/LiveClient.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ import { EventEmitter } from "events";
22
import { appendSearchParams, isBrowser } from "../lib/helpers";
33
import WebSocket from "modern-isomorphic-ws";
44
import { LiveConnectionState, LiveTranscriptionEvents } from "../lib/enums";
5+
import { DEFAULT_HEADERS } from "../lib/constants";
6+
import { DeepgramError } from "../lib/errors";
7+
58
import type {
69
LiveSchema,
710
LiveConfigOptions,
811
LiveMetadataEvent,
912
LiveTranscriptionEvent,
1013
} from "../lib/types";
11-
import { DEFAULT_HEADERS } from "../lib/constants";
1214

1315
export class LiveClient extends EventEmitter {
1416
private _socket: WebSocket;
@@ -101,16 +103,21 @@ export class LiveClient extends EventEmitter {
101103
* Sends data to the Deepgram API via websocket connection
102104
* @param data Audio data to send to Deepgram
103105
*
104-
* Conforms to RFC #146 - does not send an empty byte.
106+
* Conforms to RFC #146 for Node.js - does not send an empty byte.
107+
* In the browser, a Blob will contain length with no audio.
105108
* @see https://github.com/deepgram/deepgram-python-sdk/issues/146
106109
*/
107-
public send(data: string | ArrayBufferLike | ArrayBufferView): void {
110+
public send(data: string | ArrayBufferLike | Blob): void {
108111
if (this._socket.readyState === LiveConnectionState.OPEN) {
109112
if (typeof data === "string") {
110113
this._socket.send(data); // send text data
114+
} else if ((data as any) instanceof Blob) {
115+
this._socket.send(data as unknown as ArrayBufferLike); // send blob data
111116
} else {
112-
if (data.byteLength > 0) {
113-
this._socket.send(data); // send buffer when not zero-byte
117+
const buffer = data as ArrayBufferLike;
118+
119+
if (buffer.byteLength > 0) {
120+
this._socket.send(buffer); // send buffer when not zero-byte (or browser)
114121
} else {
115122
this.emit(
116123
LiveTranscriptionEvents.Warning,
@@ -119,7 +126,7 @@ export class LiveClient extends EventEmitter {
119126
}
120127
}
121128
} else {
122-
this.emit(LiveTranscriptionEvents.Error, "Could not send. Connection not open.");
129+
throw new DeepgramError("Could not send. Connection not open.");
123130
}
124131
}
125132

0 commit comments

Comments
 (0)