@lightsparkdev/core 1.0.1 → 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 CHANGED
@@ -1,5 +1,11 @@
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
+
3
9
  ## 1.0.1
4
10
 
5
11
  ### 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,30 @@ 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) {
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
+
459
484
  // src/requester/Requester.ts
460
485
  var import_auto_bind2 = __toESM(require("auto-bind"), 1);
461
486
  var import_dayjs = __toESM(require("dayjs"), 1);
@@ -507,6 +532,7 @@ var Requester = class {
507
532
  return query.constructObject(data);
508
533
  }
509
534
  subscribe(queryPayload, variables = {}) {
535
+ logger.info(`Requester.subscribe params`, queryPayload, variables);
510
536
  const operationNameRegex = /^\s*(query|mutation|subscription)\s+(\w+)/i;
511
537
  const operationMatch = queryPayload.match(operationNameRegex);
512
538
  if (!operationMatch || operationMatch.length < 3) {
@@ -530,13 +556,15 @@ var Requester = class {
530
556
  variables,
531
557
  operationName: operation
532
558
  };
533
- return new import_zen_observable_ts.Observable(
534
- (observer) => this.wsClient.subscribe(bodyData, {
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, {
535
563
  next: (data) => observer.next(data),
536
564
  error: (err) => observer.error(err),
537
565
  complete: () => observer.complete()
538
- })
539
- );
566
+ });
567
+ });
540
568
  }
541
569
  async makeRawRequest(queryPayload, variables = {}, signingNodeId = void 0, skipAuth = false) {
542
570
  const operationNameRegex = /^\s*(query|mutation|subscription)\s+(\w+)/i;
@@ -671,7 +699,8 @@ var createSha256Hash = async (data) => {
671
699
  return new Uint8Array(await window.crypto.subtle.digest("SHA-256", data));
672
700
  } else {
673
701
  const { createHash } = await import("crypto");
674
- return createHash("sha256").update(data).digest();
702
+ const buffer = createHash("sha256").update(data).digest();
703
+ return new Uint8Array(buffer);
675
704
  }
676
705
  };
677
706
 
@@ -791,6 +820,7 @@ var isType = (typename) => (node) => {
791
820
  LightsparkAuthException,
792
821
  LightsparkException,
793
822
  LightsparkSigningException,
823
+ Logger,
794
824
  NodeKeyCache,
795
825
  RSASigningKey,
796
826
  Requester,
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;
@@ -221,4 +228,4 @@ declare const isType: <T extends string>(typename: T) => <N extends {
221
228
  __typename: T;
222
229
  }>;
223
230
 
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 };
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
- return new Observable(
471
- (observer) => this.wsClient.subscribe(bodyData, {
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
- return createHash("sha256").update(data).digest();
638
+ const buffer = createHash("sha256").update(data).digest();
639
+ return new Uint8Array(buffer);
612
640
  }
613
641
  };
614
642
 
@@ -727,6 +755,7 @@ export {
727
755
  LightsparkAuthException_default as LightsparkAuthException,
728
756
  LightsparkException_default as LightsparkException,
729
757
  LightsparkSigningException_default as LightsparkSigningException,
758
+ Logger,
730
759
  NodeKeyCache_default as NodeKeyCache,
731
760
  RSASigningKey,
732
761
  Requester_default as Requester,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lightsparkdev/core",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "Lightspark JS SDK",
5
5
  "author": "Lightspark Inc.",
6
6
  "keywords": [
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
@@ -3,6 +3,7 @@
3
3
  export * from "./auth/index.js";
4
4
  export * from "./crypto/index.js";
5
5
  export { default as LightsparkException } from "./LightsparkException.js";
6
+ export { Logger } from "./Logger.js";
6
7
  export * from "./requester/index.js";
7
8
  export {
8
9
  apiDomainForEnvironment,
@@ -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
- return new Observable<{ data: T }>((observer) =>
96
- this.wsClient.subscribe(bodyData, {
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(
@@ -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
- return createHash("sha256").update(data).digest();
10
+ const buffer = createHash("sha256").update(data).digest();
11
+ return new Uint8Array(buffer);
11
12
  }
12
13
  };