glitch-javascript-sdk 1.8.1 → 1.8.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.
@@ -0,0 +1,69 @@
1
+ import Route from "./interface";
2
+ import HTTP_METHODS from "../constants/HttpMethods";
3
+
4
+ class WebsiteAnalyticsRoute {
5
+ public static routes: { [key: string]: Route } = {
6
+ listSessions: {
7
+ url: '/analytics/sessions',
8
+ method: HTTP_METHODS.GET
9
+ },
10
+ listPageviews: {
11
+ url: '/analytics/pageviews',
12
+ method: HTTP_METHODS.GET
13
+ },
14
+ listEvents: {
15
+ url: '/analytics/events',
16
+ method: HTTP_METHODS.GET
17
+ },
18
+ overview: {
19
+ url: '/analytics/overview',
20
+ method: HTTP_METHODS.GET
21
+ },
22
+ collect: {
23
+ url: '/analytics/collect',
24
+ method: HTTP_METHODS.POST
25
+ },
26
+ sessionsAverage: {
27
+ url: '/analytics/sessions/average',
28
+ method: HTTP_METHODS.GET
29
+ },
30
+ sessionsHistogram: {
31
+ url: '/analytics/sessions/histogram',
32
+ method: HTTP_METHODS.GET
33
+ },
34
+ pageviewsOverTime: {
35
+ url: '/analytics/pageviews/over-time',
36
+ method: HTTP_METHODS.GET
37
+ },
38
+ topPages: {
39
+ url: '/analytics/pageviews/top-pages',
40
+ method: HTTP_METHODS.GET
41
+ },
42
+ eventsSummary: {
43
+ url: '/analytics/events/summary',
44
+ method: HTTP_METHODS.GET
45
+ },
46
+ popularEvents: {
47
+ url: '/analytics/events/popular',
48
+ method: HTTP_METHODS.GET
49
+ },
50
+ geoDistribution: {
51
+ url: '/analytics/geo-distribution',
52
+ method: HTTP_METHODS.GET
53
+ },
54
+ deviceBreakdown: {
55
+ url: '/analytics/device-breakdown',
56
+ method: HTTP_METHODS.GET
57
+ },
58
+ referrers: {
59
+ url: '/analytics/referrers',
60
+ method: HTTP_METHODS.GET
61
+ },
62
+ utmPerformance: {
63
+ url: '/analytics/utm-performance',
64
+ method: HTTP_METHODS.GET
65
+ }
66
+ };
67
+ }
68
+
69
+ export default WebsiteAnalyticsRoute;
@@ -1,8 +1,70 @@
1
1
  import { Config } from "../config";
2
2
  import Storage from "./Storage";
3
3
 
4
- class Session {
4
+ // Type declarations for crypto functionality
5
+ interface HmacInterface {
6
+ update(data: string): HmacInterface;
7
+ digest(encoding: 'hex'): string;
8
+ }
9
+
10
+ interface CryptoInterface {
11
+ createHmac(algorithm: string, secret: string): HmacInterface;
12
+ }
13
+
14
+ // Browser implementation using crypto-js
15
+ class BrowserCrypto implements CryptoInterface {
16
+ createHmac(algorithm: string, secret: string): HmacInterface {
17
+ const CryptoJS = require('crypto-js');
18
+
19
+ let data = '';
20
+
21
+ const hmac: HmacInterface = {
22
+ update: (updateData: string): HmacInterface => {
23
+ data = updateData;
24
+ return hmac;
25
+ },
26
+ digest: (encoding: 'hex'): string => {
27
+ if (encoding !== 'hex') {
28
+ throw new Error('Only hex encoding is supported in browser implementation');
29
+ }
30
+ return CryptoJS.HmacSHA256(data, secret).toString(CryptoJS.enc.Hex);
31
+ }
32
+ };
33
+
34
+ return hmac;
35
+ }
36
+ }
37
+
38
+ // Node.js implementation using native crypto
39
+ class NodeCrypto implements CryptoInterface {
40
+ private crypto: typeof import('crypto');
41
+
42
+ constructor() {
43
+ this.crypto = require('crypto');
44
+ }
45
+
46
+ createHmac(algorithm: string, secret: string): HmacInterface {
47
+ return this.crypto.createHmac(algorithm, secret);
48
+ }
49
+ }
50
+
51
+ // Determine which crypto implementation to use
52
+ const getCrypto = (): CryptoInterface => {
53
+ try {
54
+ // Check if we're in Node.js environment
55
+ if (typeof process !== 'undefined' && process.versions && process.versions.node) {
56
+ return new NodeCrypto();
57
+ }
58
+ // Fall back to browser implementation
59
+ return new BrowserCrypto();
60
+ } catch (e) {
61
+ return new BrowserCrypto();
62
+ }
63
+ };
64
+
65
+ const crypto = getCrypto();
5
66
 
67
+ class Session {
6
68
  private static _id_key = 'user_id';
7
69
  private static _first_name_key = 'user_first_name';
8
70
  private static _last_name_key = 'user_last_name';
@@ -37,7 +99,7 @@ class Session {
37
99
  public static hasJoinedCommunity() {
38
100
  const community = Storage.get('community');
39
101
 
40
- if(!community) {
102
+ if (!community) {
41
103
  return false;
42
104
  }
43
105
 
@@ -53,7 +115,7 @@ class Session {
53
115
  Storage.set(Session._username_key, null);
54
116
  }
55
117
 
56
- public static processAuthentication(data: { token: { access_token: string }, id: string, first_name: string, last_name: string, email: string, username: string }): void {
118
+ public static processAuthentication(data: { token: { access_token: string }, id: string, first_name: string, last_name: string, email: string, username: string }): void {
57
119
  Storage.setAuthToken(data.token.access_token);
58
120
  Storage.set(Session._id_key, data.id);
59
121
  Storage.set(Session._first_name_key, data.first_name);
@@ -63,6 +125,32 @@ class Session {
63
125
 
64
126
  Config.setAuthToken(data.token.access_token);
65
127
  }
128
+
129
+ /**
130
+ * Generate a tracking token for analytics collection
131
+ * @param titleId The title ID to generate token for
132
+ * @param secret The secret key (should match server config)
133
+ * @returns HMAC-SHA256 token
134
+ * @throws Error if crypto operations fail
135
+ */
136
+ public static generateTrackingToken(titleId: string, secret: string): string {
137
+ try {
138
+ if (!titleId) {
139
+ throw new Error('titleId is required');
140
+ }
141
+ if (!secret) {
142
+ throw new Error('secret is required');
143
+ }
144
+
145
+ return crypto
146
+ .createHmac('sha256', secret)
147
+ .update(titleId)
148
+ .digest('hex');
149
+ } catch (error) {
150
+ console.error('Failed to generate tracking token:', error);
151
+ throw new Error('Failed to generate tracking token');
152
+ }
153
+ }
66
154
  }
67
155
 
68
- export default Session;
156
+ export default Session;