@mushi-mushi/core 1.20.0 → 1.20.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.
package/dist/index.js CHANGED
@@ -40,6 +40,47 @@ function formatBytes(n) {
40
40
  return `${(n / (1024 * 1024)).toFixed(2)} MB`;
41
41
  }
42
42
 
43
+ // src/digest.ts
44
+ function getSubtle() {
45
+ try {
46
+ const g = globalThis;
47
+ const crypto2 = g["crypto"];
48
+ if (crypto2?.subtle) return crypto2.subtle;
49
+ } catch {
50
+ }
51
+ return null;
52
+ }
53
+ function bytesToHex(bytes) {
54
+ return Array.from(bytes).map((b) => b.toString(16).padStart(2, "0")).join("");
55
+ }
56
+ async function sha256Hex(value) {
57
+ const subtle = getSubtle();
58
+ if (subtle) {
59
+ const buffer = await subtle.digest("SHA-256", new TextEncoder().encode(value));
60
+ return bytesToHex(new Uint8Array(buffer));
61
+ }
62
+ const { sha256 } = await import('@noble/hashes/sha2.js');
63
+ return bytesToHex(sha256(new TextEncoder().encode(value)));
64
+ }
65
+ async function hmacSha256Hex(secret, value) {
66
+ const subtle = getSubtle();
67
+ if (subtle) {
68
+ const key = await subtle.importKey(
69
+ "raw",
70
+ new TextEncoder().encode(secret),
71
+ { name: "HMAC", hash: "SHA-256" },
72
+ false,
73
+ ["sign"]
74
+ );
75
+ const buffer = await subtle.sign("HMAC", key, new TextEncoder().encode(value));
76
+ return bytesToHex(new Uint8Array(buffer));
77
+ }
78
+ const { hmac } = await import('@noble/hashes/hmac.js');
79
+ const { sha256 } = await import('@noble/hashes/sha2.js');
80
+ const result = hmac(sha256, new TextEncoder().encode(secret), new TextEncoder().encode(value));
81
+ return bytesToHex(result);
82
+ }
83
+
43
84
  // src/api-client.ts
44
85
  var _credWarningEmitted = false;
45
86
  var MUSHI_USER_TOKEN_HEADER = "X-Mushi-User-Token";
@@ -409,21 +450,6 @@ function createApiClient(options) {
409
450
  }
410
451
  };
411
452
  }
412
- async function sha256Hex(value) {
413
- const buffer = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(value));
414
- return Array.from(new Uint8Array(buffer)).map((b) => b.toString(16).padStart(2, "0")).join("");
415
- }
416
- async function hmacSha256Hex(secret, value) {
417
- const key = await crypto.subtle.importKey(
418
- "raw",
419
- new TextEncoder().encode(secret),
420
- { name: "HMAC", hash: "SHA-256" },
421
- false,
422
- ["sign"]
423
- );
424
- const buffer = await crypto.subtle.sign("HMAC", key, new TextEncoder().encode(value));
425
- return Array.from(new Uint8Array(buffer)).map((b) => b.toString(16).padStart(2, "0")).join("");
426
- }
427
453
  function sleep(ms) {
428
454
  return new Promise((resolve) => setTimeout(resolve, ms));
429
455
  }