UNPKG

filepond

Version:

FilePond, where files go to stretch their bits.

153 lines (152 loc) • 4.08 kB
/*! * FilePond v5.0.2 * Copyright (c) 2017-2026 Pqina B.V. * Released under the MIT License * https://filepond.com */ import { createExtension as w } from "./createExtension.js"; import { isFileEntry as V, isBlobOrFile as b, isFile as M } from "../../utils/test.js"; import { Status as n } from "../../common/status.js"; import { upperCaseFirstLetter as U } from "../../utils/string.js"; function $(y) { const { name: I, props: A, factory: m } = y; return w({ name: I, type: "validator", props: A, factory: (d, u) => { const { didSetProps: h, props: c } = d, { getEntries: L, on: p, pushTask: i, abortTask: D, setEntryExtensionStatus: s, setEntryExtensionState: l, getEntryExtensionState: r } = u, { // by default can only validate a blob/file canValidateEntry: O = (t) => V(t) && b(t.file), // by default all validate entry returns "all is well" state validateEntry: T = () => null } = m(d, u); h(() => { S(); }); function S() { for (const t of L()) f(t); } function f(t) { const { canValidate: a } = r(t); a !== null && l(t, { // null means it's undetermined if we can validate, need to retest canValidate: null, // null means it's undetermined if we should validate, need to retest shouldValidate: null, // reset status status: { type: n.System, code: "VALIDATION_LIMBO", values: null, meta: null } }); } async function N(t) { t = s(t, { type: n.System, code: "VALIDATION_BUSY", progress: 1 / 0 }); let a; try { a = await T(t); } catch (o) { throw s(t, { type: n.Error, code: "VALIDATION_ERROR", values: { error: o } }), o; } let e = "unknown"; if (V(t) && M(t.file) && (e = `fileMainType${U(t.file.type.split("/").shift() ?? "")}`), s( t, a ? { type: n.Error, code: "VALIDATION_INVALID", subcode: a.code, values: { ...a.values, fileMainType: e } } : { type: n.System, code: "VALIDATION_COMPLETE" } ), a) return !1; } async function v(t) { const { shouldValidate: a } = c; if (!a) return; const e = await a(t); l(t, { shouldValidate: e, // have determined if we should validate, switch to idle status: { type: n.System, code: "VALIDATION_IDLE" } }); } async function E(t) { let a; try { a = await O(t); } catch (e) { throw s(t, { type: n.Error, code: "VALIDATION_ERROR", values: { error: e } }), e; } l(t, { canValidate: a, // have determined if we can validate, switch to idle status: { type: n.System, code: "VALIDATION_IDLE" } }); } function _(t) { const { canValidate: a } = r(t); a !== null && (D(t.id, E), f(t)); } function x(t) { const { canValidate: a, shouldValidate: e, status: o } = r(t), k = o?.code === "VALIDATION_COMPLETE"; if (!(o?.type === "error" || k || a === !1 || e === !1)) { if (a === null) { i(t.id, E); return; } if (c.shouldValidate && e === null) { i(t.id, v); return; } a === !0 && i(t.id, N); } } const F = p("updateEntryData", _), R = p("updateEntry", x); return { destroy() { R(), F(); } }; } }); } export { $ as createValidatorExtension };