gingersnap 0.22.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/_virtual/_tslib.cjs +102 -0
- package/_virtual/_tslib.mjs +93 -0
- package/data/AtomicValue.d.ts +15 -0
- package/data/bus.cjs +179 -0
- package/data/bus.d.ts +72 -0
- package/data/bus.mjs +177 -0
- package/data/decoders/index.d.ts +4 -0
- package/data/decoders/json.cjs +12 -0
- package/data/decoders/json.d.ts +7 -0
- package/data/decoders/json.mjs +10 -0
- package/data/decoders/msgpack.cjs +14 -0
- package/data/decoders/msgpack.d.ts +7 -0
- package/data/decoders/msgpack.mjs +12 -0
- package/data/decoders/protobuf.d.ts +13 -0
- package/data/decoders/string.cjs +12 -0
- package/data/decoders/string.d.ts +7 -0
- package/data/decoders/string.mjs +10 -0
- package/data/decoders/type.d.ts +7 -0
- package/data/decoders.cjs +11 -0
- package/data/decoders.mjs +3 -0
- package/data/model/credentials.cjs +114 -0
- package/data/model/credentials.d.ts +61 -0
- package/data/model/credentials.mjs +109 -0
- package/data/model/extra.cjs +88 -0
- package/data/model/extra.d.ts +5 -0
- package/data/model/extra.mjs +66 -0
- package/data/model/index.d.ts +6 -0
- package/data/model/model.cjs +541 -0
- package/data/model/model.d.ts +207 -0
- package/data/model/model.mjs +518 -0
- package/data/model/plugin.cjs +96 -0
- package/data/model/plugin.d.ts +38 -0
- package/data/model/plugin.mjs +72 -0
- package/data/model/property.cjs +310 -0
- package/data/model/property.d.ts +83 -0
- package/data/model/property.mjs +278 -0
- package/data/model/types.cjs +32 -0
- package/data/model/types.d.ts +28 -0
- package/data/model/types.mjs +32 -0
- package/data/model.cjs +43 -0
- package/data/model.mjs +6 -0
- package/data/signal.cjs +47 -0
- package/data/signal.d.ts +17 -0
- package/data/signal.mjs +45 -0
- package/data/store/index.d.ts +4 -0
- package/data/store/json.cjs +58 -0
- package/data/store/json.d.ts +18 -0
- package/data/store/json.mjs +56 -0
- package/data/store/manager.cjs +206 -0
- package/data/store/manager.d.ts +44 -0
- package/data/store/manager.mjs +203 -0
- package/data/store/web.cjs +92 -0
- package/data/store/web.d.ts +25 -0
- package/data/store/web.mjs +89 -0
- package/data/value.cjs +66 -0
- package/data/value.mjs +64 -0
- package/data-structures/array/CyclicalList.cjs +57 -0
- package/data-structures/array/CyclicalList.d.ts +12 -0
- package/data-structures/array/CyclicalList.mjs +55 -0
- package/data-structures/array/Pair.cjs +30 -0
- package/data-structures/array/Pair.d.ts +17 -0
- package/data-structures/array/Pair.mjs +27 -0
- package/data-structures/array/Stack.cjs +96 -0
- package/data-structures/array/Stack.d.ts +41 -0
- package/data-structures/array/Stack.mjs +94 -0
- package/data-structures/array/Triple.cjs +34 -0
- package/data-structures/array/Triple.d.ts +19 -0
- package/data-structures/array/Triple.mjs +31 -0
- package/data-structures/array/Tuple.d.ts +8 -0
- package/data-structures/array/index.d.ts +5 -0
- package/data-structures/array.cjs +15 -0
- package/data-structures/array.mjs +4 -0
- package/data-structures/object/BufferQueue.cjs +98 -0
- package/data-structures/object/BufferQueue.d.ts +21 -0
- package/data-structures/object/BufferQueue.mjs +77 -0
- package/data-structures/object/CyclicalObject.cjs +155 -0
- package/data-structures/object/CyclicalObject.d.ts +74 -0
- package/data-structures/object/CyclicalObject.mjs +134 -0
- package/data-structures/object/Queue.cjs +178 -0
- package/data-structures/object/Queue.d.ts +31 -0
- package/data-structures/object/Queue.mjs +176 -0
- package/data-structures/object/SimpleQueue.cjs +53 -0
- package/data-structures/object/SimpleQueue.d.ts +13 -0
- package/data-structures/object/SimpleQueue.mjs +51 -0
- package/data-structures/object/TimeableObject.cjs +73 -0
- package/data-structures/object/TimeableObject.d.ts +30 -0
- package/data-structures/object/TimeableObject.mjs +71 -0
- package/data-structures/object/WaitableObject.cjs +61 -0
- package/data-structures/object/WaitableObject.d.ts +29 -0
- package/data-structures/object/WaitableObject.mjs +59 -0
- package/data-structures/object/WatchableObject.cjs +251 -0
- package/data-structures/object/WatchableObject.d.ts +88 -0
- package/data-structures/object/WatchableObject.mjs +230 -0
- package/data-structures/object/index.d.ts +7 -0
- package/data-structures/object.cjs +23 -0
- package/data-structures/object.mjs +7 -0
- package/errors/AbortError.cjs +9 -0
- package/errors/AbortError.d.ts +5 -0
- package/errors/AbortError.mjs +7 -0
- package/errors/CallExecutionError.cjs +13 -0
- package/errors/CallExecutionError.d.ts +7 -0
- package/errors/CallExecutionError.mjs +11 -0
- package/errors/FutureCancelled.cjs +11 -0
- package/errors/FutureCancelled.d.ts +6 -0
- package/errors/FutureCancelled.mjs +9 -0
- package/errors/FutureError.cjs +11 -0
- package/errors/FutureError.d.ts +6 -0
- package/errors/FutureError.mjs +9 -0
- package/errors/IllegalArgumentsError.cjs +13 -0
- package/errors/IllegalArgumentsError.d.ts +7 -0
- package/errors/IllegalArgumentsError.mjs +11 -0
- package/errors/IllegalOperationError.cjs +6 -0
- package/errors/IllegalOperationError.d.ts +2 -0
- package/errors/IllegalOperationError.mjs +4 -0
- package/errors/InvalidType.cjs +6 -0
- package/errors/InvalidType.d.ts +2 -0
- package/errors/InvalidType.mjs +4 -0
- package/errors/InvalidValue.cjs +11 -0
- package/errors/InvalidValue.d.ts +4 -0
- package/errors/InvalidValue.mjs +9 -0
- package/errors/MissingArgumentsError.cjs +13 -0
- package/errors/MissingArgumentsError.d.ts +7 -0
- package/errors/MissingArgumentsError.mjs +11 -0
- package/errors/NetworkError.cjs +13 -0
- package/errors/NetworkError.d.ts +8 -0
- package/errors/NetworkError.mjs +11 -0
- package/errors/NoSuchElement.cjs +6 -0
- package/errors/NoSuchElement.d.ts +2 -0
- package/errors/NoSuchElement.mjs +4 -0
- package/errors/NotImplemented.cjs +9 -0
- package/errors/NotImplemented.d.ts +5 -0
- package/errors/NotImplemented.mjs +7 -0
- package/errors/ParsingError.cjs +10 -0
- package/errors/ParsingError.d.ts +5 -0
- package/errors/ParsingError.mjs +8 -0
- package/errors/QueueEmptyError.cjs +6 -0
- package/errors/QueueEmptyError.d.ts +2 -0
- package/errors/QueueEmptyError.mjs +4 -0
- package/errors/QueueFullError.cjs +6 -0
- package/errors/QueueFullError.d.ts +2 -0
- package/errors/QueueFullError.mjs +4 -0
- package/errors/StackEmptyError.cjs +6 -0
- package/errors/StackEmptyError.d.ts +2 -0
- package/errors/StackEmptyError.mjs +4 -0
- package/errors/StackFullError.cjs +6 -0
- package/errors/StackFullError.d.ts +2 -0
- package/errors/StackFullError.mjs +4 -0
- package/errors/StreamEnded.cjs +6 -0
- package/errors/StreamEnded.d.ts +2 -0
- package/errors/StreamEnded.mjs +4 -0
- package/errors/TimeoutError.cjs +11 -0
- package/errors/TimeoutError.d.ts +6 -0
- package/errors/TimeoutError.mjs +9 -0
- package/errors/ValueDestroyedError.cjs +6 -0
- package/errors/ValueDestroyedError.d.ts +2 -0
- package/errors/ValueDestroyedError.mjs +4 -0
- package/errors/VerificationError.cjs +9 -0
- package/errors/VerificationError.d.ts +5 -0
- package/errors/VerificationError.mjs +7 -0
- package/errors/index.d.ts +18 -0
- package/errors.cjs +41 -0
- package/errors.mjs +18 -0
- package/files.cjs +201 -0
- package/files.d.ts +31 -0
- package/files.mjs +180 -0
- package/functools/index.d.ts +31 -0
- package/functools.cjs +371 -0
- package/functools.mjs +334 -0
- package/future/future.cjs +570 -0
- package/future/future.d.ts +208 -0
- package/future/future.mjs +548 -0
- package/future/group.cjs +180 -0
- package/future/group.d.ts +82 -0
- package/future/group.mjs +159 -0
- package/future/index.d.ts +3 -0
- package/future/result.cjs +19 -0
- package/future/result.d.ts +18 -0
- package/future/result.mjs +17 -0
- package/future.cjs +11 -0
- package/future.mjs +3 -0
- package/managers/context.d.ts +8 -0
- package/managers/index.d.ts +1 -0
- package/managers.cjs +2 -0
- package/managers.mjs +1 -0
- package/mocks.cjs +301 -0
- package/mocks.d.ts +125 -0
- package/mocks.mjs +293 -0
- package/networking/EventSourceService.cjs +104 -0
- package/networking/EventSourceService.d.ts +11 -0
- package/networking/EventSourceService.mjs +83 -0
- package/networking/NetworkService.cjs +394 -0
- package/networking/NetworkService.d.ts +97 -0
- package/networking/NetworkService.mjs +373 -0
- package/networking/SocketService.cjs +218 -0
- package/networking/SocketService.d.ts +23 -0
- package/networking/SocketService.mjs +197 -0
- package/networking/decorators/index.d.ts +4 -0
- package/networking/decorators/options.cjs +289 -0
- package/networking/decorators/options.d.ts +148 -0
- package/networking/decorators/options.mjs +242 -0
- package/networking/decorators/requests.cjs +80 -0
- package/networking/decorators/requests.d.ts +34 -0
- package/networking/decorators/requests.mjs +52 -0
- package/networking/decorators/socket.cjs +81 -0
- package/networking/decorators/socket.d.ts +13 -0
- package/networking/decorators/socket.mjs +51 -0
- package/networking/http.cjs +69 -0
- package/networking/http.d.ts +66 -0
- package/networking/http.mjs +69 -0
- package/networking/index.d.ts +39 -0
- package/networking/request.cjs +18 -0
- package/networking/request.d.ts +9 -0
- package/networking/request.mjs +16 -0
- package/networking/types.cjs +37 -0
- package/networking/types.d.ts +132 -0
- package/networking/types.mjs +35 -0
- package/networking.cjs +91 -0
- package/networking.mjs +33 -0
- package/package.json +1 -0
- package/reflection/injector.cjs +43 -0
- package/reflection/injector.d.ts +3 -0
- package/reflection/injector.mjs +40 -0
- package/socket.cjs +239 -0
- package/socket.d.ts +65 -0
- package/socket.mjs +237 -0
- package/store.cjs +12 -0
- package/store.mjs +3 -0
- package/stream/call.cjs +157 -0
- package/stream/call.d.ts +66 -0
- package/stream/call.mjs +154 -0
- package/stream/collector.cjs +217 -0
- package/stream/collector.d.ts +14 -0
- package/stream/collector.mjs +215 -0
- package/stream/index.d.ts +257 -0
- package/stream/observable.cjs +110 -0
- package/stream/observable.d.ts +65 -0
- package/stream/observable.mjs +106 -0
- package/stream/state.cjs +10 -0
- package/stream/state.d.ts +5 -0
- package/stream/state.mjs +8 -0
- package/stream.cjs +1153 -0
- package/stream.mjs +1132 -0
- package/synchronize.cjs +106 -0
- package/synchronize.d.ts +54 -0
- package/synchronize.mjs +103 -0
- package/typing/types.d.ts +24 -0
- package/typing.cjs +2 -0
- package/typing.mjs +1 -0
- package/utils/parser.cjs +37 -0
- package/utils/parser.d.ts +7 -0
- package/utils/parser.mjs +16 -0
|
@@ -0,0 +1,373 @@
|
|
|
1
|
+
import { __awaiter } from '../_virtual/_tslib.mjs';
|
|
2
|
+
import { Model } from '../data/model/model.mjs';
|
|
3
|
+
import * as R from 'ramda';
|
|
4
|
+
import { CallExecutionError } from '../errors/CallExecutionError.mjs';
|
|
5
|
+
import '../data/model/types.mjs';
|
|
6
|
+
import { Credentials } from '../data/model/credentials.mjs';
|
|
7
|
+
import { ResponseType, BodyType } from './types.mjs';
|
|
8
|
+
import { Call } from '../stream/call.mjs';
|
|
9
|
+
import { THROTTLE_DEFAULT_MS } from './decorators/options.mjs';
|
|
10
|
+
import { CachingMethod } from './decorators/requests.mjs';
|
|
11
|
+
import { HTTPStatus } from './http.mjs';
|
|
12
|
+
import 'uuid';
|
|
13
|
+
import { request } from './request.mjs';
|
|
14
|
+
import { Future } from '../future/future.mjs';
|
|
15
|
+
import { Stream } from '../stream.mjs';
|
|
16
|
+
import hash from 'object-hash';
|
|
17
|
+
import { CacheManager } from '../data/store/manager.mjs';
|
|
18
|
+
import { Collectors } from '../stream/collector.mjs';
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* A Snap Service for managing network requests
|
|
22
|
+
*/
|
|
23
|
+
class NetworkService {
|
|
24
|
+
constructor({ baseUrl, retryLimit, cacheManager } = {}) {
|
|
25
|
+
var _a, _b, _c;
|
|
26
|
+
this.retryLimit = retryLimit;
|
|
27
|
+
this.__internal__ = (_a = this.__internal__) !== null && _a !== void 0 ? _a : { classConfig: {}, methodConfig: {} };
|
|
28
|
+
this.baseUrl = (_c = (_b = this.__internal__.classConfig.baseUrl) !== null && _b !== void 0 ? _b : baseUrl) !== null && _c !== void 0 ? _c : "";
|
|
29
|
+
this.cacheManager = cacheManager !== null && cacheManager !== void 0 ? cacheManager : new CacheManager();
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Converts the given value to a JSON object
|
|
33
|
+
* @param value Any data
|
|
34
|
+
* @private
|
|
35
|
+
*/
|
|
36
|
+
convertToJSON(value) {
|
|
37
|
+
if (value instanceof Model) {
|
|
38
|
+
return value.json();
|
|
39
|
+
}
|
|
40
|
+
else if (value instanceof Array || Array.isArray(value)) {
|
|
41
|
+
return `[${value.map((v) => this.convertToJSON(v))}]`;
|
|
42
|
+
}
|
|
43
|
+
return typeof value === "string" ? value : JSON.stringify(value);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Internal method called to wrap all network request methods with actual networking functionalities
|
|
47
|
+
* @private
|
|
48
|
+
*/
|
|
49
|
+
__setup__() {
|
|
50
|
+
const hostname = this.baseUrl;
|
|
51
|
+
const self = this;
|
|
52
|
+
R.forEach(([key, value]) => {
|
|
53
|
+
var _a, _b, _c, _d, _e, _f;
|
|
54
|
+
let headers = (_a = value.headers) !== null && _a !== void 0 ? _a : {};
|
|
55
|
+
const apiPath = (_b = value.apiPath) !== null && _b !== void 0 ? _b : "";
|
|
56
|
+
const oldMethod = self[key];
|
|
57
|
+
const auth = { global: (_f = (_d = (_c = value.authRefresher) === null || _c === void 0 ? void 0 : _c.global) !== null && _d !== void 0 ? _d : (_e = value.authenticator) === null || _e === void 0 ? void 0 : _e.global) !== null && _f !== void 0 ? _f : false };
|
|
58
|
+
if (value.requestType === undefined) {
|
|
59
|
+
this.__setup_authentication__(value, key, self[key], auth);
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
const requestType = value.requestType;
|
|
63
|
+
const cacheInfo = value.cache;
|
|
64
|
+
let cache;
|
|
65
|
+
if (cacheInfo) {
|
|
66
|
+
cache = this.cacheManager.createCache(this.baseUrl, cacheInfo.persist, (resp) => __awaiter(this, void 0, void 0, function* () {
|
|
67
|
+
return JSON.stringify({
|
|
68
|
+
headers: Object.fromEntries(resp.headers),
|
|
69
|
+
status: resp.status,
|
|
70
|
+
statusText: resp.statusText,
|
|
71
|
+
body: yield Stream.of(resp.body).collect(Collectors.asList()),
|
|
72
|
+
});
|
|
73
|
+
}), (v) => {
|
|
74
|
+
const { headers, status, statusText, body } = JSON.parse(v);
|
|
75
|
+
return new Response(Stream.of(body).readableStream, { status, statusText, headers });
|
|
76
|
+
}, undefined, cacheInfo.duration);
|
|
77
|
+
}
|
|
78
|
+
self[key] = (...args) => {
|
|
79
|
+
var _a;
|
|
80
|
+
let url = new URL((hostname.endsWith("/") ? hostname.substring(0, hostname.length) : hostname) +
|
|
81
|
+
(apiPath.startsWith("/") ? apiPath : "/" + apiPath));
|
|
82
|
+
return new Call((signal) => __awaiter(this, void 0, void 0, function* () {
|
|
83
|
+
headers = Object.assign(Object.assign({}, headers), this.__get_auth_headers__(auth));
|
|
84
|
+
const result = this.__constructor_call_args__(url, headers, value, args);
|
|
85
|
+
headers = result.headers;
|
|
86
|
+
url = result.url;
|
|
87
|
+
const body = result.body;
|
|
88
|
+
if (cache && cacheInfo.method === CachingMethod.HIT_FIRST) {
|
|
89
|
+
let key = `${url.toString()}-${requestType.toString()}-${hash.sha1(headers)}`;
|
|
90
|
+
if (body) {
|
|
91
|
+
key += hash(body);
|
|
92
|
+
}
|
|
93
|
+
const result = yield cache.get(key).registerSignal(signal);
|
|
94
|
+
if (result) {
|
|
95
|
+
return result.clone();
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
const lookup = (retries = 0) => request(url, { headers, method: requestType.toString(), body }).thenApply(({ value: resp }) => __awaiter(this, void 0, void 0, function* () {
|
|
99
|
+
let credentials;
|
|
100
|
+
if (resp.status === HTTPStatus.UNAUTHORIZED &&
|
|
101
|
+
!value.noAuth &&
|
|
102
|
+
self[key] !== auth.authenticator &&
|
|
103
|
+
self[key] !== auth.authRefresher &&
|
|
104
|
+
(credentials = yield this.__get_credentials__(auth))) {
|
|
105
|
+
return request(url, {
|
|
106
|
+
headers: Object.assign(Object.assign({}, headers), credentials.buildAuthHeaders()),
|
|
107
|
+
method: requestType.toString(),
|
|
108
|
+
body,
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
else if ((resp.status === HTTPStatus.GATEWAY_TIMEOUT || resp.status === HTTPStatus.SERVICE_UNAVAILABLE) &&
|
|
112
|
+
self.retryLimit &&
|
|
113
|
+
retries < self.retryLimit) {
|
|
114
|
+
return Future.sleep({ milliseconds: THROTTLE_DEFAULT_MS }).thenApply(() => lookup(retries + 1));
|
|
115
|
+
}
|
|
116
|
+
if (cache) {
|
|
117
|
+
let key = `${url.toString()}-${requestType.toString()}-${hash.sha1(headers)}`;
|
|
118
|
+
if (resp.ok) {
|
|
119
|
+
if (body) {
|
|
120
|
+
key += hash(body);
|
|
121
|
+
}
|
|
122
|
+
cache.set(key, resp.clone());
|
|
123
|
+
}
|
|
124
|
+
else if (cache && !resp.ok && cacheInfo.method === CachingMethod.FALLBACK_ON_MISSING) {
|
|
125
|
+
return cache.get(key);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return resp;
|
|
129
|
+
}));
|
|
130
|
+
return lookup().registerSignal(signal);
|
|
131
|
+
}), oldMethod, value.responseClass, value.throttle, (_a = value.responseType) !== null && _a !== void 0 ? _a : ResponseType.NONE, value.responseArray);
|
|
132
|
+
};
|
|
133
|
+
this.__setup_authentication__(value, key, self[key], auth);
|
|
134
|
+
}, R.toPairs(this.__internal__.methodConfig));
|
|
135
|
+
}
|
|
136
|
+
__constructor_call_args__(url, headers, value, args) {
|
|
137
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
|
|
138
|
+
let body;
|
|
139
|
+
if (value.parameters) {
|
|
140
|
+
const paramHeaders = (_a = value.parameters.headers) !== null && _a !== void 0 ? _a : {};
|
|
141
|
+
const paramQueries = (_b = value.parameters.queries) !== null && _b !== void 0 ? _b : {};
|
|
142
|
+
const paramHeaderPairs = R.concat(R.toPairs(paramHeaders), R.map((k) => [k, paramHeaders[k]], Object.getOwnPropertySymbols(paramHeaders)));
|
|
143
|
+
const paramQueryPairs = R.concat(R.toPairs(paramQueries), R.map((k) => [k, paramQueries[k]], Object.getOwnPropertySymbols(paramQueries)));
|
|
144
|
+
R.forEach(([key, index]) => {
|
|
145
|
+
const arg = args[index];
|
|
146
|
+
if (arg === undefined) {
|
|
147
|
+
throw new CallExecutionError(`header parameter is missing at index ${index}`);
|
|
148
|
+
}
|
|
149
|
+
if (typeof key === "symbol" && typeof arg === "object") {
|
|
150
|
+
headers = Object.assign(Object.assign({}, headers), arg);
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
headers[key] = headers[key] ? `${R.prop(key, headers)};${String(args[index])}` : String(args[index]);
|
|
154
|
+
}
|
|
155
|
+
}, paramHeaderPairs);
|
|
156
|
+
R.forEach(([key, index]) => {
|
|
157
|
+
if (args[index] === undefined) {
|
|
158
|
+
throw new CallExecutionError(`path parameter is missing at index ${index}`);
|
|
159
|
+
}
|
|
160
|
+
url.pathname = url.pathname.replace(encodeURI(`{${key}}`), encodeURI(args[index]));
|
|
161
|
+
}, R.toPairs((_c = value.parameters.pathVariables) !== null && _c !== void 0 ? _c : {}));
|
|
162
|
+
R.forEach(([key, index]) => {
|
|
163
|
+
const arg = args[index];
|
|
164
|
+
if (arg === undefined) {
|
|
165
|
+
throw new CallExecutionError(`query parameter is missing at index ${index}`);
|
|
166
|
+
}
|
|
167
|
+
if (typeof key === "symbol" && typeof arg === "object") {
|
|
168
|
+
R.forEach(([k, v]) => {
|
|
169
|
+
url.searchParams.set(k, v);
|
|
170
|
+
}, R.toPairs(arg));
|
|
171
|
+
}
|
|
172
|
+
else {
|
|
173
|
+
url.searchParams.set(key, arg);
|
|
174
|
+
}
|
|
175
|
+
}, paramQueryPairs);
|
|
176
|
+
if ((_d = value.parameters.body) === null || _d === void 0 ? void 0 : _d.type) {
|
|
177
|
+
switch (value.parameters.body.type) {
|
|
178
|
+
case BodyType.STRING: {
|
|
179
|
+
const index = (_e = value.parameters.body.parameterIndex) !== null && _e !== void 0 ? _e : -1;
|
|
180
|
+
if (args[index] === undefined) {
|
|
181
|
+
throw new CallExecutionError(`body parameter is missing at index ${index}`);
|
|
182
|
+
}
|
|
183
|
+
body = this.convertToJSON(args[index]);
|
|
184
|
+
break;
|
|
185
|
+
}
|
|
186
|
+
case BodyType.JSON: {
|
|
187
|
+
headers["Content-Type"] = (_f = headers["Content-Type"]) !== null && _f !== void 0 ? _f : "application/json";
|
|
188
|
+
const index = (_g = value.parameters.body.parameterIndex) !== null && _g !== void 0 ? _g : -1;
|
|
189
|
+
let data = {};
|
|
190
|
+
if (args[index] === undefined) {
|
|
191
|
+
const fields = value.parameters.body.fields;
|
|
192
|
+
if (!fields) {
|
|
193
|
+
throw new CallExecutionError(`body parameter is missing at index ${index}`);
|
|
194
|
+
}
|
|
195
|
+
R.forEach(([key, index]) => {
|
|
196
|
+
const arg = args[index];
|
|
197
|
+
if (arg === undefined) {
|
|
198
|
+
throw new CallExecutionError(`form field is missing at index ${index}`);
|
|
199
|
+
}
|
|
200
|
+
data[key] = arg;
|
|
201
|
+
}, R.toPairs((_h = value.parameters.body.fields) !== null && _h !== void 0 ? _h : {}));
|
|
202
|
+
R.forEach(([key, index]) => {
|
|
203
|
+
const arg = args[index];
|
|
204
|
+
if (arg !== undefined && arg !== null) {
|
|
205
|
+
data[key] = arg;
|
|
206
|
+
}
|
|
207
|
+
}, R.toPairs((_j = value.parameters.body.optionalFields) !== null && _j !== void 0 ? _j : {}));
|
|
208
|
+
}
|
|
209
|
+
else {
|
|
210
|
+
data = args[index];
|
|
211
|
+
}
|
|
212
|
+
body = this.convertToJSON(data);
|
|
213
|
+
break;
|
|
214
|
+
}
|
|
215
|
+
case BodyType.XML: {
|
|
216
|
+
const index = value.parameters.body.parameterIndex;
|
|
217
|
+
const arg = args[index];
|
|
218
|
+
if (arg === undefined) {
|
|
219
|
+
throw new CallExecutionError(`body parameter is missing at index ${index}`);
|
|
220
|
+
}
|
|
221
|
+
body = arg instanceof Model ? arg.xml() : new XMLSerializer().serializeToString(arg);
|
|
222
|
+
break;
|
|
223
|
+
}
|
|
224
|
+
case BodyType.FORMURLENCODED: {
|
|
225
|
+
headers["Content-Type"] = (_k = headers["Content-Type"]) !== null && _k !== void 0 ? _k : "application/x-www-form-urlencoded";
|
|
226
|
+
const formData = new URLSearchParams();
|
|
227
|
+
R.forEach(([key, index]) => {
|
|
228
|
+
const arg = args[index];
|
|
229
|
+
if (arg === undefined) {
|
|
230
|
+
throw new CallExecutionError(`form field is missing at index ${index}`);
|
|
231
|
+
}
|
|
232
|
+
formData.set(key, arg);
|
|
233
|
+
}, R.toPairs((_l = value.parameters.body.fields) !== null && _l !== void 0 ? _l : {}));
|
|
234
|
+
R.forEach(([key, index]) => {
|
|
235
|
+
const arg = args[index];
|
|
236
|
+
if (arg !== undefined && arg !== null) {
|
|
237
|
+
formData.set(key, arg);
|
|
238
|
+
}
|
|
239
|
+
}, R.toPairs((_m = value.parameters.body.optionalFields) !== null && _m !== void 0 ? _m : {}));
|
|
240
|
+
body = formData;
|
|
241
|
+
break;
|
|
242
|
+
}
|
|
243
|
+
case BodyType.MULTIPART: {
|
|
244
|
+
headers["Content-Type"] = (_o = headers["Content-Type"]) !== null && _o !== void 0 ? _o : "multipart/form-data";
|
|
245
|
+
const formData = new FormData();
|
|
246
|
+
R.forEach(([key, index]) => {
|
|
247
|
+
const arg = args[index];
|
|
248
|
+
if (arg === undefined) {
|
|
249
|
+
throw new CallExecutionError(`form part is missing at index ${index}`);
|
|
250
|
+
}
|
|
251
|
+
formData.append(key, arg);
|
|
252
|
+
}, R.toPairs((_p = value.parameters.body.parts) !== null && _p !== void 0 ? _p : {}));
|
|
253
|
+
body = formData;
|
|
254
|
+
break;
|
|
255
|
+
}
|
|
256
|
+
default:
|
|
257
|
+
throw new CallExecutionError(`Unsupported body type: ${(_q = value.parameters.body) === null || _q === void 0 ? void 0 : _q.type}`);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
return { body, headers, url };
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* Retrieves the authentication headers
|
|
265
|
+
* @param auth AuthInstance
|
|
266
|
+
* @private
|
|
267
|
+
*/
|
|
268
|
+
__get_auth_headers__(auth) {
|
|
269
|
+
const classRef = this.constructor;
|
|
270
|
+
if (!auth.global && classRef.credentials)
|
|
271
|
+
return classRef.credentials.buildAuthHeaders();
|
|
272
|
+
else if (auth.global && NetworkService.credentials)
|
|
273
|
+
return NetworkService.credentials.buildAuthHeaders();
|
|
274
|
+
return {};
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* Retrieves the auth credentials, and if no valid on exists, attempt to fetch it via the authenticator/authRefresher
|
|
278
|
+
* @param auth AuthInstance
|
|
279
|
+
* @private
|
|
280
|
+
*/
|
|
281
|
+
__get_credentials__(auth) {
|
|
282
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
283
|
+
let result;
|
|
284
|
+
let credentials;
|
|
285
|
+
const classRef = this.constructor;
|
|
286
|
+
if (auth.global && NetworkService.credentials && auth.authRefresher) {
|
|
287
|
+
result = auth.authRefresher(NetworkService.credentials);
|
|
288
|
+
}
|
|
289
|
+
else if (!auth.global && classRef.credentials && auth.authRefresher) {
|
|
290
|
+
result = auth.authRefresher(classRef.credentials);
|
|
291
|
+
}
|
|
292
|
+
else if (auth.authenticator) {
|
|
293
|
+
result = auth.authenticator();
|
|
294
|
+
}
|
|
295
|
+
else {
|
|
296
|
+
return;
|
|
297
|
+
}
|
|
298
|
+
if (result instanceof Call) {
|
|
299
|
+
credentials = yield result.execute();
|
|
300
|
+
}
|
|
301
|
+
else if (result instanceof Promise) {
|
|
302
|
+
credentials = yield result;
|
|
303
|
+
}
|
|
304
|
+
else {
|
|
305
|
+
credentials = result;
|
|
306
|
+
}
|
|
307
|
+
if (auth.global)
|
|
308
|
+
NetworkService.credentials = credentials;
|
|
309
|
+
else
|
|
310
|
+
classRef.credentials = credentials;
|
|
311
|
+
return credentials;
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
/**
|
|
315
|
+
* Configures authentication if none exist by assigning the authenticator to the correct scope
|
|
316
|
+
* (Global -> Service, Local -> this)
|
|
317
|
+
* @param config MethodConfiguration
|
|
318
|
+
* @param methodName name of the method that should hold the authenticator
|
|
319
|
+
* @param functor authenticator
|
|
320
|
+
* @param auth AuthInstance
|
|
321
|
+
* @private
|
|
322
|
+
*/
|
|
323
|
+
__setup_authentication__(config, methodName, functor, auth) {
|
|
324
|
+
const classRef = this.constructor;
|
|
325
|
+
if (config.authenticator) {
|
|
326
|
+
if (config.authenticator.global && !NetworkService.authenticator) {
|
|
327
|
+
NetworkService.authenticator = functor;
|
|
328
|
+
}
|
|
329
|
+
else if (!config.authenticator.global && !classRef.authenticator) {
|
|
330
|
+
classRef.authenticator = functor;
|
|
331
|
+
}
|
|
332
|
+
const parentRef = config.authenticator.global ? NetworkService : classRef;
|
|
333
|
+
const oldMethod = this[methodName];
|
|
334
|
+
this[methodName] = (...args) => {
|
|
335
|
+
const result = oldMethod(...args);
|
|
336
|
+
if (result instanceof Promise) {
|
|
337
|
+
return result.then((v) => {
|
|
338
|
+
if (v instanceof Credentials) {
|
|
339
|
+
parentRef.credentials = v;
|
|
340
|
+
}
|
|
341
|
+
return v;
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
else if (result instanceof Credentials) {
|
|
345
|
+
parentRef.credentials = result;
|
|
346
|
+
}
|
|
347
|
+
return result;
|
|
348
|
+
};
|
|
349
|
+
}
|
|
350
|
+
if (config.authRefresher) {
|
|
351
|
+
if (config.authRefresher.global && !NetworkService.authRefresher) {
|
|
352
|
+
NetworkService.authRefresher = functor;
|
|
353
|
+
}
|
|
354
|
+
else if (!config.authRefresher.global && !classRef.authRefresher) {
|
|
355
|
+
classRef.authRefresher = functor;
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
if (NetworkService.authenticator) {
|
|
359
|
+
auth.authenticator = NetworkService.authenticator;
|
|
360
|
+
}
|
|
361
|
+
else if (classRef.authenticator) {
|
|
362
|
+
auth.authenticator = classRef.authenticator;
|
|
363
|
+
}
|
|
364
|
+
if (NetworkService.authRefresher) {
|
|
365
|
+
auth.authRefresher = NetworkService.authRefresher;
|
|
366
|
+
}
|
|
367
|
+
else if (classRef.authRefresher) {
|
|
368
|
+
auth.authRefresher = classRef.authRefresher;
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
export { NetworkService };
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var _tslib = require('../_virtual/_tslib.cjs');
|
|
4
|
+
var NetworkService = require('./NetworkService.cjs');
|
|
5
|
+
var socket = require('../socket.cjs');
|
|
6
|
+
var R = require('ramda');
|
|
7
|
+
var CallExecutionError = require('../errors/CallExecutionError.cjs');
|
|
8
|
+
var ParsingError = require('../errors/ParsingError.cjs');
|
|
9
|
+
var stream = require('../stream.cjs');
|
|
10
|
+
var future = require('../future/future.cjs');
|
|
11
|
+
var result = require('../future/result.cjs');
|
|
12
|
+
var json = require('../data/decoders/json.cjs');
|
|
13
|
+
require('@msgpack/msgpack');
|
|
14
|
+
var model = require('../data/model/model.cjs');
|
|
15
|
+
require('../data/model/types.cjs');
|
|
16
|
+
require('../data/model/credentials.cjs');
|
|
17
|
+
|
|
18
|
+
function _interopNamespaceDefault(e) {
|
|
19
|
+
var n = Object.create(null);
|
|
20
|
+
if (e) {
|
|
21
|
+
Object.keys(e).forEach(function (k) {
|
|
22
|
+
if (k !== 'default') {
|
|
23
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
24
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
25
|
+
enumerable: true,
|
|
26
|
+
get: function () { return e[k]; }
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
n.default = e;
|
|
32
|
+
return Object.freeze(n);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
var R__namespace = /*#__PURE__*/_interopNamespaceDefault(R);
|
|
36
|
+
|
|
37
|
+
class WebSocketService extends NetworkService.NetworkService {
|
|
38
|
+
constructor(_a = {}) {
|
|
39
|
+
var { decoder } = _a, other = _tslib.__rest(_a, ["decoder"]);
|
|
40
|
+
super(other !== null && other !== void 0 ? other : {});
|
|
41
|
+
const internals = this.__internal__;
|
|
42
|
+
this.socket = new socket.StreamableWebSocket(this.baseUrl, decoder !== null && decoder !== void 0 ? decoder : (internals.classConfig.Decoder ? new internals.classConfig.Decoder() : new json.JSONDecoder()));
|
|
43
|
+
}
|
|
44
|
+
get connected() {
|
|
45
|
+
return this.socket.opened;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Called when socket connection is closed
|
|
49
|
+
* @protected
|
|
50
|
+
*/
|
|
51
|
+
onceConnectionClosed() { }
|
|
52
|
+
/**
|
|
53
|
+
* Shutdown the socket connection
|
|
54
|
+
*/
|
|
55
|
+
shutdown() {
|
|
56
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
57
|
+
this.socket.close();
|
|
58
|
+
this.onceConnectionClosed();
|
|
59
|
+
yield this.socket.closedFuture();
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Awaits socket closed
|
|
64
|
+
*/
|
|
65
|
+
closedFuture() {
|
|
66
|
+
return this.socket.closedFuture();
|
|
67
|
+
}
|
|
68
|
+
ready() {
|
|
69
|
+
return future.Future.shield(this.socket.open());
|
|
70
|
+
}
|
|
71
|
+
__setup__() {
|
|
72
|
+
const internals = this.__internal__;
|
|
73
|
+
if (this.socket.decoder.load) {
|
|
74
|
+
this.socket.decoder.load();
|
|
75
|
+
}
|
|
76
|
+
const socketMethods = R__namespace.filter(([_, v]) => { var _a, _b; return ((_b = (_a = v.socketReadStream) !== null && _a !== void 0 ? _a : v.socketWriteStream) !== null && _b !== void 0 ? _b : v.socketRequestReply) !== undefined; }, R__namespace.toPairs(internals.methodConfig));
|
|
77
|
+
R__namespace.forEach(([key, config]) => {
|
|
78
|
+
var _a;
|
|
79
|
+
const oldMethod = this[key];
|
|
80
|
+
const details = (_a = internals.methodConfig[key]) === null || _a === void 0 ? void 0 : _a.socketReadStream;
|
|
81
|
+
let dataComparator = (v) => false;
|
|
82
|
+
if (config.socketReadStream) {
|
|
83
|
+
if (!details)
|
|
84
|
+
throw new ParsingError.ParsingError([], "ReadStreamDetailsMissing");
|
|
85
|
+
if (!config.responseClass)
|
|
86
|
+
throw new ParsingError.ParsingError([], "ResponseTypeMissing");
|
|
87
|
+
let equalsChecker;
|
|
88
|
+
switch (typeof details.value) {
|
|
89
|
+
case "boolean":
|
|
90
|
+
equalsChecker = R__namespace.equals(Boolean(details.value));
|
|
91
|
+
break;
|
|
92
|
+
case "string":
|
|
93
|
+
equalsChecker = R__namespace.equals(String(details.value));
|
|
94
|
+
break;
|
|
95
|
+
case "number":
|
|
96
|
+
equalsChecker = R__namespace.equals(Number(details.value));
|
|
97
|
+
break;
|
|
98
|
+
default:
|
|
99
|
+
if (details.value instanceof RegExp)
|
|
100
|
+
equalsChecker = details.value.test;
|
|
101
|
+
else
|
|
102
|
+
equalsChecker = R__namespace.equals(details.value);
|
|
103
|
+
}
|
|
104
|
+
dataComparator = R__namespace.compose((v) => equalsChecker(v), R__namespace.view(typeof details.keyPath === "string" ? R__namespace.lensProp(details.keyPath) : R__namespace.lensPath(details.keyPath)));
|
|
105
|
+
}
|
|
106
|
+
// TODO add caching for websockets
|
|
107
|
+
// const cacheInfo = config.cache;
|
|
108
|
+
// let cache: Cache<string, ArrayBuffer> | undefined;
|
|
109
|
+
//
|
|
110
|
+
// if (cacheInfo) {
|
|
111
|
+
// cache = this.cacheManager.createCache(
|
|
112
|
+
// this.baseUrl,
|
|
113
|
+
// cacheInfo.persist,
|
|
114
|
+
// async (buffer) => JSON.stringify(new Uint8Array(buffer)),
|
|
115
|
+
// (v) => Uint8Array.of(JSON.parse(v)).buffer,
|
|
116
|
+
// undefined,
|
|
117
|
+
// cacheInfo.duration
|
|
118
|
+
// );
|
|
119
|
+
// }
|
|
120
|
+
this[key] = (body) => {
|
|
121
|
+
var _a, _b;
|
|
122
|
+
if (config.socketRequestReply) {
|
|
123
|
+
const guid = config.socketRequestReply.guidGen();
|
|
124
|
+
const lens = R__namespace.lensPath(config.socketRequestReply.guidPath);
|
|
125
|
+
return stream.Stream.seed(() => future.Future.of(() => _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
126
|
+
if (!this.socket.opened) {
|
|
127
|
+
yield this.socket.open();
|
|
128
|
+
}
|
|
129
|
+
let data = body;
|
|
130
|
+
if (body instanceof model.Model) {
|
|
131
|
+
data = body.object();
|
|
132
|
+
}
|
|
133
|
+
data = R__namespace.set(lens, guid, data);
|
|
134
|
+
yield this.socket.send(JSON.stringify(data));
|
|
135
|
+
return 1;
|
|
136
|
+
})))
|
|
137
|
+
.map(() => this.socket.streamView(R__namespace.compose(R__namespace.equals(guid), R__namespace.view(lens)), config.socketRequestReply.objectMaxSize, config.socketRequestReply.expiryPeriod))
|
|
138
|
+
.map((data) => {
|
|
139
|
+
if (config.responseClass.prototype instanceof model.Model) {
|
|
140
|
+
const ModelClass = config.responseClass;
|
|
141
|
+
return ModelClass.fromJSON(data);
|
|
142
|
+
}
|
|
143
|
+
return config.responseClass(data);
|
|
144
|
+
})
|
|
145
|
+
.flatten()
|
|
146
|
+
.map((v) => _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
147
|
+
let result$1 = oldMethod(v);
|
|
148
|
+
if (result$1 === null || result$1 === undefined)
|
|
149
|
+
return v;
|
|
150
|
+
if (result$1 instanceof Promise)
|
|
151
|
+
result$1 = yield result$1;
|
|
152
|
+
if (result$1 instanceof result.FutureResult)
|
|
153
|
+
result$1 = result$1.value;
|
|
154
|
+
return result$1;
|
|
155
|
+
}));
|
|
156
|
+
}
|
|
157
|
+
else if (config.socketWriteStream) {
|
|
158
|
+
if (body === undefined || body === null)
|
|
159
|
+
throw new CallExecutionError.CallExecutionError("Empty body detected for a write stream");
|
|
160
|
+
return new stream.Stream((signal) => _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
161
|
+
if (!this.socket.opened) {
|
|
162
|
+
yield this.socket.open();
|
|
163
|
+
}
|
|
164
|
+
if (body instanceof model.Model) {
|
|
165
|
+
yield this.socket.send(body.blob());
|
|
166
|
+
}
|
|
167
|
+
else if (body instanceof ArrayBuffer || body instanceof Blob) {
|
|
168
|
+
yield this.socket.send(body);
|
|
169
|
+
}
|
|
170
|
+
else {
|
|
171
|
+
yield this.socket.send(JSON.stringify(body));
|
|
172
|
+
}
|
|
173
|
+
const result = oldMethod();
|
|
174
|
+
if (result instanceof Promise) {
|
|
175
|
+
yield result;
|
|
176
|
+
}
|
|
177
|
+
return null;
|
|
178
|
+
})).once();
|
|
179
|
+
}
|
|
180
|
+
else {
|
|
181
|
+
let stream = this.socket.streamView(dataComparator);
|
|
182
|
+
if (((_a = config.socketReadStream) === null || _a === void 0 ? void 0 : _a.skip) !== undefined)
|
|
183
|
+
stream = stream.skip(config.socketReadStream.skip);
|
|
184
|
+
if (((_b = config.socketReadStream) === null || _b === void 0 ? void 0 : _b.take) !== undefined)
|
|
185
|
+
stream = stream.take(config.socketReadStream.take);
|
|
186
|
+
return stream
|
|
187
|
+
.map((data) => {
|
|
188
|
+
if (config.responseClass.prototype instanceof model.Model) {
|
|
189
|
+
const ModelClass = config.responseClass;
|
|
190
|
+
return ModelClass.fromJSON(data);
|
|
191
|
+
}
|
|
192
|
+
return config.responseClass(data);
|
|
193
|
+
})
|
|
194
|
+
.flatten()
|
|
195
|
+
.map((v) => _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
196
|
+
let result$1 = oldMethod(v);
|
|
197
|
+
if (result$1 === null || result$1 === undefined)
|
|
198
|
+
return v;
|
|
199
|
+
if (result$1 instanceof Promise)
|
|
200
|
+
result$1 = yield result$1;
|
|
201
|
+
if (result$1 instanceof result.FutureResult)
|
|
202
|
+
result$1 = result$1.value;
|
|
203
|
+
return result$1;
|
|
204
|
+
}));
|
|
205
|
+
}
|
|
206
|
+
};
|
|
207
|
+
}, socketMethods);
|
|
208
|
+
if (!this.socket.opened) {
|
|
209
|
+
void this.socket.open();
|
|
210
|
+
}
|
|
211
|
+
const originalMethodConfig = internals.methodConfig;
|
|
212
|
+
internals.methodConfig = R__namespace.fromPairs(R__namespace.filter(([_, v]) => !(v.socketReadStream && v.socketWriteStream), R__namespace.toPairs(internals.methodConfig)));
|
|
213
|
+
super.__setup__();
|
|
214
|
+
internals.methodConfig = originalMethodConfig;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
exports.WebSocketService = WebSocketService;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { NetworkService } from "./NetworkService";
|
|
2
|
+
import { Future } from "../future";
|
|
3
|
+
import { GingerSnapProps } from "./index";
|
|
4
|
+
export declare class WebSocketService extends NetworkService {
|
|
5
|
+
private readonly socket;
|
|
6
|
+
constructor({ decoder, ...other }?: GingerSnapProps);
|
|
7
|
+
get connected(): boolean;
|
|
8
|
+
/**
|
|
9
|
+
* Called when socket connection is closed
|
|
10
|
+
* @protected
|
|
11
|
+
*/
|
|
12
|
+
protected onceConnectionClosed(): void;
|
|
13
|
+
/**
|
|
14
|
+
* Shutdown the socket connection
|
|
15
|
+
*/
|
|
16
|
+
shutdown(): Promise<void>;
|
|
17
|
+
/**
|
|
18
|
+
* Awaits socket closed
|
|
19
|
+
*/
|
|
20
|
+
closedFuture(): Future<void>;
|
|
21
|
+
ready(): Future<void>;
|
|
22
|
+
protected __setup__(): void;
|
|
23
|
+
}
|