@lightsparkdev/core 1.0.0 → 1.0.2
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 +64 -5
- package/dist/index.d.ts +16 -1
- package/dist/index.js +59 -5
- package/package.json +1 -1
- package/src/Logger.ts +30 -0
- package/src/index.ts +1 -0
- package/src/requester/Requester.ts +8 -4
- package/src/utils/createHash.ts +2 -1
- package/src/utils/errors.ts +36 -0
- package/src/utils/index.ts +1 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# @lightsparkdev/core
|
|
2
2
|
|
|
3
|
+
## 1.0.2
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 0e8767b: Add Logger for browser console and some debugging logs
|
|
8
|
+
|
|
9
|
+
## 1.0.1
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- 808c77a: Consolidate some imports to lightspark-sdk and core
|
|
14
|
+
|
|
3
15
|
## 1.0.0
|
|
4
16
|
|
|
5
17
|
### Major 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,
|
|
@@ -49,8 +50,12 @@ __export(src_exports, {
|
|
|
49
50
|
bytesToHex: () => bytesToHex,
|
|
50
51
|
convertCurrencyAmount: () => convertCurrencyAmount,
|
|
51
52
|
createSha256Hash: () => createSha256Hash,
|
|
53
|
+
getErrorMsg: () => getErrorMsg,
|
|
52
54
|
hexToBytes: () => hexToBytes,
|
|
53
55
|
isBrowser: () => isBrowser,
|
|
56
|
+
isError: () => isError,
|
|
57
|
+
isErrorMsg: () => isErrorMsg,
|
|
58
|
+
isErrorWithMessage: () => isErrorWithMessage,
|
|
54
59
|
isNode: () => isNode,
|
|
55
60
|
isType: () => isType,
|
|
56
61
|
urlsafe_b64decode: () => urlsafe_b64decode
|
|
@@ -452,6 +457,30 @@ var NodeKeyCache = class {
|
|
|
452
457
|
};
|
|
453
458
|
var NodeKeyCache_default = NodeKeyCache;
|
|
454
459
|
|
|
460
|
+
// src/Logger.ts
|
|
461
|
+
var Logger = class {
|
|
462
|
+
context;
|
|
463
|
+
loggingEnabled = false;
|
|
464
|
+
constructor(loggerContext) {
|
|
465
|
+
this.context = loggerContext;
|
|
466
|
+
if (isBrowser) {
|
|
467
|
+
try {
|
|
468
|
+
this.loggingEnabled = localStorage.getItem("lightspark-logging-enabled") === "1";
|
|
469
|
+
} catch (e) {
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
if (this.loggingEnabled) {
|
|
473
|
+
console.log(`[${this.context}] Logging enabled`);
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
info(message, ...rest) {
|
|
477
|
+
if (this.loggingEnabled) {
|
|
478
|
+
console.log(`[${this.context}] ${message}`, ...rest);
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
};
|
|
482
|
+
var logger = new Logger("@lightsparkdev/core");
|
|
483
|
+
|
|
455
484
|
// src/requester/Requester.ts
|
|
456
485
|
var import_auto_bind2 = __toESM(require("auto-bind"), 1);
|
|
457
486
|
var import_dayjs = __toESM(require("dayjs"), 1);
|
|
@@ -503,6 +532,7 @@ var Requester = class {
|
|
|
503
532
|
return query.constructObject(data);
|
|
504
533
|
}
|
|
505
534
|
subscribe(queryPayload, variables = {}) {
|
|
535
|
+
logger.info(`Requester.subscribe params`, queryPayload, variables);
|
|
506
536
|
const operationNameRegex = /^\s*(query|mutation|subscription)\s+(\w+)/i;
|
|
507
537
|
const operationMatch = queryPayload.match(operationNameRegex);
|
|
508
538
|
if (!operationMatch || operationMatch.length < 3) {
|
|
@@ -526,13 +556,15 @@ var Requester = class {
|
|
|
526
556
|
variables,
|
|
527
557
|
operationName: operation
|
|
528
558
|
};
|
|
529
|
-
|
|
530
|
-
|
|
559
|
+
logger.info(`Requester.subscribe bodyData`, bodyData);
|
|
560
|
+
return new import_zen_observable_ts.Observable((observer) => {
|
|
561
|
+
logger.info(`Requester.subscribe observer`, observer);
|
|
562
|
+
return this.wsClient.subscribe(bodyData, {
|
|
531
563
|
next: (data) => observer.next(data),
|
|
532
564
|
error: (err) => observer.error(err),
|
|
533
565
|
complete: () => observer.complete()
|
|
534
|
-
})
|
|
535
|
-
);
|
|
566
|
+
});
|
|
567
|
+
});
|
|
536
568
|
}
|
|
537
569
|
async makeRawRequest(queryPayload, variables = {}, signingNodeId = void 0, skipAuth = false) {
|
|
538
570
|
const operationNameRegex = /^\s*(query|mutation|subscription)\s+(\w+)/i;
|
|
@@ -667,7 +699,8 @@ var createSha256Hash = async (data) => {
|
|
|
667
699
|
return new Uint8Array(await window.crypto.subtle.digest("SHA-256", data));
|
|
668
700
|
} else {
|
|
669
701
|
const { createHash } = await import("crypto");
|
|
670
|
-
|
|
702
|
+
const buffer = createHash("sha256").update(data).digest();
|
|
703
|
+
return new Uint8Array(buffer);
|
|
671
704
|
}
|
|
672
705
|
};
|
|
673
706
|
|
|
@@ -741,6 +774,27 @@ var convertCurrencyAmount = (from, toUnit) => {
|
|
|
741
774
|
};
|
|
742
775
|
};
|
|
743
776
|
|
|
777
|
+
// src/utils/errors.ts
|
|
778
|
+
var isError = (e) => {
|
|
779
|
+
return Boolean(
|
|
780
|
+
typeof e === "object" && e !== null && "name" in e && typeof e.name === "string" && "message" in e && typeof e.message === "string" && "stack" in e && (!e.stack || typeof e.stack === "string")
|
|
781
|
+
);
|
|
782
|
+
};
|
|
783
|
+
var isErrorWithMessage = (e) => {
|
|
784
|
+
return Boolean(
|
|
785
|
+
typeof e === "object" && e !== null && "message" in e && typeof e.message === "string"
|
|
786
|
+
);
|
|
787
|
+
};
|
|
788
|
+
var getErrorMsg = (e) => {
|
|
789
|
+
return isErrorWithMessage(e) ? e.message : "Unknown error";
|
|
790
|
+
};
|
|
791
|
+
var isErrorMsg = (e, msg) => {
|
|
792
|
+
if (isError(e)) {
|
|
793
|
+
return e.message === msg;
|
|
794
|
+
}
|
|
795
|
+
return false;
|
|
796
|
+
};
|
|
797
|
+
|
|
744
798
|
// src/utils/hex.ts
|
|
745
799
|
var bytesToHex = (bytes) => {
|
|
746
800
|
return bytes.reduce((acc, byte) => {
|
|
@@ -766,6 +820,7 @@ var isType = (typename) => (node) => {
|
|
|
766
820
|
LightsparkAuthException,
|
|
767
821
|
LightsparkException,
|
|
768
822
|
LightsparkSigningException,
|
|
823
|
+
Logger,
|
|
769
824
|
NodeKeyCache,
|
|
770
825
|
RSASigningKey,
|
|
771
826
|
Requester,
|
|
@@ -780,8 +835,12 @@ var isType = (typename) => (node) => {
|
|
|
780
835
|
bytesToHex,
|
|
781
836
|
convertCurrencyAmount,
|
|
782
837
|
createSha256Hash,
|
|
838
|
+
getErrorMsg,
|
|
783
839
|
hexToBytes,
|
|
784
840
|
isBrowser,
|
|
841
|
+
isError,
|
|
842
|
+
isErrorMsg,
|
|
843
|
+
isErrorWithMessage,
|
|
785
844
|
isNode,
|
|
786
845
|
isType,
|
|
787
846
|
urlsafe_b64decode
|
package/dist/index.d.ts
CHANGED
|
@@ -101,6 +101,13 @@ declare class NodeKeyCache {
|
|
|
101
101
|
private stripPemTags;
|
|
102
102
|
}
|
|
103
103
|
|
|
104
|
+
declare class Logger {
|
|
105
|
+
context: string;
|
|
106
|
+
loggingEnabled: boolean;
|
|
107
|
+
constructor(loggerContext: string);
|
|
108
|
+
info(message: string, ...rest: unknown[]): void;
|
|
109
|
+
}
|
|
110
|
+
|
|
104
111
|
type Query<T> = {
|
|
105
112
|
/** The string representation of the query payload for graphQL. **/
|
|
106
113
|
queryPayload: string;
|
|
@@ -196,6 +203,14 @@ declare const convertCurrencyAmount: (from: CurrencyAmount, toUnit: CurrencyUnit
|
|
|
196
203
|
declare const isBrowser: boolean;
|
|
197
204
|
declare const isNode: boolean;
|
|
198
205
|
|
|
206
|
+
declare const isError: (e: unknown) => e is Error;
|
|
207
|
+
type ErrorWithMessage = {
|
|
208
|
+
message: string;
|
|
209
|
+
};
|
|
210
|
+
declare const isErrorWithMessage: (e: unknown) => e is ErrorWithMessage;
|
|
211
|
+
declare const getErrorMsg: (e: unknown) => string;
|
|
212
|
+
declare const isErrorMsg: (e: unknown, msg: string) => boolean;
|
|
213
|
+
|
|
199
214
|
declare const bytesToHex: (bytes: Uint8Array) => string;
|
|
200
215
|
declare const hexToBytes: (hex: string) => Uint8Array;
|
|
201
216
|
|
|
@@ -213,4 +228,4 @@ declare const isType: <T extends string>(typename: T) => <N extends {
|
|
|
213
228
|
__typename: T;
|
|
214
229
|
}>;
|
|
215
230
|
|
|
216
|
-
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, hexToBytes, isBrowser, isNode, isType, urlsafe_b64decode };
|
|
231
|
+
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,30 @@ 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) {
|
|
401
|
+
this.context = loggerContext;
|
|
402
|
+
if (isBrowser) {
|
|
403
|
+
try {
|
|
404
|
+
this.loggingEnabled = localStorage.getItem("lightspark-logging-enabled") === "1";
|
|
405
|
+
} catch (e) {
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
if (this.loggingEnabled) {
|
|
409
|
+
console.log(`[${this.context}] Logging enabled`);
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
info(message, ...rest) {
|
|
413
|
+
if (this.loggingEnabled) {
|
|
414
|
+
console.log(`[${this.context}] ${message}`, ...rest);
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
};
|
|
418
|
+
var logger = new Logger("@lightsparkdev/core");
|
|
419
|
+
|
|
396
420
|
// src/requester/Requester.ts
|
|
397
421
|
import autoBind2 from "auto-bind";
|
|
398
422
|
import dayjs from "dayjs";
|
|
@@ -444,6 +468,7 @@ var Requester = class {
|
|
|
444
468
|
return query.constructObject(data);
|
|
445
469
|
}
|
|
446
470
|
subscribe(queryPayload, variables = {}) {
|
|
471
|
+
logger.info(`Requester.subscribe params`, queryPayload, variables);
|
|
447
472
|
const operationNameRegex = /^\s*(query|mutation|subscription)\s+(\w+)/i;
|
|
448
473
|
const operationMatch = queryPayload.match(operationNameRegex);
|
|
449
474
|
if (!operationMatch || operationMatch.length < 3) {
|
|
@@ -467,13 +492,15 @@ var Requester = class {
|
|
|
467
492
|
variables,
|
|
468
493
|
operationName: operation
|
|
469
494
|
};
|
|
470
|
-
|
|
471
|
-
|
|
495
|
+
logger.info(`Requester.subscribe bodyData`, bodyData);
|
|
496
|
+
return new Observable((observer) => {
|
|
497
|
+
logger.info(`Requester.subscribe observer`, observer);
|
|
498
|
+
return this.wsClient.subscribe(bodyData, {
|
|
472
499
|
next: (data) => observer.next(data),
|
|
473
500
|
error: (err) => observer.error(err),
|
|
474
501
|
complete: () => observer.complete()
|
|
475
|
-
})
|
|
476
|
-
);
|
|
502
|
+
});
|
|
503
|
+
});
|
|
477
504
|
}
|
|
478
505
|
async makeRawRequest(queryPayload, variables = {}, signingNodeId = void 0, skipAuth = false) {
|
|
479
506
|
const operationNameRegex = /^\s*(query|mutation|subscription)\s+(\w+)/i;
|
|
@@ -608,7 +635,8 @@ var createSha256Hash = async (data) => {
|
|
|
608
635
|
return new Uint8Array(await window.crypto.subtle.digest("SHA-256", data));
|
|
609
636
|
} else {
|
|
610
637
|
const { createHash } = await import("crypto");
|
|
611
|
-
|
|
638
|
+
const buffer = createHash("sha256").update(data).digest();
|
|
639
|
+
return new Uint8Array(buffer);
|
|
612
640
|
}
|
|
613
641
|
};
|
|
614
642
|
|
|
@@ -682,6 +710,27 @@ var convertCurrencyAmount = (from, toUnit) => {
|
|
|
682
710
|
};
|
|
683
711
|
};
|
|
684
712
|
|
|
713
|
+
// src/utils/errors.ts
|
|
714
|
+
var isError = (e) => {
|
|
715
|
+
return Boolean(
|
|
716
|
+
typeof e === "object" && e !== null && "name" in e && typeof e.name === "string" && "message" in e && typeof e.message === "string" && "stack" in e && (!e.stack || typeof e.stack === "string")
|
|
717
|
+
);
|
|
718
|
+
};
|
|
719
|
+
var isErrorWithMessage = (e) => {
|
|
720
|
+
return Boolean(
|
|
721
|
+
typeof e === "object" && e !== null && "message" in e && typeof e.message === "string"
|
|
722
|
+
);
|
|
723
|
+
};
|
|
724
|
+
var getErrorMsg = (e) => {
|
|
725
|
+
return isErrorWithMessage(e) ? e.message : "Unknown error";
|
|
726
|
+
};
|
|
727
|
+
var isErrorMsg = (e, msg) => {
|
|
728
|
+
if (isError(e)) {
|
|
729
|
+
return e.message === msg;
|
|
730
|
+
}
|
|
731
|
+
return false;
|
|
732
|
+
};
|
|
733
|
+
|
|
685
734
|
// src/utils/hex.ts
|
|
686
735
|
var bytesToHex = (bytes) => {
|
|
687
736
|
return bytes.reduce((acc, byte) => {
|
|
@@ -706,6 +755,7 @@ export {
|
|
|
706
755
|
LightsparkAuthException_default as LightsparkAuthException,
|
|
707
756
|
LightsparkException_default as LightsparkException,
|
|
708
757
|
LightsparkSigningException_default as LightsparkSigningException,
|
|
758
|
+
Logger,
|
|
709
759
|
NodeKeyCache_default as NodeKeyCache,
|
|
710
760
|
RSASigningKey,
|
|
711
761
|
Requester_default as Requester,
|
|
@@ -720,8 +770,12 @@ export {
|
|
|
720
770
|
bytesToHex,
|
|
721
771
|
convertCurrencyAmount,
|
|
722
772
|
createSha256Hash,
|
|
773
|
+
getErrorMsg,
|
|
723
774
|
hexToBytes,
|
|
724
775
|
isBrowser,
|
|
776
|
+
isError,
|
|
777
|
+
isErrorMsg,
|
|
778
|
+
isErrorWithMessage,
|
|
725
779
|
isNode,
|
|
726
780
|
isType,
|
|
727
781
|
urlsafe_b64decode
|
package/package.json
CHANGED
package/src/Logger.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { isBrowser } from "./index.js";
|
|
2
|
+
|
|
3
|
+
export class Logger {
|
|
4
|
+
context: string;
|
|
5
|
+
loggingEnabled = false;
|
|
6
|
+
|
|
7
|
+
constructor(loggerContext: string) {
|
|
8
|
+
this.context = loggerContext;
|
|
9
|
+
if (isBrowser) {
|
|
10
|
+
try {
|
|
11
|
+
this.loggingEnabled =
|
|
12
|
+
localStorage.getItem("lightspark-logging-enabled") === "1";
|
|
13
|
+
} catch (e) {
|
|
14
|
+
/* ignore */
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
if (this.loggingEnabled) {
|
|
19
|
+
console.log(`[${this.context}] Logging enabled`);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
info(message: string, ...rest: unknown[]) {
|
|
24
|
+
if (this.loggingEnabled) {
|
|
25
|
+
console.log(`[${this.context}] ${message}`, ...rest);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
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
|
};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
export const isError = (e: unknown): e is Error => {
|
|
2
|
+
return Boolean(
|
|
3
|
+
typeof e === "object" &&
|
|
4
|
+
e !== null &&
|
|
5
|
+
"name" in e &&
|
|
6
|
+
typeof e.name === "string" &&
|
|
7
|
+
"message" in e &&
|
|
8
|
+
typeof e.message === "string" &&
|
|
9
|
+
"stack" in e &&
|
|
10
|
+
(!e.stack || typeof e.stack === "string"),
|
|
11
|
+
);
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
type ErrorWithMessage = {
|
|
15
|
+
message: string;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export const isErrorWithMessage = (e: unknown): e is ErrorWithMessage => {
|
|
19
|
+
return Boolean(
|
|
20
|
+
typeof e === "object" &&
|
|
21
|
+
e !== null &&
|
|
22
|
+
"message" in e &&
|
|
23
|
+
typeof e.message === "string",
|
|
24
|
+
);
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export const getErrorMsg = (e: unknown): string => {
|
|
28
|
+
return isErrorWithMessage(e) ? e.message : "Unknown error";
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export const isErrorMsg = (e: unknown, msg: string) => {
|
|
32
|
+
if (isError(e)) {
|
|
33
|
+
return e.message === msg;
|
|
34
|
+
}
|
|
35
|
+
return false;
|
|
36
|
+
};
|