Skip to content

Commit

Permalink
fix(types): fix node16 resolution
Browse files Browse the repository at this point in the history
Using the tool attw we saw that the default export was not
behaving correctly when node16 or nodenext was used as a resolution
algorithm. We fixed that and intergrated a lot more tests.

We also added support for the legacy node11 (node) resolution algorithm.

BREAKING CHANGES: we changed the exproted types
  • Loading branch information
dnlup committed Aug 24, 2023
1 parent febca73 commit 87d4038
Show file tree
Hide file tree
Showing 32 changed files with 523 additions and 59 deletions.
3 changes: 2 additions & 1 deletion .taprc
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
files:
- 'test/**/*.test.js'
- 'test/*.test.js'
lines: 85
functions: 95
branches: 80
statements: 95
node-arg: "--trace-warnings"
timeout: 120
46 changes: 22 additions & 24 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
import {
FastifyError,
FastifyRequest,
FastifyReply,
FastifyPluginCallback,
FastifyPluginAsync
} from 'fastify';
import Sentry, { Hub } from '@sentry/node';

import { RequestKeys, UserData, RequestData, shouldHandleError, errorResponse, getTransactionName, extractRequestData, extractUserData } from './types/utils';
import { shouldHandleError, errorResponse, getTransactionName, extractRequestData, extractUserData } from './utils';

export interface SentryPluginOptions extends Sentry.NodeOptions {
/** Set the plugin error handler */
setErrorHandler?: boolean
/** Called inside the error handler, it should return `true` of `false`depending on the fact we want to send the error to Sentry or not */
shouldHandleError?: typeof shouldHandleError
/** Custom hook to respond the errored request */
errorResponse?: typeof errorResponse
/** Custom function to build the transaction name from the request */
getTransactionName?: typeof getTransactionName
/** Custom function to extract the request data */
extractRequestData?: typeof extractRequestData
/** Custom function to extract the user data from the request */
extractUserData?: typeof extractUserData
}
type FastifySentryPlugin = FastifyPluginCallback<fastifySentry.FastifySentryOptions>

export const sentryPluginCallback: FastifyPluginCallback<SentryPluginOptions>;
export const sentryPluginAsync: FastifyPluginAsync<SentryPluginOptions>;
declare namespace fastifySentry {
export interface FastifySentryOptions {
/** Set the plugin error handler */
setErrorHandler?: boolean
/** Called inside the error handler, it should return `true` of `false`depending on the fact we want to send the error to Sentry or not */
shouldHandleError?: typeof shouldHandleError
/** Custom hook to respond the errored request */
errorResponse?: typeof errorResponse
/** Custom function to build the transaction name from the request */
getTransactionName?: typeof getTransactionName
/** Custom function to extract the request data */
extractRequestData?: typeof extractRequestData
/** Custom function to extract the user data from the request */
extractUserData?: typeof extractUserData
}
export const fastifySentry: FastifySentryPlugin
export { fastifySentry as default }
}

export default sentryPluginCallback;
declare function fastifySentry(...params: Parameters<FastifySentryPlugin>): ReturnType<FastifySentryPlugin>
export = fastifySentry;

declare module 'fastify' {
interface FastifyInstance {
Expand All @@ -40,5 +40,3 @@ declare module 'fastify' {
sentryTransaction: ReturnType<Hub['startTransaction']> | null
}
}

export * from './types/utils';
35 changes: 18 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const {
extractUserData,
errorResponse,
shouldHandleError,
} = require('./lib/utils');
} = require('./utils');

const DEFAULT_CONFIG = {
setErrorHandler: true,
Expand All @@ -21,20 +21,21 @@ const DEFAULT_CONFIG = {
extractUserData,
};

module.exports = fp(
function (fastify, opts, next) {
const config = Object.assign({}, DEFAULT_CONFIG, opts);
try {
validate(config);
} catch (error) {
return next(error);
}
base(fastify, config);
request(fastify);
next();
},
{
name: '@immobiliarelabs/fastify-sentry',
fastify: '4.x',
const fastifySentry = function (fastify, opts, next) {
const config = Object.assign({}, DEFAULT_CONFIG, opts);
try {
validate(config);
} catch (error) {
return next(error);
}
);
base(fastify, config);
request(fastify);
next();
};

module.exports = fp(fastifySentry, {
name: '@immobiliarelabs/fastify-sentry',
fastify: '4.x',
});
module.exports.default = fastifySentry;
module.exports.fastifySentry = fastifySentry;
2 changes: 1 addition & 1 deletion lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const {
baggageHeaderToDynamicSamplingContext,
} = require('@sentry/utils');
const { hasTracingEnabled } = require('@sentry/tracing');
const { extractPathForTransaction } = require('./utils');
const { extractPathForTransaction } = require('../utils');
const {
kSentryRequestData,
kSentryExtractRequestData,
Expand Down
2 changes: 1 addition & 1 deletion nyc.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';

module.exports = {
exclude: ['*tests*'],
exclude: ['*tests*', '*tap-snapshots*', '*test*'],
};
31 changes: 17 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@
"main": "index.js",
"type": "commonjs",
"exports": {
".": "./index.js",
"./utils": "./lib/utils.js"
".": {
"default": "./index.js",
"types": "./index.d.ts"
},
"./utils": {
"default": "./utils.js",
"types": "./utils.d.ts"
}
},
"publishConfig": {
"access": "public"
Expand All @@ -28,12 +34,14 @@
"files": [
"lib",
"index.js",
"utils.js",
"index.d.ts",
"types"
"utils.d.ts"
],
"scripts": {
"lint": "eslint --fix --ignore-path .gitignore .",
"test": "tsd && tap",
"test": "npm run test:ts && tap",
"test:ts": "attw --pack && tsd",
"bench": "npm run bench:base && npm run bench:plugin",
"bench:base": "npx concurrently -k -s first \"node ./benchmarks/base.js\" \"node -e 'setTimeout(() => {}, 1000)' && npx autocannon localhost:4001 && npx autocannon localhost:4001/error\"",
"bench:plugin": "npx concurrently -k -s first \"node ./benchmarks/sentry-mock.js\" \"node ./benchmarks/with-sentry.js\" \"node -e 'setTimeout(() => {}, 1000)' && npx autocannon localhost:4002 && npx autocannon localhost:4002/error\"",
Expand All @@ -55,6 +63,7 @@
"npm": ">=8"
},
"devDependencies": {
"@arethetypeswrong/cli": "^0.7.1",
"@commitlint/cli": "^17.0.0",
"@commitlint/config-conventional": "^17.0.0",
"@fastify/sensible": "^5.1.0",
Expand Down Expand Up @@ -91,21 +100,15 @@
"path": "./node_modules/cz-conventional-changelog"
}
},
"types": "./index.d.ts",
"tsd": {
"directory": "test/types"
},
"dependencies": {
"@sentry/node": "^7.60.0",
"@sentry/tracing": "^7.60.0",
"@sentry/utils": "^7.60.0",
"cookie": "^0.5.0",
"fastify-plugin": "^4.3.0"
},
"typesVersions": {
"*": {
"utils": [
"types/utils.d.ts"
]
}
},
"tsd": {
"directory": "test/types"
}
}
11 changes: 11 additions & 0 deletions test/fixtures/cjs/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

const fastify = require('fastify');
const plugin = require('@immobiliarelabs/fastify-sentry');
const assert = require('assert');

const app = fastify();
app.register(plugin);
app.ready().then(() => {
assert(app.Sentry !== undefined);
});
16 changes: 16 additions & 0 deletions test/fixtures/cjs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "cjs",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@immobiliarelabs/fastify-sentry": "file:../pack.tgz",
"fastify": "^4.21.0"
}
}
9 changes: 9 additions & 0 deletions test/fixtures/esm/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import fastify from 'fastify';
import plugin from '@immobiliarelabs/fastify-sentry';
import assert from 'assert';

const app = fastify();
app.register(plugin);
app.ready().then(() => {
assert(app.Sentry !== undefined)
})
16 changes: 16 additions & 0 deletions test/fixtures/esm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "esm",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@immobiliarelabs/fastify-sentry": "file:../pack.tgz",
"fastify": "^4.21.0"
}
}
65 changes: 65 additions & 0 deletions test/fixtures/ts/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Logs
logs
*.log
npm-debug.log*

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http:https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http:https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules
jspm_packages

# Optional npm cache directory
.npm

# Optional REPL history
.node_repl_history

# 0x
profile-*

# mac files
.DS_Store

# vim swap files
*.swp

# webstorm
.idea

# vscode
.vscode
*code-workspace

# clinic
profile*
*clinic*
*flamegraph*

# generated code
examples/typescript-server.js
test/types/index.js

# compiled app
dist
5 changes: 5 additions & 0 deletions test/fixtures/ts/.taprc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
test-env: [
TS_NODE_FILES=true,
TS_NODE_PROJECT=./test/tsconfig.json
]
timeout: 120
23 changes: 23 additions & 0 deletions test/fixtures/ts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Getting Started with [Fastify-CLI](https://www.npmjs.com/package/fastify-cli)
This project was bootstrapped with Fastify-CLI.

## Available Scripts

In the project directory, you can run:

### `npm run dev`

To start the app in dev mode.\
Open [http:https://localhost:3000](http:https://localhost:3000) to view it in the browser.

### `npm start`

For production mode

### `npm run test`

Run the test cases.

## Learn More

To learn Fastify, check out the [Fastify documentation](https://www.fastify.io/docs/latest/).
37 changes: 37 additions & 0 deletions test/fixtures/ts/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "ts",
"version": "1.0.0",
"description": "This project was bootstrapped with Fastify-CLI.",
"main": "app.ts",
"directories": {
"test": "test"
},
"scripts": {
"test": "npm run build:ts && tsc -p test/tsconfig.json && tap --ts \"test/**/*.test.ts\"",
"start": "npm run build:ts && fastify start -l info dist/app.js",
"build:ts": "tsc",
"watch:ts": "tsc -w",
"dev": "npm run build:ts && concurrently -k -p \"[{name}]\" -n \"TypeScript,App\" -c \"yellow.bold,cyan.bold\" \"npm:watch:ts\" \"npm:dev:start\"",
"dev:start": "fastify start --ignore-watch=.ts$ -w -l info -P dist/app.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@fastify/autoload": "^5.0.0",
"@fastify/sensible": "^5.0.0",
"@immobiliarelabs/fastify-sentry": "file:../pack.tgz",
"fastify": "^4.0.0",
"fastify-cli": "^5.8.0",
"fastify-plugin": "^4.0.0"
},
"devDependencies": {
"@types/node": "^20.4.4",
"@types/tap": "^15.0.5",
"concurrently": "^7.0.0",
"fastify-tsconfig": "^1.0.1",
"tap": "^16.1.0",
"ts-node": "^10.4.0",
"typescript": "^4.5.4"
}
}
Loading

0 comments on commit 87d4038

Please sign in to comment.