Skip to content

Commit

Permalink
fix: blob datatype issues where blob as arraybufferlike had no bytele…
Browse files Browse the repository at this point in the history
…ngth (#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
  • Loading branch information
lukeocodes committed Oct 20, 2023
1 parent 05f8210 commit 681d746
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 102 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
node: ["16"]
node: ["18"]

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

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
node: ["16"]
node: ["18"]

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

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
name: Release / Node ${{ matrix.node }}
strategy:
matrix:
node: ["16"]
node: ["18"]

runs-on: ubuntu-latest

Expand Down
96 changes: 3 additions & 93 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
"dist",
"src"
],
"engines": {
"node": ">=18.0.0"
},
"main": "dist/main/index.js",
"module": "dist/module/index.js",
"types": "dist/module/index.d.ts",
Expand Down
19 changes: 13 additions & 6 deletions src/packages/LiveClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import { EventEmitter } from "events";
import { appendSearchParams, isBrowser } from "../lib/helpers";
import WebSocket from "modern-isomorphic-ws";
import { LiveConnectionState, LiveTranscriptionEvents } from "../lib/enums";
import { DEFAULT_HEADERS } from "../lib/constants";
import { DeepgramError } from "../lib/errors";

import type {
LiveSchema,
LiveConfigOptions,
LiveMetadataEvent,
LiveTranscriptionEvent,
} from "../lib/types";
import { DEFAULT_HEADERS } from "../lib/constants";

export class LiveClient extends EventEmitter {
private _socket: WebSocket;
Expand Down Expand Up @@ -101,16 +103,21 @@ export class LiveClient extends EventEmitter {
* Sends data to the Deepgram API via websocket connection
* @param data Audio data to send to Deepgram
*
* Conforms to RFC #146 - does not send an empty byte.
* Conforms to RFC #146 for Node.js - does not send an empty byte.
* In the browser, a Blob will contain length with no audio.
* @see https://github.com/deepgram/deepgram-python-sdk/issues/146
*/
public send(data: string | ArrayBufferLike | ArrayBufferView): void {
public send(data: string | ArrayBufferLike | Blob): void {
if (this._socket.readyState === LiveConnectionState.OPEN) {
if (typeof data === "string") {
this._socket.send(data); // send text data
} else if ((data as any) instanceof Blob) {
this._socket.send(data as unknown as ArrayBufferLike); // send blob data
} else {
if (data.byteLength > 0) {
this._socket.send(data); // send buffer when not zero-byte
const buffer = data as ArrayBufferLike;

if (buffer.byteLength > 0) {
this._socket.send(buffer); // send buffer when not zero-byte (or browser)
} else {
this.emit(
LiveTranscriptionEvents.Warning,
Expand All @@ -119,7 +126,7 @@ export class LiveClient extends EventEmitter {
}
}
} else {
this.emit(LiveTranscriptionEvents.Error, "Could not send. Connection not open.");
throw new DeepgramError("Could not send. Connection not open.");
}
}

Expand Down

0 comments on commit 681d746

Please sign in to comment.