This is an automated email from the ASF dual-hosted git repository.
Xuanwo pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/opendal.git
The following commit(s) were added to refs/heads/main by this push:
new 6804bb73d3 fix(bindings/nodejs): abort unfinished writable streams
(#9065)
6804bb73d3 is described below
commit 6804bb73d3e5222c2067c8fab922d0a0d66a07d3
Author: Xuanwo <[email protected]>
AuthorDate: Wed Nov 25 20:12:33 2026 +0800
fix(bindings/nodejs): abort unfinished writable streams (#9065)
---
bindings/nodejs/generated.d.ts | 7 ++++
bindings/nodejs/index.cjs | 54 +++++++++++++++++-----------
bindings/nodejs/index.d.ts | 6 +++-
bindings/nodejs/index.mjs | 54 +++++++++++++++++-----------
bindings/nodejs/src/lib.rs | 10 ++++++
bindings/nodejs/tests/entrypoint.test.mjs | 7 ++--
bindings/nodejs/tests/suites/async.suite.mjs | 15 +++++++-
7 files changed, 107 insertions(+), 46 deletions(-)
diff --git a/bindings/nodejs/generated.d.ts b/bindings/nodejs/generated.d.ts
index 434ee12f8d..a89aa74217 100644
--- a/bindings/nodejs/generated.d.ts
+++ b/bindings/nodejs/generated.d.ts
@@ -1097,6 +1097,13 @@ export declare class Writer {
* ```
*/
close(): Promise<void>
+ /**
+ * Abort an unfinished write and release resources held by the service.
+ *
+ * A successful abort ends the writer. Services that write directly to the
+ * destination may leave partial data.
+ */
+ abort(): Promise<void>
}
export interface DeleteOptions {
diff --git a/bindings/nodejs/index.cjs b/bindings/nodejs/index.cjs
index 24c35372ba..d87393acb8 100644
--- a/bindings/nodejs/index.cjs
+++ b/bindings/nodejs/index.cjs
@@ -83,36 +83,50 @@ class WriteStream extends Writable {
constructor(writer, options) {
super(options)
this.writer = writer
+ this.pending = null
+ this.committed = false
}
- _write(chunk, encoding, callback) {
- let data
+ async _write(chunk, encoding, callback) {
try {
- data = toWriterInput(chunk, encoding)
+ this.pending = this.writer.write(toWriterInput(chunk, encoding))
+ await this.pending
} catch (error) {
callback(error)
return
}
+ callback()
+ }
- this.writer
- .write(data)
- .then(() => {
- callback()
- })
- .catch((error) => {
- callback(error)
- })
+ async _final(callback) {
+ try {
+ this.pending = this.writer.close()
+ await this.pending
+ } catch (error) {
+ callback(error)
+ return
+ }
+ this.committed = true
+ callback()
}
- _final(callback) {
- this.writer
- .close()
- .then(() => {
- callback()
- })
- .catch((error) => {
- callback(error)
- })
+ async _destroy(error, callback) {
+ // Wait for construction and any I/O before deciding whether to abort.
+ // A pending write may start a multipart upload, and a pending close may
commit it.
+ try {
+ await this.pending
+ } catch (operationError) {
+ error = error || operationError
+ }
+
+ if (!this.committed) {
+ try {
+ await this.writer.abort()
+ } catch (abortError) {
+ error = error || abortError
+ }
+ }
+ callback(error)
}
}
diff --git a/bindings/nodejs/index.d.ts b/bindings/nodejs/index.d.ts
index 8c22b10e14..0556007af7 100644
--- a/bindings/nodejs/index.d.ts
+++ b/bindings/nodejs/index.d.ts
@@ -35,7 +35,11 @@ declare module './generated' {
}
interface Writer {
- /** Create a writable stream from the underlying writer. */
+ /**
+ * Create a writable stream that commits the write on successful
completion.
+ * Destroying an unfinished stream waits for pending I/O and attempts to
abort the writer.
+ * Cleanup depends on the service. If the stream already has an error, it
reports that error.
+ */
createWriteStream(options?: WritableOptions): Writable
}
}
diff --git a/bindings/nodejs/index.mjs b/bindings/nodejs/index.mjs
index 1f0a698afd..16fe5791a8 100644
--- a/bindings/nodejs/index.mjs
+++ b/bindings/nodejs/index.mjs
@@ -83,36 +83,50 @@ class WriteStream extends Writable {
constructor(writer, options) {
super(options)
this.writer = writer
+ this.pending = null
+ this.committed = false
}
- _write(chunk, encoding, callback) {
- let data
+ async _write(chunk, encoding, callback) {
try {
- data = toWriterInput(chunk, encoding)
+ this.pending = this.writer.write(toWriterInput(chunk, encoding))
+ await this.pending
} catch (error) {
callback(error)
return
}
+ callback()
+ }
- this.writer
- .write(data)
- .then(() => {
- callback()
- })
- .catch((error) => {
- callback(error)
- })
+ async _final(callback) {
+ try {
+ this.pending = this.writer.close()
+ await this.pending
+ } catch (error) {
+ callback(error)
+ return
+ }
+ this.committed = true
+ callback()
}
- _final(callback) {
- this.writer
- .close()
- .then(() => {
- callback()
- })
- .catch((error) => {
- callback(error)
- })
+ async _destroy(error, callback) {
+ // Wait for construction and any I/O before deciding whether to abort.
+ // A pending write may start a multipart upload, and a pending close may
commit it.
+ try {
+ await this.pending
+ } catch (operationError) {
+ error = error || operationError
+ }
+
+ if (!this.committed) {
+ try {
+ await this.writer.abort()
+ } catch (abortError) {
+ error = error || abortError
+ }
+ }
+ callback(error)
}
}
diff --git a/bindings/nodejs/src/lib.rs b/bindings/nodejs/src/lib.rs
index aab8342519..f8f536bbcf 100644
--- a/bindings/nodejs/src/lib.rs
+++ b/bindings/nodejs/src/lib.rs
@@ -1357,6 +1357,16 @@ impl Writer {
let mut inner = self.inner.lock().await;
inner.close().await.map(|_| ()).map_err(format_napi_error)
}
+
+ /// Abort an unfinished write and release resources held by the service.
+ ///
+ /// A successful abort ends the writer. Services that write directly to the
+ /// destination may leave partial data.
+ #[napi]
+ pub async fn abort(&self) -> Result<()> {
+ let mut inner = self.inner.lock().await;
+ inner.abort().await.map_err(format_napi_error)
+ }
}
/// Lister is designed to list entries at a given path in an asynchronous
diff --git a/bindings/nodejs/tests/entrypoint.test.mjs
b/bindings/nodejs/tests/entrypoint.test.mjs
index 34555202e6..311f0d3e4c 100644
--- a/bindings/nodejs/tests/entrypoint.test.mjs
+++ b/bindings/nodejs/tests/entrypoint.test.mjs
@@ -143,10 +143,8 @@ describe('entrypoint exports parity', () => {
assert.deepEqual(op.readSync('cjs-sync.bin'), expected)
// Destroying a stream leaves the writer uncommitted.
- for (const ws of [
- (await op.writer('cjs-abort-async.bin')).createWriteStream(),
- op.writerSync('cjs-abort-sync.bin').createWriteStream(),
- ]) {
+ const abortedWriter = await op.writer('cjs-abort-async.bin')
+ for (const ws of [abortedWriter.createWriteStream(),
op.writerSync('cjs-abort-sync.bin').createWriteStream()]) {
const wsClosed = once(ws, 'close')
ws.destroy()
await wsClosed
@@ -154,6 +152,7 @@ describe('entrypoint exports parity', () => {
}
assert.equal(await op.exists('cjs-abort-async.bin'), false)
assert.equal(op.existsSync('cjs-abort-sync.bin'), false)
+ await assert.rejects(abortedWriter.close(), /writer has been closed or
aborted/)
})().catch((err) => {
console.error(err)
process.exit(1)
diff --git a/bindings/nodejs/tests/suites/async.suite.mjs
b/bindings/nodejs/tests/suites/async.suite.mjs
index 8333237f25..0a54d9d4b5 100644
--- a/bindings/nodejs/tests/suites/async.suite.mjs
+++ b/bindings/nodejs/tests/suites/async.suite.mjs
@@ -410,7 +410,7 @@ export function run(op) {
expect(await memory.read('encoded.bin')).toEqual(expected)
})
- test('write stream destroy releases writer without closing', async () => {
+ test('write stream destroy waits for a pending write and aborts the
writer', async () => {
const memory = new Operator('memory')
const writer = await memory.writer('aborted.bin')
let closeCalled = false
@@ -419,15 +419,28 @@ export function run(op) {
closeCalled = true
return originalClose()
}
+ let releaseWrite
+ const blocked = new Promise((resolve) => {
+ releaseWrite = resolve
+ })
+ const originalWrite = writer.write.bind(writer)
+ writer.write = (chunk) => blocked.then(() => originalWrite(chunk))
const ws = writer.createWriteStream()
const closed = once(ws, 'close')
+ ws.write(Buffer.from('unfinished data'))
ws.destroy()
+ ws.destroy()
+ await setImmediate()
+ const closedBeforeWrite = ws.closed
+ releaseWrite()
await closed
+ expect(closedBeforeWrite).toBe(false)
expect(ws.destroyed).toBe(true)
expect(closeCalled).toBe(false)
expect(await memory.exists('aborted.bin')).toBe(false)
+ await expect(originalClose()).rejects.toThrow('writer has been closed or
aborted')
})
test.each(['index.mjs', 'index.cjs'])(