@comapeo/map-server 1.0.0-pre.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 (179) hide show
  1. package/README.md +610 -0
  2. package/dist/context.d.ts +46 -0
  3. package/dist/context.d.ts.map +1 -0
  4. package/dist/context.js +181 -0
  5. package/dist/index.d.ts +25 -0
  6. package/dist/index.d.ts.map +1 -0
  7. package/dist/index.js +112 -0
  8. package/dist/lib/constants.d.ts +7 -0
  9. package/dist/lib/constants.d.ts.map +1 -0
  10. package/dist/lib/constants.js +6 -0
  11. package/dist/lib/download-request.d.ts +17 -0
  12. package/dist/lib/download-request.d.ts.map +1 -0
  13. package/dist/lib/download-request.js +113 -0
  14. package/dist/lib/errors.d.ts +88 -0
  15. package/dist/lib/errors.d.ts.map +1 -0
  16. package/dist/lib/errors.js +158 -0
  17. package/dist/lib/event-stream-response.d.ts +17 -0
  18. package/dist/lib/event-stream-response.d.ts.map +1 -0
  19. package/dist/lib/event-stream-response.js +39 -0
  20. package/dist/lib/event-target.d.ts +9 -0
  21. package/dist/lib/event-target.d.ts.map +1 -0
  22. package/dist/lib/event-target.js +4 -0
  23. package/dist/lib/fetch-api.d.ts +3 -0
  24. package/dist/lib/fetch-api.d.ts.map +1 -0
  25. package/dist/lib/fetch-api.js +16 -0
  26. package/dist/lib/map-share.d.ts +52 -0
  27. package/dist/lib/map-share.d.ts.map +1 -0
  28. package/dist/lib/map-share.js +142 -0
  29. package/dist/lib/secret-stream-fetch.d.ts +7 -0
  30. package/dist/lib/secret-stream-fetch.d.ts.map +1 -0
  31. package/dist/lib/secret-stream-fetch.js +34 -0
  32. package/dist/lib/self-evicting-map.d.ts +16 -0
  33. package/dist/lib/self-evicting-map.d.ts.map +1 -0
  34. package/dist/lib/self-evicting-map.js +29 -0
  35. package/dist/lib/state-update-event.d.ts +8 -0
  36. package/dist/lib/state-update-event.d.ts.map +1 -0
  37. package/dist/lib/state-update-event.js +10 -0
  38. package/dist/lib/utils.d.ts +32 -0
  39. package/dist/lib/utils.d.ts.map +1 -0
  40. package/dist/lib/utils.js +96 -0
  41. package/dist/middlewares/localhost-only.d.ts +11 -0
  42. package/dist/middlewares/localhost-only.d.ts.map +1 -0
  43. package/dist/middlewares/localhost-only.js +10 -0
  44. package/dist/middlewares/parse-request.d.ts +11 -0
  45. package/dist/middlewares/parse-request.d.ts.map +1 -0
  46. package/dist/middlewares/parse-request.js +25 -0
  47. package/dist/routes/downloads.d.ts +15 -0
  48. package/dist/routes/downloads.d.ts.map +1 -0
  49. package/dist/routes/downloads.js +60 -0
  50. package/dist/routes/map-shares.d.ts +19 -0
  51. package/dist/routes/map-shares.d.ts.map +1 -0
  52. package/dist/routes/map-shares.js +192 -0
  53. package/dist/routes/maps.d.ts +6 -0
  54. package/dist/routes/maps.d.ts.map +1 -0
  55. package/dist/routes/maps.js +118 -0
  56. package/dist/routes/root.d.ts +6 -0
  57. package/dist/routes/root.d.ts.map +1 -0
  58. package/dist/routes/root.js +29 -0
  59. package/dist/types.d.ts +110 -0
  60. package/dist/types.d.ts.map +1 -0
  61. package/dist/types.js +96 -0
  62. package/node_modules/@envelop/instrumentation/LICENSE +21 -0
  63. package/node_modules/@envelop/instrumentation/README.md +30 -0
  64. package/node_modules/@envelop/instrumentation/cjs/index.js +5 -0
  65. package/node_modules/@envelop/instrumentation/cjs/instrumentation.js +89 -0
  66. package/node_modules/@envelop/instrumentation/cjs/package.json +1 -0
  67. package/node_modules/@envelop/instrumentation/esm/index.js +2 -0
  68. package/node_modules/@envelop/instrumentation/esm/instrumentation.js +82 -0
  69. package/node_modules/@envelop/instrumentation/package.json +57 -0
  70. package/node_modules/@envelop/instrumentation/typings/index.d.cts +1 -0
  71. package/node_modules/@envelop/instrumentation/typings/index.d.ts +1 -0
  72. package/node_modules/@envelop/instrumentation/typings/instrumentation.d.cts +44 -0
  73. package/node_modules/@envelop/instrumentation/typings/instrumentation.d.ts +44 -0
  74. package/node_modules/@whatwg-node/disposablestack/cjs/AsyncDisposableStack.js +73 -0
  75. package/node_modules/@whatwg-node/disposablestack/cjs/DisposableStack.js +62 -0
  76. package/node_modules/@whatwg-node/disposablestack/cjs/SupressedError.js +16 -0
  77. package/node_modules/@whatwg-node/disposablestack/cjs/index.js +11 -0
  78. package/node_modules/@whatwg-node/disposablestack/cjs/package.json +1 -0
  79. package/node_modules/@whatwg-node/disposablestack/cjs/symbols.js +20 -0
  80. package/node_modules/@whatwg-node/disposablestack/cjs/utils.js +11 -0
  81. package/node_modules/@whatwg-node/disposablestack/esm/AsyncDisposableStack.js +69 -0
  82. package/node_modules/@whatwg-node/disposablestack/esm/DisposableStack.js +58 -0
  83. package/node_modules/@whatwg-node/disposablestack/esm/SupressedError.js +12 -0
  84. package/node_modules/@whatwg-node/disposablestack/esm/index.js +7 -0
  85. package/node_modules/@whatwg-node/disposablestack/esm/symbols.js +16 -0
  86. package/node_modules/@whatwg-node/disposablestack/esm/utils.js +7 -0
  87. package/node_modules/@whatwg-node/disposablestack/package.json +44 -0
  88. package/node_modules/@whatwg-node/disposablestack/typings/AsyncDisposableStack.d.cts +15 -0
  89. package/node_modules/@whatwg-node/disposablestack/typings/AsyncDisposableStack.d.ts +15 -0
  90. package/node_modules/@whatwg-node/disposablestack/typings/DisposableStack.d.cts +14 -0
  91. package/node_modules/@whatwg-node/disposablestack/typings/DisposableStack.d.ts +14 -0
  92. package/node_modules/@whatwg-node/disposablestack/typings/SupressedError.d.cts +5 -0
  93. package/node_modules/@whatwg-node/disposablestack/typings/SupressedError.d.ts +5 -0
  94. package/node_modules/@whatwg-node/disposablestack/typings/index.d.cts +4 -0
  95. package/node_modules/@whatwg-node/disposablestack/typings/index.d.ts +4 -0
  96. package/node_modules/@whatwg-node/disposablestack/typings/symbols.d.cts +5 -0
  97. package/node_modules/@whatwg-node/disposablestack/typings/symbols.d.ts +5 -0
  98. package/node_modules/@whatwg-node/disposablestack/typings/utils.d.cts +2 -0
  99. package/node_modules/@whatwg-node/disposablestack/typings/utils.d.ts +2 -0
  100. package/node_modules/@whatwg-node/promise-helpers/cjs/index.js +270 -0
  101. package/node_modules/@whatwg-node/promise-helpers/cjs/package.json +1 -0
  102. package/node_modules/@whatwg-node/promise-helpers/esm/index.js +257 -0
  103. package/node_modules/@whatwg-node/promise-helpers/package.json +43 -0
  104. package/node_modules/@whatwg-node/promise-helpers/typings/index.d.cts +31 -0
  105. package/node_modules/@whatwg-node/promise-helpers/typings/index.d.ts +31 -0
  106. package/node_modules/@whatwg-node/server/README.md +590 -0
  107. package/node_modules/@whatwg-node/server/cjs/createServerAdapter.js +368 -0
  108. package/node_modules/@whatwg-node/server/cjs/index.js +17 -0
  109. package/node_modules/@whatwg-node/server/cjs/package.json +1 -0
  110. package/node_modules/@whatwg-node/server/cjs/plugins/types.js +0 -0
  111. package/node_modules/@whatwg-node/server/cjs/plugins/useContentEncoding.js +73 -0
  112. package/node_modules/@whatwg-node/server/cjs/plugins/useCors.js +124 -0
  113. package/node_modules/@whatwg-node/server/cjs/plugins/useErrorHandling.js +52 -0
  114. package/node_modules/@whatwg-node/server/cjs/types.js +0 -0
  115. package/node_modules/@whatwg-node/server/cjs/utils.js +599 -0
  116. package/node_modules/@whatwg-node/server/cjs/uwebsockets.js +241 -0
  117. package/node_modules/@whatwg-node/server/esm/createServerAdapter.js +365 -0
  118. package/node_modules/@whatwg-node/server/esm/index.js +11 -0
  119. package/node_modules/@whatwg-node/server/esm/plugins/types.js +0 -0
  120. package/node_modules/@whatwg-node/server/esm/plugins/useContentEncoding.js +70 -0
  121. package/node_modules/@whatwg-node/server/esm/plugins/useCors.js +120 -0
  122. package/node_modules/@whatwg-node/server/esm/plugins/useErrorHandling.js +46 -0
  123. package/node_modules/@whatwg-node/server/esm/types.js +0 -0
  124. package/node_modules/@whatwg-node/server/esm/utils.js +588 -0
  125. package/node_modules/@whatwg-node/server/esm/uwebsockets.js +234 -0
  126. package/node_modules/@whatwg-node/server/package.json +46 -0
  127. package/node_modules/@whatwg-node/server/typings/createServerAdapter.d.cts +19 -0
  128. package/node_modules/@whatwg-node/server/typings/createServerAdapter.d.ts +19 -0
  129. package/node_modules/@whatwg-node/server/typings/index.d.cts +11 -0
  130. package/node_modules/@whatwg-node/server/typings/index.d.ts +11 -0
  131. package/node_modules/@whatwg-node/server/typings/plugins/types.d.cts +76 -0
  132. package/node_modules/@whatwg-node/server/typings/plugins/types.d.ts +76 -0
  133. package/node_modules/@whatwg-node/server/typings/plugins/useContentEncoding.d.cts +2 -0
  134. package/node_modules/@whatwg-node/server/typings/plugins/useContentEncoding.d.ts +2 -0
  135. package/node_modules/@whatwg-node/server/typings/plugins/useCors.d.cts +14 -0
  136. package/node_modules/@whatwg-node/server/typings/plugins/useCors.d.ts +14 -0
  137. package/node_modules/@whatwg-node/server/typings/plugins/useErrorHandling.d.cts +13 -0
  138. package/node_modules/@whatwg-node/server/typings/plugins/useErrorHandling.d.ts +13 -0
  139. package/node_modules/@whatwg-node/server/typings/types.d.cts +100 -0
  140. package/node_modules/@whatwg-node/server/typings/types.d.ts +100 -0
  141. package/node_modules/@whatwg-node/server/typings/utils.d.cts +42 -0
  142. package/node_modules/@whatwg-node/server/typings/utils.d.ts +42 -0
  143. package/node_modules/@whatwg-node/server/typings/uwebsockets.d.cts +32 -0
  144. package/node_modules/@whatwg-node/server/typings/uwebsockets.d.ts +32 -0
  145. package/node_modules/tslib/CopyrightNotice.txt +15 -0
  146. package/node_modules/tslib/LICENSE.txt +12 -0
  147. package/node_modules/tslib/README.md +164 -0
  148. package/node_modules/tslib/SECURITY.md +41 -0
  149. package/node_modules/tslib/modules/index.d.ts +38 -0
  150. package/node_modules/tslib/modules/index.js +70 -0
  151. package/node_modules/tslib/modules/package.json +3 -0
  152. package/node_modules/tslib/package.json +47 -0
  153. package/node_modules/tslib/tslib.d.ts +460 -0
  154. package/node_modules/tslib/tslib.es6.html +1 -0
  155. package/node_modules/tslib/tslib.es6.js +402 -0
  156. package/node_modules/tslib/tslib.es6.mjs +401 -0
  157. package/node_modules/tslib/tslib.html +1 -0
  158. package/node_modules/tslib/tslib.js +484 -0
  159. package/package.json +87 -0
  160. package/src/context.ts +203 -0
  161. package/src/index.ts +193 -0
  162. package/src/lib/constants.ts +6 -0
  163. package/src/lib/download-request.ts +142 -0
  164. package/src/lib/errors.ts +187 -0
  165. package/src/lib/event-stream-response.ts +57 -0
  166. package/src/lib/event-target.ts +11 -0
  167. package/src/lib/fetch-api.ts +18 -0
  168. package/src/lib/map-share.ts +185 -0
  169. package/src/lib/secret-stream-fetch.ts +42 -0
  170. package/src/lib/self-evicting-map.ts +35 -0
  171. package/src/lib/state-update-event.ts +14 -0
  172. package/src/lib/utils.ts +110 -0
  173. package/src/middlewares/localhost-only.ts +16 -0
  174. package/src/middlewares/parse-request.ts +34 -0
  175. package/src/routes/downloads.ts +92 -0
  176. package/src/routes/map-shares.ts +246 -0
  177. package/src/routes/maps.ts +146 -0
  178. package/src/routes/root.ts +37 -0
  179. package/src/types.ts +152 -0
@@ -0,0 +1,234 @@
1
+ import { fakePromise } from '@whatwg-node/promise-helpers';
2
+ export function isUWSResponse(res) {
3
+ return !!res.onData;
4
+ }
5
+ export function getRequestFromUWSRequest({ req, res, fetchAPI, controller, }) {
6
+ const method = req.getMethod();
7
+ let duplex;
8
+ const chunks = [];
9
+ const pushFns = [
10
+ (chunk) => {
11
+ chunks.push(chunk);
12
+ },
13
+ ];
14
+ const push = (chunk) => {
15
+ for (const pushFn of pushFns) {
16
+ pushFn(chunk);
17
+ }
18
+ };
19
+ let stopped = false;
20
+ const stopFns = [
21
+ () => {
22
+ stopped = true;
23
+ },
24
+ ];
25
+ const stop = () => {
26
+ for (const stopFn of stopFns) {
27
+ stopFn();
28
+ }
29
+ };
30
+ res.onData(function (ab, isLast) {
31
+ push(Buffer.from(Buffer.from(ab, 0, ab.byteLength)));
32
+ if (isLast) {
33
+ stop();
34
+ }
35
+ });
36
+ let getReadableStream;
37
+ if (method !== 'get' && method !== 'head') {
38
+ duplex = 'half';
39
+ controller.signal.addEventListener('abort', () => {
40
+ stop();
41
+ }, { once: true });
42
+ let readableStream;
43
+ getReadableStream = () => {
44
+ if (!readableStream) {
45
+ readableStream = new fetchAPI.ReadableStream({
46
+ start(streamCtrl) {
47
+ for (const chunk of chunks) {
48
+ streamCtrl.enqueue(chunk);
49
+ }
50
+ if (stopped) {
51
+ streamCtrl.close();
52
+ return;
53
+ }
54
+ pushFns.push((chunk) => {
55
+ streamCtrl.enqueue(chunk);
56
+ });
57
+ stopFns.push(() => {
58
+ if (controller.signal.reason) {
59
+ streamCtrl.error(controller.signal.reason);
60
+ return;
61
+ }
62
+ if (streamCtrl.desiredSize) {
63
+ streamCtrl.close();
64
+ }
65
+ });
66
+ },
67
+ });
68
+ }
69
+ return readableStream;
70
+ };
71
+ }
72
+ const headers = new fetchAPI.Headers();
73
+ req.forEach((key, value) => {
74
+ headers.append(key, value);
75
+ });
76
+ let url = `http://localhost${req.getUrl()}`;
77
+ const query = req.getQuery();
78
+ if (query) {
79
+ url += `?${query}`;
80
+ }
81
+ let buffer;
82
+ function getBody() {
83
+ if (!getReadableStream) {
84
+ return null;
85
+ }
86
+ if (stopped) {
87
+ return getBufferFromChunks();
88
+ }
89
+ return getReadableStream();
90
+ }
91
+ const request = new fetchAPI.Request(url, {
92
+ method,
93
+ headers,
94
+ get body() {
95
+ return getBody();
96
+ },
97
+ signal: controller.signal,
98
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
99
+ // @ts-ignore - not in the TS types yet
100
+ duplex,
101
+ });
102
+ function getBufferFromChunks() {
103
+ if (!buffer) {
104
+ buffer = chunks.length === 1 ? chunks[0] : Buffer.concat(chunks);
105
+ }
106
+ return buffer;
107
+ }
108
+ function collectBuffer() {
109
+ if (stopped) {
110
+ return fakePromise(getBufferFromChunks());
111
+ }
112
+ return new Promise((resolve, reject) => {
113
+ try {
114
+ stopFns.push(() => {
115
+ resolve(getBufferFromChunks());
116
+ });
117
+ }
118
+ catch (e) {
119
+ reject(e);
120
+ }
121
+ });
122
+ }
123
+ Object.defineProperties(request, {
124
+ body: {
125
+ get() {
126
+ return getBody();
127
+ },
128
+ configurable: true,
129
+ enumerable: true,
130
+ },
131
+ json: {
132
+ value() {
133
+ return collectBuffer()
134
+ .then(b => b.toString('utf8'))
135
+ .then(t => JSON.parse(t));
136
+ },
137
+ configurable: true,
138
+ enumerable: true,
139
+ },
140
+ text: {
141
+ value() {
142
+ return collectBuffer().then(b => b.toString('utf8'));
143
+ },
144
+ configurable: true,
145
+ enumerable: true,
146
+ },
147
+ arrayBuffer: {
148
+ value() {
149
+ return collectBuffer();
150
+ },
151
+ configurable: true,
152
+ enumerable: true,
153
+ },
154
+ });
155
+ return request;
156
+ }
157
+ export function createWritableFromUWS(uwsResponse, fetchAPI) {
158
+ return new fetchAPI.WritableStream({
159
+ write(chunk) {
160
+ uwsResponse.cork(() => {
161
+ uwsResponse.write(chunk);
162
+ });
163
+ },
164
+ close() {
165
+ uwsResponse.cork(() => {
166
+ uwsResponse.end();
167
+ });
168
+ },
169
+ });
170
+ }
171
+ export function sendResponseToUwsOpts(uwsResponse, fetchResponse, controller, fetchAPI) {
172
+ if (!fetchResponse) {
173
+ uwsResponse.writeStatus('404 Not Found');
174
+ uwsResponse.end();
175
+ return;
176
+ }
177
+ const bufferOfRes = fetchResponse._buffer;
178
+ // @ts-expect-error - Handle the case where the response is a string
179
+ const strBody = fetchResponse['bodyType'] === 'String' ? fetchResponse.bodyInit : undefined;
180
+ if (controller.signal.aborted) {
181
+ return;
182
+ }
183
+ uwsResponse.cork(() => {
184
+ uwsResponse.writeStatus(`${fetchResponse.status} ${fetchResponse.statusText}`);
185
+ let isSetCookieHandled = false;
186
+ for (const [key, value] of fetchResponse.headers) {
187
+ // content-length causes an error with Node.js's fetch
188
+ if (key !== 'content-length') {
189
+ if (key === 'set-cookie') {
190
+ if (isSetCookieHandled) {
191
+ continue;
192
+ }
193
+ isSetCookieHandled = true;
194
+ const setCookies = fetchResponse.headers.getSetCookie?.();
195
+ if (setCookies) {
196
+ for (const setCookie of setCookies) {
197
+ uwsResponse.writeHeader(key, setCookie);
198
+ }
199
+ continue;
200
+ }
201
+ }
202
+ uwsResponse.writeHeader(key, value);
203
+ }
204
+ }
205
+ if (strBody) {
206
+ uwsResponse.end(strBody);
207
+ }
208
+ else if (bufferOfRes) {
209
+ uwsResponse.end(bufferOfRes);
210
+ }
211
+ else if (!fetchResponse.body) {
212
+ uwsResponse.end();
213
+ }
214
+ });
215
+ if (strBody || bufferOfRes || !fetchResponse.body) {
216
+ return;
217
+ }
218
+ controller.signal.addEventListener('abort', () => {
219
+ if (!fetchResponse.body?.locked) {
220
+ fetchResponse.body?.cancel(controller.signal.reason);
221
+ }
222
+ }, { once: true });
223
+ return fetchResponse.body
224
+ .pipeTo(createWritableFromUWS(uwsResponse, fetchAPI), {
225
+ signal: controller.signal,
226
+ })
227
+ .catch(err => {
228
+ if (controller.signal.aborted) {
229
+ return;
230
+ }
231
+ throw err;
232
+ });
233
+ }
234
+ export { fakePromise };
@@ -0,0 +1,46 @@
1
+ {
2
+ "name": "@whatwg-node/server",
3
+ "version": "0.10.17",
4
+ "description": "Fetch API compliant HTTP Server adapter",
5
+ "sideEffects": false,
6
+ "dependencies": {
7
+ "@envelop/instrumentation": "^1.0.0",
8
+ "@whatwg-node/disposablestack": "^0.0.6",
9
+ "@whatwg-node/promise-helpers": "^1.3.2",
10
+ "tslib": "^2.6.3"
11
+ },
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "ardatan/whatwg-node",
15
+ "directory": "packages/server"
16
+ },
17
+ "author": "Arda TANRIKULU <ardatanrikulu@gmail.com>",
18
+ "license": "MIT",
19
+ "engines": {
20
+ "node": ">=18.0.0"
21
+ },
22
+ "main": "cjs/index.js",
23
+ "module": "esm/index.js",
24
+ "typings": "typings/index.d.ts",
25
+ "typescript": {
26
+ "definition": "typings/index.d.ts"
27
+ },
28
+ "type": "module",
29
+ "exports": {
30
+ ".": {
31
+ "require": {
32
+ "types": "./typings/index.d.cts",
33
+ "default": "./cjs/index.js"
34
+ },
35
+ "import": {
36
+ "types": "./typings/index.d.ts",
37
+ "default": "./esm/index.js"
38
+ },
39
+ "default": {
40
+ "types": "./typings/index.d.ts",
41
+ "default": "./esm/index.js"
42
+ }
43
+ },
44
+ "./package.json": "./package.json"
45
+ }
46
+ }
@@ -0,0 +1,19 @@
1
+ import { ServerAdapterPlugin } from './plugins/types.cjs';
2
+ import { FetchAPI, ServerAdapter, ServerAdapterBaseObject, ServerAdapterRequestHandler } from './types.cjs';
3
+ export interface ServerAdapterOptions<TServerContext> {
4
+ plugins?: ServerAdapterPlugin<TServerContext>[];
5
+ fetchAPI?: Partial<FetchAPI>;
6
+ /**
7
+ * Node.js only!
8
+ *
9
+ * If true, the server adapter will dispose itself when the process is terminated.
10
+ * If false, you have to dispose the server adapter by using the `dispose` method,
11
+ * or [Explicit Resource Management](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-2.html)
12
+ */
13
+ disposeOnProcessTerminate?: boolean;
14
+ __useCustomAbortCtrl?: boolean;
15
+ __useSingleWriteHead?: boolean;
16
+ }
17
+ declare function createServerAdapter<TServerContext = {}, THandleRequest extends ServerAdapterRequestHandler<TServerContext> = ServerAdapterRequestHandler<TServerContext>>(serverAdapterRequestHandler: THandleRequest, options?: ServerAdapterOptions<TServerContext>): ServerAdapter<TServerContext, ServerAdapterBaseObject<TServerContext, THandleRequest>>;
18
+ declare function createServerAdapter<TServerContext, TBaseObject extends ServerAdapterBaseObject<TServerContext>>(serverAdapterBaseObject: TBaseObject, options?: ServerAdapterOptions<TServerContext>): ServerAdapter<TServerContext, TBaseObject>;
19
+ export { createServerAdapter };
@@ -0,0 +1,19 @@
1
+ import { ServerAdapterPlugin } from './plugins/types.js';
2
+ import { FetchAPI, ServerAdapter, ServerAdapterBaseObject, ServerAdapterRequestHandler } from './types.js';
3
+ export interface ServerAdapterOptions<TServerContext> {
4
+ plugins?: ServerAdapterPlugin<TServerContext>[];
5
+ fetchAPI?: Partial<FetchAPI>;
6
+ /**
7
+ * Node.js only!
8
+ *
9
+ * If true, the server adapter will dispose itself when the process is terminated.
10
+ * If false, you have to dispose the server adapter by using the `dispose` method,
11
+ * or [Explicit Resource Management](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-2.html)
12
+ */
13
+ disposeOnProcessTerminate?: boolean;
14
+ __useCustomAbortCtrl?: boolean;
15
+ __useSingleWriteHead?: boolean;
16
+ }
17
+ declare function createServerAdapter<TServerContext = {}, THandleRequest extends ServerAdapterRequestHandler<TServerContext> = ServerAdapterRequestHandler<TServerContext>>(serverAdapterRequestHandler: THandleRequest, options?: ServerAdapterOptions<TServerContext>): ServerAdapter<TServerContext, ServerAdapterBaseObject<TServerContext, THandleRequest>>;
18
+ declare function createServerAdapter<TServerContext, TBaseObject extends ServerAdapterBaseObject<TServerContext>>(serverAdapterBaseObject: TBaseObject, options?: ServerAdapterOptions<TServerContext>): ServerAdapter<TServerContext, TBaseObject>;
19
+ export { createServerAdapter };
@@ -0,0 +1,11 @@
1
+ export * from './createServerAdapter.cjs';
2
+ export * from './types.cjs';
3
+ export * from './utils.cjs';
4
+ export * from './plugins/types.cjs';
5
+ export * from './plugins/useCors.cjs';
6
+ export * from './plugins/useErrorHandling.cjs';
7
+ export * from './plugins/useContentEncoding.cjs';
8
+ export * from './uwebsockets.cjs';
9
+ export { Response } from '@whatwg-node/fetch';
10
+ export { DisposableSymbols } from '@whatwg-node/disposablestack';
11
+ export * from '@envelop/instrumentation';
@@ -0,0 +1,11 @@
1
+ export * from './createServerAdapter.js';
2
+ export * from './types.js';
3
+ export * from './utils.js';
4
+ export * from './plugins/types.js';
5
+ export * from './plugins/useCors.js';
6
+ export * from './plugins/useErrorHandling.js';
7
+ export * from './plugins/useContentEncoding.js';
8
+ export * from './uwebsockets.js';
9
+ export { Response } from '@whatwg-node/fetch';
10
+ export { DisposableSymbols } from '@whatwg-node/disposablestack';
11
+ export * from '@envelop/instrumentation';
@@ -0,0 +1,76 @@
1
+ import { FetchAPI, ServerAdapterRequestHandler, type ServerAdapterInitialContext } from '../types.cjs';
2
+ export interface ServerAdapterPlugin<TServerContext = {}> {
3
+ /**
4
+ * A tracer instance. It can be used to wrap the entire request handling pipeline (including the
5
+ * plugin hooks). It is mostly used for observability (monitoring, tracing, etc...).
6
+ */
7
+ instrumentation?: Instrumentation;
8
+ /**
9
+ * This hook is invoked for ANY incoming HTTP request. Here you can manipulate the request,
10
+ * create a short circuit before the request handler takes it over.
11
+ *
12
+ * Warning: Exceptions thrown by this hook are not caught.
13
+ * This means they will buble up to the HTTP server underlying implementation.
14
+ * For example, the `node:http` server crashes the entire process on uncaught exceptions.
15
+ */
16
+ onRequest?: OnRequestHook<TServerContext & ServerAdapterInitialContext>;
17
+ /**
18
+ * This hook is invoked after a HTTP request (both GraphQL and NON GraphQL) has been processed
19
+ * and after the response has been forwarded to the client. Here you can perform any cleanup
20
+ * or logging operations, or you can manipulate the outgoing response object.
21
+ *
22
+ * Warning: Exceptions thrown by this hook are not caught.
23
+ * This means they will buble up to the HTTP server underlying implementation.
24
+ * For example, the `node:http` server crashes the entire process on uncaught exceptions.
25
+ */
26
+ onResponse?: OnResponseHook<TServerContext & ServerAdapterInitialContext>;
27
+ /**
28
+ * This hook is invoked when the server is being disposed.
29
+ * The server disposal is triggered either by the process termination or the explicit server disposal.
30
+ * @see https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-2.html
31
+ */
32
+ [Symbol.dispose]?: () => void;
33
+ /**
34
+ * This hook is invoked when the server is being disposed.
35
+ * The server disposal is triggered either by the process termination or the explicit server disposal.
36
+ * @see https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-2.html
37
+ */
38
+ [Symbol.asyncDispose]?: () => PromiseLike<void> | void;
39
+ /**
40
+ * This hook is invoked when the server is being disposed.
41
+ * The server disposal is triggered either by the process termination or the explicit server disposal.
42
+ * @see https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-2.html
43
+ */
44
+ onDispose?: () => PromiseLike<void> | void;
45
+ }
46
+ export type Instrumentation = {
47
+ /**
48
+ * Run code befor, after or around the handling of each request.
49
+ * This instrument can't modify result or paramters of the request handling.
50
+ * To have access to the input or the output of the request handling, use the `onRequest` hook.
51
+ *
52
+ * Note: The `wrapped` function must be called, otherwise the request will not be handled properly
53
+ */
54
+ request?: (payload: {
55
+ request: Request;
56
+ }, wrapped: () => Promise<void> | void) => Promise<void> | void;
57
+ };
58
+ export type OnRequestHook<TServerContext> = (payload: OnRequestEventPayload<TServerContext>) => Promise<void> | void;
59
+ export interface OnRequestEventPayload<TServerContext> {
60
+ request: Request;
61
+ setRequest(newRequest: Request): void;
62
+ serverContext: TServerContext;
63
+ fetchAPI: FetchAPI;
64
+ requestHandler: ServerAdapterRequestHandler<TServerContext>;
65
+ setRequestHandler(newRequestHandler: ServerAdapterRequestHandler<TServerContext>): void;
66
+ endResponse(response: Response): void;
67
+ url: URL;
68
+ }
69
+ export type OnResponseHook<TServerContext> = (payload: OnResponseEventPayload<TServerContext>) => Promise<void> | void;
70
+ export interface OnResponseEventPayload<TServerContext> {
71
+ request: Request;
72
+ serverContext: TServerContext;
73
+ response: Response;
74
+ setResponse(newResponse: Response): void;
75
+ fetchAPI: FetchAPI;
76
+ }
@@ -0,0 +1,76 @@
1
+ import { FetchAPI, ServerAdapterRequestHandler, type ServerAdapterInitialContext } from '../types.js';
2
+ export interface ServerAdapterPlugin<TServerContext = {}> {
3
+ /**
4
+ * A tracer instance. It can be used to wrap the entire request handling pipeline (including the
5
+ * plugin hooks). It is mostly used for observability (monitoring, tracing, etc...).
6
+ */
7
+ instrumentation?: Instrumentation;
8
+ /**
9
+ * This hook is invoked for ANY incoming HTTP request. Here you can manipulate the request,
10
+ * create a short circuit before the request handler takes it over.
11
+ *
12
+ * Warning: Exceptions thrown by this hook are not caught.
13
+ * This means they will buble up to the HTTP server underlying implementation.
14
+ * For example, the `node:http` server crashes the entire process on uncaught exceptions.
15
+ */
16
+ onRequest?: OnRequestHook<TServerContext & ServerAdapterInitialContext>;
17
+ /**
18
+ * This hook is invoked after a HTTP request (both GraphQL and NON GraphQL) has been processed
19
+ * and after the response has been forwarded to the client. Here you can perform any cleanup
20
+ * or logging operations, or you can manipulate the outgoing response object.
21
+ *
22
+ * Warning: Exceptions thrown by this hook are not caught.
23
+ * This means they will buble up to the HTTP server underlying implementation.
24
+ * For example, the `node:http` server crashes the entire process on uncaught exceptions.
25
+ */
26
+ onResponse?: OnResponseHook<TServerContext & ServerAdapterInitialContext>;
27
+ /**
28
+ * This hook is invoked when the server is being disposed.
29
+ * The server disposal is triggered either by the process termination or the explicit server disposal.
30
+ * @see https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-2.html
31
+ */
32
+ [Symbol.dispose]?: () => void;
33
+ /**
34
+ * This hook is invoked when the server is being disposed.
35
+ * The server disposal is triggered either by the process termination or the explicit server disposal.
36
+ * @see https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-2.html
37
+ */
38
+ [Symbol.asyncDispose]?: () => PromiseLike<void> | void;
39
+ /**
40
+ * This hook is invoked when the server is being disposed.
41
+ * The server disposal is triggered either by the process termination or the explicit server disposal.
42
+ * @see https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-2.html
43
+ */
44
+ onDispose?: () => PromiseLike<void> | void;
45
+ }
46
+ export type Instrumentation = {
47
+ /**
48
+ * Run code befor, after or around the handling of each request.
49
+ * This instrument can't modify result or paramters of the request handling.
50
+ * To have access to the input or the output of the request handling, use the `onRequest` hook.
51
+ *
52
+ * Note: The `wrapped` function must be called, otherwise the request will not be handled properly
53
+ */
54
+ request?: (payload: {
55
+ request: Request;
56
+ }, wrapped: () => Promise<void> | void) => Promise<void> | void;
57
+ };
58
+ export type OnRequestHook<TServerContext> = (payload: OnRequestEventPayload<TServerContext>) => Promise<void> | void;
59
+ export interface OnRequestEventPayload<TServerContext> {
60
+ request: Request;
61
+ setRequest(newRequest: Request): void;
62
+ serverContext: TServerContext;
63
+ fetchAPI: FetchAPI;
64
+ requestHandler: ServerAdapterRequestHandler<TServerContext>;
65
+ setRequestHandler(newRequestHandler: ServerAdapterRequestHandler<TServerContext>): void;
66
+ endResponse(response: Response): void;
67
+ url: URL;
68
+ }
69
+ export type OnResponseHook<TServerContext> = (payload: OnResponseEventPayload<TServerContext>) => Promise<void> | void;
70
+ export interface OnResponseEventPayload<TServerContext> {
71
+ request: Request;
72
+ serverContext: TServerContext;
73
+ response: Response;
74
+ setResponse(newResponse: Response): void;
75
+ fetchAPI: FetchAPI;
76
+ }
@@ -0,0 +1,2 @@
1
+ import type { ServerAdapterPlugin } from './types.cjs';
2
+ export declare function useContentEncoding<TServerContext>(): ServerAdapterPlugin<TServerContext>;
@@ -0,0 +1,2 @@
1
+ import type { ServerAdapterPlugin } from './types.js';
2
+ export declare function useContentEncoding<TServerContext>(): ServerAdapterPlugin<TServerContext>;
@@ -0,0 +1,14 @@
1
+ import { MaybePromise } from '@whatwg-node/promise-helpers';
2
+ import type { ServerAdapterPlugin } from './types.cjs';
3
+ export type CORSOptions = {
4
+ origin?: string[] | string;
5
+ methods?: string[];
6
+ allowedHeaders?: string[];
7
+ exposedHeaders?: string[];
8
+ credentials?: boolean;
9
+ maxAge?: number;
10
+ } | false;
11
+ export type CORSPluginOptions<TServerContext> = CORSOptionsFactory<TServerContext> | CORSOptions | boolean;
12
+ export type CORSOptionsFactory<TServerContext> = (request: Request, ...args: {} extends TServerContext ? [serverContext?: TServerContext | undefined] : [serverContext: TServerContext]) => MaybePromise<CORSOptions>;
13
+ export declare function getCORSHeadersByRequestAndOptions(request: Request, corsOptions: CORSOptions): Record<string, string> | null;
14
+ export declare function useCORS<TServerContext>(options?: CORSPluginOptions<TServerContext>): ServerAdapterPlugin<TServerContext>;
@@ -0,0 +1,14 @@
1
+ import { MaybePromise } from '@whatwg-node/promise-helpers';
2
+ import type { ServerAdapterPlugin } from './types.js';
3
+ export type CORSOptions = {
4
+ origin?: string[] | string;
5
+ methods?: string[];
6
+ allowedHeaders?: string[];
7
+ exposedHeaders?: string[];
8
+ credentials?: boolean;
9
+ maxAge?: number;
10
+ } | false;
11
+ export type CORSPluginOptions<TServerContext> = CORSOptionsFactory<TServerContext> | CORSOptions | boolean;
12
+ export type CORSOptionsFactory<TServerContext> = (request: Request, ...args: {} extends TServerContext ? [serverContext?: TServerContext | undefined] : [serverContext: TServerContext]) => MaybePromise<CORSOptions>;
13
+ export declare function getCORSHeadersByRequestAndOptions(request: Request, corsOptions: CORSOptions): Record<string, string> | null;
14
+ export declare function useCORS<TServerContext>(options?: CORSPluginOptions<TServerContext>): ServerAdapterPlugin<TServerContext>;
@@ -0,0 +1,13 @@
1
+ import { MaybePromise } from '@whatwg-node/promise-helpers';
2
+ import type { ServerAdapterPlugin } from './types.cjs';
3
+ export declare function createDefaultErrorHandler<TServerContext = {}>(ResponseCtor?: typeof Response): ErrorHandler<TServerContext>;
4
+ export declare class HTTPError extends Error {
5
+ status: number;
6
+ message: string;
7
+ headers: HeadersInit;
8
+ details?: any | undefined;
9
+ name: string;
10
+ constructor(status: number | undefined, message: string, headers?: HeadersInit, details?: any | undefined);
11
+ }
12
+ export type ErrorHandler<TServerContext> = (e: any, request: Request, ctx: TServerContext) => MaybePromise<Response> | void;
13
+ export declare function useErrorHandling<TServerContext>(onError?: ErrorHandler<TServerContext>): ServerAdapterPlugin<TServerContext>;
@@ -0,0 +1,13 @@
1
+ import { MaybePromise } from '@whatwg-node/promise-helpers';
2
+ import type { ServerAdapterPlugin } from './types.js';
3
+ export declare function createDefaultErrorHandler<TServerContext = {}>(ResponseCtor?: typeof Response): ErrorHandler<TServerContext>;
4
+ export declare class HTTPError extends Error {
5
+ status: number;
6
+ message: string;
7
+ headers: HeadersInit;
8
+ details?: any | undefined;
9
+ name: string;
10
+ constructor(status: number | undefined, message: string, headers?: HeadersInit, details?: any | undefined);
11
+ }
12
+ export type ErrorHandler<TServerContext> = (e: any, request: Request, ctx: TServerContext) => MaybePromise<Response> | void;
13
+ export declare function useErrorHandling<TServerContext>(onError?: ErrorHandler<TServerContext>): ServerAdapterPlugin<TServerContext>;
@@ -0,0 +1,100 @@
1
+ import type { RequestListener } from 'node:http';
2
+ import type { MaybePromise, MaybePromiseLike } from '@whatwg-node/promise-helpers';
3
+ import type { NodeRequest, NodeResponse } from './utils.cjs';
4
+ import { UWSHandler, UWSRequest, UWSResponse } from './uwebsockets.cjs';
5
+ export interface FetchEvent extends Event {
6
+ waitUntil(f: MaybePromise<void>): void;
7
+ request: Request;
8
+ respondWith(r: MaybePromiseLike<Response>): void;
9
+ }
10
+ export interface ServerAdapterBaseObject<TServerContext, THandleRequest extends ServerAdapterRequestHandler<TServerContext> = ServerAdapterRequestHandler<TServerContext>> {
11
+ /**
12
+ * An async function that takes `Request` and the server context and returns a `Response`.
13
+ * If you use `requestListener`, the server context is `{ req: IncomingMessage, res: ServerResponse }`.
14
+ */
15
+ handle: THandleRequest;
16
+ }
17
+ export interface ServerAdapterObject<TServerContext> extends EventListenerObject, AsyncDisposable {
18
+ /**
19
+ * A basic request listener that takes a `Request` with the server context and returns a `Response`.
20
+ */
21
+ handleRequest: (request: Request, ctx: TServerContext & Partial<ServerAdapterInitialContext>) => MaybePromise<Response>;
22
+ /**
23
+ * WHATWG Fetch spec compliant `fetch` function that can be used for testing purposes.
24
+ */
25
+ fetch(request: Request, ctx: TServerContext): MaybePromise<Response>;
26
+ fetch(request: Request, ...ctx: Partial<TServerContext>[]): MaybePromise<Response>;
27
+ fetch(urlStr: string, ctx: TServerContext): MaybePromise<Response>;
28
+ fetch(urlStr: string, ...ctx: Partial<TServerContext>[]): MaybePromise<Response>;
29
+ fetch(urlStr: string, init: RequestInit, ctx: TServerContext): MaybePromise<Response>;
30
+ fetch(urlStr: string, init: RequestInit, ...ctx: Partial<TServerContext>[]): MaybePromise<Response>;
31
+ fetch(url: URL, ctx: TServerContext): MaybePromise<Response>;
32
+ fetch(url: URL, ...ctx: Partial<TServerContext>[]): MaybePromise<Response>;
33
+ fetch(url: URL, init: RequestInit, ctx: TServerContext): MaybePromise<Response>;
34
+ fetch(url: URL, init: RequestInit, ...ctx: Partial<TServerContext>[]): MaybePromise<Response>;
35
+ /**
36
+ * This function takes Node's request object and returns a WHATWG Fetch spec compliant `Response` object.
37
+ *
38
+ * @deprecated Use `handleNodeRequestAndResponse` instead.
39
+ **/
40
+ handleNodeRequest(nodeRequest: NodeRequest, ...ctx: Partial<TServerContext & ServerAdapterInitialContext>[]): MaybePromise<Response>;
41
+ /**
42
+ * This function takes Node's request and response objects and returns a WHATWG Fetch spec compliant `Response` object.
43
+ */
44
+ handleNodeRequestAndResponse(nodeRequest: NodeRequest, nodeResponseOrContainer: {
45
+ raw: NodeResponse;
46
+ } | NodeResponse, ...ctx: Partial<TServerContext & ServerAdapterInitialContext>[]): MaybePromise<Response>;
47
+ /**
48
+ * A request listener function that can be used with any Node server variation.
49
+ */
50
+ requestListener: RequestListener;
51
+ handleUWS: UWSHandler;
52
+ handle(req: NodeRequest, res: NodeResponse, ...ctx: Partial<TServerContext & ServerAdapterInitialContext>[]): Promise<void>;
53
+ handle(requestLike: RequestLike, ...ctx: Partial<TServerContext & ServerAdapterInitialContext>[]): MaybePromise<Response>;
54
+ handle(request: Request, ...ctx: Partial<TServerContext & ServerAdapterInitialContext>[]): MaybePromise<Response>;
55
+ handle(request: Request, ...ctx: Partial<TServerContext>[]): MaybePromise<Response>;
56
+ handle(fetchEvent: FetchEvent & Partial<TServerContext & ServerAdapterInitialContext>, ...ctx: Partial<TServerContext>[]): void;
57
+ handle(res: UWSResponse, req: UWSRequest, ...ctx: Partial<TServerContext & ServerAdapterInitialContext>[]): Promise<void>;
58
+ handle(container: {
59
+ request: Request;
60
+ } & Partial<TServerContext & ServerAdapterInitialContext>, ...ctx: Partial<TServerContext & ServerAdapterInitialContext>[]): MaybePromise<Response>;
61
+ disposableStack: AsyncDisposableStack;
62
+ dispose(): Promise<void> | void;
63
+ /**
64
+ * Register a promise that should be waited in the background for before the server process is exited.
65
+ *
66
+ * This also avoids unhandled promise rejections, which would otherwise cause the process to exit on some environment like Node.
67
+ * @param promise The promise to wait for
68
+ * @returns
69
+ */
70
+ waitUntil: WaitUntilFn;
71
+ }
72
+ export interface RequestLike {
73
+ url: string;
74
+ method: string;
75
+ headers: HeadersLike;
76
+ body?: AsyncIterable<Uint8Array> | null;
77
+ }
78
+ export interface HeadersLike extends Iterable<[string, string]> {
79
+ get(name: string): string | null | undefined;
80
+ set(name: string, value: string): void;
81
+ has(name: string): boolean;
82
+ }
83
+ export type ServerAdapter<TServerContext, TBaseObject extends ServerAdapterBaseObject<TServerContext>> = TBaseObject & ServerAdapterObject<TServerContext>['handle'] & ServerAdapterObject<TServerContext>;
84
+ export type ServerAdapterRequestHandler<TServerContext> = (request: Request, ctx: TServerContext & ServerAdapterInitialContext) => MaybePromise<Response>;
85
+ export type ServerAdapterNodeContext = {
86
+ req: NodeRequest;
87
+ res: NodeResponse;
88
+ };
89
+ export type WaitUntilFn = (promise: Promise<void> | void) => void;
90
+ export type FetchAPI = ReturnType<typeof import('@whatwg-node/fetch').createFetch>;
91
+ export type ServerAdapterInitialContext = {
92
+ /**
93
+ * Register a promise that should be waited in the background for before the server process is exited.
94
+ *
95
+ * This also avoids unhandled promise rejections, which would otherwise cause the process to exit on some environment like Node.
96
+ * @param promise The promise to wait for
97
+ * @returns
98
+ */
99
+ waitUntil: WaitUntilFn;
100
+ };