Skip to content

Commit

Permalink
refactor: use Promise constructor to wrap builtin gzip (#7229)
Browse files Browse the repository at this point in the history
support for `Cloudflare Workers`

ref: #6034

Co-authored-by: David Glasser <glasser@davidglasser.net>
  • Loading branch information
dnalborczyk and glasser committed Dec 7, 2022
1 parent 6b4674d commit d057e2f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/good-schools-glow.md
@@ -0,0 +1,5 @@
---
'@apollo/server': patch
---

Improve compatibility with Cloudflare workers by avoiding the use of the Node `util` package. This change is intended to be a no-op.
9 changes: 5 additions & 4 deletions packages/server/src/plugin/usageReporting/plugin.ts
Expand Up @@ -11,7 +11,6 @@ import type LRUCache from 'lru-cache';
import { AbortController } from 'node-abort-controller';
import fetch from 'node-fetch';
import os from 'os';
import { promisify } from 'util';
import { gzip } from 'zlib';
import type {
ApolloServerPlugin,
Expand Down Expand Up @@ -41,8 +40,6 @@ import { computeCoreSchemaHash } from '../../utils/computeCoreSchemaHash.js';
import type { HeaderMap } from '../../utils/HeaderMap.js';
import { schemaIsSubgraph } from '../schemaIsSubgraph.js';

const gzipPromise = promisify(gzip);

const reportHeaderDefaults = {
hostname: os.hostname(),
agentVersion: `@apollo/server@${packageVersion}`,
Expand Down Expand Up @@ -305,7 +302,11 @@ export function ApolloServerPluginUsageReporting<TContext extends BaseContext>(
);
}

const compressed = await gzipPromise(message);
const compressed = await new Promise<Buffer>((resolve, reject) => {
gzip(message!, (error, result) => {
error ? reject(error) : resolve(result);
});
});
// Let the uncompressed message be garbage collected (helpful if the
// HTTP request is slow).
message = null;
Expand Down

0 comments on commit d057e2f

Please sign in to comment.