-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: pub/sub support for
bun
and uws
- Loading branch information
Showing
10 changed files
with
150 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
type BufferLike = string | Buffer | Uint8Array | ArrayBuffer; | ||
|
||
export function toBufferLike(val: any): BufferLike { | ||
if (val === undefined || val === null) { | ||
return ""; | ||
} | ||
|
||
if (typeof val === "string") { | ||
return val; | ||
} | ||
|
||
if (isPlainObject(val)) { | ||
return JSON.stringify(val); | ||
} | ||
|
||
return val; | ||
} | ||
|
||
// Forked from sindresorhus/is-plain-obj (MIT) | ||
// Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com) | ||
// From https://github.com/unjs/defu/blob/main/src/_utils.ts | ||
export function isPlainObject(value: unknown): boolean { | ||
if (value === null || typeof value !== "object") { | ||
return false; | ||
} | ||
const prototype = Object.getPrototypeOf(value); | ||
|
||
if ( | ||
prototype !== null && | ||
prototype !== Object.prototype && | ||
Object.getPrototypeOf(prototype) !== null | ||
) { | ||
return false; | ||
} | ||
|
||
if (Symbol.iterator in value) { | ||
return false; | ||
} | ||
|
||
if (Symbol.toStringTag in value) { | ||
return Object.prototype.toString.call(value) === "[object Module]"; | ||
} | ||
|
||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,27 @@ | ||
export class WebSocketMessage { | ||
import { toBufferLike } from "./_utils"; | ||
|
||
export class WSMessage { | ||
constructor( | ||
public readonly rawData: string | ArrayBuffer | Uint8Array, | ||
public readonly rawData: any, | ||
public readonly isBinary?: boolean, | ||
) {} | ||
|
||
text(): string { | ||
if (typeof this.rawData === "string") { | ||
return this.rawData; | ||
} | ||
return new TextDecoder().decode(this.rawData); | ||
const buff = toBufferLike(this.rawData); | ||
if (typeof buff === "string") { | ||
return buff; | ||
} | ||
return new TextDecoder().decode(buff); | ||
} | ||
|
||
toString() { | ||
return `<WebSocketMessage: ${this.text()}>`; | ||
return this.text(); | ||
} | ||
|
||
[Symbol.for("nodejs.util.inspect.custom")]() { | ||
return this.toString(); | ||
return this.text(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters