Skip to content

Commit

Permalink
chore: upgrade fastify to v5 (#7101)
Browse files Browse the repository at this point in the history
* chore: upgrade fastify to v5

* Upgrade fastify plugins

* Clean up yarn lock
  • Loading branch information
nflaig committed Sep 23, 2024
1 parent 0dceb83 commit d0ba6bc
Show file tree
Hide file tree
Showing 7 changed files with 228 additions and 208 deletions.
2 changes: 1 addition & 1 deletion packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
"@types/eventsource": "^1.1.11",
"@types/qs": "^6.9.7",
"ajv": "^8.12.0",
"fastify": "^4.27.0"
"fastify": "^5.0.0"
},
"keywords": [
"ethereum",
Expand Down
12 changes: 11 additions & 1 deletion packages/api/src/utils/schema.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {MediaType} from "./headers.js";
import {Endpoint, HeaderParams, PathParams, QueryParams} from "./types.js";

// Reasoning: Allows to declare JSON schemas for server routes in a succinct typesafe way.
Expand Down Expand Up @@ -91,7 +92,16 @@ export function getFastifySchema<T extends Endpoint["request"]>(schemaDef: Schem
const schema: {params?: JsonSchemaObj; querystring?: JsonSchemaObj; headers?: JsonSchemaObj; body?: JsonSchema} = {};

if (schemaDef.body != null) {
schema.body = getJsonSchemaItem(schemaDef.body);
schema.body = {
content: {
[MediaType.json]: {
schema: getJsonSchemaItem(schemaDef.body),
},
[MediaType.ssz]: {
schema: {},
},
},
};
}

if (schemaDef.params) {
Expand Down
14 changes: 1 addition & 13 deletions packages/api/src/utils/server/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,10 @@ import type * as fastify from "fastify";
import {MediaType} from "../headers.js";

export function addSszContentTypeParser(server: fastify.FastifyInstance): void {
// Cache body schema symbol, does not change per request
let bodySchemaSymbol: symbol | undefined;

server.addContentTypeParser(
MediaType.ssz,
{parseAs: "buffer"},
async (request: fastify.FastifyRequest, payload: Buffer) => {
if (bodySchemaSymbol === undefined) {
// Get body schema symbol to be able to access validation function
// https://github.com/fastify/fastify/blob/af2ccb5ff681c1d0ac22eb7314c6fa803f73c873/lib/symbols.js#L25
bodySchemaSymbol = Object.getOwnPropertySymbols(request.context).find((s) => s.description === "body-schema");
}
// JSON schema validation will be applied to `Buffer` object, it is required to override validation function
// See https://github.com/fastify/help/issues/1012, it is not possible right now to define a schema per content type
(request.context as unknown as Record<symbol, unknown>)[bodySchemaSymbol as symbol] = () => true;

async (_request: fastify.FastifyRequest, payload: Buffer) => {
// We could just return the `Buffer` here which is a subclass of `Uint8Array` but downstream code does not require it
// and it's better to convert it here to avoid unexpected behavior such as `Buffer.prototype.slice` not copying memory
// See https://github.com/nodejs/node/issues/41588#issuecomment-1016269584
Expand Down
10 changes: 5 additions & 5 deletions packages/beacon-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,10 @@
"@chainsafe/ssz": "^0.17.1",
"@chainsafe/threads": "^1.11.1",
"@ethersproject/abi": "^5.7.0",
"@fastify/bearer-auth": "^9.0.0",
"@fastify/cors": "^8.2.1",
"@fastify/swagger": "^8.10.0",
"@fastify/swagger-ui": "^1.9.3",
"@fastify/bearer-auth": "^10.0.1",
"@fastify/cors": "^10.0.1",
"@fastify/swagger": "^9.0.0",
"@fastify/swagger-ui": "^5.0.1",
"@libp2p/bootstrap": "^10.0.21",
"@libp2p/identify": "^1.0.20",
"@libp2p/interface": "^1.3.0",
Expand All @@ -136,7 +136,7 @@
"datastore-core": "^9.1.1",
"datastore-level": "^10.1.1",
"deepmerge": "^4.3.1",
"fastify": "^4.27.0",
"fastify": "^5.0.0",
"interface-datastore": "^8.2.7",
"it-all": "^3.0.4",
"it-pipe": "^3.0.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,6 @@
"@types/inquirer": "^9.0.3",
"@types/proper-lockfile": "^4.1.4",
"@types/yargs": "^17.0.24",
"fastify": "^4.27.0"
"fastify": "^5.0.0"
}
}
2 changes: 1 addition & 1 deletion packages/light-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
"devDependencies": {
"@chainsafe/as-sha256": "^0.5.0",
"@types/qs": "^6.9.7",
"fastify": "^4.27.0",
"fastify": "^5.0.0",
"qs": "^6.11.1",
"uint8arrays": "^5.0.1"
},
Expand Down
Loading

0 comments on commit d0ba6bc

Please sign in to comment.