-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrabbit2.js
302 lines (213 loc) · 7.96 KB
/
rabbit2.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
const path = require('path');
const getCallerFile = require('get-caller-file');
const JSONdb = require('simple-json-db');
const rabbit2Db = new JSONdb('./.data/.rabbit2.json', {
syncOnWrite: true,
jsonSpaces: 0,
});
// Grabs the db from disk into memory.
var rabbit2MemDb = rabbit2Db.JSON();
/**
* Object representation of all available commands and their properties.
* Refer to /commands/index.js for the supported structure.
*/
module.exports.cmdDirectory = require('./commands');
/**
* Executes the `run()` function of the appropriate command as specified in `inputParams`,
* which is determined by one of either of the following sequentially:
*
* - An exact match on the command's filename (as per `inputParams.cmdStr`) and valid operators (as per `inputParams.opsArr`).
* - The resolved command from a match on any commands' aliases regex pattern matches, recursive invocation of this function ⤴️.
* - Fallback: the `g` (Google) command with the `inputParams.rawStr` as its input.
*
* @param {Object} inputParams all possible tokens from the user input (cmdStr, opsArr, argStr, rawStr)
* @returns {string} the return value of the command's run() function.
*/
module.exports.execRun = async function (inputParams) {
console.log(inputParams);
const inputCmd = inputParams.cmdStr;
const inputOps = inputParams.opsArr;
const inputArg = inputParams.argStr;
const inputRaw = inputParams.rawStr;
const cmdObj = this.cmdDirectory[inputCmd];
// Case 1: Command exists with an _exact_ match of the `inputCmd.inputOps` input.
if (cmdObj && typeof cmdObj.run === "function") {
if (cmdObj.operators && inputOps.every(op => Object.keys(cmdObj.operators).includes(op))) {
incrementRunCount.call(this, inputCmd);
return await cmdObj.run.call(this, inputArg, inputOps, inputRaw);
}
}
// Case 2: Attempt to find a command with an _alias_ match of the raw input.
// We replace a match with its replace pattern and pass it as the argument to the target command.
for (let [cmdKey, cmdObj] of Object.entries(this.cmdDirectory)) {
for (let [aliasRegexString, replacePattern] of Object.entries(cmdObj.aliases)) {
// Make the regex a little safer.
if (aliasRegexString.charAt(0) !== '^') {
aliasRegexString = `^${aliasRegexString}`;
}
let regexp = new RegExp(aliasRegexString);
if (regexp.test(inputRaw)) {
let replacedArgString = inputRaw.replace(regexp, replacePattern);
return this.execRun({
cmdStr: cmdKey,
opsArr: inputOps, // Blindly forwarded from the original parse.
argStr: replacedArgString,
rawStr: inputRaw,
});
}
}
}
// Case 3: No suitable matches, so pipe the original raw input to a Google search.
// We invoke the command's run() directly to avoid incrementing the run counter for the `g` command.
incrementRunCount.call(this); // Without a cmdId parameter, counts towards "no match" (Google search implied).
return this.cmdDirectory['g'].run.call(this, inputRaw, inputOps, inputRaw);
}
/**
*
*/
module.exports.execSuggest = async function (inputParams) {
const inputCmd = inputParams.cmdStr;
//const inputOps = inputParams.opsArr;
// const inputArg = inputParams.argStr;
const inputRaw = inputParams.rawStr;
const cmdObj = this.cmdDirectory[inputCmd];
if (cmdObj && typeof cmdObj.suggest === "function") {
return await cmdObj.suggest.call(this, inputRaw);
}
}
/**
* Provides a key-value persistent store solution for commands.
* Commands can't access data from other commands and lose access to data if its filename changes.
* A commands' data is scoped by its name space (defined by its filename).
* Uses the stack trace to determine the command's filename.
*
* @param {string} key unique key identifier within the object store.
* @param {string} val data value to store.
*
* @returns {Object} the value of the object stored in the `key` value (possibly undefined).
* @returns {Object} the entire DB object (when niether `key` or `val` are provided).
* @returns {boolean} `false` if attempting to overwrite an existing value, `true` when successfully stored.
*/
module.exports.storage = function (key, val) {
const cmdId = getCallerFileName(3);
const cmdDb = new JSONdb(`./.data/${cmdId}.json`, {
syncOnWrite: false,
jsonSpaces: 0,
});
// No arguments provided.
if (!key && !val) {
return cmdDb.JSON();
}
var retrievedValue = cmdDb.get(key);
// Only @key provided, attempting to access existing entry.
if (!val) {
return retrievedValue;
}
// Attempting to save onto an existing entry.
if (retrievedValue) {
return false;
}
// Create a new entry in the db.
cmdDb.set(key, val);
cmdDb.sync();
return true;
}
/**
* Allows commands to read/set cookies.
* Properly handles JSON objects, serialising/deserialising when appropriate.
* A prefix is prepended to namepsace the cookie name: `rabbit2-{cmdId}-`,
* using the stack trace to determine the command's filename.
* Default TTL value is 1 year.
*/
module.exports.cookie = function (name, value, ttl = 31556952000) {
const cmdId = getCallerFileName(3);
const cookieName = `rabbit2-${cmdId}-${name}`
// Set cookie, serialises if applicable
if (name && value !== undefined) {
let cookieValue = typeof value === "string"
? value
: JSON.stringify(value)
;
this.serverResponse.cookie(cookieName, cookieValue, {
maxAge: ttl,
httpOnly: true, // Disables accessing cookie via client side JS.
});
return;
}
// Get cookie, deserialising if possible
var getCookieValue = this.serverRequest.cookies[cookieName];
try {
getCookieValue = JSON.parse(getCookieValue);
} catch (e) { }
return getCookieValue;
}
/**
* Exposes a view renderer function to commands.
* Determines the orgin command's filename via stack trace.
* Injects additional data made available to the view.
* The original <data> passed by the command is made available under a <data> property.
*/
module.exports.renderView = function (data) {
const cmdId = getCallerFileName(3);
var viewData = {
data: data,
// Expose additional data (e.g. used by /includes/header.ejs)
cmdObj: {
id: cmdId,
name: this.cmdDirectory[cmdId].name,
summary: this.cmdDirectory[cmdId].summary,
description: this.cmdDirectory[cmdId].description,
}
};
// Special to the `list` view, expose the run count data of commands.
if (cmdId === 'list') {
viewData.cmdRunCounts = rabbit2MemDb.commandRunCounts;
}
this.serverResponse.render(cmdId, viewData);
}
/**
* Convenience function to escape regex characters in a <string>.
*
* https://www.npmjs.com/package/escape-string-regexp
*/
module.exports.escapeRegexString = function (string) {
if (typeof string === 'string') {
return string
.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&')
.replace(/-/g, '\\x2d');
} else {
return '';
}
}
/**
* Convenience function to help validate if a <urlString> is a valid URL.
* Returns a URL object or undefined otherwise.
*/
module.exports.urlise = function (urlString) {
try {
return new URL(urlString);
} catch (e) { }
}
/**
* Increments the run counter for <cmd>.
* If no <cmd> is defined, increments a special "unmatched command" counter (key: '').
*/
function incrementRunCount(cmd = "") {
var isDevEnvironment = this.serverRequest.get('Referrer') === 'https://glitch.com/';
// Avoids incrementing the count with repeated refreshes that can occur during development.
if (isDevEnvironment) {
return;
}
var count = (rabbit2MemDb.commandRunCounts[cmd] || 0) + 1;
rabbit2MemDb.commandRunCounts[cmd] = count;
// Persist the latest db from memory to disk.
rabbit2Db.JSON(rabbit2MemDb);
rabbit2Db.sync();
}
/**
* Returns the name of the file in commands/ that initiated the callstack.
*/
function getCallerFileName(stackDepth) {
const callerPath = getCallerFile(stackDepth);
return path.parse(callerPath).name.toLowerCase().split(".")[0];
}