Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
00e027ef71 | ||
|
|
8bc2c7d70c | ||
|
|
e99fc35e25 | ||
|
|
bbd975feb6 | ||
|
|
1bd153de5b | ||
|
|
a1838bc153 | ||
|
|
65e8e64079 | ||
|
|
579c4bf131 | ||
|
|
1cc6906cc9 | ||
|
|
4884247282 | ||
|
|
1fa35ba82e |
Generated
+19
-14
@@ -1,36 +1,41 @@
|
||||
{
|
||||
"name": "node-file-system-adapter",
|
||||
"name": "@sugoidogo/node-file-system-adapter",
|
||||
"version": "0.0.0",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "node-file-system-adapter",
|
||||
"name": "@sugoidogo/node-file-system-adapter",
|
||||
"version": "0.0.0",
|
||||
"dependencies": {
|
||||
"mime": "^4.1.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^25.0.3",
|
||||
"@types/web": "^0.0.310",
|
||||
"@sugoidogo/importable-types-web": "^1.0.0",
|
||||
"@types/node": "^25.0.9",
|
||||
"typescript": "^5.9.3"
|
||||
}
|
||||
},
|
||||
"node_modules/@sugoidogo/importable-types-web": {
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://gitea.sugoidogo.com/api/packages/sugoidogo/npm/%40sugoidogo%2Fimportable-types-web/-/1.0.0/importable-types-web-1.0.0.tgz",
|
||||
"integrity": "sha512-MqWkQ/WsllO7GiQjqWiFB3ZzsDwJwGPXIWkyd0E24d3CMcI0FKGgsOn6G/RVjCO9k2d/aRlzxqVmyuPAYOZ8tQ==",
|
||||
"dev": true,
|
||||
"license": "Apache-2.0",
|
||||
"dependencies": {
|
||||
"undici-types": "^7.16.0"
|
||||
}
|
||||
},
|
||||
"node_modules/@types/node": {
|
||||
"version": "25.0.3",
|
||||
"resolved": "https://registry.npmjs.org/@types/node/-/node-25.0.3.tgz",
|
||||
"integrity": "sha512-W609buLVRVmeW693xKfzHeIV6nJGGz98uCPfeXI1ELMLXVeKYZ9m15fAMSaUPBHYLGFsVRcMmSCksQOrZV9BYA==",
|
||||
"version": "25.0.9",
|
||||
"resolved": "https://registry.npmjs.org/@types/node/-/node-25.0.9.tgz",
|
||||
"integrity": "sha512-/rpCXHlCWeqClNBwUhDcusJxXYDjZTyE8v5oTO7WbL8eij2nKhUeU89/6xgjU7N4/Vh3He0BtyhJdQbDyhiXAw==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"undici-types": "~7.16.0"
|
||||
}
|
||||
},
|
||||
"node_modules/@types/web": {
|
||||
"version": "0.0.310",
|
||||
"resolved": "https://registry.npmjs.org/@types/web/-/web-0.0.310.tgz",
|
||||
"integrity": "sha512-XN++V7UZGEeRg8MXJBkzMqmcAWoCRN1UV/zpQxg9YnJQ9pIPSuzEG7oidC+nXi/J8pmfb3UgFv+TjEDzMiNnRg==",
|
||||
"dev": true,
|
||||
"license": "Apache-2.0"
|
||||
},
|
||||
"node_modules/mime": {
|
||||
"version": "4.1.0",
|
||||
"resolved": "https://registry.npmjs.org/mime/-/mime-4.1.0.tgz",
|
||||
|
||||
+3
-2
@@ -1,5 +1,6 @@
|
||||
{
|
||||
"name": "@sugoidogo/node-file-system-adapter",
|
||||
"version": "0.0.0",
|
||||
"main": "dist/NodeFileSystemDirectoryHandle.js",
|
||||
"files": [
|
||||
"dist",
|
||||
@@ -15,8 +16,8 @@
|
||||
"mime": "^4.1.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^25.0.3",
|
||||
"@types/web": "^0.0.310",
|
||||
"@sugoidogo/importable-types-web": "^1.0.0",
|
||||
"@types/node": "^25.0.9",
|
||||
"typescript": "^5.9.3"
|
||||
}
|
||||
}
|
||||
|
||||
+14
-7
@@ -1,14 +1,21 @@
|
||||
import NodeFileSystemDirectoryHandle from '../dist/NodeFileSystemDirectoryHandle.js'
|
||||
import NodeFileSystemDirectoryHandle from '../src/NodeFileSystemDirectoryHandle.ts'
|
||||
|
||||
const textText="Hello World!"
|
||||
const cwd_directory_handle = new NodeFileSystemDirectoryHandle('.')
|
||||
const test_directory_handle = await cwd_directory_handle.getDirectoryHandle('test', { 'create': true })
|
||||
const file_handle = await test_directory_handle.getFileHandle('test.txt', { 'create': true })
|
||||
const writable = await file_handle.createWritable()
|
||||
writable.write('Hello World!')
|
||||
writable.close()
|
||||
for (let i = 0; i < 10000; i++) {
|
||||
const file_handle = await test_directory_handle.getFileHandle(i+'.txt', { 'create': true })
|
||||
let writable = await file_handle.createWritable()
|
||||
await writable.write(textText+textText)
|
||||
await writable.close()
|
||||
writable = await file_handle.createWritable()
|
||||
await writable.write(textText)
|
||||
await writable.close()
|
||||
const file = await file_handle.getFile()
|
||||
const file_text = await file.text()
|
||||
console.log(file_text)
|
||||
console.log(file.webkitRelativePath)
|
||||
const text = await file.text()
|
||||
if (text !== textText) throw new Error("failed to read back written data")
|
||||
}
|
||||
const result = await cwd_directory_handle.getFileHandle('test/test.txt').catch(error => 'blocked path traversal')
|
||||
if (typeof result === 'string') console.log(result)
|
||||
else console.error('path traversal not blocked')
|
||||
|
||||
@@ -1,25 +1,36 @@
|
||||
import * as fs from 'node:fs';
|
||||
import * as asyncfs from 'node:fs/promises';
|
||||
import * as path from 'node:path';
|
||||
import NodeFileSystemHandle from './NodeFileSystemHandle.js';
|
||||
import NodeFileSystemFileHandle from './NodeFileSystemFileHandle.js';
|
||||
import * as Path from 'node:path';
|
||||
import NodeFileSystemHandle from './NodeFileSystemHandle.ts';
|
||||
import NodeFileSystemFileHandle from './NodeFileSystemFileHandle.ts';
|
||||
import type {
|
||||
FileSystemDirectoryHandle,
|
||||
FileSystemGetDirectoryOptions,
|
||||
FileSystemDirectoryHandleAsyncIterator,
|
||||
FileSystemGetFileOptions,
|
||||
FileSystemRemoveOptions,
|
||||
FileSystemHandle
|
||||
} from '@sugoidogo/importable-types-web'
|
||||
|
||||
export default class NodeFileSystemDirectoryHandle extends NodeFileSystemHandle implements FileSystemDirectoryHandle {
|
||||
declare kind: "directory";
|
||||
constructor(fpath: string, options: FileSystemGetDirectoryOptions = {}) {
|
||||
super()
|
||||
fpath = path.normalize(fpath)
|
||||
if (!fs.existsSync(fpath) && options.create) fs.mkdirSync(fpath)
|
||||
if (!fs.lstatSync(fpath).isDirectory()) throw new Error('not a directory')
|
||||
this.path = fpath
|
||||
constructor(path: string, options: FileSystemGetDirectoryOptions & { origin?: NodeFileSystemDirectoryHandle } = {}) {
|
||||
super(path, options)
|
||||
try {
|
||||
const stats = fs.statSync(this.path)
|
||||
if (!stats.isDirectory()) throw new Error("tried to open file as directory: " + this.path)
|
||||
} catch (error) {
|
||||
if (options.create) fs.mkdirSync(this.path)
|
||||
else throw error
|
||||
}
|
||||
}
|
||||
entries(): FileSystemDirectoryHandleAsyncIterator<[string, NodeFileSystemDirectoryHandle | NodeFileSystemFileHandle]> {
|
||||
const entries: Array<[string, NodeFileSystemDirectoryHandle | NodeFileSystemFileHandle]> = []
|
||||
fs.readdirSync(this.path).forEach(name => {
|
||||
if (fs.lstatSync(name).isDirectory()) {
|
||||
entries.push([name, new NodeFileSystemDirectoryHandle(path.join(this.path, name))])
|
||||
entries.push([name, new NodeFileSystemDirectoryHandle(Path.join(this.path, name), { origin: this.origin || this })])
|
||||
} else {
|
||||
entries.push([name, new NodeFileSystemFileHandle(path.join(this.path, name))])
|
||||
entries.push([name, new NodeFileSystemFileHandle(Path.join(this.path, name), { origin: this.origin || this })])
|
||||
}
|
||||
})
|
||||
return entries[Symbol.asyncIterator]
|
||||
@@ -31,37 +42,33 @@ export default class NodeFileSystemDirectoryHandle extends NodeFileSystemHandle
|
||||
const values: Array<NodeFileSystemDirectoryHandle | NodeFileSystemFileHandle> = [];
|
||||
fs.readdirSync(this.path).forEach(name => {
|
||||
if (fs.lstatSync(name).isDirectory()) {
|
||||
values.push(new NodeFileSystemDirectoryHandle(path.join(this.path, name)))
|
||||
values.push(new NodeFileSystemDirectoryHandle(Path.join(this.path, name), { origin: this.origin || this }))
|
||||
} else {
|
||||
values.push(new NodeFileSystemFileHandle(path.join(this.path, name)))
|
||||
values.push(new NodeFileSystemFileHandle(Path.join(this.path, name), { origin: this.origin || this }))
|
||||
}
|
||||
})
|
||||
return values[Symbol.asyncIterator]
|
||||
}
|
||||
#getValidatedPath(name: string) {
|
||||
name = path.normalize(name)
|
||||
if (name.includes(path.sep)) throw new TypeError('Invalid Filename')
|
||||
return path.join(this.path, name)
|
||||
name = Path.normalize(name)
|
||||
if (name.includes(Path.sep)) throw new TypeError('Invalid Filename')
|
||||
return Path.join(this.path, name)
|
||||
}
|
||||
async getDirectoryHandle(name: string, options: FileSystemGetDirectoryOptions = {}): Promise<NodeFileSystemDirectoryHandle> {
|
||||
Object.assign(options, { origin: this.origin || this })
|
||||
return new NodeFileSystemDirectoryHandle(this.#getValidatedPath(name), options)
|
||||
}
|
||||
async getFileHandle(name: string, options: FileSystemGetFileOptions = {}): Promise<NodeFileSystemFileHandle> {
|
||||
Object.assign(options, { origin: this.origin || this })
|
||||
return new NodeFileSystemFileHandle(this.#getValidatedPath(name), options)
|
||||
}
|
||||
async removeEntry(name: string, options: FileSystemRemoveOptions = { recursive: false }): Promise<void> {
|
||||
return asyncfs.rm(this.#getValidatedPath(name), { recursive: options.recursive, force: options.recursive })
|
||||
}
|
||||
async resolve(possibleDescendant: FileSystemHandle): Promise<string[] | null> {
|
||||
if (typeof possibleDescendant !== typeof this) return null
|
||||
let thisName = this.path
|
||||
let possibleDescendantName = possibleDescendant.name
|
||||
if (!path.isAbsolute(this.path)) thisName = path.join(process.cwd(), this.path)
|
||||
if (!path.isAbsolute(possibleDescendant.name)) possibleDescendantName = path.join(process.cwd(), possibleDescendant.name)
|
||||
if (!possibleDescendantName.startsWith(thisName)) return null
|
||||
possibleDescendantName = possibleDescendantName.substring(thisName.length - 1)
|
||||
if (possibleDescendantName[0] == path.sep) possibleDescendantName = possibleDescendantName.substring(1)
|
||||
return possibleDescendantName.split(path.sep)
|
||||
if (!(possibleDescendant instanceof NodeFileSystemHandle)) return null
|
||||
if (!possibleDescendant.path.startsWith(this.path)) return null
|
||||
return possibleDescendant.path.substring(this.path.length + 1).split(Path.sep)
|
||||
}
|
||||
[Symbol.asyncIterator] = this.entries
|
||||
}
|
||||
@@ -1,27 +1,34 @@
|
||||
import * as fs from 'node:fs';
|
||||
import * as asyncfs from 'node:fs/promises';
|
||||
import * as path from 'node:path';
|
||||
import NodeFileSystemHandle from './NodeFileSystemHandle.js';
|
||||
import NodeFileSystemWritableFileStream from './NodeFileSystemWriteableFileStream.js';
|
||||
import * as Path from "node:path"
|
||||
import NodeFileSystemHandle from './NodeFileSystemHandle.ts';
|
||||
import NodeFileSystemWritableFileStream from './NodeFileSystemWriteableFileStream.ts';
|
||||
import NonBufferedFile from './NonBufferedFile.ts'
|
||||
import type {
|
||||
FileSystemFileHandle,
|
||||
FileSystemGetFileOptions,
|
||||
FileSystemCreateWritableOptions,
|
||||
FileSystemWritableFileStream,
|
||||
File as WebFile
|
||||
} from '@sugoidogo/importable-types-web'
|
||||
import type NodeFileSystemDirectoryHandle from './NodeFileSystemDirectoryHandle.ts';
|
||||
|
||||
export default class NodeFileSystemFileHandle extends NodeFileSystemHandle implements FileSystemFileHandle {
|
||||
declare kind: "file";
|
||||
constructor(fpath: string, options: FileSystemGetFileOptions={}) {
|
||||
super()
|
||||
fpath = path.normalize(fpath)
|
||||
if (!fs.existsSync(fpath) && options.create) fs.writeFileSync(fpath, '')
|
||||
if (fs.lstatSync(fpath).isDirectory()) new Error('not a file')
|
||||
this.path = fpath
|
||||
constructor(path: string, options: FileSystemGetFileOptions & { origin?: NodeFileSystemDirectoryHandle } = {}) {
|
||||
super(path,options)
|
||||
let flags = fs.constants.O_RDWR
|
||||
if (options.create) flags |= fs.constants.O_CREAT
|
||||
fs.closeSync(fs.openSync(this.path, flags))
|
||||
}
|
||||
async createWritable(options: FileSystemCreateWritableOptions = {}): Promise<FileSystemWritableFileStream> {
|
||||
return new NodeFileSystemWritableFileStream(this.path,options)
|
||||
let flags = fs.constants.O_WRONLY
|
||||
const fileHandle = await asyncfs.open(this.path, flags)
|
||||
if (!options.keepExistingData) await fileHandle.truncate(0)
|
||||
return new NodeFileSystemWritableFileStream(fileHandle)
|
||||
}
|
||||
async getFile(): Promise<File> {
|
||||
const stats = await asyncfs.stat(this.path)
|
||||
return asyncfs.readFile(this.path).then(buffer => Object.assign(new Blob([buffer]), {
|
||||
lastModified: stats.mtimeMs,
|
||||
name: this.path,
|
||||
webkitRelativePath: this.path
|
||||
}))
|
||||
async getFile(): Promise<WebFile> {
|
||||
const webkitRelativePathSegments=await this.origin.resolve(this)
|
||||
return new NonBufferedFile(this.path,webkitRelativePathSegments.join(Path.sep))
|
||||
}
|
||||
}
|
||||
@@ -1,15 +1,22 @@
|
||||
import * as path from 'node:path'
|
||||
import * as Path from 'node:path'
|
||||
import type {
|
||||
FileSystemHandle,
|
||||
FileSystemHandleKind
|
||||
} from '@sugoidogo/importable-types-web'
|
||||
import type NodeFileSystemDirectoryHandle from './NodeFileSystemDirectoryHandle';
|
||||
|
||||
export default class NodeFileSystemHandle implements FileSystemHandle {
|
||||
kind: FileSystemHandleKind;
|
||||
path: string;
|
||||
get name() { return path.basename(this.path) }
|
||||
origin: NodeFileSystemDirectoryHandle
|
||||
get name() { return Path.basename(this.path) }
|
||||
constructor(path: string, options: { origin?: NodeFileSystemDirectoryHandle } = {}) {
|
||||
if (!Path.isAbsolute(path)) path = Path.resolve(path)
|
||||
this.path = path
|
||||
this.origin = options.origin
|
||||
}
|
||||
async isSameEntry(other: FileSystemHandle): Promise<boolean> {
|
||||
if (typeof other != typeof this) return false
|
||||
let thisName = this.name
|
||||
let otherName = other.name
|
||||
if (!path.isAbsolute(this.name)) thisName = path.join(process.cwd(), this.name)
|
||||
if (!path.isAbsolute(other.name)) otherName = path.join(process.cwd(), other.name)
|
||||
return thisName === otherName
|
||||
if (!(other instanceof NodeFileSystemHandle)) return false
|
||||
return this.path === other.path
|
||||
}
|
||||
}
|
||||
@@ -1,38 +1,44 @@
|
||||
import * as fs from 'node:fs'
|
||||
import * as asyncfs from 'node:fs/promises'
|
||||
import * as path from 'node:path'
|
||||
import { Writable } from 'node:stream'
|
||||
import type {
|
||||
FileSystemWritableFileStream,
|
||||
FileSystemWriteChunkType,
|
||||
WriteParams
|
||||
} from '@sugoidogo/importable-types-web'
|
||||
import type { FileHandle } from 'node:fs/promises'
|
||||
|
||||
function isWriteParams(data: FileSystemWriteChunkType): data is WriteParams {
|
||||
return ["write", "seek", "truncate"].includes((data as WriteParams).type)
|
||||
}
|
||||
|
||||
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)
|
||||
#position = 0
|
||||
#fileHandle: FileHandle
|
||||
constructor(fileHandle: FileHandle) {
|
||||
const writeStream = fileHandle.createWriteStream({ "encoding": "binary" })
|
||||
const writeableStream = Writable.toWeb(writeStream)
|
||||
super(writeableStream)
|
||||
this.#fileHandle = fileHandle
|
||||
}
|
||||
async seek(position: number): Promise<void> {
|
||||
this.#seek_position = position
|
||||
if (this.locked) throw new Error("stream is locked")
|
||||
this.#position = position
|
||||
}
|
||||
truncate(size: number): Promise<void> {
|
||||
return asyncfs.truncate(this.#name, size)
|
||||
async truncate(size: number): Promise<void> {
|
||||
if (this.locked) throw new Error("stream is locked")
|
||||
return this.#fileHandle.truncate(size)
|
||||
}
|
||||
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
|
||||
async write(data: FileSystemWriteChunkType): Promise<void> {
|
||||
if (this.locked) throw new Error("stream is locked")
|
||||
let offset: number, length: number, position=this.#position
|
||||
if (isWriteParams(data)) {
|
||||
if (data.type === "seek") return this.seek(data.position)
|
||||
if (data.type === "truncate") return this.truncate(data.size)
|
||||
if (typeof data.position === "number") position = data.position
|
||||
length = data.size
|
||||
data = data.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
|
||||
if (typeof data === 'string') data = new TextEncoder().encode(data)
|
||||
const { bytesWritten } = await this.#fileHandle.write(data as any, offset, length, position)
|
||||
this.#position=position+bytesWritten
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
+2
-1
@@ -7,7 +7,8 @@
|
||||
"sourceMap": true,
|
||||
"declaration": true,
|
||||
"declarationMap": true,
|
||||
"outDir": "dist"
|
||||
"outDir": "dist",
|
||||
"rewriteRelativeImportExtensions": true
|
||||
},
|
||||
"include": ["src"]
|
||||
}
|
||||
Reference in New Issue
Block a user