@soga/lowdb 0.5.0 → 1.0.1
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.
- package/dist/index.d.mts +1 -3
- package/dist/index.d.ts +1 -3
- package/dist/index.js +1 -414
- package/dist/index.mjs +1 -31
- package/package.json +9 -5
- package/dist/node-S32WFKE4.mjs +0 -286
package/dist/index.d.mts
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import * as _________node_modules_lowdb_lib_core_Low_js from '../../../node_modules/lowdb/lib/core/Low.js';
|
|
2
|
-
import * as lowdb_node from 'lowdb/node';
|
|
3
2
|
|
|
4
|
-
declare function getJsonFilePreset(): Promise<typeof lowdb_node.JSONFilePreset>;
|
|
5
3
|
declare function getDb<T = any>(filepath: string, defaultData?: T): Promise<_________node_modules_lowdb_lib_core_Low_js.Low<T>>;
|
|
6
4
|
type LowType<T = any> = Awaited<ReturnType<typeof getDb<T>>>;
|
|
7
5
|
|
|
8
|
-
export { type LowType, getDb
|
|
6
|
+
export { type LowType, getDb };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import * as _________node_modules_lowdb_lib_core_Low_js from '../../../node_modules/lowdb/lib/core/Low.js';
|
|
2
|
-
import * as lowdb_node from 'lowdb/node';
|
|
3
2
|
|
|
4
|
-
declare function getJsonFilePreset(): Promise<typeof lowdb_node.JSONFilePreset>;
|
|
5
3
|
declare function getDb<T = any>(filepath: string, defaultData?: T): Promise<_________node_modules_lowdb_lib_core_Low_js.Low<T>>;
|
|
6
4
|
type LowType<T = any> = Awaited<ReturnType<typeof getDb<T>>>;
|
|
7
5
|
|
|
8
|
-
export { type LowType, getDb
|
|
6
|
+
export { type LowType, getDb };
|
package/dist/index.js
CHANGED
|
@@ -1,414 +1 @@
|
|
|
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 __esm = (fn, res) => function __init() {
|
|
9
|
-
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
10
|
-
};
|
|
11
|
-
var __export = (target, all) => {
|
|
12
|
-
for (var name in all)
|
|
13
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
14
|
-
};
|
|
15
|
-
var __copyProps = (to, from, except, desc) => {
|
|
16
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
17
|
-
for (let key of __getOwnPropNames(from))
|
|
18
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
19
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
20
|
-
}
|
|
21
|
-
return to;
|
|
22
|
-
};
|
|
23
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
24
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
25
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
26
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
27
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
28
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
29
|
-
mod
|
|
30
|
-
));
|
|
31
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
32
|
-
|
|
33
|
-
// ../../node_modules/steno/lib/index.js
|
|
34
|
-
function getTempFilename(file) {
|
|
35
|
-
const f = file instanceof URL ? (0, import_node_url.fileURLToPath)(file) : file.toString();
|
|
36
|
-
return (0, import_node_path.join)((0, import_node_path.dirname)(f), `.${(0, import_node_path.basename)(f)}.tmp`);
|
|
37
|
-
}
|
|
38
|
-
async function retryAsyncOperation(fn, maxRetries, delayMs) {
|
|
39
|
-
for (let i = 0; i < maxRetries; i++) {
|
|
40
|
-
try {
|
|
41
|
-
return await fn();
|
|
42
|
-
} catch (error) {
|
|
43
|
-
if (i < maxRetries - 1) {
|
|
44
|
-
await new Promise((resolve) => setTimeout(resolve, delayMs));
|
|
45
|
-
} else {
|
|
46
|
-
throw error;
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
var import_promises, import_node_path, import_node_url, Writer;
|
|
52
|
-
var init_lib = __esm({
|
|
53
|
-
"../../node_modules/steno/lib/index.js"() {
|
|
54
|
-
"use strict";
|
|
55
|
-
import_promises = require("fs/promises");
|
|
56
|
-
import_node_path = require("path");
|
|
57
|
-
import_node_url = require("url");
|
|
58
|
-
Writer = class {
|
|
59
|
-
#filename;
|
|
60
|
-
#tempFilename;
|
|
61
|
-
#locked = false;
|
|
62
|
-
#prev = null;
|
|
63
|
-
#next = null;
|
|
64
|
-
#nextPromise = null;
|
|
65
|
-
#nextData = null;
|
|
66
|
-
// File is locked, add data for later
|
|
67
|
-
#add(data) {
|
|
68
|
-
this.#nextData = data;
|
|
69
|
-
this.#nextPromise ||= new Promise((resolve, reject) => {
|
|
70
|
-
this.#next = [resolve, reject];
|
|
71
|
-
});
|
|
72
|
-
return new Promise((resolve, reject) => {
|
|
73
|
-
this.#nextPromise?.then(resolve).catch(reject);
|
|
74
|
-
});
|
|
75
|
-
}
|
|
76
|
-
// File isn't locked, write data
|
|
77
|
-
async #write(data) {
|
|
78
|
-
this.#locked = true;
|
|
79
|
-
try {
|
|
80
|
-
await (0, import_promises.writeFile)(this.#tempFilename, data, "utf-8");
|
|
81
|
-
await retryAsyncOperation(async () => {
|
|
82
|
-
await (0, import_promises.rename)(this.#tempFilename, this.#filename);
|
|
83
|
-
}, 10, 100);
|
|
84
|
-
this.#prev?.[0]();
|
|
85
|
-
} catch (err) {
|
|
86
|
-
if (err instanceof Error) {
|
|
87
|
-
this.#prev?.[1](err);
|
|
88
|
-
}
|
|
89
|
-
throw err;
|
|
90
|
-
} finally {
|
|
91
|
-
this.#locked = false;
|
|
92
|
-
this.#prev = this.#next;
|
|
93
|
-
this.#next = this.#nextPromise = null;
|
|
94
|
-
if (this.#nextData !== null) {
|
|
95
|
-
const nextData = this.#nextData;
|
|
96
|
-
this.#nextData = null;
|
|
97
|
-
await this.write(nextData);
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
constructor(filename) {
|
|
102
|
-
this.#filename = filename;
|
|
103
|
-
this.#tempFilename = getTempFilename(filename);
|
|
104
|
-
}
|
|
105
|
-
async write(data) {
|
|
106
|
-
return this.#locked ? this.#add(data) : this.#write(data);
|
|
107
|
-
}
|
|
108
|
-
};
|
|
109
|
-
}
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
// ../../node_modules/lowdb/lib/adapters/node/TextFile.js
|
|
113
|
-
var import_node_fs, import_promises2, import_node_path2, TextFile, TextFileSync;
|
|
114
|
-
var init_TextFile = __esm({
|
|
115
|
-
"../../node_modules/lowdb/lib/adapters/node/TextFile.js"() {
|
|
116
|
-
"use strict";
|
|
117
|
-
import_node_fs = require("fs");
|
|
118
|
-
import_promises2 = require("fs/promises");
|
|
119
|
-
import_node_path2 = __toESM(require("path"), 1);
|
|
120
|
-
init_lib();
|
|
121
|
-
TextFile = class {
|
|
122
|
-
#filename;
|
|
123
|
-
#writer;
|
|
124
|
-
constructor(filename) {
|
|
125
|
-
this.#filename = filename;
|
|
126
|
-
this.#writer = new Writer(filename);
|
|
127
|
-
}
|
|
128
|
-
async read() {
|
|
129
|
-
let data;
|
|
130
|
-
try {
|
|
131
|
-
data = await (0, import_promises2.readFile)(this.#filename, "utf-8");
|
|
132
|
-
} catch (e) {
|
|
133
|
-
if (e.code === "ENOENT") {
|
|
134
|
-
return null;
|
|
135
|
-
}
|
|
136
|
-
throw e;
|
|
137
|
-
}
|
|
138
|
-
return data;
|
|
139
|
-
}
|
|
140
|
-
write(str) {
|
|
141
|
-
return this.#writer.write(str);
|
|
142
|
-
}
|
|
143
|
-
};
|
|
144
|
-
TextFileSync = class {
|
|
145
|
-
#tempFilename;
|
|
146
|
-
#filename;
|
|
147
|
-
constructor(filename) {
|
|
148
|
-
this.#filename = filename;
|
|
149
|
-
const f = filename.toString();
|
|
150
|
-
this.#tempFilename = import_node_path2.default.join(import_node_path2.default.dirname(f), `.${import_node_path2.default.basename(f)}.tmp`);
|
|
151
|
-
}
|
|
152
|
-
read() {
|
|
153
|
-
let data;
|
|
154
|
-
try {
|
|
155
|
-
data = (0, import_node_fs.readFileSync)(this.#filename, "utf-8");
|
|
156
|
-
} catch (e) {
|
|
157
|
-
if (e.code === "ENOENT") {
|
|
158
|
-
return null;
|
|
159
|
-
}
|
|
160
|
-
throw e;
|
|
161
|
-
}
|
|
162
|
-
return data;
|
|
163
|
-
}
|
|
164
|
-
write(str) {
|
|
165
|
-
(0, import_node_fs.writeFileSync)(this.#tempFilename, str);
|
|
166
|
-
(0, import_node_fs.renameSync)(this.#tempFilename, this.#filename);
|
|
167
|
-
}
|
|
168
|
-
};
|
|
169
|
-
}
|
|
170
|
-
});
|
|
171
|
-
|
|
172
|
-
// ../../node_modules/lowdb/lib/adapters/node/DataFile.js
|
|
173
|
-
var DataFile, DataFileSync;
|
|
174
|
-
var init_DataFile = __esm({
|
|
175
|
-
"../../node_modules/lowdb/lib/adapters/node/DataFile.js"() {
|
|
176
|
-
"use strict";
|
|
177
|
-
init_TextFile();
|
|
178
|
-
DataFile = class {
|
|
179
|
-
#adapter;
|
|
180
|
-
#parse;
|
|
181
|
-
#stringify;
|
|
182
|
-
constructor(filename, { parse, stringify }) {
|
|
183
|
-
this.#adapter = new TextFile(filename);
|
|
184
|
-
this.#parse = parse;
|
|
185
|
-
this.#stringify = stringify;
|
|
186
|
-
}
|
|
187
|
-
async read() {
|
|
188
|
-
const data = await this.#adapter.read();
|
|
189
|
-
if (data === null) {
|
|
190
|
-
return null;
|
|
191
|
-
} else {
|
|
192
|
-
return this.#parse(data);
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
write(obj) {
|
|
196
|
-
return this.#adapter.write(this.#stringify(obj));
|
|
197
|
-
}
|
|
198
|
-
};
|
|
199
|
-
DataFileSync = class {
|
|
200
|
-
#adapter;
|
|
201
|
-
#parse;
|
|
202
|
-
#stringify;
|
|
203
|
-
constructor(filename, { parse, stringify }) {
|
|
204
|
-
this.#adapter = new TextFileSync(filename);
|
|
205
|
-
this.#parse = parse;
|
|
206
|
-
this.#stringify = stringify;
|
|
207
|
-
}
|
|
208
|
-
read() {
|
|
209
|
-
const data = this.#adapter.read();
|
|
210
|
-
if (data === null) {
|
|
211
|
-
return null;
|
|
212
|
-
} else {
|
|
213
|
-
return this.#parse(data);
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
write(obj) {
|
|
217
|
-
this.#adapter.write(this.#stringify(obj));
|
|
218
|
-
}
|
|
219
|
-
};
|
|
220
|
-
}
|
|
221
|
-
});
|
|
222
|
-
|
|
223
|
-
// ../../node_modules/lowdb/lib/adapters/node/JSONFile.js
|
|
224
|
-
var JSONFile, JSONFileSync;
|
|
225
|
-
var init_JSONFile = __esm({
|
|
226
|
-
"../../node_modules/lowdb/lib/adapters/node/JSONFile.js"() {
|
|
227
|
-
"use strict";
|
|
228
|
-
init_DataFile();
|
|
229
|
-
JSONFile = class extends DataFile {
|
|
230
|
-
constructor(filename) {
|
|
231
|
-
super(filename, {
|
|
232
|
-
parse: JSON.parse,
|
|
233
|
-
stringify: (data) => JSON.stringify(data, null, 2)
|
|
234
|
-
});
|
|
235
|
-
}
|
|
236
|
-
};
|
|
237
|
-
JSONFileSync = class extends DataFileSync {
|
|
238
|
-
constructor(filename) {
|
|
239
|
-
super(filename, {
|
|
240
|
-
parse: JSON.parse,
|
|
241
|
-
stringify: (data) => JSON.stringify(data, null, 2)
|
|
242
|
-
});
|
|
243
|
-
}
|
|
244
|
-
};
|
|
245
|
-
}
|
|
246
|
-
});
|
|
247
|
-
|
|
248
|
-
// ../../node_modules/lowdb/lib/adapters/Memory.js
|
|
249
|
-
var Memory, MemorySync;
|
|
250
|
-
var init_Memory = __esm({
|
|
251
|
-
"../../node_modules/lowdb/lib/adapters/Memory.js"() {
|
|
252
|
-
"use strict";
|
|
253
|
-
Memory = class {
|
|
254
|
-
#data = null;
|
|
255
|
-
read() {
|
|
256
|
-
return Promise.resolve(this.#data);
|
|
257
|
-
}
|
|
258
|
-
write(obj) {
|
|
259
|
-
this.#data = obj;
|
|
260
|
-
return Promise.resolve();
|
|
261
|
-
}
|
|
262
|
-
};
|
|
263
|
-
MemorySync = class {
|
|
264
|
-
#data = null;
|
|
265
|
-
read() {
|
|
266
|
-
return this.#data || null;
|
|
267
|
-
}
|
|
268
|
-
write(obj) {
|
|
269
|
-
this.#data = obj;
|
|
270
|
-
}
|
|
271
|
-
};
|
|
272
|
-
}
|
|
273
|
-
});
|
|
274
|
-
|
|
275
|
-
// ../../node_modules/lowdb/lib/core/Low.js
|
|
276
|
-
function checkArgs(adapter, defaultData) {
|
|
277
|
-
if (adapter === void 0)
|
|
278
|
-
throw new Error("lowdb: missing adapter");
|
|
279
|
-
if (defaultData === void 0)
|
|
280
|
-
throw new Error("lowdb: missing default data");
|
|
281
|
-
}
|
|
282
|
-
var Low, LowSync;
|
|
283
|
-
var init_Low = __esm({
|
|
284
|
-
"../../node_modules/lowdb/lib/core/Low.js"() {
|
|
285
|
-
"use strict";
|
|
286
|
-
Low = class {
|
|
287
|
-
adapter;
|
|
288
|
-
data;
|
|
289
|
-
constructor(adapter, defaultData) {
|
|
290
|
-
checkArgs(adapter, defaultData);
|
|
291
|
-
this.adapter = adapter;
|
|
292
|
-
this.data = defaultData;
|
|
293
|
-
}
|
|
294
|
-
async read() {
|
|
295
|
-
const data = await this.adapter.read();
|
|
296
|
-
if (data)
|
|
297
|
-
this.data = data;
|
|
298
|
-
}
|
|
299
|
-
async write() {
|
|
300
|
-
if (this.data)
|
|
301
|
-
await this.adapter.write(this.data);
|
|
302
|
-
}
|
|
303
|
-
async update(fn) {
|
|
304
|
-
fn(this.data);
|
|
305
|
-
await this.write();
|
|
306
|
-
}
|
|
307
|
-
};
|
|
308
|
-
LowSync = class {
|
|
309
|
-
adapter;
|
|
310
|
-
data;
|
|
311
|
-
constructor(adapter, defaultData) {
|
|
312
|
-
checkArgs(adapter, defaultData);
|
|
313
|
-
this.adapter = adapter;
|
|
314
|
-
this.data = defaultData;
|
|
315
|
-
}
|
|
316
|
-
read() {
|
|
317
|
-
const data = this.adapter.read();
|
|
318
|
-
if (data)
|
|
319
|
-
this.data = data;
|
|
320
|
-
}
|
|
321
|
-
write() {
|
|
322
|
-
if (this.data)
|
|
323
|
-
this.adapter.write(this.data);
|
|
324
|
-
}
|
|
325
|
-
update(fn) {
|
|
326
|
-
fn(this.data);
|
|
327
|
-
this.write();
|
|
328
|
-
}
|
|
329
|
-
};
|
|
330
|
-
}
|
|
331
|
-
});
|
|
332
|
-
|
|
333
|
-
// ../../node_modules/lowdb/lib/presets/node.js
|
|
334
|
-
async function JSONFilePreset(filename, defaultData) {
|
|
335
|
-
const adapter = process.env.NODE_ENV === "test" ? new Memory() : new JSONFile(filename);
|
|
336
|
-
const db = new Low(adapter, defaultData);
|
|
337
|
-
await db.read();
|
|
338
|
-
return db;
|
|
339
|
-
}
|
|
340
|
-
function JSONFileSyncPreset(filename, defaultData) {
|
|
341
|
-
const adapter = process.env.NODE_ENV === "test" ? new MemorySync() : new JSONFileSync(filename);
|
|
342
|
-
const db = new LowSync(adapter, defaultData);
|
|
343
|
-
db.read();
|
|
344
|
-
return db;
|
|
345
|
-
}
|
|
346
|
-
var init_node = __esm({
|
|
347
|
-
"../../node_modules/lowdb/lib/presets/node.js"() {
|
|
348
|
-
"use strict";
|
|
349
|
-
init_Memory();
|
|
350
|
-
init_JSONFile();
|
|
351
|
-
init_Low();
|
|
352
|
-
}
|
|
353
|
-
});
|
|
354
|
-
|
|
355
|
-
// ../../node_modules/lowdb/lib/node.js
|
|
356
|
-
var node_exports = {};
|
|
357
|
-
__export(node_exports, {
|
|
358
|
-
DataFile: () => DataFile,
|
|
359
|
-
DataFileSync: () => DataFileSync,
|
|
360
|
-
JSONFile: () => JSONFile,
|
|
361
|
-
JSONFilePreset: () => JSONFilePreset,
|
|
362
|
-
JSONFileSync: () => JSONFileSync,
|
|
363
|
-
JSONFileSyncPreset: () => JSONFileSyncPreset,
|
|
364
|
-
TextFile: () => TextFile,
|
|
365
|
-
TextFileSync: () => TextFileSync
|
|
366
|
-
});
|
|
367
|
-
var init_node2 = __esm({
|
|
368
|
-
"../../node_modules/lowdb/lib/node.js"() {
|
|
369
|
-
"use strict";
|
|
370
|
-
init_DataFile();
|
|
371
|
-
init_JSONFile();
|
|
372
|
-
init_TextFile();
|
|
373
|
-
init_node();
|
|
374
|
-
}
|
|
375
|
-
});
|
|
376
|
-
|
|
377
|
-
// src/index.ts
|
|
378
|
-
var index_exports = {};
|
|
379
|
-
__export(index_exports, {
|
|
380
|
-
getDb: () => getDb,
|
|
381
|
-
getJsonFilePreset: () => getJsonFilePreset
|
|
382
|
-
});
|
|
383
|
-
module.exports = __toCommonJS(index_exports);
|
|
384
|
-
var import_fs_extra = require("fs-extra");
|
|
385
|
-
var import_path = require("path");
|
|
386
|
-
var lowdb_map = {
|
|
387
|
-
json_file_preset_instance: null,
|
|
388
|
-
json_file_preset_status: false
|
|
389
|
-
};
|
|
390
|
-
async function getJsonFilePreset() {
|
|
391
|
-
while (lowdb_map.json_file_preset_status) {
|
|
392
|
-
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
393
|
-
}
|
|
394
|
-
if (lowdb_map.json_file_preset_instance) {
|
|
395
|
-
return lowdb_map.json_file_preset_instance;
|
|
396
|
-
}
|
|
397
|
-
lowdb_map.json_file_preset_status = true;
|
|
398
|
-
const { JSONFilePreset: JSONFilePreset2 } = await Promise.resolve().then(() => (init_node2(), node_exports));
|
|
399
|
-
lowdb_map.json_file_preset_instance = JSONFilePreset2;
|
|
400
|
-
lowdb_map.json_file_preset_status = false;
|
|
401
|
-
return JSONFilePreset2;
|
|
402
|
-
}
|
|
403
|
-
async function getDb(filepath, defaultData = {}) {
|
|
404
|
-
const jsonFilePrest = await getJsonFilePreset();
|
|
405
|
-
const folder = (0, import_path.dirname)(filepath);
|
|
406
|
-
await (0, import_fs_extra.ensureDir)(folder);
|
|
407
|
-
const db = await jsonFilePrest(filepath, defaultData);
|
|
408
|
-
return db;
|
|
409
|
-
}
|
|
410
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
411
|
-
0 && (module.exports = {
|
|
412
|
-
getDb,
|
|
413
|
-
getJsonFilePreset
|
|
414
|
-
});
|
|
1
|
+
"use strict";var e,r=Object.defineProperty,t=Object.getOwnPropertyDescriptor,o=Object.getOwnPropertyNames,a=Object.prototype.hasOwnProperty,i={};((e,t)=>{for(var o in t)r(e,o,{get:t[o],enumerable:!0})})(i,{getDb:()=>b}),module.exports=(e=i,((e,i,n,u)=>{if(i&&"object"==typeof i||"function"==typeof i)for(let l of o(i))a.call(e,l)||l===n||r(e,l,{get:()=>i[l],enumerable:!(u=t(i,l))||u.enumerable});return e})(r({},"__esModule",{value:!0}),e));var n=require("fs-extra"),u=require("path"),l=require("lowdb/node"),s=require("@soga/error");async function b(e,r={}){try{const t=(0,u.dirname)(e);await(0,n.ensureDir)(t);return await(0,l.JSONFilePreset)(e,r)}catch(r){throw(0,s.buildDError)(r,{rewritable:!0,message:"Failed to use lowdb",detail:`Failed to use lowdb: ${e}`})}}
|
package/dist/index.mjs
CHANGED
|
@@ -1,31 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import { ensureDir } from "fs-extra";
|
|
3
|
-
import { dirname } from "path";
|
|
4
|
-
var lowdb_map = {
|
|
5
|
-
json_file_preset_instance: null,
|
|
6
|
-
json_file_preset_status: false
|
|
7
|
-
};
|
|
8
|
-
async function getJsonFilePreset() {
|
|
9
|
-
while (lowdb_map.json_file_preset_status) {
|
|
10
|
-
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
11
|
-
}
|
|
12
|
-
if (lowdb_map.json_file_preset_instance) {
|
|
13
|
-
return lowdb_map.json_file_preset_instance;
|
|
14
|
-
}
|
|
15
|
-
lowdb_map.json_file_preset_status = true;
|
|
16
|
-
const { JSONFilePreset } = await import("./node-S32WFKE4.mjs");
|
|
17
|
-
lowdb_map.json_file_preset_instance = JSONFilePreset;
|
|
18
|
-
lowdb_map.json_file_preset_status = false;
|
|
19
|
-
return JSONFilePreset;
|
|
20
|
-
}
|
|
21
|
-
async function getDb(filepath, defaultData = {}) {
|
|
22
|
-
const jsonFilePrest = await getJsonFilePreset();
|
|
23
|
-
const folder = dirname(filepath);
|
|
24
|
-
await ensureDir(folder);
|
|
25
|
-
const db = await jsonFilePrest(filepath, defaultData);
|
|
26
|
-
return db;
|
|
27
|
-
}
|
|
28
|
-
export {
|
|
29
|
-
getDb,
|
|
30
|
-
getJsonFilePreset
|
|
31
|
-
};
|
|
1
|
+
import{ensureDir as o}from"fs-extra";import{dirname as r}from"path";import{JSONFilePreset as t}from"lowdb/node";import{buildDError as a}from"@soga/error";async function e(e,i={}){try{const a=r(e);await o(a);return await t(e,i)}catch(o){throw a(o,{rewritable:!0,message:"Failed to use lowdb",detail:`Failed to use lowdb: ${e}`})}}export{e as getDb};
|
package/package.json
CHANGED
|
@@ -3,13 +3,13 @@
|
|
|
3
3
|
"publishConfig": {
|
|
4
4
|
"access": "public"
|
|
5
5
|
},
|
|
6
|
-
"version": "0.
|
|
6
|
+
"version": "1.0.1",
|
|
7
7
|
"main": "./dist/index.js",
|
|
8
8
|
"module": "./dist/index.mjs",
|
|
9
9
|
"types": "./dist/index.d.ts",
|
|
10
10
|
"scripts": {
|
|
11
|
-
"
|
|
12
|
-
"
|
|
11
|
+
"demo": "tsx ./demo/demo.ts",
|
|
12
|
+
"build": "rimraf dist && tsup src/index.ts --format cjs,esm --dts --minify terser",
|
|
13
13
|
"prepublishOnly": "npm run build"
|
|
14
14
|
},
|
|
15
15
|
"files": [
|
|
@@ -20,11 +20,15 @@
|
|
|
20
20
|
"license": "ISC",
|
|
21
21
|
"description": "",
|
|
22
22
|
"dependencies": {
|
|
23
|
+
"@soga/error": "^1.0.7",
|
|
23
24
|
"fs-extra": "^11.3.0",
|
|
24
|
-
"
|
|
25
|
+
"lowdb": "^7.0.1"
|
|
25
26
|
},
|
|
26
27
|
"devDependencies": {
|
|
27
28
|
"@types/fs-extra": "^11.0.4",
|
|
28
|
-
"rimraf": "^6.0.1"
|
|
29
|
+
"rimraf": "^6.0.1",
|
|
30
|
+
"terser": "^5.43.1",
|
|
31
|
+
"tsup": "^8.5.0",
|
|
32
|
+
"tsx": "^4.20.3"
|
|
29
33
|
}
|
|
30
34
|
}
|
package/dist/node-S32WFKE4.mjs
DELETED
|
@@ -1,286 +0,0 @@
|
|
|
1
|
-
// ../../node_modules/lowdb/lib/adapters/node/TextFile.js
|
|
2
|
-
import { readFileSync, renameSync, writeFileSync } from "fs";
|
|
3
|
-
import { readFile } from "fs/promises";
|
|
4
|
-
import path from "path";
|
|
5
|
-
|
|
6
|
-
// ../../node_modules/steno/lib/index.js
|
|
7
|
-
import { rename, writeFile } from "fs/promises";
|
|
8
|
-
import { basename, dirname, join } from "path";
|
|
9
|
-
import { fileURLToPath } from "url";
|
|
10
|
-
function getTempFilename(file) {
|
|
11
|
-
const f = file instanceof URL ? fileURLToPath(file) : file.toString();
|
|
12
|
-
return join(dirname(f), `.${basename(f)}.tmp`);
|
|
13
|
-
}
|
|
14
|
-
async function retryAsyncOperation(fn, maxRetries, delayMs) {
|
|
15
|
-
for (let i = 0; i < maxRetries; i++) {
|
|
16
|
-
try {
|
|
17
|
-
return await fn();
|
|
18
|
-
} catch (error) {
|
|
19
|
-
if (i < maxRetries - 1) {
|
|
20
|
-
await new Promise((resolve) => setTimeout(resolve, delayMs));
|
|
21
|
-
} else {
|
|
22
|
-
throw error;
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
var Writer = class {
|
|
28
|
-
#filename;
|
|
29
|
-
#tempFilename;
|
|
30
|
-
#locked = false;
|
|
31
|
-
#prev = null;
|
|
32
|
-
#next = null;
|
|
33
|
-
#nextPromise = null;
|
|
34
|
-
#nextData = null;
|
|
35
|
-
// File is locked, add data for later
|
|
36
|
-
#add(data) {
|
|
37
|
-
this.#nextData = data;
|
|
38
|
-
this.#nextPromise ||= new Promise((resolve, reject) => {
|
|
39
|
-
this.#next = [resolve, reject];
|
|
40
|
-
});
|
|
41
|
-
return new Promise((resolve, reject) => {
|
|
42
|
-
this.#nextPromise?.then(resolve).catch(reject);
|
|
43
|
-
});
|
|
44
|
-
}
|
|
45
|
-
// File isn't locked, write data
|
|
46
|
-
async #write(data) {
|
|
47
|
-
this.#locked = true;
|
|
48
|
-
try {
|
|
49
|
-
await writeFile(this.#tempFilename, data, "utf-8");
|
|
50
|
-
await retryAsyncOperation(async () => {
|
|
51
|
-
await rename(this.#tempFilename, this.#filename);
|
|
52
|
-
}, 10, 100);
|
|
53
|
-
this.#prev?.[0]();
|
|
54
|
-
} catch (err) {
|
|
55
|
-
if (err instanceof Error) {
|
|
56
|
-
this.#prev?.[1](err);
|
|
57
|
-
}
|
|
58
|
-
throw err;
|
|
59
|
-
} finally {
|
|
60
|
-
this.#locked = false;
|
|
61
|
-
this.#prev = this.#next;
|
|
62
|
-
this.#next = this.#nextPromise = null;
|
|
63
|
-
if (this.#nextData !== null) {
|
|
64
|
-
const nextData = this.#nextData;
|
|
65
|
-
this.#nextData = null;
|
|
66
|
-
await this.write(nextData);
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
constructor(filename) {
|
|
71
|
-
this.#filename = filename;
|
|
72
|
-
this.#tempFilename = getTempFilename(filename);
|
|
73
|
-
}
|
|
74
|
-
async write(data) {
|
|
75
|
-
return this.#locked ? this.#add(data) : this.#write(data);
|
|
76
|
-
}
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
// ../../node_modules/lowdb/lib/adapters/node/TextFile.js
|
|
80
|
-
var TextFile = class {
|
|
81
|
-
#filename;
|
|
82
|
-
#writer;
|
|
83
|
-
constructor(filename) {
|
|
84
|
-
this.#filename = filename;
|
|
85
|
-
this.#writer = new Writer(filename);
|
|
86
|
-
}
|
|
87
|
-
async read() {
|
|
88
|
-
let data;
|
|
89
|
-
try {
|
|
90
|
-
data = await readFile(this.#filename, "utf-8");
|
|
91
|
-
} catch (e) {
|
|
92
|
-
if (e.code === "ENOENT") {
|
|
93
|
-
return null;
|
|
94
|
-
}
|
|
95
|
-
throw e;
|
|
96
|
-
}
|
|
97
|
-
return data;
|
|
98
|
-
}
|
|
99
|
-
write(str) {
|
|
100
|
-
return this.#writer.write(str);
|
|
101
|
-
}
|
|
102
|
-
};
|
|
103
|
-
var TextFileSync = class {
|
|
104
|
-
#tempFilename;
|
|
105
|
-
#filename;
|
|
106
|
-
constructor(filename) {
|
|
107
|
-
this.#filename = filename;
|
|
108
|
-
const f = filename.toString();
|
|
109
|
-
this.#tempFilename = path.join(path.dirname(f), `.${path.basename(f)}.tmp`);
|
|
110
|
-
}
|
|
111
|
-
read() {
|
|
112
|
-
let data;
|
|
113
|
-
try {
|
|
114
|
-
data = readFileSync(this.#filename, "utf-8");
|
|
115
|
-
} catch (e) {
|
|
116
|
-
if (e.code === "ENOENT") {
|
|
117
|
-
return null;
|
|
118
|
-
}
|
|
119
|
-
throw e;
|
|
120
|
-
}
|
|
121
|
-
return data;
|
|
122
|
-
}
|
|
123
|
-
write(str) {
|
|
124
|
-
writeFileSync(this.#tempFilename, str);
|
|
125
|
-
renameSync(this.#tempFilename, this.#filename);
|
|
126
|
-
}
|
|
127
|
-
};
|
|
128
|
-
|
|
129
|
-
// ../../node_modules/lowdb/lib/adapters/node/DataFile.js
|
|
130
|
-
var DataFile = class {
|
|
131
|
-
#adapter;
|
|
132
|
-
#parse;
|
|
133
|
-
#stringify;
|
|
134
|
-
constructor(filename, { parse, stringify }) {
|
|
135
|
-
this.#adapter = new TextFile(filename);
|
|
136
|
-
this.#parse = parse;
|
|
137
|
-
this.#stringify = stringify;
|
|
138
|
-
}
|
|
139
|
-
async read() {
|
|
140
|
-
const data = await this.#adapter.read();
|
|
141
|
-
if (data === null) {
|
|
142
|
-
return null;
|
|
143
|
-
} else {
|
|
144
|
-
return this.#parse(data);
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
write(obj) {
|
|
148
|
-
return this.#adapter.write(this.#stringify(obj));
|
|
149
|
-
}
|
|
150
|
-
};
|
|
151
|
-
var DataFileSync = class {
|
|
152
|
-
#adapter;
|
|
153
|
-
#parse;
|
|
154
|
-
#stringify;
|
|
155
|
-
constructor(filename, { parse, stringify }) {
|
|
156
|
-
this.#adapter = new TextFileSync(filename);
|
|
157
|
-
this.#parse = parse;
|
|
158
|
-
this.#stringify = stringify;
|
|
159
|
-
}
|
|
160
|
-
read() {
|
|
161
|
-
const data = this.#adapter.read();
|
|
162
|
-
if (data === null) {
|
|
163
|
-
return null;
|
|
164
|
-
} else {
|
|
165
|
-
return this.#parse(data);
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
write(obj) {
|
|
169
|
-
this.#adapter.write(this.#stringify(obj));
|
|
170
|
-
}
|
|
171
|
-
};
|
|
172
|
-
|
|
173
|
-
// ../../node_modules/lowdb/lib/adapters/node/JSONFile.js
|
|
174
|
-
var JSONFile = class extends DataFile {
|
|
175
|
-
constructor(filename) {
|
|
176
|
-
super(filename, {
|
|
177
|
-
parse: JSON.parse,
|
|
178
|
-
stringify: (data) => JSON.stringify(data, null, 2)
|
|
179
|
-
});
|
|
180
|
-
}
|
|
181
|
-
};
|
|
182
|
-
var JSONFileSync = class extends DataFileSync {
|
|
183
|
-
constructor(filename) {
|
|
184
|
-
super(filename, {
|
|
185
|
-
parse: JSON.parse,
|
|
186
|
-
stringify: (data) => JSON.stringify(data, null, 2)
|
|
187
|
-
});
|
|
188
|
-
}
|
|
189
|
-
};
|
|
190
|
-
|
|
191
|
-
// ../../node_modules/lowdb/lib/adapters/Memory.js
|
|
192
|
-
var Memory = class {
|
|
193
|
-
#data = null;
|
|
194
|
-
read() {
|
|
195
|
-
return Promise.resolve(this.#data);
|
|
196
|
-
}
|
|
197
|
-
write(obj) {
|
|
198
|
-
this.#data = obj;
|
|
199
|
-
return Promise.resolve();
|
|
200
|
-
}
|
|
201
|
-
};
|
|
202
|
-
var MemorySync = class {
|
|
203
|
-
#data = null;
|
|
204
|
-
read() {
|
|
205
|
-
return this.#data || null;
|
|
206
|
-
}
|
|
207
|
-
write(obj) {
|
|
208
|
-
this.#data = obj;
|
|
209
|
-
}
|
|
210
|
-
};
|
|
211
|
-
|
|
212
|
-
// ../../node_modules/lowdb/lib/core/Low.js
|
|
213
|
-
function checkArgs(adapter, defaultData) {
|
|
214
|
-
if (adapter === void 0)
|
|
215
|
-
throw new Error("lowdb: missing adapter");
|
|
216
|
-
if (defaultData === void 0)
|
|
217
|
-
throw new Error("lowdb: missing default data");
|
|
218
|
-
}
|
|
219
|
-
var Low = class {
|
|
220
|
-
adapter;
|
|
221
|
-
data;
|
|
222
|
-
constructor(adapter, defaultData) {
|
|
223
|
-
checkArgs(adapter, defaultData);
|
|
224
|
-
this.adapter = adapter;
|
|
225
|
-
this.data = defaultData;
|
|
226
|
-
}
|
|
227
|
-
async read() {
|
|
228
|
-
const data = await this.adapter.read();
|
|
229
|
-
if (data)
|
|
230
|
-
this.data = data;
|
|
231
|
-
}
|
|
232
|
-
async write() {
|
|
233
|
-
if (this.data)
|
|
234
|
-
await this.adapter.write(this.data);
|
|
235
|
-
}
|
|
236
|
-
async update(fn) {
|
|
237
|
-
fn(this.data);
|
|
238
|
-
await this.write();
|
|
239
|
-
}
|
|
240
|
-
};
|
|
241
|
-
var LowSync = class {
|
|
242
|
-
adapter;
|
|
243
|
-
data;
|
|
244
|
-
constructor(adapter, defaultData) {
|
|
245
|
-
checkArgs(adapter, defaultData);
|
|
246
|
-
this.adapter = adapter;
|
|
247
|
-
this.data = defaultData;
|
|
248
|
-
}
|
|
249
|
-
read() {
|
|
250
|
-
const data = this.adapter.read();
|
|
251
|
-
if (data)
|
|
252
|
-
this.data = data;
|
|
253
|
-
}
|
|
254
|
-
write() {
|
|
255
|
-
if (this.data)
|
|
256
|
-
this.adapter.write(this.data);
|
|
257
|
-
}
|
|
258
|
-
update(fn) {
|
|
259
|
-
fn(this.data);
|
|
260
|
-
this.write();
|
|
261
|
-
}
|
|
262
|
-
};
|
|
263
|
-
|
|
264
|
-
// ../../node_modules/lowdb/lib/presets/node.js
|
|
265
|
-
async function JSONFilePreset(filename, defaultData) {
|
|
266
|
-
const adapter = process.env.NODE_ENV === "test" ? new Memory() : new JSONFile(filename);
|
|
267
|
-
const db = new Low(adapter, defaultData);
|
|
268
|
-
await db.read();
|
|
269
|
-
return db;
|
|
270
|
-
}
|
|
271
|
-
function JSONFileSyncPreset(filename, defaultData) {
|
|
272
|
-
const adapter = process.env.NODE_ENV === "test" ? new MemorySync() : new JSONFileSync(filename);
|
|
273
|
-
const db = new LowSync(adapter, defaultData);
|
|
274
|
-
db.read();
|
|
275
|
-
return db;
|
|
276
|
-
}
|
|
277
|
-
export {
|
|
278
|
-
DataFile,
|
|
279
|
-
DataFileSync,
|
|
280
|
-
JSONFile,
|
|
281
|
-
JSONFilePreset,
|
|
282
|
-
JSONFileSync,
|
|
283
|
-
JSONFileSyncPreset,
|
|
284
|
-
TextFile,
|
|
285
|
-
TextFileSync
|
|
286
|
-
};
|