assemblyai
Advanced tools
Comparing version 3.1.3 to 4.0.0-beta.0
/// <reference types="node" /> | ||
import { WritableStream } from "@swimburger/isomorphic-streams"; | ||
import { RealtimeServiceParams, RealtimeTranscript, PartialTranscript, FinalTranscript, SessionBeginsEventData } from "../.."; | ||
@@ -21,5 +22,5 @@ export declare class RealtimeService { | ||
connect(): Promise<SessionBeginsEventData>; | ||
sendAudio(audio: ArrayBuffer): void; | ||
stream(): NodeJS.WritableStream; | ||
sendAudio(audio: ArrayBufferLike): void; | ||
stream(): WritableStream<ArrayBufferLike>; | ||
close(waitForSessionTermination?: boolean): Promise<void>; | ||
} |
{ | ||
"name": "assemblyai", | ||
"version": "3.1.3", | ||
"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.", | ||
"version": "4.0.0-beta.0", | ||
"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.", | ||
"exports": { | ||
@@ -10,2 +10,3 @@ ".": { | ||
"require": "./dist/index.cjs", | ||
"browser": "./dist/index.browser.js", | ||
"default": "./dist/index.cjs" | ||
@@ -55,4 +56,8 @@ }, | ||
"devDependencies": { | ||
"@rollup/plugin-alias": "^5.0.1", | ||
"@rollup/plugin-node-resolve": "^15.2.3", | ||
"@rollup/plugin-terser": "^0.4.4", | ||
"@types/jest": "^29.5.5", | ||
"@types/node": "^20.5.7", | ||
"@types/websocket": "^1.0.8", | ||
"@types/ws": "^8.5.5", | ||
@@ -83,4 +88,6 @@ "@typescript-eslint/eslint-plugin": "^6.7.5", | ||
"dependencies": { | ||
"@swimburger/isomorphic-streams": "^1.0.5", | ||
"isomorphic-ws": "^5.0.0", | ||
"ws": "^8.13.0" | ||
} | ||
} | ||
} |
@@ -13,6 +13,7 @@ <img src="https://github.com/AssemblyAI/assemblyai-node-sdk/blob/main/assemblyai.png?raw=true" width="500"/> | ||
# AssemblyAI Node.js SDK | ||
# AssemblyAI JavaScript SDK | ||
The AssemblyAI Node.js SDK provides an easy-to-use interface for interacting with the AssemblyAI API, | ||
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. | ||
It is written primarily for Node.js in TypeScript with all types exported, but also [compatible with other runtimes](./docs/compat.md). | ||
@@ -55,3 +56,3 @@ ## Installation | ||
When you create a transcript, you can either pass in a URL to an audio file, or upload a file directly. | ||
When you create a transcript, you can either pass in a URL to an audio file or upload a file directly. | ||
@@ -247,6 +248,6 @@ ```javascript | ||
Or send audio data via a stream by piping to the realtime stream. | ||
Or send audio data via a stream by piping to the real-time stream. | ||
```typescript | ||
audioStream.pipe(rt.stream()); | ||
audioStream.pipeTo(rt.stream()); | ||
``` | ||
@@ -253,0 +254,0 @@ |
@@ -1,3 +0,1 @@ | ||
// import the fs module instead if specific named exports | ||
// to keep the assemblyai module more compatible. Some fs polyfills don't include `createReadStream`. | ||
import fs from "fs"; | ||
@@ -4,0 +2,0 @@ import { BaseService } from "../base"; |
@@ -1,2 +0,4 @@ | ||
import WebSocket from "ws"; | ||
import { WritableStream } from "@swimburger/isomorphic-streams"; | ||
import WebSocket from "isomorphic-ws"; | ||
import { ErrorEvent, MessageEvent, CloseEvent } from "ws"; | ||
import { | ||
@@ -17,3 +19,2 @@ RealtimeEvents, | ||
} from "../../utils/errors"; | ||
import Stream from "stream"; | ||
@@ -92,12 +93,11 @@ const defaultRealtimeUrl = "wss://api.assemblyai.com/v2/realtime/ws"; | ||
let headers; | ||
if (this.token) { | ||
headers = undefined; | ||
} else if (this.apiKey) { | ||
headers = { Authorization: this.apiKey }; | ||
this.socket = new WebSocket(url.toString()); | ||
} else { | ||
this.socket = new WebSocket(url.toString(), { | ||
headers: { Authorization: this.apiKey }, | ||
}); | ||
} | ||
this.socket = new WebSocket(url.toString(), { headers }); | ||
this.socket.onclose = ({ code, reason }: WebSocket.CloseEvent) => { | ||
this.socket.onclose = ({ code, reason }: CloseEvent) => { | ||
if (!reason) { | ||
@@ -111,8 +111,8 @@ if (code in RealtimeErrorType) { | ||
this.socket.onerror = (errorEvent: WebSocket.ErrorEvent) => { | ||
if (errorEvent.error) this.listeners.error?.(errorEvent.error as Error); | ||
else this.listeners.error?.(new Error(errorEvent.message)); | ||
this.socket.onerror = (event: ErrorEvent) => { | ||
if (event.error) this.listeners.error?.(event.error as Error); | ||
else this.listeners.error?.(new Error(event.message)); | ||
}; | ||
this.socket.onmessage = ({ data }: WebSocket.MessageEvent) => { | ||
this.socket.onmessage = ({ data }: MessageEvent) => { | ||
const message = JSON.parse(data.toString()) as RealtimeMessage; | ||
@@ -156,9 +156,21 @@ if ("error" in message) { | ||
sendAudio(audio: ArrayBuffer) { | ||
sendAudio(audio: ArrayBufferLike) { | ||
if (!this.socket || this.socket.readyState !== WebSocket.OPEN) { | ||
throw new Error("Socket is not open for communication"); | ||
} | ||
let audioData; | ||
if (typeof Buffer !== "undefined") { | ||
audioData = Buffer.from(audio).toString("base64"); | ||
} else { | ||
// Buffer is not available in the browser by default | ||
// https://stackoverflow.com/a/42334410/2919731 | ||
audioData = btoa( | ||
new Uint8Array(audio).reduce( | ||
(data, byte) => data + String.fromCharCode(byte), | ||
"" | ||
) | ||
); | ||
} | ||
const payload = { | ||
audio_data: Buffer.from(audio).toString("base64"), | ||
audio_data: audioData, | ||
}; | ||
@@ -168,10 +180,8 @@ this.socket.send(JSON.stringify(payload)); | ||
stream(): NodeJS.WritableStream { | ||
const stream = new Stream.Writable({ | ||
write: (chunk: Buffer, encoding, next) => { | ||
stream(): WritableStream<ArrayBufferLike> { | ||
return new WritableStream<ArrayBufferLike>({ | ||
write: (chunk: ArrayBufferLike) => { | ||
this.sendAudio(chunk); | ||
next(); | ||
}, | ||
}); | ||
return stream; | ||
} | ||
@@ -193,3 +203,3 @@ | ||
} | ||
this.socket.removeAllListeners(); | ||
if ("removeAllListeners" in this.socket) this.socket.removeAllListeners(); | ||
this.socket.close(); | ||
@@ -196,0 +206,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
252761
50
5681
266
3
29
1
5
+ Addedisomorphic-ws@^5.0.0
+ Added@swimburger/isomorphic-streams@1.1.1(transitive)
+ Addedisomorphic-ws@5.0.0(transitive)