NODEDC_PLATFORM/packages/n8n-nodes-ndc/nodes/shared/contracts.ts

239 lines
9.3 KiB
TypeScript

import { createHash } from 'node:crypto';
import type { IDataObject, INodeExecutionData, INodeListSearchItems } from 'n8n-workflow';
import {
DATA_PRODUCT_PUBLISH_SCHEMA_VERSION,
FOUNDRY_BINDING_UPSERT_SCHEMA_VERSION,
} from './constants';
const IDENTIFIER = /^[a-z][a-z0-9._:-]{2,127}$/;
const MAX_FACTS = 5000;
const MAX_ATTRIBUTES_BYTES = 64 * 1024;
const SECRET_LIKE_KEY = /(token|secret|password|authorization|access[_-]?token|refresh[_-]?token|api[_-]?key)/i;
const SECRET_LIKE_VALUE = /(?:ndc_edp(?:wb|rb)_[A-Za-z0-9_-]*|(?:bearer|basic)\s+\S+|eyJ[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+)/i;
export interface NdcFact extends IDataObject {
sourceId: string;
semanticType: string;
observedAt: string;
attributes?: IDataObject;
geometry?: {
type: 'Point';
coordinates: [number, number];
};
}
export interface NdcPublishPayload extends IDataObject {
schemaVersion: typeof DATA_PRODUCT_PUBLISH_SCHEMA_VERSION;
batch: {
runId: string;
sequence: number;
idempotencyKey: string;
};
facts: NdcFact[];
}
export interface NdcFoundryBindingInput extends IDataObject {
applicationId: string;
pageId: string;
binding: {
id: string;
dataProductId: string;
slotId: string;
semanticTypes: string[];
fieldProjection: string[];
};
}
export function normalizeBaseUrl(value: unknown): string {
const normalized = String(value ?? '').trim().replace(/\/+$/, '');
if (!/^https?:\/\/[^\s]+$/i.test(normalized)) throw new Error('ndc_internal_api_base_url_invalid');
return normalized;
}
export function requireIdentifier(value: unknown, field: string): string {
const normalized = String(value ?? '').trim();
if (!IDENTIFIER.test(normalized)) throw new Error(`${field}_invalid`);
return normalized;
}
export function encodeIdentifierPath(value: unknown, field: string): string {
return encodeURIComponent(requireIdentifier(value, field));
}
export function factsFromItems(items: INodeExecutionData[]): NdcFact[] {
if (!items.length) throw new Error('facts_required');
if (items.length > MAX_FACTS) throw new Error('facts_limit_exceeded');
const entityKeys = new Set<string>();
return items.map((item, index) => {
const source = isObject(item.json.fact) ? item.json.fact : item.json;
const fact: NdcFact = {
sourceId: requireIdentifier(source.sourceId, `facts_${index}_sourceId`),
semanticType: requireIdentifier(source.semanticType, `facts_${index}_semanticType`),
observedAt: requireIsoTimestamp(source.observedAt, `facts_${index}_observedAt`),
};
const entityKey = `${fact.sourceId}\u0000${fact.semanticType}`;
if (entityKeys.has(entityKey)) throw new Error('facts_duplicate_entity_key');
entityKeys.add(entityKey);
if (source.attributes !== undefined) {
if (!isObject(source.attributes)) throw new Error(`facts_${index}_attributes_invalid`);
if (Buffer.byteLength(JSON.stringify(source.attributes)) > MAX_ATTRIBUTES_BYTES) {
throw new Error(`facts_${index}_attributes_size_exceeded`);
}
if (containsSecretLikeMaterial(source.attributes)) throw new Error(`facts_${index}_attributes_secret_material_forbidden`);
fact.attributes = source.attributes;
}
if (source.geometry !== undefined) fact.geometry = normalizePoint(source.geometry, index);
return fact;
});
}
export function buildPublishPayload(
items: INodeExecutionData[],
executionId: string,
workflowId: string,
nodeId: string,
dataProductId: string,
sequence: number,
): NdcPublishPayload {
if (!Number.isInteger(sequence) || sequence < 0 || sequence > 2_147_483_647) throw new Error('batch_sequence_invalid');
requireIdentifier(dataProductId, 'dataProductId');
const runId = `run-${sha256(`${workflowId}|${executionId}`).slice(0, 48)}`;
const idempotencyKey = `publish-${sha256(
`${workflowId}|${executionId}|${nodeId}|${dataProductId}|${sequence}`,
)}`;
return {
schemaVersion: DATA_PRODUCT_PUBLISH_SCHEMA_VERSION,
batch: { runId, sequence, idempotencyKey },
facts: factsFromItems(items),
};
}
export function normalizeSnapshotFacts(value: unknown): IDataObject[] {
if (!isObject(value) || !Array.isArray(value.facts)) throw new Error('data_product_snapshot_invalid');
return value.facts.map((fact, index) => {
if (!isObject(fact)) throw new Error(`snapshot_fact_${index}_invalid`);
return fact;
});
}
export function snapshotReadParameters(
dataProductId: unknown,
pageSize: unknown,
): { encodedProductId: string; pageSize: number } {
const normalizedPageSize = Number(pageSize);
if (!Number.isInteger(normalizedPageSize) || normalizedPageSize < 1 || normalizedPageSize > 5000) {
throw new Error('page_size_invalid');
}
return {
encodedProductId: encodeIdentifierPath(dataProductId, 'dataProductId'),
pageSize: normalizedPageSize,
};
}
export function dataProductOptions(value: unknown): INodeListSearchItems[] {
if (!isObject(value)) throw new Error('data_product_catalog_invalid');
const rows = Array.isArray(value.dataProducts)
? value.dataProducts
: Array.isArray(value.products)
? value.products
: [];
return rows.flatMap((row) => {
if (typeof row === 'string') {
return IDENTIFIER.test(row) ? [{ name: row, value: row }] : [];
}
if (!isObject(row)) return [];
const id = String(row.id ?? row.dataProductId ?? '').trim();
if (!IDENTIFIER.test(id)) return [];
const version = String(row.version ?? '').trim();
const label = String(row.label ?? row.name ?? id).trim() || id;
return [{ name: version ? `${label} · ${version}` : label, value: id }];
});
}
export function buildFoundryBindingPayload(input: NdcFoundryBindingInput): IDataObject {
const applicationId = requireFoundryApplicationId(input.applicationId);
const pageId = requireFoundryPageId(input.pageId);
const bindingId = requireIdentifier(input.binding.id, 'binding_id');
const dataProductId = requireIdentifier(input.binding.dataProductId, 'dataProductId');
const slotId = requireFoundrySlotId(input.binding.slotId);
const semanticTypes = uniqueIdentifiers(input.binding.semanticTypes, 'semanticTypes');
if (!semanticTypes.length) throw new Error('semanticTypes_required');
const fieldProjection = uniqueIdentifiers(input.binding.fieldProjection, 'fieldProjection', true);
const binding = { id: bindingId, dataProductId, slotId, semanticTypes, fieldProjection };
const digest = createHash('sha256')
.update(JSON.stringify({ applicationId, pageId, binding }))
.digest('hex')
.slice(0, 32);
return {
schemaVersion: FOUNDRY_BINDING_UPSERT_SCHEMA_VERSION,
applicationId,
pageId,
idempotencyKey: `foundry-binding-${digest}`,
binding,
};
}
function requireFoundryApplicationId(value: unknown): string {
const normalized = String(value ?? '').trim();
if (!/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(normalized)) {
throw new Error('applicationId_invalid');
}
return normalized;
}
function requireFoundryPageId(value: unknown): string {
const normalized = String(value ?? '').trim();
if (!/^[a-z0-9][a-z0-9-]{0,79}$/.test(normalized)) throw new Error('pageId_invalid');
return normalized;
}
function requireFoundrySlotId(value: unknown): string {
const normalized = String(value ?? '').trim();
if (!/^[A-Za-z0-9][A-Za-z0-9-]{0,79}$/.test(normalized)) throw new Error('slotId_invalid');
return normalized;
}
function normalizePoint(value: unknown, index: number): NdcFact['geometry'] {
if (!isObject(value) || value.type !== 'Point' || !Array.isArray(value.coordinates) || value.coordinates.length !== 2) {
throw new Error(`facts_${index}_geometry_invalid`);
}
const [longitude, latitude] = value.coordinates;
if (![longitude, latitude].every((coordinate) => typeof coordinate === 'number' && Number.isFinite(coordinate))) {
throw new Error(`facts_${index}_geometry_coordinates_invalid`);
}
if ((longitude as number) < -180 || (longitude as number) > 180 || (latitude as number) < -90 || (latitude as number) > 90) {
throw new Error(`facts_${index}_geometry_coordinates_out_of_range`);
}
return { type: 'Point', coordinates: [longitude as number, latitude as number] };
}
function requireIsoTimestamp(value: unknown, field: string): string {
const normalized = String(value ?? '').trim();
if (!normalized || Number.isNaN(Date.parse(normalized))) throw new Error(`${field}_invalid`);
return normalized;
}
function uniqueIdentifiers(value: unknown, field: string, allowEmpty = false): string[] {
if (!Array.isArray(value)) throw new Error(`${field}_invalid`);
const normalized = [...new Set(value.map((item) => requireIdentifier(item, field)))];
if (!allowEmpty && !normalized.length) throw new Error(`${field}_required`);
return normalized;
}
function sha256(value: string): string {
return createHash('sha256').update(value).digest('hex');
}
function containsSecretLikeMaterial(value: unknown): boolean {
if (typeof value === 'string') return SECRET_LIKE_VALUE.test(value);
if (Array.isArray(value)) return value.some(containsSecretLikeMaterial);
if (!isObject(value)) return false;
return Object.entries(value).some(([key, child]) => SECRET_LIKE_KEY.test(key) || containsSecretLikeMaterial(child));
}
function isObject(value: unknown): value is IDataObject {
return Boolean(value) && typeof value === 'object' && !Array.isArray(value);
}