@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,270 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isPromise = isPromise;
|
|
4
|
+
exports.isActualPromise = isActualPromise;
|
|
5
|
+
exports.handleMaybePromise = handleMaybePromise;
|
|
6
|
+
exports.fakePromise = fakePromise;
|
|
7
|
+
exports.createDeferredPromise = createDeferredPromise;
|
|
8
|
+
exports.iterateAsync = iterateAsync;
|
|
9
|
+
exports.iterateAsyncVoid = iterateAsync;
|
|
10
|
+
exports.fakeRejectPromise = fakeRejectPromise;
|
|
11
|
+
exports.mapMaybePromise = mapMaybePromise;
|
|
12
|
+
exports.mapAsyncIterator = mapAsyncIterator;
|
|
13
|
+
exports.promiseLikeFinally = promiseLikeFinally;
|
|
14
|
+
exports.unfakePromise = unfakePromise;
|
|
15
|
+
const kFakePromise = Symbol.for('@whatwg-node/promise-helpers/FakePromise');
|
|
16
|
+
function isPromise(value) {
|
|
17
|
+
return value?.then != null;
|
|
18
|
+
}
|
|
19
|
+
function isActualPromise(value) {
|
|
20
|
+
const maybePromise = value;
|
|
21
|
+
return maybePromise && maybePromise.then && maybePromise.catch && maybePromise.finally;
|
|
22
|
+
}
|
|
23
|
+
function handleMaybePromise(inputFactory, outputSuccessFactory, outputErrorFactory, finallyFactory) {
|
|
24
|
+
let result$ = fakePromise().then(inputFactory).then(outputSuccessFactory, outputErrorFactory);
|
|
25
|
+
if (finallyFactory) {
|
|
26
|
+
result$ = result$.finally(finallyFactory);
|
|
27
|
+
}
|
|
28
|
+
return unfakePromise(result$);
|
|
29
|
+
}
|
|
30
|
+
function fakePromise(value) {
|
|
31
|
+
if (value && isActualPromise(value)) {
|
|
32
|
+
return value;
|
|
33
|
+
}
|
|
34
|
+
if (isPromise(value)) {
|
|
35
|
+
return {
|
|
36
|
+
then: (resolve, reject) => fakePromise(value.then(resolve, reject)),
|
|
37
|
+
catch: reject => fakePromise(value.then(res => res, reject)),
|
|
38
|
+
finally: cb => fakePromise(cb ? promiseLikeFinally(value, cb) : value),
|
|
39
|
+
[Symbol.toStringTag]: 'Promise',
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
// Write a fake promise to avoid the promise constructor
|
|
43
|
+
// being called with `new Promise` in the browser.
|
|
44
|
+
return {
|
|
45
|
+
then(resolve) {
|
|
46
|
+
if (resolve) {
|
|
47
|
+
try {
|
|
48
|
+
return fakePromise(resolve(value));
|
|
49
|
+
}
|
|
50
|
+
catch (err) {
|
|
51
|
+
return fakeRejectPromise(err);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return this;
|
|
55
|
+
},
|
|
56
|
+
catch() {
|
|
57
|
+
return this;
|
|
58
|
+
},
|
|
59
|
+
finally(cb) {
|
|
60
|
+
if (cb) {
|
|
61
|
+
try {
|
|
62
|
+
return fakePromise(cb()).then(() => value, () => value);
|
|
63
|
+
}
|
|
64
|
+
catch (err) {
|
|
65
|
+
return fakeRejectPromise(err);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return this;
|
|
69
|
+
},
|
|
70
|
+
[Symbol.toStringTag]: 'Promise',
|
|
71
|
+
__fakePromiseValue: value,
|
|
72
|
+
[kFakePromise]: 'resolved',
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
function createDeferredPromise() {
|
|
76
|
+
if (Promise.withResolvers) {
|
|
77
|
+
return Promise.withResolvers();
|
|
78
|
+
}
|
|
79
|
+
let resolveFn;
|
|
80
|
+
let rejectFn;
|
|
81
|
+
const promise = new Promise(function deferredPromiseExecutor(resolve, reject) {
|
|
82
|
+
resolveFn = resolve;
|
|
83
|
+
rejectFn = reject;
|
|
84
|
+
});
|
|
85
|
+
return {
|
|
86
|
+
promise,
|
|
87
|
+
get resolve() {
|
|
88
|
+
return resolveFn;
|
|
89
|
+
},
|
|
90
|
+
get reject() {
|
|
91
|
+
return rejectFn;
|
|
92
|
+
},
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
function iterateAsync(iterable, callback, results) {
|
|
96
|
+
if (iterable?.length === 0) {
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
const iterator = iterable[Symbol.iterator]();
|
|
100
|
+
let index = 0;
|
|
101
|
+
function iterate() {
|
|
102
|
+
const { done: endOfIterator, value } = iterator.next();
|
|
103
|
+
if (endOfIterator) {
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
let endedEarly = false;
|
|
107
|
+
function endEarly() {
|
|
108
|
+
endedEarly = true;
|
|
109
|
+
}
|
|
110
|
+
return handleMaybePromise(function handleCallback() {
|
|
111
|
+
return callback(value, endEarly, index++);
|
|
112
|
+
}, function handleCallbackResult(result) {
|
|
113
|
+
if (result) {
|
|
114
|
+
results?.push(result);
|
|
115
|
+
}
|
|
116
|
+
if (endedEarly) {
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
return iterate();
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
return iterate();
|
|
123
|
+
}
|
|
124
|
+
function fakeRejectPromise(error) {
|
|
125
|
+
return {
|
|
126
|
+
then(_resolve, reject) {
|
|
127
|
+
if (reject) {
|
|
128
|
+
try {
|
|
129
|
+
return fakePromise(reject(error));
|
|
130
|
+
}
|
|
131
|
+
catch (err) {
|
|
132
|
+
return fakeRejectPromise(err);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return this;
|
|
136
|
+
},
|
|
137
|
+
catch(reject) {
|
|
138
|
+
if (reject) {
|
|
139
|
+
try {
|
|
140
|
+
return fakePromise(reject(error));
|
|
141
|
+
}
|
|
142
|
+
catch (err) {
|
|
143
|
+
return fakeRejectPromise(err);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return this;
|
|
147
|
+
},
|
|
148
|
+
finally(cb) {
|
|
149
|
+
if (cb) {
|
|
150
|
+
try {
|
|
151
|
+
cb();
|
|
152
|
+
}
|
|
153
|
+
catch (err) {
|
|
154
|
+
return fakeRejectPromise(err);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
return this;
|
|
158
|
+
},
|
|
159
|
+
__fakeRejectError: error,
|
|
160
|
+
[Symbol.toStringTag]: 'Promise',
|
|
161
|
+
[kFakePromise]: 'rejected',
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
function mapMaybePromise(input, onSuccess, onError) {
|
|
165
|
+
return handleMaybePromise(() => input, onSuccess, onError);
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Given an AsyncIterable and a callback function, return an AsyncIterator
|
|
169
|
+
* which produces values mapped via calling the callback function.
|
|
170
|
+
*/
|
|
171
|
+
function mapAsyncIterator(iterator, onNext, onError, onEnd) {
|
|
172
|
+
if (Symbol.asyncIterator in iterator) {
|
|
173
|
+
iterator = iterator[Symbol.asyncIterator]();
|
|
174
|
+
}
|
|
175
|
+
let $return;
|
|
176
|
+
let abruptClose;
|
|
177
|
+
let onEndWithValue;
|
|
178
|
+
if (onEnd) {
|
|
179
|
+
let onEndWithValueResult /** R in onEndWithValue */;
|
|
180
|
+
onEndWithValue = value => {
|
|
181
|
+
onEndWithValueResult ||= handleMaybePromise(onEnd, () => value, () => value);
|
|
182
|
+
return onEndWithValueResult;
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
if (typeof iterator.return === 'function') {
|
|
186
|
+
$return = iterator.return;
|
|
187
|
+
abruptClose = (error) => {
|
|
188
|
+
const rethrow = () => {
|
|
189
|
+
throw error;
|
|
190
|
+
};
|
|
191
|
+
return $return.call(iterator).then(rethrow, rethrow);
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
function mapResult(result) {
|
|
195
|
+
if (result.done) {
|
|
196
|
+
return onEndWithValue ? onEndWithValue(result) : result;
|
|
197
|
+
}
|
|
198
|
+
return handleMaybePromise(() => result.value, value => handleMaybePromise(() => onNext(value), iteratorResult, abruptClose));
|
|
199
|
+
}
|
|
200
|
+
let mapReject;
|
|
201
|
+
if (onError) {
|
|
202
|
+
let onErrorResult;
|
|
203
|
+
// Capture rejectCallback to ensure it cannot be null.
|
|
204
|
+
const reject = onError;
|
|
205
|
+
mapReject = (error) => {
|
|
206
|
+
onErrorResult ||= handleMaybePromise(() => error, error => handleMaybePromise(() => reject(error), iteratorResult, abruptClose));
|
|
207
|
+
return onErrorResult;
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
return {
|
|
211
|
+
next() {
|
|
212
|
+
return iterator.next().then(mapResult, mapReject);
|
|
213
|
+
},
|
|
214
|
+
return() {
|
|
215
|
+
const res$ = $return
|
|
216
|
+
? $return.call(iterator).then(mapResult, mapReject)
|
|
217
|
+
: fakePromise({ value: undefined, done: true });
|
|
218
|
+
return onEndWithValue ? res$.then(onEndWithValue) : res$;
|
|
219
|
+
},
|
|
220
|
+
throw(error) {
|
|
221
|
+
if (typeof iterator.throw === 'function') {
|
|
222
|
+
return iterator.throw(error).then(mapResult, mapReject);
|
|
223
|
+
}
|
|
224
|
+
if (abruptClose) {
|
|
225
|
+
return abruptClose(error);
|
|
226
|
+
}
|
|
227
|
+
return fakeRejectPromise(error);
|
|
228
|
+
},
|
|
229
|
+
[Symbol.asyncIterator]() {
|
|
230
|
+
return this;
|
|
231
|
+
},
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
function iteratorResult(value) {
|
|
235
|
+
return { value, done: false };
|
|
236
|
+
}
|
|
237
|
+
function isFakePromise(value) {
|
|
238
|
+
return value?.[kFakePromise] === 'resolved';
|
|
239
|
+
}
|
|
240
|
+
function isFakeRejectPromise(value) {
|
|
241
|
+
return value?.[kFakePromise] === 'rejected';
|
|
242
|
+
}
|
|
243
|
+
function promiseLikeFinally(value, onFinally) {
|
|
244
|
+
if ('finally' in value) {
|
|
245
|
+
return value.finally(onFinally);
|
|
246
|
+
}
|
|
247
|
+
return value.then(res => {
|
|
248
|
+
const finallyRes = onFinally();
|
|
249
|
+
return isPromise(finallyRes) ? finallyRes.then(() => res) : res;
|
|
250
|
+
}, err => {
|
|
251
|
+
const finallyRes = onFinally();
|
|
252
|
+
if (isPromise(finallyRes)) {
|
|
253
|
+
return finallyRes.then(() => {
|
|
254
|
+
throw err;
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
else {
|
|
258
|
+
throw err;
|
|
259
|
+
}
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
function unfakePromise(promise) {
|
|
263
|
+
if (isFakePromise(promise)) {
|
|
264
|
+
return promise.__fakePromiseValue;
|
|
265
|
+
}
|
|
266
|
+
if (isFakeRejectPromise(promise)) {
|
|
267
|
+
throw promise.__fakeRejectError;
|
|
268
|
+
}
|
|
269
|
+
return promise;
|
|
270
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"commonjs"}
|
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
const kFakePromise = Symbol.for('@whatwg-node/promise-helpers/FakePromise');
|
|
2
|
+
export function isPromise(value) {
|
|
3
|
+
return value?.then != null;
|
|
4
|
+
}
|
|
5
|
+
export function isActualPromise(value) {
|
|
6
|
+
const maybePromise = value;
|
|
7
|
+
return maybePromise && maybePromise.then && maybePromise.catch && maybePromise.finally;
|
|
8
|
+
}
|
|
9
|
+
export function handleMaybePromise(inputFactory, outputSuccessFactory, outputErrorFactory, finallyFactory) {
|
|
10
|
+
let result$ = fakePromise().then(inputFactory).then(outputSuccessFactory, outputErrorFactory);
|
|
11
|
+
if (finallyFactory) {
|
|
12
|
+
result$ = result$.finally(finallyFactory);
|
|
13
|
+
}
|
|
14
|
+
return unfakePromise(result$);
|
|
15
|
+
}
|
|
16
|
+
export function fakePromise(value) {
|
|
17
|
+
if (value && isActualPromise(value)) {
|
|
18
|
+
return value;
|
|
19
|
+
}
|
|
20
|
+
if (isPromise(value)) {
|
|
21
|
+
return {
|
|
22
|
+
then: (resolve, reject) => fakePromise(value.then(resolve, reject)),
|
|
23
|
+
catch: reject => fakePromise(value.then(res => res, reject)),
|
|
24
|
+
finally: cb => fakePromise(cb ? promiseLikeFinally(value, cb) : value),
|
|
25
|
+
[Symbol.toStringTag]: 'Promise',
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
// Write a fake promise to avoid the promise constructor
|
|
29
|
+
// being called with `new Promise` in the browser.
|
|
30
|
+
return {
|
|
31
|
+
then(resolve) {
|
|
32
|
+
if (resolve) {
|
|
33
|
+
try {
|
|
34
|
+
return fakePromise(resolve(value));
|
|
35
|
+
}
|
|
36
|
+
catch (err) {
|
|
37
|
+
return fakeRejectPromise(err);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return this;
|
|
41
|
+
},
|
|
42
|
+
catch() {
|
|
43
|
+
return this;
|
|
44
|
+
},
|
|
45
|
+
finally(cb) {
|
|
46
|
+
if (cb) {
|
|
47
|
+
try {
|
|
48
|
+
return fakePromise(cb()).then(() => value, () => value);
|
|
49
|
+
}
|
|
50
|
+
catch (err) {
|
|
51
|
+
return fakeRejectPromise(err);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return this;
|
|
55
|
+
},
|
|
56
|
+
[Symbol.toStringTag]: 'Promise',
|
|
57
|
+
__fakePromiseValue: value,
|
|
58
|
+
[kFakePromise]: 'resolved',
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
export function createDeferredPromise() {
|
|
62
|
+
if (Promise.withResolvers) {
|
|
63
|
+
return Promise.withResolvers();
|
|
64
|
+
}
|
|
65
|
+
let resolveFn;
|
|
66
|
+
let rejectFn;
|
|
67
|
+
const promise = new Promise(function deferredPromiseExecutor(resolve, reject) {
|
|
68
|
+
resolveFn = resolve;
|
|
69
|
+
rejectFn = reject;
|
|
70
|
+
});
|
|
71
|
+
return {
|
|
72
|
+
promise,
|
|
73
|
+
get resolve() {
|
|
74
|
+
return resolveFn;
|
|
75
|
+
},
|
|
76
|
+
get reject() {
|
|
77
|
+
return rejectFn;
|
|
78
|
+
},
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
export { iterateAsync as iterateAsyncVoid };
|
|
82
|
+
export function iterateAsync(iterable, callback, results) {
|
|
83
|
+
if (iterable?.length === 0) {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
const iterator = iterable[Symbol.iterator]();
|
|
87
|
+
let index = 0;
|
|
88
|
+
function iterate() {
|
|
89
|
+
const { done: endOfIterator, value } = iterator.next();
|
|
90
|
+
if (endOfIterator) {
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
let endedEarly = false;
|
|
94
|
+
function endEarly() {
|
|
95
|
+
endedEarly = true;
|
|
96
|
+
}
|
|
97
|
+
return handleMaybePromise(function handleCallback() {
|
|
98
|
+
return callback(value, endEarly, index++);
|
|
99
|
+
}, function handleCallbackResult(result) {
|
|
100
|
+
if (result) {
|
|
101
|
+
results?.push(result);
|
|
102
|
+
}
|
|
103
|
+
if (endedEarly) {
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
return iterate();
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
return iterate();
|
|
110
|
+
}
|
|
111
|
+
export function fakeRejectPromise(error) {
|
|
112
|
+
return {
|
|
113
|
+
then(_resolve, reject) {
|
|
114
|
+
if (reject) {
|
|
115
|
+
try {
|
|
116
|
+
return fakePromise(reject(error));
|
|
117
|
+
}
|
|
118
|
+
catch (err) {
|
|
119
|
+
return fakeRejectPromise(err);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return this;
|
|
123
|
+
},
|
|
124
|
+
catch(reject) {
|
|
125
|
+
if (reject) {
|
|
126
|
+
try {
|
|
127
|
+
return fakePromise(reject(error));
|
|
128
|
+
}
|
|
129
|
+
catch (err) {
|
|
130
|
+
return fakeRejectPromise(err);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return this;
|
|
134
|
+
},
|
|
135
|
+
finally(cb) {
|
|
136
|
+
if (cb) {
|
|
137
|
+
try {
|
|
138
|
+
cb();
|
|
139
|
+
}
|
|
140
|
+
catch (err) {
|
|
141
|
+
return fakeRejectPromise(err);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return this;
|
|
145
|
+
},
|
|
146
|
+
__fakeRejectError: error,
|
|
147
|
+
[Symbol.toStringTag]: 'Promise',
|
|
148
|
+
[kFakePromise]: 'rejected',
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
export function mapMaybePromise(input, onSuccess, onError) {
|
|
152
|
+
return handleMaybePromise(() => input, onSuccess, onError);
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Given an AsyncIterable and a callback function, return an AsyncIterator
|
|
156
|
+
* which produces values mapped via calling the callback function.
|
|
157
|
+
*/
|
|
158
|
+
export function mapAsyncIterator(iterator, onNext, onError, onEnd) {
|
|
159
|
+
if (Symbol.asyncIterator in iterator) {
|
|
160
|
+
iterator = iterator[Symbol.asyncIterator]();
|
|
161
|
+
}
|
|
162
|
+
let $return;
|
|
163
|
+
let abruptClose;
|
|
164
|
+
let onEndWithValue;
|
|
165
|
+
if (onEnd) {
|
|
166
|
+
let onEndWithValueResult /** R in onEndWithValue */;
|
|
167
|
+
onEndWithValue = value => {
|
|
168
|
+
onEndWithValueResult ||= handleMaybePromise(onEnd, () => value, () => value);
|
|
169
|
+
return onEndWithValueResult;
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
if (typeof iterator.return === 'function') {
|
|
173
|
+
$return = iterator.return;
|
|
174
|
+
abruptClose = (error) => {
|
|
175
|
+
const rethrow = () => {
|
|
176
|
+
throw error;
|
|
177
|
+
};
|
|
178
|
+
return $return.call(iterator).then(rethrow, rethrow);
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
function mapResult(result) {
|
|
182
|
+
if (result.done) {
|
|
183
|
+
return onEndWithValue ? onEndWithValue(result) : result;
|
|
184
|
+
}
|
|
185
|
+
return handleMaybePromise(() => result.value, value => handleMaybePromise(() => onNext(value), iteratorResult, abruptClose));
|
|
186
|
+
}
|
|
187
|
+
let mapReject;
|
|
188
|
+
if (onError) {
|
|
189
|
+
let onErrorResult;
|
|
190
|
+
// Capture rejectCallback to ensure it cannot be null.
|
|
191
|
+
const reject = onError;
|
|
192
|
+
mapReject = (error) => {
|
|
193
|
+
onErrorResult ||= handleMaybePromise(() => error, error => handleMaybePromise(() => reject(error), iteratorResult, abruptClose));
|
|
194
|
+
return onErrorResult;
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
return {
|
|
198
|
+
next() {
|
|
199
|
+
return iterator.next().then(mapResult, mapReject);
|
|
200
|
+
},
|
|
201
|
+
return() {
|
|
202
|
+
const res$ = $return
|
|
203
|
+
? $return.call(iterator).then(mapResult, mapReject)
|
|
204
|
+
: fakePromise({ value: undefined, done: true });
|
|
205
|
+
return onEndWithValue ? res$.then(onEndWithValue) : res$;
|
|
206
|
+
},
|
|
207
|
+
throw(error) {
|
|
208
|
+
if (typeof iterator.throw === 'function') {
|
|
209
|
+
return iterator.throw(error).then(mapResult, mapReject);
|
|
210
|
+
}
|
|
211
|
+
if (abruptClose) {
|
|
212
|
+
return abruptClose(error);
|
|
213
|
+
}
|
|
214
|
+
return fakeRejectPromise(error);
|
|
215
|
+
},
|
|
216
|
+
[Symbol.asyncIterator]() {
|
|
217
|
+
return this;
|
|
218
|
+
},
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
function iteratorResult(value) {
|
|
222
|
+
return { value, done: false };
|
|
223
|
+
}
|
|
224
|
+
function isFakePromise(value) {
|
|
225
|
+
return value?.[kFakePromise] === 'resolved';
|
|
226
|
+
}
|
|
227
|
+
function isFakeRejectPromise(value) {
|
|
228
|
+
return value?.[kFakePromise] === 'rejected';
|
|
229
|
+
}
|
|
230
|
+
export function promiseLikeFinally(value, onFinally) {
|
|
231
|
+
if ('finally' in value) {
|
|
232
|
+
return value.finally(onFinally);
|
|
233
|
+
}
|
|
234
|
+
return value.then(res => {
|
|
235
|
+
const finallyRes = onFinally();
|
|
236
|
+
return isPromise(finallyRes) ? finallyRes.then(() => res) : res;
|
|
237
|
+
}, err => {
|
|
238
|
+
const finallyRes = onFinally();
|
|
239
|
+
if (isPromise(finallyRes)) {
|
|
240
|
+
return finallyRes.then(() => {
|
|
241
|
+
throw err;
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
else {
|
|
245
|
+
throw err;
|
|
246
|
+
}
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
export function unfakePromise(promise) {
|
|
250
|
+
if (isFakePromise(promise)) {
|
|
251
|
+
return promise.__fakePromiseValue;
|
|
252
|
+
}
|
|
253
|
+
if (isFakeRejectPromise(promise)) {
|
|
254
|
+
throw promise.__fakeRejectError;
|
|
255
|
+
}
|
|
256
|
+
return promise;
|
|
257
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@whatwg-node/promise-helpers",
|
|
3
|
+
"version": "1.3.2",
|
|
4
|
+
"description": "Promise helpers",
|
|
5
|
+
"sideEffects": false,
|
|
6
|
+
"dependencies": {
|
|
7
|
+
"tslib": "^2.6.3"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "ardatan/whatwg-node",
|
|
12
|
+
"directory": "packages/promise-helpers"
|
|
13
|
+
},
|
|
14
|
+
"author": "Arda TANRIKULU <ardatanrikulu@gmail.com>",
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"engines": {
|
|
17
|
+
"node": ">=16.0.0"
|
|
18
|
+
},
|
|
19
|
+
"main": "cjs/index.js",
|
|
20
|
+
"module": "esm/index.js",
|
|
21
|
+
"typings": "typings/index.d.ts",
|
|
22
|
+
"typescript": {
|
|
23
|
+
"definition": "typings/index.d.ts"
|
|
24
|
+
},
|
|
25
|
+
"type": "module",
|
|
26
|
+
"exports": {
|
|
27
|
+
".": {
|
|
28
|
+
"require": {
|
|
29
|
+
"types": "./typings/index.d.cts",
|
|
30
|
+
"default": "./cjs/index.js"
|
|
31
|
+
},
|
|
32
|
+
"import": {
|
|
33
|
+
"types": "./typings/index.d.ts",
|
|
34
|
+
"default": "./esm/index.js"
|
|
35
|
+
},
|
|
36
|
+
"default": {
|
|
37
|
+
"types": "./typings/index.d.ts",
|
|
38
|
+
"default": "./esm/index.js"
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
"./package.json": "./package.json"
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export type MaybePromise<T> = Promise<T> | T;
|
|
2
|
+
export type MaybePromiseLike<T> = PromiseLike<T> | T;
|
|
3
|
+
export declare function isPromise<T>(value: MaybePromise<T>): value is Promise<T>;
|
|
4
|
+
export declare function isPromise<T>(value: MaybePromiseLike<T>): value is PromiseLike<T>;
|
|
5
|
+
export declare function isActualPromise<T>(value: MaybePromiseLike<T>): value is Promise<T>;
|
|
6
|
+
export declare function handleMaybePromise<TInput, TOutput>(inputFactory: () => MaybePromise<TInput>, outputSuccessFactory: (value: TInput) => MaybePromise<TOutput>, outputErrorFactory?: (err: any) => MaybePromise<TOutput>, finallyFactory?: () => MaybePromise<void>): MaybePromise<TOutput>;
|
|
7
|
+
export declare function handleMaybePromise<TInput, TOutput>(inputFactory: () => MaybePromiseLike<TInput>, outputSuccessFactory: (value: TInput) => MaybePromiseLike<TOutput>, outputErrorFactory?: (err: any) => MaybePromiseLike<TOutput>, finallyFactory?: () => MaybePromiseLike<void>): MaybePromiseLike<TOutput>;
|
|
8
|
+
export declare function fakePromise<T>(value: MaybePromise<T>): Promise<T>;
|
|
9
|
+
export declare function fakePromise<T>(value: MaybePromiseLike<T>): Promise<T>;
|
|
10
|
+
export declare function fakePromise(value: void): Promise<void>;
|
|
11
|
+
export interface DeferredPromise<T = void> {
|
|
12
|
+
promise: Promise<T>;
|
|
13
|
+
resolve: (value: T) => void;
|
|
14
|
+
reject: (reason: any) => void;
|
|
15
|
+
}
|
|
16
|
+
export declare function createDeferredPromise<T = void>(): DeferredPromise<T>;
|
|
17
|
+
export { iterateAsync as iterateAsyncVoid };
|
|
18
|
+
export declare function iterateAsync<TInput, TOutput>(iterable: Iterable<TInput>, callback: (input: TInput, endEarly: () => void, index: number) => MaybePromise<TOutput | undefined | null | void>, results?: TOutput[]): MaybePromise<void>;
|
|
19
|
+
export declare function fakeRejectPromise<T>(error: unknown): Promise<T>;
|
|
20
|
+
/**
|
|
21
|
+
* @deprecated Use `handleMaybePromise` instead.
|
|
22
|
+
*/
|
|
23
|
+
export declare function mapMaybePromise<TInput, TOutput>(input: MaybePromise<TInput>, onSuccess: (value: TInput) => MaybePromise<TOutput>, onError?: (err: any) => MaybePromise<TOutput>): MaybePromise<TOutput>;
|
|
24
|
+
export declare function mapMaybePromise<TInput, TOutput>(input: MaybePromiseLike<TInput>, onSuccess: (value: TInput) => MaybePromiseLike<TOutput>, onError?: (err: any) => MaybePromiseLike<TOutput>): MaybePromiseLike<TOutput>;
|
|
25
|
+
/**
|
|
26
|
+
* Given an AsyncIterable and a callback function, return an AsyncIterator
|
|
27
|
+
* which produces values mapped via calling the callback function.
|
|
28
|
+
*/
|
|
29
|
+
export declare function mapAsyncIterator<T, U>(iterator: AsyncIterable<T> | AsyncIterator<T>, onNext: (value: T) => MaybePromise<U>, onError?: any, onEnd?: () => MaybePromise<void>): AsyncIterableIterator<U>;
|
|
30
|
+
export declare function promiseLikeFinally<T>(value: PromiseLike<T> | Promise<T>, onFinally: () => MaybePromiseLike<void>): PromiseLike<T>;
|
|
31
|
+
export declare function unfakePromise<T>(promise: Promise<T>): MaybePromise<T>;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export type MaybePromise<T> = Promise<T> | T;
|
|
2
|
+
export type MaybePromiseLike<T> = PromiseLike<T> | T;
|
|
3
|
+
export declare function isPromise<T>(value: MaybePromise<T>): value is Promise<T>;
|
|
4
|
+
export declare function isPromise<T>(value: MaybePromiseLike<T>): value is PromiseLike<T>;
|
|
5
|
+
export declare function isActualPromise<T>(value: MaybePromiseLike<T>): value is Promise<T>;
|
|
6
|
+
export declare function handleMaybePromise<TInput, TOutput>(inputFactory: () => MaybePromise<TInput>, outputSuccessFactory: (value: TInput) => MaybePromise<TOutput>, outputErrorFactory?: (err: any) => MaybePromise<TOutput>, finallyFactory?: () => MaybePromise<void>): MaybePromise<TOutput>;
|
|
7
|
+
export declare function handleMaybePromise<TInput, TOutput>(inputFactory: () => MaybePromiseLike<TInput>, outputSuccessFactory: (value: TInput) => MaybePromiseLike<TOutput>, outputErrorFactory?: (err: any) => MaybePromiseLike<TOutput>, finallyFactory?: () => MaybePromiseLike<void>): MaybePromiseLike<TOutput>;
|
|
8
|
+
export declare function fakePromise<T>(value: MaybePromise<T>): Promise<T>;
|
|
9
|
+
export declare function fakePromise<T>(value: MaybePromiseLike<T>): Promise<T>;
|
|
10
|
+
export declare function fakePromise(value: void): Promise<void>;
|
|
11
|
+
export interface DeferredPromise<T = void> {
|
|
12
|
+
promise: Promise<T>;
|
|
13
|
+
resolve: (value: T) => void;
|
|
14
|
+
reject: (reason: any) => void;
|
|
15
|
+
}
|
|
16
|
+
export declare function createDeferredPromise<T = void>(): DeferredPromise<T>;
|
|
17
|
+
export { iterateAsync as iterateAsyncVoid };
|
|
18
|
+
export declare function iterateAsync<TInput, TOutput>(iterable: Iterable<TInput>, callback: (input: TInput, endEarly: () => void, index: number) => MaybePromise<TOutput | undefined | null | void>, results?: TOutput[]): MaybePromise<void>;
|
|
19
|
+
export declare function fakeRejectPromise<T>(error: unknown): Promise<T>;
|
|
20
|
+
/**
|
|
21
|
+
* @deprecated Use `handleMaybePromise` instead.
|
|
22
|
+
*/
|
|
23
|
+
export declare function mapMaybePromise<TInput, TOutput>(input: MaybePromise<TInput>, onSuccess: (value: TInput) => MaybePromise<TOutput>, onError?: (err: any) => MaybePromise<TOutput>): MaybePromise<TOutput>;
|
|
24
|
+
export declare function mapMaybePromise<TInput, TOutput>(input: MaybePromiseLike<TInput>, onSuccess: (value: TInput) => MaybePromiseLike<TOutput>, onError?: (err: any) => MaybePromiseLike<TOutput>): MaybePromiseLike<TOutput>;
|
|
25
|
+
/**
|
|
26
|
+
* Given an AsyncIterable and a callback function, return an AsyncIterator
|
|
27
|
+
* which produces values mapped via calling the callback function.
|
|
28
|
+
*/
|
|
29
|
+
export declare function mapAsyncIterator<T, U>(iterator: AsyncIterable<T> | AsyncIterator<T>, onNext: (value: T) => MaybePromise<U>, onError?: any, onEnd?: () => MaybePromise<void>): AsyncIterableIterator<U>;
|
|
30
|
+
export declare function promiseLikeFinally<T>(value: PromiseLike<T> | Promise<T>, onFinally: () => MaybePromiseLike<void>): PromiseLike<T>;
|
|
31
|
+
export declare function unfakePromise<T>(promise: Promise<T>): MaybePromise<T>;
|