@nsnanocat/util 2.5.13 → 2.5.14

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 (2) hide show
  1. package/package.json +1 -1
  2. package/polyfill/Storage.js +27 -36
package/package.json CHANGED
@@ -41,5 +41,5 @@
41
41
  "registry": "https://registry.npmjs.org/",
42
42
  "access": "public"
43
43
  },
44
- "version": "2.5.13"
44
+ "version": "2.5.14"
45
45
  }
@@ -284,24 +284,22 @@ export class Storage {
284
284
  * @param {string} dataFile 数据文件名 / Data file name.
285
285
  * @returns {Record<string, any>}
286
286
  */
287
- static #loaddata = dataFile => {
288
- if ($app === "Node.js") {
289
- this.fs = this.fs ? this.fs : require("node:fs");
290
- this.path = this.path ? this.path : require("node:path");
291
- const curDirDataFilePath = this.path.resolve(dataFile);
292
- const rootDirDataFilePath = this.path.resolve(process.cwd(), dataFile);
293
- const isCurDirDataFile = this.fs.existsSync(curDirDataFilePath);
294
- const isRootDirDataFile = !isCurDirDataFile && this.fs.existsSync(rootDirDataFilePath);
295
- if (isCurDirDataFile || isRootDirDataFile) {
296
- const datPath = isCurDirDataFile ? curDirDataFilePath : rootDirDataFilePath;
297
- try {
298
- return JSON.parse(this.fs.readFileSync(datPath));
299
- } catch (e) {
300
- return {};
301
- }
302
- } else return {};
303
- } else return {};
304
- };
287
+ static #loaddata(dataFile) {
288
+ if ($app !== "Node.js") return {};
289
+ // 优先复用全局运行时对象,不存在时再按需加载。
290
+ // Prefer reusing global runtime modules and lazily require when missing.
291
+ if (!globalThis.fs) globalThis.fs = require("node:fs");
292
+ if (!globalThis.path) globalThis.path = require("node:path");
293
+ const dataFilePathCandidates = [...new Set([globalThis.path.resolve(dataFile), globalThis.path.resolve(process.cwd(), dataFile)])];
294
+ const dataFilePath = dataFilePathCandidates.find(filePath => globalThis.fs.existsSync(filePath));
295
+ if (!dataFilePath) return {};
296
+ try {
297
+ const jsonData = globalThis.fs.readFileSync(dataFilePath, "utf8");
298
+ return jsonData ? JSON.parse(jsonData) : {};
299
+ } catch (e) {
300
+ return {};
301
+ }
302
+ }
305
303
 
306
304
  /**
307
305
  * 将内存数据写入 Node.js 数据文件。
@@ -311,22 +309,15 @@ export class Storage {
311
309
  * @param {string} [dataFile=this.dataFile] 数据文件名 / Data file name.
312
310
  * @returns {void}
313
311
  */
314
- static #writedata = (dataFile = this.dataFile) => {
315
- if ($app === "Node.js") {
316
- this.fs = this.fs ? this.fs : require("node:fs");
317
- this.path = this.path ? this.path : require("node:path");
318
- const curDirDataFilePath = this.path.resolve(dataFile);
319
- const rootDirDataFilePath = this.path.resolve(process.cwd(), dataFile);
320
- const isCurDirDataFile = this.fs.existsSync(curDirDataFilePath);
321
- const isRootDirDataFile = !isCurDirDataFile && this.fs.existsSync(rootDirDataFilePath);
322
- const jsondata = JSON.stringify(this.data);
323
- if (isCurDirDataFile) {
324
- this.fs.writeFileSync(curDirDataFilePath, jsondata);
325
- } else if (isRootDirDataFile) {
326
- this.fs.writeFileSync(rootDirDataFilePath, jsondata);
327
- } else {
328
- this.fs.writeFileSync(curDirDataFilePath, jsondata);
329
- }
330
- }
331
- };
312
+ static #writedata(dataFile = this.dataFile) {
313
+ if ($app !== "Node.js") return;
314
+ // 优先复用全局运行时对象,不存在时再按需加载。
315
+ // Prefer reusing global runtime modules and lazily require when missing.
316
+ if (!globalThis.fs) globalThis.fs = require("node:fs");
317
+ if (!globalThis.path) globalThis.path = require("node:path");
318
+ const dataFilePathCandidates = [...new Set([globalThis.path.resolve(dataFile), globalThis.path.resolve(process.cwd(), dataFile)])];
319
+ const dataFilePath = dataFilePathCandidates.find(filePath => globalThis.fs.existsSync(filePath)) ?? dataFilePathCandidates[0];
320
+ const jsonData = JSON.stringify(this.data);
321
+ globalThis.fs.writeFileSync(dataFilePath, jsonData);
322
+ }
332
323
  }