| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent fe65996 commit a6e1e7a
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2869,6 +2869,67 @@ added: v17.0.0 | |||
| 2869 | 2869 | * `signal` {AbortSignal} | |
| 2870 | 2870 | * Returns: {stream.Duplex} | |
| 2871 | 2871 | ||
| 2872 | + ```mjs | ||
| 2873 | + import { Duplex } from 'node:stream'; | ||
| 2874 | + import { | ||
| 2875 | + ReadableStream, | ||
| 2876 | + WritableStream | ||
| 2877 | + } from 'node:stream/web'; | ||
| 2878 | + | ||
| 2879 | + const readable = new ReadableStream({ | ||
| 2880 | + start(controller) { | ||
| 2881 | + controller.enqueue('world'); | ||
| 2882 | + }, | ||
| 2883 | + }); | ||
| 2884 | + | ||
| 2885 | + const writable = new WritableStream({ | ||
| 2886 | + write(chunk) { | ||
| 2887 | + console.log('writable', chunk); | ||
| 2888 | + } | ||
| 2889 | + }); | ||
| 2890 | + | ||
| 2891 | + const pair = { | ||
| 2892 | + readable, | ||
| 2893 | + writable | ||
| 2894 | + }; | ||
| 2895 | + const duplex = Duplex.fromWeb(pair, { encoding: 'utf8', objectMode: true }); | ||
| 2896 | + | ||
| 2897 | + duplex.write('hello'); | ||
| 2898 | + | ||
| 2899 | + for await (const chunk of duplex) { | ||
| 2900 | + console.log('readable', chunk); | ||
| 2901 | + } | ||
| 2902 | + ``` | ||
| 2903 | + | ||
| 2904 | + ```cjs | ||
| 2905 | + const { Duplex } = require('node:stream'); | ||
| 2906 | + const { | ||
| 2907 | + ReadableStream, | ||
| 2908 | + WritableStream | ||
| 2909 | + } = require('node:stream/web'); | ||
| 2910 | + | ||
| 2911 | + const readable = new ReadableStream({ | ||
| 2912 | + start(controller) { | ||
| 2913 | + controller.enqueue('world'); | ||
| 2914 | + }, | ||
| 2915 | + }); | ||
| 2916 | + | ||
| 2917 | + const writable = new WritableStream({ | ||
| 2918 | + write(chunk) { | ||
| 2919 | + console.log('writable', chunk); | ||
| 2920 | + } | ||
| 2921 | + }); | ||
| 2922 | + | ||
| 2923 | + const pair = { | ||
| 2924 | + readable, | ||
| 2925 | + writable | ||
| 2926 | + }; | ||
| 2927 | + const duplex = Duplex.fromWeb(pair, { encoding: 'utf8', objectMode: true }); | ||
| 2928 | + | ||
| 2929 | + duplex.write('hello'); | ||
| 2930 | + duplex.once('readable', () => console.log('readable', duplex.read())); | ||
| 2931 | + ``` | ||
| 2932 | + | ||
| 2872 | 2933 | ### `stream.Duplex.toWeb(streamDuplex)` | |
| 2873 | 2934 | ||
| 2874 | 2935 | <!-- YAML | |
@@ -2882,6 +2943,51 @@ added: v17.0.0 | |||
| 2882 | 2943 | * `readable` {ReadableStream} | |
| 2883 | 2944 | * `writable` {WritableStream} | |
| 2884 | 2945 | ||
| 2946 | + ```mjs | ||
| 2947 | + import { Duplex } from 'node:stream'; | ||
| 2948 | + | ||
| 2949 | + const duplex = Duplex({ | ||
| 2950 | + objectMode: true, | ||
| 2951 | + read() { | ||
| 2952 | + this.push('world'); | ||
| 2953 | + this.push(null); | ||
| 2954 | + }, | ||
| 2955 | + write(chunk, encoding, callback) { | ||
| 2956 | + console.log('writable', chunk); | ||
| 2957 | + callback(); | ||
| 2958 | + } | ||
| 2959 | + }); | ||
| 2960 | + | ||
| 2961 | + const { readable, writable } = Duplex.toWeb(duplex); | ||
| 2962 | + writable.getWriter().write('hello'); | ||
| 2963 | + | ||
| 2964 | + const { value } = await readable.getReader().read(); | ||
| 2965 | + console.log('readable', value); | ||
| 2966 | + ``` | ||
| 2967 | + | ||
| 2968 | + ```cjs | ||
| 2969 | + const { Duplex } = require('node:stream'); | ||
| 2970 | + | ||
| 2971 | + const duplex = Duplex({ | ||
| 2972 | + objectMode: true, | ||
| 2973 | + read() { | ||
| 2974 | + this.push('world'); | ||
| 2975 | + this.push(null); | ||
| 2976 | + }, | ||
| 2977 | + write(chunk, encoding, callback) { | ||
| 2978 | + console.log('writable', chunk); | ||
| 2979 | + callback(); | ||
| 2980 | + } | ||
| 2981 | + }); | ||
| 2982 | + | ||
| 2983 | + const { readable, writable } = Duplex.toWeb(duplex); | ||
| 2984 | + writable.getWriter().write('hello'); | ||
| 2985 | + | ||
| 2986 | + readable.getReader().read().then((result) => { | ||
| 2987 | + console.log('readable', result.value); | ||
| 2988 | + }); | ||
| 2989 | + ``` | ||
| 2990 | + | ||
| 2885 | 2991 | ### `stream.addAbortSignal(signal, stream)` | |
| 2886 | 2992 | ||
| 2887 | 2993 | <!-- YAML | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -20,9 +20,10 @@ | |||
| 20 | 20 | // USE OR OTHER DEALINGS IN THE SOFTWARE. | |
| 21 | 21 | 'use strict'; | |
| 22 | 22 | ||
| 23 | - require('../common'); | ||
| 23 | + const common = require('../common'); | ||
| 24 | 24 | const assert = require('assert'); | |
| 25 | 25 | const Duplex = require('stream').Duplex; | |
| 26 | + const { ReadableStream, WritableStream } = require('stream/web'); | ||
| 26 | 27 | ||
| 27 | 28 | const stream = new Duplex({ objectMode: true }); | |
| 28 | 29 | ||
@@ -53,3 +54,80 @@ process.on('exit', () => { | |||
| 53 | 54 | assert.strictEqual(read.val, 1); | |
| 54 | 55 | assert.strictEqual(written.val, 2); | |
| 55 | 56 | }); | |
| 57 | + | ||
| 58 | + // Duplex.fromWeb | ||
| 59 | + { | ||
| 60 | + const dataToRead = Buffer.from('hello'); | ||
| 61 | + const dataToWrite = Buffer.from('world'); | ||
| 62 | + | ||
| 63 | + const readable = new ReadableStream({ | ||
| 64 | + start(controller) { | ||
| 65 | + controller.enqueue(dataToRead); | ||
| 66 | + }, | ||
| 67 | + }); | ||
| 68 | + | ||
| 69 | + const writable = new WritableStream({ | ||
| 70 | + write: common.mustCall((chunk) => { | ||
| 71 | + assert.strictEqual(chunk, dataToWrite); | ||
| 72 | + }) | ||
| 73 | + }); | ||
| 74 | + | ||
| 75 | + const pair = { readable, writable }; | ||
| 76 | + const duplex = Duplex.fromWeb(pair); | ||
| 77 | + | ||
| 78 | + duplex.write(dataToWrite); | ||
| 79 | + duplex.once('data', common.mustCall((chunk) => { | ||
| 80 | + assert.strictEqual(chunk, dataToRead); | ||
| 81 | + })); | ||
| 82 | + } | ||
| 83 | + | ||
| 84 | + // Duplex.fromWeb - using utf8 and objectMode | ||
| 85 | + { | ||
| 86 | + const dataToRead = 'hello'; | ||
| 87 | + const dataToWrite = 'world'; | ||
| 88 | + | ||
| 89 | + const readable = new ReadableStream({ | ||
| 90 | + start(controller) { | ||
| 91 | + controller.enqueue(dataToRead); | ||
| 92 | + }, | ||
| 93 | + }); | ||
| 94 | + | ||
| 95 | + const writable = new WritableStream({ | ||
| 96 | + write: common.mustCall((chunk) => { | ||
| 97 | + assert.strictEqual(chunk, dataToWrite); | ||
| 98 | + }) | ||
| 99 | + }); | ||
| 100 | + | ||
| 101 | + const pair = { | ||
| 102 | + readable, | ||
| 103 | + writable | ||
| 104 | + }; | ||
| 105 | + const duplex = Duplex.fromWeb(pair, { encoding: 'utf8', objectMode: true }); | ||
| 106 | + | ||
| 107 | + duplex.write(dataToWrite); | ||
| 108 | + duplex.once('data', common.mustCall((chunk) => { | ||
| 109 | + assert.strictEqual(chunk, dataToRead); | ||
| 110 | + })); | ||
| 111 | + } | ||
| 112 | + // Duplex.toWeb | ||
| 113 | + { | ||
| 114 | + const dataToRead = Buffer.from('hello'); | ||
| 115 | + const dataToWrite = Buffer.from('world'); | ||
| 116 | + | ||
| 117 | + const duplex = Duplex({ | ||
| 118 | + read() { | ||
| 119 | + this.push(dataToRead); | ||
| 120 | + this.push(null); | ||
| 121 | + }, | ||
| 122 | + write: common.mustCall((chunk) => { | ||
| 123 | + assert.strictEqual(chunk, dataToWrite); | ||
| 124 | + }) | ||
| 125 | + }); | ||
| 126 | + | ||
| 127 | + const { writable, readable } = Duplex.toWeb(duplex); | ||
| 128 | + writable.getWriter().write(dataToWrite); | ||
| 129 | + | ||
| 130 | + readable.getReader().read().then(common.mustCall((result) => { | ||
| 131 | + assert.deepStrictEqual(Buffer.from(result.value), dataToRead); | ||
| 132 | + })); | ||
| 133 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments