@graphql-yoga/plugin-apollo-usage-report 0.8.0 → 0.8.1

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/cjs/index.js CHANGED
@@ -6,6 +6,7 @@ const graphql_1 = require("graphql");
6
6
  const graphql_yoga_1 = require("graphql-yoga");
7
7
  const usage_reporting_protobuf_1 = require("@apollo/usage-reporting-protobuf");
8
8
  const plugin_apollo_inline_trace_1 = require("@graphql-yoga/plugin-apollo-inline-trace");
9
+ const promise_helpers_1 = require("@whatwg-node/promise-helpers");
9
10
  function getEnvVar(name, defaultValue) {
10
11
  return globalThis.process?.env?.[name] || defaultValue || undefined;
11
12
  }
@@ -42,7 +43,7 @@ function useApolloUsageReport(options = {}) {
42
43
  },
43
44
  onSchemaChange({ schema }) {
44
45
  if (schema) {
45
- schemaIdSet$ = hashSHA256((0, graphql_1.printSchema)(schema), yoga.fetchAPI).then(id => {
46
+ schemaIdSet$ = (0, promise_helpers_1.handleMaybePromise)(() => hashSHA256((0, graphql_1.printSchema)(schema), yoga.fetchAPI), id => {
46
47
  schemaId = id;
47
48
  schemaIdSet$ = undefined;
48
49
  });
@@ -108,50 +109,48 @@ function useApolloUsageReport(options = {}) {
108
109
  },
109
110
  };
110
111
  }
111
- async function hashSHA256(str, api = globalThis) {
112
- const { crypto, TextEncoder } = api;
113
- const textEncoder = new TextEncoder();
114
- const utf8 = textEncoder.encode(str);
115
- const hashBuffer = await crypto.subtle.digest('SHA-256', utf8);
116
- let hashHex = '';
117
- for (const bytes of new Uint8Array(hashBuffer)) {
118
- hashHex += bytes.toString(16).padStart(2, '0');
119
- }
120
- return hashHex;
112
+ function hashSHA256(text, api = globalThis) {
113
+ const inputUint8Array = new api.TextEncoder().encode(text);
114
+ return (0, promise_helpers_1.handleMaybePromise)(() => api.crypto.subtle.digest({ name: 'SHA-256' }, inputUint8Array), arrayBuf => {
115
+ const outputUint8Array = new Uint8Array(arrayBuf);
116
+ let hash = '';
117
+ for (const byte of outputUint8Array) {
118
+ const hex = byte.toString(16);
119
+ hash += '00'.slice(0, Math.max(0, 2 - hex.length)) + hex;
120
+ }
121
+ return hash;
122
+ });
121
123
  }
122
- async function sendTrace(options, logger, fetch, schemaId, tracesPerQuery, agentVersion) {
124
+ function sendTrace(options, logger, fetch, schemaId, tracesPerQuery, agentVersion) {
123
125
  const { graphRef = getEnvVar('APOLLO_GRAPH_REF'), apiKey = getEnvVar('APOLLO_KEY'), endpoint = DEFAULT_REPORTING_ENDPOINT, } = options;
124
- try {
125
- const body = usage_reporting_protobuf_1.Report.encode({
126
- header: {
127
- agentVersion,
128
- graphRef,
129
- executableSchemaId: schemaId,
130
- },
131
- operationCount: 1,
132
- tracesPerQuery,
133
- }).finish();
134
- const response = await fetch(endpoint, {
135
- method: 'POST',
136
- headers: {
137
- 'content-type': 'application/protobuf',
138
- // The presence of the api key is already checked at Yoga initialization time
139
- 'x-api-key': apiKey,
140
- accept: 'application/json',
141
- },
142
- body,
143
- });
144
- const responseText = await response.text();
126
+ const body = usage_reporting_protobuf_1.Report.encode({
127
+ header: {
128
+ agentVersion,
129
+ graphRef,
130
+ executableSchemaId: schemaId,
131
+ },
132
+ operationCount: 1,
133
+ tracesPerQuery,
134
+ }).finish();
135
+ return (0, promise_helpers_1.handleMaybePromise)(() => fetch(endpoint, {
136
+ method: 'POST',
137
+ headers: {
138
+ 'content-type': 'application/protobuf',
139
+ // The presence of the api key is already checked at Yoga initialization time
140
+ 'x-api-key': apiKey,
141
+ accept: 'application/json',
142
+ },
143
+ body,
144
+ }), response => (0, promise_helpers_1.handleMaybePromise)(() => response.text(), responseText => {
145
145
  if (response.ok) {
146
146
  logger.debug('Traces sent:', responseText);
147
147
  }
148
148
  else {
149
149
  logger.error('Failed to send trace:', responseText);
150
150
  }
151
- }
152
- catch (err) {
151
+ }), err => {
153
152
  logger.error('Failed to send trace:', err);
154
- }
153
+ });
155
154
  }
156
155
  function isDocumentNode(data) {
157
156
  const isObject = (data) => !!data && typeof data === 'object';
package/esm/index.js CHANGED
@@ -2,6 +2,7 @@ import { getOperationAST, Kind, printSchema, stripIgnoredCharacters } from 'grap
2
2
  import { isAsyncIterable, } from 'graphql-yoga';
3
3
  import { Report } from '@apollo/usage-reporting-protobuf';
4
4
  import { useApolloInstrumentation, } from '@graphql-yoga/plugin-apollo-inline-trace';
5
+ import { handleMaybePromise } from '@whatwg-node/promise-helpers';
5
6
  function getEnvVar(name, defaultValue) {
6
7
  return globalThis.process?.env?.[name] || defaultValue || undefined;
7
8
  }
@@ -38,7 +39,7 @@ export function useApolloUsageReport(options = {}) {
38
39
  },
39
40
  onSchemaChange({ schema }) {
40
41
  if (schema) {
41
- schemaIdSet$ = hashSHA256(printSchema(schema), yoga.fetchAPI).then(id => {
42
+ schemaIdSet$ = handleMaybePromise(() => hashSHA256(printSchema(schema), yoga.fetchAPI), id => {
42
43
  schemaId = id;
43
44
  schemaIdSet$ = undefined;
44
45
  });
@@ -104,50 +105,48 @@ export function useApolloUsageReport(options = {}) {
104
105
  },
105
106
  };
106
107
  }
107
- export async function hashSHA256(str, api = globalThis) {
108
- const { crypto, TextEncoder } = api;
109
- const textEncoder = new TextEncoder();
110
- const utf8 = textEncoder.encode(str);
111
- const hashBuffer = await crypto.subtle.digest('SHA-256', utf8);
112
- let hashHex = '';
113
- for (const bytes of new Uint8Array(hashBuffer)) {
114
- hashHex += bytes.toString(16).padStart(2, '0');
115
- }
116
- return hashHex;
108
+ export function hashSHA256(text, api = globalThis) {
109
+ const inputUint8Array = new api.TextEncoder().encode(text);
110
+ return handleMaybePromise(() => api.crypto.subtle.digest({ name: 'SHA-256' }, inputUint8Array), arrayBuf => {
111
+ const outputUint8Array = new Uint8Array(arrayBuf);
112
+ let hash = '';
113
+ for (const byte of outputUint8Array) {
114
+ const hex = byte.toString(16);
115
+ hash += '00'.slice(0, Math.max(0, 2 - hex.length)) + hex;
116
+ }
117
+ return hash;
118
+ });
117
119
  }
118
- async function sendTrace(options, logger, fetch, schemaId, tracesPerQuery, agentVersion) {
120
+ function sendTrace(options, logger, fetch, schemaId, tracesPerQuery, agentVersion) {
119
121
  const { graphRef = getEnvVar('APOLLO_GRAPH_REF'), apiKey = getEnvVar('APOLLO_KEY'), endpoint = DEFAULT_REPORTING_ENDPOINT, } = options;
120
- try {
121
- const body = Report.encode({
122
- header: {
123
- agentVersion,
124
- graphRef,
125
- executableSchemaId: schemaId,
126
- },
127
- operationCount: 1,
128
- tracesPerQuery,
129
- }).finish();
130
- const response = await fetch(endpoint, {
131
- method: 'POST',
132
- headers: {
133
- 'content-type': 'application/protobuf',
134
- // The presence of the api key is already checked at Yoga initialization time
135
- 'x-api-key': apiKey,
136
- accept: 'application/json',
137
- },
138
- body,
139
- });
140
- const responseText = await response.text();
122
+ const body = Report.encode({
123
+ header: {
124
+ agentVersion,
125
+ graphRef,
126
+ executableSchemaId: schemaId,
127
+ },
128
+ operationCount: 1,
129
+ tracesPerQuery,
130
+ }).finish();
131
+ return handleMaybePromise(() => fetch(endpoint, {
132
+ method: 'POST',
133
+ headers: {
134
+ 'content-type': 'application/protobuf',
135
+ // The presence of the api key is already checked at Yoga initialization time
136
+ 'x-api-key': apiKey,
137
+ accept: 'application/json',
138
+ },
139
+ body,
140
+ }), response => handleMaybePromise(() => response.text(), responseText => {
141
141
  if (response.ok) {
142
142
  logger.debug('Traces sent:', responseText);
143
143
  }
144
144
  else {
145
145
  logger.error('Failed to send trace:', responseText);
146
146
  }
147
- }
148
- catch (err) {
147
+ }), err => {
149
148
  logger.error('Failed to send trace:', err);
150
- }
149
+ });
151
150
  }
152
151
  function isDocumentNode(data) {
153
152
  const isObject = (data) => !!data && typeof data === 'object';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@graphql-yoga/plugin-apollo-usage-report",
3
- "version": "0.8.0",
3
+ "version": "0.8.1",
4
4
  "description": "Apollo's GraphOS usage report plugin for GraphQL Yoga.",
5
5
  "peerDependencies": {
6
6
  "graphql": "^15.2.0 || ^16.0.0",
@@ -8,6 +8,7 @@
8
8
  },
9
9
  "dependencies": {
10
10
  "@apollo/usage-reporting-protobuf": "^4.1.1",
11
+ "@whatwg-node/promise-helpers": "^1.2.4",
11
12
  "tslib": "^2.8.1",
12
13
  "@graphql-yoga/plugin-apollo-inline-trace": "^3.13.0"
13
14
  },
@@ -1,4 +1,4 @@
1
- import { type Maybe, type Plugin, type YogaInitialContext } from 'graphql-yoga';
1
+ import { type Maybe, type Plugin, type PromiseOrValue, type YogaInitialContext } from 'graphql-yoga';
2
2
  import { ApolloInlineGraphqlTraceContext, ApolloInlineRequestTraceContext, ApolloInlineTracePluginOptions } from '@graphql-yoga/plugin-apollo-inline-trace';
3
3
  type ApolloUsageReportOptions = ApolloInlineTracePluginOptions & {
4
4
  /**
@@ -47,8 +47,8 @@ export interface ApolloUsageReportGraphqlContext extends ApolloInlineGraphqlTrac
47
47
  }
48
48
  type StringFromRequestFn = (req: Request) => Maybe<string>;
49
49
  export declare function useApolloUsageReport(options?: ApolloUsageReportOptions): Plugin;
50
- export declare function hashSHA256(str: string, api?: {
50
+ export declare function hashSHA256(text: string, api?: {
51
51
  crypto: Crypto;
52
52
  TextEncoder: (typeof globalThis)['TextEncoder'];
53
- }): Promise<string>;
53
+ }): PromiseOrValue<string>;
54
54
  export {};
@@ -1,4 +1,4 @@
1
- import { type Maybe, type Plugin, type YogaInitialContext } from 'graphql-yoga';
1
+ import { type Maybe, type Plugin, type PromiseOrValue, type YogaInitialContext } from 'graphql-yoga';
2
2
  import { ApolloInlineGraphqlTraceContext, ApolloInlineRequestTraceContext, ApolloInlineTracePluginOptions } from '@graphql-yoga/plugin-apollo-inline-trace';
3
3
  type ApolloUsageReportOptions = ApolloInlineTracePluginOptions & {
4
4
  /**
@@ -47,8 +47,8 @@ export interface ApolloUsageReportGraphqlContext extends ApolloInlineGraphqlTrac
47
47
  }
48
48
  type StringFromRequestFn = (req: Request) => Maybe<string>;
49
49
  export declare function useApolloUsageReport(options?: ApolloUsageReportOptions): Plugin;
50
- export declare function hashSHA256(str: string, api?: {
50
+ export declare function hashSHA256(text: string, api?: {
51
51
  crypto: Crypto;
52
52
  TextEncoder: (typeof globalThis)['TextEncoder'];
53
- }): Promise<string>;
53
+ }): PromiseOrValue<string>;
54
54
  export {};