@makano/rew 1.1.81 → 1.2.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.
Files changed (72) hide show
  1. package/lib/coffeescript/browser.js +144 -139
  2. package/lib/coffeescript/cake.js +132 -133
  3. package/lib/coffeescript/coffeescript.js +437 -381
  4. package/lib/coffeescript/command.js +806 -724
  5. package/lib/coffeescript/grammar.js +1908 -2474
  6. package/lib/coffeescript/helpers.js +509 -473
  7. package/lib/coffeescript/index.js +228 -215
  8. package/lib/coffeescript/lexer.js +2282 -1909
  9. package/lib/coffeescript/nodes.js +9782 -9202
  10. package/lib/coffeescript/optparse.js +255 -227
  11. package/lib/coffeescript/parser.js +20305 -1265
  12. package/lib/coffeescript/register.js +107 -87
  13. package/lib/coffeescript/repl.js +307 -284
  14. package/lib/coffeescript/rewriter.js +1389 -1079
  15. package/lib/coffeescript/scope.js +176 -172
  16. package/lib/coffeescript/sourcemap.js +242 -227
  17. package/lib/rew/cli/cli.js +296 -239
  18. package/lib/rew/cli/log.js +27 -27
  19. package/lib/rew/cli/run.js +8 -8
  20. package/lib/rew/cli/utils.js +353 -199
  21. package/lib/rew/const/config_path.js +2 -2
  22. package/lib/rew/const/default.js +38 -53
  23. package/lib/rew/const/files.js +11 -14
  24. package/lib/rew/const/opt.js +6 -6
  25. package/lib/rew/css/theme.css +1 -1
  26. package/lib/rew/functions/core.js +55 -55
  27. package/lib/rew/functions/curl.js +23 -0
  28. package/lib/rew/functions/emitter.js +52 -55
  29. package/lib/rew/functions/exec.js +25 -25
  30. package/lib/rew/functions/export.js +17 -17
  31. package/lib/rew/functions/fs.js +57 -59
  32. package/lib/rew/functions/future.js +29 -21
  33. package/lib/rew/functions/id.js +8 -9
  34. package/lib/rew/functions/import.js +106 -122
  35. package/lib/rew/functions/map.js +10 -10
  36. package/lib/rew/functions/match.js +35 -42
  37. package/lib/rew/functions/path.js +8 -8
  38. package/lib/rew/functions/require.js +32 -36
  39. package/lib/rew/functions/sleep.js +2 -2
  40. package/lib/rew/functions/stdout.js +18 -18
  41. package/lib/rew/functions/types.js +82 -106
  42. package/lib/rew/html/ui.html +12 -12
  43. package/lib/rew/html/ui.js +196 -201
  44. package/lib/rew/main.js +14 -14
  45. package/lib/rew/misc/bin.js +37 -0
  46. package/lib/rew/misc/findAppInfo.js +13 -13
  47. package/lib/rew/misc/findAppPath.js +15 -15
  48. package/lib/rew/misc/req.js +7 -0
  49. package/lib/rew/misc/seededid.js +8 -8
  50. package/lib/rew/models/enum.js +12 -12
  51. package/lib/rew/models/struct.js +30 -32
  52. package/lib/rew/modules/compiler.js +237 -209
  53. package/lib/rew/modules/fs.js +10 -10
  54. package/lib/rew/modules/runtime.js +17 -21
  55. package/lib/rew/modules/yaml.js +27 -30
  56. package/lib/rew/pkgs/conf.js +82 -92
  57. package/lib/rew/pkgs/data.js +10 -10
  58. package/lib/rew/pkgs/date.js +27 -27
  59. package/lib/rew/pkgs/env.js +5 -5
  60. package/lib/rew/pkgs/modules/data/bintree.js +51 -51
  61. package/lib/rew/pkgs/modules/data/doublylinked.js +84 -84
  62. package/lib/rew/pkgs/modules/data/linkedList.js +72 -72
  63. package/lib/rew/pkgs/modules/data/queue.js +18 -18
  64. package/lib/rew/pkgs/modules/data/stack.js +18 -18
  65. package/lib/rew/pkgs/modules/threads/worker.js +36 -36
  66. package/lib/rew/pkgs/modules/ui/classes.js +181 -184
  67. package/lib/rew/pkgs/pkgs.js +9 -9
  68. package/lib/rew/pkgs/rune.js +373 -410
  69. package/lib/rew/pkgs/threads.js +62 -66
  70. package/lib/rew/pkgs/ui.js +148 -160
  71. package/lib/rew/qrew/compile.js +12 -0
  72. package/package.json +4 -3
@@ -1,17 +1,15 @@
1
- const fs = require("fs");
2
- const { v4: uuidv4 } = require("uuid");
3
- const path = require("path");
4
- const msgpack = require("tiny-msgpack");
5
- const crypto = require("crypto");
6
- const { CONFIG_PATH } = require("../const/config_path");
1
+ const fs = require('fs');
2
+ const { v4: uuidv4 } = require('uuid');
3
+ const path = require('path');
4
+ const { CONFIG_PATH } = require('../const/config_path');
5
+ const { serializeData, deserializeData, gen_key } = require('../misc/bin');
7
6
 
8
- const ENCRYPTION_KEY =
9
- "e6ad8b0792b9e0472ea44d1f3adfd1d503182efcce25991b05cc5ef83f307ffc";
7
+ const ENCRYPTION_KEY = 'e6ad8b0792b9e0472ea44d1f3adfd1d503182efcce25991b05cc5ef83f307ffc';
10
8
 
11
9
  class Change {
12
- constructor(values) {
13
- this.values = values;
14
- }
10
+ constructor(values) {
11
+ this.values = values;
12
+ }
15
13
  }
16
14
 
17
15
  class PopChange extends Change {}
@@ -21,417 +19,382 @@ class PushChange extends Change {}
21
19
  const runePush = (...values) => new PushChange(values);
22
20
  const runePop = (...values) => new PopChange(values);
23
21
 
24
- function makeRef(value, props = "") {
25
- if (!value["@rune.id"]) return null;
26
- const collection = getCollectionFromID(value["@rune.id"]);
27
- const ref = collection + "." + value["@rune.id"];
28
- return "@rune.ref " + ref + props;
22
+ function makeRef(value, props = '') {
23
+ if (!value['@rune.id']) return null;
24
+ const collection = getCollectionFromID(value['@rune.id']);
25
+ const ref = collection + '.' + value['@rune.id'];
26
+ return '@rune.ref ' + ref + props;
29
27
  }
30
28
 
31
29
  const eid = (s, diff) =>
32
- s
33
- .split("")
34
- .map((i) => {
35
- let charCode = i.charCodeAt(0) + diff;
36
- if (charCode > 122) {
37
- charCode -= 26;
38
- }
39
- return String.fromCharCode(charCode);
40
- })
41
- .join("");
30
+ s
31
+ .split('')
32
+ .map((i) => {
33
+ let charCode = i.charCodeAt(0) + diff;
34
+ if (charCode > 122) {
35
+ charCode -= 26;
36
+ }
37
+ return String.fromCharCode(charCode);
38
+ })
39
+ .join('');
42
40
 
43
41
  function generateID(id, collection) {
44
- return eid(collection, 5) + "+" + id;
42
+ return eid(collection, 5) + '+' + id;
45
43
  }
46
44
 
47
45
  function getCollectionFromID(id) {
48
- return eid(id.split("+")[0], -5);
46
+ return eid(id.split('+')[0], -5);
49
47
  }
50
48
 
51
49
  const createDB = (dbName, dirname, dbData = {}, encryptionKey) => {
52
- const dbDirPath = path.join(dirname, dbName);
53
- const mainFilePath = path.join(dbDirPath, "main.bin");
54
- const algorithm = "aes-256-ctr";
55
-
56
- if (!fs.existsSync(dbDirPath)) {
57
- fs.mkdirSync(dbDirPath);
58
- }
59
-
60
- const encrypt = (data) => {
61
- const iv = crypto.randomBytes(16);
62
- const cipher = crypto.createCipheriv(
63
- algorithm,
64
- Buffer.from(encryptionKey, "hex"),
65
- iv,
66
- );
67
- const encrypted = Buffer.concat([cipher.update(data), cipher.final()]);
68
- return Buffer.concat([iv, encrypted]);
69
- };
70
-
71
- const decrypt = (data) => {
72
- const iv = data.slice(0, 16);
73
- const encryptedData = data.slice(16);
74
- const decipher = crypto.createDecipheriv(
75
- algorithm,
76
- Buffer.from(encryptionKey, "hex"),
77
- iv,
78
- );
79
- const decrypted = Buffer.concat([
80
- decipher.update(encryptedData),
81
- decipher.final(),
82
- ]);
83
- return decrypted;
84
- };
85
-
86
- const serializeData = (data) => {
87
- return msgpack.encode(data);
88
- };
89
-
90
- const deserializeData = (buffer) => {
91
- return msgpack.decode(decrypt(buffer));
92
- };
93
-
94
- const getData = () => {
95
- return readMainData().data;
96
- };
97
- getData.key = (key) => {
98
- return getData()[key];
99
- };
100
-
101
- const setData = (data) => {
102
- const newData = readMainData();
103
- for (let i in data) if (data[i] !== undefined) newData.data[i] = data[i];
104
- writeMainData(newData);
105
- };
106
-
107
- setData.key = (key, value) => {
108
- setData({ [key]: value });
109
- };
110
-
111
- setData.rm = (key) => {
112
- setData({ [key]: undefined });
113
- };
114
-
115
- setData.reset = () => {
116
- writeMainData({
117
- ...readMainData(),
118
- data: { ...dbData, name: dbName },
119
- });
120
- };
121
-
122
- const readMainData = () => {
123
- if (!fs.existsSync(mainFilePath)) {
124
- writeMainData({
125
- collections: [],
126
- data: { ...dbData, name: dbName },
127
- maps: [],
128
- });
129
- }
130
- const buffer = fs.readFileSync(mainFilePath);
131
- return deserializeData(buffer);
132
- };
133
-
134
- const writeMainData = (data) => {
135
- const buffer = encrypt(serializeData(data));
136
- fs.writeFileSync(mainFilePath, buffer);
137
- };
138
-
139
- const readDataFile = (filePath) => {
140
- const buffer = fs.readFileSync(filePath);
141
- return deserializeData(buffer);
142
- };
143
-
144
- const writeDataFile = (filePath, data) => {
145
- const buffer = encrypt(serializeData(data));
146
- fs.writeFileSync(filePath, buffer);
147
- };
148
-
149
- const collection = (collectionName) => {
150
- const collectionFilePath = path.join(dbDirPath, `${collectionName}.col`);
151
-
152
- const insert = (record) => {
153
- const mainData = readMainData();
154
- if (!mainData.collections.includes(collectionName)) {
155
- mainData.collections.push(collectionName);
156
- writeMainData(mainData);
157
- }
158
-
159
- let data = [];
160
- if (fs.existsSync(collectionFilePath)) {
161
- data = readDataFile(collectionFilePath);
162
- }
163
- const id = uuidv4();
164
- record["@rune.id"] = generateID(id, collectionName);
165
- data.push(record);
166
- writeDataFile(collectionFilePath, data);
167
- return record;
168
- };
169
-
170
- const read = (id, evaluate = true) => {
171
- if (typeof id == "object" && "@rune.id" in id) id = id["@rune.id"];
172
- if (!fs.existsSync(collectionFilePath)) return null;
173
- const data = readDataFile(collectionFilePath);
174
- const record = data.find((record) => record["@rune.id"] === id);
175
- if (record) {
176
- return evaluateRecord(record);
177
- }
178
- return null;
179
- };
180
-
181
- const evaluateRecord = (record, prevRecord) => {
182
- const evaluateValue = (val) => {
183
- if (typeof val == "string" && val.startsWith("@rune.ref")) {
184
- const ref = val.split("@rune.ref")[1].trim();
185
- const refData = findRef(ref, false);
186
- if (!refData) {
187
- return null;
188
- } else {
189
- let value = refData;
190
- if (refData["@rune.id"]) {
191
- value =
192
- prevRecord && prevRecord["@rune.id"] == refData["@rune.id"]
193
- ? prevRecord
194
- : evaluateRecord(refData, record);
195
- }
196
- return value;
197
- }
198
- }
199
- if (Array.isArray(val)) {
200
- val = val.map((i) => evaluateValue(i));
201
- }
202
- return val;
203
- };
204
- for (let i in record) {
205
- const val = record[i];
206
- record[i] = evaluateValue(val);
207
- }
208
- return record;
209
- };
210
-
211
- const update = (caseRecord, newRecord) => {
212
- let id;
213
- if (typeof caseRecord === "string") {
214
- id = caseRecord;
215
- } else if (typeof caseRecord === "object") {
216
- const data = readDataFile(collectionFilePath);
217
- const record = data.find((record) => {
218
- for (const key in caseRecord) {
219
- if (record[key] !== caseRecord[key]) return false;
220
- }
221
- return true;
222
- });
223
- if (record) {
224
- id = record["@rune.id"];
225
- } else {
226
- return null; // No matching record found
227
- }
228
- }
229
-
230
- if (!id) return null;
231
-
232
- const data = readDataFile(collectionFilePath);
233
- const index = data.findIndex((record) => record["@rune.id"] === id);
234
- if (index !== -1) {
235
- const oldRecord = data[index];
236
- for (const key in newRecord) {
237
- const value = newRecord[key];
238
- if (value instanceof PushChange) {
239
- if (!oldRecord[key] || !Array.isArray(oldRecord[key])) {
240
- oldRecord[key] = [];
241
- }
242
- oldRecord[key].push(...value.values);
243
- } else if (value instanceof PopChange) {
244
- if (oldRecord[key] && Array.isArray(oldRecord[key])) {
245
- value.values.forEach((val) => {
246
- const index = oldRecord[key].indexOf(val);
247
- if (index !== -1) {
248
- oldRecord[key].splice(index, 1);
249
- }
250
- });
251
- }
252
- } else {
253
- oldRecord[key] = value;
254
- }
255
- }
256
- data[index] = oldRecord;
257
- writeDataFile(collectionFilePath, data);
258
- return data[index];
259
- }
260
- return null;
261
- };
262
-
263
- const find = (criteria) => {
264
- if (typeof criteria == "string") return read(criteria);
265
- if (!criteria || typeof criteria !== "object") return null;
266
-
267
- const data = readDataFile(collectionFilePath);
268
- const record =
269
- data.find((record) => {
270
- for (const key in criteria) {
271
- if (record[key] !== criteria[key]) return false;
272
- }
273
- return true;
274
- }) || null;
275
- if (record) {
276
- return evaluateRecord(record);
277
- }
278
- return null;
279
- };
280
-
281
- const remove = (id) => {
282
- if ("@rune.id" in id) id = id["@rune.id"];
283
- if (!fs.existsSync(collectionFilePath)) return false;
284
- let data = readDataFile(collectionFilePath);
285
- const index = data.findIndex((record) => record["@rune.id"] === id);
286
- if (index !== -1) {
287
- data.splice(index, 1);
288
- writeDataFile(collectionFilePath, data);
289
- return true;
290
- }
291
- return false;
292
- };
293
-
294
- const list = () => {
295
- if (!fs.existsSync(collectionFilePath)) return [];
296
- const data = readDataFile(collectionFilePath);
297
- return data.map((rec) => evaluateRecord(rec));
298
- };
299
-
300
- const map = (cb, mutate = false) => {
301
- const data = readDataFile(collectionFilePath);
302
- const mappedData = data.map(cb);
303
- if (mutate) {
304
- writeDataFile(collectionFilePath, mappedData);
305
- }
306
- return mappedData;
307
- };
308
-
309
- const transform = (cb, mutate = true) => {
310
- const data = readDataFile(collectionFilePath);
311
- const transformedData = cb(data);
312
- if (mutate) {
313
- writeDataFile(collectionFilePath, transformedData);
314
- }
315
- return transformedData;
316
- };
317
-
318
- const filter = (cb, mutate = false) => {
319
- const data = readDataFile(collectionFilePath);
320
- const filteredData = data.filter(cb);
321
- if (mutate) {
322
- writeDataFile(collectionFilePath, filteredData);
323
- }
324
- return filteredData;
325
- };
326
-
327
- const sort = (cb, mutate = false) => {
328
- const data = readDataFile(collectionFilePath);
329
- const sortedData = data.sort(cb);
330
- if (mutate) {
331
- writeDataFile(collectionFilePath, sortedData);
332
- }
333
- return sortedData;
334
- };
335
-
336
- return {
337
- insert,
338
- read,
339
- update,
340
- remove,
341
- find,
342
- map,
343
- transform,
344
- filter,
345
- sort,
346
- list,
347
- };
348
- };
349
-
350
- const findRef = (ref, evaluate = true) => {
351
- const [name, id, ...rest] = ref.split(".");
352
- const col = collection(name);
353
- const record = col.read(id, evaluate);
354
- if (rest.length === 0) return record;
355
- let value = record;
356
- for (const prop of rest) {
357
- if (typeof value != "object") break;
358
- if (!(prop in value)) return null;
359
- value = value[prop];
360
- }
361
- return value;
362
- };
363
-
364
- const map = (mapName) => {
365
- const mapFilePath = path.join(dbDirPath, `${mapName}.map`);
366
-
367
- const set = (key, value) => {
368
- const mainData = readMainData();
369
- if (!mainData.maps.includes(mapName)) {
370
- mainData.maps.push(mapName);
371
- writeMainData(mainData);
372
- }
373
-
374
- let data = {};
375
- if (fs.existsSync(mapFilePath)) {
376
- data = readDataFile(mapFilePath);
377
- }
378
- data[key] = value;
379
- writeDataFile(mapFilePath, data);
380
- };
381
-
382
- const get = (key) => {
383
- if (!fs.existsSync(mapFilePath)) return null;
384
- const data = readDataFile(mapFilePath);
385
- return data[key] || null;
386
- };
387
-
388
- const remove = (key) => {
389
- if (!fs.existsSync(mapFilePath)) return false;
390
- const data = readDataFile(mapFilePath);
391
- if (data[key]) {
392
- delete data[key];
393
- writeDataFile(mapFilePath, data);
394
- return true;
395
- }
396
- return false;
397
- };
398
-
399
- const transform = (cb, mutate = false) => {
400
- const data = readDataFile(mapFilePath);
401
- const transformedData = cb(data);
402
- if (mutate) {
403
- writeDataFile(mapFilePath, transformedData);
404
- }
405
- return transformedData;
406
- };
407
-
408
- const list = () => {
409
- if (!fs.existsSync(mapFilePath)) return {};
410
- const data = readDataFile(mapFilePath);
411
- return data;
412
- };
413
-
414
- return { set, get, remove, list, transform };
415
- };
416
-
417
- readMainData();
418
-
419
- return { setData, getData, collection, findRef, makeRef, map };
50
+ const dbDirPath = path.join(dirname, dbName);
51
+ const mainFilePath = path.join(dbDirPath, 'main.bin');
52
+
53
+ if (!fs.existsSync(dbDirPath)) {
54
+ fs.mkdirSync(dbDirPath);
55
+ }
56
+
57
+ const getData = () => {
58
+ return readMainData().data;
59
+ };
60
+ getData.key = (key) => {
61
+ return getData()[key];
62
+ };
63
+
64
+ const setData = (data) => {
65
+ const newData = readMainData();
66
+ for (let i in data) if (data[i] !== undefined) newData.data[i] = data[i];
67
+ writeMainData(newData);
68
+ };
69
+
70
+ setData.key = (key, value) => {
71
+ setData({ [key]: value });
72
+ };
73
+
74
+ setData.rm = (key) => {
75
+ setData({ [key]: undefined });
76
+ };
77
+
78
+ setData.reset = () => {
79
+ writeMainData({
80
+ ...readMainData(),
81
+ data: { ...dbData, name: dbName },
82
+ });
83
+ };
84
+
85
+ const readMainData = () => {
86
+ if (!fs.existsSync(mainFilePath)) {
87
+ writeMainData({
88
+ collections: [],
89
+ data: { ...dbData, name: dbName },
90
+ maps: [],
91
+ });
92
+ }
93
+ const buffer = fs.readFileSync(mainFilePath);
94
+ return deserializeData(buffer, encryptionKey);
95
+ };
96
+
97
+ const writeMainData = (data) => {
98
+ const buffer = serializeData(data, encryptionKey);
99
+ fs.writeFileSync(mainFilePath, buffer);
100
+ };
101
+
102
+ const readDataFile = (filePath) => {
103
+ const buffer = fs.readFileSync(filePath);
104
+ return deserializeData(buffer, encryptionKey);
105
+ };
106
+
107
+ const writeDataFile = (filePath, data) => {
108
+ const buffer = serializeData(data, encryptionKey);
109
+ fs.writeFileSync(filePath, buffer);
110
+ };
111
+
112
+ const collection = (collectionName) => {
113
+ const collectionFilePath = path.join(dbDirPath, `${collectionName}.col`);
114
+
115
+ const insert = (record) => {
116
+ const mainData = readMainData();
117
+ if (!mainData.collections.includes(collectionName)) {
118
+ mainData.collections.push(collectionName);
119
+ writeMainData(mainData);
120
+ }
121
+
122
+ let data = [];
123
+ if (fs.existsSync(collectionFilePath)) {
124
+ data = readDataFile(collectionFilePath);
125
+ }
126
+ const id = uuidv4();
127
+ record['@rune.id'] = generateID(id, collectionName);
128
+ data.push(record);
129
+ writeDataFile(collectionFilePath, data);
130
+ return record;
131
+ };
132
+
133
+ const read = (id, evaluate = true) => {
134
+ if (typeof id == 'object' && '@rune.id' in id) id = id['@rune.id'];
135
+ if (!fs.existsSync(collectionFilePath)) return null;
136
+ const data = readDataFile(collectionFilePath);
137
+ const record = data.find((record) => record['@rune.id'] === id);
138
+ if (record) {
139
+ return evaluateRecord(record);
140
+ }
141
+ return null;
142
+ };
143
+
144
+ const evaluateRecord = (record, prevRecord) => {
145
+ const evaluateValue = (val) => {
146
+ if (typeof val == 'string' && val.startsWith('@rune.ref')) {
147
+ const ref = val.split('@rune.ref')[1].trim();
148
+ const refData = findRef(ref, false);
149
+ if (!refData) {
150
+ return null;
151
+ } else {
152
+ let value = refData;
153
+ if (refData['@rune.id']) {
154
+ value = prevRecord && prevRecord['@rune.id'] == refData['@rune.id'] ? prevRecord : evaluateRecord(refData, record);
155
+ }
156
+ return value;
157
+ }
158
+ }
159
+ if (Array.isArray(val)) {
160
+ val = val.map((i) => evaluateValue(i));
161
+ }
162
+ return val;
163
+ };
164
+ for (let i in record) {
165
+ const val = record[i];
166
+ record[i] = evaluateValue(val);
167
+ }
168
+ return record;
169
+ };
170
+
171
+ const update = (caseRecord, newRecord) => {
172
+ let id;
173
+ if (typeof caseRecord === 'string') {
174
+ id = caseRecord;
175
+ } else if (typeof caseRecord === 'object') {
176
+ const data = readDataFile(collectionFilePath);
177
+ const record = data.find((record) => {
178
+ for (const key in caseRecord) {
179
+ if (record[key] !== caseRecord[key]) return false;
180
+ }
181
+ return true;
182
+ });
183
+ if (record) {
184
+ id = record['@rune.id'];
185
+ } else {
186
+ return null; // No matching record found
187
+ }
188
+ }
189
+
190
+ if (!id) return null;
191
+
192
+ const data = readDataFile(collectionFilePath);
193
+ const index = data.findIndex((record) => record['@rune.id'] === id);
194
+ if (index !== -1) {
195
+ const oldRecord = data[index];
196
+ for (const key in newRecord) {
197
+ const value = newRecord[key];
198
+ if (value instanceof PushChange) {
199
+ if (!oldRecord[key] || !Array.isArray(oldRecord[key])) {
200
+ oldRecord[key] = [];
201
+ }
202
+ oldRecord[key].push(...value.values);
203
+ } else if (value instanceof PopChange) {
204
+ if (oldRecord[key] && Array.isArray(oldRecord[key])) {
205
+ value.values.forEach((val) => {
206
+ const index = oldRecord[key].indexOf(val);
207
+ if (index !== -1) {
208
+ oldRecord[key].splice(index, 1);
209
+ }
210
+ });
211
+ }
212
+ } else {
213
+ oldRecord[key] = value;
214
+ }
215
+ }
216
+ data[index] = oldRecord;
217
+ writeDataFile(collectionFilePath, data);
218
+ return data[index];
219
+ }
220
+ return null;
221
+ };
222
+
223
+ const find = (criteria) => {
224
+ if (typeof criteria == 'string') return read(criteria);
225
+ if (!criteria || typeof criteria !== 'object') return null;
226
+
227
+ const data = readDataFile(collectionFilePath);
228
+ const record =
229
+ data.find((record) => {
230
+ for (const key in criteria) {
231
+ if (record[key] !== criteria[key]) return false;
232
+ }
233
+ return true;
234
+ }) || null;
235
+ if (record) {
236
+ return evaluateRecord(record);
237
+ }
238
+ return null;
239
+ };
240
+
241
+ const remove = (id) => {
242
+ if ('@rune.id' in id) id = id['@rune.id'];
243
+ if (!fs.existsSync(collectionFilePath)) return false;
244
+ let data = readDataFile(collectionFilePath);
245
+ const index = data.findIndex((record) => record['@rune.id'] === id);
246
+ if (index !== -1) {
247
+ data.splice(index, 1);
248
+ writeDataFile(collectionFilePath, data);
249
+ return true;
250
+ }
251
+ return false;
252
+ };
253
+
254
+ const list = () => {
255
+ if (!fs.existsSync(collectionFilePath)) return [];
256
+ const data = readDataFile(collectionFilePath);
257
+ return data.map((rec) => evaluateRecord(rec));
258
+ };
259
+
260
+ const map = (cb, mutate = false) => {
261
+ const data = readDataFile(collectionFilePath);
262
+ const mappedData = data.map(cb);
263
+ if (mutate) {
264
+ writeDataFile(collectionFilePath, mappedData);
265
+ }
266
+ return mappedData;
267
+ };
268
+
269
+ const transform = (cb, mutate = true) => {
270
+ const data = readDataFile(collectionFilePath);
271
+ const transformedData = cb(data);
272
+ if (mutate) {
273
+ writeDataFile(collectionFilePath, transformedData);
274
+ }
275
+ return transformedData;
276
+ };
277
+
278
+ const filter = (cb, mutate = false) => {
279
+ const data = readDataFile(collectionFilePath);
280
+ const filteredData = data.filter(cb);
281
+ if (mutate) {
282
+ writeDataFile(collectionFilePath, filteredData);
283
+ }
284
+ return filteredData;
285
+ };
286
+
287
+ const sort = (cb, mutate = false) => {
288
+ const data = readDataFile(collectionFilePath);
289
+ const sortedData = data.sort(cb);
290
+ if (mutate) {
291
+ writeDataFile(collectionFilePath, sortedData);
292
+ }
293
+ return sortedData;
294
+ };
295
+
296
+ return {
297
+ insert,
298
+ read,
299
+ update,
300
+ remove,
301
+ find,
302
+ map,
303
+ transform,
304
+ filter,
305
+ sort,
306
+ list,
307
+ };
308
+ };
309
+
310
+ const findRef = (ref, evaluate = true) => {
311
+ const [name, id, ...rest] = ref.split('.');
312
+ const col = collection(name);
313
+ const record = col.read(id, evaluate);
314
+ if (rest.length === 0) return record;
315
+ let value = record;
316
+ for (const prop of rest) {
317
+ if (typeof value != 'object') break;
318
+ if (!(prop in value)) return null;
319
+ value = value[prop];
320
+ }
321
+ return value;
322
+ };
323
+
324
+ const map = (mapName) => {
325
+ const mapFilePath = path.join(dbDirPath, `${mapName}.map`);
326
+
327
+ const set = (key, value) => {
328
+ const mainData = readMainData();
329
+ if (!mainData.maps.includes(mapName)) {
330
+ mainData.maps.push(mapName);
331
+ writeMainData(mainData);
332
+ }
333
+
334
+ let data = {};
335
+ if (fs.existsSync(mapFilePath)) {
336
+ data = readDataFile(mapFilePath);
337
+ }
338
+ data[key] = value;
339
+ writeDataFile(mapFilePath, data);
340
+ };
341
+
342
+ const get = (key) => {
343
+ if (!fs.existsSync(mapFilePath)) return null;
344
+ const data = readDataFile(mapFilePath);
345
+ return data[key] || null;
346
+ };
347
+
348
+ const remove = (key) => {
349
+ if (!fs.existsSync(mapFilePath)) return false;
350
+ const data = readDataFile(mapFilePath);
351
+ if (data[key]) {
352
+ delete data[key];
353
+ writeDataFile(mapFilePath, data);
354
+ return true;
355
+ }
356
+ return false;
357
+ };
358
+
359
+ const transform = (cb, mutate = false) => {
360
+ const data = readDataFile(mapFilePath);
361
+ const transformedData = cb(data);
362
+ if (mutate) {
363
+ writeDataFile(mapFilePath, transformedData);
364
+ }
365
+ return transformedData;
366
+ };
367
+
368
+ const list = () => {
369
+ if (!fs.existsSync(mapFilePath)) return {};
370
+ const data = readDataFile(mapFilePath);
371
+ return data;
372
+ };
373
+
374
+ return { set, get, remove, list, transform };
375
+ };
376
+
377
+ readMainData();
378
+
379
+ return { setData, getData, collection, findRef, makeRef, map };
420
380
  };
421
381
 
422
382
  module.exports = (context) => ({
423
- _onImport() {
424
- delete this.createDB;
425
- return this;
426
- },
427
- db(dbname, data = {}, encryptionKey) {
428
- if (!context.app) throw new Error("rune can only be used in apps");
429
- const pkg = path.join(CONFIG_PATH, context.app.config.package, "db");
430
- if (!fs.existsSync(pkg)) fs.mkdirSync(pkg, { recursive: true });
431
- return createDB(dbname, pkg, data, encryptionKey || ENCRYPTION_KEY);
432
- },
433
- makeRef,
434
- runePop,
435
- runePush,
436
- createDB,
383
+ _onImport() {
384
+ delete this.createDB;
385
+ return this;
386
+ },
387
+ db(dbname, data = {}, encryptionKey) {
388
+ if (!context.app) throw new Error('rune can only be used in apps');
389
+ const pkg = path.join(CONFIG_PATH, context.app.config.package, 'db');
390
+ if (!fs.existsSync(pkg)) fs.mkdirSync(pkg, { recursive: true });
391
+ return createDB(dbname, pkg, data, encryptionKey || ENCRYPTION_KEY);
392
+ },
393
+ genKey(secret){
394
+ return gen_key(secret);
395
+ },
396
+ makeRef,
397
+ runePop,
398
+ runePush,
399
+ createDB,
437
400
  });