graphql-http 1.4.0 → 1.6.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/README.md CHANGED
@@ -68,11 +68,12 @@ const server = http.createServer(async (req, res) => {
68
68
  url: req.url,
69
69
  method: req.method,
70
70
  headers: req.headers,
71
- body: await new Promise((resolve) => {
72
- let body = '';
73
- req.on('data', (chunk) => (body += chunk));
74
- req.on('end', () => resolve(body));
75
- }),
71
+ body: () =>
72
+ new Promise((resolve) => {
73
+ let body = '';
74
+ req.on('data', (chunk) => (body += chunk));
75
+ req.on('end', () => resolve(body));
76
+ }),
76
77
  raw: req,
77
78
  });
78
79
  res.writeHead(init.status, init.statusText, init.headers).end(body);
@@ -119,11 +120,12 @@ const server = http2.createSecureServer(
119
120
  url: req.url,
120
121
  method: req.method,
121
122
  headers: req.headers,
122
- body: await new Promise((resolve) => {
123
- let body = '';
124
- req.on('data', (chunk) => (body += chunk));
125
- req.on('end', () => resolve(body));
126
- }),
123
+ body: () =>
124
+ new Promise((resolve) => {
125
+ let body = '';
126
+ req.on('data', (chunk) => (body += chunk));
127
+ req.on('end', () => resolve(body));
128
+ }),
127
129
  raw: req,
128
130
  });
129
131
  res.writeHead(init.status, init.statusText, init.headers).end(body);
@@ -155,11 +157,12 @@ app.use('/graphql', async (req, res) => {
155
157
  url: req.url,
156
158
  method: req.method,
157
159
  headers: req.headers,
158
- body: await new Promise((resolve) => {
159
- let body = '';
160
- req.on('data', (chunk) => (body += chunk));
161
- req.on('end', () => resolve(body));
162
- }),
160
+ body: () =>
161
+ new Promise((resolve) => {
162
+ let body = '';
163
+ req.on('data', (chunk) => (body += chunk));
164
+ req.on('end', () => resolve(body));
165
+ }),
163
166
  raw: req,
164
167
  });
165
168
  res.writeHead(init.status, init.statusText, init.headers).end(body);
@@ -227,7 +230,7 @@ await serve(
227
230
  url: req.url,
228
231
  method: req.method,
229
232
  headers,
230
- body: await req.text(),
233
+ body: () => req.text(),
231
234
  raw: req,
232
235
  });
233
236
  return new Response(body, init);
@@ -686,11 +689,12 @@ const server = http.createServer(async (req, res) => {
686
689
  url: req.url,
687
690
  method: req.method,
688
691
  headers: req.headers,
689
- body: await new Promise((resolve) => {
690
- let body = '';
691
- req.on('data', (chunk) => (body += chunk));
692
- req.on('end', () => resolve(body));
693
- }),
692
+ body: () =>
693
+ new Promise((resolve) => {
694
+ let body = '';
695
+ req.on('data', (chunk) => (body += chunk));
696
+ req.on('end', () => resolve(body));
697
+ }),
694
698
  raw: req,
695
699
  });
696
700
  res.writeHead(init.status, init.statusText, init.headers).end(body);
@@ -705,10 +709,40 @@ console.log('Listening to port 4000');
705
709
 
706
710
  </details>
707
711
 
712
+ <details id="audit-jest">
713
+ <summary><a href="#audit-jest">🔗</a> Audit for servers usage in <a href="https://jestjs.io">Jest</a> environment</summary>
714
+
715
+ ```js
716
+ import { fetch } from '@whatwg-node/fetch';
717
+ import { serverAudits } from 'graphql-http';
718
+
719
+ for (const audit of serverAudits({
720
+ url: 'http://localhost:4000/graphql',
721
+ fetchFn: fetch,
722
+ })) {
723
+ test(audit.name, async () => {
724
+ const result = await audit.fn();
725
+ if (result.status === 'error') {
726
+ throw result.reason;
727
+ }
728
+ if (result.status === 'warn') {
729
+ console.warn(result.reason); // or throw if you want full compliance (warnings are not requirements)
730
+ }
731
+ // result.status === 'ok'
732
+ });
733
+ }
734
+ ```
735
+
736
+ </details>
737
+
708
738
  ## [Documentation](docs/)
709
739
 
710
740
  Check the [docs folder](docs/) out for [TypeDoc](https://typedoc.org) generated documentation.
711
741
 
742
+ ## [Audits](implementations/)
743
+
744
+ Inspect audits of other implementations in the [implementations folder](implementations/). Adding your implementation is very welcome!
745
+
712
746
  ## [Want to help?](CONTRIBUTING.md)
713
747
 
714
748
  File a bug, contribute with code, or improve documentation? Read up on our guidelines for [contributing](CONTRIBUTING.md) and drive development with `yarn test --watch` away!
@@ -0,0 +1,58 @@
1
+ /**
2
+ *
3
+ * audit/common
4
+ *
5
+ */
6
+ /**
7
+ * Audit requirement levels as per [RFC2119](https://www.rfc-editor.org/rfc/rfc2119).
8
+ *
9
+ * @category Audits
10
+ */
11
+ export declare type AuditRequirement = 'MUST' | 'SHOULD' | 'MAY';
12
+ /**
13
+ * Audit name starting with the audit requirement level.
14
+ *
15
+ * @category Audits
16
+ */
17
+ export declare type AuditName = `${AuditRequirement} ${string}`;
18
+ /**
19
+ * Actual audit test returning an result.
20
+ *
21
+ * The test function will throw only if the error is fatal.
22
+ *
23
+ * @category Audits
24
+ */
25
+ export interface Audit {
26
+ name: AuditName;
27
+ fn: () => Promise<AuditResult>;
28
+ }
29
+ /**
30
+ * Indicates that the audit was successful.
31
+ *
32
+ * @category Audits
33
+ */
34
+ export interface AuditOk {
35
+ name: AuditName;
36
+ status: 'ok';
37
+ }
38
+ /**
39
+ * Indicates that the audit failed.
40
+ *
41
+ * If the status is `warn`, the audit is not a requirement but rather a recommendation.
42
+ *
43
+ * On the other hand, if the status is `error`, the audit is a requirement and the source
44
+ * is therefore not compliant.
45
+ *
46
+ * @category Audits
47
+ */
48
+ export interface AuditFail {
49
+ name: AuditName;
50
+ status: 'warn' | 'error';
51
+ reason: string;
52
+ }
53
+ /**
54
+ * Result of the performed audit. See `AuditOk` and `AuditFail` for more information.
55
+ *
56
+ * @category Audits
57
+ */
58
+ export declare type AuditResult = AuditOk | AuditFail;
@@ -0,0 +1,58 @@
1
+ /**
2
+ *
3
+ * audit/common
4
+ *
5
+ */
6
+ /**
7
+ * Audit requirement levels as per [RFC2119](https://www.rfc-editor.org/rfc/rfc2119).
8
+ *
9
+ * @category Audits
10
+ */
11
+ export declare type AuditRequirement = 'MUST' | 'SHOULD' | 'MAY';
12
+ /**
13
+ * Audit name starting with the audit requirement level.
14
+ *
15
+ * @category Audits
16
+ */
17
+ export declare type AuditName = `${AuditRequirement} ${string}`;
18
+ /**
19
+ * Actual audit test returning an result.
20
+ *
21
+ * The test function will throw only if the error is fatal.
22
+ *
23
+ * @category Audits
24
+ */
25
+ export interface Audit {
26
+ name: AuditName;
27
+ fn: () => Promise<AuditResult>;
28
+ }
29
+ /**
30
+ * Indicates that the audit was successful.
31
+ *
32
+ * @category Audits
33
+ */
34
+ export interface AuditOk {
35
+ name: AuditName;
36
+ status: 'ok';
37
+ }
38
+ /**
39
+ * Indicates that the audit failed.
40
+ *
41
+ * If the status is `warn`, the audit is not a requirement but rather a recommendation.
42
+ *
43
+ * On the other hand, if the status is `error`, the audit is a requirement and the source
44
+ * is therefore not compliant.
45
+ *
46
+ * @category Audits
47
+ */
48
+ export interface AuditFail {
49
+ name: AuditName;
50
+ status: 'warn' | 'error';
51
+ reason: string;
52
+ }
53
+ /**
54
+ * Result of the performed audit. See `AuditOk` and `AuditFail` for more information.
55
+ *
56
+ * @category Audits
57
+ */
58
+ export declare type AuditResult = AuditOk | AuditFail;
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ /**
3
+ *
4
+ * audit/common
5
+ *
6
+ */
7
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,6 @@
1
+ /**
2
+ *
3
+ * audit/common
4
+ *
5
+ */
6
+ export {};
@@ -0,0 +1,2 @@
1
+ export * from './common';
2
+ export * from './server';
@@ -0,0 +1,2 @@
1
+ export * from './common';
2
+ export * from './server';
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./common"), exports);
18
+ __exportStar(require("./server"), exports);
@@ -0,0 +1,2 @@
1
+ export * from './common.mjs';
2
+ export * from './server.mjs';
@@ -0,0 +1,39 @@
1
+ /**
2
+ *
3
+ * audit/server
4
+ *
5
+ */
6
+ import { Audit, AuditResult } from './common';
7
+ /**
8
+ * Options for server audits required to check GraphQL over HTTP spec conformance.
9
+ *
10
+ * @category Audits
11
+ */
12
+ export interface ServerAuditOptions {
13
+ /**
14
+ * The URL of the GraphQL server for the audit.
15
+ */
16
+ url: string;
17
+ /**
18
+ * The Fetch function to use.
19
+ *
20
+ * For NodeJS environments consider using [`@whatwg-node/fetch`](https://github.com/ardatan/whatwg-node/tree/master/packages/fetch).
21
+ *
22
+ * @default global.fetch
23
+ */
24
+ fetchFn?: unknown;
25
+ }
26
+ /**
27
+ * List of server audits required to check GraphQL over HTTP spec conformance.
28
+ *
29
+ * @category Audits
30
+ */
31
+ export declare function serverAudits(opts: ServerAuditOptions): Audit[];
32
+ /**
33
+ * Performs the full list of server audits required for GraphQL over HTTP spec conformance.
34
+ *
35
+ * Please consult the `AuditResult` for more information.
36
+ *
37
+ * @category Audits
38
+ */
39
+ export declare function auditServer(opts: ServerAuditOptions): Promise<AuditResult[]>;
@@ -0,0 +1,39 @@
1
+ /**
2
+ *
3
+ * audit/server
4
+ *
5
+ */
6
+ import { Audit, AuditResult } from './common';
7
+ /**
8
+ * Options for server audits required to check GraphQL over HTTP spec conformance.
9
+ *
10
+ * @category Audits
11
+ */
12
+ export interface ServerAuditOptions {
13
+ /**
14
+ * The URL of the GraphQL server for the audit.
15
+ */
16
+ url: string;
17
+ /**
18
+ * The Fetch function to use.
19
+ *
20
+ * For NodeJS environments consider using [`@whatwg-node/fetch`](https://github.com/ardatan/whatwg-node/tree/master/packages/fetch).
21
+ *
22
+ * @default global.fetch
23
+ */
24
+ fetchFn?: unknown;
25
+ }
26
+ /**
27
+ * List of server audits required to check GraphQL over HTTP spec conformance.
28
+ *
29
+ * @category Audits
30
+ */
31
+ export declare function serverAudits(opts: ServerAuditOptions): Audit[];
32
+ /**
33
+ * Performs the full list of server audits required for GraphQL over HTTP spec conformance.
34
+ *
35
+ * Please consult the `AuditResult` for more information.
36
+ *
37
+ * @category Audits
38
+ */
39
+ export declare function auditServer(opts: ServerAuditOptions): Promise<AuditResult[]>;