@zenfs/core
Advanced tools
Comparing version 0.5.5 to 0.5.6
@@ -125,3 +125,3 @@ import { FileSystem, FileSystemMetadata } from '../filesystem.js'; | ||
} | ||
export declare const AsyncMirror: Backend; | ||
export declare const AsyncMirror: Backend<AsyncMirrorFS>; | ||
export {}; |
@@ -233,2 +233,3 @@ import { FileSystem, Sync } from '../filesystem.js'; | ||
type: 'object', | ||
required: true, | ||
description: 'The synchronous file system to mirror the asynchronous file system to.', | ||
@@ -243,2 +244,3 @@ validator: async (backend) => { | ||
type: 'object', | ||
required: true, | ||
description: 'The asynchronous file system to mirror.', | ||
@@ -245,0 +247,0 @@ }, |
@@ -14,3 +14,3 @@ import { FileSystem } from '../filesystem.js'; | ||
*/ | ||
required?: boolean; | ||
required: boolean; | ||
/** | ||
@@ -17,0 +17,0 @@ * Description of the option. Used in error messages and documentation. |
import type { Ino } from '../inode.js'; | ||
import type { Backend } from './backend.js'; | ||
import { SyncStore, SimpleSyncStore, SyncTransaction } from './SyncStore.js'; | ||
import { SyncStore, SimpleSyncStore, SyncTransaction, SyncStoreFS } from './SyncStore.js'; | ||
/** | ||
@@ -21,2 +21,2 @@ * A simple in-memory store | ||
*/ | ||
export declare const InMemory: Backend; | ||
export declare const InMemory: Backend<SyncStoreFS>; |
@@ -42,2 +42,3 @@ import { SimpleSyncTransaction, SyncStoreFS } from './SyncStore.js'; | ||
type: 'string', | ||
required: false, | ||
description: 'The name of the store', | ||
@@ -44,0 +45,0 @@ }, |
@@ -122,2 +122,2 @@ import { FileSystem, FileSystemMetadata } from '../filesystem.js'; | ||
} | ||
export declare const Overlay: Backend; | ||
export declare const Overlay: Backend<OverlayFS>; |
@@ -108,1 +108,6 @@ /** File is visible to the calling process. */ | ||
export declare const S_IXOTH = 1; | ||
/** | ||
* When set, a memory file mapping is used to access the file. | ||
* This flag is ignored since a unix-like FS is emulated | ||
*/ | ||
export declare const UV_FS_O_FILEMAP = 0; |
@@ -120,1 +120,6 @@ /* | ||
export const S_IXOTH = 1; | ||
/** | ||
* When set, a memory file mapping is used to access the file. | ||
* This flag is ignored since a unix-like FS is emulated | ||
*/ | ||
export const UV_FS_O_FILEMAP = 0; |
@@ -6,5 +6,6 @@ /// <reference types="node" resolution-mode="require"/> | ||
export declare class Dirent implements _Dirent { | ||
name: string; | ||
path: string; | ||
protected stats: Stats; | ||
constructor(name: string, stats: Stats); | ||
get name(): string; | ||
constructor(path: string, stats: Stats); | ||
isFile(): boolean; | ||
@@ -11,0 +12,0 @@ isDirectory(): boolean; |
import { readdir } from './promises.js'; | ||
import { ApiError, ErrorCode } from '../ApiError.js'; | ||
import { readdirSync } from './sync.js'; | ||
import { basename } from './path.js'; | ||
export class Dirent { | ||
constructor(name, stats) { | ||
this.name = name; | ||
get name() { | ||
return basename(this.path); | ||
} | ||
constructor(path, stats) { | ||
this.path = path; | ||
this.stats = stats; | ||
@@ -8,0 +12,0 @@ } |
@@ -1,2 +0,2 @@ | ||
export * from './callbacks.js'; | ||
export * from './async.js'; | ||
export * from './sync.js'; | ||
@@ -8,2 +8,2 @@ export * as promises from './promises.js'; | ||
export { initialize, mounts, mount, umount, _toUnixTimestamp } from './shared.js'; | ||
export { Stats, BigIntStats } from '../stats.js'; | ||
export { Stats, BigIntStats, StatsFs } from '../stats.js'; |
@@ -1,2 +0,2 @@ | ||
export * from './callbacks.js'; | ||
export * from './async.js'; | ||
export * from './sync.js'; | ||
@@ -8,2 +8,2 @@ export * as promises from './promises.js'; | ||
export { initialize, mounts, mount, umount, _toUnixTimestamp } from './shared.js'; | ||
export { Stats, BigIntStats } from '../stats.js'; | ||
export { Stats, BigIntStats, StatsFs } from '../stats.js'; |
/// <reference types="node" resolution-mode="require"/> | ||
/// <reference types="node" resolution-mode="require"/> | ||
/// <reference types="node" resolution-mode="require"/> | ||
/// <reference types="node" resolution-mode="require"/> | ||
/// <reference types="node" resolution-mode="require"/> | ||
/// <reference types="node" resolution-mode="require"/> | ||
import { Buffer } from 'buffer'; | ||
import type * as Node from 'node:fs'; | ||
import type * as promises from 'node:fs/promises'; | ||
import type { CreateReadStreamOptions, CreateWriteStreamOptions, FileChangeInfo, FileReadResult, FlagAndOpenMode } from 'node:fs/promises'; | ||
import { FileContents } from '../filesystem.js'; | ||
import { BigIntStats, type BigIntStatsFs, type Stats, type StatsFs } from '../stats.js'; | ||
import { Dirent, type Dir } from './dir.js'; | ||
import type { PathLike } from './shared.js'; | ||
export * as constants from './constants.js'; | ||
import type { PathLike, BufferToUint8Array } from './shared.js'; | ||
import { FileContents } from '../filesystem.js'; | ||
import { BigIntStats, type Stats } from '../stats.js'; | ||
import { Dirent } from './dir.js'; | ||
export declare class FileHandle implements BufferToUint8Array<Node.promises.FileHandle> { | ||
import type { ReadableStream } from 'node:stream/web'; | ||
import type { Interface as ReadlineInterface } from 'readline'; | ||
export declare class FileHandle implements promises.FileHandle { | ||
/** | ||
@@ -20,2 +28,4 @@ * Gets the file descriptor for this file handle. | ||
fd: number); | ||
private get file(); | ||
private get path(); | ||
/** | ||
@@ -59,7 +69,3 @@ * Asynchronous fchown(2) - Change ownership of a file. | ||
*/ | ||
appendFile(data: string | Uint8Array, _options?: { | ||
encoding?: BufferEncoding; | ||
mode?: Node.Mode; | ||
flag?: Node.OpenMode; | ||
} | BufferEncoding): Promise<void>; | ||
appendFile(data: string | Uint8Array, _options?: (Node.ObjectEncodingOptions & FlagAndOpenMode) | BufferEncoding): Promise<void>; | ||
/** | ||
@@ -73,6 +79,3 @@ * Asynchronously reads data from the file. | ||
*/ | ||
read<TBuffer extends Uint8Array>(buffer: TBuffer, offset?: number, length?: number, position?: number): Promise<{ | ||
bytesRead: number; | ||
buffer: TBuffer; | ||
}>; | ||
read<TBuffer extends NodeJS.ArrayBufferView>(buffer: TBuffer, offset?: number, length?: number, position?: number): Promise<FileReadResult<TBuffer>>; | ||
/** | ||
@@ -86,8 +89,19 @@ * Asynchronously reads the entire contents of a file. The underlying file will _not_ be closed automatically. | ||
flag?: Node.OpenMode; | ||
}): Promise<Uint8Array>; | ||
readFile(_options: { | ||
encoding: BufferEncoding; | ||
flag?: Node.OpenMode; | ||
} | BufferEncoding): Promise<string>; | ||
}): Promise<Buffer>; | ||
readFile(_options: (Node.ObjectEncodingOptions & FlagAndOpenMode) | BufferEncoding): Promise<string>; | ||
/** | ||
* Returns a `ReadableStream` that may be used to read the files data. | ||
* | ||
* An error will be thrown if this method is called more than once or is called after the `FileHandle` is closed | ||
* or closing. | ||
* | ||
* While the `ReadableStream` will read the file to completion, it will not close the `FileHandle` automatically. User code must still call the `fileHandle.close()` method. | ||
* | ||
* @since v17.0.0 | ||
* @experimental | ||
*/ | ||
readableWebStream(options?: promises.ReadableWebStreamOptions): ReadableStream; | ||
readLines(options?: promises.CreateReadStreamOptions): ReadlineInterface; | ||
[Symbol.asyncDispose](): Promise<void>; | ||
/** | ||
* Asynchronous fstat(2) - Get file status. | ||
@@ -111,5 +125,5 @@ */ | ||
*/ | ||
write(buffer: Uint8Array, offset?: number, length?: number, position?: number): Promise<{ | ||
write<TBuffer extends Uint8Array>(buffer: TBuffer, offset?: number, length?: number, position?: number): Promise<{ | ||
bytesWritten: number; | ||
buffer: Uint8Array; | ||
buffer: TBuffer; | ||
}>; | ||
@@ -143,8 +157,10 @@ /** | ||
* See `fs.writev` promisified version. | ||
* @todo Implement | ||
*/ | ||
writev(buffers: ReadonlyArray<Uint8Array>, position?: number): Promise<Node.WriteVResult>; | ||
writev(buffers: NodeJS.ArrayBufferView[], position?: number): Promise<Node.WriteVResult>; | ||
/** | ||
* See `fs.readv` promisified version. | ||
* @todo Implement | ||
*/ | ||
readv(buffers: ReadonlyArray<Uint8Array>, position?: number): Promise<Node.ReadVResult>; | ||
readv(buffers: readonly NodeJS.ArrayBufferView[], position?: number): Promise<Node.ReadVResult>; | ||
/** | ||
@@ -154,2 +170,4 @@ * Asynchronous close(2) - close a `FileHandle`. | ||
close(): Promise<void>; | ||
createReadStream(options?: CreateReadStreamOptions): Node.ReadStream; | ||
createWriteStream(options?: CreateWriteStreamOptions): Node.WriteStream; | ||
} | ||
@@ -223,4 +241,4 @@ /** | ||
flag?: Node.OpenMode; | ||
}): Promise<Uint8Array>; | ||
export declare function readFile(filename: PathLike, options: (Node.BaseEncodingOptions & { | ||
}): Promise<Buffer>; | ||
export declare function readFile(filename: PathLike, options: (Node.EncodingOption & { | ||
flag?: Node.OpenMode; | ||
@@ -250,3 +268,3 @@ }) | BufferEncoding): Promise<string>; | ||
*/ | ||
export declare function appendFile(filename: PathLike, data: FileContents, _options?: BufferEncoding | (Node.BaseEncodingOptions & { | ||
export declare function appendFile(filename: PathLike, data: FileContents, _options?: BufferEncoding | (Node.EncodingOption & { | ||
mode?: Node.Mode; | ||
@@ -256,56 +274,2 @@ flag?: Node.OpenMode; | ||
/** | ||
* Write buffer to the file specified by `fd`. | ||
* Note that it is unsafe to use fs.write multiple times on the same file without waiting for it to return. | ||
* @param handle | ||
* @param data Uint8Array containing the data to write to the file. | ||
* @param offset Offset in the buffer to start reading data from. | ||
* @param length The amount of bytes to write to the file. | ||
* @param position Offset from the beginning of the file where this data should be written. If position is null, the data will be written at the current position. | ||
*/ | ||
export declare function write(handle: FileHandle, data: Uint8Array, offset: number, length: number, position?: number): Promise<{ | ||
bytesWritten: number; | ||
buffer: Uint8Array; | ||
}>; | ||
export declare function write(handle: FileHandle, data: string, position?: number, encoding?: BufferEncoding): Promise<{ | ||
bytesWritten: number; | ||
buffer: string; | ||
}>; | ||
/** | ||
* Read data from the file specified by `fd`. | ||
* @param handle | ||
* @param buffer The buffer that the data will be | ||
* written to. | ||
* @param offset The offset within the buffer where writing will | ||
* start. | ||
* @param length An integer specifying the number of bytes to read. | ||
* @param position An integer specifying where to begin reading from | ||
* in the file. If position is null, data will be read from the current file | ||
* position. | ||
*/ | ||
export declare function read(handle: FileHandle, buffer: Uint8Array, offset: number, length: number, position?: number): Promise<{ | ||
bytesRead: number; | ||
buffer: Uint8Array; | ||
}>; | ||
/** | ||
* `fchown`. | ||
* @param handle | ||
* @param uid | ||
* @param gid | ||
*/ | ||
export declare function fchown(handle: FileHandle, uid: number, gid: number): Promise<void>; | ||
/** | ||
* `fchmod`. | ||
* @param handle | ||
* @param mode | ||
*/ | ||
export declare function fchmod(handle: FileHandle, mode: Node.Mode): Promise<void>; | ||
/** | ||
* Change the file timestamps of a file referenced by the supplied file | ||
* descriptor. | ||
* @param handle | ||
* @param atime | ||
* @param mtime | ||
*/ | ||
export declare function futimes(handle: FileHandle, atime: string | number | Date, mtime: string | number | Date): Promise<void>; | ||
/** | ||
* `rmdir`. | ||
@@ -330,3 +294,3 @@ * @param path | ||
*/ | ||
export declare function readdir(path: PathLike, options?: (Node.BaseEncodingOptions & { | ||
export declare function readdir(path: PathLike, options?: (Node.EncodingOption & { | ||
withFileTypes?: false; | ||
@@ -336,4 +300,4 @@ }) | BufferEncoding): Promise<string[]>; | ||
withFileTypes?: false; | ||
}): Promise<Uint8Array[]>; | ||
export declare function readdir(path: PathLike, options: Node.BaseEncodingOptions & { | ||
}): Promise<Buffer[]>; | ||
export declare function readdir(path: PathLike, options: Node.EncodingOption & { | ||
withFileTypes: true; | ||
@@ -358,4 +322,4 @@ }): Promise<Dirent[]>; | ||
*/ | ||
export declare function readlink(path: PathLike, options: Node.BufferEncodingOption): Promise<Uint8Array>; | ||
export declare function readlink(path: PathLike, options?: Node.BaseEncodingOptions | BufferEncoding): Promise<string>; | ||
export declare function readlink(path: PathLike, options: Node.BufferEncodingOption): Promise<Buffer>; | ||
export declare function readlink(path: PathLike, options?: Node.EncodingOption | BufferEncoding): Promise<string>; | ||
/** | ||
@@ -408,15 +372,12 @@ * `chown`. | ||
*/ | ||
export declare function realpath(path: PathLike, options: Node.BufferEncodingOption): Promise<Uint8Array>; | ||
export declare function realpath(path: PathLike, options?: Node.BaseEncodingOptions | BufferEncoding): Promise<string>; | ||
export declare function watchFile(filename: PathLike, listener: (curr: Stats, prev: Stats) => void): Promise<void>; | ||
export declare function watchFile(filename: PathLike, options: { | ||
persistent?: boolean; | ||
interval?: number; | ||
}, listener: (curr: Stats, prev: Stats) => void): Promise<void>; | ||
export declare function unwatchFile(filename: PathLike, listener?: (curr: Stats, prev: Stats) => void): Promise<void>; | ||
export declare function watch(filename: PathLike, listener?: (event: string, filename: PathLike) => any): Promise<Node.FSWatcher>; | ||
export declare function watch(filename: PathLike, options: { | ||
persistent?: boolean; | ||
}, listener?: (event: string, filename: string) => any): Promise<Node.FSWatcher>; | ||
export declare function realpath(path: PathLike, options: Node.BufferEncodingOption): Promise<Buffer>; | ||
export declare function realpath(path: PathLike, options?: Node.EncodingOption | BufferEncoding): Promise<string>; | ||
/** | ||
* @todo Implement | ||
*/ | ||
export declare function watch(filename: PathLike, options: (Node.WatchOptions & { | ||
encoding: 'buffer'; | ||
}) | 'buffer'): AsyncIterable<FileChangeInfo<Buffer>>; | ||
export declare function watch(filename: PathLike, options?: Node.WatchOptions | BufferEncoding): AsyncIterable<FileChangeInfo<string>>; | ||
/** | ||
* `access`. | ||
@@ -427,17 +388,30 @@ * @param path | ||
export declare function access(path: PathLike, mode?: number): Promise<void>; | ||
export declare function createReadStream(path: PathLike, options?: { | ||
flags?: string; | ||
encoding?: string; | ||
fd?: number; | ||
mode?: number; | ||
autoClose?: boolean; | ||
}): Promise<Node.ReadStream>; | ||
export declare function createWriteStream(path: PathLike, options?: { | ||
flags?: string; | ||
encoding?: string; | ||
fd?: number; | ||
mode?: number; | ||
}): Promise<Node.WriteStream>; | ||
export declare function rm(path: PathLike): Promise<void>; | ||
export declare function mkdtemp(path: PathLike): Promise<void>; | ||
export declare function copyFile(path: PathLike): Promise<void>; | ||
/** | ||
* @todo Implement | ||
*/ | ||
export declare function rm(path: PathLike, options?: Node.RmOptions): Promise<void>; | ||
/** | ||
* @todo Implement | ||
*/ | ||
export declare function mkdtemp(prefix: string, options?: Node.EncodingOption): Promise<string>; | ||
export declare function mkdtemp(prefix: string, options?: Node.BufferEncodingOption): Promise<Buffer>; | ||
/** | ||
* @todo Implement | ||
*/ | ||
export declare function copyFile(src: PathLike, dest: PathLike, mode?: number): Promise<void>; | ||
/** | ||
* @todo Implement | ||
*/ | ||
export declare function opendir(path: PathLike, options?: Node.OpenDirOptions): Promise<Dir>; | ||
export declare function cp(source: PathLike, destination: PathLike, opts?: Node.CopyOptions): Promise<void>; | ||
/** | ||
* @since v18.15.0 | ||
* @return Fulfills with an {fs.StatFs} for the file system. | ||
*/ | ||
export declare function statfs(path: PathLike, opts?: Node.StatFsOptions & { | ||
bigint?: false; | ||
}): Promise<StatsFs>; | ||
export declare function statfs(path: PathLike, opts: Node.StatFsOptions & { | ||
bigint: true; | ||
}): Promise<BigIntStatsFs>; | ||
export declare function statfs(path: PathLike, opts?: Node.StatFsOptions): Promise<StatsFs | BigIntStatsFs>; |
@@ -0,10 +1,10 @@ | ||
import { Buffer } from 'buffer'; | ||
import { ApiError, ErrorCode } from '../ApiError.js'; | ||
export * as constants from './constants.js'; | ||
import { ActionType, isReadable, isWriteable, isAppendable, parseFlag, pathExistsAction, pathNotExistsAction } from '../file.js'; | ||
import { normalizePath, normalizeMode, getFdForFile, normalizeOptions, fd2file, fdMap, normalizeTime, cred, nop, resolveFS, fixError, mounts } from './shared.js'; | ||
import { ActionType, isAppendable, isReadable, isWriteable, parseFlag, pathExistsAction, pathNotExistsAction } from '../file.js'; | ||
import { BigIntStats, FileType } from '../stats.js'; | ||
import { decode, encode } from '../utils.js'; | ||
import { F_OK } from './constants.js'; | ||
import { Dirent } from './dir.js'; | ||
import { dirname, join } from './path.js'; | ||
import { F_OK } from './constants.js'; | ||
import { cred, fd2file, fdMap, fixError, getFdForFile, mounts, normalizeMode, normalizeOptions, normalizePath, normalizeTime, resolveFS } from './shared.js'; | ||
export * as constants from './constants.js'; | ||
export class FileHandle { | ||
@@ -18,2 +18,8 @@ constructor( | ||
} | ||
get file() { | ||
return fd2file(this.fd); | ||
} | ||
get path() { | ||
return this.file.path; | ||
} | ||
/** | ||
@@ -23,3 +29,3 @@ * Asynchronous fchown(2) - Change ownership of a file. | ||
chown(uid, gid) { | ||
return fd2file(this.fd).chown(uid, gid); | ||
return this.file.chown(uid, gid); | ||
} | ||
@@ -35,3 +41,3 @@ /** | ||
} | ||
return fd2file(this.fd).chmod(numMode); | ||
return this.file.chmod(numMode); | ||
} | ||
@@ -42,3 +48,3 @@ /** | ||
datasync() { | ||
return fd2file(this.fd).datasync(); | ||
return this.file.datasync(); | ||
} | ||
@@ -49,3 +55,3 @@ /** | ||
sync() { | ||
return fd2file(this.fd).sync(); | ||
return this.file.sync(); | ||
} | ||
@@ -60,3 +66,3 @@ /** | ||
} | ||
return fd2file(this.fd).truncate(len); | ||
return this.file.truncate(len); | ||
} | ||
@@ -69,3 +75,3 @@ /** | ||
utimes(atime, mtime) { | ||
return fd2file(this.fd).utimes(normalizeTime(atime), normalizeTime(mtime)); | ||
return this.file.utimes(normalizeTime(atime), normalizeTime(mtime)); | ||
} | ||
@@ -91,4 +97,4 @@ /** | ||
} | ||
const encodedData = typeof data == 'string' ? encode(data) : data; | ||
await fd2file(this.fd).write(encodedData, 0, encodedData.length, null); | ||
const encodedData = typeof data == 'string' ? Buffer.from(data, options.encoding) : data; | ||
await this.file.write(encodedData, 0, encodedData.length, null); | ||
} | ||
@@ -105,5 +111,5 @@ /** | ||
if (isNaN(+position)) { | ||
position = fd2file(this.fd).position; | ||
position = this.file.position; | ||
} | ||
return fd2file(this.fd).read(buffer, offset, length, position); | ||
return this.file.read(buffer, offset, length, position); | ||
} | ||
@@ -118,7 +124,28 @@ async readFile(_options) { | ||
const data = new Uint8Array(size); | ||
await fd2file(this.fd).read(data, 0, size, 0); | ||
return options.encoding ? decode(data, options.encoding) : data; | ||
await this.file.read(data, 0, size, 0); | ||
const buffer = Buffer.from(data); | ||
return options.encoding ? buffer.toString(options.encoding) : buffer; | ||
} | ||
/** | ||
* Returns a `ReadableStream` that may be used to read the files data. | ||
* | ||
* An error will be thrown if this method is called more than once or is called after the `FileHandle` is closed | ||
* or closing. | ||
* | ||
* While the `ReadableStream` will read the file to completion, it will not close the `FileHandle` automatically. User code must still call the `fileHandle.close()` method. | ||
* | ||
* @since v17.0.0 | ||
* @experimental | ||
*/ | ||
readableWebStream(options) { | ||
throw ApiError.With('ENOTSUP', this.path, 'FileHandle.readableWebStream'); | ||
} | ||
readLines(options) { | ||
throw ApiError.With('ENOTSUP', this.path, 'FileHandle.readLines'); | ||
} | ||
[Symbol.asyncDispose]() { | ||
throw ApiError.With('ENOTSUP', this.path, 'FileHandle.@@asyncDispose'); | ||
} | ||
async stat(opts) { | ||
const stats = await fd2file(this.fd).stat(); | ||
const stats = await this.file.stat(); | ||
return opts?.bigint ? new BigIntStats(stats) : stats; | ||
@@ -133,3 +160,3 @@ } | ||
offset = 0; | ||
buffer = encode(data, encoding); | ||
buffer = Buffer.from(data, encoding); | ||
length = buffer.length; | ||
@@ -144,4 +171,4 @@ } | ||
} | ||
position ?? (position = fd2file(this.fd).position); | ||
const bytesWritten = await fd2file(this.fd).write(buffer, offset, length, position); | ||
position ?? (position = this.file.position); | ||
const bytesWritten = await this.file.write(buffer, offset, length, position); | ||
return { buffer, bytesWritten }; | ||
@@ -169,16 +196,18 @@ } | ||
} | ||
const encodedData = typeof data == 'string' ? encode(data, options.encoding) : data; | ||
await fd2file(this.fd).write(encodedData, 0, encodedData.length, 0); | ||
const encodedData = typeof data == 'string' ? Buffer.from(data, options.encoding) : data; | ||
await this.file.write(encodedData, 0, encodedData.length, 0); | ||
} | ||
/** | ||
* See `fs.writev` promisified version. | ||
* @todo Implement | ||
*/ | ||
writev(buffers, position) { | ||
throw new ApiError(ErrorCode.ENOTSUP); | ||
throw ApiError.With('ENOTSUP', this.path, 'FileHandle.writev'); | ||
} | ||
/** | ||
* See `fs.readv` promisified version. | ||
* @todo Implement | ||
*/ | ||
readv(buffers, position) { | ||
throw new ApiError(ErrorCode.ENOTSUP); | ||
throw ApiError.With('ENOTSUP', this.path, 'FileHandle.readv'); | ||
} | ||
@@ -189,5 +218,11 @@ /** | ||
async close() { | ||
await fd2file(this.fd).close(); | ||
await this.file.close(); | ||
fdMap.delete(this.fd); | ||
} | ||
createReadStream(options) { | ||
throw ApiError.With('ENOTSUP', this.path, 'createReadStream'); | ||
} | ||
createWriteStream(options) { | ||
throw ApiError.With('ENOTSUP', this.path, 'createWriteStream'); | ||
} | ||
} | ||
@@ -380,4 +415,4 @@ /** | ||
} | ||
const data = await _readFile(filename, options.flag, true); | ||
return options.encoding ? decode(data, options.encoding) : data; | ||
const data = Buffer.from(await _readFile(filename, options.flag, true)); | ||
return options.encoding ? data.toString(options.encoding) : data; | ||
} | ||
@@ -439,56 +474,6 @@ readFile; | ||
} | ||
const encodedData = typeof data == 'string' ? encode(data) : data; | ||
const encodedData = typeof data == 'string' ? Buffer.from(data, options.encoding) : data; | ||
await _appendFile(filename, encodedData, options.flag, options.mode, true); | ||
} | ||
appendFile; | ||
export function write(handle, data, posOrOff, lenOrEnc, position) { | ||
return handle.write(data, posOrOff, lenOrEnc, position); | ||
} | ||
write; | ||
/** | ||
* Read data from the file specified by `fd`. | ||
* @param handle | ||
* @param buffer The buffer that the data will be | ||
* written to. | ||
* @param offset The offset within the buffer where writing will | ||
* start. | ||
* @param length An integer specifying the number of bytes to read. | ||
* @param position An integer specifying where to begin reading from | ||
* in the file. If position is null, data will be read from the current file | ||
* position. | ||
*/ | ||
export function read(handle, buffer, offset, length, position) { | ||
return handle.read(buffer, offset, length, position); | ||
} | ||
read; | ||
/** | ||
* `fchown`. | ||
* @param handle | ||
* @param uid | ||
* @param gid | ||
*/ | ||
export function fchown(handle, uid, gid) { | ||
return handle.chown(uid, gid); | ||
} | ||
fchown; | ||
/** | ||
* `fchmod`. | ||
* @param handle | ||
* @param mode | ||
*/ | ||
export function fchmod(handle, mode) { | ||
return handle.chmod(mode); | ||
} | ||
fchmod; | ||
/** | ||
* Change the file timestamps of a file referenced by the supplied file | ||
* descriptor. | ||
* @param handle | ||
* @param atime | ||
* @param mtime | ||
*/ | ||
export function futimes(handle, atime, mtime) { | ||
return handle.utimes(atime, mtime); | ||
} | ||
futimes; | ||
// DIRECTORY-ONLY METHODS | ||
@@ -558,3 +543,3 @@ /** | ||
export async function readlink(path, options) { | ||
const value = await _readFile(path, 'r', false); | ||
const value = Buffer.from(await _readFile(path, 'r', false)); | ||
const encoding = typeof options == 'object' ? options.encoding : options; | ||
@@ -564,3 +549,3 @@ if (encoding == 'buffer') { | ||
} | ||
return decode(value, encoding); | ||
return value.toString(encoding); | ||
} | ||
@@ -671,3 +656,3 @@ readlink; | ||
} | ||
const dst = mountPoint + normalizePath(decode(await _readFile(resolvedPath, 'r+', false))); | ||
const dst = mountPoint + normalizePath(Buffer.from(await _readFile(resolvedPath, 'r+', false)).toString()); | ||
return realpath(dst); | ||
@@ -680,11 +665,6 @@ } | ||
realpath; | ||
export async function watchFile(filename, arg2, listener = nop) { | ||
throw new ApiError(ErrorCode.ENOTSUP); | ||
export function watch(filename, options) { | ||
throw ApiError.With('ENOTSUP', filename, 'watch'); | ||
} | ||
export async function unwatchFile(filename, listener = nop) { | ||
throw new ApiError(ErrorCode.ENOTSUP); | ||
} | ||
export async function watch(filename, arg2, listener = nop) { | ||
throw new ApiError(ErrorCode.ENOTSUP); | ||
} | ||
watch; | ||
/** | ||
@@ -701,16 +681,34 @@ * `access`. | ||
} | ||
export async function createReadStream(path, options) { | ||
throw new ApiError(ErrorCode.ENOTSUP); | ||
access; | ||
/** | ||
* @todo Implement | ||
*/ | ||
export async function rm(path, options) { | ||
throw ApiError.With('ENOTSUP', path, 'rm'); | ||
} | ||
export async function createWriteStream(path, options) { | ||
throw new ApiError(ErrorCode.ENOTSUP); | ||
rm; | ||
export async function mkdtemp(prefix, options) { | ||
throw ApiError.With('ENOTSUP', prefix, 'mkdtemp'); | ||
} | ||
export async function rm(path) { | ||
throw new ApiError(ErrorCode.ENOTSUP); | ||
mkdtemp; | ||
/** | ||
* @todo Implement | ||
*/ | ||
export async function copyFile(src, dest, mode) { | ||
throw ApiError.With('ENOTSUP', src, 'copyFile'); | ||
} | ||
export async function mkdtemp(path) { | ||
throw new ApiError(ErrorCode.ENOTSUP); | ||
copyFile; | ||
/** | ||
* @todo Implement | ||
*/ | ||
export async function opendir(path, options) { | ||
throw ApiError.With('ENOTSUP', path, 'opendir'); | ||
} | ||
export async function copyFile(path) { | ||
throw new ApiError(ErrorCode.ENOTSUP); | ||
opendir; | ||
export async function cp(source, destination, opts) { | ||
throw ApiError.With('ENOTSUP', source, 'cp'); | ||
} | ||
cp; | ||
export async function statfs(path, opts) { | ||
throw ApiError.With('ENOTSUP', path, 'statfs'); | ||
} |
@@ -6,3 +6,3 @@ /// <reference types="node" resolution-mode="require"/> | ||
import type { File } from '../file.js'; | ||
import type { BaseEncodingOptions, OpenMode, WriteFileOptions } from 'node:fs'; | ||
import type { EncodingOption, OpenMode, WriteFileOptions } from 'node:fs'; | ||
/** | ||
@@ -38,3 +38,3 @@ * converts Date or number to a integer UNIX timestamp | ||
*/ | ||
export declare function normalizeOptions(options?: WriteFileOptions | (BaseEncodingOptions & { | ||
export declare function normalizeOptions(options?: WriteFileOptions | (EncodingOption & { | ||
flag?: OpenMode; | ||
@@ -96,10 +96,1 @@ }), encoding?: BufferEncoding, flag?: string, mode?: number): { | ||
export type PathLike = string; | ||
/** | ||
* @internal | ||
* | ||
* Recursivly converts `From` in `Target` to `To` | ||
*/ | ||
export type Convert<Target, From, To> = Target extends From ? To : Target extends (...args: any[]) => unknown ? (...args: Convert<Parameters<Target>, From, To> & Array<unknown>) => Convert<ReturnType<Target>, From, To> : Target extends object ? { | ||
[K in keyof Target]: Convert<Target[K], From, To>; | ||
} : Target; | ||
export type BufferToUint8Array<T> = Convert<T, Buffer, Uint8Array>; |
@@ -70,2 +70,3 @@ /// <reference types="node" resolution-mode="require"/> | ||
addListener(event: 'unpipe', listener: (src: Readable) => void): this; | ||
addListener(event: string | symbol, listener: (...args: any[]) => void): this; | ||
on(event: 'close', listener: () => void): this; | ||
@@ -79,2 +80,3 @@ on(event: 'drain', listener: () => void): this; | ||
on(event: 'unpipe', listener: (src: Readable) => void): this; | ||
on(event: string | symbol, listener: (...args: any[]) => void): this; | ||
once(event: 'close', listener: () => void): this; | ||
@@ -88,2 +90,3 @@ once(event: 'drain', listener: () => void): this; | ||
once(event: 'unpipe', listener: (src: Readable) => void): this; | ||
once(event: string | symbol, listener: (...args: any[]) => void): this; | ||
prependListener(event: 'close', listener: () => void): this; | ||
@@ -97,2 +100,3 @@ prependListener(event: 'drain', listener: () => void): this; | ||
prependListener(event: 'unpipe', listener: (src: Readable) => void): this; | ||
prependListener(event: string | symbol, listener: (...args: any[]) => void): this; | ||
prependOnceListener(event: 'close', listener: () => void): this; | ||
@@ -106,2 +110,3 @@ prependOnceListener(event: 'drain', listener: () => void): this; | ||
prependOnceListener(event: 'unpipe', listener: (src: Readable) => void): this; | ||
prependOnceListener(event: string | symbol, listener: (...args: any[]) => void): this; | ||
} |
/// <reference types="node" resolution-mode="require"/> | ||
/// <reference types="node" resolution-mode="require"/> | ||
import { FileContents } from '../filesystem.js'; | ||
import { BigIntStats, type Stats } from '../stats.js'; | ||
import type { symlink, ReadSyncOptions, BaseEncodingOptions, BufferEncodingOption } from 'fs'; | ||
import { BigIntStats, type BigIntStatsFs, type Stats, type StatsFs } from '../stats.js'; | ||
import type { symlink, ReadSyncOptions, EncodingOption, BufferEncodingOption } from 'fs'; | ||
import type * as Node from 'fs'; | ||
@@ -26,3 +26,3 @@ import { PathLike } from './shared.js'; | ||
export declare function statSync(path: PathLike, options?: { | ||
bigint: false; | ||
bigint?: false; | ||
}): Stats; | ||
@@ -39,3 +39,3 @@ export declare function statSync(path: PathLike, options: { | ||
export declare function lstatSync(path: PathLike, options?: { | ||
bigint: false; | ||
bigint?: false; | ||
}): Stats; | ||
@@ -80,4 +80,4 @@ export declare function lstatSync(path: PathLike, options: { | ||
flag?: string; | ||
}): Uint8Array; | ||
export declare function readFileSync(filename: string, options: (Node.BaseEncodingOptions & { | ||
}): Buffer; | ||
export declare function readFileSync(filename: string, options: (Node.EncodingOption & { | ||
flag?: string; | ||
@@ -110,3 +110,3 @@ }) | BufferEncoding): string; | ||
*/ | ||
export declare function appendFileSync(filename: string, data: FileContents, arg3?: Node.WriteFileOptions): void; | ||
export declare function appendFileSync(filename: string, data: FileContents, _options?: Node.WriteFileOptions): void; | ||
/** | ||
@@ -223,3 +223,3 @@ * Synchronous `fstat`. | ||
withFileTypes?: false; | ||
} | 'buffer'): Uint8Array[]; | ||
} | 'buffer'): Buffer[]; | ||
export declare function readdirSync(path: PathLike, options: { | ||
@@ -245,4 +245,4 @@ withFileTypes: true; | ||
*/ | ||
export declare function readlinkSync(path: PathLike, options?: BufferEncodingOption): Uint8Array; | ||
export declare function readlinkSync(path: PathLike, options: BaseEncodingOptions | BufferEncoding): string; | ||
export declare function readlinkSync(path: PathLike, options?: BufferEncodingOption): Buffer; | ||
export declare function readlinkSync(path: PathLike, options: EncodingOption | BufferEncoding): string; | ||
/** | ||
@@ -296,4 +296,4 @@ * Synchronous `chown`. | ||
*/ | ||
export declare function realpathSync(path: PathLike, options: BufferEncodingOption): Uint8Array; | ||
export declare function realpathSync(path: PathLike, options?: BaseEncodingOptions | BufferEncoding): string; | ||
export declare function realpathSync(path: PathLike, options: BufferEncodingOption): Buffer; | ||
export declare function realpathSync(path: PathLike, options?: EncodingOption): string; | ||
/** | ||
@@ -305,8 +305,43 @@ * Synchronous `access`. | ||
export declare function accessSync(path: PathLike, mode?: number): void; | ||
/** | ||
* @todo Implement | ||
*/ | ||
export declare function rmSync(path: PathLike): void; | ||
export declare function mkdtempSync(prefix: string, options: BufferEncodingOption): Uint8Array; | ||
export declare function mkdtempSync(prefix: string, options?: BaseEncodingOptions | BufferEncoding): string; | ||
/** | ||
* @todo Implement | ||
*/ | ||
export declare function mkdtempSync(prefix: string, options: BufferEncodingOption): Buffer; | ||
export declare function mkdtempSync(prefix: string, options?: EncodingOption): string; | ||
/** | ||
* @todo Implement | ||
*/ | ||
export declare function copyFileSync(src: string, dest: string, flags?: number): void; | ||
/** | ||
* @todo Implement | ||
*/ | ||
export declare function readvSync(fd: number, buffers: readonly Uint8Array[], position?: number): number; | ||
/** | ||
* @todo Implement | ||
*/ | ||
export declare function writevSync(fd: number, buffers: readonly Uint8Array[], position?: number): number; | ||
/** | ||
* @todo Implement | ||
*/ | ||
export declare function opendirSync(path: PathLike, options?: Node.OpenDirOptions): Dir; | ||
/** | ||
* @todo Implement | ||
*/ | ||
export declare function cpSync(source: PathLike, destination: PathLike, opts?: Node.CopySyncOptions): void; | ||
/** | ||
* Synchronous statfs(2). Returns information about the mounted file system which contains path. The callback gets two arguments (err, stats) where stats is an <fs.StatFs> object. | ||
* In case of an error, the err.code will be one of Common System Errors. | ||
* @param path A path to an existing file or directory on the file system to be queried. | ||
* @param callback | ||
*/ | ||
export declare function statfsSync(path: PathLike, options?: Node.StatFsOptions & { | ||
bigint?: false; | ||
}): StatsFs; | ||
export declare function statfsSync(path: PathLike, options: Node.StatFsOptions & { | ||
bigint: true; | ||
}): BigIntStatsFs; | ||
export declare function statfsSync(path: PathLike, options?: Node.StatFsOptions): StatsFs | BigIntStatsFs; |
import { ApiError, ErrorCode } from '../ApiError.js'; | ||
import { ActionType, isAppendable, isReadable, isWriteable, parseFlag, pathExistsAction, pathNotExistsAction } from '../file.js'; | ||
import { BigIntStats, FileType } from '../stats.js'; | ||
import { normalizePath, cred, getFdForFile, normalizeMode, normalizeOptions, fdMap, fd2file, normalizeTime, resolveFS, fixError, mounts, } from './shared.js'; | ||
import { decode, encode } from '../utils.js'; | ||
import { normalizePath, cred, getFdForFile, normalizeMode, normalizeOptions, fdMap, fd2file, normalizeTime, resolveFS, fixError, mounts } from './shared.js'; | ||
import { Dirent } from './dir.js'; | ||
@@ -182,4 +181,4 @@ import { dirname, join } from './path.js'; | ||
} | ||
const data = _readFileSync(filename, options.flag, true); | ||
return options.encoding ? decode(data, options.encoding) : data; | ||
const data = Buffer.from(_readFileSync(filename, options.flag, true)); | ||
return options.encoding ? data.toString(options.encoding) : data; | ||
} | ||
@@ -211,3 +210,3 @@ readFileSync; | ||
} | ||
const encodedData = typeof data == 'string' ? encode(data, options.encoding) : data; | ||
const encodedData = typeof data == 'string' ? Buffer.from(data, options.encoding) : data; | ||
if (encodedData === undefined) { | ||
@@ -243,4 +242,4 @@ throw new ApiError(ErrorCode.EINVAL, 'Data not specified'); | ||
*/ | ||
export function appendFileSync(filename, data, arg3) { | ||
const options = normalizeOptions(arg3, 'utf8', 'a', 0o644); | ||
export function appendFileSync(filename, data, _options) { | ||
const options = normalizeOptions(_options, 'utf8', 'a', 0o644); | ||
const flag = parseFlag(options.flag); | ||
@@ -253,3 +252,3 @@ if (!isAppendable(flag)) { | ||
} | ||
const encodedData = typeof data == 'string' ? encode(data) : data; | ||
const encodedData = typeof data == 'string' ? Buffer.from(data, options.encoding) : data; | ||
_appendFileSync(filename, encodedData, options.flag, options.mode, true); | ||
@@ -307,3 +306,3 @@ } | ||
offset = 0; | ||
buffer = encode(data, encoding); | ||
buffer = Buffer.from(data, encoding); | ||
length = buffer.length; | ||
@@ -327,7 +326,9 @@ } | ||
const file = fd2file(fd); | ||
let offset = opts; | ||
const offset = typeof opts == 'object' ? opts.offset : opts; | ||
if (typeof opts == 'object') { | ||
({ offset, length, position } = opts); | ||
length = opts.length; | ||
position = opts.position; | ||
} | ||
if (isNaN(+position)) { | ||
position = Number(position); | ||
if (isNaN(position)) { | ||
position = file.position; | ||
@@ -405,3 +406,3 @@ } | ||
if (options == 'buffer' || (typeof options == 'object' && options.encoding == 'buffer')) { | ||
return encode(entry); | ||
return Buffer.from(entry); | ||
} | ||
@@ -442,3 +443,3 @@ return entry; | ||
export function readlinkSync(path, options) { | ||
const value = _readFileSync(path, 'r', false); | ||
const value = Buffer.from(_readFileSync(path, 'r', false)); | ||
const encoding = typeof options == 'object' ? options.encoding : options; | ||
@@ -448,3 +449,3 @@ if (encoding == 'buffer') { | ||
} | ||
return decode(value, encoding); | ||
return value.toString(encoding); | ||
} | ||
@@ -531,3 +532,3 @@ readlinkSync; | ||
} | ||
const dst = normalizePath(mountPoint + decode(_readFileSync(resolvedPath, 'r+', false))); | ||
const dst = normalizePath(mountPoint + Buffer.from(_readFileSync(resolvedPath, 'r+', false)).toString()); | ||
return realpathSync(dst); | ||
@@ -552,25 +553,50 @@ } | ||
accessSync; | ||
/** | ||
* @todo Implement | ||
*/ | ||
export function rmSync(path) { | ||
throw new ApiError(ErrorCode.ENOTSUP); | ||
throw ApiError.With('ENOTSUP', path, 'rmSync'); | ||
} | ||
rmSync; | ||
export function mkdtempSync(prefix, options) { | ||
throw new ApiError(ErrorCode.ENOTSUP); | ||
throw ApiError.With('ENOTSUP', prefix, 'mkdtempSync'); | ||
} | ||
mkdtempSync; | ||
/** | ||
* @todo Implement | ||
*/ | ||
export function copyFileSync(src, dest, flags) { | ||
throw new ApiError(ErrorCode.ENOTSUP); | ||
throw ApiError.With('ENOTSUP', src, 'copyFileSync'); | ||
} | ||
copyFileSync; | ||
/** | ||
* @todo Implement | ||
*/ | ||
export function readvSync(fd, buffers, position) { | ||
throw new ApiError(ErrorCode.ENOTSUP); | ||
throw ApiError.With('ENOTSUP', fd2file(fd).path, 'readvSync'); | ||
} | ||
readvSync; | ||
/** | ||
* @todo Implement | ||
*/ | ||
export function writevSync(fd, buffers, position) { | ||
throw new ApiError(ErrorCode.ENOTSUP); | ||
throw ApiError.With('ENOTSUP', fd2file(fd).path, 'writevSync'); | ||
} | ||
writevSync; | ||
/** | ||
* @todo Implement | ||
*/ | ||
export function opendirSync(path, options) { | ||
throw new ApiError(ErrorCode.ENOTSUP); | ||
throw ApiError.With('ENOTSUP', path, 'opendirSync'); | ||
} | ||
opendirSync; | ||
/** | ||
* @todo Implement | ||
*/ | ||
export function cpSync(source, destination, opts) { | ||
throw ApiError.With('ENOTSUP', source, 'cpSync'); | ||
} | ||
cpSync; | ||
export function statfsSync(path, options) { | ||
throw ApiError.With('ENOTSUP', path, 'statfsSync'); | ||
} |
@@ -0,1 +1,4 @@ | ||
/// <reference types="node" resolution-mode="require"/> | ||
/// <reference types="node" resolution-mode="require"/> | ||
import type { FileReadResult } from 'node:fs/promises'; | ||
import type { FileSystem } from './filesystem.js'; | ||
@@ -125,6 +128,3 @@ import { Stats, type FileType } from './stats.js'; | ||
*/ | ||
abstract read<TBuffer extends Uint8Array>(buffer: TBuffer, offset?: number, length?: number, position?: number): Promise<{ | ||
bytesRead: number; | ||
buffer: TBuffer; | ||
}>; | ||
abstract read<TBuffer extends NodeJS.ArrayBufferView>(buffer: TBuffer, offset?: number, length?: number, position?: number): Promise<FileReadResult<TBuffer>>; | ||
/** | ||
@@ -139,3 +139,3 @@ * Read data from the file. | ||
*/ | ||
abstract readSync(buffer: Uint8Array, offset?: number, length?: number, position?: number): number; | ||
abstract readSync(buffer: ArrayBufferView, offset?: number, length?: number, position?: number): number; | ||
/** | ||
@@ -307,3 +307,3 @@ * Asynchronous `datasync`. | ||
*/ | ||
read<TBuffer extends Uint8Array>(buffer: TBuffer, offset?: number, length?: number, position?: number): Promise<{ | ||
read<TBuffer extends ArrayBufferView>(buffer: TBuffer, offset?: number, length?: number, position?: number): Promise<{ | ||
bytesRead: number; | ||
@@ -323,3 +323,3 @@ buffer: TBuffer; | ||
*/ | ||
readSync(buffer: Uint8Array, offset?: number, length?: number, position?: number): number; | ||
readSync(buffer: ArrayBufferView, offset?: number, length?: number, position?: number): number; | ||
/** | ||
@@ -326,0 +326,0 @@ * Asynchronous `fchmod`. |
@@ -382,3 +382,3 @@ import { ApiError, ErrorCode } from './ApiError.js'; | ||
} | ||
buffer.set(this._buffer.slice(position, end), offset); | ||
new Uint8Array(buffer.buffer).set(this._buffer.slice(position, end), offset); | ||
return bytesRead; | ||
@@ -385,0 +385,0 @@ } |
@@ -204,17 +204,33 @@ /// <reference types="node" resolution-mode="require"/> | ||
} | ||
/** | ||
* @returns true if stats is a file. | ||
*/ | ||
export declare function isFile(stats: StatsLike): boolean; | ||
/** | ||
* @returns True if stats is a directory. | ||
*/ | ||
export declare function isDirectory(stats: StatsLike): boolean; | ||
/** | ||
* @returns true if stats is a symbolic link | ||
*/ | ||
export declare function isSymbolicLink(stats: StatsLike): boolean; | ||
export declare function isSocket(): boolean; | ||
export declare function isBlockDevice(): boolean; | ||
export declare function isCharacterDevice(): boolean; | ||
export declare function isFIFO(): boolean; | ||
export declare class StatsFs implements Node.StatsFsBase<number> { | ||
/** Type of file system. */ | ||
type: number; | ||
/** Optimal transfer block size. */ | ||
bsize: number; | ||
/** Total data blocks in file system. */ | ||
blocks: number; | ||
/** Free blocks in file system. */ | ||
bfree: number; | ||
/** Available blocks for unprivileged users */ | ||
bavail: number; | ||
/** Total file nodes in file system. */ | ||
files: number; | ||
/** Free file nodes in file system. */ | ||
ffree: number; | ||
} | ||
export declare class BigIntStatsFs implements Node.StatsFsBase<bigint> { | ||
/** Type of file system. */ | ||
type: bigint; | ||
/** Optimal transfer block size. */ | ||
bsize: bigint; | ||
/** Total data blocks in file system. */ | ||
blocks: bigint; | ||
/** Free blocks in file system. */ | ||
bfree: bigint; | ||
/** Available blocks for unprivileged users */ | ||
bavail: bigint; | ||
/** Total file nodes in file system. */ | ||
files: bigint; | ||
/** Free file nodes in file system. */ | ||
ffree: bigint; | ||
} |
@@ -238,32 +238,5 @@ import { S_IFDIR, S_IFLNK, S_IFMT, S_IFREG, S_IRWXG, S_IRWXO, S_IRWXU } from './emulation/constants.js'; | ||
} | ||
BigIntStats; | ||
/** | ||
* @returns true if stats is a file. | ||
*/ | ||
export function isFile(stats) { | ||
return (Number(stats.mode) & S_IFMT) === S_IFREG; | ||
export class StatsFs { | ||
} | ||
/** | ||
* @returns True if stats is a directory. | ||
*/ | ||
export function isDirectory(stats) { | ||
return (Number(stats.mode) & S_IFMT) === S_IFDIR; | ||
export class BigIntStatsFs { | ||
} | ||
/** | ||
* @returns true if stats is a symbolic link | ||
*/ | ||
export function isSymbolicLink(stats) { | ||
return (Number(stats.mode) & S_IFMT) === S_IFLNK; | ||
} | ||
export function isSocket() { | ||
return false; | ||
} | ||
export function isBlockDevice() { | ||
return false; | ||
} | ||
export function isCharacterDevice() { | ||
return false; | ||
} | ||
export function isFIFO() { | ||
return false; | ||
} |
@@ -1,2 +0,1 @@ | ||
/// <reference types="node" resolution-mode="require"/> | ||
import { FileSystem } from './filesystem.js'; | ||
@@ -28,3 +27,3 @@ import { Cred } from './cred.js'; | ||
*/ | ||
export declare function encode(input: string, encoding?: BufferEncoding): Uint8Array; | ||
export declare function encode(input: string): Uint8Array; | ||
/** | ||
@@ -34,3 +33,3 @@ * Decodes a string from a buffer | ||
*/ | ||
export declare function decode(input?: Uint8Array, encoding?: BufferEncoding): string; | ||
export declare function decode(input?: Uint8Array): string; | ||
/** | ||
@@ -37,0 +36,0 @@ * Decodes a directory listing |
@@ -100,46 +100,7 @@ import { ApiError, ErrorCode } from './ApiError.js'; | ||
*/ | ||
export function encode(input, encoding = 'utf8') { | ||
export function encode(input) { | ||
if (typeof input != 'string') { | ||
throw new ApiError(ErrorCode.EINVAL, 'Can not encode a non-string'); | ||
} | ||
switch (encoding) { | ||
case 'ascii': | ||
return new Uint8Array(Array.from(input).map(char => char.charCodeAt(0) & 0x7f)); | ||
case 'latin1': | ||
case 'binary': | ||
return new Uint8Array(Array.from(input).map(char => char.charCodeAt(0))); | ||
case 'utf8': | ||
case 'utf-8': | ||
return new Uint8Array(Array.from(input).flatMap(char => { | ||
const code = char.charCodeAt(0); | ||
if (code < 0x80) { | ||
return code; | ||
} | ||
const a = (code & 0x3f) | 0x80; | ||
if (code < 0x800) { | ||
return [(code >> 6) | 0xc0, a]; | ||
} | ||
const b = ((code >> 6) & 0x3f) | 0x80; | ||
if (code < 0x10000) { | ||
return [(code >> 12) | 0xe0, b, a]; | ||
} | ||
return [(code >> 18) | 0xf0, ((code >> 12) & 0x3f) | 0x80, b, a]; | ||
})); | ||
case 'base64': | ||
return encode(atob(input), 'binary'); | ||
case 'base64url': | ||
return encode(input.replace('_', '/').replace('-', '+'), 'base64'); | ||
case 'hex': | ||
return new Uint8Array(input.match(/.{1,2}/g).map(e => parseInt(e, 16))); | ||
case 'utf16le': | ||
case 'ucs2': | ||
case 'ucs-2': | ||
const u16 = new Uint16Array(new ArrayBuffer(input.length * 2)); | ||
for (let i = 0; i < input.length; i++) { | ||
u16[i] = input.charCodeAt(i); | ||
} | ||
return new Uint8Array(u16.buffer); | ||
default: | ||
throw new ApiError(ErrorCode.EINVAL, 'Invalid encoding: ' + encoding); | ||
} | ||
return new Uint8Array(Array.from(input).map(char => char.charCodeAt(0))); | ||
} | ||
@@ -150,56 +111,9 @@ /** | ||
*/ | ||
export function decode(input, encoding = 'utf8') { | ||
export function decode(input) { | ||
if (!(input instanceof Uint8Array)) { | ||
throw new ApiError(ErrorCode.EINVAL, 'Can not decode a non-Uint8Array'); | ||
} | ||
switch (encoding) { | ||
case 'ascii': | ||
return Array.from(input) | ||
.map(char => String.fromCharCode(char & 0x7f)) | ||
.join(''); | ||
case 'latin1': | ||
case 'binary': | ||
return Array.from(input) | ||
.map(char => String.fromCharCode(char)) | ||
.join(''); | ||
case 'utf8': | ||
case 'utf-8': | ||
let utf8String = ''; | ||
for (let i = 0; i < input.length; i++) { | ||
let code; | ||
if (input[i] < 0x80) { | ||
code = input[i]; | ||
} | ||
else if (input[i] < 0xe0) { | ||
code = ((input[i] & 0x1f) << 6) | (input[++i] & 0x3f); | ||
} | ||
else if (input[i] < 0xf0) { | ||
code = ((input[i] & 0x0f) << 12) | ((input[++i] & 0x3f) << 6) | (input[++i] & 0x3f); | ||
} | ||
else { | ||
code = ((input[i] & 0x07) << 18) | ((input[++i] & 0x3f) << 12) | ((input[++i] & 0x3f) << 6) | (input[++i] & 0x3f); | ||
} | ||
utf8String += String.fromCharCode(code); | ||
} | ||
return utf8String; | ||
case 'utf16le': | ||
case 'ucs2': | ||
case 'ucs-2': | ||
let utf16leString = ''; | ||
for (let i = 0; i < input.length; i += 2) { | ||
const code = input[i] | (input[i + 1] << 8); | ||
utf16leString += String.fromCharCode(code); | ||
} | ||
return utf16leString; | ||
case 'base64': | ||
return btoa(decode(input, 'binary')); | ||
case 'base64url': | ||
return decode(input, 'base64').replace('/', '_').replace('+', '-'); | ||
case 'hex': | ||
return Array.from(input) | ||
.map(e => e.toString(16).padStart(2, '0')) | ||
.join(''); | ||
default: | ||
throw new ApiError(ErrorCode.EINVAL, 'Invalid encoding: ' + encoding); | ||
} | ||
return Array.from(input) | ||
.map(char => String.fromCharCode(char)) | ||
.join(''); | ||
} | ||
@@ -206,0 +120,0 @@ /** |
{ | ||
"name": "@zenfs/core", | ||
"version": "0.5.5", | ||
"version": "0.5.6", | ||
"description": "A filesystem in your browser", | ||
@@ -54,4 +54,5 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"@types/node": "^14.0.0", | ||
"@types/node": "^20.12.5", | ||
"@types/readable-stream": "^4.0.10", | ||
"buffer": "^6.0.3", | ||
"minimatch": "^9.0.3", | ||
@@ -58,0 +59,0 @@ "readable-stream": "^4.5.2" |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1480219
11354
5
+ Addedbuffer@^6.0.3
+ Added@types/node@20.17.12(transitive)
+ Addedundici-types@6.19.8(transitive)
- Removed@types/node@14.18.63(transitive)
Updated@types/node@^20.12.5