@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.
- package/lib/coffeescript/browser.js +144 -139
- package/lib/coffeescript/cake.js +132 -133
- package/lib/coffeescript/coffeescript.js +437 -381
- package/lib/coffeescript/command.js +806 -724
- package/lib/coffeescript/grammar.js +1908 -2474
- package/lib/coffeescript/helpers.js +509 -473
- package/lib/coffeescript/index.js +228 -215
- package/lib/coffeescript/lexer.js +2282 -1909
- package/lib/coffeescript/nodes.js +9782 -9202
- package/lib/coffeescript/optparse.js +255 -227
- package/lib/coffeescript/parser.js +20305 -1265
- package/lib/coffeescript/register.js +107 -87
- package/lib/coffeescript/repl.js +307 -284
- package/lib/coffeescript/rewriter.js +1389 -1079
- package/lib/coffeescript/scope.js +176 -172
- package/lib/coffeescript/sourcemap.js +242 -227
- package/lib/rew/cli/cli.js +243 -202
- package/lib/rew/cli/log.js +31 -32
- package/lib/rew/cli/run.js +10 -12
- package/lib/rew/cli/utils.js +248 -176
- package/lib/rew/const/config_path.js +2 -3
- package/lib/rew/const/default.js +38 -35
- package/lib/rew/const/files.js +9 -9
- package/lib/rew/const/opt.js +8 -8
- package/lib/rew/css/theme.css +2 -2
- package/lib/rew/functions/core.js +55 -57
- package/lib/rew/functions/curl.js +23 -0
- package/lib/rew/functions/emitter.js +52 -55
- package/lib/rew/functions/exec.js +25 -21
- package/lib/rew/functions/export.js +18 -20
- package/lib/rew/functions/fs.js +55 -54
- package/lib/rew/functions/future.js +29 -21
- package/lib/rew/functions/id.js +8 -9
- package/lib/rew/functions/import.js +116 -110
- package/lib/rew/functions/map.js +13 -16
- package/lib/rew/functions/match.js +35 -26
- package/lib/rew/functions/path.js +8 -8
- package/lib/rew/functions/require.js +32 -33
- package/lib/rew/functions/sleep.js +2 -2
- package/lib/rew/functions/stdout.js +20 -20
- package/lib/rew/functions/types.js +96 -95
- package/lib/rew/html/ui.html +12 -13
- package/lib/rew/html/ui.js +205 -168
- package/lib/rew/main.js +14 -14
- package/lib/rew/misc/findAppInfo.js +14 -14
- package/lib/rew/misc/findAppPath.js +16 -16
- package/lib/rew/misc/seededid.js +9 -11
- package/lib/rew/models/enum.js +12 -12
- package/lib/rew/models/struct.js +30 -32
- package/lib/rew/modules/compiler.js +228 -177
- package/lib/rew/modules/context.js +33 -21
- package/lib/rew/modules/fs.js +10 -10
- package/lib/rew/modules/runtime.js +17 -21
- package/lib/rew/modules/yaml.js +27 -30
- package/lib/rew/pkgs/conf.js +80 -80
- package/lib/rew/pkgs/data.js +12 -7
- package/lib/rew/pkgs/date.js +27 -28
- package/lib/rew/pkgs/env.js +6 -8
- package/lib/rew/pkgs/modules/data/bintree.js +52 -52
- package/lib/rew/pkgs/modules/data/doublylinked.js +85 -85
- package/lib/rew/pkgs/modules/data/linkedList.js +73 -73
- package/lib/rew/pkgs/modules/data/queue.js +19 -20
- package/lib/rew/pkgs/modules/data/stack.js +19 -19
- package/lib/rew/pkgs/modules/threads/worker.js +36 -26
- package/lib/rew/pkgs/modules/ui/classes.js +182 -178
- package/lib/rew/pkgs/pkgs.js +9 -10
- package/lib/rew/pkgs/rune.js +391 -345
- package/lib/rew/pkgs/threads.js +57 -53
- package/lib/rew/pkgs/ui.js +148 -136
- package/package.json +1 -1
package/lib/rew/pkgs/rune.js
CHANGED
@@ -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
|
-
|
13
|
-
|
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
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
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) =>
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
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
|
-
|
43
|
+
return eid(collection, 5) + '+' + id;
|
44
44
|
}
|
45
45
|
|
46
46
|
function getCollectionFromID(id) {
|
47
|
-
|
47
|
+
return eid(id.split('+')[0], -5);
|
48
48
|
}
|
49
49
|
|
50
|
-
const createDB = (dbName, dirname, encryptionKey) => {
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
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
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
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
|
});
|