@ayonli/jsext
Advanced tools
Comparing version 0.9.13 to 0.9.14
@@ -274,3 +274,3 @@ import bytes, { concat as concatBytes } from "../bytes.ts"; | ||
relativePath: string, | ||
data: string | ArrayBuffer | Uint8Array | DataView | Blob | ReadableStream<Uint8Array> | null, | ||
data: string | ArrayBuffer | ArrayBufferView | ReadableStream<Uint8Array> | Blob | null, | ||
info: Partial<Omit<TarEntry, "relativePath">>, | ||
@@ -316,9 +316,9 @@ ): TarEntryWithData { | ||
size = _data.byteLength; | ||
} else if (data instanceof ArrayBuffer) { | ||
body = toReadableStream([new Uint8Array(data)]); | ||
size = data.byteLength; | ||
} else if (data instanceof Uint8Array) { | ||
body = toReadableStream([data]); | ||
size = data.byteLength; | ||
} else if (data instanceof ArrayBuffer) { | ||
body = toReadableStream([new Uint8Array(data)]); | ||
size = data.byteLength; | ||
} else if (data instanceof DataView) { | ||
} else if (ArrayBuffer.isView(data)) { | ||
const _data = new Uint8Array(data.buffer, data.byteOffset, data.byteLength); | ||
@@ -339,3 +339,3 @@ body = toReadableStream([_data]); | ||
} else { | ||
throw new TypeError("data must be a string, Uint8Array, ArrayBuffer, DataView, Blob, or ReadableStream"); | ||
throw new TypeError("data must be a string, Uint8Array, ArrayBuffer, ArrayBufferView, Blob, or ReadableStream"); | ||
} | ||
@@ -406,7 +406,7 @@ | ||
append( | ||
data: string | ArrayBuffer | Uint8Array | DataView | Blob | ReadableStream<Uint8Array> | null, | ||
data: string | ArrayBuffer | ArrayBufferView | Blob | ReadableStream<Uint8Array> | null, | ||
info: Ensured<Partial<TarEntry>, "relativePath"> | ||
): void; | ||
append( | ||
data: string | ArrayBuffer | Uint8Array | DataView | Blob | ReadableStream<Uint8Array> | null, | ||
data: string | ArrayBuffer | ArrayBufferView | Blob | ReadableStream<Uint8Array> | null, | ||
info: Partial<TarEntry> = {} | ||
@@ -490,3 +490,3 @@ ): void { | ||
relativePath: string, | ||
data: string | ArrayBuffer | Uint8Array | DataView | Blob | File | ReadableStream<Uint8Array> | null, | ||
data: string | ArrayBuffer | ArrayBufferView | ReadableStream<Uint8Array> | Blob | null, | ||
info: Partial<Omit<TarEntry, "relativePath">> = {} | ||
@@ -493,0 +493,0 @@ ): boolean { |
@@ -208,5 +208,6 @@ import { concat as concatBytes } from "../bytes.ts"; | ||
} else if (totalBytes && totalWrittenBytes < totalBytes && options.onProgress) { | ||
totalWrittenBytes = totalBytes; | ||
options.onProgress(createProgressEvent( | ||
totalWrittenBytes, | ||
totalBytes, | ||
totalBytes, | ||
true | ||
@@ -213,0 +214,0 @@ )); |
54
bytes.ts
@@ -20,3 +20,43 @@ /** | ||
const defaultDecoder = new TextDecoder(); | ||
const base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; | ||
function base64(bytes: Uint8Array): string { | ||
let result = ""; | ||
let i: number; | ||
const l = bytes.length; | ||
for (i = 2; i < l; i += 3) { | ||
result += base64Chars[(bytes[i - 2]!) >> 2]; | ||
result += base64Chars[ | ||
(((bytes[i - 2]!) & 0x03) << 4) | | ||
((bytes[i - 1]!) >> 4) | ||
]; | ||
result += base64Chars[ | ||
(((bytes[i - 1]!) & 0x0f) << 2) | | ||
((bytes[i]!) >> 6) | ||
]; | ||
result += base64Chars[(bytes[i]!) & 0x3f]; | ||
} | ||
if (i === l + 1) { | ||
// 1 octet yet to write | ||
result += base64Chars[(bytes[i - 2]!) >> 2]; | ||
result += base64Chars[((bytes[i - 2]!) & 0x03) << 4]; | ||
result += "=="; | ||
} | ||
if (i === l) { | ||
// 2 octets yet to write | ||
result += base64Chars[(bytes[i - 2]!) >> 2]; | ||
result += base64Chars[ | ||
(((bytes[i - 2]!) & 0x03) << 4) | | ||
((bytes[i - 1]!) >> 4) | ||
]; | ||
result += base64Chars[((bytes[i - 1]!) & 0x0f) << 2]; | ||
result += "="; | ||
} | ||
return result; | ||
} | ||
/** | ||
@@ -55,5 +95,5 @@ * A byte array is a `Uint8Array` that can be coerced to a string with `utf8` | ||
export default function bytes(buf: ArrayBufferLike): ByteArray; | ||
export default function bytes(view: DataView): ByteArray; | ||
export default function bytes(view: ArrayBufferView): ByteArray; | ||
export default function bytes( | ||
data: number | string | ArrayLike<number> | ArrayBufferLike | DataView | ||
data: number | string | ArrayLike<number> | ArrayBufferLike | ArrayBufferView | ||
): ByteArray { | ||
@@ -64,4 +104,4 @@ if (typeof data === "number") { | ||
return new ByteArray(defaultEncoder.encode(data).buffer); | ||
} else if (data instanceof DataView) { | ||
return new ByteArray(data.buffer); | ||
} else if (ArrayBuffer.isView(data)) { | ||
return new ByteArray(data.buffer, data.byteOffset, data.byteLength); | ||
} else { | ||
@@ -92,3 +132,3 @@ return new ByteArray(data); | ||
} else { | ||
return btoa(defaultDecoder.decode(bytes)); | ||
return base64(bytes); | ||
} | ||
@@ -116,3 +156,5 @@ } else if (typeof Buffer === "function" && bytes instanceof Buffer) { | ||
const ctor = ((arrays[0] as T)?.constructor || Uint8Array) as Constructor<T>; | ||
const result = new ctor(length); | ||
const result = typeof Buffer === "function" && Object.is(ctor, Buffer) | ||
? Buffer.alloc(length) as unknown as T | ||
: new ctor(length); | ||
let offset = 0; | ||
@@ -119,0 +161,0 @@ |
@@ -208,2 +208,6 @@ 'use strict'; | ||
} | ||
else if (data instanceof ArrayBuffer) { | ||
body = reader.toReadableStream([new Uint8Array(data)]); | ||
size = data.byteLength; | ||
} | ||
else if (data instanceof Uint8Array) { | ||
@@ -213,7 +217,3 @@ body = reader.toReadableStream([data]); | ||
} | ||
else if (data instanceof ArrayBuffer) { | ||
body = reader.toReadableStream([new Uint8Array(data)]); | ||
size = data.byteLength; | ||
} | ||
else if (data instanceof DataView) { | ||
else if (ArrayBuffer.isView(data)) { | ||
const _data = new Uint8Array(data.buffer, data.byteOffset, data.byteLength); | ||
@@ -237,3 +237,3 @@ body = reader.toReadableStream([_data]); | ||
else { | ||
throw new TypeError("data must be a string, Uint8Array, ArrayBuffer, DataView, Blob, or ReadableStream"); | ||
throw new TypeError("data must be a string, Uint8Array, ArrayBuffer, ArrayBufferView, Blob, or ReadableStream"); | ||
} | ||
@@ -240,0 +240,0 @@ const kind = (_b = info.kind) !== null && _b !== void 0 ? _b : "file"; |
@@ -155,3 +155,4 @@ 'use strict'; | ||
else if (totalBytes && totalWrittenBytes < totalBytes && options.onProgress) { | ||
options.onProgress(createProgressEvent(totalBytes, totalBytes, true)); | ||
totalWrittenBytes = totalBytes; | ||
options.onProgress(createProgressEvent(totalWrittenBytes, totalBytes, true)); | ||
} | ||
@@ -158,0 +159,0 @@ } |
@@ -15,2 +15,31 @@ 'use strict'; | ||
const defaultDecoder = new TextDecoder(); | ||
const base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; | ||
function base64(bytes) { | ||
let result = ""; | ||
let i; | ||
const l = bytes.length; | ||
for (i = 2; i < l; i += 3) { | ||
result += base64Chars[(bytes[i - 2]) >> 2]; | ||
result += base64Chars[(((bytes[i - 2]) & 0x03) << 4) | | ||
((bytes[i - 1]) >> 4)]; | ||
result += base64Chars[(((bytes[i - 1]) & 0x0f) << 2) | | ||
((bytes[i]) >> 6)]; | ||
result += base64Chars[(bytes[i]) & 0x3f]; | ||
} | ||
if (i === l + 1) { | ||
// 1 octet yet to write | ||
result += base64Chars[(bytes[i - 2]) >> 2]; | ||
result += base64Chars[((bytes[i - 2]) & 0x03) << 4]; | ||
result += "=="; | ||
} | ||
if (i === l) { | ||
// 2 octets yet to write | ||
result += base64Chars[(bytes[i - 2]) >> 2]; | ||
result += base64Chars[(((bytes[i - 2]) & 0x03) << 4) | | ||
((bytes[i - 1]) >> 4)]; | ||
result += base64Chars[((bytes[i - 1]) & 0x0f) << 2]; | ||
result += "="; | ||
} | ||
return result; | ||
} | ||
/** | ||
@@ -38,4 +67,4 @@ * A byte array is a `Uint8Array` that can be coerced to a string with `utf8` | ||
} | ||
else if (data instanceof DataView) { | ||
return new ByteArray(data.buffer); | ||
else if (ArrayBuffer.isView(data)) { | ||
return new ByteArray(data.buffer, data.byteOffset, data.byteLength); | ||
} | ||
@@ -67,3 +96,3 @@ else { | ||
else { | ||
return btoa(defaultDecoder.decode(bytes)); | ||
return base64(bytes); | ||
} | ||
@@ -91,3 +120,5 @@ } | ||
const ctor = (((_a = arrays[0]) === null || _a === void 0 ? void 0 : _a.constructor) || Uint8Array); | ||
const result = new ctor(length); | ||
const result = typeof Buffer === "function" && Object.is(ctor, Buffer) | ||
? Buffer.alloc(length) | ||
: new ctor(length); | ||
let offset = 0; | ||
@@ -94,0 +125,0 @@ for (const arr of arrays) { |
@@ -109,4 +109,91 @@ 'use strict'; | ||
const { title = "", type = "", multiple = false, directory = false } = options; | ||
if (directory && typeof globalThis["showDirectoryPicker"] === "function") { | ||
if (directory) { | ||
return await openDirectory({ title }); | ||
} | ||
else if (multiple) { | ||
return await openFiles({ title, type }); | ||
} | ||
if (typeof globalThis["showOpenFilePicker"] === "function") { | ||
const handle = await dialog_terminal_file_browser.browserPickFile(type); | ||
return handle ? await handle.getFile().then(fs_util.fixFileType) : null; | ||
} | ||
else if (env.isBrowserWindow) { | ||
const input = document.createElement("input"); | ||
input.type = "file"; | ||
input.accept = type !== null && type !== void 0 ? type : ""; | ||
return await new Promise(resolve => { | ||
input.onchange = () => { | ||
var _a; | ||
const file = (_a = input.files) === null || _a === void 0 ? void 0 : _a[0]; | ||
resolve(file ? fs_util.fixFileType(file) : null); | ||
}; | ||
input.oncancel = () => { | ||
resolve(null); | ||
}; | ||
if (typeof input.showPicker === "function") { | ||
input.showPicker(); | ||
} | ||
else { | ||
input.click(); | ||
} | ||
}); | ||
} | ||
else if (env.isDeno || env.isNodeLike) { | ||
let filename = await pickFile({ title, type }); | ||
if (filename) { | ||
return await fs.readFileAsFile(filename); | ||
} | ||
else { | ||
return null; | ||
} | ||
} | ||
else { | ||
throw new Error("Unsupported runtime"); | ||
} | ||
} | ||
/** | ||
* Open the file picker dialog and pick multiple files to open. | ||
*/ | ||
async function openFiles(options = {}) { | ||
if (typeof globalThis["showOpenFilePicker"] === "function") { | ||
const handles = await dialog_terminal_file_browser.browserPickFiles(options.type); | ||
const files = []; | ||
for (const handle of handles) { | ||
const file = await handle.getFile(); | ||
files.push(fs_util.fixFileType(file)); | ||
} | ||
return files; | ||
} | ||
else if (env.isBrowserWindow) { | ||
const input = document.createElement("input"); | ||
input.type = "file"; | ||
input.multiple = true; | ||
input.accept = options.type || ""; | ||
return await new Promise(resolve => { | ||
input.onchange = () => { | ||
const files = input.files; | ||
resolve(files ? [...files].map(fs_util.fixFileType) : []); | ||
}; | ||
input.oncancel = () => { | ||
resolve([]); | ||
}; | ||
if (typeof input.showPicker === "function") { | ||
input.showPicker(); | ||
} | ||
else { | ||
input.click(); | ||
} | ||
}); | ||
} | ||
else if (env.isDeno || env.isNodeLike) { | ||
const filenames = await pickFiles(options); | ||
return await Promise.all(filenames.map(path => fs.readFileAsFile(path))); | ||
} | ||
else { | ||
throw new Error("Unsupported runtime"); | ||
} | ||
} | ||
async function openDirectory(options = {}) { | ||
if (typeof globalThis["showDirectoryPicker"] === "function") { | ||
const files = []; | ||
const dir = await dialog_terminal_file_browser.browserPickFolder(); | ||
@@ -130,41 +217,13 @@ if (!dir) { | ||
} | ||
else if (typeof globalThis["showOpenFilePicker"] === "function") { | ||
if (multiple) { | ||
const handles = await dialog_terminal_file_browser.browserPickFiles(type); | ||
const files = []; | ||
for (const handle of handles) { | ||
const file = await handle.getFile(); | ||
files.push(fs_util.fixFileType(file)); | ||
} | ||
return files; | ||
} | ||
else { | ||
const handle = await dialog_terminal_file_browser.browserPickFile(type); | ||
return handle ? await handle.getFile().then(fs_util.fixFileType) : null; | ||
} | ||
} | ||
else if (env.isBrowserWindow) { | ||
const input = document.createElement("input"); | ||
input.type = "file"; | ||
input.accept = type !== null && type !== void 0 ? type : ""; | ||
input.multiple = multiple !== null && multiple !== void 0 ? multiple : false; | ||
input.webkitdirectory = directory !== null && directory !== void 0 ? directory : false; | ||
input.webkitdirectory = true; | ||
return await new Promise(resolve => { | ||
input.onchange = () => { | ||
var _a; | ||
const files = input.files; | ||
if (directory || multiple) { | ||
resolve(files ? [...files] : []); | ||
} | ||
else { | ||
resolve(files ? ((_a = files[0]) !== null && _a !== void 0 ? _a : null) : null); | ||
} | ||
resolve(files ? [...files].map(fs_util.fixFileType) : []); | ||
}; | ||
input.oncancel = () => { | ||
if (directory || multiple) { | ||
resolve([]); | ||
} | ||
else { | ||
resolve(null); | ||
} | ||
resolve([]); | ||
}; | ||
@@ -180,14 +239,3 @@ if (typeof input.showPicker === "function") { | ||
else if (env.isDeno || env.isNodeLike) { | ||
let filename; | ||
let filenames; | ||
let dirname; | ||
if (directory) { | ||
dirname = await pickDirectory({ title }); | ||
} | ||
else if (multiple) { | ||
filenames = await pickFiles({ title, type }); | ||
} | ||
else { | ||
filename = await pickFile({ title, type }); | ||
} | ||
const dirname = await pickDirectory(options); | ||
if (dirname) { | ||
@@ -210,14 +258,5 @@ const files = []; | ||
} | ||
else if (filenames) { | ||
return await Promise.all(filenames.map(path => fs.readFileAsFile(path))); | ||
} | ||
else if (filename) { | ||
return await fs.readFileAsFile(filename); | ||
} | ||
else if (directory || multiple) { | ||
else { | ||
return []; | ||
} | ||
else { | ||
return null; | ||
} | ||
} | ||
@@ -352,3 +391,5 @@ else { | ||
exports.downloadFile = downloadFile; | ||
exports.openDirectory = openDirectory; | ||
exports.openFile = openFile; | ||
exports.openFiles = openFiles; | ||
exports.pickDirectory = pickDirectory; | ||
@@ -355,0 +396,0 @@ exports.pickFile = pickFile; |
@@ -612,6 +612,9 @@ 'use strict'; | ||
} | ||
else if (data instanceof DataView) { | ||
else if (data instanceof Uint8Array) { | ||
return await rawOp(Deno.writeFile(filename, data, options)); | ||
} | ||
else if (ArrayBuffer.isView(data)) { | ||
return await rawOp(Deno.writeFile(filename, bytes.default(data), options)); | ||
} | ||
else { | ||
else if (data) { | ||
return await rawOp(Deno.writeFile(filename, data, options)); | ||
@@ -637,6 +640,9 @@ } | ||
} | ||
else if (data instanceof DataView) { | ||
_data = bytes.default(data); // Bun may not support writing DataView | ||
else if (data instanceof Uint8Array) { | ||
_data = data; | ||
} | ||
else if (typeof data === "string" || "buffer" in data) { | ||
else if (ArrayBuffer.isView(data)) { | ||
_data = bytes.default(data); | ||
} | ||
else if (typeof data === "string") { | ||
_data = data; | ||
@@ -643,0 +649,0 @@ } |
@@ -65,6 +65,3 @@ 'use strict'; | ||
} | ||
else if (typeof Buffer === "function" && source instanceof Buffer) { | ||
return source.buffer.slice(0, source.length); | ||
} | ||
else if (source instanceof Uint8Array) { | ||
else if (ArrayBuffer.isView(source)) { | ||
return source.buffer; | ||
@@ -84,3 +81,3 @@ } | ||
async function readAsBlob(source, type) { | ||
if (source instanceof ArrayBuffer || source instanceof Uint8Array) { | ||
if (source instanceof ArrayBuffer || ArrayBuffer.isView(source)) { | ||
return new Blob([source], { type }); | ||
@@ -103,2 +100,7 @@ } | ||
} | ||
else if (ArrayBuffer.isView(source)) { | ||
const bytes$1 = new Uint8Array(source.buffer, source.byteOffset, source.byteLength); | ||
const base64 = bytes.text(bytes$1, "base64"); | ||
return `data:${type};base64,${base64}`; | ||
} | ||
const buffer = await readAsArrayBuffer(source); | ||
@@ -113,6 +115,6 @@ const _bytes = new Uint8Array(buffer); | ||
async function readAsObjectURL(source, type) { | ||
if (typeof Blob === "function" && source instanceof Blob) { | ||
if (source instanceof Blob) { | ||
return URL.createObjectURL(source); | ||
} | ||
else if (source instanceof ArrayBuffer || source instanceof Uint8Array) { | ||
else if (source instanceof ArrayBuffer || ArrayBuffer.isView(source)) { | ||
const blob = new Blob([source], { type }); | ||
@@ -132,3 +134,3 @@ return URL.createObjectURL(blob); | ||
} | ||
else if (source instanceof ArrayBuffer || source instanceof Uint8Array) { | ||
else if (source instanceof ArrayBuffer || ArrayBuffer.isView(source)) { | ||
return new TextDecoder(encoding).decode(source); | ||
@@ -135,0 +137,0 @@ } |
@@ -143,3 +143,5 @@ import { isBrowserWindow, isDeno, isNodeLike } from "../env.ts"; | ||
/** Open the file picker dialog and pick a file to open. */ | ||
/** | ||
* Open the file picker dialog and pick a file to open. | ||
*/ | ||
export function openFile(options?: { | ||
@@ -154,3 +156,7 @@ /** Custom the dialog's title. This option is ignored in the browser. */ | ||
}): Promise<File | null>; | ||
/** Open the file picker dialog and pick multiple files to open. */ | ||
/** | ||
* Open the file picker dialog and pick multiple files to open. | ||
* | ||
* @deprecated use {@link openFiles} instead. | ||
*/ | ||
export function openFile(options: { | ||
@@ -161,3 +167,7 @@ title?: string; | ||
}): Promise<File[]>; | ||
/** Open the file picker dialog and pick a directory to open. */ | ||
/** | ||
* Open the file picker dialog and pick a directory to open. | ||
* | ||
* @deprecated use {@link openDirectory} instead. | ||
*/ | ||
export function openFile(options: { | ||
@@ -175,4 +185,101 @@ title?: string; | ||
if (directory && typeof (globalThis as any)["showDirectoryPicker"] === "function") { | ||
if (directory) { | ||
return await openDirectory({ title }); | ||
} else if (multiple) { | ||
return await openFiles({ title, type }); | ||
} | ||
if (typeof (globalThis as any)["showOpenFilePicker"] === "function") { | ||
const handle = await browserPickFile(type); | ||
return handle ? await handle.getFile().then(fixFileType) : null; | ||
} else if (isBrowserWindow) { | ||
const input = document.createElement("input"); | ||
input.type = "file"; | ||
input.accept = type ?? ""; | ||
return await new Promise<File | File[] | null>(resolve => { | ||
input.onchange = () => { | ||
const file = input.files?.[0]; | ||
resolve(file ? fixFileType(file) : null); | ||
}; | ||
input.oncancel = () => { | ||
resolve(null); | ||
}; | ||
if (typeof input.showPicker === "function") { | ||
input.showPicker(); | ||
} else { | ||
input.click(); | ||
} | ||
}); | ||
} else if (isDeno || isNodeLike) { | ||
let filename = await pickFile({ title, type }) as string | null; | ||
if (filename) { | ||
return await readFileAsFile(filename); | ||
} else { | ||
return null; | ||
} | ||
} else { | ||
throw new Error("Unsupported runtime"); | ||
} | ||
} | ||
/** | ||
* Open the file picker dialog and pick multiple files to open. | ||
*/ | ||
export async function openFiles(options: { | ||
/** Custom the dialog's title. This option is ignored in the browser. */ | ||
title?: string; | ||
/** | ||
* Filter files by providing a MIME type or suffix, multiple types can be | ||
* separated via `,`. | ||
*/ | ||
type?: string; | ||
} = {}): Promise<File[]> { | ||
if (typeof (globalThis as any)["showOpenFilePicker"] === "function") { | ||
const handles = await browserPickFiles(options.type); | ||
const files: File[] = []; | ||
for (const handle of handles) { | ||
const file = await handle.getFile(); | ||
files.push(fixFileType(file)); | ||
} | ||
return files; | ||
} else if (isBrowserWindow) { | ||
const input = document.createElement("input"); | ||
input.type = "file"; | ||
input.multiple = true; | ||
input.accept = options.type || ""; | ||
return await new Promise<File[]>(resolve => { | ||
input.onchange = () => { | ||
const files = input.files; | ||
resolve(files ? [...files].map(fixFileType) : []); | ||
}; | ||
input.oncancel = () => { | ||
resolve([]); | ||
}; | ||
if (typeof input.showPicker === "function") { | ||
input.showPicker(); | ||
} else { | ||
input.click(); | ||
} | ||
}); | ||
} else if (isDeno || isNodeLike) { | ||
const filenames = await pickFiles(options) as string[]; | ||
return await Promise.all(filenames.map(path => readFileAsFile(path))); | ||
} else { | ||
throw new Error("Unsupported runtime"); | ||
} | ||
} | ||
export async function openDirectory(options: { | ||
/** Custom the dialog's title. This option is ignored in the browser. */ | ||
title?: string; | ||
} = {}): Promise<File[]> { | ||
if (typeof (globalThis as any)["showDirectoryPicker"] === "function") { | ||
const files: File[] = []; | ||
const dir = await browserPickFolder(); | ||
@@ -200,40 +307,14 @@ | ||
return files; | ||
} else if (typeof (globalThis as any)["showOpenFilePicker"] === "function") { | ||
if (multiple) { | ||
const handles = await browserPickFiles(type); | ||
const files: File[] = []; | ||
for (const handle of handles) { | ||
const file = await handle.getFile(); | ||
files.push(fixFileType(file)); | ||
} | ||
return files; | ||
} else { | ||
const handle = await browserPickFile(type); | ||
return handle ? await handle.getFile().then(fixFileType) : null; | ||
} | ||
} else if (isBrowserWindow) { | ||
const input = document.createElement("input"); | ||
input.type = "file"; | ||
input.accept = type ?? ""; | ||
input.multiple = multiple ?? false; | ||
input.webkitdirectory = directory ?? false; | ||
input.webkitdirectory = true; | ||
return await new Promise<File | File[] | null>(resolve => { | ||
return await new Promise<File[]>(resolve => { | ||
input.onchange = () => { | ||
const files = input.files; | ||
if (directory || multiple) { | ||
resolve(files ? [...files] : []); | ||
} else { | ||
resolve(files ? (files[0] ?? null) : null); | ||
} | ||
resolve(files ? [...files].map(fixFileType) : []); | ||
}; | ||
input.oncancel = () => { | ||
if (directory || multiple) { | ||
resolve([]); | ||
} else { | ||
resolve(null); | ||
} | ||
resolve([]); | ||
}; | ||
@@ -248,14 +329,4 @@ | ||
} else if (isDeno || isNodeLike) { | ||
let filename: string | null | undefined; | ||
let filenames: string[] | null | undefined; | ||
let dirname: string | null | undefined; | ||
const dirname = await pickDirectory(options) as string | null; | ||
if (directory) { | ||
dirname = await pickDirectory({ title }) as string | null; | ||
} else if (multiple) { | ||
filenames = await pickFiles({ title, type }) as string[]; | ||
} else { | ||
filename = await pickFile({ title, type }) as string | null; | ||
} | ||
if (dirname) { | ||
@@ -281,10 +352,4 @@ const files: File[] = []; | ||
return files; | ||
} else if (filenames) { | ||
return await Promise.all(filenames.map(path => readFileAsFile(path))); | ||
} else if (filename) { | ||
return await readFileAsFile(filename); | ||
} else if (directory || multiple) { | ||
} else { | ||
return []; | ||
} else { | ||
return null; | ||
} | ||
@@ -308,3 +373,3 @@ } else { | ||
export async function saveFile( | ||
file: Blob | DataView | ReadableStream<Uint8Array> | Uint8Array | ArrayBuffer, | ||
file: Blob | ArrayBuffer | ArrayBufferView | ReadableStream<Uint8Array>, | ||
options: { | ||
@@ -320,3 +385,3 @@ /** The suggested name of the file. */ | ||
export async function saveFile( | ||
file: File | Blob | DataView | ReadableStream<Uint8Array> | Uint8Array | ArrayBuffer, | ||
file: File | Blob | ArrayBuffer | ArrayBufferView | ReadableStream<Uint8Array>, | ||
options: { | ||
@@ -323,0 +388,0 @@ title?: string; |
@@ -204,2 +204,6 @@ import bytes, { concat as concat$1 } from '../bytes.js'; | ||
} | ||
else if (data instanceof ArrayBuffer) { | ||
body = toReadableStream([new Uint8Array(data)]); | ||
size = data.byteLength; | ||
} | ||
else if (data instanceof Uint8Array) { | ||
@@ -209,7 +213,3 @@ body = toReadableStream([data]); | ||
} | ||
else if (data instanceof ArrayBuffer) { | ||
body = toReadableStream([new Uint8Array(data)]); | ||
size = data.byteLength; | ||
} | ||
else if (data instanceof DataView) { | ||
else if (ArrayBuffer.isView(data)) { | ||
const _data = new Uint8Array(data.buffer, data.byteOffset, data.byteLength); | ||
@@ -233,3 +233,3 @@ body = toReadableStream([_data]); | ||
else { | ||
throw new TypeError("data must be a string, Uint8Array, ArrayBuffer, DataView, Blob, or ReadableStream"); | ||
throw new TypeError("data must be a string, Uint8Array, ArrayBuffer, ArrayBufferView, Blob, or ReadableStream"); | ||
} | ||
@@ -236,0 +236,0 @@ const kind = (_b = info.kind) !== null && _b !== void 0 ? _b : "file"; |
@@ -151,3 +151,4 @@ import { concat } from '../bytes.js'; | ||
else if (totalBytes && totalWrittenBytes < totalBytes && options.onProgress) { | ||
options.onProgress(createProgressEvent(totalBytes, totalBytes, true)); | ||
totalWrittenBytes = totalBytes; | ||
options.onProgress(createProgressEvent(totalWrittenBytes, totalBytes, true)); | ||
} | ||
@@ -154,0 +155,0 @@ } |
@@ -11,2 +11,31 @@ import { equals as equals$1, includeSlice, startsWith as startsWith$1, endsWith as endsWith$1, split as split$1, chunk as chunk$1 } from './array/base.js'; | ||
const defaultDecoder = new TextDecoder(); | ||
const base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; | ||
function base64(bytes) { | ||
let result = ""; | ||
let i; | ||
const l = bytes.length; | ||
for (i = 2; i < l; i += 3) { | ||
result += base64Chars[(bytes[i - 2]) >> 2]; | ||
result += base64Chars[(((bytes[i - 2]) & 0x03) << 4) | | ||
((bytes[i - 1]) >> 4)]; | ||
result += base64Chars[(((bytes[i - 1]) & 0x0f) << 2) | | ||
((bytes[i]) >> 6)]; | ||
result += base64Chars[(bytes[i]) & 0x3f]; | ||
} | ||
if (i === l + 1) { | ||
// 1 octet yet to write | ||
result += base64Chars[(bytes[i - 2]) >> 2]; | ||
result += base64Chars[((bytes[i - 2]) & 0x03) << 4]; | ||
result += "=="; | ||
} | ||
if (i === l) { | ||
// 2 octets yet to write | ||
result += base64Chars[(bytes[i - 2]) >> 2]; | ||
result += base64Chars[(((bytes[i - 2]) & 0x03) << 4) | | ||
((bytes[i - 1]) >> 4)]; | ||
result += base64Chars[((bytes[i - 1]) & 0x0f) << 2]; | ||
result += "="; | ||
} | ||
return result; | ||
} | ||
/** | ||
@@ -34,4 +63,4 @@ * A byte array is a `Uint8Array` that can be coerced to a string with `utf8` | ||
} | ||
else if (data instanceof DataView) { | ||
return new ByteArray(data.buffer); | ||
else if (ArrayBuffer.isView(data)) { | ||
return new ByteArray(data.buffer, data.byteOffset, data.byteLength); | ||
} | ||
@@ -63,3 +92,3 @@ else { | ||
else { | ||
return btoa(defaultDecoder.decode(bytes)); | ||
return base64(bytes); | ||
} | ||
@@ -87,3 +116,5 @@ } | ||
const ctor = (((_a = arrays[0]) === null || _a === void 0 ? void 0 : _a.constructor) || Uint8Array); | ||
const result = new ctor(length); | ||
const result = typeof Buffer === "function" && Object.is(ctor, Buffer) | ||
? Buffer.alloc(length) | ||
: new ctor(length); | ||
let offset = 0; | ||
@@ -90,0 +121,0 @@ for (const arr of arrays) { |
@@ -12,3 +12,3 @@ import { isDeno, isNodeLike, isBrowserWindow } from '../env.js'; | ||
import { getExtensions } from '../filetype.js'; | ||
import { readDir, readFileAsFile, writeFile } from '../fs.js'; | ||
import { readFileAsFile, readDir, writeFile } from '../fs.js'; | ||
import { fixFileType } from '../fs/util.js'; | ||
@@ -108,4 +108,91 @@ import { as } from '../object.js'; | ||
const { title = "", type = "", multiple = false, directory = false } = options; | ||
if (directory && typeof globalThis["showDirectoryPicker"] === "function") { | ||
if (directory) { | ||
return await openDirectory({ title }); | ||
} | ||
else if (multiple) { | ||
return await openFiles({ title, type }); | ||
} | ||
if (typeof globalThis["showOpenFilePicker"] === "function") { | ||
const handle = await browserPickFile(type); | ||
return handle ? await handle.getFile().then(fixFileType) : null; | ||
} | ||
else if (isBrowserWindow) { | ||
const input = document.createElement("input"); | ||
input.type = "file"; | ||
input.accept = type !== null && type !== void 0 ? type : ""; | ||
return await new Promise(resolve => { | ||
input.onchange = () => { | ||
var _a; | ||
const file = (_a = input.files) === null || _a === void 0 ? void 0 : _a[0]; | ||
resolve(file ? fixFileType(file) : null); | ||
}; | ||
input.oncancel = () => { | ||
resolve(null); | ||
}; | ||
if (typeof input.showPicker === "function") { | ||
input.showPicker(); | ||
} | ||
else { | ||
input.click(); | ||
} | ||
}); | ||
} | ||
else if (isDeno || isNodeLike) { | ||
let filename = await pickFile({ title, type }); | ||
if (filename) { | ||
return await readFileAsFile(filename); | ||
} | ||
else { | ||
return null; | ||
} | ||
} | ||
else { | ||
throw new Error("Unsupported runtime"); | ||
} | ||
} | ||
/** | ||
* Open the file picker dialog and pick multiple files to open. | ||
*/ | ||
async function openFiles(options = {}) { | ||
if (typeof globalThis["showOpenFilePicker"] === "function") { | ||
const handles = await browserPickFiles(options.type); | ||
const files = []; | ||
for (const handle of handles) { | ||
const file = await handle.getFile(); | ||
files.push(fixFileType(file)); | ||
} | ||
return files; | ||
} | ||
else if (isBrowserWindow) { | ||
const input = document.createElement("input"); | ||
input.type = "file"; | ||
input.multiple = true; | ||
input.accept = options.type || ""; | ||
return await new Promise(resolve => { | ||
input.onchange = () => { | ||
const files = input.files; | ||
resolve(files ? [...files].map(fixFileType) : []); | ||
}; | ||
input.oncancel = () => { | ||
resolve([]); | ||
}; | ||
if (typeof input.showPicker === "function") { | ||
input.showPicker(); | ||
} | ||
else { | ||
input.click(); | ||
} | ||
}); | ||
} | ||
else if (isDeno || isNodeLike) { | ||
const filenames = await pickFiles(options); | ||
return await Promise.all(filenames.map(path => readFileAsFile(path))); | ||
} | ||
else { | ||
throw new Error("Unsupported runtime"); | ||
} | ||
} | ||
async function openDirectory(options = {}) { | ||
if (typeof globalThis["showDirectoryPicker"] === "function") { | ||
const files = []; | ||
const dir = await browserPickFolder(); | ||
@@ -129,41 +216,13 @@ if (!dir) { | ||
} | ||
else if (typeof globalThis["showOpenFilePicker"] === "function") { | ||
if (multiple) { | ||
const handles = await browserPickFiles(type); | ||
const files = []; | ||
for (const handle of handles) { | ||
const file = await handle.getFile(); | ||
files.push(fixFileType(file)); | ||
} | ||
return files; | ||
} | ||
else { | ||
const handle = await browserPickFile(type); | ||
return handle ? await handle.getFile().then(fixFileType) : null; | ||
} | ||
} | ||
else if (isBrowserWindow) { | ||
const input = document.createElement("input"); | ||
input.type = "file"; | ||
input.accept = type !== null && type !== void 0 ? type : ""; | ||
input.multiple = multiple !== null && multiple !== void 0 ? multiple : false; | ||
input.webkitdirectory = directory !== null && directory !== void 0 ? directory : false; | ||
input.webkitdirectory = true; | ||
return await new Promise(resolve => { | ||
input.onchange = () => { | ||
var _a; | ||
const files = input.files; | ||
if (directory || multiple) { | ||
resolve(files ? [...files] : []); | ||
} | ||
else { | ||
resolve(files ? ((_a = files[0]) !== null && _a !== void 0 ? _a : null) : null); | ||
} | ||
resolve(files ? [...files].map(fixFileType) : []); | ||
}; | ||
input.oncancel = () => { | ||
if (directory || multiple) { | ||
resolve([]); | ||
} | ||
else { | ||
resolve(null); | ||
} | ||
resolve([]); | ||
}; | ||
@@ -179,14 +238,3 @@ if (typeof input.showPicker === "function") { | ||
else if (isDeno || isNodeLike) { | ||
let filename; | ||
let filenames; | ||
let dirname; | ||
if (directory) { | ||
dirname = await pickDirectory({ title }); | ||
} | ||
else if (multiple) { | ||
filenames = await pickFiles({ title, type }); | ||
} | ||
else { | ||
filename = await pickFile({ title, type }); | ||
} | ||
const dirname = await pickDirectory(options); | ||
if (dirname) { | ||
@@ -209,14 +257,5 @@ const files = []; | ||
} | ||
else if (filenames) { | ||
return await Promise.all(filenames.map(path => readFileAsFile(path))); | ||
} | ||
else if (filename) { | ||
return await readFileAsFile(filename); | ||
} | ||
else if (directory || multiple) { | ||
else { | ||
return []; | ||
} | ||
else { | ||
return null; | ||
} | ||
} | ||
@@ -350,3 +389,3 @@ else { | ||
export { downloadFile, openFile, pickDirectory, pickFile, pickFiles, saveFile }; | ||
export { downloadFile, openDirectory, openFile, openFiles, pickDirectory, pickFile, pickFiles, saveFile }; | ||
//# sourceMappingURL=file.js.map |
@@ -610,6 +610,9 @@ import { abortable } from './async.js'; | ||
} | ||
else if (data instanceof DataView) { | ||
else if (data instanceof Uint8Array) { | ||
return await rawOp(Deno.writeFile(filename, data, options)); | ||
} | ||
else if (ArrayBuffer.isView(data)) { | ||
return await rawOp(Deno.writeFile(filename, bytes(data), options)); | ||
} | ||
else { | ||
else if (data) { | ||
return await rawOp(Deno.writeFile(filename, data, options)); | ||
@@ -635,6 +638,9 @@ } | ||
} | ||
else if (data instanceof DataView) { | ||
_data = bytes(data); // Bun may not support writing DataView | ||
else if (data instanceof Uint8Array) { | ||
_data = data; | ||
} | ||
else if (typeof data === "string" || "buffer" in data) { | ||
else if (ArrayBuffer.isView(data)) { | ||
_data = bytes(data); | ||
} | ||
else if (typeof data === "string") { | ||
_data = data; | ||
@@ -641,0 +647,0 @@ } |
@@ -64,6 +64,3 @@ import { concat as concat$1, text } from './bytes.js'; | ||
} | ||
else if (typeof Buffer === "function" && source instanceof Buffer) { | ||
return source.buffer.slice(0, source.length); | ||
} | ||
else if (source instanceof Uint8Array) { | ||
else if (ArrayBuffer.isView(source)) { | ||
return source.buffer; | ||
@@ -83,3 +80,3 @@ } | ||
async function readAsBlob(source, type) { | ||
if (source instanceof ArrayBuffer || source instanceof Uint8Array) { | ||
if (source instanceof ArrayBuffer || ArrayBuffer.isView(source)) { | ||
return new Blob([source], { type }); | ||
@@ -102,2 +99,7 @@ } | ||
} | ||
else if (ArrayBuffer.isView(source)) { | ||
const bytes = new Uint8Array(source.buffer, source.byteOffset, source.byteLength); | ||
const base64 = text(bytes, "base64"); | ||
return `data:${type};base64,${base64}`; | ||
} | ||
const buffer = await readAsArrayBuffer(source); | ||
@@ -112,6 +114,6 @@ const _bytes = new Uint8Array(buffer); | ||
async function readAsObjectURL(source, type) { | ||
if (typeof Blob === "function" && source instanceof Blob) { | ||
if (source instanceof Blob) { | ||
return URL.createObjectURL(source); | ||
} | ||
else if (source instanceof ArrayBuffer || source instanceof Uint8Array) { | ||
else if (source instanceof ArrayBuffer || ArrayBuffer.isView(source)) { | ||
const blob = new Blob([source], { type }); | ||
@@ -131,3 +133,3 @@ return URL.createObjectURL(blob); | ||
} | ||
else if (source instanceof ArrayBuffer || source instanceof Uint8Array) { | ||
else if (source instanceof ArrayBuffer || ArrayBuffer.isView(source)) { | ||
return new TextDecoder(encoding).decode(source); | ||
@@ -134,0 +136,0 @@ } |
@@ -16,2 +16,8 @@ async function alert(message, options = {}) { | ||
} | ||
async function openFiles(options) { | ||
throw new Error("Unsupported runtime"); | ||
} | ||
async function openDirectory(options) { | ||
throw new Error("Unsupported runtime"); | ||
} | ||
async function pickFile(options = {}) { | ||
@@ -30,3 +36,3 @@ throw new Error("Unsupported runtime"); | ||
export { alert, confirm, openFile, pickDirectory, pickFile, pickFiles, progress, prompt, saveFile }; | ||
export { alert, confirm, openDirectory, openFile, openFiles, pickDirectory, pickFile, pickFiles, progress, prompt, saveFile }; | ||
//# sourceMappingURL=dialog.js.map |
18
fs.ts
@@ -694,3 +694,3 @@ /** | ||
target: string | FileSystemFileHandle, | ||
data: string | ArrayBuffer | Uint8Array | DataView | ReadableStream<Uint8Array> | Blob | File, | ||
data: string | ArrayBuffer | ArrayBufferView | ReadableStream<Uint8Array> | Blob, | ||
options: FileSystemOptions & { | ||
@@ -724,5 +724,7 @@ /** | ||
return await rawOp(Deno.writeFile(filename, new Uint8Array(data), options)); | ||
} else if (data instanceof DataView) { | ||
} else if (data instanceof Uint8Array) { | ||
return await rawOp(Deno.writeFile(filename, data, options)); | ||
} else if (ArrayBuffer.isView(data)) { | ||
return await rawOp(Deno.writeFile(filename, bytes(data), options)); | ||
} else { | ||
} else if (data) { | ||
return await rawOp(Deno.writeFile(filename, data, options)); | ||
@@ -745,6 +747,8 @@ } | ||
_data = new Uint8Array(data); | ||
} else if (data instanceof DataView) { | ||
_data = bytes(data); // Bun may not support writing DataView | ||
} else if (typeof data === "string" || "buffer" in data) { | ||
} else if (data instanceof Uint8Array) { | ||
_data = data; | ||
} else if (ArrayBuffer.isView(data)) { | ||
_data = bytes(data); | ||
} else if (typeof data === "string") { | ||
_data = data; | ||
} else { | ||
@@ -767,3 +771,3 @@ throw new TypeError("Unsupported data type"); | ||
handle: FileSystemFileHandle, | ||
data: string | ArrayBuffer | Uint8Array | DataView | ReadableStream<Uint8Array> | Blob | File, | ||
data: string | ArrayBuffer | ArrayBufferView | ReadableStream<Uint8Array> | Blob, | ||
options: { | ||
@@ -770,0 +774,0 @@ append?: boolean; |
{ | ||
"name": "@ayonli/jsext", | ||
"version": "0.9.13", | ||
"version": "0.9.14", | ||
"description": "Additional functions for JavaScript to build strong applications.", | ||
@@ -166,2 +166,10 @@ "exports": { | ||
}, | ||
"./hash": { | ||
"types": "./types/hash.d.ts", | ||
"bun": "./hash.ts", | ||
"workerd": "./esm/workerd/hash.js", | ||
"require": "./cjs/hash.js", | ||
"import": "./esm/hash.js", | ||
"default": "./hash.ts" | ||
}, | ||
"./json": { | ||
@@ -168,0 +176,0 @@ "types": "./types/json.d.ts", |
@@ -99,10 +99,8 @@ /** | ||
export async function readAsArrayBuffer( | ||
source: Blob | Uint8Array | AsyncIterable<Uint8Array> | ReadableStream<Uint8Array> | ||
source: Blob | ArrayBufferView | AsyncIterable<Uint8Array> | ReadableStream<Uint8Array> | ||
): Promise<ArrayBuffer> { | ||
if (typeof Blob === "function" && source instanceof Blob) { | ||
return await source.arrayBuffer(); | ||
} else if (typeof Buffer === "function" && source instanceof Buffer) { | ||
return source.buffer.slice(0, source.length); | ||
} else if (source instanceof Uint8Array) { | ||
return source.buffer as ArrayBuffer; | ||
} else if (ArrayBuffer.isView(source)) { | ||
return source.buffer; | ||
} | ||
@@ -126,6 +124,6 @@ | ||
export async function readAsBlob( | ||
source: ArrayBuffer | Uint8Array | AsyncIterable<Uint8Array> | ReadableStream<Uint8Array>, | ||
source: ArrayBuffer | ArrayBufferView | AsyncIterable<Uint8Array> | ReadableStream<Uint8Array>, | ||
type: string | ||
): Promise<Blob> { | ||
if (source instanceof ArrayBuffer || source instanceof Uint8Array) { | ||
if (source instanceof ArrayBuffer || ArrayBuffer.isView(source)) { | ||
return new Blob([source], { type }); | ||
@@ -142,3 +140,3 @@ } | ||
export async function readAsDataURL( | ||
source: Blob | ArrayBuffer | Uint8Array | AsyncIterable<Uint8Array> | ReadableStream<Uint8Array>, | ||
source: Blob | ArrayBuffer | ArrayBufferView | AsyncIterable<Uint8Array> | ReadableStream<Uint8Array>, | ||
type: string | ||
@@ -152,2 +150,6 @@ ): Promise<string> { | ||
return `data:${type};base64,${base64}`; | ||
} else if (ArrayBuffer.isView(source)) { | ||
const bytes = new Uint8Array(source.buffer, source.byteOffset, source.byteLength); | ||
const base64 = text(bytes, "base64"); | ||
return `data:${type};base64,${base64}`; | ||
} | ||
@@ -165,8 +167,8 @@ | ||
export async function readAsObjectURL( | ||
source: Blob | ArrayBuffer | Uint8Array | AsyncIterable<Uint8Array> | ReadableStream<Uint8Array>, | ||
source: Blob | ArrayBuffer | ArrayBufferView | AsyncIterable<Uint8Array> | ReadableStream<Uint8Array>, | ||
type: string | ||
): Promise<string> { | ||
if (typeof Blob === "function" && source instanceof Blob) { | ||
if (source instanceof Blob) { | ||
return URL.createObjectURL(source); | ||
} else if (source instanceof ArrayBuffer || source instanceof Uint8Array) { | ||
} else if (source instanceof ArrayBuffer || ArrayBuffer.isView(source)) { | ||
const blob = new Blob([source], { type }); | ||
@@ -185,3 +187,3 @@ return URL.createObjectURL(blob); | ||
export async function readAsText( | ||
source: Blob | ArrayBuffer | Uint8Array | AsyncIterable<Uint8Array> | ReadableStream<Uint8Array>, | ||
source: Blob | ArrayBuffer | ArrayBufferView | AsyncIterable<Uint8Array> | ReadableStream<Uint8Array>, | ||
encoding: string | undefined = undefined | ||
@@ -191,3 +193,3 @@ ): Promise<string> { | ||
return await source.text(); | ||
} else if (source instanceof ArrayBuffer || source instanceof Uint8Array) { | ||
} else if (source instanceof ArrayBuffer || ArrayBuffer.isView(source)) { | ||
return new TextDecoder(encoding).decode(source); | ||
@@ -204,3 +206,3 @@ } | ||
export async function readAsJSON<T>( | ||
source: Blob | Uint8Array | AsyncIterable<Uint8Array> | ReadableStream<Uint8Array>, | ||
source: Blob | ArrayBuffer | ArrayBufferView | AsyncIterable<Uint8Array> | ReadableStream<Uint8Array>, | ||
): Promise<T> { | ||
@@ -207,0 +209,0 @@ const text = await readAsText(source); |
@@ -92,2 +92,4 @@ # JsExt | ||
system APIs for both server and browser applications. | ||
- [hash](https://jsr.io/@ayonli/jsext/doc/hash/~) Simplified hash functions for | ||
various data types. | ||
- [json](https://jsr.io/@ayonli/jsext/doc/json/~) Functions for parsing JSONs to | ||
@@ -94,0 +96,0 @@ specific structures. |
@@ -124,3 +124,3 @@ import { Ensured } from "../types.ts"; | ||
append(data: File): void; | ||
append(data: string | ArrayBuffer | Uint8Array | DataView | Blob | ReadableStream<Uint8Array> | null, info: Ensured<Partial<TarEntry>, "relativePath">): void; | ||
append(data: string | ArrayBuffer | ArrayBufferView | Blob | ReadableStream<Uint8Array> | null, info: Ensured<Partial<TarEntry>, "relativePath">): void; | ||
/** | ||
@@ -144,3 +144,3 @@ * Retrieves an entry in the archive by its relative path. | ||
*/ | ||
replace(relativePath: string, data: string | ArrayBuffer | Uint8Array | DataView | Blob | File | ReadableStream<Uint8Array> | null, info?: Partial<Omit<TarEntry, "relativePath">>): boolean; | ||
replace(relativePath: string, data: string | ArrayBuffer | ArrayBufferView | ReadableStream<Uint8Array> | Blob | null, info?: Partial<Omit<TarEntry, "relativePath">>): boolean; | ||
[Symbol.iterator](): IterableIterator<TarEntry>; | ||
@@ -147,0 +147,0 @@ /** |
@@ -32,3 +32,3 @@ /** | ||
export default function bytes(buf: ArrayBufferLike): ByteArray; | ||
export default function bytes(view: DataView): ByteArray; | ||
export default function bytes(view: ArrayBufferView): ByteArray; | ||
/** | ||
@@ -35,0 +35,0 @@ * Converts the byte array (or `Uint8Array`) to a string. |
@@ -45,3 +45,5 @@ /** | ||
}): Promise<string | FileSystemDirectoryHandle | null>; | ||
/** Open the file picker dialog and pick a file to open. */ | ||
/** | ||
* Open the file picker dialog and pick a file to open. | ||
*/ | ||
export declare function openFile(options?: { | ||
@@ -56,3 +58,7 @@ /** Custom the dialog's title. This option is ignored in the browser. */ | ||
}): Promise<File | null>; | ||
/** Open the file picker dialog and pick multiple files to open. */ | ||
/** | ||
* Open the file picker dialog and pick multiple files to open. | ||
* | ||
* @deprecated use {@link openFiles} instead. | ||
*/ | ||
export declare function openFile(options: { | ||
@@ -63,3 +69,7 @@ title?: string; | ||
}): Promise<File[]>; | ||
/** Open the file picker dialog and pick a directory to open. */ | ||
/** | ||
* Open the file picker dialog and pick a directory to open. | ||
* | ||
* @deprecated use {@link openDirectory} instead. | ||
*/ | ||
export declare function openFile(options: { | ||
@@ -70,2 +80,18 @@ title?: string; | ||
/** | ||
* Open the file picker dialog and pick multiple files to open. | ||
*/ | ||
export declare function openFiles(options?: { | ||
/** Custom the dialog's title. This option is ignored in the browser. */ | ||
title?: string; | ||
/** | ||
* Filter files by providing a MIME type or suffix, multiple types can be | ||
* separated via `,`. | ||
*/ | ||
type?: string; | ||
}): Promise<File[]>; | ||
export declare function openDirectory(options?: { | ||
/** Custom the dialog's title. This option is ignored in the browser. */ | ||
title?: string; | ||
}): Promise<File[]>; | ||
/** | ||
* Save a file to the file system. | ||
@@ -81,3 +107,3 @@ * | ||
}): Promise<void>; | ||
export declare function saveFile(file: Blob | DataView | ReadableStream<Uint8Array> | Uint8Array | ArrayBuffer, options: { | ||
export declare function saveFile(file: Blob | ArrayBuffer | ArrayBufferView | ReadableStream<Uint8Array>, options: { | ||
/** The suggested name of the file. */ | ||
@@ -84,0 +110,0 @@ name?: string; |
@@ -164,3 +164,3 @@ /** | ||
*/ | ||
export declare function writeFile(target: string | FileSystemFileHandle, data: string | ArrayBuffer | Uint8Array | DataView | ReadableStream<Uint8Array> | Blob | File, options?: FileSystemOptions & { | ||
export declare function writeFile(target: string | FileSystemFileHandle, data: string | ArrayBuffer | ArrayBufferView | ReadableStream<Uint8Array> | Blob, options?: FileSystemOptions & { | ||
/** | ||
@@ -167,0 +167,0 @@ * Append the data to the file instead of overwriting it. |
@@ -44,23 +44,23 @@ /// <reference types="mocha" /> | ||
*/ | ||
export declare function readAsArrayBuffer(source: Blob | Uint8Array | AsyncIterable<Uint8Array> | ReadableStream<Uint8Array>): Promise<ArrayBuffer>; | ||
export declare function readAsArrayBuffer(source: Blob | ArrayBufferView | AsyncIterable<Uint8Array> | ReadableStream<Uint8Array>): Promise<ArrayBuffer>; | ||
/** | ||
* Reads all data from the given source to a `Blob`. | ||
*/ | ||
export declare function readAsBlob(source: ArrayBuffer | Uint8Array | AsyncIterable<Uint8Array> | ReadableStream<Uint8Array>, type: string): Promise<Blob>; | ||
export declare function readAsBlob(source: ArrayBuffer | ArrayBufferView | AsyncIterable<Uint8Array> | ReadableStream<Uint8Array>, type: string): Promise<Blob>; | ||
/** | ||
* Reads all data from the given source to a data URL. | ||
*/ | ||
export declare function readAsDataURL(source: Blob | ArrayBuffer | Uint8Array | AsyncIterable<Uint8Array> | ReadableStream<Uint8Array>, type: string): Promise<string>; | ||
export declare function readAsDataURL(source: Blob | ArrayBuffer | ArrayBufferView | AsyncIterable<Uint8Array> | ReadableStream<Uint8Array>, type: string): Promise<string>; | ||
/** | ||
* Reads all data from the given source to an object URL. | ||
*/ | ||
export declare function readAsObjectURL(source: Blob | ArrayBuffer | Uint8Array | AsyncIterable<Uint8Array> | ReadableStream<Uint8Array>, type: string): Promise<string>; | ||
export declare function readAsObjectURL(source: Blob | ArrayBuffer | ArrayBufferView | AsyncIterable<Uint8Array> | ReadableStream<Uint8Array>, type: string): Promise<string>; | ||
/** | ||
* Reads all data from the given source to a string. | ||
*/ | ||
export declare function readAsText(source: Blob | ArrayBuffer | Uint8Array | AsyncIterable<Uint8Array> | ReadableStream<Uint8Array>, encoding?: string | undefined): Promise<string>; | ||
export declare function readAsText(source: Blob | ArrayBuffer | ArrayBufferView | AsyncIterable<Uint8Array> | ReadableStream<Uint8Array>, encoding?: string | undefined): Promise<string>; | ||
/** | ||
* Reads all data from the given source to a JSON object. | ||
*/ | ||
export declare function readAsJSON<T>(source: Blob | Uint8Array | AsyncIterable<Uint8Array> | ReadableStream<Uint8Array>): Promise<T>; | ||
export declare function readAsJSON<T>(source: Blob | ArrayBuffer | ArrayBufferView | AsyncIterable<Uint8Array> | ReadableStream<Uint8Array>): Promise<T>; | ||
/** | ||
@@ -67,0 +67,0 @@ * Concatenates multiple async iterable objects into a single one. |
@@ -21,2 +21,5 @@ import type { ProgressState, ProgressFunc, ProgressAbortHandler } from "../dialog/progress.ts"; | ||
}): Promise<File | null>; | ||
/** | ||
* @deprecated use {@link openFiles} instead. | ||
*/ | ||
export declare function openFile(options: { | ||
@@ -27,2 +30,5 @@ title?: string; | ||
}): Promise<File[]>; | ||
/** | ||
* @deprecated use {@link openDirectory} instead. | ||
*/ | ||
export declare function openFile(options: { | ||
@@ -32,2 +38,9 @@ title?: string; | ||
}): Promise<File[]>; | ||
export declare function openFiles(options?: { | ||
title?: string; | ||
type?: string; | ||
}): Promise<File[] | null>; | ||
export declare function openDirectory(options?: { | ||
title?: string; | ||
}): Promise<File | null>; | ||
export declare function pickFile(options?: { | ||
@@ -49,3 +62,3 @@ title?: string | undefined; | ||
}): Promise<void>; | ||
export declare function saveFile(file: Blob | ReadableStream<Uint8Array> | Uint8Array, options: { | ||
export declare function saveFile(file: Blob | ArrayBuffer | ArrayBufferView | ReadableStream<Uint8Array>, options: { | ||
name: string; | ||
@@ -52,0 +65,0 @@ type?: string; |
@@ -30,3 +30,3 @@ import type { FileSystemOptions, FileInfo, DirEntry, DirTree } from "../fs/types"; | ||
}): Promise<string>; | ||
export declare function writeFile(target: string | FileSystemFileHandle, data: Uint8Array | string, options?: FileSystemOptions & { | ||
export declare function writeFile(target: string | FileSystemFileHandle, data: string | ArrayBuffer | ArrayBufferView | ReadableStream<Uint8Array> | Blob, options?: FileSystemOptions & { | ||
append?: boolean; | ||
@@ -33,0 +33,0 @@ mode?: number; |
@@ -52,2 +52,5 @@ import type { ProgressState, ProgressFunc, ProgressAbortHandler } from "../dialog/progress.ts"; | ||
}): Promise<File | null>; | ||
/** | ||
* @deprecated use {@link openFiles} instead. | ||
*/ | ||
export function openFile(options: { | ||
@@ -58,2 +61,5 @@ title?: string; | ||
}): Promise<File[]>; | ||
/** | ||
* @deprecated use {@link openDirectory} instead. | ||
*/ | ||
export function openFile(options: { | ||
@@ -73,2 +79,17 @@ title?: string; | ||
export async function openFiles(options?: { | ||
title?: string; | ||
type?: string; | ||
}): Promise<File[] | null> { | ||
void options; | ||
throw new Error("Unsupported runtime"); | ||
} | ||
export async function openDirectory(options?: { | ||
title?: string; | ||
}): Promise<File | null> { | ||
void options; | ||
throw new Error("Unsupported runtime"); | ||
} | ||
export async function pickFile(options: { | ||
@@ -102,14 +123,20 @@ title?: string | undefined; | ||
}): Promise<void>; | ||
export async function saveFile(file: Blob | ReadableStream<Uint8Array> | Uint8Array, options: { | ||
name: string; | ||
type?: string; | ||
title?: string; | ||
}): Promise<void>; | ||
export async function saveFile(file: File | Blob | ReadableStream<Uint8Array> | Uint8Array, options: { | ||
title?: string; | ||
name?: string; | ||
type?: string; | ||
} = {}): Promise<void> { | ||
export async function saveFile( | ||
file: Blob | ArrayBuffer | ArrayBufferView | ReadableStream<Uint8Array>, | ||
options: { | ||
name: string; | ||
type?: string; | ||
title?: string; | ||
} | ||
): Promise<void>; | ||
export async function saveFile( | ||
file: File | Blob | ArrayBuffer | ArrayBufferView | ReadableStream<Uint8Array>, | ||
options: { | ||
title?: string; | ||
name?: string; | ||
type?: string; | ||
} = {} | ||
): Promise<void> { | ||
void file, options; | ||
throw new Error("Unsupported runtime"); | ||
} |
@@ -79,4 +79,5 @@ import type { FileSystemOptions, FileInfo, DirEntry, DirTree } from "../fs/types"; | ||
export async function writeFile(target: string | FileSystemFileHandle, | ||
data: Uint8Array | string, | ||
export async function writeFile( | ||
target: string | FileSystemFileHandle, | ||
data: string | ArrayBuffer | ArrayBufferView | ReadableStream<Uint8Array> | Blob, | ||
options: FileSystemOptions & { | ||
@@ -83,0 +84,0 @@ append?: boolean; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
3638583
644
51174
1241