glitch-javascript-sdk 1.8.2 → 1.8.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "glitch-javascript-sdk",
3
- "version": "1.8.2",
3
+ "version": "1.8.3",
4
4
  "description": "Javascript SDK for Glitch",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",
@@ -13,9 +13,13 @@ interface CryptoInterface {
13
13
 
14
14
  // Browser implementation using crypto-js
15
15
  class BrowserCrypto implements CryptoInterface {
16
+ private CryptoJS: any;
17
+
18
+ constructor() {
19
+ this.CryptoJS = require('crypto-js');
20
+ }
21
+
16
22
  createHmac(algorithm: string, secret: string): HmacInterface {
17
- const CryptoJS = require('crypto-js');
18
-
19
23
  let data = '';
20
24
 
21
25
  const hmac: HmacInterface = {
@@ -27,7 +31,7 @@ class BrowserCrypto implements CryptoInterface {
27
31
  if (encoding !== 'hex') {
28
32
  throw new Error('Only hex encoding is supported in browser implementation');
29
33
  }
30
- return CryptoJS.HmacSHA256(data, secret).toString(CryptoJS.enc.Hex);
34
+ return this.CryptoJS.HmacSHA256(data, secret).toString(this.CryptoJS.enc.Hex);
31
35
  }
32
36
  };
33
37
 
@@ -35,15 +39,24 @@ class BrowserCrypto implements CryptoInterface {
35
39
  }
36
40
  }
37
41
 
38
- // Node.js implementation using native crypto
42
+ // Node.js implementation that maintains sync interface
39
43
  class NodeCrypto implements CryptoInterface {
40
- private crypto: typeof import('crypto');
44
+ private crypto?: typeof import('crypto');
41
45
 
42
46
  constructor() {
43
- this.crypto = require('crypto');
47
+ // Use dynamic import but handle it synchronously for interface compliance
48
+ try {
49
+ // This will throw in browser environments
50
+ this.crypto = require('crypto');
51
+ } catch (e) {
52
+ this.crypto = undefined;
53
+ }
44
54
  }
45
55
 
46
56
  createHmac(algorithm: string, secret: string): HmacInterface {
57
+ if (!this.crypto) {
58
+ throw new Error('Node.js crypto module not available');
59
+ }
47
60
  return this.crypto.createHmac(algorithm, secret);
48
61
  }
49
62
  }
@@ -51,18 +64,26 @@ class NodeCrypto implements CryptoInterface {
51
64
  // Determine which crypto implementation to use
52
65
  const getCrypto = (): CryptoInterface => {
53
66
  try {
54
- // Check if we're in Node.js environment
55
- if (typeof process !== 'undefined' && process.versions && process.versions.node) {
56
- return new NodeCrypto();
67
+ // Check if we're in Node.js environment and crypto is available
68
+ if (typeof process !== 'undefined' && process.versions?.node) {
69
+ const nodeCrypto = new NodeCrypto();
70
+ // Verify crypto was actually loaded
71
+ try {
72
+ nodeCrypto.createHmac('sha256', 'test');
73
+ return nodeCrypto;
74
+ } catch (e) {
75
+ console.warn('Node.js crypto not available, falling back to browser implementation');
76
+ }
57
77
  }
58
- // Fall back to browser implementation
59
- return new BrowserCrypto();
60
78
  } catch (e) {
61
- return new BrowserCrypto();
79
+ console.warn('Node.js environment detection failed, falling back to browser implementation');
62
80
  }
81
+ // Fall back to browser implementation
82
+ return new BrowserCrypto();
63
83
  };
64
84
 
65
- const crypto = getCrypto();
85
+ // Singleton crypto instance
86
+ const cryptoInstance: CryptoInterface = getCrypto();
66
87
 
67
88
  class Session {
68
89
  private static _id_key = 'user_id';
@@ -96,14 +117,9 @@ class Session {
96
117
  return Storage.get(Session._email_key);
97
118
  }
98
119
 
99
- public static hasJoinedCommunity() {
120
+ public static hasJoinedCommunity(): boolean {
100
121
  const community = Storage.get('community');
101
-
102
- if (!community) {
103
- return false;
104
- }
105
-
106
- return (community?.me) ? true : false;
122
+ return !!community?.me;
107
123
  }
108
124
 
109
125
  public static end(): void {
@@ -115,7 +131,14 @@ class Session {
115
131
  Storage.set(Session._username_key, null);
116
132
  }
117
133
 
118
- public static processAuthentication(data: { token: { access_token: string }, id: string, first_name: string, last_name: string, email: string, username: string }): void {
134
+ public static processAuthentication(data: {
135
+ token: { access_token: string },
136
+ id: string,
137
+ first_name: string,
138
+ last_name: string,
139
+ email: string,
140
+ username: string
141
+ }): void {
119
142
  Storage.setAuthToken(data.token.access_token);
120
143
  Storage.set(Session._id_key, data.id);
121
144
  Storage.set(Session._first_name_key, data.first_name);
@@ -142,7 +165,7 @@ class Session {
142
165
  throw new Error('secret is required');
143
166
  }
144
167
 
145
- return crypto
168
+ return cryptoInstance
146
169
  .createHmac('sha256', secret)
147
170
  .update(titleId)
148
171
  .digest('hex');