@qooal/vault 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +39 -0
- package/LICENSE +21 -0
- package/README.md +270 -2
- package/dist/chunks/chunk-2AAWIHQO.js +1028 -0
- package/dist/chunks/chunk-MKUYQFGG.js +90 -0
- package/dist/chunks/chunk-PEWIUW3U.js +484 -0
- package/dist/chunks/chunk-Z5XT6XS2.js +115 -0
- package/dist/index.js +24 -0
- package/dist/next.js +65 -0
- package/dist/react.js +276 -0
- package/dist/server.js +357 -0
- package/dist/sign.js +20 -0
- package/dist/types/http.d.ts +49 -0
- package/dist/types/index.d.ts +8 -0
- package/dist/types/next.d.ts +39 -0
- package/dist/types/react/index.d.ts +93 -0
- package/dist/types/react/props.d.ts +62 -0
- package/dist/types/server/client.d.ts +160 -0
- package/dist/types/server/index.d.ts +16 -0
- package/dist/types/server/sign-url.d.ts +28 -0
- package/dist/types/server/webhook.d.ts +31 -0
- package/dist/types/sign.d.ts +48 -0
- package/dist/types/types.d.ts +415 -0
- package/dist/types/upload/element.d.ts +45 -0
- package/dist/types/upload/index.d.ts +14 -0
- package/dist/types/upload/perform.d.ts +66 -0
- package/dist/types/upload/uploader.d.ts +62 -0
- package/dist/types/url.d.ts +163 -0
- package/dist/upload.js +28 -0
- package/dist/widget.js +34 -0
- package/package.json +82 -4
package/dist/index.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import {
|
|
2
|
+
VAULT_PLACEHOLDER_HOSTS,
|
|
3
|
+
VaultGrammarError,
|
|
4
|
+
assertPublicId,
|
|
5
|
+
canonicalTransformation,
|
|
6
|
+
parseTransformation,
|
|
7
|
+
parseVaultPath,
|
|
8
|
+
srcSet,
|
|
9
|
+
vaultLibraryUrls,
|
|
10
|
+
vaultSignedPart,
|
|
11
|
+
vaultUrl
|
|
12
|
+
} from "./chunks/chunk-PEWIUW3U.js";
|
|
13
|
+
export {
|
|
14
|
+
VAULT_PLACEHOLDER_HOSTS,
|
|
15
|
+
VaultGrammarError,
|
|
16
|
+
assertPublicId,
|
|
17
|
+
canonicalTransformation,
|
|
18
|
+
parseTransformation,
|
|
19
|
+
parseVaultPath,
|
|
20
|
+
srcSet,
|
|
21
|
+
vaultLibraryUrls,
|
|
22
|
+
vaultSignedPart,
|
|
23
|
+
vaultUrl
|
|
24
|
+
};
|
package/dist/next.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import {
|
|
2
|
+
parseVaultPath,
|
|
3
|
+
srcSet
|
|
4
|
+
} from "./chunks/chunk-PEWIUW3U.js";
|
|
5
|
+
|
|
6
|
+
// src/next.ts
|
|
7
|
+
function snap(width, steps) {
|
|
8
|
+
if (!steps?.length) return width;
|
|
9
|
+
const sorted = steps.toSorted((a, b) => a - b);
|
|
10
|
+
return sorted.find((s) => s >= width) ?? sorted[sorted.length - 1];
|
|
11
|
+
}
|
|
12
|
+
function withParam(transformation, param, drop) {
|
|
13
|
+
const parts = transformation.split(",").filter((p) => p && !p.startsWith(drop));
|
|
14
|
+
if (param) parts.push(param);
|
|
15
|
+
return parts.join(",");
|
|
16
|
+
}
|
|
17
|
+
function createVaultLoader(options = {}) {
|
|
18
|
+
return ({ src, width, quality }) => {
|
|
19
|
+
let base;
|
|
20
|
+
let publicId;
|
|
21
|
+
let transformation;
|
|
22
|
+
let format;
|
|
23
|
+
let version;
|
|
24
|
+
let resourceType = "image";
|
|
25
|
+
if (/^https?:\/\//i.test(src)) {
|
|
26
|
+
const u = new URL(src);
|
|
27
|
+
const p = parseVaultPath(u.pathname);
|
|
28
|
+
if (p.signature !== null || p.delivery !== "upload")
|
|
29
|
+
throw new Error("vaultLoader resizes public delivery URLs only (a signed URL would need signing again)");
|
|
30
|
+
base = `${u.origin}/${p.library}`;
|
|
31
|
+
publicId = p.publicId;
|
|
32
|
+
transformation = [p.preset !== null ? `t_${p.preset}` : "", p.transformation ?? ""].filter(Boolean).join(",");
|
|
33
|
+
format = p.extension ?? void 0;
|
|
34
|
+
version = p.version ?? void 0;
|
|
35
|
+
resourceType = p.resource;
|
|
36
|
+
} else {
|
|
37
|
+
if (!options.library)
|
|
38
|
+
throw new Error("vaultLoader got a bare public ID: create the loader with createVaultLoader({ library })");
|
|
39
|
+
base = options.library;
|
|
40
|
+
const dot = src.lastIndexOf(".");
|
|
41
|
+
publicId = (dot > 0 ? src.slice(0, dot) : src).replace(/^\/+/, "");
|
|
42
|
+
format = dot > 0 ? src.slice(dot + 1) : void 0;
|
|
43
|
+
transformation = options.transformation ?? "";
|
|
44
|
+
}
|
|
45
|
+
const q = quality !== void 0 && quality !== (options.defaultQuality ?? 75) ? `q_${quality}` : null;
|
|
46
|
+
if (q) transformation = withParam(transformation, q, "q_");
|
|
47
|
+
if (format === void 0 && !transformation.split(",").some((p) => p.startsWith("f_")))
|
|
48
|
+
transformation = withParam(transformation, "f_auto", "f_");
|
|
49
|
+
const w = snap(width, options.widthSteps);
|
|
50
|
+
const url = srcSet(base, publicId, [w], {
|
|
51
|
+
transformation,
|
|
52
|
+
resourceType,
|
|
53
|
+
...format !== void 0 ? { format } : {},
|
|
54
|
+
...version !== void 0 ? { version } : {}
|
|
55
|
+
});
|
|
56
|
+
return url.replace(/ \d+w$/, "");
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
var vaultLoader = createVaultLoader();
|
|
60
|
+
var next_default = vaultLoader;
|
|
61
|
+
export {
|
|
62
|
+
createVaultLoader,
|
|
63
|
+
next_default as default,
|
|
64
|
+
vaultLoader
|
|
65
|
+
};
|
package/dist/react.js
ADDED
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import {
|
|
3
|
+
VAULT_UPLOADER_TAG,
|
|
4
|
+
defineVaultUploader
|
|
5
|
+
} from "./chunks/chunk-2AAWIHQO.js";
|
|
6
|
+
import "./chunks/chunk-Z5XT6XS2.js";
|
|
7
|
+
import {
|
|
8
|
+
parseTransformation,
|
|
9
|
+
srcSet,
|
|
10
|
+
vaultUrl
|
|
11
|
+
} from "./chunks/chunk-PEWIUW3U.js";
|
|
12
|
+
|
|
13
|
+
// src/react/index.tsx
|
|
14
|
+
import {
|
|
15
|
+
createElement,
|
|
16
|
+
useEffect,
|
|
17
|
+
useRef,
|
|
18
|
+
useState
|
|
19
|
+
} from "react";
|
|
20
|
+
|
|
21
|
+
// src/react/props.ts
|
|
22
|
+
var DEFAULT_WIDTHS = [320, 480, 640, 750, 828, 1080, 1200, 1600, 1920, 2560];
|
|
23
|
+
var MAX_SIDE = 8192;
|
|
24
|
+
var HEX = /^#[0-9a-f]{6}$/i;
|
|
25
|
+
function urlAt(o, transformation, w) {
|
|
26
|
+
const opts = { transformation };
|
|
27
|
+
if (o.format !== void 0) opts.format = o.format;
|
|
28
|
+
if (o.version !== void 0) opts.version = o.version;
|
|
29
|
+
return srcSet(o.library, o.publicId, [w], opts).replace(/ \d+w$/, "");
|
|
30
|
+
}
|
|
31
|
+
var ok = (w) => Number.isInteger(w) && w >= 1 && w <= MAX_SIDE;
|
|
32
|
+
function vaultImageProps(o) {
|
|
33
|
+
let transformation = o.transformation ?? "";
|
|
34
|
+
if (o.format === void 0 && parseTransformation(transformation).f === void 0)
|
|
35
|
+
transformation = transformation ? `${transformation},f_auto` : "f_auto";
|
|
36
|
+
let widths;
|
|
37
|
+
if (o.width !== void 0 && o.sizes === void 0) widths = [o.width, o.width * 2].filter(ok);
|
|
38
|
+
else {
|
|
39
|
+
const cap = o.width !== void 0 ? o.width * 2 : MAX_SIDE;
|
|
40
|
+
widths = [...o.widths ?? DEFAULT_WIDTHS].filter((w) => ok(w) && w <= cap);
|
|
41
|
+
if (o.width !== void 0 && ok(o.width) && !widths.includes(o.width)) widths.push(o.width);
|
|
42
|
+
}
|
|
43
|
+
widths = [...new Set(widths)].toSorted((a, b) => a - b);
|
|
44
|
+
if (widths.length === 0) throw new RangeError("no usable srcset width (1-8192)");
|
|
45
|
+
const fallback = o.width !== void 0 && ok(o.width) ? o.width : widths[Math.floor(widths.length / 2)];
|
|
46
|
+
const set = srcSet(o.library, o.publicId, widths, {
|
|
47
|
+
transformation,
|
|
48
|
+
...o.format !== void 0 ? { format: o.format } : {},
|
|
49
|
+
...o.version !== void 0 ? { version: o.version } : {}
|
|
50
|
+
});
|
|
51
|
+
const out = {
|
|
52
|
+
src: urlAt(o, transformation, fallback),
|
|
53
|
+
srcSet: set,
|
|
54
|
+
sizes: o.sizes ?? (o.width !== void 0 ? `${o.width}px` : "100vw"),
|
|
55
|
+
placeholder: o.dominantColor && HEX.test(o.dominantColor) ? o.dominantColor : null
|
|
56
|
+
};
|
|
57
|
+
if (o.width !== void 0) out.width = o.width;
|
|
58
|
+
if (o.height !== void 0) out.height = o.height;
|
|
59
|
+
return out;
|
|
60
|
+
}
|
|
61
|
+
var warned = /* @__PURE__ */ new Set();
|
|
62
|
+
function isDevelopment() {
|
|
63
|
+
try {
|
|
64
|
+
return process.env.NODE_ENV !== "production";
|
|
65
|
+
} catch {
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
function warnOnce(message) {
|
|
70
|
+
if (warned.has(message) || !isDevelopment()) return;
|
|
71
|
+
warned.add(message);
|
|
72
|
+
console.warn(message);
|
|
73
|
+
}
|
|
74
|
+
function vaultVideoSources(o) {
|
|
75
|
+
const v = o.version !== void 0 ? { version: o.version } : {};
|
|
76
|
+
const format = o.posterFormat;
|
|
77
|
+
if (format !== void 0 && format !== "jpg" && format !== "jpeg")
|
|
78
|
+
warnOnce(
|
|
79
|
+
`@qooal/vault: posterFormat ${JSON.stringify(format)} is not served (posters are always JPEG); using "jpg". posterFormat is deprecated.`
|
|
80
|
+
);
|
|
81
|
+
if (o.posterTransformation !== void 0 && o.posterTransformation !== "")
|
|
82
|
+
warnOnce(
|
|
83
|
+
"@qooal/vault: posterTransformation is ignored (the edge serves the stored JPEG poster as is). posterTransformation is deprecated."
|
|
84
|
+
);
|
|
85
|
+
return {
|
|
86
|
+
hls: vaultUrl(o.library, o.publicId, { resourceType: "video", format: "m3u8", ...v }),
|
|
87
|
+
mp4: vaultUrl(o.library, o.publicId, { resourceType: "video", format: "mp4", ...v }),
|
|
88
|
+
poster: vaultUrl(o.library, o.publicId, { resourceType: "video", format: "jpg", ...v })
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// src/react/index.tsx
|
|
93
|
+
import { jsx } from "react/jsx-runtime";
|
|
94
|
+
function VaultImage(props) {
|
|
95
|
+
const {
|
|
96
|
+
library,
|
|
97
|
+
publicId,
|
|
98
|
+
asset,
|
|
99
|
+
transformation,
|
|
100
|
+
format,
|
|
101
|
+
version,
|
|
102
|
+
widths,
|
|
103
|
+
width,
|
|
104
|
+
height,
|
|
105
|
+
sizes,
|
|
106
|
+
dominantColor,
|
|
107
|
+
alt,
|
|
108
|
+
priority,
|
|
109
|
+
style,
|
|
110
|
+
onLoad,
|
|
111
|
+
loading,
|
|
112
|
+
decoding,
|
|
113
|
+
...rest
|
|
114
|
+
} = props;
|
|
115
|
+
const [loaded, setLoaded] = useState(false);
|
|
116
|
+
const id = publicId ?? asset?.publicId;
|
|
117
|
+
if (!id) throw new TypeError("VaultImage needs publicId or asset");
|
|
118
|
+
const altText = alt ?? asset?.altText ?? void 0;
|
|
119
|
+
if (altText === void 0) throw new TypeError('VaultImage needs alt text (use alt="" for decorative images)');
|
|
120
|
+
const opts = { library, publicId: id };
|
|
121
|
+
if (transformation !== void 0) opts.transformation = transformation;
|
|
122
|
+
if (format !== void 0) opts.format = format;
|
|
123
|
+
const v = version ?? asset?.version;
|
|
124
|
+
if (v !== void 0) opts.version = v;
|
|
125
|
+
if (widths !== void 0) opts.widths = widths;
|
|
126
|
+
const iw = asset?.width ?? null;
|
|
127
|
+
const ih = asset?.height ?? null;
|
|
128
|
+
const w = width;
|
|
129
|
+
const h = height ?? (w !== void 0 && iw && ih ? Math.round(w * ih / iw) : void 0);
|
|
130
|
+
if (w !== void 0) opts.width = w;
|
|
131
|
+
if (h !== void 0) opts.height = h;
|
|
132
|
+
if (widths === void 0 && w === void 0 && iw) {
|
|
133
|
+
const capped = DEFAULT_WIDTHS.filter((x) => x <= iw);
|
|
134
|
+
opts.widths = capped.length ? capped : [iw];
|
|
135
|
+
}
|
|
136
|
+
if (sizes !== void 0) opts.sizes = sizes;
|
|
137
|
+
const color = dominantColor ?? asset?.dominantColor ?? null;
|
|
138
|
+
if (color) opts.dominantColor = color;
|
|
139
|
+
const a = vaultImageProps(opts);
|
|
140
|
+
return /* @__PURE__ */ jsx(
|
|
141
|
+
"img",
|
|
142
|
+
{
|
|
143
|
+
...rest,
|
|
144
|
+
alt: altText,
|
|
145
|
+
src: a.src,
|
|
146
|
+
srcSet: a.srcSet,
|
|
147
|
+
sizes: a.sizes,
|
|
148
|
+
width: a.width,
|
|
149
|
+
height: a.height,
|
|
150
|
+
loading: priority ? "eager" : loading ?? "lazy",
|
|
151
|
+
decoding: decoding ?? "async",
|
|
152
|
+
...priority ? { fetchPriority: "high" } : {},
|
|
153
|
+
style: a.placeholder && !loaded ? { backgroundColor: a.placeholder, ...style } : style,
|
|
154
|
+
onLoad: (e) => {
|
|
155
|
+
setLoaded(true);
|
|
156
|
+
onLoad?.(e);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
);
|
|
160
|
+
}
|
|
161
|
+
var loadHlsJs = async () => (await import("hls.js")).default;
|
|
162
|
+
function VaultVideo(props) {
|
|
163
|
+
const { library, publicId, version, poster, src, loadHls, controls, playsInline, preload, ...rest } = props;
|
|
164
|
+
const s = vaultVideoSources({ library, publicId, ...version !== void 0 ? { version } : {} });
|
|
165
|
+
const hls = src ?? s.hls;
|
|
166
|
+
const mp4 = s.mp4;
|
|
167
|
+
const ref = useRef(null);
|
|
168
|
+
const load = useRef(loadHls ?? loadHlsJs);
|
|
169
|
+
useEffect(() => {
|
|
170
|
+
const video = ref.current;
|
|
171
|
+
if (!video) return void 0;
|
|
172
|
+
if (video.canPlayType("application/vnd.apple.mpegurl")) {
|
|
173
|
+
video.src = hls;
|
|
174
|
+
return () => {
|
|
175
|
+
video.removeAttribute("src");
|
|
176
|
+
video.load();
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
let stopped = false;
|
|
180
|
+
let player = null;
|
|
181
|
+
load.current().then((Hls) => {
|
|
182
|
+
if (stopped) return;
|
|
183
|
+
if (!Hls.isSupported()) {
|
|
184
|
+
video.src = mp4;
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
player = new Hls({ capLevelToPlayerSize: true });
|
|
188
|
+
player.loadSource(hls);
|
|
189
|
+
player.attachMedia(video);
|
|
190
|
+
}).catch(() => {
|
|
191
|
+
if (!stopped) video.src = mp4;
|
|
192
|
+
});
|
|
193
|
+
return () => {
|
|
194
|
+
stopped = true;
|
|
195
|
+
player?.destroy();
|
|
196
|
+
};
|
|
197
|
+
}, [hls, mp4]);
|
|
198
|
+
return (
|
|
199
|
+
// oxlint-disable-next-line jsx-a11y/media-has-caption
|
|
200
|
+
/* @__PURE__ */ jsx(
|
|
201
|
+
"video",
|
|
202
|
+
{
|
|
203
|
+
...rest,
|
|
204
|
+
ref,
|
|
205
|
+
controls: controls ?? true,
|
|
206
|
+
playsInline: playsInline ?? true,
|
|
207
|
+
preload: preload ?? "metadata",
|
|
208
|
+
...poster === false ? {} : { poster: poster ?? s.poster }
|
|
209
|
+
}
|
|
210
|
+
)
|
|
211
|
+
);
|
|
212
|
+
}
|
|
213
|
+
var flag = (on) => on ? "" : void 0;
|
|
214
|
+
function VaultUploader(props) {
|
|
215
|
+
const [el, setEl] = useState(null);
|
|
216
|
+
const handlers = useRef(props);
|
|
217
|
+
useEffect(() => {
|
|
218
|
+
handlers.current = props;
|
|
219
|
+
});
|
|
220
|
+
useEffect(() => {
|
|
221
|
+
defineVaultUploader();
|
|
222
|
+
if (!el) return void 0;
|
|
223
|
+
const on = (type, fn) => {
|
|
224
|
+
const listener = (e) => {
|
|
225
|
+
if (e instanceof CustomEvent) fn(e.detail);
|
|
226
|
+
};
|
|
227
|
+
el.addEventListener(type, listener);
|
|
228
|
+
return () => el.removeEventListener(type, listener);
|
|
229
|
+
};
|
|
230
|
+
const offs = [
|
|
231
|
+
on("vault-upload", (d) => handlers.current.onUpload?.(d.asset, d.file)),
|
|
232
|
+
on("vault-error", (d) => handlers.current.onError?.(d.error, d.file)),
|
|
233
|
+
on("vault-progress", (d) => handlers.current.onProgress?.(d.loaded, d.total, d.file)),
|
|
234
|
+
on("vault-complete", (d) => handlers.current.onComplete?.(d.assets))
|
|
235
|
+
];
|
|
236
|
+
return () => {
|
|
237
|
+
for (const off of offs) off();
|
|
238
|
+
};
|
|
239
|
+
}, [el]);
|
|
240
|
+
useEffect(() => {
|
|
241
|
+
if (el && props.options && "configure" in el && typeof el.configure === "function") el.configure(props.options);
|
|
242
|
+
}, [el, props.options]);
|
|
243
|
+
const attrs = {
|
|
244
|
+
ref: setEl,
|
|
245
|
+
library: props.library,
|
|
246
|
+
ticket: props.ticket,
|
|
247
|
+
"ticket-endpoint": props.ticketEndpoint,
|
|
248
|
+
preset: props.preset,
|
|
249
|
+
"turnstile-sitekey": props.turnstileSitekey,
|
|
250
|
+
"api-base": props.apiBase,
|
|
251
|
+
accept: props.accept,
|
|
252
|
+
multiple: flag(props.multiple),
|
|
253
|
+
"max-files": props.maxFiles?.toString(),
|
|
254
|
+
"max-bytes": props.maxBytes?.toString(),
|
|
255
|
+
"folder-id": props.folderId,
|
|
256
|
+
"public-id": props.publicId,
|
|
257
|
+
tags: props.tags?.join(","),
|
|
258
|
+
access: props.access,
|
|
259
|
+
theme: props.theme,
|
|
260
|
+
label: props.label,
|
|
261
|
+
name: props.name,
|
|
262
|
+
disabled: flag(props.disabled),
|
|
263
|
+
paste: props.globalPaste ? "global" : void 0,
|
|
264
|
+
camera: props.noCamera ? "off" : void 0,
|
|
265
|
+
className: props.className
|
|
266
|
+
};
|
|
267
|
+
return createElement(VAULT_UPLOADER_TAG, attrs);
|
|
268
|
+
}
|
|
269
|
+
export {
|
|
270
|
+
DEFAULT_WIDTHS,
|
|
271
|
+
VaultImage,
|
|
272
|
+
VaultUploader,
|
|
273
|
+
VaultVideo,
|
|
274
|
+
vaultImageProps,
|
|
275
|
+
vaultVideoSources
|
|
276
|
+
};
|