Add layer and map credits through the renderer's credits port

The street-level provider credits, the map source credit, the shared
inline OpenStreetMap and OpenMapTiles credits and the ALPR provider credit
go through the renderer's credits port instead of Cesium's credit display.
Their markup has one link each and is read as the port's plain data; the
ALPR credit is built as plain data, which the port escapes. src/maps/credits.js
and src/layers/streetLevel/credits.js no longer reach Cesium and leave the
allow-list.

findCredits finds the port as findCamera finds the camera: a Cesium viewer
of a caller's own, with no renderer bound, gets a credits port over its
own credit display, so the public entries keep their attribution. A port
built that way is released, not disposed, when a renderer binds later, and
a credit let go after its viewer is destroyed leaves the display alone.

The per-layer data credits and the transit and event credits stay on
Cesium's display: their markup carries several links each, which the
contract's credit cannot hold.
This commit is contained in:
Sameh Khamis committed 2026-11-06 16:32:23 -08:00
1 parent 979e2213b9
commit 765a0db094
26 files changed
+418 -142

No files matched your search

+2
View File
@@ -10,6 +10,8 @@ import { listenerList, toCartesian, uncaught } from './common.js';
import { cameraFlightActive } from './internals.js';
import { createCesiumFollow } from './follow.js';
import { cameraOf, findCamera, provideUnboundCamera } from '../rendererOf.js';
// Loads the factory an unbound viewer gets its credits port from.
import './credits.js';
/**
* `cameraOf` and `findCamera`, for code that already reaches Cesium:
+41 -6
View File
@@ -24,7 +24,8 @@ import {
creditFromMarkup,
safeUrl,
} from '../creditMarkup.js';
import { listenerList } from './common.js';
import { provideUnboundCredits } from '../rendererOf.js';
import { listenerList, uncaught } from './common.js';
import { creditDisplayContents } from './internals.js';
const CREDIT_KEYS = new Set(['text', 'href', 'logoUrl', 'inline']);
@@ -86,6 +87,9 @@ function copyCredit(credit) {
*/
export function createCesiumCredits({ display, scene, report, requestRender }) {
const listeners = listenerList(report);
// A viewer may be destroyed before the code holding credits lets
// them go; a destroyed display throws on every call.
const displayLive = () => Boolean(display) && !display.isDestroyed?.();
/** The page's credits, in the order added: each `{credit, markup}`. */
const added = [];
/** The page's credits as Cesium shows them, by markup: `{credit, count}`. */
@@ -120,7 +124,9 @@ export function createCesiumCredits({ display, scene, report, requestRender }) {
* leaves it out.
*/
function shownCredits() {
const { statics, screen, lightbox, logo } = creditDisplayContents(display);
const { statics, screen, lightbox, logo } = creditDisplayContents(
displayLive() ? display : undefined,
);
const out = [];
const seen = new Set();
const take = (credit) => {
@@ -204,7 +210,7 @@ export function createCesiumCredits({ display, scene, report, requestRender }) {
entry = { credit: new Cesium.Credit(markup, inline), count: 0 };
shown.set(key, entry);
coreCredits.add(entry.credit);
display?.addStaticCredit(entry.credit);
if (displayLive()) display.addStaticCredit(entry.credit);
}
entry.count += 1;
return key;
@@ -217,7 +223,7 @@ export function createCesiumCredits({ display, scene, report, requestRender }) {
if (entry.count > 0) return;
shown.delete(key);
coreCredits.delete(entry.credit);
display?.removeStaticCredit(entry.credit);
if (displayLive()) display.removeStaticCredit(entry.credit);
}
let disposed = false;
@@ -251,15 +257,44 @@ export function createCesiumCredits({ display, scene, report, requestRender }) {
return {
port,
/** Detach from the scene and take the page's credits off the display. */
dispose() {
disposed = true;
offFrame();
listeners.clear();
if (!display?.isDestroyed?.())
if (displayLive())
for (const { credit } of shown.values())
display?.removeStaticCredit(credit);
display.removeStaticCredit(credit);
shown.clear();
coreCredits.clear();
},
/**
* Detach from the scene only: the credits added stay shown until their
* own Off, for a port another one supersedes.
*/
release() {
offFrame();
listeners.clear();
},
};
}
// A Cesium viewer with no renderer bound to it gets a
// credits port over its credit display when code first asks `findCredits`
// for one. `camera.js` loads this module, so the entries that load the
// unbound camera load this too.
provideUnboundCredits((viewer) =>
// By shape, not `instanceof`: the viewer may come from another
// copy of Cesium.
typeof viewer.creditDisplay?.addStaticCredit === 'function' &&
typeof viewer.scene?.postRender?.addEventListener === 'function'
? createCesiumCredits({
display: viewer.creditDisplay,
scene: viewer.scene,
report: uncaught,
requestRender: () => {
if (!viewer.scene.isDestroyed?.()) viewer.scene.requestRender?.();
},
})
: null,
);
+51
View File
@@ -2,6 +2,7 @@ import assert from 'node:assert/strict';
import test from 'node:test';
import * as Cesium from 'cesium';
import { createCesiumCredits, creditMarkup } from './credits.js';
import { findCredits } from '../rendererOf.js';
/** A credit display as Cesium keeps it: static credits and a frame's. */
function fakeDisplay() {
@@ -271,6 +272,31 @@ test('dispose detaches from the frame and takes the page’s credits off', () =>
assert.equal(t.renders(), 1, 'no render asked of a disposed scene');
});
test('a credit let go after its display is destroyed leaves the display alone', () => {
const t = setup();
const off = t.port.addStatic({ text: 'x' });
t.display.isDestroyed = () => true;
t.display.removeStaticCredit = () => {
throw new Error('destroyed');
};
off();
assert.deepEqual(t.port.list(), []);
t.part.dispose();
});
test('a released port stops following frames and keeps its credits until their Off', () => {
const t = setup();
const seen = [];
t.port.on('change', () => seen.push(1));
const off = t.port.addStatic({ text: 'x' });
t.part.release();
assert.equal(t.listeners.size, 0);
assert.equal(t.display._staticCredits.length, 1, 'still shown');
off();
assert.equal(t.display._staticCredits.length, 0);
assert.deepEqual(seen, [1], 'its listeners are gone');
});
test('credit markup escapes every value and links only web URLs', () => {
assert.equal(
creditMarkup({ text: 'a "b" & <c>' }),
@@ -289,3 +315,28 @@ test('credit markup escapes every value and links only web URLs', () => {
'<a href="https://example.com/?a=1&amp;b=%222%22" target="_blank" rel="noopener">x</a>',
);
});
test('a Cesium viewer with no renderer gets a credits port over its display', () => {
const display = fakeDisplay();
const listeners = new Set();
let renders = 0;
const viewer = {
creditDisplay: display,
scene: {
postRender: {
addEventListener(cb) {
listeners.add(cb);
return () => listeners.delete(cb);
},
},
requestRender: () => (renders += 1),
},
};
const port = findCredits(viewer);
assert.equal(findCredits(viewer), port);
port.addStatic({ text: 'Unbound' });
assert.equal(display._staticCredits.length, 1);
assert.equal(renders, 1);
assert.equal(listeners.size, 1);
assert.equal(findCredits({ creditDisplay: display }), null, 'no scene');
});
-2
View File
@@ -147,7 +147,6 @@
"src/layers/satellites/testing.js",
"src/layers/satellites/tracking.js",
"src/layers/streetLevel/cameraFraming.js",
"src/layers/streetLevel/credits.js",
"src/layers/streetLevel/groundClick.js",
"src/layers/streetLevel/index.js",
"src/layers/streetLevel/lineCoverage.js",
@@ -203,7 +202,6 @@
"src/locations.js",
"src/maps/catalog.js",
"src/maps/controller.js",
"src/maps/credits.js",
"src/maps/google3d.js",
"src/maps/imagery.js",
"src/maps/terrain.js",
+48 -10
View File
@@ -15,6 +15,9 @@ const renderers = new WeakMap();
*/
const unboundCameras = new WeakMap();
let buildUnboundCamera = null;
/** Credits ports built likewise, each `{port, dispose, release}`. */
const unboundCredits = new WeakMap();
let buildUnboundCredits = null;
/**
* Provide the factory `cameraOf` builds a camera port with for a scene
@@ -26,6 +29,17 @@ export function provideUnboundCamera(build) {
buildUnboundCamera = build;
}
/**
* Provide the factory `findCredits` builds a credits port with for a scene
* object no renderer is bound to, as `provideUnboundCamera` does. `release`
* detaches a port a bound renderer supersedes, keeping its credits shown.
* @param {(host: object) => ({port: object, dispose: () => void,
* release: () => void} | null)} build
*/
export function provideUnboundCredits(build) {
buildUnboundCredits = build;
}
/**
* Bind a renderer to its scene object. A camera `cameraOf` built for it
* while it had no renderer is disposed: its listeners hear nothing more,
@@ -39,6 +53,11 @@ export function bindRenderer(host, renderer) {
// The renderer's camera supersedes one built while there was none.
unboundCameras.get(host)?.dispose();
unboundCameras.delete(host);
// Its credits port does too for credits added from now on; one built
// before is released, not disposed: the credits added through it stay
// shown until their own Off.
unboundCredits.get(host)?.release?.();
unboundCredits.delete(host);
renderers.set(host, renderer);
return () => {
if (renderers.get(host) === renderer) renderers.delete(host);
@@ -60,21 +79,40 @@ export const rendererOf = (host) => (host && renderers.get(host)) || null;
* has no renderer and no camera can be built for it, or is destroyed.
*/
export function findCamera(host) {
const camera = rendererOf(host)?.camera;
if (camera) return camera;
return (
rendererOf(host)?.camera ??
findUnbound(host, unboundCameras, buildUnboundCamera)
);
}
/**
* The credits port of the renderer bound to `host`; with no renderer bound,
* one built over `host` by the provided factory and kept with it while
* `host` lives, as `findCamera` does.
* @param {object|null|undefined} host
* @returns {import('./contract/credits').CreditsPort | null}
*/
export function findCredits(host) {
return (
rendererOf(host)?.credits ??
findUnbound(host, unboundCredits, buildUnboundCredits)
);
}
function findUnbound(host, built, build) {
if (!host || typeof host !== 'object') return null;
if (host.isDestroyed?.()) {
// Gone with its scene: let the built camera go with it.
unboundCameras.get(host)?.dispose();
unboundCameras.delete(host);
// Gone with its scene: let the built port go with it.
built.get(host)?.dispose();
built.delete(host);
return null;
}
let built = unboundCameras.get(host);
if (!built && buildUnboundCamera) {
built = buildUnboundCamera(host);
if (built) unboundCameras.set(host, built);
let part = built.get(host);
if (!part && build) {
part = build(host);
if (part) built.set(host, part);
}
return built?.port ?? null;
return part?.port ?? null;
}
/**
+43
View File
@@ -4,7 +4,9 @@ import {
bindRenderer,
cameraOf,
findCamera,
findCredits,
provideUnboundCamera,
provideUnboundCredits,
rendererOf,
} from './rendererOf.js';
@@ -87,3 +89,44 @@ test('a destroyed scene gets no built camera, and lets go of the one it had', (t
assert.throws(() => cameraOf(viewer), /destroyed/);
assert.equal(disposed, 1, 'once');
});
test('with no renderer bound, findCredits builds one port per scene and keeps it working once one is', (t) => {
t.after(() => provideUnboundCredits(null));
const built = [];
provideUnboundCredits((host) => {
if (!host.creditDisplay) return null;
const entry = { port: { id: built.length }, disposed: 0, released: 0 };
entry.dispose = () => (entry.disposed += 1);
entry.release = () => (entry.released += 1);
built.push(entry);
return entry;
});
const viewer = { creditDisplay: {} };
assert.equal(findCredits(viewer), built[0].port);
assert.equal(findCredits(viewer), built[0].port, 'kept, not rebuilt');
assert.equal(findCredits({}), null, 'nothing to build over');
assert.equal(findCredits(null), null);
const credits = { list: () => [] };
const unbind = bindRenderer(viewer, { credits });
assert.equal(findCredits(viewer), credits);
assert.equal(
built[0].disposed,
0,
'the credits added through the built port stay until their Off',
);
assert.equal(built[0].released, 1, 'but it stops following the frames');
unbind();
assert.equal(
findCredits(viewer),
built[1].port,
'unbound again: a fresh one',
);
let destroyed = false;
const gone = { creditDisplay: {}, isDestroyed: () => destroyed };
const port = findCredits(gone);
destroyed = true;
assert.equal(findCredits(gone), null);
assert.equal(built.find((entry) => entry.port === port).disposed, 1);
});
+8 -1
View File
@@ -3,7 +3,8 @@
* `cameraOf` a viewer of the caller's own, with no renderer bound. The
* bridge builds a camera port for it with the factory the Cesium adapter
* provides as `render/cesium/camera.js` loads, so every such entry must load
* that module too.
* that module too. It loads `render/cesium/credits.js`, whose factory
* builds `findCredits`' port for such a viewer the same way.
*
* The graph is every static import, re-export and literal dynamic import of
* the entry's module and those it reaches, read by the parsers the Cesium
@@ -129,6 +130,12 @@ function reachable(entry, { eager = false } = {}) {
return seen;
}
test('the unbound camera factory loads the unbound credits factory', () => {
assert.ok(
reachable(FACTORY, { eager: true }).has('src/render/cesium/credits.js'),
);
});
test('every public entry that reaches the renderer bridge loads the unbound camera factory', () => {
const missing = [];
let bridged = 0;