Skip to content

Commit

Permalink
$mol_file_transaction write support array of buffer and string
Browse files Browse the repository at this point in the history
  • Loading branch information
zerkalica committed Dec 19, 2024
1 parent 0dbeaff commit e75f34c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
14 changes: 12 additions & 2 deletions file/transaction/transaction.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
}

Expand Down
4 changes: 3 additions & 1 deletion file/transaction/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion file/transaction/transaction.web.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down

0 comments on commit e75f34c

Please sign in to comment.