add deno-compatible NonBufferedFile
Release / release (push) Successful in 23s

This commit is contained in:
2026-02-16 17:28:28 -08:00
parent 8bc2c7d70c
commit 00e027ef71
2 changed files with 51 additions and 9 deletions
+3 -9
View File
@@ -3,7 +3,7 @@ import * as asyncfs from 'node:fs/promises';
import * as Path from "node:path"
import NodeFileSystemHandle from './NodeFileSystemHandle.ts';
import NodeFileSystemWritableFileStream from './NodeFileSystemWriteableFileStream.ts';
import mime from 'mime'
import NonBufferedFile from './NonBufferedFile.ts'
import type {
FileSystemFileHandle,
FileSystemGetFileOptions,
@@ -28,13 +28,7 @@ export default class NodeFileSystemFileHandle extends NodeFileSystemHandle imple
return new NodeFileSystemWritableFileStream(fileHandle)
}
async getFile(): Promise<WebFile> {
const type = mime.getType(this.path.split('.').pop())
const stats = await asyncfs.stat(this.path)
const relativePathSegments = await this.origin.resolve(this)
return Object.assign(await fs.openAsBlob(this.path, { "type": type }), {
name: this.name,
webkitRelativePath: relativePathSegments.join(Path.sep),
lastModified: stats.mtimeMs
}) as any
const webkitRelativePathSegments=await this.origin.resolve(this)
return new NonBufferedFile(this.path,webkitRelativePathSegments.join(Path.sep))
}
}
+48
View File
@@ -0,0 +1,48 @@
import * as fs from "node:fs"
import * as asyncfs from "node:fs/promises"
import * as Path from "node:path"
import mime from "mime"
import type { Blob as WebBlob, File as WebFile, ReadableStream } from "@sugoidogo/importable-types-web";
import { Blob } from "node:buffer";
import { Readable } from "node:stream";
export default class NonBufferedFile implements WebFile {
webkitRelativePath: string;
#path: string
constructor(path: string, webkitRelativePath:string) {
this.#path = Path.resolve(path)
this.webkitRelativePath=webkitRelativePath
}
get lastModified() {
return fs.statSync(this.#path).mtimeMs
}
get size() {
return fs.statSync(this.#path).size
}
get type() {
return mime.getType(this.#path)
}
get name() {
return Path.basename(this.#path)
}
async bytes(): Promise<Uint8Array<ArrayBuffer>> {
return asyncfs.readFile(this.#path)
}
async arrayBuffer(): Promise<ArrayBuffer> {
const bytes = await asyncfs.readFile(this.#path)
return bytes.buffer
}
async text(): Promise<string> {
return asyncfs.readFile(this.#path,"utf8")
}
slice(start?: number, end?: number, contentType?: string): WebBlob {
const fd = fs.openSync(this.#path, fs.constants.O_RDONLY)
const buffer=new Uint8Array()
fs.readSync(fd, buffer, { "position": start, "length": end - start })
fs.closeSync(fd)
return new Blob([buffer],{"type":contentType||this.type}) as any
}
stream(): ReadableStream<Uint8Array<ArrayBuffer>> {
return Readable.toWeb(fs.createReadStream(this.#path)) as any
}
}