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 e6bc9fb5e random bytes to read
e6bc9fb5e is described below
commit e6bc9fb5e47f189796575855a5d56da73117c558
Author: suyanhanx <[email protected]>
AuthorDate: Sat Nov 25 14:36:08 2023 +0800
random bytes to read
Signed-off-by: suyanhanx <[email protected]>
---
bindings/nodejs/index.js | 2 +-
bindings/nodejs/tests/fixtures/random | 19 ----------------
bindings/nodejs/tests/suites/async.suite.mjs | 7 ++++--
bindings/nodejs/tests/suites/sync.suite.mjs | 14 ++++++++----
bindings/nodejs/tests/utils.mjs | 34 ++++++++++++++++++++++------
5 files changed, 43 insertions(+), 33 deletions(-)
diff --git a/bindings/nodejs/index.js b/bindings/nodejs/index.js
index 8778e27bd..47ee77957 100644
--- a/bindings/nodejs/index.js
+++ b/bindings/nodejs/index.js
@@ -19,7 +19,7 @@
/// <reference types="node" />
-const { Writable } = require('node:stream')
+const { Writable, Readable } = require('node:stream')
class WriterStream extends Writable {
constructor(operator, path, options) {
diff --git a/bindings/nodejs/tests/fixtures/random
b/bindings/nodejs/tests/fixtures/random
deleted file mode 100644
index 21563e8c4..000000000
--- a/bindings/nodejs/tests/fixtures/random
+++ /dev/null
@@ -1,19 +0,0 @@
-Enjoyed minutes related as at on on. Is fanny dried as often me. Goodness as
reserved raptures to mistaken steepest oh screened he. Gravity he mr sixteen
esteems. Mile home its new way with high told said. Finished no horrible
blessing landlord dwelling dissuade if. Rent fond am he in on read. Anxious
cordial demands settled entered in do to colonel.
-
-The him father parish looked has sooner. Attachment frequently gay terminated
son. You greater nay use prudent placing. Passage to so distant behaved natural
between do talking. Friends off her windows painful. Still gay event you being
think nay for. In three if aware he point it. Effects warrant me by no on
feeling settled resolve.
-
-It as announcing it me stimulated frequently continuing. Least their she you
now above going stand forth. He pretty future afraid should genius spirit on.
Set property addition building put likewise get. Of will at sell well at as.
Too want but tall nay like old. Removing yourself be in answered he. Consider
occasion get improved him she eat. Letter by lively oh denote an.
-
-On insensible possession oh particular attachment at excellence in. The books
arose but miles happy she. It building contempt or interest children mistress
of unlocked no. Offending she contained mrs led listening resembled. Delicate
marianne absolute men dashwood landlord and offended. Suppose cottage between
and way. Minuter him own clothes but observe country. Agreement far boy
otherwise rapturous incommode favourite.
-
-Alteration literature to or an sympathize mr imprudence. Of is ferrars subject
as enjoyed or tedious cottage. Procuring as in resembled by in agreeable. Next
long no gave mr eyes. Admiration advantages no he celebrated so pianoforte
unreserved. Not its herself forming charmed amiable. Him why feebly expect
future now.
-
-It sportsman earnestly ye preserved an on. Moment led family sooner cannot her
window pulled any. Or raillery if improved landlord to speaking hastened
differed he. Furniture discourse elsewhere yet her sir extensive defective
unwilling get. Why resolution one motionless you him thoroughly. Noise is round
to in it quick timed doors. Written address greatly get attacks inhabit pursuit
our but. Lasted hunted enough an up seeing in lively letter. Had judgment out
opinions property the supplied.
-
-It real sent your at. Amounted all shy set why followed declared. Repeated of
endeavor mr position kindness offering ignorant so up. Simplicity are
melancholy preference considered saw companions. Disposal on outweigh do
speedily in on. Him ham although thoughts entirely drawings. Acceptance
unreserved old admiration projection nay yet him. Lasted am so before on esteem
vanity oh.
-
-Not far stuff she think the jokes. Going as by do known noise he wrote round
leave. Warmly put branch people narrow see. Winding its waiting yet parlors
married own feeling. Marry fruit do spite jokes an times. Whether at it unknown
warrant herself winding if. Him same none name sake had post love. An busy feel
form hand am up help. Parties it brother amongst an fortune of. Twenty behind
wicket why age now itself ten.
-
-Be me shall purse my ought times. Joy years doors all would again rooms these.
Solicitude announcing as to sufficient my. No my reached suppose proceed
pressed perhaps he. Eagerness it delighted pronounce repulsive furniture no.
Excuse few the remain highly feebly add people manner say. It high at my mind
by roof. No wonder worthy in dinner.
-
-Whole every miles as tiled at seven or. Wished he entire esteem mr oh by.
Possible bed you pleasure civility boy elegance ham. He prevent request by if
in pleased. Picture too and concern has was comfort. Ten difficult resembled
eagerness nor. Same park bore on be. Warmth his law design say are person.
Pronounce suspected in belonging conveying ye repulsive.
diff --git a/bindings/nodejs/tests/suites/async.suite.mjs
b/bindings/nodejs/tests/suites/async.suite.mjs
index b153d86c3..e232f4ca4 100644
--- a/bindings/nodejs/tests/suites/async.suite.mjs
+++ b/bindings/nodejs/tests/suites/async.suite.mjs
@@ -19,7 +19,7 @@
import { randomUUID } from 'node:crypto'
import { test } from 'vitest'
-import { generateBytes } from '../utils.mjs'
+import { generateBytes, generateFixedBytes } from '../utils.mjs'
export function run(op) {
test('async stat not exist files', async () => {
@@ -39,10 +39,13 @@ export function run(op) {
const filename = `random_file_${randomUUID()}`
const writer = await op.writer(filename)
- const data = generateBytes(1024)
+
+ const data = generateFixedBytes(1024 * 1024)
+
await writer.write(data)
await writer.write(data)
await writer.close()
+
const stat = await op.stat(filename)
assert.equal(stat.contentLength, data.length * 2)
})
diff --git a/bindings/nodejs/tests/suites/sync.suite.mjs
b/bindings/nodejs/tests/suites/sync.suite.mjs
index 6be7eaac0..d3dcf966a 100644
--- a/bindings/nodejs/tests/suites/sync.suite.mjs
+++ b/bindings/nodejs/tests/suites/sync.suite.mjs
@@ -19,9 +19,8 @@
import { randomUUID } from 'node:crypto'
import { test } from 'vitest'
-import fs from 'node:fs'
-import path from 'node:path'
import { WriterStream } from '../../index.js'
+import { BufferStream, generateFixedBytes } from '../utils.mjs'
export function run(op) {
test('sync stat not exist files', () => {
@@ -40,12 +39,19 @@ export function run(op) {
}
const filename = `random_file_${randomUUID()}`
- const r = fs.createReadStream(path.resolve(__dirname,
'../fixtures/random'))
+ const buf = generateFixedBytes(5 * 1024 * 1024)
+ const r = new BufferStream(buf, {
+ highWaterMark: 1024 * 1024, // to buffer 1MB data to read
+ })
const w = new WriterStream(op, filename)
r.pipe(w)
+
w.on('finish', () => {
const t = op.statSync(filename)
- assert.equal(t.contentLength, fs.statSync(path.resolve(__dirname,
'../fixtures/random')).size)
+ assert.equal(t.contentLength, buf.length)
+
+ const content = op.readSync(filename)
+ assert.equal(Buffer.compare(content, buf), 0) // 0 means equal
})
})
}
diff --git a/bindings/nodejs/tests/utils.mjs b/bindings/nodejs/tests/utils.mjs
index 6124cff98..874b3f67d 100644
--- a/bindings/nodejs/tests/utils.mjs
+++ b/bindings/nodejs/tests/utils.mjs
@@ -18,16 +18,16 @@
*/
import crypto from 'node:crypto'
+import { Readable } from 'node:stream'
+// Generate random bytes between 1 and 1024 KB
export function generateBytes() {
- const size = Math.floor(Math.random() * 1024) + 1
- const content = []
-
- for (let i = 0; i < size; i++) {
- content.push(Math.floor(Math.random() * 256))
- }
+ return crypto.randomBytes((Math.floor(Math.random() * 1024) + 1) * 1024)
+}
- return Buffer.from(content)
+// Generate random bytes with given size
+export function generateFixedBytes(size) {
+ return crypto.randomBytes(size)
}
export function loadTestSchemeFromEnv() {
@@ -54,3 +54,23 @@ export function loadConfigFromEnv(scheme) {
.map(([key, value]) => [key.replace(prefix, ''), value]),
)
}
+
+// A readable stream that reads from a buffer
+export class BufferStream extends Readable {
+ constructor(buffer, options) {
+ super(options)
+ this.buffer = buffer
+ this.offset = 0
+ }
+
+ _read(size) {
+ if (this.offset >= this.buffer.length) {
+ this.push(null) // readable stream end, push `null` to end the stream
+ return
+ }
+
+ const chunk = this.buffer.slice(this.offset, this.offset + size)
+ this.push(chunk)
+ this.offset += size
+ }
+}