Hub 2.0 step 2: protocol v2 next to v1

- Accepts protocol 1 and 2 (utils/protocol.js); v2 bodies checked with the
  contracts' zod schemas (400 INVALID_REQUEST), v1 unchanged
- v2 register/heartbeat carry product (409 WRONG_PRODUCT on a mismatch),
  register stores the install's public key, stats stored per product
- v2 replies add linkedProducts (the church's other installs in use with a
  public key); v1 replies unchanged
- Licence tokens carry customerId and product
- Until releases per product, only Events installs get releases / "behind"
- Tests: every contract sample through the real routes, replies checked
  against the contract response schemas

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-04 15:37:02 +02:00
co-authored by Claude Opus 5.5
parent 56370419bb
commit ab6a0c6914
6 changed files with 378 additions and 19 deletions
+4
View File
@@ -35,6 +35,10 @@ function buildPayload(licence, install, customerName, now = new Date()) {
v: 1,
licenceId: licence.id,
installId: install.id,
// Hub 2.0 (contracts licencePayloadSchema): optional fields, still v 1. An install refuses a
// token for another product; Cross Events 2.1 ignores both.
customerId: licence.customerId,
product: licence.product || 'events',
customer: customerName,
plan: licence.plan,
status: licence.status,
+55
View File
@@ -0,0 +1,55 @@
/**
* Hub protocol versions (Hub 2.0). v1 is Cross Events 2.1 (docs/hub-protocol.md); v2 is every
* Cross Code product, defined in @crosscode/contracts (`hub`). The Hub accepts both, so Events
* installs keep reporting until they update; v1 requests and replies are exactly as before.
*
* v2 adds: `product` on register and heartbeat (stats per product), the install's public key,
* and `linkedProducts` in the replies. v2 bodies are checked with the contracts' zod schemas:
* unknown fields are dropped, anything missing or malformed is `400 INVALID_REQUEST`.
*/
const { hub } = require('@crosscode/contracts');
const prisma = require('../config/db');
const PROTOCOL_VERSIONS = [...hub.HUB_PROTOCOL_VERSIONS];
const isV2 = body => body?.protocolVersion === 2;
/** "install.url: Invalid URL" for the first problem zod found. */
function describeIssues(error) {
const issue = error.issues[0];
if (!issue) return 'The request is not valid.';
const where = issue.path.length ? `${issue.path.join('.')}: ` : '';
return `${where}${issue.message}`;
}
/**
* A v2 body checked against a contract schema: { ok: true, data } or { ok: false, status, code,
* message }. v1 bodies aren't checked here (they keep the v1 handling).
*/
function parseV2(schema, body) {
const result = schema.safeParse(body);
if (result.success) return { ok: true, data: result.data };
return { ok: false, status: 400, code: 'INVALID_REQUEST', message: describeIssues(result.error) };
}
/**
* The church's other installs, for v2 replies: product, id, URL and public key, nothing else.
* Only installs that are in use (not released, licence not revoked) and have sent a public key
* (protocol v2) are listed, so a sibling can always verify what it's sent.
*/
async function linkedProductsFor(install, db = prisma) {
const others = await db.install.findMany({
where: {
customerId: install.customerId,
id: { not: install.id },
releasedAt: null,
publicKey: { not: null },
licence: { is: { status: { not: 'revoked' } } },
},
select: { id: true, product: true, url: true, publicKey: true },
orderBy: { registeredAt: 'asc' },
});
return others.map(o => ({ product: o.product, installId: o.id, url: o.url, publicKey: o.publicKey }));
}
module.exports = { PROTOCOL_VERSIONS, isV2, parseV2, describeIssues, linkedProductsFor };