@modern-js/runtime-utils 0.0.0-next-20231208031249 → 0.0.0-next-20231208075552

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,80 @@
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 fileReader_exports = {};
20
+ __export(fileReader_exports, {
21
+ FileReader: () => FileReader,
22
+ fileReader: () => fileReader
23
+ });
24
+ module.exports = __toCommonJS(fileReader_exports);
25
+ var import_fs_extra = require("@modern-js/utils/fs-extra");
26
+ var import_storer = require("./storer");
27
+ class FileReader {
28
+ async readFile(path, encoding = "utf-8") {
29
+ const cache = this.storage.get(path);
30
+ if (cache === null) {
31
+ return null;
32
+ }
33
+ if (cache) {
34
+ return this.encodingContent(cache, encoding);
35
+ }
36
+ const isExistFile = await new Promise((resolve) => {
37
+ (0, import_fs_extra.stat)(path, (err, stats) => {
38
+ if (err) {
39
+ resolve(false);
40
+ return;
41
+ }
42
+ if (stats.isFile()) {
43
+ resolve(true);
44
+ } else {
45
+ resolve(false);
46
+ }
47
+ });
48
+ });
49
+ if (isExistFile) {
50
+ const content = await (0, import_fs_extra.readFile)(path);
51
+ this.storage.set(path, content);
52
+ return this.encodingContent(content, encoding);
53
+ } else {
54
+ this.storage.set(path, null);
55
+ return null;
56
+ }
57
+ }
58
+ /**
59
+ * Clear the fileCache entriely.
60
+ */
61
+ reset() {
62
+ var _this_storage_clear, _this_storage;
63
+ (_this_storage_clear = (_this_storage = this.storage).clear) === null || _this_storage_clear === void 0 ? void 0 : _this_storage_clear.call(_this_storage);
64
+ }
65
+ encodingContent(value, encoding) {
66
+ if (encoding === "utf-8") {
67
+ return value.toString();
68
+ }
69
+ return value;
70
+ }
71
+ constructor(storage) {
72
+ this.storage = storage;
73
+ }
74
+ }
75
+ const fileReader = new FileReader((0, import_storer.createMemoryStorage)("__file__system"));
76
+ // Annotate the CommonJS export names for ESM import in node:
77
+ 0 && (module.exports = {
78
+ FileReader,
79
+ fileReader
80
+ });
@@ -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
+ get(key) {
37
+ return this.cache.get(key);
38
+ }
39
+ set(key, value) {
40
+ this.cache.set(key, value);
41
+ return this;
42
+ }
43
+ has(key) {
44
+ return this.cache.has(key);
45
+ }
46
+ delete(key) {
47
+ const exist = 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
+ 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
+ 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
+ set(key, value) {
53
+ const uniqueKey = this.computedUniqueKey(key);
54
+ 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
+ clear() {
69
+ var _this_keys, _this;
70
+ const keys = (_this_keys = (_this = this).keys) === null || _this_keys === void 0 ? void 0 : _this_keys.call(_this);
71
+ keys === null || keys === void 0 ? void 0 : keys.forEach((key) => {
72
+ 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,98 @@
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 { readFile, stat } 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.storage = storage;
11
+ }
12
+ var _proto = FileReader2.prototype;
13
+ _proto.readFile = function readFile1(path) {
14
+ var encoding = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : "utf-8";
15
+ var _this = this;
16
+ return _async_to_generator(function() {
17
+ var cache, isExistFile, content;
18
+ return _ts_generator(this, function(_state) {
19
+ switch (_state.label) {
20
+ case 0:
21
+ cache = _this.storage.get(path);
22
+ if (cache === null) {
23
+ return [
24
+ 2,
25
+ null
26
+ ];
27
+ }
28
+ if (cache) {
29
+ return [
30
+ 2,
31
+ _this.encodingContent(cache, encoding)
32
+ ];
33
+ }
34
+ return [
35
+ 4,
36
+ new Promise(function(resolve) {
37
+ stat(path, function(err, stats) {
38
+ if (err) {
39
+ resolve(false);
40
+ return;
41
+ }
42
+ if (stats.isFile()) {
43
+ resolve(true);
44
+ } else {
45
+ resolve(false);
46
+ }
47
+ });
48
+ })
49
+ ];
50
+ case 1:
51
+ isExistFile = _state.sent();
52
+ if (!isExistFile)
53
+ return [
54
+ 3,
55
+ 3
56
+ ];
57
+ return [
58
+ 4,
59
+ readFile(path)
60
+ ];
61
+ case 2:
62
+ content = _state.sent();
63
+ _this.storage.set(path, content);
64
+ return [
65
+ 2,
66
+ _this.encodingContent(content, encoding)
67
+ ];
68
+ case 3:
69
+ _this.storage.set(path, null);
70
+ return [
71
+ 2,
72
+ null
73
+ ];
74
+ case 4:
75
+ return [
76
+ 2
77
+ ];
78
+ }
79
+ });
80
+ })();
81
+ };
82
+ _proto.reset = function reset() {
83
+ var _this_storage_clear, _this_storage;
84
+ (_this_storage_clear = (_this_storage = this.storage).clear) === null || _this_storage_clear === void 0 ? void 0 : _this_storage_clear.call(_this_storage);
85
+ };
86
+ _proto.encodingContent = function encodingContent(value, encoding) {
87
+ if (encoding === "utf-8") {
88
+ return value.toString();
89
+ }
90
+ return value;
91
+ };
92
+ return FileReader2;
93
+ }();
94
+ var fileReader = new FileReader(createMemoryStorage("__file__system"));
95
+ export {
96
+ FileReader,
97
+ fileReader
98
+ };
@@ -0,0 +1,48 @@
1
+ import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
2
+ import LRU from "lru-cache";
3
+ var MemoryContainer = /* @__PURE__ */ function() {
4
+ "use strict";
5
+ function MemoryContainer2() {
6
+ var _ref = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {}, max = _ref.max, maxAge = _ref.maxAge;
7
+ _class_call_check(this, MemoryContainer2);
8
+ this.cache = new LRU({
9
+ max: (max || 256) * MemoryContainer2.MB,
10
+ maxAge: maxAge || MemoryContainer2.hour
11
+ });
12
+ }
13
+ var _proto = MemoryContainer2.prototype;
14
+ _proto.get = function get(key) {
15
+ return this.cache.get(key);
16
+ };
17
+ _proto.set = function set(key, value) {
18
+ this.cache.set(key, value);
19
+ return this;
20
+ };
21
+ _proto.has = function has(key) {
22
+ return this.cache.has(key);
23
+ };
24
+ _proto.delete = function _delete(key) {
25
+ var exist = this.has(key);
26
+ if (exist) {
27
+ this.cache.del(key);
28
+ }
29
+ return exist;
30
+ };
31
+ _proto.forEach = function forEach(callbackFn) {
32
+ var _this = this;
33
+ this.cache.forEach(function(value, key) {
34
+ callbackFn(value, key, _this);
35
+ });
36
+ };
37
+ return MemoryContainer2;
38
+ }();
39
+ MemoryContainer.BYTE = 1;
40
+ MemoryContainer.KB = 1024 * MemoryContainer.BYTE;
41
+ MemoryContainer.MB = 1024 * MemoryContainer.KB;
42
+ MemoryContainer.ms = 1;
43
+ MemoryContainer.second = MemoryContainer.ms * 1e3;
44
+ MemoryContainer.minute = MemoryContainer.second * 60;
45
+ MemoryContainer.hour = MemoryContainer.minute * 60;
46
+ export {
47
+ MemoryContainer
48
+ };
@@ -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,70 @@
1
+ import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
2
+ var Storage = /* @__PURE__ */ function() {
3
+ "use strict";
4
+ function Storage2(namespace, containter) {
5
+ _class_call_check(this, Storage2);
6
+ this.namespace = namespace;
7
+ this.container = containter;
8
+ }
9
+ var _proto = Storage2.prototype;
10
+ _proto.keys = function keys() {
11
+ var _this_forEach, _this;
12
+ var _keys = [];
13
+ (_this_forEach = (_this = this).forEach) === null || _this_forEach === void 0 ? void 0 : _this_forEach.call(_this, function(_, k) {
14
+ _keys.push(k);
15
+ });
16
+ return _keys;
17
+ };
18
+ _proto.values = function values() {
19
+ var _this_forEach, _this;
20
+ var _values = [];
21
+ (_this_forEach = (_this = this).forEach) === null || _this_forEach === void 0 ? void 0 : _this_forEach.call(_this, function(v) {
22
+ _values.push(v);
23
+ });
24
+ return _values;
25
+ };
26
+ _proto.get = function get(key) {
27
+ var uniqueKey = this.computedUniqueKey(key);
28
+ return this.container.get(uniqueKey);
29
+ };
30
+ _proto.set = function set(key, value) {
31
+ var uniqueKey = this.computedUniqueKey(key);
32
+ this.container.set(uniqueKey, value);
33
+ return this;
34
+ };
35
+ _proto.has = function has(key) {
36
+ var uniqueKey = this.computedUniqueKey(key);
37
+ return this.container.has(uniqueKey);
38
+ };
39
+ _proto.delete = function _delete(key) {
40
+ var uniqueKey = this.computedUniqueKey(key);
41
+ return this.container.delete(uniqueKey);
42
+ };
43
+ _proto.clear = function clear() {
44
+ var _this = this;
45
+ var _this_keys, _this1;
46
+ var keys = (_this_keys = (_this1 = this).keys) === null || _this_keys === void 0 ? void 0 : _this_keys.call(_this1);
47
+ keys === null || keys === void 0 ? void 0 : keys.forEach(function(key) {
48
+ _this.delete(key);
49
+ });
50
+ };
51
+ _proto.forEach = function forEach(fallbackFn) {
52
+ var _this = this;
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, function(v, k) {
55
+ if (_this.checkIsOwnkey(k)) {
56
+ fallbackFn(v, k, _this);
57
+ }
58
+ });
59
+ };
60
+ _proto.computedUniqueKey = function computedUniqueKey(k) {
61
+ return "".concat(this.namespace, ":").concat(k);
62
+ };
63
+ _proto.checkIsOwnkey = function checkIsOwnkey(k) {
64
+ return k.startsWith(this.namespace);
65
+ };
66
+ return Storage2;
67
+ }();
68
+ export {
69
+ Storage
70
+ };
@@ -0,0 +1,55 @@
1
+ import { readFile, stat } from "@modern-js/utils/fs-extra";
2
+ import { createMemoryStorage } from "./storer";
3
+ class FileReader {
4
+ async readFile(path, encoding = "utf-8") {
5
+ const cache = this.storage.get(path);
6
+ if (cache === null) {
7
+ return null;
8
+ }
9
+ if (cache) {
10
+ return this.encodingContent(cache, encoding);
11
+ }
12
+ const isExistFile = await new Promise((resolve) => {
13
+ stat(path, (err, stats) => {
14
+ if (err) {
15
+ resolve(false);
16
+ return;
17
+ }
18
+ if (stats.isFile()) {
19
+ resolve(true);
20
+ } else {
21
+ resolve(false);
22
+ }
23
+ });
24
+ });
25
+ if (isExistFile) {
26
+ const content = await readFile(path);
27
+ this.storage.set(path, content);
28
+ return this.encodingContent(content, encoding);
29
+ } else {
30
+ this.storage.set(path, null);
31
+ return null;
32
+ }
33
+ }
34
+ /**
35
+ * Clear the fileCache entriely.
36
+ */
37
+ reset() {
38
+ var _this_storage_clear, _this_storage;
39
+ (_this_storage_clear = (_this_storage = this.storage).clear) === null || _this_storage_clear === void 0 ? void 0 : _this_storage_clear.call(_this_storage);
40
+ }
41
+ encodingContent(value, encoding) {
42
+ if (encoding === "utf-8") {
43
+ return value.toString();
44
+ }
45
+ return value;
46
+ }
47
+ constructor(storage) {
48
+ this.storage = storage;
49
+ }
50
+ }
51
+ const fileReader = new FileReader(createMemoryStorage("__file__system"));
52
+ export {
53
+ FileReader,
54
+ fileReader
55
+ };
@@ -0,0 +1,41 @@
1
+ import LRU from "lru-cache";
2
+ class MemoryContainer {
3
+ get(key) {
4
+ return this.cache.get(key);
5
+ }
6
+ set(key, value) {
7
+ this.cache.set(key, value);
8
+ return this;
9
+ }
10
+ has(key) {
11
+ return this.cache.has(key);
12
+ }
13
+ delete(key) {
14
+ const exist = 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
+ 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
+ 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
+ set(key, value) {
30
+ const uniqueKey = this.computedUniqueKey(key);
31
+ 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
+ clear() {
46
+ var _this_keys, _this;
47
+ const keys = (_this_keys = (_this = this).keys) === null || _this_keys === void 0 ? void 0 : _this_keys.call(_this);
48
+ keys === null || keys === void 0 ? void 0 : keys.forEach((key) => {
49
+ 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,13 @@
1
+ /// <reference types="node" />
2
+ import { Storage } from './storer/storage';
3
+ export declare class FileReader {
4
+ private storage;
5
+ constructor(storage: Storage<Buffer | null>);
6
+ readFile(path: string, encoding?: 'utf-8' | 'buffer'): Promise<string | Buffer | null>;
7
+ /**
8
+ * Clear the fileCache entriely.
9
+ */
10
+ reset(): void;
11
+ private encodingContent;
12
+ }
13
+ 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): V | undefined;
22
+ set(key: K, value: V): this;
23
+ has(key: K): boolean;
24
+ delete(key: K): 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?(): string[];
7
+ values?(): 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): 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): this;
17
+ /**
18
+ * @returns boolean indicating whether an element with the specified key exists or not.
19
+ */
20
+ has(key: string): boolean;
21
+ delete(key: string): boolean;
22
+ clear?(): 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": "0.0.0-next-20231208031249",
18
+ "version": "0.0.0-next-20231208075552",
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": "0.0.0-next-20231208031249"
109
+ "@modern-js/utils": "0.0.0-next-20231208075552"
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": "0.0.0-next-20231208031249",
115
- "@scripts/build": "0.0.0-next-20231208031249",
116
- "@scripts/jest-config": "0.0.0-next-20231208031249"
132
+ "@modern-js/types": "0.0.0-next-20231208075552",
133
+ "@scripts/build": "0.0.0-next-20231208075552",
134
+ "@scripts/jest-config": "0.0.0-next-20231208075552"
117
135
  },
118
136
  "sideEffects": false,
119
137
  "scripts": {