canopycms 0.0.57 → 0.0.58
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/dist/ai/json-to-markdown.js +74 -3
- package/dist/ai/json-to-markdown.js.map +1 -1
- package/dist/api/__test__/mock-client.d.ts +9 -5
- package/dist/api/__test__/mock-client.d.ts.map +1 -1
- package/dist/api/__test__/mock-client.js +15 -7
- package/dist/api/__test__/mock-client.js.map +1 -1
- package/dist/api/assets.d.ts +100 -36
- package/dist/api/assets.d.ts.map +1 -1
- package/dist/api/assets.js +338 -51
- package/dist/api/assets.js.map +1 -1
- package/dist/api/client.d.ts +16 -5
- package/dist/api/client.d.ts.map +1 -1
- package/dist/api/client.js +24 -6
- package/dist/api/client.js.map +1 -1
- package/dist/api/entries.d.ts +4 -4
- package/dist/api/index.d.ts +1 -1
- package/dist/api/index.d.ts.map +1 -1
- package/dist/api/index.js.map +1 -1
- package/dist/api/route-builder.d.ts +9 -0
- package/dist/api/route-builder.d.ts.map +1 -1
- package/dist/api/route-builder.js +1 -0
- package/dist/api/route-builder.js.map +1 -1
- package/dist/api/schema.d.ts +4 -4
- package/dist/api/types.d.ts +11 -1
- package/dist/api/types.d.ts.map +1 -1
- package/dist/assets/asset-prefixes.d.ts +31 -0
- package/dist/assets/asset-prefixes.d.ts.map +1 -0
- package/dist/assets/asset-prefixes.js +20 -0
- package/dist/assets/asset-prefixes.js.map +1 -0
- package/dist/assets/asset-src.d.ts +23 -0
- package/dist/assets/asset-src.d.ts.map +1 -0
- package/dist/assets/asset-src.js +28 -0
- package/dist/assets/asset-src.js.map +1 -0
- package/dist/assets/asset-url.d.ts +35 -0
- package/dist/assets/asset-url.d.ts.map +1 -0
- package/dist/assets/asset-url.js +77 -0
- package/dist/assets/asset-url.js.map +1 -0
- package/dist/assets/factory.d.ts +18 -0
- package/dist/assets/factory.d.ts.map +1 -0
- package/dist/assets/factory.js +42 -0
- package/dist/assets/factory.js.map +1 -0
- package/dist/assets/finalize.d.ts +36 -0
- package/dist/assets/finalize.d.ts.map +1 -0
- package/dist/assets/finalize.js +72 -0
- package/dist/assets/finalize.js.map +1 -0
- package/dist/assets/index.d.ts +20 -0
- package/dist/assets/index.d.ts.map +1 -0
- package/dist/assets/index.js +19 -0
- package/dist/assets/index.js.map +1 -0
- package/dist/assets/keys.d.ts +56 -0
- package/dist/assets/keys.d.ts.map +1 -0
- package/dist/assets/keys.js +88 -0
- package/dist/assets/keys.js.map +1 -0
- package/dist/assets/pipeline.d.ts +50 -0
- package/dist/assets/pipeline.d.ts.map +1 -0
- package/dist/assets/pipeline.js +202 -0
- package/dist/assets/pipeline.js.map +1 -0
- package/dist/assets/store-local.d.ts +84 -0
- package/dist/assets/store-local.d.ts.map +1 -0
- package/dist/assets/store-local.js +254 -0
- package/dist/assets/store-local.js.map +1 -0
- package/dist/assets/store-s3.d.ts +71 -0
- package/dist/assets/store-s3.d.ts.map +1 -0
- package/dist/assets/store-s3.js +183 -0
- package/dist/assets/store-s3.js.map +1 -0
- package/dist/assets/svg-sanitizer.d.ts +37 -0
- package/dist/assets/svg-sanitizer.d.ts.map +1 -0
- package/dist/assets/svg-sanitizer.js +222 -0
- package/dist/assets/svg-sanitizer.js.map +1 -0
- package/dist/assets/transform-directives.d.ts +97 -0
- package/dist/assets/transform-directives.d.ts.map +1 -0
- package/dist/assets/transform-directives.js +267 -0
- package/dist/assets/transform-directives.js.map +1 -0
- package/dist/assets/transform.d.ts +51 -0
- package/dist/assets/transform.d.ts.map +1 -0
- package/dist/assets/transform.js +190 -0
- package/dist/assets/transform.js.map +1 -0
- package/dist/assets/types.d.ts +82 -0
- package/dist/assets/types.d.ts.map +1 -0
- package/dist/assets/types.js +10 -0
- package/dist/assets/types.js.map +1 -0
- package/dist/authorization/groups/schema.d.ts +2 -2
- package/dist/authorization/permissions/schema.d.ts +2 -2
- package/dist/cli/generate-ai-content.js +92 -7
- package/dist/cli/init.js +16 -4
- package/dist/config/helpers.d.ts.map +1 -1
- package/dist/config/helpers.js +2 -1
- package/dist/config/helpers.js.map +1 -1
- package/dist/config/index.d.ts +2 -2
- package/dist/config/index.d.ts.map +1 -1
- package/dist/config/index.js +1 -1
- package/dist/config/index.js.map +1 -1
- package/dist/config/schemas/config.d.ts +10 -0
- package/dist/config/schemas/config.d.ts.map +1 -1
- package/dist/config/schemas/field.d.ts +38 -3
- package/dist/config/schemas/field.d.ts.map +1 -1
- package/dist/config/schemas/field.js +18 -0
- package/dist/config/schemas/field.js.map +1 -1
- package/dist/config/schemas/media.d.ts +8 -0
- package/dist/config/schemas/media.d.ts.map +1 -1
- package/dist/config/schemas/media.js +9 -0
- package/dist/config/schemas/media.js.map +1 -1
- package/dist/config/types.d.ts +43 -3
- package/dist/config/types.d.ts.map +1 -1
- package/dist/config/types.js +4 -1
- package/dist/config/types.js.map +1 -1
- package/dist/editor/CanopyEditor.d.ts.map +1 -1
- package/dist/editor/CanopyEditor.js +1 -1
- package/dist/editor/CanopyEditor.js.map +1 -1
- package/dist/editor/Editor.d.ts +2 -0
- package/dist/editor/Editor.d.ts.map +1 -1
- package/dist/editor/Editor.js +82 -80
- package/dist/editor/Editor.js.map +1 -1
- package/dist/editor/FormRenderer.d.ts.map +1 -1
- package/dist/editor/FormRenderer.js +9 -0
- package/dist/editor/FormRenderer.js.map +1 -1
- package/dist/editor/components/EditorSidebar.d.ts +6 -1
- package/dist/editor/components/EditorSidebar.d.ts.map +1 -1
- package/dist/editor/components/EditorSidebar.js +4 -3
- package/dist/editor/components/EditorSidebar.js.map +1 -1
- package/dist/editor/context/AssetContext.d.ts +25 -0
- package/dist/editor/context/AssetContext.d.ts.map +1 -0
- package/dist/editor/context/AssetContext.js +26 -0
- package/dist/editor/context/AssetContext.js.map +1 -0
- package/dist/editor/context/index.d.ts +1 -0
- package/dist/editor/context/index.d.ts.map +1 -1
- package/dist/editor/context/index.js +1 -0
- package/dist/editor/context/index.js.map +1 -1
- package/dist/editor/fields/ImageField.d.ts +22 -0
- package/dist/editor/fields/ImageField.d.ts.map +1 -0
- package/dist/editor/fields/ImageField.js +100 -0
- package/dist/editor/fields/ImageField.js.map +1 -0
- package/dist/editor/fields/MarkdownField.d.ts.map +1 -1
- package/dist/editor/fields/MarkdownField.js +30 -4
- package/dist/editor/fields/MarkdownField.js.map +1 -1
- package/dist/editor/fields/MdxImageDialog.d.ts +16 -0
- package/dist/editor/fields/MdxImageDialog.d.ts.map +1 -0
- package/dist/editor/fields/MdxImageDialog.js +73 -0
- package/dist/editor/fields/MdxImageDialog.js.map +1 -0
- package/dist/editor/media/AssetCard.d.ts +21 -0
- package/dist/editor/media/AssetCard.d.ts.map +1 -0
- package/dist/editor/media/AssetCard.js +41 -0
- package/dist/editor/media/AssetCard.js.map +1 -0
- package/dist/editor/media/CropStep.d.ts +26 -0
- package/dist/editor/media/CropStep.d.ts.map +1 -0
- package/dist/editor/media/CropStep.js +38 -0
- package/dist/editor/media/CropStep.js.map +1 -0
- package/dist/editor/media/MediaLibrary.d.ts +22 -0
- package/dist/editor/media/MediaLibrary.d.ts.map +1 -0
- package/dist/editor/media/MediaLibrary.js +17 -0
- package/dist/editor/media/MediaLibrary.js.map +1 -0
- package/dist/editor/media/MediaLibraryBody.d.ts +17 -0
- package/dist/editor/media/MediaLibraryBody.d.ts.map +1 -0
- package/dist/editor/media/MediaLibraryBody.js +133 -0
- package/dist/editor/media/MediaLibraryBody.js.map +1 -0
- package/dist/editor/media/crop-math.d.ts +37 -0
- package/dist/editor/media/crop-math.d.ts.map +1 -0
- package/dist/editor/media/crop-math.js +62 -0
- package/dist/editor/media/crop-math.js.map +1 -0
- package/dist/editor/media/upload-asset.d.ts +31 -0
- package/dist/editor/media/upload-asset.d.ts.map +1 -0
- package/dist/editor/media/upload-asset.js +56 -0
- package/dist/editor/media/upload-asset.js.map +1 -0
- package/dist/editor/media/upload-constants.d.ts +27 -0
- package/dist/editor/media/upload-constants.d.ts.map +1 -0
- package/dist/editor/media/upload-constants.js +40 -0
- package/dist/editor/media/upload-constants.js.map +1 -0
- package/dist/editor/media/useAssetUpload.d.ts +19 -0
- package/dist/editor/media/useAssetUpload.d.ts.map +1 -0
- package/dist/editor/media/useAssetUpload.js +53 -0
- package/dist/editor/media/useAssetUpload.js.map +1 -0
- package/dist/editor/media/xhr-upload.d.ts +20 -0
- package/dist/editor/media/xhr-upload.d.ts.map +1 -0
- package/dist/editor/media/xhr-upload.js +42 -0
- package/dist/editor/media/xhr-upload.js.map +1 -0
- package/dist/editor/theme.d.ts +2 -1
- package/dist/editor/theme.d.ts.map +1 -1
- package/dist/editor/theme.js +1 -0
- package/dist/editor/theme.js.map +1 -1
- package/dist/entry-schema.d.ts +19 -0
- package/dist/entry-schema.d.ts.map +1 -1
- package/dist/entry-schema.js.map +1 -1
- package/dist/http/handler.d.ts +5 -2
- package/dist/http/handler.d.ts.map +1 -1
- package/dist/http/handler.js +13 -4
- package/dist/http/handler.js.map +1 -1
- package/dist/http/index.d.ts +2 -2
- package/dist/http/index.d.ts.map +1 -1
- package/dist/http/index.js +1 -1
- package/dist/http/index.js.map +1 -1
- package/dist/http/router.d.ts +14 -1
- package/dist/http/router.d.ts.map +1 -1
- package/dist/http/router.js +4 -1
- package/dist/http/router.js.map +1 -1
- package/dist/http/types.d.ts +40 -0
- package/dist/http/types.d.ts.map +1 -1
- package/dist/http/types.js +9 -0
- package/dist/http/types.js.map +1 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -1
- package/dist/schema/schema-store.d.ts +2 -2
- package/dist/server.d.ts +45 -0
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +41 -0
- package/dist/server.js.map +1 -1
- package/dist/validation/entry-validator.d.ts.map +1 -1
- package/dist/validation/entry-validator.js +56 -1
- package/dist/validation/entry-validator.js.map +1 -1
- package/package.json +13 -1
- package/dist/asset-store.d.ts +0 -32
- package/dist/asset-store.d.ts.map +0 -1
- package/dist/asset-store.js +0 -85
- package/dist/asset-store.js.map +0 -1
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SVG sanitization. Server-only - never import from client/editor code.
|
|
3
|
+
*
|
|
4
|
+
* Sanitizer choice: the design record (.claude/future-tasks/assets-media-system.md)
|
|
5
|
+
* suggested `dompurify` + a lightweight DOM shim (e.g. `linkedom`) in place of the
|
|
6
|
+
* heavier `jsdom`. In practice that pairing is unsafe: DOMPurify's own README warns
|
|
7
|
+
* that immature DOM shims can cause it to fail in ways that produce an XSS hole even
|
|
8
|
+
* when DOMPurify itself behaves correctly, and that is exactly what was reproduced
|
|
9
|
+
* here - `createDOMPurify(linkedomWindow).sanitize(dirtySvg)` returned the INPUT
|
|
10
|
+
* UNCHANGED (a `<script>`, `onload=`, and `<foreignObject>` all survived), because
|
|
11
|
+
* linkedom's window has no `NodeFilter` global and DOMPurify silently no-ops when it
|
|
12
|
+
* doesn't recognize the environment as supported. That is a dangerous failure mode
|
|
13
|
+
* for a security boundary, so this module uses `sanitize-html` instead: it works
|
|
14
|
+
* directly on the parser tree (htmlparser2, no DOM shim to be incomplete), is
|
|
15
|
+
* actively maintained, and - verified empirically - correctly strips scripts, event
|
|
16
|
+
* handler attributes, and `foreignObject` while preserving a valid, still-parseable
|
|
17
|
+
* SVG document.
|
|
18
|
+
*
|
|
19
|
+
* Allowlist-based (not blocklist): only elements/attributes known to be safe survive.
|
|
20
|
+
* `href`/`xlink:href` are further restricted to local fragment references (`#...`) so
|
|
21
|
+
* `<use>`/`<a>` can reference in-document defs but can never point at an external URL
|
|
22
|
+
* (tracking pixels, `javascript:` URLs, etc.) - sanitize-html's tag-level scheme
|
|
23
|
+
* allowlisting does not cover attribute name patterns like this, so it is enforced
|
|
24
|
+
* directly in `transformTags` below.
|
|
25
|
+
*/
|
|
26
|
+
import sanitizeHtml from 'sanitize-html';
|
|
27
|
+
const ALLOWED_SVG_TAGS = [
|
|
28
|
+
'svg',
|
|
29
|
+
'g',
|
|
30
|
+
'defs',
|
|
31
|
+
'symbol',
|
|
32
|
+
'use',
|
|
33
|
+
'title',
|
|
34
|
+
'desc',
|
|
35
|
+
'rect',
|
|
36
|
+
'circle',
|
|
37
|
+
'ellipse',
|
|
38
|
+
'line',
|
|
39
|
+
'polyline',
|
|
40
|
+
'polygon',
|
|
41
|
+
'path',
|
|
42
|
+
'text',
|
|
43
|
+
'tspan',
|
|
44
|
+
'textPath',
|
|
45
|
+
'linearGradient',
|
|
46
|
+
'radialGradient',
|
|
47
|
+
'stop',
|
|
48
|
+
'clipPath',
|
|
49
|
+
'mask',
|
|
50
|
+
'pattern',
|
|
51
|
+
'filter',
|
|
52
|
+
'feGaussianBlur',
|
|
53
|
+
'feOffset',
|
|
54
|
+
'feBlend',
|
|
55
|
+
'feColorMatrix',
|
|
56
|
+
'feComponentTransfer',
|
|
57
|
+
'feFuncA',
|
|
58
|
+
'feFuncR',
|
|
59
|
+
'feFuncG',
|
|
60
|
+
'feFuncB',
|
|
61
|
+
'feComposite',
|
|
62
|
+
'feFlood',
|
|
63
|
+
'feMerge',
|
|
64
|
+
'feMergeNode',
|
|
65
|
+
'feMorphology',
|
|
66
|
+
'feTile',
|
|
67
|
+
'feTurbulence',
|
|
68
|
+
'feDropShadow',
|
|
69
|
+
'feDisplacementMap',
|
|
70
|
+
'image',
|
|
71
|
+
'a',
|
|
72
|
+
'marker',
|
|
73
|
+
'metadata',
|
|
74
|
+
];
|
|
75
|
+
const ALLOWED_SVG_ATTRS = [
|
|
76
|
+
'id',
|
|
77
|
+
'class',
|
|
78
|
+
'style',
|
|
79
|
+
'transform',
|
|
80
|
+
'fill',
|
|
81
|
+
'fill-rule',
|
|
82
|
+
'fill-opacity',
|
|
83
|
+
'stroke',
|
|
84
|
+
'stroke-width',
|
|
85
|
+
'stroke-linecap',
|
|
86
|
+
'stroke-linejoin',
|
|
87
|
+
'stroke-dasharray',
|
|
88
|
+
'stroke-opacity',
|
|
89
|
+
'opacity',
|
|
90
|
+
'd',
|
|
91
|
+
'x',
|
|
92
|
+
'y',
|
|
93
|
+
'width',
|
|
94
|
+
'height',
|
|
95
|
+
'cx',
|
|
96
|
+
'cy',
|
|
97
|
+
'r',
|
|
98
|
+
'rx',
|
|
99
|
+
'ry',
|
|
100
|
+
'x1',
|
|
101
|
+
'y1',
|
|
102
|
+
'x2',
|
|
103
|
+
'y2',
|
|
104
|
+
'points',
|
|
105
|
+
'viewBox',
|
|
106
|
+
'preserveAspectRatio',
|
|
107
|
+
'xmlns',
|
|
108
|
+
'xmlns:xlink',
|
|
109
|
+
'version',
|
|
110
|
+
'offset',
|
|
111
|
+
'stop-color',
|
|
112
|
+
'stop-opacity',
|
|
113
|
+
'gradientUnits',
|
|
114
|
+
'gradientTransform',
|
|
115
|
+
'patternUnits',
|
|
116
|
+
'patternTransform',
|
|
117
|
+
'href',
|
|
118
|
+
'xlink:href',
|
|
119
|
+
'clip-path',
|
|
120
|
+
'mask',
|
|
121
|
+
'filter',
|
|
122
|
+
'font-family',
|
|
123
|
+
'font-size',
|
|
124
|
+
'font-weight',
|
|
125
|
+
'text-anchor',
|
|
126
|
+
'dx',
|
|
127
|
+
'dy',
|
|
128
|
+
'marker-start',
|
|
129
|
+
'marker-mid',
|
|
130
|
+
'marker-end',
|
|
131
|
+
];
|
|
132
|
+
/**
|
|
133
|
+
* Elements whose entire subtree (tags AND text) is discarded when they are not
|
|
134
|
+
* in the allowlist. Without this, sanitize-html's default `disallowedTagsMode:
|
|
135
|
+
* 'discard'` removes the tag itself but leaves loose text content behind (e.g.
|
|
136
|
+
* a stripped `<foreignObject><div>evil</div></foreignObject>` would otherwise
|
|
137
|
+
* leave a bare "evil" text node in the sanitized output).
|
|
138
|
+
*/
|
|
139
|
+
const NON_TEXT_TAGS = ['script', 'style', 'textarea', 'option', 'foreignObject'];
|
|
140
|
+
/**
|
|
141
|
+
* Presentation attributes (beyond `style`) whose SVG grammar accepts a
|
|
142
|
+
* `<funciri>` (`url(...)`) value - `fill`/`stroke` reference paint servers
|
|
143
|
+
* (gradients/patterns), `filter`/`mask`/`clip-path` reference filter-effect
|
|
144
|
+
* elements, and the three `marker-*` attributes reference `<marker>`
|
|
145
|
+
* elements. Every one of these is in `ALLOWED_SVG_ATTRS`, so every one of
|
|
146
|
+
* them is a route for the same external-fetch beacon `style`'s `url(...)`
|
|
147
|
+
* hardening (below) already closes: `<rect fill="url(https://evil/beacon)">`
|
|
148
|
+
* fetches externally the moment the SVG is opened as a document, exactly
|
|
149
|
+
* like a `style="background:url(...)"` would.
|
|
150
|
+
*/
|
|
151
|
+
const URL_BEARING_ATTRS = new Set([
|
|
152
|
+
'style',
|
|
153
|
+
'fill',
|
|
154
|
+
'stroke',
|
|
155
|
+
'filter',
|
|
156
|
+
'mask',
|
|
157
|
+
'clip-path',
|
|
158
|
+
'marker-start',
|
|
159
|
+
'marker-mid',
|
|
160
|
+
'marker-end',
|
|
161
|
+
]);
|
|
162
|
+
/**
|
|
163
|
+
* True when `value` is safe for a URL-bearing presentation attribute: either
|
|
164
|
+
* it has no `url(...)` funciri and no backslash at all (the common case -
|
|
165
|
+
* plain colors, `none`, etc.), or its only `url(...)` reference is a local
|
|
166
|
+
* fragment (`url(#foo)`) - the same in-document-only allowance `href`/
|
|
167
|
+
* `xlink:href` get, and exactly what gradients/filters/markers legitimately
|
|
168
|
+
* need to reference document-local `<linearGradient>`/`<filter>`/`<marker>`
|
|
169
|
+
* defs. Backslashes are rejected unconditionally (even alongside an
|
|
170
|
+
* otherwise-local `url(#foo)`): CSS escapes (e.g. `\75 rl(...)` -> `url(...)`)
|
|
171
|
+
* let a crafted value slip past a plain `url(` text match yet still resolve
|
|
172
|
+
* to an external fetch in the browser's CSS/SVG value parser, and
|
|
173
|
+
* backslashes have no legitimate use in any of these attribute values.
|
|
174
|
+
*/
|
|
175
|
+
function isSafeUrlBearingValue(value) {
|
|
176
|
+
if (value.includes('\\'))
|
|
177
|
+
return false;
|
|
178
|
+
const urlMatches = value.matchAll(/url\s*\(\s*(['"]?)\s*([^)'"]*?)\s*\1\s*\)/gi);
|
|
179
|
+
let sawUrl = false;
|
|
180
|
+
for (const match of urlMatches) {
|
|
181
|
+
sawUrl = true;
|
|
182
|
+
if (!match[2].startsWith('#'))
|
|
183
|
+
return false;
|
|
184
|
+
}
|
|
185
|
+
return sawUrl ? true : !/url\s*\(/i.test(value);
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Sanitize an SVG document (as text). Strips scripts, event-handler attributes,
|
|
189
|
+
* `<foreignObject>` (and its content), inline `<style>` (CSS injection surface),
|
|
190
|
+
* any `href`/`xlink:href` that isn't a local fragment reference, and any
|
|
191
|
+
* `url(...)` reference on a paint/filter/marker attribute that isn't a local
|
|
192
|
+
* fragment reference (external-fetch tracking beacon).
|
|
193
|
+
*
|
|
194
|
+
* Input must already be confirmed to be an SVG document (see `sniffSvg` in
|
|
195
|
+
* pipeline.ts) - this function does not re-validate the root element.
|
|
196
|
+
*/
|
|
197
|
+
export function sanitizeSvg(svgText) {
|
|
198
|
+
return sanitizeHtml(svgText, {
|
|
199
|
+
allowedTags: ALLOWED_SVG_TAGS,
|
|
200
|
+
allowedAttributes: { '*': ALLOWED_SVG_ATTRS },
|
|
201
|
+
nonTextTags: NON_TEXT_TAGS,
|
|
202
|
+
// xmlMode makes htmlparser2 treat this as XML (self-closing tags, no HTML
|
|
203
|
+
// element-specific parsing quirks) rather than HTML, which matters for SVG.
|
|
204
|
+
parser: { xmlMode: true, decodeEntities: true },
|
|
205
|
+
transformTags: {
|
|
206
|
+
'*': (tagName, attribs) => {
|
|
207
|
+
const filtered = {};
|
|
208
|
+
for (const [key, value] of Object.entries(attribs)) {
|
|
209
|
+
if ((key === 'href' || key === 'xlink:href') && !value.startsWith('#')) {
|
|
210
|
+
continue; // drop external/non-local references
|
|
211
|
+
}
|
|
212
|
+
if (URL_BEARING_ATTRS.has(key) && !isSafeUrlBearingValue(value)) {
|
|
213
|
+
continue; // drop external/non-local url(...) references (or any backslash)
|
|
214
|
+
}
|
|
215
|
+
filtered[key] = value;
|
|
216
|
+
}
|
|
217
|
+
return { tagName, attribs: filtered };
|
|
218
|
+
},
|
|
219
|
+
},
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
//# sourceMappingURL=svg-sanitizer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"svg-sanitizer.js","sourceRoot":"","sources":["../../src/assets/svg-sanitizer.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAEH,OAAO,YAAY,MAAM,eAAe,CAAA;AAExC,MAAM,gBAAgB,GAAG;IACvB,KAAK;IACL,GAAG;IACH,MAAM;IACN,QAAQ;IACR,KAAK;IACL,OAAO;IACP,MAAM;IACN,MAAM;IACN,QAAQ;IACR,SAAS;IACT,MAAM;IACN,UAAU;IACV,SAAS;IACT,MAAM;IACN,MAAM;IACN,OAAO;IACP,UAAU;IACV,gBAAgB;IAChB,gBAAgB;IAChB,MAAM;IACN,UAAU;IACV,MAAM;IACN,SAAS;IACT,QAAQ;IACR,gBAAgB;IAChB,UAAU;IACV,SAAS;IACT,eAAe;IACf,qBAAqB;IACrB,SAAS;IACT,SAAS;IACT,SAAS;IACT,SAAS;IACT,aAAa;IACb,SAAS;IACT,SAAS;IACT,aAAa;IACb,cAAc;IACd,QAAQ;IACR,cAAc;IACd,cAAc;IACd,mBAAmB;IACnB,OAAO;IACP,GAAG;IACH,QAAQ;IACR,UAAU;CACX,CAAA;AAED,MAAM,iBAAiB,GAAG;IACxB,IAAI;IACJ,OAAO;IACP,OAAO;IACP,WAAW;IACX,MAAM;IACN,WAAW;IACX,cAAc;IACd,QAAQ;IACR,cAAc;IACd,gBAAgB;IAChB,iBAAiB;IACjB,kBAAkB;IAClB,gBAAgB;IAChB,SAAS;IACT,GAAG;IACH,GAAG;IACH,GAAG;IACH,OAAO;IACP,QAAQ;IACR,IAAI;IACJ,IAAI;IACJ,GAAG;IACH,IAAI;IACJ,IAAI;IACJ,IAAI;IACJ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UAAU,WAAW,CAAC,OAAe;IACzC,OAAO,YAAY,CAAC,OAAO,EAAE;QAC3B,WAAW,EAAE,gBAAgB;QAC7B,iBAAiB,EAAE,EAAE,GAAG,EAAE,iBAAiB,EAAE;QAC7C,WAAW,EAAE,aAAa;QAC1B,0EAA0E;QAC1E,4EAA4E;QAC5E,MAAM,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,cAAc,EAAE,IAAI,EAAE;QAC/C,aAAa,EAAE;YACb,GAAG,EAAE,CAAC,OAAO,EAAE,OAAO,EAAE,EAAE;gBACxB,MAAM,QAAQ,GAA2B,EAAE,CAAA;gBAC3C,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC;oBACnD,IAAI,CAAC,GAAG,KAAK,MAAM,IAAI,GAAG,KAAK,YAAY,CAAC,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC;wBACvE,SAAQ,CAAC,qCAAqC;oBAChD,CAAC;oBACD,IAAI,iBAAiB,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,qBAAqB,CAAC,KAAK,CAAC,EAAE,CAAC;wBAChE,SAAQ,CAAC,iEAAiE;oBAC5E,CAAC;oBACD,QAAQ,CAAC,GAAG,CAAC,GAAG,KAAK,CAAA;gBACvB,CAAC;gBACD,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,CAAA;YACvC,CAAC;SACF;KACF,CAAC,CAAA;AACJ,CAAC"}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure parser/formatter for `/assets/t/{directives}/{hash32}/{slug}.{ext}`
|
|
3
|
+
* transform URLs. Isomorphic and dependency-free by design: this module has
|
|
4
|
+
* NO imports (not even other files in this directory) so it can be imported
|
|
5
|
+
* from client bundles (via `assetUrl`/`assetSrcSet` in asset-url.ts, exported
|
|
6
|
+
* off the package's main entry) as well as from the server-only transform
|
|
7
|
+
* engine (transform.ts) and the future prod transform Lambda (PR 7), without
|
|
8
|
+
* ever pulling in node:crypto, sharp, or any other server-only dependency.
|
|
9
|
+
*
|
|
10
|
+
* `{directives}` is either the literal identity token (`orig`) or a
|
|
11
|
+
* comma-separated list of `key=value` pairs drawn from:
|
|
12
|
+
* w={int} output width - allowlisted to multiples of 160 in [160, 4096]
|
|
13
|
+
* (bounds cache-stuffing; upscaling is rejected at transform
|
|
14
|
+
* time via `withoutEnlargement`, not here)
|
|
15
|
+
* f={fmt} output format: webp | jpeg | png - when present, the URL's
|
|
16
|
+
* `{ext}` must equal it exactly
|
|
17
|
+
* q={int} quality 1..100 (encoder-dependent)
|
|
18
|
+
* c={rect} normalized crop rect `x:y:w:h`, four floats in [0,1] with
|
|
19
|
+
* x+w<=1 and y+h<=1, w>0 and h>0 (colon-separated - commas are
|
|
20
|
+
* the directive separator)
|
|
21
|
+
*
|
|
22
|
+
* Duplicate keys, unknown keys, and empty directive strings are rejected.
|
|
23
|
+
* `orig` must appear alone.
|
|
24
|
+
*/
|
|
25
|
+
/** The identity transform directive name - EXIF-strip only, no other change. */
|
|
26
|
+
export declare const IDENTITY_TRANSFORM_DIRECTIVE = "orig";
|
|
27
|
+
export type OutputFormat = 'webp' | 'jpeg' | 'png';
|
|
28
|
+
export interface CropRect {
|
|
29
|
+
readonly x: number;
|
|
30
|
+
readonly y: number;
|
|
31
|
+
readonly w: number;
|
|
32
|
+
readonly h: number;
|
|
33
|
+
}
|
|
34
|
+
/** Identity ('orig'): EXIF-strip only, no resize/format/quality change. */
|
|
35
|
+
export interface IdentityDirectives {
|
|
36
|
+
readonly identity: true;
|
|
37
|
+
}
|
|
38
|
+
/** Non-identity directive set - every field optional, but at least one is present. */
|
|
39
|
+
export interface ResizeDirectives {
|
|
40
|
+
readonly identity: false;
|
|
41
|
+
readonly width?: number;
|
|
42
|
+
readonly format?: OutputFormat;
|
|
43
|
+
readonly quality?: number;
|
|
44
|
+
readonly crop?: CropRect;
|
|
45
|
+
}
|
|
46
|
+
export type TransformDirectives = IdentityDirectives | ResizeDirectives;
|
|
47
|
+
export interface ParsedTransformPath {
|
|
48
|
+
readonly directives: TransformDirectives;
|
|
49
|
+
readonly hash32: string;
|
|
50
|
+
readonly slug: string;
|
|
51
|
+
readonly ext: string;
|
|
52
|
+
}
|
|
53
|
+
export type ParseTransformPathResult = ({
|
|
54
|
+
readonly ok: true;
|
|
55
|
+
} & ParsedTransformPath) | {
|
|
56
|
+
readonly ok: false;
|
|
57
|
+
readonly error: string;
|
|
58
|
+
};
|
|
59
|
+
/**
|
|
60
|
+
* Decompression-bomb cap: the max `width * height` (post-decode pixel count)
|
|
61
|
+
* any raster the pipeline handles may have - checked both at upload finalize
|
|
62
|
+
* time (pipeline.ts, against the raw source dims) and at transform decode
|
|
63
|
+
* time (transform.ts, via sharp's `limitInputPixels`). Shared here (rather
|
|
64
|
+
* than duplicated in each server-only module) so it stays a single number,
|
|
65
|
+
* and because this file is dependency-free/isomorphic it can be imported by
|
|
66
|
+
* both without pulling sharp or file-type into anything. 4096x4096 (16.7 MP)
|
|
67
|
+
* comfortably covers the largest output this system ever serves (MAX_WIDTH)
|
|
68
|
+
* while still bounding decode-time memory for a small/compressible-but-huge
|
|
69
|
+
* source (e.g. a 30000x30000 solid-color PNG).
|
|
70
|
+
*/
|
|
71
|
+
export declare const MAX_INPUT_PIXELS: number;
|
|
72
|
+
/** True if `width` is on the allowlist: a multiple of 160 in [160, 4096]. */
|
|
73
|
+
export declare function isAllowedTransformWidth(width: number): boolean;
|
|
74
|
+
/** True if `quality` is on the allowlist: a multiple of 5 in [30, 95]. */
|
|
75
|
+
export declare function isAllowedTransformQuality(quality: number): boolean;
|
|
76
|
+
/**
|
|
77
|
+
* True when x, y, w, h describe a valid normalized crop rect: all four values
|
|
78
|
+
* finite and in [0,1], w and h strictly positive, and the rect fits within
|
|
79
|
+
* bounds (x+w<=1, y+h<=1). Shared by the URL-directive parser (`parseCrop`,
|
|
80
|
+
* below, from a `x:y:w:h` string) and image field value validation (from a
|
|
81
|
+
* `{x,y,w,h}` object, in validation/entry-validator.ts) so both enforce
|
|
82
|
+
* identical constraints.
|
|
83
|
+
*/
|
|
84
|
+
export declare function isValidCropRect(x: number, y: number, w: number, h: number): boolean;
|
|
85
|
+
/**
|
|
86
|
+
* Parse the three path segments after the `assets/t/` prefix:
|
|
87
|
+
* `[directivesRaw, hash32, "{slug}.{ext}"]`. The raw route strips the
|
|
88
|
+
* `assets/t/` prefix and splits the remaining key by `/` before calling this.
|
|
89
|
+
*/
|
|
90
|
+
export declare function parseTransformPath(segments: readonly string[]): ParseTransformPathResult;
|
|
91
|
+
/**
|
|
92
|
+
* Canonical string form of a directive set, so equivalent directive sets
|
|
93
|
+
* (different key order, different float formatting) always map to the same
|
|
94
|
+
* cache key. Order is fixed alphabetically by key: c, f, q, w.
|
|
95
|
+
*/
|
|
96
|
+
export declare function formatDirectives(directives: TransformDirectives): string;
|
|
97
|
+
//# sourceMappingURL=transform-directives.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transform-directives.d.ts","sourceRoot":"","sources":["../../src/assets/transform-directives.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAEH,gFAAgF;AAChF,eAAO,MAAM,4BAA4B,SAAS,CAAA;AAElD,MAAM,MAAM,YAAY,GAAG,MAAM,GAAG,MAAM,GAAG,KAAK,CAAA;AAElD,MAAM,WAAW,QAAQ;IACvB,QAAQ,CAAC,CAAC,EAAE,MAAM,CAAA;IAClB,QAAQ,CAAC,CAAC,EAAE,MAAM,CAAA;IAClB,QAAQ,CAAC,CAAC,EAAE,MAAM,CAAA;IAClB,QAAQ,CAAC,CAAC,EAAE,MAAM,CAAA;CACnB;AAED,2EAA2E;AAC3E,MAAM,WAAW,kBAAkB;IACjC,QAAQ,CAAC,QAAQ,EAAE,IAAI,CAAA;CACxB;AAED,sFAAsF;AAC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AkCxF;AAED;;;;GAIG;AACH,wBAAgB,gBAAgB,CAAC,UAAU,EAAE,mBAAmB,GAAG,MAAM,CA4BxE"}
|
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure parser/formatter for `/assets/t/{directives}/{hash32}/{slug}.{ext}`
|
|
3
|
+
* transform URLs. Isomorphic and dependency-free by design: this module has
|
|
4
|
+
* NO imports (not even other files in this directory) so it can be imported
|
|
5
|
+
* from client bundles (via `assetUrl`/`assetSrcSet` in asset-url.ts, exported
|
|
6
|
+
* off the package's main entry) as well as from the server-only transform
|
|
7
|
+
* engine (transform.ts) and the future prod transform Lambda (PR 7), without
|
|
8
|
+
* ever pulling in node:crypto, sharp, or any other server-only dependency.
|
|
9
|
+
*
|
|
10
|
+
* `{directives}` is either the literal identity token (`orig`) or a
|
|
11
|
+
* comma-separated list of `key=value` pairs drawn from:
|
|
12
|
+
* w={int} output width - allowlisted to multiples of 160 in [160, 4096]
|
|
13
|
+
* (bounds cache-stuffing; upscaling is rejected at transform
|
|
14
|
+
* time via `withoutEnlargement`, not here)
|
|
15
|
+
* f={fmt} output format: webp | jpeg | png - when present, the URL's
|
|
16
|
+
* `{ext}` must equal it exactly
|
|
17
|
+
* q={int} quality 1..100 (encoder-dependent)
|
|
18
|
+
* c={rect} normalized crop rect `x:y:w:h`, four floats in [0,1] with
|
|
19
|
+
* x+w<=1 and y+h<=1, w>0 and h>0 (colon-separated - commas are
|
|
20
|
+
* the directive separator)
|
|
21
|
+
*
|
|
22
|
+
* Duplicate keys, unknown keys, and empty directive strings are rejected.
|
|
23
|
+
* `orig` must appear alone.
|
|
24
|
+
*/
|
|
25
|
+
/** The identity transform directive name - EXIF-strip only, no other change. */
|
|
26
|
+
export const IDENTITY_TRANSFORM_DIRECTIVE = 'orig';
|
|
27
|
+
const MIN_WIDTH = 160;
|
|
28
|
+
const MAX_WIDTH = 4096;
|
|
29
|
+
const WIDTH_STEP = 160;
|
|
30
|
+
/**
|
|
31
|
+
* Decompression-bomb cap: the max `width * height` (post-decode pixel count)
|
|
32
|
+
* any raster the pipeline handles may have - checked both at upload finalize
|
|
33
|
+
* time (pipeline.ts, against the raw source dims) and at transform decode
|
|
34
|
+
* time (transform.ts, via sharp's `limitInputPixels`). Shared here (rather
|
|
35
|
+
* than duplicated in each server-only module) so it stays a single number,
|
|
36
|
+
* and because this file is dependency-free/isomorphic it can be imported by
|
|
37
|
+
* both without pulling sharp or file-type into anything. 4096x4096 (16.7 MP)
|
|
38
|
+
* comfortably covers the largest output this system ever serves (MAX_WIDTH)
|
|
39
|
+
* while still bounding decode-time memory for a small/compressible-but-huge
|
|
40
|
+
* source (e.g. a 30000x30000 solid-color PNG).
|
|
41
|
+
*/
|
|
42
|
+
export const MAX_INPUT_PIXELS = 4096 * 4096;
|
|
43
|
+
// Quality is allowlisted (multiples of 5 in [30, 95] - 14 values) for the
|
|
44
|
+
// same cache-stuffing reason as width: every accepted directive combination
|
|
45
|
+
// becomes a stored cache object in prod, so unbounded q would multiply the
|
|
46
|
+
// per-asset variant space by 100. Crop remains the one effectively unbounded
|
|
47
|
+
// dimension (editor rects need float precision) - prod mitigation (rate
|
|
48
|
+
// limiting / signed crops) is tracked in the design record for the CDK PR.
|
|
49
|
+
const MIN_QUALITY = 30;
|
|
50
|
+
const MAX_QUALITY = 95;
|
|
51
|
+
const QUALITY_STEP = 5;
|
|
52
|
+
const CROP_PRECISION = 4;
|
|
53
|
+
const HASH32_RE = /^[a-f0-9]{32}$/;
|
|
54
|
+
const SLUG_RE = /^[a-z0-9-]+$/;
|
|
55
|
+
const EXT_RE = /^[a-z0-9]{1,10}$/;
|
|
56
|
+
const POSITIVE_INT_RE = /^[1-9][0-9]*$/;
|
|
57
|
+
// The two alternatives are mutually exclusive on their very first character
|
|
58
|
+
// ('0' vs '1'), and neither branch nests a quantifier inside another - each
|
|
59
|
+
// character class match advances the position by exactly one, so there is
|
|
60
|
+
// exactly one way to parse any matching string (no backtracking ambiguity).
|
|
61
|
+
// The security plugin's heuristic flags any alternation-plus-quantifier
|
|
62
|
+
// shape regardless; input here is also a single bounded URL path segment,
|
|
63
|
+
// not attacker-scalable text.
|
|
64
|
+
// eslint-disable-next-line security/detect-unsafe-regex
|
|
65
|
+
const UNIT_FLOAT_RE = /^(?:0(?:\.[0-9]+)?|1(?:\.0+)?)$/;
|
|
66
|
+
const KNOWN_DIRECTIVE_KEYS = new Set(['w', 'f', 'q', 'c']);
|
|
67
|
+
function isOutputFormat(value) {
|
|
68
|
+
return value === 'webp' || value === 'jpeg' || value === 'png';
|
|
69
|
+
}
|
|
70
|
+
/** True if `width` is on the allowlist: a multiple of 160 in [160, 4096]. */
|
|
71
|
+
export function isAllowedTransformWidth(width) {
|
|
72
|
+
return (Number.isInteger(width) && width >= MIN_WIDTH && width <= MAX_WIDTH && width % WIDTH_STEP === 0);
|
|
73
|
+
}
|
|
74
|
+
function parseWidth(value) {
|
|
75
|
+
if (!POSITIVE_INT_RE.test(value))
|
|
76
|
+
return null;
|
|
77
|
+
const n = Number(value);
|
|
78
|
+
return isAllowedTransformWidth(n) ? n : null;
|
|
79
|
+
}
|
|
80
|
+
/** True if `quality` is on the allowlist: a multiple of 5 in [30, 95]. */
|
|
81
|
+
export function isAllowedTransformQuality(quality) {
|
|
82
|
+
return (Number.isInteger(quality) &&
|
|
83
|
+
quality >= MIN_QUALITY &&
|
|
84
|
+
quality <= MAX_QUALITY &&
|
|
85
|
+
quality % QUALITY_STEP === 0);
|
|
86
|
+
}
|
|
87
|
+
function parseQuality(value) {
|
|
88
|
+
if (!POSITIVE_INT_RE.test(value))
|
|
89
|
+
return null;
|
|
90
|
+
const n = Number(value);
|
|
91
|
+
return isAllowedTransformQuality(n) ? n : null;
|
|
92
|
+
}
|
|
93
|
+
function parseUnitFloat(value) {
|
|
94
|
+
if (!UNIT_FLOAT_RE.test(value))
|
|
95
|
+
return null;
|
|
96
|
+
const n = Number(value);
|
|
97
|
+
return n >= 0 && n <= 1 ? n : null;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* True when x, y, w, h describe a valid normalized crop rect: all four values
|
|
101
|
+
* finite and in [0,1], w and h strictly positive, and the rect fits within
|
|
102
|
+
* bounds (x+w<=1, y+h<=1). Shared by the URL-directive parser (`parseCrop`,
|
|
103
|
+
* below, from a `x:y:w:h` string) and image field value validation (from a
|
|
104
|
+
* `{x,y,w,h}` object, in validation/entry-validator.ts) so both enforce
|
|
105
|
+
* identical constraints.
|
|
106
|
+
*/
|
|
107
|
+
export function isValidCropRect(x, y, w, h) {
|
|
108
|
+
for (const n of [x, y, w, h]) {
|
|
109
|
+
if (!Number.isFinite(n) || n < 0 || n > 1)
|
|
110
|
+
return false;
|
|
111
|
+
}
|
|
112
|
+
if (w <= 0 || h <= 0)
|
|
113
|
+
return false;
|
|
114
|
+
if (x + w > 1 || y + h > 1)
|
|
115
|
+
return false;
|
|
116
|
+
return true;
|
|
117
|
+
}
|
|
118
|
+
function parseCrop(value) {
|
|
119
|
+
const parts = value.split(':');
|
|
120
|
+
if (parts.length !== 4)
|
|
121
|
+
return null;
|
|
122
|
+
const nums = parts.map(parseUnitFloat);
|
|
123
|
+
if (nums.some((n) => n === null))
|
|
124
|
+
return null;
|
|
125
|
+
const [x, y, w, h] = nums;
|
|
126
|
+
if (!isValidCropRect(x, y, w, h))
|
|
127
|
+
return null;
|
|
128
|
+
return { x, y, w, h };
|
|
129
|
+
}
|
|
130
|
+
function formatUnitFloat(n) {
|
|
131
|
+
return n.toFixed(CROP_PRECISION);
|
|
132
|
+
}
|
|
133
|
+
function err(error) {
|
|
134
|
+
return { ok: false, error };
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Parse a `key=value,key=value` directive string (everything between
|
|
138
|
+
* `assets/t/` and the hash32 segment) into a `TransformDirectives`, or
|
|
139
|
+
* `orig` into the identity directive.
|
|
140
|
+
*/
|
|
141
|
+
function parseDirectivesString(raw) {
|
|
142
|
+
if (raw === IDENTITY_TRANSFORM_DIRECTIVE) {
|
|
143
|
+
return { ok: true, directives: { identity: true } };
|
|
144
|
+
}
|
|
145
|
+
if (raw.length === 0) {
|
|
146
|
+
return err('Empty directives');
|
|
147
|
+
}
|
|
148
|
+
const seen = new Set();
|
|
149
|
+
let width;
|
|
150
|
+
let format;
|
|
151
|
+
let quality;
|
|
152
|
+
let crop;
|
|
153
|
+
for (const pair of raw.split(',')) {
|
|
154
|
+
const eqIdx = pair.indexOf('=');
|
|
155
|
+
// Also catches `orig` combined with other directives (e.g. `orig,w=320`):
|
|
156
|
+
// `orig` has no `=`, so it falls into this same malformed-pair branch -
|
|
157
|
+
// `orig` is only ever valid when it is the entire directives string.
|
|
158
|
+
if (eqIdx <= 0 || eqIdx === pair.length - 1) {
|
|
159
|
+
return err(`Malformed directive: '${pair}'`);
|
|
160
|
+
}
|
|
161
|
+
const key = pair.slice(0, eqIdx);
|
|
162
|
+
const value = pair.slice(eqIdx + 1);
|
|
163
|
+
if (!KNOWN_DIRECTIVE_KEYS.has(key)) {
|
|
164
|
+
return err(`Unknown directive key: '${key}'`);
|
|
165
|
+
}
|
|
166
|
+
if (seen.has(key)) {
|
|
167
|
+
return err(`Duplicate directive key: '${key}'`);
|
|
168
|
+
}
|
|
169
|
+
seen.add(key);
|
|
170
|
+
switch (key) {
|
|
171
|
+
case 'w': {
|
|
172
|
+
const w = parseWidth(value);
|
|
173
|
+
if (w === null)
|
|
174
|
+
return err(`Invalid width: '${value}'`);
|
|
175
|
+
width = w;
|
|
176
|
+
break;
|
|
177
|
+
}
|
|
178
|
+
case 'f': {
|
|
179
|
+
if (!isOutputFormat(value))
|
|
180
|
+
return err(`Invalid format: '${value}'`);
|
|
181
|
+
format = value;
|
|
182
|
+
break;
|
|
183
|
+
}
|
|
184
|
+
case 'q': {
|
|
185
|
+
const q = parseQuality(value);
|
|
186
|
+
if (q === null)
|
|
187
|
+
return err(`Invalid quality: '${value}'`);
|
|
188
|
+
quality = q;
|
|
189
|
+
break;
|
|
190
|
+
}
|
|
191
|
+
case 'c': {
|
|
192
|
+
const c = parseCrop(value);
|
|
193
|
+
if (!c)
|
|
194
|
+
return err(`Invalid crop: '${value}'`);
|
|
195
|
+
crop = c;
|
|
196
|
+
break;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
return { ok: true, directives: { identity: false, width, format, quality, crop } };
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Parse the three path segments after the `assets/t/` prefix:
|
|
204
|
+
* `[directivesRaw, hash32, "{slug}.{ext}"]`. The raw route strips the
|
|
205
|
+
* `assets/t/` prefix and splits the remaining key by `/` before calling this.
|
|
206
|
+
*/
|
|
207
|
+
export function parseTransformPath(segments) {
|
|
208
|
+
if (segments.length !== 3) {
|
|
209
|
+
return err(`Expected 3 path segments (directives/hash32/slug.ext), got ${segments.length}`);
|
|
210
|
+
}
|
|
211
|
+
const [directivesRaw, hash32, slugExt] = segments;
|
|
212
|
+
if (!HASH32_RE.test(hash32)) {
|
|
213
|
+
return err(`Invalid hash32: '${hash32}'`);
|
|
214
|
+
}
|
|
215
|
+
const lastDot = slugExt.lastIndexOf('.');
|
|
216
|
+
if (lastDot <= 0 || lastDot === slugExt.length - 1) {
|
|
217
|
+
return err(`Invalid slug.ext segment: '${slugExt}'`);
|
|
218
|
+
}
|
|
219
|
+
const slug = slugExt.slice(0, lastDot);
|
|
220
|
+
const ext = slugExt.slice(lastDot + 1);
|
|
221
|
+
if (!SLUG_RE.test(slug)) {
|
|
222
|
+
return err(`Invalid slug: '${slug}'`);
|
|
223
|
+
}
|
|
224
|
+
if (!EXT_RE.test(ext)) {
|
|
225
|
+
return err(`Invalid ext: '${ext}'`);
|
|
226
|
+
}
|
|
227
|
+
const parsedDirectives = parseDirectivesString(directivesRaw);
|
|
228
|
+
if (!parsedDirectives.ok) {
|
|
229
|
+
return parsedDirectives;
|
|
230
|
+
}
|
|
231
|
+
const { directives } = parsedDirectives;
|
|
232
|
+
if (!directives.identity && directives.format !== undefined && directives.format !== ext) {
|
|
233
|
+
return err(`Extension '${ext}' does not match format '${directives.format}'`);
|
|
234
|
+
}
|
|
235
|
+
return { ok: true, directives, hash32, slug, ext };
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Canonical string form of a directive set, so equivalent directive sets
|
|
239
|
+
* (different key order, different float formatting) always map to the same
|
|
240
|
+
* cache key. Order is fixed alphabetically by key: c, f, q, w.
|
|
241
|
+
*/
|
|
242
|
+
export function formatDirectives(directives) {
|
|
243
|
+
if (directives.identity) {
|
|
244
|
+
return IDENTITY_TRANSFORM_DIRECTIVE;
|
|
245
|
+
}
|
|
246
|
+
const parts = [];
|
|
247
|
+
if (directives.crop) {
|
|
248
|
+
const { x, y, w, h } = directives.crop;
|
|
249
|
+
parts.push(`c=${formatUnitFloat(x)}:${formatUnitFloat(y)}:${formatUnitFloat(w)}:${formatUnitFloat(h)}`);
|
|
250
|
+
}
|
|
251
|
+
if (directives.format !== undefined) {
|
|
252
|
+
parts.push(`f=${directives.format}`);
|
|
253
|
+
}
|
|
254
|
+
if (directives.quality !== undefined) {
|
|
255
|
+
parts.push(`q=${directives.quality}`);
|
|
256
|
+
}
|
|
257
|
+
if (directives.width !== undefined) {
|
|
258
|
+
parts.push(`w=${directives.width}`);
|
|
259
|
+
}
|
|
260
|
+
// A validly-parsed ResizeDirectives always has at least one field set
|
|
261
|
+
// (parseDirectivesString rejects an empty directive string), but a
|
|
262
|
+
// programmatically-constructed one (e.g. from asset-url.ts's merge logic)
|
|
263
|
+
// could end up all-undefined - fall back to identity rather than emit an
|
|
264
|
+
// empty directives segment.
|
|
265
|
+
return parts.length > 0 ? parts.join(',') : IDENTITY_TRANSFORM_DIRECTIVE;
|
|
266
|
+
}
|
|
267
|
+
//# sourceMappingURL=transform-directives.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transform-directives.js","sourceRoot":"","sources":["../../src/assets/transform-directives.ts"],"names":[],"mappings":"AAAA;;;;;;;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AC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,4BAA4B,CAAA;AAC1E,CAAC"}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The shared transform engine: applies a parsed `TransformDirectives` to
|
|
3
|
+
* source image bytes with sharp. Server-only - never import this from
|
|
4
|
+
* client/editor code (this is why it lives in its own file, separate from
|
|
5
|
+
* the dependency-free transform-directives.ts). Used by the dev-mode lazy
|
|
6
|
+
* `/assets/t/*` emulation in api/assets.ts today, and will be reused
|
|
7
|
+
* unchanged by the prod transform Lambda (PR 7).
|
|
8
|
+
*
|
|
9
|
+
* Pipeline: a cheap metadata-only probe (`limitInputPixels: MAX_INPUT_PIXELS`,
|
|
10
|
+
* decompression-bomb defense #1) learns the real page count, so animated
|
|
11
|
+
* GIF/WebP frames can be capped at `MAX_ANIMATED_FRAMES` (decompression-bomb
|
|
12
|
+
* defense #2) without exceeding it and making sharp throw -> load for real
|
|
13
|
+
* with `{ pages: min(totalPages, MAX_ANIMATED_FRAMES), limitInputPixels }` ->
|
|
14
|
+
* `.rotate()` with no args (bakes EXIF orientation into pixels,
|
|
15
|
+
* dropping the orientation tag) -> optional crop via `.extract()` -> optional
|
|
16
|
+
* `.resize({ width, withoutEnlargement: true })` (never upscales) -> encode.
|
|
17
|
+
*
|
|
18
|
+
* Identity (`orig`) and any request that omits an explicit `f=` format
|
|
19
|
+
* re-encode through the SOURCE format rather than a fixed one: this is what
|
|
20
|
+
* guarantees EXIF/GPS gets stripped even when no other change is requested
|
|
21
|
+
* (sharp strips metadata by default on re-encode - `withMetadata()` is never
|
|
22
|
+
* called here, which would undo that).
|
|
23
|
+
*
|
|
24
|
+
* GIF has no EXIF segment (EXIF is a JPEG/TIFF APP1 marker; GIF has no such
|
|
25
|
+
* extension block), so an identity GIF has nothing to strip - it is still
|
|
26
|
+
* re-encoded through sharp's `.gif()` (verified: current sharp bundles cgif
|
|
27
|
+
* and re-encodes multi-frame GIFs through the `{ animated: true }` input
|
|
28
|
+
* path with no extra native dependency) purely for pipeline uniformity, not
|
|
29
|
+
* because GIF needs stripping.
|
|
30
|
+
*/
|
|
31
|
+
import { type TransformDirectives } from './transform-directives';
|
|
32
|
+
export interface ApplyTransformInput {
|
|
33
|
+
data: Uint8Array;
|
|
34
|
+
/** Source extension (lowercased file-type ext, e.g. 'png' | 'jpg' | 'webp' | 'gif'). */
|
|
35
|
+
ext: string;
|
|
36
|
+
}
|
|
37
|
+
export interface TransformSuccess {
|
|
38
|
+
ok: true;
|
|
39
|
+
data: Uint8Array;
|
|
40
|
+
contentType: string;
|
|
41
|
+
ext: string;
|
|
42
|
+
}
|
|
43
|
+
export interface TransformRejection {
|
|
44
|
+
ok: false;
|
|
45
|
+
/** 400: unsupported input; 413: output too large; 422: sharp failed to process the input. */
|
|
46
|
+
status: 400 | 413 | 422;
|
|
47
|
+
error: string;
|
|
48
|
+
}
|
|
49
|
+
export type TransformResult = TransformSuccess | TransformRejection;
|
|
50
|
+
export declare function applyTransform(input: ApplyTransformInput, directives: TransformDirectives): Promise<TransformResult>;
|
|
51
|
+
//# sourceMappingURL=transform.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transform.d.ts","sourceRoot":"","sources":["../../src/assets/transform.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AAKH,OAAO,EAIL,KAAK,mBAAmB,EACzB,MAAM,wBAAwB,CAAA;AAyC/B,MAAM,WAAW,mBAAmB;IAClC,IAAI,EAAE,UAAU,CAAA;IAChB,wFAAwF;IACxF,GAAG,EAAE,MAAM,CAAA;CACZ;AAED,MAAM,WAAW,gBAAgB;IAC/B,EAAE,EAAE,IAAI,CAAA;IACR,IAAI,EAAE,UAAU,CAAA;IAChB,WAAW,EAAE,MAAM,CAAA;IACnB,GAAG,EAAE,MAAM,CAAA;CACZ;AAED,MAAM,WAAW,kBAAkB;IACjC,EAAE,EAAE,KAAK,CAAA;IACT,6FAA6F;IAC7F,MAAM,EAAE,GAAG,GAAG,GAAG,GAAG,GAAG,CAAA;IACvB,KAAK,EAAE,MAAM,CAAA;CACd;AAED,MAAM,MAAM,eAAe,GAAG,gBAAgB,GAAG,kBAAkB,CAAA;AAgFnE,wBAAsB,cAAc,CAClC,KAAK,EAAE,mBAAmB,EAC1B,UAAU,EAAE,mBAAmB,GAC9B,OAAO,CAAC,eAAe,CAAC,CAwE1B"}
|