graphql-http 1.17.1 → 1.19.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.
Files changed (47) hide show
  1. package/README.md +17 -1
  2. package/lib/audits/common.d.mts +1 -1
  3. package/lib/audits/common.d.ts +1 -1
  4. package/lib/audits/render.js +15 -0
  5. package/lib/audits/render.mjs +15 -0
  6. package/lib/audits/server.js +42 -172
  7. package/lib/audits/server.mjs +42 -172
  8. package/lib/audits/utils.js +6 -3
  9. package/lib/audits/utils.mjs +6 -3
  10. package/lib/handler.d.mts +20 -3
  11. package/lib/handler.d.ts +20 -3
  12. package/lib/handler.js +36 -14
  13. package/lib/handler.mjs +37 -15
  14. package/lib/use/express.js +1 -18
  15. package/lib/use/express.mjs +1 -18
  16. package/lib/use/fastify.js +1 -19
  17. package/lib/use/fastify.mjs +1 -19
  18. package/lib/use/fetch.d.mts +1 -1
  19. package/lib/use/fetch.d.ts +1 -1
  20. package/lib/use/fetch.js +2 -22
  21. package/lib/use/fetch.mjs +2 -22
  22. package/lib/use/http.d.mts +1 -1
  23. package/lib/use/http.d.ts +1 -1
  24. package/lib/use/http.js +2 -19
  25. package/lib/use/http.mjs +2 -19
  26. package/lib/use/http2.d.mts +1 -1
  27. package/lib/use/http2.d.ts +1 -1
  28. package/lib/use/http2.js +2 -19
  29. package/lib/use/http2.mjs +2 -19
  30. package/lib/use/koa.js +0 -14
  31. package/lib/use/koa.mjs +0 -14
  32. package/lib/use/node.d.mts +1 -1
  33. package/lib/use/node.d.ts +1 -1
  34. package/lib/use/node.js +1 -1
  35. package/lib/use/node.mjs +1 -1
  36. package/lib/use/uWebSockets.d.mts +36 -0
  37. package/lib/use/uWebSockets.d.ts +36 -0
  38. package/lib/use/uWebSockets.js +84 -0
  39. package/lib/use/uWebSockets.mjs +80 -0
  40. package/lib/utils.d.mts +2 -0
  41. package/lib/utils.d.ts +2 -0
  42. package/lib/utils.js +15 -1
  43. package/lib/utils.mjs +13 -0
  44. package/package.json +30 -25
  45. package/umd/graphql-http-audits.js +63 -175
  46. package/umd/graphql-http-audits.min.js +1 -1
  47. package/umd/graphql-http-audits.min.js.gz +0 -0
package/lib/use/node.d.ts CHANGED
@@ -16,7 +16,7 @@ export type HandlerOptions<Context extends OperationContext = undefined> = HttpH
16
16
  * ```js
17
17
  * import http from 'http';
18
18
  * import { createHandler } from 'graphql-http/lib/use/node';
19
- * import { schema } from './my-graphql-step';
19
+ * import { schema } from './my-graphql-schema';
20
20
  *
21
21
  * const server = http.createServer(createHandler({ schema }));
22
22
  *
package/lib/use/node.js CHANGED
@@ -9,7 +9,7 @@ const http_1 = require("./http");
9
9
  * ```js
10
10
  * import http from 'http';
11
11
  * import { createHandler } from 'graphql-http/lib/use/node';
12
- * import { schema } from './my-graphql-step';
12
+ * import { schema } from './my-graphql-schema';
13
13
  *
14
14
  * const server = http.createServer(createHandler({ schema }));
15
15
  *
package/lib/use/node.mjs CHANGED
@@ -6,7 +6,7 @@ import { createHandler as httpCreateHandler, } from './http.mjs';
6
6
  * ```js
7
7
  * import http from 'http';
8
8
  * import { createHandler } from 'graphql-http/lib/use/node';
9
- * import { schema } from './my-graphql-step/index.mjs';
9
+ * import { schema } from './my-graphql-schema/index.mjs';
10
10
  *
11
11
  * const server = http.createServer(createHandler({ schema }));
12
12
  *
@@ -0,0 +1,36 @@
1
+ import type { HttpRequest, HttpResponse } from 'uWebSockets.js';
2
+ import { HandlerOptions as RawHandlerOptions, OperationContext } from '../handler.mjs';
3
+ /**
4
+ * The context in the request for the handler.
5
+ *
6
+ * @category Server/uWebSockets
7
+ */
8
+ export interface RequestContext {
9
+ res: HttpResponse;
10
+ }
11
+ /**
12
+ * Handler options when using the http adapter.
13
+ *
14
+ * @category Server/uWebSockets
15
+ */
16
+ export type HandlerOptions<Context extends OperationContext = undefined> = RawHandlerOptions<HttpRequest, RequestContext, Context>;
17
+ /**
18
+ * Create a GraphQL over HTTP spec compliant request handler for
19
+ * the Node environment [uWebSockets.js module](https://github.com/uNetworking/uWebSockets.js/).
20
+ *
21
+ * ```js
22
+ * import uWS from 'uWebSockets.js'; // yarn add uWebSockets.js@uNetworking/uWebSockets.js#<version>
23
+ * import { createHandler } from 'graphql-http/lib/use/uWebSockets';
24
+ * import { schema } from './my-graphql-schema/index.mjs';
25
+ *
26
+ * uWS
27
+ * .App()
28
+ * .any('/graphql', createHandler({ schema }))
29
+ * .listen(4000, () => {
30
+ * console.log('Listening to port 4000');
31
+ * });
32
+ * ```
33
+ *
34
+ * @category Server/uWebSockets
35
+ */
36
+ export declare function createHandler<Context extends OperationContext = undefined>(options: HandlerOptions<Context>): (res: HttpResponse, req: HttpRequest) => Promise<void>;
@@ -0,0 +1,36 @@
1
+ import type { HttpRequest, HttpResponse } from 'uWebSockets.js';
2
+ import { HandlerOptions as RawHandlerOptions, OperationContext } from '../handler';
3
+ /**
4
+ * The context in the request for the handler.
5
+ *
6
+ * @category Server/uWebSockets
7
+ */
8
+ export interface RequestContext {
9
+ res: HttpResponse;
10
+ }
11
+ /**
12
+ * Handler options when using the http adapter.
13
+ *
14
+ * @category Server/uWebSockets
15
+ */
16
+ export type HandlerOptions<Context extends OperationContext = undefined> = RawHandlerOptions<HttpRequest, RequestContext, Context>;
17
+ /**
18
+ * Create a GraphQL over HTTP spec compliant request handler for
19
+ * the Node environment [uWebSockets.js module](https://github.com/uNetworking/uWebSockets.js/).
20
+ *
21
+ * ```js
22
+ * import uWS from 'uWebSockets.js'; // yarn add uWebSockets.js@uNetworking/uWebSockets.js#<version>
23
+ * import { createHandler } from 'graphql-http/lib/use/uWebSockets';
24
+ * import { schema } from './my-graphql-schema';
25
+ *
26
+ * uWS
27
+ * .App()
28
+ * .any('/graphql', createHandler({ schema }))
29
+ * .listen(4000, () => {
30
+ * console.log('Listening to port 4000');
31
+ * });
32
+ * ```
33
+ *
34
+ * @category Server/uWebSockets
35
+ */
36
+ export declare function createHandler<Context extends OperationContext = undefined>(options: HandlerOptions<Context>): (res: HttpResponse, req: HttpRequest) => Promise<void>;
@@ -0,0 +1,84 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createHandler = void 0;
4
+ const handler_1 = require("../handler");
5
+ /**
6
+ * Create a GraphQL over HTTP spec compliant request handler for
7
+ * the Node environment [uWebSockets.js module](https://github.com/uNetworking/uWebSockets.js/).
8
+ *
9
+ * ```js
10
+ * import uWS from 'uWebSockets.js'; // yarn add uWebSockets.js@uNetworking/uWebSockets.js#<version>
11
+ * import { createHandler } from 'graphql-http/lib/use/uWebSockets';
12
+ * import { schema } from './my-graphql-schema';
13
+ *
14
+ * uWS
15
+ * .App()
16
+ * .any('/graphql', createHandler({ schema }))
17
+ * .listen(4000, () => {
18
+ * console.log('Listening to port 4000');
19
+ * });
20
+ * ```
21
+ *
22
+ * @category Server/uWebSockets
23
+ */
24
+ function createHandler(options) {
25
+ const handle = (0, handler_1.createHandler)(options);
26
+ return async function requestListener(res, req) {
27
+ let aborted = false;
28
+ res.onAborted(() => (aborted = true));
29
+ try {
30
+ let url = req.getUrl();
31
+ const query = req.getQuery();
32
+ if (query) {
33
+ url += '?' + query;
34
+ }
35
+ const [body, init] = await handle({
36
+ url,
37
+ method: req.getMethod().toUpperCase(),
38
+ headers: { get: (key) => req.getHeader(key) },
39
+ body: () => new Promise((resolve) => {
40
+ let body = '';
41
+ if (aborted) {
42
+ resolve(body);
43
+ }
44
+ else {
45
+ res.onData((chunk, isLast) => {
46
+ body += Buffer.from(chunk, 0, chunk.byteLength).toString();
47
+ if (isLast) {
48
+ resolve(body);
49
+ }
50
+ });
51
+ }
52
+ }),
53
+ raw: req,
54
+ context: { res },
55
+ });
56
+ if (!aborted) {
57
+ res.cork(() => {
58
+ res.writeStatus(`${init.status} ${init.statusText}`);
59
+ for (const [key, val] of Object.entries(init.headers || {})) {
60
+ res.writeHeader(key, val);
61
+ }
62
+ if (body) {
63
+ res.end(body);
64
+ }
65
+ else {
66
+ res.endWithoutBody();
67
+ }
68
+ });
69
+ }
70
+ }
71
+ catch (err) {
72
+ // The handler shouldnt throw errors.
73
+ // If you wish to handle them differently, consider implementing your own request handler.
74
+ console.error('Internal error occurred during request handling. ' +
75
+ 'Please check your implementation.', err);
76
+ if (!aborted) {
77
+ res.cork(() => {
78
+ res.writeStatus('500 Internal Server Error').endWithoutBody();
79
+ });
80
+ }
81
+ }
82
+ };
83
+ }
84
+ exports.createHandler = createHandler;
@@ -0,0 +1,80 @@
1
+ import { createHandler as createRawHandler, } from '../handler.mjs';
2
+ /**
3
+ * Create a GraphQL over HTTP spec compliant request handler for
4
+ * the Node environment [uWebSockets.js module](https://github.com/uNetworking/uWebSockets.js/).
5
+ *
6
+ * ```js
7
+ * import uWS from 'uWebSockets.js'; // yarn add uWebSockets.js@uNetworking/uWebSockets.js#<version>
8
+ * import { createHandler } from 'graphql-http/lib/use/uWebSockets';
9
+ * import { schema } from './my-graphql-schema/index.mjs';
10
+ *
11
+ * uWS
12
+ * .App()
13
+ * .any('/graphql', createHandler({ schema }))
14
+ * .listen(4000, () => {
15
+ * console.log('Listening to port 4000');
16
+ * });
17
+ * ```
18
+ *
19
+ * @category Server/uWebSockets
20
+ */
21
+ export function createHandler(options) {
22
+ const handle = createRawHandler(options);
23
+ return async function requestListener(res, req) {
24
+ let aborted = false;
25
+ res.onAborted(() => (aborted = true));
26
+ try {
27
+ let url = req.getUrl();
28
+ const query = req.getQuery();
29
+ if (query) {
30
+ url += '?' + query;
31
+ }
32
+ const [body, init] = await handle({
33
+ url,
34
+ method: req.getMethod().toUpperCase(),
35
+ headers: { get: (key) => req.getHeader(key) },
36
+ body: () => new Promise((resolve) => {
37
+ let body = '';
38
+ if (aborted) {
39
+ resolve(body);
40
+ }
41
+ else {
42
+ res.onData((chunk, isLast) => {
43
+ body += Buffer.from(chunk, 0, chunk.byteLength).toString();
44
+ if (isLast) {
45
+ resolve(body);
46
+ }
47
+ });
48
+ }
49
+ }),
50
+ raw: req,
51
+ context: { res },
52
+ });
53
+ if (!aborted) {
54
+ res.cork(() => {
55
+ res.writeStatus(`${init.status} ${init.statusText}`);
56
+ for (const [key, val] of Object.entries(init.headers || {})) {
57
+ res.writeHeader(key, val);
58
+ }
59
+ if (body) {
60
+ res.end(body);
61
+ }
62
+ else {
63
+ res.endWithoutBody();
64
+ }
65
+ });
66
+ }
67
+ }
68
+ catch (err) {
69
+ // The handler shouldnt throw errors.
70
+ // If you wish to handle them differently, consider implementing your own request handler.
71
+ console.error('Internal error occurred during request handling. ' +
72
+ 'Please check your implementation.', err);
73
+ if (!aborted) {
74
+ res.cork(() => {
75
+ res.writeStatus('500 Internal Server Error').endWithoutBody();
76
+ });
77
+ }
78
+ }
79
+ };
80
+ }
package/lib/utils.d.mts CHANGED
@@ -17,3 +17,5 @@ export declare function isGraphQLError(obj: unknown): obj is GraphQLError;
17
17
  export declare function isExecutionResult(val: unknown): val is ExecutionResult;
18
18
  /** @private */
19
19
  export declare function isAsyncIterable<T = unknown>(val: unknown): val is AsyncIterable<T>;
20
+ /** @private */
21
+ export declare function jsonErrorReplacer(_key: string, val: any): any;
package/lib/utils.d.ts CHANGED
@@ -17,3 +17,5 @@ export declare function isGraphQLError(obj: unknown): obj is GraphQLError;
17
17
  export declare function isExecutionResult(val: unknown): val is ExecutionResult;
18
18
  /** @private */
19
19
  export declare function isAsyncIterable<T = unknown>(val: unknown): val is AsyncIterable<T>;
20
+ /** @private */
21
+ export declare function jsonErrorReplacer(_key: string, val: any): any;
package/lib/utils.js CHANGED
@@ -5,7 +5,7 @@
5
5
  *
6
6
  */
7
7
  Object.defineProperty(exports, "__esModule", { value: true });
8
- exports.isAsyncIterable = exports.isExecutionResult = exports.isGraphQLError = exports.areGraphQLErrors = exports.isObject = exports.extendedTypeof = void 0;
8
+ exports.jsonErrorReplacer = exports.isAsyncIterable = exports.isExecutionResult = exports.isGraphQLError = exports.areGraphQLErrors = exports.isObject = exports.extendedTypeof = void 0;
9
9
  const graphql_1 = require("graphql");
10
10
  /** @private */
11
11
  function extendedTypeof(val) {
@@ -47,3 +47,17 @@ function isAsyncIterable(val) {
47
47
  return typeof Object(val)[Symbol.asyncIterator] === 'function';
48
48
  }
49
49
  exports.isAsyncIterable = isAsyncIterable;
50
+ /** @private */
51
+ function jsonErrorReplacer(_key, val) {
52
+ if (val instanceof Error &&
53
+ // GraphQL errors implement their own stringer
54
+ !isGraphQLError(val)) {
55
+ return {
56
+ // name: val.name, name is included in message
57
+ message: val.message,
58
+ // stack: val.stack, can leak sensitive details
59
+ };
60
+ }
61
+ return val;
62
+ }
63
+ exports.jsonErrorReplacer = jsonErrorReplacer;
package/lib/utils.mjs CHANGED
@@ -38,3 +38,16 @@ export function isExecutionResult(val) {
38
38
  export function isAsyncIterable(val) {
39
39
  return typeof Object(val)[Symbol.asyncIterator] === 'function';
40
40
  }
41
+ /** @private */
42
+ export function jsonErrorReplacer(_key, val) {
43
+ if (val instanceof Error &&
44
+ // GraphQL errors implement their own stringer
45
+ !isGraphQLError(val)) {
46
+ return {
47
+ // name: val.name, name is included in message
48
+ message: val.message,
49
+ // stack: val.stack, can leak sensitive details
50
+ };
51
+ }
52
+ return val;
53
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "graphql-http",
3
- "version": "1.17.1",
3
+ "version": "1.19.0",
4
4
  "description": "Simple, pluggable, zero-dependency, GraphQL over HTTP spec compliant server, client and audit suite.",
5
5
  "keywords": [
6
6
  "graphql",
@@ -24,7 +24,7 @@
24
24
  "engines": {
25
25
  "node": ">=12"
26
26
  },
27
- "packageManager": "yarn@3.5.0",
27
+ "packageManager": "yarn@3.6.0",
28
28
  "main": "lib/index.js",
29
29
  "module": "lib/index.mjs",
30
30
  "browser": "umd/graphql-http.js",
@@ -70,6 +70,11 @@
70
70
  "require": "./lib/use/koa.js",
71
71
  "import": "./lib/use/koa.mjs"
72
72
  },
73
+ "./lib/use/uWebSockets": {
74
+ "types": "./lib/use/uWebSockets.d.ts",
75
+ "require": "./lib/use/uWebSockets.js",
76
+ "import": "./lib/use/uWebSockets.mjs"
77
+ },
73
78
  "./package.json": "./package.json"
74
79
  },
75
80
  "types": "lib/index.d.ts",
@@ -106,49 +111,49 @@
106
111
  "graphql": ">=0.11 <=16"
107
112
  },
108
113
  "devDependencies": {
109
- "@babel/core": "^7.21.4",
114
+ "@babel/core": "^7.22.1",
110
115
  "@babel/plugin-proposal-class-properties": "^7.18.6",
111
116
  "@babel/plugin-proposal-nullish-coalescing-operator": "^7.18.6",
112
117
  "@babel/plugin-proposal-object-rest-spread": "^7.20.7",
113
118
  "@babel/plugin-proposal-optional-chaining": "^7.21.0",
114
- "@babel/preset-env": "^7.21.4",
115
- "@babel/preset-typescript": "^7.21.4",
116
- "@rollup/plugin-terser": "^0.4.0",
117
- "@rollup/plugin-typescript": "^11.0.0",
119
+ "@babel/preset-env": "^7.22.4",
120
+ "@babel/preset-typescript": "^7.21.5",
121
+ "@rollup/plugin-terser": "^0.4.3",
122
+ "@rollup/plugin-typescript": "^11.1.1",
118
123
  "@semantic-release/changelog": "^6.0.3",
119
124
  "@semantic-release/git": "^10.0.1",
120
- "@types/eslint": "^8.37.0",
125
+ "@types/eslint": "^8.40.0",
121
126
  "@types/express": "^4.17.17",
122
127
  "@types/glob": "^8.1.0",
123
128
  "@types/html-validator": "^5.0.3",
124
- "@types/jest": "^29.5.0",
129
+ "@types/jest": "^29.5.2",
130
+ "@types/k6": "^0.44.2",
125
131
  "@types/koa": "^2.13.6",
126
132
  "@types/koa-mount": "^4.0.2",
127
- "@typescript-eslint/eslint-plugin": "^5.57.0",
128
- "@typescript-eslint/parser": "^5.57.0",
129
- "@whatwg-node/fetch": "^0.8.4",
133
+ "@typescript-eslint/eslint-plugin": "^5.59.8",
134
+ "@typescript-eslint/parser": "^5.59.8",
135
+ "@whatwg-node/fetch": "^0.9.2",
130
136
  "babel-jest": "^29.5.0",
131
- "eslint": "^8.37.0",
137
+ "eslint": "^8.42.0",
132
138
  "eslint-config-prettier": "^8.8.0",
133
139
  "express": "^4.18.2",
134
- "fastify": "^4.15.0",
140
+ "fastify": "^4.17.0",
141
+ "glob": "^10.2.6",
135
142
  "graphql": "^16.6.0",
136
143
  "html-validator": "^6.0.1",
137
144
  "jest": "^29.5.0",
138
145
  "jest-jasmine2": "^29.5.0",
139
- "koa": "^2.14.1",
146
+ "koa": "^2.14.2",
140
147
  "koa-mount": "^4.0.0",
141
148
  "node-fetch": "^3.3.1",
142
- "prettier": "^2.8.7",
143
- "rollup": "^3.20.2",
149
+ "prettier": "^2.8.8",
150
+ "rollup": "^3.23.1",
144
151
  "rollup-plugin-gzip": "^3.1.0",
145
- "semantic-release": "^21.0.0",
146
- "tslib": "^2.5.0",
147
- "typedoc": "^0.23.28",
148
- "typedoc-plugin-markdown": "^3.14.0",
149
- "typescript": "^5.0.3"
150
- },
151
- "resolutions": {
152
- "npm/libnpmversion": "^3.0.6"
152
+ "semantic-release": "^21.0.3",
153
+ "tslib": "^2.5.3",
154
+ "typedoc": "^0.24.8",
155
+ "typedoc-plugin-markdown": "^3.15.3",
156
+ "typescript": "^5.1.3",
157
+ "uWebSockets.js": "uNetworking/uWebSockets.js#v20.30.0"
153
158
  }
154
159
  }