wenay-common 1.0.205 → 1.0.207

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,34 @@
1
+ export type saveKeyValue = ReturnType<typeof saveKeyValue>;
2
+ export declare function saveKeyValue({ dirDef, key: _key }: {
3
+ dirDef: string;
4
+ key?: string;
5
+ }): {
6
+ get: ({ key, path }?: {
7
+ key?: string | undefined;
8
+ path?: string | undefined;
9
+ }) => Promise<string>;
10
+ has: ({ key, path }?: {
11
+ key?: string | undefined;
12
+ path?: string | undefined;
13
+ }) => Promise<boolean>;
14
+ set: (args_0: {
15
+ key?: string;
16
+ obj: string;
17
+ path?: string;
18
+ }) => Promise<void>;
19
+ setElMap: (args_0: {
20
+ key?: string;
21
+ keyEl: string;
22
+ valueEl: any;
23
+ path?: string;
24
+ }) => Promise<void>;
25
+ delEl: (args_0: {
26
+ key?: string;
27
+ keyEl: string;
28
+ path?: string;
29
+ }) => Promise<boolean>;
30
+ del: ({ key, path }?: {
31
+ key?: string | undefined;
32
+ path?: string | undefined;
33
+ }) => Promise<void>;
34
+ };
@@ -0,0 +1,87 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.saveKeyValue = saveKeyValue;
37
+ const fs = __importStar(require("fs"));
38
+ const wenay_common_1 = require("wenay-common");
39
+ function saveKeyValue({ dirDef = "", key: _key = "" }) {
40
+ async function ensureDir(dir) {
41
+ const fullDir = dirDef ? `${dirDef}/${dir}` : dir;
42
+ await fs.promises.mkdir(fullDir, { recursive: true });
43
+ return `${fullDir}/`;
44
+ }
45
+ const getPath = (path = "") => (dirDef ? `${dirDef}/${path}` : path);
46
+ async function get({ key = _key, path = "" } = {}) {
47
+ const fullPath = await ensureDir(path);
48
+ return fs.promises.readFile(`${fullPath}${key}`, "utf-8");
49
+ }
50
+ async function has({ key = _key, path = "" } = {}) {
51
+ return fs.promises.access(`${getPath(path)}/${key}`)
52
+ .then(() => true)
53
+ .catch(() => false);
54
+ }
55
+ async function set({ key = _key, obj, path = "" }) {
56
+ const fullPath = await ensureDir(path);
57
+ await fs.promises.writeFile(`${fullPath}${key}`, obj);
58
+ }
59
+ async function setElMap({ key = _key, keyEl, valueEl, path = "" }) {
60
+ const exists = await has({ key, path });
61
+ const data = exists ? JSON.parse(await get({ key, path })) : {};
62
+ data[keyEl] = valueEl;
63
+ await set({ key, obj: JSON.stringify(data), path });
64
+ }
65
+ async function delEl({ key = _key, keyEl, path = "" }) {
66
+ if (!(await has({ key, path })))
67
+ throw new Error(`Файл не найден: ${path}${key}`);
68
+ const data = JSON.parse(await get({ key, path }));
69
+ delete data[keyEl];
70
+ await set({ key, obj: JSON.stringify(data), path });
71
+ return true;
72
+ }
73
+ async function del({ key = _key, path = "" } = {}) {
74
+ const fullPath = getPath(path);
75
+ await fs.promises.rm(`${fullPath}/${key}`);
76
+ }
77
+ const queue = (0, wenay_common_1.createAsyncQueue)(1);
78
+ const queueFunc = (f) => (0, wenay_common_1.Transformer)(f, ([args, func]) => queue.enqueue(() => func(...args)));
79
+ return {
80
+ get,
81
+ has,
82
+ set: queueFunc(set),
83
+ setElMap: queueFunc(setElMap),
84
+ delEl: queueFunc(delEl),
85
+ del,
86
+ };
87
+ }
@@ -37,7 +37,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
37
37
  };
38
38
  Object.defineProperty(exports, "__esModule", { value: true });
39
39
  exports.createWebhookClient = exports.createWebhookServer = exports.apiSaveData = void 0;
40
- const express = __importStar(require("express"));
40
+ const express_1 = __importDefault(require("express"));
41
41
  const axios_1 = __importDefault(require("axios"));
42
42
  const fs = __importStar(require("fs"));
43
43
  const wenay_common_1 = require("wenay-common");
@@ -65,9 +65,9 @@ exports.apiSaveData = {
65
65
  saveSubscribers
66
66
  };
67
67
  const createWebhookServer = (params) => {
68
- const app = params.app ?? express();
68
+ const app = params.app ?? (0, express_1.default)();
69
69
  if (!params.app)
70
- app.use(express.json());
70
+ app.use(express_1.default.json());
71
71
  const file = params.file ?? exports.apiSaveData;
72
72
  const { authToken, port } = params;
73
73
  const subscribers = file.loadSubscribers();
@@ -148,9 +148,9 @@ const createWebhookServer = (params) => {
148
148
  exports.createWebhookServer = createWebhookServer;
149
149
  const createWebhookClient = (options) => {
150
150
  const { app: app_, serverUrl, clientPort, authToken, autoRenew = false, renewIntervalMs = 86400000 } = options;
151
- const app = app_ ? app_ : express();
151
+ const app = app_ ? app_ : (0, express_1.default)();
152
152
  if (!app_)
153
- app.use(express.json());
153
+ app.use(express_1.default.json());
154
154
  const handlers = {};
155
155
  const activeTags = new Set();
156
156
  const connect = async (tag, handler) => {
package/lib/index.d.ts CHANGED
@@ -15,6 +15,7 @@ export * from "./Common/objectPath";
15
15
  export * from "./Common/waitRun";
16
16
  export * from "./Common/Listen";
17
17
  export * from "./toError/myThrow";
18
+ export * from "./Common/fsKeyVolume";
18
19
  export * from "./Common/webHook2";
19
20
  export * from "./Exchange/Bars";
20
21
  export * from "./Exchange/LoadBase";
package/lib/index.js CHANGED
@@ -55,6 +55,7 @@ __exportStar(require("./Common/objectPath"), exports);
55
55
  __exportStar(require("./Common/waitRun"), exports);
56
56
  __exportStar(require("./Common/Listen"), exports);
57
57
  __exportStar(require("./toError/myThrow"), exports);
58
+ __exportStar(require("./Common/fsKeyVolume"), exports);
58
59
  __exportStar(require("./Common/webHook2"), exports);
59
60
  __exportStar(require("./Exchange/Bars"), exports);
60
61
  __exportStar(require("./Exchange/LoadBase"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wenay-common",
3
- "version": "1.0.205",
3
+ "version": "1.0.207",
4
4
  "description": "Common library",
5
5
  "strict": true,
6
6
  "main": "lib/index.js",