@widgetic/creator 0.4.2 → 0.4.3

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.
@@ -0,0 +1,145 @@
1
+ /** Time limit for `urlResolver` / `storeFileUrl` lookups only (not for the upload transfer). */
2
+ const URL_RESOLVE_TIMEOUT_MS = 10_000;
3
+ function isValidFile(entry) {
4
+ if (typeof entry !== 'object' || entry === null)
5
+ return false;
6
+ const file = entry;
7
+ return typeof file.id === 'string' && file.id !== '' && typeof file.url === 'string' && file.url !== '';
8
+ }
9
+ function isHttpUrl(value) {
10
+ try {
11
+ const url = new URL(value);
12
+ return url.protocol === 'http:' || url.protocol === 'https:';
13
+ }
14
+ catch {
15
+ return false;
16
+ }
17
+ }
18
+ function warnUploadOnce(warnings, message, error) {
19
+ if (warnings?.mediaLibraryUpload)
20
+ return;
21
+ if (error !== undefined)
22
+ console.warn(message, error);
23
+ else
24
+ console.warn(message);
25
+ if (warnings)
26
+ warnings.mediaLibraryUpload = true;
27
+ }
28
+ async function withTimeout(work, timeoutMs, signal) {
29
+ if (signal?.aborted)
30
+ throw new DOMException('Aborted', 'AbortError');
31
+ let timer;
32
+ const abortPromise = signal &&
33
+ new Promise((_, reject) => {
34
+ const onAbort = () => {
35
+ signal.removeEventListener('abort', onAbort);
36
+ reject(new DOMException('Aborted', 'AbortError'));
37
+ };
38
+ signal.addEventListener('abort', onAbort, { once: true });
39
+ });
40
+ try {
41
+ const raced = abortPromise ? Promise.race([work, abortPromise]) : work;
42
+ return await Promise.race([
43
+ raced,
44
+ new Promise((_, reject) => {
45
+ timer = setTimeout(() => reject(new Error('timeout')), timeoutMs);
46
+ }),
47
+ ]);
48
+ }
49
+ finally {
50
+ if (timer !== undefined)
51
+ clearTimeout(timer);
52
+ }
53
+ }
54
+ async function raceAbort(work, signal) {
55
+ if (signal?.aborted)
56
+ throw new DOMException('Aborted', 'AbortError');
57
+ if (!signal)
58
+ return work;
59
+ return Promise.race([
60
+ work,
61
+ new Promise((_, reject) => {
62
+ const onAbort = () => {
63
+ signal.removeEventListener('abort', onAbort);
64
+ reject(new DOMException('Aborted', 'AbortError'));
65
+ };
66
+ signal.addEventListener('abort', onAbort, { once: true });
67
+ }),
68
+ ]);
69
+ }
70
+ export function uploadFromMediaLibrary(mediaLibrary) {
71
+ if (typeof mediaLibrary !== 'object' || mediaLibrary === null)
72
+ return undefined;
73
+ const upload = mediaLibrary.upload;
74
+ return typeof upload === 'function' ? upload : undefined;
75
+ }
76
+ /** One partner `upload` call per file; no time limit on the transfer (only `signal` abort). */
77
+ export async function runPartnerMediaUpload(upload, request, warnings) {
78
+ if (request.signal?.aborted)
79
+ throw new DOMException('Aborted', 'AbortError');
80
+ try {
81
+ const raw = await raceAbort(upload(request), request.signal);
82
+ if (request.signal?.aborted)
83
+ throw new DOMException('Aborted', 'AbortError');
84
+ if (!isValidFile(raw)) {
85
+ warnUploadOnce(warnings, '[widgetic-ui] mediaLibrary.upload: result was not a valid file; Widgetic storage is not used.');
86
+ throw Object.assign(new Error('The upload failed.'), { status: 500 });
87
+ }
88
+ return raw;
89
+ }
90
+ catch (error) {
91
+ if (request.signal?.aborted)
92
+ throw new DOMException('Aborted', 'AbortError');
93
+ const name = error instanceof Error ? error.name : '';
94
+ if (name === 'AbortError')
95
+ throw error;
96
+ warnUploadOnce(warnings, '[widgetic-ui] mediaLibrary.upload: the partner upload failed; Widgetic storage is not used.', error);
97
+ throw error;
98
+ }
99
+ }
100
+ async function resolveStoredUrl(file, mediaLibrary, storeFileUrl, signal) {
101
+ if (storeFileUrl) {
102
+ try {
103
+ const resolved = await withTimeout(Promise.resolve(storeFileUrl(file)), URL_RESOLVE_TIMEOUT_MS, signal);
104
+ if (typeof resolved === 'string' && isHttpUrl(resolved))
105
+ return resolved;
106
+ }
107
+ catch (error) {
108
+ if (signal?.aborted)
109
+ throw new DOMException('Aborted', 'AbortError');
110
+ // storeFileUrl is optional; fall through to the file's own url.
111
+ }
112
+ }
113
+ const resolver = mediaLibrary && 'urlResolver' in mediaLibrary
114
+ ? mediaLibrary.urlResolver
115
+ : undefined;
116
+ if (typeof resolver === 'function') {
117
+ try {
118
+ const resolved = await withTimeout(Promise.resolve(resolver(file)), URL_RESOLVE_TIMEOUT_MS, signal);
119
+ if (typeof resolved === 'string' && isHttpUrl(resolved))
120
+ return resolved;
121
+ }
122
+ catch (error) {
123
+ if (signal?.aborted)
124
+ throw new DOMException('Aborted', 'AbortError');
125
+ // fall through
126
+ }
127
+ }
128
+ return file.url;
129
+ }
130
+ /**
131
+ * When `mediaLibrary.upload` is given: one object request per file, no Widgetic fallback on failure.
132
+ * When it is not given: returns `null` so the caller uses Widgetic's upload API.
133
+ */
134
+ export async function tryPartnerMediaUpload(mediaLibrary, request, options = {}) {
135
+ const upload = uploadFromMediaLibrary(mediaLibrary);
136
+ if (!upload)
137
+ return null;
138
+ const file = await runPartnerMediaUpload(upload, request, options.warnings);
139
+ const url = await resolveStoredUrl(file, mediaLibrary, options.storeFileUrl, request.signal);
140
+ if (!isHttpUrl(url)) {
141
+ warnUploadOnce(options.warnings, '[widgetic-ui] mediaLibrary.upload: result url was not http(s); Widgetic storage is not used.');
142
+ throw Object.assign(new Error('The upload failed.'), { status: 500 });
143
+ }
144
+ return { ...file, url };
145
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@widgetic/creator",
3
- "version": "0.4.2",
3
+ "version": "0.4.3",
4
4
  "type": "module",
5
5
  "peerDependencies": {
6
6
  "@sveltejs/kit": "^2.20.0",