@push-rpc/next 2.0.12 → 2.0.14

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 (72) hide show
  1. package/dist/client/HttpClient.d.ts +11 -0
  2. package/dist/client/HttpClient.js +84 -0
  3. package/dist/client/HttpClient.js.map +1 -0
  4. package/dist/client/RemoteSubscriptions.d.ts +18 -0
  5. package/dist/client/RemoteSubscriptions.js +120 -0
  6. package/dist/client/RemoteSubscriptions.js.map +1 -0
  7. package/dist/client/RpcClientImpl.d.ts +22 -0
  8. package/dist/client/RpcClientImpl.js +121 -0
  9. package/dist/client/RpcClientImpl.js.map +1 -0
  10. package/dist/client/WebSocketConnection.d.ts +36 -0
  11. package/dist/client/WebSocketConnection.js +161 -0
  12. package/dist/client/WebSocketConnection.js.map +1 -0
  13. package/dist/client/index.d.ts +29 -0
  14. package/dist/client/index.js +39 -0
  15. package/dist/client/index.js.map +1 -0
  16. package/dist/client/remote.d.ts +14 -0
  17. package/dist/client/remote.js +66 -0
  18. package/dist/client/remote.js.map +1 -0
  19. package/dist/index.d.ts +13 -0
  20. package/dist/index.js.map +1 -0
  21. package/dist/logger.d.ts +8 -0
  22. package/dist/logger.js +9 -0
  23. package/dist/logger.js.map +1 -0
  24. package/dist/rpc.d.ts +37 -0
  25. package/dist/rpc.js +33 -0
  26. package/dist/rpc.js.map +1 -0
  27. package/dist/server/ConnectionsServer.d.ts +13 -0
  28. package/dist/server/ConnectionsServer.js +72 -0
  29. package/dist/server/ConnectionsServer.js.map +1 -0
  30. package/dist/server/LocalSubscriptions.d.ts +12 -0
  31. package/dist/server/LocalSubscriptions.js +113 -0
  32. package/dist/server/LocalSubscriptions.js.map +1 -0
  33. package/dist/server/RpcServerImpl.d.ts +24 -0
  34. package/dist/server/RpcServerImpl.js +200 -0
  35. package/dist/server/RpcServerImpl.js.map +1 -0
  36. package/dist/server/http.d.ts +9 -0
  37. package/dist/server/http.js +93 -0
  38. package/dist/server/http.js.map +1 -0
  39. package/dist/server/index.d.ts +32 -0
  40. package/dist/server/index.js +33 -0
  41. package/dist/server/index.js.map +1 -0
  42. package/dist/server/local.d.ts +15 -0
  43. package/dist/server/local.js +46 -0
  44. package/dist/server/local.js.map +1 -0
  45. package/dist/utils/cookies.d.ts +7 -0
  46. package/dist/utils/cookies.js +31 -0
  47. package/dist/utils/cookies.js.map +1 -0
  48. package/dist/utils/env.d.ts +6 -0
  49. package/dist/utils/env.js +22 -0
  50. package/dist/utils/env.js.map +1 -0
  51. package/dist/utils/json.d.ts +2 -0
  52. package/dist/utils/json.js +34 -0
  53. package/dist/utils/json.js.map +1 -0
  54. package/dist/utils/middleware.d.ts +2 -0
  55. package/dist/utils/middleware.js +34 -0
  56. package/dist/utils/middleware.js.map +1 -0
  57. package/dist/utils/promises.d.ts +5 -0
  58. package/dist/utils/promises.js +29 -0
  59. package/dist/utils/promises.js.map +1 -0
  60. package/dist/utils/server.d.ts +5 -0
  61. package/dist/utils/server.js +48 -0
  62. package/dist/utils/server.js.map +1 -0
  63. package/dist/utils/throttle.d.ts +4 -0
  64. package/dist/utils/throttle.js +40 -0
  65. package/dist/utils/throttle.js.map +1 -0
  66. package/dist/utils/types.d.ts +1 -0
  67. package/dist/utils/types.js +3 -0
  68. package/dist/utils/types.js.map +1 -0
  69. package/package.json +1 -1
  70. package/src/client/HttpClient.ts +5 -4
  71. package/src/server/http.ts +10 -5
  72. package/package-lock.json +0 -1291
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.adelay = exports.PromiseCache = void 0;
4
+ class PromiseCache {
5
+ constructor() {
6
+ this.cache = {};
7
+ }
8
+ invoke(cacheKey, supplier) {
9
+ const key = JSON.stringify(cacheKey);
10
+ if (!this.cache[key]) {
11
+ this.cache[key] = supplier()
12
+ .then((r) => {
13
+ delete this.cache[key];
14
+ return r;
15
+ })
16
+ .catch((e) => {
17
+ delete this.cache[key];
18
+ throw e;
19
+ });
20
+ }
21
+ return this.cache[key];
22
+ }
23
+ }
24
+ exports.PromiseCache = PromiseCache;
25
+ async function adelay(ms) {
26
+ return new Promise((r) => setTimeout(r, ms));
27
+ }
28
+ exports.adelay = adelay;
29
+ //# sourceMappingURL=promises.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"promises.js","sourceRoot":"","sources":["../../src/utils/promises.ts"],"names":[],"mappings":";;;AAAA,MAAa,YAAY;IAAzB;QAmBU,UAAK,GAAkC,EAAE,CAAA;IACnD,CAAC;IAnBC,MAAM,CAAI,QAAiB,EAAE,QAA0B;QACrD,MAAM,GAAG,GAAG,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAA;QAEpC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE,CAAC;YACrB,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,QAAQ,EAAE;iBACzB,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE;gBACV,OAAO,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;gBACtB,OAAO,CAAC,CAAA;YACV,CAAC,CAAC;iBACD,KAAK,CAAC,CAAC,CAAC,EAAE,EAAE;gBACX,OAAO,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;gBACtB,MAAM,CAAC,CAAA;YACT,CAAC,CAAC,CAAA;QACN,CAAC;QAED,OAAO,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;IACxB,CAAC;CAGF;AApBD,oCAoBC;AAEM,KAAK,UAAU,MAAM,CAAC,EAAU;IACrC,OAAO,IAAI,OAAO,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,UAAU,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAA;AAC9C,CAAC;AAFD,wBAEC"}
@@ -0,0 +1,5 @@
1
+ /// <reference types="node" />
2
+ /// <reference types="node" />
3
+ import * as http from "http";
4
+ import * as Stream from "stream";
5
+ export declare function decompressRequest(request: http.IncomingMessage): Stream;
@@ -0,0 +1,48 @@
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 (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.decompressRequest = void 0;
27
+ const zlib = __importStar(require("zlib"));
28
+ const stream_1 = require("stream");
29
+ function decompressRequest(request) {
30
+ const contentEncoding = (request.headers["content-encoding"] || "").toLowerCase();
31
+ if (!["gzip", "deflate", "br"].includes(contentEncoding)) {
32
+ return request;
33
+ }
34
+ const isBrotli = contentEncoding === "br";
35
+ const decompress = isBrotli ? zlib.createBrotliDecompress() : zlib.createUnzip();
36
+ const stream = new stream_1.PassThrough();
37
+ decompress.on("error", (error) => {
38
+ // Ignore empty request
39
+ if (error.code === "Z_BUF_ERROR") {
40
+ stream.end();
41
+ return;
42
+ }
43
+ stream.emit("error", error);
44
+ });
45
+ return (0, stream_1.pipeline)(request, decompress, stream, () => { });
46
+ }
47
+ exports.decompressRequest = decompressRequest;
48
+ //# sourceMappingURL=server.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"server.js","sourceRoot":"","sources":["../../src/utils/server.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AACA,2CAA4B;AAC5B,mCAAmF;AAGnF,SAAgB,iBAAiB,CAAC,OAA6B;IAC7D,MAAM,eAAe,GAAG,CAAC,OAAO,CAAC,OAAO,CAAC,kBAAkB,CAAC,IAAI,EAAE,CAAC,CAAC,WAAW,EAAE,CAAA;IAEjF,IAAI,CAAC,CAAC,MAAM,EAAE,SAAS,EAAE,IAAI,CAAC,CAAC,QAAQ,CAAC,eAAe,CAAC,EAAE,CAAC;QACzD,OAAO,OAAO,CAAA;IAChB,CAAC;IAED,MAAM,QAAQ,GAAG,eAAe,KAAK,IAAI,CAAA;IAEzC,MAAM,UAAU,GAAG,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,sBAAsB,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,WAAW,EAAE,CAAA;IAChF,MAAM,MAAM,GAAG,IAAI,oBAAiB,EAAE,CAAA;IAEtC,UAAU,CAAC,EAAE,CAAC,OAAO,EAAE,CAAC,KAAU,EAAE,EAAE;QACpC,uBAAuB;QACvB,IAAI,KAAK,CAAC,IAAI,KAAK,aAAa,EAAE,CAAC;YACjC,MAAM,CAAC,GAAG,EAAE,CAAA;YACZ,OAAM;QACR,CAAC;QAED,MAAM,CAAC,IAAI,CAAC,OAAO,EAAE,KAAK,CAAC,CAAA;IAC7B,CAAC,CAAC,CAAA;IAEF,OAAO,IAAA,iBAAc,EAAC,OAAO,EAAE,UAAU,EAAE,MAAM,EAAE,GAAG,EAAE,GAAE,CAAC,CAAC,CAAA;AAC9D,CAAC;AAvBD,8CAuBC"}
@@ -0,0 +1,4 @@
1
+ export type ThrottleArgsReducer<D> = (prevValue: D, newValue: D) => D;
2
+ export declare function lastValueReducer<D>(prevValue: D, newValue: D): D;
3
+ export declare function groupReducer<D>(prevValue: D[], newValue: D[]): D[];
4
+ export declare function throttle<D>(callback: (d: D) => void, delay: number, reducer: ThrottleArgsReducer<D>): (d: D) => void;
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.throttle = exports.groupReducer = exports.lastValueReducer = void 0;
4
+ function lastValueReducer(prevValue, newValue) {
5
+ return newValue;
6
+ }
7
+ exports.lastValueReducer = lastValueReducer;
8
+ function groupReducer(prevValue, newValue) {
9
+ if (!Array.isArray(newValue))
10
+ throw new Error("groupReducer should only be used with topics that return arrays");
11
+ return prevValue ? [...prevValue, ...newValue] : newValue;
12
+ }
13
+ exports.groupReducer = groupReducer;
14
+ function throttle(callback, delay, reducer) {
15
+ let timer;
16
+ let lastExec = 0;
17
+ let reducedArg;
18
+ function wrapper(d) {
19
+ let self = this;
20
+ let elapsed = Date.now() - lastExec;
21
+ function exec() {
22
+ lastExec = Date.now();
23
+ callback.call(self, reducedArg);
24
+ reducedArg = undefined;
25
+ }
26
+ if (timer) {
27
+ clearTimeout(timer);
28
+ }
29
+ reducedArg = reducer(reducedArg, d);
30
+ if (elapsed > delay) {
31
+ exec();
32
+ }
33
+ else {
34
+ timer = setTimeout(exec, delay - elapsed);
35
+ }
36
+ }
37
+ return wrapper;
38
+ }
39
+ exports.throttle = throttle;
40
+ //# sourceMappingURL=throttle.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"throttle.js","sourceRoot":"","sources":["../../src/utils/throttle.ts"],"names":[],"mappings":";;;AAEA,SAAgB,gBAAgB,CAAI,SAAY,EAAE,QAAW;IAC3D,OAAO,QAAQ,CAAA;AACjB,CAAC;AAFD,4CAEC;AAED,SAAgB,YAAY,CAAI,SAAc,EAAE,QAAa;IAC3D,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC;QAC1B,MAAM,IAAI,KAAK,CAAC,iEAAiE,CAAC,CAAA;IAEpF,OAAO,SAAS,CAAC,CAAC,CAAC,CAAC,GAAG,SAAS,EAAE,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAA;AAC3D,CAAC;AALD,oCAKC;AAED,SAAgB,QAAQ,CACtB,QAAwB,EACxB,KAAa,EACb,OAA+B;IAE/B,IAAI,KAAqB,CAAA;IACzB,IAAI,QAAQ,GAAG,CAAC,CAAA;IAEhB,IAAI,UAAe,CAAA;IAEnB,SAAS,OAAO,CAAY,CAAI;QAC9B,IAAI,IAAI,GAAG,IAAI,CAAA;QACf,IAAI,OAAO,GAAG,IAAI,CAAC,GAAG,EAAE,GAAG,QAAQ,CAAA;QAEnC,SAAS,IAAI;YACX,QAAQ,GAAG,IAAI,CAAC,GAAG,EAAE,CAAA;YACrB,QAAQ,CAAC,IAAI,CAAC,IAAI,EAAE,UAAU,CAAC,CAAA;YAC/B,UAAU,GAAG,SAAS,CAAA;QACxB,CAAC;QAED,IAAI,KAAK,EAAE,CAAC;YACV,YAAY,CAAC,KAAK,CAAC,CAAA;QACrB,CAAC;QAED,UAAU,GAAG,OAAO,CAAC,UAAU,EAAE,CAAC,CAAC,CAAA;QAEnC,IAAI,OAAO,GAAG,KAAK,EAAE,CAAC;YACpB,IAAI,EAAE,CAAA;QACR,CAAC;aAAM,CAAC;YACN,KAAK,GAAG,UAAU,CAAC,IAAI,EAAE,KAAK,GAAG,OAAO,CAAC,CAAA;QAC3C,CAAC;IACH,CAAC;IAED,OAAO,OAAO,CAAA;AAChB,CAAC;AAlCD,4BAkCC"}
@@ -0,0 +1 @@
1
+ export type ExtractPromiseResult<Type> = Type extends Promise<infer X> ? X : never;
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/utils/types.ts"],"names":[],"mappings":""}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@push-rpc/next",
3
- "version": "2.0.12",
3
+ "version": "2.0.14",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "scripts": {
@@ -5,8 +5,9 @@ export class HttpClient {
5
5
  constructor(
6
6
  private url: string,
7
7
  private clientId: string,
8
- private getHeaders: () => Promise<Record<string, string>>
9
- ) {}
8
+ private getHeaders: () => Promise<Record<string, string>>,
9
+ ) {
10
+ }
10
11
 
11
12
  async call(itemName: string, params: unknown[], callTimeout: number): Promise<unknown> {
12
13
  return this.httpRequest("POST", itemName, params, callTimeout, await this.getHeaders())
@@ -29,7 +30,7 @@ export class HttpClient {
29
30
  itemName: string,
30
31
  params: unknown[],
31
32
  callTimeout: number,
32
- headers: Record<string, string>
33
+ headers: Record<string, string>,
33
34
  ): Promise<unknown> {
34
35
  const itemUrl = this.getItemUrl(itemName)
35
36
 
@@ -61,7 +62,7 @@ export class HttpClient {
61
62
  contentType && contentType.includes("application/json") ? safeParseJson(text) : text
62
63
 
63
64
  if (response.status < 200 || response.status >= 300) {
64
- const error = new Error(response.statusText)
65
+ const error = new Error(response.headers.get("x-error") ?? undefined)
65
66
 
66
67
  Object.assign(error, {code: response.status})
67
68
 
@@ -10,7 +10,7 @@ export async function serveHttpRequest(
10
10
  res: ServerResponse,
11
11
  path: string,
12
12
  hooks: HttpServerHooks,
13
- createConnectionContext: (req: IncomingMessage) => Promise<RpcConnectionContext>
13
+ createConnectionContext: (req: IncomingMessage) => Promise<RpcConnectionContext>,
14
14
  ) {
15
15
  // if port is in options - response 404 on other URLs
16
16
  // oherwise just handle request
@@ -65,7 +65,10 @@ export async function serveHttpRequest(
65
65
  } catch (e: any) {
66
66
  if (e.code) {
67
67
  res.statusCode = e.code
68
- res.statusMessage = e.message
68
+
69
+ if (e.message) {
70
+ res.setHeader("X-Error", e["message"])
71
+ }
69
72
  const {code, message, stack, ...rest} = e
70
73
  if (Object.keys(rest).length > 0) {
71
74
  res.setHeader("Content-Type", "application/json")
@@ -77,7 +80,9 @@ export async function serveHttpRequest(
77
80
  log.warn(`Error in ${req.url}.`, e)
78
81
 
79
82
  res.statusCode = 500
80
- res.statusMessage = e["message"] || "Internal Server Error"
83
+ if (e["message"]) {
84
+ res.setHeader("X-Error", e["message"])
85
+ }
81
86
  res.end()
82
87
  return
83
88
  }
@@ -107,11 +112,11 @@ export type HttpServerHooks = {
107
112
  subscribe(
108
113
  clientId: RpcConnectionContext,
109
114
  itemName: string,
110
- parameters: unknown[]
115
+ parameters: unknown[],
111
116
  ): Promise<unknown>
112
117
  unsubscribe(
113
118
  clientId: RpcConnectionContext,
114
119
  itemName: string,
115
- parameters: unknown[]
120
+ parameters: unknown[],
116
121
  ): Promise<unknown>
117
122
  }