-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(pss): subscribe in browsers, removed readable in browsers (#180)
* fix: pss subscribe in browsers * chore: return type on isBufferArray function * test: browser pss send and receive * chore: removed unnecessary comment in config * chore: applied PR review suggestions Co-authored-by: nugaon <50576770+nugaon@users.noreply.github.com> * chore: implemented PR review suggestions * refactor: removed readable-stream since streaming for requests is not supported Co-authored-by: nugaon <50576770+nugaon@users.noreply.github.com>
- Loading branch information
1 parent
ff8d178
commit a88277d
Showing
8 changed files
with
13,429 additions
and
47 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module.exports = { | ||
launch: { | ||
dumpio: true, // Forwards browser console into test console for easier debugging | ||
}, | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* Validates input and converts to Uint8Array | ||
* | ||
* @param data any string, ArrayBuffer or Uint8Array | ||
*/ | ||
export function prepareData(data: string | ArrayBuffer | Uint8Array): Uint8Array | never { | ||
if (typeof data === 'string') return new TextEncoder().encode(data) | ||
|
||
if (data instanceof ArrayBuffer) return new Uint8Array(data) | ||
|
||
if (data instanceof Uint8Array) return data | ||
|
||
throw new TypeError('unknown data type') | ||
} | ||
|
||
export async function prepareWebsocketData(data: string | ArrayBuffer | Blob): Promise<Uint8Array> | never { | ||
if (typeof data === 'string') return new TextEncoder().encode(data) | ||
|
||
if (data instanceof ArrayBuffer) return new Uint8Array(data) | ||
|
||
if (data instanceof Blob) return new Uint8Array(await new Response(data as Blob).arrayBuffer()) | ||
|
||
throw new TypeError('unknown websocket data type') | ||
} |
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,24 +1,35 @@ | ||
import type { Readable } from 'stream' | ||
import { Readable } from 'stream' | ||
import type { Data } from 'ws' | ||
|
||
export function prepareData(data: string | ArrayBuffer | Uint8Array | Readable): Uint8Array | Readable { | ||
if (typeof data === 'string') { | ||
return new TextEncoder().encode(data) | ||
} else if (data instanceof ArrayBuffer) { | ||
return new Uint8Array(data) | ||
} | ||
/** | ||
* Validates input and converts to Uint8Array or Readable | ||
* | ||
* @param data any string, ArrayBuffer, Uint8Array or Readable | ||
*/ | ||
export function prepareData(data: string | ArrayBuffer | Uint8Array | Readable): Uint8Array | Readable | never { | ||
if (typeof data === 'string') return new TextEncoder().encode(data) | ||
|
||
return data | ||
if (data instanceof ArrayBuffer) return new Uint8Array(data) | ||
|
||
if (data instanceof Uint8Array || data instanceof Readable) return data | ||
|
||
throw new TypeError('unknown data type') | ||
} | ||
|
||
export function prepareWebsocketData(data: Data): Uint8Array { | ||
if (typeof data === 'string') { | ||
return new TextEncoder().encode(data) | ||
} else if (data instanceof Buffer) { | ||
return new Uint8Array(data) | ||
} else if (data instanceof ArrayBuffer) { | ||
return new Uint8Array(data) | ||
} else { | ||
return new Uint8Array(Buffer.concat(data)) | ||
} | ||
function isBufferArray(buffer: unknown): buffer is Buffer[] { | ||
return Array.isArray(buffer) && buffer.length > 0 && buffer.every(data => data instanceof Buffer) | ||
} | ||
|
||
export async function prepareWebsocketData(data: Data | Blob): Promise<Uint8Array> | never { | ||
if (typeof data === 'string') return new TextEncoder().encode(data) | ||
|
||
if (data instanceof Buffer) return new Uint8Array(data) | ||
|
||
if (data instanceof ArrayBuffer) return new Uint8Array(data) | ||
|
||
if (data instanceof Blob) return new Uint8Array(await new Response(data as Blob).arrayBuffer()) | ||
|
||
if (isBufferArray(data)) return new Uint8Array(Buffer.concat(data)) | ||
|
||
throw new TypeError('unknown websocket data type') | ||
} |
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