@capacms/sdk 1.0.0-next.1 → 1.0.0-next.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/CHANGELOG.md +487 -0
- package/README.md +1810 -183
- package/bin/capa-codegen.js +192 -5
- package/bin/capa.js +235 -0
- package/bin/graphql-project.js +142 -0
- package/bin/project-env.js +58 -0
- package/dist/client.d.ts +89 -4
- package/dist/client.js +40 -5
- package/dist/codegen.d.ts +55 -0
- package/dist/codegen.js +320 -39
- package/dist/config.d.ts +5 -36
- package/dist/config.js +47 -1
- package/dist/esm/image/index.d.ts +187 -0
- package/dist/esm/image/index.js +353 -0
- package/dist/esm/image/shared-params.generated.d.ts +314 -0
- package/dist/esm/image/shared-params.generated.js +680 -0
- package/dist/esm/nextjs/image-loader.d.ts +60 -0
- package/dist/esm/nextjs/image-loader.js +67 -0
- package/dist/esm/nextjs/overlay.d.ts +30 -0
- package/dist/esm/nextjs/overlay.js +75 -0
- package/dist/esm/overlay/index.d.ts +32 -0
- package/dist/esm/overlay/index.js +576 -0
- package/dist/esm/overlay/protocol.d.ts +187 -0
- package/dist/esm/overlay/protocol.js +240 -0
- package/dist/esm/package.json +4 -0
- package/dist/graphql-codegen.d.ts +117 -0
- package/dist/graphql-codegen.js +705 -0
- package/dist/http.js +1 -1
- package/dist/image/index.d.ts +187 -0
- package/dist/image/index.js +362 -0
- package/dist/image/shared-params.generated.d.ts +314 -0
- package/dist/image/shared-params.generated.js +700 -0
- package/dist/index.d.ts +3 -3
- package/dist/index.js +2 -1
- package/dist/next/attrs.d.ts +84 -10
- package/dist/next/attrs.js +119 -2
- package/dist/next/client.d.ts +176 -32
- package/dist/next/client.js +212 -90
- package/dist/next/entry-fields.d.ts +203 -0
- package/dist/next/entry-fields.js +2 -0
- package/dist/next/errors.d.ts +136 -0
- package/dist/next/errors.js +214 -0
- package/dist/next/field-names.d.ts +37 -0
- package/dist/next/field-names.js +145 -0
- package/dist/next/graphql/build.d.ts +27 -0
- package/dist/next/graphql/build.js +98 -0
- package/dist/next/graphql/documents.d.ts +67 -0
- package/dist/next/graphql/documents.js +35 -0
- package/dist/next/graphql/edit-mode.d.ts +16 -0
- package/dist/next/graphql/edit-mode.js +93 -0
- package/dist/next/graphql/filter-values.d.ts +34 -0
- package/dist/next/graphql/filter-values.js +96 -0
- package/dist/next/graphql/introspection.d.ts +89 -0
- package/dist/next/graphql/introspection.js +102 -0
- package/dist/next/graphql/plan.d.ts +115 -0
- package/dist/next/graphql/plan.js +531 -0
- package/dist/next/graphql/request.d.ts +228 -0
- package/dist/next/graphql/request.js +283 -0
- package/dist/next/graphql/rest.d.ts +66 -0
- package/dist/next/graphql/rest.js +502 -0
- package/dist/next/graphql/selection.d.ts +55 -0
- package/dist/next/graphql/selection.js +212 -0
- package/dist/next/graphql/sha256.d.ts +13 -0
- package/dist/next/graphql/sha256.js +86 -0
- package/dist/next/graphql/summary.d.ts +83 -0
- package/dist/next/graphql/summary.js +151 -0
- package/dist/next/graphql/tree-layout.d.ts +36 -0
- package/dist/next/graphql/tree-layout.js +20 -0
- package/dist/next/graphql/tree.d.ts +171 -0
- package/dist/next/graphql/tree.js +258 -0
- package/dist/next/graphql/typed.d.ts +261 -0
- package/dist/next/graphql/typed.js +146 -0
- package/dist/next/index.d.ts +30 -5
- package/dist/next/index.js +32 -1
- package/dist/next/inflate.d.ts +51 -0
- package/dist/next/inflate.js +243 -0
- package/dist/next/key-family.d.ts +31 -0
- package/dist/next/key-family.js +66 -0
- package/dist/next/select-types.d.ts +58 -5
- package/dist/next/system-keys.d.ts +27 -0
- package/dist/next/system-keys.js +42 -0
- package/dist/nextjs/image-loader.d.ts +60 -0
- package/dist/nextjs/image-loader.js +71 -0
- package/dist/nextjs/index.d.ts +484 -5
- package/dist/nextjs/index.js +688 -6
- package/dist/nextjs/overlay.d.ts +30 -0
- package/dist/nextjs/overlay.js +78 -0
- package/dist/overlay/index.d.ts +14 -2
- package/dist/overlay/index.js +282 -43
- package/dist/overlay/protocol.d.ts +98 -2
- package/dist/overlay/protocol.js +151 -4
- package/dist/webhooks.d.ts +8 -0
- package/package.json +63 -15
|
@@ -0,0 +1,680 @@
|
|
|
1
|
+
// GENERATED by scripts/shared-image.js from packages/shared/src/image/params.ts, the rules the API serves
|
|
2
|
+
// /files/* by. Do not edit: change the source, and the API and this package follow.
|
|
3
|
+
/**
|
|
4
|
+
* Query parsing, validation and canonicalisation for `GET /files/*`
|
|
5
|
+
* (ADMIN_UI_OVERHAUL §0i.1, "The image processor, made tight").
|
|
6
|
+
*
|
|
7
|
+
* PURE ON PURPOSE. Nothing here touches sharp, the network or the database, so
|
|
8
|
+
* the whole parameter surface — every accepted value, every rejection, and the
|
|
9
|
+
* canonical spelling of every URL — is testable without a server and without a
|
|
10
|
+
* live Backblaze object. That matters for this route in particular: its eight
|
|
11
|
+
* parity fixtures are all 400/404 cases because a DB hit immediately leaves the
|
|
12
|
+
* process (docs/TURBINE_PORT_NOTES.md, "Deferred to the P6 cloud parity pass"),
|
|
13
|
+
* so the transform surface has no golden coverage at all unless it is reachable
|
|
14
|
+
* from a unit test.
|
|
15
|
+
*
|
|
16
|
+
* THE RULE THAT SHAPES EVERYTHING BELOW: a caller that sends only today's three
|
|
17
|
+
* parameters (`width`, `height`, `blur`) must get today's response. Every
|
|
18
|
+
* default here is therefore today's behaviour, and the one place that could
|
|
19
|
+
* have broken it — the canonical-query redirect — is deliberately gated on a
|
|
20
|
+
* NEW parameter being present. See `canonicalise`.
|
|
21
|
+
*
|
|
22
|
+
* WHY IT LIVES IN @capa/shared. `@capacms/sdk` builds image URLs, and a URL
|
|
23
|
+
* that is not spelled the way `canonicalise` spells it costs every first view
|
|
24
|
+
* a 301 that the edge then keeps for a year. So the SDK compiles this file into
|
|
25
|
+
* its own build rather than keeping a second copy of the rules, and its tests
|
|
26
|
+
* run what it builds back through `canonicalise`. The API imports it from here.
|
|
27
|
+
*/
|
|
28
|
+
/** Content type emitted for each explicitly requestable output format. */
|
|
29
|
+
export const OUTPUT_MIME = {
|
|
30
|
+
webp: "image/webp",
|
|
31
|
+
avif: "image/avif",
|
|
32
|
+
jpeg: "image/jpeg",
|
|
33
|
+
png: "image/png",
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* The longest edge this route will ever produce.
|
|
37
|
+
*
|
|
38
|
+
* A cap rather than a clamp, because a clamp makes two different URLs return
|
|
39
|
+
* the same bytes and therefore occupy two edge objects for one representation.
|
|
40
|
+
* 4096 is the §0i.1 figure; it is also comfortably above any real layout width
|
|
41
|
+
* at dpr 3 (1365 CSS px).
|
|
42
|
+
*/
|
|
43
|
+
export const MAX_OUTPUT_EDGE = 4096;
|
|
44
|
+
/** Today's blur bounds, previously applied as a silent clamp. */
|
|
45
|
+
export const MIN_BLUR = 1;
|
|
46
|
+
export const MAX_BLUR = 256;
|
|
47
|
+
/** Default quality for lossy outputs except AVIF. Ignored for png, which is lossless. */
|
|
48
|
+
export const DEFAULT_QUALITY = 80;
|
|
49
|
+
/**
|
|
50
|
+
* AVIF's own default quality (Penelope Hospitality onboarding, #300). At 80,
|
|
51
|
+
* AVIF came back larger than WebP at 80; 60 at effort 2 is 16 to 18% smaller
|
|
52
|
+
* than WebP with equal or better SSIM on eight client photos. The route's
|
|
53
|
+
* encoder and the effort live in apps/api (image-transform.ts); the number
|
|
54
|
+
* lives here because canonicalQuery has to know it (see droppableQuality).
|
|
55
|
+
*/
|
|
56
|
+
export const AVIF_DEFAULT_QUALITY = 60;
|
|
57
|
+
/**
|
|
58
|
+
* The quality an encode into `output` uses when the URL names none.
|
|
59
|
+
*
|
|
60
|
+
* The route only ever calls this with a format the URL asked for, explicitly or
|
|
61
|
+
* through `auto`: with no `format` and no `quality` it selects no encoder at
|
|
62
|
+
* all and the source keeps its own format (`resolveOutputFormat`).
|
|
63
|
+
*/
|
|
64
|
+
export function defaultQuality(output) {
|
|
65
|
+
return output === "avif" ? AVIF_DEFAULT_QUALITY : DEFAULT_QUALITY;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* The `quality` canonicalQuery drops from a URL with this `format`, or null
|
|
69
|
+
* when it keeps every value.
|
|
70
|
+
*
|
|
71
|
+
* CANONICALISATION MUST NEVER CHANGE WHAT A URL RENDERS. A value may be dropped
|
|
72
|
+
* only when the URL without it encodes at that same value. After #300 gave
|
|
73
|
+
* AVIF its own default, dropping 80 everywhere sent `format=avif&quality=80`
|
|
74
|
+
* to `format=avif`, which is quality 60.
|
|
75
|
+
*
|
|
76
|
+
* - `avif`: null. 80 is not AVIF's default, so it must stay. 60 is, and could
|
|
77
|
+
* be dropped without changing the bytes; it is KEPT ON PURPOSE. The SDK's
|
|
78
|
+
* frozen URLs (#316, and @capacms/sdk's own tests) include
|
|
79
|
+
* `format=avif&quality=60`, which would start taking a 301; and an explicit
|
|
80
|
+
* number keeps meaning that number if AVIF's default is retuned, which
|
|
81
|
+
* `format=avif` does not. The cost is two edge objects for one image when a
|
|
82
|
+
* caller spells the default out.
|
|
83
|
+
* - `auto`: null. Negotiation picks AVIF (default 60) or WebP or the source's
|
|
84
|
+
* own format (default 80), so no single value means "no quality" for every
|
|
85
|
+
* browser.
|
|
86
|
+
* - `webp`, `jpeg`, `png` and no format: DEFAULT_QUALITY, as before.
|
|
87
|
+
*
|
|
88
|
+
* KNOWN GAP, older than #300 and left as it is because a URL with no `format`
|
|
89
|
+
* keeps its spelling: with no format the output follows the SOURCE, which a URL
|
|
90
|
+
* cannot see. `?quality=80&width=200` on an AVIF or HEIF source is an AVIF at
|
|
91
|
+
* 80, and `?width=200` keeps sharp's own AVIF default (50). `?quality=80` with
|
|
92
|
+
* nothing else redirects to the bare URL, which is the stored file, not a
|
|
93
|
+
* re-encode.
|
|
94
|
+
*/
|
|
95
|
+
export function droppableQuality(format) {
|
|
96
|
+
if (format === "avif" || format === "auto")
|
|
97
|
+
return null;
|
|
98
|
+
return DEFAULT_QUALITY;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* The parameters that existed before §0i.1.
|
|
102
|
+
*
|
|
103
|
+
* A URL built only from these is GRANDFATHERED: it is treated as already
|
|
104
|
+
* canonical and is never redirected, however it is spelled. Without this, every
|
|
105
|
+
* live `?width=100&height=50` in every customer's markup would start taking a
|
|
106
|
+
* 301 — which is not a behaviour anyone asked to change, and "keep today's
|
|
107
|
+
* calls byte-identical" outranks de-duplicating an edge object that has been
|
|
108
|
+
* duplicated for years already.
|
|
109
|
+
*/
|
|
110
|
+
export const LEGACY_PARAMS = new Set(["width", "height", "blur"]);
|
|
111
|
+
/** Parameters introduced by §0i.1. Their presence is what enables canonicalisation. */
|
|
112
|
+
export const NEW_PARAMS = new Set(["format", "quality", "fit", "dpr", "upscale", "keepMetadata"]);
|
|
113
|
+
/**
|
|
114
|
+
* Fastify gives `?a=1&a=2` as an array. A repeated parameter has no defensible
|
|
115
|
+
* meaning here and silently picking one is exactly the "never silently ignored"
|
|
116
|
+
* failure §0i.1 calls out, so it is a rejection.
|
|
117
|
+
*/
|
|
118
|
+
function single(raw, param) {
|
|
119
|
+
if (raw === undefined || raw === null)
|
|
120
|
+
return { ok: true };
|
|
121
|
+
if (Array.isArray(raw)) {
|
|
122
|
+
return { ok: false, rejection: { error: `\`${param}\` was given more than once`, param } };
|
|
123
|
+
}
|
|
124
|
+
if (typeof raw !== "string") {
|
|
125
|
+
return { ok: false, rejection: { error: `\`${param}\` must be a single value`, param } };
|
|
126
|
+
}
|
|
127
|
+
return { ok: true, value: raw };
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Strict integer parse.
|
|
131
|
+
*
|
|
132
|
+
* `parseInt` is what this route used to do and it is the reason `?width=abc`
|
|
133
|
+
* was a 500: `parseInt("abc")` is NaN, NaN reaches sharp, sharp throws, and the
|
|
134
|
+
* outer catch turns a bad request into "Failed to process file". `parseInt`
|
|
135
|
+
* also accepts `12px` and `0x10`. Neither is a width.
|
|
136
|
+
*/
|
|
137
|
+
function strictInt(value) {
|
|
138
|
+
if (!/^-?\d+$/.test(value.trim()))
|
|
139
|
+
return null;
|
|
140
|
+
const n = Number(value.trim());
|
|
141
|
+
return Number.isSafeInteger(n) ? n : null;
|
|
142
|
+
}
|
|
143
|
+
function strictNumber(value) {
|
|
144
|
+
if (!/^-?\d+(\.\d+)?$/.test(value.trim()))
|
|
145
|
+
return null;
|
|
146
|
+
const n = Number(value.trim());
|
|
147
|
+
return Number.isFinite(n) ? n : null;
|
|
148
|
+
}
|
|
149
|
+
const BOOL_TRUE = new Set(["1", "true"]);
|
|
150
|
+
const BOOL_FALSE = new Set(["0", "false"]);
|
|
151
|
+
/**
|
|
152
|
+
* Parse and validate the whole query in one pass.
|
|
153
|
+
*
|
|
154
|
+
* Unknown VALUES for a known parameter are rejected. Unknown KEYS are not:
|
|
155
|
+
* `?v=3` cache-busters and analytics tags are real traffic on a public CDN
|
|
156
|
+
* origin, and turning them into 400s would break callers who are not using the
|
|
157
|
+
* image surface at all. They are carried through canonicalisation untouched.
|
|
158
|
+
*/
|
|
159
|
+
export function parseImageParams(query) {
|
|
160
|
+
const reject = (error, param) => ({ ok: false, rejection: { error, param } });
|
|
161
|
+
// --- dpr first: it multiplies the dimensions, so the cap has to see it.
|
|
162
|
+
const dprRaw = single(query.dpr, "dpr");
|
|
163
|
+
if (!dprRaw.ok)
|
|
164
|
+
return { ok: false, rejection: dprRaw.rejection };
|
|
165
|
+
let dpr = 1;
|
|
166
|
+
if (dprRaw.value !== undefined) {
|
|
167
|
+
const n = strictNumber(dprRaw.value);
|
|
168
|
+
if (n === null || n < 1 || n > 3) {
|
|
169
|
+
return reject("`dpr` must be a number between 1 and 3", "dpr");
|
|
170
|
+
}
|
|
171
|
+
dpr = n;
|
|
172
|
+
}
|
|
173
|
+
const dimension = (name) => {
|
|
174
|
+
const raw = single(query[name], name);
|
|
175
|
+
if (!raw.ok)
|
|
176
|
+
return raw;
|
|
177
|
+
if (raw.value === undefined)
|
|
178
|
+
return { ok: true };
|
|
179
|
+
const n = strictInt(raw.value);
|
|
180
|
+
if (n === null || n < 1) {
|
|
181
|
+
return { ok: false, rejection: { error: `\`${name}\` must be a positive integer`, param: name } };
|
|
182
|
+
}
|
|
183
|
+
const effective = Math.round(n * dpr);
|
|
184
|
+
if (effective > MAX_OUTPUT_EDGE) {
|
|
185
|
+
return {
|
|
186
|
+
ok: false,
|
|
187
|
+
rejection: {
|
|
188
|
+
error: `\`${name}\` × \`dpr\` is ${effective}px; the maximum output edge is ${MAX_OUTPUT_EDGE}px`,
|
|
189
|
+
param: name,
|
|
190
|
+
},
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
return { ok: true, value: effective };
|
|
194
|
+
};
|
|
195
|
+
const w = dimension("width");
|
|
196
|
+
if (!w.ok)
|
|
197
|
+
return { ok: false, rejection: w.rejection };
|
|
198
|
+
const h = dimension("height");
|
|
199
|
+
if (!h.ok)
|
|
200
|
+
return { ok: false, rejection: h.rejection };
|
|
201
|
+
const blurRaw = single(query.blur, "blur");
|
|
202
|
+
if (!blurRaw.ok)
|
|
203
|
+
return { ok: false, rejection: blurRaw.rejection };
|
|
204
|
+
let blur;
|
|
205
|
+
if (blurRaw.value !== undefined) {
|
|
206
|
+
const n = strictInt(blurRaw.value);
|
|
207
|
+
// This used to be `Math.max(1, Math.min(256, parseInt(blur)))` — a silent
|
|
208
|
+
// clamp, so `blur=99999` and `blur=256` were one representation under two
|
|
209
|
+
// URLs, and `blur=abc` was a 500.
|
|
210
|
+
if (n === null || n < MIN_BLUR || n > MAX_BLUR) {
|
|
211
|
+
return reject(`\`blur\` must be an integer between ${MIN_BLUR} and ${MAX_BLUR}`, "blur");
|
|
212
|
+
}
|
|
213
|
+
blur = n;
|
|
214
|
+
}
|
|
215
|
+
const formatRaw = single(query.format, "format");
|
|
216
|
+
if (!formatRaw.ok)
|
|
217
|
+
return { ok: false, rejection: formatRaw.rejection };
|
|
218
|
+
let format;
|
|
219
|
+
if (formatRaw.value !== undefined) {
|
|
220
|
+
const v = formatRaw.value.trim().toLowerCase();
|
|
221
|
+
const canon = v === "jpg" ? "jpeg" : v;
|
|
222
|
+
if (canon !== "webp" && canon !== "avif" && canon !== "jpeg" && canon !== "png" && canon !== "auto") {
|
|
223
|
+
return reject("`format` must be one of webp, avif, jpeg, png, auto", "format");
|
|
224
|
+
}
|
|
225
|
+
format = canon;
|
|
226
|
+
}
|
|
227
|
+
const qualityRaw = single(query.quality, "quality");
|
|
228
|
+
if (!qualityRaw.ok)
|
|
229
|
+
return { ok: false, rejection: qualityRaw.rejection };
|
|
230
|
+
let quality;
|
|
231
|
+
if (qualityRaw.value !== undefined) {
|
|
232
|
+
const n = strictInt(qualityRaw.value);
|
|
233
|
+
if (n === null || n < 1 || n > 100) {
|
|
234
|
+
return reject("`quality` must be an integer between 1 and 100", "quality");
|
|
235
|
+
}
|
|
236
|
+
quality = n;
|
|
237
|
+
}
|
|
238
|
+
const fitRaw = single(query.fit, "fit");
|
|
239
|
+
if (!fitRaw.ok)
|
|
240
|
+
return { ok: false, rejection: fitRaw.rejection };
|
|
241
|
+
let fit = "inside";
|
|
242
|
+
if (fitRaw.value !== undefined) {
|
|
243
|
+
const v = fitRaw.value.trim().toLowerCase();
|
|
244
|
+
if (v !== "inside" && v !== "cover" && v !== "contain") {
|
|
245
|
+
return reject("`fit` must be one of inside, cover, contain", "fit");
|
|
246
|
+
}
|
|
247
|
+
fit = v;
|
|
248
|
+
}
|
|
249
|
+
const flag = (name) => {
|
|
250
|
+
const raw = single(query[name], name);
|
|
251
|
+
if (!raw.ok)
|
|
252
|
+
return raw;
|
|
253
|
+
if (raw.value === undefined)
|
|
254
|
+
return { ok: true, value: false };
|
|
255
|
+
const v = raw.value.trim().toLowerCase();
|
|
256
|
+
if (BOOL_TRUE.has(v))
|
|
257
|
+
return { ok: true, value: true };
|
|
258
|
+
if (BOOL_FALSE.has(v))
|
|
259
|
+
return { ok: true, value: false };
|
|
260
|
+
return { ok: false, rejection: { error: `\`${name}\` must be 0 or 1`, param: name } };
|
|
261
|
+
};
|
|
262
|
+
const upscale = flag("upscale");
|
|
263
|
+
if (!upscale.ok)
|
|
264
|
+
return { ok: false, rejection: upscale.rejection };
|
|
265
|
+
const keepMetadata = flag("keepMetadata");
|
|
266
|
+
if (!keepMetadata.ok)
|
|
267
|
+
return { ok: false, rejection: keepMetadata.rejection };
|
|
268
|
+
/**
|
|
269
|
+
* What counts as "the caller asked for a transform".
|
|
270
|
+
*
|
|
271
|
+
* `fit`, `dpr` and `upscale` only modify a resize, so on their own they
|
|
272
|
+
* change nothing — a URL carrying just `?fit=cover` must stay the pass-through
|
|
273
|
+
* it is today. `keepMetadata` likewise: there is nothing to strip if we are
|
|
274
|
+
* not re-encoding.
|
|
275
|
+
*/
|
|
276
|
+
const wantsTransform = w.value !== undefined ||
|
|
277
|
+
h.value !== undefined ||
|
|
278
|
+
blur !== undefined ||
|
|
279
|
+
format !== undefined ||
|
|
280
|
+
quality !== undefined;
|
|
281
|
+
return {
|
|
282
|
+
ok: true,
|
|
283
|
+
params: {
|
|
284
|
+
width: w.value,
|
|
285
|
+
height: h.value,
|
|
286
|
+
blur,
|
|
287
|
+
format,
|
|
288
|
+
quality,
|
|
289
|
+
fit,
|
|
290
|
+
dpr,
|
|
291
|
+
upscale: upscale.value,
|
|
292
|
+
keepMetadata: keepMetadata.value,
|
|
293
|
+
wantsTransform,
|
|
294
|
+
},
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
/** Render a number the way the canonical URL spells it (no trailing zeros). */
|
|
298
|
+
function num(n) {
|
|
299
|
+
return String(Number(n));
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
* One key or value of the canonical query, percent-encoded so that a browser
|
|
303
|
+
* sends it back byte for byte.
|
|
304
|
+
*
|
|
305
|
+
* `encodeURIComponent` leaves `'` as it is, but a browser's URL parser
|
|
306
|
+
* percent-encodes `'` in the query of an http(s) URL. A redirect whose Location
|
|
307
|
+
* carried a raw `'` was therefore requested as `%27`, which is not the spelling
|
|
308
|
+
* this module wrote, so it was redirected again, forever. Every other
|
|
309
|
+
* character `encodeURIComponent` leaves alone (`!` `(` `)` `*` `~` and the
|
|
310
|
+
* unreserved set) a browser leaves alone too.
|
|
311
|
+
*/
|
|
312
|
+
function encodePart(text) {
|
|
313
|
+
return encodeURIComponent(text).replace(/'/g, "%27");
|
|
314
|
+
}
|
|
315
|
+
/**
|
|
316
|
+
* `fp` and `rect` are comma lists, spelled with bare commas: a browser sends a
|
|
317
|
+
* query's `,` as it is, so `fp=0.25,0.5` comes back byte for byte, and reads
|
|
318
|
+
* better than `fp=0.25%2C0.5`. Only where they are parameters; an unknown key
|
|
319
|
+
* keeps `encodePart`'s spelling, as before.
|
|
320
|
+
*/
|
|
321
|
+
function listValue(key, options) {
|
|
322
|
+
return options.focal === true && FOCAL_PARAMS.has(key);
|
|
323
|
+
}
|
|
324
|
+
function encodeList(value) {
|
|
325
|
+
return value.split(",").map(encodePart).join(",");
|
|
326
|
+
}
|
|
327
|
+
/**
|
|
328
|
+
* The canonical spelling of a query, so `?width=200&format=webp` and
|
|
329
|
+
* `?format=webp&width=200` are one edge object rather than two.
|
|
330
|
+
*
|
|
331
|
+
* GATED, DELIBERATELY. Canonicalisation only applies once the caller uses a
|
|
332
|
+
* parameter introduced by §0i.1. A URL made only of `width`/`height`/`blur` is
|
|
333
|
+
* declared canonical as received, whatever its key order — see LEGACY_PARAMS
|
|
334
|
+
* for why. The gate is also what keeps the `files-404-params-ignored` parity
|
|
335
|
+
* fixture (`?width=100&height=50&blur=9`) from becoming a 301.
|
|
336
|
+
*
|
|
337
|
+
* @param rawQuery the query string as received, without the leading `?`
|
|
338
|
+
*/
|
|
339
|
+
export function canonicalise(rawQuery, params, options = {}) {
|
|
340
|
+
const received = new URLSearchParams(rawQuery);
|
|
341
|
+
const usesNewParam = [...received.keys()].some((k) => NEW_PARAMS.has(k) || (options.focal === true && FOCAL_PARAMS.has(k)));
|
|
342
|
+
if (!usesNewParam)
|
|
343
|
+
return { query: rawQuery, isCanonical: true };
|
|
344
|
+
const query = canonicalQuery(rawQuery, params, options);
|
|
345
|
+
return { query, isCanonical: query === rawQuery };
|
|
346
|
+
}
|
|
347
|
+
/**
|
|
348
|
+
* The canonical spelling itself, without `canonicalise`'s gate.
|
|
349
|
+
*
|
|
350
|
+
* `canonicalise` is this plus the LEGACY_PARAMS grandfather clause. For a query
|
|
351
|
+
* that uses a new parameter the two agree; for one made only of
|
|
352
|
+
* `width`/`height`/`blur` this still sorts and re-spells it, which the API
|
|
353
|
+
* accepts as canonical too, since the gate declares any legacy spelling
|
|
354
|
+
* canonical. So a URL built with this is never redirected, whichever
|
|
355
|
+
* parameters it carries. @capacms/sdk builds every image URL with it.
|
|
356
|
+
*
|
|
357
|
+
* @param rawQuery the query string, without the leading `?`; read only for the
|
|
358
|
+
* keys this module does not know, which are kept
|
|
359
|
+
*/
|
|
360
|
+
export function canonicalQuery(rawQuery, params, options = {}) {
|
|
361
|
+
const received = new URLSearchParams(rawQuery);
|
|
362
|
+
const keys = [...new Set([...received.keys()])];
|
|
363
|
+
const out = new Map();
|
|
364
|
+
// Known parameters are re-rendered from the PARSED value, so `width=0200`,
|
|
365
|
+
// `format=JPG` and `upscale=true` all collapse onto one spelling. Defaults are
|
|
366
|
+
// dropped: a URL that asks for the default is the same representation as one
|
|
367
|
+
// that does not mention it. `quality` only where that is true for every
|
|
368
|
+
// output the URL can produce (droppableQuality).
|
|
369
|
+
//
|
|
370
|
+
// `width`/`height` are re-rendered PRE-dpr, because dpr stays in the URL as
|
|
371
|
+
// its own term; folding it into the dimension would make `?width=200&dpr=2`
|
|
372
|
+
// redirect to `?width=400`, which is a different request as far as the caller
|
|
373
|
+
// who wrote it is concerned.
|
|
374
|
+
if (params.width !== undefined)
|
|
375
|
+
out.set("width", num(Math.round(params.width / params.dpr)));
|
|
376
|
+
if (params.height !== undefined)
|
|
377
|
+
out.set("height", num(Math.round(params.height / params.dpr)));
|
|
378
|
+
if (params.blur !== undefined)
|
|
379
|
+
out.set("blur", num(params.blur));
|
|
380
|
+
if (params.format !== undefined)
|
|
381
|
+
out.set("format", params.format);
|
|
382
|
+
if (params.quality !== undefined && params.quality !== droppableQuality(params.format))
|
|
383
|
+
out.set("quality", num(params.quality));
|
|
384
|
+
if (params.fit !== "inside")
|
|
385
|
+
out.set("fit", params.fit);
|
|
386
|
+
if (params.dpr !== 1)
|
|
387
|
+
out.set("dpr", num(params.dpr));
|
|
388
|
+
if (params.upscale)
|
|
389
|
+
out.set("upscale", "1");
|
|
390
|
+
if (params.keepMetadata)
|
|
391
|
+
out.set("keepMetadata", "1");
|
|
392
|
+
if (options.focal === true) {
|
|
393
|
+
if (params.focalPoint !== undefined)
|
|
394
|
+
out.set("fp", fractions([params.focalPoint.x, params.focalPoint.y]));
|
|
395
|
+
if (params.rect !== undefined)
|
|
396
|
+
out.set("rect", fractions([params.rect.x, params.rect.y, params.rect.width, params.rect.height]));
|
|
397
|
+
}
|
|
398
|
+
// Unknown keys are preserved verbatim (first value wins, repeats collapse),
|
|
399
|
+
// so a cache-buster survives the redirect instead of being silently dropped.
|
|
400
|
+
//
|
|
401
|
+
// An empty pair (`&=`, an empty key with an empty value) is dropped: written
|
|
402
|
+
// back it would be an empty segment, which parses to nothing, so the URL it
|
|
403
|
+
// redirected to was itself redirected again.
|
|
404
|
+
for (const key of keys) {
|
|
405
|
+
if (LEGACY_PARAMS.has(key) || NEW_PARAMS.has(key))
|
|
406
|
+
continue;
|
|
407
|
+
if (options.focal === true && FOCAL_PARAMS.has(key))
|
|
408
|
+
continue;
|
|
409
|
+
const value = received.get(key) ?? "";
|
|
410
|
+
if (key === "" && value === "")
|
|
411
|
+
continue;
|
|
412
|
+
out.set(key, value);
|
|
413
|
+
}
|
|
414
|
+
const sorted = [...out.entries()].sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0));
|
|
415
|
+
return sorted
|
|
416
|
+
.map(([k, v]) => (v === "" ? encodePart(k) : `${encodePart(k)}=${listValue(k, options) ? encodeList(v) : encodePart(v)}`))
|
|
417
|
+
.join("&");
|
|
418
|
+
}
|
|
419
|
+
/**
|
|
420
|
+
* A stable string identifying the transform, for the ETag.
|
|
421
|
+
*
|
|
422
|
+
* Built from the RESOLVED parameters rather than the URL, so two spellings of
|
|
423
|
+
* one representation share an ETag even when the redirect is not in play.
|
|
424
|
+
* `auto` resolves per request, so the caller passes the resolved format in.
|
|
425
|
+
*/
|
|
426
|
+
export function buildSignature(params, resolvedFormat) {
|
|
427
|
+
if (!params.wantsTransform)
|
|
428
|
+
return "raw";
|
|
429
|
+
const parts = [];
|
|
430
|
+
if (params.width !== undefined)
|
|
431
|
+
parts.push(`w${params.width}`);
|
|
432
|
+
if (params.height !== undefined)
|
|
433
|
+
parts.push(`h${params.height}`);
|
|
434
|
+
if (params.width !== undefined || params.height !== undefined) {
|
|
435
|
+
parts.push(`f${params.fit}`);
|
|
436
|
+
if (params.upscale)
|
|
437
|
+
parts.push("up");
|
|
438
|
+
}
|
|
439
|
+
if (params.blur !== undefined)
|
|
440
|
+
parts.push(`b${params.blur}`);
|
|
441
|
+
if (resolvedFormat)
|
|
442
|
+
parts.push(`o${resolvedFormat}`);
|
|
443
|
+
if (params.quality !== undefined)
|
|
444
|
+
parts.push(`q${params.quality}`);
|
|
445
|
+
if (params.keepMetadata)
|
|
446
|
+
parts.push("meta");
|
|
447
|
+
// The crop and the focal point this transform applies (`appliedFocal`), from
|
|
448
|
+
// the URL or from the file, so a new focal point on the file is a new ETag
|
|
449
|
+
// under the same URL. Colons, not the URL's commas: If-None-Match is a
|
|
450
|
+
// comma-separated list, and an ETag with a comma in it never matches its
|
|
451
|
+
// own revalidation (files.ts ifNoneMatchSatisfied), so every one would be a
|
|
452
|
+
// full 200.
|
|
453
|
+
if (params.rect !== undefined)
|
|
454
|
+
parts.push(`r${fractions([params.rect.x, params.rect.y, params.rect.width, params.rect.height], ":")}`);
|
|
455
|
+
if (params.focalPoint !== undefined)
|
|
456
|
+
parts.push(`p${fractions([params.focalPoint.x, params.focalPoint.y], ":")}`);
|
|
457
|
+
return parts.join("_") || "raw";
|
|
458
|
+
}
|
|
459
|
+
/**
|
|
460
|
+
* Which concrete format `format=auto` resolves to for this request.
|
|
461
|
+
*
|
|
462
|
+
* Returns null when the client advertises neither modern format, in which case
|
|
463
|
+
* the source format is kept — `auto` never makes a response worse than the
|
|
464
|
+
* object already is.
|
|
465
|
+
*
|
|
466
|
+
* `q=0` is an explicit REFUSAL in RFC 9110 content negotiation, not a weak
|
|
467
|
+
* preference, so an entry carrying it is skipped rather than matched.
|
|
468
|
+
*/
|
|
469
|
+
export function resolveAutoFormat(accept) {
|
|
470
|
+
if (!accept)
|
|
471
|
+
return null;
|
|
472
|
+
const offered = new Map();
|
|
473
|
+
for (const entry of accept.split(",")) {
|
|
474
|
+
const [type, ...paramParts] = entry.split(";");
|
|
475
|
+
const name = type.trim().toLowerCase();
|
|
476
|
+
if (!name)
|
|
477
|
+
continue;
|
|
478
|
+
let q = 1;
|
|
479
|
+
for (const p of paramParts) {
|
|
480
|
+
const m = /^\s*q\s*=\s*([\d.]+)\s*$/i.exec(p);
|
|
481
|
+
if (m)
|
|
482
|
+
q = Number(m[1]);
|
|
483
|
+
}
|
|
484
|
+
offered.set(name, Number.isFinite(q) ? q : 1);
|
|
485
|
+
}
|
|
486
|
+
if ((offered.get("image/avif") ?? 0) > 0)
|
|
487
|
+
return "avif";
|
|
488
|
+
if ((offered.get("image/webp") ?? 0) > 0)
|
|
489
|
+
return "webp";
|
|
490
|
+
return null;
|
|
491
|
+
}
|
|
492
|
+
/** `fp` and `rect`: parameters only where the focal point and crop are on. */
|
|
493
|
+
export const FOCAL_PARAMS = new Set(["fp", "rect"]);
|
|
494
|
+
/** Decimal places a fraction keeps: a ten-thousandth, under a pixel up to 10,000 px. */
|
|
495
|
+
export const FRACTION_DECIMALS = 4;
|
|
496
|
+
/** Slack for `x + width <= 1` after rounding to FRACTION_DECIMALS. */
|
|
497
|
+
const FRACTION_EPSILON = 1e-9;
|
|
498
|
+
/** A number rounded to FRACTION_DECIMALS. */
|
|
499
|
+
export function roundFraction(n) {
|
|
500
|
+
const scale = 10 ** FRACTION_DECIMALS;
|
|
501
|
+
return Math.round(n * scale) / scale;
|
|
502
|
+
}
|
|
503
|
+
function fraction(value) {
|
|
504
|
+
if (typeof value !== "number" || !Number.isFinite(value) || value < 0 || value > 1)
|
|
505
|
+
return null;
|
|
506
|
+
return roundFraction(value);
|
|
507
|
+
}
|
|
508
|
+
function plainObject(value) {
|
|
509
|
+
return value !== null && typeof value === "object" && !Array.isArray(value) ? value : null;
|
|
510
|
+
}
|
|
511
|
+
/**
|
|
512
|
+
* A focal point from stored metadata or a request body, or null when it is
|
|
513
|
+
* not one: an object whose `x` and `y` are numbers from 0 to 1.
|
|
514
|
+
*/
|
|
515
|
+
export function readFocalPoint(value) {
|
|
516
|
+
const v = plainObject(value);
|
|
517
|
+
if (!v)
|
|
518
|
+
return null;
|
|
519
|
+
const x = fraction(v.x);
|
|
520
|
+
const y = fraction(v.y);
|
|
521
|
+
return x === null || y === null ? null : { x, y };
|
|
522
|
+
}
|
|
523
|
+
/**
|
|
524
|
+
* A crop from stored metadata or a request body, or null when it is not one:
|
|
525
|
+
* `x`, `y`, `width` and `height` from 0 to 1, a width and a height above 0,
|
|
526
|
+
* and inside the image.
|
|
527
|
+
*/
|
|
528
|
+
export function readCrop(value) {
|
|
529
|
+
const v = plainObject(value);
|
|
530
|
+
if (!v)
|
|
531
|
+
return null;
|
|
532
|
+
const x = fraction(v.x);
|
|
533
|
+
const y = fraction(v.y);
|
|
534
|
+
const width = fraction(v.width);
|
|
535
|
+
const height = fraction(v.height);
|
|
536
|
+
if (x === null || y === null || width === null || height === null)
|
|
537
|
+
return null;
|
|
538
|
+
if (width <= 0 || height <= 0)
|
|
539
|
+
return null;
|
|
540
|
+
if (x + width > 1 + FRACTION_EPSILON || y + height > 1 + FRACTION_EPSILON)
|
|
541
|
+
return null;
|
|
542
|
+
return { x, y, width, height };
|
|
543
|
+
}
|
|
544
|
+
/** True when `value` has exactly these own keys, in any order. */
|
|
545
|
+
function hasExactly(value, keys) {
|
|
546
|
+
const own = Object.keys(value);
|
|
547
|
+
return own.length === keys.length && keys.every((key) => own.includes(key));
|
|
548
|
+
}
|
|
549
|
+
/**
|
|
550
|
+
* A focal point exactly as the api stores one (`normaliseFocalPatch` in
|
|
551
|
+
* apps/api routes/v2/file-uploads.ts): `{ x, y }` and no other key, numbers
|
|
552
|
+
* from 0 to 1. Stricter than `readFocalPoint`, which reads one out of any
|
|
553
|
+
* object that has them: this is the test for "ENG-5959 wrote this", which a
|
|
554
|
+
* replace carries over and the legacy routes hide. Anything else under the
|
|
555
|
+
* name is a project's own metadata and is left alone.
|
|
556
|
+
*/
|
|
557
|
+
export function isStoredFocalPoint(value) {
|
|
558
|
+
const v = plainObject(value);
|
|
559
|
+
return v !== null && hasExactly(v, ["x", "y"]) && readFocalPoint(v) !== null;
|
|
560
|
+
}
|
|
561
|
+
/** A crop exactly as the api stores one: `{ x, y, width, height }` and no other key. See `isStoredFocalPoint`. */
|
|
562
|
+
export function isStoredCrop(value) {
|
|
563
|
+
const v = plainObject(value);
|
|
564
|
+
return v !== null && hasExactly(v, ["x", "y", "width", "height"]) && readCrop(v) !== null;
|
|
565
|
+
}
|
|
566
|
+
/** Numbers as the canonical URL spells them: comma separated, no trailing zeros. */
|
|
567
|
+
function fractions(values, separator = ",") {
|
|
568
|
+
return values.map((n) => String(roundFraction(n))).join(separator);
|
|
569
|
+
}
|
|
570
|
+
/** `0.25,0.5` into numbers, or null when any part is not a plain decimal. */
|
|
571
|
+
function decimalList(text, count) {
|
|
572
|
+
const parts = text.split(",");
|
|
573
|
+
if (parts.length !== count)
|
|
574
|
+
return null;
|
|
575
|
+
const out = [];
|
|
576
|
+
for (const part of parts) {
|
|
577
|
+
const n = strictNumber(part);
|
|
578
|
+
if (n === null)
|
|
579
|
+
return null;
|
|
580
|
+
out.push(n);
|
|
581
|
+
}
|
|
582
|
+
return out;
|
|
583
|
+
}
|
|
584
|
+
/**
|
|
585
|
+
* `fp` and `rect`, for a project with the focal point and crop on. The route
|
|
586
|
+
* calls it after it knows the file's project; anywhere else the two are
|
|
587
|
+
* unknown keys and this is never called.
|
|
588
|
+
*/
|
|
589
|
+
export function parseFocalParams(query) {
|
|
590
|
+
const reject = (error, param) => ({ ok: false, rejection: { error, param } });
|
|
591
|
+
const fpRaw = single(query.fp, "fp");
|
|
592
|
+
if (!fpRaw.ok)
|
|
593
|
+
return { ok: false, rejection: fpRaw.rejection };
|
|
594
|
+
let focalPoint;
|
|
595
|
+
if (fpRaw.value !== undefined) {
|
|
596
|
+
const list = decimalList(fpRaw.value, 2);
|
|
597
|
+
const read = list ? readFocalPoint({ x: list[0], y: list[1] }) : null;
|
|
598
|
+
if (!read)
|
|
599
|
+
return reject("`fp` must be two numbers from 0 to 1, x,y", "fp");
|
|
600
|
+
focalPoint = read;
|
|
601
|
+
}
|
|
602
|
+
const rectRaw = single(query.rect, "rect");
|
|
603
|
+
if (!rectRaw.ok)
|
|
604
|
+
return { ok: false, rejection: rectRaw.rejection };
|
|
605
|
+
let rect;
|
|
606
|
+
if (rectRaw.value !== undefined) {
|
|
607
|
+
const list = decimalList(rectRaw.value, 4);
|
|
608
|
+
const read = list ? readCrop({ x: list[0], y: list[1], width: list[2], height: list[3] }) : null;
|
|
609
|
+
if (!read) {
|
|
610
|
+
return reject("`rect` must be four numbers from 0 to 1, x,y,width,height, with a width and a height above 0, inside the image", "rect");
|
|
611
|
+
}
|
|
612
|
+
rect = read;
|
|
613
|
+
}
|
|
614
|
+
return { ok: true, focalPoint, rect };
|
|
615
|
+
}
|
|
616
|
+
/**
|
|
617
|
+
* The crop and focal point a request applies: the URL's, else the file's,
|
|
618
|
+
* and the file's only for a `fit=cover` crop to both a width and a height.
|
|
619
|
+
* A blur placeholder keeps its own square geometry and applies only a `rect`
|
|
620
|
+
* the URL names.
|
|
621
|
+
*/
|
|
622
|
+
/**
|
|
623
|
+
* A crop to a shape: `fit=cover` with a width and a height, and no blur. The
|
|
624
|
+
* one request the file's stored focal point and crop apply to.
|
|
625
|
+
*/
|
|
626
|
+
export function isCoverCrop(params) {
|
|
627
|
+
return params.fit === "cover" && params.width !== undefined && params.height !== undefined && params.blur === undefined;
|
|
628
|
+
}
|
|
629
|
+
/**
|
|
630
|
+
* True when this request's bytes depend on the file's STORED focal point or
|
|
631
|
+
* crop: a cover crop that does not name both `fp` and `rect` itself. Its
|
|
632
|
+
* bytes change under the same URL when an editor saves either, so a browser
|
|
633
|
+
* must not keep them as long as an immutable variant (`GET /files/*`).
|
|
634
|
+
*/
|
|
635
|
+
export function followsStoredFocal(params) {
|
|
636
|
+
return isCoverCrop(params) && (params.focalPoint === undefined || params.rect === undefined);
|
|
637
|
+
}
|
|
638
|
+
export function appliedFocal(params, stored) {
|
|
639
|
+
const cover = isCoverCrop(params);
|
|
640
|
+
const rect = params.rect ?? (cover ? stored?.crop ?? null : null);
|
|
641
|
+
const focalPoint = cover ? params.focalPoint ?? stored?.focalPoint ?? null : null;
|
|
642
|
+
return { rect, focalPoint };
|
|
643
|
+
}
|
|
644
|
+
/**
|
|
645
|
+
* The part of the source to cut out before the resize, or null for none.
|
|
646
|
+
*
|
|
647
|
+
* `source` is the image as a person sees it. `target` is the `fit=cover` box
|
|
648
|
+
* (width and height after dpr), or null for any other request. The crop is cut
|
|
649
|
+
* first; with a target and a focal point, the window of the target's shape
|
|
650
|
+
* that a cover resize would keep is then moved, inside the crop, so its centre
|
|
651
|
+
* is as close to the focal point as the crop allows. Without a focal point the
|
|
652
|
+
* crop alone is returned and the resize centres in it as it always has.
|
|
653
|
+
*
|
|
654
|
+
* The window is the largest of the target's shape that fits the crop, so the
|
|
655
|
+
* resize that follows scales it to the target without cutting anything more.
|
|
656
|
+
*/
|
|
657
|
+
export function focalRegion(source, target, rect, focalPoint) {
|
|
658
|
+
const W = Math.max(1, Math.round(source.width));
|
|
659
|
+
const H = Math.max(1, Math.round(source.height));
|
|
660
|
+
let region = { left: 0, top: 0, width: W, height: H };
|
|
661
|
+
if (rect) {
|
|
662
|
+
const left = Math.min(W - 1, Math.round(rect.x * W));
|
|
663
|
+
const top = Math.min(H - 1, Math.round(rect.y * H));
|
|
664
|
+
region = {
|
|
665
|
+
left,
|
|
666
|
+
top,
|
|
667
|
+
width: Math.max(1, Math.min(W - left, Math.round(rect.width * W))),
|
|
668
|
+
height: Math.max(1, Math.min(H - top, Math.round(rect.height * H))),
|
|
669
|
+
};
|
|
670
|
+
}
|
|
671
|
+
if (!target || !focalPoint)
|
|
672
|
+
return rect ? region : null;
|
|
673
|
+
const scale = Math.max(target.width / region.width, target.height / region.height);
|
|
674
|
+
const width = Math.max(1, Math.min(region.width, Math.round(target.width / scale)));
|
|
675
|
+
const height = Math.max(1, Math.min(region.height, Math.round(target.height / scale)));
|
|
676
|
+
const clamp = (value, lo, hi) => Math.min(hi, Math.max(lo, value));
|
|
677
|
+
const left = clamp(Math.round(focalPoint.x * W - width / 2), region.left, region.left + region.width - width);
|
|
678
|
+
const top = clamp(Math.round(focalPoint.y * H - height / 2), region.top, region.top + region.height - height);
|
|
679
|
+
return { left, top, width, height };
|
|
680
|
+
}
|