@mainsoft/agent-cms 0.10.2 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (87) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/README.md +91 -2
  3. package/TODO.md +6 -6
  4. package/UPGRADE-0.11.md +43 -0
  5. package/dist/agent.d.cts +1 -1
  6. package/dist/agent.d.ts +1 -1
  7. package/dist/agent.js +7 -7
  8. package/dist/bin.js +236 -23
  9. package/dist/{chunk-IU2SMWAM.js → chunk-42PW3HQ3.js} +15 -1
  10. package/dist/{chunk-6PB4ZCHH.js → chunk-CIMW73L3.js} +58 -1
  11. package/dist/{chunk-7Y6TU2UA.js → chunk-HY4ED7LT.js} +2 -2
  12. package/dist/{chunk-DSJETZT4.js → chunk-I3DF6DMI.js} +15 -10
  13. package/dist/{chunk-KAO3Z3CF.js → chunk-I5UHTAAW.js} +3 -1
  14. package/dist/{chunk-7SCYDG2G.js → chunk-J25IKU7C.js} +164 -28
  15. package/dist/{chunk-BFVBHIHV.js → chunk-J5DW3RFQ.js} +4 -1
  16. package/dist/{chunk-7WJIIJCI.js → chunk-LELPCGDX.js} +8 -5
  17. package/dist/{chunk-5ODZ2ZJF.js → chunk-NO6S7KOL.js} +1 -1
  18. package/dist/{chunk-5FMVVBDR.js → chunk-OLHLSSTD.js} +3 -3
  19. package/dist/{chunk-3OPKTBEI.js → chunk-SV3ZA6XP.js} +1 -1
  20. package/dist/{chunk-PEQY4N73.js → chunk-TKKTEGHL.js} +1 -0
  21. package/dist/{chunk-PGV2QPIL.js → chunk-WWG7XDIZ.js} +2 -0
  22. package/dist/cli.cjs +236 -23
  23. package/dist/cli.d.cts +2 -2
  24. package/dist/cli.d.ts +2 -2
  25. package/dist/cli.js +18 -16
  26. package/dist/client.cjs +4 -1
  27. package/dist/client.d.cts +14 -2
  28. package/dist/client.d.ts +14 -2
  29. package/dist/client.js +1 -1
  30. package/dist/{cms-pqJyowu1.d.ts → cms-A9tmLmTP.d.ts} +100 -5
  31. package/dist/{cms-C_vkAxYV.d.cts → cms-CaB81PPX.d.cts} +100 -5
  32. package/dist/config.d.cts +1 -1
  33. package/dist/config.d.ts +1 -1
  34. package/dist/{drizzle-repo-DWLD0rWe.d.ts → drizzle-repo-BXV-hFkE.d.ts} +1 -1
  35. package/dist/{drizzle-repo-CviH3EX7.d.cts → drizzle-repo-CblPVYnK.d.cts} +1 -1
  36. package/dist/errors.cjs +4 -0
  37. package/dist/errors.d.cts +3 -1
  38. package/dist/errors.d.ts +3 -1
  39. package/dist/errors.js +2 -2
  40. package/dist/frontmatter.d.cts +1 -1
  41. package/dist/frontmatter.d.ts +1 -1
  42. package/dist/http.cjs +17 -7
  43. package/dist/http.d.cts +2 -2
  44. package/dist/http.d.ts +2 -2
  45. package/dist/http.js +16 -15
  46. package/dist/image-sharp.cjs +104 -0
  47. package/dist/image-sharp.d.cts +47 -0
  48. package/dist/image-sharp.d.ts +47 -0
  49. package/dist/image-sharp.js +70 -0
  50. package/dist/index.cjs +244 -27
  51. package/dist/index.d.cts +3 -3
  52. package/dist/index.d.ts +3 -3
  53. package/dist/index.js +21 -21
  54. package/dist/mcp.cjs +9 -4
  55. package/dist/mcp.d.cts +1 -1
  56. package/dist/mcp.d.ts +1 -1
  57. package/dist/mcp.js +11 -11
  58. package/dist/messages/en.cjs +2 -0
  59. package/dist/messages/en.js +1 -1
  60. package/dist/messages/ko.cjs +2 -0
  61. package/dist/messages/ko.js +2 -0
  62. package/dist/registry/cms-doc-editor.json +6 -1
  63. package/dist/registry/cms-lib.json +2 -2
  64. package/dist/registry/cms-media-picker.json +2 -2
  65. package/dist/registry/cms-media.json +1 -1
  66. package/dist/registry/registry.json +4 -0
  67. package/dist/storage-vercel-blob.cjs +199 -0
  68. package/dist/storage-vercel-blob.d.cts +98 -0
  69. package/dist/storage-vercel-blob.d.ts +98 -0
  70. package/dist/storage-vercel-blob.js +165 -0
  71. package/dist/storage.cjs +63 -3
  72. package/dist/storage.d.cts +27 -2
  73. package/dist/storage.d.ts +27 -2
  74. package/dist/storage.js +9 -3
  75. package/dist/tables.cjs +15 -1
  76. package/dist/tables.d.cts +160 -7
  77. package/dist/tables.d.ts +160 -7
  78. package/dist/tables.js +2 -2
  79. package/dist/tools.cjs +8 -3
  80. package/dist/tools.d.cts +1 -1
  81. package/dist/tools.d.ts +1 -1
  82. package/dist/tools.js +9 -9
  83. package/dist/types.cjs +5 -0
  84. package/dist/types.d.cts +6 -1
  85. package/dist/types.d.ts +6 -1
  86. package/dist/types.js +3 -3
  87. package/package.json +36 -2
@@ -1,4 +1,4 @@
1
- import { r as DocInput } from './cms-C_vkAxYV.cjs';
1
+ import { t as DocInput } from './cms-CaB81PPX.cjs';
2
2
  import './storage.cjs';
3
3
  import './types.cjs';
4
4
  import 'zod';
@@ -1,4 +1,4 @@
1
- import { r as DocInput } from './cms-pqJyowu1.js';
1
+ import { t as DocInput } from './cms-A9tmLmTP.js';
2
2
  import './storage.js';
3
3
  import './types.js';
4
4
  import 'zod';
package/dist/http.cjs CHANGED
@@ -174,6 +174,8 @@ var en = {
174
174
  import_blocked: "That address cannot be fetched",
175
175
  import_failed: "Could not fetch the file ({reason})",
176
176
  asset_rejected: "This file did not pass the check",
177
+ asset_replace_type: "A file can only be replaced by one of the same type ({mime})",
178
+ unknown_image_size: "There is no image size named \u201C{size}\u201D",
177
179
  asset_missing: "The file {id} does not exist",
178
180
  asset_wrong_kind: "The file {id} is not a kind this field takes",
179
181
  asset_alt_missing: "The image on line {line} has no alt text",
@@ -263,6 +265,8 @@ var ERROR_CODES = {
263
265
  import_blocked: "invalid",
264
266
  import_failed: "invalid",
265
267
  asset_rejected: "invalid",
268
+ asset_replace_type: "invalid",
269
+ unknown_image_size: "not_found",
266
270
  asset_missing: "invalid",
267
271
  asset_wrong_kind: "invalid",
268
272
  asset_alt_missing: "invalid",
@@ -887,9 +891,14 @@ var CMS_TOOLS = [
887
891
  {
888
892
  name: "update_asset",
889
893
  media: "write",
890
- description: "Change a file's alt text or tags. Documents that use the file without their own alt text show the new one.",
891
- input: import_zod.z.object({ id: assetId, alt: import_zod.z.string().optional(), tags: import_zod.z.array(import_zod.z.string()).optional() }),
892
- run: async (cms, actor, a) => assetOut(await cms.assets.update(actor, a.id, { alt: a.alt, tags: a.tags }))
894
+ description: "Change a file's alt text, tags or focal point. Documents that use the file without their own alt text show the new one.",
895
+ input: import_zod.z.object({
896
+ id: assetId,
897
+ alt: import_zod.z.string().optional(),
898
+ tags: import_zod.z.array(import_zod.z.string()).optional(),
899
+ focal: import_zod.z.object({ x: import_zod.z.number().min(0).max(1), y: import_zod.z.number().min(0).max(1) }).nullable().optional().describe("Images: the point that stays in view when the picture is cropped, from the left and from the top (0..1). null clears it.")
900
+ }),
901
+ run: async (cms, actor, a) => assetOut(await cms.assets.update(actor, a.id, { alt: a.alt, tags: a.tags, focal: a.focal }))
893
902
  },
894
903
  {
895
904
  name: "move_asset",
@@ -958,7 +967,7 @@ async function runTool(cms, actor, name, args = {}, opts = {}) {
958
967
  }
959
968
 
960
969
  // src/version.ts
961
- var VERSION = "0.10.2";
970
+ var VERSION = "0.11.0";
962
971
 
963
972
  // src/mcp.ts
964
973
  var ANONYMOUS_READ_TOOLS = ["list_collections", "search_documents", "read_for_answer", "list_documents", "get_document"];
@@ -1384,7 +1393,7 @@ function createCmsHandler(opts) {
1384
1393
  }
1385
1394
  }
1386
1395
  if (seg[0] === "files" && seg[1] && seg.length <= 3 && (m === "GET" || m === "HEAD")) {
1387
- const to = await core.assets.open(actor, seg[1], { thumb: q.get("thumb") === "1", download: q.get("download") === "1" });
1396
+ const to = await core.assets.open(actor, seg[1], { thumb: q.get("thumb") === "1", download: q.get("download") === "1", size: q.get("size") });
1388
1397
  return new Response(null, { status: 302, headers: { location: to.url, "cache-control": to.visibility === "public" && q.get("download") !== "1" ? "public, max-age=300" : "private, no-store" } });
1389
1398
  }
1390
1399
  if (seg[0] === "stores" && seg[1] && seg[2] === "files") {
@@ -1434,7 +1443,8 @@ function createCmsHandler(opts) {
1434
1443
  store: import_zod2.z.string().nullable().optional(),
1435
1444
  alt: import_zod2.z.string().max(500).optional(),
1436
1445
  tags,
1437
- field: import_zod2.z.object({ collection: import_zod2.z.string(), path: import_zod2.z.string() }).nullable().optional()
1446
+ field: import_zod2.z.object({ collection: import_zod2.z.string(), path: import_zod2.z.string() }).nullable().optional(),
1447
+ replace: import_zod2.z.string().max(40).nullable().optional()
1438
1448
  })
1439
1449
  );
1440
1450
  return json(await assets.requestUpload(actor, b), 201);
@@ -1457,7 +1467,7 @@ function createCmsHandler(opts) {
1457
1467
  const id = seg[1];
1458
1468
  if (id && !id.startsWith("_") && id !== "uploads") {
1459
1469
  if (seg.length === 2 && m === "GET") return json({ asset: await assets.get(actor, id) });
1460
- if (seg.length === 2 && m === "PATCH") return json({ asset: await assets.update(actor, id, await body(req, import_zod2.z.object({ alt: import_zod2.z.string().max(500).optional(), tags }))) });
1470
+ if (seg.length === 2 && m === "PATCH") return json({ asset: await assets.update(actor, id, await body(req, import_zod2.z.object({ alt: import_zod2.z.string().max(500).optional(), tags, focal: import_zod2.z.object({ x: import_zod2.z.number().min(0).max(1), y: import_zod2.z.number().min(0).max(1) }).nullable().optional() }))) });
1461
1471
  if (seg.length === 2 && m === "DELETE") {
1462
1472
  await assets.remove(actor, id, { confirm: q.get("confirm") === "1" });
1463
1473
  return json({ ok: true });
package/dist/http.d.cts CHANGED
@@ -1,4 +1,4 @@
1
- import { C as CmsLike } from './cms-C_vkAxYV.cjs';
1
+ import { C as CmsLike } from './cms-CaB81PPX.cjs';
2
2
  import { McpOptions } from './mcp.cjs';
3
3
  import { Actor } from './types.cjs';
4
4
  import './storage.cjs';
@@ -64,7 +64,7 @@ import '@modelcontextprotocol/sdk/server/index.js';
64
64
  * POST /assets/:id/move {folder?, name?}
65
65
  * POST /assets/:id/thumb body: a WebP, JPEG or PNG up to 512KB
66
66
  * DELETE /assets/:id 409 asset_in_use with the documents; ?confirm=1 deletes anyway
67
- * GET /files/:id/:name redirect to the file (?thumb=1, ?download=1); private files need a session or key
67
+ * GET /files/:id/:name redirect to the file (?thumb=1, ?download=1, ?size=<name>); private files need a session or key
68
68
  *
69
69
  * File manager (the bucket as it is, also files the CMS did not upload):
70
70
  *
package/dist/http.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { C as CmsLike } from './cms-pqJyowu1.js';
1
+ import { C as CmsLike } from './cms-A9tmLmTP.js';
2
2
  import { McpOptions } from './mcp.js';
3
3
  import { Actor } from './types.js';
4
4
  import './storage.js';
@@ -64,7 +64,7 @@ import '@modelcontextprotocol/sdk/server/index.js';
64
64
  * POST /assets/:id/move {folder?, name?}
65
65
  * POST /assets/:id/thumb body: a WebP, JPEG or PNG up to 512KB
66
66
  * DELETE /assets/:id 409 asset_in_use with the documents; ?confirm=1 deletes anyway
67
- * GET /files/:id/:name redirect to the file (?thumb=1, ?download=1); private files need a session or key
67
+ * GET /files/:id/:name redirect to the file (?thumb=1, ?download=1, ?size=<name>); private files need a session or key
68
68
  *
69
69
  * File manager (the bucket as it is, also files the CMS did not upload):
70
70
  *
package/dist/http.js CHANGED
@@ -1,32 +1,32 @@
1
1
  import {
2
2
  handleMcp,
3
3
  toJsonSchema
4
- } from "./chunk-5FMVVBDR.js";
5
- import "./chunk-5ODZ2ZJF.js";
6
- import "./chunk-DSJETZT4.js";
7
- import "./chunk-BFVBHIHV.js";
8
- import "./chunk-7Y6TU2UA.js";
4
+ } from "./chunk-OLHLSSTD.js";
5
+ import "./chunk-NO6S7KOL.js";
6
+ import "./chunk-I3DF6DMI.js";
7
+ import "./chunk-HY4ED7LT.js";
9
8
  import {
10
9
  BULK_ACTIONS,
11
10
  PREVIEW_TTLS,
12
11
  coreOf,
13
12
  keyFromHeader
14
- } from "./chunk-7SCYDG2G.js";
13
+ } from "./chunk-J25IKU7C.js";
15
14
  import "./chunk-MA6VKJ3F.js";
16
15
  import {
17
16
  ASSET_KINDS,
18
17
  DOC_STATUSES,
19
18
  KEY_SCOPES
20
- } from "./chunk-PEQY4N73.js";
21
- import "./chunk-3OPKTBEI.js";
22
- import "./chunk-AKN7SUXU.js";
19
+ } from "./chunk-TKKTEGHL.js";
23
20
  import {
24
21
  CmsError,
25
22
  formatMessage,
26
23
  isCmsError
27
- } from "./chunk-KAO3Z3CF.js";
28
- import "./chunk-PGV2QPIL.js";
29
- import "./chunk-6PB4ZCHH.js";
24
+ } from "./chunk-I5UHTAAW.js";
25
+ import "./chunk-WWG7XDIZ.js";
26
+ import "./chunk-SV3ZA6XP.js";
27
+ import "./chunk-AKN7SUXU.js";
28
+ import "./chunk-J5DW3RFQ.js";
29
+ import "./chunk-CIMW73L3.js";
30
30
  import "./chunk-2NTK7H4W.js";
31
31
 
32
32
  // src/http.ts
@@ -244,7 +244,7 @@ function createCmsHandler(opts) {
244
244
  }
245
245
  }
246
246
  if (seg[0] === "files" && seg[1] && seg.length <= 3 && (m === "GET" || m === "HEAD")) {
247
- const to = await core.assets.open(actor, seg[1], { thumb: q.get("thumb") === "1", download: q.get("download") === "1" });
247
+ const to = await core.assets.open(actor, seg[1], { thumb: q.get("thumb") === "1", download: q.get("download") === "1", size: q.get("size") });
248
248
  return new Response(null, { status: 302, headers: { location: to.url, "cache-control": to.visibility === "public" && q.get("download") !== "1" ? "public, max-age=300" : "private, no-store" } });
249
249
  }
250
250
  if (seg[0] === "stores" && seg[1] && seg[2] === "files") {
@@ -294,7 +294,8 @@ function createCmsHandler(opts) {
294
294
  store: z.string().nullable().optional(),
295
295
  alt: z.string().max(500).optional(),
296
296
  tags,
297
- field: z.object({ collection: z.string(), path: z.string() }).nullable().optional()
297
+ field: z.object({ collection: z.string(), path: z.string() }).nullable().optional(),
298
+ replace: z.string().max(40).nullable().optional()
298
299
  })
299
300
  );
300
301
  return json(await assets.requestUpload(actor, b), 201);
@@ -317,7 +318,7 @@ function createCmsHandler(opts) {
317
318
  const id = seg[1];
318
319
  if (id && !id.startsWith("_") && id !== "uploads") {
319
320
  if (seg.length === 2 && m === "GET") return json({ asset: await assets.get(actor, id) });
320
- if (seg.length === 2 && m === "PATCH") return json({ asset: await assets.update(actor, id, await body(req, z.object({ alt: z.string().max(500).optional(), tags }))) });
321
+ if (seg.length === 2 && m === "PATCH") return json({ asset: await assets.update(actor, id, await body(req, z.object({ alt: z.string().max(500).optional(), tags, focal: z.object({ x: z.number().min(0).max(1), y: z.number().min(0).max(1) }).nullable().optional() }))) });
321
322
  if (seg.length === 2 && m === "DELETE") {
322
323
  await assets.remove(actor, id, { confirm: q.get("confirm") === "1" });
323
324
  return json({ ok: true });
@@ -0,0 +1,104 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/image-sharp.ts
31
+ var image_sharp_exports = {};
32
+ __export(image_sharp_exports, {
33
+ MAX_IMAGE_SIDE: () => MAX_IMAGE_SIDE,
34
+ sharpTransforms: () => sharpTransforms
35
+ });
36
+ module.exports = __toCommonJS(image_sharp_exports);
37
+ var MAX_IMAGE_SIDE = 4096;
38
+ var FORMATS = ["webp", "avif", "jpeg", "png"];
39
+ var clamp = (v, lo, hi) => Math.min(hi, Math.max(lo, v));
40
+ function sharpTransforms(o) {
41
+ const g = globalThis;
42
+ if (typeof g.EdgeRuntime === "string" || g.process?.env?.NEXT_RUNTIME === "edge" || !g.process?.versions?.node) {
43
+ throw new Error('agent-cms: sharpTransforms() needs the Node.js runtime (sharp does not run on the Edge runtime). Move the CMS route to Node (`export const runtime = "nodejs"`) or leave `media.transforms` out there.');
44
+ }
45
+ const format = o.formats?.[0] ?? "webp";
46
+ for (const f of [format, ...o.formats ?? []]) if (!FORMATS.includes(f)) throw new Error(`agent-cms: sharpTransforms: unknown format "${f}" (use ${FORMATS.join(", ")})`);
47
+ const side = (name, which, v) => {
48
+ if (v === void 0) return;
49
+ if (!Number.isInteger(v) || v < 1 || v > MAX_IMAGE_SIDE) throw new Error(`agent-cms: sharpTransforms: size "${name}" has ${which} ${String(v)}; use a whole number from 1 to ${MAX_IMAGE_SIDE}`);
50
+ };
51
+ for (const [name, p] of Object.entries(o.presets)) {
52
+ if (!/^[a-z0-9][a-z0-9_-]{0,31}$/.test(name)) throw new Error(`agent-cms: sharpTransforms: "${name}" is not a size name (lowercase letters, digits, - and _)`);
53
+ side(name, "width", p.width);
54
+ side(name, "height", p.height);
55
+ if (!p.width && !p.height) throw new Error(`agent-cms: sharpTransforms: size "${name}" needs a width or a height`);
56
+ if (p.fit === "cover" && !(p.width && p.height)) throw new Error(`agent-cms: sharpTransforms: size "${name}" uses fit "cover", which needs both a width and a height`);
57
+ if (p.format && !FORMATS.includes(p.format)) throw new Error(`agent-cms: sharpTransforms: size "${name}" has the unknown format "${p.format}"`);
58
+ if (p.quality !== void 0 && !(p.quality >= 1 && p.quality <= 100)) throw new Error(`agent-cms: sharpTransforms: size "${name}" has quality ${p.quality}; use 1 to 100`);
59
+ }
60
+ const limitInputPixels = o.maxInputPixels ?? 1e8;
61
+ let lib = null;
62
+ const load = () => lib ??= import("sharp").then(
63
+ (m) => m.default ?? m,
64
+ (e) => {
65
+ lib = null;
66
+ throw new Error(`agent-cms: sharpTransforms() could not load "sharp" (npm install sharp): ${e instanceof Error ? e.message : String(e)}`);
67
+ }
68
+ );
69
+ return {
70
+ presets: { ...o.presets },
71
+ format,
72
+ maxInputBytes: o.maxInputBytes,
73
+ concurrency: o.concurrency,
74
+ timeoutMs: o.timeoutMs,
75
+ async make(bytes, job) {
76
+ const sharp = await load();
77
+ const meta = await sharp(bytes, { limitInputPixels }).metadata();
78
+ const turned = (meta.orientation ?? 1) >= 5;
79
+ const w = (turned ? meta.height : meta.width) ?? 0;
80
+ const h = (turned ? meta.width : meta.height) ?? 0;
81
+ if (!w || !h) throw new Error("not a picture sharp can read");
82
+ let img = sharp(bytes, { limitInputPixels }).rotate();
83
+ if (job.fit === "cover" && job.width && job.height) {
84
+ const k = Math.min(1, w / job.width, h / job.height);
85
+ const W = Math.max(1, Math.round(job.width * k));
86
+ const H = Math.max(1, Math.round(job.height * k));
87
+ const scale = Math.max(W / w, H / h);
88
+ const rw = Math.max(W, Math.round(w * scale));
89
+ const rh = Math.max(H, Math.round(h * scale));
90
+ const f = job.focal ?? { x: 0.5, y: 0.5 };
91
+ img = img.resize(rw, rh, { fit: "fill" }).extract({ left: clamp(Math.round(f.x * rw - W / 2), 0, rw - W), top: clamp(Math.round(f.y * rh - H / 2), 0, rh - H), width: W, height: H });
92
+ } else {
93
+ img = img.resize({ width: job.width ?? void 0, height: job.height ?? void 0, fit: "inside", withoutEnlargement: true });
94
+ }
95
+ const { data, info } = await img.toFormat(job.format, job.quality ? { quality: job.quality } : {}).toBuffer({ resolveWithObject: true });
96
+ return { bytes: new Uint8Array(data.buffer, data.byteOffset, data.byteLength), width: info.width, height: info.height };
97
+ }
98
+ };
99
+ }
100
+ // Annotate the CommonJS export names for ESM import in node:
101
+ 0 && (module.exports = {
102
+ MAX_IMAGE_SIDE,
103
+ sharpTransforms
104
+ });
@@ -0,0 +1,47 @@
1
+ import { I as ImagePreset, a as ImageFormat, b as ImageTransforms } from './cms-CaB81PPX.cjs';
2
+ import './storage.cjs';
3
+ import './types.cjs';
4
+ import 'zod';
5
+ import './errors.cjs';
6
+ import './markdown.cjs';
7
+ import './diff.cjs';
8
+
9
+ /**
10
+ * Image sizes made with sharp (`npm install sharp`). Node only.
11
+ *
12
+ * import { sharpTransforms } from "@mainsoft/agent-cms/image-sharp";
13
+ *
14
+ * createCms({
15
+ * media: {
16
+ * stores: { ... },
17
+ * transforms: sharpTransforms({
18
+ * presets: { thumb: { width: 480 }, card: { width: 800, height: 450, fit: "cover" }, full: { width: 1600 } },
19
+ * }),
20
+ * },
21
+ * });
22
+ *
23
+ * Only the sizes named here exist: `/api/cms/files/<id>/<name>?size=card`.
24
+ * Nobody can ask for an arbitrary width, so nobody can fill the store with
25
+ * copies. A size is made the first time it is asked for and kept in the
26
+ * store under `_cms/derived/`.
27
+ */
28
+
29
+ /** No size may be wider or taller than this. */
30
+ declare const MAX_IMAGE_SIDE = 4096;
31
+ interface SharpTransformsOptions {
32
+ /** Name → size. Names are lowercase letters, digits, `-` and `_`. */
33
+ presets: Record<string, ImagePreset>;
34
+ /** The first one is the format of sizes that do not name their own. Default `["webp"]`. */
35
+ formats?: readonly ImageFormat[];
36
+ /** Originals above this many bytes are served as they are. Default 40MB. */
37
+ maxInputBytes?: number;
38
+ /** Originals with more pixels than this are served as they are. Default 100 million. */
39
+ maxInputPixels?: number;
40
+ /** Copies made at the same time. Default 4. */
41
+ concurrency?: number;
42
+ /** A request waits this long for a copy, then gets the original. Default 10000. */
43
+ timeoutMs?: number;
44
+ }
45
+ declare function sharpTransforms(o: SharpTransformsOptions): ImageTransforms;
46
+
47
+ export { MAX_IMAGE_SIDE, type SharpTransformsOptions, sharpTransforms };
@@ -0,0 +1,47 @@
1
+ import { I as ImagePreset, a as ImageFormat, b as ImageTransforms } from './cms-A9tmLmTP.js';
2
+ import './storage.js';
3
+ import './types.js';
4
+ import 'zod';
5
+ import './errors.js';
6
+ import './markdown.js';
7
+ import './diff.js';
8
+
9
+ /**
10
+ * Image sizes made with sharp (`npm install sharp`). Node only.
11
+ *
12
+ * import { sharpTransforms } from "@mainsoft/agent-cms/image-sharp";
13
+ *
14
+ * createCms({
15
+ * media: {
16
+ * stores: { ... },
17
+ * transforms: sharpTransforms({
18
+ * presets: { thumb: { width: 480 }, card: { width: 800, height: 450, fit: "cover" }, full: { width: 1600 } },
19
+ * }),
20
+ * },
21
+ * });
22
+ *
23
+ * Only the sizes named here exist: `/api/cms/files/<id>/<name>?size=card`.
24
+ * Nobody can ask for an arbitrary width, so nobody can fill the store with
25
+ * copies. A size is made the first time it is asked for and kept in the
26
+ * store under `_cms/derived/`.
27
+ */
28
+
29
+ /** No size may be wider or taller than this. */
30
+ declare const MAX_IMAGE_SIDE = 4096;
31
+ interface SharpTransformsOptions {
32
+ /** Name → size. Names are lowercase letters, digits, `-` and `_`. */
33
+ presets: Record<string, ImagePreset>;
34
+ /** The first one is the format of sizes that do not name their own. Default `["webp"]`. */
35
+ formats?: readonly ImageFormat[];
36
+ /** Originals above this many bytes are served as they are. Default 40MB. */
37
+ maxInputBytes?: number;
38
+ /** Originals with more pixels than this are served as they are. Default 100 million. */
39
+ maxInputPixels?: number;
40
+ /** Copies made at the same time. Default 4. */
41
+ concurrency?: number;
42
+ /** A request waits this long for a copy, then gets the original. Default 10000. */
43
+ timeoutMs?: number;
44
+ }
45
+ declare function sharpTransforms(o: SharpTransformsOptions): ImageTransforms;
46
+
47
+ export { MAX_IMAGE_SIDE, type SharpTransformsOptions, sharpTransforms };
@@ -0,0 +1,70 @@
1
+ import "./chunk-2NTK7H4W.js";
2
+
3
+ // src/image-sharp.ts
4
+ var MAX_IMAGE_SIDE = 4096;
5
+ var FORMATS = ["webp", "avif", "jpeg", "png"];
6
+ var clamp = (v, lo, hi) => Math.min(hi, Math.max(lo, v));
7
+ function sharpTransforms(o) {
8
+ const g = globalThis;
9
+ if (typeof g.EdgeRuntime === "string" || g.process?.env?.NEXT_RUNTIME === "edge" || !g.process?.versions?.node) {
10
+ throw new Error('agent-cms: sharpTransforms() needs the Node.js runtime (sharp does not run on the Edge runtime). Move the CMS route to Node (`export const runtime = "nodejs"`) or leave `media.transforms` out there.');
11
+ }
12
+ const format = o.formats?.[0] ?? "webp";
13
+ for (const f of [format, ...o.formats ?? []]) if (!FORMATS.includes(f)) throw new Error(`agent-cms: sharpTransforms: unknown format "${f}" (use ${FORMATS.join(", ")})`);
14
+ const side = (name, which, v) => {
15
+ if (v === void 0) return;
16
+ if (!Number.isInteger(v) || v < 1 || v > MAX_IMAGE_SIDE) throw new Error(`agent-cms: sharpTransforms: size "${name}" has ${which} ${String(v)}; use a whole number from 1 to ${MAX_IMAGE_SIDE}`);
17
+ };
18
+ for (const [name, p] of Object.entries(o.presets)) {
19
+ if (!/^[a-z0-9][a-z0-9_-]{0,31}$/.test(name)) throw new Error(`agent-cms: sharpTransforms: "${name}" is not a size name (lowercase letters, digits, - and _)`);
20
+ side(name, "width", p.width);
21
+ side(name, "height", p.height);
22
+ if (!p.width && !p.height) throw new Error(`agent-cms: sharpTransforms: size "${name}" needs a width or a height`);
23
+ if (p.fit === "cover" && !(p.width && p.height)) throw new Error(`agent-cms: sharpTransforms: size "${name}" uses fit "cover", which needs both a width and a height`);
24
+ if (p.format && !FORMATS.includes(p.format)) throw new Error(`agent-cms: sharpTransforms: size "${name}" has the unknown format "${p.format}"`);
25
+ if (p.quality !== void 0 && !(p.quality >= 1 && p.quality <= 100)) throw new Error(`agent-cms: sharpTransforms: size "${name}" has quality ${p.quality}; use 1 to 100`);
26
+ }
27
+ const limitInputPixels = o.maxInputPixels ?? 1e8;
28
+ let lib = null;
29
+ const load = () => lib ??= import("sharp").then(
30
+ (m) => m.default ?? m,
31
+ (e) => {
32
+ lib = null;
33
+ throw new Error(`agent-cms: sharpTransforms() could not load "sharp" (npm install sharp): ${e instanceof Error ? e.message : String(e)}`);
34
+ }
35
+ );
36
+ return {
37
+ presets: { ...o.presets },
38
+ format,
39
+ maxInputBytes: o.maxInputBytes,
40
+ concurrency: o.concurrency,
41
+ timeoutMs: o.timeoutMs,
42
+ async make(bytes, job) {
43
+ const sharp = await load();
44
+ const meta = await sharp(bytes, { limitInputPixels }).metadata();
45
+ const turned = (meta.orientation ?? 1) >= 5;
46
+ const w = (turned ? meta.height : meta.width) ?? 0;
47
+ const h = (turned ? meta.width : meta.height) ?? 0;
48
+ if (!w || !h) throw new Error("not a picture sharp can read");
49
+ let img = sharp(bytes, { limitInputPixels }).rotate();
50
+ if (job.fit === "cover" && job.width && job.height) {
51
+ const k = Math.min(1, w / job.width, h / job.height);
52
+ const W = Math.max(1, Math.round(job.width * k));
53
+ const H = Math.max(1, Math.round(job.height * k));
54
+ const scale = Math.max(W / w, H / h);
55
+ const rw = Math.max(W, Math.round(w * scale));
56
+ const rh = Math.max(H, Math.round(h * scale));
57
+ const f = job.focal ?? { x: 0.5, y: 0.5 };
58
+ img = img.resize(rw, rh, { fit: "fill" }).extract({ left: clamp(Math.round(f.x * rw - W / 2), 0, rw - W), top: clamp(Math.round(f.y * rh - H / 2), 0, rh - H), width: W, height: H });
59
+ } else {
60
+ img = img.resize({ width: job.width ?? void 0, height: job.height ?? void 0, fit: "inside", withoutEnlargement: true });
61
+ }
62
+ const { data, info } = await img.toFormat(job.format, job.quality ? { quality: job.quality } : {}).toBuffer({ resolveWithObject: true });
63
+ return { bytes: new Uint8Array(data.buffer, data.byteOffset, data.byteLength), width: info.width, height: info.height };
64
+ }
65
+ };
66
+ }
67
+ export {
68
+ MAX_IMAGE_SIDE,
69
+ sharpTransforms
70
+ };