@celljs/rpc 4.2.0 → 4.4.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 (545) hide show
  1. package/README.md +117 -1
  2. package/cell.schema.json +100 -0
  3. package/cell.yml +12 -8
  4. package/esm/common/annotation/index.d.ts +2 -0
  5. package/esm/common/annotation/index.js +2 -0
  6. package/esm/common/annotation/index.js.map +1 -1
  7. package/esm/common/annotation/rpc-client.d.ts +7 -0
  8. package/esm/common/annotation/rpc-client.js +18 -0
  9. package/esm/common/annotation/rpc-client.js.map +1 -0
  10. package/esm/common/annotation/rpc-protocol.d.ts +7 -0
  11. package/esm/common/annotation/rpc-protocol.js +2 -0
  12. package/esm/common/annotation/rpc-protocol.js.map +1 -0
  13. package/esm/common/annotation/rpc.d.ts +4 -0
  14. package/esm/common/annotation/rpc.js +14 -18
  15. package/esm/common/annotation/rpc.js.map +1 -1
  16. package/esm/common/client/client-protocol.d.ts +6 -0
  17. package/esm/common/client/client-protocol.js +2 -0
  18. package/esm/common/client/client-protocol.js.map +1 -0
  19. package/esm/common/client/index.d.ts +2 -0
  20. package/esm/common/client/index.js +3 -0
  21. package/esm/common/client/index.js.map +1 -0
  22. package/esm/common/client/rpc-client-registry.d.ts +14 -0
  23. package/esm/common/client/rpc-client-registry.js +51 -0
  24. package/esm/common/client/rpc-client-registry.js.map +1 -0
  25. package/esm/common/constants.d.ts +3 -1
  26. package/esm/common/constants.js +3 -1
  27. package/esm/common/constants.js.map +1 -1
  28. package/esm/common/converter/converter-protocol.d.ts +6 -2
  29. package/esm/common/converter/converter-protocol.js +1 -0
  30. package/esm/common/converter/converter-protocol.js.map +1 -1
  31. package/esm/common/endpoint/endpoint-protocol.d.ts +3 -0
  32. package/esm/common/endpoint/endpoint-resolver.d.ts +2 -0
  33. package/esm/common/endpoint/endpoint-resolver.js +17 -3
  34. package/esm/common/endpoint/endpoint-resolver.js.map +1 -1
  35. package/esm/common/error/error.d.ts +23 -0
  36. package/esm/common/error/error.js +34 -2
  37. package/esm/common/error/error.js.map +1 -1
  38. package/esm/common/index.d.ts +6 -6
  39. package/esm/common/index.js +6 -6
  40. package/esm/common/index.js.map +1 -1
  41. package/esm/common/module.js +5 -4
  42. package/esm/common/module.js.map +1 -1
  43. package/esm/common/processor/client-config-processor.d.ts +2 -2
  44. package/esm/common/processor/client-config-processor.js.map +1 -1
  45. package/esm/common/processor/processor-protocol.d.ts +2 -2
  46. package/esm/common/protocol/index.d.ts +1 -0
  47. package/esm/common/protocol/index.js +2 -0
  48. package/esm/common/protocol/index.js.map +1 -0
  49. package/esm/common/protocol/json-rpc.d.ts +42 -0
  50. package/esm/common/protocol/json-rpc.js +48 -0
  51. package/esm/common/protocol/json-rpc.js.map +1 -0
  52. package/esm/common/proxy/index.d.ts +1 -4
  53. package/esm/common/proxy/index.js +1 -4
  54. package/esm/common/proxy/index.js.map +1 -1
  55. package/esm/common/proxy/rpc-proxy.d.ts +6 -0
  56. package/esm/common/proxy/rpc-proxy.js +40 -0
  57. package/esm/common/proxy/rpc-proxy.js.map +1 -0
  58. package/esm/common/transport/http-transport.d.ts +50 -0
  59. package/esm/common/transport/http-transport.js +98 -0
  60. package/esm/common/transport/http-transport.js.map +1 -0
  61. package/esm/common/transport/index.d.ts +4 -0
  62. package/esm/common/transport/index.js +5 -0
  63. package/esm/common/transport/index.js.map +1 -0
  64. package/esm/common/transport/transport-protocol.d.ts +12 -0
  65. package/esm/common/transport/transport-protocol.js +2 -0
  66. package/esm/common/transport/transport-protocol.js.map +1 -0
  67. package/esm/common/transport/transport-provider.d.ts +34 -0
  68. package/esm/common/transport/transport-provider.js +103 -0
  69. package/esm/common/transport/transport-provider.js.map +1 -0
  70. package/esm/common/transport/websocket-transport.d.ts +54 -0
  71. package/esm/common/transport/websocket-transport.js +144 -0
  72. package/esm/common/transport/websocket-transport.js.map +1 -0
  73. package/esm/common/utils/index.d.ts +1 -1
  74. package/esm/common/utils/index.js +1 -1
  75. package/esm/common/utils/index.js.map +1 -1
  76. package/esm/common/utils/rpc-method-util.d.ts +10 -0
  77. package/esm/common/utils/rpc-method-util.js +28 -0
  78. package/esm/common/utils/rpc-method-util.js.map +1 -0
  79. package/esm/common/utils/rpc-util.d.ts +9 -9
  80. package/esm/common/utils/rpc-util.js +24 -30
  81. package/esm/common/utils/rpc-util.js.map +1 -1
  82. package/esm/node/connection/connection-protocol.d.ts +26 -0
  83. package/esm/node/connection/connection-protocol.js +3 -0
  84. package/esm/node/connection/connection-protocol.js.map +1 -0
  85. package/esm/node/connection/index.d.ts +4 -0
  86. package/esm/node/connection/index.js +5 -0
  87. package/esm/node/connection/index.js.map +1 -0
  88. package/esm/node/connection/rpc-connection-manager.d.ts +10 -0
  89. package/esm/node/connection/rpc-connection-manager.js +35 -0
  90. package/esm/node/connection/rpc-connection-manager.js.map +1 -0
  91. package/esm/node/connection/rpc-connection.d.ts +33 -0
  92. package/esm/node/connection/rpc-connection.js +106 -0
  93. package/esm/node/connection/rpc-connection.js.map +1 -0
  94. package/esm/node/connection/rpc-context.d.ts +14 -0
  95. package/esm/node/connection/rpc-context.js +29 -0
  96. package/esm/node/connection/rpc-context.js.map +1 -0
  97. package/esm/node/index.d.ts +3 -2
  98. package/esm/node/index.js +3 -2
  99. package/esm/node/index.js.map +1 -1
  100. package/esm/node/server/index.d.ts +2 -0
  101. package/esm/node/server/index.js +3 -0
  102. package/esm/node/server/index.js.map +1 -0
  103. package/esm/node/server/rpc-handler-adapter.d.ts +15 -0
  104. package/esm/node/server/rpc-handler-adapter.js +74 -0
  105. package/esm/node/server/rpc-handler-adapter.js.map +1 -0
  106. package/esm/node/server/rpc-invoker.d.ts +28 -0
  107. package/esm/node/server/rpc-invoker.js +113 -0
  108. package/esm/node/server/rpc-invoker.js.map +1 -0
  109. package/esm/node/test/websocket-fixture-protocol.d.ts +36 -0
  110. package/esm/node/test/websocket-fixture-protocol.js +7 -0
  111. package/esm/node/test/websocket-fixture-protocol.js.map +1 -0
  112. package/esm/node/test/websocket-fixture.d.ts +40 -0
  113. package/esm/node/test/websocket-fixture.js +128 -0
  114. package/esm/node/test/websocket-fixture.js.map +1 -0
  115. package/esm/node/websocket/index.d.ts +4 -0
  116. package/esm/node/websocket/index.js +5 -0
  117. package/esm/node/websocket/index.js.map +1 -0
  118. package/esm/node/websocket/rpc-websocket-server.d.ts +45 -0
  119. package/esm/node/websocket/rpc-websocket-server.js +167 -0
  120. package/esm/node/websocket/rpc-websocket-server.js.map +1 -0
  121. package/esm/node/websocket/websocket-connection.d.ts +55 -0
  122. package/esm/node/websocket/websocket-connection.js +181 -0
  123. package/esm/node/websocket/websocket-connection.js.map +1 -0
  124. package/esm/node/websocket/websocket-exchange.d.ts +38 -0
  125. package/esm/node/websocket/websocket-exchange.js +124 -0
  126. package/esm/node/websocket/websocket-exchange.js.map +1 -0
  127. package/esm/node/websocket/websocket-frames.d.ts +31 -0
  128. package/esm/node/websocket/websocket-frames.js +119 -0
  129. package/esm/node/websocket/websocket-frames.js.map +1 -0
  130. package/lib/common/annotation/index.d.ts +2 -0
  131. package/lib/common/annotation/index.d.ts.map +1 -1
  132. package/lib/common/annotation/index.js +2 -0
  133. package/lib/common/annotation/index.js.map +1 -1
  134. package/lib/common/annotation/rpc-client.d.ts +8 -0
  135. package/lib/common/annotation/rpc-client.d.ts.map +1 -0
  136. package/lib/common/annotation/rpc-client.js +21 -0
  137. package/lib/common/annotation/rpc-client.js.map +1 -0
  138. package/lib/common/annotation/rpc-protocol.d.ts +8 -0
  139. package/lib/common/annotation/rpc-protocol.d.ts.map +1 -0
  140. package/lib/common/annotation/rpc-protocol.js +5 -0
  141. package/lib/common/annotation/rpc-protocol.js.map +1 -0
  142. package/lib/common/annotation/rpc.d.ts +4 -0
  143. package/lib/common/annotation/rpc.d.ts.map +1 -1
  144. package/lib/common/annotation/rpc.js +12 -16
  145. package/lib/common/annotation/rpc.js.map +1 -1
  146. package/lib/common/client/client-protocol.d.ts +7 -0
  147. package/lib/common/client/client-protocol.d.ts.map +1 -0
  148. package/lib/common/client/client-protocol.js +5 -0
  149. package/lib/common/client/client-protocol.js.map +1 -0
  150. package/lib/common/client/index.d.ts +3 -0
  151. package/lib/common/client/index.d.ts.map +1 -0
  152. package/lib/common/{handler → client}/index.js +2 -2
  153. package/lib/common/client/index.js.map +1 -0
  154. package/lib/common/client/rpc-client-registry.d.ts +15 -0
  155. package/lib/common/client/rpc-client-registry.d.ts.map +1 -0
  156. package/lib/common/client/rpc-client-registry.js +54 -0
  157. package/lib/common/client/rpc-client-registry.js.map +1 -0
  158. package/lib/common/constants.d.ts +3 -1
  159. package/lib/common/constants.d.ts.map +1 -1
  160. package/lib/common/constants.js +4 -2
  161. package/lib/common/constants.js.map +1 -1
  162. package/lib/common/converter/converter-protocol.d.ts +6 -2
  163. package/lib/common/converter/converter-protocol.d.ts.map +1 -1
  164. package/lib/common/converter/converter-protocol.js +1 -0
  165. package/lib/common/converter/converter-protocol.js.map +1 -1
  166. package/lib/common/endpoint/endpoint-protocol.d.ts +3 -0
  167. package/lib/common/endpoint/endpoint-protocol.d.ts.map +1 -1
  168. package/lib/common/endpoint/endpoint-resolver.d.ts +2 -0
  169. package/lib/common/endpoint/endpoint-resolver.d.ts.map +1 -1
  170. package/lib/common/endpoint/endpoint-resolver.js +16 -2
  171. package/lib/common/endpoint/endpoint-resolver.js.map +1 -1
  172. package/lib/common/error/error.d.ts +23 -0
  173. package/lib/common/error/error.d.ts.map +1 -1
  174. package/lib/common/error/error.js +35 -2
  175. package/lib/common/error/error.js.map +1 -1
  176. package/lib/common/index.d.ts +6 -6
  177. package/lib/common/index.d.ts.map +1 -1
  178. package/lib/common/index.js +6 -6
  179. package/lib/common/index.js.map +1 -1
  180. package/lib/common/module.d.ts.map +1 -1
  181. package/lib/common/module.js +4 -3
  182. package/lib/common/module.js.map +1 -1
  183. package/lib/common/processor/client-config-processor.d.ts +2 -2
  184. package/lib/common/processor/client-config-processor.d.ts.map +1 -1
  185. package/lib/common/processor/client-config-processor.js.map +1 -1
  186. package/lib/common/processor/processor-protocol.d.ts +2 -2
  187. package/lib/common/processor/processor-protocol.d.ts.map +1 -1
  188. package/lib/common/protocol/index.d.ts +2 -0
  189. package/lib/common/protocol/index.d.ts.map +1 -0
  190. package/lib/common/{channal → protocol}/index.js +1 -2
  191. package/lib/common/protocol/index.js.map +1 -0
  192. package/lib/common/protocol/json-rpc.d.ts +43 -0
  193. package/lib/common/protocol/json-rpc.d.ts.map +1 -0
  194. package/lib/common/protocol/json-rpc.js +51 -0
  195. package/lib/common/protocol/json-rpc.js.map +1 -0
  196. package/lib/common/proxy/index.d.ts +1 -4
  197. package/lib/common/proxy/index.d.ts.map +1 -1
  198. package/lib/common/proxy/index.js +1 -4
  199. package/lib/common/proxy/index.js.map +1 -1
  200. package/lib/common/proxy/rpc-proxy.d.ts +7 -0
  201. package/lib/common/proxy/rpc-proxy.d.ts.map +1 -0
  202. package/lib/common/proxy/rpc-proxy.js +43 -0
  203. package/lib/common/proxy/rpc-proxy.js.map +1 -0
  204. package/lib/common/transport/http-transport.d.ts +51 -0
  205. package/lib/common/transport/http-transport.d.ts.map +1 -0
  206. package/lib/common/transport/http-transport.js +102 -0
  207. package/lib/common/transport/http-transport.js.map +1 -0
  208. package/lib/common/transport/index.d.ts +5 -0
  209. package/lib/common/transport/index.d.ts.map +1 -0
  210. package/lib/common/{factory → transport}/index.js +4 -4
  211. package/lib/common/transport/index.js.map +1 -0
  212. package/lib/common/transport/transport-protocol.d.ts +13 -0
  213. package/lib/common/transport/transport-protocol.d.ts.map +1 -0
  214. package/lib/common/transport/transport-protocol.js +5 -0
  215. package/lib/common/transport/transport-protocol.js.map +1 -0
  216. package/lib/common/transport/transport-provider.d.ts +35 -0
  217. package/lib/common/transport/transport-provider.d.ts.map +1 -0
  218. package/lib/common/transport/transport-provider.js +106 -0
  219. package/lib/common/transport/transport-provider.js.map +1 -0
  220. package/lib/common/transport/websocket-transport.d.ts +55 -0
  221. package/lib/common/transport/websocket-transport.d.ts.map +1 -0
  222. package/lib/common/transport/websocket-transport.js +148 -0
  223. package/lib/common/transport/websocket-transport.js.map +1 -0
  224. package/lib/common/utils/index.d.ts +1 -1
  225. package/lib/common/utils/index.d.ts.map +1 -1
  226. package/lib/common/utils/index.js +1 -1
  227. package/lib/common/utils/index.js.map +1 -1
  228. package/lib/common/utils/rpc-method-util.d.ts +11 -0
  229. package/lib/common/utils/rpc-method-util.d.ts.map +1 -0
  230. package/lib/common/utils/rpc-method-util.js +31 -0
  231. package/lib/common/utils/rpc-method-util.js.map +1 -0
  232. package/lib/common/utils/rpc-util.d.ts +9 -9
  233. package/lib/common/utils/rpc-util.d.ts.map +1 -1
  234. package/lib/common/utils/rpc-util.js +24 -30
  235. package/lib/common/utils/rpc-util.js.map +1 -1
  236. package/lib/node/connection/connection-protocol.d.ts +27 -0
  237. package/lib/node/connection/connection-protocol.d.ts.map +1 -0
  238. package/lib/node/connection/connection-protocol.js +6 -0
  239. package/lib/node/connection/connection-protocol.js.map +1 -0
  240. package/lib/node/connection/index.d.ts +5 -0
  241. package/lib/node/connection/index.d.ts.map +1 -0
  242. package/lib/node/{channel → connection}/index.js +4 -4
  243. package/lib/node/connection/index.js.map +1 -0
  244. package/lib/node/connection/rpc-connection-manager.d.ts +11 -0
  245. package/lib/node/connection/rpc-connection-manager.d.ts.map +1 -0
  246. package/lib/node/connection/rpc-connection-manager.js +38 -0
  247. package/lib/node/connection/rpc-connection-manager.js.map +1 -0
  248. package/lib/node/connection/rpc-connection.d.ts +34 -0
  249. package/lib/node/connection/rpc-connection.d.ts.map +1 -0
  250. package/lib/node/connection/rpc-connection.js +110 -0
  251. package/lib/node/connection/rpc-connection.js.map +1 -0
  252. package/lib/node/connection/rpc-context.d.ts +15 -0
  253. package/lib/node/connection/rpc-context.d.ts.map +1 -0
  254. package/lib/node/connection/rpc-context.js +33 -0
  255. package/lib/node/connection/rpc-context.js.map +1 -0
  256. package/lib/node/index.d.ts +3 -2
  257. package/lib/node/index.d.ts.map +1 -1
  258. package/lib/node/index.js +3 -2
  259. package/lib/node/index.js.map +1 -1
  260. package/lib/node/rpc-http.spec.d.ts +2 -0
  261. package/lib/node/rpc-http.spec.d.ts.map +1 -0
  262. package/lib/node/rpc-http.spec.js +121 -0
  263. package/lib/node/rpc-http.spec.js.map +1 -0
  264. package/lib/node/rpc-websocket.spec.d.ts +6 -0
  265. package/lib/node/rpc-websocket.spec.d.ts.map +1 -0
  266. package/lib/node/rpc-websocket.spec.js +196 -0
  267. package/lib/node/rpc-websocket.spec.js.map +1 -0
  268. package/lib/node/server/index.d.ts +3 -0
  269. package/lib/node/server/index.d.ts.map +1 -0
  270. package/lib/node/{handler → server}/index.js +2 -2
  271. package/lib/node/server/index.js.map +1 -0
  272. package/lib/node/server/rpc-handler-adapter.d.ts +16 -0
  273. package/lib/node/server/rpc-handler-adapter.d.ts.map +1 -0
  274. package/lib/node/{handler/handler-adapter.js → server/rpc-handler-adapter.js} +40 -18
  275. package/lib/node/server/rpc-handler-adapter.js.map +1 -0
  276. package/lib/node/server/rpc-invoker.d.ts +29 -0
  277. package/lib/node/server/rpc-invoker.d.ts.map +1 -0
  278. package/lib/node/server/rpc-invoker.js +116 -0
  279. package/lib/node/server/rpc-invoker.js.map +1 -0
  280. package/lib/node/test/websocket-fixture-protocol.d.ts +37 -0
  281. package/lib/node/test/websocket-fixture-protocol.d.ts.map +1 -0
  282. package/lib/node/test/websocket-fixture-protocol.js +10 -0
  283. package/lib/node/test/websocket-fixture-protocol.js.map +1 -0
  284. package/lib/node/test/websocket-fixture.d.ts +41 -0
  285. package/lib/node/test/websocket-fixture.d.ts.map +1 -0
  286. package/lib/node/test/websocket-fixture.js +135 -0
  287. package/lib/node/test/websocket-fixture.js.map +1 -0
  288. package/lib/node/websocket/index.d.ts +5 -0
  289. package/lib/node/websocket/index.d.ts.map +1 -0
  290. package/lib/node/websocket/index.js +21 -0
  291. package/lib/node/websocket/index.js.map +1 -0
  292. package/lib/node/websocket/rpc-websocket-server.d.ts +46 -0
  293. package/lib/node/websocket/rpc-websocket-server.d.ts.map +1 -0
  294. package/lib/node/websocket/rpc-websocket-server.js +170 -0
  295. package/lib/node/websocket/rpc-websocket-server.js.map +1 -0
  296. package/lib/node/websocket/websocket-connection.d.ts +56 -0
  297. package/lib/node/websocket/websocket-connection.d.ts.map +1 -0
  298. package/lib/node/websocket/websocket-connection.js +185 -0
  299. package/lib/node/websocket/websocket-connection.js.map +1 -0
  300. package/lib/node/websocket/websocket-exchange.d.ts +39 -0
  301. package/lib/node/websocket/websocket-exchange.d.ts.map +1 -0
  302. package/lib/node/websocket/websocket-exchange.js +128 -0
  303. package/lib/node/websocket/websocket-exchange.js.map +1 -0
  304. package/lib/node/websocket/websocket-frames.d.ts +32 -0
  305. package/lib/node/websocket/websocket-frames.d.ts.map +1 -0
  306. package/lib/node/websocket/websocket-frames.js +124 -0
  307. package/lib/node/websocket/websocket-frames.js.map +1 -0
  308. package/lib/node/websocket/websocket-frames.spec.d.ts +2 -0
  309. package/lib/node/websocket/websocket-frames.spec.d.ts.map +1 -0
  310. package/lib/node/websocket/websocket-frames.spec.js +59 -0
  311. package/lib/node/websocket/websocket-frames.spec.js.map +1 -0
  312. package/package.json +28 -25
  313. package/src/common/annotation/index.ts +2 -0
  314. package/src/common/annotation/rpc-client.ts +17 -0
  315. package/src/common/annotation/rpc-protocol.ts +8 -0
  316. package/src/common/annotation/rpc.ts +13 -21
  317. package/src/common/client/client-protocol.ts +7 -0
  318. package/src/common/client/index.ts +2 -0
  319. package/src/common/client/rpc-client-registry.ts +42 -0
  320. package/src/common/constants.ts +4 -1
  321. package/src/common/converter/converter-protocol.ts +7 -2
  322. package/src/common/endpoint/endpoint-protocol.ts +3 -0
  323. package/src/common/endpoint/endpoint-resolver.ts +18 -4
  324. package/src/common/error/error.ts +41 -2
  325. package/src/common/index.ts +6 -6
  326. package/src/common/module.ts +7 -8
  327. package/src/common/processor/client-config-processor.ts +2 -2
  328. package/src/common/processor/processor-protocol.ts +2 -2
  329. package/src/common/protocol/index.ts +1 -0
  330. package/src/common/protocol/json-rpc.ts +74 -0
  331. package/src/common/proxy/index.ts +1 -5
  332. package/src/common/proxy/rpc-proxy.ts +42 -0
  333. package/src/common/transport/http-transport.ts +134 -0
  334. package/src/common/transport/index.ts +4 -0
  335. package/src/common/transport/transport-protocol.ts +16 -0
  336. package/src/common/transport/transport-provider.ts +86 -0
  337. package/src/common/transport/websocket-transport.ts +171 -0
  338. package/src/common/utils/index.ts +1 -1
  339. package/src/common/utils/rpc-method-util.ts +26 -0
  340. package/src/common/utils/rpc-util.ts +24 -35
  341. package/src/node/connection/connection-protocol.ts +30 -0
  342. package/src/node/connection/index.ts +3 -0
  343. package/src/node/connection/rpc-connection-manager.ts +29 -0
  344. package/src/node/connection/rpc-connection.ts +122 -0
  345. package/src/node/connection/rpc-context.ts +29 -0
  346. package/src/node/index.ts +3 -2
  347. package/src/node/rpc-http.spec.ts +126 -0
  348. package/src/node/rpc-websocket.spec.ts +196 -0
  349. package/src/node/server/index.ts +2 -0
  350. package/src/node/server/rpc-handler-adapter.ts +60 -0
  351. package/src/node/server/rpc-invoker.ts +104 -0
  352. package/src/node/test/websocket-fixture-protocol.ts +32 -0
  353. package/src/node/test/websocket-fixture.ts +122 -0
  354. package/src/node/websocket/index.ts +4 -0
  355. package/src/node/websocket/rpc-websocket-server.ts +163 -0
  356. package/src/node/websocket/websocket-connection.ts +198 -0
  357. package/src/node/websocket/websocket-exchange.ts +138 -0
  358. package/src/node/websocket/websocket-frames.spec.ts +62 -0
  359. package/src/node/websocket/websocket-frames.ts +122 -0
  360. package/esm/common/channal/channel-protocol.d.ts +0 -51
  361. package/esm/common/channal/channel-protocol.js +0 -34
  362. package/esm/common/channal/channel-protocol.js.map +0 -1
  363. package/esm/common/channal/http-channel.d.ts +0 -8
  364. package/esm/common/channal/http-channel.js +0 -22
  365. package/esm/common/channal/http-channel.js.map +0 -1
  366. package/esm/common/channal/index.d.ts +0 -2
  367. package/esm/common/channal/index.js +0 -3
  368. package/esm/common/channal/index.js.map +0 -1
  369. package/esm/common/factory/connection-factory.d.ts +0 -9
  370. package/esm/common/factory/connection-factory.js +0 -25
  371. package/esm/common/factory/connection-factory.js.map +0 -1
  372. package/esm/common/factory/index.d.ts +0 -4
  373. package/esm/common/factory/index.js +0 -5
  374. package/esm/common/factory/index.js.map +0 -1
  375. package/esm/common/factory/proxy-factory.d.ts +0 -56
  376. package/esm/common/factory/proxy-factory.js +0 -177
  377. package/esm/common/factory/proxy-factory.js.map +0 -1
  378. package/esm/common/factory/reader.d.ts +0 -15
  379. package/esm/common/factory/reader.js +0 -44
  380. package/esm/common/factory/reader.js.map +0 -1
  381. package/esm/common/factory/writer.d.ts +0 -9
  382. package/esm/common/factory/writer.js +0 -21
  383. package/esm/common/factory/writer.js.map +0 -1
  384. package/esm/common/handler/connection-handler.d.ts +0 -6
  385. package/esm/common/handler/connection-handler.js +0 -4
  386. package/esm/common/handler/connection-handler.js.map +0 -1
  387. package/esm/common/handler/handler-protocol.d.ts +0 -6
  388. package/esm/common/handler/handler-protocol.js +0 -2
  389. package/esm/common/handler/handler-protocol.js.map +0 -1
  390. package/esm/common/handler/index.d.ts +0 -2
  391. package/esm/common/handler/index.js +0 -3
  392. package/esm/common/handler/index.js.map +0 -1
  393. package/esm/common/proxy/delegating-http-proxy-creator.d.ts +0 -23
  394. package/esm/common/proxy/delegating-http-proxy-creator.js +0 -86
  395. package/esm/common/proxy/delegating-http-proxy-creator.js.map +0 -1
  396. package/esm/common/proxy/http-proxy-creator.d.ts +0 -42
  397. package/esm/common/proxy/http-proxy-creator.js +0 -139
  398. package/esm/common/proxy/http-proxy-creator.js.map +0 -1
  399. package/esm/common/proxy/proxy-protocol.d.ts +0 -20
  400. package/esm/common/proxy/proxy-protocol.js +0 -3
  401. package/esm/common/proxy/proxy-protocol.js.map +0 -1
  402. package/esm/common/proxy/proxy-provider.d.ts +0 -9
  403. package/esm/common/proxy/proxy-provider.js +0 -40
  404. package/esm/common/proxy/proxy-provider.js.map +0 -1
  405. package/esm/common/utils/logger.d.ts +0 -8
  406. package/esm/common/utils/logger.js +0 -18
  407. package/esm/common/utils/logger.js.map +0 -1
  408. package/esm/node/channel/channel-manager.d.ts +0 -9
  409. package/esm/node/channel/channel-manager.js +0 -56
  410. package/esm/node/channel/channel-manager.js.map +0 -1
  411. package/esm/node/channel/channel-middleware.d.ts +0 -7
  412. package/esm/node/channel/channel-middleware.js +0 -37
  413. package/esm/node/channel/channel-middleware.js.map +0 -1
  414. package/esm/node/channel/channel-protocol.d.ts +0 -12
  415. package/esm/node/channel/channel-protocol.js +0 -6
  416. package/esm/node/channel/channel-protocol.js.map +0 -1
  417. package/esm/node/channel/channel-strategy.d.ts +0 -9
  418. package/esm/node/channel/channel-strategy.js +0 -61
  419. package/esm/node/channel/channel-strategy.js.map +0 -1
  420. package/esm/node/channel/index.d.ts +0 -4
  421. package/esm/node/channel/index.js +0 -5
  422. package/esm/node/channel/index.js.map +0 -1
  423. package/esm/node/handler/handler-adapter.d.ts +0 -13
  424. package/esm/node/handler/handler-adapter.js +0 -52
  425. package/esm/node/handler/handler-adapter.js.map +0 -1
  426. package/esm/node/handler/handler-protocol.d.ts +0 -1
  427. package/esm/node/handler/handler-protocol.js +0 -2
  428. package/esm/node/handler/handler-protocol.js.map +0 -1
  429. package/esm/node/handler/index.d.ts +0 -2
  430. package/esm/node/handler/index.js +0 -3
  431. package/esm/node/handler/index.js.map +0 -1
  432. package/lib/common/channal/channel-protocol.d.ts +0 -52
  433. package/lib/common/channal/channel-protocol.d.ts.map +0 -1
  434. package/lib/common/channal/channel-protocol.js +0 -38
  435. package/lib/common/channal/channel-protocol.js.map +0 -1
  436. package/lib/common/channal/http-channel.d.ts +0 -9
  437. package/lib/common/channal/http-channel.d.ts.map +0 -1
  438. package/lib/common/channal/http-channel.js +0 -26
  439. package/lib/common/channal/http-channel.js.map +0 -1
  440. package/lib/common/channal/index.d.ts +0 -3
  441. package/lib/common/channal/index.d.ts.map +0 -1
  442. package/lib/common/channal/index.js.map +0 -1
  443. package/lib/common/factory/connection-factory.d.ts +0 -10
  444. package/lib/common/factory/connection-factory.d.ts.map +0 -1
  445. package/lib/common/factory/connection-factory.js +0 -28
  446. package/lib/common/factory/connection-factory.js.map +0 -1
  447. package/lib/common/factory/index.d.ts +0 -5
  448. package/lib/common/factory/index.d.ts.map +0 -1
  449. package/lib/common/factory/index.js.map +0 -1
  450. package/lib/common/factory/proxy-factory.d.ts +0 -57
  451. package/lib/common/factory/proxy-factory.d.ts.map +0 -1
  452. package/lib/common/factory/proxy-factory.js +0 -182
  453. package/lib/common/factory/proxy-factory.js.map +0 -1
  454. package/lib/common/factory/reader.d.ts +0 -16
  455. package/lib/common/factory/reader.d.ts.map +0 -1
  456. package/lib/common/factory/reader.js +0 -48
  457. package/lib/common/factory/reader.js.map +0 -1
  458. package/lib/common/factory/writer.d.ts +0 -10
  459. package/lib/common/factory/writer.d.ts.map +0 -1
  460. package/lib/common/factory/writer.js +0 -25
  461. package/lib/common/factory/writer.js.map +0 -1
  462. package/lib/common/handler/connection-handler.d.ts +0 -7
  463. package/lib/common/handler/connection-handler.d.ts.map +0 -1
  464. package/lib/common/handler/connection-handler.js +0 -8
  465. package/lib/common/handler/connection-handler.js.map +0 -1
  466. package/lib/common/handler/handler-protocol.d.ts +0 -7
  467. package/lib/common/handler/handler-protocol.d.ts.map +0 -1
  468. package/lib/common/handler/handler-protocol.js +0 -5
  469. package/lib/common/handler/handler-protocol.js.map +0 -1
  470. package/lib/common/handler/index.d.ts +0 -3
  471. package/lib/common/handler/index.d.ts.map +0 -1
  472. package/lib/common/handler/index.js.map +0 -1
  473. package/lib/common/proxy/delegating-http-proxy-creator.d.ts +0 -24
  474. package/lib/common/proxy/delegating-http-proxy-creator.d.ts.map +0 -1
  475. package/lib/common/proxy/delegating-http-proxy-creator.js +0 -89
  476. package/lib/common/proxy/delegating-http-proxy-creator.js.map +0 -1
  477. package/lib/common/proxy/http-proxy-creator.d.ts +0 -43
  478. package/lib/common/proxy/http-proxy-creator.d.ts.map +0 -1
  479. package/lib/common/proxy/http-proxy-creator.js +0 -143
  480. package/lib/common/proxy/http-proxy-creator.js.map +0 -1
  481. package/lib/common/proxy/proxy-protocol.d.ts +0 -21
  482. package/lib/common/proxy/proxy-protocol.d.ts.map +0 -1
  483. package/lib/common/proxy/proxy-protocol.js +0 -6
  484. package/lib/common/proxy/proxy-protocol.js.map +0 -1
  485. package/lib/common/proxy/proxy-provider.d.ts +0 -10
  486. package/lib/common/proxy/proxy-provider.d.ts.map +0 -1
  487. package/lib/common/proxy/proxy-provider.js +0 -43
  488. package/lib/common/proxy/proxy-provider.js.map +0 -1
  489. package/lib/common/utils/logger.d.ts +0 -9
  490. package/lib/common/utils/logger.d.ts.map +0 -1
  491. package/lib/common/utils/logger.js +0 -22
  492. package/lib/common/utils/logger.js.map +0 -1
  493. package/lib/node/channel/channel-manager.d.ts +0 -10
  494. package/lib/node/channel/channel-manager.d.ts.map +0 -1
  495. package/lib/node/channel/channel-manager.js +0 -59
  496. package/lib/node/channel/channel-manager.js.map +0 -1
  497. package/lib/node/channel/channel-middleware.d.ts +0 -8
  498. package/lib/node/channel/channel-middleware.d.ts.map +0 -1
  499. package/lib/node/channel/channel-middleware.js +0 -40
  500. package/lib/node/channel/channel-middleware.js.map +0 -1
  501. package/lib/node/channel/channel-protocol.d.ts +0 -13
  502. package/lib/node/channel/channel-protocol.d.ts.map +0 -1
  503. package/lib/node/channel/channel-protocol.js +0 -9
  504. package/lib/node/channel/channel-protocol.js.map +0 -1
  505. package/lib/node/channel/channel-strategy.d.ts +0 -10
  506. package/lib/node/channel/channel-strategy.d.ts.map +0 -1
  507. package/lib/node/channel/channel-strategy.js +0 -64
  508. package/lib/node/channel/channel-strategy.js.map +0 -1
  509. package/lib/node/channel/index.d.ts +0 -5
  510. package/lib/node/channel/index.d.ts.map +0 -1
  511. package/lib/node/channel/index.js.map +0 -1
  512. package/lib/node/handler/handler-adapter.d.ts +0 -14
  513. package/lib/node/handler/handler-adapter.d.ts.map +0 -1
  514. package/lib/node/handler/handler-adapter.js.map +0 -1
  515. package/lib/node/handler/handler-protocol.d.ts +0 -2
  516. package/lib/node/handler/handler-protocol.d.ts.map +0 -1
  517. package/lib/node/handler/handler-protocol.js +0 -5
  518. package/lib/node/handler/handler-protocol.js.map +0 -1
  519. package/lib/node/handler/index.d.ts +0 -3
  520. package/lib/node/handler/index.d.ts.map +0 -1
  521. package/lib/node/handler/index.js.map +0 -1
  522. package/src/common/channal/channel-protocol.ts +0 -82
  523. package/src/common/channal/http-channel.ts +0 -30
  524. package/src/common/channal/index.ts +0 -2
  525. package/src/common/factory/connection-factory.ts +0 -21
  526. package/src/common/factory/index.ts +0 -4
  527. package/src/common/factory/proxy-factory.ts +0 -219
  528. package/src/common/factory/reader.ts +0 -50
  529. package/src/common/factory/writer.ts +0 -26
  530. package/src/common/handler/connection-handler.ts +0 -7
  531. package/src/common/handler/handler-protocol.ts +0 -8
  532. package/src/common/handler/index.ts +0 -2
  533. package/src/common/proxy/delegating-http-proxy-creator.ts +0 -68
  534. package/src/common/proxy/http-proxy-creator.ts +0 -179
  535. package/src/common/proxy/proxy-protocol.ts +0 -26
  536. package/src/common/proxy/proxy-provider.ts +0 -29
  537. package/src/common/utils/logger.ts +0 -23
  538. package/src/node/channel/channel-manager.ts +0 -44
  539. package/src/node/channel/channel-middleware.ts +0 -27
  540. package/src/node/channel/channel-protocol.ts +0 -21
  541. package/src/node/channel/channel-strategy.ts +0 -56
  542. package/src/node/channel/index.ts +0 -4
  543. package/src/node/handler/handler-adapter.ts +0 -38
  544. package/src/node/handler/handler-protocol.ts +0 -1
  545. package/src/node/handler/index.ts +0 -2
@@ -0,0 +1,59 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const bun_test_1 = require("bun:test");
4
+ const websocket_frames_1 = require("./websocket-frames");
5
+ /** A frame as a client sends it: masked. */
6
+ function clientFrame(opcode, payload, fin = true) {
7
+ const plain = websocket_frames_1.WebSocketFrames.encode(opcode, payload);
8
+ const headerLength = plain.length - payload.length;
9
+ const mask = Buffer.from([1, 2, 3, 4]);
10
+ const header = Buffer.from(plain.subarray(0, headerLength));
11
+ header[0] = (fin ? 0x80 : 0) | opcode;
12
+ header[1] |= 0x80;
13
+ const masked = Buffer.from(payload.map((b, i) => b ^ mask[i & 3]));
14
+ return Buffer.concat([header, mask, masked]);
15
+ }
16
+ function protocolError(fn) {
17
+ try {
18
+ fn();
19
+ }
20
+ catch (error) {
21
+ return error;
22
+ }
23
+ throw new Error('expected a WebSocketProtocolError');
24
+ }
25
+ (0, bun_test_1.describe)('WebSocketFrames', () => {
26
+ (0, bun_test_1.it)('encodes payloads of 7-, 16- and 64-bit lengths, unmasked', () => {
27
+ (0, bun_test_1.expect)([...websocket_frames_1.WebSocketFrames.encode(websocket_frames_1.WebSocketFrames.TEXT, Buffer.from('hi'))]).toEqual([0x81, 2, 0x68, 0x69]);
28
+ (0, bun_test_1.expect)(websocket_frames_1.WebSocketFrames.encode(websocket_frames_1.WebSocketFrames.TEXT, Buffer.alloc(300)).subarray(0, 4)).toEqual(Buffer.from([0x81, 126, 1, 44]));
29
+ (0, bun_test_1.expect)(websocket_frames_1.WebSocketFrames.encode(websocket_frames_1.WebSocketFrames.TEXT, Buffer.alloc(70000)).subarray(0, 10)).toEqual(Buffer.from([0x81, 127, 0, 0, 0, 0, 0, 1, 0x11, 0x70]));
30
+ });
31
+ (0, bun_test_1.it)('parses masked frames of every length, also when they arrive in pieces', () => {
32
+ const parser = new websocket_frames_1.WebSocketFrameParser(1 << 20);
33
+ for (const size of [0, 125, 126, 65535, 65536]) {
34
+ const payload = Buffer.alloc(size, 7);
35
+ const bytes = clientFrame(websocket_frames_1.WebSocketFrames.TEXT, payload);
36
+ const frames = [...parser.push(bytes.subarray(0, 3)), ...parser.push(bytes.subarray(3))];
37
+ (0, bun_test_1.expect)(frames.length).toBe(1);
38
+ (0, bun_test_1.expect)(frames[0].payload.equals(payload)).toBe(true);
39
+ }
40
+ const two = Buffer.concat([clientFrame(websocket_frames_1.WebSocketFrames.TEXT, Buffer.from('a'), false), clientFrame(websocket_frames_1.WebSocketFrames.CONTINUATION, Buffer.from('b'))]);
41
+ (0, bun_test_1.expect)(parser.push(two).map(f => [f.opcode, f.fin, f.payload.toString()])).toEqual([[1, false, 'a'], [0, true, 'b']]);
42
+ });
43
+ (0, bun_test_1.it)('refuses unmasked frames, reserved bits, unknown opcodes, large control frames and too large messages', () => {
44
+ const parser = () => new websocket_frames_1.WebSocketFrameParser(1000);
45
+ (0, bun_test_1.expect)(protocolError(() => parser().push(websocket_frames_1.WebSocketFrames.encode(websocket_frames_1.WebSocketFrames.TEXT, Buffer.from('x')))).code).toBe(1002);
46
+ const reserved = clientFrame(websocket_frames_1.WebSocketFrames.TEXT, Buffer.from('x'));
47
+ reserved[0] |= 0x40;
48
+ (0, bun_test_1.expect)(protocolError(() => parser().push(reserved)).code).toBe(1002);
49
+ const unknown = clientFrame(websocket_frames_1.WebSocketFrames.TEXT, Buffer.from('x'));
50
+ unknown[0] = 0x83;
51
+ (0, bun_test_1.expect)(protocolError(() => parser().push(unknown)).code).toBe(1002);
52
+ (0, bun_test_1.expect)(protocolError(() => parser().push(clientFrame(websocket_frames_1.WebSocketFrames.PING, Buffer.alloc(126)))).code).toBe(1002);
53
+ (0, bun_test_1.expect)(protocolError(() => parser().push(clientFrame(websocket_frames_1.WebSocketFrames.TEXT, Buffer.alloc(1001)))).code).toBe(1009);
54
+ });
55
+ (0, bun_test_1.it)('writes the code and reason of a close frame', () => {
56
+ (0, bun_test_1.expect)([...websocket_frames_1.WebSocketFrames.closePayload(1001, 'bye')]).toEqual([3, 233, 98, 121, 101]);
57
+ });
58
+ });
59
+ //# sourceMappingURL=websocket-frames.spec.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"websocket-frames.spec.js","sourceRoot":"","sources":["../../../src/node/websocket/websocket-frames.spec.ts"],"names":[],"mappings":";;AAAA,uCAA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Ae,CAAC,IAAI,EAAE,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACtH,CAAC,CAAC,CAAC;IAEH,IAAA,aAAE,EAAC,6CAA6C,EAAE,GAAG,EAAE;QACnD,IAAA,iBAAM,EAAC,CAAC,GAAG,kCAAe,CAAC,YAAY,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,GAAG,EAAE,EAAE,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC,CAAC;IAC3F,CAAC,CAAC,CAAC;AACP,CAAC,CAAC,CAAC"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@celljs/rpc",
3
- "version": "4.2.0",
4
- "description": "",
3
+ "version": "4.4.0",
4
+ "description": "Typed JSON-RPC between the frontend and backends of Cell applications, over HTTP or WebSocket",
5
5
  "keywords": [
6
6
  "cell-component"
7
7
  ],
@@ -23,7 +23,8 @@
23
23
  "lib",
24
24
  "esm",
25
25
  "src",
26
- "cell.yml"
26
+ "cell.yml",
27
+ "cell.schema.json"
27
28
  ],
28
29
  "scripts": {
29
30
  "build": "tsc -b && bun ../../scripts/build-esm.ts",
@@ -32,11 +33,9 @@
32
33
  "watch": "tsc -b -w --preserveWatchOutput"
33
34
  },
34
35
  "dependencies": {
35
- "@celljs/core": "4.2.0",
36
- "@celljs/http": "4.2.0",
37
- "@celljs/web": "4.2.0",
38
- "axios": "^0.35.0",
39
- "vscode-jsonrpc": "^6.0.0"
36
+ "@celljs/core": "4.4.0",
37
+ "@celljs/http": "4.4.0",
38
+ "@celljs/web": "4.4.0"
40
39
  },
41
40
  "publishConfig": {
42
41
  "access": "public"
@@ -59,9 +58,9 @@
59
58
  "import": "./esm/common/annotation/detached/index.js",
60
59
  "default": "./lib/common/annotation/detached/index.js"
61
60
  },
62
- "./lib/common/channal": {
63
- "import": "./esm/common/channal/index.js",
64
- "default": "./lib/common/channal/index.js"
61
+ "./lib/common/client": {
62
+ "import": "./esm/common/client/index.js",
63
+ "default": "./lib/common/client/index.js"
65
64
  },
66
65
  "./lib/common/converter": {
67
66
  "import": "./esm/common/converter/index.js",
@@ -75,22 +74,22 @@
75
74
  "import": "./esm/common/error/index.js",
76
75
  "default": "./lib/common/error/index.js"
77
76
  },
78
- "./lib/common/factory": {
79
- "import": "./esm/common/factory/index.js",
80
- "default": "./lib/common/factory/index.js"
81
- },
82
- "./lib/common/handler": {
83
- "import": "./esm/common/handler/index.js",
84
- "default": "./lib/common/handler/index.js"
85
- },
86
77
  "./lib/common/processor": {
87
78
  "import": "./esm/common/processor/index.js",
88
79
  "default": "./lib/common/processor/index.js"
89
80
  },
81
+ "./lib/common/protocol": {
82
+ "import": "./esm/common/protocol/index.js",
83
+ "default": "./lib/common/protocol/index.js"
84
+ },
90
85
  "./lib/common/proxy": {
91
86
  "import": "./esm/common/proxy/index.js",
92
87
  "default": "./lib/common/proxy/index.js"
93
88
  },
89
+ "./lib/common/transport": {
90
+ "import": "./esm/common/transport/index.js",
91
+ "default": "./lib/common/transport/index.js"
92
+ },
94
93
  "./lib/common/utils": {
95
94
  "import": "./esm/common/utils/index.js",
96
95
  "default": "./lib/common/utils/index.js"
@@ -99,13 +98,17 @@
99
98
  "import": "./esm/node/index.js",
100
99
  "default": "./lib/node/index.js"
101
100
  },
102
- "./lib/node/channel": {
103
- "import": "./esm/node/channel/index.js",
104
- "default": "./lib/node/channel/index.js"
101
+ "./lib/node/connection": {
102
+ "import": "./esm/node/connection/index.js",
103
+ "default": "./lib/node/connection/index.js"
104
+ },
105
+ "./lib/node/server": {
106
+ "import": "./esm/node/server/index.js",
107
+ "default": "./lib/node/server/index.js"
105
108
  },
106
- "./lib/node/handler": {
107
- "import": "./esm/node/handler/index.js",
108
- "default": "./lib/node/handler/index.js"
109
+ "./lib/node/websocket": {
110
+ "import": "./esm/node/websocket/index.js",
111
+ "default": "./lib/node/websocket/index.js"
109
112
  },
110
113
  "./lib/*.js": {
111
114
  "import": "./esm/*.js",
@@ -1,2 +1,4 @@
1
+ export * from './rpc-protocol';
1
2
  export * from './rpc';
3
+ export * from './rpc-client';
2
4
  export * from './autorpc';
@@ -0,0 +1,17 @@
1
+ import { ComponentDecorator, applyComponentDecorator, parseComponentOption } from '@celljs/core';
2
+ import { RPC_CLIENT } from '../client/client-protocol';
3
+ import { RpcUtil } from '../utils/rpc-util';
4
+
5
+ /**
6
+ * A component of the frontend (or of a backend calling another one) that the backend calls over the
7
+ * WebSocket connection: `RpcConnection.client<T>(id)` on the server is a proxy of it. Needs
8
+ * `rpc.transport: websocket`.
9
+ */
10
+ export const RpcClient = <ComponentDecorator>function (...idOrOption: any): ClassDecorator {
11
+ return target => {
12
+ const { ids } = applyComponentDecorator(parseComponentOption(target, idOrOption), target);
13
+ const id = ids[1] ?? ids[0];
14
+ const path = RpcUtil.toPath(id);
15
+ applyComponentDecorator({ id: RPC_CLIENT, onActivation: ({ container }) => ({ path, get: () => container.get(id) }) }, target);
16
+ };
17
+ };
@@ -0,0 +1,8 @@
1
+ export const RPC_SERVICE = Symbol('RPC_SERVICE');
2
+
3
+ /** Registered by `@Rpc(id)`: the path of the service, and the component. */
4
+ export interface RpcServiceMetadata {
5
+ path: string;
6
+ id: import('@celljs/core').ComponentId;
7
+ get(): object;
8
+ }
@@ -1,11 +1,14 @@
1
- import { ComponentDecorator, applyComponentDecorator, ComponentOption, parseComponentOption, Logger, Container } from '@celljs/core';
2
- import { AOP_POINTCUT, PipeManager } from '@celljs/web';
3
- import { ConnectionHandler } from '../handler';
4
- import { JsonRpcConnectionHandler } from '../factory';
5
- import { RpcUtil, ConverterUtil } from '../utils';
1
+ import { ComponentDecorator, applyComponentDecorator, ComponentOption, parseComponentOption } from '@celljs/core';
2
+ import { AOP_POINTCUT } from '@celljs/web';
3
+ import { RpcUtil } from '../utils/rpc-util';
4
+ import { RPC_SERVICE } from './rpc-protocol';
6
5
 
7
6
  export const RPC_TAG = 'Rpc';
8
7
 
8
+ /**
9
+ * An RPC service: a component (it takes the options of `@Component`) whose methods are called with
10
+ * JSON-RPC at `POST /<rpc.path>/<id>` and over the WebSocket connection (`<id>/<method>`).
11
+ */
9
12
  export const Rpc = <ComponentDecorator>function (...idOrOption: any): ClassDecorator {
10
13
  return target => {
11
14
  const option = parseRpcOption(target, idOrOption);
@@ -15,8 +18,7 @@ export const Rpc = <ComponentDecorator>function (...idOrOption: any): ClassDecor
15
18
 
16
19
  export function parseRpcOption(target: any, idOrOption: any) {
17
20
  const parsed = parseComponentOption(target, idOrOption);
18
-
19
- if (idOrOption[0].proxy === undefined) {
21
+ if (idOrOption[0]?.proxy === undefined) {
20
22
  parsed.proxy = true;
21
23
  }
22
24
  parsed.sysTags!.push(AOP_POINTCUT, RPC_TAG);
@@ -25,17 +27,7 @@ export function parseRpcOption(target: any, idOrOption: any) {
25
27
 
26
28
  export function applyRpcDecorator(option: ComponentOption, target: any) {
27
29
  const { ids } = applyComponentDecorator(option, target);
28
- const id = ids[1] || ids[0];
29
- return applyComponentDecorator({ id: ConnectionHandler, onActivation: context => {
30
- const t = context.container.get(id);
31
- const pipeManager = context.container.get<PipeManager>(PipeManager);
32
- const logger = context.container.get<Logger>(Logger);
33
- const errorConverters = ConverterUtil.getGlobalErrorConverters(context.container as Container);
34
- const errorConverter = ConverterUtil.getErrorConverters(id, context.container as Container);
35
- if (errorConverter) {
36
- errorConverters.push(errorConverter);
37
- }
38
- return new JsonRpcConnectionHandler(RpcUtil.toPath(id), () => t, errorConverters, pipeManager, logger);
39
- }}, target);
40
- }
41
-
30
+ const id = ids[1] ?? ids[0];
31
+ const path = RpcUtil.toPath(id);
32
+ return applyComponentDecorator({ id: RPC_SERVICE, onActivation: ({ container }) => ({ path, id, get: () => container.get(id) }) }, target);
33
+ }
@@ -0,0 +1,7 @@
1
+ export const RPC_CLIENT = Symbol('RPC_CLIENT');
2
+
3
+ /** Registered by `@RpcClient(id)`: the path the backend calls it by, and the component. */
4
+ export interface RpcClientMetadata {
5
+ path: string;
6
+ get(): object;
7
+ }
@@ -0,0 +1,2 @@
1
+ export * from './client-protocol';
2
+ export * from './rpc-client-registry';
@@ -0,0 +1,42 @@
1
+ import { Autowired, Component, Optional } from '@celljs/core';
2
+ import { JsonRpc } from '../protocol';
3
+ import { RpcMethodUtil } from '../utils/rpc-method-util';
4
+ import { RPC_CLIENT, RpcClientMetadata } from './client-protocol';
5
+
6
+ export const RpcClientRegistry = Symbol('RpcClientRegistry');
7
+
8
+ /** The `@RpcClient` components, which answer the calls a backend makes over the WebSocket connection. */
9
+ export interface RpcClientRegistry {
10
+ readonly empty: boolean;
11
+ /** The response to a request of the server (`<client>/<method>`). */
12
+ handle(request: JsonRpc.Request): Promise<JsonRpc.Response>;
13
+ }
14
+
15
+ @Component(RpcClientRegistry)
16
+ export class RpcClientRegistryImpl implements RpcClientRegistry {
17
+
18
+ @Autowired(RPC_CLIENT) @Optional()
19
+ protected readonly clients: RpcClientMetadata[] = [];
20
+
21
+ get empty(): boolean {
22
+ return this.clients.length === 0;
23
+ }
24
+
25
+ async handle(request: JsonRpc.Request): Promise<JsonRpc.Response> {
26
+ const id = request.id ?? null;
27
+ const parts = RpcMethodUtil.split(request.method);
28
+ const metadata = parts && this.clients.find(c => c.path === parts[0]);
29
+ if (!parts || !metadata) {
30
+ return JsonRpc.error(id, JsonRpc.METHOD_NOT_FOUND, `No @RpcClient for ${request.method}.`);
31
+ }
32
+ const client = metadata.get() as Record<string, (...args: unknown[]) => unknown>;
33
+ if (!RpcMethodUtil.callable(client, parts[1])) {
34
+ return JsonRpc.error(id, JsonRpc.METHOD_NOT_FOUND, `${parts[0]} has no method ${parts[1]}.`);
35
+ }
36
+ try {
37
+ return JsonRpc.result(id, await client[parts[1]](...(request.params ?? [])));
38
+ } catch (error) {
39
+ return JsonRpc.error(id, JsonRpc.SERVER_ERROR, error instanceof Error ? error.message : String(error));
40
+ }
41
+ }
42
+ }
@@ -1 +1,4 @@
1
- export const RPC_PATH = 'cell.rpc.path';
1
+ export const RPC_PATH = 'rpc.path';
2
+
3
+ /** The WebSocket endpoint, below `rpc.path`. */
4
+ export const RPC_WEBSOCKET_PATH = 'ws';
@@ -1,8 +1,13 @@
1
+ import { JsonRpc } from '../protocol';
2
+
1
3
  export const ErrorConverter = Symbol('ErrorConverter');
2
4
 
5
+ /** Named `GlobalConverter`, an `ErrorConverter` applies to every service; named after a service id, to that one. */
3
6
  export const GlobalConverter = Symbol('GlobalConverter');
4
7
 
5
8
  export interface ErrorConverter {
6
- serialize(e: any): any;
7
- deserialize(e: any): any
9
+ /** Server: the JSON-RPC error for an error a service threw, or undefined for the default. */
10
+ serialize(error: unknown): JsonRpc.ErrorObject | undefined;
11
+ /** Client: the error to throw for the error of a response, or undefined to keep it. */
12
+ deserialize(error: Error): Error | undefined;
8
13
  }
@@ -1,5 +1,8 @@
1
1
  export const EndpointResolver = Symbol('EndpointResolver');
2
2
 
3
3
  export interface EndpointResolver {
4
+ /** The HTTP URL of a service. */
4
5
  resolve(serviceIdentifier: string): Promise<string>;
6
+ /** The URL of the WebSocket endpoint: `rpc.websocket.endpoint`, or `/<rpc.path>/ws` of the page's origin. */
7
+ resolveWebSocket?(): Promise<string>;
5
8
  }
@@ -1,5 +1,5 @@
1
1
  import { Autowired, Component, Value } from '@celljs/core';
2
- import { RPC_PATH } from '../constants';
2
+ import { RPC_PATH, RPC_WEBSOCKET_PATH } from '../constants';
3
3
  import { PathResolver } from '@celljs/web';
4
4
  import { EndpointNotFoundError } from '../error';
5
5
  import { EndpointResolver } from './endpoint-protocol';
@@ -7,15 +7,18 @@ import { EndpointResolver } from './endpoint-protocol';
7
7
  @Component(EndpointResolver)
8
8
  export class EndpointResolverImpl implements EndpointResolver {
9
9
 
10
- @Value('cell.rpc.endpoint')
10
+ @Value('rpc.endpoint')
11
11
  protected readonly endpoint?: { [id: string]: string };
12
12
 
13
- @Value('cell.rpc.defaultEndpoint')
13
+ @Value('rpc.defaultEndpoint')
14
14
  protected readonly defaultEndpoint?: string;
15
15
 
16
16
  @Value(RPC_PATH)
17
17
  protected readonly rpcPath: string;
18
18
 
19
+ @Value('rpc.websocket.endpoint')
20
+ protected readonly websocketEndpoint?: string;
21
+
19
22
  @Autowired(PathResolver)
20
23
  protected pathResolver: PathResolver;
21
24
 
@@ -43,4 +46,15 @@ export class EndpointResolverImpl implements EndpointResolver {
43
46
  .replace(/{serviceIdentifier}/g, serviceIdentifier);
44
47
  }
45
48
 
46
- }
49
+ async resolveWebSocket(): Promise<string> {
50
+ if (this.websocketEndpoint) {
51
+ return this.websocketEndpoint.replace(/{rpcPath}/g, await this.pathResolver.resolve(this.rpcPath));
52
+ }
53
+ if (typeof location === 'undefined') {
54
+ throw new EndpointNotFoundError('rpc.websocket.endpoint');
55
+ }
56
+ const protocol = location.protocol === 'https:' ? 'wss:' : 'ws:';
57
+ return `${protocol}//${location.host}${await this.pathResolver.resolve(this.rpcPath, RPC_WEBSOCKET_PATH)}`;
58
+ }
59
+
60
+ }
@@ -1,7 +1,46 @@
1
- import { CustomError } from '@celljs/core';
1
+ import { ApplicationError, CustomError } from '@celljs/core';
2
+ import { JsonRpc } from '../protocol';
2
3
 
3
4
  export class EndpointNotFoundError extends CustomError {
4
5
  constructor(public serviceId: string) {
5
- super(`No endpoint found: ${serviceId}`);
6
+ super(`No endpoint found for the RPC service ${serviceId}: set rpc.endpoint.${serviceId} or rpc.defaultEndpoint.`);
7
+ }
8
+ }
9
+
10
+ /** What the remote side of a call answered with, or why there is no answer. */
11
+ export interface RpcErrorData {
12
+ /** HTTP status: of the error the service threw (`HttpError`), or of the answer of an HTTP call. */
13
+ status?: number;
14
+ /** `application` for an `ApplicationError`, whose code and data are kept. */
15
+ kind?: 'application';
16
+ [key: string]: unknown;
17
+ }
18
+
19
+ /**
20
+ * A call that failed: `code` is the JSON-RPC error code ({@link JsonRpc}), `data.status` the HTTP status
21
+ * when there is one. The stack ends with the place of the call.
22
+ */
23
+ export class RpcError extends CustomError {
24
+ constructor(message: string, readonly code: number = JsonRpc.SERVER_ERROR, readonly data: RpcErrorData = {}) {
25
+ super(message);
26
+ }
27
+
28
+ get status(): number | undefined {
29
+ return this.data.status;
30
+ }
31
+ }
32
+
33
+ export namespace RpcErrorUtil {
34
+
35
+ /** The error of a JSON-RPC answer: an `ApplicationError` again when it was one, an {@link RpcError} otherwise. */
36
+ export function fromErrorObject(error: JsonRpc.ErrorObject, call: Error): Error {
37
+ const data = (typeof error.data === 'object' && error.data !== null ? error.data : {}) as RpcErrorData;
38
+ const stack = call.stack?.replace(/^.*\n/, '') ?? '';
39
+ if (data.kind === 'application') {
40
+ return ApplicationError.fromJson(error.code, { message: error.message, data: data.data, stack: `Error: ${error.message}\n${stack}` });
41
+ }
42
+ const result = new RpcError(error.message, error.code, data);
43
+ result.stack = `${result.name}: ${result.message}\n${stack}`;
44
+ return result;
6
45
  }
7
46
  }
@@ -1,11 +1,11 @@
1
1
  export * from './annotation';
2
- export * from './channal';
2
+ export * from './client';
3
+ export * from './constants';
4
+ export * from './converter';
3
5
  export * from './endpoint';
4
6
  export * from './error';
5
- export * from './factory';
6
- export * from './handler';
7
+ export * from './processor';
8
+ export * from './protocol';
7
9
  export * from './proxy';
10
+ export * from './transport';
8
11
  export * from './utils';
9
- export * from './constants';
10
- export * from './converter';
11
- export * from './processor';
@@ -1,21 +1,20 @@
1
- import { autoBind } from '@celljs/core';
2
- import { ProxyProvider } from './proxy';
1
+ import { autoBind, ComponentId } from '@celljs/core';
3
2
  import { ID_KEY, RPC } from './annotation';
4
- import { RpcUtil, ConverterUtil } from './utils';
3
+ import { RpcProxy } from './proxy';
4
+ import { RpcTransportProvider } from './transport';
5
+ import { ConverterUtil, RpcUtil } from './utils';
5
6
  import './index';
6
7
 
7
8
  export default autoBind(bind => {
8
-
9
+ // @Autorpc(id) and RpcUtil.get(id): a proxy of the service, with its error converters.
9
10
  bind(RPC).toDynamicValue(ctx => {
10
- const id = ctx.target.tags![ID_KEY];
11
+ const id = ctx.target.tags![ID_KEY] as ComponentId;
11
12
  const path = RpcUtil.toPath(id);
12
- const proxyProvider = ctx.container.get<ProxyProvider>(ProxyProvider);
13
13
  const errorConverters = ConverterUtil.getGlobalErrorConverters(ctx.container);
14
14
  const errorConverter = ConverterUtil.getErrorConverters(id, ctx.container);
15
15
  if (errorConverter) {
16
16
  errorConverters.push(errorConverter);
17
17
  }
18
- return proxyProvider.provide(path, errorConverters);
18
+ return RpcProxy.create(path, ctx.container.get<RpcTransportProvider>(RpcTransportProvider).provide(path), errorConverters);
19
19
  });
20
-
21
20
  });
@@ -1,11 +1,11 @@
1
1
  import { Component } from '@celljs/core';
2
2
  import { ClientConfigProcessor } from './processor-protocol';
3
- import { AxiosRequestConfig } from 'axios';
3
+ import { RequestConfig } from '@celljs/http';
4
4
 
5
5
  @Component(ClientConfigProcessor)
6
6
  export class NoOpClientConfigProcessor implements ClientConfigProcessor {
7
7
 
8
- async process(config: AxiosRequestConfig): Promise<void> {
8
+ async process(config: RequestConfig): Promise<void> {
9
9
 
10
10
  }
11
11
 
@@ -1,7 +1,7 @@
1
- import { AxiosRequestConfig } from 'axios';
1
+ import { RequestConfig } from '@celljs/http';
2
2
 
3
3
  export const ClientConfigProcessor = Symbol('ClientConfigProcessor');
4
4
 
5
5
  export interface ClientConfigProcessor {
6
- process(config: AxiosRequestConfig): Promise<void>;
6
+ process(config: RequestConfig): Promise<void>;
7
7
  }
@@ -0,0 +1 @@
1
+ export * from './json-rpc';
@@ -0,0 +1,74 @@
1
+ /**
2
+ * JSON-RPC 2.0 (https://www.jsonrpc.org/specification), the format of every RPC message: a POST body
3
+ * of `/<rpc.path>/<service>`, or a WebSocket message, where methods are `<service>/<method>`.
4
+ */
5
+ export namespace JsonRpc {
6
+
7
+ export type Id = number | string;
8
+
9
+ export interface Request {
10
+ jsonrpc: '2.0';
11
+ /** Absent for a notification, which gets no response. */
12
+ id?: Id;
13
+ method: string;
14
+ params?: unknown[];
15
+ }
16
+
17
+ export interface ErrorObject {
18
+ code: number;
19
+ message: string;
20
+ data?: unknown;
21
+ }
22
+
23
+ export interface Response {
24
+ jsonrpc: '2.0';
25
+ id: Id | null;
26
+ result?: unknown;
27
+ error?: ErrorObject;
28
+ }
29
+
30
+ export type Message = Request | Response;
31
+
32
+ export const PARSE_ERROR = -32700;
33
+ export const INVALID_REQUEST = -32600;
34
+ export const METHOD_NOT_FOUND = -32601;
35
+ export const INVALID_PARAMS = -32602;
36
+ export const INTERNAL_ERROR = -32603;
37
+ /** An error thrown by the service, or an HTTP error status (`data.status`). */
38
+ export const SERVER_ERROR = -32000;
39
+ /** The WebSocket connection closed before the response arrived. */
40
+ export const CONNECTION_CLOSED = -32003;
41
+ /** No response within the timeout. */
42
+ export const TIMEOUT = -32004;
43
+
44
+ export function isRequest(message: unknown): message is Request {
45
+ return isObject(message) && message.jsonrpc === '2.0' && typeof message.method === 'string'
46
+ && (message.params === undefined || Array.isArray(message.params)) && (message.id === undefined || isId(message.id));
47
+ }
48
+
49
+ export function isResponse(message: unknown): message is Response {
50
+ return isObject(message) && message.jsonrpc === '2.0' && typeof message.method !== 'string' && ('result' in message || isObject(message.error))
51
+ && (message.id === null || isId(message.id));
52
+ }
53
+
54
+ export function request(id: Id | undefined, method: string, params: unknown[]): Request {
55
+ return id === undefined ? { jsonrpc: '2.0', method, params } : { jsonrpc: '2.0', id, method, params };
56
+ }
57
+
58
+ export function result(id: Id | null, value: unknown): Response {
59
+ // undefined is not JSON: a method without a result answers null.
60
+ return { jsonrpc: '2.0', id, result: value === undefined ? null : value };
61
+ }
62
+
63
+ export function error(id: Id | null, code: number, message: string, data?: unknown): Response {
64
+ return { jsonrpc: '2.0', id, error: data === undefined ? { code, message } : { code, message, data } };
65
+ }
66
+
67
+ function isId(id: unknown): id is Id {
68
+ return typeof id === 'string' || (typeof id === 'number' && Number.isFinite(id));
69
+ }
70
+
71
+ function isObject(value: unknown): value is Record<string, any> {
72
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
73
+ }
74
+ }
@@ -1,5 +1 @@
1
- export * from './proxy-provider';
2
- export * from './proxy-protocol';
3
- export * from './http-proxy-creator';
4
- export * from './delegating-http-proxy-creator';
5
-
1
+ export * from './rpc-proxy';
@@ -0,0 +1,42 @@
1
+ import { ErrorConverter } from '../converter';
2
+ import { RpcErrorUtil } from '../error';
3
+ import { RpcTransport } from '../transport/transport-protocol';
4
+
5
+ /** A typed proxy of a remote service: every method returns a promise of the remote result. */
6
+ export namespace RpcProxy {
7
+
8
+ export function create<T extends object>(service: string, transport: RpcTransport, errorConverters: ErrorConverter[] = []): T {
9
+ const methods = new Map<string, (...args: unknown[]) => Promise<unknown>>();
10
+ return new Proxy({} as T, {
11
+ get(_target, property) {
12
+ // Not a thenable, not serializable: awaiting or logging the proxy makes no call.
13
+ if (typeof property !== 'string' || property === 'then' || property === 'toJSON') {
14
+ return undefined;
15
+ }
16
+ let method = methods.get(property);
17
+ if (!method) {
18
+ method = (...params: unknown[]) => invoke(service, property, params, transport, errorConverters);
19
+ methods.set(property, method);
20
+ }
21
+ return method;
22
+ },
23
+ });
24
+ }
25
+
26
+ async function invoke(service: string, method: string, params: unknown[], transport: RpcTransport, errorConverters: ErrorConverter[]): Promise<unknown> {
27
+ // Created before the call, so the stack of the error leads to the caller.
28
+ const call = new Error(`${service}.${method}`);
29
+ const response = await transport.call(service, method, params);
30
+ if (!response.error) {
31
+ return response.result;
32
+ }
33
+ const error = RpcErrorUtil.fromErrorObject(response.error, call);
34
+ for (const converter of errorConverters) {
35
+ const converted = converter.deserialize(error);
36
+ if (converted) {
37
+ throw converted;
38
+ }
39
+ }
40
+ throw error;
41
+ }
42
+ }