Skip to content

Commit

Permalink
add: node to web
Browse files Browse the repository at this point in the history
  • Loading branch information
Snowflake107 committed May 12, 2021
1 parent b4f0cab commit 5369c06
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 25 deletions.
21 changes: 13 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,32 @@
Based on **[Borewit/readable-web-to-node-stream](https://github.com/Borewit/readable-web-to-node-stream)**.

# Example
## Web streams to node stream

```js
import Converter from "https://deno.land/x/streamconv/mod.ts";

const res = await fetch("https://mysite.com");

if (res.body) {
const stream = new Converter(res.body); // Readable stream instance
const stream = Converter.WebToReadable(res.body); // Readable stream instance
stream.on("data", chunk => console.log(chunk.toString()));
} else {
console.log("no stream");
}
```

# License
(The MIT License)
## Node stream to web stream

Copyright (c) 2019 Borewit

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
```js
import Converter from "https://deno.land/x/streamconv/mod.ts";

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
const res = await fetch("https://mysite.com");

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
if (res.body) {
const stream = Converter.WebToReadable(res.body);
const webstream = Converter.WebToReadable(stream); // web ReadableStream instance
} else {
console.log("no stream");
}
```
11 changes: 8 additions & 3 deletions mod.ts
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;
19 changes: 5 additions & 14 deletions src/Conv.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,10 @@
/*
based on https://github.com/Borewit/readable-web-to-node-stream
License:
(The MIT License)
Copyright (c) 2019 Borewit
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

import { Readable } from "https://deno.land/std@0.88.0/node/stream.ts";
import { Readable } from "https://deno.land/std/node/stream.ts";

export class StreamConv extends Readable {
class StreamConv extends Readable {

public bytesRead: number = 0;
public released = false;
Expand Down Expand Up @@ -61,4 +50,6 @@ export class StreamConv extends Readable {
}
}

export default StreamConv;
export function convert(stream: ReadableStream) {
return new StreamConv(stream);
}
61 changes: 61 additions & 0 deletions src/toWeb.ts
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 })
}

0 comments on commit 5369c06

Please sign in to comment.