Skip to content

Commit cb94f12

Browse files
committed
chore: update plugin
1 parent 0dd4044 commit cb94f12

13 files changed

Lines changed: 45 additions & 70 deletions

.prettierrc.json

Lines changed: 0 additions & 7 deletions
This file was deleted.

.taprc

Lines changed: 0 additions & 2 deletions
This file was deleted.

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
[![NPM Version][npm-image]][npm-url]
44
[![NPM Downloads][downloads-image]][downloads-url]
55
[![CI](https://github.com/fastify/fastify-request-context/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/fastify/fastify-request-context/actions/workflows/ci.yml)
6+
[![neostandard javascript style](https://img.shields.io/badge/code_style-neostandard-brightgreen?style=flat)](https://github.com/neostandard/neostandard)
67

78
Request-scoped storage support, based on [AsyncLocalStorage](https://nodejs.org/api/async_context.html#asynchronous-context-tracking).
89

@@ -13,14 +14,14 @@ nor will variables remain available once a request is completed.
1314

1415
Frequent use-cases are persisting request-aware logger instances and user authorization information.
1516

16-
17-
1817
## Install
18+
1919
```
2020
npm i @fastify/request-context
2121
```
2222

2323
### Compatibility
24+
2425
| Plugin version | Fastify version |
2526
| ---------------|-----------------|
2627
| `>=6.x` | `^5.x` |
@@ -29,7 +30,6 @@ npm i @fastify/request-context
2930
| `^1.x` | `^2.x` |
3031
| `^1.x` | `^1.x` |
3132

32-
3333
Please note that if a Fastify version is out of support, then so are the corresponding versions of this plugin
3434
in the table above.
3535
See [Fastify's LTS policy](https://github.com/fastify/fastify/blob/main/docs/Reference/LTS.md) for more details.
@@ -133,7 +133,7 @@ return app.ready()
133133
In TypeScript you are expected to augment the module to type your context:
134134

135135
```ts
136-
import {requestContext} from '@fastify/request-context'
136+
import { requestContext } from '@fastify/request-context'
137137

138138
declare module '@fastify/request-context' {
139139
interface RequestContextData {

eslint.config.js

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,6 @@
11
'use strict'
22

3-
const globals = require('globals')
4-
const js = require('@eslint/js')
5-
const prettier = require('eslint-plugin-prettier/recommended')
6-
7-
module.exports = [
8-
{
9-
languageOptions: {
10-
globals: {
11-
...globals.node,
12-
...globals.jest,
13-
},
14-
},
15-
},
16-
js.configs.recommended,
17-
prettier,
18-
]
3+
module.exports = require('neostandard')({
4+
ignores: require('neostandard').resolveIgnoresFromGitignore(),
5+
ts: true,
6+
})

index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ const requestContext = {
2424
},
2525
}
2626

27-
function fastifyRequestContext(fastify, opts, next) {
27+
function fastifyRequestContext (fastify, opts, next) {
2828
fastify.decorate('requestContext', requestContext)
2929
fastify.decorateRequest('requestContext', { getter: () => requestContext })
3030
fastify.decorateRequest(asyncResourceSymbol, null)
3131
const hook = opts.hook || 'onRequest'
3232
const hasDefaultStoreValuesFactory = typeof opts.defaultStoreValues === 'function'
3333

34-
fastify.addHook(hook, function requestContextHook(req, _res, done) {
34+
fastify.addHook(hook, function requestContextHook (req, _res, done) {
3535
const defaultStoreValues = hasDefaultStoreValuesFactory
3636
? opts.defaultStoreValues(req)
3737
: opts.defaultStoreValues
@@ -51,7 +51,7 @@ function fastifyRequestContext(fastify, opts, next) {
5151
// in a different async context, as req/res may emit events in a different context.
5252
// Related to https://github.com/nodejs/node/issues/34430 and https://github.com/nodejs/node/issues/33723
5353
if (hook === 'onRequest' || hook === 'preParsing') {
54-
fastify.addHook('preValidation', function requestContextPreValidationHook(req, _res, done) {
54+
fastify.addHook('preValidation', function requestContextPreValidationHook (req, _res, done) {
5555
const asyncResource = req[asyncResourceSymbol]
5656
asyncResource.runInAsyncScope(done, req.raw)
5757
})

package.json

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,18 @@
1616
"test": "npm run test:unit && npm run test:typescript",
1717
"test:unit": "c8 --100 node --test",
1818
"test:typescript": "tsd",
19-
"lint": "eslint \"test/**/*.js\" \"test-tap/**/*.js\" index.js",
20-
"prettier": "prettier --write \"{lib,test,test-tap}/**/*.js\" index.js \"types/**/*.ts\""
19+
"lint": "eslint",
20+
"lint:fix": "eslint --fix"
2121
},
2222
"dependencies": {
2323
"fastify-plugin": "^5.0.0"
2424
},
2525
"devDependencies": {
2626
"@types/node": "^25.0.3",
2727
"c8": "^10.1.3",
28-
"eslint": "^9.6.0",
29-
"eslint-config-prettier": "^10.0.1",
30-
"eslint-plugin-prettier": "^5.1.3",
28+
"eslint": "^9.39.0",
3129
"fastify": "^5.0.0",
32-
"prettier": "^3.2.5",
30+
"neostandard": "^0.12.0",
3331
"superagent": "^10.0.0",
3432
"tsd": "^0.33.0"
3533
},

test/internal/appInitializer.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
const fastify = require('fastify')
44
const { fastifyRequestContext } = require('../..')
55

6-
function initAppGet(endpoint) {
6+
function initAppGet (endpoint) {
77
const app = fastify({ logger: true })
88
app.register(fastifyRequestContext)
99

1010
app.get('/', endpoint)
1111
return app
1212
}
1313

14-
function initAppPost(endpoint) {
14+
function initAppPost (endpoint) {
1515
const app = fastify({ logger: true })
1616
app.register(fastifyRequestContext)
1717

@@ -20,7 +20,7 @@ function initAppPost(endpoint) {
2020
return app
2121
}
2222

23-
function initAppPostWithPrevalidation(endpoint) {
23+
function initAppPostWithPrevalidation (endpoint) {
2424
const app = fastify({ logger: true })
2525
app.register(fastifyRequestContext, { hook: 'preValidation' })
2626

@@ -40,7 +40,7 @@ function initAppPostWithPrevalidation(endpoint) {
4040
return app
4141
}
4242

43-
function initAppPostWithAllPlugins(endpoint, requestHook) {
43+
function initAppPostWithAllPlugins (endpoint, requestHook) {
4444
const app = fastify({ logger: true })
4545
app.register(fastifyRequestContext, { hook: requestHook })
4646

@@ -85,7 +85,7 @@ function initAppPostWithAllPlugins(endpoint, requestHook) {
8585
return app
8686
}
8787

88-
function initAppGetWithDefaultStoreValues(endpoint, defaultStoreValues) {
88+
function initAppGetWithDefaultStoreValues (endpoint, defaultStoreValues) {
8989
const app = fastify({ logger: true })
9090
app.register(fastifyRequestContext, {
9191
defaultStoreValues,

test/internal/testService.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,29 @@ const { requestContext } = require('../..')
44

55
// Test class to check if nested calls with promises work correctly with async local storage
66
class TestService {
7-
constructor(fastify) {
7+
constructor (fastify) {
88
this.appRequestContext = fastify.requestContext
99
}
1010

11-
processRequest(requestId) {
11+
processRequest (requestId) {
1212
return this.fetchData().then(() => {
1313
const testValueFromApp = this.appRequestContext.get('testKey')
1414
const testValueFromLib = requestContext.get('testKey')
1515
if (testValueFromApp !== `testValue${requestId}`) {
1616
throw new Error(
17-
`Wrong value retrieved from app context for request ${requestId}: ${testValueFromApp}`,
17+
`Wrong value retrieved from app context for request ${requestId}: ${testValueFromApp}`
1818
)
1919
}
2020

2121
if (testValueFromLib !== `testValue${requestId}`) {
2222
throw new Error(
23-
`Wrong value retrieved from lib context for request ${requestId}: ${testValueFromLib}`,
23+
`Wrong value retrieved from lib context for request ${requestId}: ${testValueFromLib}`
2424
)
2525
}
2626
})
2727
}
2828

29-
fetchData() {
29+
fetchData () {
3030
return new Promise((resolve) => {
3131
setTimeout(resolve, 10)
3232
})

test/internal/watcherService.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ const { executionAsyncId, createHook, AsyncResource } = require('node:async_hook
44
const { EventEmitter } = require('node:events')
55

66
class CustomResource extends AsyncResource {
7-
constructor(type, traceId) {
7+
constructor (type, traceId) {
88
super(type)
99

1010
this.traceId = traceId
1111
}
1212
}
1313

1414
class AsyncWatcher extends EventEmitter {
15-
setupInitHook() {
15+
setupInitHook () {
1616
// init is called during object construction. The resource may not have
1717
// completed construction when this callback runs, therefore all fields of the
1818
// resource referenced by "asyncId" may not have been populated.
@@ -28,7 +28,7 @@ class AsyncWatcher extends EventEmitter {
2828
return this
2929
}
3030

31-
setupDestroyHook() {
31+
setupDestroyHook () {
3232
// Destroy is called when an AsyncWrap instance is destroyed.
3333
this.destroy = (asyncId) => {
3434
this.emit('DESTROY', {
@@ -39,7 +39,7 @@ class AsyncWatcher extends EventEmitter {
3939
return this
4040
}
4141

42-
start() {
42+
start () {
4343
createHook({
4444
init: this.init.bind(this),
4545
destroy: this.destroy.bind(this),
@@ -50,7 +50,7 @@ class AsyncWatcher extends EventEmitter {
5050
}
5151

5252
class AsyncHookContainer {
53-
constructor(types) {
53+
constructor (types) {
5454
const checkedTypes = types
5555

5656
const idMap = new Map()
@@ -86,7 +86,7 @@ class AsyncHookContainer {
8686
this.watcher = watcher
8787
}
8888

89-
getStore(asyncId) {
89+
getStore (asyncId) {
9090
let resource = this.resourceMap.get(asyncId)
9191

9292
if (resource != null) {

test/requestContextPlugin.e2e.spec.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable promise/param-names */
12
'use strict'
23

34
const request = require('superagent')
@@ -26,7 +27,7 @@ describe('requestContextPlugin E2E', () => {
2627
const route = (req) => {
2728
const requestId = req.requestContext.get('testKey')
2829

29-
function prepareReply() {
30+
function prepareReply () {
3031
return testService.processRequest(requestId.replace('testValue', '')).then(() => {
3132
const storedValue = req.requestContext.get('testKey')
3233
return Promise.resolve({ storedValue })
@@ -104,7 +105,7 @@ describe('requestContextPlugin E2E', () => {
104105

105106
const requestId = `testValue${preHandlerValue}`
106107

107-
function prepareReply() {
108+
function prepareReply () {
108109
return testService.processRequest(requestId.replace('testValue', '')).then(() => {
109110
const storedValue = req.requestContext.get('preValidation')
110111
return Promise.resolve({ storedValue: `testValue${storedValue}` })

0 commit comments

Comments
 (0)