summaryrefslogtreecommitdiffstats
path: root/test/utils/stream.js
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--test/utils/stream.js48
1 files changed, 48 insertions, 0 deletions
diff --git a/test/utils/stream.js b/test/utils/stream.js
new file mode 100644
index 0000000..b78ff5c
--- /dev/null
+++ b/test/utils/stream.js
@@ -0,0 +1,48 @@
+'use strict'
+
+const { Readable, Writable } = require('stream')
+
+let ReadableStream
+
+function createReadable (data) {
+ return new Readable({
+ read () {
+ this.push(Buffer.from(data))
+ this.push(null)
+ }
+ })
+}
+
+function createWritable (target) {
+ return new Writable({
+ write (chunk, _, callback) {
+ target.push(chunk.toString())
+ callback()
+ },
+ final (callback) {
+ callback()
+ }
+ })
+}
+
+class Source {
+ constructor (data) {
+ this.data = data
+ }
+
+ async start (controller) {
+ this.controller = controller
+ }
+
+ async pull (controller) {
+ controller.enqueue(this.data)
+ controller.close()
+ }
+}
+
+function createReadableStream (data) {
+ ReadableStream = require('stream/web').ReadableStream
+ return new ReadableStream(new Source(data))
+}
+
+module.exports = { createReadableStream, createReadable, createWritable }