react-native-fast-io
Version:
Modern IO for React Native, built on top of Nitro and Web standards
96 lines (94 loc) • 2.61 kB
JavaScript
import { FileSystem } from "../native/fs.nitro.js";
import { StreamFactory } from "../native/streams.nitro.js";
import { Blob } from "./blob.js";
import { toReadableStream } from "./streams.js";
export class File extends Blob {
constructor(fileBits, name, options = {}) {
super(fileBits, options);
this.name = name;
this.lastModified = options.lastModified ?? Date.now();
}
get [Symbol.toStringTag]() {
return 'File';
}
get webkitRelativePath() {
throw new Error('Not implemented');
}
}
class NativeFile extends File {
#path;
constructor({
name,
path,
type,
size,
lastModified
}) {
super([], name, {
lastModified,
type
});
this.#path = path;
this._size = size;
}
stream() {
const nativeStream = StreamFactory.createInputStream(this.#path);
return toReadableStream(nativeStream);
}
get [Symbol.toStringTag]() {
return 'File';
}
}
class FileSystemFileHandle {
kind = 'file';
get name() {
return this.#metadata.name;
}
#metadata;
constructor(path) {
this.#metadata = FileSystem.getMetadata(path);
}
async getFile() {
return new NativeFile(this.#metadata);
}
async createWritable() {
throw new Error('Not implemented');
}
async isSameEntry(other) {
return other instanceof FileSystemFileHandle && this.#metadata.root === other.#metadata.root && this.#metadata.path === other.#metadata.path;
}
get [Symbol.toStringTag]() {
return 'FileSystemFileHandle';
}
}
export async function showOpenFilePicker(options = {}) {
const nativePickerOptions = {};
if (options.startIn) {
nativePickerOptions.startIn = FileSystem.getWellKnownDirectoryPath(options.startIn);
}
if (options.multiple) {
nativePickerOptions.multiple = options.multiple;
}
if (options.types) {
nativePickerOptions.extensions = options.types.reduce((acc, type) => {
if (!type.accept) {
return acc;
}
return acc.concat(...Object.values(type.accept));
}, []);
nativePickerOptions.mimeTypes = options.types.flatMap(type => Object.keys(type.accept || {}));
}
const paths = await FileSystem.showOpenFilePicker(nativePickerOptions);
return paths.map(path => new FileSystemFileHandle(path));
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export function showSaveFilePicker(options) {
throw new Error('Not implemented');
}
export function showDirectoryPicker(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
options) {
throw new Error('Not implemented');
}
//# sourceMappingURL=fs.js.map
;