@crabcrop/url 0.9.10 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +35 -7
- package/dist/bytes.d.ts +21 -0
- package/dist/bytes.js +86 -0
- package/dist/crypto/aes-gcm.d.ts +21 -0
- package/dist/crypto/aes-gcm.js +248 -0
- package/dist/crypto/node.d.ts +11 -0
- package/dist/crypto/node.js +41 -0
- package/dist/crypto/portable.d.ts +14 -0
- package/dist/crypto/portable.js +60 -0
- package/dist/crypto/sha256.d.ts +14 -0
- package/dist/crypto/sha256.js +131 -0
- package/dist/explain.d.ts +56 -8
- package/dist/explain.js +152 -63
- package/dist/index.d.ts +2 -1
- package/dist/index.js +60 -50
- package/dist/limits.d.ts +9 -0
- package/dist/limits.js +9 -0
- package/dist/operations.d.ts +8 -18
- package/dist/operations.js +139 -70
- package/package.json +11 -3
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SHA-256 (FIPS 180-4) and HMAC-SHA-256 (RFC 2104) in plain
|
|
3
|
+
* TypeScript, for runtimes without `node:crypto`. WebCrypto
|
|
4
|
+
* cannot stand in: `crypto.subtle` is asynchronous, and the
|
|
5
|
+
* SDK's signing functions are synchronous.
|
|
6
|
+
*
|
|
7
|
+
* All arithmetic is on 32-bit words with no data-dependent
|
|
8
|
+
* branches or table indices, so the time taken depends only
|
|
9
|
+
* on the input lengths.
|
|
10
|
+
*/
|
|
11
|
+
// Int32Array keeps every word a small integer to V8 (a
|
|
12
|
+
// Uint32Array would hand back doubles above 2^31).
|
|
13
|
+
const K = new Int32Array([
|
|
14
|
+
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
|
|
15
|
+
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
|
|
16
|
+
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
|
17
|
+
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
|
|
18
|
+
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
|
|
19
|
+
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
|
20
|
+
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
|
|
21
|
+
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
|
|
22
|
+
]);
|
|
23
|
+
const BLOCK = 64;
|
|
24
|
+
// Scratch space, reused across calls: the functions here are
|
|
25
|
+
// synchronous and never re-entered, and reusing it keeps a
|
|
26
|
+
// signature from allocating a dozen typed arrays.
|
|
27
|
+
const W = new Int32Array(64);
|
|
28
|
+
const TAIL = new Uint8Array(2 * BLOCK);
|
|
29
|
+
const TAIL_VIEW = new DataView(TAIL.buffer);
|
|
30
|
+
const PAD = new Uint8Array(BLOCK);
|
|
31
|
+
/** One compression round over the 64-byte block at `offset`. */
|
|
32
|
+
function compress(state, view, offset) {
|
|
33
|
+
const w = W;
|
|
34
|
+
for (let i = 0; i < 16; i++) {
|
|
35
|
+
w[i] = view.getInt32(offset + i * 4);
|
|
36
|
+
}
|
|
37
|
+
for (let i = 16; i < 64; i++) {
|
|
38
|
+
const a = w[i - 15];
|
|
39
|
+
const b = w[i - 2];
|
|
40
|
+
const s0 = ((a >>> 7) | (a << 25)) ^ ((a >>> 18) | (a << 14)) ^ (a >>> 3);
|
|
41
|
+
const s1 = ((b >>> 17) | (b << 15)) ^ ((b >>> 19) | (b << 13)) ^ (b >>> 10);
|
|
42
|
+
w[i] = (w[i - 16] + s0 + w[i - 7] + s1) | 0;
|
|
43
|
+
}
|
|
44
|
+
let a = state[0];
|
|
45
|
+
let b = state[1];
|
|
46
|
+
let c = state[2];
|
|
47
|
+
let d = state[3];
|
|
48
|
+
let e = state[4];
|
|
49
|
+
let f = state[5];
|
|
50
|
+
let g = state[6];
|
|
51
|
+
let h = state[7];
|
|
52
|
+
for (let i = 0; i < 64; i++) {
|
|
53
|
+
const S1 = ((e >>> 6) | (e << 26)) ^ ((e >>> 11) | (e << 21)) ^ ((e >>> 25) | (e << 7));
|
|
54
|
+
const ch = (e & f) ^ (~e & g);
|
|
55
|
+
const t1 = (h + S1 + ch + K[i] + w[i]) | 0;
|
|
56
|
+
const S0 = ((a >>> 2) | (a << 30)) ^ ((a >>> 13) | (a << 19)) ^ ((a >>> 22) | (a << 10));
|
|
57
|
+
const maj = (a & b) ^ (a & c) ^ (b & c);
|
|
58
|
+
const t2 = (S0 + maj) | 0;
|
|
59
|
+
h = g;
|
|
60
|
+
g = f;
|
|
61
|
+
f = e;
|
|
62
|
+
e = (d + t1) | 0;
|
|
63
|
+
d = c;
|
|
64
|
+
c = b;
|
|
65
|
+
b = a;
|
|
66
|
+
a = (t1 + t2) | 0;
|
|
67
|
+
}
|
|
68
|
+
state[0] = (state[0] + a) | 0;
|
|
69
|
+
state[1] = (state[1] + b) | 0;
|
|
70
|
+
state[2] = (state[2] + c) | 0;
|
|
71
|
+
state[3] = (state[3] + d) | 0;
|
|
72
|
+
state[4] = (state[4] + e) | 0;
|
|
73
|
+
state[5] = (state[5] + f) | 0;
|
|
74
|
+
state[6] = (state[6] + g) | 0;
|
|
75
|
+
state[7] = (state[7] + h) | 0;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* SHA-256 of `prefix || data`, where `prefix` is one whole
|
|
79
|
+
* block or absent: HMAC's padded keys go in without copying
|
|
80
|
+
* the message behind them.
|
|
81
|
+
*/
|
|
82
|
+
function digest(prefix, data) {
|
|
83
|
+
const state = Int32Array.of(0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19);
|
|
84
|
+
if (prefix !== null) {
|
|
85
|
+
compress(state, new DataView(prefix.buffer, prefix.byteOffset, BLOCK), 0);
|
|
86
|
+
}
|
|
87
|
+
const view = new DataView(data.buffer, data.byteOffset, data.byteLength);
|
|
88
|
+
const whole = data.length - (data.length % BLOCK);
|
|
89
|
+
for (let offset = 0; offset < whole; offset += BLOCK) {
|
|
90
|
+
compress(state, view, offset);
|
|
91
|
+
}
|
|
92
|
+
// The tail: the leftover bytes, a 0x80 byte, zeros, and the
|
|
93
|
+
// message length in bits as a big-endian 64-bit integer,
|
|
94
|
+
// split into words without leaving the safe-integer range.
|
|
95
|
+
const rest = data.length - whole;
|
|
96
|
+
const tailLength = rest < BLOCK - 8 ? BLOCK : 2 * BLOCK;
|
|
97
|
+
const total = data.length + (prefix === null ? 0 : BLOCK);
|
|
98
|
+
TAIL.fill(0);
|
|
99
|
+
TAIL.set(data.subarray(whole));
|
|
100
|
+
TAIL[rest] = 0x80;
|
|
101
|
+
TAIL_VIEW.setUint32(tailLength - 8, Math.floor(total / 0x20000000));
|
|
102
|
+
TAIL_VIEW.setUint32(tailLength - 4, (total << 3) >>> 0);
|
|
103
|
+
compress(state, TAIL_VIEW, 0);
|
|
104
|
+
if (tailLength === 2 * BLOCK) {
|
|
105
|
+
compress(state, TAIL_VIEW, BLOCK);
|
|
106
|
+
}
|
|
107
|
+
const out = new Uint8Array(32);
|
|
108
|
+
const outView = new DataView(out.buffer);
|
|
109
|
+
for (let i = 0; i < 8; i++) {
|
|
110
|
+
outView.setInt32(i * 4, state[i]);
|
|
111
|
+
}
|
|
112
|
+
return out;
|
|
113
|
+
}
|
|
114
|
+
/** The SHA-256 digest of `data`. */
|
|
115
|
+
export function sha256(data) {
|
|
116
|
+
return digest(null, data);
|
|
117
|
+
}
|
|
118
|
+
/** HMAC-SHA-256 of `message` under `key` (RFC 2104). */
|
|
119
|
+
export function hmacSha256(key, message) {
|
|
120
|
+
const k = key.length > BLOCK ? sha256(key) : key;
|
|
121
|
+
PAD.fill(0x36);
|
|
122
|
+
for (let i = 0; i < k.length; i++) {
|
|
123
|
+
PAD[i] ^= k[i];
|
|
124
|
+
}
|
|
125
|
+
const inner = digest(PAD, message);
|
|
126
|
+
// opad = ipad ^ (0x36 ^ 0x5c), byte for byte.
|
|
127
|
+
for (let i = 0; i < BLOCK; i++) {
|
|
128
|
+
PAD[i] ^= 0x36 ^ 0x5c;
|
|
129
|
+
}
|
|
130
|
+
return digest(PAD, inner);
|
|
131
|
+
}
|
package/dist/explain.d.ts
CHANGED
|
@@ -1,14 +1,7 @@
|
|
|
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
1
|
/** The segment kinds an explanation is made of. */
|
|
9
2
|
export type SegmentKind = "expiry" | "maxage" | "debug" | "preset" | "operation";
|
|
10
3
|
/** The signature kinds an explanation can report. */
|
|
11
|
-
export type SignatureKind = "hmac" | "unsafe" | "invalid";
|
|
4
|
+
export type SignatureKind = "hmac" | "unsafe" | "sealed" | "invalid";
|
|
12
5
|
export interface ExplainedSegment {
|
|
13
6
|
raw: string;
|
|
14
7
|
kind: SegmentKind;
|
|
@@ -30,6 +23,61 @@ export interface Explanation {
|
|
|
30
23
|
errors: string[];
|
|
31
24
|
}
|
|
32
25
|
export declare const FILTER_DESCRIPTIONS: Record<string, string>;
|
|
26
|
+
/** Crop alignments (url.rs parse_align); `center` is the bare `crop:WxH`. */
|
|
27
|
+
export declare const CROP_ALIGNS: readonly ["center", "smart", "face", "top", "bottom", "left", "right", "top-left", "top-right", "bottom-left", "bottom-right"];
|
|
28
|
+
/** Text overlay anchors (url.rs Anchor::parse). */
|
|
29
|
+
export declare const TEXT_ANCHORS: readonly ["top-left", "top-center", "top-right", "center-left", "center", "center-right", "bottom-left", "bottom-center", "bottom-right"];
|
|
30
|
+
/** Output formats `format(...)` names; `jpg` is an input alias of `jpeg`. */
|
|
31
|
+
export declare const IMAGE_FORMATS: readonly ["jpeg", "jpg", "png", "webp", "jxl", "avif", "gif", "mp4", "webm"];
|
|
32
|
+
/**
|
|
33
|
+
* Formats a per-format `quality(...)` entry names, in
|
|
34
|
+
* canonical (name) order: the still formats, since a video
|
|
35
|
+
* format's quality is not the server's to set. `jpg` is
|
|
36
|
+
* accepted as the input alias of `jpeg`.
|
|
37
|
+
*/
|
|
38
|
+
export declare const QUALITY_FORMATS: readonly ["avif", "gif", "jpeg", "jxl", "png", "webp"];
|
|
39
|
+
/** JPEG chroma subsamplings `format(jpeg,subsampling:...)` takes. */
|
|
40
|
+
export declare const JPEG_SUBSAMPLINGS: readonly [444, 422, 420];
|
|
41
|
+
/** Overlay blend modes. */
|
|
42
|
+
export declare const BLEND_MODES: readonly ["normal", "multiply", "screen", "overlay"];
|
|
43
|
+
/** Channels `curve(...)` remaps. */
|
|
44
|
+
export declare const CURVE_CHANNELS: readonly ["rgb", "r", "g", "b"];
|
|
45
|
+
/** Modes `studio(...)` runs. */
|
|
46
|
+
export declare const STUDIO_MODES: readonly ["product", "scene"];
|
|
47
|
+
/** Named backdrops for `studio(product,bg:...)`; any RRGGBB color also works. */
|
|
48
|
+
export declare const STUDIO_BACKDROPS: readonly ["white", "brand", "none"];
|
|
49
|
+
/** Contact shadow strengths for `studio(product, ...)`. */
|
|
50
|
+
export declare const STUDIO_SHADOWS: readonly ["soft", "strong", "none"];
|
|
51
|
+
/** Factors `upscale(...)` takes. */
|
|
52
|
+
export declare const UPSCALE_FACTORS: readonly [2, 4];
|
|
53
|
+
/** Device pixel ratios `dpr(...)` takes. */
|
|
54
|
+
export declare const DPR_FACTORS: readonly [1, 1.5, 2, 3];
|
|
55
|
+
/** Shapes `mask(...)` cuts. */
|
|
56
|
+
export declare const MASK_SHAPES: readonly ["circle", "ellipse"];
|
|
57
|
+
/** Axes `flip(...)` mirrors across. */
|
|
58
|
+
export declare const FLIP_AXES: readonly ["h", "v"];
|
|
59
|
+
/** Whether `value` is a member of the closed set `list`. */
|
|
60
|
+
export declare function isOneOf<T extends string | number>(list: readonly T[], value: unknown): value is T;
|
|
61
|
+
/**
|
|
62
|
+
* The objects crop's cap on distinct class names (url.rs
|
|
63
|
+
* MAX_OBJECTS_CLASSES).
|
|
64
|
+
*/
|
|
65
|
+
export declare const MAX_OBJECTS_CLASSES = 32;
|
|
66
|
+
/**
|
|
67
|
+
* A detection class name as the server's parse_class_name
|
|
68
|
+
* takes it, already trimmed: 1 to 64 UTF-8 bytes (bytes, not
|
|
69
|
+
* UTF-16 code units, as on the server) of letters, digits,
|
|
70
|
+
* space, `_`, `.`, `-`, `/`.
|
|
71
|
+
*/
|
|
72
|
+
export declare function isClassName(name: string): boolean;
|
|
73
|
+
/**
|
|
74
|
+
* The key a class name folds to when the objects crop drops
|
|
75
|
+
* repeats. The server compares names with
|
|
76
|
+
* `eq_ignore_ascii_case` (url.rs parse_crop_align), so only
|
|
77
|
+
* A to Z fold: `E` and `e` are one class, `É` and `é` are two
|
|
78
|
+
* (issue #482).
|
|
79
|
+
*/
|
|
80
|
+
export declare function classFoldKey(name: string): string;
|
|
33
81
|
/**
|
|
34
82
|
* Parse a crabcrop URL (or bare path) back into its pieces,
|
|
35
83
|
* explaining each segment. Mirrors the server's dispatch:
|
package/dist/explain.js
CHANGED
|
@@ -1,10 +1,15 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Parse a crabcrop URL (or bare path) back into its pieces and
|
|
3
3
|
* explain each segment, mirroring the server's grammar. This is
|
|
4
|
-
* the
|
|
5
|
-
*
|
|
6
|
-
*
|
|
4
|
+
* the one JavaScript implementation of the grammar (issue #482):
|
|
5
|
+
* the typed builders in operations.ts validate with the checks
|
|
6
|
+
* exported here, and the playground serves this module's build
|
|
7
|
+
* (playground/sdk/, refreshed by scripts/sync_playground_sdk.py,
|
|
8
|
+
* whose --check fails on drift or on a second copy). Every
|
|
9
|
+
* surface is pinned to the same verdict corpora (audit PL-10,
|
|
10
|
+
* P-3).
|
|
7
11
|
*/
|
|
12
|
+
import { MAX_DIMENSION } from "./limits.js";
|
|
8
13
|
export const FILTER_DESCRIPTIONS = {
|
|
9
14
|
grayscale: "convert to grayscale",
|
|
10
15
|
invert: "invert the colors",
|
|
@@ -58,7 +63,6 @@ export const FILTER_DESCRIPTIONS = {
|
|
|
58
63
|
pixelate_region: "pixelate the rectangle (x,y,w,h) in pixels at this point in the chain (redaction)",
|
|
59
64
|
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
65
|
};
|
|
61
|
-
const MAX_DIMENSION = 16383;
|
|
62
66
|
function parseDimension(part) {
|
|
63
67
|
let flip = false;
|
|
64
68
|
let digits = part;
|
|
@@ -105,6 +109,84 @@ function sizeBlurb(seg) {
|
|
|
105
109
|
].filter(Boolean);
|
|
106
110
|
return `resize to ${describe(size.w, "width")} by ${describe(size.h, "height")}${flips.length ? ` (${flips.join(", ")})` : ""}`;
|
|
107
111
|
}
|
|
112
|
+
// The grammar's closed sets, declared once for the explainer
|
|
113
|
+
// and the typed builders (issue #482). Each is `as const` so
|
|
114
|
+
// test/grammar-sets.test.ts can check it against the union
|
|
115
|
+
// type operations.ts gives callers: a value added to one side
|
|
116
|
+
// only fails the type check.
|
|
117
|
+
/** Crop alignments (url.rs parse_align); `center` is the bare `crop:WxH`. */
|
|
118
|
+
export const CROP_ALIGNS = ["center", "smart", "face", "top", "bottom", "left", "right", "top-left", "top-right", "bottom-left", "bottom-right"];
|
|
119
|
+
/** Text overlay anchors (url.rs Anchor::parse). */
|
|
120
|
+
export const TEXT_ANCHORS = [
|
|
121
|
+
"top-left",
|
|
122
|
+
"top-center",
|
|
123
|
+
"top-right",
|
|
124
|
+
"center-left",
|
|
125
|
+
"center",
|
|
126
|
+
"center-right",
|
|
127
|
+
"bottom-left",
|
|
128
|
+
"bottom-center",
|
|
129
|
+
"bottom-right",
|
|
130
|
+
];
|
|
131
|
+
/** Output formats `format(...)` names; `jpg` is an input alias of `jpeg`. */
|
|
132
|
+
export const IMAGE_FORMATS = ["jpeg", "jpg", "png", "webp", "jxl", "avif", "gif", "mp4", "webm"];
|
|
133
|
+
/**
|
|
134
|
+
* Formats a per-format `quality(...)` entry names, in
|
|
135
|
+
* canonical (name) order: the still formats, since a video
|
|
136
|
+
* format's quality is not the server's to set. `jpg` is
|
|
137
|
+
* accepted as the input alias of `jpeg`.
|
|
138
|
+
*/
|
|
139
|
+
export const QUALITY_FORMATS = ["avif", "gif", "jpeg", "jxl", "png", "webp"];
|
|
140
|
+
/** JPEG chroma subsamplings `format(jpeg,subsampling:...)` takes. */
|
|
141
|
+
export const JPEG_SUBSAMPLINGS = [444, 422, 420];
|
|
142
|
+
/** Overlay blend modes. */
|
|
143
|
+
export const BLEND_MODES = ["normal", "multiply", "screen", "overlay"];
|
|
144
|
+
/** Channels `curve(...)` remaps. */
|
|
145
|
+
export const CURVE_CHANNELS = ["rgb", "r", "g", "b"];
|
|
146
|
+
/** Modes `studio(...)` runs. */
|
|
147
|
+
export const STUDIO_MODES = ["product", "scene"];
|
|
148
|
+
/** Named backdrops for `studio(product,bg:...)`; any RRGGBB color also works. */
|
|
149
|
+
export const STUDIO_BACKDROPS = ["white", "brand", "none"];
|
|
150
|
+
/** Contact shadow strengths for `studio(product, ...)`. */
|
|
151
|
+
export const STUDIO_SHADOWS = ["soft", "strong", "none"];
|
|
152
|
+
/** Factors `upscale(...)` takes. */
|
|
153
|
+
export const UPSCALE_FACTORS = [2, 4];
|
|
154
|
+
/** Device pixel ratios `dpr(...)` takes. */
|
|
155
|
+
export const DPR_FACTORS = [1, 1.5, 2, 3];
|
|
156
|
+
/** Shapes `mask(...)` cuts. */
|
|
157
|
+
export const MASK_SHAPES = ["circle", "ellipse"];
|
|
158
|
+
/** Axes `flip(...)` mirrors across. */
|
|
159
|
+
export const FLIP_AXES = ["h", "v"];
|
|
160
|
+
/** Whether `value` is a member of the closed set `list`. */
|
|
161
|
+
export function isOneOf(list, value) {
|
|
162
|
+
return list.includes(value);
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* The objects crop's cap on distinct class names (url.rs
|
|
166
|
+
* MAX_OBJECTS_CLASSES).
|
|
167
|
+
*/
|
|
168
|
+
export const MAX_OBJECTS_CLASSES = 32;
|
|
169
|
+
/**
|
|
170
|
+
* A detection class name as the server's parse_class_name
|
|
171
|
+
* takes it, already trimmed: 1 to 64 UTF-8 bytes (bytes, not
|
|
172
|
+
* UTF-16 code units, as on the server) of letters, digits,
|
|
173
|
+
* space, `_`, `.`, `-`, `/`.
|
|
174
|
+
*/
|
|
175
|
+
export function isClassName(name) {
|
|
176
|
+
return (name.length >= 1 &&
|
|
177
|
+
new TextEncoder().encode(name).length <= 64 &&
|
|
178
|
+
/^[\p{L}\p{N} _.\-/]+$/u.test(name));
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* The key a class name folds to when the objects crop drops
|
|
182
|
+
* repeats. The server compares names with
|
|
183
|
+
* `eq_ignore_ascii_case` (url.rs parse_crop_align), so only
|
|
184
|
+
* A to Z fold: `E` and `e` are one class, `É` and `é` are two
|
|
185
|
+
* (issue #482).
|
|
186
|
+
*/
|
|
187
|
+
export function classFoldKey(name) {
|
|
188
|
+
return name.replace(/[A-Z]/g, (c) => c.toLowerCase());
|
|
189
|
+
}
|
|
108
190
|
const NO_ARG_FILTERS = new Set([
|
|
109
191
|
"grayscale",
|
|
110
192
|
"invert",
|
|
@@ -117,18 +199,8 @@ const NO_ARG_FILTERS = new Set([
|
|
|
117
199
|
"blurfaces",
|
|
118
200
|
"pixelatefaces",
|
|
119
201
|
]);
|
|
120
|
-
const FORMAT_NAMES = new Set(
|
|
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
|
-
]);
|
|
202
|
+
const FORMAT_NAMES = new Set(IMAGE_FORMATS);
|
|
203
|
+
const ANCHOR_NAMES = new Set(TEXT_ANCHORS);
|
|
132
204
|
function isIntIn(s, min, max) {
|
|
133
205
|
// Rust's integer from_str syntax: an optional sign (the
|
|
134
206
|
// unsigned parsers accept '+' too), then digits.
|
|
@@ -246,9 +318,9 @@ function validateFilter(name, args) {
|
|
|
246
318
|
else {
|
|
247
319
|
const name = part.slice(0, colon);
|
|
248
320
|
const value = part.slice(colon + 1);
|
|
249
|
-
if (!FORMAT_NAMES.has(name) || name === "mp4" || name === "webm")
|
|
250
|
-
return bad;
|
|
251
321
|
const canonical = name === "jpg" ? "jpeg" : name;
|
|
322
|
+
if (!isOneOf(QUALITY_FORMATS, canonical))
|
|
323
|
+
return bad;
|
|
252
324
|
if (seen.has(canonical))
|
|
253
325
|
return bad;
|
|
254
326
|
seen.add(canonical);
|
|
@@ -278,7 +350,7 @@ function validateFilter(name, args) {
|
|
|
278
350
|
if (name === "jpeg" || name === "jpg") {
|
|
279
351
|
if (option === "progressive")
|
|
280
352
|
continue;
|
|
281
|
-
if (
|
|
353
|
+
if (JPEG_SUBSAMPLINGS.some((s) => option === `subsampling:${s}`))
|
|
282
354
|
continue;
|
|
283
355
|
return bad;
|
|
284
356
|
}
|
|
@@ -308,7 +380,7 @@ function validateFilter(name, args) {
|
|
|
308
380
|
if (args === null)
|
|
309
381
|
return bad;
|
|
310
382
|
const parts = args.split(",");
|
|
311
|
-
if (!
|
|
383
|
+
if (!isOneOf(CURVE_CHANNELS, parts[0]))
|
|
312
384
|
return bad;
|
|
313
385
|
const coords = parts.slice(1);
|
|
314
386
|
if (coords.length < 4 || coords.length > 32 || coords.length % 2 !== 0)
|
|
@@ -339,7 +411,7 @@ function validateFilter(name, args) {
|
|
|
339
411
|
? null
|
|
340
412
|
: bad;
|
|
341
413
|
case "upscale":
|
|
342
|
-
return args !== null && /^[+-]?\d+$/.test(args) && (
|
|
414
|
+
return args !== null && /^[+-]?\d+$/.test(args) && isOneOf(UPSCALE_FACTORS, Number(args))
|
|
343
415
|
? null
|
|
344
416
|
: bad;
|
|
345
417
|
case "studio": {
|
|
@@ -354,7 +426,7 @@ function validateFilter(name, args) {
|
|
|
354
426
|
return bad;
|
|
355
427
|
const parts = args.split(",");
|
|
356
428
|
const mode = parts[0];
|
|
357
|
-
if (
|
|
429
|
+
if (!isOneOf(STUDIO_MODES, mode))
|
|
358
430
|
return bad;
|
|
359
431
|
const seen = new Set();
|
|
360
432
|
for (const part of parts.slice(1)) {
|
|
@@ -374,15 +446,11 @@ function validateFilter(name, args) {
|
|
|
374
446
|
return bad;
|
|
375
447
|
}
|
|
376
448
|
else if (mode === "product" && key === "bg") {
|
|
377
|
-
if (value
|
|
378
|
-
value !== "brand" &&
|
|
379
|
-
value !== "none" &&
|
|
380
|
-
!/^[0-9a-fA-F]{6}$/.test(value)) {
|
|
449
|
+
if (!isOneOf(STUDIO_BACKDROPS, value) && !/^[0-9a-fA-F]{6}$/.test(value))
|
|
381
450
|
return bad;
|
|
382
|
-
}
|
|
383
451
|
}
|
|
384
452
|
else if (mode === "product" && key === "shadow") {
|
|
385
|
-
if (
|
|
453
|
+
if (!isOneOf(STUDIO_SHADOWS, value))
|
|
386
454
|
return bad;
|
|
387
455
|
}
|
|
388
456
|
else {
|
|
@@ -392,7 +460,7 @@ function validateFilter(name, args) {
|
|
|
392
460
|
return null;
|
|
393
461
|
}
|
|
394
462
|
case "dpr":
|
|
395
|
-
return args !== null && isFloat(args) &&
|
|
463
|
+
return args !== null && isFloat(args) && isOneOf(DPR_FACTORS, Number(args))
|
|
396
464
|
? null
|
|
397
465
|
: bad;
|
|
398
466
|
case "rotate":
|
|
@@ -406,7 +474,7 @@ function validateFilter(name, args) {
|
|
|
406
474
|
return bad;
|
|
407
475
|
const parts = args.split(",");
|
|
408
476
|
return (parts.length === 1 || parts.length === 4) &&
|
|
409
|
-
parts.every((p) => isIntIn(p, 0,
|
|
477
|
+
parts.every((p) => isIntIn(p, 0, MAX_DIMENSION))
|
|
410
478
|
? null
|
|
411
479
|
: bad;
|
|
412
480
|
}
|
|
@@ -415,13 +483,13 @@ function validateFilter(name, args) {
|
|
|
415
483
|
return bad;
|
|
416
484
|
const parts = args.split(",");
|
|
417
485
|
return parts.length === 2 &&
|
|
418
|
-
isIntIn(parts[0], 0,
|
|
486
|
+
isIntIn(parts[0], 0, MAX_DIMENSION) &&
|
|
419
487
|
/^[0-9a-fA-F]{6}$/.test(parts[1])
|
|
420
488
|
? null
|
|
421
489
|
: bad;
|
|
422
490
|
}
|
|
423
491
|
case "mask":
|
|
424
|
-
return
|
|
492
|
+
return isOneOf(MASK_SHAPES, args) ? null : bad;
|
|
425
493
|
case "duotone": {
|
|
426
494
|
if (args === null)
|
|
427
495
|
return bad;
|
|
@@ -431,9 +499,9 @@ function validateFilter(name, args) {
|
|
|
431
499
|
case "monochrome":
|
|
432
500
|
return args !== null && /^[0-9a-fA-F]{6}$/.test(args) ? null : bad;
|
|
433
501
|
case "pixelate":
|
|
434
|
-
return args !== null && isIntIn(args, 1,
|
|
502
|
+
return args !== null && isIntIn(args, 1, MAX_DIMENSION) ? null : bad;
|
|
435
503
|
case "flip":
|
|
436
|
-
return
|
|
504
|
+
return isOneOf(FLIP_AXES, args) ? null : bad;
|
|
437
505
|
case "overlay": {
|
|
438
506
|
if (args === null)
|
|
439
507
|
return bad;
|
|
@@ -451,7 +519,7 @@ function validateFilter(name, args) {
|
|
|
451
519
|
rest = fields.slice(0, -1);
|
|
452
520
|
}
|
|
453
521
|
const blend = rest[rest.length - 1];
|
|
454
|
-
if (!
|
|
522
|
+
if (!isOneOf(BLEND_MODES, blend))
|
|
455
523
|
return bad;
|
|
456
524
|
if (!isIntIn(rest[rest.length - 2], 0, 100))
|
|
457
525
|
return bad;
|
|
@@ -467,14 +535,7 @@ function validateFilter(name, args) {
|
|
|
467
535
|
if (args === null)
|
|
468
536
|
return bad;
|
|
469
537
|
const decoded = percentDecodable(args);
|
|
470
|
-
|
|
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;
|
|
538
|
+
return decoded !== null && isClassName(decoded.trim()) ? null : bad;
|
|
478
539
|
}
|
|
479
540
|
case "recolorclass": {
|
|
480
541
|
if (args === null)
|
|
@@ -485,14 +546,7 @@ function validateFilter(name, args) {
|
|
|
485
546
|
if (!/^[0-9a-fA-F]{6}$/.test(args.slice(cut + 1)))
|
|
486
547
|
return bad;
|
|
487
548
|
const decoded = percentDecodable(args.slice(0, cut));
|
|
488
|
-
|
|
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;
|
|
549
|
+
return decoded !== null && isClassName(decoded.trim()) ? null : bad;
|
|
496
550
|
}
|
|
497
551
|
case "blur_region":
|
|
498
552
|
case "pixelate_region":
|
|
@@ -501,7 +555,7 @@ function validateFilter(name, args) {
|
|
|
501
555
|
return bad;
|
|
502
556
|
const parts = args.split(",");
|
|
503
557
|
return parts.length === 4 &&
|
|
504
|
-
parts.every((p) => isIntIn(p, 0,
|
|
558
|
+
parts.every((p) => isIntIn(p, 0, MAX_DIMENSION)) &&
|
|
505
559
|
Number(parts[2]) >= 1 &&
|
|
506
560
|
Number(parts[3]) >= 1
|
|
507
561
|
? null
|
|
@@ -599,14 +653,17 @@ function explainFilters(rest) {
|
|
|
599
653
|
}
|
|
600
654
|
return { detail: blurbs.join("; ") };
|
|
601
655
|
}
|
|
602
|
-
const ALIGN_NAMES = new Set(
|
|
656
|
+
const ALIGN_NAMES = new Set(CROP_ALIGNS);
|
|
603
657
|
const U64_MAX = 18446744073709551615n;
|
|
604
658
|
/**
|
|
605
659
|
* The objects crop alignment (url.rs parse_objects, ported):
|
|
606
660
|
* `objects(<class>[,<class>...])[:<scale>]`. Returns the
|
|
607
|
-
* decoded class names (duplicates folded, first
|
|
608
|
-
* kept, as the server's canonical form folds them)
|
|
609
|
-
* scale, or null when the server would reject the
|
|
661
|
+
* decoded class names (duplicates folded ASCII-only, first
|
|
662
|
+
* spelling kept, as the server's canonical form folds them)
|
|
663
|
+
* and the scale, or null when the server would reject the
|
|
664
|
+
* form. The 32-class cap counts distinct names, as the
|
|
665
|
+
* server's does: repeats are free, and a 33rd distinct name
|
|
666
|
+
* rejects the form (issue #408).
|
|
610
667
|
*/
|
|
611
668
|
function parseObjectsAlign(align) {
|
|
612
669
|
if (!align.startsWith("objects("))
|
|
@@ -629,8 +686,6 @@ function parseObjectsAlign(align) {
|
|
|
629
686
|
return null;
|
|
630
687
|
}
|
|
631
688
|
const parts = list.split(",");
|
|
632
|
-
if (parts.length > 32)
|
|
633
|
-
return null;
|
|
634
689
|
const classes = [];
|
|
635
690
|
const seen = new Set();
|
|
636
691
|
for (const part of parts) {
|
|
@@ -638,13 +693,12 @@ function parseObjectsAlign(align) {
|
|
|
638
693
|
if (decoded === null)
|
|
639
694
|
return null;
|
|
640
695
|
const name = decoded.trim();
|
|
641
|
-
if (name
|
|
642
|
-
new TextEncoder().encode(name).length > 64 ||
|
|
643
|
-
!/^[\p{L}\p{N} _.\-/]+$/u.test(name)) {
|
|
696
|
+
if (!isClassName(name))
|
|
644
697
|
return null;
|
|
645
|
-
|
|
646
|
-
const key = name.toLowerCase();
|
|
698
|
+
const key = classFoldKey(name);
|
|
647
699
|
if (!seen.has(key)) {
|
|
700
|
+
if (classes.length === MAX_OBJECTS_CLASSES)
|
|
701
|
+
return null;
|
|
648
702
|
seen.add(key);
|
|
649
703
|
classes.push(name);
|
|
650
704
|
}
|
|
@@ -947,6 +1001,21 @@ export function explainUrl(input, nowSeconds = Math.floor(Date.now() / 1000)) {
|
|
|
947
1001
|
path = path.slice(0, cut);
|
|
948
1002
|
}
|
|
949
1003
|
const errors = [];
|
|
1004
|
+
// A sealed URL is a single segment: the token carries
|
|
1005
|
+
// the whole remainder encrypted, so there is no slash
|
|
1006
|
+
// and nothing visible to itemize (issue #388).
|
|
1007
|
+
const sealedWhole = path.match(/^(?:([A-Za-z0-9_-]{1,32}):)?s1\.([A-Za-z0-9_-]+)$/);
|
|
1008
|
+
if (sealedWhole) {
|
|
1009
|
+
const signature = {
|
|
1010
|
+
kind: "sealed",
|
|
1011
|
+
value: `s1.${sealedWhole[2]}`,
|
|
1012
|
+
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",
|
|
1013
|
+
};
|
|
1014
|
+
if (sealedWhole[1]) {
|
|
1015
|
+
signature.keyId = sealedWhole[1];
|
|
1016
|
+
}
|
|
1017
|
+
return { signature, segments: [], source: "", remainder: "", errors: [] };
|
|
1018
|
+
}
|
|
950
1019
|
const slash = path.indexOf("/");
|
|
951
1020
|
if (slash === -1) {
|
|
952
1021
|
return { signature: null, segments: [], source: "", remainder: "", errors: ["no signature segment: the path is a single segment"] };
|
|
@@ -955,6 +1024,22 @@ export function explainUrl(input, nowSeconds = Math.floor(Date.now() / 1000)) {
|
|
|
955
1024
|
const remainder = path.slice(slash + 1);
|
|
956
1025
|
let signature;
|
|
957
1026
|
const keyed = signatureRaw.match(/^([A-Za-z0-9_-]{1,32}):([0-9a-f]{64})$/);
|
|
1027
|
+
const sealedTok = signatureRaw.match(/^(?:([A-Za-z0-9_-]{1,32}):)?s1\.([A-Za-z0-9_-]+)$/);
|
|
1028
|
+
if (sealedTok) {
|
|
1029
|
+
// A sealed token with a trailing path (issue #388):
|
|
1030
|
+
// the server opens the token and serves the URL it
|
|
1031
|
+
// carries; the visible remainder behind it is not
|
|
1032
|
+
// part of that URL, so there is nothing to itemize.
|
|
1033
|
+
signature = {
|
|
1034
|
+
kind: "sealed",
|
|
1035
|
+
value: `s1.${sealedTok[2]}`,
|
|
1036
|
+
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",
|
|
1037
|
+
};
|
|
1038
|
+
if (sealedTok[1]) {
|
|
1039
|
+
signature.keyId = sealedTok[1];
|
|
1040
|
+
}
|
|
1041
|
+
return { signature, segments: [], source: "", remainder, errors };
|
|
1042
|
+
}
|
|
958
1043
|
if (signatureRaw === "unsafe") {
|
|
959
1044
|
signature = { kind: "unsafe", value: signatureRaw, detail: "unsigned; the server honors it only with CRABCROP_ALLOW_UNSAFE or in presets-only deployments" };
|
|
960
1045
|
}
|
|
@@ -968,6 +1053,10 @@ export function explainUrl(input, nowSeconds = Math.floor(Date.now() / 1000)) {
|
|
|
968
1053
|
signature = { kind: "invalid", value: signatureRaw, detail: "lowercase hex, but not a full 64-character HMAC tag: the server will answer 403" };
|
|
969
1054
|
errors.push("malformed signature segment");
|
|
970
1055
|
}
|
|
1056
|
+
else if (/^(?:[A-Za-z0-9_-]{1,32}:)?s\d+\./.test(signatureRaw)) {
|
|
1057
|
+
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" };
|
|
1058
|
+
errors.push("malformed signature segment");
|
|
1059
|
+
}
|
|
971
1060
|
else {
|
|
972
1061
|
signature = { kind: "invalid", value: signatureRaw, detail: "not `unsafe` and not lowercase hex: the server rejects it as malformed (400)" };
|
|
973
1062
|
errors.push("malformed signature segment");
|
package/dist/index.d.ts
CHANGED
|
@@ -145,8 +145,9 @@ export declare function buildSrcSet(base: string, secret: string | null, source:
|
|
|
145
145
|
export { explainUrl } from "./explain.ts";
|
|
146
146
|
export { FILTER_DESCRIPTIONS } from "./explain.ts";
|
|
147
147
|
export type { ExplainedSegment, Explanation, SegmentKind, SignatureKind } from "./explain.ts";
|
|
148
|
+
export { MAX_DIMENSION } from "./limits.ts";
|
|
148
149
|
export { filter, joinOps, ops } from "./operations.ts";
|
|
149
|
-
export type { Anchor, BlendMode, CropAlign, CurveChannel, Dimension, ImageFormat, MaskShape, StudioMode, StudioOptions, StudioShadow, } from "./operations.ts";
|
|
150
|
+
export type { Anchor, BlendMode, CropAlign, CurveChannel, Dimension, FormatOptions, ImageFormat, MaskShape, PerFormatQualityFormat, StudioMode, StudioOptions, StudioShadow, } from "./operations.ts";
|
|
150
151
|
/**
|
|
151
152
|
* Verify a URL's signature locally. Returns true or false, or
|
|
152
153
|
* null when the URL is unsigned or malformed.
|