@makano/rew 1.1.7 → 1.1.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (74) hide show
  1. package/README.md +1 -1
  2. package/bin/ui +0 -0
  3. package/build.sh +3 -1
  4. package/lib/coffeescript/browser.js +144 -139
  5. package/lib/coffeescript/cake.js +132 -133
  6. package/lib/coffeescript/coffeescript.js +437 -381
  7. package/lib/coffeescript/command.js +806 -724
  8. package/lib/coffeescript/grammar.js +1908 -2474
  9. package/lib/coffeescript/helpers.js +509 -473
  10. package/lib/coffeescript/index.js +228 -215
  11. package/lib/coffeescript/lexer.js +2282 -1909
  12. package/lib/coffeescript/nodes.js +9782 -9202
  13. package/lib/coffeescript/optparse.js +255 -227
  14. package/lib/coffeescript/parser.js +20305 -1265
  15. package/lib/coffeescript/register.js +107 -87
  16. package/lib/coffeescript/repl.js +307 -284
  17. package/lib/coffeescript/rewriter.js +1389 -1079
  18. package/lib/coffeescript/scope.js +176 -172
  19. package/lib/coffeescript/sourcemap.js +242 -227
  20. package/lib/rew/cli/cli.js +245 -186
  21. package/lib/rew/cli/log.js +31 -32
  22. package/lib/rew/cli/run.js +10 -12
  23. package/lib/rew/cli/utils.js +248 -175
  24. package/lib/rew/const/config_path.js +4 -0
  25. package/lib/rew/const/default.js +38 -35
  26. package/lib/rew/const/files.js +9 -9
  27. package/lib/rew/const/opt.js +8 -8
  28. package/lib/rew/css/theme.css +2 -2
  29. package/lib/rew/functions/core.js +55 -57
  30. package/lib/rew/functions/curl.js +23 -0
  31. package/lib/rew/functions/emitter.js +52 -55
  32. package/lib/rew/functions/exec.js +25 -21
  33. package/lib/rew/functions/export.js +18 -20
  34. package/lib/rew/functions/fs.js +55 -54
  35. package/lib/rew/functions/future.js +29 -21
  36. package/lib/rew/functions/id.js +8 -9
  37. package/lib/rew/functions/import.js +104 -93
  38. package/lib/rew/functions/map.js +13 -16
  39. package/lib/rew/functions/match.js +35 -26
  40. package/lib/rew/functions/path.js +8 -8
  41. package/lib/rew/functions/require.js +32 -33
  42. package/lib/rew/functions/sleep.js +2 -2
  43. package/lib/rew/functions/stdout.js +20 -20
  44. package/lib/rew/functions/types.js +96 -95
  45. package/lib/rew/html/ui.html +12 -13
  46. package/lib/rew/html/ui.js +205 -168
  47. package/lib/rew/main.js +14 -14
  48. package/lib/rew/misc/findAppInfo.js +16 -0
  49. package/lib/rew/misc/findAppPath.js +21 -0
  50. package/lib/rew/misc/seededid.js +13 -0
  51. package/lib/rew/models/enum.js +12 -12
  52. package/lib/rew/models/struct.js +30 -32
  53. package/lib/rew/modules/compiler.js +228 -177
  54. package/lib/rew/modules/context.js +35 -22
  55. package/lib/rew/modules/fs.js +10 -10
  56. package/lib/rew/modules/runtime.js +17 -21
  57. package/lib/rew/modules/yaml.js +27 -30
  58. package/lib/rew/pkgs/conf.js +82 -75
  59. package/lib/rew/pkgs/data.js +12 -7
  60. package/lib/rew/pkgs/date.js +27 -28
  61. package/lib/rew/pkgs/env.js +6 -8
  62. package/lib/rew/pkgs/modules/data/bintree.js +52 -52
  63. package/lib/rew/pkgs/modules/data/doublylinked.js +85 -85
  64. package/lib/rew/pkgs/modules/data/linkedList.js +73 -73
  65. package/lib/rew/pkgs/modules/data/queue.js +19 -20
  66. package/lib/rew/pkgs/modules/data/stack.js +19 -19
  67. package/lib/rew/pkgs/modules/threads/worker.js +36 -26
  68. package/lib/rew/pkgs/modules/ui/classes.js +182 -178
  69. package/lib/rew/pkgs/pkgs.js +9 -10
  70. package/lib/rew/pkgs/rune.js +422 -0
  71. package/lib/rew/pkgs/threads.js +57 -53
  72. package/lib/rew/pkgs/ui.js +148 -136
  73. package/meson.build +13 -0
  74. package/package.json +9 -2
@@ -0,0 +1,422 @@
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');
7
+
8
+ const ENCRYPTION_KEY = 'e6ad8b0792b9e0472ea44d1f3adfd1d503182efcce25991b05cc5ef83f307ffc';
9
+
10
+ class Change {
11
+ constructor(values) {
12
+ this.values = values;
13
+ }
14
+ }
15
+
16
+ class PopChange extends Change {}
17
+
18
+ class PushChange extends Change {}
19
+
20
+ const runePush = (...values) => new PushChange(values);
21
+ const runePop = (...values) => new PopChange(values);
22
+
23
+ function makeRef(value, 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;
28
+ }
29
+
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
+
42
+ function generateID(id, collection) {
43
+ return eid(collection, 5) + '+' + id;
44
+ }
45
+
46
+ function getCollectionFromID(id) {
47
+ return eid(id.split('+')[0], -5);
48
+ }
49
+
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 };
405
+ };
406
+
407
+ module.exports = (context) => ({
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,
422
+ });
@@ -5,67 +5,71 @@ const { struct } = require('../models/struct');
5
5
  const path = require('path');
6
6
 
7
7
  module.exports = (context) => ({
8
- thread: (cb) => {
9
- const workers = [];
8
+ thread: (cb) => {
9
+ const workers = [];
10
10
 
11
- const stopWorker = (worker) => {
12
- workers.splice(workers.indexOf(worker), 1);
13
- worker.terminate()
14
- };
11
+ const stopWorker = (worker) => {
12
+ workers.splice(workers.indexOf(worker), 1);
13
+ worker.terminate();
14
+ };
15
15
 
16
- const athread = {
17
- stopAll: () => {
18
- if (!run) return;
19
- workers.forEach(stopWorker);
20
- },
21
- start: (context) => {
22
- let dataResult = future(() => {}), listener = emitter();
23
- const worker = new Worker(path.resolve(__dirname, './modules/threads/worker.js'), {
24
- workerData: { context, cb: cb.toString() }
25
- });
26
- workers.push(worker);
16
+ const athread = {
17
+ stopAll: () => {
18
+ if (!run) return;
19
+ workers.forEach(stopWorker);
20
+ },
21
+ start: (context) => {
22
+ let dataResult = future(() => {}),
23
+ listener = emitter();
24
+ const worker = new Worker(path.resolve(__dirname, './modules/threads/worker.js'), {
25
+ workerData: { context, cb: cb.toString() },
26
+ });
27
+ workers.push(worker);
27
28
 
28
- const stop = () => stopWorker(worker);
29
+ const stop = () => stopWorker(worker);
29
30
 
30
- worker.on('message', (data) => {
31
- if (data.type === 'result') {
32
- dataResult.resolve(data.result);
33
- stop();
34
- } else if (data.type === 'error') {
35
- reject(new Error(data.error));
36
- stop();
37
- } else if (data.type === 'event') {
38
- listener.emit(data.event, data.data);
39
- }
40
- });
31
+ worker.on('message', (data) => {
32
+ if (data.type === 'result') {
33
+ dataResult.resolve(data.result);
34
+ stop();
35
+ } else if (data.type === 'error') {
36
+ reject(new Error(data.error));
37
+ stop();
38
+ } else if (data.type === 'event') {
39
+ listener.emit(data.event, data.data);
40
+ }
41
+ });
41
42
 
42
- worker.on('error', (error) => {
43
- console.log(error);
44
- stop();
45
- });
43
+ worker.on('error', (error) => {
44
+ console.log(error);
45
+ stop();
46
+ });
46
47
 
47
- let exiting = false;
48
+ let exiting = false;
48
49
 
49
- worker.on('exit', (code) => {
50
- if(exiting) return;
51
- stop();
52
- if (code !== 0) {
53
- throw new Error(`Worker stopped with exit code ${code}`);
54
- }
55
- });
50
+ worker.on('exit', (code) => {
51
+ if (exiting) return;
52
+ stop();
53
+ if (code !== 0) {
54
+ throw new Error(`Worker stopped with exit code ${code}`);
55
+ }
56
+ });
56
57
 
57
- worker.postMessage({ type: 'start' });
58
+ worker.postMessage({ type: 'start' });
58
59
 
59
- return {
60
- on: (e, cb) => listener.on(e, cb),
61
- off: (e, cb) => listener.off(e, cb),
62
- emit: (e, data) => worker?.postMessage({ type: 'event', event: e, data }),
63
- get: () => dataResult.wait(),
64
- stop: () => { exiting = true; stop(); }
65
- };
66
- }
67
- };
60
+ return {
61
+ on: (e, cb) => listener.on(e, cb),
62
+ off: (e, cb) => listener.off(e, cb),
63
+ emit: (e, data) => worker?.postMessage({ type: 'event', event: e, data }),
64
+ get: () => dataResult.wait(),
65
+ stop: () => {
66
+ exiting = true;
67
+ stop();
68
+ },
69
+ };
70
+ },
71
+ };
68
72
 
69
- return athread;
70
- }
73
+ return athread;
74
+ },
71
75
  });