pinia-plugin-persistedstate.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. // G:/work/project/2024/手机壳DIY/用户端/phone_uni/node_modules/destr/dist/index.mjs
  2. var suspectProtoRx = /"(?:_|\\u0{2}5[Ff]){2}(?:p|\\u0{2}70)(?:r|\\u0{2}72)(?:o|\\u0{2}6[Ff])(?:t|\\u0{2}74)(?:o|\\u0{2}6[Ff])(?:_|\\u0{2}5[Ff]){2}"\s*:/;
  3. var suspectConstructorRx = /"(?:c|\\u0063)(?:o|\\u006[Ff])(?:n|\\u006[Ee])(?:s|\\u0073)(?:t|\\u0074)(?:r|\\u0072)(?:u|\\u0075)(?:c|\\u0063)(?:t|\\u0074)(?:o|\\u006[Ff])(?:r|\\u0072)"\s*:/;
  4. var JsonSigRx = /^\s*["[{]|^\s*-?\d{1,16}(\.\d{1,17})?([Ee][+-]?\d+)?\s*$/;
  5. function jsonParseTransform(key, value) {
  6. if (key === "__proto__" || key === "constructor" && value && typeof value === "object" && "prototype" in value) {
  7. warnKeyDropped(key);
  8. return;
  9. }
  10. return value;
  11. }
  12. function warnKeyDropped(key) {
  13. console.warn(`[destr] Dropping "${key}" key to prevent prototype pollution.`);
  14. }
  15. function destr(value, options = {}) {
  16. if (typeof value !== "string") {
  17. return value;
  18. }
  19. const _value = value.trim();
  20. if (
  21. // eslint-disable-next-line unicorn/prefer-at
  22. value[0] === '"' && value.endsWith('"') && !value.includes("\\")
  23. ) {
  24. return _value.slice(1, -1);
  25. }
  26. if (_value.length <= 9) {
  27. const _lval = _value.toLowerCase();
  28. if (_lval === "true") {
  29. return true;
  30. }
  31. if (_lval === "false") {
  32. return false;
  33. }
  34. if (_lval === "undefined") {
  35. return void 0;
  36. }
  37. if (_lval === "null") {
  38. return null;
  39. }
  40. if (_lval === "nan") {
  41. return Number.NaN;
  42. }
  43. if (_lval === "infinity") {
  44. return Number.POSITIVE_INFINITY;
  45. }
  46. if (_lval === "-infinity") {
  47. return Number.NEGATIVE_INFINITY;
  48. }
  49. }
  50. if (!JsonSigRx.test(value)) {
  51. if (options.strict) {
  52. throw new SyntaxError("[destr] Invalid JSON");
  53. }
  54. return value;
  55. }
  56. try {
  57. if (suspectProtoRx.test(value) || suspectConstructorRx.test(value)) {
  58. if (options.strict) {
  59. throw new Error("[destr] Possible prototype pollution");
  60. }
  61. return JSON.parse(value, jsonParseTransform);
  62. }
  63. return JSON.parse(value);
  64. } catch (error) {
  65. if (options.strict) {
  66. throw error;
  67. }
  68. return value;
  69. }
  70. }
  71. // G:/work/project/2024/手机壳DIY/用户端/phone_uni/node_modules/deep-pick-omit/dist/index.mjs
  72. function get(obj, path) {
  73. if (obj == null)
  74. return void 0;
  75. let value = obj;
  76. for (let i = 0; i < path.length; i++) {
  77. if (value == null || value[path[i]] == null)
  78. return void 0;
  79. value = value[path[i]];
  80. }
  81. return value;
  82. }
  83. function set(obj, value, path) {
  84. if (path.length === 0)
  85. return value;
  86. const idx = path[0];
  87. if (path.length > 1) {
  88. value = set(
  89. typeof obj !== "object" || obj === null || !Object.prototype.hasOwnProperty.call(obj, idx) ? Number.isInteger(Number(path[1])) ? [] : {} : obj[idx],
  90. value,
  91. Array.prototype.slice.call(path, 1)
  92. );
  93. }
  94. if (Number.isInteger(Number(idx)) && Array.isArray(obj))
  95. return obj.slice()[idx];
  96. return Object.assign({}, obj, { [idx]: value });
  97. }
  98. function unset(obj, path) {
  99. if (obj == null || path.length === 0)
  100. return obj;
  101. if (path.length === 1) {
  102. if (obj == null)
  103. return obj;
  104. if (Number.isInteger(path[0]) && Array.isArray(obj))
  105. return Array.prototype.slice.call(obj, 0).splice(path[0], 1);
  106. const result = {};
  107. for (const p in obj)
  108. result[p] = obj[p];
  109. delete result[path[0]];
  110. return result;
  111. }
  112. if (obj[path[0]] == null) {
  113. if (Number.isInteger(path[0]) && Array.isArray(obj))
  114. return Array.prototype.concat.call([], obj);
  115. const result = {};
  116. for (const p in obj)
  117. result[p] = obj[p];
  118. return result;
  119. }
  120. return set(
  121. obj,
  122. unset(
  123. obj[path[0]],
  124. Array.prototype.slice.call(path, 1)
  125. ),
  126. [path[0]]
  127. );
  128. }
  129. function deepPickUnsafe(obj, paths) {
  130. return paths.map((p) => p.split(".")).map((p) => [p, get(obj, p)]).filter((t) => t[1] !== void 0).reduce((acc, cur) => set(acc, cur[1], cur[0]), {});
  131. }
  132. function deepOmitUnsafe(obj, paths) {
  133. return paths.map((p) => p.split(".")).reduce((acc, cur) => unset(acc, cur), obj);
  134. }
  135. // G:/work/project/2024/手机壳DIY/用户端/phone_uni/node_modules/pinia-plugin-persistedstate/dist/index.js
  136. function hydrateStore(store, {
  137. storage,
  138. serializer,
  139. key,
  140. debug,
  141. pick,
  142. omit,
  143. beforeHydrate,
  144. afterHydrate
  145. }, context, runHooks = true) {
  146. try {
  147. if (runHooks)
  148. beforeHydrate == null ? void 0 : beforeHydrate(context);
  149. const fromStorage = storage.getItem(key);
  150. if (fromStorage) {
  151. const deserialized = serializer.deserialize(fromStorage);
  152. const picked = pick ? deepPickUnsafe(deserialized, pick) : deserialized;
  153. const omitted = omit ? deepOmitUnsafe(picked, omit) : picked;
  154. store.$patch(omitted);
  155. }
  156. if (runHooks)
  157. afterHydrate == null ? void 0 : afterHydrate(context);
  158. } catch (error) {
  159. if (debug)
  160. console.error("[pinia-plugin-persistedstate]", error);
  161. }
  162. }
  163. function persistState(state, {
  164. storage,
  165. serializer,
  166. key,
  167. debug,
  168. pick,
  169. omit
  170. }) {
  171. try {
  172. const picked = pick ? deepPickUnsafe(state, pick) : state;
  173. const omitted = omit ? deepOmitUnsafe(picked, omit) : picked;
  174. const toStorage = serializer.serialize(omitted);
  175. storage.setItem(key, toStorage);
  176. } catch (error) {
  177. if (debug)
  178. console.error("[pinia-plugin-persistedstate]", error);
  179. }
  180. }
  181. function createPersistence(context, optionsParser, auto) {
  182. const { pinia, store, options: { persist = auto } } = context;
  183. if (!persist)
  184. return;
  185. if (!(store.$id in pinia.state.value)) {
  186. const originalStore = pinia._s.get(store.$id.replace("__hot:", ""));
  187. if (originalStore)
  188. Promise.resolve().then(() => originalStore.$persist());
  189. return;
  190. }
  191. const persistenceOptions = Array.isArray(persist) ? persist : persist === true ? [{}] : [persist];
  192. const persistences = persistenceOptions.map(optionsParser);
  193. store.$hydrate = ({ runHooks = true } = {}) => {
  194. persistences.forEach((p) => {
  195. hydrateStore(store, p, context, runHooks);
  196. });
  197. };
  198. store.$persist = () => {
  199. persistences.forEach((p) => {
  200. persistState(store.$state, p);
  201. });
  202. };
  203. persistences.forEach((p) => {
  204. hydrateStore(store, p, context);
  205. store.$subscribe(
  206. (_mutation, state) => persistState(state, p),
  207. { detached: true }
  208. );
  209. });
  210. }
  211. function createPersistedState(options = {}) {
  212. return function(context) {
  213. createPersistence(
  214. context,
  215. (p) => ({
  216. key: (options.key ? options.key : (x) => x)(p.key ?? context.store.$id),
  217. debug: p.debug ?? options.debug ?? false,
  218. serializer: p.serializer ?? options.serializer ?? {
  219. serialize: (data) => JSON.stringify(data),
  220. deserialize: (data) => destr(data)
  221. },
  222. storage: p.storage ?? options.storage ?? window.localStorage,
  223. beforeHydrate: p.beforeHydrate,
  224. afterHydrate: p.afterHydrate,
  225. pick: p.pick,
  226. omit: p.omit
  227. }),
  228. options.auto ?? false
  229. );
  230. };
  231. }
  232. var src_default = createPersistedState();
  233. export {
  234. createPersistedState,
  235. src_default as default
  236. };
  237. //# sourceMappingURL=pinia-plugin-persistedstate.js.map