-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbracery-bookmark.js
52 lines (43 loc) · 1.51 KB
/
bracery-bookmark.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
/* This is a small AWS lambda function for making permalinks.
*/
//console.log('Loading function');
const fs = require('fs');
const util = require('./bracery-util');
const config = require('./bracery-config');
const dynamoPromise = util.dynamoPromise();
// The Lambda function
exports.handler = async (event, context, callback) => {
//console.log('Received event:', JSON.stringify(event, null, 2));
// Set up some returns
let session = await util.getSession (event, dynamoPromise);
const respond = util.respond (callback, event, session);
// Wrap all downstream calls (to dynamo etc) in try...catch
try {
// Get app state parameters
const appState = util.getParams (event);
const { name, initText, evalText, vars, expansion } = appState;
// Permalink requested?
const permalink = util.getBody(event).link;
if (permalink) {
// Make bookmark permalink and return
const bookmark = await util.createBookmark (appState, session, dynamoPromise);
respond.ok (bookmark);
} else {
// Update session with state and return
await dynamoPromise('updateItem')
({ TableName: config.sessionTableName,
Key: { cookie: session.cookie },
UpdateExpression: 'SET #s = :s',
ExpressionAttributeNames: {
'#s': 'state',
},
ExpressionAttributeValues: {
':s': JSON.stringify (appState),
} });
respond.ok();
}
} catch (e) {
console.warn (e); // to CloudWatch
respond.serverError (e);
}
};