@couch-kit/core 0.8.2 → 0.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/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @couch-kit/core
2
2
 
3
+ ## 0.8.3
4
+
5
+ ### Patch Changes
6
+
7
+ - [#33](https://github.com/faluciano/react-native-couch-kit/pull/33) [`8773b1d`](https://github.com/faluciano/react-native-couch-kit/commit/8773b1d8687247f3f77faf116c041a26c634c8d0) Thanks [@faluciano](https://github.com/faluciano)! - Access `crypto` via `globalThis.crypto` to avoid `ReferenceError` in React Native's Hermes engine where `typeof crypto` throws instead of returning `"undefined"`. Fixes `derivePlayerId` and `generateId` on Android TV.
8
+
3
9
  ## 0.8.2
4
10
 
5
11
  ### Patch Changes
package/dist/index.js CHANGED
@@ -172,12 +172,12 @@ var DEFAULT_DISCONNECT_TIMEOUT = 5 * 60 * 1000;
172
172
  var KEEPALIVE_INTERVAL = 30000;
173
173
  var KEEPALIVE_TIMEOUT = 1e4;
174
174
  function generateId() {
175
- if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
176
- return crypto.randomUUID();
175
+ if (typeof globalThis.crypto !== "undefined" && typeof globalThis.crypto.randomUUID === "function") {
176
+ return globalThis.crypto.randomUUID();
177
177
  }
178
- if (typeof crypto !== "undefined" && typeof crypto.getRandomValues === "function") {
178
+ if (typeof globalThis.crypto !== "undefined" && typeof globalThis.crypto.getRandomValues === "function") {
179
179
  const bytes = new Uint8Array(16);
180
- crypto.getRandomValues(bytes);
180
+ globalThis.crypto.getRandomValues(bytes);
181
181
  return Array.from(bytes).map((b2) => b2.toString(16).padStart(2, "0")).join("");
182
182
  }
183
183
  const a = Math.random().toString(36).substring(2, 15);
@@ -189,9 +189,9 @@ function isValidSecret(secret) {
189
189
  return hex.length >= 32 && /^[0-9a-f]+$/i.test(hex);
190
190
  }
191
191
  async function derivePlayerId(secret) {
192
- if (typeof crypto !== "undefined" && typeof crypto.subtle?.digest === "function") {
192
+ if (typeof globalThis.crypto !== "undefined" && typeof globalThis.crypto.subtle?.digest === "function") {
193
193
  const data = new TextEncoder().encode(secret);
194
- const hash = await crypto.subtle.digest("SHA-256", data);
194
+ const hash = await globalThis.crypto.subtle.digest("SHA-256", data);
195
195
  return Array.from(new Uint8Array(hash)).map((b) => b.toString(16).padStart(2, "0")).join("").slice(0, 16);
196
196
  }
197
197
  return derivePlayerIdLegacy(secret);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@couch-kit/core",
3
- "version": "0.8.2",
3
+ "version": "0.8.3",
4
4
  "publishConfig": {
5
5
  "access": "public",
6
6
  "provenance": true
package/src/constants.ts CHANGED
@@ -44,19 +44,19 @@ export const KEEPALIVE_TIMEOUT = 10000;
44
44
  */
45
45
  export function generateId(): string {
46
46
  if (
47
- typeof crypto !== "undefined" &&
48
- typeof crypto.randomUUID === "function"
47
+ typeof globalThis.crypto !== "undefined" &&
48
+ typeof globalThis.crypto.randomUUID === "function"
49
49
  ) {
50
- return crypto.randomUUID();
50
+ return globalThis.crypto.randomUUID();
51
51
  }
52
52
 
53
53
  // Fallback: 32 hex chars from crypto.getRandomValues
54
54
  if (
55
- typeof crypto !== "undefined" &&
56
- typeof crypto.getRandomValues === "function"
55
+ typeof globalThis.crypto !== "undefined" &&
56
+ typeof globalThis.crypto.getRandomValues === "function"
57
57
  ) {
58
58
  const bytes = new Uint8Array(16);
59
- crypto.getRandomValues(bytes);
59
+ globalThis.crypto.getRandomValues(bytes);
60
60
  return Array.from(bytes)
61
61
  .map((b) => b.toString(16).padStart(2, "0"))
62
62
  .join("");
@@ -87,11 +87,11 @@ export function isValidSecret(secret: string): boolean {
87
87
  */
88
88
  export async function derivePlayerId(secret: string): Promise<string> {
89
89
  if (
90
- typeof crypto !== "undefined" &&
91
- typeof crypto.subtle?.digest === "function"
90
+ typeof globalThis.crypto !== "undefined" &&
91
+ typeof globalThis.crypto.subtle?.digest === "function"
92
92
  ) {
93
93
  const data = new TextEncoder().encode(secret);
94
- const hash = await crypto.subtle.digest("SHA-256", data);
94
+ const hash = await globalThis.crypto.subtle.digest("SHA-256", data);
95
95
  return Array.from(new Uint8Array(hash))
96
96
  .map((b) => b.toString(16).padStart(2, "0"))
97
97
  .join("")