/
vasya2
/
var
Обзор
Документация
Войти
/
vasya2
/
var
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
api/src/plugins/cookie-update.ts
42 строки
1 KB
Mrugesh Mohapatra
fix(api): adjust levels for cookie related logs (#59946)
24 апр 2025, 22:20
Не верифицирован
24 апр 2025, 22:20
ff38233
Код
Авторство
О чём код?
import { FastifyPluginCallback } from 'fastify'; import type { CookieSerializeOptions } from './cookies'; type Options = { cookies: string[]; attributes: CookieSerializeOptions }; /** * Plugin that updates the attributes of cookies in the response, without * changing the value. * * @param fastify The Fastify instance. * @param options Options passed to the plugin via `fastify.register(plugin, * options)`. * @param options.cookies The names of the cookies to update. * @param options.attributes The attributes to update the cookies with. NOTE: * The attributes are merged with the default values given to \@fastify/cookie. * @param done Callback to signal that the logic has completed. */ export const cookieUpdate: FastifyPluginCallback<Options> = ( fastify, options, done ) => { fastify.addHook('onSend', (request, reply, _payload, next) => { const logger = fastify.log.child({ request }); for (const cookie of options.cookies) { const oldCookie = request.cookies[cookie]; if (!oldCookie) continue; const unsigned = reply.unsignCookie(oldCookie); const raw = unsigned.valid ? unsigned.value : oldCookie; void reply.setCookie(cookie, raw, options.attributes); } logger.trace(`Updated cookies for user ${request.user?.id}.`); next(); }); done(); };