@softistx/image 0.0.0-stage → 0.6.2

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.
Files changed (74) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +551 -2
  3. package/dist/chunks/index-b71y3faz.js +20 -0
  4. package/dist/chunks/index-b71y3faz.js.map +10 -0
  5. package/dist/chunks/index-pj79f6q4.js +732 -0
  6. package/dist/chunks/index-pj79f6q4.js.map +23 -0
  7. package/dist/chunks/index-wfsrqqk2.js +16 -0
  8. package/dist/chunks/index-wfsrqqk2.js.map +10 -0
  9. package/dist/crop/crop-png.d.ts +39 -0
  10. package/dist/crop/crop-png.d.ts.map +1 -0
  11. package/dist/crop/from-query.d.ts +11 -0
  12. package/dist/crop/from-query.d.ts.map +1 -0
  13. package/dist/crop/png-chunks.d.ts +29 -0
  14. package/dist/crop/png-chunks.d.ts.map +1 -0
  15. package/dist/crop/png.d.ts +24 -0
  16. package/dist/crop/png.d.ts.map +1 -0
  17. package/dist/crop/row-assembler.d.ts +17 -0
  18. package/dist/crop/row-assembler.d.ts.map +1 -0
  19. package/dist/crop/unfilter.d.ts +10 -0
  20. package/dist/crop/unfilter.d.ts.map +1 -0
  21. package/dist/errors/image-error.d.ts +49 -0
  22. package/dist/errors/image-error.d.ts.map +1 -0
  23. package/dist/index.d.ts +10 -0
  24. package/dist/index.d.ts.map +1 -0
  25. package/dist/index.js +42 -0
  26. package/dist/index.js.map +11 -0
  27. package/dist/input/read-input.d.ts +10 -0
  28. package/dist/input/read-input.d.ts.map +1 -0
  29. package/dist/input/sniff.d.ts +9 -0
  30. package/dist/input/sniff.d.ts.map +1 -0
  31. package/dist/pdf/index.d.ts +3 -0
  32. package/dist/pdf/index.d.ts.map +1 -0
  33. package/dist/pdf/index.js +166 -0
  34. package/dist/pdf/index.js.map +10 -0
  35. package/dist/pdf/render-pdf-page.d.ts +56 -0
  36. package/dist/pdf/render-pdf-page.d.ts.map +1 -0
  37. package/dist/process/bun-errors.d.ts +8 -0
  38. package/dist/process/bun-errors.d.ts.map +1 -0
  39. package/dist/process/geometry.d.ts +49 -0
  40. package/dist/process/geometry.d.ts.map +1 -0
  41. package/dist/process/process.d.ts +23 -0
  42. package/dist/process/process.d.ts.map +1 -0
  43. package/dist/render/render-image.d.ts +61 -0
  44. package/dist/render/render-image.d.ts.map +1 -0
  45. package/dist/s3/bind-image-store.d.ts +27 -0
  46. package/dist/s3/bind-image-store.d.ts.map +1 -0
  47. package/dist/s3/index.d.ts +4 -0
  48. package/dist/s3/index.d.ts.map +1 -0
  49. package/dist/s3/index.js +73 -0
  50. package/dist/s3/index.js.map +11 -0
  51. package/dist/s3/types.d.ts +61 -0
  52. package/dist/s3/types.d.ts.map +1 -0
  53. package/dist/s3/upload-image.d.ts +23 -0
  54. package/dist/s3/upload-image.d.ts.map +1 -0
  55. package/dist/set/define-image-set.d.ts +25 -0
  56. package/dist/set/define-image-set.d.ts.map +1 -0
  57. package/dist/set/limit.d.ts +7 -0
  58. package/dist/set/limit.d.ts.map +1 -0
  59. package/dist/set/options.d.ts +104 -0
  60. package/dist/set/options.d.ts.map +1 -0
  61. package/dist/set/runtime.d.ts +7 -0
  62. package/dist/set/runtime.d.ts.map +1 -0
  63. package/dist/set/types.d.ts +60 -0
  64. package/dist/set/types.d.ts.map +1 -0
  65. package/dist/set/whole.d.ts +3 -0
  66. package/dist/set/whole.d.ts.map +1 -0
  67. package/docs/README.md +13 -0
  68. package/docs/guide/pdf.md +60 -0
  69. package/docs/guide/processing.md +313 -0
  70. package/docs/guide/rendering.md +88 -0
  71. package/docs/guide/storing.md +219 -0
  72. package/docs/roadmap.md +71 -0
  73. package/docs/troubleshooting.md +724 -0
  74. package/package.json +90 -5
@@ -0,0 +1,166 @@
1
+ import {
2
+ readInput,
3
+ isWhole,
4
+ createLimit,
5
+ RENDER_CONTENT_TYPES
6
+ } from "../chunks/index-pj79f6q4.js";
7
+ import {
8
+ ImageError2
9
+ } from "../chunks/index-wfsrqqk2.js";
10
+
11
+ // src/pdf/render-pdf-page.ts
12
+ var DEFAULT_MAX_BYTES = 5 * 1024 * 1024;
13
+ var DEFAULT_MAX_PIXELS = 25000000;
14
+ var DEFAULT_QUALITY = 80;
15
+ var MAX_SIDE = 8192;
16
+ var PDF_HEADER = "%PDF-";
17
+ var limit = createLimit(2);
18
+ function whole(name, value, min, max) {
19
+ if (!isWhole(value, min, max)) {
20
+ const range = max === undefined ? `${min} or more` : `${min} to ${max}`;
21
+ throw new TypeError(`${name} must be a whole number of ${range}, got ${String(value)}`);
22
+ }
23
+ return value;
24
+ }
25
+ function checkOptions(options) {
26
+ const { format } = options;
27
+ if (format !== "jpeg" && format !== "webp" && format !== "png") {
28
+ throw new TypeError(`format must be 'jpeg', 'webp' or 'png', got ${String(format)}`);
29
+ }
30
+ if (format === "png" && options.quality !== undefined) {
31
+ throw new TypeError("quality applies to JPEG and WebP only: a PNG is always lossless");
32
+ }
33
+ return {
34
+ width: whole("width", options.width, 1, MAX_SIDE),
35
+ height: options.height === undefined ? undefined : whole("height", options.height, 1, MAX_SIDE),
36
+ quality: whole("quality", options.quality ?? DEFAULT_QUALITY, 1, 100),
37
+ maxBytes: whole("maxBytes", options.maxBytes ?? DEFAULT_MAX_BYTES, 1),
38
+ maxPixels: whole("maxPixels", options.maxPixels ?? DEFAULT_MAX_PIXELS, 1)
39
+ };
40
+ }
41
+ function looksLikePdf(bytes) {
42
+ const head = bytes.subarray(0, 1024);
43
+ const needle = PDF_HEADER.split("").map((c) => c.charCodeAt(0));
44
+ for (let i = 0;i + needle.length <= head.length; i++) {
45
+ if (needle.every((value, j) => head[i + j] === value))
46
+ return true;
47
+ }
48
+ return false;
49
+ }
50
+ var peers;
51
+ async function loadPeers() {
52
+ try {
53
+ const [pdfjs, canvas] = await Promise.all([
54
+ import("pdfjs-dist/legacy/build/pdf.mjs"),
55
+ import("@napi-rs/canvas")
56
+ ]);
57
+ const pkg = import.meta.resolve("pdfjs-dist/package.json");
58
+ const assets = pkg.slice(0, pkg.lastIndexOf("/") + 1);
59
+ return { pdfjs, createCanvas: canvas.createCanvas, assets };
60
+ } catch (cause) {
61
+ peers = undefined;
62
+ const code = cause?.code;
63
+ if (code !== "ERR_MODULE_NOT_FOUND" && code !== "MODULE_NOT_FOUND") {
64
+ const text = String(cause?.message ?? "");
65
+ if (!/Cannot find (module|package)/i.test(text))
66
+ throw cause;
67
+ }
68
+ throw new Error("@softistx/image/pdf needs the optional peers pdfjs-dist and @napi-rs/canvas: run `bun add pdfjs-dist @napi-rs/canvas`", { cause });
69
+ }
70
+ }
71
+ async function encode(png, format, quality) {
72
+ if (format === "png")
73
+ return png;
74
+ const image = new Bun.Image(png);
75
+ return format === "jpeg" ? image.jpeg({ quality }).bytes() : image.webp({ quality }).bytes();
76
+ }
77
+ function undecodable(cause, why) {
78
+ const error = new ImageError2("UNDECODABLE", "decode", `the PDF cannot be rendered: ${why}`);
79
+ error.cause = cause;
80
+ return error;
81
+ }
82
+ async function renderPdfFirstPage(input, options) {
83
+ const { width, height, quality, maxBytes, maxPixels } = checkOptions(options);
84
+ const { signal } = options;
85
+ signal?.throwIfAborted();
86
+ const bytes = await readInput(input, maxBytes);
87
+ signal?.throwIfAborted();
88
+ if (!looksLikePdf(bytes)) {
89
+ throw new ImageError2("UNSUPPORTED_TYPE", "format", "the upload is not a PDF");
90
+ }
91
+ return limit(async () => {
92
+ signal?.throwIfAborted();
93
+ peers ??= loadPeers();
94
+ const { pdfjs, createCanvas, assets } = await peers;
95
+ const task = pdfjs.getDocument({
96
+ data: bytes.slice(),
97
+ useSystemFonts: false,
98
+ verbosity: 0,
99
+ standardFontDataUrl: `${assets}standard_fonts/`,
100
+ wasmUrl: `${assets}wasm/`,
101
+ cMapUrl: `${assets}cmaps/`,
102
+ iccUrl: `${assets}iccs/`
103
+ });
104
+ try {
105
+ let doc;
106
+ try {
107
+ doc = await task.promise;
108
+ } catch (cause) {
109
+ throw undecodable(cause, "it is damaged or encrypted");
110
+ }
111
+ signal?.throwIfAborted();
112
+ if (doc.numPages < 1)
113
+ throw undecodable(undefined, "it has no page");
114
+ const page = await doc.getPage(1);
115
+ const base = page.getViewport({ scale: 1 });
116
+ if (!(base.width > 0 && base.height > 0)) {
117
+ throw undecodable(undefined, "the first page has no size");
118
+ }
119
+ let scale = width / base.width;
120
+ if (height !== undefined)
121
+ scale = Math.min(scale, height / base.height);
122
+ const outWidth = Math.max(1, Math.round(base.width * scale));
123
+ const outHeight = Math.max(1, Math.round(base.height * scale));
124
+ if (outWidth > MAX_SIDE || outHeight > MAX_SIDE || outWidth * outHeight > maxPixels) {
125
+ throw new ImageError2("TOO_LARGE", "pixels", `the page would render to ${outWidth}x${outHeight} pixels, over the limit`);
126
+ }
127
+ const viewport = page.getViewport({ scale });
128
+ const canvas = createCanvas(outWidth, outHeight);
129
+ try {
130
+ await page.render({
131
+ canvas,
132
+ canvasContext: canvas.getContext("2d"),
133
+ viewport,
134
+ background: "#ffffff"
135
+ }).promise;
136
+ } catch (cause) {
137
+ throw undecodable(cause, "the first page is damaged");
138
+ }
139
+ signal?.throwIfAborted();
140
+ const out = await encode(canvas.toBuffer("image/png"), options.format, quality);
141
+ signal?.throwIfAborted();
142
+ return {
143
+ source: {
144
+ format: "pdf",
145
+ pages: doc.numPages,
146
+ width: base.width,
147
+ height: base.height
148
+ },
149
+ bytes: out,
150
+ width: outWidth,
151
+ height: outHeight,
152
+ contentType: RENDER_CONTENT_TYPES[options.format]
153
+ };
154
+ } finally {
155
+ await task.destroy().catch(() => {
156
+ return;
157
+ });
158
+ }
159
+ });
160
+ }
161
+ export {
162
+ renderPdfFirstPage
163
+ };
164
+
165
+ //# debugId=4F5EA0B1FC4561EE64756E2164756E21
166
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,10 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/pdf/render-pdf-page.ts"],
4
+ "sourcesContent": [
5
+ "import { ImageError } from '../errors/image-error';\nimport { type ImageInput, readInput } from '../input/read-input';\nimport {\n\tRENDER_CONTENT_TYPES,\n\ttype RenderFormat,\n} from '../render/render-image';\nimport { createLimit } from '../set/limit';\nimport { isWhole } from '../set/whole';\n\n/** What `renderPdfFirstPage` accepts: the same inputs as `renderImage`. */\nexport type PdfInput = ImageInput;\n\nexport type PdfRenderOptions<F extends RenderFormat = RenderFormat> = {\n\t/** The output width, 1 to 8192. A PDF is vector: the page is drawn at this width, never stretched. */\n\twidth: number;\n\t/**\n\t * The most height the output may have, 1 to 8192. Default: the height the\n\t * page's ratio gives. A page taller than `width` x `height` allows is\n\t * scaled down to fit, so the output can be narrower than `width`.\n\t */\n\theight?: number;\n\tformat: F;\n\t/** JPEG or WebP quality, 1 to 100. Default 80. Refused with `png`. */\n\tquality?: number;\n\t/** The PDF's byte limit. Default 5 MiB. */\n\tmaxBytes?: number;\n\t/** The most pixels the output may have. Default 25 000 000. */\n\tmaxPixels?: number;\n\t/** Aborts the call: it rejects with the signal's reason. */\n\tsignal?: AbortSignal;\n};\n\nexport type PdfRenderedImage<F extends RenderFormat = RenderFormat> = {\n\t/** The PDF as read: its page count and the first page's size in points (1/72 inch), rotation applied. */\n\tsource: { format: 'pdf'; pages: number; width: number; height: number };\n\tbytes: Uint8Array;\n\twidth: number;\n\theight: number;\n\tcontentType: (typeof RENDER_CONTENT_TYPES)[F];\n};\n\nconst DEFAULT_MAX_BYTES = 5 * 1024 * 1024;\nconst DEFAULT_MAX_PIXELS = 25_000_000;\nconst DEFAULT_QUALITY = 80;\nconst MAX_SIDE = 8192;\nconst PDF_HEADER = '%PDF-';\n\n/** Renders decode two at a time, like `renderImage`. */\nconst limit = createLimit(2);\n\nfunction whole(name: string, value: unknown, min: number, max?: number) {\n\tif (!isWhole(value, min, max)) {\n\t\tconst range = max === undefined ? `${min} or more` : `${min} to ${max}`;\n\t\tthrow new TypeError(\n\t\t\t`${name} must be a whole number of ${range}, got ${String(value)}`,\n\t\t);\n\t}\n\treturn value;\n}\n\nfunction checkOptions(options: PdfRenderOptions) {\n\tconst { format } = options;\n\tif (format !== 'jpeg' && format !== 'webp' && format !== 'png') {\n\t\tthrow new TypeError(\n\t\t\t`format must be 'jpeg', 'webp' or 'png', got ${String(format)}`,\n\t\t);\n\t}\n\tif (format === 'png' && options.quality !== undefined) {\n\t\tthrow new TypeError(\n\t\t\t'quality applies to JPEG and WebP only: a PNG is always lossless',\n\t\t);\n\t}\n\treturn {\n\t\twidth: whole('width', options.width, 1, MAX_SIDE),\n\t\theight:\n\t\t\toptions.height === undefined\n\t\t\t\t? undefined\n\t\t\t\t: whole('height', options.height, 1, MAX_SIDE),\n\t\tquality: whole('quality', options.quality ?? DEFAULT_QUALITY, 1, 100),\n\t\tmaxBytes: whole('maxBytes', options.maxBytes ?? DEFAULT_MAX_BYTES, 1),\n\t\tmaxPixels: whole('maxPixels', options.maxPixels ?? DEFAULT_MAX_PIXELS, 1),\n\t};\n}\n\n/** A PDF starts with `%PDF-` within its first 1024 bytes (the spec's own tolerance). */\nfunction looksLikePdf(bytes: Uint8Array): boolean {\n\tconst head = bytes.subarray(0, 1024);\n\tconst needle = PDF_HEADER.split('').map((c) => c.charCodeAt(0));\n\tfor (let i = 0; i + needle.length <= head.length; i++) {\n\t\tif (needle.every((value, j) => head[i + j] === value)) return true;\n\t}\n\treturn false;\n}\n\ntype Peers = {\n\tpdfjs: typeof import('pdfjs-dist/legacy/build/pdf.mjs');\n\tcreateCanvas: typeof import('@napi-rs/canvas').createCanvas;\n\tassets: string;\n};\n\nlet peers: Promise<Peers> | undefined;\n\nasync function loadPeers(): Promise<Peers> {\n\ttry {\n\t\tconst [pdfjs, canvas] = await Promise.all([\n\t\t\timport('pdfjs-dist/legacy/build/pdf.mjs'),\n\t\t\timport('@napi-rs/canvas'),\n\t\t]);\n\t\tconst pkg = import.meta.resolve('pdfjs-dist/package.json');\n\t\tconst assets = pkg.slice(0, pkg.lastIndexOf('/') + 1);\n\t\treturn { pdfjs, createCanvas: canvas.createCanvas, assets };\n\t} catch (cause) {\n\t\tpeers = undefined;\n\t\tconst code = (cause as { code?: string } | null)?.code;\n\t\tif (code !== 'ERR_MODULE_NOT_FOUND' && code !== 'MODULE_NOT_FOUND') {\n\t\t\tconst text = String((cause as Error | null)?.message ?? '');\n\t\t\tif (!/Cannot find (module|package)/i.test(text)) throw cause;\n\t\t}\n\t\tthrow new Error(\n\t\t\t'@softistx/image/pdf needs the optional peers pdfjs-dist and @napi-rs/canvas: run `bun add pdfjs-dist @napi-rs/canvas`',\n\t\t\t{ cause },\n\t\t);\n\t}\n}\n\n/** The bytes as PNG by the canvas, or as JPEG/WebP through `Bun.Image`. */\nasync function encode(\n\tpng: Uint8Array,\n\tformat: RenderFormat,\n\tquality: number,\n): Promise<Uint8Array> {\n\tif (format === 'png') return png;\n\tconst image = new Bun.Image(png);\n\treturn format === 'jpeg'\n\t\t? image.jpeg({ quality }).bytes()\n\t\t: image.webp({ quality }).bytes();\n}\n\nfunction undecodable(cause: unknown, why: string): ImageError {\n\tconst error = new ImageError(\n\t\t'UNDECODABLE',\n\t\t'decode',\n\t\t`the PDF cannot be rendered: ${why}`,\n\t);\n\terror.cause = cause;\n\treturn error;\n}\n\n/**\n * The first page of a PDF as one picture: checked by its `%PDF-` header,\n * drawn on a white background at `width` (the height follows the page's\n * ratio, or `height` caps it), and encoded as JPEG, WebP or PNG through\n * `Bun.Image`. Same input types, limits and `ImageError` refusals as\n * `renderImage`; `UNSUPPORTED_TYPE` for bytes that are not a PDF,\n * `UNDECODABLE` for a PDF that is damaged, encrypted or has no page.\n *\n * Needs the optional peers `pdfjs-dist` and `@napi-rs/canvas`. No system\n * binary is used.\n *\n * ```ts\n * const { bytes, contentType } = await renderPdfFirstPage(blob, {\n * width: 256,\n * format: 'webp',\n * });\n * ```\n */\nexport async function renderPdfFirstPage<F extends RenderFormat>(\n\tinput: PdfInput,\n\toptions: PdfRenderOptions<F>,\n): Promise<PdfRenderedImage<F>> {\n\tconst { width, height, quality, maxBytes, maxPixels } = checkOptions(options);\n\tconst { signal } = options;\n\tsignal?.throwIfAborted();\n\tconst bytes = await readInput(input, maxBytes);\n\tsignal?.throwIfAborted();\n\tif (!looksLikePdf(bytes)) {\n\t\tthrow new ImageError(\n\t\t\t'UNSUPPORTED_TYPE',\n\t\t\t'format',\n\t\t\t'the upload is not a PDF',\n\t\t);\n\t}\n\treturn limit(async () => {\n\t\tsignal?.throwIfAborted();\n\t\tpeers ??= loadPeers();\n\t\tconst { pdfjs, createCanvas, assets } = await peers;\n\t\t// pdf.js may transfer the buffer: give it a copy, the caller keeps theirs.\n\t\tconst task = pdfjs.getDocument({\n\t\t\tdata: bytes.slice(),\n\t\t\tuseSystemFonts: false,\n\t\t\tverbosity: 0,\n\t\t\tstandardFontDataUrl: `${assets}standard_fonts/`,\n\t\t\twasmUrl: `${assets}wasm/`,\n\t\t\tcMapUrl: `${assets}cmaps/`,\n\t\t\ticcUrl: `${assets}iccs/`,\n\t\t});\n\t\ttry {\n\t\t\tlet doc: Awaited<typeof task.promise>;\n\t\t\ttry {\n\t\t\t\tdoc = await task.promise;\n\t\t\t} catch (cause) {\n\t\t\t\tthrow undecodable(cause, 'it is damaged or encrypted');\n\t\t\t}\n\t\t\tsignal?.throwIfAborted();\n\t\t\tif (doc.numPages < 1) throw undecodable(undefined, 'it has no page');\n\t\t\tconst page = await doc.getPage(1);\n\t\t\tconst base = page.getViewport({ scale: 1 });\n\t\t\tif (!(base.width > 0 && base.height > 0)) {\n\t\t\t\tthrow undecodable(undefined, 'the first page has no size');\n\t\t\t}\n\t\t\tlet scale = width / base.width;\n\t\t\tif (height !== undefined) scale = Math.min(scale, height / base.height);\n\t\t\tconst outWidth = Math.max(1, Math.round(base.width * scale));\n\t\t\tconst outHeight = Math.max(1, Math.round(base.height * scale));\n\t\t\tif (\n\t\t\t\toutWidth > MAX_SIDE ||\n\t\t\t\toutHeight > MAX_SIDE ||\n\t\t\t\toutWidth * outHeight > maxPixels\n\t\t\t) {\n\t\t\t\tthrow new ImageError(\n\t\t\t\t\t'TOO_LARGE',\n\t\t\t\t\t'pixels',\n\t\t\t\t\t`the page would render to ${outWidth}x${outHeight} pixels, over the limit`,\n\t\t\t\t);\n\t\t\t}\n\t\t\tconst viewport = page.getViewport({ scale });\n\t\t\tconst canvas = createCanvas(outWidth, outHeight);\n\t\t\ttry {\n\t\t\t\tawait page.render({\n\t\t\t\t\t// pdf.js types ask for DOM canvases; @napi-rs/canvas is the node-side twin.\n\t\t\t\t\tcanvas: canvas as never,\n\t\t\t\t\tcanvasContext: canvas.getContext('2d') as never,\n\t\t\t\t\tviewport,\n\t\t\t\t\tbackground: '#ffffff',\n\t\t\t\t}).promise;\n\t\t\t} catch (cause) {\n\t\t\t\tthrow undecodable(cause, 'the first page is damaged');\n\t\t\t}\n\t\t\tsignal?.throwIfAborted();\n\t\t\tconst out = await encode(\n\t\t\t\tcanvas.toBuffer('image/png'),\n\t\t\t\toptions.format,\n\t\t\t\tquality,\n\t\t\t);\n\t\t\tsignal?.throwIfAborted();\n\t\t\treturn {\n\t\t\t\tsource: {\n\t\t\t\t\tformat: 'pdf' as const,\n\t\t\t\t\tpages: doc.numPages,\n\t\t\t\t\twidth: base.width,\n\t\t\t\t\theight: base.height,\n\t\t\t\t},\n\t\t\t\tbytes: out,\n\t\t\t\twidth: outWidth,\n\t\t\t\theight: outHeight,\n\t\t\t\tcontentType: RENDER_CONTENT_TYPES[options.format],\n\t\t\t};\n\t\t} finally {\n\t\t\tawait task.destroy().catch(() => undefined);\n\t\t}\n\t});\n}\n"
6
+ ],
7
+ "mappings": ";;;;;;;;;;;AAyCA,IAAM,oBAAoB,IAAI,OAAO;AACrC,IAAM,qBAAqB;AAC3B,IAAM,kBAAkB;AACxB,IAAM,WAAW;AACjB,IAAM,aAAa;AAGnB,IAAM,QAAQ,YAAY,CAAC;AAE3B,SAAS,KAAK,CAAC,MAAc,OAAgB,KAAa,KAAc;AAAA,EACvE,IAAI,CAAC,QAAQ,OAAO,KAAK,GAAG,GAAG;AAAA,IAC9B,MAAM,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Y,kCACzC;AAAA,MACD;AAAA,MACA,MAAM,WAAW,KAAK,YAAY,EAAE,MAAM,CAAC;AAAA,MAC3C,MAAM,SAAS,aAAa,UAAU,SAAS;AAAA,MAC/C,IAAI;AAAA,QACH,MAAM,KAAK,OAAO;AAAA,UAEjB;AAAA,UACA,eAAe,OAAO,WAAW,IAAI;AAAA,UACrC;AAAA,UACA,YAAY;AAAA,QACb,CAAC,EAAE;AAAA,QACF,OAAO,OAAO;AAAA,QACf,MAAM,YAAY,OAAO,2BAA2B;AAAA;AAAA,MAErD,QAAQ,eAAe;AAAA,MACvB,MAAM,MAAM,MAAM,OACjB,OAAO,SAAS,WAAW,GAC3B,QAAQ,QACR,OACD;AAAA,MACA,QAAQ,eAAe;AAAA,MACvB,OAAO;AAAA,QACN,QAAQ;AAAA,UACP,QAAQ;AAAA,UACR,OAAO,IAAI;AAAA,UACX,OAAO,KAAK;AAAA,UACZ,QAAQ,KAAK;AAAA,QACd;AAAA,QACA,OAAO;AAAA,QACP,OAAO;AAAA,QACP,QAAQ;AAAA,QACR,aAAa,qBAAqB,QAAQ;AAAA,MAC3C;AAAA,MACC,QAAQ;AAAA,MACT,MAAM,KAAK,QAAQ,EAAE,MAAM,MAAG;AAAA,QAAG;AAAA,OAAS;AAAA;AAAA,GAE3C;AAAA;",
8
+ "debugId": "4F5EA0B1FC4561EE64756E2164756E21",
9
+ "names": []
10
+ }
@@ -0,0 +1,56 @@
1
+ import { type ImageInput } from '../input/read-input.js';
2
+ import { RENDER_CONTENT_TYPES, type RenderFormat } from '../render/render-image.js';
3
+ /** What `renderPdfFirstPage` accepts: the same inputs as `renderImage`. */
4
+ export type PdfInput = ImageInput;
5
+ export type PdfRenderOptions<F extends RenderFormat = RenderFormat> = {
6
+ /** The output width, 1 to 8192. A PDF is vector: the page is drawn at this width, never stretched. */
7
+ width: number;
8
+ /**
9
+ * The most height the output may have, 1 to 8192. Default: the height the
10
+ * page's ratio gives. A page taller than `width` x `height` allows is
11
+ * scaled down to fit, so the output can be narrower than `width`.
12
+ */
13
+ height?: number;
14
+ format: F;
15
+ /** JPEG or WebP quality, 1 to 100. Default 80. Refused with `png`. */
16
+ quality?: number;
17
+ /** The PDF's byte limit. Default 5 MiB. */
18
+ maxBytes?: number;
19
+ /** The most pixels the output may have. Default 25 000 000. */
20
+ maxPixels?: number;
21
+ /** Aborts the call: it rejects with the signal's reason. */
22
+ signal?: AbortSignal;
23
+ };
24
+ export type PdfRenderedImage<F extends RenderFormat = RenderFormat> = {
25
+ /** The PDF as read: its page count and the first page's size in points (1/72 inch), rotation applied. */
26
+ source: {
27
+ format: 'pdf';
28
+ pages: number;
29
+ width: number;
30
+ height: number;
31
+ };
32
+ bytes: Uint8Array;
33
+ width: number;
34
+ height: number;
35
+ contentType: (typeof RENDER_CONTENT_TYPES)[F];
36
+ };
37
+ /**
38
+ * The first page of a PDF as one picture: checked by its `%PDF-` header,
39
+ * drawn on a white background at `width` (the height follows the page's
40
+ * ratio, or `height` caps it), and encoded as JPEG, WebP or PNG through
41
+ * `Bun.Image`. Same input types, limits and `ImageError` refusals as
42
+ * `renderImage`; `UNSUPPORTED_TYPE` for bytes that are not a PDF,
43
+ * `UNDECODABLE` for a PDF that is damaged, encrypted or has no page.
44
+ *
45
+ * Needs the optional peers `pdfjs-dist` and `@napi-rs/canvas`. No system
46
+ * binary is used.
47
+ *
48
+ * ```ts
49
+ * const { bytes, contentType } = await renderPdfFirstPage(blob, {
50
+ * width: 256,
51
+ * format: 'webp',
52
+ * });
53
+ * ```
54
+ */
55
+ export declare function renderPdfFirstPage<F extends RenderFormat>(input: PdfInput, options: PdfRenderOptions<F>): Promise<PdfRenderedImage<F>>;
56
+ //# sourceMappingURL=render-pdf-page.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"render-pdf-page.d.ts","sourceRoot":"","sources":["../../src/pdf/render-pdf-page.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,UAAU,EAAa,MAAM,qBAAqB,CAAC;AACjE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bun.Image` rejection, as this package's
4
+ * `ImageError`; any other error comes back untouched, so a bug or an
5
+ * out-of-memory is never taken for a bad upload. Bun's error is the `cause`.
6
+ */
7
+ export declare function mapBunError(error: unknown, format: SniffedFormat): unknown;
8
+ //# sourceMappingURL=bun-errors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bun-errors.d.ts","sourceRoot":"","sources":["../../src/process/bun-errors.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAapD;;;;GAIG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,aAAa,GAAG,OAAO,CAmC1E"}
@@ -0,0 +1,49 @@
1
+ import type { CropBox } from '../set/types.js';
2
+ /** The size of an image, in pixels. */
3
+ export type Dimensions = {
4
+ width: number;
5
+ height: number;
6
+ };
7
+ /**
8
+ * How much to shrink before cropping: enough that the crop keeps twice the
9
+ * largest variant (so the last resize still has pixels to average), never
10
+ * more than 1, since a source is never enlarged.
11
+ */
12
+ export declare function shrinkScale(cropSide: number, largestSize: number): number;
13
+ /** The size the shrunk image has: at least one pixel on each side. */
14
+ export declare function scaledDimensions({ width, height }: Dimensions, scale: number): Dimensions;
15
+ /** The square of side `min(width, height)` in the middle of the image. */
16
+ export declare function centredCrop({ width, height }: Dimensions): CropBox;
17
+ /**
18
+ * Refuses a crop that is not three whole non-negative numbers, as an
19
+ * `ImageError` (`INVALID_CROP`): the crop can come from a request.
20
+ */
21
+ export declare function assertCropShape(crop: CropBox): void;
22
+ /** Refuses a crop that is not inside the (oriented) image (`INVALID_CROP`). */
23
+ export declare function assertCropFits(crop: CropBox, image: Dimensions): void;
24
+ /**
25
+ * The crop in the shrunk image's pixels: each number rounded, then clamped so
26
+ * the square stays inside it (rounding can overshoot an edge by a pixel).
27
+ */
28
+ export declare function scaleCrop(crop: CropBox, scale: number, shrunk: Dimensions): CropBox;
29
+ /** A rectangle to keep: `(x, y)` is its top-left corner, in pixels. */
30
+ export type Rect = {
31
+ x: number;
32
+ y: number;
33
+ width: number;
34
+ height: number;
35
+ };
36
+ /**
37
+ * Refuses a rectangle that is not four whole numbers (`x`, `y` of 0 or more,
38
+ * `width`, `height` of 1 or more) or does not fit the (oriented) image, as an
39
+ * `ImageError` (`INVALID_CROP`): the crop can come from a request.
40
+ */
41
+ export declare function assertRectFits(crop: Rect, image: Dimensions): void;
42
+ /**
43
+ * How much to shrink before cropping a rectangle: enough that the crop keeps
44
+ * twice the target on both sides, never more than 1.
45
+ */
46
+ export declare function shrinkScaleFor(crop: Dimensions, target: Dimensions): number;
47
+ /** The rectangle in the shrunk image's pixels, rounded and clamped inside it. */
48
+ export declare function scaleRect(crop: Rect, scale: number, shrunk: Dimensions): Rect;
49
+ //# sourceMappingURL=geometry.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"geometry.d.ts","sourceRoot":"","sources":["../../src/process/geometry.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAG5C,uCAAuC;AACvC,MAAM,MAAM,UAAU,GAAG;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC;AAE3D;;;;GAIG;AACH,wBAAgB,WAAW,CAAC,QAAQ,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,GAAG,MAAM,CAEzE;AAED,sEAAsE;AACtE,wBAAgB,gBAAgB,CAC/B,EAAE,KAAK,EAAE,MAAM,EAAE,EAAE,UAAU,EAC7B,KAAK,EAAE,MAAM,GACX,UAAU,CAKZ;AAED,0EAA0E;AAC1E,wBAAgB,WAAW,CAAC,EAAE,KAAK,EAAE,MAAM,EAAE,EAAE,UAAU,GAAG,OAAO,CAOlE;AAED;;;GAGG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,OAAO,GAAG,IAAI,CAYnD;AAED,+EAA+E;AAC/E,wBAAgB,cAAc,CAAC,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,UAAU,GAAG,IAAI,CAYrE;AAED;;;GAGG;AACH,wBAAgB,SAAS,CACxB,IAAI,EAAE,OAAO,EACb,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,UAAU,GAChB,OAAO,CAYT;AAED,uEAAuE;AACvE,MAAM,MAAM,IAAI,GAAG;IAAE,CAAC,EAAE,MAAM,CAAC;IAAC,CAAC,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC;AAE3E;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,UAAU,GAAG,IAAI,CAyBlE;AAED;;;GAGG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,UAAU,EAAE,MAAM,EAAE,UAAU,GAAG,MAAM,CAQ3E;AAED,iFAAiF;AACjF,wBAAgB,SAAS,CAAC,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,UAAU,GAAG,IAAI,CAa7E"}
@@ -0,0 +1,23 @@
1
+ import { type ImageInput } from '../input/read-input.js';
2
+ import { type SniffedFormat } from '../input/sniff.js';
3
+ import { type ResolvedOptions } from '../set/options.js';
4
+ import type { ProcessedImage, ProcessOptions } from '../set/types.js';
5
+ import { type Dimensions } from './geometry.js';
6
+ /** Runs a Bun pipeline step, its refusals turned into `ImageError`. */
7
+ export declare function guarded<T>(format: SniffedFormat, step: () => Promise<T>): Promise<T>;
8
+ /** The oriented size: `metadata()` reports it after EXIF rotation (measured). */
9
+ export declare function readDimensions(bytes: Uint8Array, format: SniffedFormat, maxPixels: number): Promise<Dimensions>;
10
+ /**
11
+ * The image shrunk once, as a PNG at Bun's default level. That level gives
12
+ * the smallest PNG, and Bun 1.4.3 holds each `png()` output until a later
13
+ * collection, so the smallest PNG is the least memory held: a 36 MP photo
14
+ * gave a 45 MB PNG at the default level and 146 MB at level 0 (Bun 1.4.3,
15
+ * photo-like JPEG), and a process
16
+ * grew by about the PNG's size per call (measured). Level 0 is faster, but
17
+ * its bigger output is what stays. Even so, sequential calls with a small
18
+ * crop climb before the memory falls back (see `maxPixels`).
19
+ */
20
+ export declare function shrink(bytes: Uint8Array, format: SniffedFormat, image: Dimensions, scale: number, maxPixels: number): Promise<Uint8Array>;
21
+ /** The pipeline behind `ImageSet.process`; the steps are in docs/guide/processing.md. */
22
+ export declare function processImage(input: ImageInput, { crop }: ProcessOptions, options: ResolvedOptions, limit: <T>(task: () => Promise<T>) => Promise<T>): Promise<ProcessedImage>;
23
+ //# sourceMappingURL=process.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"process.d.ts","sourceRoot":"","sources":["../../src/process/process.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,KAAK,UAAU,EAAa,MAAM,qBAAqB,CAAC;AACjE,OAAO,EAAE,KAAK,aAAa,EAAe,MAAM,gBAAgB,CAAC;AACjE,OAAO,EAAiB,KAAK,eAAe,EAAE,MAAM,gBAAgB,CAAC;AACrE,OAAO,KAAK,EAGX,cAAc,EACd,cAAc,EACd,MAAM,cAAc,CAAC;AAEtB,OAAO,EAIN,KAAK,UAAU,EAIf,MAAM,YAAY,CAAC;AAEpB,uEAAuE;AACvE,wBAAsB,OAAO,CAAC,CAAC,EAC9B,MAAM,EAAE,aAAa,EACrB,IAAI,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GACpB,OAAO,CAAC,CAAC,CAAC,CAMZ;AAED,iFAAiF;AACjF,wBAAsB,cAAc,CACnC,KAAK,EAAE,UAAU,EACjB,MAAM,EAAE,aAAa,EACrB,SAAS,EAAE,MAAM,GACf,OAAO,CAAC,UAAU,CAAC,CAKrB;AAED;;;;;;;;;GASG;AACH,wBAAgB,MAAM,CACrB,KAAK,EAAE,UAAU,EACjB,MAAM,EAAE,aAAa,EACrB,KAAK,EAAE,UAAU,EACjB,KAAK,EAAE,MAAM,EACb,SAAS,EAAE,MAAM,GACf,OAAO,CAAC,UAAU,CAAC,CASrB;AAiDD,yFAAyF;AACzF,wBAAsB,YAAY,CACjC,KAAK,EAAE,UAAU,EACjB,EAAE,IAAI,EAAE,EAAE,cAAc,EACxB,OAAO,EAAE,eAAe,EACxB,KAAK,EAAE,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,GAC9C,OAAO,CAAC,cAAc,CAAC,CAmCzB"}
@@ -0,0 +1,61 @@
1
+ import { type ImageInput } from '../input/read-input.js';
2
+ import { type SniffedFormat } from '../input/sniff.js';
3
+ import { type Rect } from '../process/geometry.js';
4
+ /** The formats `renderImage` writes. */
5
+ export type RenderFormat = 'jpeg' | 'webp' | 'png';
6
+ /** The content type of each render format. */
7
+ export declare const RENDER_CONTENT_TYPES: Readonly<{
8
+ readonly jpeg: "image/jpeg";
9
+ readonly webp: "image/webp";
10
+ readonly png: "image/png";
11
+ }>;
12
+ /** A rectangle to keep, in pixels of the auto-oriented picture. */
13
+ export type CropRect = Rect;
14
+ export type RenderOptions<F extends RenderFormat = RenderFormat> = {
15
+ /** The rectangle to keep. Default: the whole (upright) picture. */
16
+ crop?: CropRect;
17
+ /** The output width, 1 to 8192. The crop is never enlarged to it. */
18
+ width: number;
19
+ /** The output height, 1 to 8192. The crop is never enlarged to it. */
20
+ height: number;
21
+ format: F;
22
+ /** JPEG or WebP quality, 1 to 100. Default 80. Refused with `png`. */
23
+ quality?: number;
24
+ /** The upload's byte limit. Default 5 MiB. */
25
+ maxBytes?: number;
26
+ /** The most pixels the upload may claim. Default 25 000 000. */
27
+ maxPixels?: number;
28
+ /** Aborts the call: it rejects with the signal's reason. */
29
+ signal?: AbortSignal;
30
+ };
31
+ export type RenderedImage<F extends RenderFormat = RenderFormat> = {
32
+ /** The upload as read: its format and its oriented size. */
33
+ source: {
34
+ format: SniffedFormat;
35
+ width: number;
36
+ height: number;
37
+ };
38
+ bytes: Uint8Array;
39
+ width: number;
40
+ height: number;
41
+ contentType: (typeof RENDER_CONTENT_TYPES)[F];
42
+ };
43
+ /**
44
+ * One picture from an upload: checked by its first bytes, the optional
45
+ * rectangle `crop` kept, resized to `width` x `height` (never enlarged) and
46
+ * encoded as JPEG, WebP or PNG with every metadata stripped. Same limits,
47
+ * orientation and refusals as `ImageSet.process`; `INVALID_CROP` for a
48
+ * rectangle that is badly shaped or outside the picture, `TOO_SMALL` when the
49
+ * crop is smaller than `width` x `height` on either side.
50
+ *
51
+ * ```ts
52
+ * const { bytes, contentType } = await renderImage(blob, {
53
+ * crop: { x: 0, y: 120, width: 1600, height: 900 },
54
+ * width: 800,
55
+ * height: 450,
56
+ * format: 'jpeg',
57
+ * });
58
+ * ```
59
+ */
60
+ export declare function renderImage<F extends RenderFormat>(input: ImageInput, options: RenderOptions<F>): Promise<RenderedImage<F>>;
61
+ //# sourceMappingURL=render-image.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"render-image.d.ts","sourceRoot":"","sources":["../../src/render/render-image.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,KAAK,UAAU,EAAa,MAAM,qBAAqB,CAAC;AACjE,OAAO,EAAE,KAAK,aAAa,EAAe,MAAM,gBAAgB,CAAC;AACjE,OAAO,EAGN,KAAK,IAAI,EAGT,MAAM,qBAAqB,CAAC;AAK7B,wCAAwC;AACxC,MAAM,MAAM,YAAY,GAAG,MAAM,GAAG,MAAM,GAAG,KAAK,CAAC;AAEnD,8CAA8C;AAC9C,eAAO,MAAM,oBAAoB;;;;EAItB,CAAC;AAEZ,mEAAmE;AACnE,MAAM,MAAM,QAAQ,GAAG,IAAI,CAAC;AAE5B,MAAM,MAAM,aAAa,CAAC,CAAC,SAAS,YAAY,GAAG,YAAY,IAAI;IAClE,mEAAmE;IACnE,IAAI,CAAC,EAAE,QAAQ,CAAC;IAChB,qEAAqE;IACrE,KAAK,EAAE,MAAM,CAAC;IACd,sEAAsE;IACtE,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,CAAC,CAAC;IACV,sEAAsE;IACtE,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,8CAA8C;IAC9C,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,gEAAgE;IAChE,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,4DAA4D;IAC5D,MAAM,CAAC,EAAE,WAAW,CAAC;CACrB,CAAC;AAEF,MAAM,MAAM,aAAa,CAAC,CAAC,SAAS,YAAY,GAAG,YAAY,IAAI;IAClE,4DAA4D;IAC5D,MAAM,EAAE;QAAE,MAAM,EAAE,aAAa,CAAC;QAAC,KAAK,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,MAAM,CAAA;KAAE,CAAC;IACjE,KAAK,EAAE,UAAU,CAAC;IAClB,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,WAAW,EAAE,CAAC,OAAO,oBAAoB,CAAC,CAAC,CAAC,CAAC,CAAC;CAC9C,CAAC;AA2FF;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAsB,WAAW,CAAC,CAAC,SAAS,YAAY,EACvD,KAAK,EAAE,UAAU,EACjB,OAAO,EAAE,aAAa,CAAC,CAAC,CAAC,GACvB,OAAO,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC,CAqD3B"}
@@ -0,0 +1,27 @@
1
+ import type { BoundBucket } from '@softistx/s3';
2
+ import type { ImageFormat } from '../set/options.js';
3
+ import type { ImageSet } from '../set/types.js';
4
+ import type { ImageStore } from './types.js';
5
+ /**
6
+ * Binds an image set to a `@softistx/s3` bucket whose key is built from the
7
+ * params **and a `size`**.
8
+ *
9
+ * ```ts
10
+ * const avatars = defineBucket({
11
+ * bucket: 'avatars',
12
+ * key: ({ imageId, size }: { imageId: string; size: number }) =>
13
+ * `avatars/${imageId}/${size}.webp`,
14
+ * });
15
+ * const store = bindImageStore(defineImageSet(), bindBucket(avatars));
16
+ * const imageId = crypto.randomUUID();
17
+ * const { version } = await store.replace({ imageId }, await request.blob());
18
+ * ```
19
+ *
20
+ * Objects are never public: puts pass only `type`, no `acl`, so bind the bucket
21
+ * without a public default `acl` (Bun would send it on every put). Serve them
22
+ * with `presignGet`. Use a fresh id per upload (see `ImageStore.replace`).
23
+ */
24
+ export declare function bindImageStore<P extends {
25
+ size: number;
26
+ }, F extends ImageFormat = ImageFormat>(set: ImageSet<F>, bucket: BoundBucket<P>): ImageStore<Omit<P, 'size'>, F>;
27
+ //# sourceMappingURL=bind-image-store.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bind-image-store.d.ts","sourceRoot":"","sources":["../../src/s3/bind-image-store.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAChD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAClD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAC7C,OAAO,KAAK,EAAE,UAAU,EAAe,MAAM,SAAS,CAAC;AASvD;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,cAAc,CAC7B,CAAC,SAAS;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,EAC1B,CAAC,SAAS,WAAW,GAAG,WAAW,EAClC,GAAG,EAAE,QAAQ,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,WAAW,CAAC,CAAC,CAAC,GAAG,UAAU,CAAC,IAAI,CAAC,CAAC,EAAE,MAAM,CAAC,EAAE,CAAC,CAAC,CA8D1E"}
@@ -0,0 +1,4 @@
1
+ export { bindImageStore } from './bind-image-store.js';
2
+ export type { ImageStore, StoredImage, StoredVariant } from './types.js';
3
+ export { uploadImage } from './upload-image.js';
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/s3/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AACpD,YAAY,EAAE,UAAU,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AACtE,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC"}
@@ -0,0 +1,73 @@
1
+ import {
2
+ cropFromQuery2
3
+ } from "../chunks/index-b71y3faz.js";
4
+ import {
5
+ ImageError2
6
+ } from "../chunks/index-wfsrqqk2.js";
7
+
8
+ // src/s3/bind-image-store.ts
9
+ function versionOf(parts) {
10
+ const hasher = new Bun.CryptoHasher("sha256");
11
+ for (const part of parts)
12
+ hasher.update(part);
13
+ return hasher.digest("hex").slice(0, 16);
14
+ }
15
+ function bindImageStore(set, bucket) {
16
+ const at = (call, params, size) => {
17
+ if (!set.sizes.includes(size)) {
18
+ throw new RangeError(`${call}: size ${size} is not one of the set's sizes (${set.sizes.join(", ")})`);
19
+ }
20
+ return { ...params, size };
21
+ };
22
+ const removeAll = (params) => Promise.all(set.sizes.map((size) => bucket.delete(at("remove", params, size))));
23
+ return {
24
+ async replace(params, input, options) {
25
+ const { variants } = await set.process(input, options);
26
+ const results = await Promise.allSettled(variants.map((v) => bucket.put(at("replace", params, v.size), v.bytes, {
27
+ type: v.contentType
28
+ })));
29
+ const failed = results.find((r) => r.status === "rejected");
30
+ if (failed) {
31
+ await Promise.allSettled(variants.flatMap((v, i) => results[i]?.status === "fulfilled" ? [bucket.delete(at("replace", params, v.size))] : []));
32
+ throw failed.reason;
33
+ }
34
+ return {
35
+ version: versionOf(variants.map((v) => v.bytes)),
36
+ variants: variants.map((v) => ({
37
+ size: v.size,
38
+ key: bucket.keyFor(at("replace", params, v.size)),
39
+ width: v.width,
40
+ height: v.height,
41
+ contentType: v.contentType,
42
+ byteLength: v.bytes.byteLength
43
+ }))
44
+ };
45
+ },
46
+ async remove(params) {
47
+ await removeAll(params);
48
+ },
49
+ presignGet: (params, size, options) => bucket.presignGet(at("presignGet", params, size), options),
50
+ async bytes(params, size) {
51
+ return bucket.bytes(at("bytes", params, size));
52
+ },
53
+ keyFor: (params, size) => bucket.keyFor(at("keyFor", params, size))
54
+ };
55
+ }
56
+ // src/s3/upload-image.ts
57
+ async function uploadImage(store, owner, request) {
58
+ const type = request.headers.get("content-type") ?? "";
59
+ if (!/^\s*image\//i.test(type)) {
60
+ throw new ImageError2("UNSUPPORTED_TYPE", "format", "the request is not an image: its content-type must be image/*");
61
+ }
62
+ const id = crypto.randomUUID();
63
+ const crop = cropFromQuery2(new URL(request.url).searchParams);
64
+ const stored = await store.replace({ ...owner, imageId: id }, request.body ?? new Uint8Array(0), crop ? { crop } : undefined);
65
+ return { id, version: stored.version, variants: stored.variants };
66
+ }
67
+ export {
68
+ bindImageStore,
69
+ uploadImage
70
+ };
71
+
72
+ //# debugId=0E4DC01570A818EE64756E2164756E21
73
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,11 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/s3/bind-image-store.ts", "../src/s3/upload-image.ts"],
4
+ "sourcesContent": [
5
+ "import type { BoundBucket } from '@softistx/s3';\nimport type { ImageFormat } from '../set/options';\nimport type { ImageSet } from '../set/types';\nimport type { ImageStore, StoredImage } from './types';\n\n/** The first 16 hex characters of the SHA-256 over the parts, in order. */\nfunction versionOf(parts: readonly Uint8Array[]): string {\n\tconst hasher = new Bun.CryptoHasher('sha256');\n\tfor (const part of parts) hasher.update(part);\n\treturn hasher.digest('hex').slice(0, 16);\n}\n\n/**\n * Binds an image set to a `@softistx/s3` bucket whose key is built from the\n * params **and a `size`**.\n *\n * ```ts\n * const avatars = defineBucket({\n * \tbucket: 'avatars',\n * \tkey: ({ imageId, size }: { imageId: string; size: number }) =>\n * \t\t`avatars/${imageId}/${size}.webp`,\n * });\n * const store = bindImageStore(defineImageSet(), bindBucket(avatars));\n * const imageId = crypto.randomUUID();\n * const { version } = await store.replace({ imageId }, await request.blob());\n * ```\n *\n * Objects are never public: puts pass only `type`, no `acl`, so bind the bucket\n * without a public default `acl` (Bun would send it on every put). Serve them\n * with `presignGet`. Use a fresh id per upload (see `ImageStore.replace`).\n */\nexport function bindImageStore<\n\tP extends { size: number },\n\tF extends ImageFormat = ImageFormat,\n>(set: ImageSet<F>, bucket: BoundBucket<P>): ImageStore<Omit<P, 'size'>, F> {\n\tconst at = (call: string, params: Omit<P, 'size'>, size: number) => {\n\t\tif (!set.sizes.includes(size)) {\n\t\t\tthrow new RangeError(\n\t\t\t\t`${call}: size ${size} is not one of the set's sizes (${set.sizes.join(', ')})`,\n\t\t\t);\n\t\t}\n\t\treturn { ...params, size } as P;\n\t};\n\tconst removeAll = (params: Omit<P, 'size'>) =>\n\t\tPromise.all(\n\t\t\tset.sizes.map((size) => bucket.delete(at('remove', params, size))),\n\t\t);\n\n\treturn {\n\t\tasync replace(params, input, options) {\n\t\t\tconst { variants } = await set.process(input, options);\n\t\t\tconst results = await Promise.allSettled(\n\t\t\t\tvariants.map((v) =>\n\t\t\t\t\tbucket.put(at('replace', params, v.size), v.bytes, {\n\t\t\t\t\t\ttype: v.contentType,\n\t\t\t\t\t}),\n\t\t\t\t),\n\t\t\t);\n\t\t\tconst failed = results.find((r) => r.status === 'rejected');\n\t\t\tif (failed) {\n\t\t\t\t// Best effort, and only what this call wrote: the keys whose put was\n\t\t\t\t// fulfilled. The refused one is never deleted, since an earlier object\n\t\t\t\t// may sit there (fixed params) and a refused put wrote nothing. A put\n\t\t\t\t// that failed after sending (a network error) may still land later;\n\t\t\t\t// with a fresh id per upload that leaves an unreferenced object.\n\t\t\t\tawait Promise.allSettled(\n\t\t\t\t\tvariants.flatMap((v, i) =>\n\t\t\t\t\t\tresults[i]?.status === 'fulfilled'\n\t\t\t\t\t\t\t? [bucket.delete(at('replace', params, v.size))]\n\t\t\t\t\t\t\t: [],\n\t\t\t\t\t),\n\t\t\t\t);\n\t\t\t\tthrow failed.reason;\n\t\t\t}\n\t\t\treturn {\n\t\t\t\tversion: versionOf(variants.map((v) => v.bytes)),\n\t\t\t\tvariants: variants.map((v) => ({\n\t\t\t\t\tsize: v.size,\n\t\t\t\t\tkey: bucket.keyFor(at('replace', params, v.size)),\n\t\t\t\t\twidth: v.width,\n\t\t\t\t\theight: v.height,\n\t\t\t\t\tcontentType: v.contentType,\n\t\t\t\t\tbyteLength: v.bytes.byteLength,\n\t\t\t\t})),\n\t\t\t} satisfies StoredImage<F>;\n\t\t},\n\t\tasync remove(params) {\n\t\t\tawait removeAll(params);\n\t\t},\n\t\tpresignGet: (params, size, options) =>\n\t\t\tbucket.presignGet(at('presignGet', params, size), options),\n\t\tasync bytes(params, size) {\n\t\t\treturn bucket.bytes(at('bytes', params, size));\n\t\t},\n\t\tkeyFor: (params, size) => bucket.keyFor(at('keyFor', params, size)),\n\t};\n}\n",
6
+ "import { cropFromQuery } from '../crop/from-query';\nimport { ImageError } from '../errors/image-error';\nimport type { ImageFormat } from '../set/options';\nimport type { ImageStore, StoredImage } from './types';\n\n/**\n * Stores the image a request uploads, under a fresh id.\n *\n * - The request's `content-type` must be `image/*` (any case, parameters\n * ignored); otherwise `ImageError` `UNSUPPORTED_TYPE` / `format`, before the\n * body is read. The bytes themselves are still judged by their magic bytes.\n * - The body is streamed into `store.replace`, so a body over the set's byte\n * limit is an `ImageError` `TOO_LARGE` / `bytes` and is cancelled at the\n * chunk that crosses it.\n * - The optional crop comes from `?x=&y=&size=` (see `cropFromQuery`); a\n * bad one is `INVALID_CROP`.\n *\n * `owner` is the store's params without `imageId`; the id is\n * `crypto.randomUUID()` and is returned, to be saved beside `version`.\n */\nexport async function uploadImage<\n\tP extends { imageId: string },\n\tF extends ImageFormat = ImageFormat,\n>(\n\tstore: Pick<ImageStore<P, F>, 'replace'>,\n\towner: Omit<P, 'imageId'>,\n\trequest: Request,\n): Promise<{ id: string } & StoredImage<F>> {\n\tconst type = request.headers.get('content-type') ?? '';\n\tif (!/^\\s*image\\//i.test(type)) {\n\t\tthrow new ImageError(\n\t\t\t'UNSUPPORTED_TYPE',\n\t\t\t'format',\n\t\t\t'the request is not an image: its content-type must be image/*',\n\t\t);\n\t}\n\tconst id = crypto.randomUUID();\n\tconst crop = cropFromQuery(new URL(request.url).searchParams);\n\t// TypeScript cannot see that `Omit<P, 'imageId'>` plus `imageId` is a `P`.\n\tconst stored = await store.replace(\n\t\t{ ...owner, imageId: id } as P,\n\t\trequest.body ?? new Uint8Array(0),\n\t\tcrop ? { crop } : undefined,\n\t);\n\treturn { id, version: stored.version, variants: stored.variants };\n}\n"
7
+ ],
8
+ "mappings": ";;;;;;;;AAMA,SAAS,SAAS,CAAC,OAAsC;AAAA,EACxD,MAAM,SAAS,IAAI,IAAI,aAAa,QAAQ;AAAA,EAC5C,WAAW,QAAQ;AAAA,IAAO,OAAO,OAAO,IAAI;AAAA,EAC5C,OAAO,OAAO,OAAO,KAAK,EAAE,MAAM,GAAG,EAAE;AAAA;AAsBjC,SAAS,cAGf,CAAC,KAAkB,QAAwD;AAAA,EAC3E,MAAM,KAAK,CAAC,MAAc,QAAyB,SAAiB;AAAA,IACnE,IAAI,CAAC,IAAI,MAAM,SAAS,IAAI,GAAG;AAAA,MAC9B,MAAM,IAAI,WACT,GAAG,cAAc,uCAAuC,IAAI,MAAM,KAAK,IAAI,IAC5E;AAAA,IACD;AAAA,IACA,OAAO,KAAK,QAAQ,KAAK;AAAA;AAAA,EAE1B,MAAM,YAAY,CAAC,WAClB,QAAQ,IACP,IAAI,MAAM,IAAI,CAAC,SAAS,OAAO,OAAO,GAAG,UAAU,QAAQ,IAAI,CAAC,CAAC,CAClE;AAAA,EAED,OAAO;AAAA,UACA,OAAO,CAAC,QAAQ,OAAO,SAAS;AAAA,MACrC,QAAQ,aAAa,MAAM,IAAI,QAAQ,OAAO,OAAO;AAAA,MACrD,MAAM,UAAU,MAAM,QAAQ,WAC7B,SAAS,IAAI,CAAC,MACb,OAAO,IAAI,GAAG,WAAW,QAAQ,EAAE,IAAI,GAAG,EAAE,OAAO;AAAA,QAClD,MAAM,EAAE;AAAA,MACT,CAAC,CACF,CACD;AAAA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AM,KAAK,OAAO,WAAW;AAAA,EAC7B,MAAM,OAAO,eAAc,IAAI,IAAI,QAAQ,GAAG,EAAE,YAAY;AAAA,EAE5D,MAAM,SAAS,MAAM,MAAM,QAC1B,KAAK,OAAO,SAAS,GAAG,GACxB,QAAQ,QAAQ,IAAI,WAAW,CAAC,GAChC,OAAO,EAAE,KAAK,IAAI,SACnB;AAAA,EACA,OAAO,EAAE,IAAI,SAAS,OAAO,SAAS,UAAU,OAAO,SAAS;AAAA;",
9
+ "debugId": "0E4DC01570A818EE64756E2164756E21",
10
+ "names": []
11
+ }
@@ -0,0 +1,61 @@
1
+ import type { PresignGetOptions } from '@softistx/s3';
2
+ import type { ImageInput } from '../input/read-input.js';
3
+ import type { ImageContentType, ImageFormat } from '../set/options.js';
4
+ import type { ProcessOptions } from '../set/types.js';
5
+ /** One stored size. */
6
+ export type StoredVariant<F extends ImageFormat = ImageFormat> = {
7
+ size: number;
8
+ /** The object's key in the bucket. */
9
+ key: string;
10
+ width: number;
11
+ height: number;
12
+ /** The set's `contentType`. */
13
+ contentType: ImageContentType<F>;
14
+ byteLength: number;
15
+ };
16
+ /** What `replace` stored. */
17
+ export type StoredImage<F extends ImageFormat = ImageFormat> = {
18
+ /**
19
+ * 16 hex characters of the SHA-256 over the variants' bytes, in the order
20
+ * of the set's `sizes`: the same picture processed again gives the same
21
+ * version, so it can be a cache-busting query or an ETag.
22
+ */
23
+ version: string;
24
+ variants: StoredVariant<F>[];
25
+ };
26
+ /**
27
+ * An image set bound to a bucket; `P` is the bucket's key parameters minus
28
+ * `size`, and `F` the set's format.
29
+ */
30
+ export type ImageStore<P, F extends ImageFormat = ImageFormat> = {
31
+ /**
32
+ * Processes the upload, then stores every size at `{ ...params, size }`.
33
+ *
34
+ * An `ImageError` from processing (a bad crop included) comes out before
35
+ * anything is written. If a put fails, the keys whose put was fulfilled are
36
+ * deleted (best effort) and the first put error is rethrown unchanged,
37
+ * "first" meaning first in the set's size order, not first in time. The key
38
+ * whose put was refused is never deleted: with fixed params an earlier
39
+ * object may sit there, and a refused put wrote nothing. A put that failed
40
+ * after sending (a network error) may still land later; with a fresh id per
41
+ * upload that leaves at worst an unreferenced object.
42
+ *
43
+ * Recommended pattern: `const imageId = crypto.randomUUID()`, `replace`
44
+ * under it, store the id, then `remove` the former id's set. With fixed
45
+ * params, a `replace` followed by removing the "former" image would delete
46
+ * the new one.
47
+ */
48
+ replace(params: P, input: ImageInput, options?: ProcessOptions): Promise<StoredImage<F>>;
49
+ /**
50
+ * Deletes every size of the set at `params`. A missing object is not an
51
+ * error.
52
+ */
53
+ remove(params: P): Promise<void>;
54
+ /** A signed URL for one size. A `size` outside the set is a `RangeError`. */
55
+ presignGet(params: P, size: number, options?: PresignGetOptions): string;
56
+ /** The bytes of one size, or `undefined` when missing. */
57
+ bytes(params: P, size: number): Promise<Uint8Array | undefined>;
58
+ /** The key of one size. A `size` outside the set is a `RangeError`. */
59
+ keyFor(params: P, size: number): string;
60
+ };
61
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/s3/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AACtD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AACtD,OAAO,KAAK,EAAE,gBAAgB,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AACpE,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAEnD,uBAAuB;AACvB,MAAM,MAAM,aAAa,CAAC,CAAC,SAAS,WAAW,GAAG,WAAW,IAAI;IAChE,IAAI,EAAE,MAAM,CAAC;IACb,sCAAsC;IACtC,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,+BAA+B;IAC/B,WAAW,EAAE,gBAAgB,CAAC,CAAC,CAAC,CA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