From 1a4331d9ab2671db7860b4a7cac4ac047ea38ad3 Mon Sep 17 00:00:00 2001 From: Tim Kye Date: Mon, 27 May 2019 11:00:50 -0700 Subject: [PATCH] Lazy-load `subscriptions-transport-ws` and `util.promisify` polyfill. (#2324) * lazy load unused lambda packages in core * Style adjustments plus changes to account for other PRs with similar motives. The work in https://github.com/apollographql/apollo-server/pull/2054 was designed in a way that, irregardless of the environment, the `graphql-upload` package would only be loaded if uploads were enabled. Therefore, the guard which checks `process.env.AWS_EXECUTION_ENV` should no longer be necessary. Additionally, we don't need to prefix our type-only variables with underscores, as that's not a style that we've otherwise adopted. * The work in this was mostly also implemented by #2305, #2304 and #2054, but the remaining subscriptions-transport-ws and unnecessary util.promisify imports are still super worth addressing. So, thank you! --- CHANGELOG.md | 2 ++ .../apollo-server-core/src/ApolloServer.ts | 2 +- packages/apollo-server-env/src/index.ts | 6 ++++- .../src/utils/runtimeSupportsPromisify.ts | 24 +++++++++++++++++++ 4 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 packages/apollo-server-env/src/utils/runtimeSupportsPromisify.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 21ca9567472..74388218c5a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ - core: Expose SHA-256 hex hash digest of the Engine API key to plugins, when available, as `engine.apiKeyHash`. [PR# 2685](https://github.com/apollographql/apollo-server/pull/2685) - `apollo-datasource-rest`: If another `Content-type` is already set on the response, don't overwrite it with `application/json`, allowing the user's initial `Content-type` to prevail. [PR #2520](https://github.com/apollographql/apollo-server/issues/2035) - `apollo-cache-control`: Do not respond with `Cache-control` headers if the HTTP response contains `errors`. [PR #2715](https://github.com/apollographql/apollo-server/pull/2715) +- `apollo-server-core`: Skip loading `util.promifisy` polyfill in Node.js engines >= 8.0 [PR #2278](https://github.com/apollographql/apollo-server/pull/2278) +- `apollo-server-core`: Lazy load `subscriptions-transport-ws` in core [PR #2278](https://github.com/apollographql/apollo-server/pull/2278) - `apollo-server-cache-redis`: **BREAKING FOR USERS OF `apollo-server-cache-redis`** (This is a package that must be updated separately but shares the same `CHANGELOG.md` with Apollo Server itself.) A new **major** version of this package has been published and updated to support Redis Standalone, Cluster and Sentinel modes. This is a breaking change since it is now based on [`ioredis`](https://github.com/luin/ioredis) instead of [`node_redis`](https://github.com/NodeRedis/node_redis). Although this update is compatible with the most common uses of `apollo-server-cache-redis`, please check the [options supported by `ioredis`](https://github.com/luin/ioredis/blob/master/API.md#new-redisport-host-options) while updating to this version. The constructor options are passed directly from `RedisCache` to the new Redis adapter. The pre-1.0 versions should continue to work with Apollo Server without modification. [PR #1770](https://github.com/apollographql/apollo-server/pull/1770) ### v2.5.0 diff --git a/packages/apollo-server-core/src/ApolloServer.ts b/packages/apollo-server-core/src/ApolloServer.ts index 8316340f451..ea0a433a0e3 100644 --- a/packages/apollo-server-core/src/ApolloServer.ts +++ b/packages/apollo-server-core/src/ApolloServer.ts @@ -439,7 +439,7 @@ export class ApolloServerBase { ); } } - + const { SubscriptionServer } = require('subscriptions-transport-ws'); const { onDisconnect, onConnect, diff --git a/packages/apollo-server-env/src/index.ts b/packages/apollo-server-env/src/index.ts index eb5864bcd0b..9ae309af676 100644 --- a/packages/apollo-server-env/src/index.ts +++ b/packages/apollo-server-env/src/index.ts @@ -1,7 +1,11 @@ import './polyfills/Object.values'; import './polyfills/Object.entries'; -require('util.promisify').shim(); +import runtimeSupportsPromisify from './utils/runtimeSupportsPromisify'; + +if (!runtimeSupportsPromisify) { + require('util.promisify').shim(); +} export * from './polyfills/fetch'; export * from './polyfills/url'; diff --git a/packages/apollo-server-env/src/utils/runtimeSupportsPromisify.ts b/packages/apollo-server-env/src/utils/runtimeSupportsPromisify.ts new file mode 100644 index 00000000000..620a6277cef --- /dev/null +++ b/packages/apollo-server-env/src/utils/runtimeSupportsPromisify.ts @@ -0,0 +1,24 @@ +const runtimeSupportsPromisify = (() => { + if ( + process && + process.release && + process.release.name === 'node' && + process.versions && + typeof process.versions.node === 'string' + ) { + const [nodeMajor] = process.versions.node + .split('.', 1) + .map(segment => parseInt(segment, 10)); + + if (nodeMajor >= 8) { + return true; + } + return false; + } + + // If we haven't matched any of the above criteria, we'll remain unsupported + // for this mysterious environment until a pull-request proves us otherwise. + return false; +})(); + +export default runtimeSupportsPromisify;