@neuraiproject/neurai-rpc 0.6.0 → 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -113,3 +113,17 @@ Generated command documentation is in
113
113
 
114
114
  - [GitHub repository](https://github.com/neuraiproject/neurai-rpc)
115
115
  - [NPM package](https://www.npmjs.com/package/@neuraiproject/neurai-rpc)
116
+
117
+ ### Exact numeric values
118
+
119
+ RPC responses are parsed from their original JSON text. Safe numbers retain
120
+ `number` compatibility; numeric tokens that would lose digits (large raw
121
+ integers or high-precision decimals) are returned as strings. Normalize monetary
122
+ fields to bigint/decimal text in your application, never with parseFloat.
123
+
124
+ For request parameters use bigint for exact integer JSON tokens, or
125
+ `rpcNumber('100000000.00000001')` for an exact decimal JSON token. Ordinary
126
+ strings remain quoted strings: use the representation required by the RPC
127
+ method. Unsafe integer numbers, NaN and Infinity are rejected. No global
128
+ BigInt.prototype mutation is performed. `parseRpcJson` and `stringifyRpcJson`
129
+ are exported for callers implementing their own transport.
package/dist/index.cjs CHANGED
@@ -20,9 +20,13 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  var index_exports = {};
21
21
  __export(index_exports, {
22
22
  getRPC: () => getRPC,
23
- methods: () => methods
23
+ methods: () => methods,
24
+ parseRpcJson: () => parseRpcJson,
25
+ rpcNumber: () => rpcNumber,
26
+ stringifyRpcJson: () => stringifyRpcJson
24
27
  });
25
28
  module.exports = __toCommonJS(index_exports);
29
+ var import_lossless_json = require("lossless-json");
26
30
 
27
31
  // docs.ts
28
32
  var methods = {
@@ -5041,6 +5045,25 @@ var methods = {
5041
5045
  };
5042
5046
 
5043
5047
  // index.ts
5048
+ function rpcNumber(value) {
5049
+ return new import_lossless_json.LosslessNumber(value);
5050
+ }
5051
+ function parseRpcJson(text) {
5052
+ return (0, import_lossless_json.parse)(text, void 0, (token) => {
5053
+ const value = Number(token);
5054
+ return (0, import_lossless_json.isSafeNumber)(token) && Math.abs(value) <= Number.MAX_SAFE_INTEGER && (Number.isInteger(value) || Math.abs(value) <= Number.MAX_SAFE_INTEGER / 1e8) ? value : token;
5055
+ });
5056
+ }
5057
+ function stringifyRpcJson(value) {
5058
+ const result = (0, import_lossless_json.stringify)(value, (_key, item) => {
5059
+ if (typeof item === "number" && (!Number.isFinite(item) || Number.isInteger(item) && !Number.isSafeInteger(item) || !Number.isInteger(item) && Math.abs(item) > Number.MAX_SAFE_INTEGER / 1e8)) {
5060
+ throw new Error("Unsafe RPC numeric parameter: use bigint or rpcNumber(decimalText)");
5061
+ }
5062
+ return item;
5063
+ });
5064
+ if (result === void 0) throw new Error("RPC payload is not serializable");
5065
+ return result;
5066
+ }
5044
5067
  function throwSyntaxError() {
5045
5068
  throw new Error("Syntax error, call getRPC with (username, password, URL)");
5046
5069
  }
@@ -5066,7 +5089,7 @@ function getRPC(username, password, URL) {
5066
5089
  const rpcResponse = postData(URL, data, username, password);
5067
5090
  rpcResponse.then(async (response) => {
5068
5091
  if (response.ok) {
5069
- const obj = await response.json();
5092
+ const obj = parseRpcJson(await response.text());
5070
5093
  if (obj && obj.error) {
5071
5094
  rejectionFunc({
5072
5095
  error: obj.error,
@@ -5081,7 +5104,7 @@ function getRPC(username, password, URL) {
5081
5104
  description: null
5082
5105
  };
5083
5106
  try {
5084
- obj = await response.json();
5107
+ obj = parseRpcJson(await response.text());
5085
5108
  } catch (e) {
5086
5109
  }
5087
5110
  const myError = {
@@ -5101,7 +5124,7 @@ function getRPC(username, password, URL) {
5101
5124
  });
5102
5125
  });
5103
5126
  } catch (e) {
5104
- rejectionFunc(e.response);
5127
+ rejectionFunc(e);
5105
5128
  }
5106
5129
  });
5107
5130
  return promise;
@@ -5130,7 +5153,7 @@ async function postData(url = "", data = {}, username, password) {
5130
5153
  // manual, *follow, error
5131
5154
  referrerPolicy: "no-referrer",
5132
5155
  // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
5133
- body: JSON.stringify(data)
5156
+ body: stringifyRpcJson(data)
5134
5157
  // body data type must match "Content-Type" header
5135
5158
  });
5136
5159
  return response;
@@ -5138,5 +5161,8 @@ async function postData(url = "", data = {}, username, password) {
5138
5161
  // Annotate the CommonJS export names for ESM import in node:
5139
5162
  0 && (module.exports = {
5140
5163
  getRPC,
5141
- methods
5164
+ methods,
5165
+ parseRpcJson,
5166
+ rpcNumber,
5167
+ stringifyRpcJson
5142
5168
  });
package/dist/index.d.cts CHANGED
@@ -1,3 +1,5 @@
1
+ import { LosslessNumber } from 'lossless-json';
2
+
1
3
  interface IMethods {
2
4
  abandontransaction: string;
3
5
  abortrescan: string;
@@ -199,6 +201,11 @@ interface IMethods {
199
201
  }
200
202
  declare const methods: IMethods;
201
203
 
204
+ /** Exact decimal parameter, emitted as a JSON number rather than a quoted string. */
205
+ declare function rpcNumber(value: string): LosslessNumber;
206
+ /** Preserve unsafe JSON numeric tokens as strings; retain safe number compatibility. */
207
+ declare function parseRpcJson(text: string): any;
208
+ declare function stringifyRpcJson(value: unknown): string;
202
209
  declare function getRPC(username: string, password: string, URL: string): (method: string, params: any[]) => Promise<unknown>;
203
210
 
204
- export { getRPC, methods };
211
+ export { getRPC, methods, parseRpcJson, rpcNumber, stringifyRpcJson };
package/dist/index.d.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import { LosslessNumber } from 'lossless-json';
2
+
1
3
  interface IMethods {
2
4
  abandontransaction: string;
3
5
  abortrescan: string;
@@ -199,6 +201,11 @@ interface IMethods {
199
201
  }
200
202
  declare const methods: IMethods;
201
203
 
204
+ /** Exact decimal parameter, emitted as a JSON number rather than a quoted string. */
205
+ declare function rpcNumber(value: string): LosslessNumber;
206
+ /** Preserve unsafe JSON numeric tokens as strings; retain safe number compatibility. */
207
+ declare function parseRpcJson(text: string): any;
208
+ declare function stringifyRpcJson(value: unknown): string;
202
209
  declare function getRPC(username: string, password: string, URL: string): (method: string, params: any[]) => Promise<unknown>;
203
210
 
204
- export { getRPC, methods };
211
+ export { getRPC, methods, parseRpcJson, rpcNumber, stringifyRpcJson };
package/dist/index.mjs CHANGED
@@ -1,3 +1,6 @@
1
+ // index.ts
2
+ import { parse, stringify, isSafeNumber, LosslessNumber } from "lossless-json";
3
+
1
4
  // docs.ts
2
5
  var methods = {
3
6
  /** abandontransaction "txid"
@@ -5015,6 +5018,25 @@ var methods = {
5015
5018
  };
5016
5019
 
5017
5020
  // index.ts
5021
+ function rpcNumber(value) {
5022
+ return new LosslessNumber(value);
5023
+ }
5024
+ function parseRpcJson(text) {
5025
+ return parse(text, void 0, (token) => {
5026
+ const value = Number(token);
5027
+ return isSafeNumber(token) && Math.abs(value) <= Number.MAX_SAFE_INTEGER && (Number.isInteger(value) || Math.abs(value) <= Number.MAX_SAFE_INTEGER / 1e8) ? value : token;
5028
+ });
5029
+ }
5030
+ function stringifyRpcJson(value) {
5031
+ const result = stringify(value, (_key, item) => {
5032
+ if (typeof item === "number" && (!Number.isFinite(item) || Number.isInteger(item) && !Number.isSafeInteger(item) || !Number.isInteger(item) && Math.abs(item) > Number.MAX_SAFE_INTEGER / 1e8)) {
5033
+ throw new Error("Unsafe RPC numeric parameter: use bigint or rpcNumber(decimalText)");
5034
+ }
5035
+ return item;
5036
+ });
5037
+ if (result === void 0) throw new Error("RPC payload is not serializable");
5038
+ return result;
5039
+ }
5018
5040
  function throwSyntaxError() {
5019
5041
  throw new Error("Syntax error, call getRPC with (username, password, URL)");
5020
5042
  }
@@ -5040,7 +5062,7 @@ function getRPC(username, password, URL) {
5040
5062
  const rpcResponse = postData(URL, data, username, password);
5041
5063
  rpcResponse.then(async (response) => {
5042
5064
  if (response.ok) {
5043
- const obj = await response.json();
5065
+ const obj = parseRpcJson(await response.text());
5044
5066
  if (obj && obj.error) {
5045
5067
  rejectionFunc({
5046
5068
  error: obj.error,
@@ -5055,7 +5077,7 @@ function getRPC(username, password, URL) {
5055
5077
  description: null
5056
5078
  };
5057
5079
  try {
5058
- obj = await response.json();
5080
+ obj = parseRpcJson(await response.text());
5059
5081
  } catch (e) {
5060
5082
  }
5061
5083
  const myError = {
@@ -5075,7 +5097,7 @@ function getRPC(username, password, URL) {
5075
5097
  });
5076
5098
  });
5077
5099
  } catch (e) {
5078
- rejectionFunc(e.response);
5100
+ rejectionFunc(e);
5079
5101
  }
5080
5102
  });
5081
5103
  return promise;
@@ -5104,12 +5126,15 @@ async function postData(url = "", data = {}, username, password) {
5104
5126
  // manual, *follow, error
5105
5127
  referrerPolicy: "no-referrer",
5106
5128
  // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
5107
- body: JSON.stringify(data)
5129
+ body: stringifyRpcJson(data)
5108
5130
  // body data type must match "Content-Type" header
5109
5131
  });
5110
5132
  return response;
5111
5133
  }
5112
5134
  export {
5113
5135
  getRPC,
5114
- methods
5136
+ methods,
5137
+ parseRpcJson,
5138
+ rpcNumber,
5139
+ stringifyRpcJson
5115
5140
  };
package/index.ts CHANGED
@@ -1,7 +1,36 @@
1
+ import { parse, stringify, isSafeNumber, LosslessNumber } from 'lossless-json';
1
2
  import { methods } from "./docs";
2
3
 
3
4
  export { methods };
4
5
 
6
+ /** Exact decimal parameter, emitted as a JSON number rather than a quoted string. */
7
+ export function rpcNumber(value: string): LosslessNumber {
8
+ return new LosslessNumber(value);
9
+ }
10
+
11
+ /** Preserve unsafe JSON numeric tokens as strings; retain safe number compatibility. */
12
+ export function parseRpcJson(text: string): any {
13
+ return parse(text, undefined, (token: string) => {
14
+ const value = Number(token);
15
+ return isSafeNumber(token) && Math.abs(value) <= Number.MAX_SAFE_INTEGER
16
+ && (Number.isInteger(value) || Math.abs(value) <= Number.MAX_SAFE_INTEGER / 1e8)
17
+ ? value : token;
18
+ });
19
+ }
20
+
21
+ export function stringifyRpcJson(value: unknown): string {
22
+ const result = stringify(value, (_key, item) => {
23
+ if (typeof item === 'number' && (!Number.isFinite(item) ||
24
+ (Number.isInteger(item) && !Number.isSafeInteger(item)) ||
25
+ (!Number.isInteger(item) && Math.abs(item) > Number.MAX_SAFE_INTEGER / 1e8))) {
26
+ throw new Error('Unsafe RPC numeric parameter: use bigint or rpcNumber(decimalText)');
27
+ }
28
+ return item;
29
+ });
30
+ if (result === undefined) throw new Error('RPC payload is not serializable');
31
+ return result;
32
+ }
33
+
5
34
  // DePIN protocol 2 uses this same authenticated HTTP JSON-RPC transport. The
6
35
  // former raw-TCP gateway and its dedicated port were removed from the node.
7
36
 
@@ -41,7 +70,7 @@ export function getRPC(username: string, password: string, URL: string) {
41
70
  */
42
71
 
43
72
  if (response.ok) {
44
- const obj = await response.json(); //Convert to JSON
73
+ const obj = parseRpcJson(await response.text()); //Convert to JSON
45
74
  // A JSON-RPC error can arrive with HTTP 200 — it must reject,
46
75
  // not resolve undefined.
47
76
  if (obj && obj.error) {
@@ -59,7 +88,7 @@ export function getRPC(username: string, password: string, URL: string) {
59
88
  description: null,
60
89
  };
61
90
  try {
62
- obj = await response.json();
91
+ obj = parseRpcJson(await response.text());
63
92
  } catch (e) {}
64
93
  const myError = {
65
94
  statusText: response.statusText,
@@ -81,7 +110,7 @@ export function getRPC(username: string, password: string, URL: string) {
81
110
  });
82
111
  });
83
112
  } catch (e) {
84
- rejectionFunc(e.response);
113
+ rejectionFunc(e);
85
114
  }
86
115
  });
87
116
  return promise;
@@ -121,7 +150,7 @@ async function postData(
121
150
  },
122
151
  redirect: "follow", // manual, *follow, error
123
152
  referrerPolicy: "no-referrer", // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
124
- body: JSON.stringify(data), // body data type must match "Content-Type" header
153
+ body: stringifyRpcJson(data), // body data type must match "Content-Type" header
125
154
  });
126
155
  return response;
127
156
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@neuraiproject/neurai-rpc",
3
- "version": "0.6.0",
3
+ "version": "0.6.1",
4
4
  "description": "Helps you access your Neurai full node using RPC from JavaScript",
5
5
  "types": "dist/index.d.ts",
6
6
  "type": "module",
@@ -52,5 +52,8 @@
52
52
  },
53
53
  "overrides": {
54
54
  "esbuild": "^0.28.1"
55
+ },
56
+ "dependencies": {
57
+ "lossless-json": "^4.3.0"
55
58
  }
56
59
  }