-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Snowflake107
committed
May 12, 2021
1 parent
b4f0cab
commit 5369c06
Showing
4 changed files
with
87 additions
and
25 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 |
---|---|---|
@@ -1,4 +1,9 @@ | ||
import Conv from "./src/Conv.ts"; | ||
import { convert as WebToReadable } from "./src/Conv.ts"; | ||
import { toWebStream as ReadableToWeb } from "./src/toWeb.ts"; | ||
|
||
export default Conv; | ||
export { Conv as Converter } | ||
export const Converter = { | ||
WebToReadable, | ||
ReadableToWeb | ||
} | ||
|
||
export default Converter; |
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,61 @@ | ||
/* | ||
based on https://github.com/xuset/readable-stream-node-to-web | ||
*/ | ||
|
||
import { Readable as NodeLikeReadable } from "https://deno.land/std/node/stream.ts"; | ||
|
||
interface ListenerInterface { | ||
data: (chunk: any) => void; | ||
end: (chunk: any) => void; | ||
close: (err: any) => void; | ||
error: (err: any) => void; | ||
} | ||
|
||
export function toWebStream(nodeStream: NodeLikeReadable) { | ||
let destroyed = false; | ||
const listeners = {} as ListenerInterface; | ||
|
||
function start(controller: any) { | ||
listeners['data'] = onData; | ||
listeners['end'] = onData; | ||
listeners['end'] = onDestroy; | ||
listeners['close'] = onDestroy; | ||
listeners['error'] = onDestroy; | ||
for (const name in listeners) nodeStream.on(name, listeners[name as keyof ListenerInterface]) | ||
|
||
nodeStream.pause() | ||
|
||
function onData(chunk: any) { | ||
if (destroyed) return | ||
controller.enqueue(new Uint8Array(chunk)) | ||
nodeStream.pause() | ||
} | ||
|
||
function onDestroy(err: any) { | ||
if (destroyed) return | ||
destroyed = true | ||
|
||
for (let name in listeners) nodeStream.removeListener(name, listeners[name as keyof ListenerInterface]) | ||
|
||
if (err) controller.error(err) | ||
else controller.close() | ||
} | ||
} | ||
|
||
function pull() { | ||
if (destroyed) return | ||
nodeStream.resume() | ||
} | ||
|
||
function cancel() { | ||
destroyed = true | ||
|
||
for (const name in listeners) nodeStream.removeListener(name, listeners[name as keyof ListenerInterface]) | ||
|
||
nodeStream.push(null) | ||
nodeStream.pause() | ||
if (nodeStream.destroy) nodeStream.destroy() | ||
} | ||
|
||
return new ReadableStream({ start: start, pull: pull, cancel: cancel }) | ||
} |