Skip to content

Commit c60280a

Browse files
committed
chore(ilc/server): update fastify to v3
1 parent 11191ae commit c60280a

22 files changed

+644
-213
lines changed

ilc/package-lock.json

Lines changed: 540 additions & 125 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ilc/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"author": "",
2121
"license": "Apache-2.0",
2222
"dependencies": {
23+
"@fastify/express": "^1.1.0",
2324
"@namecheap/error-extender": "^2.2.1",
2425
"@namecheap/tailorx": "^8.2.1",
2526
"@newrelic/native-metrics": "^12.0.0",
@@ -32,7 +33,7 @@
3233
"deepmerge": "4.3.1",
3334
"eventemitter3": "^5.0.1",
3435
"exponential-backoff": "^3.1.3",
35-
"fastify": "^2.15.3",
36+
"fastify": "^3.29.5",
3637
"http-status-codes": "^2.3.0",
3738
"ilc-plugins-sdk": "^2.3.0",
3839
"ilc-sdk": "^6.1.1",
@@ -58,6 +59,7 @@
5859
"@babel/preset-typescript": "^7.28.5",
5960
"@types/chai": "^5.2.3",
6061
"@types/config": "^3.3.5",
62+
"@types/express": "^5.0.6",
6163
"@types/mocha": "^10.0.10",
6264
"@types/newrelic": "^9.14.8",
6365
"@types/node": "^22.19.2",

ilc/server/TransitionHooksExecutor.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ describe('TransitionHooksExecutor', () => {
4848
pluginManager.getTransitionHooksPlugin.returns(transitionHooksPlugin);
4949
transitionHooksPlugin.getTransitionHooks.returns(hooks);
5050

51-
const app = createApp(helpers.getRegistryMock(), pluginManager, context);
51+
const app = await createApp(helpers.getRegistryMock(), pluginManager, context);
5252

5353
try {
5454
res = await app.inject({ method: 'GET', url: '/all' });
@@ -73,7 +73,7 @@ describe('TransitionHooksExecutor', () => {
7373
pluginManager.getTransitionHooksPlugin.returns(transitionHooksPlugin);
7474
transitionHooksPlugin.getTransitionHooks.returns(hooks);
7575

76-
const app = createApp(helpers.getRegistryMock(), pluginManager, context);
76+
const app = await createApp(helpers.getRegistryMock(), pluginManager, context);
7777

7878
try {
7979
res = await app.inject({ method: 'GET', url: '/all' });
@@ -96,7 +96,7 @@ describe('TransitionHooksExecutor', () => {
9696
pluginManager.getTransitionHooksPlugin.returns(transitionHooksPlugin);
9797
transitionHooksPlugin.getTransitionHooks.returns(hooks);
9898

99-
const app = createApp(helpers.getRegistryMock(), pluginManager, context);
99+
const app = await createApp(helpers.getRegistryMock(), pluginManager, context);
100100

101101
try {
102102
res = await app.inject({ method: 'GET', url: '/all' });

ilc/server/TransitionHooksExecutor.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { PluginManager, TransitionHooksPlugin } from 'ilc-plugins-sdk';
22
import { TransitionHookError } from '../common/transition-hooks/errors';
3-
import { FastifyRequest } from 'fastify';
4-
import { PatchedHttpRequest } from './types/PatchedHttpRequest';
3+
import { PatchedFastifyRequest } from './types/PatchedHttpRequest';
54

65
type TransitionResult = {
76
location: string;
@@ -15,7 +14,7 @@ export class TransitionHooksExecutor {
1514
this.transitionHooksPlugin = pluginManager.getTransitionHooksPlugin();
1615
}
1716

18-
async redirectTo(req: FastifyRequest<PatchedHttpRequest>): Promise<TransitionResult | null> {
17+
async redirectTo(req: PatchedFastifyRequest): Promise<TransitionResult | null> {
1918
if (!req.raw.router) {
2019
throw new Error('Router not initialized');
2120
}

ilc/server/app.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import config from 'config';
22
import fastify from 'fastify';
3+
import { fastifyExpress } from '@fastify/express';
34
import { AsyncResource } from 'node:async_hooks';
45
import { errorHandlerFactory } from './errorHandler/factory';
56
import { pingPluginFactroy } from './routes/pingPluginFactory';
@@ -19,13 +20,14 @@ const { isStaticFile, isHealthCheck, isDataUri } = require('./utils/utils');
1920
/**
2021
* @param {Registry} registryService
2122
*/
22-
module.exports = (registryService, pluginManager, context) => {
23+
module.exports = async function createApplication(registryService, pluginManager, context) {
2324
const reportingPlugin = reportingPluginManager.getInstance();
2425
const errorHandler = errorHandlerFactory();
2526
const appConfig = Application.getConfig(reportingPlugin);
2627
const logger = reportingPluginManager.getLogger();
2728
const accessLogger = new AccessLogger(config, logger);
2829
const app = fastify(appConfig);
30+
await app.register(fastifyExpress);
2931

3032
const asyncResourceSymbol = Symbol('asyncResource');
3133

ilc/server/app.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const { context } = require('./context/context');
66
const createApp = require('./app');
77

88
async function createTestServer(mockRegistryOptions = {}, mockPluginOptions = {}) {
9-
const app = createApp(
9+
const app = await createApp(
1010
helpers.getRegistryMock(mockRegistryOptions),
1111
helpers.getPluginManagerMock(mockPluginOptions),
1212
context,

ilc/server/context/context.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ module.exports = {
1515
const store = new Map();
1616

1717
store.set('url', request.raw.url);
18-
store.set('protocol', request.raw.connection.encrypted ? 'https' : 'http');
18+
store.set('protocol', request.raw.socket.encrypted ? 'https' : 'http');
1919
store.set('path', parsedUrl.pathname);
2020
store.set('domain', request.hostname);
2121
store.set('requestId', requestId);

ilc/server/errorHandler/ErrorHandler.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ describe('ErrorHandler', () => {
3838
});
3939

4040
beforeEach(async () => {
41-
app = createApp(
41+
app = await createApp(
4242
helpers.getRegistryMock() as unknown as Registry,
4343
helpers.getPluginManagerMock() as unknown as PluginManager,
4444
context,

ilc/server/errorHandler/ErrorHandler.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,13 @@ export default class ErrorHandler {
7070
// This handler serves as Fastify & Tailor handler.
7171
// While Fastify will pass it's own Reply object
7272
// Tailor passes http.ServerResponse from Node core
73-
let nres: ServerResponse;
73+
let rawResponse: ServerResponse;
7474
if (isFastifyReply(res)) {
75-
nres = res.res;
75+
rawResponse = res.raw;
7676
// Claim full responsibility of the low-level response from Fastify
77-
res.sent = true;
77+
res.hijack();
7878
} else {
79-
nres = res as ServerResponse;
79+
rawResponse = res as ServerResponse;
8080
}
8181

8282
try {
@@ -87,24 +87,24 @@ export default class ErrorHandler {
8787
let data = await this.registryService.getTemplate('500', { locale, forDomain: currentDomain });
8888
const content = data.data.content.replace('%ERRORID%', `Error ID: ${errorId}`);
8989

90-
this.ensureInternalErrorHeaders(nres, StatusCodes.INTERNAL_SERVER_ERROR);
91-
nres.write(content);
92-
nres.end();
90+
this.ensureInternalErrorHeaders(rawResponse, StatusCodes.INTERNAL_SERVER_ERROR);
91+
rawResponse.write(content);
92+
rawResponse.end();
9393
} catch (causeErr) {
9494
const handlingError = new ErrorHandlingError({
9595
message: 'Additional error in error handling',
9696
cause: causeErr as Error,
9797
data: { errorId, originalError: safeJsonStringify(err) },
9898
});
9999
this.logger.error(handlingError);
100-
this.writeStaticError(nres);
100+
this.writeStaticError(rawResponse);
101101
}
102102
}
103103

104104
async handleClientError(reply: ServerResponseFastifyReply, error: Error, statusCode?: number): Promise<void> {
105105
this.logger.warn(error);
106-
reply.sent = true;
107-
this.writeStaticError(reply.res!, statusCode);
106+
reply.hijack();
107+
this.writeStaticError(reply.raw, statusCode);
108108
}
109109

110110
private ensureInternalErrorHeaders(nres: ServerResponse, statusCode: number): void {

ilc/server/i18n.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ const onRequestFactory =
7373
const encodedNextI18nCookie = i18nCookie.encode(pluginProcessedI18nConfig);
7474

7575
if (encodedI18nCookie !== encodedNextI18nCookie) {
76-
reply.res.setHeader(
76+
reply.raw.setHeader(
7777
'Set-Cookie',
7878
Cookie.serialize(i18nCookie.name, encodedNextI18nCookie, i18nCookie.getOpts()),
7979
);

0 commit comments

Comments
 (0)