Skip to content

Commit 44f984d

Browse files
committed
lnc: port request/response hacks from TW
1 parent 8c0bcef commit 44f984d

File tree

2 files changed

+76
-7
lines changed

2 files changed

+76
-7
lines changed

lib/lnc.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { CredentialStore, LncConfig, WasmGlobal } from './types/lnc';
99
import LncCredentialStore from './util/credentialStore';
1010
import { wasmLog as log } from './util/log';
1111
import { camelKeysToSnake, isObject, snakeKeysToCamel } from './util/objects';
12+
import { JS_RESERVED_WORDS } from './util/reservedWords';
1213

1314
/** The default values for the LncConfig options */
1415
const DEFAULT_CONFIG = {
@@ -292,9 +293,7 @@ export default class LNC {
292293
mapKeys: string[] = [
293294
'leaseDurationBuckets', // poolrpc.Trader.LeaseDurations
294295
'leaseDurations', // poolrpc.Trader.LeaseDurations
295-
'matchedMarkets', // poolrpc.Trader.BatchSnapshot
296-
'matchedOrders', // poolrpc.Trader.BatchSnapshot
297-
'routes' // lnrpc.Lightning.QueryRoutes
296+
'matchedMarkets' // poolrpc.Trader.BatchSnapshot
298297
];
299298

300299
/**
@@ -305,22 +304,28 @@ export default class LNC {
305304
hackListsAndMaps(response: any) {
306305
const o: Record<string, any> = {};
307306
return Object.entries<any>(response).reduce((updated, [key, value]) => {
308-
if (Array.isArray(value)) {
309-
updated[`${key}List`] = value;
310-
} else if (this.mapKeys.includes(key)) {
307+
if (this.mapKeys.includes(key)) {
311308
// convert Maps from an object to an array of arrays
312309
// leaseDurationBuckets: { 2016: "MARKET_OPEN", 4032: "MARKET_OPEN" }
313310
// leaseDurationBucketsMap: [ [2016, 3], [4032, 3] ]
314311
updated[`${key}Map`] = Object.entries<any>(value).reduce(
315312
(all, [k, v]) => {
313+
const j = isNaN(parseInt(k, 10)) ? k : parseInt(k, 10);
316314
all.push([
317-
k,
315+
j,
318316
isObject(v) ? this.hackListsAndMaps(v) : v
319317
]);
320318
return all;
321319
},
322320
[] as any[]
323321
);
322+
} else if (Array.isArray(value)) {
323+
updated[`${key}List`] = (value as any[]).map((v) =>
324+
isObject(v) ? this.hackListsAndMaps(v) : v
325+
);
326+
} else if (JS_RESERVED_WORDS.includes(key)) {
327+
// add the 'pb_' prefix for keys that are a JS reserved word
328+
updated[`pb_${key}`] = value;
324329
} else if (isObject(value)) {
325330
// recurse into nested objects
326331
updated[key] = this.hackListsAndMaps(value);

lib/util/reservedWords.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* The list of JS reserved words that are prefixed with "pb_" by the protobuf compiler
3+
* Source: https://github.com/protocolbuffers/protobuf/blob/4.0.x/src/google/protobuf/compiler/js/js_generator.cc#L60
4+
*/
5+
export const JS_RESERVED_WORDS: string[] = [
6+
'abstract',
7+
'boolean',
8+
'break',
9+
'byte',
10+
'case',
11+
'catch',
12+
'char',
13+
'class',
14+
'const',
15+
'continue',
16+
'debugger',
17+
'default',
18+
'delete',
19+
'do',
20+
'double',
21+
'else',
22+
'enum',
23+
'export',
24+
'extends',
25+
'false',
26+
'final',
27+
'finally',
28+
'float',
29+
'for',
30+
'function',
31+
'goto',
32+
'if',
33+
'implements',
34+
'import',
35+
'in',
36+
'instanceof',
37+
'int',
38+
'interface',
39+
'long',
40+
'native',
41+
'new',
42+
'null',
43+
'package',
44+
'private',
45+
'protected',
46+
'public',
47+
'return',
48+
'short',
49+
'static',
50+
'super',
51+
'switch',
52+
'synchronized',
53+
'this',
54+
'throw',
55+
'throws',
56+
'transient',
57+
'try',
58+
'typeof',
59+
'var',
60+
'void',
61+
'volatile',
62+
'while',
63+
'with'
64+
];

0 commit comments

Comments
 (0)