UNPKG

4.01 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const tslib_1 = require("tslib");
4const path = require("path");
5const fs = require("async-file");
6const file = require("fs");
7const filenamify = require("filenamify");
8/**
9 * :package: **botbuilder**
10 *
11 * A file based storage provider. Items will be persisted to a folder on disk.
12 *
13 * **Usage Example**
14 *
15 * ```JavaScript
16 * const { FileStorage } = require('botbuilder');
17 * const path = require('path');
18 *
19 * const storage = new FileStorage(path.join(__dirname, './state'));
20 * ```
21 */
22class FileStorage {
23 /**
24 * Creates a new FileStorage instance.
25 * @param path Root filesystem path for where the provider should store its items.
26 */
27 constructor(filePath) {
28 this.path = filePath;
29 }
30 read(keys) {
31 return tslib_1.__awaiter(this, void 0, void 0, function* () {
32 yield this.ensureFolder();
33 const data = {};
34 const promises = keys.map(key => {
35 const filePath = this.getFilePath(key);
36 return parseFile(filePath)
37 .then((obj) => {
38 if (obj) {
39 data[key] = obj;
40 }
41 });
42 });
43 yield Promise.all(promises);
44 return data;
45 });
46 }
47 write(changes) {
48 return tslib_1.__awaiter(this, void 0, void 0, function* () {
49 yield this.ensureFolder();
50 const promises = Object.keys(changes).map(key => {
51 const filePath = this.getFilePath(key);
52 return fs.exists(filePath)
53 .then((exists) => {
54 const newObj = Object.assign({}, changes[key]);
55 newObj.eTag = (parseInt(newObj.eTag || '0', 10) + 1).toString();
56 return fs.writeTextFile(filePath, JSON.stringify(newObj));
57 });
58 });
59 yield Promise.all(promises);
60 });
61 }
62 delete(keys) {
63 return tslib_1.__awaiter(this, void 0, void 0, function* () {
64 yield this.ensureFolder();
65 const promises = keys.map(key => {
66 const filePath = this.getFilePath(key);
67 return fs.exists(filePath)
68 .then((exists) => {
69 if (exists) {
70 file.unlinkSync(filePath);
71 }
72 });
73 });
74 yield Promise.all(promises);
75 });
76 }
77 ensureFolder() {
78 return tslib_1.__awaiter(this, void 0, void 0, function* () {
79 if (this.pEnsureFolder) {
80 return this.pEnsureFolder;
81 }
82 const exists = yield fs.exists(this.path);
83 if (!exists) {
84 this.pEnsureFolder = fs.mkdirp(this.path);
85 }
86 });
87 }
88 getFileName(key) {
89 return filenamify(key);
90 }
91 getFilePath(key) {
92 return path.join(this.path, this.getFileName(key));
93 }
94}
95FileStorage.nextTag = 0;
96exports.FileStorage = FileStorage;
97function parseFile(filePath) {
98 return tslib_1.__awaiter(this, void 0, void 0, function* () {
99 try {
100 const exists = yield fs.exists(filePath);
101 const data = yield (exists
102 ? fs.readTextFile(filePath)
103 : Promise.resolve(undefined));
104 try {
105 if (data) {
106 return JSON.parse(data);
107 }
108 }
109 catch (err) {
110 console.warn(`FileStorage: error parsing "${filePath}": ${err.toString()}`);
111 }
112 return undefined;
113 }
114 catch (error) {
115 // File could legitimately have been deleted
116 if (error.code != "ENOENT") {
117 console.warn(`FileStorage: error reading "${filePath}": ${error.toString()}`);
118 }
119 return undefined;
120 }
121 });
122}
123//# sourceMappingURL=FileStorage.js.map
\No newline at end of file