@@ -1,31 +1,38 @@
|
||||
import * as fs from 'node:fs'
|
||||
import * as fsp from 'node:fs/promises'
|
||||
import * as asyncfs from 'node:fs/promises'
|
||||
import * as path from 'node:path'
|
||||
import { Writable } from 'node:stream'
|
||||
|
||||
export default class NodeFileSystemWriteableFileStream implements FileSystemWritableFileStream {
|
||||
#name:string
|
||||
constructor(name: string, options?: FileSystemCreateWritableOptions) {
|
||||
if (options.keepExistingData) {
|
||||
|
||||
}
|
||||
this.#name=name
|
||||
export default class NodeFileSystemWritableFileStream extends WritableStream implements FileSystemWritableFileStream {
|
||||
#seek_position = 0
|
||||
#name: string
|
||||
constructor(name: string, options: FileSystemCreateWritableOptions={}) {
|
||||
name = path.normalize(name)
|
||||
super(Writable.toWeb(fs.createWriteStream(name)))
|
||||
this.#name = name
|
||||
if(!options.keepExistingData) this.truncate(0)
|
||||
}
|
||||
seek(position: number): Promise<void> {
|
||||
throw new Error("Method not implemented.");
|
||||
async seek(position: number): Promise<void> {
|
||||
this.#seek_position = position
|
||||
}
|
||||
truncate(size: number): Promise<void> {
|
||||
return fsp.truncate(this.#name,size)
|
||||
return asyncfs.truncate(this.#name, size)
|
||||
}
|
||||
async write(data: FileSystemWriteChunkType): Promise<void> {
|
||||
return fsp.writeFile(this.#name,(data as string))
|
||||
}
|
||||
locked: boolean;
|
||||
abort(reason?: any): Promise<void> {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
close(): Promise<void> {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
getWriter(): WritableStreamDefaultWriter<any> {
|
||||
throw new Error("Method not implemented")
|
||||
async write(wdata: FileSystemWriteChunkType): Promise<void> {
|
||||
if (wdata instanceof Blob) wdata = await wdata.bytes()
|
||||
if (typeof wdata === 'string') wdata = new TextEncoder().encode(wdata)
|
||||
let wsize: number = null
|
||||
let pseek_position = this.#seek_position
|
||||
if ((wdata as WriteParams).type) {
|
||||
const { type, position, size, data } = (wdata as WriteParams)
|
||||
if (type === 'truncate') return asyncfs.truncate(this.#name, size)
|
||||
if (position) this.#seek_position = position
|
||||
if (type === 'seek') return
|
||||
if (size) wsize = size
|
||||
wdata = data
|
||||
}
|
||||
const file = await asyncfs.open(this.#name,'r+')
|
||||
await file.write((wdata as Uint8Array), null, wsize, this.#seek_position)
|
||||
this.#seek_position = pseek_position
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user