From cc22907c75c1f7118c30637df11e68550053c97e Mon Sep 17 00:00:00 2001 From: AssemblyAI Date: Mon, 7 Oct 2024 04:34:47 -0400 Subject: [PATCH] Project import generated by Copybara. GitOrigin-RevId: 1d4d3f79c144a4ca7a97521ff2e1a8545a07799c --- README.md | 8 ++--- package.json | 53 +++++++++++++++++----------- src/services/base.ts | 11 +++--- src/services/realtime/service.ts | 8 +++++ src/types/openapi.generated.ts | 12 +++---- src/utils/conditions/browser.ts | 12 +++++++ src/utils/conditions/bun.ts | 12 +++++++ src/utils/conditions/conditions.ts | 10 ++++++ src/utils/conditions/default.ts | 12 +++++++ src/utils/conditions/deno.ts | 12 +++++++ src/utils/conditions/node.ts | 12 +++++++ src/utils/conditions/react-native.ts | 12 +++++++ src/utils/conditions/workerd.ts | 12 +++++++ tests/integration/transcript.test.ts | 3 +- tests/unit/transcript.test.ts | 3 +- 15 files changed, 153 insertions(+), 39 deletions(-) create mode 100644 src/utils/conditions/browser.ts create mode 100644 src/utils/conditions/bun.ts create mode 100644 src/utils/conditions/conditions.ts create mode 100644 src/utils/conditions/default.ts create mode 100644 src/utils/conditions/deno.ts create mode 100644 src/utils/conditions/node.ts create mode 100644 src/utils/conditions/react-native.ts create mode 100644 src/utils/conditions/workerd.ts diff --git a/README.md b/README.md index 1dcc06d..422c9da 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ [![AssemblyAI Twitter](https://img.shields.io/twitter/follow/AssemblyAI?label=%40AssemblyAI&style=social)](https://twitter.com/AssemblyAI) [![AssemblyAI YouTube](https://img.shields.io/youtube/channel/subscribers/UCtatfZMf-8EkIwASXM4ts0A)](https://www.youtube.com/@AssemblyAI) [![Discord](https://img.shields.io/discord/875120158014853141?logo=discord&label=Discord&link=https%3A%2F%2Fdiscord.com%2Fchannels%2F875120158014853141&style=social) -](https://assemblyai.com/discord) +](https://assembly.ai/discord) # AssemblyAI JavaScript SDK @@ -97,7 +97,7 @@ When you create a transcript, you can either pass in a URL to an audio file or u ```js // Transcribe file at remote URL let transcript = await client.transcripts.transcribe({ - audio: "https://storage.googleapis.com/aai-web-samples/espn-bears.m4a", + audio: "https://assembly.ai/espn.m4a", }); ``` @@ -110,7 +110,7 @@ If you don't want to wait until the transcript is ready, you can use `submit`: ```js let transcript = await client.transcripts.submit({ - audio: "https://storage.googleapis.com/aai-web-samples/espn-bears.m4a", + audio: "https://assembly.ai/espn.m4a", }); ``` @@ -151,7 +151,7 @@ For example, here's how to enable [Speaker diarization](https://www.assemblyai.c ```js let transcript = await client.transcripts.transcribe({ - audio: "https://storage.googleapis.com/aai-web-samples/espn-bears.m4a", + audio: "https://assembly.ai/espn.m4a", speaker_labels: true, }); for (let utterance of transcript.utterances) { diff --git a/package.json b/package.json index 44dfa47..8ef8308 100644 --- a/package.json +++ b/package.json @@ -56,6 +56,15 @@ "browser": "./src/polyfills/websocket/browser.ts", "node": "./src/polyfills/websocket/default.ts", "default": "./src/polyfills/websocket/default.ts" + }, + "#conditions": { + "bun": "./src/utils/conditions/bun.ts", + "deno": "./src/utils/conditions/deno.ts", + "workerd": "./src/utils/conditions/workerd.ts", + "react-native": "./src/utils/conditions/react-native.ts", + "browser": "./src/utils/conditions/browser.ts", + "node": "./src/utils/conditions/node.ts", + "default": "./src/utils/conditions/default.ts" } }, "type": "commonjs", @@ -115,19 +124,19 @@ "docs" ], "devDependencies": { - "@babel/preset-env": "^7.24.7", + "@babel/preset-env": "^7.25.4", "@babel/preset-typescript": "^7.24.7", - "@rollup/plugin-node-resolve": "^15.2.3", - "@rollup/plugin-replace": "^5.0.7", + "@rollup/plugin-node-resolve": "^15.3.0", + "@rollup/plugin-replace": "^6.0.1", "@rollup/plugin-terser": "^0.4.4", - "@rollup/plugin-typescript": "^11.1.6", - "@types/jest": "^29.5.12", - "@types/node": "^18.19.38", + "@rollup/plugin-typescript": "^12.1.0", + "@types/jest": "^29.5.13", + "@types/node": "^18.19.53", "@types/websocket": "^1.0.10", - "@types/ws": "^8.5.10", - "@typescript-eslint/eslint-plugin": "^7.13.1", + "@types/ws": "^8.5.12", + "@typescript-eslint/eslint-plugin": "^7.18.0", "dotenv": "^16.4.5", - "eslint": "^8.57.0", + "eslint": "^8.57.1", "eslint-plugin-tsdoc": "^0.3.0", "jest": "^29.7.0", "jest-cli": "^29.7.0", @@ -135,31 +144,33 @@ "jest-junit": "^16.0.0", "jest-websocket-mock": "^2.5.0", "mock-socket": "^9.3.1", - "openapi-typescript": "^6.7.5", - "prettier": "^3.3.2", - "publint": "^0.2.8", - "rimraf": "^5.0.7", - "rollup": "^4.18.0", - "ts-jest": "^29.1.5", - "tslib": "^2.6.3", - "typedoc": "^0.25.13", - "typedoc-plugin-extras": "^3.0.0", + "openapi-typescript": "^6.7.6", + "prettier": "^3.3.3", + "publint": "^0.2.11", + "rimraf": "^6.0.1", + "rollup": "^4.22.4", + "ts-jest": "^29.2.5", + "tslib": "^2.7.0", + "typedoc": "^0.26.7", + "typedoc-plugin-extras": "^3.1.0", "typescript": "^5.4.5" }, "dependencies": { - "ws": "^8.17.1" + "ws": "^8.18.0" }, "pnpm": { "packageExtensions": { "ws": { "peerDependencies": { - "@types/ws": "^8.5.10" + "@types/ws": "^8.5.12" } } }, "overrides": { "undici@<5.28.4": ">=5.28.4", - "braces@<3.0.3": ">=3.0.3" + "braces@<3.0.3": ">=3.0.3", + "micromatch@<4.0.8": ">=4.0.8", + "rollup@<2.79.2": ">=2.79.2" } } } diff --git a/src/services/base.ts b/src/services/base.ts index 3b6684c..7e7875f 100644 --- a/src/services/base.ts +++ b/src/services/base.ts @@ -1,3 +1,4 @@ +import { conditions } from "#conditions"; import { DEFAULT_FETCH_INIT } from "#fetch"; import { BaseServiceParams, Error as JsonError } from ".."; import { buildUserAgent } from "../utils/userAgent"; @@ -33,10 +34,12 @@ export abstract class BaseService { if (this.userAgent) { (headers as Record)["User-Agent"] = this.userAgent; - // chromium browsers have a bug where the user agent can't be modified - if (typeof window !== "undefined" && "chrome" in window) { - (headers as Record)["AssemblyAI-Agent"] = - this.userAgent; + if (conditions.browser || conditions.default) { + // chromium browsers have a bug where the user agent can't be modified + if (typeof window !== "undefined" && "chrome" in window) { + (headers as Record)["AssemblyAI-Agent"] = + this.userAgent; + } } } init.headers = headers; diff --git a/src/services/realtime/service.ts b/src/services/realtime/service.ts index 8695c01..f98515a 100644 --- a/src/services/realtime/service.ts +++ b/src/services/realtime/service.ts @@ -1,4 +1,5 @@ import { WritableStream } from "#streams"; +import { conditions } from "#conditions"; import { PolyfillWebSocket, factory as polyfillWebSocketFactory, @@ -190,6 +191,13 @@ export class RealtimeTranscriber { if (this.token) { this.socket = polyfillWebSocketFactory(url.toString()); } else { + if (conditions.browser) { + console.warn( + `API key authentication is not supported for the RealtimeTranscriber in browser environment. Use temporary token authentication instead. +Learn more at https://github.com/AssemblyAI/assemblyai-node-sdk/blob/main/docs/compat.md#browser-compatibility.`, + ); + } + this.socket = polyfillWebSocketFactory(url.toString(), { headers: { Authorization: this.apiKey }, }); diff --git a/src/types/openapi.generated.ts b/src/types/openapi.generated.ts index a0ca082..7ebfe04 100644 --- a/src/types/openapi.generated.ts +++ b/src/types/openapi.generated.ts @@ -1615,7 +1615,7 @@ export type TopicDetectionResult = { * "acoustic_model": "assemblyai_default", * "language_code": "en_us", * "status": "completed", - * "audio_url": "https://github.com/AssemblyAI-Examples/audio-examples/raw/main/20230607_me_canadian_wildfires.mp3", + * "audio_url": "https://assembly.ai/wildfires.mp3", * "text": "Smoke from hundreds of wildfires in Canada is triggering air quality alerts throughout the US. Skylines from Maine to Maryland to Minnesota are gray and smoggy. And in some places, the air quality warnings include the warning to stay inside. We wanted to better understand what's happening here and why, so we called Peter de Carlo, an associate professor in the Department of Environmental Health and Engineering at Johns Hopkins University Varsity. Good morning, professor. Good morning. What is it about the conditions right now that have caused this round of wildfires to affect so many people so far away? Well, there's a couple of things. The season has been pretty dry already. And then the fact that we're getting hit in the US. Is because there's a couple of weather systems that are essentially channeling the smoke from those Canadian wildfires through Pennsylvania into the Mid Atlantic and the Northeast and kind of just dropping the smoke there. So what is it in this haze that makes it harmful? And I'm assuming it is harmful. It is. The levels outside right now in Baltimore are considered unhealthy. And most of that is due to what's called particulate matter, which are tiny particles, microscopic smaller than the width of your hair that can get into your lungs and impact your respiratory system, your cardiovascular system, and even your neurological your brain. What makes this particularly harmful? Is it the volume of particulant? Is it something in particular? What is it exactly? Can you just drill down on that a little bit more? Yeah. So the concentration of particulate matter I was looking at some of the monitors that we have was reaching levels of what are, in science, big 150 micrograms per meter cubed, which is more than ten times what the annual average should be and about four times higher than what you're supposed to have on a 24 hours average. And so the concentrations of these particles in the air are just much, much higher than we typically see. And exposure to those high levels can lead to a host of health problems. And who is most vulnerable? I noticed that in New York City, for example, they're canceling outdoor activities. And so here it is in the early days of summer, and they have to keep all the kids inside. So who tends to be vulnerable in a situation like this? It's the youngest. So children, obviously, whose bodies are still developing. The elderly, who are their bodies are more in decline and they're more susceptible to the health impacts of breathing, the poor air quality. And then people who have preexisting health conditions, people with respiratory conditions or heart conditions can be triggered by high levels of air pollution. Could this get worse? That's a good question. In some areas, it's much worse than others. And it just depends on kind of where the smoke is concentrated. I think New York has some of the higher concentrations right now, but that's going to change as that air moves away from the New York area. But over the course of the next few days, we will see different areas being hit at different times with the highest concentrations. I was going to ask you about more fires start burning. I don't expect the concentrations to go up too much higher. I was going to ask you how and you started to answer this, but how much longer could this last? Or forgive me if I'm asking you to speculate, but what do you think? Well, I think the fires are going to burn for a little bit longer, but the key for us in the US. Is the weather system changing. And so right now, it's kind of the weather systems that are pulling that air into our mid Atlantic and Northeast region. As those weather systems change and shift, we'll see that smoke going elsewhere and not impact us in this region as much. And so I think that's going to be the defining factor. And I think the next couple of days we're going to see a shift in that weather pattern and start to push the smoke away from where we are. And finally, with the impacts of climate change, we are seeing more wildfires. Will we be seeing more of these kinds of wide ranging air quality consequences or circumstances? I mean, that is one of the predictions for climate change. Looking into the future, the fire season is starting earlier and lasting longer, and we're seeing more frequent fires. So, yeah, this is probably something that we'll be seeing more frequently. This tends to be much more of an issue in the Western US. So the eastern US. Getting hit right now is a little bit new. But yeah, I think with climate change moving forward, this is something that is going to happen more frequently. That's Peter De Carlo, associate professor in the Department of Environmental Health and Engineering at Johns Hopkins University. Sergeant Carlo, thanks so much for joining us and sharing this expertise with us. Thank you for having me.", * "words": [ * { @@ -2717,8 +2717,8 @@ export type TranscriptLanguageCode = * "status": "error", * "created": "2024-03-11T21:23:59.979420", * "completed": null, - * "audio_url": "https://storage.googleapis.com/client-docs-samples/nbc.mp3", - * "error": "Download error, unable to download https://storage.googleapis.com/client-docs-samples/nbc.mp3. Please make sure the file exists and is accessible from the internet." + * "audio_url": "https://storage.googleapis.com/client-docs-samples/nbc.oopsie", + * "error": "Download error, unable to download https://storage.googleapis.com/client-docs-samples/nbc.oopsie. Please make sure the file exists and is accessible from the internet." * }, * { * "id": "28a73d01-98db-41dd-9e98-2533ba0af117", @@ -2726,7 +2726,7 @@ export type TranscriptLanguageCode = * "status": "completed", * "created": "2024-03-11T21:12:57.372215", * "completed": "2024-03-11T21:13:03.267020", - * "audio_url": "https://storage.googleapis.com/aai-docs-samples/nbc.mp3", + * "audio_url": "https://assembly.ai/nbc.mp3", * "error": null * } * ] @@ -2747,7 +2747,7 @@ export type TranscriptList = { * "status": "completed", * "created": "2023-11-02T21:49:25.586965", * "completed": "2023-11-02T21:49:25.586965", - * "audio_url": "https://github.com/AssemblyAI-Examples/audio-examples/raw/main/20230607_me_canadian_wildfires.mp3", + * "audio_url": "https://assembly.ai/wildfires.mp3", * "error": null * } * ``` @@ -3039,7 +3039,7 @@ export type TranscriptParagraph = { * { * "speech_model": null, * "language_code": "en_us", - * "audio_url": "https://github.com/AssemblyAI-Examples/audio-examples/raw/main/20230607_me_canadian_wildfires.mp3", + * "audio_url": "https://assembly.ai/wildfires.mp3", * "punctuate": true, * "format_text": true, * "dual_channel": true, diff --git a/src/utils/conditions/browser.ts b/src/utils/conditions/browser.ts new file mode 100644 index 0000000..f96c4a5 --- /dev/null +++ b/src/utils/conditions/browser.ts @@ -0,0 +1,12 @@ +import { Conditions } from "./conditions"; + +export const conditions: Conditions = { + name: "browser", + default: false, + node: false, + browser: true, + bun: false, + deno: false, + workerd: false, + reactNative: false, +}; diff --git a/src/utils/conditions/bun.ts b/src/utils/conditions/bun.ts new file mode 100644 index 0000000..9f62758 --- /dev/null +++ b/src/utils/conditions/bun.ts @@ -0,0 +1,12 @@ +import { Conditions } from "./conditions"; + +export const conditions: Conditions = { + name: "bun", + default: false, + node: false, + browser: false, + bun: true, + deno: false, + workerd: false, + reactNative: false, +}; diff --git a/src/utils/conditions/conditions.ts b/src/utils/conditions/conditions.ts new file mode 100644 index 0000000..6a6b884 --- /dev/null +++ b/src/utils/conditions/conditions.ts @@ -0,0 +1,10 @@ +export type Conditions = { + name: string; + default: boolean; + node: boolean; + browser: boolean; + bun: boolean; + deno: boolean; + workerd: boolean; + reactNative: boolean; +}; diff --git a/src/utils/conditions/default.ts b/src/utils/conditions/default.ts new file mode 100644 index 0000000..99522e1 --- /dev/null +++ b/src/utils/conditions/default.ts @@ -0,0 +1,12 @@ +import { Conditions } from "./conditions"; + +export const conditions: Conditions = { + name: "default", + default: true, + node: false, + browser: false, + bun: false, + deno: false, + workerd: false, + reactNative: false, +}; diff --git a/src/utils/conditions/deno.ts b/src/utils/conditions/deno.ts new file mode 100644 index 0000000..84299da --- /dev/null +++ b/src/utils/conditions/deno.ts @@ -0,0 +1,12 @@ +import { Conditions } from "./conditions"; + +export const conditions: Conditions = { + name: "deno", + default: false, + node: false, + browser: false, + bun: false, + deno: true, + workerd: false, + reactNative: false, +}; diff --git a/src/utils/conditions/node.ts b/src/utils/conditions/node.ts new file mode 100644 index 0000000..69ea6d8 --- /dev/null +++ b/src/utils/conditions/node.ts @@ -0,0 +1,12 @@ +import { Conditions } from "./conditions"; + +export const conditions: Conditions = { + name: "node", + default: false, + node: true, + browser: false, + bun: false, + deno: false, + workerd: false, + reactNative: false, +}; diff --git a/src/utils/conditions/react-native.ts b/src/utils/conditions/react-native.ts new file mode 100644 index 0000000..cb8aee0 --- /dev/null +++ b/src/utils/conditions/react-native.ts @@ -0,0 +1,12 @@ +import { Conditions } from "./conditions"; + +export const conditions: Conditions = { + name: "react-native", + default: false, + node: false, + browser: false, + bun: false, + deno: false, + workerd: false, + reactNative: true, +}; diff --git a/src/utils/conditions/workerd.ts b/src/utils/conditions/workerd.ts new file mode 100644 index 0000000..1f77cd6 --- /dev/null +++ b/src/utils/conditions/workerd.ts @@ -0,0 +1,12 @@ +import { Conditions } from "./conditions"; + +export const conditions: Conditions = { + name: "workerd", + default: false, + node: false, + browser: false, + bun: false, + deno: false, + workerd: true, + reactNative: false, +}; diff --git a/tests/integration/transcript.test.ts b/tests/integration/transcript.test.ts index efb544e..ac41bde 100644 --- a/tests/integration/transcript.test.ts +++ b/tests/integration/transcript.test.ts @@ -4,8 +4,7 @@ import { AssemblyAI } from "../../src"; import { readFile } from "fs/promises"; const testDir = process.env["TESTDATA_DIR"] ?? "tests/static"; -const remoteAudioUrl = - "https://storage.googleapis.com/aai-web-samples/espn-bears.m4a"; +const remoteAudioUrl = "https://assembly.ai/espn.m4a"; const badRemoteAudioURL = "https://storage.googleapis.com/aai-web-samples/does-not-exist.m4a"; const knownTranscriptId = process.env.TEST_TRANSCRIPT_ID!; diff --git a/tests/unit/transcript.test.ts b/tests/unit/transcript.test.ts index 1346cfc..de6da24 100644 --- a/tests/unit/transcript.test.ts +++ b/tests/unit/transcript.test.ts @@ -16,8 +16,7 @@ const assembly = createClient(); const knownTranscriptIds = ["transcript_123"]; const transcriptId = knownTranscriptIds[0]; -const remoteAudioURL = - "https://storage.googleapis.com/aai-web-samples/espn-bears.m4a"; +const remoteAudioURL = "https://assembly.ai/espn.m4a"; const badRemoteAudioURL = "https://storage.googleapis.com/aai-web-samples/does-not-exist.m4a";