Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: buffer expected throws in bun environment #320

Merged
merged 3 commits into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export interface DataChannel extends Channel {
// Channel implementation
close(): void;
sendMessage(msg: string): boolean;
sendMessageBinary(buffer: Uint8Array): boolean;
sendMessageBinary(buffer: Buffer | Uint8Array): boolean;
isOpen(): boolean;
bufferedAmount(): number;
maxMessageSize(): number;
Expand Down
14 changes: 11 additions & 3 deletions src/polyfill/RTCDataChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,20 @@ export default class RTCDataChannel extends EventTarget implements globalThis.RT
// Needs network error, type error implemented
if (typeof data === 'string') {
this.#dataChannel.sendMessage(data);
} else if (data instanceof Blob) {
} else if (data instanceof Blob) {
data.arrayBuffer().then((ab) => {
if (process?.versions?.bun) {
this.#dataChannel.sendMessageBinary(Buffer.from(ab));
} else {
this.#dataChannel.sendMessageBinary(new Uint8Array(ab));
}
});
} else {
this.#dataChannel.sendMessageBinary(new Uint8Array(data));
} else {
if (process?.versions?.bun) {
this.#dataChannel.sendMessageBinary(Buffer.from(data));
} else {
this.#dataChannel.sendMessageBinary(new Uint8Array(data));
}
}
}

Expand Down