@makano/rew 1.1.8 → 1.1.9

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