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.
Files changed (251) hide show
  1. package/_virtual/_tslib.cjs +102 -0
  2. package/_virtual/_tslib.mjs +93 -0
  3. package/data/AtomicValue.d.ts +15 -0
  4. package/data/bus.cjs +179 -0
  5. package/data/bus.d.ts +72 -0
  6. package/data/bus.mjs +177 -0
  7. package/data/decoders/index.d.ts +4 -0
  8. package/data/decoders/json.cjs +12 -0
  9. package/data/decoders/json.d.ts +7 -0
  10. package/data/decoders/json.mjs +10 -0
  11. package/data/decoders/msgpack.cjs +14 -0
  12. package/data/decoders/msgpack.d.ts +7 -0
  13. package/data/decoders/msgpack.mjs +12 -0
  14. package/data/decoders/protobuf.d.ts +13 -0
  15. package/data/decoders/string.cjs +12 -0
  16. package/data/decoders/string.d.ts +7 -0
  17. package/data/decoders/string.mjs +10 -0
  18. package/data/decoders/type.d.ts +7 -0
  19. package/data/decoders.cjs +11 -0
  20. package/data/decoders.mjs +3 -0
  21. package/data/model/credentials.cjs +114 -0
  22. package/data/model/credentials.d.ts +61 -0
  23. package/data/model/credentials.mjs +109 -0
  24. package/data/model/extra.cjs +88 -0
  25. package/data/model/extra.d.ts +5 -0
  26. package/data/model/extra.mjs +66 -0
  27. package/data/model/index.d.ts +6 -0
  28. package/data/model/model.cjs +541 -0
  29. package/data/model/model.d.ts +207 -0
  30. package/data/model/model.mjs +518 -0
  31. package/data/model/plugin.cjs +96 -0
  32. package/data/model/plugin.d.ts +38 -0
  33. package/data/model/plugin.mjs +72 -0
  34. package/data/model/property.cjs +310 -0
  35. package/data/model/property.d.ts +83 -0
  36. package/data/model/property.mjs +278 -0
  37. package/data/model/types.cjs +32 -0
  38. package/data/model/types.d.ts +28 -0
  39. package/data/model/types.mjs +32 -0
  40. package/data/model.cjs +43 -0
  41. package/data/model.mjs +6 -0
  42. package/data/signal.cjs +47 -0
  43. package/data/signal.d.ts +17 -0
  44. package/data/signal.mjs +45 -0
  45. package/data/store/index.d.ts +4 -0
  46. package/data/store/json.cjs +58 -0
  47. package/data/store/json.d.ts +18 -0
  48. package/data/store/json.mjs +56 -0
  49. package/data/store/manager.cjs +206 -0
  50. package/data/store/manager.d.ts +44 -0
  51. package/data/store/manager.mjs +203 -0
  52. package/data/store/web.cjs +92 -0
  53. package/data/store/web.d.ts +25 -0
  54. package/data/store/web.mjs +89 -0
  55. package/data/value.cjs +66 -0
  56. package/data/value.mjs +64 -0
  57. package/data-structures/array/CyclicalList.cjs +57 -0
  58. package/data-structures/array/CyclicalList.d.ts +12 -0
  59. package/data-structures/array/CyclicalList.mjs +55 -0
  60. package/data-structures/array/Pair.cjs +30 -0
  61. package/data-structures/array/Pair.d.ts +17 -0
  62. package/data-structures/array/Pair.mjs +27 -0
  63. package/data-structures/array/Stack.cjs +96 -0
  64. package/data-structures/array/Stack.d.ts +41 -0
  65. package/data-structures/array/Stack.mjs +94 -0
  66. package/data-structures/array/Triple.cjs +34 -0
  67. package/data-structures/array/Triple.d.ts +19 -0
  68. package/data-structures/array/Triple.mjs +31 -0
  69. package/data-structures/array/Tuple.d.ts +8 -0
  70. package/data-structures/array/index.d.ts +5 -0
  71. package/data-structures/array.cjs +15 -0
  72. package/data-structures/array.mjs +4 -0
  73. package/data-structures/object/BufferQueue.cjs +98 -0
  74. package/data-structures/object/BufferQueue.d.ts +21 -0
  75. package/data-structures/object/BufferQueue.mjs +77 -0
  76. package/data-structures/object/CyclicalObject.cjs +155 -0
  77. package/data-structures/object/CyclicalObject.d.ts +74 -0
  78. package/data-structures/object/CyclicalObject.mjs +134 -0
  79. package/data-structures/object/Queue.cjs +178 -0
  80. package/data-structures/object/Queue.d.ts +31 -0
  81. package/data-structures/object/Queue.mjs +176 -0
  82. package/data-structures/object/SimpleQueue.cjs +53 -0
  83. package/data-structures/object/SimpleQueue.d.ts +13 -0
  84. package/data-structures/object/SimpleQueue.mjs +51 -0
  85. package/data-structures/object/TimeableObject.cjs +73 -0
  86. package/data-structures/object/TimeableObject.d.ts +30 -0
  87. package/data-structures/object/TimeableObject.mjs +71 -0
  88. package/data-structures/object/WaitableObject.cjs +61 -0
  89. package/data-structures/object/WaitableObject.d.ts +29 -0
  90. package/data-structures/object/WaitableObject.mjs +59 -0
  91. package/data-structures/object/WatchableObject.cjs +251 -0
  92. package/data-structures/object/WatchableObject.d.ts +88 -0
  93. package/data-structures/object/WatchableObject.mjs +230 -0
  94. package/data-structures/object/index.d.ts +7 -0
  95. package/data-structures/object.cjs +23 -0
  96. package/data-structures/object.mjs +7 -0
  97. package/errors/AbortError.cjs +9 -0
  98. package/errors/AbortError.d.ts +5 -0
  99. package/errors/AbortError.mjs +7 -0
  100. package/errors/CallExecutionError.cjs +13 -0
  101. package/errors/CallExecutionError.d.ts +7 -0
  102. package/errors/CallExecutionError.mjs +11 -0
  103. package/errors/FutureCancelled.cjs +11 -0
  104. package/errors/FutureCancelled.d.ts +6 -0
  105. package/errors/FutureCancelled.mjs +9 -0
  106. package/errors/FutureError.cjs +11 -0
  107. package/errors/FutureError.d.ts +6 -0
  108. package/errors/FutureError.mjs +9 -0
  109. package/errors/IllegalArgumentsError.cjs +13 -0
  110. package/errors/IllegalArgumentsError.d.ts +7 -0
  111. package/errors/IllegalArgumentsError.mjs +11 -0
  112. package/errors/IllegalOperationError.cjs +6 -0
  113. package/errors/IllegalOperationError.d.ts +2 -0
  114. package/errors/IllegalOperationError.mjs +4 -0
  115. package/errors/InvalidType.cjs +6 -0
  116. package/errors/InvalidType.d.ts +2 -0
  117. package/errors/InvalidType.mjs +4 -0
  118. package/errors/InvalidValue.cjs +11 -0
  119. package/errors/InvalidValue.d.ts +4 -0
  120. package/errors/InvalidValue.mjs +9 -0
  121. package/errors/MissingArgumentsError.cjs +13 -0
  122. package/errors/MissingArgumentsError.d.ts +7 -0
  123. package/errors/MissingArgumentsError.mjs +11 -0
  124. package/errors/NetworkError.cjs +13 -0
  125. package/errors/NetworkError.d.ts +8 -0
  126. package/errors/NetworkError.mjs +11 -0
  127. package/errors/NoSuchElement.cjs +6 -0
  128. package/errors/NoSuchElement.d.ts +2 -0
  129. package/errors/NoSuchElement.mjs +4 -0
  130. package/errors/NotImplemented.cjs +9 -0
  131. package/errors/NotImplemented.d.ts +5 -0
  132. package/errors/NotImplemented.mjs +7 -0
  133. package/errors/ParsingError.cjs +10 -0
  134. package/errors/ParsingError.d.ts +5 -0
  135. package/errors/ParsingError.mjs +8 -0
  136. package/errors/QueueEmptyError.cjs +6 -0
  137. package/errors/QueueEmptyError.d.ts +2 -0
  138. package/errors/QueueEmptyError.mjs +4 -0
  139. package/errors/QueueFullError.cjs +6 -0
  140. package/errors/QueueFullError.d.ts +2 -0
  141. package/errors/QueueFullError.mjs +4 -0
  142. package/errors/StackEmptyError.cjs +6 -0
  143. package/errors/StackEmptyError.d.ts +2 -0
  144. package/errors/StackEmptyError.mjs +4 -0
  145. package/errors/StackFullError.cjs +6 -0
  146. package/errors/StackFullError.d.ts +2 -0
  147. package/errors/StackFullError.mjs +4 -0
  148. package/errors/StreamEnded.cjs +6 -0
  149. package/errors/StreamEnded.d.ts +2 -0
  150. package/errors/StreamEnded.mjs +4 -0
  151. package/errors/TimeoutError.cjs +11 -0
  152. package/errors/TimeoutError.d.ts +6 -0
  153. package/errors/TimeoutError.mjs +9 -0
  154. package/errors/ValueDestroyedError.cjs +6 -0
  155. package/errors/ValueDestroyedError.d.ts +2 -0
  156. package/errors/ValueDestroyedError.mjs +4 -0
  157. package/errors/VerificationError.cjs +9 -0
  158. package/errors/VerificationError.d.ts +5 -0
  159. package/errors/VerificationError.mjs +7 -0
  160. package/errors/index.d.ts +18 -0
  161. package/errors.cjs +41 -0
  162. package/errors.mjs +18 -0
  163. package/files.cjs +201 -0
  164. package/files.d.ts +31 -0
  165. package/files.mjs +180 -0
  166. package/functools/index.d.ts +31 -0
  167. package/functools.cjs +371 -0
  168. package/functools.mjs +334 -0
  169. package/future/future.cjs +570 -0
  170. package/future/future.d.ts +208 -0
  171. package/future/future.mjs +548 -0
  172. package/future/group.cjs +180 -0
  173. package/future/group.d.ts +82 -0
  174. package/future/group.mjs +159 -0
  175. package/future/index.d.ts +3 -0
  176. package/future/result.cjs +19 -0
  177. package/future/result.d.ts +18 -0
  178. package/future/result.mjs +17 -0
  179. package/future.cjs +11 -0
  180. package/future.mjs +3 -0
  181. package/managers/context.d.ts +8 -0
  182. package/managers/index.d.ts +1 -0
  183. package/managers.cjs +2 -0
  184. package/managers.mjs +1 -0
  185. package/mocks.cjs +301 -0
  186. package/mocks.d.ts +125 -0
  187. package/mocks.mjs +293 -0
  188. package/networking/EventSourceService.cjs +104 -0
  189. package/networking/EventSourceService.d.ts +11 -0
  190. package/networking/EventSourceService.mjs +83 -0
  191. package/networking/NetworkService.cjs +394 -0
  192. package/networking/NetworkService.d.ts +97 -0
  193. package/networking/NetworkService.mjs +373 -0
  194. package/networking/SocketService.cjs +218 -0
  195. package/networking/SocketService.d.ts +23 -0
  196. package/networking/SocketService.mjs +197 -0
  197. package/networking/decorators/index.d.ts +4 -0
  198. package/networking/decorators/options.cjs +289 -0
  199. package/networking/decorators/options.d.ts +148 -0
  200. package/networking/decorators/options.mjs +242 -0
  201. package/networking/decorators/requests.cjs +80 -0
  202. package/networking/decorators/requests.d.ts +34 -0
  203. package/networking/decorators/requests.mjs +52 -0
  204. package/networking/decorators/socket.cjs +81 -0
  205. package/networking/decorators/socket.d.ts +13 -0
  206. package/networking/decorators/socket.mjs +51 -0
  207. package/networking/http.cjs +69 -0
  208. package/networking/http.d.ts +66 -0
  209. package/networking/http.mjs +69 -0
  210. package/networking/index.d.ts +39 -0
  211. package/networking/request.cjs +18 -0
  212. package/networking/request.d.ts +9 -0
  213. package/networking/request.mjs +16 -0
  214. package/networking/types.cjs +37 -0
  215. package/networking/types.d.ts +132 -0
  216. package/networking/types.mjs +35 -0
  217. package/networking.cjs +91 -0
  218. package/networking.mjs +33 -0
  219. package/package.json +1 -0
  220. package/reflection/injector.cjs +43 -0
  221. package/reflection/injector.d.ts +3 -0
  222. package/reflection/injector.mjs +40 -0
  223. package/socket.cjs +239 -0
  224. package/socket.d.ts +65 -0
  225. package/socket.mjs +237 -0
  226. package/store.cjs +12 -0
  227. package/store.mjs +3 -0
  228. package/stream/call.cjs +157 -0
  229. package/stream/call.d.ts +66 -0
  230. package/stream/call.mjs +154 -0
  231. package/stream/collector.cjs +217 -0
  232. package/stream/collector.d.ts +14 -0
  233. package/stream/collector.mjs +215 -0
  234. package/stream/index.d.ts +257 -0
  235. package/stream/observable.cjs +110 -0
  236. package/stream/observable.d.ts +65 -0
  237. package/stream/observable.mjs +106 -0
  238. package/stream/state.cjs +10 -0
  239. package/stream/state.d.ts +5 -0
  240. package/stream/state.mjs +8 -0
  241. package/stream.cjs +1153 -0
  242. package/stream.mjs +1132 -0
  243. package/synchronize.cjs +106 -0
  244. package/synchronize.d.ts +54 -0
  245. package/synchronize.mjs +103 -0
  246. package/typing/types.d.ts +24 -0
  247. package/typing.cjs +2 -0
  248. package/typing.mjs +1 -0
  249. package/utils/parser.cjs +37 -0
  250. package/utils/parser.d.ts +7 -0
  251. package/utils/parser.mjs +16 -0
package/mocks.mjs ADDED
@@ -0,0 +1,293 @@
1
+ import { __awaiter } from './_virtual/_tslib.mjs';
2
+ import { AbortError } from './errors/AbortError.mjs';
3
+ import { VerificationError } from './errors/VerificationError.mjs';
4
+ import './data/model/model.mjs';
5
+ import 'ramda';
6
+ import './data/model/types.mjs';
7
+ import './data/model/credentials.mjs';
8
+ import './networking/types.mjs';
9
+ import './stream.mjs';
10
+ import { Future } from './future/future.mjs';
11
+ import './networking/decorators/options.mjs';
12
+ import './networking/decorators/requests.mjs';
13
+ import { HTTPStatus } from './networking/http.mjs';
14
+ import 'uuid';
15
+ import 'object-hash';
16
+ import './data/store/manager.mjs';
17
+ import './data-structures/object/WatchableObject.mjs';
18
+ import 'modern-isomorphic-ws';
19
+ import '@msgpack/msgpack';
20
+
21
+ /**
22
+ * Tracks specific details that should be used to match specific given network request
23
+ */
24
+ class RequestDetails {
25
+ constructor() {
26
+ this.headers = {};
27
+ this.queries = {};
28
+ }
29
+ withMethod(method) {
30
+ this.method = method;
31
+ return this;
32
+ }
33
+ withPath(path) {
34
+ this.path = path;
35
+ return this;
36
+ }
37
+ withHeader(key, value) {
38
+ this.headers[key] = value;
39
+ return this;
40
+ }
41
+ withHeaders(value) {
42
+ this.headers = value;
43
+ return this;
44
+ }
45
+ withQuery(key, value) {
46
+ this.queries[key] = value;
47
+ return this;
48
+ }
49
+ withQueries(value) {
50
+ this.queries = value;
51
+ return this;
52
+ }
53
+ withBody(value) {
54
+ this.body = value;
55
+ return this;
56
+ }
57
+ }
58
+ class ResponseBuilder {
59
+ constructor() {
60
+ this.status = HTTPStatus.OK;
61
+ this.headers = {};
62
+ }
63
+ withStatus(status) {
64
+ this.status = status;
65
+ return this;
66
+ }
67
+ withHeader(key, value) {
68
+ this.headers[key] = value;
69
+ return this;
70
+ }
71
+ withHeaders(value) {
72
+ this.headers = value;
73
+ return this;
74
+ }
75
+ withBody(value) {
76
+ this.body = value;
77
+ return this;
78
+ }
79
+ withURI(value) {
80
+ this.uri = value;
81
+ return this;
82
+ }
83
+ withFragment(value) {
84
+ this.fragment = value;
85
+ return this;
86
+ }
87
+ withDelay(value) {
88
+ this.delay = value;
89
+ return this;
90
+ }
91
+ build() {
92
+ var _a, _b;
93
+ let data;
94
+ if (this.body && typeof this.body === "string") {
95
+ data = new Blob([this.body]);
96
+ }
97
+ else if (this.body instanceof Blob) {
98
+ data = this.body;
99
+ }
100
+ const status = (_a = this.status) !== null && _a !== void 0 ? _a : HTTPStatus.OK;
101
+ const response = new Response(data, {
102
+ status,
103
+ statusText: (_b = Object.keys(HTTPStatus)
104
+ .find((key) => HTTPStatus[key] === status)) === null || _b === void 0 ? void 0 : _b.toLowerCase().split("_").map((v) => v[0].toUpperCase() + v.slice(1)).join(" "),
105
+ headers: Object.entries(this.headers),
106
+ });
107
+ if (this.uri) {
108
+ const url = new URL(this.uri);
109
+ if (this.fragment) {
110
+ url.hash = this.fragment;
111
+ }
112
+ Object.defineProperty(response, "url", { value: url.href });
113
+ }
114
+ return { response, delay: this.delay };
115
+ }
116
+ }
117
+ class API {
118
+ constructor() {
119
+ this.matchers = [];
120
+ this.processedRequests = [];
121
+ }
122
+ resolve(req) {
123
+ return __awaiter(this, void 0, void 0, function* () {
124
+ const resp = yield this.findMatchingResponse(req);
125
+ if (resp) {
126
+ this.processedRequests.push([req, resp]);
127
+ return resp;
128
+ }
129
+ return new Response(null, { status: HTTPStatus.NOT_FOUND });
130
+ });
131
+ }
132
+ when(request, response) {
133
+ this.matchers.push(this.createMatcher(request, response));
134
+ return this;
135
+ }
136
+ verify(request, validator) {
137
+ return __awaiter(this, void 0, void 0, function* () {
138
+ const matcher = this.requestMatcher(request);
139
+ const responses = yield Promise.all(this.processedRequests.map(([req, resp]) => matcher(req).then((v) => (v ? resp : null))));
140
+ if (!validator(responses.filter((v) => v !== null))) {
141
+ throw new VerificationError();
142
+ }
143
+ });
144
+ }
145
+ reset() {
146
+ this.processedRequests = [];
147
+ }
148
+ findMatchingResponse(request) {
149
+ return __awaiter(this, void 0, void 0, function* () {
150
+ for (const matcher of this.matchers) {
151
+ const resp = yield matcher(request);
152
+ if (resp) {
153
+ return resp;
154
+ }
155
+ }
156
+ });
157
+ }
158
+ requestMatcher(mock) {
159
+ const mockRequest = mock;
160
+ const pathChecker = (req) => mockRequest.path ? new URL(req.url).pathname.startsWith(mockRequest.path) : true;
161
+ const methodChecker = (req) => mockRequest.method ? req.method.toUpperCase() === mockRequest.method : true;
162
+ const headersChecker = (req) => Object.entries(mockRequest.headers).every(([k, v]) => req.headers.get(k) === v);
163
+ const bodyChecker = (req) => {
164
+ if (!mockRequest.body) {
165
+ return Promise.resolve(true);
166
+ }
167
+ const data = typeof mockRequest.body !== "string" ? JSON.stringify(mockRequest.body) : mockRequest.body;
168
+ const buffer = new TextEncoder().encode(data);
169
+ return req.arrayBuffer().then((v) => {
170
+ const data = new Uint8Array(v);
171
+ for (let i = 0; i < buffer.length; i++) {
172
+ if (buffer.at(i) !== data.at(i)) {
173
+ return false;
174
+ }
175
+ }
176
+ return true;
177
+ });
178
+ };
179
+ const queriesChecker = (req) => {
180
+ const params = new URL(req.url).searchParams;
181
+ return Object.entries(mockRequest.queries).every(([k, v]) => params.get(k) === v);
182
+ };
183
+ return (req) => bodyChecker(req).then((result) => result && queriesChecker(req) && headersChecker(req) && pathChecker(req) && methodChecker(req));
184
+ }
185
+ createMatcher(mockRequest, response) {
186
+ const matcher = this.requestMatcher(mockRequest);
187
+ return (request) => matcher(request).then((match) => __awaiter(this, void 0, void 0, function* () {
188
+ if (match) {
189
+ const { response: resp, delay } = response.build();
190
+ if (delay) {
191
+ yield Future.sleep(delay);
192
+ }
193
+ return resp;
194
+ }
195
+ }));
196
+ }
197
+ }
198
+ /**
199
+ * Mocks network requests send via fetch API
200
+ */
201
+ class MockNetworkService {
202
+ /**
203
+ * @param blockUnmatchedOrigins check whether to block requests sent to origins not specified as
204
+ * an API in this network service
205
+ */
206
+ constructor(blockUnmatchedOrigins = true) {
207
+ this.blockUnmatchedOrigins = blockUnmatchedOrigins;
208
+ this.realFetch = global.fetch;
209
+ this.apis = new Map();
210
+ }
211
+ /**
212
+ * Launches the network service to monitor requests
213
+ */
214
+ start() {
215
+ this.future = Future.of((resolve, reject, signal) => {
216
+ signal.onabort = () => {
217
+ global.fetch = this.realFetch;
218
+ reject(new AbortError());
219
+ };
220
+ global.fetch = (input, init) => {
221
+ const req = new Request(input, init);
222
+ const uri = new URL(req.url);
223
+ const api = this.apis.get(uri.origin);
224
+ if (!api && this.blockUnmatchedOrigins) {
225
+ return Promise.resolve(new Response(null, { status: HTTPStatus.NOT_FOUND }));
226
+ }
227
+ else if (!api) {
228
+ return this.realFetch(input, init);
229
+ }
230
+ return api.resolve(req);
231
+ };
232
+ }).schedule();
233
+ }
234
+ /**
235
+ * Stops network monitoring
236
+ */
237
+ stop() {
238
+ if (this.future) {
239
+ this.future.cancel();
240
+ this.future = undefined;
241
+ }
242
+ }
243
+ /**
244
+ * Creates an API service - used to intercept requests for a given origin
245
+ * @param origin
246
+ */
247
+ createAPI(origin) {
248
+ const api = new API();
249
+ this.apis.set(origin, api);
250
+ return api;
251
+ }
252
+ /**
253
+ * Remove all created API services from network request routing
254
+ */
255
+ reset() {
256
+ this.apis.clear();
257
+ }
258
+ }
259
+ /**
260
+ * Validators
261
+ */
262
+ /**
263
+ * Validates that the request was processed at least X times
264
+ * @param amount number of times the request was sent
265
+ */
266
+ const atLeast = (amount) => (matches) => matches.length >= amount;
267
+ /**
268
+ * Validates that the request was process at most X times
269
+ * @param amount number of times the request was sent
270
+ */
271
+ const atMost = (amount) => (matches) => matches.length <= amount;
272
+ /**
273
+ * Validates that the request was processed exactly X times
274
+ * @param amount number of times the request was sent
275
+ */
276
+ const exact = (amount) => (matches) => matches.length === amount;
277
+ /**
278
+ * Validates that the request was processed within the given range of times
279
+ * @param lower minimum times the request matched
280
+ * @param upper maximum times the request matched
281
+ * @param includeUpper if true, the upper limit provided with be included. Otherwise, the maximum will be upper - 1
282
+ */
283
+ const range = (lower, upper, includeUpper = false) => (matches) => matches.length >= lower && (includeUpper ? matches.length <= upper : matches.length < upper);
284
+ /**
285
+ * creates a request details instance that is used to track requests with a given set of criteria
286
+ */
287
+ const request = () => new RequestDetails();
288
+ /**
289
+ * creates a response builder
290
+ */
291
+ const response = () => new ResponseBuilder();
292
+
293
+ export { MockNetworkService, atLeast, atMost, exact, range, request, response };
@@ -0,0 +1,104 @@
1
+ 'use strict';
2
+
3
+ var _tslib = require('../_virtual/_tslib.cjs');
4
+ var NetworkService = require('./NetworkService.cjs');
5
+ var R = require('ramda');
6
+ var CallExecutionError = require('../errors/CallExecutionError.cjs');
7
+ var synchronize = require('../synchronize.cjs');
8
+ require('uuid');
9
+ var result = require('../future/result.cjs');
10
+ var stream = require('../stream.cjs');
11
+ var model = require('../data/model/model.cjs');
12
+ require('../data/model/types.cjs');
13
+ require('../data/model/credentials.cjs');
14
+
15
+ function _interopNamespaceDefault(e) {
16
+ var n = Object.create(null);
17
+ if (e) {
18
+ Object.keys(e).forEach(function (k) {
19
+ if (k !== 'default') {
20
+ var d = Object.getOwnPropertyDescriptor(e, k);
21
+ Object.defineProperty(n, k, d.get ? d : {
22
+ enumerable: true,
23
+ get: function () { return e[k]; }
24
+ });
25
+ }
26
+ });
27
+ }
28
+ n.default = e;
29
+ return Object.freeze(n);
30
+ }
31
+
32
+ var R__namespace = /*#__PURE__*/_interopNamespaceDefault(R);
33
+
34
+ class EventSourceService extends NetworkService.NetworkService {
35
+ constructor(config = {}) {
36
+ super(config);
37
+ this.evtSource = new EventSource(this.baseUrl);
38
+ }
39
+ /**
40
+ * Shutdown the event source connection
41
+ */
42
+ shutdown() {
43
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
44
+ this.evtSource.close();
45
+ });
46
+ }
47
+ __setup__() {
48
+ const internals = this.__internal__;
49
+ const socketMethods = R__namespace.filter(([_, v]) => v.socketReadStream !== undefined, R__namespace.toPairs(internals.methodConfig));
50
+ R__namespace.forEach(([key, config]) => {
51
+ var _a;
52
+ const oldMethod = this[key];
53
+ const details = (_a = internals.methodConfig[key]) === null || _a === void 0 ? void 0 : _a.socketReadStream;
54
+ if (!details) {
55
+ throw new CallExecutionError.CallExecutionError("ReadStream details is missing for EventSource");
56
+ }
57
+ this[key] = () => {
58
+ var _a, _b;
59
+ const queue = [];
60
+ const dataReadyEvt = new synchronize.FutureEvent();
61
+ this.evtSource.addEventListener(details.keyPath instanceof Array ? details.keyPath.join(".") : details.keyPath, (evt) => {
62
+ queue.push(evt.data);
63
+ dataReadyEvt.set();
64
+ });
65
+ let stream$1 = new stream.Stream(() => _tslib.__awaiter(this, void 0, void 0, function* () {
66
+ if (queue.length === 0) {
67
+ dataReadyEvt.clear();
68
+ yield dataReadyEvt.wait();
69
+ }
70
+ return queue.shift();
71
+ }));
72
+ if (((_a = config.socketReadStream) === null || _a === void 0 ? void 0 : _a.skip) !== undefined)
73
+ stream$1 = stream$1.skip(config.socketReadStream.skip);
74
+ if (((_b = config.socketReadStream) === null || _b === void 0 ? void 0 : _b.take) !== undefined)
75
+ stream$1 = stream$1.take(config.socketReadStream.take);
76
+ return stream$1
77
+ .map((data) => {
78
+ if (config.responseClass.prototype instanceof model.Model) {
79
+ const ModelClass = config.responseClass;
80
+ return ModelClass.fromString(data, config.dataFormat);
81
+ }
82
+ return config.responseClass(data, config.dataFormat);
83
+ })
84
+ .flatten()
85
+ .map((v) => _tslib.__awaiter(this, void 0, void 0, function* () {
86
+ let result$1 = oldMethod(v);
87
+ if (result$1 === null || result$1 === undefined)
88
+ return v;
89
+ if (result$1 instanceof Promise)
90
+ result$1 = yield result$1;
91
+ if (result$1 instanceof result.FutureResult)
92
+ result$1 = result$1.value;
93
+ return result$1;
94
+ }));
95
+ };
96
+ }, socketMethods);
97
+ const originalMethodConfig = internals.methodConfig;
98
+ internals.methodConfig = R__namespace.fromPairs(R__namespace.filter(([_, v]) => !(v.socketReadStream && v.socketWriteStream), R__namespace.toPairs(internals.methodConfig)));
99
+ super.__setup__();
100
+ internals.methodConfig = originalMethodConfig;
101
+ }
102
+ }
103
+
104
+ exports.EventSourceService = EventSourceService;
@@ -0,0 +1,11 @@
1
+ import { NetworkService } from "./NetworkService";
2
+ import { GingerSnapProps } from "./index";
3
+ export declare class EventSourceService extends NetworkService {
4
+ private readonly evtSource;
5
+ constructor(config?: GingerSnapProps);
6
+ /**
7
+ * Shutdown the event source connection
8
+ */
9
+ shutdown(): Promise<void>;
10
+ protected __setup__(): void;
11
+ }
@@ -0,0 +1,83 @@
1
+ import { __awaiter } from '../_virtual/_tslib.mjs';
2
+ import { NetworkService } from './NetworkService.mjs';
3
+ import * as R from 'ramda';
4
+ import { CallExecutionError } from '../errors/CallExecutionError.mjs';
5
+ import { FutureEvent } from '../synchronize.mjs';
6
+ import 'uuid';
7
+ import { FutureResult } from '../future/result.mjs';
8
+ import { Stream } from '../stream.mjs';
9
+ import { Model } from '../data/model/model.mjs';
10
+ import '../data/model/types.mjs';
11
+ import '../data/model/credentials.mjs';
12
+
13
+ class EventSourceService extends NetworkService {
14
+ constructor(config = {}) {
15
+ super(config);
16
+ this.evtSource = new EventSource(this.baseUrl);
17
+ }
18
+ /**
19
+ * Shutdown the event source connection
20
+ */
21
+ shutdown() {
22
+ return __awaiter(this, void 0, void 0, function* () {
23
+ this.evtSource.close();
24
+ });
25
+ }
26
+ __setup__() {
27
+ const internals = this.__internal__;
28
+ const socketMethods = R.filter(([_, v]) => v.socketReadStream !== undefined, R.toPairs(internals.methodConfig));
29
+ R.forEach(([key, config]) => {
30
+ var _a;
31
+ const oldMethod = this[key];
32
+ const details = (_a = internals.methodConfig[key]) === null || _a === void 0 ? void 0 : _a.socketReadStream;
33
+ if (!details) {
34
+ throw new CallExecutionError("ReadStream details is missing for EventSource");
35
+ }
36
+ this[key] = () => {
37
+ var _a, _b;
38
+ const queue = [];
39
+ const dataReadyEvt = new FutureEvent();
40
+ this.evtSource.addEventListener(details.keyPath instanceof Array ? details.keyPath.join(".") : details.keyPath, (evt) => {
41
+ queue.push(evt.data);
42
+ dataReadyEvt.set();
43
+ });
44
+ let stream = new Stream(() => __awaiter(this, void 0, void 0, function* () {
45
+ if (queue.length === 0) {
46
+ dataReadyEvt.clear();
47
+ yield dataReadyEvt.wait();
48
+ }
49
+ return queue.shift();
50
+ }));
51
+ if (((_a = config.socketReadStream) === null || _a === void 0 ? void 0 : _a.skip) !== undefined)
52
+ stream = stream.skip(config.socketReadStream.skip);
53
+ if (((_b = config.socketReadStream) === null || _b === void 0 ? void 0 : _b.take) !== undefined)
54
+ stream = stream.take(config.socketReadStream.take);
55
+ return stream
56
+ .map((data) => {
57
+ if (config.responseClass.prototype instanceof Model) {
58
+ const ModelClass = config.responseClass;
59
+ return ModelClass.fromString(data, config.dataFormat);
60
+ }
61
+ return config.responseClass(data, config.dataFormat);
62
+ })
63
+ .flatten()
64
+ .map((v) => __awaiter(this, void 0, void 0, function* () {
65
+ let result = oldMethod(v);
66
+ if (result === null || result === undefined)
67
+ return v;
68
+ if (result instanceof Promise)
69
+ result = yield result;
70
+ if (result instanceof FutureResult)
71
+ result = result.value;
72
+ return result;
73
+ }));
74
+ };
75
+ }, socketMethods);
76
+ const originalMethodConfig = internals.methodConfig;
77
+ internals.methodConfig = R.fromPairs(R.filter(([_, v]) => !(v.socketReadStream && v.socketWriteStream), R.toPairs(internals.methodConfig)));
78
+ super.__setup__();
79
+ internals.methodConfig = originalMethodConfig;
80
+ }
81
+ }
82
+
83
+ export { EventSourceService };