@crabcrop/mcp 0.0.0-stage → 0.9.11
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 +62 -2
- package/dist/index.js +424 -0
- package/node_modules/@crabcrop/url/README.md +97 -0
- package/node_modules/@crabcrop/url/dist/explain.d.ts +42 -0
- package/node_modules/@crabcrop/url/dist/explain.js +1050 -0
- package/node_modules/@crabcrop/url/dist/index.d.ts +161 -0
- package/node_modules/@crabcrop/url/dist/index.js +347 -0
- package/node_modules/@crabcrop/url/dist/operations.d.ts +285 -0
- package/node_modules/@crabcrop/url/dist/operations.js +722 -0
- package/node_modules/@crabcrop/url/package.json +31 -0
- package/package.json +48 -4
- package/server.json +1 -0
|
@@ -0,0 +1,1050 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parse a crabcrop URL (or bare path) back into its pieces and
|
|
3
|
+
* explain each segment, mirroring the server's grammar. This is
|
|
4
|
+
* the canonical implementation: the playground keeps a plain-JS
|
|
5
|
+
* copy for its no-build static page, and both are pinned to the
|
|
6
|
+
* same verdict corpus (audit PL-10, P-3).
|
|
7
|
+
*/
|
|
8
|
+
export const FILTER_DESCRIPTIONS = {
|
|
9
|
+
grayscale: "convert to grayscale",
|
|
10
|
+
invert: "invert the colors",
|
|
11
|
+
no_upscale: "never enlarge the source",
|
|
12
|
+
blur: "gaussian blur, argument is the sigma",
|
|
13
|
+
blurfaces: "blur detected faces (needs a face model on the server)",
|
|
14
|
+
brighten: "brighten by the given amount (-255..255)",
|
|
15
|
+
contrast: "contrast in percent, -100..100 (0 is unchanged, -100 is flat)",
|
|
16
|
+
gamma: "gamma correction with divisor g, 0 < g <= 10: each channel becomes 255 * (v / 255)^(1 / g) (1 is unchanged, above 1 brightens midtones)",
|
|
17
|
+
exposure: "exposure in stops, -10..10: multiply the linear light by 2^stops (0 is unchanged, positive brightens, negative darkens)",
|
|
18
|
+
highlights_shadows: "adjust the highlights and shadows ranges independently: two percents, each -100..100 (0 is unchanged)",
|
|
19
|
+
vibrance: "saturation weighted toward the muted colors: percent, -100..100 (0 is unchanged; vivid colors and skin tones are protected)",
|
|
20
|
+
huerotate: "rotate the hue by the given degrees",
|
|
21
|
+
rotate: "rotate by the given angle in degrees; the canvas expands to fit",
|
|
22
|
+
quality: "output quality 1..100, or `auto` for perceptual targeting; or per-format values in one list (quality(80,jpeg:90,webp:75)), resolved after format negotiation",
|
|
23
|
+
palette: "write PNG output as an indexed (palette) image with at most this many colors, 2..256; ignored by the other formats",
|
|
24
|
+
format: "force the output format; a format also takes its encoder options (format(jpeg,progressive), format(png,interlace), format(webp,method:6,sns:50)); an option its encoder cannot honour is refused",
|
|
25
|
+
fill: "the background color other operations pad with",
|
|
26
|
+
facepad: "how much a face crop expands around the face",
|
|
27
|
+
pixelatefaces: "pixelate detected faces (needs a face model on the server)",
|
|
28
|
+
blurclass: "blur every detected object of the named class (needs a detection model on the server)",
|
|
29
|
+
pixelateclass: "pixelate every detected object of the named class (needs a detection model on the server)",
|
|
30
|
+
recolorclass: "recolor every detected object of the named class to the given color (needs a detection model on the server)",
|
|
31
|
+
removebg: "remove the background (needs an AI model on the server)",
|
|
32
|
+
studio: "studio mode: `product` composites the subject onto a backdrop (bg: white, a RRGGBB color, brand, or none) with a synthesized contact shadow (shadow: soft, strong, or none), scaled and centered to a pad margin (pad: 0..40, default 10); `scene` keeps the photograph's scene, reframing it on the detected subject with light and color correction (pad only) (needs an AI model on the server)",
|
|
33
|
+
keep_profile: "keep the source color profile instead of converting to sRGB",
|
|
34
|
+
upscale: "AI upscale by the given factor (needs an AI model on the server)",
|
|
35
|
+
equalize: "histogram equalization",
|
|
36
|
+
enhance: "gentle per-channel auto-levels (0.5th percentile to 0, 99.5th to 255)",
|
|
37
|
+
saturation: "saturation factor, 0..255 (0 is grayscale, 1 is identity)",
|
|
38
|
+
sharpen: "sharpen by the given amount",
|
|
39
|
+
text: "draw a text overlay (needs a font on the server)",
|
|
40
|
+
rgb: "tint toward the given red/green/blue amounts",
|
|
41
|
+
curve: "tone curve: remap the named channel (rgb, r, g, or b) through 2 to 16 x,y control points, linearly interpolated, ends clamped",
|
|
42
|
+
convolution: "convolve with a 3x3 or 5x5 kernel: 9 or 25 row-major values (plain decimals, -1024..1024), then a normalize flag (0 or 1); edges replicate",
|
|
43
|
+
noise: "add monochrome film grain: one deterministic noise value per pixel on all channels, strength 0..100 percent of full scale (0 is a no-op)",
|
|
44
|
+
trim: "trim uniform borders; the five-argument form (tolerance,left,top,right,bottom) also removes that many pixels per side after the tolerance pass",
|
|
45
|
+
dpr: "device pixel ratio the sizes are multiplied by",
|
|
46
|
+
watermark: "overlay a watermark image",
|
|
47
|
+
max_bytes: "cap the output size, lowering quality to fit",
|
|
48
|
+
radius: "round the corners by the given radius in pixels (one value, or four in CSS corner order); needs an alpha-capable format",
|
|
49
|
+
border: "paint a border of the given width and color (RRGGBB) inside the frame",
|
|
50
|
+
mask: "keep only the inscribed `circle` or `ellipse`; the rest becomes transparent",
|
|
51
|
+
sepia: "the classic warm sepia tone",
|
|
52
|
+
duotone: "map shadows to the first color and highlights to the second, by luminance",
|
|
53
|
+
monochrome: "single-hue rendering in the given color (RRGGBB)",
|
|
54
|
+
pixelate: "mosaic into blocks of the given pixel size, each block averaged",
|
|
55
|
+
flip: "mirror the image: `h` horizontal, `v` vertical",
|
|
56
|
+
blur_region: "gaussian-blur the rectangle (x,y,w,h) in pixels at this point in the chain (redaction)",
|
|
57
|
+
overlay: "composite another image over the result: (url,x,y,alpha,blend[,width %]) with blend normal, multiply, screen, or overlay",
|
|
58
|
+
pixelate_region: "pixelate the rectangle (x,y,w,h) in pixels at this point in the chain (redaction)",
|
|
59
|
+
remove_region: "erase the rectangle (x,y,w,h) in pixels at this point in the chain and inpaint it from the surrounding pixels (AI region removal)",
|
|
60
|
+
};
|
|
61
|
+
const MAX_DIMENSION = 16383;
|
|
62
|
+
function parseDimension(part) {
|
|
63
|
+
let flip = false;
|
|
64
|
+
let digits = part;
|
|
65
|
+
if (digits.startsWith("-")) {
|
|
66
|
+
flip = true;
|
|
67
|
+
digits = digits.slice(1);
|
|
68
|
+
}
|
|
69
|
+
if (digits === "" || digits === "0")
|
|
70
|
+
return { kind: "proportional", value: 0, flip };
|
|
71
|
+
if (digits === "orig")
|
|
72
|
+
return { kind: "original", value: 0, flip };
|
|
73
|
+
if (!/^\+?\d+$/.test(digits))
|
|
74
|
+
return null;
|
|
75
|
+
if (BigInt(digits) > BigInt(MAX_DIMENSION))
|
|
76
|
+
return null;
|
|
77
|
+
return { kind: "pixels", value: Number(digits), flip };
|
|
78
|
+
}
|
|
79
|
+
function parseSizeSegment(seg) {
|
|
80
|
+
const x = seg.indexOf("x");
|
|
81
|
+
if (x === -1)
|
|
82
|
+
return null;
|
|
83
|
+
const w = parseDimension(seg.slice(0, x));
|
|
84
|
+
const h = parseDimension(seg.slice(x + 1));
|
|
85
|
+
if (!w || !h)
|
|
86
|
+
return null;
|
|
87
|
+
return { w, h };
|
|
88
|
+
}
|
|
89
|
+
function isExactBox(size) {
|
|
90
|
+
const px = (d) => d.kind === "pixels" && d.value >= 1 && !d.flip;
|
|
91
|
+
return px(size.w) && px(size.h);
|
|
92
|
+
}
|
|
93
|
+
function sizeBlurb(seg) {
|
|
94
|
+
const size = parseSizeSegment(seg);
|
|
95
|
+
if (!size)
|
|
96
|
+
return `resize per ${seg}`;
|
|
97
|
+
const describe = (d, axis) => d.kind === "original"
|
|
98
|
+
? `the original ${axis}`
|
|
99
|
+
: d.kind === "proportional"
|
|
100
|
+
? `proportional ${axis}`
|
|
101
|
+
: `${d.value}px ${axis}`;
|
|
102
|
+
const flips = [
|
|
103
|
+
size.w.flip ? "flipped horizontally" : null,
|
|
104
|
+
size.h.flip ? "flipped vertically" : null,
|
|
105
|
+
].filter(Boolean);
|
|
106
|
+
return `resize to ${describe(size.w, "width")} by ${describe(size.h, "height")}${flips.length ? ` (${flips.join(", ")})` : ""}`;
|
|
107
|
+
}
|
|
108
|
+
const NO_ARG_FILTERS = new Set([
|
|
109
|
+
"grayscale",
|
|
110
|
+
"invert",
|
|
111
|
+
"no_upscale",
|
|
112
|
+
"equalize",
|
|
113
|
+
"enhance",
|
|
114
|
+
"removebg",
|
|
115
|
+
"keep_profile",
|
|
116
|
+
"sepia",
|
|
117
|
+
"blurfaces",
|
|
118
|
+
"pixelatefaces",
|
|
119
|
+
]);
|
|
120
|
+
const FORMAT_NAMES = new Set(["jpeg", "jpg", "png", "webp", "jxl", "avif", "gif", "mp4", "webm"]);
|
|
121
|
+
const ANCHOR_NAMES = new Set([
|
|
122
|
+
"top-left",
|
|
123
|
+
"top-center",
|
|
124
|
+
"top-right",
|
|
125
|
+
"center-left",
|
|
126
|
+
"center",
|
|
127
|
+
"center-right",
|
|
128
|
+
"bottom-left",
|
|
129
|
+
"bottom-center",
|
|
130
|
+
"bottom-right",
|
|
131
|
+
]);
|
|
132
|
+
function isIntIn(s, min, max) {
|
|
133
|
+
// Rust's integer from_str syntax: an optional sign (the
|
|
134
|
+
// unsigned parsers accept '+' too), then digits.
|
|
135
|
+
return /^[+-]?\d+$/.test(s) && BigInt(s) >= BigInt(min) && BigInt(s) <= BigInt(max);
|
|
136
|
+
}
|
|
137
|
+
const RUST_FLOAT = /^[+-]?(\d+\.?\d*|\.\d+)([eE][+-]?\d+)?$/;
|
|
138
|
+
function isFloat(s) {
|
|
139
|
+
// Rust's f32::from_str syntax (AP-23): plain decimal with
|
|
140
|
+
// an optional exponent; no hex, no surrounding whitespace,
|
|
141
|
+
// both of which Number() would otherwise accept. The
|
|
142
|
+
// inf/nan spellings Rust also parses are non-finite, and
|
|
143
|
+
// every caller rejects non-finite values, so the verdicts
|
|
144
|
+
// agree without them.
|
|
145
|
+
return RUST_FLOAT.test(s) && Number.isFinite(Number(s));
|
|
146
|
+
}
|
|
147
|
+
const KERNEL_VALUE = /^-?\d+(\.\d+)?$/;
|
|
148
|
+
function isKernelValue(s) {
|
|
149
|
+
// The server's kernel-value grammar (url.rs
|
|
150
|
+
// parse_kernel_value): the rotate angle's strict decimal
|
|
151
|
+
// form, no exponent and no leading dot, bounded to 1024
|
|
152
|
+
// in absolute value.
|
|
153
|
+
return KERNEL_VALUE.test(s) && Math.abs(Number(s)) <= 1024;
|
|
154
|
+
}
|
|
155
|
+
function percentDecodable(s) {
|
|
156
|
+
try {
|
|
157
|
+
return decodeURIComponent(s);
|
|
158
|
+
}
|
|
159
|
+
catch {
|
|
160
|
+
return null;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Validate one filter against the server's rules (url.rs
|
|
165
|
+
* parse_filters, ported). `args` is null when the filter has
|
|
166
|
+
* no parentheses (empty parens count as none, like the
|
|
167
|
+
* server) and the raw argument string otherwise. Returns an
|
|
168
|
+
* error string, or null when the server would accept it.
|
|
169
|
+
*/
|
|
170
|
+
function validateFilter(name, args) {
|
|
171
|
+
const bad = `the server rejects this filter: ${name}${args === null ? "" : `(${args})`}`;
|
|
172
|
+
if (NO_ARG_FILTERS.has(name)) {
|
|
173
|
+
return args === null ? null : bad;
|
|
174
|
+
}
|
|
175
|
+
switch (name) {
|
|
176
|
+
case "trim": {
|
|
177
|
+
if (args === null)
|
|
178
|
+
return null;
|
|
179
|
+
const parts = args.split(",");
|
|
180
|
+
if (parts.length === 1)
|
|
181
|
+
return isIntIn(parts[0], 0, 255) ? null : bad;
|
|
182
|
+
return parts.length === 5 &&
|
|
183
|
+
isIntIn(parts[0], 0, 255) &&
|
|
184
|
+
parts.slice(1).every((p) => isIntIn(p, 0, 4294967295))
|
|
185
|
+
? null
|
|
186
|
+
: bad;
|
|
187
|
+
}
|
|
188
|
+
case "blur":
|
|
189
|
+
// The server's blur bound (url.rs MAX_BLUR_SIGMA):
|
|
190
|
+
// positive and at most 512.
|
|
191
|
+
return args !== null && isFloat(args) && Number(args) > 0 && Number(args) <= 512
|
|
192
|
+
? null
|
|
193
|
+
: bad;
|
|
194
|
+
case "sharpen":
|
|
195
|
+
return args !== null && isFloat(args) && Number(args) > 0 ? null : bad;
|
|
196
|
+
case "brighten":
|
|
197
|
+
return args !== null && isIntIn(args, -255, 255) ? null : bad;
|
|
198
|
+
case "huerotate":
|
|
199
|
+
return args !== null && isIntIn(args, -2147483648, 2147483647) ? null : bad;
|
|
200
|
+
case "contrast":
|
|
201
|
+
return args !== null && isFloat(args) && Number(args) >= -100 && Number(args) <= 100
|
|
202
|
+
? null
|
|
203
|
+
: bad;
|
|
204
|
+
case "gamma":
|
|
205
|
+
return args !== null && isFloat(args) && Number(args) > 0 && Number(args) <= 10
|
|
206
|
+
? null
|
|
207
|
+
: bad;
|
|
208
|
+
case "exposure":
|
|
209
|
+
return args !== null && isFloat(args) && Number(args) >= -10 && Number(args) <= 10
|
|
210
|
+
? null
|
|
211
|
+
: bad;
|
|
212
|
+
case "highlights_shadows": {
|
|
213
|
+
if (args === null)
|
|
214
|
+
return bad;
|
|
215
|
+
const parts = args.split(",");
|
|
216
|
+
return parts.length === 2 &&
|
|
217
|
+
parts.every((p) => isFloat(p) && Number(p) >= -100 && Number(p) <= 100)
|
|
218
|
+
? null
|
|
219
|
+
: bad;
|
|
220
|
+
}
|
|
221
|
+
case "vibrance":
|
|
222
|
+
return args !== null && isFloat(args) && Number(args) >= -100 && Number(args) <= 100
|
|
223
|
+
? null
|
|
224
|
+
: bad;
|
|
225
|
+
case "saturation":
|
|
226
|
+
return args !== null && isFloat(args) && Number(args) >= 0 && Number(args) <= 255
|
|
227
|
+
? null
|
|
228
|
+
: bad;
|
|
229
|
+
case "quality": {
|
|
230
|
+
if (args === null)
|
|
231
|
+
return bad;
|
|
232
|
+
if (args === "auto" || isIntIn(args, 1, 100))
|
|
233
|
+
return null;
|
|
234
|
+
// Per-format list (issue #319): at most one bare
|
|
235
|
+
// fallback, at most one entry per format, no video
|
|
236
|
+
// formats (their quality is not the server's to set).
|
|
237
|
+
const seen = new Set();
|
|
238
|
+
let fallbackSeen = false;
|
|
239
|
+
for (const part of args.split(",")) {
|
|
240
|
+
const colon = part.indexOf(":");
|
|
241
|
+
if (colon === -1) {
|
|
242
|
+
if (fallbackSeen || !(part === "auto" || isIntIn(part, 1, 100)))
|
|
243
|
+
return bad;
|
|
244
|
+
fallbackSeen = true;
|
|
245
|
+
}
|
|
246
|
+
else {
|
|
247
|
+
const name = part.slice(0, colon);
|
|
248
|
+
const value = part.slice(colon + 1);
|
|
249
|
+
if (!FORMAT_NAMES.has(name) || name === "mp4" || name === "webm")
|
|
250
|
+
return bad;
|
|
251
|
+
const canonical = name === "jpg" ? "jpeg" : name;
|
|
252
|
+
if (seen.has(canonical))
|
|
253
|
+
return bad;
|
|
254
|
+
seen.add(canonical);
|
|
255
|
+
if (!(value === "auto" || isIntIn(value, 1, 100)))
|
|
256
|
+
return bad;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
return seen.size + (fallbackSeen ? 1 : 0) > 0 ? null : bad;
|
|
260
|
+
}
|
|
261
|
+
case "palette":
|
|
262
|
+
return args !== null && isIntIn(args, 2, 256) ? null : bad;
|
|
263
|
+
case "format": {
|
|
264
|
+
if (args === null)
|
|
265
|
+
return bad;
|
|
266
|
+
// A format name plus its encoder options (issue #319),
|
|
267
|
+
// each validated against the format it belongs to.
|
|
268
|
+
const parts = args.split(",");
|
|
269
|
+
const [name, ...options] = parts;
|
|
270
|
+
if (!FORMAT_NAMES.has(name))
|
|
271
|
+
return bad;
|
|
272
|
+
const seen = new Set();
|
|
273
|
+
for (const option of options) {
|
|
274
|
+
const key = option.includes(":") ? option.slice(0, option.indexOf(":")) : option;
|
|
275
|
+
if (seen.has(key))
|
|
276
|
+
return bad;
|
|
277
|
+
seen.add(key);
|
|
278
|
+
if (name === "jpeg" || name === "jpg") {
|
|
279
|
+
if (option === "progressive")
|
|
280
|
+
continue;
|
|
281
|
+
if (/^subsampling:(444|422|420)$/.test(option))
|
|
282
|
+
continue;
|
|
283
|
+
return bad;
|
|
284
|
+
}
|
|
285
|
+
if (name === "png") {
|
|
286
|
+
if (option === "interlace")
|
|
287
|
+
continue;
|
|
288
|
+
return bad;
|
|
289
|
+
}
|
|
290
|
+
if (name === "webp") {
|
|
291
|
+
if (/^method:[0-6]$/.test(option))
|
|
292
|
+
continue;
|
|
293
|
+
if (/^sns:\d+$/.test(option) && isIntIn(option.slice(4), 0, 100))
|
|
294
|
+
continue;
|
|
295
|
+
return bad;
|
|
296
|
+
}
|
|
297
|
+
return bad;
|
|
298
|
+
}
|
|
299
|
+
return null;
|
|
300
|
+
}
|
|
301
|
+
case "rgb": {
|
|
302
|
+
if (args === null)
|
|
303
|
+
return bad;
|
|
304
|
+
const parts = args.split(",");
|
|
305
|
+
return parts.length === 3 && parts.every((p) => isIntIn(p, -255, 255)) ? null : bad;
|
|
306
|
+
}
|
|
307
|
+
case "curve": {
|
|
308
|
+
if (args === null)
|
|
309
|
+
return bad;
|
|
310
|
+
const parts = args.split(",");
|
|
311
|
+
if (!["rgb", "r", "g", "b"].includes(parts[0]))
|
|
312
|
+
return bad;
|
|
313
|
+
const coords = parts.slice(1);
|
|
314
|
+
if (coords.length < 4 || coords.length > 32 || coords.length % 2 !== 0)
|
|
315
|
+
return bad;
|
|
316
|
+
if (!coords.every((p) => isIntIn(p, 0, 255)))
|
|
317
|
+
return bad;
|
|
318
|
+
for (let i = 2; i < coords.length; i += 2) {
|
|
319
|
+
if (Number(coords[i]) <= Number(coords[i - 2]))
|
|
320
|
+
return bad;
|
|
321
|
+
}
|
|
322
|
+
return null;
|
|
323
|
+
}
|
|
324
|
+
case "convolution": {
|
|
325
|
+
if (args === null)
|
|
326
|
+
return bad;
|
|
327
|
+
const parts = args.split(",");
|
|
328
|
+
if (parts.length !== 10 && parts.length !== 26)
|
|
329
|
+
return bad;
|
|
330
|
+
const flag = parts[parts.length - 1];
|
|
331
|
+
if (flag !== "0" && flag !== "1")
|
|
332
|
+
return bad;
|
|
333
|
+
return parts.slice(0, -1).every((p) => isKernelValue(p)) ? null : bad;
|
|
334
|
+
}
|
|
335
|
+
case "noise":
|
|
336
|
+
return args !== null && isIntIn(args, 0, 100) ? null : bad;
|
|
337
|
+
case "facepad":
|
|
338
|
+
return args !== null && isFloat(args) && Number(args) >= 0 && Number(args) <= 3
|
|
339
|
+
? null
|
|
340
|
+
: bad;
|
|
341
|
+
case "upscale":
|
|
342
|
+
return args !== null && /^[+-]?\d+$/.test(args) && (Number(args) === 2 || Number(args) === 4)
|
|
343
|
+
? null
|
|
344
|
+
: bad;
|
|
345
|
+
case "studio": {
|
|
346
|
+
// The server's studio grammar (url.rs parse_studio):
|
|
347
|
+
// the mode (`product` or `scene`) first, then named
|
|
348
|
+
// `key:value` arguments, each key at most once.
|
|
349
|
+
// Product takes `bg`, `shadow`, and `pad`; scene
|
|
350
|
+
// takes `pad` only, because a backdrop and a shadow
|
|
351
|
+
// are product concepts (naming one under scene is a
|
|
352
|
+
// bad argument, not a silent no-op).
|
|
353
|
+
if (args === null)
|
|
354
|
+
return bad;
|
|
355
|
+
const parts = args.split(",");
|
|
356
|
+
const mode = parts[0];
|
|
357
|
+
if (mode !== "product" && mode !== "scene")
|
|
358
|
+
return bad;
|
|
359
|
+
const seen = new Set();
|
|
360
|
+
for (const part of parts.slice(1)) {
|
|
361
|
+
const cut = part.indexOf(":");
|
|
362
|
+
if (cut === -1)
|
|
363
|
+
return bad;
|
|
364
|
+
const key = part.slice(0, cut);
|
|
365
|
+
const value = part.slice(cut + 1);
|
|
366
|
+
if (seen.has(key))
|
|
367
|
+
return bad;
|
|
368
|
+
seen.add(key);
|
|
369
|
+
if (key === "pad") {
|
|
370
|
+
// The pad parses as a Rust u8 (an optional `+`,
|
|
371
|
+
// then digits; no minus sign, not even `-0`) and
|
|
372
|
+
// is bounded to 40.
|
|
373
|
+
if (!/^\+?\d+$/.test(value) || BigInt(value) > 40n)
|
|
374
|
+
return bad;
|
|
375
|
+
}
|
|
376
|
+
else if (mode === "product" && key === "bg") {
|
|
377
|
+
if (value !== "white" &&
|
|
378
|
+
value !== "brand" &&
|
|
379
|
+
value !== "none" &&
|
|
380
|
+
!/^[0-9a-fA-F]{6}$/.test(value)) {
|
|
381
|
+
return bad;
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
else if (mode === "product" && key === "shadow") {
|
|
385
|
+
if (value !== "none" && value !== "soft" && value !== "strong")
|
|
386
|
+
return bad;
|
|
387
|
+
}
|
|
388
|
+
else {
|
|
389
|
+
return bad;
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
return null;
|
|
393
|
+
}
|
|
394
|
+
case "dpr":
|
|
395
|
+
return args !== null && isFloat(args) && [1, 1.5, 2, 3].includes(Number(args))
|
|
396
|
+
? null
|
|
397
|
+
: bad;
|
|
398
|
+
case "rotate":
|
|
399
|
+
return args !== null && /^-?\d+(\.\d+)?$/.test(args) && Math.abs(Number(args)) <= 36000
|
|
400
|
+
? null
|
|
401
|
+
: bad;
|
|
402
|
+
case "fill":
|
|
403
|
+
return args !== null && /^[0-9a-fA-F]{6}$/.test(args) ? null : bad;
|
|
404
|
+
case "radius": {
|
|
405
|
+
if (args === null)
|
|
406
|
+
return bad;
|
|
407
|
+
const parts = args.split(",");
|
|
408
|
+
return (parts.length === 1 || parts.length === 4) &&
|
|
409
|
+
parts.every((p) => isIntIn(p, 0, 16383))
|
|
410
|
+
? null
|
|
411
|
+
: bad;
|
|
412
|
+
}
|
|
413
|
+
case "border": {
|
|
414
|
+
if (args === null)
|
|
415
|
+
return bad;
|
|
416
|
+
const parts = args.split(",");
|
|
417
|
+
return parts.length === 2 &&
|
|
418
|
+
isIntIn(parts[0], 0, 16383) &&
|
|
419
|
+
/^[0-9a-fA-F]{6}$/.test(parts[1])
|
|
420
|
+
? null
|
|
421
|
+
: bad;
|
|
422
|
+
}
|
|
423
|
+
case "mask":
|
|
424
|
+
return args === "circle" || args === "ellipse" ? null : bad;
|
|
425
|
+
case "duotone": {
|
|
426
|
+
if (args === null)
|
|
427
|
+
return bad;
|
|
428
|
+
const parts = args.split(",");
|
|
429
|
+
return parts.length === 2 && parts.every((p) => /^[0-9a-fA-F]{6}$/.test(p)) ? null : bad;
|
|
430
|
+
}
|
|
431
|
+
case "monochrome":
|
|
432
|
+
return args !== null && /^[0-9a-fA-F]{6}$/.test(args) ? null : bad;
|
|
433
|
+
case "pixelate":
|
|
434
|
+
return args !== null && isIntIn(args, 1, 16383) ? null : bad;
|
|
435
|
+
case "flip":
|
|
436
|
+
return args === "h" || args === "v" ? null : bad;
|
|
437
|
+
case "overlay": {
|
|
438
|
+
if (args === null)
|
|
439
|
+
return bad;
|
|
440
|
+
// The server splits from the right (rsplitn(6, ',')):
|
|
441
|
+
// with six or more fields the rightmost IS the width
|
|
442
|
+
// and must parse, and the URL keeps any commas left
|
|
443
|
+
// over. Splitting any other way misreads both.
|
|
444
|
+
const fields = args.split(",");
|
|
445
|
+
if (fields.length < 5)
|
|
446
|
+
return bad;
|
|
447
|
+
let rest = fields;
|
|
448
|
+
if (fields.length >= 6) {
|
|
449
|
+
if (!isIntIn(fields[fields.length - 1], 1, 400))
|
|
450
|
+
return bad;
|
|
451
|
+
rest = fields.slice(0, -1);
|
|
452
|
+
}
|
|
453
|
+
const blend = rest[rest.length - 1];
|
|
454
|
+
if (!["normal", "multiply", "screen", "overlay"].includes(blend))
|
|
455
|
+
return bad;
|
|
456
|
+
if (!isIntIn(rest[rest.length - 2], 0, 100))
|
|
457
|
+
return bad;
|
|
458
|
+
if (!isIntIn(rest[rest.length - 3], -2147483648, 2147483647))
|
|
459
|
+
return bad;
|
|
460
|
+
if (!isIntIn(rest[rest.length - 4], -2147483648, 2147483647))
|
|
461
|
+
return bad;
|
|
462
|
+
const url = percentDecodable(rest.slice(0, -4).join(","));
|
|
463
|
+
return url !== null && url !== "" ? null : bad;
|
|
464
|
+
}
|
|
465
|
+
case "blurclass":
|
|
466
|
+
case "pixelateclass": {
|
|
467
|
+
if (args === null)
|
|
468
|
+
return bad;
|
|
469
|
+
const decoded = percentDecodable(args);
|
|
470
|
+
if (decoded === null)
|
|
471
|
+
return bad;
|
|
472
|
+
const name = decoded.trim();
|
|
473
|
+
return name.length >= 1 &&
|
|
474
|
+
new TextEncoder().encode(name).length <= 64 &&
|
|
475
|
+
/^[\p{L}\p{N} _.\-/]+$/u.test(name)
|
|
476
|
+
? null
|
|
477
|
+
: bad;
|
|
478
|
+
}
|
|
479
|
+
case "recolorclass": {
|
|
480
|
+
if (args === null)
|
|
481
|
+
return bad;
|
|
482
|
+
const cut = args.lastIndexOf(",");
|
|
483
|
+
if (cut < 0)
|
|
484
|
+
return bad;
|
|
485
|
+
if (!/^[0-9a-fA-F]{6}$/.test(args.slice(cut + 1)))
|
|
486
|
+
return bad;
|
|
487
|
+
const decoded = percentDecodable(args.slice(0, cut));
|
|
488
|
+
if (decoded === null)
|
|
489
|
+
return bad;
|
|
490
|
+
const name = decoded.trim();
|
|
491
|
+
return name.length >= 1 &&
|
|
492
|
+
new TextEncoder().encode(name).length <= 64 &&
|
|
493
|
+
/^[\p{L}\p{N} _.\-/]+$/u.test(name)
|
|
494
|
+
? null
|
|
495
|
+
: bad;
|
|
496
|
+
}
|
|
497
|
+
case "blur_region":
|
|
498
|
+
case "pixelate_region":
|
|
499
|
+
case "remove_region": {
|
|
500
|
+
if (args === null)
|
|
501
|
+
return bad;
|
|
502
|
+
const parts = args.split(",");
|
|
503
|
+
return parts.length === 4 &&
|
|
504
|
+
parts.every((p) => isIntIn(p, 0, 16383)) &&
|
|
505
|
+
Number(parts[2]) >= 1 &&
|
|
506
|
+
Number(parts[3]) >= 1
|
|
507
|
+
? null
|
|
508
|
+
: bad;
|
|
509
|
+
}
|
|
510
|
+
case "max_bytes":
|
|
511
|
+
return args !== null &&
|
|
512
|
+
/^[+-]?\d+$/.test(args) &&
|
|
513
|
+
BigInt(args) >= 1n &&
|
|
514
|
+
BigInt(args) <= 18446744073709551615n
|
|
515
|
+
? null
|
|
516
|
+
: bad;
|
|
517
|
+
case "text": {
|
|
518
|
+
if (args === null)
|
|
519
|
+
return bad;
|
|
520
|
+
const parts = args.split(",");
|
|
521
|
+
if (parts.length < 4)
|
|
522
|
+
return bad;
|
|
523
|
+
const anchor = parts[parts.length - 1];
|
|
524
|
+
const color = parts[parts.length - 2];
|
|
525
|
+
const size = parts[parts.length - 3];
|
|
526
|
+
const text = percentDecodable(parts.slice(0, -3).join(","));
|
|
527
|
+
return ANCHOR_NAMES.has(anchor) &&
|
|
528
|
+
/^[0-9a-fA-F]{6}([0-9a-fA-F]{2})?$/.test(color) &&
|
|
529
|
+
isIntIn(size, 1, 1000) &&
|
|
530
|
+
text !== null &&
|
|
531
|
+
text !== ""
|
|
532
|
+
? null
|
|
533
|
+
: bad;
|
|
534
|
+
}
|
|
535
|
+
case "watermark": {
|
|
536
|
+
if (args === null)
|
|
537
|
+
return bad;
|
|
538
|
+
const parts = args.split(",");
|
|
539
|
+
if (parts.length < 4)
|
|
540
|
+
return bad;
|
|
541
|
+
const alpha = parts[parts.length - 1];
|
|
542
|
+
const y = parts[parts.length - 2];
|
|
543
|
+
const x = parts[parts.length - 3];
|
|
544
|
+
const url = percentDecodable(parts.slice(0, -3).join(","));
|
|
545
|
+
return isIntIn(alpha, 0, 100) &&
|
|
546
|
+
isIntIn(y, -2147483648, 2147483647) &&
|
|
547
|
+
isIntIn(x, -2147483648, 2147483647) &&
|
|
548
|
+
url !== null &&
|
|
549
|
+
url !== ""
|
|
550
|
+
? null
|
|
551
|
+
: bad;
|
|
552
|
+
}
|
|
553
|
+
default:
|
|
554
|
+
return `unknown filter: ${name}`;
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
function explainFilters(rest) {
|
|
558
|
+
// The server splits the list on the ':' separators that
|
|
559
|
+
// sit outside parentheses (url.rs split_filter_chain):
|
|
560
|
+
// an argument list may itself contain ':' (the studio
|
|
561
|
+
// filter's named arguments are `key:value`), so a naive
|
|
562
|
+
// split would cut those filters apart. Unbalanced
|
|
563
|
+
// parentheses leave the tail in one part, which then
|
|
564
|
+
// fails its own parse, as on the server.
|
|
565
|
+
const parts = [];
|
|
566
|
+
let depth = 0;
|
|
567
|
+
let start = 0;
|
|
568
|
+
for (let i = 0; i < rest.length; i++) {
|
|
569
|
+
const c = rest[i];
|
|
570
|
+
if (c === "(") {
|
|
571
|
+
depth += 1;
|
|
572
|
+
}
|
|
573
|
+
else if (c === ")") {
|
|
574
|
+
depth = Math.max(0, depth - 1);
|
|
575
|
+
}
|
|
576
|
+
else if (c === ":" && depth === 0) {
|
|
577
|
+
parts.push(rest.slice(start, i));
|
|
578
|
+
start = i + 1;
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
parts.push(rest.slice(start));
|
|
582
|
+
const blurbs = [];
|
|
583
|
+
for (const part of parts) {
|
|
584
|
+
const match = part.match(/^([a-z_0-9]+)(?:\((.*)\))?$/);
|
|
585
|
+
if (!match) {
|
|
586
|
+
return { error: `malformed filter: ${part}` };
|
|
587
|
+
}
|
|
588
|
+
const [, name, rawArgs] = match;
|
|
589
|
+
const args = rawArgs === undefined || rawArgs === "" ? null : rawArgs;
|
|
590
|
+
const blurb = FILTER_DESCRIPTIONS[name];
|
|
591
|
+
if (!blurb) {
|
|
592
|
+
return { error: `unknown filter: ${name}` };
|
|
593
|
+
}
|
|
594
|
+
const invalid = validateFilter(name, args);
|
|
595
|
+
if (invalid) {
|
|
596
|
+
return { error: invalid };
|
|
597
|
+
}
|
|
598
|
+
blurbs.push(args !== null ? `${name}(${args}): ${blurb}` : `${name}: ${blurb}`);
|
|
599
|
+
}
|
|
600
|
+
return { detail: blurbs.join("; ") };
|
|
601
|
+
}
|
|
602
|
+
const ALIGN_NAMES = new Set(["center", "smart", "face", "top", "bottom", "left", "right", "top-left", "top-right", "bottom-left", "bottom-right"]);
|
|
603
|
+
const U64_MAX = 18446744073709551615n;
|
|
604
|
+
/**
|
|
605
|
+
* The objects crop alignment (url.rs parse_objects, ported):
|
|
606
|
+
* `objects(<class>[,<class>...])[:<scale>]`. Returns the
|
|
607
|
+
* decoded class names (duplicates folded, first spelling
|
|
608
|
+
* kept, as the server's canonical form folds them) and the
|
|
609
|
+
* scale, or null when the server would reject the form.
|
|
610
|
+
* The 32-class cap counts distinct names, as the server's
|
|
611
|
+
* does: repeats are free, and a 33rd distinct name rejects
|
|
612
|
+
* the form (issue #408).
|
|
613
|
+
*/
|
|
614
|
+
function parseObjectsAlign(align) {
|
|
615
|
+
if (!align.startsWith("objects("))
|
|
616
|
+
return null;
|
|
617
|
+
const inner = align.slice("objects(".length);
|
|
618
|
+
const close = inner.indexOf(")");
|
|
619
|
+
if (close < 0)
|
|
620
|
+
return null;
|
|
621
|
+
const list = inner.slice(0, close);
|
|
622
|
+
const tail = inner.slice(close + 1);
|
|
623
|
+
let scale = 1;
|
|
624
|
+
if (tail !== "") {
|
|
625
|
+
if (!tail.startsWith(":"))
|
|
626
|
+
return null;
|
|
627
|
+
const text = tail.slice(1);
|
|
628
|
+
if (!isFloat(text))
|
|
629
|
+
return null;
|
|
630
|
+
scale = Number(text);
|
|
631
|
+
if (!(scale > 0) || scale > 100)
|
|
632
|
+
return null;
|
|
633
|
+
}
|
|
634
|
+
const parts = list.split(",");
|
|
635
|
+
const classes = [];
|
|
636
|
+
const seen = new Set();
|
|
637
|
+
for (const part of parts) {
|
|
638
|
+
const decoded = percentDecodable(part);
|
|
639
|
+
if (decoded === null)
|
|
640
|
+
return null;
|
|
641
|
+
const name = decoded.trim();
|
|
642
|
+
if (name.length < 1 ||
|
|
643
|
+
new TextEncoder().encode(name).length > 64 ||
|
|
644
|
+
!/^[\p{L}\p{N} _.\-/]+$/u.test(name)) {
|
|
645
|
+
return null;
|
|
646
|
+
}
|
|
647
|
+
const key = name.toLowerCase();
|
|
648
|
+
if (!seen.has(key)) {
|
|
649
|
+
if (classes.length === 32)
|
|
650
|
+
return null;
|
|
651
|
+
seen.add(key);
|
|
652
|
+
classes.push(name);
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
return classes.length > 0 ? { classes, scale } : null;
|
|
656
|
+
}
|
|
657
|
+
function explainSegment(seg, nowSeconds) {
|
|
658
|
+
if (seg.startsWith("exp:")) {
|
|
659
|
+
const value = seg.slice(4);
|
|
660
|
+
if (!/^\d+$/.test(value)) {
|
|
661
|
+
return { kind: "expiry", title: "Expiry", error: `malformed expiry: ${seg}` };
|
|
662
|
+
}
|
|
663
|
+
// Parse as BigInt: the server reads a u64, and a huge
|
|
664
|
+
// value must report its clean 400, not crash the date
|
|
665
|
+
// arithmetic (audit P-3).
|
|
666
|
+
const seconds = BigInt(value);
|
|
667
|
+
if (seconds > U64_MAX) {
|
|
668
|
+
return {
|
|
669
|
+
kind: "expiry",
|
|
670
|
+
title: "Expiry",
|
|
671
|
+
error: `expiry overflows the server's u64 parser: ${seg} (the server answers 400)`,
|
|
672
|
+
};
|
|
673
|
+
}
|
|
674
|
+
const expired = BigInt(nowSeconds) >= seconds;
|
|
675
|
+
const asNumber = Number(seconds);
|
|
676
|
+
const when = asNumber <= 8640000000000
|
|
677
|
+
? new Date(asNumber * 1000).toISOString().replace("T", " ").replace(".000Z", " UTC")
|
|
678
|
+
: "a date beyond representation";
|
|
679
|
+
return {
|
|
680
|
+
kind: "expiry",
|
|
681
|
+
title: "Expiry",
|
|
682
|
+
detail: `valid strictly before ${when} (${value})${expired ? " — EXPIRED at the reference time" : ""}`,
|
|
683
|
+
expired,
|
|
684
|
+
};
|
|
685
|
+
}
|
|
686
|
+
if (seg.startsWith("maxage:")) {
|
|
687
|
+
const value = seg.slice(7);
|
|
688
|
+
if (!/^\d+$/.test(value)) {
|
|
689
|
+
return {
|
|
690
|
+
kind: "maxage",
|
|
691
|
+
title: "Max-age",
|
|
692
|
+
error: `malformed maxage: ${seg} (want maxage:<seconds> from 0 to 31536000)`,
|
|
693
|
+
};
|
|
694
|
+
}
|
|
695
|
+
if (BigInt(value) > 31536000n) {
|
|
696
|
+
return {
|
|
697
|
+
kind: "maxage",
|
|
698
|
+
title: "Max-age",
|
|
699
|
+
error: `maxage out of range: ${seg} (0 to 31536000 seconds; the server answers 400)`,
|
|
700
|
+
};
|
|
701
|
+
}
|
|
702
|
+
return {
|
|
703
|
+
kind: "maxage",
|
|
704
|
+
title: "Max-age",
|
|
705
|
+
detail: value === "0"
|
|
706
|
+
? "this response is served with Cache-Control: public, max-age=0 and no immutable flag: caches must revalidate before reuse; the stored image is unchanged"
|
|
707
|
+
: `this response is served with Cache-Control max-age ${value} seconds instead of the default year; the stored image is unchanged`,
|
|
708
|
+
};
|
|
709
|
+
}
|
|
710
|
+
if (seg.startsWith("debug:")) {
|
|
711
|
+
const value = seg.slice(6);
|
|
712
|
+
if (value !== "focal") {
|
|
713
|
+
return {
|
|
714
|
+
kind: "debug",
|
|
715
|
+
title: "Debug view",
|
|
716
|
+
error: `unsupported debug view: ${seg} (want debug:focal; the server answers 400)`,
|
|
717
|
+
};
|
|
718
|
+
}
|
|
719
|
+
return {
|
|
720
|
+
kind: "debug",
|
|
721
|
+
title: "Debug view",
|
|
722
|
+
detail: "the response is a diagnostic image instead of the transform result: the canvas the next crop operates on, with the chosen focal point (magenta crosshair), the focal rectangle when one is in play, the detection boxes that fed the decision (amber), and the crop window outline (blue); with no crop it shows the focal analysis of the final image; debug responses are never cached and are served with Cache-Control: no-store; animated sources and video formats are refused with 400",
|
|
723
|
+
};
|
|
724
|
+
}
|
|
725
|
+
if (seg.startsWith("frame:")) {
|
|
726
|
+
const rest = seg.slice(6);
|
|
727
|
+
// The server's frame grammar (url.rs parse_frame): the
|
|
728
|
+
// poster keyword, or a strict decimal (no sign, no
|
|
729
|
+
// exponent, no leading dot) as seconds up to 86400 or,
|
|
730
|
+
// with a % suffix, as a percent up to 100.
|
|
731
|
+
const decimal = (text, max) => /^\d+(\.\d+)?$/.test(text) && Number(text) <= max;
|
|
732
|
+
if (rest === "poster") {
|
|
733
|
+
return { kind: "operation", title: "Frame", detail: "render the video source's poster frame" };
|
|
734
|
+
}
|
|
735
|
+
if (rest.endsWith("%") && decimal(rest.slice(0, -1), 100)) {
|
|
736
|
+
return { kind: "operation", title: "Frame", detail: `render the frame ${rest} through the video source` };
|
|
737
|
+
}
|
|
738
|
+
if (!rest.endsWith("%") && decimal(rest, 86400)) {
|
|
739
|
+
return { kind: "operation", title: "Frame", detail: `render the frame at ${rest} seconds into the video source` };
|
|
740
|
+
}
|
|
741
|
+
return {
|
|
742
|
+
kind: "operation",
|
|
743
|
+
title: "Frame",
|
|
744
|
+
error: `malformed frame segment: ${seg} (want frame:poster, frame:<seconds> up to 86400, or frame:<percent>% up to 100; the server answers 400)`,
|
|
745
|
+
};
|
|
746
|
+
}
|
|
747
|
+
if (seg.startsWith("page:")) {
|
|
748
|
+
const rest = seg.slice(5);
|
|
749
|
+
// The server's page grammar (url.rs parse_page): ASCII
|
|
750
|
+
// digits, leading zeros allowed, 1 to 999999.
|
|
751
|
+
const digits = rest.replace(/^0+/, "");
|
|
752
|
+
if (!/^\d+$/.test(rest) || digits === "" || digits.length > 6) {
|
|
753
|
+
return {
|
|
754
|
+
kind: "operation",
|
|
755
|
+
title: "Page",
|
|
756
|
+
error: `malformed page segment: ${seg} (want page:<n> with n from 1 to 999999; the server answers 400)`,
|
|
757
|
+
};
|
|
758
|
+
}
|
|
759
|
+
return { kind: "operation", title: "Page", detail: `render page ${Number(digits)} of the PDF source` };
|
|
760
|
+
}
|
|
761
|
+
if (seg.startsWith("preset:")) {
|
|
762
|
+
const name = seg.slice(7);
|
|
763
|
+
if (!name) {
|
|
764
|
+
return { kind: "preset", title: "Preset", error: "empty preset name" };
|
|
765
|
+
}
|
|
766
|
+
return {
|
|
767
|
+
kind: "preset",
|
|
768
|
+
title: "Preset",
|
|
769
|
+
detail: `expands server-side to the operations the operator named "${name}"; the signature covers this reference, not the expansion`,
|
|
770
|
+
};
|
|
771
|
+
}
|
|
772
|
+
if (seg.startsWith("crop-rect:")) {
|
|
773
|
+
const rest = seg.slice(10);
|
|
774
|
+
const m = rest.match(/^([+-]?\d+)x([+-]?\d+):([+-]?\d+)x([+-]?\d+)$/);
|
|
775
|
+
if (!m ||
|
|
776
|
+
m.slice(1).some((n) => BigInt(n) < 0n || BigInt(n) > 4294967295n) ||
|
|
777
|
+
BigInt(m[3]) <= BigInt(m[1]) ||
|
|
778
|
+
BigInt(m[4]) <= BigInt(m[2])) {
|
|
779
|
+
return {
|
|
780
|
+
kind: "operation",
|
|
781
|
+
title: "Crop rectangle",
|
|
782
|
+
error: `malformed crop-rect segment: ${seg} (want crop-rect:<l>x<t>:<r>x<b> with right > left and bottom > top)`,
|
|
783
|
+
};
|
|
784
|
+
}
|
|
785
|
+
return { kind: "operation", title: "Crop rectangle", detail: `crop the exact rectangle ${rest} (left x top : right x bottom)` };
|
|
786
|
+
}
|
|
787
|
+
if (seg.startsWith("focal:")) {
|
|
788
|
+
const rest = seg.slice(6);
|
|
789
|
+
const m = rest.match(/^([+-]?\d+)x([+-]?\d+):([+-]?\d+)x([+-]?\d+)$/);
|
|
790
|
+
if (!m ||
|
|
791
|
+
m.slice(1).some((n) => BigInt(n) < 0n || BigInt(n) > 4294967295n) ||
|
|
792
|
+
BigInt(m[3]) <= BigInt(m[1]) ||
|
|
793
|
+
BigInt(m[4]) <= BigInt(m[2])) {
|
|
794
|
+
return {
|
|
795
|
+
kind: "operation",
|
|
796
|
+
title: "Focal rectangle",
|
|
797
|
+
error: `malformed focal segment: ${seg} (want focal:<l>x<t>:<r>x<b> with right > left and bottom > top)`,
|
|
798
|
+
};
|
|
799
|
+
}
|
|
800
|
+
return { kind: "operation", title: "Focal rectangle", detail: `set the focal rectangle ${rest} for the next crop (left x top : right x bottom)` };
|
|
801
|
+
}
|
|
802
|
+
if (seg.startsWith("focalpoint:")) {
|
|
803
|
+
const rest = seg.slice(11);
|
|
804
|
+
const colon = rest.indexOf(":");
|
|
805
|
+
const point = colon === -1 ? rest : rest.slice(0, colon);
|
|
806
|
+
const zoomText = colon === -1 ? null : rest.slice(colon + 1);
|
|
807
|
+
const xy = point.split("x");
|
|
808
|
+
const ok = xy.length === 2 &&
|
|
809
|
+
isFloat(xy[0]) &&
|
|
810
|
+
isFloat(xy[1]) &&
|
|
811
|
+
Number(xy[0]) >= 0 &&
|
|
812
|
+
Number(xy[0]) <= 1 &&
|
|
813
|
+
Number(xy[1]) >= 0 &&
|
|
814
|
+
Number(xy[1]) <= 1 &&
|
|
815
|
+
(zoomText === null ||
|
|
816
|
+
(isFloat(zoomText) && Number(zoomText) > 0 && Number(zoomText) <= 100));
|
|
817
|
+
if (!ok) {
|
|
818
|
+
return {
|
|
819
|
+
kind: "operation",
|
|
820
|
+
title: "Focal point",
|
|
821
|
+
error: `malformed focalpoint segment: ${seg} (want focalpoint:<x>x<y>[:<zoom>] with x and y in 0..1 and zoom in (0, 100])`,
|
|
822
|
+
};
|
|
823
|
+
}
|
|
824
|
+
return {
|
|
825
|
+
kind: "operation",
|
|
826
|
+
title: "Focal point",
|
|
827
|
+
detail: `set the focal point ${point} at zoom ${zoomText ?? "1"} for the next crop (fractions of the image dimensions; zoom above 1 tightens the crop around the point)`,
|
|
828
|
+
};
|
|
829
|
+
}
|
|
830
|
+
if (seg.startsWith("crop:")) {
|
|
831
|
+
const rest = seg.slice(5);
|
|
832
|
+
const idx = rest.indexOf(":");
|
|
833
|
+
const dims = idx === -1 ? rest : rest.slice(0, idx);
|
|
834
|
+
const align = idx === -1 ? "center" : rest.slice(idx + 1);
|
|
835
|
+
const cropSize = parseSizeSegment(dims);
|
|
836
|
+
const objects = align.startsWith("objects") ? parseObjectsAlign(align) : null;
|
|
837
|
+
if (!cropSize || !isExactBox(cropSize) || (!ALIGN_NAMES.has(align) && objects === null)) {
|
|
838
|
+
return {
|
|
839
|
+
kind: "operation",
|
|
840
|
+
title: "Crop",
|
|
841
|
+
error: `malformed crop segment: ${seg} (want crop:<w>x<h>[:center|smart|face|top|bottom|left|right|top-left|top-right|bottom-left|bottom-right|objects(<class>[,<class>...])[:<scale>]], pixels only, no flips)`,
|
|
842
|
+
};
|
|
843
|
+
}
|
|
844
|
+
if (objects !== null) {
|
|
845
|
+
return {
|
|
846
|
+
kind: "operation",
|
|
847
|
+
title: "Crop",
|
|
848
|
+
detail: `crop to the aspect of ${dims} and resize to it; alignment: objects (${objects.classes.join(", ")}): frame the union of every detection of the named classes, the union box scaled by ${objects.scale} about its center and the crop centered on it; needs a detection model on the server, and falls back to the plain center crop of the same size when none of the classes is detected`,
|
|
849
|
+
};
|
|
850
|
+
}
|
|
851
|
+
return { kind: "operation", title: "Crop", detail: `crop to the aspect of ${dims} and resize to it; alignment: ${align === "center" ? "center (or the focal point for smart)" : align}` };
|
|
852
|
+
}
|
|
853
|
+
if (seg.startsWith("fit-in:")) {
|
|
854
|
+
const rest = seg.slice(7);
|
|
855
|
+
const idx = rest.indexOf(":");
|
|
856
|
+
const dims = idx === -1 ? rest : rest.slice(0, idx);
|
|
857
|
+
const modifier = idx === -1 ? null : rest.slice(idx + 1);
|
|
858
|
+
const size = parseSizeSegment(dims);
|
|
859
|
+
if (!size || size.w.flip || size.h.flip || (modifier !== null && modifier !== "adaptive")) {
|
|
860
|
+
return { kind: "operation", title: "Fit in", error: `malformed fit-in segment: ${seg} (want fit-in:<w>x<h>[:adaptive], flips are not allowed here)` };
|
|
861
|
+
}
|
|
862
|
+
if (modifier === "adaptive") {
|
|
863
|
+
return { kind: "operation", title: "Fit inside a box", detail: `scale proportionally to fit inside ${dims}, swapping the box axes when the source orientation disagrees (adaptive)` };
|
|
864
|
+
}
|
|
865
|
+
return { kind: "operation", title: "Fit inside a box", detail: `scale proportionally to fit inside ${rest}` };
|
|
866
|
+
}
|
|
867
|
+
if (seg.startsWith("pad:")) {
|
|
868
|
+
const rest = seg.slice(4);
|
|
869
|
+
const idx = rest.indexOf(":");
|
|
870
|
+
const dims = idx === -1 ? rest : rest.slice(0, idx);
|
|
871
|
+
const fill = idx === -1 ? null : rest.slice(idx + 1);
|
|
872
|
+
const padSize = parseSizeSegment(dims);
|
|
873
|
+
if (!padSize || !isExactBox(padSize) || (fill !== null && fill !== "blur" && !/^[0-9a-fA-F]{6}$/.test(fill))) {
|
|
874
|
+
return { kind: "operation", title: "Pad", error: `malformed pad segment: ${seg} (want pad:<w>x<h>[:<hex>|blur], pixels only, no flips)` };
|
|
875
|
+
}
|
|
876
|
+
return { kind: "operation", title: "Pad", detail: `fit inside ${dims} and fill the canvas to exactly that size, fill: ${fill ?? "the fill(...) color or black"}` };
|
|
877
|
+
}
|
|
878
|
+
if (seg.startsWith("filters:")) {
|
|
879
|
+
const { detail, error } = explainFilters(seg.slice(8));
|
|
880
|
+
if (error) {
|
|
881
|
+
return { kind: "operation", title: "Filters", error };
|
|
882
|
+
}
|
|
883
|
+
return { kind: "operation", title: "Filters", detail };
|
|
884
|
+
}
|
|
885
|
+
if (parseSizeSegment(seg)) {
|
|
886
|
+
return { kind: "operation", title: "Resize", detail: sizeBlurb(seg) };
|
|
887
|
+
}
|
|
888
|
+
if (isSizeShaped(seg)) {
|
|
889
|
+
// The server's dispatch (url.rs size_shaped, issue #228):
|
|
890
|
+
// a segment shaped like a size that fails the size
|
|
891
|
+
// grammar is a malformed size, not the start of the
|
|
892
|
+
// source, so the request is a 400 rather than a fetch
|
|
893
|
+
// of a nonsense source.
|
|
894
|
+
return {
|
|
895
|
+
kind: "operation",
|
|
896
|
+
title: "Resize",
|
|
897
|
+
error: `malformed size segment: ${seg} (each side is a pixel count up to ${MAX_DIMENSION}, 0, or orig; the server answers 400)`,
|
|
898
|
+
};
|
|
899
|
+
}
|
|
900
|
+
return null; // Not an operation: the source starts here.
|
|
901
|
+
}
|
|
902
|
+
/**
|
|
903
|
+
* The server's size-shape test (url.rs size_shaped), ported:
|
|
904
|
+
* an `x` joining two parts, at least one of them a dimension
|
|
905
|
+
* spelling (digits, `orig`, or empty, with an optional flip
|
|
906
|
+
* sign), and no `.` or `:` anywhere, the punctuation that
|
|
907
|
+
* marks URL text.
|
|
908
|
+
*/
|
|
909
|
+
function isSizeShaped(seg) {
|
|
910
|
+
const x = seg.indexOf("x");
|
|
911
|
+
if (x === -1)
|
|
912
|
+
return false;
|
|
913
|
+
const left = seg.slice(0, x);
|
|
914
|
+
const right = seg.slice(x + 1);
|
|
915
|
+
if (left.includes(".") || right.includes(".") || seg.includes(":"))
|
|
916
|
+
return false;
|
|
917
|
+
const spelling = (part) => {
|
|
918
|
+
const digits = part.startsWith("-") ? part.slice(1) : part;
|
|
919
|
+
return digits === "" || digits === "orig" || /^\d+$/.test(digits);
|
|
920
|
+
};
|
|
921
|
+
return spelling(left) || spelling(right);
|
|
922
|
+
}
|
|
923
|
+
/**
|
|
924
|
+
* Parse a crabcrop URL (or bare path) back into its pieces,
|
|
925
|
+
* explaining each segment. Mirrors the server's dispatch:
|
|
926
|
+
* segments are operations until the first segment that is not
|
|
927
|
+
* one; everything from there is the source.
|
|
928
|
+
*
|
|
929
|
+
* Returns { signature, segments, source, remainder, errors }
|
|
930
|
+
* where segments carry { raw, kind, title, detail?, error? }.
|
|
931
|
+
*/
|
|
932
|
+
export function explainUrl(input, nowSeconds = Math.floor(Date.now() / 1000)) {
|
|
933
|
+
// Extract the path by string surgery, never via the URL
|
|
934
|
+
// parser: WHATWG parsing resolves dot segments (.. and
|
|
935
|
+
// %2e%2e) that the server treats as literal path bytes
|
|
936
|
+
// (audit P-3). A query or fragment is not part of the
|
|
937
|
+
// signed path, on either side.
|
|
938
|
+
let path = input.trim();
|
|
939
|
+
const scheme = path.indexOf("://");
|
|
940
|
+
if (scheme > 0 && /^[a-zA-Z][a-zA-Z0-9+.-]*$/.test(path.slice(0, scheme))) {
|
|
941
|
+
const afterAuthority = path.slice(scheme + 3);
|
|
942
|
+
const slash = afterAuthority.indexOf("/");
|
|
943
|
+
path = slash === -1 ? "" : afterAuthority.slice(slash + 1);
|
|
944
|
+
}
|
|
945
|
+
else if (path.startsWith("/")) {
|
|
946
|
+
path = path.slice(1);
|
|
947
|
+
}
|
|
948
|
+
const cut = path.search(/[?#]/);
|
|
949
|
+
if (cut !== -1) {
|
|
950
|
+
path = path.slice(0, cut);
|
|
951
|
+
}
|
|
952
|
+
const errors = [];
|
|
953
|
+
// A sealed URL is a single segment: the token carries
|
|
954
|
+
// the whole remainder encrypted, so there is no slash
|
|
955
|
+
// and nothing visible to itemize (issue #388).
|
|
956
|
+
const sealedWhole = path.match(/^(?:([A-Za-z0-9_-]{1,32}):)?s1\.([A-Za-z0-9_-]+)$/);
|
|
957
|
+
if (sealedWhole) {
|
|
958
|
+
const signature = {
|
|
959
|
+
kind: "sealed",
|
|
960
|
+
value: `s1.${sealedWhole[2]}`,
|
|
961
|
+
detail: "sealed token (AES-256-GCM): the operations and source are encrypted inside it, so there is nothing visible to itemize; the server opens it and serves the URL it carries",
|
|
962
|
+
};
|
|
963
|
+
if (sealedWhole[1]) {
|
|
964
|
+
signature.keyId = sealedWhole[1];
|
|
965
|
+
}
|
|
966
|
+
return { signature, segments: [], source: "", remainder: "", errors: [] };
|
|
967
|
+
}
|
|
968
|
+
const slash = path.indexOf("/");
|
|
969
|
+
if (slash === -1) {
|
|
970
|
+
return { signature: null, segments: [], source: "", remainder: "", errors: ["no signature segment: the path is a single segment"] };
|
|
971
|
+
}
|
|
972
|
+
const signatureRaw = path.slice(0, slash);
|
|
973
|
+
const remainder = path.slice(slash + 1);
|
|
974
|
+
let signature;
|
|
975
|
+
const keyed = signatureRaw.match(/^([A-Za-z0-9_-]{1,32}):([0-9a-f]{64})$/);
|
|
976
|
+
const sealedTok = signatureRaw.match(/^(?:([A-Za-z0-9_-]{1,32}):)?s1\.([A-Za-z0-9_-]+)$/);
|
|
977
|
+
if (sealedTok) {
|
|
978
|
+
// A sealed token with a trailing path (issue #388):
|
|
979
|
+
// the server opens the token and serves the URL it
|
|
980
|
+
// carries; the visible remainder behind it is not
|
|
981
|
+
// part of that URL, so there is nothing to itemize.
|
|
982
|
+
signature = {
|
|
983
|
+
kind: "sealed",
|
|
984
|
+
value: `s1.${sealedTok[2]}`,
|
|
985
|
+
detail: "sealed token (AES-256-GCM): the operations and source are encrypted inside it; the server opens it and serves the URL it carries, and anything visible after the token is not part of that URL",
|
|
986
|
+
};
|
|
987
|
+
if (sealedTok[1]) {
|
|
988
|
+
signature.keyId = sealedTok[1];
|
|
989
|
+
}
|
|
990
|
+
return { signature, segments: [], source: "", remainder, errors };
|
|
991
|
+
}
|
|
992
|
+
if (signatureRaw === "unsafe") {
|
|
993
|
+
signature = { kind: "unsafe", value: signatureRaw, detail: "unsigned; the server honors it only with CRABCROP_ALLOW_UNSAFE or in presets-only deployments" };
|
|
994
|
+
}
|
|
995
|
+
else if (keyed) {
|
|
996
|
+
signature = { kind: "hmac", value: keyed[2], keyId: keyed[1], detail: `HMAC-SHA256 of the remainder, signed with key "${keyed[1]}"; the server verifies it against that key only (secret rotation)` };
|
|
997
|
+
}
|
|
998
|
+
else if (/^[0-9a-f]{64}$/.test(signatureRaw)) {
|
|
999
|
+
signature = { kind: "hmac", value: signatureRaw, detail: "HMAC-SHA256 of the remainder; paste the secret to verify it below" };
|
|
1000
|
+
}
|
|
1001
|
+
else if (/^[0-9a-f]+$/.test(signatureRaw)) {
|
|
1002
|
+
signature = { kind: "invalid", value: signatureRaw, detail: "lowercase hex, but not a full 64-character HMAC tag: the server will answer 403" };
|
|
1003
|
+
errors.push("malformed signature segment");
|
|
1004
|
+
}
|
|
1005
|
+
else if (/^(?:[A-Za-z0-9_-]{1,32}:)?s\d+\./.test(signatureRaw)) {
|
|
1006
|
+
signature = { kind: "invalid", value: signatureRaw, detail: "a sealed-token shape this scheme does not open (an unknown version marker, or a malformed token body): the server answers the uniform 403 refusal, indistinguishable from a token that fails to open" };
|
|
1007
|
+
errors.push("malformed signature segment");
|
|
1008
|
+
}
|
|
1009
|
+
else {
|
|
1010
|
+
signature = { kind: "invalid", value: signatureRaw, detail: "not `unsafe` and not lowercase hex: the server rejects it as malformed (400)" };
|
|
1011
|
+
errors.push("malformed signature segment");
|
|
1012
|
+
}
|
|
1013
|
+
const segments = [];
|
|
1014
|
+
let rest = remainder;
|
|
1015
|
+
let source = "";
|
|
1016
|
+
for (;;) {
|
|
1017
|
+
const idx = rest.indexOf("/");
|
|
1018
|
+
const seg = idx === -1 ? rest : rest.slice(0, idx);
|
|
1019
|
+
if (seg === "" && idx !== -1) {
|
|
1020
|
+
// An empty segment in the operations run (a doubled
|
|
1021
|
+
// slash): the server answers 400 (missing source URL)
|
|
1022
|
+
// instead of treating the rest as a source with a
|
|
1023
|
+
// leading slash (AP-23).
|
|
1024
|
+
const error = "empty segment in the path: the server answers 400 (missing source URL)";
|
|
1025
|
+
segments.push({ raw: seg, kind: "operation", title: "Empty segment", error });
|
|
1026
|
+
errors.push(error);
|
|
1027
|
+
rest = rest.slice(idx + 1);
|
|
1028
|
+
continue;
|
|
1029
|
+
}
|
|
1030
|
+
const explained = seg === "" ? null : explainSegment(seg, nowSeconds);
|
|
1031
|
+
if (!explained) {
|
|
1032
|
+
source = rest;
|
|
1033
|
+
break;
|
|
1034
|
+
}
|
|
1035
|
+
segments.push({ raw: seg, ...explained });
|
|
1036
|
+
if (explained.error) {
|
|
1037
|
+
errors.push(explained.error);
|
|
1038
|
+
}
|
|
1039
|
+
if (idx === -1) {
|
|
1040
|
+
errors.push("the URL ends after the operations: no source");
|
|
1041
|
+
rest = "";
|
|
1042
|
+
break;
|
|
1043
|
+
}
|
|
1044
|
+
rest = rest.slice(idx + 1);
|
|
1045
|
+
}
|
|
1046
|
+
if (!source && rest) {
|
|
1047
|
+
source = rest;
|
|
1048
|
+
}
|
|
1049
|
+
return { signature, segments, source, remainder, errors };
|
|
1050
|
+
}
|