@wix/himalaya-cli 0.1724.0 → 0.1726.0

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.
Files changed (69) hide show
  1. package/dist/authoring-runtime/node_modules/@wix/himalaya/dist/_types/core/sdk/iap/src/wix-payments-contract.d.ts +6 -4
  2. package/dist/authoring-runtime/node_modules/@wix/himalaya/dist/_types/core/sdk/location/src/index.d.ts +5 -3
  3. package/dist/authoring-runtime/node_modules/@wix/himalaya/dist/_types/core/sdk/on-device-ai/src/index.d.ts +19 -7
  4. package/dist/authoring-runtime/node_modules/@wix/himalaya/dist/_types/core/sdk/sdk-types/src/capabilities.generated.d.ts +1 -1
  5. package/dist/authoring-runtime/node_modules/@wix/himalaya/dist/_types/stdlib/flows/iap-checkout/src/checkout-source.d.ts +3 -0
  6. package/dist/authoring-runtime/node_modules/@wix/himalaya/dist/_types/stdlib/flows/iap-checkout/src/iap-verticals/videos.d.ts +31 -1
  7. package/dist/authoring-runtime/node_modules/@wix/himalaya/dist/_types/stdlib/flows/iap-checkout/src/index.d.ts +1 -1
  8. package/dist/authoring-runtime/node_modules/@wix/himalaya/dist/audio-record.mjs +1 -1
  9. package/dist/authoring-runtime/node_modules/@wix/himalaya/dist/calendar.mjs +1 -1
  10. package/dist/authoring-runtime/node_modules/@wix/himalaya/dist/calls.mjs +1 -1
  11. package/dist/authoring-runtime/node_modules/@wix/himalaya/dist/contacts.mjs +1 -1
  12. package/dist/authoring-runtime/node_modules/@wix/himalaya/dist/iap-checkout.mjs +83 -9
  13. package/dist/authoring-runtime/node_modules/@wix/himalaya/dist/location.mjs +1 -1
  14. package/dist/authoring-runtime/node_modules/@wix/himalaya/dist/motion.mjs +1 -1
  15. package/dist/authoring-runtime/node_modules/@wix/himalaya/dist/on-device-ai.mjs +49 -3
  16. package/dist/authoring-runtime/node_modules/@wix/himalaya/dist/photos.mjs +1 -1
  17. package/dist/authoring-runtime/node_modules/@wix/himalaya/dist/screens.mjs +92 -2
  18. package/dist/authoring-runtime/node_modules/@wix/himalaya/dist/sdk-types.mjs +1 -1
  19. package/dist/authoring-runtime/node_modules/@wix/himalaya/dist/tasks.mjs +1 -1
  20. package/dist/browser-plane/assets.json +7 -7
  21. package/dist/browser-plane/preview-deferred/{a6e25c06d2a17ab2b5e34bf2b5965fa3ddd76ce3da32a036e546a60938130d7b.bin → 2df92277f550d6bf34ff43d15ae4a9082d012643a6bff39bd78724115505b716.bin} +1 -1
  22. package/dist/browser-plane/sw.js +8 -8
  23. package/dist/browser-plane/t5-frame.html +16 -0
  24. package/dist/browser-plane/t5-worker-frame.html +16 -0
  25. package/dist/cli.mjs +1830 -1480
  26. package/dist/preview-assets.generated.bin +0 -0
  27. package/fixtures/wix-canned.json +51 -0
  28. package/guides/capabilities.md +2 -2
  29. package/guides/headless-loop.md +2 -1
  30. package/guides/iap-products.md +2 -2
  31. package/guides/iap-screen-adoption.md +1 -1
  32. package/guides/one-app-section-mappings.md +2 -2
  33. package/guides/sdk-reference.json +36 -8
  34. package/guides/skill.md +1 -1
  35. package/guides/testing-himi-apps.md +4 -3
  36. package/guides/web-page-sdk-reference.md +1 -1
  37. package/package.json +2 -2
  38. package/templates/beauty-salon/l10n/en.json +2 -2
  39. package/templates/beauty-salon/tier5-src/_shared/branded-lite-hosted.ts +3 -2
  40. package/templates/branded-lite/config.ts +14 -1
  41. package/templates/branded-lite/dev/index.ts +729 -2
  42. package/templates/branded-lite/himalaya.content.json +3 -0
  43. package/templates/branded-lite/l10n/en.json +93 -2
  44. package/templates/branded-lite/l10n/he.json +173 -0
  45. package/templates/branded-lite/tier5-src/_shared/branded-lite-hosted.ts +3 -2
  46. package/templates/branded-lite/tier5-src/_shared/wix-file-share.ts +332 -0
  47. package/templates/branded-lite/tier5-src/himi-branded-lite-file-share/index.ts +635 -0
  48. package/templates/branded-lite/tier5-src/himi-branded-lite-video/index.ts +82 -12
  49. package/templates/community-club/l10n/en.json +2 -2
  50. package/templates/community-club/tier5-src/_shared/branded-lite-hosted.ts +3 -2
  51. package/templates/conference/tier5-src/_shared/branded-lite-hosted.ts +3 -2
  52. package/templates/events-meetups/l10n/en.json +2 -2
  53. package/templates/events-meetups/tier5-src/_shared/branded-lite-hosted.ts +3 -2
  54. package/templates/gym-fitness/l10n/en.json +2 -2
  55. package/templates/gym-fitness/tier5-src/_shared/branded-lite-hosted.ts +3 -2
  56. package/templates/nonprofit-cause/l10n/en.json +2 -2
  57. package/templates/nonprofit-cause/tier5-src/_shared/branded-lite-hosted.ts +3 -2
  58. package/templates/podcast-creator/l10n/en.json +2 -2
  59. package/templates/podcast-creator/tier5-src/_shared/branded-lite-hosted.ts +3 -2
  60. package/templates/portfolio-creative/l10n/en.json +2 -2
  61. package/templates/portfolio-creative/tier5-src/_shared/branded-lite-hosted.ts +3 -2
  62. package/templates/restaurant/l10n/en.json +2 -2
  63. package/templates/restaurant/tier5-src/_shared/branded-lite-hosted.ts +3 -2
  64. package/templates/retail-store/l10n/en.json +2 -2
  65. package/templates/retail-store/tier5-src/_shared/branded-lite-hosted.ts +3 -2
  66. package/templates/salon-spa/tier5-src/_shared/branded-lite-hosted.ts +3 -2
  67. package/templates/wholesale-store/tier5-src/_shared/branded-lite-hosted.ts +3 -2
  68. package/templates/yoga-studio/l10n/en.json +2 -2
  69. package/templates/yoga-studio/tier5-src/_shared/branded-lite-hosted.ts +3 -2
@@ -0,0 +1,332 @@
1
+ // Wix File Share ("Documents") — the consumer REST contract, for Tier-5 workers. Hand-written.
2
+ //
3
+ // SOURCE. Every path, field and enum below comes from the File Share service's published schema
4
+ // (docs-schema FQDN `wix.file_sharing.v1.library_item` / `.settings`, appDefId
5
+ // 1537b24e-29d1-6d8f-b8e1-d6860f2f70b9, proto artifact com.wixpress.catalyst:file-sharing-api) and
6
+ // from its server and its JS client in wix-private/catalyst-server (file-sharing-server/). The
7
+ // public gateway maps `www.wixapis.com/file-sharing/api/v1/file-sharing/*` onto the service, so the
8
+ // calls go through the host's wixApi bridge with the visitor or member credential. Owner
9
+ // credentials are never used: a library's folder and plan permissions are evaluated for the
10
+ // identity that calls.
11
+ //
12
+ // PERMISSIONS ARE THE SERVER'S. FileSharingService checks View on every read (a query returns only
13
+ // the items the caller may see), Download on `download`, UploadFile on the target folder for
14
+ // `start-upload` and `complete-upload`. `authorize-actions` only says in advance what those checks
15
+ // will answer, so a screen can leave out a button that would be refused. It is never the gate.
16
+ //
17
+ // CACHING. None of these paths has a Wix cache rule, and the native cache policy is deny-by-default
18
+ // (WixCachePolicy), so a member's restricted listing is never served to the next account.
19
+
20
+ export const FILE_SHARE_APP_DEF_ID = "1537b24e-29d1-6d8f-b8e1-d6860f2f70b9";
21
+ export const FILE_SHARE_BASE = "/file-sharing/api/v1/file-sharing";
22
+
23
+ /** Rows per page. The service allows 1..100. */
24
+ export const PAGE_SIZE = 50;
25
+ /** `LibraryItemFilter.search` is 1..50 characters. */
26
+ export const SEARCH_MAX = 50;
27
+ /** `CompleteFileUploadAction.uploadResponse` is at most 4096 characters. */
28
+ export const UPLOAD_RESPONSE_MAX = 4096;
29
+ /** The largest file this app uploads. The upload is a multipart body sent through
30
+ * `presigned_upload.put`, whose inline bound is 8 MiB (MAX_INLINE_BASE64_BYTES); 7 MiB leaves
31
+ * room for the form fields. A larger file is refused before any request, with a message. */
32
+ export const UPLOAD_MAX_BYTES = 7 * 1024 * 1024;
33
+
34
+ export type Availability = "PUBLIC" | "MEMBER" | "PAID" | string;
35
+ export type AllowedAction =
36
+ | "MOVE" | "RENAME" | "DELETE" | "DOWNLOAD" | "CREATE_FOLDER" | "UPLOAD_FILE" | "VIEW" | "FAVORITE" | "MANAGE_PERMISSIONS";
37
+
38
+ export interface WixLibraryItem {
39
+ id?: string;
40
+ name?: string;
41
+ parentFolderId?: string;
42
+ createdAt?: string;
43
+ createdByName?: string | { value?: string };
44
+ path?: Array<{ libraryItemId?: string; name?: string }>;
45
+ availability?: Availability;
46
+ fileFields?: { extension?: string; sizeInBytes?: string | number; size?: number; category?: string; previewable?: boolean };
47
+ folderFields?: { childrenCount?: number; lastModified?: string; availability?: Availability };
48
+ }
49
+
50
+ export interface LibraryPage {
51
+ items: WixLibraryItem[];
52
+ nextCursor: string;
53
+ }
54
+
55
+ export interface AuthorizedAction {
56
+ itemId?: string;
57
+ action?: AllowedAction;
58
+ status?: "ALLOWED" | "FORBIDDEN";
59
+ reason?: string;
60
+ }
61
+
62
+ export interface Authorization {
63
+ /** itemId → the actions the server will allow on it. */
64
+ items: Map<string, Set<AllowedAction>>;
65
+ /** itemId → why each refused action is refused (`MUST_BE_A_MEMBER`, `MISSING_PERMISSION`, …). */
66
+ refusals: Map<string, Map<AllowedAction, string>>;
67
+ /** Actions allowed in the folder passed as `parentFolderId`. */
68
+ folder: Set<AllowedAction>;
69
+ }
70
+
71
+ export interface FileShareSettings {
72
+ rootFolderId: string;
73
+ whoCanDownload: string;
74
+ }
75
+
76
+ export interface UploadTarget {
77
+ url: string;
78
+ requestParameters: Record<string, string>;
79
+ actionId: string;
80
+ }
81
+
82
+ /** A refusal `start-upload` or `complete-upload` returned instead of a URL or an item. */
83
+ export interface UploadRefusal {
84
+ kind: "tooBig" | "extension" | "quota" | "video" | "unknown";
85
+ maxSize?: number;
86
+ extension?: string;
87
+ }
88
+
89
+ type WixApiBridge = { call: (method: string, path: string, body?: unknown) => Promise<unknown> };
90
+
91
+ async function call<T>(method: string, path: string, body?: unknown): Promise<T> {
92
+ const wixApi = (globalThis as { himalaya?: { wixApi?: WixApiBridge } }).himalaya?.wixApi;
93
+ if (!wixApi) throw new Error("wix-file-share: host did not install the wixApi bridge");
94
+ return (body === undefined
95
+ ? await wixApi.call(method, `${FILE_SHARE_BASE}${path}`)
96
+ : await wixApi.call(method, `${FILE_SHARE_BASE}${path}`, body)) as T;
97
+ }
98
+
99
+ /** The HTTP status of a bridge failure. The bridges put it first in the message (`HTTP 403 …`);
100
+ * some also set a field. A status quoted inside the body is data, not the answer. */
101
+ export function httpStatus(error: unknown): number | undefined {
102
+ if (!error || typeof error !== "object") return undefined;
103
+ const row = error as { status?: unknown; statusCode?: unknown; response?: { status?: unknown }; message?: unknown };
104
+ for (const value of [row.status, row.statusCode, row.response?.status]) {
105
+ if (typeof value === "number" && value >= 100) return value;
106
+ }
107
+ const match = typeof row.message === "string" ? /^(?:HTTP\s*)?(\d{3})\b/i.exec(row.message.trim()) : null;
108
+ return match ? Number(match[1]) : undefined;
109
+ }
110
+
111
+ function page(raw: unknown): LibraryPage {
112
+ const data = (raw ?? {}) as { libraryItems?: unknown; metaData?: { nextCursor?: unknown } };
113
+ const next = data.metaData?.nextCursor;
114
+ return {
115
+ items: Array.isArray(data.libraryItems) ? data.libraryItems as WixLibraryItem[] : [],
116
+ // A StringValue arrives as a bare string; tolerate the wrapped form too.
117
+ nextCursor: typeof next === "string" ? next : typeof (next as { value?: unknown })?.value === "string" ? String((next as { value: string }).value) : "",
118
+ };
119
+ }
120
+
121
+ function paging(cursor: string): { limit: number; cursor?: string } {
122
+ return cursor ? { limit: PAGE_SIZE, cursor } : { limit: PAGE_SIZE };
123
+ }
124
+
125
+ /** One page of a folder's direct children. `folderId` "" is the library root (the service
126
+ * defaults to it when no parent is given). */
127
+ export async function queryFolder(folderId: string, cursor = ""): Promise<LibraryPage> {
128
+ return page(await call("POST", "/library-folder-items/query", {
129
+ ...(folderId ? { filter: { parentLibraryItemIds: [folderId] } } : {}),
130
+ paging: paging(cursor),
131
+ }));
132
+ }
133
+
134
+ /** One page of a name / uploader search under `folderId` (the whole library for ""). */
135
+ export async function searchLibrary(text: string, folderId: string, cursor = ""): Promise<LibraryPage> {
136
+ const search = text.trim().slice(0, SEARCH_MAX);
137
+ return page(await call("POST", "/library-items/query", {
138
+ filter: { search, ...(folderId ? { descendantsOfFolderId: folderId } : {}) },
139
+ paging: paging(cursor),
140
+ }));
141
+ }
142
+
143
+ export async function appSettings(): Promise<FileShareSettings> {
144
+ const data = await call<{ rootFolderId?: unknown; settings?: { downloadSettings?: { whoCanDownload?: unknown } } }>("GET", "/settings");
145
+ return {
146
+ rootFolderId: typeof data?.rootFolderId === "string" ? data.rootFolderId : "",
147
+ whoCanDownload: typeof data?.settings?.downloadSettings?.whoCanDownload === "string" ? data.settings.downloadSettings.whoCanDownload : "",
148
+ };
149
+ }
150
+
151
+ /** What the caller may do on `itemIds` (at most 100) and inside `parentFolderId`. */
152
+ export async function authorize(itemIds: string[], parentFolderId: string): Promise<Authorization> {
153
+ const data = await call<{ authorizedActions?: AuthorizedAction[]; folderAuthorizedActions?: AuthorizedAction[] }>(
154
+ "POST",
155
+ "/authorize-actions",
156
+ { libraryItemIds: itemIds.slice(0, 100), ...(parentFolderId ? { parentFolderId } : {}) },
157
+ );
158
+ const items = new Map<string, Set<AllowedAction>>();
159
+ const refusals = new Map<string, Map<AllowedAction, string>>();
160
+ for (const row of data?.authorizedActions ?? []) {
161
+ if (!row?.itemId || !row.action) continue;
162
+ if (row.status === "ALLOWED") {
163
+ if (!items.has(row.itemId)) items.set(row.itemId, new Set());
164
+ items.get(row.itemId)!.add(row.action);
165
+ } else {
166
+ if (!refusals.has(row.itemId)) refusals.set(row.itemId, new Map());
167
+ refusals.get(row.itemId)!.set(row.action, row.reason ?? "");
168
+ }
169
+ }
170
+ const folder = new Set<AllowedAction>();
171
+ for (const row of data?.folderAuthorizedActions ?? []) {
172
+ if (row?.action && row.status === "ALLOWED") folder.add(row.action);
173
+ }
174
+ return { items, refusals, folder };
175
+ }
176
+
177
+ /** A fresh, expiring `Content-Disposition: attachment` URL for one file. */
178
+ export async function downloadUrl(itemId: string): Promise<string> {
179
+ const data = await call<{ url?: unknown }>("POST", "/library-items/download", { action: { libraryItemIds: [itemId] } });
180
+ if (typeof data?.url !== "string" || !data.url) throw new Error("wix-file-share: download returned no url");
181
+ return data.url;
182
+ }
183
+
184
+ /** A fresh `Content-Disposition: inline` URL for one file. The service counts it as a view. */
185
+ export async function viewUrl(itemId: string): Promise<string> {
186
+ const data = await call<{ urls?: Array<{ libraryItemId?: string; url?: string }> }>("POST", "/library-items/view-file", { actions: [{ libraryItemId: itemId }] });
187
+ const url = (data?.urls ?? []).find((row) => row?.libraryItemId === itemId)?.url ?? data?.urls?.[0]?.url;
188
+ if (typeof url !== "string" || !url) throw new Error("wix-file-share: view-file returned no url");
189
+ return url;
190
+ }
191
+
192
+ function refusalOf(row: Record<string, unknown> | undefined): UploadRefusal {
193
+ const tooBig = row?.fileTooBig as { maxSize?: unknown } | undefined;
194
+ if (tooBig) return { kind: "tooBig", ...(Number.isFinite(Number(tooBig.maxSize)) ? { maxSize: Number(tooBig.maxSize) } : {}) };
195
+ const ext = row?.fileExtensionNotSupported as { extension?: unknown } | undefined;
196
+ if (ext) return { kind: "extension", ...(typeof ext.extension === "string" ? { extension: ext.extension } : {}) };
197
+ if (row?.quotaExceeded) return { kind: "quota" };
198
+ if (row?.videoFilesForbidden) return { kind: "video" };
199
+ return { kind: "unknown" };
200
+ }
201
+
202
+ /** Phase 1: ask for an upload URL for one file into `folderId` ("" = root). */
203
+ export async function startUpload(
204
+ file: { name: string; sizeBytes: number },
205
+ folderId: string,
206
+ actionId: string,
207
+ ): Promise<{ target?: UploadTarget; refusal?: UploadRefusal }> {
208
+ const data = await call<{ urls?: Array<Record<string, unknown>>; failures?: Array<Record<string, unknown>> }>("POST", "/files/start-upload", {
209
+ actions: [{ name: file.name, sizeInBytes: String(file.sizeBytes), ...(folderId ? { parentFolderId: folderId } : {}), actionId }],
210
+ });
211
+ const failure = (data?.failures ?? []).find((row) => row?.actionId === actionId) ?? data?.failures?.[0];
212
+ if (failure) return { refusal: refusalOf(failure) };
213
+ const row = (data?.urls ?? []).find((candidate) => candidate?.actionId === actionId) ?? data?.urls?.[0];
214
+ const url = typeof row?.url === "string" ? row.url : "";
215
+ if (!url) return { refusal: { kind: "unknown" } };
216
+ const params: Record<string, string> = {};
217
+ for (const [key, value] of Object.entries((row?.requestParameters ?? {}) as Record<string, unknown>)) {
218
+ if (value !== undefined && value !== null) params[key] = String(value);
219
+ }
220
+ return { target: { url, requestParameters: params, actionId } };
221
+ }
222
+
223
+ /** Phase 3: hand the upload host's answer back, so the file becomes a library item. */
224
+ export async function completeUpload(
225
+ uploadResponse: string,
226
+ folderId: string,
227
+ actionId: string,
228
+ ): Promise<{ item?: WixLibraryItem; refusal?: UploadRefusal }> {
229
+ const data = await call<{ libraryItems?: WixLibraryItem[]; failures?: Array<Record<string, unknown>> }>("POST", "/files/complete-upload", {
230
+ actions: [{ uploadResponse, ...(folderId ? { parentFolderId: folderId } : {}), actionId }],
231
+ });
232
+ const failure = (data?.failures ?? []).find((row) => row?.actionId === actionId) ?? data?.failures?.[0];
233
+ if (failure) return { refusal: refusalOf(failure) };
234
+ const item = data?.libraryItems?.[0];
235
+ return item?.id ? { item } : { refusal: { kind: "unknown" } };
236
+ }
237
+
238
+ // ── multipart body, as base64 ────────────────────────────────────────────────
239
+ //
240
+ // The upload host takes `multipart/form-data`: every `requestParameters` entry as a field, then
241
+ // the bytes as `file` (catalyst-server WixMediaClient.uploadFile). `presigned_upload.put` sends a
242
+ // base64 body verbatim, so the body is built here as base64 WITHOUT decoding the file: the text
243
+ // before the file is made a multiple of 3 bytes long (by a CRLF preamble, which RFC 2046 lets a
244
+ // parser ignore), so its base64 joins the file's base64 exactly. Only the file's last base64
245
+ // quantum (at most 3 bytes) is decoded, to be re-encoded together with the closing boundary.
246
+
247
+ const B64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
248
+
249
+ export function utf8Bytes(text: string): number[] {
250
+ const out: number[] = [];
251
+ for (const ch of text) {
252
+ let code = ch.codePointAt(0)!;
253
+ if (code < 0x80) out.push(code);
254
+ else if (code < 0x800) out.push(0xc0 | (code >> 6), 0x80 | (code & 63));
255
+ else if (code < 0x10000) out.push(0xe0 | (code >> 12), 0x80 | ((code >> 6) & 63), 0x80 | (code & 63));
256
+ else {
257
+ code = Math.min(code, 0x10ffff);
258
+ out.push(0xf0 | (code >> 18), 0x80 | ((code >> 12) & 63), 0x80 | ((code >> 6) & 63), 0x80 | (code & 63));
259
+ }
260
+ }
261
+ return out;
262
+ }
263
+
264
+ export function base64Encode(bytes: number[]): string {
265
+ let out = "";
266
+ for (let i = 0; i < bytes.length; i += 3) {
267
+ const a = bytes[i]!, b = bytes[i + 1], c = bytes[i + 2];
268
+ const n = (a << 16) | ((b ?? 0) << 8) | (c ?? 0);
269
+ out += B64[(n >> 18) & 63]! + B64[(n >> 12) & 63]! + (b === undefined ? "=" : B64[(n >> 6) & 63]!) + (c === undefined ? "=" : B64[n & 63]!);
270
+ }
271
+ return out;
272
+ }
273
+
274
+ /** Decode ONE base64 quantum (4 characters) into its 1–3 bytes. */
275
+ function decodeQuantum(quantum: string): number[] {
276
+ const values = [...quantum].map((ch) => (ch === "=" ? -1 : B64.indexOf(ch)));
277
+ // Two data characters, then either two more, one and "=", or "==".
278
+ if (values.length !== 4 || values[0]! < 0 || values[1]! < 0 || (values[2]! < 0 && values[3]! >= 0)) {
279
+ throw new Error("wix-file-share: invalid base64");
280
+ }
281
+ const n = (values[0]! << 18) | (values[1]! << 12) | (Math.max(values[2]!, 0) << 6) | Math.max(values[3]!, 0);
282
+ const bytes = [(n >> 16) & 255];
283
+ if (values[2]! >= 0) bytes.push((n >> 8) & 255);
284
+ if (values[3]! >= 0) bytes.push(n & 255);
285
+ return bytes;
286
+ }
287
+
288
+ /** A header value inside double quotes: no CR/LF, quotes and backslashes escaped. */
289
+ function quoted(value: string): string {
290
+ return value.replace(/[\r\n]+/g, " ").replace(/["\\]/g, (ch) => `\\${ch}`);
291
+ }
292
+
293
+ export function multipartBase64(
294
+ fields: Record<string, string>,
295
+ file: { name: string; mimeType: string; base64: string },
296
+ boundary: string,
297
+ ): string {
298
+ if (!/^[A-Za-z0-9'()+_,.\/:=?-]{1,70}$/.test(boundary)) throw new Error("wix-file-share: invalid boundary");
299
+ const fileB64 = file.base64.replace(/\s+/g, "");
300
+ if (fileB64.length % 4 !== 0 || /[^A-Za-z0-9+/=]/.test(fileB64) || /=[^=]/.test(fileB64)) throw new Error("wix-file-share: invalid base64");
301
+ const mime = /^[A-Za-z0-9!#$&^_.+-]+\/[A-Za-z0-9!#$&^_.+-]+$/.test(file.mimeType) ? file.mimeType : "application/octet-stream";
302
+ let head = "";
303
+ for (const [name, value] of Object.entries(fields)) {
304
+ head += `--${boundary}\r\nContent-Disposition: form-data; name="${quoted(name)}"\r\n\r\n${value}\r\n`;
305
+ }
306
+ head += `--${boundary}\r\nContent-Disposition: form-data; name="file"; filename="${quoted(file.name)}"\r\nContent-Type: ${mime}\r\n\r\n`;
307
+ const headBytes = utf8Bytes(head);
308
+ // Pad to a multiple of 3 with a CRLF preamble: remainder 1 takes 2 bytes, remainder 2 takes 4.
309
+ const preamble = [[], [13, 10], [13, 10, 13, 10]][headBytes.length % 3]!;
310
+ const prefix = [...preamble, ...headBytes];
311
+ const body = fileB64.slice(0, Math.max(0, fileB64.length - 4));
312
+ const last = fileB64.length ? decodeQuantum(fileB64.slice(-4)) : [];
313
+ const tail = [...last, ...utf8Bytes(`\r\n--${boundary}--\r\n`)];
314
+ return base64Encode(prefix) + body + base64Encode(tail);
315
+ }
316
+
317
+ /** Human file size: 0 B, 12 KB, 3.4 MB. */
318
+ export function formatSize(bytes: number): string {
319
+ if (!Number.isFinite(bytes) || bytes < 0) return "";
320
+ if (bytes < 1024) return `${bytes} B`;
321
+ const units = ["KB", "MB", "GB", "TB"];
322
+ let value = bytes / 1024;
323
+ let unit = 0;
324
+ while (value >= 1024 && unit < units.length - 1) { value /= 1024; unit += 1; }
325
+ return `${value >= 10 || Number.isInteger(value) ? Math.round(value) : value.toFixed(1)} ${units[unit]}`;
326
+ }
327
+
328
+ export function sizeOf(item: WixLibraryItem): number {
329
+ const raw = item.fileFields?.sizeInBytes ?? item.fileFields?.size;
330
+ const n = typeof raw === "string" ? Number(raw) : raw;
331
+ return typeof n === "number" && Number.isFinite(n) && n >= 0 ? n : -1;
332
+ }