libopus-wasm 0.1.0 → 0.3.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.
- package/CHANGELOG.md +16 -1
- package/README.md +5 -2
- package/dist/discordjs.d.ts.map +1 -1
- package/dist/generated/libopus.generated.mjs +0 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +69 -18
- package/package.json +9 -5
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,21 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
-
##
|
|
3
|
+
## 0.3.0 - 2026-09-05
|
|
4
|
+
|
|
5
|
+
**Highlights:** Safer encoder initialization and packet-size handling, plus reliable documentation navigation. This release requires Node.js 22 or newer.
|
|
6
|
+
|
|
7
|
+
- Reject `maxPacketBytes` values outside signed 32-bit bounds so WASM allocation cannot wrap and poison the packet scratch buffer. (#11) Thanks @SebTardif.
|
|
8
|
+
- Free native encoders when post-create option setters fail, and validate `complexity`, `packetLossPercent`, and `signal` before native encoder creation. (#10) Thanks @SebTardif.
|
|
9
|
+
- Generate documentation table-of-contents links from rendered headings so code-formatted, repeated, and punctuated headings link correctly. (#7) Thanks @vincentkoc.
|
|
10
|
+
- Raise the minimum supported Node.js version from 20 to 22.
|
|
11
|
+
- Refresh pnpm to 11.25.0, TypeScript to 7.0.2, Node.js types to 26.4.1, Vite to 8.2.2, and Vitest and V8 coverage to 5.0.0; refresh transitive dependencies and GitHub Actions, build with Emscripten 6.0.9, and test through Node.js 26. Running the test suite requires Node.js 22.12 or newer.
|
|
12
|
+
- Reject dependency releases with downgraded publisher trust evidence while preserving the 48-hour release-age policy. (#8) Thanks @anupamme.
|
|
13
|
+
|
|
14
|
+
## 0.2.0 - 2026-06-01
|
|
15
|
+
|
|
16
|
+
- Add named Opus error codes, `codeName` metadata, and `isOpusError()` for structured libopus error handling.
|
|
17
|
+
- Add coverage tooling and broaden validation-path tests.
|
|
18
|
+
- Add dedicated docs pages for packet inspection (`getPacketInfo`) and error handling, list `getPacketInfo` in the API reference, and wire both into the site navigation.
|
|
4
19
|
|
|
5
20
|
## 0.1.0 - 2026-05-26
|
|
6
21
|
|
package/README.md
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# libopus-wasm
|
|
2
2
|
|
|
3
|
+

|
|
4
|
+
|
|
3
5
|
[](https://github.com/openclaw/libopus-wasm/actions/workflows/ci.yml)
|
|
4
6
|
[](https://libopus-wasm.dev)
|
|
5
7
|
|
|
@@ -24,7 +26,7 @@ packets, no Ogg/WebM container layer.
|
|
|
24
26
|
npm install libopus-wasm
|
|
25
27
|
```
|
|
26
28
|
|
|
27
|
-
ESM-only; Node
|
|
29
|
+
ESM-only; Node 22+ or any current browser. No `@types` install needed.
|
|
28
30
|
|
|
29
31
|
## Quick start
|
|
30
32
|
|
|
@@ -190,7 +192,8 @@ Read-only: `channels`, `maxFrameSize`, `sampleRate`.
|
|
|
190
192
|
`Bitrate` (`Auto`, `Max`) ·
|
|
191
193
|
`Bandwidth` (`Narrowband`…`Fullband`) ·
|
|
192
194
|
`EncoderCtl` / `DecoderCtl` request codes ·
|
|
193
|
-
`OpusError` (`code`, `operation`)
|
|
195
|
+
`OpusError` (`code`, `codeName`, `operation`) ·
|
|
196
|
+
`OpusErrorCode` · `isOpusError`.
|
|
194
197
|
|
|
195
198
|
### Supported formats
|
|
196
199
|
|
package/dist/discordjs.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"discordjs.d.ts","sourceRoot":"","sources":["../src/discordjs.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAIrC,qBAAa,WAAW;;IACtB,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAC,IAAI,CAAC,CAAC;
|
|
1
|
+
{"version":3,"file":"discordjs.d.ts","sourceRoot":"","sources":["../src/discordjs.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAIrC,qBAAa,WAAW;;IACtB,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAC,IAAI,CAAC,CAAC;IAM9B,YAAY,IAAI,SAAS,EAAE,QAAQ,SAAI,EAyBtC;IAED,OAAa,MAAM,CAAC,IAAI,SAAS,EAAE,QAAQ,SAAI,GAAG,OAAO,CAAC,WAAW,CAAC,CAIrE;IAED,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU,GAAG,MAAM,CAIvC;IAED,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU,GAAG,MAAM,CAIvC;IAED,eAAe,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI,CAEhD;IAED,eAAe,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI,CAEhD;IAED,UAAU,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI,CAEhC;IAED,UAAU,IAAI,MAAM,CAEnB;IAED,MAAM,CAAC,OAAO,EAAE,OAAO,GAAG,IAAI,CAE7B;IAED,MAAM,CAAC,UAAU,EAAE,MAAM,GAAG,IAAI,CAE/B;IAED,IAAI,IAAI,IAAI,CAOX;IAED,CAAC,MAAM,CAAC,OAAO,CAAC,IAAI,IAAI,CAEvB;CAwBF"}
|
|
Binary file
|
package/dist/index.d.ts
CHANGED
|
@@ -135,7 +135,19 @@ export declare function createDecoder(options?: DecoderOptions): Promise<OpusDec
|
|
|
135
135
|
export declare function getPacketInfo(packet: Uint8Array, options?: PacketInfoOptions): Promise<OpusPacketInfo>;
|
|
136
136
|
export declare class OpusError extends Error {
|
|
137
137
|
readonly code: number;
|
|
138
|
+
readonly codeName: OpusErrorCodeName | undefined;
|
|
138
139
|
readonly operation: string | undefined;
|
|
139
140
|
constructor(code: number, message: string, operation?: string);
|
|
140
141
|
}
|
|
142
|
+
export declare const OpusErrorCode: {
|
|
143
|
+
readonly BadArg: -1;
|
|
144
|
+
readonly BufferTooSmall: -2;
|
|
145
|
+
readonly InternalError: -3;
|
|
146
|
+
readonly InvalidPacket: -4;
|
|
147
|
+
readonly Unimplemented: -5;
|
|
148
|
+
readonly InvalidState: -6;
|
|
149
|
+
readonly AllocFail: -7;
|
|
150
|
+
};
|
|
151
|
+
export type OpusErrorCodeName = keyof typeof OpusErrorCode;
|
|
152
|
+
export declare function isOpusError(error: unknown): error is OpusError;
|
|
141
153
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,WAAW
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,WAAW;aACtB,IAAI,EAAE,IAAI;aACV,KAAK,EAAE,IAAI;aACX,kBAAkB,EAAE,IAAI;CAChB,CAAC;AAEX,eAAO,MAAM,MAAM;aACjB,IAAI,EAAE,CAAC,IAAI;aACX,KAAK,EAAE,IAAI;aACX,KAAK,EAAE,IAAI;CACH,CAAC;AAEX,eAAO,MAAM,OAAO;aAClB,IAAI,EAAE,CAAC,IAAI;aACX,GAAG,EAAE,CAAC,CAAC;CACC,CAAC;AAEX,eAAO,MAAM,SAAS;aACpB,UAAU,EAAE,IAAI;aAChB,UAAU,EAAE,IAAI;aAChB,QAAQ,EAAE,IAAI;aACd,aAAa,EAAE,IAAI;aACnB,QAAQ,EAAE,IAAI;CACN,CAAC;AAEX,eAAO,MAAM,UAAU;aACrB,cAAc,EAAE,IAAI;aACpB,UAA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AAE,iBAAsB,GAC9B,OAAO,CAAC,cAAc,CAAC,CA2CzB;AA8cD,qBAAa,SAAU,SAAQ,KAAK;IAClC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,QAAQ,EAAE,iBAAiB,GAAG,SAAS,CAAC;IACjD,QAAQ,CAAC,SAAS,EAAE,MAAM,GAAG,SAAS,CAAC;IAEvC,YAAY,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,MAAM,EAM5D;CACF;AAED,eAAO,MAAM,aAAa;aACxB,MAAM,EAAE,CAAC,CAAC;aACV,cAAc,EAAE,CAAC,CAAC;aAClB,aAAa,EAAE,CAAC,CAAC;aACjB,aAAa,EAAE,CAAC,CAAC;aACjB,aAAa,EAAE,CAAC,CAAC;aACjB,YAAY,EAAE,CAAC,CAAC;aAChB,SAAS,EAAE,CAAC,CAAC;CACL,CAAC;AAEX,MAAM,MAAM,iBAAiB,GAAG,MAAM,OAAO,aAAa,CAAC;AAE3D,wBAAgB,WAAW,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,SAAS,CAoB9D"}
|
package/dist/index.js
CHANGED
|
@@ -46,6 +46,7 @@ const DEFAULT_CHANNELS = 2;
|
|
|
46
46
|
const DEFAULT_FRAME_DURATION_MS = 20;
|
|
47
47
|
const MAX_PACKET_DURATION_MS = 120;
|
|
48
48
|
const DEFAULT_MAX_PACKET_BYTES = 4000;
|
|
49
|
+
const MAX_I32 = 0x7fff_ffff;
|
|
49
50
|
const DEFAULT_SAMPLE_RATE = 48_000;
|
|
50
51
|
const DECODER_INTEGER_CTL_REQUESTS = new Set(Object.values(DecoderCtl));
|
|
51
52
|
const ENCODER_INTEGER_CTL_REQUESTS = new Set(Object.values(EncoderCtl));
|
|
@@ -139,20 +140,29 @@ class WasmOpusEncoder {
|
|
|
139
140
|
finally {
|
|
140
141
|
module._free(errorPtr);
|
|
141
142
|
}
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
this.
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
this.
|
|
143
|
+
let configured = false;
|
|
144
|
+
try {
|
|
145
|
+
this.setBitrate(options.bitrate);
|
|
146
|
+
this.setComplexity(options.complexity);
|
|
147
|
+
this.setDtx(options.dtx);
|
|
148
|
+
this.setFec(options.fec);
|
|
149
|
+
if (options.maxBandwidth !== undefined) {
|
|
150
|
+
this.setMaxBandwidth(options.maxBandwidth);
|
|
151
|
+
}
|
|
152
|
+
this.setPacketLossPercent(options.packetLossPercent);
|
|
153
|
+
this.setSignal(options.signal);
|
|
154
|
+
if (options.vbr !== undefined) {
|
|
155
|
+
this.setVbr(options.vbr);
|
|
156
|
+
}
|
|
157
|
+
if (options.vbrConstraint !== undefined) {
|
|
158
|
+
this.setVbrConstraint(options.vbrConstraint);
|
|
159
|
+
}
|
|
160
|
+
configured = true;
|
|
153
161
|
}
|
|
154
|
-
|
|
155
|
-
|
|
162
|
+
finally {
|
|
163
|
+
if (!configured) {
|
|
164
|
+
this.free();
|
|
165
|
+
}
|
|
156
166
|
}
|
|
157
167
|
}
|
|
158
168
|
encode(pcm, options = {}) {
|
|
@@ -165,7 +175,7 @@ class WasmOpusEncoder {
|
|
|
165
175
|
throw new RangeError(`PCM frame has ${pcmBytes.byteLength} bytes; expected ${expectedBytes} for ${frameSize} samples and ${this.channels} channel(s)`);
|
|
166
176
|
}
|
|
167
177
|
const maxPacketBytes = options.maxPacketBytes ?? DEFAULT_MAX_PACKET_BYTES;
|
|
168
|
-
|
|
178
|
+
validateIntegerRange(maxPacketBytes, 1, MAX_I32, "maxPacketBytes");
|
|
169
179
|
const pcmPtr = this.#ensurePcmBytes(pcmBytes.byteLength);
|
|
170
180
|
const packetPtr = this.#ensurePacketBytes(maxPacketBytes);
|
|
171
181
|
this.#module.HEAPU8.set(pcmBytes, pcmPtr);
|
|
@@ -184,7 +194,7 @@ class WasmOpusEncoder {
|
|
|
184
194
|
throw new RangeError(`Float32 PCM frame has ${pcm.length} samples; expected ${expectedSamples} for ${frameSize} samples and ${this.channels} channel(s)`);
|
|
185
195
|
}
|
|
186
196
|
const maxPacketBytes = options.maxPacketBytes ?? DEFAULT_MAX_PACKET_BYTES;
|
|
187
|
-
|
|
197
|
+
validateIntegerRange(maxPacketBytes, 1, MAX_I32, "maxPacketBytes");
|
|
188
198
|
const pcmPtr = this.#ensurePcmBytes(pcm.byteLength);
|
|
189
199
|
const packetPtr = this.#ensurePacketBytes(maxPacketBytes);
|
|
190
200
|
this.#module.HEAPF32.set(pcm, pcmPtr >> 2);
|
|
@@ -483,18 +493,50 @@ class WasmOpusDecoder {
|
|
|
483
493
|
}
|
|
484
494
|
export class OpusError extends Error {
|
|
485
495
|
code;
|
|
496
|
+
codeName;
|
|
486
497
|
operation;
|
|
487
498
|
constructor(code, message, operation) {
|
|
488
499
|
super(message);
|
|
489
500
|
this.name = "OpusError";
|
|
490
501
|
this.code = code;
|
|
502
|
+
this.codeName = resolveOpusErrorCodeName(code);
|
|
491
503
|
this.operation = operation;
|
|
492
504
|
}
|
|
493
505
|
}
|
|
506
|
+
export const OpusErrorCode = {
|
|
507
|
+
BadArg: -1,
|
|
508
|
+
BufferTooSmall: -2,
|
|
509
|
+
InternalError: -3,
|
|
510
|
+
InvalidPacket: -4,
|
|
511
|
+
Unimplemented: -5,
|
|
512
|
+
InvalidState: -6,
|
|
513
|
+
AllocFail: -7,
|
|
514
|
+
};
|
|
515
|
+
export function isOpusError(error) {
|
|
516
|
+
if (error instanceof OpusError) {
|
|
517
|
+
return true;
|
|
518
|
+
}
|
|
519
|
+
const candidate = error;
|
|
520
|
+
return (Boolean(error) &&
|
|
521
|
+
typeof error === "object" &&
|
|
522
|
+
candidate.name === "OpusError" &&
|
|
523
|
+
typeof candidate.message === "string" &&
|
|
524
|
+
typeof candidate.code === "number" &&
|
|
525
|
+
(typeof candidate.codeName === "string" || candidate.codeName === undefined) &&
|
|
526
|
+
(typeof candidate.operation === "string" || candidate.operation === undefined));
|
|
527
|
+
}
|
|
494
528
|
async function getModule() {
|
|
495
529
|
modulePromise ??= createLibopusModule();
|
|
496
530
|
return await modulePromise;
|
|
497
531
|
}
|
|
532
|
+
function resolveOpusErrorCodeName(code) {
|
|
533
|
+
for (const [name, value] of Object.entries(OpusErrorCode)) {
|
|
534
|
+
if (value === code) {
|
|
535
|
+
return name;
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
return undefined;
|
|
539
|
+
}
|
|
498
540
|
function createOpusError(module, code, operation) {
|
|
499
541
|
const message = module.UTF8ToString(module._oc_strerror(code));
|
|
500
542
|
return new OpusError(code, `libopus ${operation} failed (${code}): ${message}`, operation);
|
|
@@ -513,18 +555,26 @@ function normalizeEncoderOptions(options) {
|
|
|
513
555
|
if (options.maxBandwidth !== undefined) {
|
|
514
556
|
validateBandwidth(options.maxBandwidth, "maxBandwidth");
|
|
515
557
|
}
|
|
558
|
+
const complexity = options.complexity ?? 10;
|
|
559
|
+
validateIntegerRange(complexity, 0, 10, "complexity");
|
|
560
|
+
const packetLossPercent = options.packetLossPercent ?? 0;
|
|
561
|
+
validateIntegerRange(packetLossPercent, 0, 100, "packetLossPercent");
|
|
562
|
+
const signal = options.signal ?? Signal.Auto;
|
|
563
|
+
if (!Object.values(Signal).includes(signal)) {
|
|
564
|
+
throw new RangeError("signal must be Signal.Auto, Signal.Voice, or Signal.Music");
|
|
565
|
+
}
|
|
516
566
|
return {
|
|
517
567
|
application: options.application ?? Application.Audio,
|
|
518
568
|
bitrate: normalizeBitrate(options.bitrate ?? 64_000),
|
|
519
569
|
channels,
|
|
520
|
-
complexity
|
|
570
|
+
complexity,
|
|
521
571
|
dtx: options.dtx ?? false,
|
|
522
572
|
fec: options.fec ?? false,
|
|
523
573
|
frameSize,
|
|
524
574
|
maxBandwidth: options.maxBandwidth,
|
|
525
|
-
packetLossPercent
|
|
575
|
+
packetLossPercent,
|
|
526
576
|
sampleRate,
|
|
527
|
-
signal
|
|
577
|
+
signal,
|
|
528
578
|
vbr: options.vbr,
|
|
529
579
|
vbrConstraint: options.vbrConstraint,
|
|
530
580
|
};
|
|
@@ -607,6 +657,7 @@ function validateIntegerRange(value, min, max, name) {
|
|
|
607
657
|
}
|
|
608
658
|
}
|
|
609
659
|
function checkedMalloc(module, bytes) {
|
|
660
|
+
validateIntegerRange(bytes, 1, MAX_I32, "malloc size");
|
|
610
661
|
const ptr = module._malloc(bytes);
|
|
611
662
|
if (ptr === 0) {
|
|
612
663
|
throw new Error(`WASM malloc failed for ${bytes} bytes`);
|
package/package.json
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "libopus-wasm",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"packageManager": "pnpm@11.25.0",
|
|
4
5
|
"description": "Small, modern WASM bindings for libopus raw packet encode/decode.",
|
|
5
6
|
"type": "module",
|
|
6
7
|
"sideEffects": false,
|
|
@@ -42,15 +43,18 @@
|
|
|
42
43
|
"docs:build": "node scripts/build-docs-site.mjs",
|
|
43
44
|
"prepack": "pnpm build",
|
|
44
45
|
"test": "pnpm build:wasm && vitest run",
|
|
46
|
+
"test:coverage": "pnpm build:wasm && vitest run --coverage",
|
|
45
47
|
"typecheck": "tsc -p tsconfig.json --noEmit"
|
|
46
48
|
},
|
|
47
49
|
"devDependencies": {
|
|
48
50
|
"@discordjs/opus": "0.10.0",
|
|
49
|
-
"@types/node": "
|
|
50
|
-
"
|
|
51
|
-
"
|
|
51
|
+
"@types/node": "26.4.1",
|
|
52
|
+
"@vitest/coverage-v8": "5.0.0",
|
|
53
|
+
"typescript": "^7.0.2",
|
|
54
|
+
"vite": "8.2.2",
|
|
55
|
+
"vitest": "5.0.0"
|
|
52
56
|
},
|
|
53
57
|
"engines": {
|
|
54
|
-
"node": ">=
|
|
58
|
+
"node": ">=22"
|
|
55
59
|
}
|
|
56
60
|
}
|