@hot-updater/plugin-insights 1.0.0-rc.20

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 (85) hide show
  1. package/LICENSE +33 -0
  2. package/README.md +24 -0
  3. package/client/package.json +7 -0
  4. package/dist/client/eventId.cjs +181 -0
  5. package/dist/client/eventId.mjs +179 -0
  6. package/dist/client/index.cjs +234 -0
  7. package/dist/client/index.d.cts +42 -0
  8. package/dist/client/index.d.mts +42 -0
  9. package/dist/client/index.mjs +233 -0
  10. package/dist/client/sender.cjs +103 -0
  11. package/dist/client/sender.mjs +103 -0
  12. package/dist/client/state.cjs +121 -0
  13. package/dist/client/state.mjs +119 -0
  14. package/dist/internal.cjs +9 -0
  15. package/dist/internal.d.cts +5 -0
  16. package/dist/internal.d.mts +5 -0
  17. package/dist/internal.mjs +5 -0
  18. package/dist/server/contract.cjs +193 -0
  19. package/dist/server/contract.mjs +189 -0
  20. package/dist/server/domain.d.cts +149 -0
  21. package/dist/server/domain.d.mts +149 -0
  22. package/dist/server/errors.cjs +15 -0
  23. package/dist/server/errors.d.cts +6 -0
  24. package/dist/server/errors.d.mts +6 -0
  25. package/dist/server/errors.mjs +14 -0
  26. package/dist/server/eventInput.cjs +284 -0
  27. package/dist/server/eventInput.d.cts +6 -0
  28. package/dist/server/eventInput.d.mts +6 -0
  29. package/dist/server/eventInput.mjs +282 -0
  30. package/dist/server/eventRow.cjs +49 -0
  31. package/dist/server/eventRow.d.cts +64 -0
  32. package/dist/server/eventRow.d.mts +64 -0
  33. package/dist/server/eventRow.mjs +47 -0
  34. package/dist/server/index.cjs +128 -0
  35. package/dist/server/index.d.cts +394 -0
  36. package/dist/server/index.d.mts +394 -0
  37. package/dist/server/index.mjs +121 -0
  38. package/dist/server/model.cjs +17 -0
  39. package/dist/server/model.d.cts +10 -0
  40. package/dist/server/model.d.mts +10 -0
  41. package/dist/server/model.mjs +17 -0
  42. package/dist/server/modelTypes.d.cts +176 -0
  43. package/dist/server/modelTypes.d.mts +176 -0
  44. package/dist/server/overview.cjs +109 -0
  45. package/dist/server/overview.d.cts +15 -0
  46. package/dist/server/overview.d.mts +15 -0
  47. package/dist/server/overview.mjs +107 -0
  48. package/dist/server/provider.cjs +375 -0
  49. package/dist/server/provider.d.cts +6 -0
  50. package/dist/server/provider.d.mts +6 -0
  51. package/dist/server/provider.mjs +374 -0
  52. package/dist/server/queryInput.cjs +93 -0
  53. package/dist/server/queryInput.mjs +91 -0
  54. package/dist/server/reads.cjs +681 -0
  55. package/dist/server/reads.d.cts +60 -0
  56. package/dist/server/reads.d.mts +60 -0
  57. package/dist/server/reads.mjs +674 -0
  58. package/dist/server/recordEvent.cjs +319 -0
  59. package/dist/server/recordEvent.d.cts +17 -0
  60. package/dist/server/recordEvent.d.mts +17 -0
  61. package/dist/server/recordEvent.mjs +316 -0
  62. package/dist/server/routes.cjs +96 -0
  63. package/dist/server/routes.d.cts +36 -0
  64. package/dist/server/routes.d.mts +36 -0
  65. package/dist/server/routes.mjs +95 -0
  66. package/dist/server/schema.cjs +489 -0
  67. package/dist/server/schema.d.cts +706 -0
  68. package/dist/server/schema.d.mts +706 -0
  69. package/dist/server/schema.mjs +483 -0
  70. package/dist/server/testing/eventRow.d.mts +64 -0
  71. package/dist/server/testing/expectInsightsIndex.mjs +18 -0
  72. package/dist/server/testing/fixtures.d.mts +8 -0
  73. package/dist/server/testing/fixtures.mjs +25 -0
  74. package/dist/server/testing/index.d.mts +24 -0
  75. package/dist/server/testing/index.mjs +25 -0
  76. package/dist/server/testing/insightsModelTests.mjs +714 -0
  77. package/dist/server/testing/modelTypes.d.mts +176 -0
  78. package/dist/server/testing/setupInsightsHttpTestSuite.d.mts +11 -0
  79. package/dist/server/testing/setupInsightsHttpTestSuite.mjs +107 -0
  80. package/dist/server/testing/setupInsightsModelTestSuite.d.mts +11 -0
  81. package/dist/server/testing/setupInsightsModelTestSuite.mjs +31 -0
  82. package/dist/server/testing/types.d.mts +26 -0
  83. package/dist/server/types.d.cts +33 -0
  84. package/dist/server/types.d.mts +33 -0
  85. package/package.json +68 -0
package/LICENSE ADDED
@@ -0,0 +1,33 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Sungyu Kang
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
23
+ ---
24
+
25
+ ADDITIONAL DISCLAIMER
26
+
27
+ The authors of this software shall not be held responsible for any legal issues,
28
+ regulatory violations, policy violations, or disputes arising from the use,
29
+ distribution, or integration of this software.
30
+
31
+ Users are solely responsible for ensuring compliance with all applicable laws,
32
+ regulations, and third-party policies (including but not limited to app store
33
+ policies).
package/README.md ADDED
@@ -0,0 +1,24 @@
1
+ # @hot-updater/plugin-insights
2
+
3
+ Hot Updater's Insights plugin. The server plugin records launches, downloads,
4
+ and update failures and serves them to the Console and the CLI; the client
5
+ plugin reports them from the app.
6
+
7
+ You don't install it on its own. `@hot-updater/server` and
8
+ `@hot-updater/react-native` depend on it and re-export it:
9
+
10
+ ```ts
11
+ // The server definition
12
+ import { insights } from "@hot-updater/server/plugins/insights";
13
+
14
+ // The app
15
+ import { insights } from "@hot-updater/react-native/plugins/insights";
16
+ ```
17
+
18
+ | Entry | Re-exported as |
19
+ | ----------- | ----------------------------------------------- |
20
+ | `./server` | `@hot-updater/server/plugins/insights` |
21
+ | `./client` | `@hot-updater/react-native/plugins/insights` |
22
+ | `./testing` | `@hot-updater/server/plugins/insights/testing` |
23
+
24
+ See [Insights](https://hot-updater.dev/docs/guides/insights).
@@ -0,0 +1,7 @@
1
+ {
2
+ "name": "@hot-updater/plugin-insights/client",
3
+ "private": true,
4
+ "main": "../dist/client/index.cjs",
5
+ "module": "../dist/client/index.mjs",
6
+ "types": "../dist/client/index.d.cts"
7
+ }
@@ -0,0 +1,181 @@
1
+ //#region src/client/eventId.ts
2
+ const formatUUIDv7 = (bytes, timestamp) => {
3
+ let time = timestamp;
4
+ for (let index = 5; index >= 0; index -= 1) {
5
+ bytes[index] = time % 256;
6
+ time = Math.floor(time / 256);
7
+ }
8
+ bytes[6] = 112 | bytes[6] & 15;
9
+ bytes[8] = 128 | bytes[8] & 63;
10
+ const hex = Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0")).join("");
11
+ return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
12
+ };
13
+ /**
14
+ * Creates an RFC 9562 UUIDv7: a 48-bit millisecond timestamp, then random bits.
15
+ *
16
+ * `createUUIDv7` from `@hot-updater/plugin-core` calls `crypto.getRandomValues`
17
+ * unconditionally, but React Native has no Web Crypto unless the app installs a
18
+ * polyfill. An Insights event ID only has to tell one event from another so a
19
+ * retried POST is counted once, so `Math.random` is enough without one.
20
+ */
21
+ const createUUIDv7 = () => {
22
+ const bytes = /* @__PURE__ */ new Uint8Array(16);
23
+ const { crypto } = globalThis;
24
+ if (typeof crypto?.getRandomValues === "function") crypto.getRandomValues(bytes);
25
+ else for (let index = 0; index < bytes.length; index += 1) bytes[index] = Math.floor(Math.random() * 256);
26
+ return formatUUIDv7(bytes, Date.now());
27
+ };
28
+ const SHA256_K = [
29
+ 1116352408,
30
+ 1899447441,
31
+ 3049323471,
32
+ 3921009573,
33
+ 961987163,
34
+ 1508970993,
35
+ 2453635748,
36
+ 2870763221,
37
+ 3624381080,
38
+ 310598401,
39
+ 607225278,
40
+ 1426881987,
41
+ 1925078388,
42
+ 2162078206,
43
+ 2614888103,
44
+ 3248222580,
45
+ 3835390401,
46
+ 4022224774,
47
+ 264347078,
48
+ 604807628,
49
+ 770255983,
50
+ 1249150122,
51
+ 1555081692,
52
+ 1996064986,
53
+ 2554220882,
54
+ 2821834349,
55
+ 2952996808,
56
+ 3210313671,
57
+ 3336571891,
58
+ 3584528711,
59
+ 113926993,
60
+ 338241895,
61
+ 666307205,
62
+ 773529912,
63
+ 1294757372,
64
+ 1396182291,
65
+ 1695183700,
66
+ 1986661051,
67
+ 2177026350,
68
+ 2456956037,
69
+ 2730485921,
70
+ 2820302411,
71
+ 3259730800,
72
+ 3345764771,
73
+ 3516065817,
74
+ 3600352804,
75
+ 4094571909,
76
+ 275423344,
77
+ 430227734,
78
+ 506948616,
79
+ 659060556,
80
+ 883997877,
81
+ 958139571,
82
+ 1322822218,
83
+ 1537002063,
84
+ 1747873779,
85
+ 1955562222,
86
+ 2024104815,
87
+ 2227730452,
88
+ 2361852424,
89
+ 2428436474,
90
+ 2756734187,
91
+ 3204031479,
92
+ 3329325298
93
+ ];
94
+ const encodeUtf8 = (value) => {
95
+ const bytes = [];
96
+ for (const character of value) {
97
+ const code = character.codePointAt(0);
98
+ if (code < 128) bytes.push(code);
99
+ else if (code < 2048) bytes.push(192 | code >> 6, 128 | code & 63);
100
+ else if (code < 65536) bytes.push(224 | code >> 12, 128 | code >> 6 & 63, 128 | code & 63);
101
+ else bytes.push(240 | code >> 18, 128 | code >> 12 & 63, 128 | code >> 6 & 63, 128 | code & 63);
102
+ }
103
+ return bytes;
104
+ };
105
+ /** SHA-256 of a string's UTF-8 bytes, since React Native has no Web Crypto. */
106
+ const sha256 = (value) => {
107
+ const bytes = encodeUtf8(value);
108
+ const bitLength = bytes.length * 8;
109
+ bytes.push(128);
110
+ while (bytes.length % 64 !== 56) bytes.push(0);
111
+ for (let shift = 56; shift >= 0; shift -= 8) bytes.push(shift >= 32 ? Math.floor(bitLength / 2 ** shift) & 255 : bitLength >>> shift & 255);
112
+ const hash = [
113
+ 1779033703,
114
+ 3144134277,
115
+ 1013904242,
116
+ 2773480762,
117
+ 1359893119,
118
+ 2600822924,
119
+ 528734635,
120
+ 1541459225
121
+ ];
122
+ const words = Array.from({ length: 64 }, () => 0);
123
+ const rotate = (word, bits) => word >>> bits | word << 32 - bits;
124
+ for (let offset = 0; offset < bytes.length; offset += 64) {
125
+ for (let index = 0; index < 16; index += 1) {
126
+ const at = offset + index * 4;
127
+ words[index] = bytes[at] << 24 | bytes[at + 1] << 16 | bytes[at + 2] << 8 | bytes[at + 3];
128
+ }
129
+ for (let index = 16; index < 64; index += 1) {
130
+ const low = words[index - 15];
131
+ const high = words[index - 2];
132
+ const s0 = rotate(low, 7) ^ rotate(low, 18) ^ low >>> 3;
133
+ const s1 = rotate(high, 17) ^ rotate(high, 19) ^ high >>> 10;
134
+ words[index] = words[index - 16] + s0 + words[index - 7] + s1 | 0;
135
+ }
136
+ let [a, b, c, d, e, f, g, h] = hash;
137
+ for (let index = 0; index < 64; index += 1) {
138
+ const s1 = rotate(e, 6) ^ rotate(e, 11) ^ rotate(e, 25);
139
+ const choose = e & f ^ ~e & g;
140
+ const temp1 = h + s1 + choose + SHA256_K[index] + words[index] | 0;
141
+ const temp2 = (rotate(a, 2) ^ rotate(a, 13) ^ rotate(a, 22)) + (a & b ^ a & c ^ b & c) | 0;
142
+ h = g;
143
+ g = f;
144
+ f = e;
145
+ e = d + temp1 | 0;
146
+ d = c;
147
+ c = b;
148
+ b = a;
149
+ a = temp1 + temp2 | 0;
150
+ }
151
+ const results = [
152
+ a,
153
+ b,
154
+ c,
155
+ d,
156
+ e,
157
+ f,
158
+ g,
159
+ h
160
+ ];
161
+ for (let index = 0; index < 8; index += 1) hash[index] = hash[index] + results[index] | 0;
162
+ }
163
+ const digest = /* @__PURE__ */ new Uint8Array(32);
164
+ hash.forEach((word, index) => {
165
+ digest[index * 4] = word >>> 24 & 255;
166
+ digest[index * 4 + 1] = word >>> 16 & 255;
167
+ digest[index * 4 + 2] = word >>> 8 & 255;
168
+ digest[index * 4 + 3] = word & 255;
169
+ });
170
+ return digest;
171
+ };
172
+ /**
173
+ * A UUIDv7 derived from a key: its time field is `timestamp` and its random
174
+ * bits are the key's SHA-256, so the same key and time always give the same
175
+ * id, and a server that stores reports by id counts it once.
176
+ */
177
+ const createKeyedUUIDv7 = (key, timestamp) => formatUUIDv7(sha256(key).slice(0, 16), timestamp);
178
+ //#endregion
179
+ exports.createKeyedUUIDv7 = createKeyedUUIDv7;
180
+ exports.createUUIDv7 = createUUIDv7;
181
+ exports.sha256 = sha256;
@@ -0,0 +1,179 @@
1
+ //#region src/client/eventId.ts
2
+ const formatUUIDv7 = (bytes, timestamp) => {
3
+ let time = timestamp;
4
+ for (let index = 5; index >= 0; index -= 1) {
5
+ bytes[index] = time % 256;
6
+ time = Math.floor(time / 256);
7
+ }
8
+ bytes[6] = 112 | bytes[6] & 15;
9
+ bytes[8] = 128 | bytes[8] & 63;
10
+ const hex = Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0")).join("");
11
+ return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
12
+ };
13
+ /**
14
+ * Creates an RFC 9562 UUIDv7: a 48-bit millisecond timestamp, then random bits.
15
+ *
16
+ * `createUUIDv7` from `@hot-updater/plugin-core` calls `crypto.getRandomValues`
17
+ * unconditionally, but React Native has no Web Crypto unless the app installs a
18
+ * polyfill. An Insights event ID only has to tell one event from another so a
19
+ * retried POST is counted once, so `Math.random` is enough without one.
20
+ */
21
+ const createUUIDv7 = () => {
22
+ const bytes = /* @__PURE__ */ new Uint8Array(16);
23
+ const { crypto } = globalThis;
24
+ if (typeof crypto?.getRandomValues === "function") crypto.getRandomValues(bytes);
25
+ else for (let index = 0; index < bytes.length; index += 1) bytes[index] = Math.floor(Math.random() * 256);
26
+ return formatUUIDv7(bytes, Date.now());
27
+ };
28
+ const SHA256_K = [
29
+ 1116352408,
30
+ 1899447441,
31
+ 3049323471,
32
+ 3921009573,
33
+ 961987163,
34
+ 1508970993,
35
+ 2453635748,
36
+ 2870763221,
37
+ 3624381080,
38
+ 310598401,
39
+ 607225278,
40
+ 1426881987,
41
+ 1925078388,
42
+ 2162078206,
43
+ 2614888103,
44
+ 3248222580,
45
+ 3835390401,
46
+ 4022224774,
47
+ 264347078,
48
+ 604807628,
49
+ 770255983,
50
+ 1249150122,
51
+ 1555081692,
52
+ 1996064986,
53
+ 2554220882,
54
+ 2821834349,
55
+ 2952996808,
56
+ 3210313671,
57
+ 3336571891,
58
+ 3584528711,
59
+ 113926993,
60
+ 338241895,
61
+ 666307205,
62
+ 773529912,
63
+ 1294757372,
64
+ 1396182291,
65
+ 1695183700,
66
+ 1986661051,
67
+ 2177026350,
68
+ 2456956037,
69
+ 2730485921,
70
+ 2820302411,
71
+ 3259730800,
72
+ 3345764771,
73
+ 3516065817,
74
+ 3600352804,
75
+ 4094571909,
76
+ 275423344,
77
+ 430227734,
78
+ 506948616,
79
+ 659060556,
80
+ 883997877,
81
+ 958139571,
82
+ 1322822218,
83
+ 1537002063,
84
+ 1747873779,
85
+ 1955562222,
86
+ 2024104815,
87
+ 2227730452,
88
+ 2361852424,
89
+ 2428436474,
90
+ 2756734187,
91
+ 3204031479,
92
+ 3329325298
93
+ ];
94
+ const encodeUtf8 = (value) => {
95
+ const bytes = [];
96
+ for (const character of value) {
97
+ const code = character.codePointAt(0);
98
+ if (code < 128) bytes.push(code);
99
+ else if (code < 2048) bytes.push(192 | code >> 6, 128 | code & 63);
100
+ else if (code < 65536) bytes.push(224 | code >> 12, 128 | code >> 6 & 63, 128 | code & 63);
101
+ else bytes.push(240 | code >> 18, 128 | code >> 12 & 63, 128 | code >> 6 & 63, 128 | code & 63);
102
+ }
103
+ return bytes;
104
+ };
105
+ /** SHA-256 of a string's UTF-8 bytes, since React Native has no Web Crypto. */
106
+ const sha256 = (value) => {
107
+ const bytes = encodeUtf8(value);
108
+ const bitLength = bytes.length * 8;
109
+ bytes.push(128);
110
+ while (bytes.length % 64 !== 56) bytes.push(0);
111
+ for (let shift = 56; shift >= 0; shift -= 8) bytes.push(shift >= 32 ? Math.floor(bitLength / 2 ** shift) & 255 : bitLength >>> shift & 255);
112
+ const hash = [
113
+ 1779033703,
114
+ 3144134277,
115
+ 1013904242,
116
+ 2773480762,
117
+ 1359893119,
118
+ 2600822924,
119
+ 528734635,
120
+ 1541459225
121
+ ];
122
+ const words = Array.from({ length: 64 }, () => 0);
123
+ const rotate = (word, bits) => word >>> bits | word << 32 - bits;
124
+ for (let offset = 0; offset < bytes.length; offset += 64) {
125
+ for (let index = 0; index < 16; index += 1) {
126
+ const at = offset + index * 4;
127
+ words[index] = bytes[at] << 24 | bytes[at + 1] << 16 | bytes[at + 2] << 8 | bytes[at + 3];
128
+ }
129
+ for (let index = 16; index < 64; index += 1) {
130
+ const low = words[index - 15];
131
+ const high = words[index - 2];
132
+ const s0 = rotate(low, 7) ^ rotate(low, 18) ^ low >>> 3;
133
+ const s1 = rotate(high, 17) ^ rotate(high, 19) ^ high >>> 10;
134
+ words[index] = words[index - 16] + s0 + words[index - 7] + s1 | 0;
135
+ }
136
+ let [a, b, c, d, e, f, g, h] = hash;
137
+ for (let index = 0; index < 64; index += 1) {
138
+ const s1 = rotate(e, 6) ^ rotate(e, 11) ^ rotate(e, 25);
139
+ const choose = e & f ^ ~e & g;
140
+ const temp1 = h + s1 + choose + SHA256_K[index] + words[index] | 0;
141
+ const temp2 = (rotate(a, 2) ^ rotate(a, 13) ^ rotate(a, 22)) + (a & b ^ a & c ^ b & c) | 0;
142
+ h = g;
143
+ g = f;
144
+ f = e;
145
+ e = d + temp1 | 0;
146
+ d = c;
147
+ c = b;
148
+ b = a;
149
+ a = temp1 + temp2 | 0;
150
+ }
151
+ const results = [
152
+ a,
153
+ b,
154
+ c,
155
+ d,
156
+ e,
157
+ f,
158
+ g,
159
+ h
160
+ ];
161
+ for (let index = 0; index < 8; index += 1) hash[index] = hash[index] + results[index] | 0;
162
+ }
163
+ const digest = /* @__PURE__ */ new Uint8Array(32);
164
+ hash.forEach((word, index) => {
165
+ digest[index * 4] = word >>> 24 & 255;
166
+ digest[index * 4 + 1] = word >>> 16 & 255;
167
+ digest[index * 4 + 2] = word >>> 8 & 255;
168
+ digest[index * 4 + 3] = word & 255;
169
+ });
170
+ return digest;
171
+ };
172
+ /**
173
+ * A UUIDv7 derived from a key: its time field is `timestamp` and its random
174
+ * bits are the key's SHA-256, so the same key and time always give the same
175
+ * id, and a server that stores reports by id counts it once.
176
+ */
177
+ const createKeyedUUIDv7 = (key, timestamp) => formatUUIDv7(sha256(key).slice(0, 16), timestamp);
178
+ //#endregion
179
+ export { createKeyedUUIDv7, createUUIDv7, sha256 };
@@ -0,0 +1,234 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ const require_eventId = require("./eventId.cjs");
3
+ const require_sender = require("./sender.cjs");
4
+ const require_state = require("./state.cjs");
5
+ let _hot_updater_core = require("@hot-updater/core");
6
+ //#region src/client/index.ts
7
+ const normalizeUserId = (user) => {
8
+ if (user === null) return null;
9
+ const userId = String(user.userId).trim();
10
+ return userId.length === 0 ? null : userId;
11
+ };
12
+ /**
13
+ * Reports update adoption to a server that runs the `insights()` server
14
+ * plugin: launches (at most once a UTC day while nothing changes), downloads,
15
+ * applies, recoveries, and update failures. After the server answers that
16
+ * Insights is off, the plugin sends nothing for 24 hours, and for the rest
17
+ * of that runtime.
18
+ *
19
+ * @example
20
+ * ```ts
21
+ * import { HotUpdater } from "@hot-updater/react-native";
22
+ * import { insights } from "@hot-updater/react-native/plugins/insights";
23
+ *
24
+ * const analytics = insights();
25
+ * HotUpdater.init({ baseURL, plugins: [analytics] });
26
+ * analytics.setUser({ userId: "user-123" });
27
+ * ```
28
+ */
29
+ const insights = (options = {}) => {
30
+ let context = null;
31
+ let state = null;
32
+ /** A user set before setup, applied once the plugin has storage. */
33
+ let pendingUser = null;
34
+ /** A download in this runtime: a later no-change report would hide it. */
35
+ let didDownload = false;
36
+ let lastDownloadedSelection = null;
37
+ const pending = /* @__PURE__ */ new Map();
38
+ let send = null;
39
+ const createEvent = (movement, eventId) => {
40
+ if (context === null || state === null) return null;
41
+ const appVersion = context.appVersion;
42
+ if (appVersion === null) {
43
+ console.warn(`[HotUpdater] Insights needs the native app version; the ${movement.type} event was not sent.`);
44
+ return null;
45
+ }
46
+ const now = context.now();
47
+ const installId = context.installId;
48
+ const userId = state.readUserId();
49
+ const body = {
50
+ ...movement,
51
+ eventId: eventId?.(now, installId) ?? require_eventId.createUUIDv7(),
52
+ installId,
53
+ ...userId === null ? {} : { userId },
54
+ platform: context.platform,
55
+ appVersion,
56
+ cohort: context.getCohort(),
57
+ fingerprintHash: context.getFingerprintHash(),
58
+ sdkVersion: context.sdkVersion
59
+ };
60
+ const day = require_state.utcDay(now);
61
+ return {
62
+ body,
63
+ day,
64
+ report: movement.type === "UNCHANGED" || movement.type === "UPDATE_APPLIED" || movement.type === "RECOVERED" ? {
65
+ day,
66
+ channel: movement.channel,
67
+ appVersion,
68
+ bundleId: movement.toBundleId,
69
+ releaseId: movement.toReleaseId,
70
+ userId
71
+ } : null,
72
+ failureKey: null
73
+ };
74
+ };
75
+ const enqueue = (event) => {
76
+ if (event === null || send === null) return;
77
+ const { type, toBundleId } = event.body;
78
+ if (type === "UPDATE_DOWNLOADED" || type === "UPDATE_APPLIED") {
79
+ for (const queued of pending.values()) if (queued.body.type === "UPDATE_FAILED" && queued.body.toBundleId === toBundleId) queued.superseded = true;
80
+ }
81
+ pending.set(event.body, event);
82
+ send(event.body).catch((error) => {
83
+ console.warn(`[HotUpdater] Insights ${event.body.type} event was not delivered:`, error);
84
+ });
85
+ };
86
+ const reportLaunch = (channel, bundleId, releaseId, fromReleaseId) => {
87
+ if (didDownload) return;
88
+ enqueue(createEvent({
89
+ type: "UNCHANGED",
90
+ channel,
91
+ fromBundleId: null,
92
+ fromReleaseId,
93
+ toBundleId: bundleId,
94
+ toReleaseId: releaseId,
95
+ updateStrategy: null
96
+ }));
97
+ };
98
+ const onAppReady = (result) => {
99
+ if (result.status === "UNCHANGED") {
100
+ reportLaunch(result.channel, result.bundleId, result.releaseId, null);
101
+ return;
102
+ }
103
+ enqueue(createEvent({
104
+ type: result.status,
105
+ channel: result.channel,
106
+ fromBundleId: result.fromBundleId,
107
+ fromReleaseId: result.fromReleaseId,
108
+ toBundleId: result.toBundleId,
109
+ toReleaseId: result.toReleaseId,
110
+ updateStrategy: result.updateStrategy,
111
+ ...result.status === "RECOVERED" && result.previousProcessExit !== null ? { metadata: { previousProcessExit: result.previousProcessExit } } : {}
112
+ }));
113
+ };
114
+ const onUpdateCheck = (result) => {
115
+ if (result.status !== "UNCHANGED") return;
116
+ reportLaunch(result.channel, result.bundleId, result.releaseId, result.previousReleaseId === result.releaseId ? null : result.previousReleaseId);
117
+ };
118
+ const onBundleDownloaded = (info) => {
119
+ const selection = JSON.stringify([
120
+ info.channel,
121
+ info.toReleaseId,
122
+ info.toBundleId
123
+ ]);
124
+ if (lastDownloadedSelection === selection) return;
125
+ lastDownloadedSelection = selection;
126
+ didDownload = true;
127
+ enqueue(createEvent({
128
+ type: "UPDATE_DOWNLOADED",
129
+ channel: info.channel,
130
+ fromBundleId: info.fromBundleId,
131
+ fromReleaseId: info.fromReleaseId,
132
+ toBundleId: info.toBundleId,
133
+ toReleaseId: info.toReleaseId,
134
+ updateStrategy: info.updateStrategy,
135
+ metadata: {
136
+ delivery: info.delivery,
137
+ ...info.patchFallback ? { patchFallback: true } : {}
138
+ }
139
+ }));
140
+ };
141
+ const onUpdateError = (error) => {
142
+ if (error.stage === "check" && error.reason === "network") return;
143
+ const targetBundleId = error.targetBundleId ?? error.bundleId;
144
+ const failureKey = [
145
+ error.stage,
146
+ error.reason,
147
+ targetBundleId
148
+ ].join(" ");
149
+ const event = createEvent({
150
+ type: "UPDATE_FAILED",
151
+ channel: error.channel,
152
+ fromBundleId: error.bundleId,
153
+ fromReleaseId: error.releaseId,
154
+ toBundleId: targetBundleId,
155
+ toReleaseId: error.targetReleaseId ?? null,
156
+ updateStrategy: error.updateStrategy,
157
+ metadata: { failure: {
158
+ stage: error.stage,
159
+ reason: error.reason,
160
+ ...error.resource === void 0 ? {} : { resource: error.resource },
161
+ ...error.httpStatus === void 0 ? {} : { httpStatus: error.httpStatus },
162
+ ...error.transport === void 0 ? {} : { transport: error.transport },
163
+ ...error.originCode === void 0 ? {} : { originCode: error.originCode },
164
+ ...error.previousProcessExit === void 0 ? {} : { previousProcessExit: error.previousProcessExit }
165
+ } }
166
+ }, (now, installId) => require_eventId.createKeyedUUIDv7([
167
+ installId,
168
+ require_state.utcDay(now),
169
+ failureKey
170
+ ].join("\n"), require_state.utcDayStart(now)));
171
+ enqueue(event === null ? null : {
172
+ ...event,
173
+ failureKey
174
+ });
175
+ };
176
+ const admit = (body) => {
177
+ const event = pending.get(body);
178
+ if (event === void 0 || state === null || context === null) return false;
179
+ if (event.superseded === true || state.isPaused(context.now())) {
180
+ pending.delete(body);
181
+ return false;
182
+ }
183
+ const admitted = body.type === "UNCHANGED" ? event.report === null || !state.repeatsReport(event.report) : body.type === "UPDATE_FAILED" ? event.failureKey === null || !state.hasFailure(event.day, event.failureKey) : true;
184
+ if (!admitted) pending.delete(body);
185
+ return admitted;
186
+ };
187
+ const settle = (body, delivery) => {
188
+ const event = pending.get(body);
189
+ pending.delete(body);
190
+ if (event === void 0 || state === null || context === null) return;
191
+ if (delivery === "failed") return;
192
+ if (delivery === "disabled") {
193
+ state.pause(context.now());
194
+ return;
195
+ }
196
+ if (event.failureKey !== null) state.recordFailure(event.day, event.failureKey);
197
+ if (delivery === "refused") return;
198
+ state.resume();
199
+ if (event.report !== null) state.recordReport(event.report);
200
+ else state.forgetReport();
201
+ };
202
+ return (0, _hot_updater_core.defineClientPlugin)({
203
+ id: "insights",
204
+ setup(pluginContext) {
205
+ context = pluginContext;
206
+ state = require_state.createInsightsState(pluginContext.storage);
207
+ if (pendingUser !== null) {
208
+ state.writeUserId(pendingUser.userId);
209
+ pendingUser = null;
210
+ }
211
+ if (pluginContext.isDebugBuild && options.debug !== true) return;
212
+ send = require_sender.createInsightsEventSender(pluginContext.fetch, {
213
+ admit,
214
+ settle
215
+ });
216
+ return {
217
+ onAppReady,
218
+ onUpdateCheck,
219
+ onBundleDownloaded,
220
+ onUpdateError
221
+ };
222
+ },
223
+ setUser(user) {
224
+ const userId = normalizeUserId(user);
225
+ if (state === null) {
226
+ pendingUser = { userId };
227
+ return;
228
+ }
229
+ state.writeUserId(userId);
230
+ }
231
+ });
232
+ };
233
+ //#endregion
234
+ exports.insights = insights;
@@ -0,0 +1,42 @@
1
+ import { HotUpdaterClientPlugin } from "@hot-updater/core";
2
+ //#region src/client/index.d.ts
3
+ interface InsightsOptions {
4
+ /**
5
+ * Also report from debug builds (`__DEV__`). Defaults to `false`, so
6
+ * development sessions stay out of production Insights.
7
+ */
8
+ readonly debug?: boolean;
9
+ }
10
+ /** The app's own id for the signed-in user, attached to later reports. */
11
+ interface InsightsUser {
12
+ readonly userId: string | number;
13
+ }
14
+ interface InsightsPlugin extends HotUpdaterClientPlugin {
15
+ readonly id: "insights";
16
+ /**
17
+ * Attaches the signed-in user's id to later reports, or clears it with
18
+ * `null`. It persists on the device, sends nothing by itself, and can be
19
+ * called before `HotUpdater.init`.
20
+ */
21
+ setUser(user: InsightsUser | null): void;
22
+ }
23
+ /**
24
+ * Reports update adoption to a server that runs the `insights()` server
25
+ * plugin: launches (at most once a UTC day while nothing changes), downloads,
26
+ * applies, recoveries, and update failures. After the server answers that
27
+ * Insights is off, the plugin sends nothing for 24 hours, and for the rest
28
+ * of that runtime.
29
+ *
30
+ * @example
31
+ * ```ts
32
+ * import { HotUpdater } from "@hot-updater/react-native";
33
+ * import { insights } from "@hot-updater/react-native/plugins/insights";
34
+ *
35
+ * const analytics = insights();
36
+ * HotUpdater.init({ baseURL, plugins: [analytics] });
37
+ * analytics.setUser({ userId: "user-123" });
38
+ * ```
39
+ */
40
+ declare const insights: (options?: InsightsOptions) => InsightsPlugin;
41
+ //#endregion
42
+ export { InsightsOptions, InsightsPlugin, InsightsUser, insights };