@activeledger/activehybrid 5.0.1 → 5.1.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 (255) hide show
  1. package/README.md +13 -46
  2. package/es/api.d.ts +53 -0
  3. package/es/api.d.ts.map +1 -0
  4. package/es/api.js +365 -0
  5. package/es/api.js.map +1 -0
  6. package/es/bootstrap.d.ts +36 -0
  7. package/es/bootstrap.d.ts.map +1 -0
  8. package/es/bootstrap.js +106 -0
  9. package/es/bootstrap.js.map +1 -0
  10. package/es/commands.d.ts +25 -0
  11. package/es/commands.d.ts.map +1 -0
  12. package/es/commands.js +185 -0
  13. package/es/commands.js.map +1 -0
  14. package/es/config.d.ts +17 -0
  15. package/es/config.d.ts.map +1 -0
  16. package/es/config.js +395 -0
  17. package/es/config.js.map +1 -0
  18. package/es/contracts.d.ts +31 -0
  19. package/es/contracts.d.ts.map +1 -0
  20. package/es/contracts.js +171 -0
  21. package/es/contracts.js.map +1 -0
  22. package/es/divergence.d.ts +8 -0
  23. package/es/divergence.d.ts.map +1 -0
  24. package/es/divergence.js +40 -0
  25. package/es/divergence.js.map +1 -0
  26. package/es/events.d.ts +38 -0
  27. package/es/events.d.ts.map +1 -0
  28. package/es/events.js +144 -0
  29. package/es/events.js.map +1 -0
  30. package/es/feed.d.ts +42 -0
  31. package/es/feed.d.ts.map +1 -0
  32. package/es/feed.js +167 -0
  33. package/es/feed.js.map +1 -0
  34. package/es/http-upstream.d.ts +83 -0
  35. package/es/http-upstream.d.ts.map +1 -0
  36. package/es/http-upstream.js +360 -0
  37. package/es/http-upstream.js.map +1 -0
  38. package/es/index.js +211 -43
  39. package/es/index.js.map +1 -1
  40. package/es/lookup.d.ts +34 -0
  41. package/es/lookup.d.ts.map +1 -0
  42. package/es/lookup.js +200 -0
  43. package/es/lookup.js.map +1 -0
  44. package/es/node.d.ts +43 -0
  45. package/es/node.d.ts.map +1 -0
  46. package/es/node.js +225 -0
  47. package/es/node.js.map +1 -0
  48. package/es/orderer.d.ts +30 -0
  49. package/es/orderer.d.ts.map +1 -0
  50. package/es/orderer.js +134 -0
  51. package/es/orderer.js.map +1 -0
  52. package/es/p2p-upstream.d.ts +47 -0
  53. package/es/p2p-upstream.d.ts.map +1 -0
  54. package/es/p2p-upstream.js +218 -0
  55. package/es/p2p-upstream.js.map +1 -0
  56. package/es/processor.d.ts +62 -0
  57. package/es/processor.d.ts.map +1 -0
  58. package/es/processor.js +640 -0
  59. package/es/processor.js.map +1 -0
  60. package/es/program.d.ts +3 -0
  61. package/es/program.d.ts.map +1 -0
  62. package/es/program.js +57 -0
  63. package/es/program.js.map +1 -0
  64. package/es/pruner.d.ts +47 -0
  65. package/es/pruner.d.ts.map +1 -0
  66. package/es/pruner.js +143 -0
  67. package/es/pruner.js.map +1 -0
  68. package/es/registry.d.ts +43 -0
  69. package/es/registry.d.ts.map +1 -0
  70. package/es/registry.js +49 -0
  71. package/es/registry.js.map +1 -0
  72. package/es/run.d.ts +36 -0
  73. package/es/run.d.ts.map +1 -0
  74. package/es/run.js +158 -0
  75. package/es/run.js.map +1 -0
  76. package/es/submit.d.ts +50 -0
  77. package/es/submit.d.ts.map +1 -0
  78. package/es/submit.js +186 -0
  79. package/es/submit.js.map +1 -0
  80. package/es/types.d.ts +67 -0
  81. package/es/types.d.ts.map +1 -0
  82. package/es/types.js +2 -0
  83. package/es/types.js.map +1 -0
  84. package/es/upstream.d.ts +36 -0
  85. package/es/upstream.d.ts.map +1 -0
  86. package/es/upstream.js +2 -0
  87. package/es/upstream.js.map +1 -0
  88. package/es/verify.d.ts +59 -0
  89. package/es/verify.d.ts.map +1 -0
  90. package/es/verify.js +203 -0
  91. package/es/verify.js.map +1 -0
  92. package/es/workspace.d.ts +4 -0
  93. package/es/workspace.d.ts.map +1 -0
  94. package/es/workspace.js +40 -0
  95. package/es/workspace.js.map +1 -0
  96. package/lib/api.d.ts +53 -0
  97. package/lib/api.d.ts.map +1 -0
  98. package/lib/api.js +402 -0
  99. package/lib/api.js.map +1 -0
  100. package/lib/bootstrap.d.ts +36 -0
  101. package/lib/bootstrap.d.ts.map +1 -0
  102. package/lib/bootstrap.js +116 -0
  103. package/lib/bootstrap.js.map +1 -0
  104. package/lib/commands.d.ts +25 -0
  105. package/lib/commands.d.ts.map +1 -0
  106. package/lib/commands.js +229 -0
  107. package/lib/commands.js.map +1 -0
  108. package/lib/config.d.ts +17 -0
  109. package/lib/config.d.ts.map +1 -0
  110. package/lib/config.js +437 -0
  111. package/lib/config.js.map +1 -0
  112. package/lib/contracts.d.ts +31 -0
  113. package/lib/contracts.d.ts.map +1 -0
  114. package/lib/contracts.js +210 -0
  115. package/lib/contracts.js.map +1 -0
  116. package/lib/divergence.d.ts +8 -0
  117. package/lib/divergence.d.ts.map +1 -0
  118. package/lib/divergence.js +46 -0
  119. package/lib/divergence.js.map +1 -0
  120. package/lib/events.d.ts +38 -0
  121. package/lib/events.d.ts.map +1 -0
  122. package/lib/events.js +152 -0
  123. package/lib/events.js.map +1 -0
  124. package/lib/feed.d.ts +42 -0
  125. package/lib/feed.d.ts.map +1 -0
  126. package/lib/feed.js +173 -0
  127. package/lib/feed.js.map +1 -0
  128. package/lib/http-upstream.d.ts +83 -0
  129. package/lib/http-upstream.d.ts.map +1 -0
  130. package/lib/http-upstream.js +401 -0
  131. package/lib/http-upstream.js.map +1 -0
  132. package/lib/index.js +210 -42
  133. package/lib/index.js.map +1 -1
  134. package/lib/lookup.d.ts +34 -0
  135. package/lib/lookup.d.ts.map +1 -0
  136. package/lib/lookup.js +207 -0
  137. package/lib/lookup.js.map +1 -0
  138. package/lib/node.d.ts +43 -0
  139. package/lib/node.d.ts.map +1 -0
  140. package/lib/node.js +263 -0
  141. package/lib/node.js.map +1 -0
  142. package/lib/orderer.d.ts +30 -0
  143. package/lib/orderer.d.ts.map +1 -0
  144. package/lib/orderer.js +138 -0
  145. package/lib/orderer.js.map +1 -0
  146. package/lib/p2p-upstream.d.ts +47 -0
  147. package/lib/p2p-upstream.d.ts.map +1 -0
  148. package/lib/p2p-upstream.js +222 -0
  149. package/lib/p2p-upstream.js.map +1 -0
  150. package/lib/processor.d.ts +62 -0
  151. package/lib/processor.d.ts.map +1 -0
  152. package/lib/processor.js +644 -0
  153. package/lib/processor.js.map +1 -0
  154. package/lib/program.d.ts +3 -0
  155. package/lib/program.d.ts.map +1 -0
  156. package/lib/program.js +61 -0
  157. package/lib/program.js.map +1 -0
  158. package/lib/pruner.d.ts +47 -0
  159. package/lib/pruner.d.ts.map +1 -0
  160. package/lib/pruner.js +147 -0
  161. package/lib/pruner.js.map +1 -0
  162. package/lib/registry.d.ts +43 -0
  163. package/lib/registry.d.ts.map +1 -0
  164. package/lib/registry.js +58 -0
  165. package/lib/registry.js.map +1 -0
  166. package/lib/run.d.ts +36 -0
  167. package/lib/run.d.ts.map +1 -0
  168. package/lib/run.js +206 -0
  169. package/lib/run.js.map +1 -0
  170. package/lib/submit.d.ts +50 -0
  171. package/lib/submit.d.ts.map +1 -0
  172. package/lib/submit.js +226 -0
  173. package/lib/submit.js.map +1 -0
  174. package/lib/types.d.ts +67 -0
  175. package/lib/types.d.ts.map +1 -0
  176. package/lib/{interfaces/hybrid.interface.js → types.js} +1 -1
  177. package/lib/types.js.map +1 -0
  178. package/lib/upstream.d.ts +36 -0
  179. package/lib/upstream.d.ts.map +1 -0
  180. package/lib/upstream.js +3 -0
  181. package/lib/upstream.js.map +1 -0
  182. package/lib/verify.d.ts +59 -0
  183. package/lib/verify.d.ts.map +1 -0
  184. package/lib/verify.js +218 -0
  185. package/lib/verify.js.map +1 -0
  186. package/lib/workspace.d.ts +4 -0
  187. package/lib/workspace.d.ts.map +1 -0
  188. package/lib/workspace.js +78 -0
  189. package/lib/workspace.js.map +1 -0
  190. package/package.json +14 -14
  191. package/es/contract.d.ts +0 -5
  192. package/es/contract.d.ts.map +0 -1
  193. package/es/contract.js +0 -33
  194. package/es/contract.js.map +0 -1
  195. package/es/contracts/default/contract.d.ts +0 -37
  196. package/es/contracts/default/contract.d.ts.map +0 -1
  197. package/es/contracts/default/contract.js +0 -848
  198. package/es/contracts/default/contract.js.map +0 -1
  199. package/es/contracts/default/fund.d.ts +0 -14
  200. package/es/contracts/default/fund.d.ts.map +0 -1
  201. package/es/contracts/default/fund.js +0 -90
  202. package/es/contracts/default/fund.js.map +0 -1
  203. package/es/contracts/default/namespace.d.ts +0 -10
  204. package/es/contracts/default/namespace.d.ts.map +0 -1
  205. package/es/contracts/default/namespace.js +0 -47
  206. package/es/contracts/default/namespace.js.map +0 -1
  207. package/es/contracts/default/onboard.d.ts +0 -7
  208. package/es/contracts/default/onboard.d.ts.map +0 -1
  209. package/es/contracts/default/onboard.js +0 -45
  210. package/es/contracts/default/onboard.js.map +0 -1
  211. package/es/contracts/default/setup.d.ts +0 -33
  212. package/es/contracts/default/setup.d.ts.map +0 -1
  213. package/es/contracts/default/setup.js +0 -413
  214. package/es/contracts/default/setup.js.map +0 -1
  215. package/es/default.config.json +0 -40
  216. package/es/interfaces/hybrid.interface.d.ts +0 -7
  217. package/es/interfaces/hybrid.interface.d.ts.map +0 -1
  218. package/es/interfaces/hybrid.interface.js +0 -2
  219. package/es/interfaces/hybrid.interface.js.map +0 -1
  220. package/es/server.d.ts +0 -23
  221. package/es/server.d.ts.map +0 -1
  222. package/es/server.js +0 -232
  223. package/es/server.js.map +0 -1
  224. package/lib/contract.d.ts +0 -5
  225. package/lib/contract.d.ts.map +0 -1
  226. package/lib/contract.js +0 -70
  227. package/lib/contract.js.map +0 -1
  228. package/lib/contracts/default/contract.d.ts +0 -37
  229. package/lib/contracts/default/contract.d.ts.map +0 -1
  230. package/lib/contracts/default/contract.js +0 -884
  231. package/lib/contracts/default/contract.js.map +0 -1
  232. package/lib/contracts/default/fund.d.ts +0 -14
  233. package/lib/contracts/default/fund.d.ts.map +0 -1
  234. package/lib/contracts/default/fund.js +0 -93
  235. package/lib/contracts/default/fund.js.map +0 -1
  236. package/lib/contracts/default/namespace.d.ts +0 -10
  237. package/lib/contracts/default/namespace.d.ts.map +0 -1
  238. package/lib/contracts/default/namespace.js +0 -83
  239. package/lib/contracts/default/namespace.js.map +0 -1
  240. package/lib/contracts/default/onboard.d.ts +0 -7
  241. package/lib/contracts/default/onboard.d.ts.map +0 -1
  242. package/lib/contracts/default/onboard.js +0 -48
  243. package/lib/contracts/default/onboard.js.map +0 -1
  244. package/lib/contracts/default/setup.d.ts +0 -33
  245. package/lib/contracts/default/setup.d.ts.map +0 -1
  246. package/lib/contracts/default/setup.js +0 -449
  247. package/lib/contracts/default/setup.js.map +0 -1
  248. package/lib/default.config.json +0 -40
  249. package/lib/interfaces/hybrid.interface.d.ts +0 -7
  250. package/lib/interfaces/hybrid.interface.d.ts.map +0 -1
  251. package/lib/interfaces/hybrid.interface.js.map +0 -1
  252. package/lib/server.d.ts +0 -23
  253. package/lib/server.d.ts.map +0 -1
  254. package/lib/server.js +0 -236
  255. package/lib/server.js.map +0 -1
package/lib/feed.js ADDED
@@ -0,0 +1,173 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FeedManager = exports.atOrPast = exports.cursorKey = void 0;
4
+ const activelogger_1 = require("@activeledger/activelogger");
5
+ const http_upstream_1 = require("./http-upstream");
6
+ const cursorKey = (cursor) => cursor.replace(/^(umid|fail):/, "");
7
+ exports.cursorKey = cursorKey;
8
+ const atOrPast = (cursor, head) => !head || (0, exports.cursorKey)(cursor) >= (0, exports.cursorKey)(head);
9
+ exports.atOrPast = atOrPast;
10
+ const defaultSleep = (ms, signal) => new Promise((resolve) => {
11
+ if (signal.aborted)
12
+ return resolve();
13
+ const timer = setTimeout(done, ms);
14
+ function done() {
15
+ clearTimeout(timer);
16
+ signal.removeEventListener("abort", done);
17
+ resolve();
18
+ }
19
+ signal.addEventListener("abort", done, { once: true });
20
+ });
21
+ class FeedManager {
22
+ options;
23
+ abort;
24
+ running;
25
+ cursor = "";
26
+ headSeen = "";
27
+ retries = [];
28
+ constructor(options) {
29
+ this.options = options;
30
+ }
31
+ now() {
32
+ return (this.options.now ?? Date.now)();
33
+ }
34
+ get head() {
35
+ return this.headSeen;
36
+ }
37
+ start(cursor) {
38
+ if (this.running)
39
+ return;
40
+ this.cursor = cursor;
41
+ this.options.state.cursor = cursor;
42
+ this.abort = new AbortController();
43
+ const signal = this.abort.signal;
44
+ this.running = (async () => {
45
+ try {
46
+ await Promise.all([this.loop(signal), this.retryLoop(signal)]);
47
+ }
48
+ finally {
49
+ this.abort?.abort();
50
+ this.running = undefined;
51
+ }
52
+ })();
53
+ }
54
+ async stop() {
55
+ this.abort?.abort();
56
+ await this.running;
57
+ }
58
+ confirm(head) {
59
+ this.headSeen = head;
60
+ if ((0, exports.atOrPast)(this.cursor, head))
61
+ this.options.state.headConfirmedAt = this.now();
62
+ }
63
+ mark(upstream, ok) {
64
+ const host = upstream.id;
65
+ const entry = this.options.state.upstreams.find((u) => u.host === host);
66
+ if (entry)
67
+ entry.ok = ok;
68
+ else
69
+ this.options.state.upstreams.push({ host, ok });
70
+ }
71
+ hold(item, from, outcome, attempts) {
72
+ if (!outcome.retry || attempts >= (this.options.retryAttempts ?? 5))
73
+ return false;
74
+ this.retries.push({ item, from, attempts });
75
+ const limit = this.options.retryLimit ?? 200;
76
+ if (this.retries.length > limit) {
77
+ const gone = this.retries.splice(0, this.retries.length - limit);
78
+ activelogger_1.ActiveLogger.warn(`Retry queue full: giving up on ${gone.length} dropped item(s)`);
79
+ }
80
+ return true;
81
+ }
82
+ async retryLoop(signal) {
83
+ const sleep = this.options.sleep ?? defaultSleep;
84
+ while (!signal.aborted) {
85
+ await sleep(this.options.retryMs ?? 30000, signal);
86
+ if (signal.aborted)
87
+ return;
88
+ if (!this.retries.length || this.options.state.reason === "outdated")
89
+ continue;
90
+ const batch = this.retries;
91
+ this.retries = [];
92
+ for (const entry of batch) {
93
+ if (signal.aborted)
94
+ return;
95
+ let outcome;
96
+ try {
97
+ outcome = await this.options.apply(entry.item, entry.from);
98
+ }
99
+ catch (e) {
100
+ activelogger_1.ActiveLogger.warn(`Retry of ${entry.item?.umid} failed: ${e?.message || e}`);
101
+ continue;
102
+ }
103
+ if (this.hold(entry.item, entry.from, outcome, entry.attempts + 1))
104
+ continue;
105
+ this.options.onOutcome?.(outcome, this.cursor);
106
+ }
107
+ }
108
+ }
109
+ async loop(signal) {
110
+ const sleep = this.options.sleep ?? defaultSleep;
111
+ const { min, max } = this.options.backoffMs ?? { min: 1000, max: 30000 };
112
+ let wait = min;
113
+ while (!signal.aborted) {
114
+ if (this.options.state.reason === "outdated") {
115
+ for (const upstream of this.options.upstreams) {
116
+ const head = await (async () => upstream.head())().catch(() => undefined);
117
+ if (head?.minVersion) {
118
+ this.options.onMinVersion?.(head.minVersion);
119
+ break;
120
+ }
121
+ }
122
+ await sleep(this.options.outdatedPauseMs ?? 5000, signal);
123
+ continue;
124
+ }
125
+ let progressed = false;
126
+ for (const upstream of this.options.upstreams) {
127
+ if (signal.aborted)
128
+ return;
129
+ try {
130
+ await upstream.feed(this.cursor, async (cursor, item) => {
131
+ if (this.options.state.reason === "outdated")
132
+ throw new FeedPaused();
133
+ const outcome = await this.options.apply(item, upstream);
134
+ this.cursor = cursor;
135
+ this.options.state.cursor = cursor;
136
+ progressed = true;
137
+ await this.options.saveCursor(cursor);
138
+ this.options.onOutcome?.(outcome, cursor);
139
+ this.hold(item, upstream, outcome, 1);
140
+ if ((0, exports.atOrPast)(cursor, this.headSeen))
141
+ this.options.state.headConfirmedAt = this.now();
142
+ }, signal, (head, _time, minVersion) => {
143
+ this.mark(upstream, true);
144
+ this.confirm(head);
145
+ if (minVersion)
146
+ this.options.onMinVersion?.(minVersion);
147
+ });
148
+ if (signal.aborted)
149
+ return;
150
+ this.mark(upstream, true);
151
+ break;
152
+ }
153
+ catch (e) {
154
+ if (e instanceof FeedPaused)
155
+ break;
156
+ this.mark(upstream, false);
157
+ const refused = e instanceof http_upstream_1.UpstreamError && e.refused;
158
+ activelogger_1.ActiveLogger.warn(refused
159
+ ? `${upstream.id} refused the feed (not registered, retired or banned)`
160
+ : `${upstream.id} feed ended: ${e.message}`);
161
+ }
162
+ }
163
+ if (signal.aborted)
164
+ return;
165
+ wait = progressed ? min : Math.min(max, wait * 2);
166
+ await sleep(wait, signal);
167
+ }
168
+ }
169
+ }
170
+ exports.FeedManager = FeedManager;
171
+ class FeedPaused extends Error {
172
+ }
173
+ //# sourceMappingURL=feed.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"feed.js","sourceRoot":"","sources":["../src/feed.ts"],"names":[],"mappings":";;;AAuBA,6DAA0D;AAE1D,mDAAgD;AAKzC,MAAM,SAAS,GAAG,CAAC,MAAc,EAAU,EAAE,CAAC,MAAM,CAAC,OAAO,CAAC,eAAe,EAAE,EAAE,CAAC,CAAC;AAA5E,QAAA,SAAS,GAAT,SAAS,CAAmE;AAGlF,MAAM,QAAQ,GAAG,CAAC,MAAc,EAAE,IAAY,EAAW,EAAE,CAChE,CAAC,IAAI,IAAI,IAAA,QAAA,SAAS,EAAC,MAAM,CAAC,IAAI,IAAA,QAAA,SAAS,EAAC,IAAI,CAAC,CAAC;AADnC,QAAA,QAAQ,GAAR,QAAQ,CAC2B;AA4BhD,MAAM,YAAY,GAAG,CAAC,EAAU,EAAE,MAAmB,EAAE,EAAE,CACvD,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,EAAE;IAC5B,IAAI,MAAM,CAAC,OAAO;QAAE,OAAO,OAAO,EAAE,CAAC;IACrC,MAAM,KAAK,GAAG,UAAU,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;IACnC,SAAS,IAAI;QACX,YAAY,CAAC,KAAK,CAAC,CAAC;QACpB,MAAM,CAAC,mBAAmB,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;QAC1C,OAAO,EAAE,CAAC;IACZ,CAAC;IACD,MAAM,CAAC,gBAAgB,CAAC,OAAO,EAAE,IAAI,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC;AACzD,CAAC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EAAE,CAAC;oBACX,IAAI,CAAC,YAAY,UAAU;wBAAE,MAAM;oBACnC,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC;oBAC3B,MAAM,OAAO,GAAG,CAAC,YAAY,6BAAa,IAAI,CAAC,CAAC,OAAO,CAAC;oBACxD,2BAAY,CAAC,IAAI,CACf,OAAO;wBACL,CAAC,CAAC,GAAG,QAAQ,CAAC,EAAE,uDAAuD;wBACvE,CAAC,CAAC,GAAG,QAAQ,CAAC,EAAE,gBAAiB,CAAW,CAAC,OAAO,EAAE,CACzD,CAAC;gBAEJ,CAAC;YACH,CAAC;YAED,IAAI,MAAM,CAAC,OAAO;gBAAE,OAAO;YAC3B,IAAI,GAAG,UAAU,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,IAAI,GAAG,CAAC,CAAC,CAAC;YAClD,MAAM,KAAK,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;QAC5B,CAAC;IACH,CAAC;CACF;;AAED,MAAM,UAAW,SAAQ,KAAK;CAAG"}
@@ -0,0 +1,83 @@
1
+ import { ActiveCrypto } from "@activeledger/activecrypto";
2
+ import { ActiveDefinitions } from "@activeledger/activedefinitions";
3
+ import { IBootstrap, Upstream } from "./upstream";
4
+ export type UpstreamErrorKind = "refused" | "http" | "network" | "timeout" | "protocol";
5
+ export declare class UpstreamError extends Error {
6
+ readonly kind: UpstreamErrorKind;
7
+ readonly status?: number | undefined;
8
+ readonly body?: any;
9
+ constructor(message: string, kind: UpstreamErrorKind, status?: number | undefined, body?: any);
10
+ get refused(): boolean;
11
+ }
12
+ export interface HybridIdentity {
13
+ pair: ActiveCrypto.KeyPair;
14
+ type: string;
15
+ publicPem: string;
16
+ keyHash: string;
17
+ }
18
+ export declare function identityFrom(json: any): HybridIdentity;
19
+ export declare const loadIdentity: (path: string) => HybridIdentity;
20
+ export interface SseEvent {
21
+ event: string;
22
+ data: string;
23
+ id?: string;
24
+ }
25
+ export declare class SseParser {
26
+ private maxBuffer;
27
+ private buf;
28
+ private started;
29
+ private event;
30
+ private data;
31
+ private dataBytes;
32
+ private id?;
33
+ constructor(maxBuffer?: number);
34
+ push(text: string): SseEvent[];
35
+ end(): SseEvent[];
36
+ private line;
37
+ }
38
+ export interface HttpUpstreamOptions {
39
+ host: string;
40
+ port: number;
41
+ identity: HybridIdentity;
42
+ timeoutMs?: number;
43
+ idleTimeoutMs?: number;
44
+ tls?: boolean;
45
+ maxResponseBytes?: number;
46
+ }
47
+ export declare class HttpUpstream implements Upstream {
48
+ private opts;
49
+ readonly id: string;
50
+ private readonly base;
51
+ private readonly timeoutMs;
52
+ private readonly idleTimeoutMs;
53
+ constructor(opts: HttpUpstreamOptions);
54
+ private readonly maxResponseBytes;
55
+ private lastStamp;
56
+ private readBody;
57
+ private auth;
58
+ private failure;
59
+ private statusError;
60
+ private request;
61
+ bootstrap(): Promise<IBootstrap>;
62
+ head(): Promise<{
63
+ cursor: string;
64
+ time: number;
65
+ minVersion?: string;
66
+ }>;
67
+ streams(reqs: {
68
+ id: string;
69
+ rev?: string;
70
+ }[]): Promise<{
71
+ docs: any[];
72
+ missing: {
73
+ id: string;
74
+ rev?: string;
75
+ }[];
76
+ }>;
77
+ umid(umid: string): Promise<any | undefined>;
78
+ attest(umid: string): Promise<ActiveDefinitions.ICommitAttestation | undefined>;
79
+ peers(): Promise<any[]>;
80
+ submit(tx: any): Promise<any>;
81
+ feed(cursor: string, onItem: (cursor: string, item: ActiveDefinitions.IFeedItem) => Promise<void>, signal: AbortSignal, onHeartbeat?: (head: string, time: number, minVersion?: string) => void): Promise<void>;
82
+ }
83
+ //# sourceMappingURL=http-upstream.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"http-upstream.d.ts","sourceRoot":"","sources":["../src/http-upstream.ts"],"names":[],"mappings":"AAwBA,OAAO,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAC;AAC1D,OAAO,EA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AAC,CAMnE;IAEM,IAAI,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,GAAG,GAAG,SAAS,CAAC,CAElD;IAEM,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,iBAAiB,CAAC,kBAAkB,GAAG,SAAS,CAAC,CAErF;IAEY,KAAK,IAAI,OAAO,CAAC,GAAG,EAAE,CAAC,CAMnC;IAEM,MAAM,CAAC,EAAE,EAAE,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC,CAEnC;IAEY,IAAI,CACf,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,iBAAiB,CAAC,SAAS,KAAK,OAAO,CAAC,IAAI,CAAC,EAC5E,MAAM,EAAE,WAAW,EACnB,WAAW,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,MAAM,KAAK,IAAI,GACtE,OAAO,CAAC,IAAI,CAAC,CA6Ff;CACF"}
@@ -0,0 +1,401 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.HttpUpstream = exports.SseParser = exports.loadIdentity = exports.UpstreamError = void 0;
37
+ exports.identityFrom = identityFrom;
38
+ const fs = __importStar(require("fs"));
39
+ const activecrypto_1 = require("@activeledger/activecrypto");
40
+ class UpstreamError extends Error {
41
+ kind;
42
+ status;
43
+ body;
44
+ constructor(message, kind, status, body) {
45
+ super(message);
46
+ this.kind = kind;
47
+ this.status = status;
48
+ this.body = body;
49
+ this.name = "UpstreamError";
50
+ }
51
+ get refused() {
52
+ return this.kind === "refused";
53
+ }
54
+ }
55
+ exports.UpstreamError = UpstreamError;
56
+ function identityFrom(json) {
57
+ const prv = json?.prv?.pkcs8pem;
58
+ const pub = json?.pub?.pkcs8pem;
59
+ if (typeof prv !== "string" || typeof pub !== "string") {
60
+ throw new Error("identity: expected {prv:{pkcs8pem}, pub:{pkcs8pem}}");
61
+ }
62
+ const type = typeof json.type === "string" && json.type ? json.type : "rsa";
63
+ return {
64
+ pair: new activecrypto_1.ActiveCrypto.KeyPair(type, prv),
65
+ type,
66
+ publicPem: pub,
67
+ keyHash: activecrypto_1.ActiveCrypto.keyHash(pub),
68
+ };
69
+ }
70
+ const loadIdentity = (path) => identityFrom(JSON.parse(fs.readFileSync(path, "utf8")));
71
+ exports.loadIdentity = loadIdentity;
72
+ class SseParser {
73
+ maxBuffer;
74
+ buf = "";
75
+ started = false;
76
+ event = "";
77
+ data = [];
78
+ dataBytes = 0;
79
+ id;
80
+ constructor(maxBuffer = 64 * 1024 * 1024) {
81
+ this.maxBuffer = maxBuffer;
82
+ }
83
+ push(text) {
84
+ if (!this.started && text.length) {
85
+ this.started = true;
86
+ if (text.charCodeAt(0) === 0xfeff)
87
+ text = text.slice(1);
88
+ }
89
+ this.buf += text;
90
+ const events = [];
91
+ let start = 0;
92
+ for (let i = 0; i < this.buf.length; i++) {
93
+ const c = this.buf[i];
94
+ if (c !== "\n" && c !== "\r")
95
+ continue;
96
+ if (c === "\r" && i + 1 === this.buf.length)
97
+ break;
98
+ const line = this.buf.slice(start, i);
99
+ if (c === "\r" && this.buf[i + 1] === "\n")
100
+ i++;
101
+ start = i + 1;
102
+ this.line(line, events);
103
+ }
104
+ this.buf = this.buf.slice(start);
105
+ if (this.buf.length + this.dataBytes > this.maxBuffer) {
106
+ throw new UpstreamError("feed line too long", "protocol");
107
+ }
108
+ return events;
109
+ }
110
+ end() {
111
+ const events = [];
112
+ if (this.buf.endsWith("\r"))
113
+ this.line(this.buf.slice(0, -1), events);
114
+ this.buf = "";
115
+ return events;
116
+ }
117
+ line(line, events) {
118
+ if (line === "") {
119
+ if (this.data.length) {
120
+ events.push({
121
+ event: this.event || "message",
122
+ data: this.data.join("\n"),
123
+ ...(this.id !== undefined ? { id: this.id } : {}),
124
+ });
125
+ }
126
+ this.event = "";
127
+ this.data = [];
128
+ this.dataBytes = 0;
129
+ this.id = undefined;
130
+ return;
131
+ }
132
+ if (line.startsWith(":"))
133
+ return;
134
+ const colon = line.indexOf(":");
135
+ const field = colon < 0 ? line : line.slice(0, colon);
136
+ let value = colon < 0 ? "" : line.slice(colon + 1);
137
+ if (value.startsWith(" "))
138
+ value = value.slice(1);
139
+ switch (field) {
140
+ case "event":
141
+ this.event = value;
142
+ break;
143
+ case "data":
144
+ this.data.push(value);
145
+ this.dataBytes += value.length;
146
+ break;
147
+ case "id":
148
+ if (!value.includes("\0"))
149
+ this.id = value;
150
+ break;
151
+ }
152
+ }
153
+ }
154
+ exports.SseParser = SseParser;
155
+ const isObject = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
156
+ class HttpUpstream {
157
+ opts;
158
+ id;
159
+ base;
160
+ timeoutMs;
161
+ idleTimeoutMs;
162
+ constructor(opts) {
163
+ this.opts = opts;
164
+ const host = opts.host.includes(":") ? `[${opts.host}]` : opts.host;
165
+ this.base = `${opts.tls ? "https" : "http"}://${host}:${opts.port}`;
166
+ this.id = `${host}:${opts.port}:http`;
167
+ this.timeoutMs = opts.timeoutMs ?? 15000;
168
+ this.idleTimeoutMs = opts.idleTimeoutMs ?? 35000;
169
+ this.maxResponseBytes = opts.maxResponseBytes ?? 64 * 1024 * 1024;
170
+ }
171
+ maxResponseBytes;
172
+ lastStamp = 0;
173
+ async readBody(res) {
174
+ const declared = Number(res.headers.get("content-length"));
175
+ if (declared > this.maxResponseBytes) {
176
+ await res.body?.cancel().catch(() => undefined);
177
+ throw new UpstreamError(`${this.id}: response too large`, "protocol", res.status);
178
+ }
179
+ if (!res.body)
180
+ return "";
181
+ const reader = res.body.getReader();
182
+ const chunks = [];
183
+ let size = 0;
184
+ for (;;) {
185
+ const { done, value } = await reader.read();
186
+ if (done)
187
+ break;
188
+ size += value.byteLength;
189
+ if (size > this.maxResponseBytes) {
190
+ await reader.cancel().catch(() => undefined);
191
+ throw new UpstreamError(`${this.id}: response too large`, "protocol", res.status);
192
+ }
193
+ chunks.push(value);
194
+ }
195
+ return Buffer.concat(chunks).toString("utf8");
196
+ }
197
+ auth(extra = {}) {
198
+ const { keyHash, pair } = this.opts.identity;
199
+ this.lastStamp = Math.max(Date.now(), this.lastStamp + 1);
200
+ const r = activecrypto_1.ActiveCrypto.signRequest(pair, keyHash, this.lastStamp);
201
+ return {
202
+ "x-hybrid-key": r.key,
203
+ "x-hybrid-timestamp": String(r.timestamp),
204
+ "x-hybrid-signature": r.signature,
205
+ ...extra,
206
+ };
207
+ }
208
+ failure(e, signal, what) {
209
+ if (e instanceof UpstreamError)
210
+ return e;
211
+ if (signal.aborted && signal.reason instanceof UpstreamError)
212
+ return signal.reason;
213
+ if (e?.name === "TimeoutError" || signal.reason?.name === "TimeoutError") {
214
+ return new UpstreamError(`${this.id} ${what}: timed out`, "timeout");
215
+ }
216
+ const cause = e?.cause?.code ?? e?.cause?.message ?? e?.message ?? String(e);
217
+ return new UpstreamError(`${this.id} ${what}: ${cause}`, "network");
218
+ }
219
+ async statusError(res, what) {
220
+ let body;
221
+ let text = "";
222
+ try {
223
+ text = await this.readBody(res);
224
+ body = JSON.parse(text);
225
+ }
226
+ catch {
227
+ body = text || undefined;
228
+ }
229
+ const reason = (isObject(body) && (body.reason ?? body.error ?? body.message)) ||
230
+ (typeof body === "string" ? body : "") ||
231
+ res.statusText;
232
+ const kind = res.status === 401 || res.status === 403 ? "refused" : "http";
233
+ return new UpstreamError(`${this.id} ${what}: ${res.status}${reason ? ` ${reason}` : ""}`, kind, res.status, body);
234
+ }
235
+ async request(method, path, body, notFound = false) {
236
+ const signal = AbortSignal.timeout(this.timeoutMs);
237
+ const what = `${method} ${path}`;
238
+ try {
239
+ const headers = this.auth({ accept: "application/json" });
240
+ if (body !== undefined)
241
+ headers["content-type"] = "application/json";
242
+ const res = await fetch(this.base + path, {
243
+ method,
244
+ headers,
245
+ body: body === undefined ? undefined : JSON.stringify(body),
246
+ signal,
247
+ });
248
+ if (notFound && res.status === 404) {
249
+ await res.body?.cancel().catch(() => undefined);
250
+ return undefined;
251
+ }
252
+ if (!res.ok)
253
+ throw await this.statusError(res, what);
254
+ const text = await this.readBody(res);
255
+ try {
256
+ return JSON.parse(text);
257
+ }
258
+ catch {
259
+ throw new UpstreamError(`${this.id} ${what}: response is not JSON`, "protocol", res.status);
260
+ }
261
+ }
262
+ catch (e) {
263
+ throw this.failure(e, signal, what);
264
+ }
265
+ }
266
+ async bootstrap() {
267
+ const b = await this.request("GET", "/h/bootstrap");
268
+ if (!isObject(b) || !Array.isArray(b.contracts) || typeof b.cursor !== "string") {
269
+ throw new UpstreamError(`${this.id} GET /h/bootstrap: malformed response`, "protocol");
270
+ }
271
+ return { ...b, required: Array.isArray(b.required) ? b.required : [] };
272
+ }
273
+ async head() {
274
+ const h = await this.request("GET", "/h/head");
275
+ if (!isObject(h) || typeof h.cursor !== "string" || typeof h.time !== "number") {
276
+ throw new UpstreamError(`${this.id} GET /h/head: malformed response`, "protocol");
277
+ }
278
+ return { cursor: h.cursor, time: h.time, minVersion: typeof h.minVersion === "string" ? h.minVersion : undefined };
279
+ }
280
+ async streams(reqs) {
281
+ const r = await this.request("POST", "/h/streams", reqs);
282
+ if (!isObject(r) || !Array.isArray(r.docs) || !Array.isArray(r.missing)) {
283
+ throw new UpstreamError(`${this.id} POST /h/streams: malformed response`, "protocol");
284
+ }
285
+ return { docs: r.docs, missing: r.missing };
286
+ }
287
+ umid(umid) {
288
+ return this.request("GET", `/h/umid/${encodeURIComponent(umid)}`, undefined, true);
289
+ }
290
+ attest(umid) {
291
+ return this.request("GET", `/h/attest/${encodeURIComponent(umid)}`, undefined, true);
292
+ }
293
+ async peers() {
294
+ const p = await this.request("GET", "/h/peers");
295
+ if (!Array.isArray(p)) {
296
+ throw new UpstreamError(`${this.id} GET /h/peers: malformed response`, "protocol");
297
+ }
298
+ return p;
299
+ }
300
+ submit(tx) {
301
+ return this.request("POST", "/", tx);
302
+ }
303
+ async feed(cursor, onItem, signal, onHeartbeat) {
304
+ if (signal.aborted)
305
+ return;
306
+ const what = "GET /h/feed";
307
+ const ctl = new AbortController();
308
+ const onAbort = () => ctl.abort(signal.reason);
309
+ signal.addEventListener("abort", onAbort, { once: true });
310
+ let idleTimer;
311
+ const idle = () => {
312
+ clearTimeout(idleTimer);
313
+ idleTimer = setTimeout(() => ctl.abort(new UpstreamError(`${this.id} ${what}: no traffic for ${this.idleTimeoutMs}ms`, "timeout")), this.idleTimeoutMs);
314
+ };
315
+ try {
316
+ idle();
317
+ const headers = this.auth({ accept: "text/event-stream" });
318
+ if (cursor)
319
+ headers["last-event-id"] = cursor;
320
+ let res;
321
+ try {
322
+ res = await fetch(`${this.base}/h/feed?from=${encodeURIComponent(cursor)}`, {
323
+ headers,
324
+ signal: ctl.signal,
325
+ });
326
+ }
327
+ catch (e) {
328
+ if (signal.aborted)
329
+ return;
330
+ throw this.failure(e, ctl.signal, what);
331
+ }
332
+ if (!res.ok)
333
+ throw await this.statusError(res, what);
334
+ const type = res.headers.get("content-type") ?? "";
335
+ if (!type.startsWith("text/event-stream") || !res.body) {
336
+ throw new UpstreamError(`${this.id} ${what}: not an event stream (${type})`, "protocol", res.status);
337
+ }
338
+ const reader = res.body.getReader();
339
+ const decoder = new TextDecoder();
340
+ const parser = new SseParser();
341
+ for (;;) {
342
+ let chunk;
343
+ try {
344
+ chunk = await reader.read();
345
+ }
346
+ catch (e) {
347
+ if (signal.aborted)
348
+ return;
349
+ throw this.failure(e, ctl.signal, what);
350
+ }
351
+ if (chunk.done) {
352
+ parser.end();
353
+ return;
354
+ }
355
+ idle();
356
+ for (const ev of parser.push(decoder.decode(chunk.value, { stream: true }))) {
357
+ if (signal.aborted)
358
+ return;
359
+ if (ev.event === "heartbeat") {
360
+ let hb;
361
+ try {
362
+ hb = JSON.parse(ev.data);
363
+ }
364
+ catch {
365
+ throw new UpstreamError(`${this.id} ${what}: heartbeat is not JSON`, "protocol");
366
+ }
367
+ if (!isObject(hb) || typeof hb.head !== "string" || typeof hb.time !== "number") {
368
+ throw new UpstreamError(`${this.id} ${what}: malformed heartbeat`, "protocol");
369
+ }
370
+ onHeartbeat?.(hb.head, hb.time, typeof hb.minVersion === "string" ? hb.minVersion : undefined);
371
+ }
372
+ else if (ev.event === "message") {
373
+ if (!ev.id) {
374
+ throw new UpstreamError(`${this.id} ${what}: item without a cursor id`, "protocol");
375
+ }
376
+ let item;
377
+ try {
378
+ item = JSON.parse(ev.data);
379
+ }
380
+ catch {
381
+ throw new UpstreamError(`${this.id} ${what}: item ${ev.id} is not JSON`, "protocol");
382
+ }
383
+ clearTimeout(idleTimer);
384
+ await onItem(ev.id, item);
385
+ if (signal.aborted)
386
+ return;
387
+ idle();
388
+ }
389
+ }
390
+ }
391
+ }
392
+ finally {
393
+ clearTimeout(idleTimer);
394
+ signal.removeEventListener("abort", onAbort);
395
+ if (!ctl.signal.aborted)
396
+ ctl.abort();
397
+ }
398
+ }
399
+ }
400
+ exports.HttpUpstream = HttpUpstream;
401
+ //# sourceMappingURL=http-upstream.js.map