graphql-http 1.21.0 → 1.22.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/lib/use/http.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  /// <reference types="node" />
2
2
  import type { IncomingMessage, ServerResponse } from 'http';
3
3
  import { HandlerOptions as RawHandlerOptions, OperationContext } from '../handler';
4
+ import { RequestParams } from '../common';
4
5
  /**
5
6
  * The context in the request for the handler.
6
7
  *
@@ -9,6 +10,49 @@ import { HandlerOptions as RawHandlerOptions, OperationContext } from '../handle
9
10
  export interface RequestContext {
10
11
  res: ServerResponse;
11
12
  }
13
+ /**
14
+ * The GraphQL over HTTP spec compliant request parser for an incoming GraphQL request.
15
+ *
16
+ * If the HTTP request _is not_ a [well-formatted GraphQL over HTTP request](https://graphql.github.io/graphql-over-http/draft/#sec-Request), the function will respond
17
+ * on the `ServerResponse` argument and return `null`.
18
+ *
19
+ * If the HTTP request _is_ a [well-formatted GraphQL over HTTP request](https://graphql.github.io/graphql-over-http/draft/#sec-Request), but is invalid or malformed,
20
+ * the function will throw an error and it is up to the user to handle and respond as they see fit.
21
+ *
22
+ * ```js
23
+ * import http from 'http';
24
+ * import { parseRequestParams } from 'graphql-http/lib/use/http';
25
+ *
26
+ * const server = http.createServer(async (req, res) => {
27
+ * if (req.url.startsWith('/graphql')) {
28
+ * try {
29
+ * const maybeParams = await parseRequestParams(req, res);
30
+ * if (!maybeParams) {
31
+ * // not a well-formatted GraphQL over HTTP request,
32
+ * // parser responded and there's nothing else to do
33
+ * return;
34
+ * }
35
+ *
36
+ * // well-formatted GraphQL over HTTP request,
37
+ * // with valid parameters
38
+ * res.writeHead(200).end(JSON.stringify(maybeParams, null, ' '));
39
+ * } catch (err) {
40
+ * // well-formatted GraphQL over HTTP request,
41
+ * // but with invalid parameters
42
+ * res.writeHead(400).end(err.message);
43
+ * }
44
+ * } else {
45
+ * res.writeHead(404).end();
46
+ * }
47
+ * });
48
+ *
49
+ * server.listen(4000);
50
+ * console.log('Listening to port 4000');
51
+ * ```
52
+ *
53
+ * @category Server/http
54
+ */
55
+ export declare function parseRequestParams(req: IncomingMessage, res: ServerResponse): Promise<RequestParams | null>;
12
56
  /**
13
57
  * Handler options when using the http adapter.
14
58
  *
package/lib/use/http.js CHANGED
@@ -1,7 +1,60 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createHandler = void 0;
3
+ exports.createHandler = exports.parseRequestParams = void 0;
4
4
  const handler_1 = require("../handler");
5
+ /**
6
+ * The GraphQL over HTTP spec compliant request parser for an incoming GraphQL request.
7
+ *
8
+ * If the HTTP request _is not_ a [well-formatted GraphQL over HTTP request](https://graphql.github.io/graphql-over-http/draft/#sec-Request), the function will respond
9
+ * on the `ServerResponse` argument and return `null`.
10
+ *
11
+ * If the HTTP request _is_ a [well-formatted GraphQL over HTTP request](https://graphql.github.io/graphql-over-http/draft/#sec-Request), but is invalid or malformed,
12
+ * the function will throw an error and it is up to the user to handle and respond as they see fit.
13
+ *
14
+ * ```js
15
+ * import http from 'http';
16
+ * import { parseRequestParams } from 'graphql-http/lib/use/http';
17
+ *
18
+ * const server = http.createServer(async (req, res) => {
19
+ * if (req.url.startsWith('/graphql')) {
20
+ * try {
21
+ * const maybeParams = await parseRequestParams(req, res);
22
+ * if (!maybeParams) {
23
+ * // not a well-formatted GraphQL over HTTP request,
24
+ * // parser responded and there's nothing else to do
25
+ * return;
26
+ * }
27
+ *
28
+ * // well-formatted GraphQL over HTTP request,
29
+ * // with valid parameters
30
+ * res.writeHead(200).end(JSON.stringify(maybeParams, null, ' '));
31
+ * } catch (err) {
32
+ * // well-formatted GraphQL over HTTP request,
33
+ * // but with invalid parameters
34
+ * res.writeHead(400).end(err.message);
35
+ * }
36
+ * } else {
37
+ * res.writeHead(404).end();
38
+ * }
39
+ * });
40
+ *
41
+ * server.listen(4000);
42
+ * console.log('Listening to port 4000');
43
+ * ```
44
+ *
45
+ * @category Server/http
46
+ */
47
+ async function parseRequestParams(req, res) {
48
+ const rawReq = toRequest(req, res);
49
+ const paramsOrRes = await (0, handler_1.parseRequestParams)(rawReq);
50
+ if (!('query' in paramsOrRes)) {
51
+ const [body, init] = paramsOrRes;
52
+ res.writeHead(init.status, init.statusText, init.headers).end(body);
53
+ return null;
54
+ }
55
+ return paramsOrRes;
56
+ }
57
+ exports.parseRequestParams = parseRequestParams;
5
58
  /**
6
59
  * Create a GraphQL over HTTP spec compliant request handler for
7
60
  * the Node environment http module.
@@ -29,18 +82,7 @@ function createHandler(options) {
29
82
  if (!req.method) {
30
83
  throw new Error('Missing request method');
31
84
  }
32
- const [body, init] = await handle({
33
- url: req.url,
34
- method: req.method,
35
- headers: req.headers,
36
- body: () => new Promise((resolve) => {
37
- let body = '';
38
- req.on('data', (chunk) => (body += chunk));
39
- req.on('end', () => resolve(body));
40
- }),
41
- raw: req,
42
- context: { res },
43
- });
85
+ const [body, init] = await handle(toRequest(req, res));
44
86
  res.writeHead(init.status, init.statusText, init.headers).end(body);
45
87
  }
46
88
  catch (err) {
@@ -53,3 +95,24 @@ function createHandler(options) {
53
95
  };
54
96
  }
55
97
  exports.createHandler = createHandler;
98
+ function toRequest(req, res) {
99
+ if (!req.url) {
100
+ throw new Error('Missing request URL');
101
+ }
102
+ if (!req.method) {
103
+ throw new Error('Missing request method');
104
+ }
105
+ return {
106
+ url: req.url,
107
+ method: req.method,
108
+ headers: req.headers,
109
+ body: () => new Promise((resolve) => {
110
+ let body = '';
111
+ req.setEncoding('utf-8');
112
+ req.on('data', (chunk) => (body += chunk));
113
+ req.on('end', () => resolve(body));
114
+ }),
115
+ raw: req,
116
+ context: { res },
117
+ };
118
+ }
package/lib/use/http.mjs CHANGED
@@ -1,4 +1,56 @@
1
- import { createHandler as createRawHandler, } from '../handler.mjs';
1
+ import { createHandler as createRawHandler, parseRequestParams as rawParseRequestParams, } from '../handler.mjs';
2
+ /**
3
+ * The GraphQL over HTTP spec compliant request parser for an incoming GraphQL request.
4
+ *
5
+ * If the HTTP request _is not_ a [well-formatted GraphQL over HTTP request](https://graphql.github.io/graphql-over-http/draft/#sec-Request), the function will respond
6
+ * on the `ServerResponse` argument and return `null`.
7
+ *
8
+ * If the HTTP request _is_ a [well-formatted GraphQL over HTTP request](https://graphql.github.io/graphql-over-http/draft/#sec-Request), but is invalid or malformed,
9
+ * the function will throw an error and it is up to the user to handle and respond as they see fit.
10
+ *
11
+ * ```js
12
+ * import http from 'http';
13
+ * import { parseRequestParams } from 'graphql-http/lib/use/http';
14
+ *
15
+ * const server = http.createServer(async (req, res) => {
16
+ * if (req.url.startsWith('/graphql')) {
17
+ * try {
18
+ * const maybeParams = await parseRequestParams(req, res);
19
+ * if (!maybeParams) {
20
+ * // not a well-formatted GraphQL over HTTP request,
21
+ * // parser responded and there's nothing else to do
22
+ * return;
23
+ * }
24
+ *
25
+ * // well-formatted GraphQL over HTTP request,
26
+ * // with valid parameters
27
+ * res.writeHead(200).end(JSON.stringify(maybeParams, null, ' '));
28
+ * } catch (err) {
29
+ * // well-formatted GraphQL over HTTP request,
30
+ * // but with invalid parameters
31
+ * res.writeHead(400).end(err.message);
32
+ * }
33
+ * } else {
34
+ * res.writeHead(404).end();
35
+ * }
36
+ * });
37
+ *
38
+ * server.listen(4000);
39
+ * console.log('Listening to port 4000');
40
+ * ```
41
+ *
42
+ * @category Server/http
43
+ */
44
+ export async function parseRequestParams(req, res) {
45
+ const rawReq = toRequest(req, res);
46
+ const paramsOrRes = await rawParseRequestParams(rawReq);
47
+ if (!('query' in paramsOrRes)) {
48
+ const [body, init] = paramsOrRes;
49
+ res.writeHead(init.status, init.statusText, init.headers).end(body);
50
+ return null;
51
+ }
52
+ return paramsOrRes;
53
+ }
2
54
  /**
3
55
  * Create a GraphQL over HTTP spec compliant request handler for
4
56
  * the Node environment http module.
@@ -26,18 +78,7 @@ export function createHandler(options) {
26
78
  if (!req.method) {
27
79
  throw new Error('Missing request method');
28
80
  }
29
- const [body, init] = await handle({
30
- url: req.url,
31
- method: req.method,
32
- headers: req.headers,
33
- body: () => new Promise((resolve) => {
34
- let body = '';
35
- req.on('data', (chunk) => (body += chunk));
36
- req.on('end', () => resolve(body));
37
- }),
38
- raw: req,
39
- context: { res },
40
- });
81
+ const [body, init] = await handle(toRequest(req, res));
41
82
  res.writeHead(init.status, init.statusText, init.headers).end(body);
42
83
  }
43
84
  catch (err) {
@@ -49,3 +90,24 @@ export function createHandler(options) {
49
90
  }
50
91
  };
51
92
  }
93
+ function toRequest(req, res) {
94
+ if (!req.url) {
95
+ throw new Error('Missing request URL');
96
+ }
97
+ if (!req.method) {
98
+ throw new Error('Missing request method');
99
+ }
100
+ return {
101
+ url: req.url,
102
+ method: req.method,
103
+ headers: req.headers,
104
+ body: () => new Promise((resolve) => {
105
+ let body = '';
106
+ req.setEncoding('utf-8');
107
+ req.on('data', (chunk) => (body += chunk));
108
+ req.on('end', () => resolve(body));
109
+ }),
110
+ raw: req,
111
+ context: { res },
112
+ };
113
+ }
@@ -1,6 +1,7 @@
1
1
  /// <reference types="node" />
2
2
  import type { Http2ServerRequest, Http2ServerResponse } from 'http2';
3
3
  import { HandlerOptions as RawHandlerOptions, OperationContext } from '../handler.mjs';
4
+ import { RequestParams } from '../common.mjs';
4
5
  /**
5
6
  * The context in the request for the handler.
6
7
  *
@@ -9,6 +10,61 @@ import { HandlerOptions as RawHandlerOptions, OperationContext } from '../handle
9
10
  export interface RequestContext {
10
11
  res: Http2ServerResponse;
11
12
  }
13
+ /**
14
+ * The GraphQL over HTTP spec compliant request parser for an incoming GraphQL request.
15
+ *
16
+ * If the HTTP request _is not_ a [well-formatted GraphQL over HTTP request](https://graphql.github.io/graphql-over-http/draft/#sec-Request), the function will respond
17
+ * on the `Http2ServerResponse` argument and return `null`.
18
+ *
19
+ * If the HTTP request _is_ a [well-formatted GraphQL over HTTP request](https://graphql.github.io/graphql-over-http/draft/#sec-Request), but is invalid or malformed,
20
+ * the function will throw an error and it is up to the user to handle and respond as they see fit.
21
+ *
22
+ * ```shell
23
+ * $ openssl req -x509 -newkey rsa:2048 -nodes -sha256 -subj '/CN=localhost' \
24
+ * -keyout localhost-privkey.pem -out localhost-cert.pem
25
+ * ```
26
+ *
27
+ * ```js
28
+ * import fs from 'fs';
29
+ * import http2 from 'http2';
30
+ * import { parseRequestParams } from 'graphql-http/lib/use/http2';
31
+ *
32
+ * const server = http2.createSecureServer(
33
+ * {
34
+ * key: fs.readFileSync('localhost-privkey.pem'),
35
+ * cert: fs.readFileSync('localhost-cert.pem'),
36
+ * },
37
+ * async (req, res) => {
38
+ * if (req.url.startsWith('/graphql')) {
39
+ * try {
40
+ * const maybeParams = await parseRequestParams(req, res);
41
+ * if (!maybeParams) {
42
+ * // not a well-formatted GraphQL over HTTP request,
43
+ * // parser responded and there's nothing else to do
44
+ * return;
45
+ * }
46
+ *
47
+ * // well-formatted GraphQL over HTTP request,
48
+ * // with valid parameters
49
+ * res.writeHead(200).end(JSON.stringify(maybeParams, null, ' '));
50
+ * } catch (err) {
51
+ * // well-formatted GraphQL over HTTP request,
52
+ * // but with invalid parameters
53
+ * res.writeHead(400).end(err.message);
54
+ * }
55
+ * } else {
56
+ * res.writeHead(404).end();
57
+ * }
58
+ * },
59
+ * );
60
+ *
61
+ * server.listen(4000);
62
+ * console.log('Listening to port 4000');
63
+ * ```
64
+ *
65
+ * @category Server/http2
66
+ */
67
+ export declare function parseRequestParams(req: Http2ServerRequest, res: Http2ServerResponse): Promise<RequestParams | null>;
12
68
  /**
13
69
  * Handler options when using the http adapter.
14
70
  *
@@ -1,6 +1,7 @@
1
1
  /// <reference types="node" />
2
2
  import type { Http2ServerRequest, Http2ServerResponse } from 'http2';
3
3
  import { HandlerOptions as RawHandlerOptions, OperationContext } from '../handler';
4
+ import { RequestParams } from '../common';
4
5
  /**
5
6
  * The context in the request for the handler.
6
7
  *
@@ -9,6 +10,61 @@ import { HandlerOptions as RawHandlerOptions, OperationContext } from '../handle
9
10
  export interface RequestContext {
10
11
  res: Http2ServerResponse;
11
12
  }
13
+ /**
14
+ * The GraphQL over HTTP spec compliant request parser for an incoming GraphQL request.
15
+ *
16
+ * If the HTTP request _is not_ a [well-formatted GraphQL over HTTP request](https://graphql.github.io/graphql-over-http/draft/#sec-Request), the function will respond
17
+ * on the `Http2ServerResponse` argument and return `null`.
18
+ *
19
+ * If the HTTP request _is_ a [well-formatted GraphQL over HTTP request](https://graphql.github.io/graphql-over-http/draft/#sec-Request), but is invalid or malformed,
20
+ * the function will throw an error and it is up to the user to handle and respond as they see fit.
21
+ *
22
+ * ```shell
23
+ * $ openssl req -x509 -newkey rsa:2048 -nodes -sha256 -subj '/CN=localhost' \
24
+ * -keyout localhost-privkey.pem -out localhost-cert.pem
25
+ * ```
26
+ *
27
+ * ```js
28
+ * import fs from 'fs';
29
+ * import http2 from 'http2';
30
+ * import { parseRequestParams } from 'graphql-http/lib/use/http2';
31
+ *
32
+ * const server = http2.createSecureServer(
33
+ * {
34
+ * key: fs.readFileSync('localhost-privkey.pem'),
35
+ * cert: fs.readFileSync('localhost-cert.pem'),
36
+ * },
37
+ * async (req, res) => {
38
+ * if (req.url.startsWith('/graphql')) {
39
+ * try {
40
+ * const maybeParams = await parseRequestParams(req, res);
41
+ * if (!maybeParams) {
42
+ * // not a well-formatted GraphQL over HTTP request,
43
+ * // parser responded and there's nothing else to do
44
+ * return;
45
+ * }
46
+ *
47
+ * // well-formatted GraphQL over HTTP request,
48
+ * // with valid parameters
49
+ * res.writeHead(200).end(JSON.stringify(maybeParams, null, ' '));
50
+ * } catch (err) {
51
+ * // well-formatted GraphQL over HTTP request,
52
+ * // but with invalid parameters
53
+ * res.writeHead(400).end(err.message);
54
+ * }
55
+ * } else {
56
+ * res.writeHead(404).end();
57
+ * }
58
+ * },
59
+ * );
60
+ *
61
+ * server.listen(4000);
62
+ * console.log('Listening to port 4000');
63
+ * ```
64
+ *
65
+ * @category Server/http2
66
+ */
67
+ export declare function parseRequestParams(req: Http2ServerRequest, res: Http2ServerResponse): Promise<RequestParams | null>;
12
68
  /**
13
69
  * Handler options when using the http adapter.
14
70
  *
package/lib/use/http2.js CHANGED
@@ -1,7 +1,78 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createHandler = void 0;
3
+ exports.createHandler = exports.parseRequestParams = void 0;
4
4
  const handler_1 = require("../handler");
5
+ /**
6
+ * The GraphQL over HTTP spec compliant request parser for an incoming GraphQL request.
7
+ *
8
+ * If the HTTP request _is not_ a [well-formatted GraphQL over HTTP request](https://graphql.github.io/graphql-over-http/draft/#sec-Request), the function will respond
9
+ * on the `Http2ServerResponse` argument and return `null`.
10
+ *
11
+ * If the HTTP request _is_ a [well-formatted GraphQL over HTTP request](https://graphql.github.io/graphql-over-http/draft/#sec-Request), but is invalid or malformed,
12
+ * the function will throw an error and it is up to the user to handle and respond as they see fit.
13
+ *
14
+ * ```shell
15
+ * $ openssl req -x509 -newkey rsa:2048 -nodes -sha256 -subj '/CN=localhost' \
16
+ * -keyout localhost-privkey.pem -out localhost-cert.pem
17
+ * ```
18
+ *
19
+ * ```js
20
+ * import fs from 'fs';
21
+ * import http2 from 'http2';
22
+ * import { parseRequestParams } from 'graphql-http/lib/use/http2';
23
+ *
24
+ * const server = http2.createSecureServer(
25
+ * {
26
+ * key: fs.readFileSync('localhost-privkey.pem'),
27
+ * cert: fs.readFileSync('localhost-cert.pem'),
28
+ * },
29
+ * async (req, res) => {
30
+ * if (req.url.startsWith('/graphql')) {
31
+ * try {
32
+ * const maybeParams = await parseRequestParams(req, res);
33
+ * if (!maybeParams) {
34
+ * // not a well-formatted GraphQL over HTTP request,
35
+ * // parser responded and there's nothing else to do
36
+ * return;
37
+ * }
38
+ *
39
+ * // well-formatted GraphQL over HTTP request,
40
+ * // with valid parameters
41
+ * res.writeHead(200).end(JSON.stringify(maybeParams, null, ' '));
42
+ * } catch (err) {
43
+ * // well-formatted GraphQL over HTTP request,
44
+ * // but with invalid parameters
45
+ * res.writeHead(400).end(err.message);
46
+ * }
47
+ * } else {
48
+ * res.writeHead(404).end();
49
+ * }
50
+ * },
51
+ * );
52
+ *
53
+ * server.listen(4000);
54
+ * console.log('Listening to port 4000');
55
+ * ```
56
+ *
57
+ * @category Server/http2
58
+ */
59
+ async function parseRequestParams(req, res) {
60
+ const rawReq = toRequest(req, res);
61
+ const paramsOrRes = await (0, handler_1.parseRequestParams)(rawReq);
62
+ if (!('query' in paramsOrRes)) {
63
+ const [body, init] = paramsOrRes;
64
+ res.writeHead(init.status, init.statusText, init.headers);
65
+ if (body) {
66
+ res.end(body);
67
+ }
68
+ else {
69
+ res.end();
70
+ }
71
+ return null;
72
+ }
73
+ return paramsOrRes;
74
+ }
75
+ exports.parseRequestParams = parseRequestParams;
5
76
  /**
6
77
  * Create a GraphQL over HTTP spec compliant request handler for
7
78
  * the Node environment http2 module.
@@ -41,18 +112,7 @@ function createHandler(options) {
41
112
  if (!req.method) {
42
113
  throw new Error('Missing request method');
43
114
  }
44
- const [body, init] = await handle({
45
- url: req.url,
46
- method: req.method,
47
- headers: req.headers,
48
- body: () => new Promise((resolve) => {
49
- let body = '';
50
- req.on('data', (chunk) => (body += chunk));
51
- req.on('end', () => resolve(body));
52
- }),
53
- raw: req,
54
- context: { res },
55
- });
115
+ const [body, init] = await handle(toRequest(req, res));
56
116
  res.writeHead(init.status, init.statusText, init.headers);
57
117
  if (body) {
58
118
  res.end(body);
@@ -71,3 +131,24 @@ function createHandler(options) {
71
131
  };
72
132
  }
73
133
  exports.createHandler = createHandler;
134
+ function toRequest(req, res) {
135
+ if (!req.url) {
136
+ throw new Error('Missing request URL');
137
+ }
138
+ if (!req.method) {
139
+ throw new Error('Missing request method');
140
+ }
141
+ return {
142
+ url: req.url,
143
+ method: req.method,
144
+ headers: req.headers,
145
+ body: () => new Promise((resolve) => {
146
+ let body = '';
147
+ req.setEncoding('utf-8');
148
+ req.on('data', (chunk) => (body += chunk));
149
+ req.on('end', () => resolve(body));
150
+ }),
151
+ raw: req,
152
+ context: { res },
153
+ };
154
+ }
package/lib/use/http2.mjs CHANGED
@@ -1,4 +1,74 @@
1
- import { createHandler as createRawHandler, } from '../handler.mjs';
1
+ import { createHandler as createRawHandler, parseRequestParams as rawParseRequestParams, } from '../handler.mjs';
2
+ /**
3
+ * The GraphQL over HTTP spec compliant request parser for an incoming GraphQL request.
4
+ *
5
+ * If the HTTP request _is not_ a [well-formatted GraphQL over HTTP request](https://graphql.github.io/graphql-over-http/draft/#sec-Request), the function will respond
6
+ * on the `Http2ServerResponse` argument and return `null`.
7
+ *
8
+ * If the HTTP request _is_ a [well-formatted GraphQL over HTTP request](https://graphql.github.io/graphql-over-http/draft/#sec-Request), but is invalid or malformed,
9
+ * the function will throw an error and it is up to the user to handle and respond as they see fit.
10
+ *
11
+ * ```shell
12
+ * $ openssl req -x509 -newkey rsa:2048 -nodes -sha256 -subj '/CN=localhost' \
13
+ * -keyout localhost-privkey.pem -out localhost-cert.pem
14
+ * ```
15
+ *
16
+ * ```js
17
+ * import fs from 'fs';
18
+ * import http2 from 'http2';
19
+ * import { parseRequestParams } from 'graphql-http/lib/use/http2';
20
+ *
21
+ * const server = http2.createSecureServer(
22
+ * {
23
+ * key: fs.readFileSync('localhost-privkey.pem'),
24
+ * cert: fs.readFileSync('localhost-cert.pem'),
25
+ * },
26
+ * async (req, res) => {
27
+ * if (req.url.startsWith('/graphql')) {
28
+ * try {
29
+ * const maybeParams = await parseRequestParams(req, res);
30
+ * if (!maybeParams) {
31
+ * // not a well-formatted GraphQL over HTTP request,
32
+ * // parser responded and there's nothing else to do
33
+ * return;
34
+ * }
35
+ *
36
+ * // well-formatted GraphQL over HTTP request,
37
+ * // with valid parameters
38
+ * res.writeHead(200).end(JSON.stringify(maybeParams, null, ' '));
39
+ * } catch (err) {
40
+ * // well-formatted GraphQL over HTTP request,
41
+ * // but with invalid parameters
42
+ * res.writeHead(400).end(err.message);
43
+ * }
44
+ * } else {
45
+ * res.writeHead(404).end();
46
+ * }
47
+ * },
48
+ * );
49
+ *
50
+ * server.listen(4000);
51
+ * console.log('Listening to port 4000');
52
+ * ```
53
+ *
54
+ * @category Server/http2
55
+ */
56
+ export async function parseRequestParams(req, res) {
57
+ const rawReq = toRequest(req, res);
58
+ const paramsOrRes = await rawParseRequestParams(rawReq);
59
+ if (!('query' in paramsOrRes)) {
60
+ const [body, init] = paramsOrRes;
61
+ res.writeHead(init.status, init.statusText, init.headers);
62
+ if (body) {
63
+ res.end(body);
64
+ }
65
+ else {
66
+ res.end();
67
+ }
68
+ return null;
69
+ }
70
+ return paramsOrRes;
71
+ }
2
72
  /**
3
73
  * Create a GraphQL over HTTP spec compliant request handler for
4
74
  * the Node environment http2 module.
@@ -38,18 +108,7 @@ export function createHandler(options) {
38
108
  if (!req.method) {
39
109
  throw new Error('Missing request method');
40
110
  }
41
- const [body, init] = await handle({
42
- url: req.url,
43
- method: req.method,
44
- headers: req.headers,
45
- body: () => new Promise((resolve) => {
46
- let body = '';
47
- req.on('data', (chunk) => (body += chunk));
48
- req.on('end', () => resolve(body));
49
- }),
50
- raw: req,
51
- context: { res },
52
- });
111
+ const [body, init] = await handle(toRequest(req, res));
53
112
  res.writeHead(init.status, init.statusText, init.headers);
54
113
  if (body) {
55
114
  res.end(body);
@@ -67,3 +126,24 @@ export function createHandler(options) {
67
126
  }
68
127
  };
69
128
  }
129
+ function toRequest(req, res) {
130
+ if (!req.url) {
131
+ throw new Error('Missing request URL');
132
+ }
133
+ if (!req.method) {
134
+ throw new Error('Missing request method');
135
+ }
136
+ return {
137
+ url: req.url,
138
+ method: req.method,
139
+ headers: req.headers,
140
+ body: () => new Promise((resolve) => {
141
+ let body = '';
142
+ req.setEncoding('utf-8');
143
+ req.on('data', (chunk) => (body += chunk));
144
+ req.on('end', () => resolve(body));
145
+ }),
146
+ raw: req,
147
+ context: { res },
148
+ };
149
+ }