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

+30 -25
View File
@@ -1,4 +1,5 @@
import * as Cesium from 'cesium';
import { findCredits } from '../render/rendererOf.js';
/**
* Per-layer data attribution registered into Cesium's credit display.
@@ -36,6 +37,20 @@ export const OPENMAPTILES_CREDIT = {
const osmDisplays = new WeakMap();
/** The short inline OSM credits, as the renderer's credits port takes them. */
const OSM_INLINE_CREDITS = Object.freeze({
osm: Object.freeze({
text: '© OpenStreetMap',
href: 'https://www.openstreetmap.org/copyright',
inline: true,
}),
tiles: Object.freeze({
text: '© OpenMapTiles',
href: 'https://openmaptiles.org',
inline: true,
}),
});
function updateOsmDisplay(viewer, state) {
const osm = state.owners.size > 0;
let tiles = false;
@@ -44,37 +59,27 @@ function updateOsmDisplay(viewer, state) {
['osm', osm],
['tiles', tiles],
]) {
if (state[key].visible === visible) continue;
state[key].visible = visible;
viewer.creditDisplay[visible ? 'addStaticCredit' : 'removeStaticCredit'](
state[key].credit,
);
viewer.scene?.requestRender?.();
if (Boolean(state.offs[key]) === visible) continue;
if (visible) {
// Looked up each time: a renderer bound later takes over.
const credits = findCredits(viewer);
if (credits) state.offs[key] = credits.addStatic(OSM_INLINE_CREDITS[key]);
} else {
state.offs[key]();
state.offs[key] = null;
}
}
}
/** Keep one short inline credit while an OSM display owner has visible data. */
/**
* Keep one short inline credit, through the renderer's credits port, while
* an OSM display owner has visible data.
*/
export function showOsmCredit(viewer, owner, { openMapTiles = false } = {}) {
if (!viewer?.creditDisplay?.addStaticCredit || !owner) return false;
if (!owner || !findCredits(viewer)) return false;
let state = osmDisplays.get(viewer);
if (!state) {
state = {
owners: new Map(),
osm: {
visible: false,
credit: new Cesium.Credit(
'<a href="https://www.openstreetmap.org/copyright" target="_blank" rel="noopener">© OpenStreetMap</a>',
true,
),
},
tiles: {
visible: false,
credit: new Cesium.Credit(
'<a href="https://openmaptiles.org" target="_blank" rel="noopener">© OpenMapTiles</a>',
true,
),
},
};
state = { owners: new Map(), offs: { osm: null, tiles: null } };
osmDisplays.set(viewer, state);
}
if (state.owners.get(owner) === openMapTiles) return false;
+5 -4
View File
@@ -1,6 +1,7 @@
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { DATA_CREDITS, NYC_DOT_DISCLAIMER } from './dataCredits.js';
import { bindFakeCesiumCredits } from '../testSupport/fakeCreditsPort.mjs';
test('every credit carries a unique key and some markup to render', () => {
const keys = DATA_CREDITS.map((entry) => entry.key);
@@ -60,13 +61,13 @@ test('inline OSM attribution persists until its last display owner leaves', asyn
const { showOsmCredit, hideOsmCredit } = await import('./dataCredits.js');
t.mock.timers.enable({ apis: ['setTimeout'] });
const visible = new Set();
const viewer = {
const viewer = bindFakeCesiumCredits({
creditDisplay: {
addStaticCredit: (c) => visible.add(c),
removeStaticCredit: (c) => visible.delete(c),
},
scene: { requestRender() {} },
};
});
assert.equal(showOsmCredit(viewer, 'alpr'), true);
assert.equal(showOsmCredit(viewer, 'alpr'), false);
assert.equal(visible.size, 1);
@@ -100,13 +101,13 @@ test('tile attribution follows source changes and viewers have independent owner
const { showOsmCredit, hideOsmCredit } = await import('./dataCredits.js');
const create = () => {
const credits = new Set();
return {
return bindFakeCesiumCredits({
credits,
creditDisplay: {
addStaticCredit: (c) => credits.add(c),
removeStaticCredit: (c) => credits.delete(c),
},
};
});
};
const a = create(),
b = create();
+2 -1
View File
@@ -2,6 +2,7 @@ import test from 'node:test';
import assert from 'node:assert/strict';
import * as C from 'cesium';
import { showOsmCredit, hideOsmCredit } from './dataCredits.js';
import { bindFakeCesiumCredits } from '../testSupport/fakeCreditsPort.mjs';
import { createInfrastructureLayers } from './infrastructure.js';
import { createBhoteKoshiLocatorLayer } from './bhoteKoshiLocator.js';
import { createWorldAnnotationRenderer } from '../annotations/worldAnnotationRenderer.js';
@@ -31,7 +32,7 @@ function viewer(t) {
scene: { requestRender: noop, canvas: { addEventListener: noop, removeEventListener: noop }, preRender: new C.Event(),
primitives: { add: (p) => p, remove: noop }, screenSpaceCameraController: { enableInputs: true } },
};
return v;
return bindFakeCesiumCredits(v);
}
function assertCredit(v) {
assert.ok(v.credits.some((credit) => credit.showOnScreen && credit.html.includes('>© OpenStreetMap<')));