@oim-media/player-oim 1.29.1 → 1.30.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/esm/index.js CHANGED
@@ -179,12 +179,159 @@ function getDeviceProfile(nav, win) {
179
179
  saveData: Boolean(n?.connection?.saveData)
180
180
  };
181
181
  }
182
+ // src/render/poster-head.ts
183
+ /**
184
+ * @license
185
+ * Copyright (c) 2026 Xue Studios LLC
186
+ * SPDX-License-Identifier: Apache-2.0
187
+ */
188
+ /**
189
+ * The start of a .oim, enough to show its poster. Since 15 Nov the backend
190
+ * writes the image, then a small OIM_Seal (the image hash signed on its own),
191
+ * then the metadata. Reading up to the seal is enough to verify and paint the
192
+ * poster; the metadata is fetched only if the work animates.
193
+ */
194
+ var SEAL_KEY = "OIM_Seal";
195
+ var DATA_KEYS = ["OIM_Data", "OIM_JSON_DATA"];
196
+ var IEND = new Uint8Array([
197
+ 0,
198
+ 0,
199
+ 0,
200
+ 0,
201
+ 73,
202
+ 69,
203
+ 78,
204
+ 68,
205
+ 174,
206
+ 66,
207
+ 96,
208
+ 130
209
+ ]);
210
+ function keyOf$1(data) {
211
+ const nul = data.indexOf(0);
212
+ return nul === -1 ? "" : new TextDecoder("latin1").decode(data.subarray(0, nul));
213
+ }
214
+ /** Scans complete chunks of `buf` (the file's first bytes) for the poster seal. */
215
+ function scanPosterHead(buf) {
216
+ const view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
217
+ const idat = [];
218
+ for (let at = 8; at + 8 <= buf.length;) {
219
+ const length = view.getUint32(at);
220
+ const end = at + 12 + length;
221
+ if (end > buf.length) return { done: false };
222
+ const type = new TextDecoder("latin1").decode(buf.subarray(at + 4, at + 8));
223
+ const data = buf.subarray(at + 8, at + 8 + length);
224
+ if (type === "IDAT") idat.push(data);
225
+ else if (type === "IEND") return {
226
+ done: true,
227
+ seal: null,
228
+ idat,
229
+ sealStart: at,
230
+ end
231
+ };
232
+ else if (type === "tEXt" || type === "zTXt" || type === "iTXt") {
233
+ const key = keyOf$1(data);
234
+ if (key === SEAL_KEY) {
235
+ let seal = null;
236
+ try {
237
+ seal = JSON.parse(new TextDecoder().decode(data.subarray(key.length + 1)));
238
+ } catch {
239
+ seal = null;
240
+ }
241
+ return {
242
+ done: true,
243
+ seal: seal && typeof seal === "object" ? seal : null,
244
+ idat,
245
+ sealStart: at,
246
+ end
247
+ };
248
+ }
249
+ if (DATA_KEYS.includes(key)) return {
250
+ done: true,
251
+ seal: null,
252
+ idat,
253
+ sealStart: at,
254
+ end
255
+ };
256
+ }
257
+ at = end;
258
+ }
259
+ return { done: false };
260
+ }
261
+ /** The poster as a standalone PNG: everything before the seal, then IEND. */
262
+ function posterPng(buf, sealStart) {
263
+ const out = new Uint8Array(sealStart + IEND.length);
264
+ out.set(buf.subarray(0, sealStart), 0);
265
+ out.set(IEND, sealStart);
266
+ return out;
267
+ }
182
268
  // src/render/file-cache.ts
183
269
  /**
184
270
  * @license
185
271
  * Copyright (c) 2026 Xue Studios LLC
186
272
  * SPDX-License-Identifier: Apache-2.0
187
273
  */
274
+ function joined(parts, total) {
275
+ const out = new Uint8Array(total);
276
+ let at = 0;
277
+ for (const p of parts) {
278
+ out.set(p, at);
279
+ at += p.length;
280
+ }
281
+ return out;
282
+ }
283
+ async function sha256Hex(parts) {
284
+ const all = joined(parts, parts.reduce((n, p) => n + p.length, 0));
285
+ const digest = new Uint8Array(await crypto.subtle.digest("SHA-256", all));
286
+ return Array.from(digest, (b) => b.toString(16).padStart(2, "0")).join("");
287
+ }
288
+ /** Reads the file until its poster seal. Without one, reads it all and caches it as the blob. */
289
+ async function readHead(src, e) {
290
+ const res = await fetch(src);
291
+ if (!res.ok) return null;
292
+ if (!res.body) {
293
+ const blob = await res.blob();
294
+ e.bytes = blob.size;
295
+ e.blob = Promise.resolve(blob);
296
+ return null;
297
+ }
298
+ const reader = res.body.getReader();
299
+ const parts = [];
300
+ let total = 0;
301
+ for (;;) {
302
+ const { value, done } = await reader.read();
303
+ if (value) {
304
+ parts.push(value);
305
+ total += value.length;
306
+ }
307
+ const buf = joined(parts, total);
308
+ const head = done ? {
309
+ done: true,
310
+ seal: null
311
+ } : scanPosterHead(buf);
312
+ if (head.done && head.seal && "idat" in head) {
313
+ await reader.cancel().catch(() => {});
314
+ e.head = buf;
315
+ return {
316
+ png: new Blob([posterPng(buf, head.sealStart)], { type: "image/png" }),
317
+ seal: head.seal,
318
+ imageHash: await sha256Hex(head.idat)
319
+ };
320
+ }
321
+ if (done || head.done) {
322
+ while (!done) {
323
+ const next = await reader.read();
324
+ if (next.done) break;
325
+ parts.push(next.value);
326
+ total += next.value.length;
327
+ }
328
+ const blob = new Blob([joined(parts, total)]);
329
+ e.bytes = blob.size;
330
+ e.blob = Promise.resolve(blob);
331
+ return null;
332
+ }
333
+ }
334
+ }
188
335
  var MAX_ENTRIES = 20;
189
336
  var MAX_BYTES = 52428800;
190
337
  var byUrl = /* @__PURE__ */ new Map();
@@ -259,10 +406,15 @@ var fileCache = {
259
406
  fetches++;
260
407
  e.pending++;
261
408
  evict();
262
- e.blob = fetch(src).then((res) => {
409
+ const head = e.head;
410
+ e.head = void 0;
411
+ e.blob = (head ? fetch(src, { headers: { Range: "bytes=" + head.length + "-" } }).then(async (res) => {
412
+ if (!res.ok) throw new Error("HTTP " + res.status);
413
+ return res.status === 206 ? new Blob([head, await res.blob()]) : res.blob();
414
+ }) : fetch(src).then((res) => {
263
415
  if (!res.ok) throw new Error("HTTP " + res.status);
264
416
  return res.blob();
265
- }).then((b) => {
417
+ })).then((b) => {
266
418
  e.bytes = b.size;
267
419
  e.lastUsed = ++tick;
268
420
  evict();
@@ -290,8 +442,27 @@ var fileCache = {
290
442
  }
291
443
  return e.verified;
292
444
  },
445
+ /** The verified-later poster from a partial read; null when the file has no seal or the read fails. */
446
+ readPoster(src) {
447
+ const e = entryFor(src);
448
+ if (!e.poster && e.blob) return Promise.resolve(null);
449
+ if (!e.poster) {
450
+ e.pending++;
451
+ e.poster = readHead(src, e).catch(() => null).finally(() => {
452
+ e.pending--;
453
+ });
454
+ }
455
+ return e.poster;
456
+ },
457
+ /** The first 16 bytes, from the partial read when there is one: never a second download. */
293
458
  sniff(src) {
294
- return fileCache.getBlob(src).then((b) => b.slice(0, 16).arrayBuffer()).then((buf) => new Uint8Array(buf)).catch(() => null);
459
+ const fromBlob = (b) => b.then((x) => x.slice(0, 16).arrayBuffer()).then((buf) => new Uint8Array(buf));
460
+ if (typeof src !== "string") return fromBlob(Promise.resolve(src)).catch(() => null);
461
+ return fileCache.readPoster(src).then(() => {
462
+ const e = entryFor(src);
463
+ if (e.head) return e.head.slice(0, 16);
464
+ return fromBlob(fileCache.getBlob(src));
465
+ }).catch(() => null);
295
466
  },
296
467
  stats() {
297
468
  return {
@@ -4449,6 +4620,47 @@ function validateOimMetadata(json) {
4449
4620
  return errors;
4450
4621
  }
4451
4622
  /**
4623
+ * The poster seal (OIM_Seal, spec 15 Nov): the image hash signed on its own,
4624
+ * read before the metadata so the poster can be verified from a partial read.
4625
+ * Same rules as a full file: an altered image is refused, an expired key says so.
4626
+ */
4627
+ async function verifyPosterSeal(seal, imageHash) {
4628
+ if (!seal || seal.v !== 1 || seal.alg !== "sha256-idat") return "invalid";
4629
+ if (typeof seal.image_hash !== "string" || typeof seal.signature !== "string" || typeof seal.key_version !== "string") return "invalid";
4630
+ if (!imageHash || seal.image_hash.toLowerCase() !== imageHash.toLowerCase()) return "invalid";
4631
+ const bytes = new TextEncoder().encode(JSON.stringify({
4632
+ alg: seal.alg,
4633
+ image_hash: seal.image_hash,
4634
+ key_version: seal.key_version,
4635
+ v: seal.v
4636
+ }));
4637
+ try {
4638
+ const raw = atob(seal.signature);
4639
+ const sig = new Uint8Array(raw.length);
4640
+ for (let i = 0; i < raw.length; i++) sig[i] = raw.charCodeAt(i);
4641
+ const check = async (jwk) => {
4642
+ const key = await crypto.subtle.importKey("jwk", jwk, {
4643
+ name: "ECDSA",
4644
+ namedCurve: "P-256"
4645
+ }, false, ["verify"]);
4646
+ return crypto.subtle.verify({
4647
+ name: "ECDSA",
4648
+ hash: { name: "SHA-256" }
4649
+ }, key, sig, bytes);
4650
+ };
4651
+ const jwk = await getPublicKey(seal.key_version);
4652
+ let valid = !!jwk && await check(jwk);
4653
+ if (!valid) {
4654
+ const serverJwk = await getServerPublicKey(seal.key_version).catch(() => null);
4655
+ if (serverJwk && (!jwk || serverJwk.x !== jwk.x || serverJwk.y !== jwk.y)) valid = await check(serverJwk);
4656
+ }
4657
+ if (!valid) return "invalid";
4658
+ } catch {
4659
+ return "invalid";
4660
+ }
4661
+ return await keyExpiredForFile({ key_version: seal.key_version }) ? "expired_key" : "ok";
4662
+ }
4663
+ /**
4452
4664
  * @license
4453
4665
  * Copyright (c) 2026 Xue Studios LLC
4454
4666
  * SPDX-License-Identifier: Apache-2.0
@@ -4511,7 +4723,7 @@ function nextWheelZoom(zoom, deltaY, deltaMode, pinch) {
4511
4723
  * the format can change without the player being touched. Tying both numbers
4512
4724
  * together would make an internal fix look like a format break.
4513
4725
  */
4514
- var CONTRACT_VERSION = "1.29.1";
4726
+ var CONTRACT_VERSION = "1.30.1";
4515
4727
  /**
4516
4728
  * What the player implements TODAY, measured and not assumed.
4517
4729
  *
@@ -5072,6 +5284,47 @@ function loadLottie() {
5072
5284
  return lib && lib.__oimExternal ? lib.load() : lib;
5073
5285
  });
5074
5286
  }
5287
+ // ../scene/src/legacy/features-entry.ts
5288
+ /**
5289
+ * @license
5290
+ * Copyright (c) 2026 Xue Studios LLC
5291
+ * SPDX-License-Identifier: Apache-2.0
5292
+ */
5293
+ /** Resolves when every part in `needs` can be called. Here they already can. */
5294
+ function ensureFeatures(_needs) {
5295
+ return Promise.resolve();
5296
+ }
5297
+ // ../scene/src/legacy/features.ts
5298
+ /**
5299
+ * @license
5300
+ * Copyright (c) 2026 Xue Studios LLC
5301
+ * SPDX-License-Identifier: Apache-2.0
5302
+ */
5303
+ var PATTERN_KEY = /^(fill|background)Pattern/;
5304
+ function featuresOf(doc) {
5305
+ const found = {
5306
+ generative: false,
5307
+ patterns: false
5308
+ };
5309
+ const seen = /* @__PURE__ */ new Set();
5310
+ const walk = (v) => {
5311
+ if (!v || typeof v !== "object" || seen.has(v) || found.generative && found.patterns) return;
5312
+ seen.add(v);
5313
+ if (Array.isArray(v)) {
5314
+ v.forEach(walk);
5315
+ return;
5316
+ }
5317
+ const o = v;
5318
+ if (o.kind === "generative" || o.fillType === "generative" || o.fillGenerative) found.generative = true;
5319
+ if (o.kind === "pattern" || o.fillType === "pattern" || "drawing" in o) found.patterns = true;
5320
+ for (const k in o) {
5321
+ if (PATTERN_KEY.test(k) && o[k] != null) found.patterns = true;
5322
+ walk(o[k]);
5323
+ }
5324
+ };
5325
+ walk(doc);
5326
+ return found;
5327
+ }
5075
5328
  // ../core/src/geoShapes.ts
5076
5329
  /**
5077
5330
  * @license
@@ -9559,11 +9812,16 @@ var LegacyRenderer = class {
9559
9812
  } else if (existingGrain) existingGrain.remove();
9560
9813
  const self = this;
9561
9814
  const slots = this.metadata.slots || this.metadata.elements || [];
9562
- try {
9563
- await ensureUpgrade();
9564
- } catch (err) {
9565
- console.warn("[OimPlayer] Could not load upgrade:", err);
9566
- }
9815
+ const report = (err) => {
9816
+ const message = String(err?.message ?? err);
9817
+ console.error("[OimPlayer] A part of the player did not load; the work is shown without it.", message);
9818
+ this.host.dispatchEvent(new CustomEvent("oim:error", { detail: {
9819
+ code: "chunk",
9820
+ message
9821
+ } }));
9822
+ };
9823
+ await ensureUpgrade().catch(report);
9824
+ await ensureFeatures(featuresOf([this.metadata, Array.from(v2LayersOf(this.metadata).values())])).catch(report);
9567
9825
  const v2Masks = v2LayersOf(self.metadata);
9568
9826
  slots.forEach(function(s) {
9569
9827
  const v2Mask = s && s.type === "mask" ? v2Masks.get(String(s.id)) : void 0;
@@ -11260,7 +11518,7 @@ var ImageEngine = class extends LegacyRenderer {
11260
11518
  host.classList.add("oim-align-" + this.align);
11261
11519
  host.classList.add("oim-tier-" + this._deviceProfile.tier);
11262
11520
  if (this._deviceProfile.isMobile) host.classList.add("oim-mobile");
11263
- shadow.innerHTML = "<div class=\"oim-player-viewport oim-fit-" + this.fit + "\" part=\"viewport\"><img class=\"oim-player-base\" crossorigin=\"anonymous\" src=\"" + this.imageUrl + "\" alt=\"\" aria-hidden=\"true\" part=\"base\" /><div class=\"oim-layer-overlay\" aria-hidden=\"true\" part=\"overlay\"></div></div>";
11521
+ shadow.innerHTML = "<div class=\"oim-player-viewport oim-fit-" + this.fit + "\" part=\"viewport\"><img class=\"oim-player-base\" crossorigin=\"anonymous\"" + (typeof src === "string" ? "" : " src=\"" + this.imageUrl + "\"") + " alt=\"\" aria-hidden=\"true\" part=\"base\" /><div class=\"oim-layer-overlay\" aria-hidden=\"true\" part=\"overlay\"></div></div>";
11264
11522
  injectStyles(shadow);
11265
11523
  this._viewport = shadow.querySelector(".oim-player-viewport");
11266
11524
  this._baseImg = shadow.querySelector(".oim-player-base");
@@ -11305,6 +11563,35 @@ var ImageEngine = class extends LegacyRenderer {
11305
11563
  this.updateViewportSize();
11306
11564
  this.updateScale();
11307
11565
  this._bindViewportEvents();
11566
+ if (typeof src === "string" && imgNode) {
11567
+ const poster = await fileCache.readPoster(src);
11568
+ if (poster) {
11569
+ const verdict = await verifyPosterSeal(poster.seal, poster.imageHash);
11570
+ if (verdict === "invalid") {
11571
+ shadow.innerHTML = "<div class=\"oim-player-error oim-player-refused\" part=\"error\"></div>";
11572
+ injectStyles(shadow);
11573
+ const box = shadow.querySelector(".oim-player-error");
11574
+ if (box) box.textContent = playerString("tampered", self.lang);
11575
+ host.dispatchEvent(new CustomEvent("error", { detail: new Error(ALTERED_FILE_ERROR) }));
11576
+ return false;
11577
+ }
11578
+ if (verdict === "expired_key") try {
11579
+ setFlag(host, "data-oim-key-retired", "data-oim-clave-retirada");
11580
+ } catch (e) {}
11581
+ this.imageUrl = URL.createObjectURL(poster.png);
11582
+ imgNode.src = this.imageUrl;
11583
+ if (this.poster === "only") {
11584
+ this.securityStatus = verdict === "ok" ? "verified" : "expired_key";
11585
+ viewportNode?.classList.add("oim-viewport--poster", "oim-viewport--ready");
11586
+ host.dispatchEvent(new CustomEvent("oim:loaded", { detail: {
11587
+ type: "image",
11588
+ metadata: null,
11589
+ security: this.securityStatus
11590
+ } }));
11591
+ return true;
11592
+ }
11593
+ } else imgNode.src = src;
11594
+ }
11308
11595
  return fileCache.getBlob(src).then((blob) => {
11309
11596
  return new Promise((resolve2, reject2) => {
11310
11597
  OIMRegistry.requestRender(self, blob, src, resolve2, reject2);