@makano/rew 1.1.73 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (73) 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 +288 -186
  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 +344 -176
  21. package/lib/rew/const/config_path.js +4 -0
  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 +107 -93
  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/bin.js +37 -0
  46. package/lib/rew/misc/findAppInfo.js +16 -0
  47. package/lib/rew/misc/findAppPath.js +21 -0
  48. package/lib/rew/misc/req.js +7 -0
  49. package/lib/rew/misc/seededid.js +13 -0
  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 -177
  53. package/lib/rew/modules/context.js +35 -22
  54. package/lib/rew/modules/fs.js +10 -10
  55. package/lib/rew/modules/runtime.js +17 -21
  56. package/lib/rew/modules/yaml.js +27 -30
  57. package/lib/rew/pkgs/conf.js +82 -75
  58. package/lib/rew/pkgs/data.js +12 -7
  59. package/lib/rew/pkgs/date.js +27 -28
  60. package/lib/rew/pkgs/env.js +6 -8
  61. package/lib/rew/pkgs/modules/data/bintree.js +52 -52
  62. package/lib/rew/pkgs/modules/data/doublylinked.js +85 -85
  63. package/lib/rew/pkgs/modules/data/linkedList.js +73 -73
  64. package/lib/rew/pkgs/modules/data/queue.js +19 -20
  65. package/lib/rew/pkgs/modules/data/stack.js +19 -19
  66. package/lib/rew/pkgs/modules/threads/worker.js +36 -26
  67. package/lib/rew/pkgs/modules/ui/classes.js +182 -178
  68. package/lib/rew/pkgs/pkgs.js +9 -10
  69. package/lib/rew/pkgs/rune.js +400 -0
  70. package/lib/rew/pkgs/threads.js +57 -53
  71. package/lib/rew/pkgs/ui.js +148 -136
  72. package/lib/rew/qrew/compile.js +12 -0
  73. package/package.json +11 -4
@@ -0,0 +1,400 @@
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');
6
+
7
+ const ENCRYPTION_KEY = 'e6ad8b0792b9e0472ea44d1f3adfd1d503182efcce25991b05cc5ef83f307ffc';
8
+
9
+ class Change {
10
+ constructor(values) {
11
+ this.values = values;
12
+ }
13
+ }
14
+
15
+ class PopChange extends Change {}
16
+
17
+ class PushChange extends Change {}
18
+
19
+ const runePush = (...values) => new PushChange(values);
20
+ const runePop = (...values) => new PopChange(values);
21
+
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;
27
+ }
28
+
29
+ const eid = (s, diff) =>
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('');
40
+
41
+ function generateID(id, collection) {
42
+ return eid(collection, 5) + '+' + id;
43
+ }
44
+
45
+ function getCollectionFromID(id) {
46
+ return eid(id.split('+')[0], -5);
47
+ }
48
+
49
+ const createDB = (dbName, dirname, dbData = {}, encryptionKey) => {
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 };
380
+ };
381
+
382
+ module.exports = (context) => ({
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,
400
+ });
@@ -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
  });