@widgetic/creator 0.4.1 → 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.
- package/README.md +2 -0
- package/dist/CreatorApp.svelte +323 -80
- package/dist/CreatorApp.svelte.d.ts +5 -1
- package/dist/components/VersionDropdownRow.svelte +1 -1
- package/dist/components/WidgetDetails.svelte +56 -16
- package/dist/components/WidgetShapeLivePreviews.svelte +18 -2
- package/dist/creator-types.d.ts +27 -3
- package/dist/i18n/language.d.ts +4 -1
- package/dist/i18n/messages.d.ts +3 -0
- package/dist/i18n/messages.js +3 -0
- package/dist/i18n/statusText.js +1 -0
- package/dist/stores/websocketStore.js +14 -2
- package/dist/utils/freezeWatchdog.js +88 -1
- package/dist/utils/operationStream.js +49 -2
- package/dist/utils/partnerMediaUpload.d.ts +25 -0
- package/dist/utils/partnerMediaUpload.js +145 -0
- package/package.json +1 -1
|
@@ -23,6 +23,11 @@ export function streamOperationUpdates(operationId, options) {
|
|
|
23
23
|
clearTimeout(watchdogTimer);
|
|
24
24
|
watchdogTimer = null;
|
|
25
25
|
}
|
|
26
|
+
if (deferredTimer) {
|
|
27
|
+
clearTimeout(deferredTimer);
|
|
28
|
+
deferredTimer = null;
|
|
29
|
+
}
|
|
30
|
+
deferredFrame = null;
|
|
26
31
|
try {
|
|
27
32
|
reader?.cancel();
|
|
28
33
|
}
|
|
@@ -51,6 +56,8 @@ export function streamOperationUpdates(operationId, options) {
|
|
|
51
56
|
// polling GET /operations/:id — the terminal event then always lands.
|
|
52
57
|
const WATCHDOG_TIMEOUT_MS = 45000;
|
|
53
58
|
let watchdogTimer = null;
|
|
59
|
+
let deferredFrame = null;
|
|
60
|
+
let deferredTimer = null;
|
|
54
61
|
const armWatchdog = () => {
|
|
55
62
|
if (watchdogTimer)
|
|
56
63
|
clearTimeout(watchdogTimer);
|
|
@@ -84,6 +91,24 @@ export function streamOperationUpdates(operationId, options) {
|
|
|
84
91
|
reader = response.body.getReader();
|
|
85
92
|
const decoder = new TextDecoder();
|
|
86
93
|
let buffer = '';
|
|
94
|
+
// Progress frames can arrive in one read. Applying each one before the
|
|
95
|
+
// next read keeps the page thread inside this loop, so a freeze there
|
|
96
|
+
// never reaches the browser. Keep the latest frame and apply it on a
|
|
97
|
+
// later turn. The finished frame stays here, so the caller still sees
|
|
98
|
+
// the result before this promise resolves.
|
|
99
|
+
const flushDeferredFrame = () => {
|
|
100
|
+
deferredTimer = null;
|
|
101
|
+
const frame = deferredFrame;
|
|
102
|
+
deferredFrame = null;
|
|
103
|
+
if (!frame || settled)
|
|
104
|
+
return;
|
|
105
|
+
try {
|
|
106
|
+
onMessage?.(frame);
|
|
107
|
+
}
|
|
108
|
+
catch (handlerError) {
|
|
109
|
+
console.warn('[OperationStream] Progress handler failed', handlerError);
|
|
110
|
+
}
|
|
111
|
+
};
|
|
87
112
|
// Start idle timer before any operation payload arrives.
|
|
88
113
|
armWatchdog();
|
|
89
114
|
while (true) {
|
|
@@ -103,20 +128,42 @@ export function streamOperationUpdates(operationId, options) {
|
|
|
103
128
|
.map((line) => line.slice(5).trimStart())
|
|
104
129
|
.join('\n');
|
|
105
130
|
// Skip empty frames and keepalive comment frames.
|
|
106
|
-
|
|
131
|
+
// `continue` must still move frameEnd. The keepalive is `: keepalive\n\n`
|
|
132
|
+
// every 15s. Leaving frameEnd on that hit spins this loop on an empty
|
|
133
|
+
// buffer and the page thread never returns.
|
|
134
|
+
if (!dataLine || !dataLine.startsWith('{')) {
|
|
135
|
+
frameEnd = buffer.indexOf('\n\n');
|
|
107
136
|
continue;
|
|
137
|
+
}
|
|
108
138
|
try {
|
|
109
139
|
const op = JSON.parse(dataLine);
|
|
140
|
+
// Before any UI update. If this line is missing and the
|
|
141
|
+
// heartbeat has already stopped, the thread died earlier.
|
|
142
|
+
if (import.meta.env.DEV) {
|
|
143
|
+
const query = new URLSearchParams({
|
|
144
|
+
probe: 'stream-read',
|
|
145
|
+
status: `${op?.status ?? ''} ${op?.progress ?? ''}`.slice(0, 180),
|
|
146
|
+
});
|
|
147
|
+
void fetch(`http://localhost:3000/health?${query}`).catch(() => { });
|
|
148
|
+
}
|
|
110
149
|
// Only reset idle timer on real operation payloads — SSE
|
|
111
150
|
// `: keepalive` comment frames must not suppress polling fallback
|
|
112
151
|
// when the terminal `completed` event is lost behind a proxy.
|
|
113
152
|
armWatchdog();
|
|
114
|
-
onMessage?.(op);
|
|
115
153
|
const status = String(op?.status || '').toLowerCase();
|
|
116
154
|
if (TERMINAL_STATUSES.has(status)) {
|
|
155
|
+
if (deferredTimer) {
|
|
156
|
+
clearTimeout(deferredTimer);
|
|
157
|
+
deferredTimer = null;
|
|
158
|
+
}
|
|
159
|
+
deferredFrame = null;
|
|
160
|
+
onMessage?.(op);
|
|
117
161
|
settle('resolve', op);
|
|
118
162
|
return;
|
|
119
163
|
}
|
|
164
|
+
deferredFrame = op;
|
|
165
|
+
if (!deferredTimer)
|
|
166
|
+
deferredTimer = setTimeout(flushDeferredFrame, 0);
|
|
120
167
|
}
|
|
121
168
|
catch (parseError) {
|
|
122
169
|
console.warn('[OperationStream] Failed to parse SSE frame', parseError);
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { MediaLibraryFile, MediaLibraryProvider } from '@widgetic/file-browser';
|
|
2
|
+
/** Why the creator asks the partner to store a file. */
|
|
3
|
+
export type MediaUploadPurpose = 'canvas' | 'chat' | 'field';
|
|
4
|
+
export interface PartnerUploadRequest {
|
|
5
|
+
file: File;
|
|
6
|
+
purpose: MediaUploadPurpose;
|
|
7
|
+
onProgress?: (percentage: number) => void;
|
|
8
|
+
signal?: AbortSignal;
|
|
9
|
+
}
|
|
10
|
+
export type PartnerUploadFn = (request: PartnerUploadRequest) => Promise<MediaLibraryFile>;
|
|
11
|
+
export declare function uploadFromMediaLibrary(mediaLibrary: unknown): PartnerUploadFn | undefined;
|
|
12
|
+
/** One partner `upload` call per file; no time limit on the transfer (only `signal` abort). */
|
|
13
|
+
export declare function runPartnerMediaUpload(upload: PartnerUploadFn, request: PartnerUploadRequest, warnings?: {
|
|
14
|
+
mediaLibraryUpload?: boolean;
|
|
15
|
+
}): Promise<MediaLibraryFile>;
|
|
16
|
+
/**
|
|
17
|
+
* When `mediaLibrary.upload` is given: one object request per file, no Widgetic fallback on failure.
|
|
18
|
+
* When it is not given: returns `null` so the caller uses Widgetic's upload API.
|
|
19
|
+
*/
|
|
20
|
+
export declare function tryPartnerMediaUpload(mediaLibrary: MediaLibraryProvider | undefined, request: PartnerUploadRequest, options?: {
|
|
21
|
+
storeFileUrl?: (file: MediaLibraryFile) => Promise<string>;
|
|
22
|
+
warnings?: {
|
|
23
|
+
mediaLibraryUpload?: boolean;
|
|
24
|
+
};
|
|
25
|
+
}): Promise<MediaLibraryFile | null>;
|
|
@@ -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
|
+
}
|