From e75f34c86c44e17f70d8a63389f9aaf509a54b5c Mon Sep 17 00:00:00 2001 From: Stefan Zerkalica Date: Thu, 19 Dec 2024 16:01:39 +0300 Subject: [PATCH] $mol_file_transaction write support array of buffer and string --- file/transaction/transaction.node.ts | 14 ++++++++++++-- file/transaction/transaction.ts | 4 +++- file/transaction/transaction.web.ts | 2 +- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/file/transaction/transaction.node.ts b/file/transaction/transaction.node.ts index 4d0c1bba5d..bdca697751 100644 --- a/file/transaction/transaction.node.ts +++ b/file/transaction/transaction.node.ts @@ -24,12 +24,22 @@ namespace $ { return $node.fs.openSync(this.path(), mode_mask(this.modes()) ) } - override write({ buffer, offset = 0, length = buffer.byteLength, position }: { - buffer: ArrayBufferView, + override write({ buffer, offset = 0, length, position = null }: { + buffer: ArrayBufferView | string | readonly ArrayBufferView[], offset?: number | null, length?: number | null, position?: number | null, }) { + if (Array.isArray(buffer)) { + return $node.fs.writevSync( this.descr(), buffer, position ?? undefined) + } + + if (typeof buffer === 'string') { + return $node.fs.writeSync( this.descr(), buffer, position ) + } + + length = length ?? (buffer as ArrayBufferView).byteLength + return $node.fs.writeSync( this.descr(), buffer as NodeJS.ArrayBufferView, offset, length, position ) } diff --git a/file/transaction/transaction.ts b/file/transaction/transaction.ts index a6e1da812b..6a40732901 100644 --- a/file/transaction/transaction.ts +++ b/file/transaction/transaction.ts @@ -3,13 +3,15 @@ namespace $ { export type $mol_file_transaction_mode = 'create' | 'exists_truncate' | 'exists_fail' | 'read_only' | 'write_only' | 'read_write' | 'append' + export type $mol_file_transaction_buffer = ArrayBufferView + export class $mol_file_transaction extends $mol_object { path() { return '' } modes() { return [] as readonly $mol_file_transaction_mode[] } write(options: { - buffer: ArrayBufferView, + buffer: ArrayBufferView | string | readonly ArrayBufferView[], offset?: number | null, length?: number | null, position?: number | null, diff --git a/file/transaction/transaction.web.ts b/file/transaction/transaction.web.ts index f8388a9a3c..55a327d085 100644 --- a/file/transaction/transaction.web.ts +++ b/file/transaction/transaction.web.ts @@ -1,7 +1,7 @@ namespace $ { export class $mol_file_transaction_web extends $mol_file_transaction { override write(options: { - buffer: ArrayBufferView, + buffer: ArrayBufferView | string | readonly ArrayBufferView[], offset?: number | null, length?: number | null, position?: number | null,