import type { GenericMessage, ReplicationApplyResult } from '@enbox/dwn-sdk-js'; import type { HandlerResponse, JsonRpcHandler } from '../../lib/json-rpc-router.js'; import log from 'loglevel'; import { invokeMessageProcessedHooks } from './message-processed-hooks.js'; import { requestDataBytesTotal } from '../../metrics.js'; import { createJsonRpcErrorResponse, createJsonRpcSuccessResponse, JsonRpcErrorCodes } from '@enbox/dwn-clients'; import { DataStream, DwnError, DwnErrorCode, DwnInterfaceName, DwnMethodName, Encoder, Message, Records, RecordsWrite } from '@enbox/dwn-sdk-js'; import { enforceQuota, enforceTenantRateLimit, validateInboundDwnMessageTransport } from './inbound-message.js'; /** `stored` adds no body bytes; it does not imply that the write is queryable. */ type StoredReplayState = 'stored' | 'completable' | 'not-stored' | 'superseded'; export const handleDwnApplyReplicatedMessage: JsonRpcHandler = async ( dwnRequest, context, ) => { const { dwn, dataStream } = context; const { ancestryOnly, encodedData, includeMaterializationConfirmation, target, message } = dwnRequest.params as { ancestryOnly?: unknown; encodedData?: string; includeMaterializationConfirmation?: unknown; target: string; message: GenericMessage; }; const requestId = dwnRequest.id ?? crypto.randomUUID(); if ((message as { encodedData?: unknown }).encodedData !== undefined) { return { jsonRpcResponse: createJsonRpcErrorResponse( requestId, JsonRpcErrorCodes.InvalidParams, 'message.encodedData is not supported; use params.encodedData', ), }; } const hasInboundData = encodedData !== undefined || dataStream !== undefined; try { const confirmationValidation = validateMaterializationConfirmationRequest({ includeMaterializationConfirmation, context, requestId }); if (confirmationValidation !== undefined) { return confirmationValidation; } const ancestryResult = await validateAncestryOnlyRequest({ ancestryOnly, hasInboundData, message, requestId, }); if (ancestryResult !== undefined) { return ancestryResult; } const transportResult = validateInboundDwnMessageTransport({ allowDatalessRecordsWriteOverNonHttp : ancestryOnly === true, allowRecordsWriteOverNonHttp : true, context, hasEncodedData : encodedData !== undefined, message, requestId, target, }); if (transportResult !== undefined) { return transportResult; } const rateLimitResult = enforceTenantRateLimit({ context, message, requestId, target }); if (rateLimitResult !== undefined) { return rateLimitResult; } const encodedDataResult = validateEncodedData({ context, encodedData, message, requestId }); if (encodedDataResult !== undefined) { return encodedDataResult; } const storedReplayState = await getStoredReplayState( context, target, message, hasInboundData, includeMaterializationConfirmation === true, ); if (storedReplayState === 'superseded' && includeMaterializationConfirmation !== true) { await dataStream?.cancel().catch((): void => { // A proven obsolete replay does not need its inbound body. }); const result = { kind: 'Superseded' } satisfies ReplicationApplyResult; recordApplyActivity(target, message, result, context); return { jsonRpcResponse: createJsonRpcSuccessResponse(requestId, { result }), }; } const quotaResult = await enforceApplyReplicatedMessageQuota({ context, hasInboundData, storedReplayState, message, target, }); if (quotaResult !== undefined) { return quotaResult; } const dataStreamForApply = getDataStreamForApply({ dataStream, encodedData, message }); const result = await dwn.applyReplicatedMessage(target, message, { dataStream : dataStreamForApply, includeMaterializationConfirmation : includeMaterializationConfirmation === true, }); if (result.kind === 'Duplicate') { await dataStreamForApply?.cancel().catch((): void => { // Duplicate echoes do not need their inbound body; cancellation is best-effort. }); } recordApplyActivity(target, message, result, context); if (result.kind === 'Applied') { invokeMessageProcessedHooks(context, target, message, appliedHookStatus(message, dataStreamForApply !== undefined)); } return { jsonRpcResponse: createJsonRpcSuccessResponse(requestId, { result }), }; } catch (error) { log.error('handleDwnApplyReplicatedMessage error', error); return { jsonRpcResponse: createJsonRpcErrorResponse( requestId, JsonRpcErrorCodes.InternalError, 'an unexpected error occurred while applying the replicated message', ), }; } }; function validateMaterializationConfirmationRequest({ includeMaterializationConfirmation, context, requestId, }: { includeMaterializationConfirmation: unknown; context: Parameters[1]; requestId: Parameters[0]; }): HandlerResponse | undefined { if (includeMaterializationConfirmation !== undefined && typeof includeMaterializationConfirmation !== 'boolean') { return invalidReplicationOptionResponse(requestId, 'includeMaterializationConfirmation must be a boolean when present'); } if (includeMaterializationConfirmation !== true) { return undefined; } if (context.transport !== 'http') { return invalidReplicationOptionResponse(requestId, 'includeMaterializationConfirmation requires HTTP transport'); } if (context.isLocalNodeAuthenticated !== true) { return { jsonRpcResponse: createJsonRpcErrorResponse( requestId, JsonRpcErrorCodes.Forbidden, 'includeMaterializationConfirmation requires an authenticated local-node connection', ), }; } } async function validateAncestryOnlyRequest({ ancestryOnly, hasInboundData, message, requestId, }: { ancestryOnly: unknown; hasInboundData: boolean; message: GenericMessage; requestId: Parameters[0]; }): Promise { const descriptor = message?.descriptor as { dataCid?: unknown; dataSize?: unknown; interface?: unknown; method?: unknown; } | undefined; const isDataBearingWrite = descriptor?.interface === DwnInterfaceName.Records && descriptor.method === DwnMethodName.Write && typeof descriptor.dataCid === 'string' && typeof descriptor.dataSize === 'number' && descriptor.dataSize >= 0; if (ancestryOnly !== undefined && ancestryOnly !== true) { return invalidReplicationOptionResponse(requestId, 'ancestryOnly must be true when present'); } if (ancestryOnly === true) { let isInitialWrite = false; try { isInitialWrite = isDataBearingWrite && await RecordsWrite.isInitialWrite(message); } catch { // Malformed messages carrying the marker are invalid input, not server failures. } if (!isInitialWrite || hasInboundData) { return invalidReplicationOptionResponse( requestId, 'ancestryOnly requires a data-less initial RecordsWrite with a payload descriptor', ); } return undefined; } } function invalidReplicationOptionResponse( requestId: Parameters[0], message: string, ): HandlerResponse { return { jsonRpcResponse: createJsonRpcErrorResponse(requestId, JsonRpcErrorCodes.InvalidParams, message), }; } function getDataStreamForApply({ dataStream, encodedData, message, }: { dataStream: ReadableStream | undefined; encodedData: string | undefined; message: GenericMessage; }): ReadableStream | undefined { if (encodedData !== undefined) { return DataStream.fromBytes(Encoder.base64UrlToBytes(encodedData)); } if (dataStream === undefined) { return undefined; } return capDataStreamAtDescriptorSize(message, dataStream); } function capDataStreamAtDescriptorSize( message: GenericMessage, dataStream: ReadableStream, ): ReadableStream { const descriptor = message.descriptor as { dataSize?: unknown; interface?: unknown; method?: unknown }; if ( descriptor.interface !== DwnInterfaceName.Records || descriptor.method !== DwnMethodName.Write || typeof descriptor.dataSize !== 'number' ) { return dataStream; } const dataSize = descriptor.dataSize; const reader = dataStream.getReader(); let bytesRead = 0; return new ReadableStream({ async pull(controller): Promise { const { done, value } = await reader.read(); if (done) { controller.close(); reader.releaseLock(); return; } bytesRead += value.length; if (bytesRead > dataSize) { await reader.cancel().catch((): void => { // The stream already exceeded the declared size; cancellation is best-effort. }); reader.releaseLock(); controller.error(new DwnError( DwnErrorCode.RecordsWriteDataSizeMismatch, `actual data size exceeds descriptor dataSize ${dataSize}`, )); return; } controller.enqueue(value); }, async cancel(reason): Promise { await reader.cancel(reason).catch((): void => { // The caller is closing early; cancellation is best-effort. }); reader.releaseLock(); }, }); } async function enforceApplyReplicatedMessageQuota({ context, hasInboundData, storedReplayState, message, target, }: { context: Parameters[1]; hasInboundData: boolean; storedReplayState: StoredReplayState; message: GenericMessage; target: string; }): Promise> { if (storedReplayState === 'stored' || storedReplayState === 'superseded') { return undefined; } if ( context.config !== undefined && context.adminStore !== undefined && message.descriptor.interface === DwnInterfaceName.Records && message.descriptor.method === DwnMethodName.Write ) { const storageBytesToAdd = hasInboundData ? (message.descriptor as { dataSize?: number }).dataSize ?? 0 : 0; return enforceQuota(target, message, context, { storageBytesToAdd, skipMessageCount: storedReplayState === 'completable', }); } return undefined; } async function getStoredReplayState( context: Parameters[1], target: string, message: GenericMessage, hasInboundData: boolean, includeMaterializationConfirmation: boolean, ): Promise { const messageCid = await Message.getCid(message); const existingMessage = await context.dwn.storage.messageStore.get(target, messageCid); if (existingMessage === undefined) { return includeMaterializationConfirmation ? classifyUnstoredConfirmation(context, target, message) : 'not-stored'; } if (!hasInboundData) { return 'stored'; } return classifyStoredReplayWithData(context, target, existingMessage, messageCid); } /** A displaced update cannot add ancestry once a newer current message already wins. */ async function classifyUnstoredConfirmation( context: Parameters[1], tenant: string, message: GenericMessage, ): Promise { if (!Records.isRecordsWrite(message) || await RecordsWrite.isInitialWrite(message)) { return 'not-stored'; } const { messages } = await context.dwn.storage.messageStore.query(tenant, [{ interface : DwnInterfaceName.Records, isLatestBaseState : true, recordId : message.recordId, }], undefined, { limit: 2 }); return messages.length === 1 && await Message.isOlder(message, messages[0]) ? 'superseded' : 'not-stored'; } /** Distinguishes a completable ancestry write from committed or superseded state. */ async function classifyStoredReplayWithData( context: Parameters[1], tenant: string, message: GenericMessage, messageCid: string, ): Promise { if ( message.descriptor.interface !== DwnInterfaceName.Records || message.descriptor.method !== DwnMethodName.Write ) { return 'stored'; } const recordId = (message as { recordId?: unknown }).recordId; if (typeof recordId !== 'string') { return 'stored'; } const { messages } = await context.dwn.storage.messageStore.query(tenant, [{ interface : DwnInterfaceName.Records, isLatestBaseState : true, recordId, }], undefined, { limit: 2 }); if (messages.length === 0) { // completeData rechecks that this is still the sole ancestry row under the store write lock. return await RecordsWrite.isInitialWrite(message) ? 'completable' : 'stored'; } if (messages.length === 1 && await Message.getCid(messages[0]) !== messageCid) { return 'superseded'; } return 'stored'; } function recordApplyActivity( target: string, message: GenericMessage, result: ReplicationApplyResult, context: Parameters[1], ): void { const dwnInterface = message.descriptor.interface as string; const dwnMethod = message.descriptor.method as string; const dataSizeBytes = (message.descriptor as { dataSize?: number }).dataSize; if (dataSizeBytes !== undefined && dataSizeBytes > 0) { requestDataBytesTotal.inc({ interface: dwnInterface, method: dwnMethod }, dataSizeBytes); } context.activityLog?.record({ tenant : target, interface : dwnInterface, method : dwnMethod, statusCode : replicationApplyStatusCode(result), transport : context.transport, dataSizeBytes, }); } function replicationApplyStatusCode(result: ReplicationApplyResult): number { switch (result.kind) { case 'Applied': return 202; case 'Duplicate': case 'Superseded': return 409; case 'Incomplete': return 424; case 'Invalid': return 400; case 'Deferred': return 503; } } function validateEncodedData({ context, encodedData, message, requestId, }: { context: Parameters[1]; encodedData?: string; message: GenericMessage; requestId: Parameters[0]; }): ReturnType { if (encodedData === undefined) { return undefined; } const decodedLength = base64UrlDecodedLength(encodedData); if (decodedLength === undefined) { return { jsonRpcResponse: createJsonRpcErrorResponse( requestId, JsonRpcErrorCodes.InvalidParams, 'encodedData must be valid base64url data', ), }; } const descriptor = message.descriptor as { dataSize?: unknown }; if (typeof descriptor.dataSize === 'number' && decodedLength !== descriptor.dataSize) { return { jsonRpcResponse: createJsonRpcErrorResponse( requestId, JsonRpcErrorCodes.InvalidParams, `encodedData length ${decodedLength} does not match descriptor dataSize ${descriptor.dataSize}`, ), }; } const maxRecordDataSize = context.config?.maxRecordDataSize; if (maxRecordDataSize !== undefined && decodedLength > maxRecordDataSize) { return { jsonRpcResponse: createJsonRpcErrorResponse( requestId, JsonRpcErrorCodes.InvalidParams, `encodedData length ${decodedLength} exceeds max record data size ${maxRecordDataSize}`, ), }; } } function base64UrlDecodedLength(encodedData: string): number | undefined { const unpaddedLength = getBase64UrlLength(encodedData); if (unpaddedLength === undefined) { return undefined; } if (unpaddedLength % 4 === 1) { return undefined; } return Math.floor((unpaddedLength * 3) / 4); } function getBase64UrlLength(encodedData: string): number | undefined { for (let i = 0; i < encodedData.length; i++) { const charCode = encodedData.codePointAt(i); if (charCode === undefined || !isBase64UrlCharCode(charCode)) { return undefined; } } return encodedData.length; } function isBase64UrlCharCode(charCode: number): boolean { return (charCode >= 65 && charCode <= 90) || (charCode >= 97 && charCode <= 122) || (charCode >= 48 && charCode <= 57) || charCode === 45 || charCode === 95; } function appliedHookStatus(message: GenericMessage, hasDataStream: boolean): { code: number; detail: string } { const descriptor = message.descriptor as { interface?: string; method?: string; dateCreated?: string; messageTimestamp?: string }; if ( descriptor.interface === DwnInterfaceName.Records && descriptor.method === DwnMethodName.Write && descriptor.dateCreated === descriptor.messageTimestamp && !hasDataStream ) { return { code: 204, detail: 'No Content' }; } return { code: 202, detail: 'Accepted' }; }