Skip to content

Commit

Permalink
Merge pull request #26 from AssemblyAI/E07417BDFEA3614F5967B1520F8B2F61
Browse files Browse the repository at this point in the history
Release 4.0.0
  • Loading branch information
Swimburger authored Dec 8, 2023
2 parents 52eadc2 + 2249e14 commit 5dcdb1c
Show file tree
Hide file tree
Showing 23 changed files with 216 additions and 76 deletions.
3 changes: 3 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"semi": true
}
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

### Added

- Add `browser` and `browser:min` exports, at `dist/index.umd.js` and `dist/index.umd.min.js`. These exports are browser compatible versions of the SDK, with a few limitations. You can't use the file system and you have to use a temporary auth token with the real-time transcriber.
- Add `node`, `deno`, `bun`, `browser`, and `workerd` (Cloudflare Workers) exports to package.json. These exports are compatible versions of the SDK, with a few limitations in some cases. For more details, consult the [SDK Compatibility document](./docs/compat.md).
- Add `dist/assemblyai.umd.js` and `dist/assemblyai.umd.min.js`. You can reference these script files directly in the browser and the SDK will be available at the global `assemblyai` variable.

### Changed

Expand Down
7 changes: 6 additions & 1 deletion docs/compat.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
The JavaScript SDK is developed for Node.js but is also compatible with other runtimes
such as the browser, Deno, Bun, Cloudflare Workers, etc.

## Node.js compatibility

The SDK supports Node.js 18, 20, and 21.
If you do use an older version of Node.js like version 16, you'll need to polyfill `fetch`.

## Browser compatibility

To make the SDK compatible with the browser, the SDK aims to use web standards as much as possible.
Expand Down Expand Up @@ -40,7 +45,7 @@ However, there are still incompatibilities between Node.js and the browser.
});
```

- You can't pass local audio file paths to `client.files.upload`, `client.transcripts.transcribe`, and `client.transcripts.submit`. If you do, you'll get the following error: "Function is not supported in this environment.".
- You can't pass local audio file paths to `client.files.upload`, `client.transcripts.transcribe`, and `client.transcripts.submit`. If you do, you'll get the following error: "Interacting with the file system is not supported in this environment.".
If you want to transcribe audio files, you must use a public URL, a stream, or a buffer.

> [!WARNING]
Expand Down
8 changes: 8 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
collectCoverage: true,
modulePathIgnorePatterns: ["<rootDir>/dist"],
};

process.env.TESTDATA_DIR = "tests/static";
78 changes: 60 additions & 18 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,37 +1,75 @@
{
"name": "assemblyai",
"version": "4.0.0-beta.0",
"version": "4.0.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.",
"engines": {
"node": ">=18"
},
"exports": {
".": {
"types": "./dist/index.d.ts",
"bun": {
"types": "./dist/index.d.ts",
"default": "./dist/bun.mjs"
},
"deno": {
"types": "./dist/index.d.ts",
"default": "./dist/deno.mjs"
},
"workerd": "./dist/index.mjs",
"browser": "./dist/index.mjs",
"node": {
"types": "./dist/index.d.ts",
"import": "./dist/node.mjs",
"require": "./dist/node.cjs"
},
"import": "./dist/index.mjs",
"require": "./dist/index.cjs",
"browser": "./dist/index.browser.js",
"default": "./dist/index.cjs"
},
"./package.json": "./package.json"
},
"imports": {
"#fs": {
"node": "./src/polyfills/fs/node.ts",
"bun": "./src/polyfills/fs/bun.ts",
"deno": "./src/polyfills/fs/deno.ts",
"default": "./src/polyfills/fs/index.ts"
},
"#streams": {
"node": "./src/polyfills/streams/node.ts",
"default": "./src/polyfills/streams/index.ts"
},
"#ws": {
"types": "./src/polyfills/ws/index.d.ts",
"browser": "./src/polyfills/ws/browser.mjs",
"default": {
"types": "./src/polyfills/ws/index.d.ts",
"import": "./src/polyfills/ws/index.mjs",
"require": "./src/polyfills/ws/index.cjs"
}
}
},
"type": "commonjs",
"main": "dist/index.cjs",
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
"typings": "dist/index.d.ts",
"main": "./dist/index.cjs",
"require": "./dist/index.cjs",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"typings": "./dist/index.d.ts",
"repository": {
"type": "git",
"url": "git+https://github.com/AssemblyAI/assemblyai-node-sdk.git"
},
"publishConfig": {
"tag": "beta",
"tag": "latest",
"access": "public",
"registry": "https://registry.npmjs.org/"
},
"scripts": {
"build": "pnpm clean && pnpm rollup -c",
"clean": "rimraf dist",
"clean": "rimraf dist/*",
"lint": "eslint -c .eslintrc.json '{src,tests}/**/*.{js,ts}' && publint",
"test": "pnpm lint && pnpm test:unit",
"test:unit": "jest --config jest.config.rollup.ts",
"test": "jest --config jest.config.js",
"format": "prettier '**/*' --write",
"generate-types": "tsx ./scripts/generate-types.ts && pnpm format",
"copybara:dry-run": "./copybara.sh dry_run --init-history",
Expand All @@ -47,27 +85,35 @@
"author": "AssemblyAI (https://www.assemblyai.com)",
"license": "MIT",
"homepage": "https://www.assemblyai.com/docs",
"bugs": {
"url": "https://github.com/AssemblyAI/assemblyai-node-sdk/issues",
"email": "support@assemblyai.com"
},
"files": [
"dist",
"src"
"src",
"package.json",
"README.md",
"CHANGELOG.md",
"docs"
],
"devDependencies": {
"@rollup/plugin-alias": "^5.0.1",
"@babel/preset-env": "^7.23.5",
"@babel/preset-typescript": "^7.23.3",
"@rollup/plugin-node-resolve": "^15.2.3",
"@rollup/plugin-terser": "^0.4.4",
"@rollup/plugin-typescript": "^11.1.5",
"@types/jest": "^29.5.5",
"@types/node": "^20.5.7",
"@types/websocket": "^1.0.8",
"@types/ws": "^8.5.5",
"@typescript-eslint/eslint-plugin": "^6.7.5",
"dotenv": "^16.3.1",
"eslint": "^8.48.0",
"i": "^0.3.7",
"jest": "^29.5.0",
"jest-cli": "^29.5.0",
"jest-fetch-mock": "^3.0.3",
"jest-junit": "^16.0.0",
"jest-mock-extended": "^3.0.4",
"jest-websocket-mock": "^2.4.1",
"mock-socket": "^9.2.1",
"npm": "^9.7.1",
Expand All @@ -76,15 +122,11 @@
"publint": "^0.2.5",
"rimraf": "^5.0.1",
"rollup": "^3.25.1",
"rollup-plugin-typescript2": "^0.34.1",
"ts-jest": "^29.1.0",
"ts-node": "^10.9.1",
"tslib": "^2.5.3",
"typescript": "^5.2.2"
},
"dependencies": {
"@swimburger/isomorphic-streams": "^1.0.5",
"isomorphic-ws": "^5.0.0",
"ws": "^8.13.0"
}
}
87 changes: 54 additions & 33 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,12 @@
const pkg = require("./package.json");
const ts = require("rollup-plugin-typescript2");
const ts = require("@rollup/plugin-typescript");
const terser = require("@rollup/plugin-terser");
const alias = require("@rollup/plugin-alias");
const { nodeResolve } = require("@rollup/plugin-node-resolve");
const nodeResolve = require("@rollup/plugin-node-resolve");

const cjsFile = pkg.main;
const esmFile = pkg.module;
const browserFile = pkg.exports["."].browser;

const defaultPlugins = [
ts({
tsconfigOverride: { exclude: ["**/*.test.ts"] },
}),
];
const defaultConfig = {
plugins: defaultPlugins,
external: ["fs", "isomorphic-ws", "@swimburger/isomorphic-streams"],
const umdConfig = {
input: "src/index.ts",
};

const browserConfig = {
...defaultConfig,
plugins: [
...defaultConfig.plugins,
alias({
entries: [{ find: "fs", replacement: "./src/browser/fs.ts" }],
ts({
compilerOptions: { target: "ES2015", customConditions: ["browser"] },
}),
nodeResolve({ browser: true }),
],
Expand All @@ -33,32 +15,70 @@ const browserConfig = {

module.exports = [
{
...defaultConfig,
input: "src/index.ts",
plugins: [
// we don't know where this will be used, could be browser, could be another runtime
// so we compile to es2015 for maximum compatibility.
ts({ compilerOptions: { target: "ES2015" } }),
],
external: ["#ws"],
output: [
{
file: `./dist/index.mjs`,
format: "es",
exports: "named",
},
{
file: `./dist/index.cjs`,
format: "cjs",
exports: "named",
},
],
},
{
input: "src/index.ts",
plugins: [ts({ compilerOptions: { customConditions: ["node"] } })],
external: ["fs", "stream", "stream/web", "#ws"],
output: [
{
file: cjsFile,
file: `./dist/node.mjs`,
format: "es",
exports: "named",
},
{
file: `./dist/node.cjs`,
format: "cjs",
exports: "named",
},
],
},
{
input: "src/index.ts",
plugins: [ts({ compilerOptions: { customConditions: ["deno"] } })],
external: ["#ws"],
output: [
{
file: esmFile,
file: `./dist/deno.mjs`,
format: "es",
exports: "named",
},
],
},
{
...browserConfig,
input: "src/index.ts",
plugins: [ts({ compilerOptions: { customConditions: ["bun"] } })],
external: ["#ws"],
output: [
{
name: "assemblyai",
file: browserFile,
format: "esm",
file: `./dist/bun.mjs`,
format: "es",
exports: "named",
},
],
},
// Browser UMD build to reference directly in the browser.
{
...browserConfig,
...umdConfig,
output: [
{
name: "assemblyai",
Expand All @@ -67,9 +87,10 @@ module.exports = [
},
],
},
// Browser UMD minified build to reference directly in the browser.
{
...browserConfig,
plugins: [...browserConfig.plugins, terser()],
...umdConfig,
plugins: [...umdConfig.plugins, terser()],
output: [
{
name: "assemblyai",
Expand Down
4 changes: 2 additions & 2 deletions scripts/kitchensink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ const transcribeParams: TranscribeParams = {
await getParagraphs(transcript);
console.error("Error expected but not thrown.");
} catch (error) {
console.log("Error expected:", error.toString());
console.log("Error expected:", error);
await deleteTranscript(transcript);
}
});
Expand All @@ -145,7 +145,7 @@ const transcribeParams: TranscribeParams = {
});
console.error("Error expected but not thrown.");
} catch (error) {
console.log("Error expected:", error.toString());
console.log("Error expected:", error);
}
})();

Expand Down
8 changes: 0 additions & 8 deletions src/browser/fs.ts

This file was deleted.

8 changes: 8 additions & 0 deletions src/polyfills/fs/bun.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
declare const Bun: {
file(path: string): BunFile;
};
declare type BunFile = {
stream(): ReadableStream<Uint8Array>;
};

export const readFile = async (path: string) => Bun.file(path).stream();
9 changes: 9 additions & 0 deletions src/polyfills/fs/deno.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
declare const Deno: {
open(path: string): Promise<DenoFile>;
};
declare type DenoFile = {
readable: ReadableStream<Uint8Array>;
};

export const readFile = async (path: string) =>
(await Deno.open(path)).readable;
8 changes: 8 additions & 0 deletions src/polyfills/fs/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const readFile = async function (
// eslint-disable-next-line @typescript-eslint/no-unused-vars
path: string
): Promise<ReadableStream<Uint8Array>> {
throw new Error(
"Interacting with the file system is not supported in this environment."
);
};
7 changes: 7 additions & 0 deletions src/polyfills/fs/node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { createReadStream } from "fs";
import { Readable } from "stream";

export const readFile = async (path: string) =>
Readable.toWeb(
createReadStream(path)
) as unknown as ReadableStream<Uint8Array>;
6 changes: 6 additions & 0 deletions src/polyfills/streams/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const { WritableStream } =
typeof window !== "undefined"
? window
: typeof global !== "undefined"
? global
: globalThis;
1 change: 1 addition & 0 deletions src/polyfills/streams/node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { WritableStream } from "stream/web";
Loading

0 comments on commit 5dcdb1c

Please sign in to comment.