-
Notifications
You must be signed in to change notification settings - Fork 256
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Gateway: introduce
make-fetch-happen
(apollographql/apollo-server#3783
) * Introduce `make-fetch-happen` for GCS requests * Implement in memory cache manager for make-fetch-happen * Provide local typings for `make-fetch-happen` * Skip node v6 tests for federation and gateway packages Apollo-Orig-Commit-AS: apollographql/apollo-server@9420aa0
- Loading branch information
1 parent
c19a13b
commit e77a0cb
Showing
9 changed files
with
164 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,19 @@ | ||
const config = require('../../jest.config.base'); | ||
|
||
module.exports = Object.assign(Object.create(null), config, { | ||
const NODE_MAJOR_VERSION = parseInt( | ||
process.versions.node.split('.', 1)[0], | ||
10 | ||
); | ||
|
||
const additionalConfig = { | ||
setupFiles: [ | ||
'core-js/features/array/flat', | ||
'core-js/features/array/flat-map', | ||
], | ||
}); | ||
testPathIgnorePatterns: [ | ||
...config.testPathIgnorePatterns, | ||
...NODE_MAJOR_VERSION === 6 ? ["<rootDir>"] : [] | ||
] | ||
}; | ||
|
||
module.exports = Object.assign(Object.create(null), config, additionalConfig); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,17 @@ | ||
const path = require('path'); | ||
const config = require('../../jest.config.base'); | ||
|
||
const NODE_MAJOR_VERSION = parseInt( | ||
process.versions.node.split('.', 1)[0], | ||
10 | ||
); | ||
|
||
const additionalConfig = { | ||
setupFilesAfterEnv: [path.resolve(__dirname, './src/__tests__/testSetup.ts')], | ||
testPathIgnorePatterns: [ | ||
...config.testPathIgnorePatterns, | ||
...NODE_MAJOR_VERSION === 6 ? ["<rootDir>"] : [] | ||
] | ||
}; | ||
module.exports = Object.assign(Object.create(null), additionalConfig, config); | ||
|
||
module.exports = Object.assign(Object.create(null), config, additionalConfig); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { CacheManager } from 'make-fetch-happen'; | ||
import { Request, Response, Headers } from 'apollo-server-env'; | ||
import { InMemoryLRUCache } from 'apollo-server-caching'; | ||
|
||
const MAX_SIZE = 5 * 1024 * 1024; // 5MB | ||
|
||
function cacheKey(request: Request) { | ||
return `gateway:request-cache:${request.method}:${request.url}`; | ||
} | ||
|
||
interface CachedRequest { | ||
body: string; | ||
status: number; | ||
statusText: string; | ||
headers: Headers; | ||
} | ||
|
||
export class HttpRequestCache implements CacheManager { | ||
constructor( | ||
public cache: InMemoryLRUCache<CachedRequest> = new InMemoryLRUCache({ | ||
maxSize: MAX_SIZE, | ||
}), | ||
) {} | ||
|
||
// Return true if entry exists, else false | ||
async delete(request: Request) { | ||
const key = cacheKey(request); | ||
const entry = await this.cache.get(key); | ||
await this.cache.delete(key); | ||
return Boolean(entry); | ||
} | ||
|
||
async put(request: Request, response: Response) { | ||
let body = await response.text(); | ||
|
||
this.cache.set(cacheKey(request), { | ||
body, | ||
status: response.status, | ||
statusText: response.statusText, | ||
headers: response.headers, | ||
}); | ||
|
||
return new Response(body, response); | ||
} | ||
|
||
async match(request: Request) { | ||
return this.cache.get(cacheKey(request)).then(response => { | ||
if (response) { | ||
const { body, ...requestInit } = response; | ||
return new Response(body, requestInit); | ||
} | ||
return; | ||
}); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
declare module 'make-fetch-happen' { | ||
import { | ||
Response, | ||
Request, | ||
RequestInfo, | ||
RequestInit, | ||
} from 'apollo-server-env'; | ||
|
||
// If adding to these options, they should mirror those from `make-fetch-happen` | ||
// @see: https://github.com/npm/make-fetch-happen/#extra-options | ||
export interface FetcherOptions { | ||
cacheManager?: string | CacheManager; | ||
// @see: https://www.npmjs.com/package/retry#retrytimeoutsoptions | ||
retry?: | ||
| boolean | ||
| number | ||
| { | ||
// The maximum amount of times to retry the operation. Default is 10. Seting this to 1 means do it once, then retry it once | ||
retries?: number; | ||
// The exponential factor to use. Default is 2. | ||
factor?: number; | ||
// The number of milliseconds before starting the first retry. Default is 1000. | ||
minTimeout?: number; | ||
// The maximum number of milliseconds between two retries. Default is Infinity. | ||
maxTimeout?: number; | ||
// Randomizes the timeouts by multiplying with a factor between 1 to 2. Default is false. | ||
randomize?: boolean; | ||
}; | ||
onRetry?(): void; | ||
} | ||
|
||
export interface CacheManager { | ||
delete(req: Request): Promise<Boolean>; | ||
put(req: Request, res: Response): Promise<Response>; | ||
match(req: Request): Promise<Response | undefined>; | ||
} | ||
|
||
/** | ||
* This is an augmentation of the fetch function types provided by `apollo-server-env` | ||
* @see: https://git.io/JvBwX | ||
*/ | ||
export interface Fetcher { | ||
(input?: RequestInfo, init?: RequestInit & FetcherOptions): Promise< | ||
Response | ||
>; | ||
} | ||
|
||
let fetch: Fetcher & { | ||
defaults(opts?: FetcherOptions): Fetcher; | ||
}; | ||
|
||
export default fetch; | ||
} |