@alibarbar/common 1.1.1 → 1.1.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/crypto.cjs +5 -1
- package/dist/crypto.js +5 -1
- package/dist/index.cjs +110 -67
- package/dist/index.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +110 -68
- package/dist/storage.cjs +110 -67
- package/dist/storage.d.mts +17 -29
- package/dist/storage.d.ts +17 -29
- package/dist/storage.js +110 -68
- package/package.json +1 -1
package/dist/crypto.cjs
CHANGED
|
@@ -76,7 +76,11 @@ async function rsaEncrypt(data, publicKey) {
|
|
|
76
76
|
}
|
|
77
77
|
const encoder = new TextEncoder();
|
|
78
78
|
const dataBuffer = encoder.encode(data);
|
|
79
|
-
const
|
|
79
|
+
const algo = publicKey.algorithm;
|
|
80
|
+
const modulusLength = typeof algo.modulusLength === "number" ? algo.modulusLength : 2048;
|
|
81
|
+
const hashName = typeof algo.hash === "object" && "name" in algo.hash ? algo.hash.name : "SHA-256";
|
|
82
|
+
const hashLength = hashName === "SHA-1" ? 20 : 32;
|
|
83
|
+
const maxChunkSize = Math.max(Math.floor(modulusLength / 8 - 2 * hashLength - 2), 1);
|
|
80
84
|
const chunks = [];
|
|
81
85
|
for (let i = 0; i < dataBuffer.length; i += maxChunkSize) {
|
|
82
86
|
const chunk = dataBuffer.slice(i, i + maxChunkSize);
|
package/dist/crypto.js
CHANGED
|
@@ -74,7 +74,11 @@ async function rsaEncrypt(data, publicKey) {
|
|
|
74
74
|
}
|
|
75
75
|
const encoder = new TextEncoder();
|
|
76
76
|
const dataBuffer = encoder.encode(data);
|
|
77
|
-
const
|
|
77
|
+
const algo = publicKey.algorithm;
|
|
78
|
+
const modulusLength = typeof algo.modulusLength === "number" ? algo.modulusLength : 2048;
|
|
79
|
+
const hashName = typeof algo.hash === "object" && "name" in algo.hash ? algo.hash.name : "SHA-256";
|
|
80
|
+
const hashLength = hashName === "SHA-1" ? 20 : 32;
|
|
81
|
+
const maxChunkSize = Math.max(Math.floor(modulusLength / 8 - 2 * hashLength - 2), 1);
|
|
78
82
|
const chunks = [];
|
|
79
83
|
for (let i = 0; i < dataBuffer.length; i += maxChunkSize) {
|
|
80
84
|
const chunk = dataBuffer.slice(i, i + maxChunkSize);
|
package/dist/index.cjs
CHANGED
|
@@ -1577,7 +1577,11 @@ async function rsaEncrypt(data, publicKey) {
|
|
|
1577
1577
|
}
|
|
1578
1578
|
const encoder = new TextEncoder();
|
|
1579
1579
|
const dataBuffer = encoder.encode(data);
|
|
1580
|
-
const
|
|
1580
|
+
const algo = publicKey.algorithm;
|
|
1581
|
+
const modulusLength = typeof algo.modulusLength === "number" ? algo.modulusLength : 2048;
|
|
1582
|
+
const hashName = typeof algo.hash === "object" && "name" in algo.hash ? algo.hash.name : "SHA-256";
|
|
1583
|
+
const hashLength = hashName === "SHA-1" ? 20 : 32;
|
|
1584
|
+
const maxChunkSize = Math.max(Math.floor(modulusLength / 8 - 2 * hashLength - 2), 1);
|
|
1581
1585
|
const chunks = [];
|
|
1582
1586
|
for (let i = 0; i < dataBuffer.length; i += maxChunkSize) {
|
|
1583
1587
|
const chunk2 = dataBuffer.slice(i, i + maxChunkSize);
|
|
@@ -1796,6 +1800,7 @@ var initOptions = {
|
|
|
1796
1800
|
var actualKeyStorageKey = null;
|
|
1797
1801
|
var keyUsageCount = 0;
|
|
1798
1802
|
var initializationPromise = null;
|
|
1803
|
+
var KEY_STORAGE_KEY_REGISTRY = "__SECURE_STORAGE_KEY__";
|
|
1799
1804
|
function generateKeyStorageKey() {
|
|
1800
1805
|
return `_sk_${generateRandomString(32)}_${Date.now()}`;
|
|
1801
1806
|
}
|
|
@@ -1814,8 +1819,18 @@ async function initializeStorageKeys(options = {}) {
|
|
|
1814
1819
|
initOptions = { ...initOptions, ...options };
|
|
1815
1820
|
if (initOptions.keyStorageKey) {
|
|
1816
1821
|
actualKeyStorageKey = initOptions.keyStorageKey;
|
|
1817
|
-
} else {
|
|
1818
|
-
|
|
1822
|
+
} else if (!actualKeyStorageKey) {
|
|
1823
|
+
if (typeof window !== "undefined" && window.localStorage) {
|
|
1824
|
+
const savedKeyName = window.localStorage.getItem(KEY_STORAGE_KEY_REGISTRY);
|
|
1825
|
+
if (savedKeyName) {
|
|
1826
|
+
actualKeyStorageKey = savedKeyName;
|
|
1827
|
+
} else {
|
|
1828
|
+
actualKeyStorageKey = generateKeyStorageKey();
|
|
1829
|
+
window.localStorage.setItem(KEY_STORAGE_KEY_REGISTRY, actualKeyStorageKey);
|
|
1830
|
+
}
|
|
1831
|
+
} else {
|
|
1832
|
+
actualKeyStorageKey = generateKeyStorageKey();
|
|
1833
|
+
}
|
|
1819
1834
|
}
|
|
1820
1835
|
if (initOptions.persistKeys && typeof window !== "undefined" && window.localStorage) {
|
|
1821
1836
|
try {
|
|
@@ -2413,73 +2428,100 @@ var cookie = {
|
|
|
2413
2428
|
return cookies;
|
|
2414
2429
|
}
|
|
2415
2430
|
};
|
|
2416
|
-
|
|
2417
|
-
|
|
2418
|
-
|
|
2419
|
-
|
|
2420
|
-
|
|
2421
|
-
|
|
2422
|
-
|
|
2423
|
-
|
|
2424
|
-
|
|
2425
|
-
|
|
2426
|
-
|
|
2427
|
-
|
|
2428
|
-
|
|
2429
|
-
|
|
2430
|
-
|
|
2431
|
-
|
|
2432
|
-
|
|
2431
|
+
function createBackendAdapter(type) {
|
|
2432
|
+
switch (type) {
|
|
2433
|
+
case "local":
|
|
2434
|
+
return {
|
|
2435
|
+
async set(key, value, options) {
|
|
2436
|
+
await localStorage.set(key, value, {
|
|
2437
|
+
expiry: options?.expiry,
|
|
2438
|
+
publicKey: options?.publicKey
|
|
2439
|
+
});
|
|
2440
|
+
},
|
|
2441
|
+
async get(key, options) {
|
|
2442
|
+
return localStorage.get(key, {
|
|
2443
|
+
defaultValue: options?.defaultValue,
|
|
2444
|
+
privateKey: options?.privateKey
|
|
2445
|
+
});
|
|
2446
|
+
},
|
|
2447
|
+
remove(key) {
|
|
2448
|
+
localStorage.remove(key);
|
|
2449
|
+
},
|
|
2450
|
+
clear() {
|
|
2451
|
+
localStorage.clear();
|
|
2452
|
+
},
|
|
2453
|
+
keys() {
|
|
2454
|
+
return localStorage.keys();
|
|
2433
2455
|
}
|
|
2434
|
-
}
|
|
2435
|
-
|
|
2436
|
-
|
|
2437
|
-
|
|
2438
|
-
|
|
2439
|
-
|
|
2440
|
-
|
|
2441
|
-
|
|
2442
|
-
|
|
2443
|
-
|
|
2444
|
-
|
|
2445
|
-
|
|
2446
|
-
|
|
2447
|
-
|
|
2448
|
-
|
|
2449
|
-
|
|
2450
|
-
|
|
2451
|
-
|
|
2452
|
-
|
|
2453
|
-
|
|
2454
|
-
|
|
2455
|
-
|
|
2456
|
-
|
|
2457
|
-
|
|
2458
|
-
|
|
2459
|
-
|
|
2460
|
-
|
|
2461
|
-
|
|
2462
|
-
|
|
2463
|
-
|
|
2464
|
-
|
|
2465
|
-
|
|
2466
|
-
|
|
2467
|
-
|
|
2468
|
-
keys() {
|
|
2469
|
-
const localKeys = localStorage.keys();
|
|
2470
|
-
const sessionKeys = [];
|
|
2471
|
-
if (typeof window !== "undefined" && window.sessionStorage) {
|
|
2472
|
-
try {
|
|
2473
|
-
for (let i = 0; i < window.sessionStorage.length; i++) {
|
|
2474
|
-
const key = window.sessionStorage.key(i);
|
|
2475
|
-
if (key) sessionKeys.push(key);
|
|
2456
|
+
};
|
|
2457
|
+
case "session":
|
|
2458
|
+
return {
|
|
2459
|
+
async set(key, value, options) {
|
|
2460
|
+
await sessionStorage.set(key, value, {
|
|
2461
|
+
publicKey: options?.publicKey
|
|
2462
|
+
});
|
|
2463
|
+
},
|
|
2464
|
+
async get(key, options) {
|
|
2465
|
+
return sessionStorage.get(key, {
|
|
2466
|
+
defaultValue: options?.defaultValue,
|
|
2467
|
+
privateKey: options?.privateKey
|
|
2468
|
+
});
|
|
2469
|
+
},
|
|
2470
|
+
remove(key) {
|
|
2471
|
+
sessionStorage.remove(key);
|
|
2472
|
+
},
|
|
2473
|
+
clear() {
|
|
2474
|
+
sessionStorage.clear();
|
|
2475
|
+
},
|
|
2476
|
+
keys() {
|
|
2477
|
+
if (typeof window === "undefined" || !window.sessionStorage) return [];
|
|
2478
|
+
const keys2 = [];
|
|
2479
|
+
try {
|
|
2480
|
+
for (let i = 0; i < window.sessionStorage.length; i++) {
|
|
2481
|
+
const key = window.sessionStorage.key(i);
|
|
2482
|
+
if (key) keys2.push(key);
|
|
2483
|
+
}
|
|
2484
|
+
} catch (error) {
|
|
2485
|
+
if (!initOptions.isProduction && typeof console !== "undefined" && console.error) {
|
|
2486
|
+
console.error("sessionStorage.keys error");
|
|
2487
|
+
}
|
|
2488
|
+
}
|
|
2489
|
+
return keys2;
|
|
2476
2490
|
}
|
|
2477
|
-
}
|
|
2478
|
-
|
|
2479
|
-
|
|
2480
|
-
|
|
2491
|
+
};
|
|
2492
|
+
case "cookie":
|
|
2493
|
+
return {
|
|
2494
|
+
async set(key, value) {
|
|
2495
|
+
cookie.set(key, String(value));
|
|
2496
|
+
},
|
|
2497
|
+
async get(key, options) {
|
|
2498
|
+
const value = cookie.get(key);
|
|
2499
|
+
if (value === void 0) return options?.defaultValue;
|
|
2500
|
+
try {
|
|
2501
|
+
return JSON.parse(value);
|
|
2502
|
+
} catch {
|
|
2503
|
+
return value;
|
|
2504
|
+
}
|
|
2505
|
+
},
|
|
2506
|
+
remove(key) {
|
|
2507
|
+
cookie.remove(key);
|
|
2508
|
+
},
|
|
2509
|
+
clear() {
|
|
2510
|
+
const all = cookie.getAll();
|
|
2511
|
+
Object.keys(all).forEach((k) => cookie.remove(k));
|
|
2512
|
+
},
|
|
2513
|
+
keys() {
|
|
2514
|
+
return Object.keys(cookie.getAll());
|
|
2515
|
+
}
|
|
2516
|
+
};
|
|
2517
|
+
default:
|
|
2518
|
+
return createBackendAdapter("local");
|
|
2481
2519
|
}
|
|
2482
|
-
}
|
|
2520
|
+
}
|
|
2521
|
+
function createSecureStorage(type = "local") {
|
|
2522
|
+
return createBackendAdapter(type);
|
|
2523
|
+
}
|
|
2524
|
+
var secureStorage = createSecureStorage("local");
|
|
2483
2525
|
|
|
2484
2526
|
// src/browser/network/index.ts
|
|
2485
2527
|
async function fetchWithRetry(url, options = {}, retryCount = 3, retryDelay = 1e3) {
|
|
@@ -4388,6 +4430,7 @@ exports.computeHMAC = computeHMAC;
|
|
|
4388
4430
|
exports.contrast = contrast;
|
|
4389
4431
|
exports.cookie = cookie;
|
|
4390
4432
|
exports.copyToClipboard = copyToClipboard;
|
|
4433
|
+
exports.createSecureStorage = createSecureStorage;
|
|
4391
4434
|
exports.createTracker = createTracker;
|
|
4392
4435
|
exports.createTranslator = createTranslator;
|
|
4393
4436
|
exports.createUploader = createUploader;
|
package/dist/index.d.mts
CHANGED
|
@@ -9,7 +9,7 @@ export { HSL, RGB, contrast, darken, hexToRgb, hslToRgb, lighten, mix, rgbToHex,
|
|
|
9
9
|
export { CurrencyCode, Locale, TranslationDictionary, createTranslator, formatCurrencyI18n, formatDateI18n, formatNumberI18n, formatRelativeTime, getLocale, pluralize, translate } from './i18n.mjs';
|
|
10
10
|
export { calculateBlobMD5, calculateFileMD5, formatFileSize, getFileExtension, getFileNameWithoutExtension, splitFileIntoChunks } from './file.mjs';
|
|
11
11
|
export { A as ApiResponse, h as ChunkInfo, b as ChunkUploadResponse, C as ChunkUploader, d as CompleteUploadResponse, U as UploadInitRequest, a as UploadInitResponse, f as UploadOptions, e as UploadProgress, g as UploadStatus, c as createUploader, u as uploadFile } from './index-DchqyDBQ.mjs';
|
|
12
|
-
export { StorageInitOptions, StorageOptions, clearPersistedKeys, cookie, getKeyUsageCount, getStorageKeyPair, initializeStorageKeys, resetKeyUsageCount, secureStorage, setStorageKeyPair } from './storage.mjs';
|
|
12
|
+
export { SecureStorageInstance, StorageInitOptions, StorageOptions, clearPersistedKeys, cookie, createSecureStorage, getKeyUsageCount, getStorageKeyPair, initializeStorageKeys, resetKeyUsageCount, secureStorage, setStorageKeyPair } from './storage.mjs';
|
|
13
13
|
export { checkOnline, downloadFile, fetchWithRetry, fetchWithTimeout, request } from './network.mjs';
|
|
14
14
|
export { $, $$, addClass, copyToClipboard, getElementOffset, getScrollPosition, getStyle, isInViewport, removeClass, scrollTo, setStyle, toggleClass } from './dom.mjs';
|
|
15
15
|
export { csvToJson, jsonToCsv, jsonToXml, jsonToYaml, xmlToJson, yamlToJson } from './transform.mjs';
|
package/dist/index.d.ts
CHANGED
|
@@ -9,7 +9,7 @@ export { HSL, RGB, contrast, darken, hexToRgb, hslToRgb, lighten, mix, rgbToHex,
|
|
|
9
9
|
export { CurrencyCode, Locale, TranslationDictionary, createTranslator, formatCurrencyI18n, formatDateI18n, formatNumberI18n, formatRelativeTime, getLocale, pluralize, translate } from './i18n.js';
|
|
10
10
|
export { calculateBlobMD5, calculateFileMD5, formatFileSize, getFileExtension, getFileNameWithoutExtension, splitFileIntoChunks } from './file.js';
|
|
11
11
|
export { A as ApiResponse, h as ChunkInfo, b as ChunkUploadResponse, C as ChunkUploader, d as CompleteUploadResponse, U as UploadInitRequest, a as UploadInitResponse, f as UploadOptions, e as UploadProgress, g as UploadStatus, c as createUploader, u as uploadFile } from './index-DchqyDBQ.js';
|
|
12
|
-
export { StorageInitOptions, StorageOptions, clearPersistedKeys, cookie, getKeyUsageCount, getStorageKeyPair, initializeStorageKeys, resetKeyUsageCount, secureStorage, setStorageKeyPair } from './storage.js';
|
|
12
|
+
export { SecureStorageInstance, StorageInitOptions, StorageOptions, clearPersistedKeys, cookie, createSecureStorage, getKeyUsageCount, getStorageKeyPair, initializeStorageKeys, resetKeyUsageCount, secureStorage, setStorageKeyPair } from './storage.js';
|
|
13
13
|
export { checkOnline, downloadFile, fetchWithRetry, fetchWithTimeout, request } from './network.js';
|
|
14
14
|
export { $, $$, addClass, copyToClipboard, getElementOffset, getScrollPosition, getStyle, isInViewport, removeClass, scrollTo, setStyle, toggleClass } from './dom.js';
|
|
15
15
|
export { csvToJson, jsonToCsv, jsonToXml, jsonToYaml, xmlToJson, yamlToJson } from './transform.js';
|
package/dist/index.js
CHANGED
|
@@ -1570,7 +1570,11 @@ async function rsaEncrypt(data, publicKey) {
|
|
|
1570
1570
|
}
|
|
1571
1571
|
const encoder = new TextEncoder();
|
|
1572
1572
|
const dataBuffer = encoder.encode(data);
|
|
1573
|
-
const
|
|
1573
|
+
const algo = publicKey.algorithm;
|
|
1574
|
+
const modulusLength = typeof algo.modulusLength === "number" ? algo.modulusLength : 2048;
|
|
1575
|
+
const hashName = typeof algo.hash === "object" && "name" in algo.hash ? algo.hash.name : "SHA-256";
|
|
1576
|
+
const hashLength = hashName === "SHA-1" ? 20 : 32;
|
|
1577
|
+
const maxChunkSize = Math.max(Math.floor(modulusLength / 8 - 2 * hashLength - 2), 1);
|
|
1574
1578
|
const chunks = [];
|
|
1575
1579
|
for (let i = 0; i < dataBuffer.length; i += maxChunkSize) {
|
|
1576
1580
|
const chunk2 = dataBuffer.slice(i, i + maxChunkSize);
|
|
@@ -1789,6 +1793,7 @@ var initOptions = {
|
|
|
1789
1793
|
var actualKeyStorageKey = null;
|
|
1790
1794
|
var keyUsageCount = 0;
|
|
1791
1795
|
var initializationPromise = null;
|
|
1796
|
+
var KEY_STORAGE_KEY_REGISTRY = "__SECURE_STORAGE_KEY__";
|
|
1792
1797
|
function generateKeyStorageKey() {
|
|
1793
1798
|
return `_sk_${generateRandomString(32)}_${Date.now()}`;
|
|
1794
1799
|
}
|
|
@@ -1807,8 +1812,18 @@ async function initializeStorageKeys(options = {}) {
|
|
|
1807
1812
|
initOptions = { ...initOptions, ...options };
|
|
1808
1813
|
if (initOptions.keyStorageKey) {
|
|
1809
1814
|
actualKeyStorageKey = initOptions.keyStorageKey;
|
|
1810
|
-
} else {
|
|
1811
|
-
|
|
1815
|
+
} else if (!actualKeyStorageKey) {
|
|
1816
|
+
if (typeof window !== "undefined" && window.localStorage) {
|
|
1817
|
+
const savedKeyName = window.localStorage.getItem(KEY_STORAGE_KEY_REGISTRY);
|
|
1818
|
+
if (savedKeyName) {
|
|
1819
|
+
actualKeyStorageKey = savedKeyName;
|
|
1820
|
+
} else {
|
|
1821
|
+
actualKeyStorageKey = generateKeyStorageKey();
|
|
1822
|
+
window.localStorage.setItem(KEY_STORAGE_KEY_REGISTRY, actualKeyStorageKey);
|
|
1823
|
+
}
|
|
1824
|
+
} else {
|
|
1825
|
+
actualKeyStorageKey = generateKeyStorageKey();
|
|
1826
|
+
}
|
|
1812
1827
|
}
|
|
1813
1828
|
if (initOptions.persistKeys && typeof window !== "undefined" && window.localStorage) {
|
|
1814
1829
|
try {
|
|
@@ -2406,73 +2421,100 @@ var cookie = {
|
|
|
2406
2421
|
return cookies;
|
|
2407
2422
|
}
|
|
2408
2423
|
};
|
|
2409
|
-
|
|
2410
|
-
|
|
2411
|
-
|
|
2412
|
-
|
|
2413
|
-
|
|
2414
|
-
|
|
2415
|
-
|
|
2416
|
-
|
|
2417
|
-
|
|
2418
|
-
|
|
2419
|
-
|
|
2420
|
-
|
|
2421
|
-
|
|
2422
|
-
|
|
2423
|
-
|
|
2424
|
-
|
|
2425
|
-
|
|
2424
|
+
function createBackendAdapter(type) {
|
|
2425
|
+
switch (type) {
|
|
2426
|
+
case "local":
|
|
2427
|
+
return {
|
|
2428
|
+
async set(key, value, options) {
|
|
2429
|
+
await localStorage.set(key, value, {
|
|
2430
|
+
expiry: options?.expiry,
|
|
2431
|
+
publicKey: options?.publicKey
|
|
2432
|
+
});
|
|
2433
|
+
},
|
|
2434
|
+
async get(key, options) {
|
|
2435
|
+
return localStorage.get(key, {
|
|
2436
|
+
defaultValue: options?.defaultValue,
|
|
2437
|
+
privateKey: options?.privateKey
|
|
2438
|
+
});
|
|
2439
|
+
},
|
|
2440
|
+
remove(key) {
|
|
2441
|
+
localStorage.remove(key);
|
|
2442
|
+
},
|
|
2443
|
+
clear() {
|
|
2444
|
+
localStorage.clear();
|
|
2445
|
+
},
|
|
2446
|
+
keys() {
|
|
2447
|
+
return localStorage.keys();
|
|
2426
2448
|
}
|
|
2427
|
-
}
|
|
2428
|
-
|
|
2429
|
-
|
|
2430
|
-
|
|
2431
|
-
|
|
2432
|
-
|
|
2433
|
-
|
|
2434
|
-
|
|
2435
|
-
|
|
2436
|
-
|
|
2437
|
-
|
|
2438
|
-
|
|
2439
|
-
|
|
2440
|
-
|
|
2441
|
-
|
|
2442
|
-
|
|
2443
|
-
|
|
2444
|
-
|
|
2445
|
-
|
|
2446
|
-
|
|
2447
|
-
|
|
2448
|
-
|
|
2449
|
-
|
|
2450
|
-
|
|
2451
|
-
|
|
2452
|
-
|
|
2453
|
-
|
|
2454
|
-
|
|
2455
|
-
|
|
2456
|
-
|
|
2457
|
-
|
|
2458
|
-
|
|
2459
|
-
|
|
2460
|
-
|
|
2461
|
-
keys() {
|
|
2462
|
-
const localKeys = localStorage.keys();
|
|
2463
|
-
const sessionKeys = [];
|
|
2464
|
-
if (typeof window !== "undefined" && window.sessionStorage) {
|
|
2465
|
-
try {
|
|
2466
|
-
for (let i = 0; i < window.sessionStorage.length; i++) {
|
|
2467
|
-
const key = window.sessionStorage.key(i);
|
|
2468
|
-
if (key) sessionKeys.push(key);
|
|
2449
|
+
};
|
|
2450
|
+
case "session":
|
|
2451
|
+
return {
|
|
2452
|
+
async set(key, value, options) {
|
|
2453
|
+
await sessionStorage.set(key, value, {
|
|
2454
|
+
publicKey: options?.publicKey
|
|
2455
|
+
});
|
|
2456
|
+
},
|
|
2457
|
+
async get(key, options) {
|
|
2458
|
+
return sessionStorage.get(key, {
|
|
2459
|
+
defaultValue: options?.defaultValue,
|
|
2460
|
+
privateKey: options?.privateKey
|
|
2461
|
+
});
|
|
2462
|
+
},
|
|
2463
|
+
remove(key) {
|
|
2464
|
+
sessionStorage.remove(key);
|
|
2465
|
+
},
|
|
2466
|
+
clear() {
|
|
2467
|
+
sessionStorage.clear();
|
|
2468
|
+
},
|
|
2469
|
+
keys() {
|
|
2470
|
+
if (typeof window === "undefined" || !window.sessionStorage) return [];
|
|
2471
|
+
const keys2 = [];
|
|
2472
|
+
try {
|
|
2473
|
+
for (let i = 0; i < window.sessionStorage.length; i++) {
|
|
2474
|
+
const key = window.sessionStorage.key(i);
|
|
2475
|
+
if (key) keys2.push(key);
|
|
2476
|
+
}
|
|
2477
|
+
} catch (error) {
|
|
2478
|
+
if (!initOptions.isProduction && typeof console !== "undefined" && console.error) {
|
|
2479
|
+
console.error("sessionStorage.keys error");
|
|
2480
|
+
}
|
|
2481
|
+
}
|
|
2482
|
+
return keys2;
|
|
2469
2483
|
}
|
|
2470
|
-
}
|
|
2471
|
-
|
|
2472
|
-
|
|
2473
|
-
|
|
2484
|
+
};
|
|
2485
|
+
case "cookie":
|
|
2486
|
+
return {
|
|
2487
|
+
async set(key, value) {
|
|
2488
|
+
cookie.set(key, String(value));
|
|
2489
|
+
},
|
|
2490
|
+
async get(key, options) {
|
|
2491
|
+
const value = cookie.get(key);
|
|
2492
|
+
if (value === void 0) return options?.defaultValue;
|
|
2493
|
+
try {
|
|
2494
|
+
return JSON.parse(value);
|
|
2495
|
+
} catch {
|
|
2496
|
+
return value;
|
|
2497
|
+
}
|
|
2498
|
+
},
|
|
2499
|
+
remove(key) {
|
|
2500
|
+
cookie.remove(key);
|
|
2501
|
+
},
|
|
2502
|
+
clear() {
|
|
2503
|
+
const all = cookie.getAll();
|
|
2504
|
+
Object.keys(all).forEach((k) => cookie.remove(k));
|
|
2505
|
+
},
|
|
2506
|
+
keys() {
|
|
2507
|
+
return Object.keys(cookie.getAll());
|
|
2508
|
+
}
|
|
2509
|
+
};
|
|
2510
|
+
default:
|
|
2511
|
+
return createBackendAdapter("local");
|
|
2474
2512
|
}
|
|
2475
|
-
}
|
|
2513
|
+
}
|
|
2514
|
+
function createSecureStorage(type = "local") {
|
|
2515
|
+
return createBackendAdapter(type);
|
|
2516
|
+
}
|
|
2517
|
+
var secureStorage = createSecureStorage("local");
|
|
2476
2518
|
|
|
2477
2519
|
// src/browser/network/index.ts
|
|
2478
2520
|
async function fetchWithRetry(url, options = {}, retryCount = 3, retryDelay = 1e3) {
|
|
@@ -4343,4 +4385,4 @@ function convertRes2Blob(response) {
|
|
|
4343
4385
|
}
|
|
4344
4386
|
}
|
|
4345
4387
|
|
|
4346
|
-
export { $, $$, BinaryTree, ChunkUploader, DataQueue, Graph, LRUCache, LinkedList, Queue, Stack, Tracker, UploadStatus, addClass, addDays, addMonths, addYears, aesGCMDecrypt, aesGCMEncrypt, base64Decode, base64Encode, batch, binarySearch, bubbleSort, buildUrl, calculateBlobMD5, calculateFileMD5, camelCase, capitalize, ceil, checkOnline, chunk, clamp, clearPersistedKeys, compact, computeHMAC, contrast, cookie, copyToClipboard, createTracker, createTranslator, createUploader, csvToJson, darken, debounce, deepClone, deepMerge, defaults, deriveKeyFromPassword, diffDays, difference, downloadFile, drop, dropWhile, endOfDay, escapeHtml, exportPrivateKey, exportPublicKey, factorial, fetchWithRetry, fetchWithTimeout, fibonacci, fibonacciSequence, findIndexBy, flatten, floor, flush, formatBytes, formatCurrency, formatCurrencyI18n, formatDate, formatDateI18n, formatFileSize, formatNumber, formatNumberI18n, formatRelativeTime, gcd, generateHMACKey, generateRSAKeyPair, generateRandomString, generateUUID, get, getDateFormatByGMT, getElementOffset, getFileExtension, getFileNameWithoutExtension, getKeyUsageCount, getLocale, getQuarter, getQueryParams, getRelativeTime, getScrollPosition, getStorageKeyPair, getStyle, getTimeFromGMT, getTracker, getWeekNumber, groupBy, hash, hexToRgb, highlight, hslToRgb, importPrivateKey, importPublicKey, initTracker, initializeStorageKeys, intersection, invert, isAbsoluteUrl, isBetween, isEmpty, isEmptyObject, isEqual, isFloat, isInViewport, isInteger, isNumeric, isToday, isValidDomain, isValidEmail, isValidHexColor, isValidIP, isValidIdCard, isValidJSON, isValidPhone, isValidUUID, isValidUrl, isWeekday, isWeekend, isYesterday, joinUrl, jsonToCsv, jsonToXml, jsonToYaml, kebabCase, keys, lcm, lighten, mapKeys, mapValues, mask, maskEmail, maskPhone, memoize, merge, mergeSort, mix, normalizeUrl, omit, omitBy, once, parseNumber, parseUrl, partition, pascalCase, percent, pick, pickBy, pluralize, quickSort, random, removeAccents, removeClass, removeQueryParams, request, resetKeyUsageCount, retry, rgbToHex, rgbToHsl, round, rsaDecrypt, rsaEncrypt, sample, scrollTo, secureStorage, set, setCommonParams, setQueryParams, setServiceEventHandlers, setStorageKeyPair, setStyle, setUserInfo, sha256, shuffle, slugify, snakeCase, sortBy, splitFileIntoChunks, startOfDay, take, takeWhile, template, throttle, timeout, toFixed, toggleClass, trackClick, trackEvent, trackExposure, trackPageView, transform, translate, truncate, unescapeHtml, union, unique, unzip, updateQueryParams, uploadFile, values, verifyHMAC, xmlToJson, yamlToJson, zip };
|
|
4388
|
+
export { $, $$, BinaryTree, ChunkUploader, DataQueue, Graph, LRUCache, LinkedList, Queue, Stack, Tracker, UploadStatus, addClass, addDays, addMonths, addYears, aesGCMDecrypt, aesGCMEncrypt, base64Decode, base64Encode, batch, binarySearch, bubbleSort, buildUrl, calculateBlobMD5, calculateFileMD5, camelCase, capitalize, ceil, checkOnline, chunk, clamp, clearPersistedKeys, compact, computeHMAC, contrast, cookie, copyToClipboard, createSecureStorage, createTracker, createTranslator, createUploader, csvToJson, darken, debounce, deepClone, deepMerge, defaults, deriveKeyFromPassword, diffDays, difference, downloadFile, drop, dropWhile, endOfDay, escapeHtml, exportPrivateKey, exportPublicKey, factorial, fetchWithRetry, fetchWithTimeout, fibonacci, fibonacciSequence, findIndexBy, flatten, floor, flush, formatBytes, formatCurrency, formatCurrencyI18n, formatDate, formatDateI18n, formatFileSize, formatNumber, formatNumberI18n, formatRelativeTime, gcd, generateHMACKey, generateRSAKeyPair, generateRandomString, generateUUID, get, getDateFormatByGMT, getElementOffset, getFileExtension, getFileNameWithoutExtension, getKeyUsageCount, getLocale, getQuarter, getQueryParams, getRelativeTime, getScrollPosition, getStorageKeyPair, getStyle, getTimeFromGMT, getTracker, getWeekNumber, groupBy, hash, hexToRgb, highlight, hslToRgb, importPrivateKey, importPublicKey, initTracker, initializeStorageKeys, intersection, invert, isAbsoluteUrl, isBetween, isEmpty, isEmptyObject, isEqual, isFloat, isInViewport, isInteger, isNumeric, isToday, isValidDomain, isValidEmail, isValidHexColor, isValidIP, isValidIdCard, isValidJSON, isValidPhone, isValidUUID, isValidUrl, isWeekday, isWeekend, isYesterday, joinUrl, jsonToCsv, jsonToXml, jsonToYaml, kebabCase, keys, lcm, lighten, mapKeys, mapValues, mask, maskEmail, maskPhone, memoize, merge, mergeSort, mix, normalizeUrl, omit, omitBy, once, parseNumber, parseUrl, partition, pascalCase, percent, pick, pickBy, pluralize, quickSort, random, removeAccents, removeClass, removeQueryParams, request, resetKeyUsageCount, retry, rgbToHex, rgbToHsl, round, rsaDecrypt, rsaEncrypt, sample, scrollTo, secureStorage, set, setCommonParams, setQueryParams, setServiceEventHandlers, setStorageKeyPair, setStyle, setUserInfo, sha256, shuffle, slugify, snakeCase, sortBy, splitFileIntoChunks, startOfDay, take, takeWhile, template, throttle, timeout, toFixed, toggleClass, trackClick, trackEvent, trackExposure, trackPageView, transform, translate, truncate, unescapeHtml, union, unique, unzip, updateQueryParams, uploadFile, values, verifyHMAC, xmlToJson, yamlToJson, zip };
|
package/dist/storage.cjs
CHANGED
|
@@ -51,7 +51,11 @@ async function rsaEncrypt(data, publicKey) {
|
|
|
51
51
|
}
|
|
52
52
|
const encoder = new TextEncoder();
|
|
53
53
|
const dataBuffer = encoder.encode(data);
|
|
54
|
-
const
|
|
54
|
+
const algo = publicKey.algorithm;
|
|
55
|
+
const modulusLength = typeof algo.modulusLength === "number" ? algo.modulusLength : 2048;
|
|
56
|
+
const hashName = typeof algo.hash === "object" && "name" in algo.hash ? algo.hash.name : "SHA-256";
|
|
57
|
+
const hashLength = hashName === "SHA-1" ? 20 : 32;
|
|
58
|
+
const maxChunkSize = Math.max(Math.floor(modulusLength / 8 - 2 * hashLength - 2), 1);
|
|
55
59
|
const chunks = [];
|
|
56
60
|
for (let i = 0; i < dataBuffer.length; i += maxChunkSize) {
|
|
57
61
|
const chunk = dataBuffer.slice(i, i + maxChunkSize);
|
|
@@ -270,6 +274,7 @@ var initOptions = {
|
|
|
270
274
|
var actualKeyStorageKey = null;
|
|
271
275
|
var keyUsageCount = 0;
|
|
272
276
|
var initializationPromise = null;
|
|
277
|
+
var KEY_STORAGE_KEY_REGISTRY = "__SECURE_STORAGE_KEY__";
|
|
273
278
|
function generateKeyStorageKey() {
|
|
274
279
|
return `_sk_${generateRandomString(32)}_${Date.now()}`;
|
|
275
280
|
}
|
|
@@ -288,8 +293,18 @@ async function initializeStorageKeys(options = {}) {
|
|
|
288
293
|
initOptions = { ...initOptions, ...options };
|
|
289
294
|
if (initOptions.keyStorageKey) {
|
|
290
295
|
actualKeyStorageKey = initOptions.keyStorageKey;
|
|
291
|
-
} else {
|
|
292
|
-
|
|
296
|
+
} else if (!actualKeyStorageKey) {
|
|
297
|
+
if (typeof window !== "undefined" && window.localStorage) {
|
|
298
|
+
const savedKeyName = window.localStorage.getItem(KEY_STORAGE_KEY_REGISTRY);
|
|
299
|
+
if (savedKeyName) {
|
|
300
|
+
actualKeyStorageKey = savedKeyName;
|
|
301
|
+
} else {
|
|
302
|
+
actualKeyStorageKey = generateKeyStorageKey();
|
|
303
|
+
window.localStorage.setItem(KEY_STORAGE_KEY_REGISTRY, actualKeyStorageKey);
|
|
304
|
+
}
|
|
305
|
+
} else {
|
|
306
|
+
actualKeyStorageKey = generateKeyStorageKey();
|
|
307
|
+
}
|
|
293
308
|
}
|
|
294
309
|
if (initOptions.persistKeys && typeof window !== "undefined" && window.localStorage) {
|
|
295
310
|
try {
|
|
@@ -887,76 +902,104 @@ var cookie = {
|
|
|
887
902
|
return cookies;
|
|
888
903
|
}
|
|
889
904
|
};
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
905
|
+
function createBackendAdapter(type) {
|
|
906
|
+
switch (type) {
|
|
907
|
+
case "local":
|
|
908
|
+
return {
|
|
909
|
+
async set(key, value, options) {
|
|
910
|
+
await localStorage.set(key, value, {
|
|
911
|
+
expiry: options?.expiry,
|
|
912
|
+
publicKey: options?.publicKey
|
|
913
|
+
});
|
|
914
|
+
},
|
|
915
|
+
async get(key, options) {
|
|
916
|
+
return localStorage.get(key, {
|
|
917
|
+
defaultValue: options?.defaultValue,
|
|
918
|
+
privateKey: options?.privateKey
|
|
919
|
+
});
|
|
920
|
+
},
|
|
921
|
+
remove(key) {
|
|
922
|
+
localStorage.remove(key);
|
|
923
|
+
},
|
|
924
|
+
clear() {
|
|
925
|
+
localStorage.clear();
|
|
926
|
+
},
|
|
927
|
+
keys() {
|
|
928
|
+
return localStorage.keys();
|
|
907
929
|
}
|
|
908
|
-
}
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
keys() {
|
|
943
|
-
const localKeys = localStorage.keys();
|
|
944
|
-
const sessionKeys = [];
|
|
945
|
-
if (typeof window !== "undefined" && window.sessionStorage) {
|
|
946
|
-
try {
|
|
947
|
-
for (let i = 0; i < window.sessionStorage.length; i++) {
|
|
948
|
-
const key = window.sessionStorage.key(i);
|
|
949
|
-
if (key) sessionKeys.push(key);
|
|
930
|
+
};
|
|
931
|
+
case "session":
|
|
932
|
+
return {
|
|
933
|
+
async set(key, value, options) {
|
|
934
|
+
await sessionStorage.set(key, value, {
|
|
935
|
+
publicKey: options?.publicKey
|
|
936
|
+
});
|
|
937
|
+
},
|
|
938
|
+
async get(key, options) {
|
|
939
|
+
return sessionStorage.get(key, {
|
|
940
|
+
defaultValue: options?.defaultValue,
|
|
941
|
+
privateKey: options?.privateKey
|
|
942
|
+
});
|
|
943
|
+
},
|
|
944
|
+
remove(key) {
|
|
945
|
+
sessionStorage.remove(key);
|
|
946
|
+
},
|
|
947
|
+
clear() {
|
|
948
|
+
sessionStorage.clear();
|
|
949
|
+
},
|
|
950
|
+
keys() {
|
|
951
|
+
if (typeof window === "undefined" || !window.sessionStorage) return [];
|
|
952
|
+
const keys = [];
|
|
953
|
+
try {
|
|
954
|
+
for (let i = 0; i < window.sessionStorage.length; i++) {
|
|
955
|
+
const key = window.sessionStorage.key(i);
|
|
956
|
+
if (key) keys.push(key);
|
|
957
|
+
}
|
|
958
|
+
} catch (error) {
|
|
959
|
+
if (!initOptions.isProduction && typeof console !== "undefined" && console.error) {
|
|
960
|
+
console.error("sessionStorage.keys error");
|
|
961
|
+
}
|
|
962
|
+
}
|
|
963
|
+
return keys;
|
|
950
964
|
}
|
|
951
|
-
}
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
965
|
+
};
|
|
966
|
+
case "cookie":
|
|
967
|
+
return {
|
|
968
|
+
async set(key, value) {
|
|
969
|
+
cookie.set(key, String(value));
|
|
970
|
+
},
|
|
971
|
+
async get(key, options) {
|
|
972
|
+
const value = cookie.get(key);
|
|
973
|
+
if (value === void 0) return options?.defaultValue;
|
|
974
|
+
try {
|
|
975
|
+
return JSON.parse(value);
|
|
976
|
+
} catch {
|
|
977
|
+
return value;
|
|
978
|
+
}
|
|
979
|
+
},
|
|
980
|
+
remove(key) {
|
|
981
|
+
cookie.remove(key);
|
|
982
|
+
},
|
|
983
|
+
clear() {
|
|
984
|
+
const all = cookie.getAll();
|
|
985
|
+
Object.keys(all).forEach((k) => cookie.remove(k));
|
|
986
|
+
},
|
|
987
|
+
keys() {
|
|
988
|
+
return Object.keys(cookie.getAll());
|
|
989
|
+
}
|
|
990
|
+
};
|
|
991
|
+
default:
|
|
992
|
+
return createBackendAdapter("local");
|
|
955
993
|
}
|
|
956
|
-
}
|
|
994
|
+
}
|
|
995
|
+
function createSecureStorage(type = "local") {
|
|
996
|
+
return createBackendAdapter(type);
|
|
997
|
+
}
|
|
998
|
+
var secureStorage = createSecureStorage("local");
|
|
957
999
|
|
|
958
1000
|
exports.clearPersistedKeys = clearPersistedKeys;
|
|
959
1001
|
exports.cookie = cookie;
|
|
1002
|
+
exports.createSecureStorage = createSecureStorage;
|
|
960
1003
|
exports.getKeyUsageCount = getKeyUsageCount;
|
|
961
1004
|
exports.getStorageKeyPair = getStorageKeyPair;
|
|
962
1005
|
exports.initializeStorageKeys = initializeStorageKeys;
|
package/dist/storage.d.mts
CHANGED
|
@@ -109,45 +109,33 @@ declare const cookie: {
|
|
|
109
109
|
getAll(): Record<string, string>;
|
|
110
110
|
};
|
|
111
111
|
/**
|
|
112
|
-
*
|
|
113
|
-
* 这是推荐的主要导出接口
|
|
112
|
+
* 统一安全存储接口(通过后端适配器封装 localStorage / sessionStorage / Cookie)
|
|
114
113
|
*/
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
* 设置值(优先使用localStorage,失败则使用sessionStorage)
|
|
118
|
-
* @param key - 键
|
|
119
|
-
* @param value - 值
|
|
120
|
-
* @param options - 选项(过期时间、公钥等)
|
|
121
|
-
* @returns Promise<void>
|
|
122
|
-
*/
|
|
114
|
+
type SecureStorageBackendType = 'local' | 'session' | 'cookie';
|
|
115
|
+
interface SecureStorageInstance {
|
|
123
116
|
set<T>(key: string, value: T, options?: {
|
|
124
117
|
expiry?: number;
|
|
125
118
|
publicKey?: CryptoKey;
|
|
119
|
+
privateKey?: CryptoKey;
|
|
120
|
+
defaultValue?: T;
|
|
126
121
|
}): Promise<void>;
|
|
127
|
-
/**
|
|
128
|
-
* 获取值
|
|
129
|
-
* @param key - 键
|
|
130
|
-
* @param options - 选项(默认值、私钥等)
|
|
131
|
-
* @returns Promise<T | undefined> 值或默认值
|
|
132
|
-
*/
|
|
133
122
|
get<T>(key: string, options?: {
|
|
134
123
|
defaultValue?: T;
|
|
135
124
|
privateKey?: CryptoKey;
|
|
136
125
|
}): Promise<T | undefined>;
|
|
137
|
-
/**
|
|
138
|
-
* 移除值
|
|
139
|
-
* @param key - 键
|
|
140
|
-
*/
|
|
141
126
|
remove(key: string): void;
|
|
142
|
-
/**
|
|
143
|
-
* 清空所有值
|
|
144
|
-
*/
|
|
145
127
|
clear(): void;
|
|
146
|
-
/**
|
|
147
|
-
* 获取所有键名
|
|
148
|
-
* @returns 键名数组
|
|
149
|
-
*/
|
|
150
128
|
keys(): string[];
|
|
151
|
-
}
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* 工厂函数:根据后端类型创建 SecureStorage 实例
|
|
132
|
+
* 使用方可在应用启动时自行决定使用 local / session / cookie
|
|
133
|
+
*/
|
|
134
|
+
declare function createSecureStorage(type?: SecureStorageBackendType): SecureStorageInstance;
|
|
135
|
+
/**
|
|
136
|
+
* 默认导出的 SecureStorage:使用 localStorage 作为后端
|
|
137
|
+
* 与之前 `secureStorage` 的语义保持一致
|
|
138
|
+
*/
|
|
139
|
+
declare const secureStorage: SecureStorageInstance;
|
|
152
140
|
|
|
153
|
-
export { type StorageInitOptions, type StorageOptions, clearPersistedKeys, cookie, getKeyUsageCount, getStorageKeyPair, initializeStorageKeys, resetKeyUsageCount, secureStorage, setStorageKeyPair };
|
|
141
|
+
export { type SecureStorageInstance, type StorageInitOptions, type StorageOptions, clearPersistedKeys, cookie, createSecureStorage, getKeyUsageCount, getStorageKeyPair, initializeStorageKeys, resetKeyUsageCount, secureStorage, setStorageKeyPair };
|
package/dist/storage.d.ts
CHANGED
|
@@ -109,45 +109,33 @@ declare const cookie: {
|
|
|
109
109
|
getAll(): Record<string, string>;
|
|
110
110
|
};
|
|
111
111
|
/**
|
|
112
|
-
*
|
|
113
|
-
* 这是推荐的主要导出接口
|
|
112
|
+
* 统一安全存储接口(通过后端适配器封装 localStorage / sessionStorage / Cookie)
|
|
114
113
|
*/
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
* 设置值(优先使用localStorage,失败则使用sessionStorage)
|
|
118
|
-
* @param key - 键
|
|
119
|
-
* @param value - 值
|
|
120
|
-
* @param options - 选项(过期时间、公钥等)
|
|
121
|
-
* @returns Promise<void>
|
|
122
|
-
*/
|
|
114
|
+
type SecureStorageBackendType = 'local' | 'session' | 'cookie';
|
|
115
|
+
interface SecureStorageInstance {
|
|
123
116
|
set<T>(key: string, value: T, options?: {
|
|
124
117
|
expiry?: number;
|
|
125
118
|
publicKey?: CryptoKey;
|
|
119
|
+
privateKey?: CryptoKey;
|
|
120
|
+
defaultValue?: T;
|
|
126
121
|
}): Promise<void>;
|
|
127
|
-
/**
|
|
128
|
-
* 获取值
|
|
129
|
-
* @param key - 键
|
|
130
|
-
* @param options - 选项(默认值、私钥等)
|
|
131
|
-
* @returns Promise<T | undefined> 值或默认值
|
|
132
|
-
*/
|
|
133
122
|
get<T>(key: string, options?: {
|
|
134
123
|
defaultValue?: T;
|
|
135
124
|
privateKey?: CryptoKey;
|
|
136
125
|
}): Promise<T | undefined>;
|
|
137
|
-
/**
|
|
138
|
-
* 移除值
|
|
139
|
-
* @param key - 键
|
|
140
|
-
*/
|
|
141
126
|
remove(key: string): void;
|
|
142
|
-
/**
|
|
143
|
-
* 清空所有值
|
|
144
|
-
*/
|
|
145
127
|
clear(): void;
|
|
146
|
-
/**
|
|
147
|
-
* 获取所有键名
|
|
148
|
-
* @returns 键名数组
|
|
149
|
-
*/
|
|
150
128
|
keys(): string[];
|
|
151
|
-
}
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* 工厂函数:根据后端类型创建 SecureStorage 实例
|
|
132
|
+
* 使用方可在应用启动时自行决定使用 local / session / cookie
|
|
133
|
+
*/
|
|
134
|
+
declare function createSecureStorage(type?: SecureStorageBackendType): SecureStorageInstance;
|
|
135
|
+
/**
|
|
136
|
+
* 默认导出的 SecureStorage:使用 localStorage 作为后端
|
|
137
|
+
* 与之前 `secureStorage` 的语义保持一致
|
|
138
|
+
*/
|
|
139
|
+
declare const secureStorage: SecureStorageInstance;
|
|
152
140
|
|
|
153
|
-
export { type StorageInitOptions, type StorageOptions, clearPersistedKeys, cookie, getKeyUsageCount, getStorageKeyPair, initializeStorageKeys, resetKeyUsageCount, secureStorage, setStorageKeyPair };
|
|
141
|
+
export { type SecureStorageInstance, type StorageInitOptions, type StorageOptions, clearPersistedKeys, cookie, createSecureStorage, getKeyUsageCount, getStorageKeyPair, initializeStorageKeys, resetKeyUsageCount, secureStorage, setStorageKeyPair };
|
package/dist/storage.js
CHANGED
|
@@ -49,7 +49,11 @@ async function rsaEncrypt(data, publicKey) {
|
|
|
49
49
|
}
|
|
50
50
|
const encoder = new TextEncoder();
|
|
51
51
|
const dataBuffer = encoder.encode(data);
|
|
52
|
-
const
|
|
52
|
+
const algo = publicKey.algorithm;
|
|
53
|
+
const modulusLength = typeof algo.modulusLength === "number" ? algo.modulusLength : 2048;
|
|
54
|
+
const hashName = typeof algo.hash === "object" && "name" in algo.hash ? algo.hash.name : "SHA-256";
|
|
55
|
+
const hashLength = hashName === "SHA-1" ? 20 : 32;
|
|
56
|
+
const maxChunkSize = Math.max(Math.floor(modulusLength / 8 - 2 * hashLength - 2), 1);
|
|
53
57
|
const chunks = [];
|
|
54
58
|
for (let i = 0; i < dataBuffer.length; i += maxChunkSize) {
|
|
55
59
|
const chunk = dataBuffer.slice(i, i + maxChunkSize);
|
|
@@ -268,6 +272,7 @@ var initOptions = {
|
|
|
268
272
|
var actualKeyStorageKey = null;
|
|
269
273
|
var keyUsageCount = 0;
|
|
270
274
|
var initializationPromise = null;
|
|
275
|
+
var KEY_STORAGE_KEY_REGISTRY = "__SECURE_STORAGE_KEY__";
|
|
271
276
|
function generateKeyStorageKey() {
|
|
272
277
|
return `_sk_${generateRandomString(32)}_${Date.now()}`;
|
|
273
278
|
}
|
|
@@ -286,8 +291,18 @@ async function initializeStorageKeys(options = {}) {
|
|
|
286
291
|
initOptions = { ...initOptions, ...options };
|
|
287
292
|
if (initOptions.keyStorageKey) {
|
|
288
293
|
actualKeyStorageKey = initOptions.keyStorageKey;
|
|
289
|
-
} else {
|
|
290
|
-
|
|
294
|
+
} else if (!actualKeyStorageKey) {
|
|
295
|
+
if (typeof window !== "undefined" && window.localStorage) {
|
|
296
|
+
const savedKeyName = window.localStorage.getItem(KEY_STORAGE_KEY_REGISTRY);
|
|
297
|
+
if (savedKeyName) {
|
|
298
|
+
actualKeyStorageKey = savedKeyName;
|
|
299
|
+
} else {
|
|
300
|
+
actualKeyStorageKey = generateKeyStorageKey();
|
|
301
|
+
window.localStorage.setItem(KEY_STORAGE_KEY_REGISTRY, actualKeyStorageKey);
|
|
302
|
+
}
|
|
303
|
+
} else {
|
|
304
|
+
actualKeyStorageKey = generateKeyStorageKey();
|
|
305
|
+
}
|
|
291
306
|
}
|
|
292
307
|
if (initOptions.persistKeys && typeof window !== "undefined" && window.localStorage) {
|
|
293
308
|
try {
|
|
@@ -885,72 +900,99 @@ var cookie = {
|
|
|
885
900
|
return cookies;
|
|
886
901
|
}
|
|
887
902
|
};
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
903
|
+
function createBackendAdapter(type) {
|
|
904
|
+
switch (type) {
|
|
905
|
+
case "local":
|
|
906
|
+
return {
|
|
907
|
+
async set(key, value, options) {
|
|
908
|
+
await localStorage.set(key, value, {
|
|
909
|
+
expiry: options?.expiry,
|
|
910
|
+
publicKey: options?.publicKey
|
|
911
|
+
});
|
|
912
|
+
},
|
|
913
|
+
async get(key, options) {
|
|
914
|
+
return localStorage.get(key, {
|
|
915
|
+
defaultValue: options?.defaultValue,
|
|
916
|
+
privateKey: options?.privateKey
|
|
917
|
+
});
|
|
918
|
+
},
|
|
919
|
+
remove(key) {
|
|
920
|
+
localStorage.remove(key);
|
|
921
|
+
},
|
|
922
|
+
clear() {
|
|
923
|
+
localStorage.clear();
|
|
924
|
+
},
|
|
925
|
+
keys() {
|
|
926
|
+
return localStorage.keys();
|
|
905
927
|
}
|
|
906
|
-
}
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
keys() {
|
|
941
|
-
const localKeys = localStorage.keys();
|
|
942
|
-
const sessionKeys = [];
|
|
943
|
-
if (typeof window !== "undefined" && window.sessionStorage) {
|
|
944
|
-
try {
|
|
945
|
-
for (let i = 0; i < window.sessionStorage.length; i++) {
|
|
946
|
-
const key = window.sessionStorage.key(i);
|
|
947
|
-
if (key) sessionKeys.push(key);
|
|
928
|
+
};
|
|
929
|
+
case "session":
|
|
930
|
+
return {
|
|
931
|
+
async set(key, value, options) {
|
|
932
|
+
await sessionStorage.set(key, value, {
|
|
933
|
+
publicKey: options?.publicKey
|
|
934
|
+
});
|
|
935
|
+
},
|
|
936
|
+
async get(key, options) {
|
|
937
|
+
return sessionStorage.get(key, {
|
|
938
|
+
defaultValue: options?.defaultValue,
|
|
939
|
+
privateKey: options?.privateKey
|
|
940
|
+
});
|
|
941
|
+
},
|
|
942
|
+
remove(key) {
|
|
943
|
+
sessionStorage.remove(key);
|
|
944
|
+
},
|
|
945
|
+
clear() {
|
|
946
|
+
sessionStorage.clear();
|
|
947
|
+
},
|
|
948
|
+
keys() {
|
|
949
|
+
if (typeof window === "undefined" || !window.sessionStorage) return [];
|
|
950
|
+
const keys = [];
|
|
951
|
+
try {
|
|
952
|
+
for (let i = 0; i < window.sessionStorage.length; i++) {
|
|
953
|
+
const key = window.sessionStorage.key(i);
|
|
954
|
+
if (key) keys.push(key);
|
|
955
|
+
}
|
|
956
|
+
} catch (error) {
|
|
957
|
+
if (!initOptions.isProduction && typeof console !== "undefined" && console.error) {
|
|
958
|
+
console.error("sessionStorage.keys error");
|
|
959
|
+
}
|
|
960
|
+
}
|
|
961
|
+
return keys;
|
|
948
962
|
}
|
|
949
|
-
}
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
963
|
+
};
|
|
964
|
+
case "cookie":
|
|
965
|
+
return {
|
|
966
|
+
async set(key, value) {
|
|
967
|
+
cookie.set(key, String(value));
|
|
968
|
+
},
|
|
969
|
+
async get(key, options) {
|
|
970
|
+
const value = cookie.get(key);
|
|
971
|
+
if (value === void 0) return options?.defaultValue;
|
|
972
|
+
try {
|
|
973
|
+
return JSON.parse(value);
|
|
974
|
+
} catch {
|
|
975
|
+
return value;
|
|
976
|
+
}
|
|
977
|
+
},
|
|
978
|
+
remove(key) {
|
|
979
|
+
cookie.remove(key);
|
|
980
|
+
},
|
|
981
|
+
clear() {
|
|
982
|
+
const all = cookie.getAll();
|
|
983
|
+
Object.keys(all).forEach((k) => cookie.remove(k));
|
|
984
|
+
},
|
|
985
|
+
keys() {
|
|
986
|
+
return Object.keys(cookie.getAll());
|
|
987
|
+
}
|
|
988
|
+
};
|
|
989
|
+
default:
|
|
990
|
+
return createBackendAdapter("local");
|
|
953
991
|
}
|
|
954
|
-
}
|
|
992
|
+
}
|
|
993
|
+
function createSecureStorage(type = "local") {
|
|
994
|
+
return createBackendAdapter(type);
|
|
995
|
+
}
|
|
996
|
+
var secureStorage = createSecureStorage("local");
|
|
955
997
|
|
|
956
|
-
export { clearPersistedKeys, cookie, getKeyUsageCount, getStorageKeyPair, initializeStorageKeys, resetKeyUsageCount, secureStorage, setStorageKeyPair };
|
|
998
|
+
export { clearPersistedKeys, cookie, createSecureStorage, getKeyUsageCount, getStorageKeyPair, initializeStorageKeys, resetKeyUsageCount, secureStorage, setStorageKeyPair };
|