@lightsparkdev/core 0.2.0 → 0.2.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.
Files changed (58) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/dist/chunk-23X5L5S3.js +604 -0
  3. package/dist/chunk-4OZ5E62P.js +608 -0
  4. package/dist/chunk-5FD46UVI.js +600 -0
  5. package/dist/chunk-BAQMCJ7C.js +630 -0
  6. package/dist/chunk-GFTKZSHK.js +627 -0
  7. package/dist/chunk-I3HKDOEE.js +627 -0
  8. package/dist/chunk-J6LSW6XO.js +601 -0
  9. package/dist/chunk-JUD4MOOQ.js +608 -0
  10. package/dist/chunk-KX6HH6CX.js +629 -0
  11. package/dist/chunk-MZCDWVLH.js +634 -0
  12. package/dist/chunk-NY3BK66J.js +624 -0
  13. package/dist/chunk-Q7UUXZNC.js +26 -0
  14. package/dist/chunk-WT2HYC6Q.js +601 -0
  15. package/dist/chunk-YYVOX4YM.js +600 -0
  16. package/dist/chunk-ZUIUADXH.js +616 -0
  17. package/dist/crypto-rn-2BYOHLMG.js +72 -0
  18. package/dist/crypto-rn-2YV53C3B.js +72 -0
  19. package/dist/crypto-rn-3HFXYG7I.js +75 -0
  20. package/dist/crypto-rn-3HLWLVZS.js +76 -0
  21. package/dist/crypto-rn-3N3JTBLE.js +83 -0
  22. package/dist/crypto-rn-3OEMMMOD.js +74 -0
  23. package/dist/crypto-rn-5EZSRY5Y.js +82 -0
  24. package/dist/crypto-rn-5KM2YVOI.js +82 -0
  25. package/dist/crypto-rn-6335R2CU.js +80 -0
  26. package/dist/crypto-rn-6ZHSL7CX.js +72 -0
  27. package/dist/crypto-rn-7DWXMO2Q.js +75 -0
  28. package/dist/crypto-rn-7GTI374I.js +81 -0
  29. package/dist/crypto-rn-AXDY3LDG.js +124 -0
  30. package/dist/crypto-rn-BZ2KZ2YR.js +68 -0
  31. package/dist/crypto-rn-CAPL7MYC.js +80 -0
  32. package/dist/crypto-rn-CBAKEB7C.js +68 -0
  33. package/dist/crypto-rn-CBWHV2F5.js +82 -0
  34. package/dist/crypto-rn-CS36MQ4X.js +77 -0
  35. package/dist/crypto-rn-E4RZNGIB.js +82 -0
  36. package/dist/crypto-rn-H4NIT5CT.js +70 -0
  37. package/dist/crypto-rn-IAC27WLZ.js +74 -0
  38. package/dist/crypto-rn-LLY6FCWE.js +82 -0
  39. package/dist/crypto-rn-MOWVVG3L.js +78 -0
  40. package/dist/crypto-rn-OAPLHNM5.js +73 -0
  41. package/dist/crypto-rn-PVAG5TVH.js +80 -0
  42. package/dist/crypto-rn-QLVBL5DI.js +75 -0
  43. package/dist/crypto-rn-TBKXR3SR.js +68 -0
  44. package/dist/crypto-rn-TSQJA6A3.js +81 -0
  45. package/dist/crypto-rn-U3XEJXIM.js +77 -0
  46. package/dist/crypto-rn-UHTZEVAR.js +74 -0
  47. package/dist/crypto-rn-V3ZNQSFI.js +79 -0
  48. package/dist/crypto-rn-VGNP6VZW.js +75 -0
  49. package/dist/crypto-rn-XMYCUEGV.js +72 -0
  50. package/dist/crypto-rn-YPOTC5RI.js +73 -0
  51. package/dist/index.cjs +93 -116
  52. package/dist/index.d.ts +31 -17
  53. package/dist/index.js +92 -106
  54. package/package.json +2 -1
  55. package/src/crypto/NodeKeyCache.ts +18 -15
  56. package/src/crypto/crypto.ts +72 -78
  57. package/src/requester/Requester.ts +11 -17
  58. package/src/utils/base64.ts +51 -2
@@ -12,8 +12,8 @@ import Query from "./Query.js";
12
12
  import AuthProvider from "../auth/AuthProvider.js";
13
13
  import StubAuthProvider from "../auth/StubAuthProvider.js";
14
14
  import {
15
- getCrypto,
16
- getNonce,
15
+ CryptoInterface,
16
+ DefaultCrypto,
17
17
  LightsparkSigningException,
18
18
  } from "../crypto/crypto.js";
19
19
  import NodeKeyCache from "../crypto/NodeKeyCache.js";
@@ -32,8 +32,10 @@ class Requester {
32
32
  constructor(
33
33
  private readonly nodeKeyCache: NodeKeyCache,
34
34
  private readonly schemaEndpoint: string,
35
+ private readonly sdkUserAgent: string,
35
36
  private readonly authProvider: AuthProvider = new StubAuthProvider(),
36
- private readonly baseUrl: string = DEFAULT_BASE_URL
37
+ private readonly baseUrl: string = DEFAULT_BASE_URL,
38
+ private readonly cryptoImpl: CryptoInterface = DefaultCrypto
37
39
  ) {
38
40
  let websocketImpl;
39
41
  if (typeof WebSocket === "undefined" && typeof window === "undefined") {
@@ -166,10 +168,7 @@ class Requester {
166
168
  private getSdkUserAgent(): string {
167
169
  const platform = isNode ? "NodeJS" : "Browser";
168
170
  const platformVersion = isNode ? process.version : "";
169
- // TODO(Jeremy): Figure out how to properly load this from the package.json. Using an import
170
- // is breaking the streaming sats extension.
171
- const sdkVersion = "1.0.4";
172
- return `lightspark-js-sdk/${sdkVersion} ${platform}/${platformVersion}`;
171
+ return `${this.sdkUserAgent} ${platform}/${platformVersion}`;
173
172
  }
174
173
 
175
174
  private async addSigningDataIfNeeded(
@@ -185,7 +184,7 @@ class Requester {
185
184
  const variables = queryPayload.variables;
186
185
  const operationName = queryPayload.operationName;
187
186
 
188
- const nonce = await getNonce();
187
+ const nonce = await this.cryptoImpl.getNonce();
189
188
  const expiration = dayjs.utc().add(1, "hour").format();
190
189
 
191
190
  const payload = {
@@ -203,16 +202,11 @@ class Requester {
203
202
  );
204
203
  }
205
204
 
205
+ if (typeof TextEncoder === "undefined") {
206
+ const TextEncoder = (await import("text-encoding")).TextEncoder;
207
+ }
206
208
  const encodedPayload = new TextEncoder().encode(JSON.stringify(payload));
207
- const cryptoImpl = await getCrypto();
208
- const signedPayload = await cryptoImpl.subtle.sign(
209
- {
210
- name: "RSA-PSS",
211
- saltLength: 32,
212
- },
213
- key,
214
- encodedPayload
215
- );
209
+ const signedPayload = await this.cryptoImpl.sign(key, encodedPayload);
216
210
  const encodedSignedPayload = b64encode(signedPayload);
217
211
 
218
212
  headers["X-Lightspark-Signing"] = JSON.stringify({
@@ -1,7 +1,56 @@
1
1
  // Copyright ©, 2023, Lightspark Group, Inc. - All Rights Reserved
2
2
 
3
+ const chars =
4
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
5
+ const Base64 = {
6
+ btoa: (input: string = "") => {
7
+ let str = input;
8
+ let output = "";
9
+
10
+ for (
11
+ let block = 0, charCode, i = 0, map = chars;
12
+ str.charAt(i | 0) || ((map = "="), i % 1);
13
+ output += map.charAt(63 & (block >> (8 - (i % 1) * 8)))
14
+ ) {
15
+ charCode = str.charCodeAt((i += 3 / 4));
16
+
17
+ if (charCode > 0xff) {
18
+ throw new Error(
19
+ "'btoa' failed: The string to be encoded contains characters outside of the Latin1 range."
20
+ );
21
+ }
22
+
23
+ block = (block << 8) | charCode;
24
+ }
25
+
26
+ return output;
27
+ },
28
+
29
+ atob: (input: string = "") => {
30
+ let str = input.replace(/=+$/, "");
31
+ let output = "";
32
+
33
+ if (str.length % 4 == 1) {
34
+ throw new Error(
35
+ "'atob' failed: The string to be decoded is not correctly encoded."
36
+ );
37
+ }
38
+ for (
39
+ let bc = 0, bs = 0, buffer, i = 0;
40
+ (buffer = str.charAt(i++));
41
+ ~buffer && ((bs = bc % 4 ? bs * 64 + buffer : buffer), bc++ % 4)
42
+ ? (output += String.fromCharCode(255 & (bs >> ((-2 * bc) & 6))))
43
+ : 0
44
+ ) {
45
+ buffer = chars.indexOf(buffer);
46
+ }
47
+
48
+ return output;
49
+ },
50
+ };
51
+
3
52
  export const b64decode = (encoded: string): Uint8Array => {
4
- return Uint8Array.from(atob(encoded), (c) => c.charCodeAt(0));
53
+ return Uint8Array.from(Base64.atob(encoded), (c) => c.charCodeAt(0));
5
54
  };
6
55
 
7
56
  export const urlsafe_b64decode = (encoded: string): Uint8Array => {
@@ -9,7 +58,7 @@ export const urlsafe_b64decode = (encoded: string): Uint8Array => {
9
58
  };
10
59
 
11
60
  export const b64encode = (data: ArrayBuffer): string => {
12
- return btoa(
61
+ return Base64.btoa(
13
62
  String.fromCharCode.apply(null, Array.from(new Uint8Array(data)))
14
63
  );
15
64
  };