@appgrowstack/app-contract 0.0.0-stage → 0.1.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.
@@ -0,0 +1,301 @@
1
+ /**
2
+ * 面ごとの寸法・枚数・形式。App Store は iPhone 6.9インチと iPad 13インチを描画し、6.3インチは ASC が求める時だけ 6.9インチから縮めて作る。
3
+ * スクリーンショットは透明を持たないので品質95の JPEG。ヘッダーは PNG で出し、Containers で透明を外す
4
+ */
5
+ export const creativeSurfaces = {
6
+ screenshots: {
7
+ store: "app_store",
8
+ layouts: ["caption_top", "caption_bottom", "hero_full_ui", "panorama"],
9
+ sizes: { iphone_69: { width: 1320, height: 2868 }, iphone_63: { width: 1206, height: 2622 }, ipad_13: { width: 2064, height: 2752 } },
10
+ ratio: null,
11
+ min: 1,
12
+ max: 10,
13
+ format: "jpeg",
14
+ seconds: null,
15
+ },
16
+ app_preview: {
17
+ store: "app_store",
18
+ layouts: ["caption_overlay"],
19
+ sizes: { iphone_69: { width: 886, height: 1920 }, ipad_13: { width: 1200, height: 1600 } },
20
+ ratio: null,
21
+ min: 1,
22
+ max: 3,
23
+ format: "mp4",
24
+ seconds: { min: 15, max: 30 },
25
+ },
26
+ header: { store: "app_store", layouts: ["header"], sizes: {}, ratio: "3840:1646", min: 1, max: 1, format: "png", seconds: null },
27
+ search: { store: "app_store", layouts: ["search"], sizes: {}, ratio: "3:2", min: 1, max: 1, format: "png", seconds: null },
28
+ event_card: { store: "app_store", layouts: ["event_card"], sizes: {}, ratio: "16:9", min: 1, max: 1, format: "jpeg", seconds: null },
29
+ event_detail: { store: "app_store", layouts: ["event_detail"], sizes: {}, ratio: "9:16", min: 1, max: 1, format: "jpeg", seconds: null },
30
+ play_phone: {
31
+ store: "play",
32
+ layouts: ["play_phone"],
33
+ sizes: { android_phone: { width: 1080, height: 1920 } },
34
+ ratio: null,
35
+ min: 2,
36
+ max: 8,
37
+ format: "jpeg",
38
+ seconds: null,
39
+ },
40
+ feature_graphic: { store: "play", layouts: ["feature_graphic"], sizes: { android_phone: { width: 1024, height: 500 } }, ratio: null, min: 1, max: 1, format: "png", seconds: null },
41
+ };
42
+ /** 欄ごとの上限・数え方・審査(docs/design/creatives.md 11.1節)。name と description の Play は Google の審査 */
43
+ export const creativeFields = {
44
+ name: { stores: ["app_store", "play"], max: 30, count: "graphemes", review: "version" },
45
+ subtitle: { stores: ["app_store"], max: 30, count: "graphemes", review: "version" },
46
+ keywords: { stores: ["app_store"], max: 100, count: "bytes", review: "version" },
47
+ promotional_text: { stores: ["app_store"], max: 170, count: "graphemes", review: "none" },
48
+ description: { stores: ["app_store", "play"], max: 4000, count: "graphemes", review: "version" },
49
+ whats_new: { stores: ["app_store"], max: 4000, count: "graphemes", review: "version" },
50
+ short_description: { stores: ["play"], max: 80, count: "graphemes", review: "google" },
51
+ event_name: { stores: ["app_store"], max: 30, count: "graphemes", review: "event" },
52
+ event_short: { stores: ["app_store"], max: 50, count: "graphemes", review: "event" },
53
+ event_long: { stores: ["app_store"], max: 120, count: "graphemes", review: "event" },
54
+ };
55
+ // --- 撮影の組のファイル(docs/design/creatives.md 4節) ---------------------------
56
+ /**
57
+ * 撮影の組の1つのファイルの上限(バイト)。ファイルは core と creative の Worker を通して R2 に書くので、Workers の受けるリクエストの本文の上限
58
+ * (Free と Pro で 100MB)より小さくする。App Preview の元の録画(30秒まで)はこれに収まる。超えるファイルは分けて上げる形が要る(docs/plans/2026-11-22-creatives.md 2.5節)
59
+ */
60
+ export const CAPTURE_FILE_MAX_BYTES = 90 * 1024 * 1024;
61
+ /** 撮影の種類ごとのファイルの形式。撮った画面は PNG、録画は MP4 */
62
+ export const captureFormats = {
63
+ image: { ext: "png", contentType: "image/png" },
64
+ video: { ext: "mp4", contentType: "video/mp4" },
65
+ };
66
+ /** 撮影の組の1つのファイルの R2 のキー(docs/design/creatives.md 4.3節) */
67
+ export function captureR2Key(o) {
68
+ const f = o.file;
69
+ return `captures/${o.project}/${o.platform}/${o.version}/${o.set}/${f.locale}/${f.device}/${f.screen}.${captureFormats[f.kind].ext}`;
70
+ }
71
+ /** 1つのファイルを上げる core の道(putCaptureFile) */
72
+ export function captureFilePath(project, set, file) {
73
+ return `/v1/creatives/projects/${project}/captures/${set}/files/${file.locale}/${file.device}/${file.screen}/${file.kind}`;
74
+ }
75
+ /** 組の中のファイルの名前(誤りの文と CLI の表示に使う) */
76
+ export function captureFileName(file) {
77
+ return `${file.locale}/${file.device}/${file.screen}/${file.kind}`;
78
+ }
79
+ // --- フォント・端末の枠・背景の素材(docs/design/creatives.md 4.3節) -----------------
80
+ /** 1つの素材の上限(バイト)。可変のフォント(Noto Sans JP で約4MB)と端末の枠の PNG が収まる大きさ */
81
+ export const CREATIVE_ASSET_MAX_BYTES = 20 * 1024 * 1024;
82
+ /** フォントの family を R2 のキーの部分にする(Noto Sans JP → noto-sans-jp) */
83
+ export function fontSlug(family) {
84
+ return family
85
+ .toLowerCase()
86
+ .replace(/[^a-z0-9]+/g, "-")
87
+ .replace(/^-|-$/g, "");
88
+ }
89
+ /** 端末の枠の R2 のキー。色ごとに1枚 */
90
+ export function frameR2Key(device, color) {
91
+ return `frames/${device}/${color}.png`;
92
+ }
93
+ /**
94
+ * 素材の R2 のキー。フォントと端末の枠は全部の製品で同じ物を使うので製品を含めない。背景は製品ごと。
95
+ * meta の形が kind に合わなければ null
96
+ */
97
+ export function assetR2Key(project, r) {
98
+ if (r.kind === "font") {
99
+ const m = r.meta;
100
+ const slug = typeof m?.family === "string" ? fontSlug(m.family) : "";
101
+ return slug ? `fonts/${slug}/${r.name}` : null;
102
+ }
103
+ if (r.kind === "frame") {
104
+ const m = r.meta;
105
+ return typeof m?.device === "string" && typeof m.color === "string" && /^[a-z][a-z0-9_]{0,39}$/.test(m.color) ? frameR2Key(m.device, m.color) : null;
106
+ }
107
+ return `art/${project}/${r.name}`;
108
+ }
109
+ /** 1つの素材を上げる core の道(putAssetFile) */
110
+ export function assetFilePath(project, asset) {
111
+ return `/v1/creatives/projects/${project}/assets/${asset}`;
112
+ }
113
+ /** PNG の頭(IHDR)から寸法を読む。PNG でなければ null。頭の24バイトがあればよい */
114
+ export function pngSize(head) {
115
+ const sig = [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a];
116
+ if (head.length < 24 || sig.some((b, i) => head[i] !== b))
117
+ return null;
118
+ if (String.fromCharCode(head[12], head[13], head[14], head[15]) !== "IHDR")
119
+ return null;
120
+ const v = new DataView(head.buffer, head.byteOffset, head.byteLength);
121
+ return { width: v.getUint32(16), height: v.getUint32(20) };
122
+ }
123
+ const MOOV_MAX_BYTES = 16 * 1024 * 1024;
124
+ function boxesIn(buf, from, to) {
125
+ const v = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
126
+ const out = [];
127
+ let at = from;
128
+ while (at + 8 <= to) {
129
+ let size = v.getUint32(at);
130
+ const type = String.fromCharCode(buf[at + 4], buf[at + 5], buf[at + 6], buf[at + 7]);
131
+ let head = 8;
132
+ if (size === 1) {
133
+ if (at + 16 > to)
134
+ break;
135
+ size = Number(v.getBigUint64(at + 8));
136
+ head = 16;
137
+ }
138
+ else if (size === 0)
139
+ size = to - at;
140
+ if (size < head || at + size > to)
141
+ break;
142
+ out.push({ type, start: at + head, end: at + size });
143
+ at += size;
144
+ }
145
+ return out;
146
+ }
147
+ /**
148
+ * MP4 の寸法と長さを読む。いちばん上の箱を頭だけ読んで moov を探し、moov の中の mvhd(長さ)と、幅の0でない tkhd(映像の寸法)を読む。
149
+ * MP4 として読めなければ null
150
+ */
151
+ export async function mp4Info(read, size) {
152
+ let at = 0;
153
+ let moov = null;
154
+ while (at + 8 <= size) {
155
+ const head = await read(at, Math.min(16, size - at));
156
+ if (head.length < 8)
157
+ return null;
158
+ const v = new DataView(head.buffer, head.byteOffset, head.byteLength);
159
+ let boxSize = v.getUint32(0);
160
+ const type = String.fromCharCode(head[4], head[5], head[6], head[7]);
161
+ let headLen = 8;
162
+ if (boxSize === 1) {
163
+ if (head.length < 16)
164
+ return null;
165
+ boxSize = Number(v.getBigUint64(8));
166
+ headLen = 16;
167
+ }
168
+ else if (boxSize === 0)
169
+ boxSize = size - at;
170
+ if (boxSize < headLen || at + boxSize > size)
171
+ return null;
172
+ if (type === "moov") {
173
+ if (boxSize - headLen > MOOV_MAX_BYTES)
174
+ return null;
175
+ moov = await read(at + headLen, boxSize - headLen);
176
+ break;
177
+ }
178
+ at += boxSize;
179
+ }
180
+ if (!moov)
181
+ return null;
182
+ const dv = new DataView(moov.buffer, moov.byteOffset, moov.byteLength);
183
+ let duration_ms = null;
184
+ let dims = null;
185
+ for (const b of boxesIn(moov, 0, moov.length)) {
186
+ if (b.type === "mvhd" && b.end - b.start >= 20) {
187
+ const version = moov[b.start];
188
+ const timescale = version === 1 ? dv.getUint32(b.start + 20) : dv.getUint32(b.start + 12);
189
+ const duration = version === 1 ? Number(dv.getBigUint64(b.start + 24)) : dv.getUint32(b.start + 16);
190
+ if (timescale > 0)
191
+ duration_ms = Math.round((duration / timescale) * 1000);
192
+ }
193
+ if (b.type === "trak" && !dims) {
194
+ const tkhd = boxesIn(moov, b.start, b.end).find((x) => x.type === "tkhd");
195
+ if (!tkhd)
196
+ continue;
197
+ const off = moov[tkhd.start] === 1 ? 88 : 76;
198
+ if (tkhd.end - tkhd.start < off + 8)
199
+ continue;
200
+ const width = dv.getUint32(tkhd.start + off) >>> 16;
201
+ const height = dv.getUint32(tkhd.start + off + 4) >>> 16;
202
+ if (width > 0 && height > 0)
203
+ dims = { width, height };
204
+ }
205
+ }
206
+ return dims && duration_ms !== null ? { ...dims, duration_ms } : null;
207
+ }
208
+ // --- 参考の素材(docs/design/creatives.md 15節) --------------------------------
209
+ /** 参考の素材の1つのファイルの上限(バイト)。撮影の組と同じく core と creative の Worker を通して R2 に書くので、同じ 90MB */
210
+ export const REFERENCE_FILE_MAX_BYTES = CAPTURE_FILE_MAX_BYTES;
211
+ /** 参考の素材のファイルの R2 のキー。同じ sha256 の物は1回だけ置く。バケットは公開の URL を持たない */
212
+ export function referenceR2Key(sha256) {
213
+ return `refs/${sha256}`;
214
+ }
215
+ /** 参考の素材の1つのファイルを送る・読む core のパス(putReferenceFile・getReferenceFile) */
216
+ export function referenceFilePath(reference, sha256) {
217
+ return `/v1/creatives/references/${reference}/files/${sha256}`;
218
+ }
219
+ /** JPEG の SOF から寸法を読む。JPEG でなければ null */
220
+ export function jpegSize(bytes) {
221
+ if (bytes.length < 4 || bytes[0] !== 0xff || bytes[1] !== 0xd8)
222
+ return null;
223
+ let at = 2;
224
+ while (at + 9 < bytes.length) {
225
+ if (bytes[at] !== 0xff)
226
+ return null;
227
+ const marker = bytes[at + 1];
228
+ if (marker === 0xff) {
229
+ at++;
230
+ continue;
231
+ }
232
+ if (marker === 0xd8 || marker === 0x01 || (marker >= 0xd0 && marker <= 0xd7)) {
233
+ at += 2;
234
+ continue;
235
+ }
236
+ const length = (bytes[at + 2] << 8) | bytes[at + 3];
237
+ // SOF0〜SOF15。DHT(c4)・JPG(c8)・DAC(cc)は寸法を持たない
238
+ if (marker >= 0xc0 && marker <= 0xcf && marker !== 0xc4 && marker !== 0xc8 && marker !== 0xcc) {
239
+ const height = (bytes[at + 5] << 8) | bytes[at + 6];
240
+ const width = (bytes[at + 7] << 8) | bytes[at + 8];
241
+ return width > 0 && height > 0 ? { width, height } : null;
242
+ }
243
+ if (marker === 0xda)
244
+ return null;
245
+ at += 2 + length;
246
+ }
247
+ return null;
248
+ }
249
+ // --- App Store のページ(ADR 0035) ---------------------------------------------
250
+ // Apple は Worker からの apps.apple.com と iTunes の lookup の読みを 403 で断る。読むのは手元の CLI(creatives refs add・creatives watch)で、
251
+ // creative の Worker は App Store のページの URL を受けない。CLI と Worker が同じ見分け方を使うので、ここに置く
252
+ /** App Store のアプリのページの国(大文字)と id。違えば null */
253
+ export function appStorePage(url) {
254
+ if (url.hostname !== "apps.apple.com")
255
+ return null;
256
+ const m = /^\/(?:([a-z]{2})\/)?app\/(?:[^/]+\/)?id(\d+)/.exec(url.pathname);
257
+ return m ? { country: (m[1] ?? "us").toUpperCase(), id: m[2] } : null;
258
+ }
259
+ /**
260
+ * 国の店の言語(lookup の lang)。タイの店は th と英語の2つを持ち、lang が無いと英語(ごほうび習慣では en-GB)の組を返す(2026-11-23 に確かめた)。
261
+ * 端末の言語がその国の言葉の人が見る組を読むため、国ごとに決めて渡す。無い国は lang を付けない
262
+ */
263
+ export const APP_STORE_LANG = {
264
+ JP: "ja_jp",
265
+ US: "en_us",
266
+ GB: "en_gb",
267
+ CN: "zh_cn",
268
+ TW: "zh_tw",
269
+ HK: "zh_hk",
270
+ KR: "ko_kr",
271
+ BR: "pt_br",
272
+ DE: "de_de",
273
+ FR: "fr_fr",
274
+ ES: "es_es",
275
+ MX: "es_mx",
276
+ IT: "it_it",
277
+ TH: "th_th",
278
+ VN: "vi_vn",
279
+ ID: "id_id",
280
+ };
281
+ /** iTunes の lookup の URL。複数の id を , でつなげて1回で読める */
282
+ export function appStoreLookupUrl(ids, country, lang = APP_STORE_LANG[country.toUpperCase()] ?? null) {
283
+ return `https://itunes.apple.com/lookup?id=${ids.join(",")}&country=${country.toLowerCase()}&entity=software${lang ? `&lang=${lang}` : ""}`;
284
+ }
285
+ /**
286
+ * iTunes の lookup の画像の URL を元の寸法の URL にする。パスの最後の `<幅>x<高さ>bb.<拡張子>` が縮小の寸法で、
287
+ * 大きな箱(3000x3000bb)を渡すと元の寸法(iPhone 6.9インチなら 1320×2868)で返る(2026-11-23 に確かめた)。形が違えば null
288
+ */
289
+ export function fullSizeStoreImage(url) {
290
+ const m = /^(https:\/\/[a-z0-9-]+\.mzstatic\.com\/image\/thumb\/.+)\/\d+x\d+bb\.(?:jpg|png|webp)$/.exec(url);
291
+ return m ? `${m[1]}/3000x3000bb.jpg` : null;
292
+ }
293
+ /**
294
+ * ページが変わったかを見る値。枚の URL の並び・説明・バージョンをつないだ文の sha256。
295
+ * 同じ値なら残さない(docs/design/creatives.md 16節)
296
+ */
297
+ export async function appStoreFingerprint(l) {
298
+ const text = JSON.stringify([l.version ?? "", l.description ?? "", [...l.screenshotUrls], [...l.ipadScreenshotUrls]]);
299
+ const digest = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(text));
300
+ return [...new Uint8Array(digest)].map((b) => b.toString(16).padStart(2, "0")).join("");
301
+ }