@kehto/paja 0.24.3 → 0.24.5
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/README.md +17 -5
- package/dist/browser-host.js +945 -329
- package/dist/browser-host.js.map +1 -1
- package/package.json +7 -7
package/dist/browser-host.js
CHANGED
|
@@ -5408,8 +5408,8 @@ function handleMediaMessage(context, windowId, msg) {
|
|
|
5408
5408
|
hooks.sendToNapplet(windowId, {
|
|
5409
5409
|
type: "media.session.create.result",
|
|
5410
5410
|
id: m.id ?? "",
|
|
5411
|
-
|
|
5412
|
-
|
|
5411
|
+
owner: m.owner,
|
|
5412
|
+
error: "media service unavailable"
|
|
5413
5413
|
});
|
|
5414
5414
|
}
|
|
5415
5415
|
}
|
|
@@ -5844,11 +5844,15 @@ function createRuntime(hooks) {
|
|
|
5844
5844
|
});
|
|
5845
5845
|
let incRuntime = null;
|
|
5846
5846
|
const aclState = createAclState(hooks.aclPersistence, "permissive", (mutation) => {
|
|
5847
|
-
if (mutation.type === "revoke" && mutation.capability !== "relay:read") return;
|
|
5848
5847
|
for (const entry of sessionRegistry2.getAllEntries()) {
|
|
5849
5848
|
if (entry.dTag === mutation.dTag && entry.aggregateHash === mutation.aggregateHash) {
|
|
5850
|
-
|
|
5851
|
-
|
|
5849
|
+
for (const service of Object.values(serviceRegistry)) {
|
|
5850
|
+
service.onCapabilityRevoked?.(entry.windowId, mutation.type === "block" ? null : mutation.capability);
|
|
5851
|
+
}
|
|
5852
|
+
if (mutation.type === "block" || mutation.capability === "relay:read") {
|
|
5853
|
+
revokeRelaySubscriptions({ hooks, serviceRegistry, subscriptions }, entry.windowId);
|
|
5854
|
+
incRuntime?.revokeWindow(entry.windowId);
|
|
5855
|
+
}
|
|
5852
5856
|
}
|
|
5853
5857
|
}
|
|
5854
5858
|
});
|
|
@@ -8270,7 +8274,30 @@ function createKeysService(options = {}) {
|
|
|
8270
8274
|
}
|
|
8271
8275
|
};
|
|
8272
8276
|
}
|
|
8273
|
-
|
|
8277
|
+
function validMediaCommand(command, actions) {
|
|
8278
|
+
if (!actions.includes(command.action)) return false;
|
|
8279
|
+
if (command.action !== "seek" && command.action !== "volume") return true;
|
|
8280
|
+
return typeof command.value === "number" && Number.isFinite(command.value) && command.value >= 0 && (command.action !== "volume" || command.value <= 1);
|
|
8281
|
+
}
|
|
8282
|
+
function silentAudioDataUrl() {
|
|
8283
|
+
const samples = 4e3;
|
|
8284
|
+
const bytes = new Uint8Array(44 + samples * 2);
|
|
8285
|
+
const view = new DataView(bytes.buffer);
|
|
8286
|
+
bytes.set(new TextEncoder().encode("RIFF"), 0);
|
|
8287
|
+
view.setUint32(4, bytes.length - 8, true);
|
|
8288
|
+
bytes.set(new TextEncoder().encode("WAVEfmt "), 8);
|
|
8289
|
+
view.setUint32(16, 16, true);
|
|
8290
|
+
view.setUint16(20, 1, true);
|
|
8291
|
+
view.setUint16(22, 1, true);
|
|
8292
|
+
view.setUint32(24, samples, true);
|
|
8293
|
+
view.setUint32(28, samples * 2, true);
|
|
8294
|
+
view.setUint16(32, 2, true);
|
|
8295
|
+
view.setUint16(34, 16, true);
|
|
8296
|
+
bytes.set(new TextEncoder().encode("data"), 36);
|
|
8297
|
+
view.setUint32(40, samples * 2, true);
|
|
8298
|
+
return `data:audio/wav;base64,${btoa(String.fromCharCode(...bytes))}`;
|
|
8299
|
+
}
|
|
8300
|
+
var SILENT_AUDIO_DATA_URL = silentAudioDataUrl();
|
|
8274
8301
|
var DEFAULT_MEDIA_ACTIONS = ["play", "pause", "next", "prev", "seek"];
|
|
8275
8302
|
var ACTION_MATRIX = [
|
|
8276
8303
|
["play", "play"],
|
|
@@ -8284,7 +8311,7 @@ function createBrowserMediaBridge(opts = {}) {
|
|
|
8284
8311
|
const doc = opts.documentTarget !== void 0 ? opts.documentTarget : typeof document !== "undefined" ? document : null;
|
|
8285
8312
|
let silentAudioEl = null;
|
|
8286
8313
|
let activeSessionId = null;
|
|
8287
|
-
|
|
8314
|
+
const sessionsActive = /* @__PURE__ */ new Set();
|
|
8288
8315
|
const actionCallbacks = /* @__PURE__ */ new Set();
|
|
8289
8316
|
function primeSilentAudio() {
|
|
8290
8317
|
if (silentAudioEl || !doc) return;
|
|
@@ -8320,13 +8347,16 @@ function createBrowserMediaBridge(opts = {}) {
|
|
|
8320
8347
|
}
|
|
8321
8348
|
continue;
|
|
8322
8349
|
}
|
|
8323
|
-
|
|
8324
|
-
|
|
8325
|
-
|
|
8326
|
-
|
|
8327
|
-
|
|
8328
|
-
|
|
8329
|
-
|
|
8350
|
+
try {
|
|
8351
|
+
ms.setActionHandler(domAction, (details) => {
|
|
8352
|
+
if (!activeSessionId) return;
|
|
8353
|
+
const value = napAction === "seek" && typeof details?.seekTime === "number" ? details.seekTime : void 0;
|
|
8354
|
+
for (const cb of actionCallbacks) {
|
|
8355
|
+
cb(activeSessionId, napAction, value);
|
|
8356
|
+
}
|
|
8357
|
+
});
|
|
8358
|
+
} catch {
|
|
8359
|
+
}
|
|
8330
8360
|
}
|
|
8331
8361
|
}
|
|
8332
8362
|
function writeMetadata(metadata) {
|
|
@@ -8378,256 +8408,239 @@ function createBrowserMediaBridge(opts = {}) {
|
|
|
8378
8408
|
}
|
|
8379
8409
|
return;
|
|
8380
8410
|
}
|
|
8381
|
-
if (sessionsActive === 0)
|
|
8382
|
-
|
|
8383
|
-
sessionsActive = 1;
|
|
8384
|
-
}
|
|
8411
|
+
if (sessionsActive.size === 0) primeSilentAudio();
|
|
8412
|
+
sessionsActive.add(sessionId);
|
|
8385
8413
|
applyActionHandlers(actions ?? DEFAULT_MEDIA_ACTIONS);
|
|
8386
8414
|
},
|
|
8387
|
-
destroySession(
|
|
8388
|
-
sessionsActive
|
|
8389
|
-
if (sessionsActive === 0) teardownSilentAudio();
|
|
8415
|
+
destroySession(sessionId) {
|
|
8416
|
+
sessionsActive.delete(sessionId);
|
|
8417
|
+
if (sessionsActive.size === 0) teardownSilentAudio();
|
|
8390
8418
|
}
|
|
8391
8419
|
};
|
|
8392
8420
|
}
|
|
8393
|
-
var MEDIA_SERVICE_VERSION = "1.
|
|
8394
|
-
|
|
8395
|
-
|
|
8396
|
-
|
|
8397
|
-
|
|
8398
|
-
sessionRegistry: /* @__PURE__ */ new Map(),
|
|
8399
|
-
windowSessions: /* @__PURE__ */ new Map(),
|
|
8400
|
-
sendHandles: /* @__PURE__ */ new Map(),
|
|
8401
|
-
activeSessionId: null,
|
|
8402
|
-
touchCounter: 0,
|
|
8403
|
-
sessionCounter: 0
|
|
8404
|
-
};
|
|
8405
|
-
}
|
|
8406
|
-
function setActive(state, sessionId, actions) {
|
|
8407
|
-
state.activeSessionId = sessionId;
|
|
8408
|
-
state.bridge.setActiveSession?.(sessionId, actions);
|
|
8409
|
-
if (!sessionId) return;
|
|
8410
|
-
const entry = state.sessionRegistry.get(sessionId);
|
|
8411
|
-
if (!entry) return;
|
|
8412
|
-
if (entry.metadata) state.bridge.setMetadata(sessionId, entry.metadata);
|
|
8413
|
-
if (entry.state) state.bridge.setPlaybackState(sessionId, entry.state.status);
|
|
8414
|
-
}
|
|
8415
|
-
function promoteNextActiveOrClear(state) {
|
|
8416
|
-
if (state.sessionRegistry.size === 0) {
|
|
8417
|
-
setActive(state, null);
|
|
8418
|
-
return;
|
|
8419
|
-
}
|
|
8420
|
-
let latest = null;
|
|
8421
|
-
for (const entry of state.sessionRegistry.values()) {
|
|
8422
|
-
if (!latest || entry.lastTouched > latest.lastTouched) latest = entry;
|
|
8423
|
-
}
|
|
8424
|
-
setActive(state, latest ? latest.sessionId : null, latest?.actions);
|
|
8425
|
-
}
|
|
8426
|
-
function sendMediaCommand(state, sessionId, action, value) {
|
|
8427
|
-
const entry = state.sessionRegistry.get(sessionId);
|
|
8428
|
-
if (!entry) return;
|
|
8429
|
-
const send = state.sendHandles.get(entry.windowId);
|
|
8430
|
-
if (!send) return;
|
|
8431
|
-
const payload = {
|
|
8432
|
-
type: "media.command",
|
|
8433
|
-
sessionId,
|
|
8434
|
-
action,
|
|
8435
|
-
...typeof value === "number" ? { value } : {}
|
|
8436
|
-
};
|
|
8437
|
-
send(payload);
|
|
8438
|
-
}
|
|
8439
|
-
function registerWindowSession(state, windowId, sessionId) {
|
|
8440
|
-
if (!state.windowSessions.has(windowId)) state.windowSessions.set(windowId, /* @__PURE__ */ new Set());
|
|
8441
|
-
state.windowSessions.get(windowId).add(sessionId);
|
|
8442
|
-
}
|
|
8443
|
-
function sendSessionCreateResult(send, id, fields) {
|
|
8444
|
-
send({
|
|
8445
|
-
type: "media.session.create.result",
|
|
8446
|
-
id: id ?? "",
|
|
8447
|
-
...fields
|
|
8448
|
-
});
|
|
8449
|
-
}
|
|
8450
|
-
function isMediaPlaybackOwner(value) {
|
|
8451
|
-
return value === "shell" || value === "napplet";
|
|
8452
|
-
}
|
|
8453
|
-
function hasSourceRef(source) {
|
|
8454
|
-
if (!source) return false;
|
|
8455
|
-
if (typeof source.url === "string" && source.url.length > 0) return true;
|
|
8456
|
-
if (typeof source.blossomHash === "string" && source.blossomHash.length > 0) return true;
|
|
8457
|
-
if (source.nostr) {
|
|
8458
|
-
return Boolean(source.nostr.eventId || source.nostr.address);
|
|
8459
|
-
}
|
|
8460
|
-
return false;
|
|
8461
|
-
}
|
|
8462
|
-
function canonicalizeSessionId(state, windowId, preferredSessionId) {
|
|
8463
|
-
const trimmed = typeof preferredSessionId === "string" ? preferredSessionId.trim() : "";
|
|
8464
|
-
const hint2 = trimmed || `session-${++state.sessionCounter}`;
|
|
8465
|
-
if (!state.sessionRegistry.has(hint2)) return hint2;
|
|
8466
|
-
let next;
|
|
8467
|
-
do {
|
|
8468
|
-
next = `${windowId}:${hint2}:${++state.sessionCounter}`;
|
|
8469
|
-
} while (state.sessionRegistry.has(next));
|
|
8470
|
-
return next;
|
|
8471
|
-
}
|
|
8472
|
-
function handleSessionCreate(state, windowId, message2, send) {
|
|
8473
|
-
if (!isMediaPlaybackOwner(message2.owner)) {
|
|
8474
|
-
sendSessionCreateResult(send, message2.id, { error: "missing owner" });
|
|
8475
|
-
return;
|
|
8476
|
-
}
|
|
8477
|
-
if (message2.owner === "shell") {
|
|
8478
|
-
if (!hasSourceRef(message2.source)) {
|
|
8479
|
-
sendSessionCreateResult(send, message2.id, { owner: "shell", error: "missing source" });
|
|
8480
|
-
return;
|
|
8481
|
-
}
|
|
8482
|
-
sendSessionCreateResult(send, message2.id, { owner: "shell", error: "unsupported owner mode" });
|
|
8483
|
-
return;
|
|
8484
|
-
}
|
|
8485
|
-
state.sendHandles.set(windowId, send);
|
|
8486
|
-
const sessionId = canonicalizeSessionId(state, windowId, message2.sessionId);
|
|
8487
|
-
const entry = {
|
|
8488
|
-
sessionId,
|
|
8489
|
-
windowId,
|
|
8490
|
-
owner: message2.owner,
|
|
8491
|
-
source: message2.source,
|
|
8492
|
-
metadata: message2.metadata,
|
|
8493
|
-
state: void 0,
|
|
8494
|
-
actions: message2.capabilities ?? DEFAULT_MEDIA_ACTIONS,
|
|
8495
|
-
lastTouched: ++state.touchCounter
|
|
8496
|
-
};
|
|
8497
|
-
state.sessionRegistry.set(sessionId, entry);
|
|
8498
|
-
registerWindowSession(state, windowId, sessionId);
|
|
8499
|
-
setActive(state, sessionId, entry.actions);
|
|
8500
|
-
state.options.onSessionCreate?.(windowId, sessionId, message2.metadata);
|
|
8501
|
-
sendSessionCreateResult(send, message2.id, { sessionId, owner: message2.owner });
|
|
8502
|
-
}
|
|
8503
|
-
function handleSessionUpdate(state, windowId, message2) {
|
|
8504
|
-
const entry = state.sessionRegistry.get(message2.sessionId);
|
|
8505
|
-
if (entry) {
|
|
8506
|
-
entry.metadata = { ...entry.metadata, ...message2.metadata };
|
|
8507
|
-
entry.lastTouched = ++state.touchCounter;
|
|
8508
|
-
if (entry.owner === "napplet" && message2.sessionId === state.activeSessionId && entry.metadata) {
|
|
8509
|
-
state.bridge.setMetadata(message2.sessionId, entry.metadata);
|
|
8510
|
-
}
|
|
8421
|
+
var MEDIA_SERVICE_VERSION = "1.2.0";
|
|
8422
|
+
var MediaRegistry = class {
|
|
8423
|
+
constructor(options, bridge) {
|
|
8424
|
+
this.options = options;
|
|
8425
|
+
this.bridge = bridge;
|
|
8511
8426
|
}
|
|
8512
|
-
|
|
8513
|
-
|
|
8514
|
-
|
|
8515
|
-
|
|
8516
|
-
|
|
8517
|
-
|
|
8518
|
-
|
|
8519
|
-
|
|
8520
|
-
|
|
8521
|
-
|
|
8522
|
-
|
|
8523
|
-
|
|
8524
|
-
|
|
8525
|
-
|
|
8526
|
-
|
|
8527
|
-
}
|
|
8528
|
-
|
|
8529
|
-
|
|
8530
|
-
|
|
8531
|
-
|
|
8532
|
-
|
|
8533
|
-
|
|
8534
|
-
|
|
8535
|
-
|
|
8427
|
+
options;
|
|
8428
|
+
bridge;
|
|
8429
|
+
sessions = /* @__PURE__ */ new Map();
|
|
8430
|
+
pending = /* @__PURE__ */ new Map();
|
|
8431
|
+
// Kept until endpoint teardown: retired identifiers must never be reused.
|
|
8432
|
+
used = /* @__PURE__ */ new Map();
|
|
8433
|
+
listeners = /* @__PURE__ */ new Set();
|
|
8434
|
+
active = null;
|
|
8435
|
+
counter = 0;
|
|
8436
|
+
destroyed = false;
|
|
8437
|
+
snapshots() {
|
|
8438
|
+
return [...this.sessions.values()].map(({ send: _send, playback: _playback, ...snapshot }) => structuredClone(snapshot));
|
|
8439
|
+
}
|
|
8440
|
+
changed() {
|
|
8441
|
+
for (const listener of this.listeners) listener(this.snapshots());
|
|
8442
|
+
}
|
|
8443
|
+
activate(sessionId) {
|
|
8444
|
+
this.active = sessionId;
|
|
8445
|
+
const entry = sessionId ? this.sessions.get(sessionId) : void 0;
|
|
8446
|
+
this.bridge.setActiveSession?.(sessionId, entry?.actions);
|
|
8447
|
+
if (entry?.metadata) {
|
|
8448
|
+
const { artwork: _artwork, ...metadata } = entry.metadata;
|
|
8449
|
+
this.bridge.setMetadata(entry.sessionId, metadata);
|
|
8450
|
+
}
|
|
8451
|
+
if (entry?.state) this.bridge.setPlaybackState(entry.sessionId, entry.state.status);
|
|
8452
|
+
}
|
|
8453
|
+
canonicalId(windowId, hint2) {
|
|
8454
|
+
const ids = this.used.get(windowId) ?? /* @__PURE__ */ new Set();
|
|
8455
|
+
this.used.set(windowId, ids);
|
|
8456
|
+
let value = typeof hint2 === "string" && hint2.trim() ? hint2.trim() : `media-${++this.counter}`;
|
|
8457
|
+
while ([...this.used.values()].some((used) => used.has(value))) value = `media-${++this.counter}`;
|
|
8458
|
+
ids.add(value);
|
|
8459
|
+
return value;
|
|
8460
|
+
}
|
|
8461
|
+
retire(sessionId, reason = "destroyed", notify2 = true) {
|
|
8462
|
+
const entry = this.sessions.get(sessionId);
|
|
8463
|
+
if (!entry) return;
|
|
8464
|
+
this.sessions.delete(sessionId);
|
|
8465
|
+
entry.playback?.destroy();
|
|
8466
|
+
this.bridge.destroySession?.(sessionId);
|
|
8467
|
+
if (this.active === sessionId) this.activate([...this.sessions.keys()].at(-1) ?? null);
|
|
8468
|
+
if (notify2) entry.send({ type: "media.session.ended", sessionId, reason });
|
|
8469
|
+
this.options.onSessionDestroy?.(entry.windowId, sessionId);
|
|
8470
|
+
this.changed();
|
|
8471
|
+
}
|
|
8472
|
+
command(sessionId, command) {
|
|
8473
|
+
const entry = this.sessions.get(sessionId);
|
|
8474
|
+
if (!entry || !validMediaCommand(command, entry.actions)) return;
|
|
8475
|
+
const safeCommand = { ...command, reason: "user" };
|
|
8476
|
+
if (entry.playback) entry.playback.command(safeCommand);
|
|
8477
|
+
else entry.send({ ...safeCommand, type: "media.command", sessionId });
|
|
8478
|
+
}
|
|
8479
|
+
publishState(entry, state, actions) {
|
|
8480
|
+
if (this.sessions.get(entry.sessionId) !== entry) return;
|
|
8481
|
+
const capabilitiesChanged = JSON.stringify(entry.actions) !== JSON.stringify(actions);
|
|
8482
|
+
entry.state = state;
|
|
8483
|
+
entry.actions = [...actions];
|
|
8484
|
+
if (state.status === "playing" || this.active === entry.sessionId) this.activate(entry.sessionId);
|
|
8485
|
+
if (entry.owner === "shell") {
|
|
8486
|
+
entry.send({ ...state, type: "media.state", sessionId: entry.sessionId });
|
|
8487
|
+
if (capabilitiesChanged) this.sendCapabilities(entry);
|
|
8488
|
+
}
|
|
8489
|
+
this.options.onState?.(entry.windowId, entry.sessionId, state);
|
|
8490
|
+
this.changed();
|
|
8491
|
+
}
|
|
8492
|
+
sendCapabilities(entry) {
|
|
8493
|
+
const { sessionId } = entry;
|
|
8494
|
+
if (this.sessions.get(sessionId) !== entry) return;
|
|
8495
|
+
if (entry.owner === "shell") entry.send({ type: "media.capabilities", sessionId, actions: [...entry.actions] });
|
|
8496
|
+
if (this.sessions.get(sessionId) !== entry) return;
|
|
8497
|
+
entry.send({ type: "media.controls", sessionId, controls: [...entry.actions] });
|
|
8498
|
+
}
|
|
8499
|
+
create(windowId, msg, send) {
|
|
8500
|
+
const fail2 = (error) => send({ type: "media.session.create.result", id: msg.id ?? "", ...msg.owner === "shell" || msg.owner === "napplet" ? { owner: msg.owner } : {}, error });
|
|
8501
|
+
if (this.destroyed) return;
|
|
8502
|
+
if (msg.owner !== "napplet" && msg.owner !== "shell") return fail2("missing owner");
|
|
8503
|
+
const count = [...this.sessions.values()].filter((entry2) => entry2.windowId === windowId).length + [...this.pending.values()].filter((pending) => pending.windowId === windowId).length;
|
|
8504
|
+
if (count >= (this.options.maxSessionsPerWindow ?? 16)) return fail2("session limit exceeded");
|
|
8505
|
+
if (msg.owner === "shell" && !msg.source) return fail2("missing source");
|
|
8506
|
+
if (msg.owner === "shell" && !this.options.playback) return fail2("unsupported owner mode");
|
|
8507
|
+
const entry = {
|
|
8508
|
+
sessionId: this.canonicalId(windowId, msg.sessionId),
|
|
8509
|
+
windowId,
|
|
8510
|
+
owner: msg.owner,
|
|
8511
|
+
metadata: msg.metadata,
|
|
8512
|
+
context: msg.context,
|
|
8513
|
+
actions: normalizeActions(msg.capabilities),
|
|
8514
|
+
send
|
|
8536
8515
|
};
|
|
8537
|
-
|
|
8538
|
-
|
|
8539
|
-
|
|
8540
|
-
|
|
8541
|
-
|
|
8542
|
-
|
|
8543
|
-
|
|
8544
|
-
|
|
8545
|
-
|
|
8546
|
-
|
|
8547
|
-
|
|
8548
|
-
|
|
8549
|
-
|
|
8550
|
-
|
|
8551
|
-
|
|
8552
|
-
|
|
8553
|
-
|
|
8554
|
-
|
|
8555
|
-
|
|
8556
|
-
|
|
8557
|
-
|
|
8558
|
-
|
|
8559
|
-
|
|
8560
|
-
|
|
8561
|
-
|
|
8562
|
-
|
|
8563
|
-
|
|
8564
|
-
|
|
8565
|
-
|
|
8566
|
-
|
|
8567
|
-
|
|
8568
|
-
|
|
8569
|
-
|
|
8570
|
-
|
|
8571
|
-
|
|
8572
|
-
|
|
8573
|
-
|
|
8574
|
-
|
|
8575
|
-
|
|
8576
|
-
|
|
8577
|
-
|
|
8516
|
+
if (msg.owner === "napplet") return this.accept(entry, msg.id);
|
|
8517
|
+
const abort = new AbortController();
|
|
8518
|
+
this.pending.set(abort, { windowId, fail: fail2 });
|
|
8519
|
+
const changed = (state, actions) => this.publishState(entry, state, actions);
|
|
8520
|
+
void Promise.resolve().then(() => {
|
|
8521
|
+
if (abort.signal.aborted) throw new Error("endpoint closed");
|
|
8522
|
+
return this.options.playback.open(windowId, msg, abort.signal, changed);
|
|
8523
|
+
}).then((playback) => {
|
|
8524
|
+
if (abort.signal.aborted || this.destroyed) return playback.destroy();
|
|
8525
|
+
this.pending.delete(abort);
|
|
8526
|
+
entry.playback = playback;
|
|
8527
|
+
entry.state = playback.state;
|
|
8528
|
+
entry.actions = [...playback.actions];
|
|
8529
|
+
this.accept(entry, msg.id);
|
|
8530
|
+
}).catch(() => {
|
|
8531
|
+
this.pending.delete(abort);
|
|
8532
|
+
if (!abort.signal.aborted && !this.destroyed) fail2("source unavailable or autoplay denied");
|
|
8533
|
+
}).finally(() => this.pending.delete(abort));
|
|
8534
|
+
}
|
|
8535
|
+
accept(entry, id) {
|
|
8536
|
+
this.sessions.set(entry.sessionId, entry);
|
|
8537
|
+
entry.send({ type: "media.session.create.result", id: id ?? "", sessionId: entry.sessionId, owner: entry.owner });
|
|
8538
|
+
if (this.sessions.get(entry.sessionId) !== entry) return;
|
|
8539
|
+
this.activate(entry.sessionId);
|
|
8540
|
+
this.sendCapabilities(entry);
|
|
8541
|
+
if (entry.owner === "shell" && entry.state) this.publishState(entry, entry.state, entry.actions);
|
|
8542
|
+
this.options.onSessionCreate?.(entry.windowId, entry.sessionId, entry.metadata);
|
|
8543
|
+
this.changed();
|
|
8544
|
+
}
|
|
8545
|
+
handle(windowId, message2, send) {
|
|
8546
|
+
if (this.destroyed) return;
|
|
8547
|
+
if (message2.type === "media.session.create") return this.create(windowId, message2, send);
|
|
8548
|
+
const msg = message2;
|
|
8549
|
+
const entry = this.sessions.get(msg.sessionId);
|
|
8550
|
+
if (!entry || entry.windowId !== windowId) return;
|
|
8551
|
+
switch (message2.type) {
|
|
8552
|
+
case "media.session.destroy":
|
|
8553
|
+
return this.retire(entry.sessionId);
|
|
8554
|
+
case "media.session.update": {
|
|
8555
|
+
const update = message2;
|
|
8556
|
+
entry.metadata = { ...entry.metadata, ...update.metadata };
|
|
8557
|
+
if (this.active === entry.sessionId) this.activate(entry.sessionId);
|
|
8558
|
+
this.options.onSessionUpdate?.(windowId, entry.sessionId, update.metadata);
|
|
8559
|
+
this.changed();
|
|
8560
|
+
return;
|
|
8561
|
+
}
|
|
8562
|
+
case "media.command":
|
|
8563
|
+
if (entry.owner === "shell") this.command(entry.sessionId, message2);
|
|
8564
|
+
return;
|
|
8565
|
+
case "media.state": {
|
|
8566
|
+
const report = message2;
|
|
8567
|
+
if (entry.owner !== "napplet" || !["playing", "paused", "stopped", "buffering"].includes(report.status)) return;
|
|
8568
|
+
const state = { status: report.status };
|
|
8569
|
+
for (const key of ["position", "duration", "volume"]) {
|
|
8570
|
+
const value = report[key];
|
|
8571
|
+
if (typeof value === "number" && Number.isFinite(value) && value >= 0 && (key !== "volume" || value <= 1)) state[key] = value;
|
|
8572
|
+
}
|
|
8573
|
+
if (typeof report.reason === "string") state.reason = report.reason;
|
|
8574
|
+
this.publishState(entry, state, entry.actions);
|
|
8575
|
+
return;
|
|
8576
|
+
}
|
|
8577
|
+
case "media.capabilities":
|
|
8578
|
+
if (entry.owner !== "napplet") return;
|
|
8579
|
+
entry.actions = normalizeActions(message2.actions);
|
|
8580
|
+
if (this.active === entry.sessionId) this.activate(entry.sessionId);
|
|
8581
|
+
this.options.onCapabilities?.(windowId, entry.sessionId, entry.actions);
|
|
8582
|
+
this.sendCapabilities(entry);
|
|
8583
|
+
this.changed();
|
|
8578
8584
|
}
|
|
8579
8585
|
}
|
|
8580
|
-
|
|
8581
|
-
|
|
8582
|
-
|
|
8583
|
-
|
|
8584
|
-
const ownedActive = state.activeSessionId !== null && sessions.has(state.activeSessionId);
|
|
8585
|
-
for (const sessionId of sessions) {
|
|
8586
|
-
state.sessionRegistry.delete(sessionId);
|
|
8587
|
-
state.bridge.destroySession?.(sessionId);
|
|
8586
|
+
destroyWindow(windowId) {
|
|
8587
|
+
for (const [abort, pending] of this.pending) if (pending.windowId === windowId) {
|
|
8588
|
+
abort.abort();
|
|
8589
|
+
this.pending.delete(abort);
|
|
8588
8590
|
}
|
|
8589
|
-
|
|
8590
|
-
if (ownedActive) promoteNextActiveOrClear(state);
|
|
8591
|
+
for (const entry of this.sessions.values()) if (entry.windowId === windowId) this.retire(entry.sessionId, "destroyed", false);
|
|
8591
8592
|
}
|
|
8592
|
-
|
|
8593
|
-
|
|
8594
|
-
|
|
8595
|
-
|
|
8596
|
-
for (const sessionId of state.sessionRegistry.keys()) state.bridge.destroySession?.(sessionId);
|
|
8597
|
-
state.bridge.setActiveSession?.(null);
|
|
8598
|
-
state.sessionRegistry.clear();
|
|
8599
|
-
state.windowSessions.clear();
|
|
8600
|
-
state.sendHandles.clear();
|
|
8601
|
-
state.activeSessionId = null;
|
|
8602
|
-
state.touchCounter = 0;
|
|
8603
|
-
state.sessionCounter = 0;
|
|
8593
|
+
};
|
|
8594
|
+
function normalizeActions(value) {
|
|
8595
|
+
const all = ["play", "pause", "stop", "next", "prev", "seek", "volume"];
|
|
8596
|
+
return Array.isArray(value) ? [...new Set(value.filter((action) => all.includes(action)))] : [];
|
|
8604
8597
|
}
|
|
8605
8598
|
function createMediaService(options = {}) {
|
|
8606
|
-
const
|
|
8607
|
-
|
|
8608
|
-
|
|
8609
|
-
|
|
8610
|
-
|
|
8611
|
-
|
|
8612
|
-
|
|
8613
|
-
|
|
8614
|
-
|
|
8615
|
-
|
|
8616
|
-
|
|
8617
|
-
|
|
8618
|
-
|
|
8619
|
-
|
|
8620
|
-
|
|
8621
|
-
|
|
8622
|
-
|
|
8623
|
-
|
|
8624
|
-
|
|
8625
|
-
|
|
8599
|
+
const bridge = options.hostBridge ?? createBrowserMediaBridge(options);
|
|
8600
|
+
const registry2 = new MediaRegistry(options, bridge);
|
|
8601
|
+
const off = bridge.onAction((sessionId, action, value) => registry2.command(sessionId, { action, value }));
|
|
8602
|
+
const service = {
|
|
8603
|
+
descriptor: { name: "media", version: MEDIA_SERVICE_VERSION, description: "NAP-MEDIA session registry and player" },
|
|
8604
|
+
handleMessage: (windowId, message2, send) => registry2.handle(windowId, message2, send),
|
|
8605
|
+
onWindowDestroyed: (windowId) => registry2.destroyWindow(windowId),
|
|
8606
|
+
onCapabilityRevoked(windowId, capability) {
|
|
8607
|
+
if (capability !== null && capability !== "media:control") return;
|
|
8608
|
+
for (const [abort, pending] of registry2.pending) {
|
|
8609
|
+
if (pending.windowId !== windowId) continue;
|
|
8610
|
+
abort.abort();
|
|
8611
|
+
registry2.pending.delete(abort);
|
|
8612
|
+
pending.fail("permission revoked");
|
|
8613
|
+
}
|
|
8614
|
+
for (const entry of registry2.sessions.values()) if (entry.windowId === windowId) registry2.retire(entry.sessionId, "policy");
|
|
8615
|
+
},
|
|
8616
|
+
getSessions: () => registry2.snapshots(),
|
|
8617
|
+
subscribe(listener) {
|
|
8618
|
+
registry2.listeners.add(listener);
|
|
8619
|
+
listener(registry2.snapshots());
|
|
8620
|
+
return () => {
|
|
8621
|
+
registry2.listeners.delete(listener);
|
|
8622
|
+
};
|
|
8626
8623
|
},
|
|
8624
|
+
command: (sessionId, command) => registry2.command(sessionId, command),
|
|
8625
|
+
retire: (sessionId, reason) => registry2.retire(sessionId, reason),
|
|
8626
|
+
getElement: (sessionId) => registry2.sessions.get(sessionId)?.playback?.element,
|
|
8627
8627
|
destroy() {
|
|
8628
|
-
|
|
8628
|
+
if (registry2.destroyed) return;
|
|
8629
|
+
registry2.destroyed = true;
|
|
8630
|
+
off();
|
|
8631
|
+
for (const [abort, pending] of registry2.pending) {
|
|
8632
|
+
abort.abort();
|
|
8633
|
+
registry2.pending.delete(abort);
|
|
8634
|
+
pending.fail("media service unavailable");
|
|
8635
|
+
}
|
|
8636
|
+
for (const id of registry2.sessions.keys()) registry2.retire(id, "policy");
|
|
8637
|
+
registry2.used.clear();
|
|
8638
|
+
registry2.listeners.clear();
|
|
8639
|
+
bridge.setActiveSession?.(null);
|
|
8629
8640
|
}
|
|
8630
8641
|
};
|
|
8642
|
+
service.onUnregistered = service.destroy;
|
|
8643
|
+
return service;
|
|
8631
8644
|
}
|
|
8632
8645
|
var NOTIFY_SERVICE_VERSION = "1.0.0";
|
|
8633
8646
|
function errorText(error) {
|
|
@@ -9500,26 +9513,10 @@ async function fetchResourceItem(options, windowId, info, request, signal) {
|
|
|
9500
9513
|
const { url } = request;
|
|
9501
9514
|
const parsedUrl = parseResourceUrl(url);
|
|
9502
9515
|
if (!parsedUrl) return resourceInvalidRequest(url, `invalid URL: ${url}`);
|
|
9503
|
-
|
|
9504
|
-
|
|
9505
|
-
|
|
9506
|
-
|
|
9507
|
-
ok: false,
|
|
9508
|
-
url,
|
|
9509
|
-
error: "blocked-by-policy",
|
|
9510
|
-
message: "napplet identity not resolvable for runtime origin policy"
|
|
9511
|
-
};
|
|
9512
|
-
}
|
|
9513
|
-
const origin = parsedUrl.origin;
|
|
9514
|
-
const grants = options.getConnectGrants(identity.dTag, identity.aggregateHash);
|
|
9515
|
-
if (!options.isOriginGranted(origin, grants)) {
|
|
9516
|
-
return {
|
|
9517
|
-
ok: false,
|
|
9518
|
-
url,
|
|
9519
|
-
error: "blocked-by-policy",
|
|
9520
|
-
message: `origin ${origin} not granted by runtime policy`
|
|
9521
|
-
};
|
|
9522
|
-
}
|
|
9516
|
+
try {
|
|
9517
|
+
assertResourceOrigin(options, windowId, parsedUrl);
|
|
9518
|
+
} catch (error) {
|
|
9519
|
+
return { ok: false, url, error: "blocked-by-policy", message: error instanceof Error ? error.message : String(error) };
|
|
9523
9520
|
}
|
|
9524
9521
|
try {
|
|
9525
9522
|
const response = await options.fetch(url, {
|
|
@@ -9549,6 +9546,19 @@ async function fetchResourceItem(options, windowId, info, request, signal) {
|
|
|
9549
9546
|
};
|
|
9550
9547
|
}
|
|
9551
9548
|
}
|
|
9549
|
+
function assertResourceOrigin(options, windowId, parsedUrl) {
|
|
9550
|
+
if (parsedUrl.origin !== "null" && options.isOriginGranted && options.getConnectGrants) {
|
|
9551
|
+
const identity = options.resolveIdentity?.(windowId) ?? null;
|
|
9552
|
+
if (!identity) {
|
|
9553
|
+
throw new ResourceServiceError("blocked-by-policy", "napplet identity not resolvable for runtime origin policy");
|
|
9554
|
+
}
|
|
9555
|
+
const origin = parsedUrl.origin;
|
|
9556
|
+
const grants = options.getConnectGrants(identity.dTag, identity.aggregateHash);
|
|
9557
|
+
if (!options.isOriginGranted(origin, grants)) {
|
|
9558
|
+
throw new ResourceServiceError("blocked-by-policy", `origin ${origin} not granted by runtime policy`);
|
|
9559
|
+
}
|
|
9560
|
+
}
|
|
9561
|
+
}
|
|
9552
9562
|
async function handleBytes(options, state, windowId, msg, send) {
|
|
9553
9563
|
const { requestId, ...request } = msg;
|
|
9554
9564
|
const controller = new AbortController();
|
|
@@ -9668,6 +9678,14 @@ function createResourceService(options) {
|
|
|
9668
9678
|
};
|
|
9669
9679
|
const handler = {
|
|
9670
9680
|
descriptor,
|
|
9681
|
+
async openStream(windowId, url, signal) {
|
|
9682
|
+
if (signal.aborted) throw new DOMException("Playback cancelled", "AbortError");
|
|
9683
|
+
const parsed = parseResourceUrl(url);
|
|
9684
|
+
if (!parsed) throw new ResourceServiceError("invalid-request", "invalid media URL");
|
|
9685
|
+
assertResourceOrigin(options, windowId, parsed);
|
|
9686
|
+
if (!options.stream) throw new ResourceServiceError("unsupported-scheme", "live media unavailable");
|
|
9687
|
+
return options.stream(url, { method: "GET", signal, windowId });
|
|
9688
|
+
},
|
|
9671
9689
|
handleMessage(windowId, message2, send) {
|
|
9672
9690
|
switch (message2.type) {
|
|
9673
9691
|
case "resource.bytes": {
|
|
@@ -12591,10 +12609,10 @@ function createGamepadService(options = {}) {
|
|
|
12591
12609
|
if (recipients.size === 0) cancel();
|
|
12592
12610
|
}
|
|
12593
12611
|
function allowSubscribe(windowId) {
|
|
12594
|
-
const
|
|
12612
|
+
const time2 = now2();
|
|
12595
12613
|
let bucket = changes.get(windowId);
|
|
12596
|
-
if (!bucket ||
|
|
12597
|
-
bucket = { start:
|
|
12614
|
+
if (!bucket || time2 < bucket.start || time2 - bucket.start >= SUBSCRIBE_WINDOW_MS) {
|
|
12615
|
+
bucket = { start: time2, count: 0 };
|
|
12598
12616
|
changes.set(windowId, bucket);
|
|
12599
12617
|
}
|
|
12600
12618
|
bucket.count += 1;
|
|
@@ -12664,6 +12682,517 @@ function browserGamepadOptions(host, resolveWindow = () => void 0) {
|
|
|
12664
12682
|
}
|
|
12665
12683
|
};
|
|
12666
12684
|
}
|
|
12685
|
+
function waitEvent(target, event, signal, operation) {
|
|
12686
|
+
return new Promise((resolve, reject2) => {
|
|
12687
|
+
const cleanup = () => {
|
|
12688
|
+
target.removeEventListener(event, done);
|
|
12689
|
+
target.removeEventListener("error", fail2);
|
|
12690
|
+
signal.removeEventListener("abort", fail2);
|
|
12691
|
+
};
|
|
12692
|
+
const done = () => {
|
|
12693
|
+
cleanup();
|
|
12694
|
+
resolve();
|
|
12695
|
+
};
|
|
12696
|
+
const fail2 = () => {
|
|
12697
|
+
cleanup();
|
|
12698
|
+
reject2(new Error("live audio failed or cancelled"));
|
|
12699
|
+
};
|
|
12700
|
+
target.addEventListener(event, done, { once: true });
|
|
12701
|
+
target.addEventListener("error", fail2, { once: true });
|
|
12702
|
+
signal.addEventListener("abort", fail2, { once: true });
|
|
12703
|
+
if (signal.aborted) return fail2();
|
|
12704
|
+
try {
|
|
12705
|
+
operation?.();
|
|
12706
|
+
} catch {
|
|
12707
|
+
fail2();
|
|
12708
|
+
}
|
|
12709
|
+
});
|
|
12710
|
+
}
|
|
12711
|
+
function delay(signal) {
|
|
12712
|
+
return new Promise((resolve) => {
|
|
12713
|
+
const done = () => {
|
|
12714
|
+
clearTimeout(timer);
|
|
12715
|
+
signal.removeEventListener("abort", done);
|
|
12716
|
+
resolve();
|
|
12717
|
+
};
|
|
12718
|
+
const timer = setTimeout(done, 250);
|
|
12719
|
+
signal.addEventListener("abort", done, { once: true });
|
|
12720
|
+
if (signal.aborted) done();
|
|
12721
|
+
});
|
|
12722
|
+
}
|
|
12723
|
+
async function pumpAudio(source, element2, reader, mime, signal) {
|
|
12724
|
+
await waitEvent(source, "sourceopen", signal);
|
|
12725
|
+
const buffer = source.addSourceBuffer(mime);
|
|
12726
|
+
while (!signal.aborted) {
|
|
12727
|
+
const item = await reader.read();
|
|
12728
|
+
if (item.done) {
|
|
12729
|
+
if (source.readyState === "open") source.endOfStream();
|
|
12730
|
+
return;
|
|
12731
|
+
}
|
|
12732
|
+
if (item.value.byteLength > 1024 * 1024) throw new Error("live audio chunk too large");
|
|
12733
|
+
for (let offset = 0; offset < item.value.byteLength; offset += 64 * 1024) {
|
|
12734
|
+
while (!signal.aborted && buffer.buffered.length && buffer.buffered.end(buffer.buffered.length - 1) - element2.currentTime > 30) {
|
|
12735
|
+
await delay(signal);
|
|
12736
|
+
}
|
|
12737
|
+
const cutoff = element2.currentTime - 15;
|
|
12738
|
+
if (buffer.buffered.length && cutoff > buffer.buffered.start(0) + 5) {
|
|
12739
|
+
await waitEvent(buffer, "updateend", signal, () => buffer.remove(0, cutoff));
|
|
12740
|
+
}
|
|
12741
|
+
const bytes = item.value.slice(offset, offset + 64 * 1024);
|
|
12742
|
+
await waitEvent(buffer, "updateend", signal, () => buffer.appendBuffer(bytes));
|
|
12743
|
+
}
|
|
12744
|
+
}
|
|
12745
|
+
}
|
|
12746
|
+
function createMediaStream(response, element2, controller, urls) {
|
|
12747
|
+
const mime = response.headers.get("content-type") ?? "";
|
|
12748
|
+
if (!response.body || typeof MediaSource === "undefined" || !MediaSource.isTypeSupported(mime)) {
|
|
12749
|
+
void response.body?.cancel().catch(() => {
|
|
12750
|
+
});
|
|
12751
|
+
throw new Error("unsupported live media type");
|
|
12752
|
+
}
|
|
12753
|
+
const source = new MediaSource();
|
|
12754
|
+
const reader = response.body.getReader();
|
|
12755
|
+
const url = urls.createObjectURL(source);
|
|
12756
|
+
let destroyed = false;
|
|
12757
|
+
const cancel = () => {
|
|
12758
|
+
void reader.cancel().catch(() => {
|
|
12759
|
+
});
|
|
12760
|
+
};
|
|
12761
|
+
controller.signal.addEventListener("abort", cancel, { once: true });
|
|
12762
|
+
void pumpAudio(source, element2, reader, mime, controller.signal).catch(() => {
|
|
12763
|
+
if (!controller.signal.aborted) {
|
|
12764
|
+
controller.abort();
|
|
12765
|
+
element2.pause();
|
|
12766
|
+
element2.dispatchEvent(new Event("error"));
|
|
12767
|
+
}
|
|
12768
|
+
}).finally(() => {
|
|
12769
|
+
controller.signal.removeEventListener("abort", cancel);
|
|
12770
|
+
reader.releaseLock();
|
|
12771
|
+
});
|
|
12772
|
+
return {
|
|
12773
|
+
url,
|
|
12774
|
+
destroy() {
|
|
12775
|
+
if (destroyed) return;
|
|
12776
|
+
destroyed = true;
|
|
12777
|
+
controller.abort();
|
|
12778
|
+
urls.revokeObjectURL(url);
|
|
12779
|
+
}
|
|
12780
|
+
};
|
|
12781
|
+
}
|
|
12782
|
+
function readSource(resource, windowId, url, signal) {
|
|
12783
|
+
return new Promise((resolve, reject2) => {
|
|
12784
|
+
const id = `media-resource-${crypto.randomUUID()}`;
|
|
12785
|
+
const cleanup = () => {
|
|
12786
|
+
clearTimeout(timer);
|
|
12787
|
+
signal.removeEventListener("abort", abort);
|
|
12788
|
+
};
|
|
12789
|
+
const abort = () => {
|
|
12790
|
+
cleanup();
|
|
12791
|
+
resource.handleMessage(windowId, { type: "resource.cancel", id }, () => {
|
|
12792
|
+
});
|
|
12793
|
+
reject2(new Error("media source cancelled"));
|
|
12794
|
+
};
|
|
12795
|
+
const timer = setTimeout(abort, 2e4);
|
|
12796
|
+
signal.addEventListener("abort", abort, { once: true });
|
|
12797
|
+
if (signal.aborted) return abort();
|
|
12798
|
+
try {
|
|
12799
|
+
resource.handleMessage(windowId, { type: "resource.bytes", id, url }, (message2) => {
|
|
12800
|
+
const result2 = message2;
|
|
12801
|
+
if (result2.id !== id || signal.aborted) return;
|
|
12802
|
+
cleanup();
|
|
12803
|
+
if (result2.type === "resource.bytes.result" && result2.blob instanceof Blob) resolve(result2.blob);
|
|
12804
|
+
else reject2(new Error("media source blocked or unavailable"));
|
|
12805
|
+
});
|
|
12806
|
+
} catch (error) {
|
|
12807
|
+
cleanup();
|
|
12808
|
+
reject2(error);
|
|
12809
|
+
}
|
|
12810
|
+
});
|
|
12811
|
+
}
|
|
12812
|
+
function sourceUrl(options) {
|
|
12813
|
+
const source = options.source;
|
|
12814
|
+
if (typeof source?.url === "string" && source.url) return source.url;
|
|
12815
|
+
if (typeof source?.blossomHash === "string" && /^[a-f0-9]{64}$/i.test(source.blossomHash)) return `blossom:${source.blossomHash}`;
|
|
12816
|
+
throw new Error("unsupported source");
|
|
12817
|
+
}
|
|
12818
|
+
function loadMedia(element2, url, signal) {
|
|
12819
|
+
return new Promise((resolve, reject2) => {
|
|
12820
|
+
const cleanup = () => {
|
|
12821
|
+
clearTimeout(timer);
|
|
12822
|
+
element2.removeEventListener("loadedmetadata", loaded);
|
|
12823
|
+
element2.removeEventListener("error", failed2);
|
|
12824
|
+
signal.removeEventListener("abort", failed2);
|
|
12825
|
+
};
|
|
12826
|
+
const loaded = () => {
|
|
12827
|
+
cleanup();
|
|
12828
|
+
resolve();
|
|
12829
|
+
};
|
|
12830
|
+
const failed2 = () => {
|
|
12831
|
+
cleanup();
|
|
12832
|
+
reject2(new Error("media decoding failed or cancelled"));
|
|
12833
|
+
};
|
|
12834
|
+
const timer = setTimeout(failed2, 5e3);
|
|
12835
|
+
element2.addEventListener("loadedmetadata", loaded, { once: true });
|
|
12836
|
+
element2.addEventListener("error", failed2, { once: true });
|
|
12837
|
+
signal.addEventListener("abort", failed2, { once: true });
|
|
12838
|
+
if (signal.aborted) return failed2();
|
|
12839
|
+
try {
|
|
12840
|
+
element2.src = url;
|
|
12841
|
+
element2.load();
|
|
12842
|
+
} catch {
|
|
12843
|
+
failed2();
|
|
12844
|
+
}
|
|
12845
|
+
});
|
|
12846
|
+
}
|
|
12847
|
+
function attachPlayback(element2, live, release, changed) {
|
|
12848
|
+
let retired = false;
|
|
12849
|
+
let generation = 0;
|
|
12850
|
+
let state = { status: "paused", position: 0, volume: element2.volume };
|
|
12851
|
+
const actions = () => [
|
|
12852
|
+
"play",
|
|
12853
|
+
"pause",
|
|
12854
|
+
"stop",
|
|
12855
|
+
"volume",
|
|
12856
|
+
...!live && Number.isFinite(element2.duration) && element2.duration > 0 ? ["seek"] : []
|
|
12857
|
+
];
|
|
12858
|
+
const publish = (status = state.status, reason) => {
|
|
12859
|
+
if (retired) return;
|
|
12860
|
+
state = {
|
|
12861
|
+
status,
|
|
12862
|
+
position: element2.currentTime,
|
|
12863
|
+
volume: element2.volume,
|
|
12864
|
+
...Number.isFinite(element2.duration) ? { duration: element2.duration } : {},
|
|
12865
|
+
...reason ? { reason } : {}
|
|
12866
|
+
};
|
|
12867
|
+
changed({ ...state }, actions());
|
|
12868
|
+
};
|
|
12869
|
+
const handlers = {
|
|
12870
|
+
playing: () => publish("playing"),
|
|
12871
|
+
pause: () => {
|
|
12872
|
+
if (state.status !== "stopped") publish("paused");
|
|
12873
|
+
},
|
|
12874
|
+
waiting: () => publish("buffering"),
|
|
12875
|
+
ended: () => {
|
|
12876
|
+
generation++;
|
|
12877
|
+
publish("stopped", "ended");
|
|
12878
|
+
},
|
|
12879
|
+
error: () => {
|
|
12880
|
+
generation++;
|
|
12881
|
+
publish("stopped", "error");
|
|
12882
|
+
},
|
|
12883
|
+
timeupdate: () => publish(),
|
|
12884
|
+
durationchange: () => publish(),
|
|
12885
|
+
loadedmetadata: () => publish(),
|
|
12886
|
+
volumechange: () => publish(),
|
|
12887
|
+
seeked: () => publish()
|
|
12888
|
+
};
|
|
12889
|
+
for (const [event, handler] of Object.entries(handlers)) element2.addEventListener(event, handler);
|
|
12890
|
+
return {
|
|
12891
|
+
element: element2,
|
|
12892
|
+
get state() {
|
|
12893
|
+
return { ...state };
|
|
12894
|
+
},
|
|
12895
|
+
get actions() {
|
|
12896
|
+
return actions();
|
|
12897
|
+
},
|
|
12898
|
+
command(command) {
|
|
12899
|
+
if (retired || !validMediaCommand(command, actions())) return;
|
|
12900
|
+
const token = ++generation;
|
|
12901
|
+
switch (command.action) {
|
|
12902
|
+
case "play":
|
|
12903
|
+
if (live && element2.buffered.length && (element2.ended || element2.currentTime < element2.buffered.start(0))) element2.currentTime = element2.buffered.start(0);
|
|
12904
|
+
void element2.play().then(() => {
|
|
12905
|
+
if (!retired && token === generation) publish("playing", command.reason);
|
|
12906
|
+
}).catch(() => {
|
|
12907
|
+
if (!retired && token === generation) publish("paused", "error");
|
|
12908
|
+
});
|
|
12909
|
+
break;
|
|
12910
|
+
case "pause":
|
|
12911
|
+
element2.pause();
|
|
12912
|
+
publish("paused", command.reason);
|
|
12913
|
+
break;
|
|
12914
|
+
case "stop":
|
|
12915
|
+
element2.pause();
|
|
12916
|
+
if (!live) element2.currentTime = 0;
|
|
12917
|
+
publish("stopped", command.reason);
|
|
12918
|
+
break;
|
|
12919
|
+
case "seek":
|
|
12920
|
+
element2.currentTime = Math.min(command.value, element2.duration);
|
|
12921
|
+
publish();
|
|
12922
|
+
break;
|
|
12923
|
+
case "volume":
|
|
12924
|
+
element2.volume = command.value;
|
|
12925
|
+
publish();
|
|
12926
|
+
break;
|
|
12927
|
+
}
|
|
12928
|
+
},
|
|
12929
|
+
destroy() {
|
|
12930
|
+
if (retired) return;
|
|
12931
|
+
retired = true;
|
|
12932
|
+
generation++;
|
|
12933
|
+
for (const [event, handler] of Object.entries(handlers)) element2.removeEventListener(event, handler);
|
|
12934
|
+
element2.pause();
|
|
12935
|
+
element2.removeAttribute("src");
|
|
12936
|
+
element2.load();
|
|
12937
|
+
element2.remove();
|
|
12938
|
+
release();
|
|
12939
|
+
}
|
|
12940
|
+
};
|
|
12941
|
+
}
|
|
12942
|
+
function createBrowserMediaPlayback(options) {
|
|
12943
|
+
const doc = options.document ?? document;
|
|
12944
|
+
const urls = options.urls ?? URL;
|
|
12945
|
+
return {
|
|
12946
|
+
async open(windowId, request, signal, changed) {
|
|
12947
|
+
if (request.live && options.stream) return openLive(windowId, request, signal, changed, options, doc, urls);
|
|
12948
|
+
const blob = await readSource(options.resource, windowId, sourceUrl(request), signal);
|
|
12949
|
+
if (signal.aborted) throw new Error("media source cancelled");
|
|
12950
|
+
if (!/^(audio|video)\//.test(blob.type)) throw new Error("unsupported media type");
|
|
12951
|
+
const element2 = doc.createElement(blob.type.startsWith("video/") ? "video" : "audio");
|
|
12952
|
+
if (!element2.canPlayType(blob.type)) throw new Error("unsupported media type");
|
|
12953
|
+
element2.preload = "metadata";
|
|
12954
|
+
if (element2.tagName === "VIDEO") element2.playsInline = true;
|
|
12955
|
+
const url = urls.createObjectURL(blob);
|
|
12956
|
+
const playback = attachPlayback(element2, request.live === true, () => urls.revokeObjectURL(url), changed);
|
|
12957
|
+
const abort = () => playback.destroy();
|
|
12958
|
+
signal.addEventListener("abort", abort, { once: true });
|
|
12959
|
+
try {
|
|
12960
|
+
await loadMedia(element2, url, signal);
|
|
12961
|
+
if (request.autoplay) await element2.play();
|
|
12962
|
+
if (signal.aborted) throw new Error("media source cancelled");
|
|
12963
|
+
return playback;
|
|
12964
|
+
} catch (error) {
|
|
12965
|
+
playback.destroy();
|
|
12966
|
+
throw error;
|
|
12967
|
+
} finally {
|
|
12968
|
+
signal.removeEventListener("abort", abort);
|
|
12969
|
+
}
|
|
12970
|
+
}
|
|
12971
|
+
};
|
|
12972
|
+
}
|
|
12973
|
+
async function openLive(windowId, request, signal, changed, options, doc, urls) {
|
|
12974
|
+
const controller = new AbortController();
|
|
12975
|
+
const abort = () => controller.abort();
|
|
12976
|
+
signal.addEventListener("abort", abort, { once: true });
|
|
12977
|
+
const timer = setTimeout(abort, 2e4);
|
|
12978
|
+
let playback;
|
|
12979
|
+
try {
|
|
12980
|
+
if (signal.aborted) controller.abort();
|
|
12981
|
+
const response = await options.stream(windowId, sourceUrl(request), controller.signal);
|
|
12982
|
+
if (controller.signal.aborted) {
|
|
12983
|
+
await response.body?.cancel();
|
|
12984
|
+
throw new Error("media source cancelled");
|
|
12985
|
+
}
|
|
12986
|
+
const element2 = doc.createElement("audio");
|
|
12987
|
+
element2.preload = "auto";
|
|
12988
|
+
const stream = createMediaStream(response, element2, controller, urls);
|
|
12989
|
+
playback = attachPlayback(element2, true, stream.destroy, changed);
|
|
12990
|
+
await loadMedia(element2, stream.url, controller.signal);
|
|
12991
|
+
if (request.autoplay) await element2.play();
|
|
12992
|
+
if (controller.signal.aborted) throw new Error("media source cancelled");
|
|
12993
|
+
return playback;
|
|
12994
|
+
} catch (error) {
|
|
12995
|
+
playback?.destroy();
|
|
12996
|
+
controller.abort();
|
|
12997
|
+
throw error;
|
|
12998
|
+
} finally {
|
|
12999
|
+
clearTimeout(timer);
|
|
13000
|
+
signal.removeEventListener("abort", abort);
|
|
13001
|
+
}
|
|
13002
|
+
}
|
|
13003
|
+
function publicStreamUrl(value) {
|
|
13004
|
+
const url = new URL(value);
|
|
13005
|
+
const host = url.hostname.toLowerCase();
|
|
13006
|
+
const local = host === "localhost" || /\.(localhost|local|internal)$/.test(host) || !host.includes(".") || host.includes(":");
|
|
13007
|
+
const ip = /^\d+\.\d+\.\d+\.\d+$/.test(host) ? host.split(".").map(Number) : null;
|
|
13008
|
+
if (url.protocol !== "https:" || url.username || url.password || local || ip && (ip[0] === 0 || ip[0] === 10 || ip[0] === 127 || ip[0] >= 224 || ip[0] === 100 && ip[1] >= 64 && ip[1] <= 127 || ip[0] === 169 && ip[1] === 254 || ip[0] === 172 && ip[1] >= 16 && ip[1] <= 31 || ip[0] === 192 && ip[1] === 168 || ip[0] === 198 && (ip[1] === 18 || ip[1] === 19))) throw new ResourceServiceError("blocked-by-policy", "live media requires a public HTTPS URL without credentials");
|
|
13009
|
+
return url;
|
|
13010
|
+
}
|
|
13011
|
+
function audioMime(bytes) {
|
|
13012
|
+
if (bytes[0] === 73 && bytes[1] === 68 && bytes[2] === 51) return "audio/mpeg";
|
|
13013
|
+
if (bytes[0] === 255 && (bytes[1] & 224) === 224 && (bytes[1] & 24) !== 8) {
|
|
13014
|
+
if ((bytes[1] & 6) !== 0 && (bytes[2] & 240) !== 240 && (bytes[2] & 12) !== 12) return "audio/mpeg";
|
|
13015
|
+
if ((bytes[1] & 246) === 240) return "audio/aac";
|
|
13016
|
+
}
|
|
13017
|
+
throw new ResourceServiceError("decode-failed", "unsupported live audio bytes");
|
|
13018
|
+
}
|
|
13019
|
+
function createBrowserMediaStreamFetch(fetcher = globalThis.fetch) {
|
|
13020
|
+
return async (value, init) => {
|
|
13021
|
+
const url = publicStreamUrl(value);
|
|
13022
|
+
const response = await fetcher(url.href, {
|
|
13023
|
+
method: "GET",
|
|
13024
|
+
signal: init.signal,
|
|
13025
|
+
redirect: "error",
|
|
13026
|
+
credentials: "omit",
|
|
13027
|
+
referrerPolicy: "no-referrer"
|
|
13028
|
+
});
|
|
13029
|
+
if (!response.ok || !response.body) {
|
|
13030
|
+
await response.body?.cancel();
|
|
13031
|
+
throw new ResourceServiceError("network-error", "live audio unavailable");
|
|
13032
|
+
}
|
|
13033
|
+
const reader = response.body.getReader();
|
|
13034
|
+
try {
|
|
13035
|
+
const prefix = new Uint8Array(4);
|
|
13036
|
+
const chunks = [];
|
|
13037
|
+
let length = 0;
|
|
13038
|
+
while (length < prefix.length) {
|
|
13039
|
+
const { value: chunk, done } = await reader.read();
|
|
13040
|
+
if (done) throw new ResourceServiceError("decode-failed", "empty live audio");
|
|
13041
|
+
if (!chunk.byteLength) continue;
|
|
13042
|
+
if (chunk.byteLength > 1024 * 1024) throw new ResourceServiceError("too-large", "live audio chunk too large");
|
|
13043
|
+
prefix.set(chunk.subarray(0, prefix.length - length), length);
|
|
13044
|
+
length += Math.min(chunk.byteLength, prefix.length - length);
|
|
13045
|
+
chunks.push(chunk);
|
|
13046
|
+
}
|
|
13047
|
+
const mime = audioMime(prefix);
|
|
13048
|
+
return new Response(new ReadableStream({
|
|
13049
|
+
start(controller) {
|
|
13050
|
+
for (const chunk of chunks) controller.enqueue(chunk);
|
|
13051
|
+
},
|
|
13052
|
+
async pull(controller) {
|
|
13053
|
+
try {
|
|
13054
|
+
const item = await reader.read();
|
|
13055
|
+
if (item.done) {
|
|
13056
|
+
reader.releaseLock();
|
|
13057
|
+
controller.close();
|
|
13058
|
+
} else controller.enqueue(item.value);
|
|
13059
|
+
} catch (error) {
|
|
13060
|
+
controller.error(error);
|
|
13061
|
+
}
|
|
13062
|
+
},
|
|
13063
|
+
async cancel() {
|
|
13064
|
+
await reader.cancel();
|
|
13065
|
+
reader.releaseLock();
|
|
13066
|
+
}
|
|
13067
|
+
}), { headers: { "content-type": mime } });
|
|
13068
|
+
} catch (error) {
|
|
13069
|
+
await reader.cancel().catch(() => {
|
|
13070
|
+
});
|
|
13071
|
+
reader.releaseLock();
|
|
13072
|
+
throw error;
|
|
13073
|
+
}
|
|
13074
|
+
};
|
|
13075
|
+
}
|
|
13076
|
+
var PLAYER_STYLE = `
|
|
13077
|
+
:host { position:fixed; right:16px; bottom:48px; z-index:10000; width:min(360px,calc(100vw - 32px)); color-scheme:dark; font:14px/1.4 system-ui,sans-serif; color:#f4f0df; }
|
|
13078
|
+
:host([hidden]) { display:none; }
|
|
13079
|
+
details { background:#171b1b; border:1px solid #68716c; border-radius:12px; box-shadow:0 6px 24px #0006; }
|
|
13080
|
+
summary { padding:12px 16px; cursor:pointer; font-weight:600; }
|
|
13081
|
+
.sessions { max-height:60vh; overflow:auto; padding:0 12px 12px; }
|
|
13082
|
+
article { border-top:1px solid #49514d; padding-top:12px; margin-top:4px; }
|
|
13083
|
+
h3,p { margin:0 0 6px; overflow-wrap:anywhere; }
|
|
13084
|
+
p { color:#c7cdc9; font-size:12px; }
|
|
13085
|
+
.buttons { display:flex; gap:6px; flex-wrap:wrap; margin:8px 0; }
|
|
13086
|
+
button { padding:8px 10px; border:1px solid #68716c; border-radius:6px; color:inherit; background:#29312c; cursor:pointer; min-height:40px; }
|
|
13087
|
+
button:disabled { opacity:.5; cursor:default; }
|
|
13088
|
+
button:focus-visible, input:focus-visible, summary:focus-visible { outline:2px solid #e9d584; outline-offset:3px; }
|
|
13089
|
+
label { display:flex; align-items:center; gap:8px; margin:8px 0; }
|
|
13090
|
+
input { flex:1; min-width:0; accent-color:#e9d584; }
|
|
13091
|
+
video { display:block; width:100%; max-height:240px; background:#000; border-radius:6px; }
|
|
13092
|
+
audio { display:none; }
|
|
13093
|
+
`;
|
|
13094
|
+
function time(value = 0) {
|
|
13095
|
+
const seconds = Number.isFinite(value) ? Math.max(0, Math.floor(value)) : 0;
|
|
13096
|
+
return `${Math.floor(seconds / 60)}:${String(seconds % 60).padStart(2, "0")}`;
|
|
13097
|
+
}
|
|
13098
|
+
function makeRow(doc, service, sessionId) {
|
|
13099
|
+
const root = doc.createElement("article");
|
|
13100
|
+
root.dataset.sessionId = sessionId;
|
|
13101
|
+
const title = doc.createElement("h3");
|
|
13102
|
+
const detail = doc.createElement("p");
|
|
13103
|
+
const status = doc.createElement("p");
|
|
13104
|
+
const media = doc.createElement("div");
|
|
13105
|
+
const controls = doc.createElement("div");
|
|
13106
|
+
controls.className = "buttons";
|
|
13107
|
+
const buttons = /* @__PURE__ */ new Map();
|
|
13108
|
+
for (const [action, text] of [["prev", "Previous"], ["play", "Play"], ["pause", "Pause"], ["stop", "Stop"], ["next", "Next"]]) {
|
|
13109
|
+
const button2 = doc.createElement("button");
|
|
13110
|
+
button2.type = "button";
|
|
13111
|
+
button2.textContent = text;
|
|
13112
|
+
button2.onclick = () => service.command(sessionId, { action, reason: "user" });
|
|
13113
|
+
controls.append(button2);
|
|
13114
|
+
buttons.set(action, button2);
|
|
13115
|
+
}
|
|
13116
|
+
const close = doc.createElement("button");
|
|
13117
|
+
close.type = "button";
|
|
13118
|
+
close.textContent = "Close session";
|
|
13119
|
+
close.onclick = () => service.retire(sessionId, "user");
|
|
13120
|
+
controls.append(close);
|
|
13121
|
+
const range = (name, action, max, step) => {
|
|
13122
|
+
const label = doc.createElement("label");
|
|
13123
|
+
label.append(name);
|
|
13124
|
+
const input = doc.createElement("input");
|
|
13125
|
+
input.type = "range";
|
|
13126
|
+
input.min = "0";
|
|
13127
|
+
input.max = max;
|
|
13128
|
+
input.step = step;
|
|
13129
|
+
input.setAttribute("aria-label", name);
|
|
13130
|
+
input.onchange = () => service.command(sessionId, { action, value: Number(input.value), reason: "user" });
|
|
13131
|
+
label.append(input);
|
|
13132
|
+
return { label, input };
|
|
13133
|
+
};
|
|
13134
|
+
const seek = range("Position", "seek", "0", "0.1");
|
|
13135
|
+
const volume = range("Volume", "volume", "1", "0.01");
|
|
13136
|
+
root.append(title, detail, media, status, controls, seek.label, volume.label);
|
|
13137
|
+
return { root, title, detail, status, buttons, seek: seek.input, volume: volume.input, media };
|
|
13138
|
+
}
|
|
13139
|
+
function updateRow(row, session, service) {
|
|
13140
|
+
row.title.textContent = session.metadata?.title || "Untitled media";
|
|
13141
|
+
row.detail.textContent = [session.metadata?.artist, session.metadata?.album, session.context?.label, session.context?.detail].filter(Boolean).join(" \xB7 ");
|
|
13142
|
+
const state = session.state;
|
|
13143
|
+
row.status.textContent = `${state?.status ?? "Ready"} \xB7 ${time(state?.position)} / ${time(state?.duration ?? session.metadata?.duration)}${state?.reason === "error" ? " \xB7 Playback failed. Try Play again." : ""}`;
|
|
13144
|
+
for (const [action, button2] of row.buttons) {
|
|
13145
|
+
button2.hidden = !session.actions.includes(action) || action === "play" && state?.status === "playing" || action === "pause" && state?.status !== "playing" && state?.status !== "buffering";
|
|
13146
|
+
}
|
|
13147
|
+
row.seek.disabled = !session.actions.includes("seek");
|
|
13148
|
+
row.seek.max = String(state?.duration ?? session.metadata?.duration ?? 0);
|
|
13149
|
+
row.volume.disabled = !session.actions.includes("volume");
|
|
13150
|
+
if (row.seek.getRootNode() instanceof ShadowRoot && row.seek.getRootNode().activeElement !== row.seek) row.seek.value = String(state?.position ?? 0);
|
|
13151
|
+
if (row.volume.getRootNode() instanceof ShadowRoot && row.volume.getRootNode().activeElement !== row.volume) row.volume.value = String(state?.volume ?? 1);
|
|
13152
|
+
const element2 = service.getElement(session.sessionId);
|
|
13153
|
+
if (element2 && element2.parentElement !== row.media) row.media.append(element2);
|
|
13154
|
+
}
|
|
13155
|
+
function mountMediaPlayer(service, container = document.body) {
|
|
13156
|
+
const doc = container.ownerDocument;
|
|
13157
|
+
const host = doc.createElement("section");
|
|
13158
|
+
host.dataset.kehtoMediaPlayer = "";
|
|
13159
|
+
host.setAttribute("aria-label", "Media player");
|
|
13160
|
+
const shadow = host.attachShadow({ mode: "open" });
|
|
13161
|
+
const style = doc.createElement("style");
|
|
13162
|
+
style.textContent = PLAYER_STYLE;
|
|
13163
|
+
const details = doc.createElement("details");
|
|
13164
|
+
details.open = true;
|
|
13165
|
+
const summary = doc.createElement("summary");
|
|
13166
|
+
summary.textContent = "Media player";
|
|
13167
|
+
const sessions = doc.createElement("div");
|
|
13168
|
+
sessions.className = "sessions";
|
|
13169
|
+
details.append(summary, sessions);
|
|
13170
|
+
shadow.append(style, details);
|
|
13171
|
+
container.append(host);
|
|
13172
|
+
const rows = /* @__PURE__ */ new Map();
|
|
13173
|
+
const off = service.subscribe((snapshots) => {
|
|
13174
|
+
host.hidden = snapshots.length === 0;
|
|
13175
|
+
summary.textContent = `Media player (${snapshots.length})`;
|
|
13176
|
+
const live = new Set(snapshots.map((session) => session.sessionId));
|
|
13177
|
+
for (const [id, row] of rows) if (!live.has(id)) {
|
|
13178
|
+
row.root.remove();
|
|
13179
|
+
rows.delete(id);
|
|
13180
|
+
}
|
|
13181
|
+
for (const session of snapshots) {
|
|
13182
|
+
let row = rows.get(session.sessionId);
|
|
13183
|
+
if (!row) {
|
|
13184
|
+
row = makeRow(doc, service, session.sessionId);
|
|
13185
|
+
rows.set(session.sessionId, row);
|
|
13186
|
+
sessions.append(row.root);
|
|
13187
|
+
}
|
|
13188
|
+
updateRow(row, session, service);
|
|
13189
|
+
}
|
|
13190
|
+
});
|
|
13191
|
+
return () => {
|
|
13192
|
+
off();
|
|
13193
|
+
host.remove();
|
|
13194
|
+
};
|
|
13195
|
+
}
|
|
12667
13196
|
|
|
12668
13197
|
// src/simulation.ts
|
|
12669
13198
|
var PajaSimulationError = class extends Error {
|
|
@@ -13241,6 +13770,18 @@ function installPajaResourceSettings() {
|
|
|
13241
13770
|
}
|
|
13242
13771
|
|
|
13243
13772
|
// ../shell/dist/index.js
|
|
13773
|
+
function randomUUID() {
|
|
13774
|
+
const provider = globalThis.crypto;
|
|
13775
|
+
if (typeof provider?.randomUUID === "function") return provider.randomUUID();
|
|
13776
|
+
if (typeof provider?.getRandomValues !== "function") {
|
|
13777
|
+
throw new Error("@kehto/shell requires crypto.randomUUID or crypto.getRandomValues for UUID generation");
|
|
13778
|
+
}
|
|
13779
|
+
const bytes = provider.getRandomValues(new Uint8Array(16));
|
|
13780
|
+
bytes[6] = bytes[6] & 15 | 64;
|
|
13781
|
+
bytes[8] = bytes[8] & 63 | 128;
|
|
13782
|
+
const hex = Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
13783
|
+
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
|
|
13784
|
+
}
|
|
13244
13785
|
function bytesToHex3(bytes) {
|
|
13245
13786
|
return Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("");
|
|
13246
13787
|
}
|
|
@@ -13305,7 +13846,7 @@ function createCacheAdapter(shellHooks) {
|
|
|
13305
13846
|
async query(filters) {
|
|
13306
13847
|
const workerRelay = shellHooks.workerRelay.getWorkerRelay();
|
|
13307
13848
|
if (!workerRelay) return [];
|
|
13308
|
-
const subId =
|
|
13849
|
+
const subId = randomUUID();
|
|
13309
13850
|
return workerRelay.query(["REQ", subId, ...filters]);
|
|
13310
13851
|
},
|
|
13311
13852
|
store(event) {
|
|
@@ -13352,7 +13893,7 @@ function createCryptoAdapter(shellHooks) {
|
|
|
13352
13893
|
return shellHooks.crypto.verifyEvent(event);
|
|
13353
13894
|
},
|
|
13354
13895
|
randomUUID() {
|
|
13355
|
-
return
|
|
13896
|
+
return randomUUID();
|
|
13356
13897
|
},
|
|
13357
13898
|
randomBytes(length) {
|
|
13358
13899
|
const bytes = new Uint8Array(length);
|
|
@@ -14363,7 +14904,7 @@ function registerNip5dSessionIfNeeded({
|
|
|
14363
14904
|
dTag: identity.dTag,
|
|
14364
14905
|
aggregateHash: identity.aggregateHash,
|
|
14365
14906
|
registeredAt: Date.now(),
|
|
14366
|
-
instanceId:
|
|
14907
|
+
instanceId: randomUUID(),
|
|
14367
14908
|
provenance: "nip-5d"
|
|
14368
14909
|
};
|
|
14369
14910
|
runtime.sessionRegistry.register(windowId, entry);
|
|
@@ -15411,36 +15952,90 @@ function nappletNamespacePrelude(domains, normalizeIntentUri2) {
|
|
|
15411
15952
|
};
|
|
15412
15953
|
}
|
|
15413
15954
|
function makeMedia() {
|
|
15955
|
+
const sessions = /* @__PURE__ */ new Set();
|
|
15956
|
+
const ended = /* @__PURE__ */ new Map();
|
|
15957
|
+
const retained = /* @__PURE__ */ new Map();
|
|
15958
|
+
const callbacks = /* @__PURE__ */ new Map();
|
|
15959
|
+
const warnings = /* @__PURE__ */ new Set();
|
|
15960
|
+
const warn = (name, replacement) => {
|
|
15961
|
+
if (warnings.has(name)) return;
|
|
15962
|
+
warnings.add(name);
|
|
15963
|
+
console.warn(`[KEHTO-MEDIA-${name}] Deprecated media API; use ${replacement}. Warned once per endpoint.`);
|
|
15964
|
+
};
|
|
15965
|
+
listen((event) => {
|
|
15966
|
+
if (!isParentMessage(event)) return;
|
|
15967
|
+
const msg = event.data;
|
|
15968
|
+
if (!msg || typeof msg !== "object" || typeof msg.sessionId !== "string") return;
|
|
15969
|
+
const sessionId = msg.sessionId;
|
|
15970
|
+
if (msg.type === "media.session.create.result" && !msg.error) {
|
|
15971
|
+
if (!ended.has(sessionId)) sessions.add(sessionId);
|
|
15972
|
+
return;
|
|
15973
|
+
}
|
|
15974
|
+
if (!sessions.has(sessionId) || ended.has(sessionId)) return;
|
|
15975
|
+
if (msg.type === "media.session.ended") {
|
|
15976
|
+
ended.set(sessionId, msg);
|
|
15977
|
+
retained.delete(sessionId);
|
|
15978
|
+
sessions.delete(sessionId);
|
|
15979
|
+
} else if (["media.state", "media.capabilities", "media.controls"].includes(msg.type)) {
|
|
15980
|
+
const cache2 = retained.get(sessionId) ?? /* @__PURE__ */ new Map();
|
|
15981
|
+
cache2.set(msg.type, msg);
|
|
15982
|
+
retained.set(sessionId, cache2);
|
|
15983
|
+
} else if (msg.type !== "media.command") return;
|
|
15984
|
+
const delivery = Array.from(callbacks.get(sessionId)?.get(msg.type) ?? []);
|
|
15985
|
+
for (const callback of delivery) callback(msg);
|
|
15986
|
+
if (ended.has(sessionId)) callbacks.delete(sessionId);
|
|
15987
|
+
});
|
|
15414
15988
|
const on = (type, sessionId, callback) => {
|
|
15415
|
-
const
|
|
15416
|
-
|
|
15417
|
-
|
|
15418
|
-
|
|
15989
|
+
const terminal = ended.get(sessionId);
|
|
15990
|
+
if (terminal) {
|
|
15991
|
+
if (type === "media.session.ended") callback(terminal);
|
|
15992
|
+
return subscriptionHandle(() => {
|
|
15993
|
+
});
|
|
15994
|
+
}
|
|
15995
|
+
const byType = callbacks.get(sessionId) ?? /* @__PURE__ */ new Map();
|
|
15996
|
+
const handlers = byType.get(type) ?? /* @__PURE__ */ new Set();
|
|
15997
|
+
handlers.add(callback);
|
|
15998
|
+
byType.set(type, handlers);
|
|
15999
|
+
callbacks.set(sessionId, byType);
|
|
16000
|
+
const cached = retained.get(sessionId)?.get(type);
|
|
16001
|
+
if (cached) callback(cached);
|
|
16002
|
+
return subscriptionHandle(() => {
|
|
16003
|
+
handlers.delete(callback);
|
|
15419
16004
|
});
|
|
15420
|
-
return subscriptionHandle(off);
|
|
15421
16005
|
};
|
|
15422
|
-
|
|
16006
|
+
const command = (sessionId, value) => {
|
|
16007
|
+
if (ended.has(sessionId)) return;
|
|
16008
|
+
fire({ ...value && typeof value === "object" ? value : {}, type: "media.command", sessionId });
|
|
16009
|
+
};
|
|
16010
|
+
return Object.freeze({
|
|
15423
16011
|
createSession: (options) => request(
|
|
15424
|
-
{
|
|
16012
|
+
{ ...options && typeof options === "object" ? options : {}, type: "media.session.create" },
|
|
15425
16013
|
"media.session.create.result",
|
|
15426
16014
|
withoutEnvelope,
|
|
15427
16015
|
{ rejectOnError: false }
|
|
15428
16016
|
),
|
|
15429
16017
|
updateSession: (sessionId, metadata) => fire({ type: "media.session.update", sessionId, metadata }),
|
|
15430
16018
|
destroySession: (sessionId) => fire({ type: "media.session.destroy", sessionId }),
|
|
15431
|
-
reportState: (sessionId, state) => fire({
|
|
16019
|
+
reportState: (sessionId, state) => fire({ ...state && typeof state === "object" ? state : {}, type: "media.state", sessionId }),
|
|
15432
16020
|
reportCapabilities: (sessionId, actions) => fire({ type: "media.capabilities", sessionId, actions }),
|
|
15433
|
-
|
|
15434
|
-
|
|
15435
|
-
|
|
15436
|
-
|
|
15437
|
-
|
|
15438
|
-
|
|
15439
|
-
|
|
15440
|
-
|
|
16021
|
+
command,
|
|
16022
|
+
sendCommand: (sessionId, action, value) => {
|
|
16023
|
+
warn("SEND-COMMAND", "media.command(sessionId, { action, value })");
|
|
16024
|
+
command(sessionId, { action, ...value === void 0 ? {} : { value } });
|
|
16025
|
+
},
|
|
16026
|
+
onCommand: (sessionId, callback) => {
|
|
16027
|
+
const legacy = callback.length >= 2;
|
|
16028
|
+
if (legacy) warn("COMMAND-CALLBACK", "media.onCommand(sessionId, command => ...)");
|
|
16029
|
+
return on("media.command", sessionId, (msg) => legacy ? callback(msg.action, msg.value) : callback({ action: msg.action, ...msg.value === void 0 ? {} : { value: msg.value }, ...msg.reason === void 0 ? {} : { reason: msg.reason } }));
|
|
16030
|
+
},
|
|
16031
|
+
onState: (sessionId, callback) => on("media.state", sessionId, (msg) => {
|
|
16032
|
+
const { type: _type, sessionId: _sessionId, ...state } = msg;
|
|
16033
|
+
callback(state);
|
|
16034
|
+
}),
|
|
15441
16035
|
onCapabilities: (sessionId, callback) => on("media.capabilities", sessionId, (msg) => callback(msg.actions)),
|
|
15442
|
-
onControls: (sessionId, callback) => on("media.controls", sessionId, (msg) => callback(msg.controls))
|
|
15443
|
-
|
|
16036
|
+
onControls: (sessionId, callback) => on("media.controls", sessionId, (msg) => callback(msg.controls)),
|
|
16037
|
+
onEnded: (sessionId, callback) => on("media.session.ended", sessionId, (msg) => callback({ sessionId: msg.sessionId, reason: msg.reason }))
|
|
16038
|
+
});
|
|
15444
16039
|
}
|
|
15445
16040
|
function makeNotify() {
|
|
15446
16041
|
const on = (type, callback) => {
|
|
@@ -16078,10 +16673,12 @@ function nappletNamespacePrelude(domains, normalizeIntentUri2) {
|
|
|
16078
16673
|
intent ??= Object.freeze(makeIntent());
|
|
16079
16674
|
return intent;
|
|
16080
16675
|
}
|
|
16676
|
+
let media;
|
|
16081
16677
|
function isProtectedDomain(domain) {
|
|
16082
|
-
return domain === "gamepad" || domain === "shell" || domain === "inc" || domain === "identity" || domain === "theme" || domain === "intent";
|
|
16678
|
+
return domain === "media" || domain === "gamepad" || domain === "shell" || domain === "inc" || domain === "identity" || domain === "theme" || domain === "intent";
|
|
16083
16679
|
}
|
|
16084
16680
|
function makeDomain(domain, existing) {
|
|
16681
|
+
if (domain === "media") return media ??= makeMedia();
|
|
16085
16682
|
if (domain === "gamepad") return gamepad ??= makeGamepad();
|
|
16086
16683
|
if (domain === "shell") return shell;
|
|
16087
16684
|
if (domain === "inc") return makeProtectedInc(existing);
|
|
@@ -16096,8 +16693,6 @@ function nappletNamespacePrelude(domains, normalizeIntentUri2) {
|
|
|
16096
16693
|
return makeStorage();
|
|
16097
16694
|
case "keys":
|
|
16098
16695
|
return makeKeys();
|
|
16099
|
-
case "media":
|
|
16100
|
-
return makeMedia();
|
|
16101
16696
|
case "notify":
|
|
16102
16697
|
return makeNotify();
|
|
16103
16698
|
case "config":
|
|
@@ -21636,6 +22231,7 @@ function createDevServices(backend, getSimulation, relayConfig, onThemeService,
|
|
|
21636
22231
|
});
|
|
21637
22232
|
void socialCache.refreshActiveIdentity();
|
|
21638
22233
|
const resourceService = createResourceService({
|
|
22234
|
+
stream: createBrowserMediaStreamFetch(),
|
|
21639
22235
|
fetch: createPajaResourceFetch({
|
|
21640
22236
|
getBlossomServers: ({ url, windowId }) => blossomEventResolver.getServers(url, windowId)
|
|
21641
22237
|
}),
|
|
@@ -21715,7 +22311,14 @@ function createDevServices(backend, getSimulation, relayConfig, onThemeService,
|
|
|
21715
22311
|
if (getSimulation().notifications.enabled && notifyOptions) {
|
|
21716
22312
|
services.notify = createNotifyService(notifyOptions);
|
|
21717
22313
|
}
|
|
21718
|
-
if (getSimulation().media.enabled && typeof
|
|
22314
|
+
if (getSimulation().media.enabled && typeof document !== "undefined" && document.body) {
|
|
22315
|
+
const media = createMediaService({ playback: createBrowserMediaPlayback({ resource: resourceService, stream: resourceService.openStream }) });
|
|
22316
|
+
const unmount = mountMediaPlayer(media);
|
|
22317
|
+
services.media = { ...media, onUnregistered() {
|
|
22318
|
+
unmount();
|
|
22319
|
+
media.destroy();
|
|
22320
|
+
} };
|
|
22321
|
+
}
|
|
21719
22322
|
if (getSimulation().capabilities.domains.theme) services.theme = theme.handler;
|
|
21720
22323
|
if (getSimulation().capabilities.domains.config && config) services.config = config.handler;
|
|
21721
22324
|
if (getSimulation().cvm.enabled && getSimulation().relay.mode === "live" && backend.isAvailable()) {
|
|
@@ -23757,22 +24360,22 @@ function renderShareButton(state, tab) {
|
|
|
23757
24360
|
share.textContent = "\u2197";
|
|
23758
24361
|
share.addEventListener("click", (event) => {
|
|
23759
24362
|
event.stopPropagation();
|
|
23760
|
-
|
|
23761
|
-
else void shareRuntimeTab(tab, share).catch((error) => console.error(error));
|
|
24363
|
+
openShareDialog(state, tab, share);
|
|
23762
24364
|
});
|
|
23763
24365
|
share.addEventListener("keydown", (event) => {
|
|
23764
24366
|
if (event.key !== "Enter" && event.key !== " ") return;
|
|
23765
24367
|
event.preventDefault();
|
|
23766
24368
|
event.stopPropagation();
|
|
23767
|
-
|
|
23768
|
-
else void shareRuntimeTab(tab, share).catch((error) => console.error(error));
|
|
24369
|
+
openShareDialog(state, tab, share);
|
|
23769
24370
|
});
|
|
23770
24371
|
return share;
|
|
23771
24372
|
}
|
|
23772
24373
|
function openShareDialog(state, tab, opener) {
|
|
23773
|
-
const dialog = document.createElement("
|
|
23774
|
-
dialog.className = "
|
|
24374
|
+
const dialog = document.createElement("div");
|
|
24375
|
+
dialog.className = "dialog-backdrop";
|
|
23775
24376
|
dialog.id = "paja-share-dialog";
|
|
24377
|
+
dialog.setAttribute("role", "dialog");
|
|
24378
|
+
dialog.setAttribute("aria-modal", "true");
|
|
23776
24379
|
const panel = document.createElement("div");
|
|
23777
24380
|
panel.className = "dialog";
|
|
23778
24381
|
const title = document.createElement("div");
|
|
@@ -23782,7 +24385,18 @@ function openShareDialog(state, tab, opener) {
|
|
|
23782
24385
|
dialog.setAttribute("aria-labelledby", title.id);
|
|
23783
24386
|
const description = document.createElement("div");
|
|
23784
24387
|
description.className = "dialog-copy";
|
|
23785
|
-
|
|
24388
|
+
const offersIntents = canCreatePajaIntentLink(state, tab);
|
|
24389
|
+
description.textContent = offersIntents ? `Share ${tab.title} as an app link or create an intent link.` : `Share ${tab.title} as an app link.`;
|
|
24390
|
+
const linkLabel = document.createElement("label");
|
|
24391
|
+
linkLabel.className = "config-field";
|
|
24392
|
+
linkLabel.textContent = "App link";
|
|
24393
|
+
const link = document.createElement("input");
|
|
24394
|
+
link.type = "text";
|
|
24395
|
+
link.readOnly = true;
|
|
24396
|
+
link.value = createPajaShareUrl(tab.pointerValue);
|
|
24397
|
+
linkLabel.append(link);
|
|
24398
|
+
const status = document.createElement("div");
|
|
24399
|
+
status.setAttribute("role", "status");
|
|
23786
24400
|
const actions = document.createElement("div");
|
|
23787
24401
|
actions.className = "dialog-actions";
|
|
23788
24402
|
const copy = document.createElement("button");
|
|
@@ -23794,39 +24408,50 @@ function openShareDialog(state, tab, opener) {
|
|
|
23794
24408
|
const cancel = document.createElement("button");
|
|
23795
24409
|
cancel.type = "button";
|
|
23796
24410
|
cancel.textContent = "Cancel";
|
|
23797
|
-
actions.append(copy, create, cancel);
|
|
23798
|
-
panel.append(title, description, actions);
|
|
24411
|
+
actions.append(copy, ...offersIntents ? [create] : [], cancel);
|
|
24412
|
+
panel.append(title, description, linkLabel, status, actions);
|
|
23799
24413
|
dialog.append(panel);
|
|
24414
|
+
const siblings = Array.from(document.body.children).filter((node) => node instanceof HTMLElement).map((node) => ({ node, inert: node.inert }));
|
|
24415
|
+
for (const { node } of siblings) node.inert = true;
|
|
23800
24416
|
document.body.append(dialog);
|
|
23801
24417
|
const retainModalFocus = (event) => {
|
|
23802
|
-
if (!dialog.
|
|
24418
|
+
if (!dialog.isConnected) return;
|
|
23803
24419
|
const next = event.relatedTarget;
|
|
23804
24420
|
if (next instanceof Node && dialog.contains(next)) return;
|
|
23805
24421
|
queueMicrotask(() => {
|
|
23806
|
-
if (dialog.
|
|
24422
|
+
if (dialog.isConnected && !dialog.matches(":focus-within")) copy.focus();
|
|
23807
24423
|
});
|
|
23808
24424
|
};
|
|
23809
24425
|
const close = (restoreFocus = true) => {
|
|
23810
|
-
if (dialog.open) dialog.close();
|
|
23811
24426
|
dialog.removeEventListener("focusout", retainModalFocus);
|
|
23812
24427
|
dialog.remove();
|
|
24428
|
+
for (const { node, inert } of siblings) node.inert = inert;
|
|
23813
24429
|
if (restoreFocus) opener.focus();
|
|
23814
24430
|
};
|
|
23815
|
-
|
|
23816
|
-
event.
|
|
23817
|
-
|
|
23818
|
-
|
|
24431
|
+
dialog.addEventListener("keydown", (event) => {
|
|
24432
|
+
if (event.key === "Escape") {
|
|
24433
|
+
event.preventDefault();
|
|
24434
|
+
close();
|
|
24435
|
+
return;
|
|
24436
|
+
}
|
|
24437
|
+
if (event.key !== "Tab") return;
|
|
24438
|
+
const controls = [link, copy, ...offersIntents ? [create] : [], cancel];
|
|
24439
|
+
const next = event.shiftKey ? controls.at(-1) : controls[0];
|
|
24440
|
+
const edge = event.shiftKey ? controls[0] : controls.at(-1);
|
|
24441
|
+
if (document.activeElement === edge) {
|
|
24442
|
+
event.preventDefault();
|
|
24443
|
+
next.focus();
|
|
24444
|
+
}
|
|
24445
|
+
});
|
|
23819
24446
|
cancel.addEventListener("click", () => close());
|
|
23820
24447
|
copy.addEventListener("click", () => {
|
|
23821
|
-
void
|
|
24448
|
+
void copyShareLink(link, status);
|
|
23822
24449
|
});
|
|
23823
24450
|
create.addEventListener("click", () => {
|
|
23824
24451
|
close();
|
|
23825
24452
|
state.createIntentLink?.(tab);
|
|
23826
24453
|
});
|
|
23827
|
-
dialog.addEventListener("cancel", onCancel);
|
|
23828
24454
|
dialog.addEventListener("focusout", retainModalFocus);
|
|
23829
|
-
dialog.showModal();
|
|
23830
24455
|
copy.focus();
|
|
23831
24456
|
}
|
|
23832
24457
|
function renderCloseButton(state, tab) {
|
|
@@ -23847,24 +24472,15 @@ function renderCloseButton(state, tab) {
|
|
|
23847
24472
|
});
|
|
23848
24473
|
return close;
|
|
23849
24474
|
}
|
|
23850
|
-
async function
|
|
23851
|
-
const shareUrl = createPajaShareUrl(tab.pointerValue);
|
|
23852
|
-
const clipboard = navigator.clipboard;
|
|
23853
|
-
if (!clipboard) {
|
|
23854
|
-
window.prompt("Copy Paja share link", shareUrl);
|
|
23855
|
-
return;
|
|
23856
|
-
}
|
|
24475
|
+
async function copyShareLink(link, status) {
|
|
23857
24476
|
try {
|
|
23858
|
-
await clipboard.writeText(
|
|
24477
|
+
await navigator.clipboard.writeText(link.value);
|
|
24478
|
+
status.textContent = "App link copied.";
|
|
23859
24479
|
} catch {
|
|
23860
|
-
|
|
23861
|
-
|
|
24480
|
+
link.focus();
|
|
24481
|
+
link.select();
|
|
24482
|
+
status.textContent = "Select and copy the app link.";
|
|
23862
24483
|
}
|
|
23863
|
-
const originalTitle = button2.title;
|
|
23864
|
-
button2.title = "Share link copied";
|
|
23865
|
-
window.setTimeout(() => {
|
|
23866
|
-
button2.title = originalTitle || "Copy app link";
|
|
23867
|
-
}, 1200);
|
|
23868
24484
|
}
|
|
23869
24485
|
function getPointerParamName(pointer) {
|
|
23870
24486
|
if (pointer.startsWith("naddr")) return "naddr";
|