Skip to content
This repository has been archived by the owner on Nov 4, 2023. It is now read-only.

Commit

Permalink
Merge pull request #3 from jeromemacias/fastify_v3
Browse files Browse the repository at this point in the history
Upgrade to Fastify v3
  • Loading branch information
jeromemacias authored Sep 23, 2021
2 parents 297f57e + d282f7e commit 51a5832
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 79 deletions.
1 change: 0 additions & 1 deletion .prettierignore

This file was deleted.

11 changes: 2 additions & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,8 @@ language: node_js

node_js:
- "10"
- "lts/*"
- "node"

env:
- FASTIFY_VERSION=^2.0.0
- FASTIFY_VERSION=2.9.0

before_script:
- npm i --no-save fastify@$FASTIFY_VERSION
- "12"
- "14"

script:
- npm run lint-ci
Expand Down
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2020 Jérôme Macias
Copyright (c) 2021 Jérôme Macias

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
## Requirements

* AWS X-Ray SDK Core (aws-xray-sdk-core)
* Fastify 2.0.0 or greater
* Fastify 3.0.0 or greater

## AWS X-Ray and Fastify

Expand Down
19 changes: 11 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
"type": "git",
"url": "git+ssh://[email protected]/jeromemacias/fastify-xray.git"
},
"files": [
"plugin.d.ts"
],
"keywords": [
"fastify",
"cookie"
Expand All @@ -30,17 +33,17 @@
},
"homepage": "https://github.com/jeromemacias/fastify-xray#readme",
"devDependencies": {
"@types/node": "^11.9.3",
"fastify": "^2.0.0",
"@types/node": "^14.14.35",
"fastify": "^3.14.0",
"pre-commit": "^1.2.2",
"sinon": "~9.0.2",
"snazzy": "^8.0.0",
"standard": "^12.0.1",
"tap": "^12.5.2",
"typescript": "^3.3.1"
"snazzy": "^9.0.0",
"standard": "^16.0.3",
"tap": "^14.11.0",
"typescript": "^4.2.3"
},
"dependencies": {
"aws-xray-sdk-core": "^3.1.0",
"fastify-plugin": "^1.6.1"
"aws-xray-sdk-core": "^3.2.0",
"fastify-plugin": "^3.0.0"
}
}
25 changes: 4 additions & 21 deletions plugin.d.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,14 @@
/// <reference types="node" />
import * as fastify from "fastify";
import { FastifyRequest } from "fastify";
import { IncomingMessage, Server, ServerResponse } from "http";
import { Http2ServerRequest } from "http2";
import * as AWSXRay from "aws-xray-sdk-core";

type HttpRequest = IncomingMessage | Http2ServerRequest;

declare module "fastify" {
interface FastifyInstance<
HttpServer = Server,
HttpRequest = IncomingMessage,
HttpResponse = ServerResponse
> {
interface FastifyInstance {
/**
* AWSXRay custom instance
*/
AWSXRay?: any;
*/
AWSXRay?: any;
}

interface FastifyRequest<
HttpRequest,
Query = fastify.DefaultQuery,
Params = fastify.DefaultParams,
Headers = fastify.DefaultHeaders,
Body = any
> {
interface FastifyRequest {
/**
* Request XRay Segment
*/
Expand Down
10 changes: 5 additions & 5 deletions plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function plugin (fastify, options, next) {

function fastifyXrayOnRequest (request, reply, done) {
const req = request.raw
const res = reply.res
const res = reply.raw
res.req = req
res.header = {}

Expand Down Expand Up @@ -45,7 +45,7 @@ function plugin (fastify, options, next) {
)

if (AWSXRay.isAutomaticMode()) {
var ns = AWSXRay.getNamespace()
const ns = AWSXRay.getNamespace()
ns.bindEmitter(req)
ns.bindEmitter(res)

Expand Down Expand Up @@ -88,8 +88,8 @@ function plugin (fastify, options, next) {
segment[cause] = true
}

if (segment.http && reply.res) {
segment.http.close(reply.res)
if (segment.http && reply.raw) {
segment.http.close(reply.raw)
}
segment.close()

Expand Down Expand Up @@ -132,6 +132,6 @@ function plugin (fastify, options, next) {
}

module.exports = fp(plugin, {
fastify: '>=2.0.0',
fastify: '>=3.0.0',
name: 'fastify-xray'
})
51 changes: 23 additions & 28 deletions test/types.test.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,44 @@
import fastify = require("fastify");
import http2 = require("http2");
import xray = require("../");
import AWSXRay = require("aws-xray-sdk-core");
import fastify from 'fastify'
import xray from '../'
import AWSXRay from 'aws-xray-sdk-core'

const appWithImplicitHttp = fastify();
const appWithImplicitHttp = fastify()

appWithImplicitHttp.decorate('AWSXRay', AWSXRay)

appWithImplicitHttp.AWSXRay.config([AWSXRay.plugins.EC2Plugin])

appWithImplicitHttp
.register(xray, {
defaultName: "May App",
defaultName: 'May App'
})
.after(() => {
appWithImplicitHttp.get("/", (request) => {
const segment = request.segment;
appWithImplicitHttp.get('/', (request) => {
const segment = request.segment

segment && segment.addMetadata("test", "meta");
});
});
segment && segment.addMetadata('test', 'meta')
})
})

appWithImplicitHttp
appWithImplicitHttp
.register(xray, {
defaultName: "May App",
defaultName: 'May App',
AWSXRay
})
.after(() => {
appWithImplicitHttp.get("/", (request) => {
const segment = request.segment;
appWithImplicitHttp.get('/', (request) => {
const segment = request.segment

segment && segment.addMetadata("test", "meta");
});
});
segment && segment.addMetadata('test', 'meta')
})
})

const appWithHttp2: fastify.FastifyInstance<
http2.Http2Server,
http2.Http2ServerRequest,
http2.Http2ServerResponse
> = fastify();
const appWithHttp2 = fastify()

appWithHttp2.register(xray).after(() => {
appWithHttp2.get("/", (request) => {
const segment = request.segment;
appWithHttp2.get('/', (request) => {
const segment = request.segment

segment && segment.addMetadata("test", "meta");
});
});
segment && segment.addMetadata('test', 'meta')
})
})
10 changes: 5 additions & 5 deletions test/xray.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ const test = require('tap').test
const xray = require('aws-xray-sdk-core')
const Fastify = require('fastify')
const sinon = require('sinon')
var EventEmitter = require('events')
var util = require('util')
const EventEmitter = require('events')
const util = require('util')
const plugin = require('../plugin')

const SegmentEmitter = require('aws-xray-sdk-core/lib/segment_emitter')
Expand All @@ -15,7 +15,7 @@ const mwUtils = xray.middleware
const IncomingRequestData = xray.middleware.IncomingRequestData
const Segment = xray.Segment

var TestUtils = {}
const TestUtils = {}

TestUtils.TestEmitter = function TestEmitter () {
EventEmitter.call(this)
Expand All @@ -41,8 +41,8 @@ function register (AWSXRay, giveInstanceAs) {
request.raw.on = TestUtils.onEvent
request.raw.headers = { host: hostName }

reply.res.emitter = new TestUtils.TestEmitter()
reply.res.on = TestUtils.onEvent
reply.raw.emitter = new TestUtils.TestEmitter()
reply.raw.on = TestUtils.onEvent

done()
})
Expand Down

0 comments on commit 51a5832

Please sign in to comment.