feat(vessels): key vessels by their source's stable id, and share a selected vessel

AIS Vessels are keyed by the id their live source gives, with the MMSI as an
attribute: an MMSI can be shared, spoofed or reused, so two vessels with one
MMSI are now two vessels. Looking a vessel up or selecting it by MMSI still
finds it.

A share link made while a vessel is selected opens that vessel. A UUID vessel
id travels as 16 bytes in a new thing field (13). A shared thing that is not
reporting now says so in a sentence per kind, with its id (ICAO, NORAD, MMSI
or vessel UUID) on a second line.
This commit is contained in:
Sameh Khamis committed 2026-11-10 18:12:53 -08:00
1 parent 57ff81f723
commit cbbf55cacd
74 files changed
+2481 -586

No files matched your search

+60 -5
View File
@@ -62,7 +62,9 @@ import {
export {
SHARE_VIEW_ENTITY_KINDS,
SHARE_VIEW_FOLLOW_OFFSET_DEFAULT,
SHARE_VIEW_FOLLOW_RANGE_MAX_M,
followCameraFor,
followOffsetFor,
followOffsetFromEnu,
followOffsetToEnu,
} from './follow.js';
@@ -387,8 +389,9 @@ const fieldNumber = (key) => FIELD_BY_KEY.get(key).number;
/**
* Fields inside the thing message (top-level field 13). Permanent. Fields 1–4
* are the entity ids, one per SHARE_VIEW_ENTITY_KINDS code; exactly one is
* present. The follow offset and the last-known fix are flat fields here, not
* are the entity ids, one per SHARE_VIEW_ENTITY_KINDS code, and field 13 is a
* vessel id that is a UUID, as its 16 bytes; exactly one of them is present.
* The follow offset and the last-known fix are flat fields here, not
* sub-messages, to save their tag and length bytes.
*/
export const SHARE_VIEW_THING_FIELDS = Object.freeze({
@@ -400,8 +403,17 @@ export const SHARE_VIEW_THING_FIELDS = Object.freeze({
alt: 10, // sint metres, its last-known altitude
ageS: 11, // sint, seconds the fix predates the created-at time (field 8)
observedAt: 12, // epoch, when the fix was taken; only without a created-at
vesselUuid: 13, // LEN, 16 bytes: a vessel id that is a UUID
});
/**
* A vessel id that is a UUID, as a source's stable id may be: lowercase,
* dashed. It travels as its 16 bytes (thing field 13), not as text.
*/
export const SHARE_VIEW_UUID_GRAMMAR =
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
const UUID_BYTES = 16;
/** The default of a field or flag for a view in `style`. */
function defaultOf(entry, style) {
if (entry.default !== BY_STYLE) return entry.default;
@@ -490,7 +502,10 @@ function normalizeEntityId(entry, value) {
: typeof value === 'string'
? value.trim().toLowerCase()
: null;
if (raw === null || !TRACKING_ID_GRAMMAR.test(raw)) return null;
if (raw === null) return null;
// A vessel's source may key it by a UUID, longer than the id grammar.
if (entry.kind === 'vessel' && SHARE_VIEW_UUID_GRAMMAR.test(raw)) return raw;
if (!TRACKING_ID_GRAMMAR.test(raw)) return null;
if (entry.kind === 'satellite' && !/^[1-9]\d{0,14}$/.test(raw)) return null;
return raw;
}
@@ -795,6 +810,31 @@ const REGISTRY_BY_TOKEN = new Map(
const charNumber = (char) => LAYER_STATE_TOKEN_ALPHABET.indexOf(char);
const CANONICAL_INTEGER = /^(0|-?[1-9]\d{0,14})$/;
/** The 16 bytes of a UUID in SHARE_VIEW_UUID_GRAMMAR. */
function uuidBytes(id) {
const hex = id.replaceAll('-', '');
return Array.from({ length: UUID_BYTES }, (_, i) =>
Number.parseInt(hex.slice(i * 2, i * 2 + 2), 16),
);
}
/** The lowercase dashed UUID that `bytes` spell. */
function uuidText(bytes) {
const hex = Array.from(bytes, (byte) =>
byte.toString(16).padStart(2, '0'),
).join('');
return [
hex.slice(0, 8),
hex.slice(8, 12),
hex.slice(12, 16),
hex.slice(16, 20),
hex.slice(20),
].join('-');
}
const isVesselUuid = (entry, id) =>
entry.kind === 'vessel' && SHARE_VIEW_UUID_GRAMMAR.test(id);
function writeEntityId(writer, entry, id) {
const field = entry.code;
if (entry.idForm === 'hex24' && /^[0-9a-f]{6}$/.test(id))
@@ -895,7 +935,8 @@ function encodeThing(writer, view, state) {
const f = SHARE_VIEW_THING_FIELDS;
const d = SHARE_VIEW_FOLLOW_OFFSET_DEFAULT;
const thing = new WireWriter();
writeEntityId(thing, entry, view.entity.id);
const uuid = isVesselUuid(entry, view.entity.id);
if (!uuid) writeEntityId(thing, entry, view.entity.id);
const { range, heading, pitch } = view.offset;
if (range !== d.range) thing.uintField(f.range, range);
if (heading !== d.heading) thing.sintField(f.heading, signedAngle(heading));
@@ -914,6 +955,8 @@ function encodeThing(writer, view, state) {
} else if (observed !== null)
thing.sintField(f.observedAt, epochSeconds(observed));
}
// Fields go in ascending order, so the UUID id (field 13) comes last.
if (uuid) thing.bytesField(f.vesselUuid, uuidBytes(view.entity.id));
writer.bytesField(fieldNumber('thing'), thing.finish());
}
@@ -1063,7 +1106,9 @@ const TOP_LEVEL_WIRE_TYPES = new Map(
const THING_WIRE_TYPES = new Map([
...SHARE_VIEW_ENTITY_KINDS.map((e) => [e.code, [WIRE_VARINT, WIRE_LEN]]),
...Object.values(SHARE_VIEW_THING_FIELDS).map((n) => [n, [WIRE_VARINT]]),
[SHARE_VIEW_THING_FIELDS.vesselUuid, [WIRE_LEN]],
]);
const VESSEL_ENTRY = SHARE_VIEW_ENTITY_KINDS.find((e) => e.kind === 'vessel');
const unz = unzigzag;
const CLOSED_ENUMS = new Set(['detectionMode', 'detectionAllocation']);
@@ -1258,8 +1303,18 @@ function decodeThing(reader, createdAtS, unknown) {
? followCameraFor(lastKnown, normalizeFollowOffset(offset))
: null;
const present = SHARE_VIEW_ENTITY_KINDS.filter((e) => fields.has(e.code));
if (present.length > 1)
const uuid = fields.get(F.vesselUuid);
if (present.length + (uuid ? 1 : 0) > 1)
throw new ShareWireError('duplicate-field', 'thing entity id');
if (uuid) {
const bytes = uuid.len.rest();
if (bytes.length !== UUID_BYTES)
throw new ShareWireError('bad-length', 'thing vessel id');
return {
camera,
view: thingView(VESSEL_ENTRY.kind, uuidText(bytes), offset, lastKnown),
};
}
const entry = present[0];
if (!entry) {
// A newer app's entity kind uses a thing field this decoder skipped.