@modern-js/runtime-utils 2.42.1 → 2.43.0

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.
@@ -0,0 +1,93 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var fileReader_exports = {};
30
+ __export(fileReader_exports, {
31
+ FileReader: () => FileReader,
32
+ fileReader: () => fileReader
33
+ });
34
+ module.exports = __toCommonJS(fileReader_exports);
35
+ var import_fs_extra = __toESM(require("@modern-js/utils/fs-extra"));
36
+ var import_storer = require("./storer");
37
+ class FileReader {
38
+ async readFile(path, encoding = "utf-8") {
39
+ const { fs } = this;
40
+ const cache = await this.storage.get(path);
41
+ if (cache === null) {
42
+ return null;
43
+ }
44
+ if (cache) {
45
+ return this.encodingContent(cache, encoding);
46
+ }
47
+ const isExistFile = await new Promise((resolve) => {
48
+ fs.stat(path, (err, stats) => {
49
+ if (err) {
50
+ resolve(false);
51
+ return;
52
+ }
53
+ if (stats.isFile()) {
54
+ resolve(true);
55
+ } else {
56
+ resolve(false);
57
+ }
58
+ });
59
+ });
60
+ if (isExistFile) {
61
+ const content = await fs.promises.readFile(path);
62
+ this.storage.set(path, content);
63
+ return this.encodingContent(content, encoding);
64
+ } else {
65
+ this.storage.set(path, null);
66
+ return null;
67
+ }
68
+ }
69
+ /**
70
+ * Clear the fileCache entriely.
71
+ */
72
+ reset(fs) {
73
+ var _this_storage_clear, _this_storage;
74
+ fs && (this.fs = fs);
75
+ return (_this_storage_clear = (_this_storage = this.storage).clear) === null || _this_storage_clear === void 0 ? void 0 : _this_storage_clear.call(_this_storage);
76
+ }
77
+ encodingContent(value, encoding) {
78
+ if (encoding === "utf-8") {
79
+ return value.toString();
80
+ }
81
+ return value;
82
+ }
83
+ constructor(storage) {
84
+ this.fs = import_fs_extra.default;
85
+ this.storage = storage;
86
+ }
87
+ }
88
+ const fileReader = new FileReader((0, import_storer.createMemoryStorage)("__file__system"));
89
+ // Annotate the CommonJS export names for ESM import in node:
90
+ 0 && (module.exports = {
91
+ FileReader,
92
+ fileReader
93
+ });
@@ -0,0 +1,75 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var container_exports = {};
30
+ __export(container_exports, {
31
+ MemoryContainer: () => MemoryContainer
32
+ });
33
+ module.exports = __toCommonJS(container_exports);
34
+ var import_lru_cache = __toESM(require("lru-cache"));
35
+ class MemoryContainer {
36
+ async get(key) {
37
+ return this.cache.get(key);
38
+ }
39
+ async set(key, value) {
40
+ this.cache.set(key, value);
41
+ return this;
42
+ }
43
+ async has(key) {
44
+ return this.cache.has(key);
45
+ }
46
+ async delete(key) {
47
+ const exist = await this.has(key);
48
+ if (exist) {
49
+ this.cache.del(key);
50
+ }
51
+ return exist;
52
+ }
53
+ forEach(callbackFn) {
54
+ this.cache.forEach((value, key) => {
55
+ callbackFn(value, key, this);
56
+ });
57
+ }
58
+ constructor({ max, maxAge } = {}) {
59
+ this.cache = new import_lru_cache.default({
60
+ max: (max || 256) * MemoryContainer.MB,
61
+ maxAge: maxAge || MemoryContainer.hour
62
+ });
63
+ }
64
+ }
65
+ MemoryContainer.BYTE = 1;
66
+ MemoryContainer.KB = 1024 * MemoryContainer.BYTE;
67
+ MemoryContainer.MB = 1024 * MemoryContainer.KB;
68
+ MemoryContainer.ms = 1;
69
+ MemoryContainer.second = MemoryContainer.ms * 1e3;
70
+ MemoryContainer.minute = MemoryContainer.second * 60;
71
+ MemoryContainer.hour = MemoryContainer.minute * 60;
72
+ // Annotate the CommonJS export names for ESM import in node:
73
+ 0 && (module.exports = {
74
+ MemoryContainer
75
+ });
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var storer_exports = {};
20
+ __export(storer_exports, {
21
+ Storage: () => import_storage2.Storage,
22
+ createMemoryStorage: () => createMemoryStorage
23
+ });
24
+ module.exports = __toCommonJS(storer_exports);
25
+ var import_container = require("./container");
26
+ var import_storage = require("./storage");
27
+ var import_storage2 = require("./storage");
28
+ const memoryContainer = new import_container.MemoryContainer();
29
+ function createMemoryStorage(namespace) {
30
+ return new import_storage.Storage(namespace, memoryContainer);
31
+ }
32
+ // Annotate the CommonJS export names for ESM import in node:
33
+ 0 && (module.exports = {
34
+ Storage,
35
+ createMemoryStorage
36
+ });
@@ -0,0 +1,97 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var storage_exports = {};
20
+ __export(storage_exports, {
21
+ Storage: () => Storage
22
+ });
23
+ module.exports = __toCommonJS(storage_exports);
24
+ class Storage {
25
+ async keys() {
26
+ var _this_forEach, _this;
27
+ const _keys = [];
28
+ (_this_forEach = (_this = this).forEach) === null || _this_forEach === void 0 ? void 0 : _this_forEach.call(_this, (_, k) => {
29
+ _keys.push(k);
30
+ });
31
+ return _keys;
32
+ }
33
+ async values() {
34
+ var _this_forEach, _this;
35
+ const _values = [];
36
+ (_this_forEach = (_this = this).forEach) === null || _this_forEach === void 0 ? void 0 : _this_forEach.call(_this, (v) => {
37
+ _values.push(v);
38
+ });
39
+ return _values;
40
+ }
41
+ /**
42
+ * Returns a specified element from the containter. If the value that is associated to the provided key is an object, then you will get a reference to that object and any change made to that object will effectively modify it inside the Containter.
43
+ * @returns Returns the element associated with the specified key. If no element is associated with the specified key, undefined is returned.
44
+ */
45
+ get(key) {
46
+ const uniqueKey = this.computedUniqueKey(key);
47
+ return this.container.get(uniqueKey);
48
+ }
49
+ /**
50
+ * Adds a new element with a specified key and value to the storage. If an element with the same key already exists, the element will be updated.
51
+ */
52
+ async set(key, value) {
53
+ const uniqueKey = this.computedUniqueKey(key);
54
+ await this.container.set(uniqueKey, value);
55
+ return this;
56
+ }
57
+ /**
58
+ * @returns boolean indicating whether an element with the specified key exists or not.
59
+ */
60
+ has(key) {
61
+ const uniqueKey = this.computedUniqueKey(key);
62
+ return this.container.has(uniqueKey);
63
+ }
64
+ delete(key) {
65
+ const uniqueKey = this.computedUniqueKey(key);
66
+ return this.container.delete(uniqueKey);
67
+ }
68
+ async clear() {
69
+ var _this_keys, _this;
70
+ const keys = await ((_this_keys = (_this = this).keys) === null || _this_keys === void 0 ? void 0 : _this_keys.call(_this));
71
+ await Promise.all((keys === null || keys === void 0 ? void 0 : keys.map(async (key) => {
72
+ return this.delete(key);
73
+ })) || []);
74
+ }
75
+ forEach(fallbackFn) {
76
+ var _this_container_forEach, _this_container;
77
+ (_this_container_forEach = (_this_container = this.container).forEach) === null || _this_container_forEach === void 0 ? void 0 : _this_container_forEach.call(_this_container, (v, k) => {
78
+ if (this.checkIsOwnkey(k)) {
79
+ fallbackFn(v, k, this);
80
+ }
81
+ });
82
+ }
83
+ computedUniqueKey(k) {
84
+ return `${this.namespace}:${k}`;
85
+ }
86
+ checkIsOwnkey(k) {
87
+ return k.startsWith(this.namespace);
88
+ }
89
+ constructor(namespace, containter) {
90
+ this.namespace = namespace;
91
+ this.container = containter;
92
+ }
93
+ }
94
+ // Annotate the CommonJS export names for ESM import in node:
95
+ 0 && (module.exports = {
96
+ Storage
97
+ });
@@ -0,0 +1,106 @@
1
+ import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
+ import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
3
+ import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
4
+ import Fs from "@modern-js/utils/fs-extra";
5
+ import { createMemoryStorage } from "./storer";
6
+ var FileReader = /* @__PURE__ */ function() {
7
+ "use strict";
8
+ function FileReader2(storage) {
9
+ _class_call_check(this, FileReader2);
10
+ this.fs = Fs;
11
+ this.storage = storage;
12
+ }
13
+ var _proto = FileReader2.prototype;
14
+ _proto.readFile = function readFile(path) {
15
+ var encoding = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : "utf-8";
16
+ var _this = this;
17
+ return _async_to_generator(function() {
18
+ var fs, cache, isExistFile, content;
19
+ return _ts_generator(this, function(_state) {
20
+ switch (_state.label) {
21
+ case 0:
22
+ fs = _this.fs;
23
+ return [
24
+ 4,
25
+ _this.storage.get(path)
26
+ ];
27
+ case 1:
28
+ cache = _state.sent();
29
+ if (cache === null) {
30
+ return [
31
+ 2,
32
+ null
33
+ ];
34
+ }
35
+ if (cache) {
36
+ return [
37
+ 2,
38
+ _this.encodingContent(cache, encoding)
39
+ ];
40
+ }
41
+ return [
42
+ 4,
43
+ new Promise(function(resolve) {
44
+ fs.stat(path, function(err, stats) {
45
+ if (err) {
46
+ resolve(false);
47
+ return;
48
+ }
49
+ if (stats.isFile()) {
50
+ resolve(true);
51
+ } else {
52
+ resolve(false);
53
+ }
54
+ });
55
+ })
56
+ ];
57
+ case 2:
58
+ isExistFile = _state.sent();
59
+ if (!isExistFile)
60
+ return [
61
+ 3,
62
+ 4
63
+ ];
64
+ return [
65
+ 4,
66
+ fs.promises.readFile(path)
67
+ ];
68
+ case 3:
69
+ content = _state.sent();
70
+ _this.storage.set(path, content);
71
+ return [
72
+ 2,
73
+ _this.encodingContent(content, encoding)
74
+ ];
75
+ case 4:
76
+ _this.storage.set(path, null);
77
+ return [
78
+ 2,
79
+ null
80
+ ];
81
+ case 5:
82
+ return [
83
+ 2
84
+ ];
85
+ }
86
+ });
87
+ })();
88
+ };
89
+ _proto.reset = function reset(fs) {
90
+ var _this_storage_clear, _this_storage;
91
+ fs && (this.fs = fs);
92
+ return (_this_storage_clear = (_this_storage = this.storage).clear) === null || _this_storage_clear === void 0 ? void 0 : _this_storage_clear.call(_this_storage);
93
+ };
94
+ _proto.encodingContent = function encodingContent(value, encoding) {
95
+ if (encoding === "utf-8") {
96
+ return value.toString();
97
+ }
98
+ return value;
99
+ };
100
+ return FileReader2;
101
+ }();
102
+ var fileReader = new FileReader(createMemoryStorage("__file__system"));
103
+ export {
104
+ FileReader,
105
+ fileReader
106
+ };
@@ -0,0 +1,91 @@
1
+ import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
+ import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
3
+ import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
4
+ import LRU from "lru-cache";
5
+ var MemoryContainer = /* @__PURE__ */ function() {
6
+ "use strict";
7
+ function MemoryContainer2() {
8
+ var _ref = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {}, max = _ref.max, maxAge = _ref.maxAge;
9
+ _class_call_check(this, MemoryContainer2);
10
+ this.cache = new LRU({
11
+ max: (max || 256) * MemoryContainer2.MB,
12
+ maxAge: maxAge || MemoryContainer2.hour
13
+ });
14
+ }
15
+ var _proto = MemoryContainer2.prototype;
16
+ _proto.get = function get(key) {
17
+ var _this = this;
18
+ return _async_to_generator(function() {
19
+ return _ts_generator(this, function(_state) {
20
+ return [
21
+ 2,
22
+ _this.cache.get(key)
23
+ ];
24
+ });
25
+ })();
26
+ };
27
+ _proto.set = function set(key, value) {
28
+ var _this = this;
29
+ return _async_to_generator(function() {
30
+ return _ts_generator(this, function(_state) {
31
+ _this.cache.set(key, value);
32
+ return [
33
+ 2,
34
+ _this
35
+ ];
36
+ });
37
+ })();
38
+ };
39
+ _proto.has = function has(key) {
40
+ var _this = this;
41
+ return _async_to_generator(function() {
42
+ return _ts_generator(this, function(_state) {
43
+ return [
44
+ 2,
45
+ _this.cache.has(key)
46
+ ];
47
+ });
48
+ })();
49
+ };
50
+ _proto.delete = function _delete(key) {
51
+ var _this = this;
52
+ return _async_to_generator(function() {
53
+ var exist;
54
+ return _ts_generator(this, function(_state) {
55
+ switch (_state.label) {
56
+ case 0:
57
+ return [
58
+ 4,
59
+ _this.has(key)
60
+ ];
61
+ case 1:
62
+ exist = _state.sent();
63
+ if (exist) {
64
+ _this.cache.del(key);
65
+ }
66
+ return [
67
+ 2,
68
+ exist
69
+ ];
70
+ }
71
+ });
72
+ })();
73
+ };
74
+ _proto.forEach = function forEach(callbackFn) {
75
+ var _this = this;
76
+ this.cache.forEach(function(value, key) {
77
+ callbackFn(value, key, _this);
78
+ });
79
+ };
80
+ return MemoryContainer2;
81
+ }();
82
+ MemoryContainer.BYTE = 1;
83
+ MemoryContainer.KB = 1024 * MemoryContainer.BYTE;
84
+ MemoryContainer.MB = 1024 * MemoryContainer.KB;
85
+ MemoryContainer.ms = 1;
86
+ MemoryContainer.second = MemoryContainer.ms * 1e3;
87
+ MemoryContainer.minute = MemoryContainer.second * 60;
88
+ MemoryContainer.hour = MemoryContainer.minute * 60;
89
+ export {
90
+ MemoryContainer
91
+ };
@@ -0,0 +1,11 @@
1
+ import { MemoryContainer } from "./container";
2
+ import { Storage } from "./storage";
3
+ import { Storage as Storage2 } from "./storage";
4
+ var memoryContainer = new MemoryContainer();
5
+ function createMemoryStorage(namespace) {
6
+ return new Storage(namespace, memoryContainer);
7
+ }
8
+ export {
9
+ Storage2 as Storage,
10
+ createMemoryStorage
11
+ };
@@ -0,0 +1,135 @@
1
+ import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
+ import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
3
+ import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
4
+ var Storage = /* @__PURE__ */ function() {
5
+ "use strict";
6
+ function Storage2(namespace, containter) {
7
+ _class_call_check(this, Storage2);
8
+ this.namespace = namespace;
9
+ this.container = containter;
10
+ }
11
+ var _proto = Storage2.prototype;
12
+ _proto.keys = function keys() {
13
+ var _this = this;
14
+ return _async_to_generator(function() {
15
+ var _this_forEach, _this1, _keys;
16
+ return _ts_generator(this, function(_state) {
17
+ _keys = [];
18
+ (_this_forEach = (_this1 = _this).forEach) === null || _this_forEach === void 0 ? void 0 : _this_forEach.call(_this1, function(_, k) {
19
+ _keys.push(k);
20
+ });
21
+ return [
22
+ 2,
23
+ _keys
24
+ ];
25
+ });
26
+ })();
27
+ };
28
+ _proto.values = function values() {
29
+ var _this = this;
30
+ return _async_to_generator(function() {
31
+ var _this_forEach, _this1, _values;
32
+ return _ts_generator(this, function(_state) {
33
+ _values = [];
34
+ (_this_forEach = (_this1 = _this).forEach) === null || _this_forEach === void 0 ? void 0 : _this_forEach.call(_this1, function(v) {
35
+ _values.push(v);
36
+ });
37
+ return [
38
+ 2,
39
+ _values
40
+ ];
41
+ });
42
+ })();
43
+ };
44
+ _proto.get = function get(key) {
45
+ var uniqueKey = this.computedUniqueKey(key);
46
+ return this.container.get(uniqueKey);
47
+ };
48
+ _proto.set = function set(key, value) {
49
+ var _this = this;
50
+ return _async_to_generator(function() {
51
+ var uniqueKey;
52
+ return _ts_generator(this, function(_state) {
53
+ switch (_state.label) {
54
+ case 0:
55
+ uniqueKey = _this.computedUniqueKey(key);
56
+ return [
57
+ 4,
58
+ _this.container.set(uniqueKey, value)
59
+ ];
60
+ case 1:
61
+ _state.sent();
62
+ return [
63
+ 2,
64
+ _this
65
+ ];
66
+ }
67
+ });
68
+ })();
69
+ };
70
+ _proto.has = function has(key) {
71
+ var uniqueKey = this.computedUniqueKey(key);
72
+ return this.container.has(uniqueKey);
73
+ };
74
+ _proto.delete = function _delete(key) {
75
+ var uniqueKey = this.computedUniqueKey(key);
76
+ return this.container.delete(uniqueKey);
77
+ };
78
+ _proto.clear = function clear() {
79
+ var _this = this;
80
+ return _async_to_generator(function() {
81
+ var _this_keys, _this1, keys;
82
+ return _ts_generator(this, function(_state) {
83
+ switch (_state.label) {
84
+ case 0:
85
+ return [
86
+ 4,
87
+ (_this_keys = (_this1 = _this).keys) === null || _this_keys === void 0 ? void 0 : _this_keys.call(_this1)
88
+ ];
89
+ case 1:
90
+ keys = _state.sent();
91
+ return [
92
+ 4,
93
+ Promise.all((keys === null || keys === void 0 ? void 0 : keys.map(function() {
94
+ var _ref = _async_to_generator(function(key) {
95
+ return _ts_generator(this, function(_state2) {
96
+ return [
97
+ 2,
98
+ _this.delete(key)
99
+ ];
100
+ });
101
+ });
102
+ return function(key) {
103
+ return _ref.apply(this, arguments);
104
+ };
105
+ }())) || [])
106
+ ];
107
+ case 2:
108
+ _state.sent();
109
+ return [
110
+ 2
111
+ ];
112
+ }
113
+ });
114
+ })();
115
+ };
116
+ _proto.forEach = function forEach(fallbackFn) {
117
+ var _this = this;
118
+ var _this_container_forEach, _this_container;
119
+ (_this_container_forEach = (_this_container = this.container).forEach) === null || _this_container_forEach === void 0 ? void 0 : _this_container_forEach.call(_this_container, function(v, k) {
120
+ if (_this.checkIsOwnkey(k)) {
121
+ fallbackFn(v, k, _this);
122
+ }
123
+ });
124
+ };
125
+ _proto.computedUniqueKey = function computedUniqueKey(k) {
126
+ return "".concat(this.namespace, ":").concat(k);
127
+ };
128
+ _proto.checkIsOwnkey = function checkIsOwnkey(k) {
129
+ return k.startsWith(this.namespace);
130
+ };
131
+ return Storage2;
132
+ }();
133
+ export {
134
+ Storage
135
+ };
@@ -0,0 +1,58 @@
1
+ import Fs from "@modern-js/utils/fs-extra";
2
+ import { createMemoryStorage } from "./storer";
3
+ class FileReader {
4
+ async readFile(path, encoding = "utf-8") {
5
+ const { fs } = this;
6
+ const cache = await this.storage.get(path);
7
+ if (cache === null) {
8
+ return null;
9
+ }
10
+ if (cache) {
11
+ return this.encodingContent(cache, encoding);
12
+ }
13
+ const isExistFile = await new Promise((resolve) => {
14
+ fs.stat(path, (err, stats) => {
15
+ if (err) {
16
+ resolve(false);
17
+ return;
18
+ }
19
+ if (stats.isFile()) {
20
+ resolve(true);
21
+ } else {
22
+ resolve(false);
23
+ }
24
+ });
25
+ });
26
+ if (isExistFile) {
27
+ const content = await fs.promises.readFile(path);
28
+ this.storage.set(path, content);
29
+ return this.encodingContent(content, encoding);
30
+ } else {
31
+ this.storage.set(path, null);
32
+ return null;
33
+ }
34
+ }
35
+ /**
36
+ * Clear the fileCache entriely.
37
+ */
38
+ reset(fs) {
39
+ var _this_storage_clear, _this_storage;
40
+ fs && (this.fs = fs);
41
+ return (_this_storage_clear = (_this_storage = this.storage).clear) === null || _this_storage_clear === void 0 ? void 0 : _this_storage_clear.call(_this_storage);
42
+ }
43
+ encodingContent(value, encoding) {
44
+ if (encoding === "utf-8") {
45
+ return value.toString();
46
+ }
47
+ return value;
48
+ }
49
+ constructor(storage) {
50
+ this.fs = Fs;
51
+ this.storage = storage;
52
+ }
53
+ }
54
+ const fileReader = new FileReader(createMemoryStorage("__file__system"));
55
+ export {
56
+ FileReader,
57
+ fileReader
58
+ };
@@ -0,0 +1,41 @@
1
+ import LRU from "lru-cache";
2
+ class MemoryContainer {
3
+ async get(key) {
4
+ return this.cache.get(key);
5
+ }
6
+ async set(key, value) {
7
+ this.cache.set(key, value);
8
+ return this;
9
+ }
10
+ async has(key) {
11
+ return this.cache.has(key);
12
+ }
13
+ async delete(key) {
14
+ const exist = await this.has(key);
15
+ if (exist) {
16
+ this.cache.del(key);
17
+ }
18
+ return exist;
19
+ }
20
+ forEach(callbackFn) {
21
+ this.cache.forEach((value, key) => {
22
+ callbackFn(value, key, this);
23
+ });
24
+ }
25
+ constructor({ max, maxAge } = {}) {
26
+ this.cache = new LRU({
27
+ max: (max || 256) * MemoryContainer.MB,
28
+ maxAge: maxAge || MemoryContainer.hour
29
+ });
30
+ }
31
+ }
32
+ MemoryContainer.BYTE = 1;
33
+ MemoryContainer.KB = 1024 * MemoryContainer.BYTE;
34
+ MemoryContainer.MB = 1024 * MemoryContainer.KB;
35
+ MemoryContainer.ms = 1;
36
+ MemoryContainer.second = MemoryContainer.ms * 1e3;
37
+ MemoryContainer.minute = MemoryContainer.second * 60;
38
+ MemoryContainer.hour = MemoryContainer.minute * 60;
39
+ export {
40
+ MemoryContainer
41
+ };
@@ -0,0 +1,11 @@
1
+ import { MemoryContainer } from "./container";
2
+ import { Storage } from "./storage";
3
+ import { Storage as Storage2 } from "./storage";
4
+ const memoryContainer = new MemoryContainer();
5
+ function createMemoryStorage(namespace) {
6
+ return new Storage(namespace, memoryContainer);
7
+ }
8
+ export {
9
+ Storage2 as Storage,
10
+ createMemoryStorage
11
+ };
@@ -0,0 +1,73 @@
1
+ class Storage {
2
+ async keys() {
3
+ var _this_forEach, _this;
4
+ const _keys = [];
5
+ (_this_forEach = (_this = this).forEach) === null || _this_forEach === void 0 ? void 0 : _this_forEach.call(_this, (_, k) => {
6
+ _keys.push(k);
7
+ });
8
+ return _keys;
9
+ }
10
+ async values() {
11
+ var _this_forEach, _this;
12
+ const _values = [];
13
+ (_this_forEach = (_this = this).forEach) === null || _this_forEach === void 0 ? void 0 : _this_forEach.call(_this, (v) => {
14
+ _values.push(v);
15
+ });
16
+ return _values;
17
+ }
18
+ /**
19
+ * Returns a specified element from the containter. If the value that is associated to the provided key is an object, then you will get a reference to that object and any change made to that object will effectively modify it inside the Containter.
20
+ * @returns Returns the element associated with the specified key. If no element is associated with the specified key, undefined is returned.
21
+ */
22
+ get(key) {
23
+ const uniqueKey = this.computedUniqueKey(key);
24
+ return this.container.get(uniqueKey);
25
+ }
26
+ /**
27
+ * Adds a new element with a specified key and value to the storage. If an element with the same key already exists, the element will be updated.
28
+ */
29
+ async set(key, value) {
30
+ const uniqueKey = this.computedUniqueKey(key);
31
+ await this.container.set(uniqueKey, value);
32
+ return this;
33
+ }
34
+ /**
35
+ * @returns boolean indicating whether an element with the specified key exists or not.
36
+ */
37
+ has(key) {
38
+ const uniqueKey = this.computedUniqueKey(key);
39
+ return this.container.has(uniqueKey);
40
+ }
41
+ delete(key) {
42
+ const uniqueKey = this.computedUniqueKey(key);
43
+ return this.container.delete(uniqueKey);
44
+ }
45
+ async clear() {
46
+ var _this_keys, _this;
47
+ const keys = await ((_this_keys = (_this = this).keys) === null || _this_keys === void 0 ? void 0 : _this_keys.call(_this));
48
+ await Promise.all((keys === null || keys === void 0 ? void 0 : keys.map(async (key) => {
49
+ return this.delete(key);
50
+ })) || []);
51
+ }
52
+ forEach(fallbackFn) {
53
+ var _this_container_forEach, _this_container;
54
+ (_this_container_forEach = (_this_container = this.container).forEach) === null || _this_container_forEach === void 0 ? void 0 : _this_container_forEach.call(_this_container, (v, k) => {
55
+ if (this.checkIsOwnkey(k)) {
56
+ fallbackFn(v, k, this);
57
+ }
58
+ });
59
+ }
60
+ computedUniqueKey(k) {
61
+ return `${this.namespace}:${k}`;
62
+ }
63
+ checkIsOwnkey(k) {
64
+ return k.startsWith(this.namespace);
65
+ }
66
+ constructor(namespace, containter) {
67
+ this.namespace = namespace;
68
+ this.container = containter;
69
+ }
70
+ }
71
+ export {
72
+ Storage
73
+ };
@@ -0,0 +1,15 @@
1
+ /// <reference types="node" />
2
+ import Fs from '@modern-js/utils/fs-extra';
3
+ import { Storage } from './storer/storage';
4
+ export declare class FileReader {
5
+ private storage;
6
+ private fs;
7
+ constructor(storage: Storage<Buffer | null>);
8
+ readFile(path: string, encoding?: 'utf-8' | 'buffer'): Promise<string | Buffer | null>;
9
+ /**
10
+ * Clear the fileCache entriely.
11
+ */
12
+ reset(fs?: typeof Fs): Promise<void>;
13
+ private encodingContent;
14
+ }
15
+ export declare const fileReader: FileReader;
@@ -0,0 +1,27 @@
1
+ import { Container } from '@modern-js/types';
2
+ interface MemoryContainerOptions {
3
+ /** The maximum size of the cache, unit(MB). The default of value is 256. */
4
+ max?: number;
5
+ maxAge?: number;
6
+ }
7
+ /**
8
+ * MemoryContainer, it use lur-cache as cahe layer.
9
+ * It has a Time to Live, by default as 1 hour.
10
+ */
11
+ export declare class MemoryContainer<K, V = unknown> implements Container<K, V> {
12
+ private static BYTE;
13
+ private static KB;
14
+ private static MB;
15
+ private static ms;
16
+ private static second;
17
+ private static minute;
18
+ private static hour;
19
+ private cache;
20
+ constructor({ max, maxAge }?: MemoryContainerOptions);
21
+ get(key: K): Promise<V | undefined>;
22
+ set(key: K, value: V): Promise<this>;
23
+ has(key: K): Promise<boolean>;
24
+ delete(key: K): Promise<boolean>;
25
+ forEach(callbackFn: (v: V, k: K, container: this) => void): void;
26
+ }
27
+ export {};
@@ -0,0 +1,3 @@
1
+ import { Storage } from './storage';
2
+ export { Storage } from './storage';
3
+ export declare function createMemoryStorage<V = unknown>(namespace: string): Storage<V>;
@@ -0,0 +1,26 @@
1
+ import { Container } from '@modern-js/types';
2
+ export declare class Storage<V = unknown> {
3
+ private namespace;
4
+ private container;
5
+ constructor(namespace: string, containter: Container<string, V>);
6
+ keys?(): Promise<string[]>;
7
+ values?(): Promise<V[]>;
8
+ /**
9
+ * Returns a specified element from the containter. If the value that is associated to the provided key is an object, then you will get a reference to that object and any change made to that object will effectively modify it inside the Containter.
10
+ * @returns Returns the element associated with the specified key. If no element is associated with the specified key, undefined is returned.
11
+ */
12
+ get(key: string): Promise<V | undefined>;
13
+ /**
14
+ * Adds a new element with a specified key and value to the storage. If an element with the same key already exists, the element will be updated.
15
+ */
16
+ set(key: string, value: V): Promise<this>;
17
+ /**
18
+ * @returns boolean indicating whether an element with the specified key exists or not.
19
+ */
20
+ has(key: string): Promise<boolean>;
21
+ delete(key: string): Promise<boolean>;
22
+ clear?(): Promise<void>;
23
+ forEach?(fallbackFn: (v: V, k: string, storage: this) => void): void;
24
+ private computedUniqueKey;
25
+ private checkIsOwnkey;
26
+ }
package/package.json CHANGED
@@ -15,7 +15,7 @@
15
15
  "modern",
16
16
  "modern.js"
17
17
  ],
18
- "version": "2.42.1",
18
+ "version": "2.43.0",
19
19
  "_comment": "Provide ESM and CJS exports, ESM is used by runtime package, for treeshaking",
20
20
  "exports": {
21
21
  "./router": {
@@ -52,6 +52,16 @@
52
52
  "types": "./dist/types/parsed.d.ts",
53
53
  "require": "./dist/cjs/parsed.js",
54
54
  "default": "./dist/esm/parsed.js"
55
+ },
56
+ "./storer": {
57
+ "types": "./dist/types/node/storer/index.d.ts",
58
+ "require": "./dist/cjs/node/storer/index.js",
59
+ "default": "./dist/esm/node/storer/index.js"
60
+ },
61
+ "./fileReader": {
62
+ "types": "./dist/types/node/fileReader.d.ts",
63
+ "require": "./dist/cjs/node/fileReader.js",
64
+ "default": "./dist/esm/node/fileReader.js"
55
65
  }
56
66
  },
57
67
  "publishConfig": {
@@ -81,15 +91,22 @@
81
91
  ],
82
92
  "parsed": [
83
93
  "./dist/types/parsed.d.ts"
94
+ ],
95
+ "storer": [
96
+ "./dist/types/node/storer/index.d.ts"
97
+ ],
98
+ "fileReader": [
99
+ "./dist/types/node/fileReader.d.ts"
84
100
  ]
85
101
  }
86
102
  },
87
103
  "dependencies": {
104
+ "lru-cache": "^6.0.0",
88
105
  "serialize-javascript": "^6.0.0",
89
106
  "react-router-dom": "6.17.0",
90
107
  "@remix-run/router": "1.10.0",
91
108
  "@swc/helpers": "0.5.3",
92
- "@modern-js/utils": "2.42.1"
109
+ "@modern-js/utils": "2.43.0"
93
110
  },
94
111
  "peerDependencies": {
95
112
  "react": ">=17.0.0",
@@ -108,12 +125,13 @@
108
125
  "react-dom": "^18.2.0",
109
126
  "@types/jest": "^29",
110
127
  "@types/node": "^14",
128
+ "@types/lru-cache": "^5.1.1",
111
129
  "jest": "^29",
112
130
  "typescript": "^5",
113
131
  "@types/serialize-javascript": "^5.0.1",
114
- "@modern-js/types": "2.42.1",
115
- "@scripts/build": "2.42.1",
116
- "@scripts/jest-config": "2.42.1"
132
+ "@modern-js/types": "2.43.0",
133
+ "@scripts/build": "2.43.0",
134
+ "@scripts/jest-config": "2.43.0"
117
135
  },
118
136
  "sideEffects": false,
119
137
  "scripts": {