-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbracery-util.js
486 lines (438 loc) · 13.9 KB
/
bracery-util.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
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
const https = require('https');
const promisify = require('util').promisify;
const braceryWeb = require ('./bracery-web');
const extend = braceryWeb.extend;
const escapeHTML = braceryWeb.escapeHTML;
const expandMarkdown = braceryWeb.expandMarkdown;
const digestText = braceryWeb.digestText;
const getWords = braceryWeb.getWords;
const defaultSymbolName = braceryWeb.defaultSymbolName;
const defaultUserName = braceryWeb.defaultUserName;
const userPartOfName = braceryWeb.userPartOfName;
const symbolPartOfName = braceryWeb.symbolPartOfName;
const bookmarkRegex = braceryWeb.bookmarkRegex;
const config = require ('./bracery-config');
function btoa (s) {
return Buffer.from(s).toString('base64');
}
function promiseDelay (delay) {
return new Promise ((resolve) => setTimeout (resolve, delay));
}
const logAllDynamoOperations = false;
function dynamoPromise() {
const doc = require('dynamodb-doc');
let dynamo = new doc.DynamoDB();
if (logAllDynamoOperations)
return (method) => ((params) => { console.log(JSON.stringify({method,params})); return promisify (dynamo[method].bind (dynamo)) (params) });
return (method) => promisify (dynamo[method].bind (dynamo));
}
function getBody (event) {
return (event.body
? (typeof(event.body) === 'string'
? JSON.parse (event.body)
: event.body)
: {});
}
function expandTemplate (template, tmpMap) {
tmpMap = extend ({}, tmpMap, { TEMPLATE_VARS: tmpMap });
return Object.keys (tmpMap).reduce ((text, templateVar) => {
const templateVal = tmpMap[templateVar];
return text
.replace (new RegExp ('%' + templateVar + '%', 'g'), templateVal)
.replace (new RegExp ('%ESCAPED_' + templateVar + '%', 'g'), escapeHTML (templateVal))
.replace (new RegExp ('%JSON_' + templateVar + '%', 'g'), JSON.stringify (templateVal));
}, template);
}
function randomChar() {
const r = Math.floor (36 * Math.random());
return r.toString (36);
}
async function makeUniqueId (tableName, idAttr, initialIdChars, dynamoPromise) {
const uniqueIdPromiser = async (id) => {
let params = {
TableName: tableName,
KeyConditionExpression: "#id = :id",
ExpressionAttributeNames: {
"#id": idAttr,
},
ExpressionAttributeValues: {
":id": id,
},
};
const res = await dynamoPromise('query') (params);
return (res && res.Items && res.Items.length
? await uniqueIdPromiser (id + randomChar())
: id);
};
return await uniqueIdPromiser (new Array(initialIdChars).fill(0).map (randomChar).join(''));
}
async function getSession (event, dynamoPromise) {
try {
const regex = new RegExp (config.cookieName + '=(\\w+)', 'g');
let match;
let cookie = event.headers && (event.headers.cookie || event.headers.Cookie);
if (cookie) {
let m;
while ((m = regex.exec (cookie))) // get last (& presumably most recent) cookie, in case document.cookie has got multiple cookies
match = m;
}
if (match) {
let cookie = match[1];
let queryRes = await dynamoPromise('query')
({ TableName: config.sessionTableName,
KeyConditionExpression: "#ckey = :cval",
ExpressionAttributeNames: {
"#ckey": "cookie"
},
ExpressionAttributeValues: {
":cval": cookie
}});
if (queryRes.Items && queryRes.Items.length)
return queryRes.Items[0];
}
const newCookie = await makeUniqueId (config.sessionTableName, 'cookie', 16, dynamoPromise);
const newSession = { cookie: newCookie,
expires: Math.ceil (Date.now() / 1000 + config.sessionExpirationSeconds) };
await dynamoPromise('putItem')
({ TableName: config.sessionTableName,
Item: newSession,
});
return newSession;
} catch (e) {
console.warn (e); // to CloudWatch
return null;
}
}
// async https.request
async function httpsRequest (opts, formData) {
return new Promise
((resolve, reject) => {
let req = https.request (opts, (res) => {
let data = '';
res.on('data', (chunk) => { data += chunk; });
res.on('end', () => {
resolve ([res, data]);
});
});
if (formData)
req.write (formData);
req.end();
});
}
async function createBookmark (params, session, dynamoPromise) {
const id = await makeUniqueId (config.bookmarkTableName, 'id', 4, dynamoPromise);
let bookmark = { id: id,
created: Date.now(),
accessed: Date.now(),
accessCount: 0 };
if (session && session.loggedIn)
bookmark.user = session.user;
Object.keys(params).forEach ((p) => {
if (params[p])
bookmark[p] = params[p];
});
await dynamoPromise('putItem')
({ TableName: config.bookmarkTableName,
Item: bookmark });
const url = config.baseUrl + config.viewPrefix + '?id=' + id;
return { id, url };
}
async function getBookmarkedParams (event, dynamoPromise) {
const id = event.queryStringParameters.id;
let params = getParams (event);
const res = await dynamoPromise('query')
({ TableName: config.bookmarkTableName,
KeyConditionExpression: "#id = :id",
ExpressionAttributeNames: {
"#id": "id"
},
ExpressionAttributeValues: {
":id": id
}});
if (res && res.Items && res.Items.length) {
const bookmark = res.Items[0];
['name','initText','evalText','vars','expansion']
.forEach ((p) => {
if (bookmark[p])
params[p] = bookmark[p];
});
await dynamoPromise('updateItem')
({ TableName: config.bookmarkTableName,
Key: { id: id },
UpdateExpression: 'SET #a = :a, #c = #c + :n',
ExpressionAttributeNames: {
'#a': 'accessed',
'#c': 'accessCount'
},
ExpressionAttributeValues: {
':a': Date.now(),
':n': 1
},
});
}
return params;
}
function getName (event) {
const body = getBody (event);
let name = body.name || (defaultUserName + '/' + defaultSymbolName);
if (event) {
const params = event.pathParameters || event.queryStringParameters;
if (params && params.user) {
name = params.user;
if (params.symbol)
name = name + '/' + params.symbol;
else
name = defaultUserName + '/' + name;
}
}
return name;
}
function getParams (event) {
const body = getBody (event);
// Get the symbol name
const name = getName (event);
// Get symbol definition override from query parameters, if supplied
const initText = ((event && event.queryStringParameters && typeof(event.queryStringParameters['text']) === 'string')
? event.queryStringParameters['text']
: (body.text || undefined));
// Get evaluation text override from query parameters, if supplied
const evalText = ((event && event.queryStringParameters && typeof(event.queryStringParameters['eval']) === 'string')
? event.queryStringParameters['eval']
: (body.eval || undefined));
// Get the expansion, if supplied
const expansion = ((event && event.queryStringParameters && typeof(event.queryStringParameters['exp']) === 'string')
? JSON.parse (event.queryStringParameters['exp'])
: (body.expansion || undefined));
// Get initial vars as query parameters, if supplied
const vars = getVars (event, body);
// Return
return { name, initText, evalText, vars, expansion };
}
function getVars (event, body) {
body = body || getBody (event);
let vars = {};
if (event.queryStringParameters && event.queryStringParameters.vars)
extend (vars, JSON.parse (event.queryStringParameters.vars));
if (body.vars)
extend (vars, body.vars);
return vars;
}
async function getBracery (name, revision, dynamoPromise) {
const query = ({ KeyConditionExpression: '#n = :n',
ExpressionAttributeNames: {
'#n': 'name'
},
ExpressionAttributeValues: {
':n': name
}});
if (revision) {
query.TableName = config.revisionsTableName;
query.KeyConditionExpression += ' AND #r = :r';
query.ExpressionAttributeNames['#r'] = 'revision';
query.ExpressionAttributeValues[':r'] = parseInt (revision);
} else
query.TableName = config.tableName;
return await dynamoPromise('query') (query);
}
async function createBracery (item, dynamoPromise) {
item.visibility = config.defaultVisibility;
item.created = item.updated = Date.now();
item.revision = 1;
await dynamoPromise('putItem')
({ TableName: config.tableName,
Item: item,
});
await putBraceryRevision (item, dynamoPromise);
return item;
}
async function updateBracery (item, dynamoPromise) {
item.updated = Date.now();
let expr = "SET #b = :b, #u = :u, #r = " + (item.revision ? "#r + :r" : ":r");
let keys = { "#b": "bracery",
"#u": "updated",
"#r": "revision" };
let attrs = { ":b": item.bracery,
":u": item.updated,
":r": 1 };
if (typeof(item.locked) !== 'undefined') {
expr = expr + ", #l = :l";
keys['#l'] = 'locked';
attrs[':l'] = item.locked;
}
if (typeof(item.owner) !== 'undefined') {
expr = expr + ", #o = :o";
keys['#o'] = 'owner';
attrs[':o'] = item.owner;
}
const update = await dynamoPromise('updateItem')
({ TableName: config.tableName,
Key: { name: item.name },
UpdateExpression: expr,
ExpressionAttributeNames: keys,
ExpressionAttributeValues: attrs,
ReturnValues: 'UPDATED_NEW',
});
item.revision = update.Attributes.revision;
await putBraceryRevision (item, dynamoPromise);
return update.Attributes;
}
async function putBraceryRevision (item, dynamoPromise) {
await dynamoPromise('putItem')
({ TableName: config.revisionsTableName,
Item: item,
});
}
function withCookie (callback, session) {
return (res) => {
let headers = {
'Content-Type': 'text/html; charset=' + config.stringEncoding,
};
if (session && session.cookie)
headers['Set-Cookie'] = config.cookieName + '=' + session.cookie;
callback (null, {
statusCode: '200',
body: res,
headers: headers,
});
};
}
function ok (callback) {
return (res, headers) => callback (null, {
statusCode: '200',
body: JSON.stringify (res || {}),
headers: extend ({
'Content-Type': 'application/json',
}, headers || {}),
});
}
function redirectFound (callback) {
return (url) => callback (null, {
statusCode: '302',
headers: {
'Location': url,
},
});
}
function redirectWithCookie (callback, session) {
return (url) => callback (null, {
statusCode: '302',
headers: {
'Location': url,
'Set-Cookie': config.cookieName + '=' + session.cookie,
},
});
}
function redirectPost (callback) {
return (url) => callback (null, {
statusCode: '303',
headers: {
'Location': url,
},
});
}
function badRequest (callback) {
return (msg) => callback (null, { statusCode: '400', body: msg || "Bad request" });
}
function forbidden (callback) {
return (msg) => callback (null, { statusCode: '403', body: msg || "Forbidden" });
}
function notFound (callback) {
return (msg) => callback (null, { statusCode: '404', body: msg || "Not found" });
}
function badMethod (callback, event) {
return () => callback ({ statusCode: '405', body: `Unsupported method "${event.httpMethod}"` });
}
function serverError (callback) {
return (msg) => callback (null, { statusCode: '500', body: msg || "Server error" });
}
function respond (callback, event, session) {
return {
withCookie: withCookie (callback, session),
ok: ok (callback),
redirectFound: redirectFound (callback),
redirectPost: redirectPost (callback),
redirectWithCookie: redirectWithCookie (callback, session),
badRequest: badRequest (callback),
forbidden: forbidden (callback),
notFound: notFound (callback),
badMethod: badMethod (callback, event),
serverError: serverError (callback),
};
}
function braceryExpandConfig (bracery, vars, dp) {
let braceryConfig = { vars };
// Create a getSymbol function that queries the database for the given name
var braceryCache = {}
const getSymbol = async (getConfig) => {
const symbolName = getConfig.symbolName || getConfig.node.name;
if (braceryCache[symbolName])
return braceryCache[symbolName];
const res = await dp('query')
({ TableName: config.tableName,
KeyConditionExpression: "#nkey = :nval",
ExpressionAttributeNames: {
"#nkey": "name"
},
ExpressionAttributeValues: {
":nval": symbolName.toLowerCase()
}});
const result = res.Items && res.Items.length && res.Items[0];
return (braceryCache[symbolName] = (result && result.bracery) ? [result.bracery] : ['']);
};
// Create an expandSymbol function that queries the database for the given name, and expands it as Bracery
const expandFull = async (expandConfig) => {
let result = null;
if (expandConfig.rhsText)
result = await bracery.expand (expandConfig.rhsText, braceryConfig);
else {
const symbolDefinition = await getSymbol (expandConfig);
result = await bracery.expand (symbolDefinition[0], braceryConfig);
}
return result;
};
// create a dummy setSymbol function
const setSymbol = () => [];
// Return
extend (braceryConfig,
braceryWeb.braceryLimits,
{ expandFull: expandFull,
get: getSymbol,
set: setSymbol,
expand: null, // signals to Bracery that we want it to fetch the symbol definition & then expand it locally
callback: true, // signals to Bracery that we want it to return promises
makeLink: braceryWeb.makeInternalLink.bind (null, btoa) });
return braceryConfig;
}
module.exports = {
// From bracery-web.js
promisify,
extend,
escapeHTML,
expandMarkdown,
digestText,
getWords,
bookmarkRegex,
defaultUserName,
defaultSymbolName,
userPartOfName,
symbolPartOfName,
// From this file
promiseDelay,
dynamoPromise,
getBracery,
createBracery,
updateBracery,
expandTemplate,
randomChar,
makeUniqueId,
getBody,
getVars,
getSession,
getParams,
getName,
getBookmarkedParams,
createBookmark,
httpsRequest,
respond,
braceryExpandConfig,
};