@ibanzajoe/uploader 0.3.0 β 1.2.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/README.md +46 -21
- package/dist/{chunk-EM6NZZSU.js β chunk-6AJTZNOI.js} +152 -51
- package/dist/chunk-6AJTZNOI.js.map +1 -0
- package/dist/chunk-DJK2SQ5I.js +56 -0
- package/dist/chunk-DJK2SQ5I.js.map +1 -0
- package/dist/client-CUmYuQ7Z.d.cts +154 -0
- package/dist/client-CUmYuQ7Z.d.ts +154 -0
- package/dist/core.cjs +158 -3
- package/dist/core.cjs.map +1 -1
- package/dist/core.d.cts +2 -1
- package/dist/core.d.ts +2 -1
- package/dist/core.js +8 -4
- package/dist/index.cjs +158 -3
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -2
- package/dist/index.d.ts +3 -2
- package/dist/index.js +8 -4
- package/dist/index.js.map +1 -1
- package/dist/server.cjs +79 -0
- package/dist/server.cjs.map +1 -0
- package/dist/server.d.cts +54 -0
- package/dist/server.d.ts +54 -0
- package/dist/server.js +26 -0
- package/dist/server.js.map +1 -0
- package/dist/styles.css +2 -2
- package/dist/transform-BTZ0kodO.d.cts +130 -0
- package/dist/transform-BTZ0kodO.d.ts +130 -0
- package/package.json +12 -2
- package/dist/chunk-EM6NZZSU.js.map +0 -1
- package/dist/transform-ybCOCWBG.d.cts +0 -225
- package/dist/transform-ybCOCWBG.d.ts +0 -225
package/README.md
CHANGED
|
@@ -1,26 +1,38 @@
|
|
|
1
|
-
# @uploader
|
|
1
|
+
# @ibanzajoe/uploader
|
|
2
2
|
|
|
3
3
|
React picker and headless upload client for the Uploader platform.
|
|
4
4
|
|
|
5
|
+
> π **Full developer guide:** [docs/14 β SDK Developer Guide](https://github.com/ibanzajoe/file-uploader/blob/main/docs/14-sdk-developer-guide.md)
|
|
6
|
+
> β install, the headless client, delivery/transform URLs, public/hotlink/signed
|
|
7
|
+
> protection, per-file protection, image editing, error handling, and an
|
|
8
|
+
> end-to-end example.
|
|
9
|
+
|
|
5
10
|
## Installation
|
|
6
11
|
|
|
7
12
|
```bash
|
|
8
|
-
npm install @uploader
|
|
13
|
+
npm install @ibanzajoe/uploader
|
|
9
14
|
```
|
|
10
15
|
|
|
11
16
|
## Quick start β React picker
|
|
12
17
|
|
|
13
18
|
```tsx
|
|
14
|
-
import {
|
|
15
|
-
import '@uploader
|
|
19
|
+
import { useState } from 'react'
|
|
20
|
+
import { PickerOverlay } from '@ibanzajoe/uploader'
|
|
21
|
+
import '@ibanzajoe/uploader/styles.css'
|
|
16
22
|
|
|
17
23
|
function App() {
|
|
24
|
+
const [open, setOpen] = useState(false)
|
|
18
25
|
return (
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
26
|
+
<>
|
|
27
|
+
<button onClick={() => setOpen(true)}>Upload</button>
|
|
28
|
+
<PickerOverlay
|
|
29
|
+
apikey="pk_your_api_key"
|
|
30
|
+
apiUrl="https://your-api.example.com"
|
|
31
|
+
open={open}
|
|
32
|
+
onClose={() => setOpen(false)}
|
|
33
|
+
onUploadDone={(res) => console.log(res.filesUploaded)}
|
|
34
|
+
/>
|
|
35
|
+
</>
|
|
24
36
|
)
|
|
25
37
|
}
|
|
26
38
|
```
|
|
@@ -28,10 +40,10 @@ function App() {
|
|
|
28
40
|
## Headless client (no React required)
|
|
29
41
|
|
|
30
42
|
```ts
|
|
31
|
-
import { UploaderClient } from '@uploader/
|
|
43
|
+
import { UploaderClient } from '@ibanzajoe/uploader/core'
|
|
32
44
|
|
|
33
45
|
const client = new UploaderClient({
|
|
34
|
-
|
|
46
|
+
apikey: 'pk_your_api_key',
|
|
35
47
|
apiUrl: 'https://your-api.example.com',
|
|
36
48
|
})
|
|
37
49
|
|
|
@@ -50,11 +62,13 @@ Full-screen modal picker with drag-and-drop, progress, and error UI.
|
|
|
50
62
|
|
|
51
63
|
| Prop | Type | Description |
|
|
52
64
|
|---|---|---|
|
|
53
|
-
| `
|
|
65
|
+
| `apikey` | `string` | API key (`pk_β¦`). **Required.** |
|
|
54
66
|
| `apiUrl` | `string` | Base URL of the API server |
|
|
67
|
+
| `open` | `boolean` | Whether the modal is open. **Required.** |
|
|
68
|
+
| `onClose` | `() => void` | Called when the modal should close (ESC, backdrop, cancel) |
|
|
55
69
|
| `onUploadDone` | `(res: PickerResponse) => void` | Called when all uploads complete |
|
|
56
|
-
| `
|
|
57
|
-
| `
|
|
70
|
+
| `pickerOptions` | `PickerOptions` | File constraints + sources β `{ accept?: string[], maxFiles?, maxSize?, fromSources?, cameraFacingMode? }` |
|
|
71
|
+
| `theme` | `UploaderTheme` | Per-instance design tokens (see Theming) |
|
|
58
72
|
|
|
59
73
|
### `<DropPane>`
|
|
60
74
|
|
|
@@ -79,8 +93,8 @@ leaks to the rest of your app. Only the keys you set change; everything else
|
|
|
79
93
|
falls back to the shipped defaults.
|
|
80
94
|
|
|
81
95
|
```tsx
|
|
82
|
-
import { PickerOverlay } from '@uploader
|
|
83
|
-
import '@uploader/
|
|
96
|
+
import { PickerOverlay } from '@ibanzajoe/uploader'
|
|
97
|
+
import '@ibanzajoe/uploader/styles.css'
|
|
84
98
|
|
|
85
99
|
<PickerOverlay
|
|
86
100
|
apikey="pk_β¦"
|
|
@@ -149,16 +163,27 @@ Notes:
|
|
|
149
163
|
|
|
150
164
|
## Signed policies
|
|
151
165
|
|
|
152
|
-
When the account
|
|
166
|
+
When the account serves files in **signed** delivery mode (or requires signed
|
|
167
|
+
uploads), attach a signed policy at the **client** level β it rides as auth on
|
|
168
|
+
every request from that client:
|
|
153
169
|
|
|
154
170
|
```ts
|
|
155
|
-
const
|
|
156
|
-
|
|
157
|
-
|
|
171
|
+
const client = new UploaderClient({
|
|
172
|
+
apikey: 'pk_β¦',
|
|
173
|
+
apiUrl: 'https://your-api.example.com',
|
|
174
|
+
security: {
|
|
175
|
+
policy: 'base64-encoded-policy', // from YOUR backend
|
|
176
|
+
signature: 'hmac-sha256-hex-signature', // from YOUR backend
|
|
177
|
+
},
|
|
158
178
|
})
|
|
159
179
|
```
|
|
160
180
|
|
|
161
|
-
|
|
181
|
+
For building **read** URLs of signed files, use `withSignedPolicy(url, { policy,
|
|
182
|
+
signature })`. The `policy`/`signature` pair is always produced **server-side** by
|
|
183
|
+
your backend (HMAC over the API key's secret) β the SDK never signs and never sees
|
|
184
|
+
the secret. Compute it yourself, or use the dev helper
|
|
185
|
+
`POST /api/admin/api-keys/:id/sign`. Full recipe + example:
|
|
186
|
+
[docs/14 Β§7.3](https://github.com/ibanzajoe/file-uploader/blob/main/docs/14-sdk-developer-guide.md#73-signed-urls-the-private-tier).
|
|
162
187
|
|
|
163
188
|
## Building
|
|
164
189
|
|
|
@@ -30,6 +30,8 @@ function planChunks(file, chunkSize = DEFAULT_CHUNK_SIZE) {
|
|
|
30
30
|
// src/core/client.ts
|
|
31
31
|
var MAX_RETRIES = 3;
|
|
32
32
|
var RETRY_BASE_MS = 200;
|
|
33
|
+
var MAX_DIRECT_PUT_BYTES = 5 * 1024 * 1024 * 1024;
|
|
34
|
+
var SHA256_MAX_BYTES = 64 * 1024 * 1024;
|
|
33
35
|
function sleep(ms, signal) {
|
|
34
36
|
return new Promise((resolve, reject) => {
|
|
35
37
|
if (signal?.aborted) {
|
|
@@ -91,14 +93,153 @@ async function fetchWithRetry(url, init, signal, maxRetries = MAX_RETRIES) {
|
|
|
91
93
|
}
|
|
92
94
|
throw lastErr;
|
|
93
95
|
}
|
|
96
|
+
async function sha256Hex(blob) {
|
|
97
|
+
try {
|
|
98
|
+
const c = globalThis.crypto;
|
|
99
|
+
if (!c?.subtle || blob.size > SHA256_MAX_BYTES) return void 0;
|
|
100
|
+
const digest = await c.subtle.digest("SHA-256", await blob.arrayBuffer());
|
|
101
|
+
return Array.from(new Uint8Array(digest)).map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
102
|
+
} catch {
|
|
103
|
+
return void 0;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
function xhrPut(url, body, opts) {
|
|
107
|
+
return new Promise((resolve, reject) => {
|
|
108
|
+
if (opts.signal?.aborted) {
|
|
109
|
+
reject(new UploaderError("ABORTED", "Upload aborted"));
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
const xhr = new XMLHttpRequest();
|
|
113
|
+
xhr.open("PUT", url);
|
|
114
|
+
xhr.setRequestHeader("Content-Type", opts.contentType);
|
|
115
|
+
if (opts.onProgress) {
|
|
116
|
+
xhr.upload.onprogress = (e) => {
|
|
117
|
+
if (e.lengthComputable) {
|
|
118
|
+
opts.onProgress(Math.round(e.loaded / e.total * 95));
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
xhr.onload = () => {
|
|
123
|
+
if (xhr.status >= 200 && xhr.status < 300) {
|
|
124
|
+
resolve();
|
|
125
|
+
} else {
|
|
126
|
+
const code = xhr.status >= 400 && xhr.status < 500 ? "CLIENT_ERROR" : "SERVER_ERROR";
|
|
127
|
+
reject(new UploaderError(code, `Bucket PUT failed: HTTP ${xhr.status}`, xhr.status));
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
xhr.onerror = () => reject(new UploaderError("NETWORK_ERROR", "Bucket PUT network error"));
|
|
131
|
+
xhr.onabort = () => reject(new UploaderError("ABORTED", "Upload aborted"));
|
|
132
|
+
if (opts.signal) {
|
|
133
|
+
opts.signal.addEventListener("abort", () => xhr.abort(), { once: true });
|
|
134
|
+
}
|
|
135
|
+
xhr.send(body);
|
|
136
|
+
});
|
|
137
|
+
}
|
|
94
138
|
var UploaderClient = class {
|
|
95
139
|
apikey;
|
|
96
140
|
apiUrl;
|
|
97
141
|
security;
|
|
142
|
+
#directUploadOption;
|
|
143
|
+
/** Client-level default per-file delivery protection (docs/13). */
|
|
144
|
+
#deliveryProtection;
|
|
145
|
+
/** Client-level default per-file allowed origins (docs/13). */
|
|
146
|
+
#allowedOrigins;
|
|
147
|
+
/** Memoized capability probe β one request per client, shared across uploads. */
|
|
148
|
+
#capsPromise = null;
|
|
98
149
|
constructor(options) {
|
|
99
150
|
this.apikey = options.apikey;
|
|
100
151
|
this.apiUrl = (options.apiUrl ?? "https://api.uploaderhq.io").replace(/\/$/, "");
|
|
101
152
|
this.security = options.security;
|
|
153
|
+
this.#directUploadOption = options.directUpload;
|
|
154
|
+
this.#deliveryProtection = options.deliveryProtection;
|
|
155
|
+
this.#allowedOrigins = options.allowedOrigins;
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Resolve the effective per-file protection for one upload: a per-upload value
|
|
159
|
+
* overrides the client-level default (docs/13). Returns an object carrying ONLY
|
|
160
|
+
* the keys that are set, so callers spread it into the request body and unset
|
|
161
|
+
* fields are omitted entirely β an old server ignores them and the file inherits
|
|
162
|
+
* the account mode (`null`).
|
|
163
|
+
*/
|
|
164
|
+
#resolveProtection(opts) {
|
|
165
|
+
const out = {};
|
|
166
|
+
const mode = opts.deliveryProtection ?? this.#deliveryProtection;
|
|
167
|
+
if (mode !== void 0) out.deliveryProtection = mode;
|
|
168
|
+
const origins = opts.allowedOrigins ?? this.#allowedOrigins;
|
|
169
|
+
if (origins !== void 0) out.allowedOrigins = origins;
|
|
170
|
+
return out;
|
|
171
|
+
}
|
|
172
|
+
// βββ Capability negotiation βββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
173
|
+
/**
|
|
174
|
+
* Probe GET /api/capabilities once per client and cache the result. Fails OPEN
|
|
175
|
+
* to the proxied flow ({ directUpload: false }) on any error/timeout, so a
|
|
176
|
+
* flaky probe never blocks uploads and old servers (404) are handled.
|
|
177
|
+
*/
|
|
178
|
+
#getCapabilities() {
|
|
179
|
+
if (this.#directUploadOption === false) {
|
|
180
|
+
return Promise.resolve({ directUpload: false });
|
|
181
|
+
}
|
|
182
|
+
if (!this.#capsPromise) {
|
|
183
|
+
this.#capsPromise = fetch(`${this.apiUrl}/api/capabilities`, {
|
|
184
|
+
headers: this.#authHeaders()
|
|
185
|
+
}).then(async (res) => {
|
|
186
|
+
if (!res.ok) return { directUpload: false };
|
|
187
|
+
const body = await res.json().catch(() => ({}));
|
|
188
|
+
return { directUpload: body["directUpload"] === true };
|
|
189
|
+
}).catch(() => ({ directUpload: false }));
|
|
190
|
+
}
|
|
191
|
+
return this.#capsPromise;
|
|
192
|
+
}
|
|
193
|
+
// βββ Direct-to-bucket upload ββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
194
|
+
/**
|
|
195
|
+
* Presign β PUT-to-bucket β confirm. Bytes go browser β bucket directly; our
|
|
196
|
+
* server only signs and records. Used when the account has the directUpload
|
|
197
|
+
* capability and the file fits a single PUT.
|
|
198
|
+
*/
|
|
199
|
+
async #uploadDirect(file, opts) {
|
|
200
|
+
const { onProgress, filename, signal } = opts;
|
|
201
|
+
const name = filename ?? (file instanceof File ? file.name : "upload");
|
|
202
|
+
const contentType = file instanceof File && file.type ? file.type : "application/octet-stream";
|
|
203
|
+
onProgress?.(0);
|
|
204
|
+
checkAbort(signal);
|
|
205
|
+
const protection = this.#resolveProtection(opts);
|
|
206
|
+
const presign = async () => {
|
|
207
|
+
const res = await fetchWithRetry(
|
|
208
|
+
`${this.apiUrl}/api/uploads/presign`,
|
|
209
|
+
{
|
|
210
|
+
method: "POST",
|
|
211
|
+
headers: { ...this.#authHeaders(), "Content-Type": "application/json" },
|
|
212
|
+
body: JSON.stringify({ filename: name, contentType, size: file.size, ...protection })
|
|
213
|
+
},
|
|
214
|
+
signal
|
|
215
|
+
);
|
|
216
|
+
return await res.json();
|
|
217
|
+
};
|
|
218
|
+
let signed = await presign();
|
|
219
|
+
const checksum = await sha256Hex(file);
|
|
220
|
+
try {
|
|
221
|
+
await xhrPut(signed.putUrl, file, { contentType: signed.contentType, onProgress, signal });
|
|
222
|
+
} catch (err) {
|
|
223
|
+
if (err instanceof UploaderError && err.statusCode === 403) {
|
|
224
|
+
signed = await presign();
|
|
225
|
+
await xhrPut(signed.putUrl, file, { contentType: signed.contentType, onProgress, signal });
|
|
226
|
+
} else {
|
|
227
|
+
throw err;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
checkAbort(signal);
|
|
231
|
+
const confirmRes = await fetchWithRetry(
|
|
232
|
+
`${this.apiUrl}/api/uploads/confirm`,
|
|
233
|
+
{
|
|
234
|
+
method: "POST",
|
|
235
|
+
headers: { ...this.#authHeaders(), "Content-Type": "application/json" },
|
|
236
|
+
body: JSON.stringify({ handle: signed.handle, checksum })
|
|
237
|
+
},
|
|
238
|
+
signal
|
|
239
|
+
);
|
|
240
|
+
const body = await confirmRes.json();
|
|
241
|
+
onProgress?.(100);
|
|
242
|
+
return this.#parseFileResult(body);
|
|
102
243
|
}
|
|
103
244
|
// βββ Auth headers ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
104
245
|
/**
|
|
@@ -126,6 +267,9 @@ var UploaderClient = class {
|
|
|
126
267
|
const form = new FormData();
|
|
127
268
|
form.append("file", file, filename ?? (file instanceof File ? file.name : "upload"));
|
|
128
269
|
if (filename) form.append("filename", filename);
|
|
270
|
+
const protection = this.#resolveProtection(opts);
|
|
271
|
+
if (protection.deliveryProtection) form.append("deliveryProtection", protection.deliveryProtection);
|
|
272
|
+
if (protection.allowedOrigins) form.append("allowedOrigins", JSON.stringify(protection.allowedOrigins));
|
|
129
273
|
const res = await fetchWithRetry(
|
|
130
274
|
`${this.apiUrl}/api/store`,
|
|
131
275
|
{
|
|
@@ -149,12 +293,13 @@ var UploaderClient = class {
|
|
|
149
293
|
const mime = file instanceof File ? file.type : "application/octet-stream";
|
|
150
294
|
onProgress?.(0);
|
|
151
295
|
checkAbort(signal);
|
|
296
|
+
const protection = this.#resolveProtection(opts);
|
|
152
297
|
const startRes = await fetchWithRetry(
|
|
153
298
|
`${this.apiUrl}/api/upload/start`,
|
|
154
299
|
{
|
|
155
300
|
method: "POST",
|
|
156
301
|
headers: { ...this.#authHeaders(), "Content-Type": "application/json" },
|
|
157
|
-
body: JSON.stringify({ filename: name, mimetype: mime, size: file.size })
|
|
302
|
+
body: JSON.stringify({ filename: name, mimetype: mime, size: file.size, ...protection })
|
|
158
303
|
},
|
|
159
304
|
signal
|
|
160
305
|
);
|
|
@@ -217,6 +362,10 @@ var UploaderClient = class {
|
|
|
217
362
|
* CLIENT_ERROR | INVALID_RESPONSE
|
|
218
363
|
*/
|
|
219
364
|
async upload(file, opts = {}) {
|
|
365
|
+
const caps = await this.#getCapabilities();
|
|
366
|
+
if (caps.directUpload && file.size <= MAX_DIRECT_PUT_BYTES) {
|
|
367
|
+
return this.#uploadDirect(file, opts);
|
|
368
|
+
}
|
|
220
369
|
const plan = planChunks(file, opts.chunkSize);
|
|
221
370
|
if (plan.mode === "single") {
|
|
222
371
|
return this.#uploadSingleShot(file, opts);
|
|
@@ -257,59 +406,11 @@ var UploaderClient = class {
|
|
|
257
406
|
}
|
|
258
407
|
};
|
|
259
408
|
|
|
260
|
-
// src/core/transform.ts
|
|
261
|
-
var resize = (params) => ({ name: "resize", params });
|
|
262
|
-
var crop = (rect) => ({
|
|
263
|
-
name: "crop",
|
|
264
|
-
params: { dim: `${rect.x},${rect.y},${rect.w},${rect.h}`, ...rect }
|
|
265
|
-
});
|
|
266
|
-
var rotate = (params) => ({ name: "rotate", params });
|
|
267
|
-
var flip = () => ({ name: "flip", params: {} });
|
|
268
|
-
var flop = () => ({ name: "flop", params: {} });
|
|
269
|
-
var quality = (params) => ({ name: "quality", params });
|
|
270
|
-
var output = (params) => ({ name: "output", params });
|
|
271
|
-
function serializeOp(op) {
|
|
272
|
-
switch (op.name) {
|
|
273
|
-
case "resize": {
|
|
274
|
-
const parts = [];
|
|
275
|
-
if (op.params.w !== void 0) parts.push(`w:${op.params.w}`);
|
|
276
|
-
if (op.params.h !== void 0) parts.push(`h:${op.params.h}`);
|
|
277
|
-
if (op.params.fit !== void 0) parts.push(`fit:${op.params.fit}`);
|
|
278
|
-
return `resize=${parts.join(",")}`;
|
|
279
|
-
}
|
|
280
|
-
case "crop":
|
|
281
|
-
return `crop=dim:${op.params.dim}`;
|
|
282
|
-
case "rotate":
|
|
283
|
-
return `rotate=deg:${op.params.deg}`;
|
|
284
|
-
case "flip":
|
|
285
|
-
return "flip";
|
|
286
|
-
case "flop":
|
|
287
|
-
return "flop";
|
|
288
|
-
case "quality":
|
|
289
|
-
return `quality=n:${op.params.n}`;
|
|
290
|
-
case "output":
|
|
291
|
-
return `output=format:${op.params.format}`;
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
|
-
function transformUrl({ handle, ops, apiUrl = "" }) {
|
|
295
|
-
const base = apiUrl.replace(/\/$/, "");
|
|
296
|
-
const chain = ops.map(serializeOp).join("/");
|
|
297
|
-
return chain ? `${base}/${chain}/${handle}` : `${base}/${handle}`;
|
|
298
|
-
}
|
|
299
|
-
|
|
300
409
|
export {
|
|
301
410
|
UploaderError,
|
|
302
411
|
MULTIPART_THRESHOLD,
|
|
303
412
|
DEFAULT_CHUNK_SIZE,
|
|
304
413
|
planChunks,
|
|
305
|
-
UploaderClient
|
|
306
|
-
resize,
|
|
307
|
-
crop,
|
|
308
|
-
rotate,
|
|
309
|
-
flip,
|
|
310
|
-
flop,
|
|
311
|
-
quality,
|
|
312
|
-
output,
|
|
313
|
-
transformUrl
|
|
414
|
+
UploaderClient
|
|
314
415
|
};
|
|
315
|
-
//# sourceMappingURL=chunk-
|
|
416
|
+
//# sourceMappingURL=chunk-6AJTZNOI.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/core/errors.ts","../src/core/chunk.ts","../src/core/client.ts"],"sourcesContent":["/**\n * Typed errors for UploaderClient.\n *\n * UploaderError is thrown by upload / uploadAll on any non-retried failure.\n * The caller can narrow on `err.code` for structured handling.\n */\n\nexport type UploaderErrorCode =\n | 'ABORTED' // AbortSignal fired\n | 'NETWORK_ERROR' // fetch() threw (no response)\n | 'SERVER_ERROR' // 5xx after all retries exhausted\n | 'CLIENT_ERROR' // 4xx (not retried)\n | 'INVALID_RESPONSE' // response body did not match expected shape\n\nexport class UploaderError extends Error {\n readonly code: UploaderErrorCode\n /** HTTP status code when available (undefined for ABORTED / NETWORK_ERROR). */\n readonly statusCode?: number\n\n constructor(code: UploaderErrorCode, message: string, statusCode?: number) {\n super(message)\n this.name = 'UploaderError'\n this.code = code\n this.statusCode = statusCode\n }\n}\n","/**\n * Chunk planner for multipart uploads.\n *\n * Decides single-shot vs multipart by comparing the file size against\n * MULTIPART_THRESHOLD. For multipart files, slices the Blob into parts of\n * `chunkSize` bytes.\n */\n\n/** Files β€ this size use single-shot POST /api/store. */\nexport const MULTIPART_THRESHOLD = 5 * 1024 * 1024 // 5 MB\n\n/** Default part size for multipart uploads. */\nexport const DEFAULT_CHUNK_SIZE = 5 * 1024 * 1024 // 5 MB\n\nexport type ChunkPlan =\n | { mode: 'single' }\n | { mode: 'multipart'; parts: Blob[]; partSize: number }\n\n/**\n * Build a chunk plan for a file.\n *\n * @param file The File or Blob to upload.\n * @param chunkSize Desired part size in bytes (default DEFAULT_CHUNK_SIZE).\n * @returns A plan describing whether to use single-shot or multipart.\n */\nexport function planChunks(file: File | Blob, chunkSize = DEFAULT_CHUNK_SIZE): ChunkPlan {\n if (file.size <= MULTIPART_THRESHOLD) {\n return { mode: 'single' }\n }\n\n const parts: Blob[] = []\n let offset = 0\n while (offset < file.size) {\n parts.push(file.slice(offset, offset + chunkSize))\n offset += chunkSize\n }\n return { mode: 'multipart', parts, partSize: chunkSize }\n}\n","/**\n * UploaderClient β headless upload client.\n *\n * Chooses single-shot (POST /api/store, multipart/form-data) vs multipart\n * (start/part/complete) based on file size relative to MULTIPART_THRESHOLD\n * (5 MB). Parts are retried individually with exponential backoff. Progress\n * is emitted as a 0β100 integer. AbortSignal cancels in-flight work.\n */\n\nimport type {\n FileResult,\n UploadStartResponse,\n UploadPartResponse,\n UploaderClientOptions,\n UploadAllOptions,\n UploadOptions,\n} from './types.js'\nimport { UploaderError } from './errors.js'\nimport { planChunks } from './chunk.js'\n\n// βββ Constants ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ\n\nconst MAX_RETRIES = 3\nconst RETRY_BASE_MS = 200\n\n/**\n * Single presigned PUT ceiling (mirrors the server's Phase-A limit). Files above\n * this fall back to the proxied multipart flow.\n */\nconst MAX_DIRECT_PUT_BYTES = 5 * 1024 * 1024 * 1024 // 5 GB\n\n/**\n * Skip the advisory client checksum above this size β SubtleCrypto has no\n * streaming API, so hashing would load the whole file into memory a second time.\n * The checksum is advisory only (the server can't verify it anyway).\n */\nconst SHA256_MAX_BYTES = 64 * 1024 * 1024 // 64 MB\n\n// βββ Internal helpers βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ\n\n/** Sleep for `ms` milliseconds, resolving early if signal fires. */\nfunction sleep(ms: number, signal?: AbortSignal): Promise<void> {\n return new Promise<void>((resolve, reject) => {\n if (signal?.aborted) {\n reject(new UploaderError('ABORTED', 'Upload aborted'))\n return\n }\n const timer = setTimeout(resolve, ms)\n signal?.addEventListener('abort', () => {\n clearTimeout(timer)\n reject(new UploaderError('ABORTED', 'Upload aborted'))\n }, { once: true })\n })\n}\n\n/** Throw UploaderError(ABORTED) if signal has already fired. */\nfunction checkAbort(signal?: AbortSignal): void {\n if (signal?.aborted) {\n throw new UploaderError('ABORTED', 'Upload aborted')\n }\n}\n\n/**\n * Fetch with retry on network errors and 5xx responses.\n * 4xx responses are not retried β they surface immediately as CLIENT_ERROR.\n */\nasync function fetchWithRetry(\n url: string,\n init: RequestInit,\n signal?: AbortSignal,\n maxRetries = MAX_RETRIES,\n): Promise<Response> {\n let lastErr: unknown\n for (let attempt = 0; attempt < maxRetries; attempt++) {\n checkAbort(signal)\n try {\n const res = await fetch(url, { ...init, signal })\n if (res.status >= 400 && res.status < 500) {\n // Client error β do not retry\n const body = await res.text().catch(() => '')\n throw new UploaderError(\n 'CLIENT_ERROR',\n `HTTP ${res.status}: ${body}`,\n res.status,\n )\n }\n if (res.status >= 500) {\n // Server error β retry with backoff\n lastErr = new UploaderError(\n 'SERVER_ERROR',\n `HTTP ${res.status}`,\n res.status,\n )\n if (attempt < maxRetries - 1) {\n await sleep(RETRY_BASE_MS * 2 ** attempt, signal)\n }\n continue\n }\n return res\n } catch (err) {\n if (err instanceof UploaderError) {\n if (err.code === 'CLIENT_ERROR' || err.code === 'ABORTED') throw err\n lastErr = err\n } else {\n // fetch() threw (network failure, CORS, etc.)\n lastErr = new UploaderError(\n 'NETWORK_ERROR',\n err instanceof Error ? err.message : String(err),\n )\n }\n if (attempt < maxRetries - 1) {\n await sleep(RETRY_BASE_MS * 2 ** attempt, signal)\n }\n }\n }\n throw lastErr\n}\n\n/**\n * Best-effort SHA-256 (hex) of a Blob for the advisory checksum sent at confirm.\n * Returns undefined when SubtleCrypto is unavailable (insecure context / older\n * runtime) or the file is large β never throws. The server treats this as a hint\n * only, so skipping it is safe.\n */\nasync function sha256Hex(blob: Blob): Promise<string | undefined> {\n try {\n const c = (globalThis as { crypto?: Crypto }).crypto\n if (!c?.subtle || blob.size > SHA256_MAX_BYTES) return undefined\n const digest = await c.subtle.digest('SHA-256', await blob.arrayBuffer())\n return Array.from(new Uint8Array(digest))\n .map((b) => b.toString(16).padStart(2, '0'))\n .join('')\n } catch {\n return undefined\n }\n}\n\n/**\n * PUT a body straight to a presigned bucket URL via XMLHttpRequest.\n *\n * XHR (not fetch) so real upload progress is reported (fetch cannot). The\n * Content-Type header MUST equal the value the server signed, or S3/R2 reject\n * the signature. Bytes go browser β bucket; our server never sees them.\n */\nfunction xhrPut(\n url: string,\n body: Blob,\n opts: { contentType: string; onProgress?: (percent: number) => void; signal?: AbortSignal },\n): Promise<void> {\n return new Promise<void>((resolve, reject) => {\n if (opts.signal?.aborted) {\n reject(new UploaderError('ABORTED', 'Upload aborted'))\n return\n }\n const xhr = new XMLHttpRequest()\n xhr.open('PUT', url)\n xhr.setRequestHeader('Content-Type', opts.contentType)\n\n if (opts.onProgress) {\n xhr.upload.onprogress = (e: ProgressEvent) => {\n if (e.lengthComputable) {\n // Reserve the last 5% for the confirm round-trip.\n opts.onProgress!(Math.round((e.loaded / e.total) * 95))\n }\n }\n }\n xhr.onload = () => {\n if (xhr.status >= 200 && xhr.status < 300) {\n resolve()\n } else {\n const code = xhr.status >= 400 && xhr.status < 500 ? 'CLIENT_ERROR' : 'SERVER_ERROR'\n reject(new UploaderError(code, `Bucket PUT failed: HTTP ${xhr.status}`, xhr.status))\n }\n }\n xhr.onerror = () => reject(new UploaderError('NETWORK_ERROR', 'Bucket PUT network error'))\n xhr.onabort = () => reject(new UploaderError('ABORTED', 'Upload aborted'))\n\n if (opts.signal) {\n opts.signal.addEventListener('abort', () => xhr.abort(), { once: true })\n }\n xhr.send(body)\n })\n}\n\n/** Shape returned by POST /api/uploads/presign. */\ntype PresignResponse = { handle: string; storageKey: string; putUrl: string; contentType: string }\n\n// βββ UploaderClient ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ\n\nexport class UploaderClient {\n readonly apikey: string\n readonly apiUrl: string\n readonly security: UploaderClientOptions['security']\n readonly #directUploadOption?: boolean\n /** Client-level default per-file delivery protection (docs/13). */\n readonly #deliveryProtection?: UploaderClientOptions['deliveryProtection']\n /** Client-level default per-file allowed origins (docs/13). */\n readonly #allowedOrigins?: string[]\n\n /** Memoized capability probe β one request per client, shared across uploads. */\n #capsPromise: Promise<{ directUpload: boolean }> | null = null\n\n constructor(options: UploaderClientOptions) {\n this.apikey = options.apikey\n this.apiUrl = (options.apiUrl ?? 'https://api.uploaderhq.io').replace(/\\/$/, '')\n this.security = options.security\n this.#directUploadOption = options.directUpload\n this.#deliveryProtection = options.deliveryProtection\n this.#allowedOrigins = options.allowedOrigins\n }\n\n /**\n * Resolve the effective per-file protection for one upload: a per-upload value\n * overrides the client-level default (docs/13). Returns an object carrying ONLY\n * the keys that are set, so callers spread it into the request body and unset\n * fields are omitted entirely β an old server ignores them and the file inherits\n * the account mode (`null`).\n */\n #resolveProtection(opts: UploadOptions): {\n deliveryProtection?: UploaderClientOptions['deliveryProtection']\n allowedOrigins?: string[]\n } {\n const out: { deliveryProtection?: UploaderClientOptions['deliveryProtection']; allowedOrigins?: string[] } = {}\n const mode = opts.deliveryProtection ?? this.#deliveryProtection\n if (mode !== undefined) out.deliveryProtection = mode\n const origins = opts.allowedOrigins ?? this.#allowedOrigins\n if (origins !== undefined) out.allowedOrigins = origins\n return out\n }\n\n // βββ Capability negotiation βββββββββββββββββββββββββββββββββββββββββββββββββ\n\n /**\n * Probe GET /api/capabilities once per client and cache the result. Fails OPEN\n * to the proxied flow ({ directUpload: false }) on any error/timeout, so a\n * flaky probe never blocks uploads and old servers (404) are handled.\n */\n #getCapabilities(): Promise<{ directUpload: boolean }> {\n // Explicit opt-out β never probe, never use direct upload.\n if (this.#directUploadOption === false) {\n return Promise.resolve({ directUpload: false })\n }\n if (!this.#capsPromise) {\n this.#capsPromise = fetch(`${this.apiUrl}/api/capabilities`, {\n headers: this.#authHeaders(),\n })\n .then(async (res) => {\n if (!res.ok) return { directUpload: false }\n const body = (await res.json().catch(() => ({}))) as Record<string, unknown>\n return { directUpload: body['directUpload'] === true }\n })\n .catch(() => ({ directUpload: false }))\n }\n return this.#capsPromise\n }\n\n // βββ Direct-to-bucket upload ββββββββββββββββββββββββββββββββββββββββββββββββ\n\n /**\n * Presign β PUT-to-bucket β confirm. Bytes go browser β bucket directly; our\n * server only signs and records. Used when the account has the directUpload\n * capability and the file fits a single PUT.\n */\n async #uploadDirect(file: File | Blob, opts: UploadOptions): Promise<FileResult> {\n const { onProgress, filename, signal } = opts\n const name = filename ?? (file instanceof File ? file.name : 'upload')\n // Must match the Content-Type we send on the PUT (the server signs it).\n const contentType = file instanceof File && file.type ? file.type : 'application/octet-stream'\n\n onProgress?.(0)\n checkAbort(signal)\n\n const protection = this.#resolveProtection(opts)\n const presign = async (): Promise<PresignResponse> => {\n const res = await fetchWithRetry(\n `${this.apiUrl}/api/uploads/presign`,\n {\n method: 'POST',\n headers: { ...this.#authHeaders(), 'Content-Type': 'application/json' },\n body: JSON.stringify({ filename: name, contentType, size: file.size, ...protection }),\n },\n signal,\n )\n return (await res.json()) as PresignResponse\n }\n\n let signed = await presign()\n\n // Advisory checksum (best-effort; unverifiable server-side).\n const checksum = await sha256Hex(file)\n\n // PUT straight to the bucket. A 403 means the short-lived signature lapsed\n // (or a stale key) β re-presign once and retry before giving up.\n try {\n await xhrPut(signed.putUrl, file, { contentType: signed.contentType, onProgress, signal })\n } catch (err) {\n if (err instanceof UploaderError && err.statusCode === 403) {\n signed = await presign()\n await xhrPut(signed.putUrl, file, { contentType: signed.contentType, onProgress, signal })\n } else {\n throw err\n }\n }\n\n // Confirm β server verifies the object exists, records usage, enqueues.\n checkAbort(signal)\n const confirmRes = await fetchWithRetry(\n `${this.apiUrl}/api/uploads/confirm`,\n {\n method: 'POST',\n headers: { ...this.#authHeaders(), 'Content-Type': 'application/json' },\n body: JSON.stringify({ handle: signed.handle, checksum }),\n },\n signal,\n )\n const body = (await confirmRes.json()) as unknown\n onProgress?.(100)\n return this.#parseFileResult(body)\n }\n\n // βββ Auth headers ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ\n\n /**\n * Build the auth headers shared by all requests.\n * Attaches the API key and, when present, the signed policy pair.\n */\n #authHeaders(): Record<string, string> {\n const headers: Record<string, string> = {\n 'X-Uploader-Key': this.apikey,\n }\n if (this.security) {\n headers['X-Uploader-Policy'] = this.security.policy\n headers['X-Uploader-Signature'] = this.security.signature\n }\n return headers\n }\n\n // βββ Single-shot upload ββββββββββββββββββββββββββββββββββββββββββββββββββββ\n\n /**\n * POST /api/store β multipart/form-data for files β€ MULTIPART_THRESHOLD.\n */\n async #uploadSingleShot(\n file: File | Blob,\n opts: UploadOptions,\n ): Promise<FileResult> {\n const { onProgress, filename, signal } = opts\n\n onProgress?.(0)\n checkAbort(signal)\n\n const form = new FormData()\n form.append('file', file, filename ?? (file instanceof File ? file.name : 'upload'))\n if (filename) form.append('filename', filename)\n // Per-file delivery protection (docs/13) β sent as form fields; allowedOrigins\n // is JSON-encoded to match the server's parse. Omitted when unset (inherit).\n const protection = this.#resolveProtection(opts)\n if (protection.deliveryProtection) form.append('deliveryProtection', protection.deliveryProtection)\n if (protection.allowedOrigins) form.append('allowedOrigins', JSON.stringify(protection.allowedOrigins))\n\n const res = await fetchWithRetry(\n `${this.apiUrl}/api/store`,\n {\n method: 'POST',\n headers: this.#authHeaders(),\n body: form,\n },\n signal,\n )\n\n const body = await res.json() as unknown\n onProgress?.(100)\n return this.#parseFileResult(body)\n }\n\n // βββ Multipart upload ββββββββββββββββββββββββββββββββββββββββββββββββββββββ\n\n /**\n * start β parts (parallel with progress) β complete for files > MULTIPART_THRESHOLD.\n */\n async #uploadMultipart(\n file: File | Blob,\n parts: Blob[],\n opts: UploadOptions,\n ): Promise<FileResult> {\n const { onProgress, filename, signal } = opts\n const name = filename ?? (file instanceof File ? file.name : 'upload')\n const mime = file instanceof File ? file.type : 'application/octet-stream'\n\n onProgress?.(0)\n checkAbort(signal)\n\n // 1. Start\n const protection = this.#resolveProtection(opts)\n const startRes = await fetchWithRetry(\n `${this.apiUrl}/api/upload/start`,\n {\n method: 'POST',\n headers: { ...this.#authHeaders(), 'Content-Type': 'application/json' },\n body: JSON.stringify({ filename: name, mimetype: mime, size: file.size, ...protection }),\n },\n signal,\n )\n const { uploadId } = await startRes.json() as UploadStartResponse\n\n // 2. Upload parts sequentially (retried individually)\n const etags: { partNumber: number; etag: string }[] = []\n let uploadedBytes = 0\n\n for (let i = 0; i < parts.length; i++) {\n checkAbort(signal)\n const partBlob = parts[i]!\n const partNumber = i + 1\n\n const partForm = new FormData()\n partForm.append('uploadId', uploadId)\n partForm.append('partNumber', String(partNumber))\n partForm.append('part', partBlob)\n\n const partRes = await fetchWithRetry(\n `${this.apiUrl}/api/upload/part`,\n {\n method: 'POST',\n headers: this.#authHeaders(),\n body: partForm,\n },\n signal,\n )\n const { etag } = await partRes.json() as UploadPartResponse\n\n etags.push({ partNumber, etag })\n uploadedBytes += partBlob.size\n onProgress?.(Math.round((uploadedBytes / file.size) * 95)) // reserve 5% for complete\n }\n\n // 3. Complete\n checkAbort(signal)\n const completeRes = await fetchWithRetry(\n `${this.apiUrl}/api/upload/complete`,\n {\n method: 'POST',\n headers: { ...this.#authHeaders(), 'Content-Type': 'application/json' },\n body: JSON.stringify({ uploadId, parts: etags }),\n },\n signal,\n )\n const body = await completeRes.json() as unknown\n onProgress?.(100)\n return this.#parseFileResult(body)\n }\n\n // βββ Response parser βββββββββββββββββββββββββββββββββββββββββββββββββββββββ\n\n #parseFileResult(body: unknown): FileResult {\n if (\n typeof body !== 'object' ||\n body === null ||\n typeof (body as Record<string, unknown>)['handle'] !== 'string' ||\n typeof (body as Record<string, unknown>)['url'] !== 'string'\n ) {\n throw new UploaderError('INVALID_RESPONSE', 'Unexpected response shape from upload API')\n }\n return body as FileResult\n }\n\n // βββ Public API βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ\n\n /**\n * Upload a single file.\n *\n * Automatically selects single-shot vs multipart upload based on file size.\n * Emits progress via `opts.onProgress` (0β100). Respects `opts.signal` for\n * cancellation. Retries network errors and 5xx responses up to 3 times with\n * exponential backoff; 4xx errors are surfaced immediately.\n *\n * @throws {UploaderError} with code ABORTED | NETWORK_ERROR | SERVER_ERROR |\n * CLIENT_ERROR | INVALID_RESPONSE\n */\n async upload(\n file: File | Blob,\n opts: UploadOptions = {},\n ): Promise<FileResult> {\n // Prefer direct-to-bucket when the account supports it and the file fits a\n // single PUT. The probe fails open, so an old server or a disabled account\n // transparently uses the proxied flow below.\n const caps = await this.#getCapabilities()\n if (caps.directUpload && file.size <= MAX_DIRECT_PUT_BYTES) {\n return this.#uploadDirect(file, opts)\n }\n\n const plan = planChunks(file, opts.chunkSize)\n if (plan.mode === 'single') {\n return this.#uploadSingleShot(file, opts)\n }\n return this.#uploadMultipart(file, plan.parts, opts)\n }\n\n /**\n * Upload multiple files with concurrency limiting.\n *\n * Resolves once all uploads settle (fulfilled or rejected). The returned\n * array preserves input order. `opts.concurrency` caps simultaneous\n * in-flight uploads (default 3). Each file shares the same opts\n * (including onProgress β the callback fires per-file, not aggregate).\n *\n * @returns Array of PromiseSettledResult in input order.\n */\n async uploadAll(\n files: Array<File | Blob>,\n opts: UploadAllOptions = {},\n ): Promise<PromiseSettledResult<FileResult>[]> {\n const concurrency = opts.concurrency ?? 3\n const results: PromiseSettledResult<FileResult>[] = new Array(files.length)\n\n let index = 0\n\n async function worker(client: UploaderClient): Promise<void> {\n while (index < files.length) {\n const i = index++\n const file = files[i]!\n try {\n results[i] = { status: 'fulfilled', value: await client.upload(file, opts) }\n } catch (err) {\n results[i] = { status: 'rejected', reason: err }\n }\n }\n }\n\n const workers = Array.from({ length: Math.min(concurrency, files.length) }, () =>\n worker(this),\n )\n await Promise.all(workers)\n return results\n }\n}\n"],"mappings":";AAcO,IAAM,gBAAN,cAA4B,MAAM;AAAA,EAC9B;AAAA;AAAA,EAEA;AAAA,EAET,YAAY,MAAyB,SAAiB,YAAqB;AACzE,UAAM,OAAO;AACb,SAAK,OAAO;AACZ,SAAK,OAAO;AACZ,SAAK,aAAa;AAAA,EACpB;AACF;;;AChBO,IAAM,sBAAsB,IAAI,OAAO;AAGvC,IAAM,qBAAqB,IAAI,OAAO;AAatC,SAAS,WAAW,MAAmB,YAAY,oBAA+B;AACvF,MAAI,KAAK,QAAQ,qBAAqB;AACpC,WAAO,EAAE,MAAM,SAAS;AAAA,EAC1B;AAEA,QAAM,QAAgB,CAAC;AACvB,MAAI,SAAS;AACb,SAAO,SAAS,KAAK,MAAM;AACzB,UAAM,KAAK,KAAK,MAAM,QAAQ,SAAS,SAAS,CAAC;AACjD,cAAU;AAAA,EACZ;AACA,SAAO,EAAE,MAAM,aAAa,OAAO,UAAU,UAAU;AACzD;;;ACfA,IAAM,cAAc;AACpB,IAAM,gBAAgB;AAMtB,IAAM,uBAAuB,IAAI,OAAO,OAAO;AAO/C,IAAM,mBAAmB,KAAK,OAAO;AAKrC,SAAS,MAAM,IAAY,QAAqC;AAC9D,S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C;AACA,WAAO,KAAK,iBAAiB,MAAM,KAAK,OAAO,IAAI;AAAA,EACrD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAYA,MAAM,UACJ,OACA,OAAyB,CAAC,GACmB;AAC7C,UAAM,cAAc,KAAK,eAAe;AACxC,UAAM,UAA8C,IAAI,MAAM,MAAM,MAAM;AAE1E,QAAI,QAAQ;AAEZ,mBAAe,OAAO,QAAuC;AAC3D,aAAO,QAAQ,MAAM,QAAQ;AAC3B,cAAM,IAAI;AACV,cAAM,OAAO,MAAM,CAAC;AACpB,YAAI;AACF,kBAAQ,CAAC,IAAI,EAAE,QAAQ,aAAa,OAAO,MAAM,OAAO,OAAO,MAAM,IAAI,EAAE;AAAA,QAC7E,SAAS,KAAK;AACZ,kBAAQ,CAAC,IAAI,EAAE,QAAQ,YAAY,QAAQ,IAAI;AAAA,QACjD;AAAA,MACF;AAAA,IACF;AAEA,UAAM,UAAU,MAAM;AAAA,MAAK,EAAE,QAAQ,KAAK,IAAI,aAAa,MAAM,MAAM,EAAE;AAAA,MAAG,MAC1E,OAAO,IAAI;AAAA,IACb;AACA,UAAM,QAAQ,IAAI,OAAO;AACzB,WAAO;AAAA,EACT;AACF;","names":[]}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
// src/core/transform.ts
|
|
2
|
+
var resize = (params) => ({ name: "resize", params });
|
|
3
|
+
var crop = (rect) => ({
|
|
4
|
+
name: "crop",
|
|
5
|
+
params: { dim: `${rect.x},${rect.y},${rect.w},${rect.h}`, ...rect }
|
|
6
|
+
});
|
|
7
|
+
var rotate = (params) => ({ name: "rotate", params });
|
|
8
|
+
var flip = () => ({ name: "flip", params: {} });
|
|
9
|
+
var flop = () => ({ name: "flop", params: {} });
|
|
10
|
+
var quality = (params) => ({ name: "quality", params });
|
|
11
|
+
var output = (params) => ({ name: "output", params });
|
|
12
|
+
function serializeOp(op) {
|
|
13
|
+
switch (op.name) {
|
|
14
|
+
case "resize": {
|
|
15
|
+
const parts = [];
|
|
16
|
+
if (op.params.w !== void 0) parts.push(`w:${op.params.w}`);
|
|
17
|
+
if (op.params.h !== void 0) parts.push(`h:${op.params.h}`);
|
|
18
|
+
if (op.params.fit !== void 0) parts.push(`fit:${op.params.fit}`);
|
|
19
|
+
return `resize=${parts.join(",")}`;
|
|
20
|
+
}
|
|
21
|
+
case "crop":
|
|
22
|
+
return `crop=dim:${op.params.dim}`;
|
|
23
|
+
case "rotate":
|
|
24
|
+
return `rotate=deg:${op.params.deg}`;
|
|
25
|
+
case "flip":
|
|
26
|
+
return "flip";
|
|
27
|
+
case "flop":
|
|
28
|
+
return "flop";
|
|
29
|
+
case "quality":
|
|
30
|
+
return `quality=n:${op.params.n}`;
|
|
31
|
+
case "output":
|
|
32
|
+
return `output=format:${op.params.format}`;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
function transformUrl({ handle, ops, apiUrl = "" }) {
|
|
36
|
+
const base = apiUrl.replace(/\/$/, "");
|
|
37
|
+
const chain = ops.map(serializeOp).join("/");
|
|
38
|
+
return chain ? `${base}/${chain}/${handle}` : `${base}/${handle}`;
|
|
39
|
+
}
|
|
40
|
+
function withSignedPolicy(url, security) {
|
|
41
|
+
const sep = url.includes("?") ? "&" : "?";
|
|
42
|
+
return `${url}${sep}policy=${encodeURIComponent(security.policy)}&signature=${encodeURIComponent(security.signature)}`;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export {
|
|
46
|
+
resize,
|
|
47
|
+
crop,
|
|
48
|
+
rotate,
|
|
49
|
+
flip,
|
|
50
|
+
flop,
|
|
51
|
+
quality,
|
|
52
|
+
output,
|
|
53
|
+
transformUrl,
|
|
54
|
+
withSignedPolicy
|
|
55
|
+
};
|
|
56
|
+
//# sourceMappingURL=chunk-DJK2SQ5I.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/core/transform.ts"],"sourcesContent":["/**\n * Delivery / transform URL builder.\n *\n * The API serves transformed derivatives at `GET /<chain>/<handle>`, where\n * <chain> is a slash-joined list of ops (e.g. `resize=w:200,h:200,fit:crop`).\n * These op builders + `transformUrl()` construct that URL from an uploaded file\n * handle, so consumers render an image at any size/format/quality without\n * hand-assembling URL strings.\n *\n * The op/param types and serialization mirror @uploader/shared EXACTLY β the\n * API's `parseTransformChain` is the other half of this contract, and\n * `contract.conformance.ts` asserts at typecheck time that they stay in sync.\n */\n\n// βββ Op vocabulary (mirrors @uploader/shared) ββββββββββββββββββββββββββββββββ\n\nexport type TransformOpName =\n | 'resize'\n | 'crop'\n | 'rotate'\n | 'flip'\n | 'flop'\n | 'quality'\n | 'output'\n\n/** Parameters for the resize operation. */\nexport type ResizeParams = {\n w?: number\n h?: number\n fit?: 'cover' | 'contain' | 'fill' | 'inside' | 'outside' | 'crop'\n}\n\n/** Parameters for the crop operation. */\nexport type CropParams = {\n /** \"x,y,w,h\" notation. */\n dim: string\n x?: number\n y?: number\n w?: number\n h?: number\n}\n\n/** Parameters for the rotate operation. */\nexport type RotateParams = { deg: number }\n\n/** Parameters for the quality operation. */\nexport type QualityParams = { n: number }\n\n/** Parameters for the output operation. */\nexport type OutputParams = { format: string }\n\n/** A single transform operation with its typed params. */\nexport type TransformOp =\n | { name: 'resize'; params: ResizeParams }\n | { name: 'crop'; params: CropParams }\n | { name: 'rotate'; params: RotateParams }\n | { name: 'flip'; params: Record<string, never> }\n | { name: 'flop'; params: Record<string, never> }\n | { name: 'quality'; params: QualityParams }\n | { name: 'output'; params: OutputParams }\n\n/** An ordered list of transform operations. */\nexport type TransformChain = { ops: TransformOp[] }\n\n// βββ Op builders βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ\n\n/** Resize to a width and/or height with an optional fit mode. */\nexport const resize = (params: ResizeParams): TransformOp => ({ name: 'resize', params })\n\n/** Crop a rectangular region: origin (x, y) and size wΓh, in source pixels. */\nexport const crop = (rect: { x: number; y: number; w: number; h: number }): TransformOp => ({\n name: 'crop',\n params: { dim: `${rect.x},${rect.y},${rect.w},${rect.h}`, ...rect },\n})\n\n/** Rotate clockwise by `deg` degrees. */\nexport const rotate = (params: RotateParams): TransformOp => ({ name: 'rotate', params })\n\n/** Flip vertically (mirror topβbottom). */\nexport const flip = (): TransformOp => ({ name: 'flip', params: {} })\n\n/** Flop horizontally (mirror leftβright). */\nexport const flop = (): TransformOp => ({ name: 'flop', params: {} })\n\n/** Set output quality 1β100 (ignored by lossless formats). */\nexport const quality = (params: QualityParams): TransformOp => ({ name: 'quality', params })\n\n/** Convert the output format, e.g. `{ format: 'webp' }`. */\nexport const output = (params: OutputParams): TransformOp => ({ name: 'output', params })\n\n// βββ Serialization (vendored from @uploader/shared, parser-compatible) ββββββββ\n\nfunction serializeOp(op: TransformOp): string {\n switch (op.name) {\n case 'resize': {\n const parts: string[] = []\n if (op.params.w !== undefined) parts.push(`w:${op.params.w}`)\n if (op.params.h !== undefined) parts.push(`h:${op.params.h}`)\n if (op.params.fit !== undefined) parts.push(`fit:${op.params.fit}`)\n return `resize=${parts.join(',')}`\n }\n case 'crop':\n return `crop=dim:${op.params.dim}`\n case 'rotate':\n return `rotate=deg:${op.params.deg}`\n case 'flip':\n return 'flip'\n case 'flop':\n return 'flop'\n case 'quality':\n return `quality=n:${op.params.n}`\n case 'output':\n return `output=format:${op.params.format}`\n }\n}\n\nexport type TransformUrlOptions = {\n /** File handle from an upload (`FileResult.handle`). */\n handle: string\n /** Ordered transform ops to apply. Must contain at least one op. */\n ops: TransformOp[]\n /**\n * Base API URL. Defaults to '' so the URL is relative β `/<chain>/<handle>` β\n * and resolves same-origin (e.g. proxied to the API). Pass an absolute URL to\n * point at a remote API directly.\n */\n apiUrl?: string\n}\n\n/**\n * Build a delivery URL that applies `ops` to the file identified by `handle`.\n *\n * @example\n * transformUrl({\n * handle: file.handle,\n * ops: [resize({ w: 200, h: 200, fit: 'crop' }), output({ format: 'webp' })],\n * })\n * // => \"/resize=w:200,h:200,fit:crop/output=format:webp/<handle>\"\n */\nexport function transformUrl({ handle, ops, apiUrl = '' }: TransformUrlOptions): string {\n const base = apiUrl.replace(/\\/$/, '')\n const chain = ops.map(serializeOp).join('/')\n // The API's transform route requires at least one op segment before the\n // handle; for the unmodified original, use FileResult.url instead.\n return chain ? `${base}/${chain}/${handle}` : `${base}/${handle}`\n}\n\n/**\n * Append a signed read policy to a delivery/transform URL for accounts on the\n * `signed` delivery-protection tier (Private plan, docs/12).\n *\n * The `policy` + `signature` pair is produced SERVER-SIDE by the customer's\n * backend (HMAC over the account's api-key secret, via @uploader/shared). This\n * helper only assembles the URL β the SDK never signs and never sees the secret.\n *\n * @example\n * const url = withSignedPolicy(\n * transformUrl({ handle, ops: [resize({ w: 400 })], apiUrl }),\n * { policy, signature }, // from your backend\n * )\n */\nexport function withSignedPolicy(\n url: string,\n security: { policy: string; signature: string },\n): string {\n const sep = url.includes('?') ? '&' : '?'\n return `${url}${sep}policy=${encodeURIComponent(security.policy)}&signature=${encodeURIComponent(security.signature)}`\n}\n"],"mappings":";AAmEO,IAAM,SAAS,CAAC,YAAuC,EAAE,MAAM,UAAU,OAAO;AAGhF,IAAM,OAAO,CAAC,UAAuE;AAAA,EAC1F,MAAM;AAAA,EACN,QAAQ,EAAE,KAAK,GAAG,KAAK,CAAC,IAAI,KAAK,CAAC,IAAI,KAAK,CAAC,IAAI,KAAK,CAAC,IAAI,GAAG,KAAK;AACpE;AAGO,IAAM,SAAS,CAAC,YAAuC,EAAE,MAAM,UAAU,OAAO;AAGhF,IAAM,OAAO,OAAoB,EAAE,MAAM,QAAQ,QAAQ,CAAC,EAAE;AAG5D,IAAM,OAAO,OAAoB,EAAE,MAAM,QAAQ,QAAQ,CAAC,EAAE;AAG5D,IAAM,UAAU,CAAC,YAAwC,EAAE,MAAM,WAAW,OAAO;AAGnF,IAAM,SAAS,CAAC,YAAuC,EAAE,MAAM,UAAU,OAAO;AAIvF,SAAS,YAAY,IAAyB;AAC5C,UAAQ,GAAG,MAAM;AAAA,IACf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