UNPKG

one

Version:

One is a React framework for web, iOS, and Android apps.

82 lines (79 loc) • 2.99 kB
import { FileMode } from "./types.mjs"; const modes = new Set(Object.values(FileMode)); const algorithms = /* @__PURE__ */ new Set([ "MD5", "SHA-1", "SHA-256", "SHA-384", "SHA-512" ]); const encodings = /* @__PURE__ */ new Set(["utf8", "base64"]); function assertUri(uri, owner) { if (typeof uri !== "string") throw new Error(`${owner}: expected a uri string`); let parsed; try { parsed = new URL(uri); } catch { throw new Error(`${owner}: expected an absolute file uri`); } if (parsed.protocol !== "file:" && parsed.protocol !== "opfs:" || !parsed.pathname.startsWith("/") || parsed.search || parsed.hash) { throw new Error(`${owner}: expected an absolute file uri`); } } function assertMode(mode, owner) { if (typeof mode !== "string" || !modes.has(mode)) { throw new Error(`${owner}: expected a FileMode`); } } function assertEncoding(encoding, owner) { if (typeof encoding !== "string" || !encodings.has(encoding)) { throw new Error(`${owner}: expected encoding 'utf8' or 'base64'`); } } function assertAlgorithm(algorithm, owner) { if (typeof algorithm !== "string" || !algorithms.has(algorithm)) { throw new Error(`${owner}: expected a digest algorithm`); } } function assertByteLength(length, owner) { if (typeof length !== "number" || !Number.isInteger(length) || length < 0) { throw new Error(`${owner}: expected a nonnegative integer byte length`); } } function assertOffset(offset, owner) { if (typeof offset !== "number" || !Number.isFinite(offset) || offset < 0) { throw new Error(`${owner}: expected a finite nonnegative offset`); } } function assertName(name, owner) { if (typeof name !== "string" || name === "" || name.includes("/") || name === "." || name === "..") { throw new Error(`${owner}: expected a single path segment`); } } function assertWriteContent(content, owner) { if (typeof content !== "string" && !(content instanceof Uint8Array) && !(content instanceof ArrayBuffer)) { throw new Error(`${owner}: expected a string, Uint8Array or ArrayBuffer`); } } function assertWriteBytes(bytes, owner) { if (!(bytes instanceof Uint8Array) && !(bytes instanceof ArrayBuffer)) { throw new Error(`${owner}: expected a Uint8Array or ArrayBuffer`); } } function assertReadable(mode, verb) { if (mode !== FileMode.ReadOnly && mode !== FileMode.ReadWrite) { throw new Error(`${verb}: the handle is not readable`); } } function assertWritable(mode, verb) { if (mode === FileMode.ReadOnly) { throw new Error(`${verb}: the handle is not writable`); } } function assertDestination(destination, owner) { if (typeof destination !== "object" || destination === null || typeof destination.uri !== "string") { throw new Error(`${owner}: expected a File or Directory destination`); } } export { assertAlgorithm, assertByteLength, assertDestination, assertEncoding, assertMode, assertName, assertOffset, assertReadable, assertUri, assertWritable, assertWriteBytes, assertWriteContent }; //# sourceMappingURL=validate.mjs.map