@tunasand/sdk 0.6.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 0.7.0
4
+
5
+ メディアの配信の URL の実体の ID を base32 にした(#776)
6
+
7
+ - 互換のない変更: 配信の URL は `<配信のオリジン>/images/<プロジェクト>/<実体>-<幅>x<高さ>.<拡張子>` の `<実体>` も URL のラベル(UUID の base32 の26文字)。UUID をそのまま書いた旧いパスは404(転送しない)。配信 API の `url` は新しい形で返るので、`url` を使うサイトは変更なし
8
+ - 互換のない変更: `mediaOf(base)` の `url` のない実体(手元の開発の CMS)の URL は `base/<ID のラベル>`(CMS の `/media/<ID>` も base32 のラベルだけを受ける)
9
+ - 追加: `mediaUrlOf(origin, { project, id, mimeType, width, height })`(`@tunasand/sdk/delivery`)。実体の ID(UUID)から配信の URL を作る。サイトで ID から URL を組み立てている所はこの関数に置き換える
10
+
3
11
  ## 0.6.0
4
12
 
5
13
  CLI を1つの bin にまとめた(#769)
package/README.md CHANGED
@@ -267,6 +267,16 @@ const html = bodyToHtml(record.body, {
267
267
  - 画像の変換のクエリ(`w` など)は署名の後に足してよい(署名の対象はパスと期限だけ)
268
268
  - 署名のない URL は404、期限切れと別の鍵の署名は403。鍵を作り直すと古い鍵の署名はすぐ通らない
269
269
 
270
+ ## 実体の ID から配信の URL(0.7.0 から)
271
+
272
+ 配信 API の `url` があればそれを使う。実体の ID(UUID)だけを持っているときは `mediaUrlOf` で作る(パスのプロジェクトと ID は base32 のラベル。自分で `/images/<UUID>` を組み立てない)
273
+
274
+ ```ts
275
+ import { mediaUrlOf } from '@tunasand/sdk/delivery';
276
+
277
+ const src = mediaUrlOf('https://cdn.tunasand.app', { project: PROJECT_ID, id: asset.id, mimeType: asset.mimeType, width: asset.width, height: asset.height });
278
+ ```
279
+
270
280
  ```ts
271
281
  import { signMediaUrl, withImageParams } from '@tunasand/sdk/delivery';
272
282
 
package/dist/app.js CHANGED
@@ -1,4 +1,4 @@
1
- import { g as JSON_BODY_MAX, h as ERROR_STATUS, n as errorBodyFor } from "./errorLabels-BcyRbv_i.js";
1
+ import { g as JSON_BODY_MAX, h as ERROR_STATUS, n as errorBodyFor } from "./errorLabels-CPKiPabZ.js";
2
2
  import { c as formatMessage, d as problemOf, f as problemText } from "./ja-C1HEWWTQ.js";
3
3
  import { i as CUSTOMER_STATUSES } from "./customers-C_vJY2_v.js";
4
4
  import { i as bodyProblemText, n as embedOfUrl } from "./embed-R-OvDYAw.js";
package/dist/delivery.js CHANGED
@@ -108,6 +108,24 @@ async function readChanges(client, since, apply, { limit } = {}) {
108
108
  if (!page.more) return cursor;
109
109
  }
110
110
  }
111
+ // ../cms-contract/src/urlLabel.ts
112
+ const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
113
+ /** URL のラベルの base32 の文字(Crockford の並びの小文字。紛らわしい i・l・o・u を使わず、UUID v7 の時刻の順を保つ) */
114
+ const BASE32 = "0123456789abcdefghjkmnpqrstvwxyz";
115
+ /**
116
+ * URL に出すプロジェクトか環境のラベル(決定は tunasand#717: URL に UUID を直接出さない)
117
+ * - UUID は base32 の26文字、UUID でない値はそのまま
118
+ */
119
+ function urlLabelOf(id) {
120
+ if (!UUID.test(id)) return id;
121
+ let n = BigInt(`0x${id.replaceAll("-", "")}`);
122
+ let out = "";
123
+ for (let i = 0; i < 26; i++) {
124
+ out = BASE32[Number(n & 31n)] + out;
125
+ n >>= 5n;
126
+ }
127
+ return out;
128
+ }
111
129
  // ../model/src/mediaUrl.ts
112
130
  /** 配信する形式と拡張子 */
113
131
  const EXTENSIONS = {
@@ -127,8 +145,20 @@ const EXTENSIONS = {
127
145
  };
128
146
  /** 形式の実体の種類 */
129
147
  const mediaKindOf = (mimeType) => mimeType.startsWith("image/") ? "image" : mimeType.startsWith("video/") ? "video" : "file";
148
+ /** 形式の拡張子(配信しない形式は null) */
149
+ const mediaExtOf = (mimeType) => EXTENSIONS[mimeType] ?? null;
130
150
  /** 拡張子の形式(配信しない拡張子は null) */
131
151
  const mediaMimeOf = (ext) => Object.entries(EXTENSIONS).find(([, e]) => e === ext)?.[0] ?? null;
152
+ const SEGMENT = /^[A-Za-z0-9_-]+$/;
153
+ /** 実体の ID の形か(CMS の /media と配信の Worker の共通、保存先のほかのキーを ID として読ませない) */
154
+ const isMediaId = (id) => SEGMENT.test(id);
155
+ /** 配信 URL のパス(プロジェクト・ID の形が不正、配信しない形式なら null) */
156
+ function mediaPathOf({ kind, project, id, mimeType, width, height }) {
157
+ const ext = mediaExtOf(mimeType);
158
+ if (!ext || !SEGMENT.test(project) || !isMediaId(id) || mediaKindOf(mimeType) !== kind) return null;
159
+ if (kind === "image") return `/images/${project}/${id}-${Math.round(width)}x${Math.round(height)}.${ext}`;
160
+ return kind === "video" ? `/videos/${project}/${id}.${ext}` : `/files/${project}/${id}.${ext}`;
161
+ }
132
162
  const IMAGE_PATH = /^\/images\/([A-Za-z0-9_-]+)\/([A-Za-z0-9_-]+)-(\d+)x(\d+)\.([a-z0-9]+)$/;
133
163
  const VIDEO_PATH = /^\/videos\/([A-Za-z0-9_-]+)\/([A-Za-z0-9_-]+)\.([a-z0-9]+)$/;
134
164
  const FILE_PATH = /^\/files\/([A-Za-z0-9_-]+)\/([A-Za-z0-9_-]+)\.([a-z0-9]+)$/;
@@ -258,17 +288,33 @@ function imageParamsOf(query) {
258
288
  };
259
289
  }
260
290
  // src/delivery/media.ts
291
+ /**
292
+ * 実体の ID から配信の URL を作る(origin は配信のオリジン、#776)
293
+ * - パスのプロジェクトと ID は URL のラベル(base32 の26文字)。配信 API の url と同じ値になる
294
+ * - 配信しない形式、ID の形が違うときは null。非公開の実体は signMediaUrl で署名してから使う
295
+ */
296
+ function mediaUrlOf(origin, { project, id, mimeType, width = 0, height = 0 }) {
297
+ const path = mediaPathOf({
298
+ kind: mediaKindOf(mimeType),
299
+ project: urlLabelOf(project),
300
+ id: urlLabelOf(id),
301
+ mimeType,
302
+ width,
303
+ height
304
+ });
305
+ return path ? `${origin.replace(/\/$/, "")}${path}` : null;
306
+ }
261
307
  /** 配信 URL の画像の切り抜き(クエリ rect、範囲なしは元の URL) */
262
308
  const deliveredImageUrl = (src, rect) => rect ? withImageParams(src, { rect }) : src;
263
309
  /**
264
310
  * 実体から表示の素材への変換(動画は代わりの画像も)
265
- * - URL: 実体の url、なしは base/<ID>(base は実体の URL の前半)
311
+ * - URL: 実体の url、なしは base/<ID のラベル>(base は実体の URL の前半、手元の CMS の /media。ID は base32 のラベル、#776)
266
312
  * - 画像の変換: imageUrl で画像(動画の実体は除く)を変換の URL に、url のある実体の既定は deliveredImageUrl、切り抜きは切り抜いた画像の URL・寸法・見せたい点
267
313
  * - url も imageUrl もない: 元の画像のまま(手元の開発)
268
314
  */
269
315
  function mediaOf(base, imageUrl) {
270
316
  const still = (a) => {
271
- const src = a.url ?? `${base}/${a.id}`;
317
+ const src = a.url ?? `${base}/${urlLabelOf(a.id)}`;
272
318
  const transform = imageUrl ?? (a.url ? deliveredImageUrl : void 0);
273
319
  const alt = a.alt ?? "";
274
320
  if (!transform || a.kind !== "image") return {
@@ -345,4 +391,4 @@ function createDeliveryClock(timeZone) {
345
391
  })
346
392
  };
347
393
  }
348
- export { DELIVERY_PATH, DeliveryError, cachedDelivery, createDeliveryClient, createDeliveryClock, deliveredImageUrl, fieldOf, imageParamsOf, limitOf, listParams, mediaOf, memoryDeliveryCache, optionLabels, pagePath, parseMediaPath, readChanges, signMediaUrl, subPath, withImageParams };
394
+ export { DELIVERY_PATH, DeliveryError, cachedDelivery, createDeliveryClient, createDeliveryClock, deliveredImageUrl, fieldOf, imageParamsOf, limitOf, listParams, mediaOf, mediaUrlOf, memoryDeliveryCache, optionLabels, pagePath, parseMediaPath, readChanges, signMediaUrl, subPath, withImageParams };
@@ -1283,7 +1283,7 @@ const ERROR_MESSAGES = {
1283
1283
  invalid_app_token: "アプリの資格が無効です。アプリの JWT かインストールのトークンを取り直してから送ってください。",
1284
1284
  api_key_forbidden: "{target, select, none {API キーにこの取得の権限がありません。} other {API キーに「{target}」を取得する権限がありません。}}設定の API キーで、キーの権限を見直してください。",
1285
1285
  account_not_found: "{name, select, none {} other {{name}}} のアカウントがありません。管理者にアカウントの登録を依頼してください。",
1286
- mcp_not_allowed: "MCP からは操作できません。スタジオから操作してください。",
1286
+ mcp_not_allowed: "MCP からは操作できません。ワークスペースから操作してください。",
1287
1287
  insufficient_scope: "AI の接続で許した範囲ではこの操作はできません。接続し直して{scope, select, content_publish {「公開まで」} other {必要な範囲}}を許してください。",
1288
1288
  admin_only: "管理者だけの操作です。必要なときは、管理者に依頼してください。",
1289
1289
  operator_read_only: "サポートのアクセスの許可が閲覧だけのため、変更できません。変更が要るときは、プロジェクトの管理者に許可の範囲の変更を依頼してください。",
package/dist/i18n.js CHANGED
@@ -1,4 +1,4 @@
1
1
  import { a as LOCALES, c as formatMessage, d as problemOf, f as problemText, i as BASE_LOCALE, n as BODY_PROBLEM_MESSAGES, o as chooseLocale, s as createTranslator, u as problemMessage } from "./ja-C1HEWWTQ.js";
2
- import { _ as BODY_PROBLEM_MESSAGES_EN, a as PROBLEM_MESSAGES, c as issueLabel, d as problemLabel, f as API_ISSUE_MESSAGES_EN, i as errorTitle, l as labelTableProblems, m as ERROR_MESSAGES_EN, o as PROBLEM_MESSAGES_EN, p as API_ISSUE_MESSAGES, r as errorMessageOf, s as importProblemLabel, t as ERROR_MESSAGES, u as problemEnglish, v as MODEL_PROBLEM_MESSAGES_EN } from "./errorLabels-BcyRbv_i.js";
2
+ import { _ as BODY_PROBLEM_MESSAGES_EN, a as PROBLEM_MESSAGES, c as issueLabel, d as problemLabel, f as API_ISSUE_MESSAGES_EN, i as errorTitle, l as labelTableProblems, m as ERROR_MESSAGES_EN, o as PROBLEM_MESSAGES_EN, p as API_ISSUE_MESSAGES, r as errorMessageOf, s as importProblemLabel, t as ERROR_MESSAGES, u as problemEnglish, v as MODEL_PROBLEM_MESSAGES_EN } from "./errorLabels-CPKiPabZ.js";
3
3
  import { t as MODEL_PROBLEM_MESSAGES } from "./ja-C6lX0CeJ.js";
4
4
  export { API_ISSUE_MESSAGES, API_ISSUE_MESSAGES_EN, BASE_LOCALE, BODY_PROBLEM_MESSAGES, BODY_PROBLEM_MESSAGES_EN, ERROR_MESSAGES, ERROR_MESSAGES_EN, LOCALES, MODEL_PROBLEM_MESSAGES, MODEL_PROBLEM_MESSAGES_EN, PROBLEM_MESSAGES, PROBLEM_MESSAGES_EN, chooseLocale, createTranslator, errorMessageOf, errorTitle, formatMessage, importProblemLabel, issueLabel, labelTableProblems, problemEnglish, problemLabel, problemMessage, problemOf, problemText };
@@ -56,7 +56,7 @@ export declare const ERROR_MESSAGES: {
56
56
  readonly invalid_app_token: "アプリの資格が無効です。アプリの JWT かインストールのトークンを取り直してから送ってください。";
57
57
  readonly api_key_forbidden: "{target, select, none {API キーにこの取得の権限がありません。} other {API キーに「{target}」を取得する権限がありません。}}設定の API キーで、キーの権限を見直してください。";
58
58
  readonly account_not_found: "{name, select, none {} other {{name}}} のアカウントがありません。管理者にアカウントの登録を依頼してください。";
59
- readonly mcp_not_allowed: "MCP からは操作できません。スタジオから操作してください。";
59
+ readonly mcp_not_allowed: "MCP からは操作できません。ワークスペースから操作してください。";
60
60
  readonly insufficient_scope: "AI の接続で許した範囲ではこの操作はできません。接続し直して{scope, select, content_publish {「公開まで」} other {必要な範囲}}を許してください。";
61
61
  readonly admin_only: "管理者だけの操作です。必要なときは、管理者に依頼してください。";
62
62
  readonly operator_read_only: "サポートのアクセスの許可が閲覧だけのため、変更できません。変更が要るときは、プロジェクトの管理者に許可の範囲の変更を依頼してください。";
@@ -0,0 +1,13 @@
1
+ /** UUID の base32 のラベルの形か */
2
+ export declare const isBase32Label: (value: string) => boolean;
3
+ /**
4
+ * URL に出すプロジェクトか環境のラベル(決定は tunasand#717: URL に UUID を直接出さない)
5
+ * - UUID は base32 の26文字、UUID でない値はそのまま
6
+ */
7
+ export declare function urlLabelOf(id: string): string;
8
+ /**
9
+ * URL のラベルから台帳を引く識別子(base32 のラベルは UUID の文字列、ラベルの形でない値はそのまま)
10
+ * - UUID をそのまま書いた値は null(正の URL はラベルの1つだけ。入口は見つからないと同じ応答にする)
11
+ * - 戻り値はブランドの型にしない(入口は台帳で引き直してレコードの値を使う)
12
+ */
13
+ export declare function idOfUrlLabel(label: string): string | null;
@@ -1,7 +1,7 @@
1
1
  export { cachedDelivery, memoryDeliveryCache, type DeliveryCache } from './cache.js';
2
2
  export { readChanges, type Change, type ChangesPage } from './changes.js';
3
3
  export { createDeliveryClient, DELIVERY_PATH, DeliveryError, subPath, type BundleResult, type Delivery, type DeliveryClient, type Prefetched } from './client.js';
4
- export { deliveredImageUrl, mediaOf, type ImageUrl, type MediaOf } from './media.js';
4
+ export { deliveredImageUrl, mediaOf, mediaUrlOf, type ImageUrl, type MediaOf, type MediaUrlInput } from './media.js';
5
5
  export { imageParamsOf, parseMediaPath, signMediaUrl, withImageParams, type DeliveredAsset, type ImageParams, type MediaPath } from '../../../model/src/index.js';
6
6
  export type { FieldPath, Selected } from './fields.js';
7
7
  export { listParams, type ListFilter, type ListQuery, type Page } from './query.js';
@@ -1,4 +1,18 @@
1
1
  import { type CropRect, type Media } from '../../../model/src/index.js';
2
+ /** 実体の配信の URL の材料(project はプロジェクトの ID、id は実体の ID で、どちらも UUID のまま渡す。width・height は画像の元の画素) */
3
+ export type MediaUrlInput = Readonly<{
4
+ project: string;
5
+ id: string;
6
+ mimeType: string;
7
+ width?: number;
8
+ height?: number;
9
+ }>;
10
+ /**
11
+ * 実体の ID から配信の URL を作る(origin は配信のオリジン、#776)
12
+ * - パスのプロジェクトと ID は URL のラベル(base32 の26文字)。配信 API の url と同じ値になる
13
+ * - 配信しない形式、ID の形が違うときは null。非公開の実体は signMediaUrl で署名してから使う
14
+ */
15
+ export declare function mediaUrlOf(origin: string, { project, id, mimeType, width, height }: MediaUrlInput): string | null;
2
16
  /** 配信する画像の URL(元の URL と、元画像の画素での切り抜きの範囲から、配信の側で変換する URL) */
3
17
  export type ImageUrl = (src: string, rect?: CropRect) => string;
4
18
  /** 配信 URL の画像の切り抜き(クエリ rect、範囲なしは元の URL) */
@@ -7,7 +21,7 @@ export declare const deliveredImageUrl: ImageUrl;
7
21
  export type MediaOf = (asset: unknown) => Media | undefined;
8
22
  /**
9
23
  * 実体から表示の素材への変換(動画は代わりの画像も)
10
- * - URL: 実体の url、なしは base/<ID>(base は実体の URL の前半)
24
+ * - URL: 実体の url、なしは base/<ID のラベル>(base は実体の URL の前半、手元の CMS の /media。ID は base32 のラベル、#776)
11
25
  * - 画像の変換: imageUrl で画像(動画の実体は除く)を変換の URL に、url のある実体の既定は deliveredImageUrl、切り抜きは切り抜いた画像の URL・寸法・見せたい点
12
26
  * - url も imageUrl もない: 元の画像のまま(手元の開発)
13
27
  */
@@ -1,3 +1,3 @@
1
- export { cachedDelivery, createDeliveryClient, createDeliveryClock, deliveredImageUrl, DELIVERY_PATH, DeliveryError, fieldOf, imageParamsOf, limitOf, listParams, mediaOf, memoryDeliveryCache, optionLabels, pagePath, parseMediaPath, readChanges, signMediaUrl, subPath, withImageParams } from './delivery/index.js';
1
+ export { cachedDelivery, createDeliveryClient, createDeliveryClock, deliveredImageUrl, mediaUrlOf, DELIVERY_PATH, DeliveryError, fieldOf, imageParamsOf, limitOf, listParams, mediaOf, memoryDeliveryCache, optionLabels, pagePath, parseMediaPath, readChanges, signMediaUrl, subPath, withImageParams } from './delivery/index.js';
2
2
  export type { Issue, IssueSeverity } from '../../cms-contract/src/errors.js';
3
- export type { BundleResult, Change, ChangesPage, DeliveredAsset, Delivery, DeliveryCache, DeliveryClient, DeliveryClock, FieldValue, ImageParams, ImageUrl, Item, ItemMeta, ItemOf, FieldPath, ListFilter, ListQuery, MediaOf, MediaPath, Page, Prefetched, RecordRef, Selected } from './delivery/index.js';
3
+ export type { BundleResult, Change, ChangesPage, DeliveredAsset, Delivery, DeliveryCache, DeliveryClient, DeliveryClock, FieldValue, ImageParams, ImageUrl, MediaUrlInput, Item, ItemMeta, ItemOf, FieldPath, ListFilter, ListQuery, MediaOf, MediaPath, Page, Prefetched, RecordRef, Selected } from './delivery/index.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tunasand/sdk",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "description": "tunasand の導入先のサイト向けの SDK(配信 API の取得・本文の型・プレビューの署名・手元の開発の配信 API)",
5
5
  "license": "UNLICENSED",
6
6
  "author": "RYUSENKEI",