@based/client 3.3.0 → 4.0.1

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 (211) hide show
  1. package/README.md +32 -336
  2. package/dist/Emitter.d.ts +7 -6
  3. package/dist/Emitter.js +31 -6
  4. package/dist/Emitter.js.map +1 -1
  5. package/dist/authState/parseAuthState.d.ts +3 -0
  6. package/dist/authState/parseAuthState.js +51 -0
  7. package/dist/authState/parseAuthState.js.map +1 -0
  8. package/dist/authState/updateAuthState.d.ts +3 -0
  9. package/dist/authState/updateAuthState.js +15 -0
  10. package/dist/authState/updateAuthState.js.map +1 -0
  11. package/dist/channel/cleanUp.d.ts +2 -0
  12. package/dist/channel/cleanUp.js +32 -0
  13. package/dist/channel/cleanUp.js.map +1 -0
  14. package/dist/channel/index.d.ts +12 -0
  15. package/dist/channel/index.js +67 -0
  16. package/dist/channel/index.js.map +1 -0
  17. package/dist/genObserveId.d.ts +1 -0
  18. package/dist/genObserveId.js +12 -0
  19. package/dist/genObserveId.js.map +1 -0
  20. package/dist/getTargetInfo.d.ts +6 -0
  21. package/dist/getTargetInfo.js +14 -0
  22. package/dist/getTargetInfo.js.map +1 -0
  23. package/dist/getUrlFromOpts.d.ts +3 -0
  24. package/dist/getUrlFromOpts.js +24 -0
  25. package/dist/getUrlFromOpts.js.map +1 -0
  26. package/dist/incoming/debug.d.ts +9 -0
  27. package/dist/incoming/debug.js +132 -0
  28. package/dist/incoming/debug.js.map +1 -0
  29. package/dist/incoming/index.d.ts +2 -0
  30. package/dist/incoming/index.js +341 -0
  31. package/dist/incoming/index.js.map +1 -0
  32. package/dist/incoming/protocol.d.ts +9 -0
  33. package/dist/incoming/protocol.js +62 -0
  34. package/dist/incoming/protocol.js.map +1 -0
  35. package/dist/index.d.ts +69 -127
  36. package/dist/index.js +224 -15
  37. package/dist/index.js.map +1 -1
  38. package/dist/outgoing/debug.d.ts +8 -0
  39. package/dist/outgoing/debug.js +70 -0
  40. package/dist/outgoing/debug.js.map +1 -0
  41. package/dist/outgoing/index.d.ts +14 -0
  42. package/dist/outgoing/index.js +227 -0
  43. package/dist/outgoing/index.js.map +1 -0
  44. package/dist/outgoing/protocol.d.ts +24 -0
  45. package/dist/outgoing/protocol.js +200 -0
  46. package/dist/outgoing/protocol.js.map +1 -0
  47. package/dist/persistentStorage/browser.d.ts +5 -0
  48. package/dist/persistentStorage/browser.js +150 -0
  49. package/dist/persistentStorage/browser.js.map +1 -0
  50. package/dist/persistentStorage/index.d.ts +6 -0
  51. package/dist/persistentStorage/index.js +56 -0
  52. package/dist/persistentStorage/index.js.map +1 -0
  53. package/dist/persistentStorage/node.d.ts +6 -0
  54. package/dist/persistentStorage/node.js +104 -0
  55. package/dist/persistentStorage/node.js.map +1 -0
  56. package/dist/query/index.d.ts +17 -0
  57. package/dist/query/index.js +112 -0
  58. package/dist/query/index.js.map +1 -0
  59. package/dist/stream/fetch.d.ts +4 -0
  60. package/dist/stream/fetch.js +42 -0
  61. package/dist/stream/fetch.js.map +1 -0
  62. package/dist/stream/index.d.ts +4 -0
  63. package/dist/stream/index.js +65 -0
  64. package/dist/stream/index.js.map +1 -0
  65. package/dist/stream/nodeStream.d.ts +7 -0
  66. package/dist/stream/nodeStream.js +117 -0
  67. package/dist/stream/nodeStream.js.map +1 -0
  68. package/dist/stream/types.d.ts +37 -0
  69. package/dist/stream/types.js +8 -0
  70. package/dist/stream/types.js.map +1 -0
  71. package/dist/stream/uploadFileBrowser.d.ts +4 -0
  72. package/dist/stream/uploadFileBrowser.js +113 -0
  73. package/dist/stream/uploadFileBrowser.js.map +1 -0
  74. package/dist/types/auth.d.ts +11 -0
  75. package/dist/{selvaTypes/get.js → types/auth.js} +1 -1
  76. package/dist/types/auth.js.map +1 -0
  77. package/dist/types/cache.d.ts +6 -0
  78. package/dist/{selvaTypes/set.js → types/cache.js} +1 -1
  79. package/dist/types/cache.js.map +1 -0
  80. package/dist/types/channel.d.ts +25 -0
  81. package/dist/types/channel.js +3 -0
  82. package/dist/types/channel.js.map +1 -0
  83. package/dist/types/error.d.ts +36 -0
  84. package/dist/types/error.js +40 -0
  85. package/dist/types/error.js.map +1 -0
  86. package/dist/types/events.d.ts +34 -0
  87. package/dist/{selvaTypes/schema.js → types/events.js} +1 -1
  88. package/dist/types/events.js.map +1 -0
  89. package/dist/types/functions.d.ts +8 -0
  90. package/dist/types/functions.js +3 -0
  91. package/dist/types/functions.js.map +1 -0
  92. package/dist/types/generic.d.ts +20 -0
  93. package/dist/types/generic.js +3 -0
  94. package/dist/types/generic.js.map +1 -0
  95. package/dist/types/index.d.ts +6 -0
  96. package/dist/{subscriptions → types}/index.js +6 -10
  97. package/dist/types/index.js.map +1 -0
  98. package/dist/types/observe.d.ts +46 -0
  99. package/dist/types/observe.js +3 -0
  100. package/dist/types/observe.js.map +1 -0
  101. package/dist/websocket/index.d.ts +1 -1
  102. package/dist/websocket/index.js +14 -5
  103. package/dist/websocket/index.js.map +1 -1
  104. package/dist/websocket/types.d.ts +0 -0
  105. package/dist/websocket/types.js +0 -0
  106. package/dist/websocket/types.js.map +0 -0
  107. package/dist/websocket/urlLoader.d.ts +0 -0
  108. package/dist/websocket/urlLoader.js +0 -0
  109. package/dist/websocket/urlLoader.js.map +0 -0
  110. package/package.json +17 -27
  111. package/dist/Client.d.ts +0 -80
  112. package/dist/Client.js +0 -271
  113. package/dist/Client.js.map +0 -1
  114. package/dist/auth.d.ts +0 -13
  115. package/dist/auth.js +0 -87
  116. package/dist/auth.js.map +0 -1
  117. package/dist/createError.d.ts +0 -3
  118. package/dist/createError.js +0 -67
  119. package/dist/createError.js.map +0 -1
  120. package/dist/debug.d.ts +0 -2
  121. package/dist/debug.js +0 -21
  122. package/dist/debug.js.map +0 -1
  123. package/dist/file/fetch.d.ts +0 -6
  124. package/dist/file/fetch.js +0 -27
  125. package/dist/file/fetch.js.map +0 -1
  126. package/dist/file/getUrl.d.ts +0 -4
  127. package/dist/file/getUrl.js +0 -42
  128. package/dist/file/getUrl.js.map +0 -1
  129. package/dist/file/index.d.ts +0 -6
  130. package/dist/file/index.js +0 -95
  131. package/dist/file/index.js.map +0 -1
  132. package/dist/file/stream/browser.d.ts +0 -7
  133. package/dist/file/stream/browser.js +0 -17
  134. package/dist/file/stream/browser.js.map +0 -1
  135. package/dist/file/stream/index.d.ts +0 -7
  136. package/dist/file/stream/index.js +0 -86
  137. package/dist/file/stream/index.js.map +0 -1
  138. package/dist/file/uploadFileBrowser.d.ts +0 -3
  139. package/dist/file/uploadFileBrowser.js +0 -72
  140. package/dist/file/uploadFileBrowser.js.map +0 -1
  141. package/dist/findPrefix.d.ts +0 -3
  142. package/dist/findPrefix.js +0 -20
  143. package/dist/findPrefix.js.map +0 -1
  144. package/dist/idleTimeout.d.ts +0 -3
  145. package/dist/idleTimeout.js +0 -17
  146. package/dist/idleTimeout.js.map +0 -1
  147. package/dist/observable/index.d.ts +0 -28
  148. package/dist/observable/index.js +0 -84
  149. package/dist/observable/index.js.map +0 -1
  150. package/dist/printBasedObject.d.ts +0 -4
  151. package/dist/printBasedObject.js +0 -76
  152. package/dist/printBasedObject.js.map +0 -1
  153. package/dist/queue.d.ts +0 -5
  154. package/dist/queue.js +0 -53
  155. package/dist/queue.js.map +0 -1
  156. package/dist/request.d.ts +0 -6
  157. package/dist/request.js +0 -67
  158. package/dist/request.js.map +0 -1
  159. package/dist/selvaTypes/get.d.ts +0 -97
  160. package/dist/selvaTypes/get.js.map +0 -1
  161. package/dist/selvaTypes/schema.d.ts +0 -89
  162. package/dist/selvaTypes/schema.js.map +0 -1
  163. package/dist/selvaTypes/set.d.ts +0 -73
  164. package/dist/selvaTypes/set.js.map +0 -1
  165. package/dist/subscriptions/addGetSubscriber.d.ts +0 -3
  166. package/dist/subscriptions/addGetSubscriber.js +0 -87
  167. package/dist/subscriptions/addGetSubscriber.js.map +0 -1
  168. package/dist/subscriptions/addSubscriber.d.ts +0 -2
  169. package/dist/subscriptions/addSubscriber.js +0 -136
  170. package/dist/subscriptions/addSubscriber.js.map +0 -1
  171. package/dist/subscriptions/generateId.d.ts +0 -1
  172. package/dist/subscriptions/generateId.js +0 -11
  173. package/dist/subscriptions/generateId.js.map +0 -1
  174. package/dist/subscriptions/incomingSubscription.d.ts +0 -3
  175. package/dist/subscriptions/incomingSubscription.js +0 -87
  176. package/dist/subscriptions/incomingSubscription.js.map +0 -1
  177. package/dist/subscriptions/incomingSubscriptionDiff.d.ts +0 -3
  178. package/dist/subscriptions/incomingSubscriptionDiff.js +0 -85
  179. package/dist/subscriptions/incomingSubscriptionDiff.js.map +0 -1
  180. package/dist/subscriptions/index.d.ts +0 -10
  181. package/dist/subscriptions/index.js.map +0 -1
  182. package/dist/subscriptions/logoutSubscriptions.d.ts +0 -3
  183. package/dist/subscriptions/logoutSubscriptions.js +0 -36
  184. package/dist/subscriptions/logoutSubscriptions.js.map +0 -1
  185. package/dist/subscriptions/removeSendSubscriptionDataFromQueue.d.ts +0 -2
  186. package/dist/subscriptions/removeSendSubscriptionDataFromQueue.js +0 -14
  187. package/dist/subscriptions/removeSendSubscriptionDataFromQueue.js.map +0 -1
  188. package/dist/subscriptions/removeSubscriber.d.ts +0 -2
  189. package/dist/subscriptions/removeSubscriber.js +0 -44
  190. package/dist/subscriptions/removeSubscriber.js.map +0 -1
  191. package/dist/subscriptions/removeUnsubscribesFromQueue.d.ts +0 -2
  192. package/dist/subscriptions/removeUnsubscribesFromQueue.js +0 -14
  193. package/dist/subscriptions/removeUnsubscribesFromQueue.js.map +0 -1
  194. package/dist/subscriptions/sendAllSubscriptions.d.ts +0 -2
  195. package/dist/subscriptions/sendAllSubscriptions.js +0 -114
  196. package/dist/subscriptions/sendAllSubscriptions.js.map +0 -1
  197. package/dist/token.d.ts +0 -4
  198. package/dist/token.js +0 -35
  199. package/dist/token.js.map +0 -1
  200. package/dist/track.d.ts +0 -8
  201. package/dist/track.js +0 -71
  202. package/dist/track.js.map +0 -1
  203. package/docs/auth-based-ui-howto.md +0 -74
  204. package/docs/auth-howto.md +0 -166
  205. package/docs/auth.md +0 -93
  206. package/docs/authorize.md +0 -56
  207. package/docs/files.md +0 -66
  208. package/docs/get-started.md +0 -60
  209. package/docs/get.md +0 -759
  210. package/docs/schema.md +0 -244
  211. package/docs/set.md +0 -652
@@ -0,0 +1,67 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BasedChannel = void 0;
4
+ const genObserveId_1 = require("../genObserveId");
5
+ const outgoing_1 = require("../outgoing");
6
+ const cleanUp_1 = require("./cleanUp");
7
+ class BasedChannel {
8
+ constructor(client, name, payload) {
9
+ this.payload = payload;
10
+ this.id = (0, genObserveId_1.genObserveId)(name, payload);
11
+ this.client = client;
12
+ this.name = name;
13
+ }
14
+ subscribe(onMessage, onError) {
15
+ let subscriberId;
16
+ if (!this.client.channelState.has(this.id) ||
17
+ this.client.channelState.get(this.id).subscribers.size === 0) {
18
+ subscriberId = 1;
19
+ const subscribers = new Map();
20
+ subscribers.set(subscriberId, { onMessage, onError });
21
+ this.client.channelState.set(this.id, {
22
+ payload: this.payload,
23
+ name: this.name,
24
+ subscribers,
25
+ removeTimer: -1,
26
+ });
27
+ (0, outgoing_1.addChannelSubscribeToQueue)(this.client, this.name, this.id, this.payload);
28
+ }
29
+ else {
30
+ const channel = this.client.channelState.get(this.id);
31
+ channel.removeTimer = -1;
32
+ subscriberId = channel.subscribers.size + 1;
33
+ channel.subscribers.set(subscriberId, { onMessage, onError });
34
+ }
35
+ return () => {
36
+ const channel = this.client.channelState.get(this.id);
37
+ channel.subscribers.delete(subscriberId);
38
+ if (channel.subscribers.size === 0) {
39
+ channel.removeTimer = 2;
40
+ (0, outgoing_1.addChannelCloseToQueue)(this.client, this.id);
41
+ }
42
+ };
43
+ }
44
+ publish(message) {
45
+ if (!this.client.channelState.has(this.id)) {
46
+ // This is a perf optmization to not send payload + name
47
+ this.client.channelState.set(this.id, {
48
+ payload: this.payload,
49
+ name: this.name,
50
+ subscribers: new Map(),
51
+ removeTimer: 2, // 2x 30sec
52
+ });
53
+ (0, cleanUp_1.cleanUpChannels)(this.client);
54
+ (0, outgoing_1.addChannelPublishIdentifier)(this.client, this.name, this.id, this.payload);
55
+ }
56
+ else {
57
+ const channel = this.client.channelState.get(this.id);
58
+ if (channel.removeTimer !== -1 && channel.removeTimer < 2) {
59
+ channel.removeTimer = 2; // 2x 30sec
60
+ (0, cleanUp_1.cleanUpChannels)(this.client);
61
+ }
62
+ }
63
+ (0, outgoing_1.addToPublishQueue)(this.client, this.id, message);
64
+ }
65
+ }
66
+ exports.BasedChannel = BasedChannel;
67
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/channel/index.ts"],"names":[],"mappings":";;;AACA,kDAA8C;AAC9C,0CAKoB;AAGpB,uCAA2C;AAE3C,MAAa,YAAY;IAMvB,YAAY,MAAmB,EAAE,IAAY,EAAE,OAAU;QACvD,IAAI,CAAC,OAAO,GAAG,OAAO,CAAA;QACtB,IAAI,CAAC,EAAE,GAAG,IAAA,2BAAY,EAAC,IAAI,EAAE,OAAO,CAAC,CAAA;QACrC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAA;QACpB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAA;IAClB,CAAC;IAED,SAAS,CACP,SAAiC,EACjC,OAAmC;QAEnC,IAAI,YAAoB,CAAA;QACxB,IACE,CAAC,IAAI,CAAC,MAAM,CAAC,YAAY,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC;YACtC,IAAI,CAAC,MAAM,CAAC,YAAY,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,WAAW,CAAC,IAAI,KAAK,CAAC,EAC5D;YACA,YAAY,GAAG,CAAC,CAAA;YAChB,MAAM,WAAW,GAAG,IAAI,GAAG,EAAE,CAAA;YAC7B,WAAW,CAAC,GAAG,CAAC,YAAY,EAAE,EAAE,SAAS,EAAE,OAAO,EAAE,CAAC,CAA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,MAAM,CAAC,YAAY,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,EAAE;gBACpC,OAAO,EAAE,IAAI,CAAC,OAAO;gBACrB,IAAI,EAAE,IAAI,CAAC,IAAI;gBACf,WAAW,EAAE,IAAI,GAAG,EAAE;gBACtB,WAAW,EAAE,CAAC,EAAE,WAAW;aAC5B,CAAC,CAAA;YACF,IAAA,yBAAe,EAAC,IAAI,CAAC,MAAM,CAAC,CAAA;YAC5B,IAAA,sCAA2B,EAAC,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,EAAE,EAAE,IAAI,CAAC,OAAO,CAAC,CAAA;SAC3E;aAAM;YACL,MAAM,OAAO,GAAG,IAAI,CAAC,MAAM,CAAC,YAAY,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,CAAA;YACrD,IAAI,OAAO,CAAC,WAAW,KAAK,CAAC,CAAC,IAAI,OAAO,CAAC,WAAW,GAAG,CAAC,EAAE;gBACzD,OAAO,CAAC,WAAW,GAAG,CAAC,CAAA,CAAC,WAAW;gBACnC,IAAA,yBAAe,EAAC,IAAI,CAAC,MAAM,CAAC,CAAA;aAC7B;SACF;QACD,IAAA,4BAAiB,EAAC,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,EAAE,EAAE,OAAO,CAAC,CAAA;IAClD,CAAC;CACF;AArED,oCAqEC"}
@@ -0,0 +1 @@
1
+ export declare const genObserveId: (name: string, payload: any) => number;
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.genObserveId = void 0;
4
+ const hash_1 = require("@saulx/hash");
5
+ const genObserveId = (name, payload) => {
6
+ if (payload === undefined) {
7
+ return (0, hash_1.hash)(name);
8
+ }
9
+ return (0, hash_1.hashObjectIgnoreKeyOrder)([name, payload]);
10
+ };
11
+ exports.genObserveId = genObserveId;
12
+ //# sourceMappingURL=genObserveId.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"genObserveId.js","sourceRoot":"","sources":["../src/genObserveId.ts"],"names":[],"mappings":";;;AAAA,sCAA4D;AAErD,MAAM,YAAY,GAAG,CAAC,IAAY,EAAE,OAAY,EAAU,EAAE;IACjE,IAAI,OAAO,KAAK,SAAS,EAAE;QACzB,OAAO,IAAA,WAAI,EAAC,IAAI,CAAC,CAAA;KAClB;IACD,OAAO,IAAA,+BAAwB,EAAC,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC,CAAA;AAClD,CAAC,CAAA;AALY,QAAA,YAAY,gBAKxB"}
@@ -0,0 +1,6 @@
1
+ import { BasedClient } from '.';
2
+ export declare const getTargetInfo: (client: BasedClient, id: number, type: 'channel' | 'sub') => {
3
+ name: string;
4
+ payload?: any;
5
+ id: number;
6
+ };
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getTargetInfo = void 0;
4
+ const getTargetInfo = (client, id, type) => {
5
+ const sub = type === 'sub' ? client.observeState.get(id) : client.channelState.get(id);
6
+ if (!sub) {
7
+ return { name: `[Cannot find ${id}]`, id };
8
+ }
9
+ return sub.payload
10
+ ? { name: sub.name, payload: sub.payload, id }
11
+ : { name: sub.name, id };
12
+ };
13
+ exports.getTargetInfo = getTargetInfo;
14
+ //# sourceMappingURL=getTargetInfo.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getTargetInfo.js","sourceRoot":"","sources":["../src/getTargetInfo.ts"],"names":[],"mappings":";;;AAEO,MAAM,aAAa,GAAG,CAC3B,MAAmB,EACnB,EAAU,EACV,IAAuB,EACsB,EAAE;IAC/C,MAAM,GAAG,GACP,IAAI,KAAK,KAAK,CAAC,CAAC,CAAC,MAAM,CAAC,YAAY,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,YAAY,CAAC,GAAG,CAAC,EAAE,CAAC,CAAA;IAC5E,IAAI,CAAC,GAAG,EAAE;QACR,OAAO,EAAE,IAAI,EAAE,gBAAgB,EAAE,GAAG,EAAE,EAAE,EAAE,CAAA;KAC3C;IACD,OAAO,GAAG,CAAC,OAAO;QAChB,CAAC,CAAC,EAAE,IAAI,EAAE,GAAG,CAAC,IAAI,EAAE,OAAO,EAAE,GAAG,CAAC,OAAO,EAAE,EAAE,EAAE;QAC9C,CAAC,CAAC,EAAE,IAAI,EAAE,GAAG,CAAC,IAAI,EAAE,EAAE,EAAE,CAAA;AAC5B,CAAC,CAAA;AAbY,QAAA,aAAa,iBAazB"}
@@ -0,0 +1,3 @@
1
+ import { BasedOpts } from './types';
2
+ declare const _default: (opts: BasedOpts) => Promise<string | (() => Promise<string>)>;
3
+ export default _default;
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const cross_fetch_1 = __importDefault(require("cross-fetch"));
7
+ exports.default = async (opts) => {
8
+ if (opts.url) {
9
+ return opts.url;
10
+ }
11
+ const discoveryUrls = opts.discoveryUrls || [
12
+ `https://${opts.org}-${opts.project}-${opts.env}.based.io`,
13
+ ];
14
+ for (const hubDiscoveryUrl of discoveryUrls) {
15
+ const dUrl = `${hubDiscoveryUrl}/status?hub=${opts.name || '@based/env-hub'}&key=${opts.key}`;
16
+ const res = await (0, cross_fetch_1.default)(dUrl);
17
+ if (res.ok) {
18
+ const hubs = (await res.json());
19
+ const [hubUrl] = hubs[~~(Math.random() * hubs.length)];
20
+ return `ws://${hubUrl}`;
21
+ }
22
+ }
23
+ };
24
+ //# sourceMappingURL=getUrlFromOpts.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getUrlFromOpts.js","sourceRoot":"","sources":["../src/getUrlFromOpts.ts"],"names":[],"mappings":";;;;;AACA,8DAA+B;AAE/B,kBAAe,KAAK,EAClB,IAAe,EAC4B,EAAE;IAC7C,IAAI,IAAI,CAAC,GAAG,EAAE;QACZ,OAAO,IAAI,CAAC,GAAG,CAAA;KAChB;IAED,MAAM,aAAa,GAAG,IAAI,CAAC,aAAa,IAAI;QAC1C,WAAW,IAAI,CAAC,GAAG,IAAI,IAAI,CAAC,OAAO,IAAI,IAAI,CAAC,GAAG,WAAW;KAC3D,CAAA;IAED,KAAK,MAAM,eAAe,IAAI,aAAa,EAAE;QAC3C,MAAM,IAAI,GAAG,GAAG,eAAe,eAC7B,IAAI,CAAC,IAAI,IAAI,gBACf,QAAQ,IAAI,CAAC,GAAG,EAAE,CAAA;QAClB,MAAM,GAAG,GAAG,MAAM,IAAA,qBAAK,EAAC,IAAI,CAAC,CAAA;QAE7B,IAAI,GAAG,CAAC,EAAE,EAAE;YACV,MAAM,IAAI,GAAG,CAAC,MAAM,GAAG,CAAC,IAAI,EAAE,CAAuB,CAAA;YACrD,MAAM,CAAC,MAAM,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,IAAI,CAAC,MAAM,CAAC,CAAC,CAAA;YACtD,OAAO,QAAQ,MAAM,EAAE,CAAA;SACxB;KACF;AACH,CAAC,CAAA"}
@@ -0,0 +1,9 @@
1
+ import { BasedClient } from '..';
2
+ export declare const debugChannelReqId: (client: BasedClient, id: number, type: 'register' | 'not-found' | 'publish', buffer?: Uint8Array, isDeflate?: boolean) => void;
3
+ export declare const debugFunction: (client: BasedClient, payload: any, id: number) => void;
4
+ export declare const debugDiff: (client: BasedClient, payload: any, id: number, checksum: number, corrupt?: boolean) => void;
5
+ export declare const debugGet: (client: BasedClient, id: number) => void;
6
+ export declare const debugSubscribe: (client: BasedClient, id: number, payload: any, checksum: number, found: boolean) => void;
7
+ export declare const debugAuth: (client: BasedClient, payload: any) => void;
8
+ export declare const debugError: (client: BasedClient, payload: any) => void;
9
+ export declare const debugChannel: (client: BasedClient, id: number, payload: any, found: boolean) => void;
@@ -0,0 +1,132 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.debugChannel = exports.debugError = exports.debugAuth = exports.debugSubscribe = exports.debugGet = exports.debugDiff = exports.debugFunction = exports.debugChannelReqId = void 0;
7
+ const fflate_1 = __importDefault(require("fflate"));
8
+ const getTargetInfo_1 = require("../getTargetInfo");
9
+ const debugChannelReqId = (client, id, type, buffer, isDeflate) => {
10
+ if (type === 'not-found') {
11
+ client.emit('debug', {
12
+ type: 'registerChannelId',
13
+ direction: 'incoming',
14
+ target: { id },
15
+ msg: 'Cannot find channel',
16
+ });
17
+ return;
18
+ }
19
+ const target = (0, getTargetInfo_1.getTargetInfo)(client, id, 'channel');
20
+ if (type === 'register') {
21
+ client.emit('debug', {
22
+ type: 'registerChannelId',
23
+ direction: 'outgoing',
24
+ target,
25
+ });
26
+ return;
27
+ }
28
+ if (!buffer) {
29
+ return;
30
+ }
31
+ let r;
32
+ let p;
33
+ const v = buffer.slice(12);
34
+ if (isDeflate) {
35
+ p = new TextDecoder().decode(fflate_1.default.inflateSync(v));
36
+ }
37
+ else {
38
+ p = new TextDecoder().decode(v);
39
+ }
40
+ try {
41
+ r = p ? JSON.parse(p) : undefined;
42
+ }
43
+ catch (e) {
44
+ r = p;
45
+ }
46
+ client.emit('debug', {
47
+ type: 'rePublishChannel',
48
+ direction: 'outgoing',
49
+ target,
50
+ payload: r,
51
+ });
52
+ };
53
+ exports.debugChannelReqId = debugChannelReqId;
54
+ const debugFunction = (client, payload, id) => {
55
+ client.emit('debug', {
56
+ type: 'function',
57
+ direction: 'incoming',
58
+ payload,
59
+ target: { id },
60
+ });
61
+ };
62
+ exports.debugFunction = debugFunction;
63
+ const debugDiff = (client, payload, id, checksum, corrupt) => {
64
+ if (corrupt) {
65
+ client.emit('debug', {
66
+ type: 'subscriptionDiff',
67
+ direction: 'incoming',
68
+ payload,
69
+ checksum,
70
+ target: (0, getTargetInfo_1.getTargetInfo)(client, id, 'sub'),
71
+ msg: 'Cannot apply corrupt patch',
72
+ });
73
+ return;
74
+ }
75
+ client.emit('debug', {
76
+ type: 'subscriptionDiff',
77
+ direction: 'incoming',
78
+ checksum,
79
+ payload,
80
+ target: (0, getTargetInfo_1.getTargetInfo)(client, id, 'sub'),
81
+ });
82
+ };
83
+ exports.debugDiff = debugDiff;
84
+ const debugGet = (client, id) => {
85
+ client.emit('debug', {
86
+ type: 'get',
87
+ direction: 'incoming',
88
+ target: { id },
89
+ msg: 'Cache is up to date',
90
+ });
91
+ };
92
+ exports.debugGet = debugGet;
93
+ const debugSubscribe = (client, id, payload, checksum, found) => {
94
+ client.emit('debug', {
95
+ type: 'subscribe',
96
+ direction: 'incoming',
97
+ payload,
98
+ checksum,
99
+ target: (0, getTargetInfo_1.getTargetInfo)(client, id, 'sub'),
100
+ ...(!found ? { msg: 'Cannot find subscription handler' } : undefined),
101
+ });
102
+ };
103
+ exports.debugSubscribe = debugSubscribe;
104
+ const debugAuth = (client, payload) => {
105
+ client.emit('debug', {
106
+ type: 'auth',
107
+ direction: 'incoming',
108
+ payload,
109
+ target: {},
110
+ });
111
+ };
112
+ exports.debugAuth = debugAuth;
113
+ const debugError = (client, payload) => {
114
+ client.emit('debug', {
115
+ type: 'error',
116
+ direction: 'incoming',
117
+ payload,
118
+ target: {},
119
+ });
120
+ };
121
+ exports.debugError = debugError;
122
+ const debugChannel = (client, id, payload, found) => {
123
+ client.emit('debug', {
124
+ type: 'channelMessage',
125
+ direction: 'incoming',
126
+ payload,
127
+ target: (0, getTargetInfo_1.getTargetInfo)(client, id, 'channel'),
128
+ ...(!found ? { msg: 'Cannot find channel handler' } : undefined),
129
+ });
130
+ };
131
+ exports.debugChannel = debugChannel;
132
+ //# sourceMappingURL=debug.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"debug.js","sourceRoot":"","sources":["../../src/incoming/debug.ts"],"names":[],"mappings":";;;;;;AACA,oDAA2B;AAC3B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E,EAAE,EAAE,SAAS,CAAC;QAC5C,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,GAAG,EAAE,6BAA6B,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC;KACjE,CAAC,CAAA;AACJ,CAAC,CAAA;AAbY,QAAA,YAAY,gBAaxB"}
@@ -0,0 +1,2 @@
1
+ import { BasedClient } from '..';
2
+ export declare const incoming: (client: BasedClient, data: any) => Promise<void>;
@@ -0,0 +1,341 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.incoming = void 0;
7
+ const fflate_1 = __importDefault(require("fflate"));
8
+ const diff_1 = require("@saulx/diff");
9
+ const error_1 = require("../types/error");
10
+ const utils_1 = require("@saulx/utils");
11
+ const updateAuthState_1 = require("../authState/updateAuthState");
12
+ const persistentStorage_1 = require("../persistentStorage");
13
+ const debug_1 = require("./debug");
14
+ const protocol_1 = require("./protocol");
15
+ const protocol_2 = require("../outgoing/protocol");
16
+ const getTargetInfo_1 = require("../getTargetInfo");
17
+ const incoming = async (client, data /* TODO: type */) => {
18
+ const debug = client.listeners.debug;
19
+ try {
20
+ const d = data.data;
21
+ const buffer = await (0, protocol_1.parseArrayBuffer)(d);
22
+ const { type, len, isDeflate } = (0, protocol_1.decodeHeader)((0, protocol_1.readUint8)(buffer, 0, 4));
23
+ // reader for batched replies
24
+ // ------- Function
25
+ if (type === 0) {
26
+ // | 4 header | 3 id | * payload |
27
+ const id = (0, protocol_1.readUint8)(buffer, 4, 3);
28
+ const start = 7;
29
+ const end = len + 4;
30
+ let payload;
31
+ // if not empty response, parse it
32
+ if (len !== 3) {
33
+ payload = JSON.parse(new TextDecoder().decode(isDeflate
34
+ ? fflate_1.default.inflateSync(buffer.slice(start, end))
35
+ : buffer.slice(start, end)));
36
+ }
37
+ if (client.functionResponseListeners.has(id)) {
38
+ client.functionResponseListeners.get(id)[0](payload);
39
+ client.functionResponseListeners.delete(id);
40
+ }
41
+ if (debug) {
42
+ (0, debug_1.debugFunction)(client, payload, id);
43
+ }
44
+ }
45
+ // ------- Get checksum is up to date
46
+ else if (type === 3) {
47
+ // | 4 header | 8 id |
48
+ const id = (0, protocol_1.readUint8)(buffer, 4, 8);
49
+ if (client.getState.has(id) && client.cache.has(id)) {
50
+ const get = client.getState.get(id);
51
+ for (const [resolve] of get) {
52
+ resolve(client.cache.get(id).value);
53
+ }
54
+ client.getState.delete(id);
55
+ }
56
+ if (debug) {
57
+ (0, debug_1.debugGet)(client, id);
58
+ }
59
+ }
60
+ // ------- Subscription diff data
61
+ else if (type === 2) {
62
+ // | 4 header | 8 id | 8 checksum | 8 previousChecksum | * diff |
63
+ const id = (0, protocol_1.readUint8)(buffer, 4, 8);
64
+ const cachedData = client.cache.get(id);
65
+ if (!cachedData) {
66
+ (0, protocol_1.requestFullData)(client, id);
67
+ return;
68
+ }
69
+ const checksum = (0, protocol_1.readUint8)(buffer, 12, 8);
70
+ const previousChecksum = (0, protocol_1.readUint8)(buffer, 20, 8);
71
+ if (cachedData.checksum !== previousChecksum) {
72
+ (0, protocol_1.requestFullData)(client, id);
73
+ return;
74
+ }
75
+ const start = 28;
76
+ const end = len + 4;
77
+ let diff;
78
+ // if not empty response, parse it
79
+ if (len !== 24) {
80
+ diff = JSON.parse(new TextDecoder().decode(isDeflate
81
+ ? fflate_1.default.inflateSync(buffer.slice(start, end))
82
+ : buffer.slice(start, end)));
83
+ }
84
+ try {
85
+ cachedData.value = (0, diff_1.applyPatch)(cachedData.value, diff);
86
+ cachedData.checksum = checksum;
87
+ }
88
+ catch (err) {
89
+ if (debug) {
90
+ (0, debug_1.debugDiff)(client, diff, id, checksum, true);
91
+ }
92
+ (0, protocol_1.requestFullData)(client, id);
93
+ return;
94
+ }
95
+ if (client.observeState.has(id)) {
96
+ const observable = client.observeState.get(id);
97
+ if (observable.persistent) {
98
+ cachedData.persistent = true;
99
+ (0, persistentStorage_1.setStorage)(client, '@based-cache-' + id, cachedData);
100
+ }
101
+ for (const [, handlers] of observable.subscribers) {
102
+ handlers.onData(cachedData.value, checksum);
103
+ }
104
+ }
105
+ if (client.getState.has(id)) {
106
+ const get = client.getState.get(id);
107
+ for (const [resolve] of get) {
108
+ resolve(cachedData.value);
109
+ }
110
+ client.getState.delete(id);
111
+ }
112
+ if (debug) {
113
+ (0, debug_1.debugDiff)(client, diff, checksum, id);
114
+ }
115
+ }
116
+ // ------- Subscription data
117
+ else if (type === 1) {
118
+ // | 4 header | 8 id | 8 checksum | * payload |
119
+ const id = (0, protocol_1.readUint8)(buffer, 4, 8);
120
+ const checksum = (0, protocol_1.readUint8)(buffer, 12, 8);
121
+ const start = 20;
122
+ const end = len + 4;
123
+ let payload;
124
+ // If not empty response, parse it
125
+ if (len !== 16) {
126
+ payload = JSON.parse(new TextDecoder().decode(isDeflate
127
+ ? fflate_1.default.inflateSync(buffer.slice(start, end))
128
+ : buffer.slice(start, end)));
129
+ }
130
+ const cacheData = {
131
+ value: payload,
132
+ checksum,
133
+ };
134
+ client.cache.set(id, cacheData);
135
+ let found = false;
136
+ if (client.observeState.has(id)) {
137
+ const observable = client.observeState.get(id);
138
+ if (observable.persistent) {
139
+ cacheData.persistent = true;
140
+ (0, persistentStorage_1.setStorage)(client, '@based-cache-' + id, cacheData);
141
+ }
142
+ for (const [, handlers] of observable.subscribers) {
143
+ handlers.onData(payload, checksum);
144
+ }
145
+ found = true;
146
+ }
147
+ if (client.getState.has(id)) {
148
+ const get = client.getState.get(id);
149
+ for (const [resolve] of get) {
150
+ resolve(payload);
151
+ }
152
+ client.getState.delete(id);
153
+ found = true;
154
+ }
155
+ if (debug) {
156
+ (0, debug_1.debugSubscribe)(client, id, payload, checksum, found);
157
+ }
158
+ }
159
+ // ------- AuthState
160
+ else if (type === 4) {
161
+ // | 4 header | * payload |
162
+ const start = 4;
163
+ const end = len + 4;
164
+ let payload;
165
+ // if not empty response, parse it
166
+ if (len !== 3) {
167
+ payload = JSON.parse(new TextDecoder().decode(isDeflate
168
+ ? fflate_1.default.inflateSync(buffer.slice(start, end))
169
+ : buffer.slice(start, end)));
170
+ }
171
+ if (payload === true) {
172
+ client.authRequest.resolve?.(client.authState);
173
+ }
174
+ else if ('error' in payload) {
175
+ // make a function updateAuthState
176
+ (0, updateAuthState_1.updateAuthState)(client, payload);
177
+ client.emit('authstate-change', client.authState);
178
+ client.authRequest.reject?.(new Error(payload.error));
179
+ }
180
+ else {
181
+ if (!(0, utils_1.deepEqual)(client.authState, payload)) {
182
+ (0, updateAuthState_1.updateAuthState)(client, payload);
183
+ client.emit('authstate-change', client.authState);
184
+ }
185
+ else {
186
+ (0, updateAuthState_1.updateAuthState)(client, payload);
187
+ }
188
+ client.authRequest.resolve?.(client.authState);
189
+ }
190
+ if (debug) {
191
+ (0, debug_1.debugAuth)(client, payload);
192
+ }
193
+ }
194
+ // ------- Errors
195
+ else if (type === 5) {
196
+ // | 4 header | * payload |
197
+ const start = 4;
198
+ const end = len + 4;
199
+ let payload;
200
+ // if not empty response, parse it
201
+ if (len !== 3) {
202
+ payload = JSON.parse(new TextDecoder().decode(isDeflate
203
+ ? fflate_1.default.inflateSync(buffer.slice(start, end))
204
+ : buffer.slice(start, end)));
205
+ }
206
+ if (payload.requestId) {
207
+ if (client.functionResponseListeners.has(payload.requestId)) {
208
+ const [, reject, stack] = client.functionResponseListeners.get(payload.requestId);
209
+ reject((0, error_1.convertDataToBasedError)(payload, stack));
210
+ client.functionResponseListeners.delete(payload.requestId);
211
+ }
212
+ }
213
+ if (payload.channelId) {
214
+ if (client.channelState.has(payload.channelId)) {
215
+ const error = (0, error_1.convertDataToBasedError)(payload);
216
+ const channel = client.channelState.get(payload.channelId);
217
+ for (const [, handlers] of channel.subscribers) {
218
+ if (handlers.onError) {
219
+ handlers.onError(error);
220
+ }
221
+ else {
222
+ console.error((0, getTargetInfo_1.getTargetInfo)(client, payload.channelId, 'channel'), error);
223
+ }
224
+ }
225
+ }
226
+ }
227
+ if (payload.observableId) {
228
+ client.cache.delete(payload.observableId);
229
+ if (client.observeState.has(payload.observableId)) {
230
+ const error = (0, error_1.convertDataToBasedError)(payload);
231
+ const observable = client.observeState.get(payload.observableId);
232
+ for (const [, handlers] of observable.subscribers) {
233
+ if (handlers.onError) {
234
+ handlers.onError(error);
235
+ }
236
+ else {
237
+ console.error((0, getTargetInfo_1.getTargetInfo)(client, payload.observableId, 'sub'), error);
238
+ }
239
+ }
240
+ }
241
+ if (client.getState.has(payload.observableId)) {
242
+ const error = (0, error_1.convertDataToBasedError)(payload);
243
+ const get = client.getState.get(payload.observableId);
244
+ for (const [, reject] of get) {
245
+ // also add stack
246
+ reject(error);
247
+ }
248
+ client.getState.delete(payload.observableId);
249
+ }
250
+ }
251
+ if (debug) {
252
+ (0, debug_1.debugError)(client, payload);
253
+ }
254
+ // else emit ERROR maybe?
255
+ } // ------- Re-Publish send channel name + payload
256
+ else if (type === 6) {
257
+ // | 4 header | 8 id | * payload |
258
+ // get id add last send on the state
259
+ const id = (0, protocol_1.readUint8)(buffer, 4, 8);
260
+ const channel = client.channelState.get(id);
261
+ if (!id) {
262
+ if (debug) {
263
+ (0, debug_1.debugChannelReqId)(client, id, 'not-found');
264
+ }
265
+ }
266
+ else {
267
+ if (!channel.inTransit) {
268
+ channel.inTransit = true;
269
+ const { buffers, len } = (0, protocol_2.encodeSubscribeChannelMessage)(id, [
270
+ 6,
271
+ channel.name,
272
+ channel.payload,
273
+ ]);
274
+ const n = new Uint8Array(len);
275
+ let c = 0;
276
+ for (const b of buffers) {
277
+ n.set(b, c);
278
+ c += b.length;
279
+ }
280
+ client.connection.ws.send(n);
281
+ if (channel.removeTimer !== -1 && channel.removeTimer < 2) {
282
+ channel.removeTimer += 1;
283
+ }
284
+ setTimeout(() => {
285
+ const channel = client.channelState.get(id);
286
+ if (channel) {
287
+ channel.inTransit = false;
288
+ }
289
+ }, 5e3);
290
+ if (debug) {
291
+ (0, debug_1.debugChannelReqId)(client, id, 'register');
292
+ }
293
+ }
294
+ client.connection.ws.send(buffer);
295
+ if (debug) {
296
+ (0, debug_1.debugChannelReqId)(client, id, 'publish', buffer, isDeflate);
297
+ }
298
+ }
299
+ } // ----------- Channel message
300
+ else if (type === 7) {
301
+ // | 4 header | 1 subType |
302
+ const subType = (0, protocol_1.readUint8)(buffer, 4, 1);
303
+ if (subType === 0) {
304
+ // | 4 header | 1 subType | 8 id | * payload |
305
+ const id = (0, protocol_1.readUint8)(buffer, 5, 8);
306
+ const start = 13;
307
+ const end = len + 5;
308
+ let payload;
309
+ // if not empty response, parse it
310
+ if (len !== 9) {
311
+ const r = new TextDecoder().decode(isDeflate
312
+ ? fflate_1.default.inflateSync(buffer.slice(start, end))
313
+ : buffer.slice(start, end));
314
+ try {
315
+ payload = JSON.parse(r);
316
+ }
317
+ catch (err) {
318
+ payload = r;
319
+ }
320
+ }
321
+ let found = false;
322
+ if (client.channelState.has(id)) {
323
+ const observable = client.channelState.get(id);
324
+ for (const [, handlers] of observable.subscribers) {
325
+ handlers.onMessage(payload);
326
+ }
327
+ found = true;
328
+ }
329
+ if (debug) {
330
+ (0, debug_1.debugChannel)(client, id, payload, found);
331
+ }
332
+ }
333
+ }
334
+ // ---------------------------------
335
+ }
336
+ catch (err) {
337
+ console.error('Error parsing incoming data', err);
338
+ }
339
+ };
340
+ exports.incoming = incoming;
341
+ //# sourceMappingURL=index.js.map