diff --git a/packages/api/src/beacon/routes/beacon/block.ts b/packages/api/src/beacon/routes/beacon/block.ts index 815815e9e365..ae92c53457cb 100644 --- a/packages/api/src/beacon/routes/beacon/block.ts +++ b/packages/api/src/beacon/routes/beacon/block.ts @@ -65,7 +65,7 @@ export type Api = { getBlock( blockId: BlockId, format?: BlockFormat - ): Promise>; + ): Promise>; /** * Get block @@ -79,11 +79,13 @@ export type Api = { ): Promise< ApiClientResponse< { - [HttpStatusCode.OK]: { - data: allForks.SignedBeaconBlock; - executionOptimistic: ExecutionOptimistic; - version: ForkName; - }; + [HttpStatusCode.OK]: + | Uint8Array + | { + data: allForks.SignedBeaconBlock; + executionOptimistic: ExecutionOptimistic; + version: ForkName; + }; }, HttpStatusCode.BAD_REQUEST | HttpStatusCode.NOT_FOUND > diff --git a/packages/api/src/beacon/server/beacon.ts b/packages/api/src/beacon/server/beacon.ts index b5862f766b56..da5a0997a0d8 100644 --- a/packages/api/src/beacon/server/beacon.ts +++ b/packages/api/src/beacon/server/beacon.ts @@ -4,6 +4,41 @@ import {ServerRoutes, getGenericJsonServer} from "../../utils/server/index.js"; import {ServerApi} from "../../interfaces.js"; export function getRoutes(config: ChainForkConfig, api: ServerApi): ServerRoutes { - // All routes return JSON, use a server auto-generator - return getGenericJsonServer, ReqTypes>({routesData, getReturnTypes, getReqSerializers}, config, api); + const reqSerializers = getReqSerializers(config); + const returnTypes = getReturnTypes(); + + // Most of routes return JSON, use a server auto-generator + const serverRoutes = getGenericJsonServer, ReqTypes>( + {routesData, getReturnTypes, getReqSerializers}, + config, + api + ); + return { + ...serverRoutes, + // Non-JSON routes. Return JSON or binary depending on "accept" header + getBlock: { + ...serverRoutes.getBlock, + handler: async (req) => { + const response = await api.getBlock(...reqSerializers.getBlock.parseReq(req)); + if (response instanceof Uint8Array) { + // Fastify 3.x.x will automatically add header `Content-Type: application/octet-stream` if Buffer + return Buffer.from(response); + } else { + return returnTypes.getBlock.toJson(response); + } + }, + }, + getBlockV2: { + ...serverRoutes.getBlockV2, + handler: async (req) => { + const response = await api.getBlockV2(...reqSerializers.getBlockV2.parseReq(req)); + if (response instanceof Uint8Array) { + // Fastify 3.x.x will automatically add header `Content-Type: application/octet-stream` if Buffer + return Buffer.from(response); + } else { + return returnTypes.getBlockV2.toJson(response); + } + }, + }, + }; }