This is an automated email from the ASF dual-hosted git repository.
suyanhanx pushed a commit to branch nodejs-stream
in repository https://gitbox.apache.org/repos/asf/incubator-opendal.git
The following commit(s) were added to refs/heads/nodejs-stream by this push:
new 5e57c8a90 instance method to create stream
5e57c8a90 is described below
commit 5e57c8a906e162a86a598808c82d044d1b4eae14
Author: suyanhanx <[email protected]>
AuthorDate: Mon Nov 27 17:25:09 2023 +0800
instance method to create stream
Signed-off-by: suyanhanx <[email protected]>
---
bindings/nodejs/index.js | 42 ++++++++++++++++++++++++-----
bindings/nodejs/tests/suites/sync.suite.mjs | 6 ++---
2 files changed, 39 insertions(+), 9 deletions(-)
diff --git a/bindings/nodejs/index.js b/bindings/nodejs/index.js
index 67eea2c1d..448ea597e 100644
--- a/bindings/nodejs/index.js
+++ b/bindings/nodejs/index.js
@@ -21,10 +21,21 @@
const { Writable, Readable } = require('node:stream')
-class ReaderStream extends Readable {
+class ReadStream extends Readable {
constructor(operator, path, options) {
super(options)
- this.reader = operator.readerSync(path)
+ this.operator = operator
+ this.path = path
+ this.reader = null
+ }
+
+ _construct(callback) {
+ try {
+ this.reader = this.operator.readerSync(this.path)
+ callback()
+ } catch (e) {
+ callback(e)
+ }
}
_read(size) {
@@ -42,10 +53,21 @@ class ReaderStream extends Readable {
}
}
-class WriterStream extends Writable {
+class WriteStream extends Writable {
constructor(operator, path, options) {
super(options)
- this.writer = operator.writerSync(path)
+ this.operator = operator
+ this.path = path
+ this.writer = null
+ }
+
+ _construct(callback) {
+ try {
+ this.writer = this.operator.writerSync(this.path)
+ callback()
+ } catch (e) {
+ callback(e)
+ }
}
_write(chunk, encoding, callback) {
@@ -69,9 +91,17 @@ class WriterStream extends Writable {
const { Operator, RetryLayer } = require('./generated.js')
+Operator.prototype.createWriteStream = function (path, options) {
+ return new WriteStream(this, path, options)
+}
+
+Operator.prototype.createReadStream = function (path, options) {
+ return new ReadStream(this, path, options)
+}
+
module.exports.Operator = Operator
module.exports.layers = {
RetryLayer,
}
-module.exports.WriterStream = WriterStream
-module.exports.ReaderStream = ReaderStream
+module.exports.WriteStream = WriteStream
+module.exports.ReadStream = ReadStream
diff --git a/bindings/nodejs/tests/suites/sync.suite.mjs
b/bindings/nodejs/tests/suites/sync.suite.mjs
index aab6df45f..298f199a7 100644
--- a/bindings/nodejs/tests/suites/sync.suite.mjs
+++ b/bindings/nodejs/tests/suites/sync.suite.mjs
@@ -19,7 +19,7 @@
import { randomUUID } from 'node:crypto'
import { test } from 'vitest'
-import { WriterStream, ReaderStream } from '../../index.js'
+import { WriteStream, ReadStream } from '../../index.js'
import { generateFixedBytes } from '../utils.mjs'
import { Readable } from 'node:stream'
@@ -41,7 +41,7 @@ export function run(op) {
const r = Readable.from(buf, {
highWaterMark: 5 * 1024 * 1024, // to buffer 5MB data to read
})
- const w = new WriterStream(op, filename)
+ const w = op.createWriteStream(filename)
r.pipe(w)
w.on('finish', () => {
@@ -61,7 +61,7 @@ export function run(op) {
await op.write(filename, c)
- const r = new ReaderStream(op, filename)
+ const r = op.createReadStream(filename)
let chunks = []
r.on('data', (chunk) => {
chunks.push(chunk)