assemblyai 3.1.2 → 4.0.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
- var WebSocket = require('ws');
4
- var Stream = require('stream');
3
+ var isomorphicStreams = require('@swimburger/isomorphic-streams');
4
+ var WebSocket = require('isomorphic-ws');
5
5
  var fs = require('fs');
6
6
 
7
7
  /******************************************************************************
@@ -214,14 +214,14 @@ class RealtimeService {
214
214
  throw new Error("Already connected");
215
215
  }
216
216
  const url = this.connectionUrl();
217
- let headers;
218
217
  if (this.token) {
219
- headers = undefined;
218
+ this.socket = new WebSocket(url.toString());
220
219
  }
221
- else if (this.apiKey) {
222
- headers = { Authorization: this.apiKey };
220
+ else {
221
+ this.socket = new WebSocket(url.toString(), {
222
+ headers: { Authorization: this.apiKey },
223
+ });
223
224
  }
224
- this.socket = new WebSocket(url.toString(), { headers });
225
225
  this.socket.onclose = ({ code, reason }) => {
226
226
  var _a, _b;
227
227
  if (!reason) {
@@ -231,12 +231,12 @@ class RealtimeService {
231
231
  }
232
232
  (_b = (_a = this.listeners).close) === null || _b === void 0 ? void 0 : _b.call(_a, code, reason);
233
233
  };
234
- this.socket.onerror = (errorEvent) => {
234
+ this.socket.onerror = (event) => {
235
235
  var _a, _b, _c, _d;
236
- if (errorEvent.error)
237
- (_b = (_a = this.listeners).error) === null || _b === void 0 ? void 0 : _b.call(_a, errorEvent.error);
236
+ if (event.error)
237
+ (_b = (_a = this.listeners).error) === null || _b === void 0 ? void 0 : _b.call(_a, event.error);
238
238
  else
239
- (_d = (_c = this.listeners).error) === null || _d === void 0 ? void 0 : _d.call(_c, new Error(errorEvent.message));
239
+ (_d = (_c = this.listeners).error) === null || _d === void 0 ? void 0 : _d.call(_c, new Error(event.message));
240
240
  };
241
241
  this.socket.onmessage = ({ data }) => {
242
242
  var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
@@ -281,19 +281,26 @@ class RealtimeService {
281
281
  if (!this.socket || this.socket.readyState !== WebSocket.OPEN) {
282
282
  throw new Error("Socket is not open for communication");
283
283
  }
284
+ let audioData;
285
+ if (typeof Buffer !== "undefined") {
286
+ audioData = Buffer.from(audio).toString("base64");
287
+ }
288
+ else {
289
+ // Buffer is not available in the browser by default
290
+ // https://stackoverflow.com/a/42334410/2919731
291
+ audioData = btoa(new Uint8Array(audio).reduce((data, byte) => data + String.fromCharCode(byte), ""));
292
+ }
284
293
  const payload = {
285
- audio_data: Buffer.from(audio).toString("base64"),
294
+ audio_data: audioData,
286
295
  };
287
296
  this.socket.send(JSON.stringify(payload));
288
297
  }
289
298
  stream() {
290
- const stream = new Stream.Writable({
291
- write: (chunk, encoding, next) => {
299
+ return new isomorphicStreams.WritableStream({
300
+ write: (chunk) => {
292
301
  this.sendAudio(chunk);
293
- next();
294
302
  },
295
303
  });
296
- return stream;
297
304
  }
298
305
  close(waitForSessionTermination = true) {
299
306
  return __awaiter(this, void 0, void 0, function* () {
@@ -311,7 +318,8 @@ class RealtimeService {
311
318
  this.socket.send(terminateSessionMessage);
312
319
  }
313
320
  }
314
- this.socket.removeAllListeners();
321
+ if ("removeAllListeners" in this.socket)
322
+ this.socket.removeAllListeners();
315
323
  this.socket.close();
316
324
  }
317
325
  this.listeners = {};
@@ -1,5 +1,5 @@
1
- import WebSocket from 'ws';
2
- import Stream from 'stream';
1
+ import { WritableStream } from '@swimburger/isomorphic-streams';
2
+ import WebSocket from 'isomorphic-ws';
3
3
  import fs from 'fs';
4
4
 
5
5
  /******************************************************************************
@@ -212,14 +212,14 @@ class RealtimeService {
212
212
  throw new Error("Already connected");
213
213
  }
214
214
  const url = this.connectionUrl();
215
- let headers;
216
215
  if (this.token) {
217
- headers = undefined;
216
+ this.socket = new WebSocket(url.toString());
218
217
  }
219
- else if (this.apiKey) {
220
- headers = { Authorization: this.apiKey };
218
+ else {
219
+ this.socket = new WebSocket(url.toString(), {
220
+ headers: { Authorization: this.apiKey },
221
+ });
221
222
  }
222
- this.socket = new WebSocket(url.toString(), { headers });
223
223
  this.socket.onclose = ({ code, reason }) => {
224
224
  var _a, _b;
225
225
  if (!reason) {
@@ -229,12 +229,12 @@ class RealtimeService {
229
229
  }
230
230
  (_b = (_a = this.listeners).close) === null || _b === void 0 ? void 0 : _b.call(_a, code, reason);
231
231
  };
232
- this.socket.onerror = (errorEvent) => {
232
+ this.socket.onerror = (event) => {
233
233
  var _a, _b, _c, _d;
234
- if (errorEvent.error)
235
- (_b = (_a = this.listeners).error) === null || _b === void 0 ? void 0 : _b.call(_a, errorEvent.error);
234
+ if (event.error)
235
+ (_b = (_a = this.listeners).error) === null || _b === void 0 ? void 0 : _b.call(_a, event.error);
236
236
  else
237
- (_d = (_c = this.listeners).error) === null || _d === void 0 ? void 0 : _d.call(_c, new Error(errorEvent.message));
237
+ (_d = (_c = this.listeners).error) === null || _d === void 0 ? void 0 : _d.call(_c, new Error(event.message));
238
238
  };
239
239
  this.socket.onmessage = ({ data }) => {
240
240
  var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
@@ -279,19 +279,26 @@ class RealtimeService {
279
279
  if (!this.socket || this.socket.readyState !== WebSocket.OPEN) {
280
280
  throw new Error("Socket is not open for communication");
281
281
  }
282
+ let audioData;
283
+ if (typeof Buffer !== "undefined") {
284
+ audioData = Buffer.from(audio).toString("base64");
285
+ }
286
+ else {
287
+ // Buffer is not available in the browser by default
288
+ // https://stackoverflow.com/a/42334410/2919731
289
+ audioData = btoa(new Uint8Array(audio).reduce((data, byte) => data + String.fromCharCode(byte), ""));
290
+ }
282
291
  const payload = {
283
- audio_data: Buffer.from(audio).toString("base64"),
292
+ audio_data: audioData,
284
293
  };
285
294
  this.socket.send(JSON.stringify(payload));
286
295
  }
287
296
  stream() {
288
- const stream = new Stream.Writable({
289
- write: (chunk, encoding, next) => {
297
+ return new WritableStream({
298
+ write: (chunk) => {
290
299
  this.sendAudio(chunk);
291
- next();
292
300
  },
293
301
  });
294
- return stream;
295
302
  }
296
303
  close(waitForSessionTermination = true) {
297
304
  return __awaiter(this, void 0, void 0, function* () {
@@ -309,7 +316,8 @@ class RealtimeService {
309
316
  this.socket.send(terminateSessionMessage);
310
317
  }
311
318
  }
312
- this.socket.removeAllListeners();
319
+ if ("removeAllListeners" in this.socket)
320
+ this.socket.removeAllListeners();
313
321
  this.socket.close();
314
322
  }
315
323
  this.listeners = {};
@@ -1,4 +1,5 @@
1
1
  /// <reference types="node" />
2
+ import { WritableStream } from "@swimburger/isomorphic-streams";
2
3
  import { RealtimeServiceParams, RealtimeTranscript, PartialTranscript, FinalTranscript, SessionBeginsEventData } from "../..";
3
4
  export declare class RealtimeService {
4
5
  private realtimeUrl;
@@ -18,7 +19,7 @@ export declare class RealtimeService {
18
19
  on(event: "error", listener: (error: Error) => void): void;
19
20
  on(event: "close", listener: (code: number, reason: string) => void): void;
20
21
  connect(): Promise<SessionBeginsEventData>;
21
- sendAudio(audio: ArrayBuffer): void;
22
- stream(): NodeJS.WritableStream;
22
+ sendAudio(audio: ArrayBufferLike): void;
23
+ stream(): WritableStream<ArrayBufferLike>;
23
24
  close(waitForSessionTermination?: boolean): Promise<void>;
24
25
  }
package/package.json CHANGED
@@ -1,30 +1,35 @@
1
1
  {
2
2
  "name": "assemblyai",
3
- "version": "3.1.2",
4
- "description": "The AssemblyAI Node.js SDK provides an easy-to-use interface for interacting with the AssemblyAI API, which supports async and real-time transcription, as well as the latest LeMUR models.",
5
- "main": "dist/index.js",
6
- "module": "dist/index.esm.js",
7
- "types": "dist/index.d.ts",
8
- "typings": "dist/index.d.ts",
3
+ "version": "4.0.0-beta.0",
4
+ "description": "The AssemblyAI JavaScript SDK provides an easy-to-use interface for interacting with the AssemblyAI API, which supports async and real-time transcription, as well as the latest LeMUR models.",
9
5
  "exports": {
10
- "require": "./dist/index.js",
11
- "import": "./dist/index.esm.js",
12
- "node": "./dist/index.js",
13
- "default": "./dist/index.js"
6
+ ".": {
7
+ "types": "./dist/index.d.ts",
8
+ "import": "./dist/index.mjs",
9
+ "require": "./dist/index.cjs",
10
+ "browser": "./dist/index.browser.js",
11
+ "default": "./dist/index.cjs"
12
+ },
13
+ "./package.json": "./package.json"
14
14
  },
15
+ "type": "commonjs",
16
+ "main": "dist/index.cjs",
17
+ "module": "dist/index.mjs",
18
+ "types": "dist/index.d.ts",
19
+ "typings": "dist/index.d.ts",
15
20
  "repository": {
16
21
  "type": "git",
17
22
  "url": "git+https://github.com/AssemblyAI/assemblyai-node-sdk.git"
18
23
  },
19
24
  "publishConfig": {
20
- "tag": "latest",
25
+ "tag": "beta",
21
26
  "access": "public",
22
27
  "registry": "https://registry.npmjs.org/"
23
28
  },
24
29
  "scripts": {
25
30
  "build": "pnpm clean && pnpm rollup -c",
26
31
  "clean": "rimraf dist",
27
- "lint": "eslint -c .eslintrc.json '{src,tests}/**/*.{js,ts}'",
32
+ "lint": "eslint -c .eslintrc.json '{src,tests}/**/*.{js,ts}' && publint",
28
33
  "test": "pnpm lint && pnpm test:unit",
29
34
  "test:unit": "jest --config jest.config.rollup.ts",
30
35
  "format": "prettier '**/*' --write",
@@ -47,8 +52,12 @@
47
52
  "src"
48
53
  ],
49
54
  "devDependencies": {
55
+ "@rollup/plugin-alias": "^5.0.1",
56
+ "@rollup/plugin-node-resolve": "^15.2.3",
57
+ "@rollup/plugin-terser": "^0.4.4",
50
58
  "@types/jest": "^29.5.5",
51
59
  "@types/node": "^20.5.7",
60
+ "@types/websocket": "^1.0.8",
52
61
  "@types/ws": "^8.5.5",
53
62
  "@typescript-eslint/eslint-plugin": "^6.7.5",
54
63
  "dotenv": "^16.3.1",
@@ -64,6 +73,7 @@
64
73
  "npm": "^9.7.1",
65
74
  "openapi-typescript": "^6.6.1",
66
75
  "prettier": "^2.8.8",
76
+ "publint": "^0.2.5",
67
77
  "rimraf": "^5.0.1",
68
78
  "rollup": "^3.25.1",
69
79
  "rollup-plugin-typescript2": "^0.34.1",
@@ -73,6 +83,8 @@
73
83
  "typescript": "^5.2.2"
74
84
  },
75
85
  "dependencies": {
86
+ "@swimburger/isomorphic-streams": "^1.0.5",
87
+ "isomorphic-ws": "^5.0.0",
76
88
  "ws": "^8.13.0"
77
89
  }
78
90
  }
@@ -0,0 +1,8 @@
1
+ function throwError() {
2
+ throw new Error("Function is not supported in this environment.");
3
+ }
4
+
5
+ export const createReadStream = throwError;
6
+ export default {
7
+ createReadStream,
8
+ };
@@ -1,5 +1,3 @@
1
- // import the fs module instead if specific named exports
2
- // to keep the assemblyai module more compatible. Some fs polyfills don't include `createReadStream`.
3
1
  import fs from "fs";
4
2
  import { BaseService } from "../base";
5
3
  import { UploadedFile, FileUploadParams, FileUploadData } from "../..";
@@ -1,4 +1,6 @@
1
- import WebSocket from "ws";
1
+ import { WritableStream } from "@swimburger/isomorphic-streams";
2
+ import WebSocket from "isomorphic-ws";
3
+ import { ErrorEvent, MessageEvent, CloseEvent } from "ws";
2
4
  import {
3
5
  RealtimeEvents,
4
6
  RealtimeListeners,
@@ -14,7 +16,6 @@ import {
14
16
  RealtimeErrorMessages,
15
17
  RealtimeErrorType,
16
18
  } from "../../utils/errors";
17
- import Stream from "stream";
18
19
 
19
20
  const defaultRealtimeUrl = "wss://api.assemblyai.com/v2/realtime/ws";
20
21
 
@@ -88,16 +89,15 @@ export class RealtimeService {
88
89
 
89
90
  const url = this.connectionUrl();
90
91
 
91
- let headers;
92
92
  if (this.token) {
93
- headers = undefined;
94
- } else if (this.apiKey) {
95
- headers = { Authorization: this.apiKey };
93
+ this.socket = new WebSocket(url.toString());
94
+ } else {
95
+ this.socket = new WebSocket(url.toString(), {
96
+ headers: { Authorization: this.apiKey },
97
+ });
96
98
  }
97
99
 
98
- this.socket = new WebSocket(url.toString(), { headers });
99
-
100
- this.socket.onclose = ({ code, reason }: WebSocket.CloseEvent) => {
100
+ this.socket.onclose = ({ code, reason }: CloseEvent) => {
101
101
  if (!reason) {
102
102
  if (code in RealtimeErrorType) {
103
103
  reason = RealtimeErrorMessages[code as RealtimeErrorType];
@@ -106,12 +106,12 @@ export class RealtimeService {
106
106
  this.listeners.close?.(code, reason);
107
107
  };
108
108
 
109
- this.socket.onerror = (errorEvent: WebSocket.ErrorEvent) => {
110
- if (errorEvent.error) this.listeners.error?.(errorEvent.error as Error);
111
- else this.listeners.error?.(new Error(errorEvent.message));
109
+ this.socket.onerror = (event: ErrorEvent) => {
110
+ if (event.error) this.listeners.error?.(event.error as Error);
111
+ else this.listeners.error?.(new Error(event.message));
112
112
  };
113
113
 
114
- this.socket.onmessage = ({ data }: WebSocket.MessageEvent) => {
114
+ this.socket.onmessage = ({ data }: MessageEvent) => {
115
115
  const message = JSON.parse(data.toString()) as RealtimeMessage;
116
116
  if ("error" in message) {
117
117
  this.listeners.error?.(new RealtimeError(message.error));
@@ -150,25 +150,35 @@ export class RealtimeService {
150
150
  });
151
151
  }
152
152
 
153
- sendAudio(audio: ArrayBuffer) {
153
+ sendAudio(audio: ArrayBufferLike) {
154
154
  if (!this.socket || this.socket.readyState !== WebSocket.OPEN) {
155
155
  throw new Error("Socket is not open for communication");
156
156
  }
157
-
157
+ let audioData;
158
+ if (typeof Buffer !== "undefined") {
159
+ audioData = Buffer.from(audio).toString("base64");
160
+ } else {
161
+ // Buffer is not available in the browser by default
162
+ // https://stackoverflow.com/a/42334410/2919731
163
+ audioData = btoa(
164
+ new Uint8Array(audio).reduce(
165
+ (data, byte) => data + String.fromCharCode(byte),
166
+ ""
167
+ )
168
+ );
169
+ }
158
170
  const payload = {
159
- audio_data: Buffer.from(audio).toString("base64"),
171
+ audio_data: audioData,
160
172
  };
161
173
  this.socket.send(JSON.stringify(payload));
162
174
  }
163
175
 
164
- stream(): NodeJS.WritableStream {
165
- const stream = new Stream.Writable({
166
- write: (chunk: Buffer, encoding, next) => {
176
+ stream(): WritableStream<ArrayBufferLike> {
177
+ return new WritableStream<ArrayBufferLike>({
178
+ write: (chunk: ArrayBufferLike) => {
167
179
  this.sendAudio(chunk);
168
- next();
169
180
  },
170
181
  });
171
- return stream;
172
182
  }
173
183
 
174
184
  async close(waitForSessionTermination = true) {
@@ -185,7 +195,7 @@ export class RealtimeService {
185
195
  this.socket.send(terminateSessionMessage);
186
196
  }
187
197
  }
188
- this.socket.removeAllListeners();
198
+ if ("removeAllListeners" in this.socket) this.socket.removeAllListeners();
189
199
  this.socket.close();
190
200
  }
191
201