Skip to content

Commit

Permalink
feat: support restify v9-v11 (#1489)
Browse files Browse the repository at this point in the history
Fixes #1488

(see also #1250 for reference)
  • Loading branch information
6utt3rfly committed Apr 25, 2023
1 parent 81d9d5c commit 746f30c
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .readme-partials.yaml
Expand Up @@ -69,7 +69,7 @@ body: |-
* [gRPC](https://www.npmjs.com/package/grpc) server (version ^1.1)
* [hapi](https://www.npmjs.com/package/hapi) (versions 8 - 19)
* [koa](https://www.npmjs.com/package/koa) (version 1 - 2)
* [restify](https://www.npmjs.com/package/restify) (versions 3 - 8)
* [restify](https://www.npmjs.com/package/restify) (versions 3 - 11)
The agent will also automatically trace RPCs from the following modules:
* Outbound HTTP requests through `http`, `https`, and `http2` core modules
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -122,7 +122,7 @@ The trace agent can do automatic tracing of the following web frameworks:
* [gRPC](https://www.npmjs.com/package/grpc) server (version ^1.1)
* [hapi](https://www.npmjs.com/package/hapi) (versions 8 - 19)
* [koa](https://www.npmjs.com/package/koa) (version 1 - 2)
* [restify](https://www.npmjs.com/package/restify) (versions 3 - 8)
* [restify](https://www.npmjs.com/package/restify) (versions 3 - 11)

The agent will also automatically trace RPCs from the following modules:
* Outbound HTTP requests through `http`, `https`, and `http2` core modules
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/plugin-restify.ts
Expand Up @@ -25,7 +25,7 @@ type Response = restify_5.Response;
type Next = restify_5.Next;
type CreateServerFn = (options?: restify_5.ServerOptions) => restify_5.Server;

const SUPPORTED_VERSIONS = '<=8.x';
const SUPPORTED_VERSIONS = '<=11.x';

function unpatchRestify(restify: Restify5) {
shimmer.unwrap(restify, 'createServer');
Expand Down
15 changes: 15 additions & 0 deletions test/fixtures/plugin-fixtures.json
Expand Up @@ -243,5 +243,20 @@
"dependencies": {
"restify": "^8.5.1"
}
},
"restify9": {
"dependencies": {
"restify": "^9.0.0"
}
},
"restify10": {
"dependencies": {
"restify": "^10.0.0"
}
},
"restify11": {
"dependencies": {
"restify": "^11.0.0"
}
}
}
6 changes: 6 additions & 0 deletions test/test-trace-web-frameworks.ts
Expand Up @@ -42,6 +42,9 @@ import {
Restify6,
Restify7,
Restify8,
Restify9,
Restify10,
Restify11,
} from './web-frameworks/restify';

// The type of a stack trace object after being parsed from a trace span's stack
Expand Down Expand Up @@ -70,6 +73,9 @@ const FRAMEWORKS: WebFrameworkConstructor[] = [
Restify6,
Restify7,
Restify8,
Restify9,
Restify10,
Restify11,
];

/**
Expand Down
40 changes: 16 additions & 24 deletions test/web-frameworks/restify.ts
Expand Up @@ -14,11 +14,7 @@

import {restify_5} from '../../src/plugins/types';

import {
WebFramework,
WebFrameworkAddHandlerOptions,
WebFrameworkResponse,
} from './base';
import {WebFramework, WebFrameworkAddHandlerOptions} from './base';

export class Restify implements WebFramework {
server: restify_5.Server;
Expand All @@ -35,31 +31,24 @@ export class Restify implements WebFramework {
);
}
if (options.hasResponse) {
this.server.get(options.path, async (req, res, next) => {
let response: WebFrameworkResponse;
try {
response = await options.fn(req.headers);
} catch (e) {
next(e);
return;
}
res.statusCode = response.statusCode;
res.end(response.message);
next();
this.server.get(options.path, (req, res, next) => {
Promise.resolve()
.then(() => options.fn(req.headers))
.then(response => {
res.statusCode = response.statusCode;
res.end(response.message);
})
.then(() => next(), next);
});
} else {
this.server.use(async (req, res, next) => {
this.server.use((req, res, next) => {
if (req.getPath() !== options.path) {
next();
return;
}
try {
await options.fn(req.headers);
} catch (e) {
next(e);
return;
}
next();
Promise.resolve()
.then(() => options.fn(req.headers))
.then(() => next(), next);
});
}
}
Expand Down Expand Up @@ -91,3 +80,6 @@ export const Restify5 = makeRestifyClass(5);
export const Restify6 = makeRestifyClass(6);
export const Restify7 = makeRestifyClass(7);
export const Restify8 = makeRestifyClass(8);
export const Restify9 = makeRestifyClass(9, '>12');
export const Restify10 = makeRestifyClass(10, '>12');
export const Restify11 = makeRestifyClass(11, '>12');

0 comments on commit 746f30c

Please sign in to comment.