@fastify/reply-from 8.4.3 → 9.0.0

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/README.md CHANGED
@@ -292,21 +292,24 @@ The default behavior is `reply.send(error)`, which will be disabled if the
292
292
  option is specified.
293
293
  It must reply the error.
294
294
 
295
- #### `rewriteHeaders(headers, req)`
295
+ #### `rewriteHeaders(headers, request)`
296
296
 
297
297
  Called to rewrite the headers of the response, before them being copied
298
298
  over to the outer response.
299
+ Parameters are the original headers and the Fastify request.
299
300
  It must return the new headers object.
300
301
 
301
- #### `rewriteRequestHeaders(originalReq, headers)`
302
+ #### `rewriteRequestHeaders(request, headers)`
302
303
 
303
304
  Called to rewrite the headers of the request, before them being sent to the other server.
305
+ Parameters are the Fastify request and the original request headers.
304
306
  It must return the new headers object.
305
307
 
306
- #### `getUpstream(originalReq, base)`
308
+ #### `getUpstream(request, base)`
307
309
 
308
310
  Called to get upstream destination, before the request is being sent. Useful when you want to decide which target server to call based on the request data.
309
311
  Helpful for a gradual rollout of new services.
312
+ Parameters are the Fastify request and the base string from the plugin options.
310
313
  It must return the upstream destination.
311
314
 
312
315
  #### `queryString` or `queryString(search, reqUrl)`
package/index.js CHANGED
@@ -63,7 +63,7 @@ const fastifyReplyFrom = fp(function from (fastify, opts, next) {
63
63
  }
64
64
 
65
65
  // we leverage caching to avoid parsing the destination URL
66
- const dest = getUpstream(req, base)
66
+ const dest = getUpstream(this.request, base)
67
67
  let url
68
68
  if (cache) {
69
69
  const cacheKey = dest + source
@@ -136,7 +136,7 @@ const fastifyReplyFrom = fp(function from (fastify, opts, next) {
136
136
 
137
137
  !disableRequestLogging && this.request.log.info({ source }, 'fetching from remote server')
138
138
 
139
- const requestHeaders = rewriteRequestHeaders(req, headers)
139
+ const requestHeaders = rewriteRequestHeaders(this.request, headers)
140
140
  const contentLength = requestHeaders['content-length']
141
141
  let requestImpl
142
142
  if (retryMethods.has(req.method) && !contentLength) {
@@ -166,11 +166,11 @@ const fastifyReplyFrom = fp(function from (fastify, opts, next) {
166
166
  !disableRequestLogging && this.request.log.info('response received')
167
167
  if (sourceHttp2) {
168
168
  copyHeaders(
169
- rewriteHeaders(stripHttp1ConnectionHeaders(res.headers), req),
169
+ rewriteHeaders(stripHttp1ConnectionHeaders(res.headers), this.request),
170
170
  this
171
171
  )
172
172
  } else {
173
- copyHeaders(rewriteHeaders(res.headers, req), this)
173
+ copyHeaders(rewriteHeaders(res.headers, this.request), this)
174
174
  }
175
175
  this.code(res.statusCode)
176
176
  if (onResponse) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fastify/reply-from",
3
- "version": "8.4.3",
3
+ "version": "9.0.0",
4
4
  "description": "forward your HTTP request to another server, for fastify",
5
5
  "main": "index.js",
6
6
  "types": "types/index.d.ts",
@@ -0,0 +1,46 @@
1
+ 'use strict'
2
+
3
+ const t = require('tap')
4
+ const Fastify = require('fastify')
5
+ const From = require('..')
6
+ const http = require('http')
7
+ const get = require('simple-get').concat
8
+
9
+ const instance = Fastify()
10
+ instance.register(From, {
11
+ disableCache: true
12
+ })
13
+
14
+ t.plan(8)
15
+ t.teardown(instance.close.bind(instance))
16
+
17
+ const target = http.createServer((req, res) => {
18
+ t.pass('request proxied')
19
+ t.equal(req.method, 'GET')
20
+ res.end(req.headers.host)
21
+ })
22
+
23
+ instance.get('/', (request, reply) => {
24
+ reply.from(`http://localhost:${target.address().port}`, {
25
+ getUpstream: (req) => {
26
+ t.pass('getUpstream called with correct request parameter')
27
+ t.equal(req, request)
28
+ return `http://localhost:${target.address().port}`
29
+ }
30
+ })
31
+ })
32
+
33
+ t.teardown(target.close.bind(target))
34
+
35
+ instance.listen({ port: 0 }, (err) => {
36
+ t.error(err)
37
+
38
+ target.listen({ port: 0 }, (err) => {
39
+ t.error(err)
40
+
41
+ get(`http://localhost:${instance.server.address().port}`, (err, res) => {
42
+ t.error(err)
43
+ t.equal(res.statusCode, 200)
44
+ })
45
+ })
46
+ })
@@ -0,0 +1,45 @@
1
+ 'use strict'
2
+
3
+ const t = require('tap')
4
+ const Fastify = require('fastify')
5
+ const From = require('..')
6
+ const http = require('http')
7
+ const get = require('simple-get').concat
8
+
9
+ const instance = Fastify()
10
+ instance.register(From)
11
+
12
+ t.plan(8)
13
+ t.teardown(instance.close.bind(instance))
14
+
15
+ const target = http.createServer((req, res) => {
16
+ t.pass('request proxied')
17
+ t.equal(req.method, 'GET')
18
+ res.statusCode = 205
19
+ res.end('hello world')
20
+ })
21
+
22
+ instance.get('/', (request, reply) => {
23
+ reply.from(`http://localhost:${target.address().port}`, {
24
+ rewriteHeaders: (headers, req) => {
25
+ t.pass('rewriteHeaders called with correct request parameter')
26
+ t.equal(req, request)
27
+ return {}
28
+ }
29
+ })
30
+ })
31
+
32
+ t.teardown(target.close.bind(target))
33
+
34
+ instance.listen({ port: 0 }, (err) => {
35
+ t.error(err)
36
+
37
+ target.listen({ port: 0 }, (err) => {
38
+ t.error(err)
39
+
40
+ get(`http://localhost:${instance.server.address().port}`, (err, res, data) => {
41
+ t.error(err)
42
+ t.equal(res.statusCode, 205)
43
+ })
44
+ })
45
+ })
@@ -0,0 +1,45 @@
1
+ 'use strict'
2
+
3
+ const t = require('tap')
4
+ const Fastify = require('fastify')
5
+ const From = require('..')
6
+ const http = require('http')
7
+ const get = require('simple-get').concat
8
+
9
+ const instance = Fastify()
10
+ instance.register(From)
11
+
12
+ t.plan(8)
13
+ t.teardown(instance.close.bind(instance))
14
+
15
+ const target = http.createServer((req, res) => {
16
+ t.pass('request proxied')
17
+ t.equal(req.method, 'GET')
18
+ res.statusCode = 205
19
+ res.end(req.headers.host)
20
+ })
21
+
22
+ instance.get('/', (request, reply) => {
23
+ reply.from(`http://localhost:${target.address().port}`, {
24
+ rewriteRequestHeaders: (originalReq, headers) => {
25
+ t.pass('rewriteRequestHeaders called with correct request parameter')
26
+ t.equal(originalReq, request)
27
+ return {}
28
+ }
29
+ })
30
+ })
31
+
32
+ t.teardown(target.close.bind(target))
33
+
34
+ instance.listen({ port: 0 }, (err) => {
35
+ t.error(err)
36
+
37
+ target.listen({ port: 0 }, (err) => {
38
+ t.error(err)
39
+
40
+ get(`http://localhost:${instance.server.address().port}`, (err, res, data) => {
41
+ t.error(err)
42
+ t.equal(res.statusCode, 205)
43
+ })
44
+ })
45
+ })
package/types/index.d.ts CHANGED
@@ -11,7 +11,6 @@ import {
11
11
  } from 'fastify';
12
12
 
13
13
  import {
14
- IncomingMessage,
15
14
  IncomingHttpHeaders,
16
15
  RequestOptions,
17
16
  AgentOptions,
@@ -23,7 +22,6 @@ import {
23
22
  Agent as SecureAgent
24
23
  } from "https";
25
24
  import {
26
- Http2ServerRequest,
27
25
  IncomingHttpHeaders as Http2IncomingHttpHeaders,
28
26
  ClientSessionRequestOptions,
29
27
  ClientSessionOptions,
@@ -59,14 +57,14 @@ declare namespace fastifyReplyFrom {
59
57
  body?: unknown;
60
58
  rewriteHeaders?: (
61
59
  headers: Http2IncomingHttpHeaders | IncomingHttpHeaders,
62
- req?: Http2ServerRequest | IncomingMessage
60
+ request?: FastifyRequest<RequestGenericInterface, RawServerBase>
63
61
  ) => Http2IncomingHttpHeaders | IncomingHttpHeaders;
64
62
  rewriteRequestHeaders?: (
65
- req: Http2ServerRequest | IncomingMessage,
63
+ request: FastifyRequest<RequestGenericInterface, RawServerBase>,
66
64
  headers: Http2IncomingHttpHeaders | IncomingHttpHeaders
67
65
  ) => Http2IncomingHttpHeaders | IncomingHttpHeaders;
68
66
  getUpstream?: (
69
- req: Http2ServerRequest | IncomingMessage,
67
+ request: FastifyRequest<RequestGenericInterface, RawServerBase>,
70
68
  base: string
71
69
  ) => string;
72
70
  }
@@ -1,11 +1,10 @@
1
1
  import replyFrom, { FastifyReplyFromOptions } from "..";
2
- import fastify, {FastifyReply, RawServerBase} from "fastify";
2
+ import fastify, {FastifyReply, FastifyRequest, RawServerBase, RequestGenericInterface} from "fastify";
3
3
  import { AddressInfo } from "net";
4
4
  import { IncomingHttpHeaders } from "http2";
5
5
  import { expectType } from 'tsd';
6
6
  import * as http from 'http';
7
7
  import * as https from 'https';
8
- import * as http2 from 'http2';
9
8
  // @ts-ignore
10
9
  import tap from 'tap'
11
10
 
@@ -67,11 +66,11 @@ async function main() {
67
66
  reply.from("/v3", {
68
67
  body: {hello: "world"},
69
68
  rewriteRequestHeaders(req, headers) {
70
- expectType<http.IncomingMessage | http2.Http2ServerRequest>(req);
69
+ expectType<FastifyRequest<RequestGenericInterface, RawServerBase>>(req);
71
70
  return headers;
72
71
  },
73
72
  getUpstream(req, base) {
74
- expectType<http.IncomingMessage | http2.Http2ServerRequest>(req);
73
+ expectType<FastifyRequest<RequestGenericInterface, RawServerBase>>(req);
75
74
  return base;
76
75
  }
77
76
  });