graphql-http 1.3.0 → 1.6.0
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 +52 -26
- package/lib/audits/common.d.ts +58 -0
- package/lib/audits/common.js +7 -0
- package/lib/audits/common.mjs +6 -0
- package/lib/audits/index.d.ts +2 -0
- package/lib/audits/index.js +18 -0
- package/lib/audits/index.mjs +2 -0
- package/lib/audits/server.d.ts +39 -0
- package/lib/audits/server.js +440 -0
- package/lib/audits/server.mjs +435 -0
- package/lib/audits/utils.d.ts +27 -0
- package/lib/audits/utils.js +100 -0
- package/lib/audits/utils.mjs +81 -0
- package/lib/handler.d.ts +8 -2
- package/lib/handler.js +13 -8
- package/lib/handler.mjs +13 -8
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/index.mjs +1 -0
- package/lib/utils.d.ts +3 -1
- package/lib/utils.js +12 -1
- package/lib/utils.mjs +10 -0
- package/package.json +20 -17
- package/umd/graphql-http.min.js.gz +0 -0
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:
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
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:
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
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:
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
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);
|
|
@@ -190,11 +193,7 @@ fastify.all('/graphql', async (req, res) => {
|
|
|
190
193
|
url: req.url,
|
|
191
194
|
method: req.method,
|
|
192
195
|
headers: req.headers,
|
|
193
|
-
body:
|
|
194
|
-
let body = '';
|
|
195
|
-
req.on('data', (chunk) => (body += chunk));
|
|
196
|
-
req.on('end', () => resolve(body));
|
|
197
|
-
}),
|
|
196
|
+
body: req.body, // fastify reads the body for you
|
|
198
197
|
raw: req,
|
|
199
198
|
});
|
|
200
199
|
res.writeHead(init.status, init.statusText, init.headers).end(body);
|
|
@@ -231,7 +230,7 @@ await serve(
|
|
|
231
230
|
url: req.url,
|
|
232
231
|
method: req.method,
|
|
233
232
|
headers,
|
|
234
|
-
body:
|
|
233
|
+
body: () => req.text(),
|
|
235
234
|
raw: req,
|
|
236
235
|
});
|
|
237
236
|
return new Response(body, init);
|
|
@@ -690,11 +689,12 @@ const server = http.createServer(async (req, res) => {
|
|
|
690
689
|
url: req.url,
|
|
691
690
|
method: req.method,
|
|
692
691
|
headers: req.headers,
|
|
693
|
-
body:
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
692
|
+
body: () =>
|
|
693
|
+
new Promise((resolve) => {
|
|
694
|
+
let body = '';
|
|
695
|
+
req.on('data', (chunk) => (body += chunk));
|
|
696
|
+
req.on('end', () => resolve(body));
|
|
697
|
+
}),
|
|
698
698
|
raw: req,
|
|
699
699
|
});
|
|
700
700
|
res.writeHead(init.status, init.statusText, init.headers).end(body);
|
|
@@ -709,6 +709,32 @@ console.log('Listening to port 4000');
|
|
|
709
709
|
|
|
710
710
|
</details>
|
|
711
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
|
+
|
|
712
738
|
## [Documentation](docs/)
|
|
713
739
|
|
|
714
740
|
Check the [docs folder](docs/) out for [TypeDoc](https://typedoc.org) generated documentation.
|
|
@@ -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,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,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[]>;
|