@fastify/cookie 11.1.1 → 11.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -1,12 +1,23 @@
1
1
  'use strict'
2
2
 
3
3
  const fp = require('fastify-plugin')
4
- const { parseCookie: parse, stringifySetCookie } = require('cookie')
5
4
 
6
5
  const { Signer, sign, unsign } = require('./signer')
7
6
 
7
+ // TODO: Use require(ESM) when Node.js 20 is nolonger supported or Fastify@6
8
+ let cookieModule = null
9
+ async function dynamicLoadCookie () {
10
+ if (cookieModule == null) {
11
+ cookieModule = await import('cookie')
12
+ }
13
+ }
14
+
8
15
  function serialize (name, value, options) {
9
- return stringifySetCookie(Object.assign({ name, value }, options))
16
+ return cookieModule.stringifySetCookie(Object.assign({ name, value }, options))
17
+ }
18
+
19
+ function parse (header, options) {
20
+ return cookieModule.parseCookie(header, options)
10
21
  }
11
22
 
12
23
  const kReplySetCookies = Symbol('fastify.reply.setCookies')
@@ -121,14 +132,12 @@ function fastifyCookieOnSendHandler (_fastifyReq, fastifyRes, _payload, done) {
121
132
  done()
122
133
  }
123
134
 
124
- function plugin (fastify, options, next) {
135
+ async function plugin (fastify, options) {
125
136
  const secret = options.secret
126
137
  const hook = getHook(options.hook)
127
138
  if (hook === undefined) {
128
- return next(
129
- new Error(
130
- "@fastify/cookie: Invalid value provided for the hook-option. You can set the hook-option only to false, 'onRequest' , 'preParsing' , 'preValidation' or 'preHandler'"
131
- )
139
+ throw new Error(
140
+ "@fastify/cookie: Invalid value provided for the hook-option. You can set the hook-option only to false, 'onRequest' , 'preParsing' , 'preValidation' or 'preHandler'"
132
141
  )
133
142
  }
134
143
  const isSigner =
@@ -167,7 +176,7 @@ function plugin (fastify, options, next) {
167
176
  // disables cookie autoparsing, not the ability to set cookies on the reply.
168
177
  fastify.addHook('onSend', fastifyCookieOnSendHandler)
169
178
 
170
- next()
179
+ await dynamicLoadCookie()
171
180
 
172
181
  // ***************************
173
182
  function parseCookie (cookieHeader) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fastify/cookie",
3
- "version": "11.1.1",
3
+ "version": "11.1.2",
4
4
  "description": "Plugin for fastify to add support for cookies",
5
5
  "main": "index.js",
6
6
  "type": "commonjs",
package/types/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  /// <reference types='node' />
2
2
 
3
- import { FastifyPluginCallback } from 'fastify'
3
+ import { FastifyPluginAsync } from 'fastify'
4
4
 
5
5
  declare module 'fastify' {
6
6
  interface FastifyInstance extends SignerMethods {
@@ -94,7 +94,7 @@ declare module 'fastify' {
94
94
  }
95
95
  }
96
96
 
97
- type FastifyCookiePlugin = FastifyPluginCallback<
97
+ type FastifyCookiePlugin = FastifyPluginAsync<
98
98
  NonNullable<fastifyCookie.FastifyCookieOptions>
99
99
  >
100
100