@lightsparkdev/core 1.0.1 → 1.0.3
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/CHANGELOG.md +12 -0
- package/dist/index.cjs +40 -5
- package/dist/index.d.ts +10 -1
- package/dist/index.js +39 -5
- package/package.json +2 -2
- package/src/Logger.ts +38 -0
- package/src/index.ts +1 -0
- package/src/requester/Requester.ts +8 -4
- package/src/utils/createHash.ts +2 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# @lightsparkdev/core
|
|
2
2
|
|
|
3
|
+
## 1.0.3
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- e451948: Use AsyncStorage in wallet-sdk to check for SDK logging enabled, for ReactNative support
|
|
8
|
+
|
|
9
|
+
## 1.0.2
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- 0e8767b: Add Logger for browser console and some debugging logs
|
|
14
|
+
|
|
3
15
|
## 1.0.1
|
|
4
16
|
|
|
5
17
|
### Patch Changes
|
package/dist/index.cjs
CHANGED
|
@@ -35,6 +35,7 @@ __export(src_exports, {
|
|
|
35
35
|
LightsparkAuthException: () => LightsparkAuthException_default,
|
|
36
36
|
LightsparkException: () => LightsparkException_default,
|
|
37
37
|
LightsparkSigningException: () => LightsparkSigningException_default,
|
|
38
|
+
Logger: () => Logger,
|
|
38
39
|
NodeKeyCache: () => NodeKeyCache_default,
|
|
39
40
|
RSASigningKey: () => RSASigningKey,
|
|
40
41
|
Requester: () => Requester_default,
|
|
@@ -456,6 +457,35 @@ var NodeKeyCache = class {
|
|
|
456
457
|
};
|
|
457
458
|
var NodeKeyCache_default = NodeKeyCache;
|
|
458
459
|
|
|
460
|
+
// src/Logger.ts
|
|
461
|
+
var Logger = class {
|
|
462
|
+
context;
|
|
463
|
+
loggingEnabled = false;
|
|
464
|
+
constructor(loggerContext, getLoggingEnabled) {
|
|
465
|
+
this.context = loggerContext;
|
|
466
|
+
this.updateLoggingEnabled(getLoggingEnabled);
|
|
467
|
+
}
|
|
468
|
+
async updateLoggingEnabled(getLoggingEnabled) {
|
|
469
|
+
if (getLoggingEnabled) {
|
|
470
|
+
this.loggingEnabled = await getLoggingEnabled();
|
|
471
|
+
} else if (isBrowser) {
|
|
472
|
+
try {
|
|
473
|
+
this.loggingEnabled = localStorage.getItem("lightspark-logging-enabled") === "1";
|
|
474
|
+
} catch (e) {
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
if (this.loggingEnabled) {
|
|
478
|
+
console.log(`[${this.context}] Logging enabled`);
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
info(message, ...rest) {
|
|
482
|
+
if (this.loggingEnabled) {
|
|
483
|
+
console.log(`[${this.context}] ${message}`, ...rest);
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
};
|
|
487
|
+
var logger = new Logger("@lightsparkdev/core");
|
|
488
|
+
|
|
459
489
|
// src/requester/Requester.ts
|
|
460
490
|
var import_auto_bind2 = __toESM(require("auto-bind"), 1);
|
|
461
491
|
var import_dayjs = __toESM(require("dayjs"), 1);
|
|
@@ -507,6 +537,7 @@ var Requester = class {
|
|
|
507
537
|
return query.constructObject(data);
|
|
508
538
|
}
|
|
509
539
|
subscribe(queryPayload, variables = {}) {
|
|
540
|
+
logger.info(`Requester.subscribe params`, queryPayload, variables);
|
|
510
541
|
const operationNameRegex = /^\s*(query|mutation|subscription)\s+(\w+)/i;
|
|
511
542
|
const operationMatch = queryPayload.match(operationNameRegex);
|
|
512
543
|
if (!operationMatch || operationMatch.length < 3) {
|
|
@@ -530,13 +561,15 @@ var Requester = class {
|
|
|
530
561
|
variables,
|
|
531
562
|
operationName: operation
|
|
532
563
|
};
|
|
533
|
-
|
|
534
|
-
|
|
564
|
+
logger.info(`Requester.subscribe bodyData`, bodyData);
|
|
565
|
+
return new import_zen_observable_ts.Observable((observer) => {
|
|
566
|
+
logger.info(`Requester.subscribe observer`, observer);
|
|
567
|
+
return this.wsClient.subscribe(bodyData, {
|
|
535
568
|
next: (data) => observer.next(data),
|
|
536
569
|
error: (err) => observer.error(err),
|
|
537
570
|
complete: () => observer.complete()
|
|
538
|
-
})
|
|
539
|
-
);
|
|
571
|
+
});
|
|
572
|
+
});
|
|
540
573
|
}
|
|
541
574
|
async makeRawRequest(queryPayload, variables = {}, signingNodeId = void 0, skipAuth = false) {
|
|
542
575
|
const operationNameRegex = /^\s*(query|mutation|subscription)\s+(\w+)/i;
|
|
@@ -671,7 +704,8 @@ var createSha256Hash = async (data) => {
|
|
|
671
704
|
return new Uint8Array(await window.crypto.subtle.digest("SHA-256", data));
|
|
672
705
|
} else {
|
|
673
706
|
const { createHash } = await import("crypto");
|
|
674
|
-
|
|
707
|
+
const buffer = createHash("sha256").update(data).digest();
|
|
708
|
+
return new Uint8Array(buffer);
|
|
675
709
|
}
|
|
676
710
|
};
|
|
677
711
|
|
|
@@ -791,6 +825,7 @@ var isType = (typename) => (node) => {
|
|
|
791
825
|
LightsparkAuthException,
|
|
792
826
|
LightsparkException,
|
|
793
827
|
LightsparkSigningException,
|
|
828
|
+
Logger,
|
|
794
829
|
NodeKeyCache,
|
|
795
830
|
RSASigningKey,
|
|
796
831
|
Requester,
|
package/dist/index.d.ts
CHANGED
|
@@ -101,6 +101,15 @@ declare class NodeKeyCache {
|
|
|
101
101
|
private stripPemTags;
|
|
102
102
|
}
|
|
103
103
|
|
|
104
|
+
type GetLoggingEnabled = (() => Promise<boolean> | boolean) | undefined;
|
|
105
|
+
declare class Logger {
|
|
106
|
+
context: string;
|
|
107
|
+
loggingEnabled: boolean;
|
|
108
|
+
constructor(loggerContext: string, getLoggingEnabled?: GetLoggingEnabled);
|
|
109
|
+
updateLoggingEnabled(getLoggingEnabled: GetLoggingEnabled): Promise<void>;
|
|
110
|
+
info(message: string, ...rest: unknown[]): void;
|
|
111
|
+
}
|
|
112
|
+
|
|
104
113
|
type Query<T> = {
|
|
105
114
|
/** The string representation of the query payload for graphQL. **/
|
|
106
115
|
queryPayload: string;
|
|
@@ -221,4 +230,4 @@ declare const isType: <T extends string>(typename: T) => <N extends {
|
|
|
221
230
|
__typename: T;
|
|
222
231
|
}>;
|
|
223
232
|
|
|
224
|
-
export { AuthProvider, ById, CryptoInterface, DefaultCrypto, ExpandRecursively, GeneratedKeyPair, KeyOrAlias, KeyOrAliasType, LightsparkAuthException, LightsparkException, LightsparkSigningException, Maybe, NodeKeyCache, OmitTypename, Query, RSASigningKey, Requester, Secp256k1SigningKey, ServerEnvironment, SigningKey, SigningKeyType, StubAuthProvider, apiDomainForEnvironment, b64decode, b64encode, bytesToHex, convertCurrencyAmount, createSha256Hash, getErrorMsg, hexToBytes, isBrowser, isError, isErrorMsg, isErrorWithMessage, isNode, isType, urlsafe_b64decode };
|
|
233
|
+
export { AuthProvider, ById, CryptoInterface, DefaultCrypto, ExpandRecursively, GeneratedKeyPair, KeyOrAlias, KeyOrAliasType, LightsparkAuthException, LightsparkException, LightsparkSigningException, Logger, Maybe, NodeKeyCache, OmitTypename, Query, RSASigningKey, Requester, Secp256k1SigningKey, ServerEnvironment, SigningKey, SigningKeyType, StubAuthProvider, apiDomainForEnvironment, b64decode, b64encode, bytesToHex, convertCurrencyAmount, createSha256Hash, getErrorMsg, hexToBytes, isBrowser, isError, isErrorMsg, isErrorWithMessage, isNode, isType, urlsafe_b64decode };
|
package/dist/index.js
CHANGED
|
@@ -393,6 +393,35 @@ var NodeKeyCache = class {
|
|
|
393
393
|
};
|
|
394
394
|
var NodeKeyCache_default = NodeKeyCache;
|
|
395
395
|
|
|
396
|
+
// src/Logger.ts
|
|
397
|
+
var Logger = class {
|
|
398
|
+
context;
|
|
399
|
+
loggingEnabled = false;
|
|
400
|
+
constructor(loggerContext, getLoggingEnabled) {
|
|
401
|
+
this.context = loggerContext;
|
|
402
|
+
this.updateLoggingEnabled(getLoggingEnabled);
|
|
403
|
+
}
|
|
404
|
+
async updateLoggingEnabled(getLoggingEnabled) {
|
|
405
|
+
if (getLoggingEnabled) {
|
|
406
|
+
this.loggingEnabled = await getLoggingEnabled();
|
|
407
|
+
} else if (isBrowser) {
|
|
408
|
+
try {
|
|
409
|
+
this.loggingEnabled = localStorage.getItem("lightspark-logging-enabled") === "1";
|
|
410
|
+
} catch (e) {
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
if (this.loggingEnabled) {
|
|
414
|
+
console.log(`[${this.context}] Logging enabled`);
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
info(message, ...rest) {
|
|
418
|
+
if (this.loggingEnabled) {
|
|
419
|
+
console.log(`[${this.context}] ${message}`, ...rest);
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
};
|
|
423
|
+
var logger = new Logger("@lightsparkdev/core");
|
|
424
|
+
|
|
396
425
|
// src/requester/Requester.ts
|
|
397
426
|
import autoBind2 from "auto-bind";
|
|
398
427
|
import dayjs from "dayjs";
|
|
@@ -444,6 +473,7 @@ var Requester = class {
|
|
|
444
473
|
return query.constructObject(data);
|
|
445
474
|
}
|
|
446
475
|
subscribe(queryPayload, variables = {}) {
|
|
476
|
+
logger.info(`Requester.subscribe params`, queryPayload, variables);
|
|
447
477
|
const operationNameRegex = /^\s*(query|mutation|subscription)\s+(\w+)/i;
|
|
448
478
|
const operationMatch = queryPayload.match(operationNameRegex);
|
|
449
479
|
if (!operationMatch || operationMatch.length < 3) {
|
|
@@ -467,13 +497,15 @@ var Requester = class {
|
|
|
467
497
|
variables,
|
|
468
498
|
operationName: operation
|
|
469
499
|
};
|
|
470
|
-
|
|
471
|
-
|
|
500
|
+
logger.info(`Requester.subscribe bodyData`, bodyData);
|
|
501
|
+
return new Observable((observer) => {
|
|
502
|
+
logger.info(`Requester.subscribe observer`, observer);
|
|
503
|
+
return this.wsClient.subscribe(bodyData, {
|
|
472
504
|
next: (data) => observer.next(data),
|
|
473
505
|
error: (err) => observer.error(err),
|
|
474
506
|
complete: () => observer.complete()
|
|
475
|
-
})
|
|
476
|
-
);
|
|
507
|
+
});
|
|
508
|
+
});
|
|
477
509
|
}
|
|
478
510
|
async makeRawRequest(queryPayload, variables = {}, signingNodeId = void 0, skipAuth = false) {
|
|
479
511
|
const operationNameRegex = /^\s*(query|mutation|subscription)\s+(\w+)/i;
|
|
@@ -608,7 +640,8 @@ var createSha256Hash = async (data) => {
|
|
|
608
640
|
return new Uint8Array(await window.crypto.subtle.digest("SHA-256", data));
|
|
609
641
|
} else {
|
|
610
642
|
const { createHash } = await import("crypto");
|
|
611
|
-
|
|
643
|
+
const buffer = createHash("sha256").update(data).digest();
|
|
644
|
+
return new Uint8Array(buffer);
|
|
612
645
|
}
|
|
613
646
|
};
|
|
614
647
|
|
|
@@ -727,6 +760,7 @@ export {
|
|
|
727
760
|
LightsparkAuthException_default as LightsparkAuthException,
|
|
728
761
|
LightsparkException_default as LightsparkException,
|
|
729
762
|
LightsparkSigningException_default as LightsparkSigningException,
|
|
763
|
+
Logger,
|
|
730
764
|
NodeKeyCache_default as NodeKeyCache,
|
|
731
765
|
RSASigningKey,
|
|
732
766
|
Requester_default as Requester,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lightsparkdev/core",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"description": "Lightspark JS SDK",
|
|
5
5
|
"author": "Lightspark Inc.",
|
|
6
6
|
"keywords": [
|
|
@@ -47,7 +47,7 @@
|
|
|
47
47
|
"CHANGELOG.md"
|
|
48
48
|
],
|
|
49
49
|
"scripts": {
|
|
50
|
-
"build": "tsup src/index.ts --format cjs,esm --dts",
|
|
50
|
+
"build": "yarn tsc && tsup src/index.ts --format cjs,esm --dts",
|
|
51
51
|
"clean": "rm -rf .turbo && rm -rf dist",
|
|
52
52
|
"dev": "yarn build -- --watch",
|
|
53
53
|
"format:fix": "prettier src --write",
|
package/src/Logger.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { isBrowser } from "./index.js";
|
|
2
|
+
|
|
3
|
+
type GetLoggingEnabled = (() => Promise<boolean> | boolean) | undefined;
|
|
4
|
+
|
|
5
|
+
export class Logger {
|
|
6
|
+
context: string;
|
|
7
|
+
loggingEnabled = false;
|
|
8
|
+
|
|
9
|
+
constructor(loggerContext: string, getLoggingEnabled?: GetLoggingEnabled) {
|
|
10
|
+
this.context = loggerContext;
|
|
11
|
+
this.updateLoggingEnabled(getLoggingEnabled);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
async updateLoggingEnabled(getLoggingEnabled: GetLoggingEnabled) {
|
|
15
|
+
if (getLoggingEnabled) {
|
|
16
|
+
this.loggingEnabled = await getLoggingEnabled();
|
|
17
|
+
} else if (isBrowser) {
|
|
18
|
+
try {
|
|
19
|
+
this.loggingEnabled =
|
|
20
|
+
localStorage.getItem("lightspark-logging-enabled") === "1";
|
|
21
|
+
} catch (e) {
|
|
22
|
+
/* ignore */
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (this.loggingEnabled) {
|
|
27
|
+
console.log(`[${this.context}] Logging enabled`);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
info(message: string, ...rest: unknown[]) {
|
|
32
|
+
if (this.loggingEnabled) {
|
|
33
|
+
console.log(`[${this.context}] ${message}`, ...rest);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export const logger = new Logger("@lightsparkdev/core");
|
package/src/index.ts
CHANGED
|
@@ -16,6 +16,7 @@ import type { CryptoInterface } from "../crypto/crypto.js";
|
|
|
16
16
|
import { DefaultCrypto, LightsparkSigningException } from "../crypto/crypto.js";
|
|
17
17
|
import type NodeKeyCache from "../crypto/NodeKeyCache.js";
|
|
18
18
|
import LightsparkException from "../LightsparkException.js";
|
|
19
|
+
import { logger } from "../Logger.js";
|
|
19
20
|
import { b64encode } from "../utils/base64.js";
|
|
20
21
|
import { isNode } from "../utils/environment.js";
|
|
21
22
|
|
|
@@ -67,6 +68,7 @@ class Requester {
|
|
|
67
68
|
queryPayload: string,
|
|
68
69
|
variables: { [key: string]: unknown } = {},
|
|
69
70
|
) {
|
|
71
|
+
logger.info(`Requester.subscribe params`, queryPayload, variables);
|
|
70
72
|
const operationNameRegex = /^\s*(query|mutation|subscription)\s+(\w+)/i;
|
|
71
73
|
const operationMatch = queryPayload.match(operationNameRegex);
|
|
72
74
|
if (!operationMatch || operationMatch.length < 3) {
|
|
@@ -92,13 +94,15 @@ class Requester {
|
|
|
92
94
|
operationName: operation,
|
|
93
95
|
};
|
|
94
96
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
+
logger.info(`Requester.subscribe bodyData`, bodyData);
|
|
98
|
+
return new Observable<{ data: T }>((observer) => {
|
|
99
|
+
logger.info(`Requester.subscribe observer`, observer);
|
|
100
|
+
return this.wsClient.subscribe(bodyData, {
|
|
97
101
|
next: (data) => observer.next(data as { data: T }),
|
|
98
102
|
error: (err) => observer.error(err),
|
|
99
103
|
complete: () => observer.complete(),
|
|
100
|
-
})
|
|
101
|
-
);
|
|
104
|
+
});
|
|
105
|
+
});
|
|
102
106
|
}
|
|
103
107
|
|
|
104
108
|
public async makeRawRequest(
|
package/src/utils/createHash.ts
CHANGED
|
@@ -7,6 +7,7 @@ export const createSha256Hash = async (
|
|
|
7
7
|
return new Uint8Array(await window.crypto.subtle.digest("SHA-256", data));
|
|
8
8
|
} else {
|
|
9
9
|
const { createHash } = await import("crypto");
|
|
10
|
-
|
|
10
|
+
const buffer = createHash("sha256").update(data).digest();
|
|
11
|
+
return new Uint8Array(buffer);
|
|
11
12
|
}
|
|
12
13
|
};
|