determineApolloConfig.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.determineApolloConfig = void 0;
  4. const utils_createhash_1 = require("@apollo/utils.createhash");
  5. function determineApolloConfig(input, logger) {
  6. const apolloConfig = {};
  7. const { APOLLO_KEY, APOLLO_GRAPH_REF, APOLLO_GRAPH_ID, APOLLO_GRAPH_VARIANT, } = process.env;
  8. if (input?.key) {
  9. apolloConfig.key = input.key.trim();
  10. }
  11. else if (APOLLO_KEY) {
  12. apolloConfig.key = APOLLO_KEY.trim();
  13. }
  14. if ((input?.key ?? APOLLO_KEY) !== apolloConfig.key) {
  15. logger.warn('The provided API key has unexpected leading or trailing whitespace. ' +
  16. 'Apollo Server will trim the key value before use.');
  17. }
  18. if (apolloConfig.key) {
  19. assertValidHeaderValue(apolloConfig.key);
  20. }
  21. if (apolloConfig.key) {
  22. apolloConfig.keyHash = (0, utils_createhash_1.createHash)('sha512')
  23. .update(apolloConfig.key)
  24. .digest('hex');
  25. }
  26. if (input?.graphRef) {
  27. apolloConfig.graphRef = input.graphRef;
  28. }
  29. else if (APOLLO_GRAPH_REF) {
  30. apolloConfig.graphRef = APOLLO_GRAPH_REF;
  31. }
  32. const graphId = input?.graphId ?? APOLLO_GRAPH_ID;
  33. const graphVariant = input?.graphVariant ?? APOLLO_GRAPH_VARIANT;
  34. if (apolloConfig.graphRef) {
  35. if (graphId) {
  36. throw new Error('Cannot specify both graph ref and graph ID. Please use ' +
  37. '`apollo.graphRef` or `APOLLO_GRAPH_REF` without also setting the graph ID.');
  38. }
  39. if (graphVariant) {
  40. throw new Error('Cannot specify both graph ref and graph variant. Please use ' +
  41. '`apollo.graphRef` or `APOLLO_GRAPH_REF` without also setting the graph variant.');
  42. }
  43. }
  44. else if (graphId) {
  45. apolloConfig.graphRef = graphVariant
  46. ? `${graphId}@${graphVariant}`
  47. : graphId;
  48. }
  49. return apolloConfig;
  50. }
  51. exports.determineApolloConfig = determineApolloConfig;
  52. function assertValidHeaderValue(value) {
  53. const invalidHeaderCharRegex = /[^\t\x20-\x7e\x80-\xff]/g;
  54. if (invalidHeaderCharRegex.test(value)) {
  55. const invalidChars = value.match(invalidHeaderCharRegex);
  56. throw new Error(`The API key provided to Apollo Server contains characters which are invalid as HTTP header values. The following characters found in the key are invalid: ${invalidChars.join(', ')}. Valid header values may only contain ASCII visible characters. If you think there is an issue with your key, please contact Apollo support.`);
  57. }
  58. }
  59. //# sourceMappingURL=determineApolloConfig.js.map