@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.
- package/README.md +610 -0
- package/dist/context.d.ts +46 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +181 -0
- package/dist/index.d.ts +25 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +112 -0
- package/dist/lib/constants.d.ts +7 -0
- package/dist/lib/constants.d.ts.map +1 -0
- package/dist/lib/constants.js +6 -0
- package/dist/lib/download-request.d.ts +17 -0
- package/dist/lib/download-request.d.ts.map +1 -0
- package/dist/lib/download-request.js +113 -0
- package/dist/lib/errors.d.ts +88 -0
- package/dist/lib/errors.d.ts.map +1 -0
- package/dist/lib/errors.js +158 -0
- package/dist/lib/event-stream-response.d.ts +17 -0
- package/dist/lib/event-stream-response.d.ts.map +1 -0
- package/dist/lib/event-stream-response.js +39 -0
- package/dist/lib/event-target.d.ts +9 -0
- package/dist/lib/event-target.d.ts.map +1 -0
- package/dist/lib/event-target.js +4 -0
- package/dist/lib/fetch-api.d.ts +3 -0
- package/dist/lib/fetch-api.d.ts.map +1 -0
- package/dist/lib/fetch-api.js +16 -0
- package/dist/lib/map-share.d.ts +52 -0
- package/dist/lib/map-share.d.ts.map +1 -0
- package/dist/lib/map-share.js +142 -0
- package/dist/lib/secret-stream-fetch.d.ts +7 -0
- package/dist/lib/secret-stream-fetch.d.ts.map +1 -0
- package/dist/lib/secret-stream-fetch.js +34 -0
- package/dist/lib/self-evicting-map.d.ts +16 -0
- package/dist/lib/self-evicting-map.d.ts.map +1 -0
- package/dist/lib/self-evicting-map.js +29 -0
- package/dist/lib/state-update-event.d.ts +8 -0
- package/dist/lib/state-update-event.d.ts.map +1 -0
- package/dist/lib/state-update-event.js +10 -0
- package/dist/lib/utils.d.ts +32 -0
- package/dist/lib/utils.d.ts.map +1 -0
- package/dist/lib/utils.js +96 -0
- package/dist/middlewares/localhost-only.d.ts +11 -0
- package/dist/middlewares/localhost-only.d.ts.map +1 -0
- package/dist/middlewares/localhost-only.js +10 -0
- package/dist/middlewares/parse-request.d.ts +11 -0
- package/dist/middlewares/parse-request.d.ts.map +1 -0
- package/dist/middlewares/parse-request.js +25 -0
- package/dist/routes/downloads.d.ts +15 -0
- package/dist/routes/downloads.d.ts.map +1 -0
- package/dist/routes/downloads.js +60 -0
- package/dist/routes/map-shares.d.ts +19 -0
- package/dist/routes/map-shares.d.ts.map +1 -0
- package/dist/routes/map-shares.js +192 -0
- package/dist/routes/maps.d.ts +6 -0
- package/dist/routes/maps.d.ts.map +1 -0
- package/dist/routes/maps.js +118 -0
- package/dist/routes/root.d.ts +6 -0
- package/dist/routes/root.d.ts.map +1 -0
- package/dist/routes/root.js +29 -0
- package/dist/types.d.ts +110 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +96 -0
- package/node_modules/@envelop/instrumentation/LICENSE +21 -0
- package/node_modules/@envelop/instrumentation/README.md +30 -0
- package/node_modules/@envelop/instrumentation/cjs/index.js +5 -0
- package/node_modules/@envelop/instrumentation/cjs/instrumentation.js +89 -0
- package/node_modules/@envelop/instrumentation/cjs/package.json +1 -0
- package/node_modules/@envelop/instrumentation/esm/index.js +2 -0
- package/node_modules/@envelop/instrumentation/esm/instrumentation.js +82 -0
- package/node_modules/@envelop/instrumentation/package.json +57 -0
- package/node_modules/@envelop/instrumentation/typings/index.d.cts +1 -0
- package/node_modules/@envelop/instrumentation/typings/index.d.ts +1 -0
- package/node_modules/@envelop/instrumentation/typings/instrumentation.d.cts +44 -0
- package/node_modules/@envelop/instrumentation/typings/instrumentation.d.ts +44 -0
- package/node_modules/@whatwg-node/disposablestack/cjs/AsyncDisposableStack.js +73 -0
- package/node_modules/@whatwg-node/disposablestack/cjs/DisposableStack.js +62 -0
- package/node_modules/@whatwg-node/disposablestack/cjs/SupressedError.js +16 -0
- package/node_modules/@whatwg-node/disposablestack/cjs/index.js +11 -0
- package/node_modules/@whatwg-node/disposablestack/cjs/package.json +1 -0
- package/node_modules/@whatwg-node/disposablestack/cjs/symbols.js +20 -0
- package/node_modules/@whatwg-node/disposablestack/cjs/utils.js +11 -0
- package/node_modules/@whatwg-node/disposablestack/esm/AsyncDisposableStack.js +69 -0
- package/node_modules/@whatwg-node/disposablestack/esm/DisposableStack.js +58 -0
- package/node_modules/@whatwg-node/disposablestack/esm/SupressedError.js +12 -0
- package/node_modules/@whatwg-node/disposablestack/esm/index.js +7 -0
- package/node_modules/@whatwg-node/disposablestack/esm/symbols.js +16 -0
- package/node_modules/@whatwg-node/disposablestack/esm/utils.js +7 -0
- package/node_modules/@whatwg-node/disposablestack/package.json +44 -0
- package/node_modules/@whatwg-node/disposablestack/typings/AsyncDisposableStack.d.cts +15 -0
- package/node_modules/@whatwg-node/disposablestack/typings/AsyncDisposableStack.d.ts +15 -0
- package/node_modules/@whatwg-node/disposablestack/typings/DisposableStack.d.cts +14 -0
- package/node_modules/@whatwg-node/disposablestack/typings/DisposableStack.d.ts +14 -0
- package/node_modules/@whatwg-node/disposablestack/typings/SupressedError.d.cts +5 -0
- package/node_modules/@whatwg-node/disposablestack/typings/SupressedError.d.ts +5 -0
- package/node_modules/@whatwg-node/disposablestack/typings/index.d.cts +4 -0
- package/node_modules/@whatwg-node/disposablestack/typings/index.d.ts +4 -0
- package/node_modules/@whatwg-node/disposablestack/typings/symbols.d.cts +5 -0
- package/node_modules/@whatwg-node/disposablestack/typings/symbols.d.ts +5 -0
- package/node_modules/@whatwg-node/disposablestack/typings/utils.d.cts +2 -0
- package/node_modules/@whatwg-node/disposablestack/typings/utils.d.ts +2 -0
- package/node_modules/@whatwg-node/promise-helpers/cjs/index.js +270 -0
- package/node_modules/@whatwg-node/promise-helpers/cjs/package.json +1 -0
- package/node_modules/@whatwg-node/promise-helpers/esm/index.js +257 -0
- package/node_modules/@whatwg-node/promise-helpers/package.json +43 -0
- package/node_modules/@whatwg-node/promise-helpers/typings/index.d.cts +31 -0
- package/node_modules/@whatwg-node/promise-helpers/typings/index.d.ts +31 -0
- package/node_modules/@whatwg-node/server/README.md +590 -0
- package/node_modules/@whatwg-node/server/cjs/createServerAdapter.js +368 -0
- package/node_modules/@whatwg-node/server/cjs/index.js +17 -0
- package/node_modules/@whatwg-node/server/cjs/package.json +1 -0
- package/node_modules/@whatwg-node/server/cjs/plugins/types.js +0 -0
- package/node_modules/@whatwg-node/server/cjs/plugins/useContentEncoding.js +73 -0
- package/node_modules/@whatwg-node/server/cjs/plugins/useCors.js +124 -0
- package/node_modules/@whatwg-node/server/cjs/plugins/useErrorHandling.js +52 -0
- package/node_modules/@whatwg-node/server/cjs/types.js +0 -0
- package/node_modules/@whatwg-node/server/cjs/utils.js +599 -0
- package/node_modules/@whatwg-node/server/cjs/uwebsockets.js +241 -0
- package/node_modules/@whatwg-node/server/esm/createServerAdapter.js +365 -0
- package/node_modules/@whatwg-node/server/esm/index.js +11 -0
- package/node_modules/@whatwg-node/server/esm/plugins/types.js +0 -0
- package/node_modules/@whatwg-node/server/esm/plugins/useContentEncoding.js +70 -0
- package/node_modules/@whatwg-node/server/esm/plugins/useCors.js +120 -0
- package/node_modules/@whatwg-node/server/esm/plugins/useErrorHandling.js +46 -0
- package/node_modules/@whatwg-node/server/esm/types.js +0 -0
- package/node_modules/@whatwg-node/server/esm/utils.js +588 -0
- package/node_modules/@whatwg-node/server/esm/uwebsockets.js +234 -0
- package/node_modules/@whatwg-node/server/package.json +46 -0
- package/node_modules/@whatwg-node/server/typings/createServerAdapter.d.cts +19 -0
- package/node_modules/@whatwg-node/server/typings/createServerAdapter.d.ts +19 -0
- package/node_modules/@whatwg-node/server/typings/index.d.cts +11 -0
- package/node_modules/@whatwg-node/server/typings/index.d.ts +11 -0
- package/node_modules/@whatwg-node/server/typings/plugins/types.d.cts +76 -0
- package/node_modules/@whatwg-node/server/typings/plugins/types.d.ts +76 -0
- package/node_modules/@whatwg-node/server/typings/plugins/useContentEncoding.d.cts +2 -0
- package/node_modules/@whatwg-node/server/typings/plugins/useContentEncoding.d.ts +2 -0
- package/node_modules/@whatwg-node/server/typings/plugins/useCors.d.cts +14 -0
- package/node_modules/@whatwg-node/server/typings/plugins/useCors.d.ts +14 -0
- package/node_modules/@whatwg-node/server/typings/plugins/useErrorHandling.d.cts +13 -0
- package/node_modules/@whatwg-node/server/typings/plugins/useErrorHandling.d.ts +13 -0
- package/node_modules/@whatwg-node/server/typings/types.d.cts +100 -0
- package/node_modules/@whatwg-node/server/typings/types.d.ts +100 -0
- package/node_modules/@whatwg-node/server/typings/utils.d.cts +42 -0
- package/node_modules/@whatwg-node/server/typings/utils.d.ts +42 -0
- package/node_modules/@whatwg-node/server/typings/uwebsockets.d.cts +32 -0
- package/node_modules/@whatwg-node/server/typings/uwebsockets.d.ts +32 -0
- package/node_modules/tslib/CopyrightNotice.txt +15 -0
- package/node_modules/tslib/LICENSE.txt +12 -0
- package/node_modules/tslib/README.md +164 -0
- package/node_modules/tslib/SECURITY.md +41 -0
- package/node_modules/tslib/modules/index.d.ts +38 -0
- package/node_modules/tslib/modules/index.js +70 -0
- package/node_modules/tslib/modules/package.json +3 -0
- package/node_modules/tslib/package.json +47 -0
- package/node_modules/tslib/tslib.d.ts +460 -0
- package/node_modules/tslib/tslib.es6.html +1 -0
- package/node_modules/tslib/tslib.es6.js +402 -0
- package/node_modules/tslib/tslib.es6.mjs +401 -0
- package/node_modules/tslib/tslib.html +1 -0
- package/node_modules/tslib/tslib.js +484 -0
- package/package.json +87 -0
- package/src/context.ts +203 -0
- package/src/index.ts +193 -0
- package/src/lib/constants.ts +6 -0
- package/src/lib/download-request.ts +142 -0
- package/src/lib/errors.ts +187 -0
- package/src/lib/event-stream-response.ts +57 -0
- package/src/lib/event-target.ts +11 -0
- package/src/lib/fetch-api.ts +18 -0
- package/src/lib/map-share.ts +185 -0
- package/src/lib/secret-stream-fetch.ts +42 -0
- package/src/lib/self-evicting-map.ts +35 -0
- package/src/lib/state-update-event.ts +14 -0
- package/src/lib/utils.ts +110 -0
- package/src/middlewares/localhost-only.ts +16 -0
- package/src/middlewares/parse-request.ts +34 -0
- package/src/routes/downloads.ts +92 -0
- package/src/routes/map-shares.ts +246 -0
- package/src/routes/maps.ts +146 -0
- package/src/routes/root.ts +37 -0
- package/src/types.ts +152 -0
|
@@ -0,0 +1,368 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createServerAdapter = createServerAdapter;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const instrumentation_1 = require("@envelop/instrumentation");
|
|
6
|
+
const disposablestack_1 = require("@whatwg-node/disposablestack");
|
|
7
|
+
const promise_helpers_1 = require("@whatwg-node/promise-helpers");
|
|
8
|
+
const utils_js_1 = require("./utils.js");
|
|
9
|
+
const uwebsockets_js_1 = require("./uwebsockets.js");
|
|
10
|
+
// Required for envs like nextjs edge runtime
|
|
11
|
+
function isRequestAccessible(serverContext) {
|
|
12
|
+
try {
|
|
13
|
+
return !!serverContext?.request;
|
|
14
|
+
}
|
|
15
|
+
catch {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
const EMPTY_OBJECT = {};
|
|
20
|
+
function createServerAdapter(serverAdapterBaseObject, options) {
|
|
21
|
+
const useSingleWriteHead = options?.__useSingleWriteHead == null ? true : options.__useSingleWriteHead;
|
|
22
|
+
const fetchAPI = {
|
|
23
|
+
...options?.fetchAPI,
|
|
24
|
+
};
|
|
25
|
+
const useCustomAbortCtrl = options?.__useCustomAbortCtrl == null
|
|
26
|
+
? fetchAPI.Request !== globalThis.Request
|
|
27
|
+
: options.__useCustomAbortCtrl;
|
|
28
|
+
const givenHandleRequest = typeof serverAdapterBaseObject === 'function'
|
|
29
|
+
? serverAdapterBaseObject
|
|
30
|
+
: serverAdapterBaseObject.handle;
|
|
31
|
+
const onRequestHooks = [];
|
|
32
|
+
const onResponseHooks = [];
|
|
33
|
+
let instrumentation;
|
|
34
|
+
const waitUntilPromises = new Set();
|
|
35
|
+
let _disposableStack;
|
|
36
|
+
function ensureDisposableStack() {
|
|
37
|
+
if (!_disposableStack) {
|
|
38
|
+
_disposableStack = new disposablestack_1.AsyncDisposableStack();
|
|
39
|
+
if (options?.disposeOnProcessTerminate) {
|
|
40
|
+
(0, utils_js_1.ensureDisposableStackRegisteredForTerminateEvents)(_disposableStack);
|
|
41
|
+
}
|
|
42
|
+
_disposableStack.defer(() => {
|
|
43
|
+
if (waitUntilPromises.size > 0) {
|
|
44
|
+
return Promise.allSettled(waitUntilPromises).then(() => {
|
|
45
|
+
waitUntilPromises.clear();
|
|
46
|
+
}, () => {
|
|
47
|
+
waitUntilPromises.clear();
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
return _disposableStack;
|
|
53
|
+
}
|
|
54
|
+
function waitUntil(maybePromise) {
|
|
55
|
+
// Ensure that the disposable stack is created
|
|
56
|
+
if ((0, utils_js_1.isPromise)(maybePromise)) {
|
|
57
|
+
ensureDisposableStack();
|
|
58
|
+
waitUntilPromises.add(maybePromise);
|
|
59
|
+
maybePromise.then(() => {
|
|
60
|
+
waitUntilPromises.delete(maybePromise);
|
|
61
|
+
}, err => {
|
|
62
|
+
console.error(`Unexpected error while waiting: ${err.message || err}`);
|
|
63
|
+
waitUntilPromises.delete(maybePromise);
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
if (options?.plugins != null) {
|
|
68
|
+
for (const plugin of options.plugins) {
|
|
69
|
+
if (plugin.instrumentation) {
|
|
70
|
+
instrumentation = instrumentation
|
|
71
|
+
? (0, instrumentation_1.chain)(instrumentation, plugin.instrumentation)
|
|
72
|
+
: plugin.instrumentation;
|
|
73
|
+
}
|
|
74
|
+
if (plugin.onRequest) {
|
|
75
|
+
onRequestHooks.push(plugin.onRequest);
|
|
76
|
+
}
|
|
77
|
+
if (plugin.onResponse) {
|
|
78
|
+
onResponseHooks.push(plugin.onResponse);
|
|
79
|
+
}
|
|
80
|
+
const disposeFn = plugin[disposablestack_1.DisposableSymbols.dispose];
|
|
81
|
+
if (disposeFn) {
|
|
82
|
+
ensureDisposableStack().defer(disposeFn);
|
|
83
|
+
}
|
|
84
|
+
const asyncDisposeFn = plugin[disposablestack_1.DisposableSymbols.asyncDispose];
|
|
85
|
+
if (asyncDisposeFn) {
|
|
86
|
+
ensureDisposableStack().defer(asyncDisposeFn);
|
|
87
|
+
}
|
|
88
|
+
if (plugin.onDispose) {
|
|
89
|
+
ensureDisposableStack().defer(plugin.onDispose);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
let handleRequest = onRequestHooks.length > 0 || onResponseHooks.length > 0
|
|
94
|
+
? function handleRequest(request, serverContext) {
|
|
95
|
+
let requestHandler = givenHandleRequest;
|
|
96
|
+
let response;
|
|
97
|
+
if (onRequestHooks.length === 0) {
|
|
98
|
+
return handleEarlyResponse();
|
|
99
|
+
}
|
|
100
|
+
let url = request['parsedUrl'] ||
|
|
101
|
+
new Proxy(EMPTY_OBJECT, {
|
|
102
|
+
get(_target, prop, _receiver) {
|
|
103
|
+
url = new fetchAPI.URL(request.url, 'http://localhost');
|
|
104
|
+
return Reflect.get(url, prop, url);
|
|
105
|
+
},
|
|
106
|
+
});
|
|
107
|
+
function handleResponse(response) {
|
|
108
|
+
if (onResponseHooks.length === 0) {
|
|
109
|
+
return response;
|
|
110
|
+
}
|
|
111
|
+
return (0, promise_helpers_1.handleMaybePromise)(() => (0, utils_js_1.iterateAsyncVoid)(onResponseHooks, onResponseHook => onResponseHook({
|
|
112
|
+
request,
|
|
113
|
+
response,
|
|
114
|
+
serverContext,
|
|
115
|
+
setResponse(newResponse) {
|
|
116
|
+
response = newResponse;
|
|
117
|
+
},
|
|
118
|
+
fetchAPI,
|
|
119
|
+
})), () => response);
|
|
120
|
+
}
|
|
121
|
+
function handleEarlyResponse() {
|
|
122
|
+
if (!response) {
|
|
123
|
+
return (0, promise_helpers_1.handleMaybePromise)(() => requestHandler(request, serverContext), handleResponse);
|
|
124
|
+
}
|
|
125
|
+
return handleResponse(response);
|
|
126
|
+
}
|
|
127
|
+
return (0, promise_helpers_1.handleMaybePromise)(() => (0, utils_js_1.iterateAsyncVoid)(onRequestHooks, (onRequestHook, stopEarly) => onRequestHook({
|
|
128
|
+
request,
|
|
129
|
+
setRequest(newRequest) {
|
|
130
|
+
request = newRequest;
|
|
131
|
+
},
|
|
132
|
+
serverContext,
|
|
133
|
+
fetchAPI,
|
|
134
|
+
url,
|
|
135
|
+
requestHandler,
|
|
136
|
+
setRequestHandler(newRequestHandler) {
|
|
137
|
+
requestHandler = newRequestHandler;
|
|
138
|
+
},
|
|
139
|
+
endResponse(newResponse) {
|
|
140
|
+
response = newResponse;
|
|
141
|
+
if (newResponse) {
|
|
142
|
+
stopEarly();
|
|
143
|
+
}
|
|
144
|
+
},
|
|
145
|
+
})), handleEarlyResponse);
|
|
146
|
+
}
|
|
147
|
+
: givenHandleRequest;
|
|
148
|
+
if (instrumentation?.request) {
|
|
149
|
+
const originalRequestHandler = handleRequest;
|
|
150
|
+
handleRequest = (request, initialContext) => {
|
|
151
|
+
return (0, instrumentation_1.getInstrumented)({ request }).asyncFn(instrumentation.request, originalRequestHandler)(request, initialContext);
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
// TODO: Remove this on the next major version
|
|
155
|
+
function handleNodeRequest(nodeRequest, ...ctx) {
|
|
156
|
+
const serverContext = ctx.length > 1 ? (0, utils_js_1.completeAssign)(...ctx) : ctx[0] || {};
|
|
157
|
+
// Ensure `waitUntil` is available in the server context
|
|
158
|
+
if (!serverContext.waitUntil) {
|
|
159
|
+
serverContext.waitUntil = waitUntil;
|
|
160
|
+
}
|
|
161
|
+
const request = (0, utils_js_1.normalizeNodeRequest)(nodeRequest, fetchAPI, undefined, useCustomAbortCtrl);
|
|
162
|
+
return handleRequest(request, serverContext);
|
|
163
|
+
}
|
|
164
|
+
function handleNodeRequestAndResponse(nodeRequest, nodeResponseOrContainer, ...ctx) {
|
|
165
|
+
const nodeResponse = nodeResponseOrContainer.raw || nodeResponseOrContainer;
|
|
166
|
+
const serverContext = ctx.length > 1 ? (0, utils_js_1.completeAssign)(...ctx) : ctx[0] || {};
|
|
167
|
+
// Ensure `waitUntil` is available in the server context
|
|
168
|
+
if (!serverContext.waitUntil) {
|
|
169
|
+
serverContext.waitUntil = waitUntil;
|
|
170
|
+
}
|
|
171
|
+
const request = (0, utils_js_1.normalizeNodeRequest)(nodeRequest, fetchAPI, nodeResponse, useCustomAbortCtrl);
|
|
172
|
+
return handleRequest(request, serverContext);
|
|
173
|
+
}
|
|
174
|
+
function requestListener(nodeRequest, nodeResponse, ...ctx) {
|
|
175
|
+
const defaultServerContext = {
|
|
176
|
+
req: nodeRequest,
|
|
177
|
+
res: nodeResponse,
|
|
178
|
+
waitUntil,
|
|
179
|
+
};
|
|
180
|
+
return (0, promise_helpers_1.unfakePromise)((0, uwebsockets_js_1.fakePromise)()
|
|
181
|
+
.then(() => handleNodeRequestAndResponse(nodeRequest, nodeResponse, defaultServerContext, ...ctx))
|
|
182
|
+
.catch(err => (0, utils_js_1.handleErrorFromRequestHandler)(err, fetchAPI.Response))
|
|
183
|
+
.then(response => (0, utils_js_1.sendNodeResponse)(response, nodeResponse, nodeRequest, useSingleWriteHead))
|
|
184
|
+
.catch(err => console.error(`Unexpected error while handling request: ${err.message || err}`)));
|
|
185
|
+
}
|
|
186
|
+
function handleUWS(res, req, ...ctx) {
|
|
187
|
+
const defaultServerContext = {
|
|
188
|
+
res,
|
|
189
|
+
req,
|
|
190
|
+
waitUntil,
|
|
191
|
+
};
|
|
192
|
+
const filteredCtxParts = ctx.filter(partCtx => partCtx != null);
|
|
193
|
+
const serverContext = filteredCtxParts.length > 0
|
|
194
|
+
? (0, utils_js_1.completeAssign)(defaultServerContext, ...ctx)
|
|
195
|
+
: defaultServerContext;
|
|
196
|
+
const controller = useCustomAbortCtrl
|
|
197
|
+
? (0, utils_js_1.createCustomAbortControllerSignal)()
|
|
198
|
+
: new AbortController();
|
|
199
|
+
const originalResEnd = res.end.bind(res);
|
|
200
|
+
let resEnded = false;
|
|
201
|
+
res.end = function (data) {
|
|
202
|
+
resEnded = true;
|
|
203
|
+
return originalResEnd(data);
|
|
204
|
+
};
|
|
205
|
+
const originalOnAborted = res.onAborted.bind(res);
|
|
206
|
+
originalOnAborted(function () {
|
|
207
|
+
controller.abort();
|
|
208
|
+
});
|
|
209
|
+
res.onAborted = function (cb) {
|
|
210
|
+
controller.signal.addEventListener('abort', cb, { once: true });
|
|
211
|
+
};
|
|
212
|
+
const request = (0, uwebsockets_js_1.getRequestFromUWSRequest)({
|
|
213
|
+
req,
|
|
214
|
+
res,
|
|
215
|
+
fetchAPI,
|
|
216
|
+
controller,
|
|
217
|
+
});
|
|
218
|
+
return (0, promise_helpers_1.handleMaybePromise)(() => (0, promise_helpers_1.handleMaybePromise)(() => handleRequest(request, serverContext), response => response, err => (0, utils_js_1.handleErrorFromRequestHandler)(err, fetchAPI.Response)), response => {
|
|
219
|
+
if (!controller.signal.aborted && !resEnded) {
|
|
220
|
+
return (0, promise_helpers_1.handleMaybePromise)(() => (0, uwebsockets_js_1.sendResponseToUwsOpts)(res, response, controller, fetchAPI), r => r, err => {
|
|
221
|
+
console.error(`Unexpected error while handling request: ${err.message || err}`);
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
function handleEvent(event, ...ctx) {
|
|
227
|
+
if (!event.respondWith || !event.request) {
|
|
228
|
+
throw new TypeError(`Expected FetchEvent, got ${event}`);
|
|
229
|
+
}
|
|
230
|
+
const filteredCtxParts = ctx.filter(partCtx => partCtx != null);
|
|
231
|
+
const serverContext = filteredCtxParts.length > 0
|
|
232
|
+
? (0, utils_js_1.completeAssign)({}, event, ...filteredCtxParts)
|
|
233
|
+
: (0, utils_js_1.isolateObject)(event);
|
|
234
|
+
const response$ = handleRequest(event.request, serverContext);
|
|
235
|
+
event.respondWith(response$);
|
|
236
|
+
}
|
|
237
|
+
function handleRequestWithWaitUntil(request, ...ctx) {
|
|
238
|
+
const filteredCtxParts = ctx.filter(partCtx => partCtx != null);
|
|
239
|
+
const serverContext = filteredCtxParts.length > 1
|
|
240
|
+
? (0, utils_js_1.completeAssign)({}, ...filteredCtxParts)
|
|
241
|
+
: (0, utils_js_1.isolateObject)(filteredCtxParts[0], filteredCtxParts[0] == null || filteredCtxParts[0].waitUntil == null
|
|
242
|
+
? waitUntil
|
|
243
|
+
: undefined);
|
|
244
|
+
return handleRequest(request, serverContext);
|
|
245
|
+
}
|
|
246
|
+
const fetchFn = (input, ...maybeCtx) => {
|
|
247
|
+
if (typeof input === 'string' || 'href' in input) {
|
|
248
|
+
const [initOrCtx, ...restOfCtx] = maybeCtx;
|
|
249
|
+
if ((0, utils_js_1.isRequestInit)(initOrCtx)) {
|
|
250
|
+
const request = new fetchAPI.Request(input, initOrCtx);
|
|
251
|
+
const res$ = handleRequestWithWaitUntil(request, ...restOfCtx);
|
|
252
|
+
const signal = initOrCtx.signal;
|
|
253
|
+
if (signal) {
|
|
254
|
+
return (0, utils_js_1.handleAbortSignalAndPromiseResponse)(res$, signal);
|
|
255
|
+
}
|
|
256
|
+
return res$;
|
|
257
|
+
}
|
|
258
|
+
const request = new fetchAPI.Request(input);
|
|
259
|
+
return handleRequestWithWaitUntil(request, ...maybeCtx);
|
|
260
|
+
}
|
|
261
|
+
const res$ = handleRequestWithWaitUntil(input, ...maybeCtx);
|
|
262
|
+
return (0, utils_js_1.handleAbortSignalAndPromiseResponse)(res$, input.signal);
|
|
263
|
+
};
|
|
264
|
+
const genericRequestHandler = (input, ...maybeCtx) => {
|
|
265
|
+
// If it is a Node request
|
|
266
|
+
const [initOrCtxOrRes, ...restOfCtx] = maybeCtx;
|
|
267
|
+
if ((0, utils_js_1.isNodeRequest)(input)) {
|
|
268
|
+
if (!(0, utils_js_1.isServerResponse)(initOrCtxOrRes)) {
|
|
269
|
+
throw new TypeError(`Expected ServerResponse, got ${initOrCtxOrRes}`);
|
|
270
|
+
}
|
|
271
|
+
return requestListener(input, initOrCtxOrRes, ...restOfCtx);
|
|
272
|
+
}
|
|
273
|
+
if ((0, uwebsockets_js_1.isUWSResponse)(input)) {
|
|
274
|
+
return handleUWS(input, initOrCtxOrRes, ...restOfCtx);
|
|
275
|
+
}
|
|
276
|
+
if ((0, utils_js_1.isServerResponse)(initOrCtxOrRes)) {
|
|
277
|
+
throw new TypeError('Got Node response without Node request');
|
|
278
|
+
}
|
|
279
|
+
// Is input a container object over Request?
|
|
280
|
+
if (isRequestAccessible(input)) {
|
|
281
|
+
// Is it FetchEvent?
|
|
282
|
+
if ((0, utils_js_1.isFetchEvent)(input)) {
|
|
283
|
+
return handleEvent(input, ...maybeCtx);
|
|
284
|
+
}
|
|
285
|
+
// In this input is also the context
|
|
286
|
+
return handleRequestWithWaitUntil(input.request, input, ...maybeCtx);
|
|
287
|
+
}
|
|
288
|
+
// Or is it Request itself?
|
|
289
|
+
// Then ctx is present and it is the context
|
|
290
|
+
return fetchFn(input, ...maybeCtx);
|
|
291
|
+
};
|
|
292
|
+
const adapterObj = {
|
|
293
|
+
handleRequest: handleRequestWithWaitUntil,
|
|
294
|
+
fetch: fetchFn,
|
|
295
|
+
handleNodeRequest,
|
|
296
|
+
handleNodeRequestAndResponse,
|
|
297
|
+
requestListener,
|
|
298
|
+
handleEvent,
|
|
299
|
+
handleUWS,
|
|
300
|
+
handle: genericRequestHandler,
|
|
301
|
+
get disposableStack() {
|
|
302
|
+
return ensureDisposableStack();
|
|
303
|
+
},
|
|
304
|
+
[disposablestack_1.DisposableSymbols.asyncDispose]() {
|
|
305
|
+
if (_disposableStack && !_disposableStack.disposed) {
|
|
306
|
+
return _disposableStack.disposeAsync();
|
|
307
|
+
}
|
|
308
|
+
return (0, uwebsockets_js_1.fakePromise)();
|
|
309
|
+
},
|
|
310
|
+
dispose() {
|
|
311
|
+
if (_disposableStack && !_disposableStack.disposed) {
|
|
312
|
+
return _disposableStack.disposeAsync();
|
|
313
|
+
}
|
|
314
|
+
return (0, uwebsockets_js_1.fakePromise)();
|
|
315
|
+
},
|
|
316
|
+
waitUntil,
|
|
317
|
+
};
|
|
318
|
+
const serverAdapter = new Proxy(genericRequestHandler, {
|
|
319
|
+
// It should have all the attributes of the handler function and the server instance
|
|
320
|
+
has: (_, prop) => {
|
|
321
|
+
return (prop in adapterObj ||
|
|
322
|
+
prop in genericRequestHandler ||
|
|
323
|
+
(serverAdapterBaseObject && prop in serverAdapterBaseObject));
|
|
324
|
+
},
|
|
325
|
+
get: (_, prop) => {
|
|
326
|
+
// Somehow Deno and Node 24 don't like bound dispose functions
|
|
327
|
+
if (globalThis.Deno || prop === Symbol.asyncDispose || prop === Symbol.dispose) {
|
|
328
|
+
const adapterProp = Reflect.get(adapterObj, prop, adapterObj);
|
|
329
|
+
if (adapterProp) {
|
|
330
|
+
return adapterProp;
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
const adapterProp = adapterObj[prop];
|
|
334
|
+
if (adapterProp) {
|
|
335
|
+
if (adapterProp.bind) {
|
|
336
|
+
return adapterProp.bind(adapterObj);
|
|
337
|
+
}
|
|
338
|
+
return adapterProp;
|
|
339
|
+
}
|
|
340
|
+
const handleProp = genericRequestHandler[prop];
|
|
341
|
+
if (handleProp) {
|
|
342
|
+
if (handleProp.bind) {
|
|
343
|
+
return handleProp.bind(genericRequestHandler);
|
|
344
|
+
}
|
|
345
|
+
return handleProp;
|
|
346
|
+
}
|
|
347
|
+
if (serverAdapterBaseObject) {
|
|
348
|
+
const serverAdapterBaseObjectProp = serverAdapterBaseObject[prop];
|
|
349
|
+
if (serverAdapterBaseObjectProp) {
|
|
350
|
+
if (serverAdapterBaseObjectProp.bind) {
|
|
351
|
+
return function (...args) {
|
|
352
|
+
const returnedVal = serverAdapterBaseObject[prop](...args);
|
|
353
|
+
if (returnedVal === serverAdapterBaseObject) {
|
|
354
|
+
return serverAdapter;
|
|
355
|
+
}
|
|
356
|
+
return returnedVal;
|
|
357
|
+
};
|
|
358
|
+
}
|
|
359
|
+
return serverAdapterBaseObjectProp;
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
},
|
|
363
|
+
apply(_, __, args) {
|
|
364
|
+
return genericRequestHandler(...args);
|
|
365
|
+
},
|
|
366
|
+
});
|
|
367
|
+
return serverAdapter;
|
|
368
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DisposableSymbols = exports.Response = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
tslib_1.__exportStar(require("./createServerAdapter.js"), exports);
|
|
6
|
+
tslib_1.__exportStar(require("./types.js"), exports);
|
|
7
|
+
tslib_1.__exportStar(require("./utils.js"), exports);
|
|
8
|
+
tslib_1.__exportStar(require("./plugins/types.js"), exports);
|
|
9
|
+
tslib_1.__exportStar(require("./plugins/useCors.js"), exports);
|
|
10
|
+
tslib_1.__exportStar(require("./plugins/useErrorHandling.js"), exports);
|
|
11
|
+
tslib_1.__exportStar(require("./plugins/useContentEncoding.js"), exports);
|
|
12
|
+
tslib_1.__exportStar(require("./uwebsockets.js"), exports);
|
|
13
|
+
var fetch_1 = require("@whatwg-node/fetch");
|
|
14
|
+
Object.defineProperty(exports, "Response", { enumerable: true, get: function () { return fetch_1.Response; } });
|
|
15
|
+
var disposablestack_1 = require("@whatwg-node/disposablestack");
|
|
16
|
+
Object.defineProperty(exports, "DisposableSymbols", { enumerable: true, get: function () { return disposablestack_1.DisposableSymbols; } });
|
|
17
|
+
tslib_1.__exportStar(require("@envelop/instrumentation"), exports);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"commonjs"}
|
|
File without changes
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useContentEncoding = useContentEncoding;
|
|
4
|
+
const utils_js_1 = require("../utils.js");
|
|
5
|
+
const emptyEncodings = ['none', 'identity'];
|
|
6
|
+
function useContentEncoding() {
|
|
7
|
+
return {
|
|
8
|
+
onRequest({ request, setRequest, fetchAPI, endResponse }) {
|
|
9
|
+
const contentEncodingHeader = request.headers.get('content-encoding');
|
|
10
|
+
if (contentEncodingHeader &&
|
|
11
|
+
contentEncodingHeader !== 'none' &&
|
|
12
|
+
contentEncodingHeader !== 'identity' &&
|
|
13
|
+
request.body) {
|
|
14
|
+
const contentEncodings = contentEncodingHeader
|
|
15
|
+
.split(',')
|
|
16
|
+
.filter(encoding => !emptyEncodings.includes(encoding));
|
|
17
|
+
if (contentEncodings.length) {
|
|
18
|
+
if (!contentEncodings.every(encoding => (0, utils_js_1.getSupportedEncodings)(fetchAPI).includes(encoding))) {
|
|
19
|
+
endResponse(new fetchAPI.Response(`Unsupported 'Content-Encoding': ${contentEncodingHeader}`, {
|
|
20
|
+
status: 415,
|
|
21
|
+
statusText: 'Unsupported Media Type',
|
|
22
|
+
}));
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
let newBody = request.body;
|
|
26
|
+
for (const contentEncoding of contentEncodings) {
|
|
27
|
+
newBody = request.body.pipeThrough(new fetchAPI.DecompressionStream(contentEncoding));
|
|
28
|
+
}
|
|
29
|
+
setRequest(new fetchAPI.Request(request.url, {
|
|
30
|
+
body: newBody,
|
|
31
|
+
cache: request.cache,
|
|
32
|
+
credentials: request.credentials,
|
|
33
|
+
headers: request.headers,
|
|
34
|
+
integrity: request.integrity,
|
|
35
|
+
keepalive: request.keepalive,
|
|
36
|
+
method: request.method,
|
|
37
|
+
mode: request.mode,
|
|
38
|
+
redirect: request.redirect,
|
|
39
|
+
referrer: request.referrer,
|
|
40
|
+
referrerPolicy: request.referrerPolicy,
|
|
41
|
+
signal: request.signal,
|
|
42
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
43
|
+
// @ts-ignore - not in the TS types yet
|
|
44
|
+
duplex: 'half',
|
|
45
|
+
}));
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
onResponse({ request, response, setResponse, fetchAPI }) {
|
|
50
|
+
const acceptEncoding = request.headers.get('accept-encoding');
|
|
51
|
+
if (acceptEncoding) {
|
|
52
|
+
const encodings = acceptEncoding.split(',');
|
|
53
|
+
if (encodings.length && response.body) {
|
|
54
|
+
const supportedEncoding = encodings.find(encoding => (0, utils_js_1.getSupportedEncodings)(fetchAPI).includes(encoding));
|
|
55
|
+
if (supportedEncoding) {
|
|
56
|
+
const compressionStream = new fetchAPI.CompressionStream(supportedEncoding);
|
|
57
|
+
const newHeaders = new fetchAPI.Headers(response.headers);
|
|
58
|
+
newHeaders.set('content-encoding', supportedEncoding);
|
|
59
|
+
newHeaders.delete('content-length');
|
|
60
|
+
const compressedBody = response.body.pipeThrough(compressionStream);
|
|
61
|
+
const compressedResponse = new fetchAPI.Response(compressedBody, {
|
|
62
|
+
status: response.status,
|
|
63
|
+
statusText: response.statusText,
|
|
64
|
+
headers: newHeaders,
|
|
65
|
+
});
|
|
66
|
+
utils_js_1.decompressedResponseMap.set(compressedResponse, response);
|
|
67
|
+
setResponse(compressedResponse);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
|
+
}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getCORSHeadersByRequestAndOptions = getCORSHeadersByRequestAndOptions;
|
|
4
|
+
exports.useCORS = useCORS;
|
|
5
|
+
const promise_helpers_1 = require("@whatwg-node/promise-helpers");
|
|
6
|
+
function getCORSHeadersByRequestAndOptions(request, corsOptions) {
|
|
7
|
+
const currentOrigin = request.headers.get('origin');
|
|
8
|
+
if (corsOptions === false || currentOrigin == null) {
|
|
9
|
+
return null;
|
|
10
|
+
}
|
|
11
|
+
const headers = {};
|
|
12
|
+
// If defined origins have '*' or undefined by any means, we should allow all origins
|
|
13
|
+
if (corsOptions.origin == null ||
|
|
14
|
+
corsOptions.origin.length === 0 ||
|
|
15
|
+
corsOptions.origin.includes('*')) {
|
|
16
|
+
headers['Access-Control-Allow-Origin'] = currentOrigin;
|
|
17
|
+
// Vary by origin because there are multiple origins
|
|
18
|
+
headers['Vary'] = 'Origin';
|
|
19
|
+
}
|
|
20
|
+
else if (typeof corsOptions.origin === 'string') {
|
|
21
|
+
// If there is one specific origin is specified, use it directly
|
|
22
|
+
headers['Access-Control-Allow-Origin'] = corsOptions.origin;
|
|
23
|
+
}
|
|
24
|
+
else if (Array.isArray(corsOptions.origin)) {
|
|
25
|
+
// If there is only one origin defined in the array, consider it as a single one
|
|
26
|
+
if (corsOptions.origin.length === 1) {
|
|
27
|
+
headers['Access-Control-Allow-Origin'] = corsOptions.origin[0];
|
|
28
|
+
}
|
|
29
|
+
else if (corsOptions.origin.includes(currentOrigin)) {
|
|
30
|
+
// If origin is available in the headers, use it
|
|
31
|
+
headers['Access-Control-Allow-Origin'] = currentOrigin;
|
|
32
|
+
// Vary by origin because there are multiple origins
|
|
33
|
+
headers['Vary'] = 'Origin';
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
// There is no origin found in the headers, so we should return null
|
|
37
|
+
headers['Access-Control-Allow-Origin'] = 'null';
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
if (corsOptions.methods?.length) {
|
|
41
|
+
headers['Access-Control-Allow-Methods'] = corsOptions.methods.join(', ');
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
const requestMethod = request.headers.get('access-control-request-method');
|
|
45
|
+
if (requestMethod) {
|
|
46
|
+
headers['Access-Control-Allow-Methods'] = requestMethod;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
if (corsOptions.allowedHeaders?.length) {
|
|
50
|
+
headers['Access-Control-Allow-Headers'] = corsOptions.allowedHeaders.join(', ');
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
const requestHeaders = request.headers.get('access-control-request-headers');
|
|
54
|
+
if (requestHeaders) {
|
|
55
|
+
headers['Access-Control-Allow-Headers'] = requestHeaders;
|
|
56
|
+
if (headers['Vary']) {
|
|
57
|
+
headers['Vary'] += ', Access-Control-Request-Headers';
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
headers['Vary'] = 'Access-Control-Request-Headers';
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
if (corsOptions.credentials != null) {
|
|
65
|
+
if (corsOptions.credentials === true) {
|
|
66
|
+
headers['Access-Control-Allow-Credentials'] = 'true';
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
else if (headers['Access-Control-Allow-Origin'] !== '*') {
|
|
70
|
+
headers['Access-Control-Allow-Credentials'] = 'true';
|
|
71
|
+
}
|
|
72
|
+
if (corsOptions.exposedHeaders) {
|
|
73
|
+
headers['Access-Control-Expose-Headers'] = corsOptions.exposedHeaders.join(', ');
|
|
74
|
+
}
|
|
75
|
+
if (corsOptions.maxAge) {
|
|
76
|
+
headers['Access-Control-Max-Age'] = corsOptions.maxAge.toString();
|
|
77
|
+
}
|
|
78
|
+
return headers;
|
|
79
|
+
}
|
|
80
|
+
function getCORSResponseHeaders(request, corsOptionsFactory, serverContext) {
|
|
81
|
+
return (0, promise_helpers_1.handleMaybePromise)(() => corsOptionsFactory(request, serverContext), corsOptions => getCORSHeadersByRequestAndOptions(request, corsOptions));
|
|
82
|
+
}
|
|
83
|
+
function useCORS(options) {
|
|
84
|
+
let corsOptionsFactory = () => ({});
|
|
85
|
+
if (options != null) {
|
|
86
|
+
if (typeof options === 'function') {
|
|
87
|
+
corsOptionsFactory = options;
|
|
88
|
+
}
|
|
89
|
+
else if (typeof options === 'object') {
|
|
90
|
+
const corsOptions = {
|
|
91
|
+
...options,
|
|
92
|
+
};
|
|
93
|
+
corsOptionsFactory = () => corsOptions;
|
|
94
|
+
}
|
|
95
|
+
else if (options === false) {
|
|
96
|
+
corsOptionsFactory = () => false;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return {
|
|
100
|
+
onRequest({ request, fetchAPI, endResponse }) {
|
|
101
|
+
if (request.method.toUpperCase() === 'OPTIONS') {
|
|
102
|
+
const response = new fetchAPI.Response(null, {
|
|
103
|
+
status: 204,
|
|
104
|
+
// Safari (and potentially other browsers) need content-length 0,
|
|
105
|
+
// for 204 or they just hang waiting for a body
|
|
106
|
+
// see: https://github.com/expressjs/cors/blob/master/lib/index.js#L176
|
|
107
|
+
headers: {
|
|
108
|
+
'Content-Length': '0',
|
|
109
|
+
},
|
|
110
|
+
});
|
|
111
|
+
endResponse(response);
|
|
112
|
+
}
|
|
113
|
+
},
|
|
114
|
+
onResponse({ request, serverContext, response }) {
|
|
115
|
+
return (0, promise_helpers_1.handleMaybePromise)(() => getCORSResponseHeaders(request, corsOptionsFactory, serverContext), headers => {
|
|
116
|
+
if (headers != null) {
|
|
117
|
+
for (const headerName in headers) {
|
|
118
|
+
response.headers.set(headerName, headers[headerName]);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
},
|
|
123
|
+
};
|
|
124
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.HTTPError = void 0;
|
|
4
|
+
exports.createDefaultErrorHandler = createDefaultErrorHandler;
|
|
5
|
+
exports.useErrorHandling = useErrorHandling;
|
|
6
|
+
const fetch_1 = require("@whatwg-node/fetch");
|
|
7
|
+
const promise_helpers_1 = require("@whatwg-node/promise-helpers");
|
|
8
|
+
function createDefaultErrorHandler(ResponseCtor = fetch_1.Response) {
|
|
9
|
+
return function defaultErrorHandler(e) {
|
|
10
|
+
if (e.details || e.status || e.headers || e.name === 'HTTPError') {
|
|
11
|
+
return new ResponseCtor(typeof e.details === 'object' ? JSON.stringify(e.details) : e.message, {
|
|
12
|
+
status: e.status,
|
|
13
|
+
headers: e.headers || {},
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
console.error(e);
|
|
17
|
+
return createDefaultErrorResponse(ResponseCtor);
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
function createDefaultErrorResponse(ResponseCtor) {
|
|
21
|
+
if (ResponseCtor.error) {
|
|
22
|
+
return ResponseCtor.error();
|
|
23
|
+
}
|
|
24
|
+
return new ResponseCtor(null, { status: 500 });
|
|
25
|
+
}
|
|
26
|
+
class HTTPError extends Error {
|
|
27
|
+
status;
|
|
28
|
+
message;
|
|
29
|
+
headers;
|
|
30
|
+
details;
|
|
31
|
+
name = 'HTTPError';
|
|
32
|
+
constructor(status = 500, message, headers = {}, details) {
|
|
33
|
+
super(message);
|
|
34
|
+
this.status = status;
|
|
35
|
+
this.message = message;
|
|
36
|
+
this.headers = headers;
|
|
37
|
+
this.details = details;
|
|
38
|
+
Error.captureStackTrace(this, HTTPError);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
exports.HTTPError = HTTPError;
|
|
42
|
+
function useErrorHandling(onError) {
|
|
43
|
+
return {
|
|
44
|
+
onRequest({ requestHandler, setRequestHandler, fetchAPI }) {
|
|
45
|
+
const errorHandler = onError || createDefaultErrorHandler(fetchAPI.Response);
|
|
46
|
+
setRequestHandler(function handlerWithErrorHandling(request, serverContext) {
|
|
47
|
+
return (0, promise_helpers_1.handleMaybePromise)(() => requestHandler(request, serverContext), response => response, e => errorHandler(e, request, serverContext) ||
|
|
48
|
+
createDefaultErrorResponse(fetchAPI.Response));
|
|
49
|
+
});
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
}
|
|
File without changes
|