-
-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathgist.js
146 lines (121 loc) · 4.41 KB
/
gist.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
const fetch = require('isomorphic-fetch');
const ENDPOINT = 'https://api.github.com/gists';
const authorization = `token ${process.env.GITHUB_GIST_TOKEN}`;
function incorrectParams(error) {
return {
statusCode: 422, // Unprocessable Entity
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ error }),
};
}
function json(data) {
return {
statusCode: 200,
body: JSON.stringify(data, null, ' '),
};
}
function hasFileWithContent(files, filename) {
return (
files[filename] &&
typeof files[filename].content === 'string' &&
Object.keys(files[filename]).length === 1
);
}
function matchExpectedFiles(files) {
return (
typeof files === 'object' &&
// check length, because we don't allow additional files
Object.keys(files).length === 3 &&
hasFileWithContent(files, 'playground.json') &&
hasFileWithContent(files, 'source.html') &&
hasFileWithContent(files, 'source.js')
);
}
async function getGist({ id, version }) {
const response = await fetch(
[ENDPOINT, id, version].filter(Boolean).join('/'),
{ headers: { authorization } },
);
const { files } = await response.json();
Object.keys(files || {}).forEach((key) => {
files[key] = { content: files[key].content };
});
// { id: string, version: string, files: { [string]: { content: string } }
return { id, version, files };
}
/**
* @param id {string}
* @param description {string}
* @param files { object.<string, { content: string }>}
* @returns {Promise<{id: *, version: *, url: string}>}
*/
async function saveGist({ id, description, files }) {
// if the id is given, we assume an update and thereby just correct the method.
const response = await fetch([ENDPOINT, id].filter(Boolean).join('/'), {
method: id ? 'PATCH' : 'POST',
headers: { authorization },
body: JSON.stringify({ description, files }),
});
const data = await response.json();
const link = `https://testing-playground.com/gist/${data.id}`;
const permaLink = `${link}/${data.history[0].version}`;
if (!id || data.description !== permaLink) {
// in case the id wasn't provided, we're creating a new gist. Update
// the gist to add the playground link to it's description. As we don't
// know the gist id beforehand, we can't do this in a single run :(
fetch([ENDPOINT, data.id].join('/'), {
method: 'PATCH',
headers: { authorization },
body: JSON.stringify({
description: link,
}),
});
}
return {
id: data.id,
version: data.history[0].version,
url: permaLink,
};
}
async function handler(event, context, callback) {
const method = event.httpMethod.toUpperCase();
const path = event.path.split('/');
// id: string, version: string, action = 'fork' | undefined
const [id, version, action] = path.slice(path.indexOf('gist') + 1);
// handle gist retrieval
// GET api/gist/id/version
if (method === 'GET') {
const data = await getGist({ id, version });
// we check the files, to be sure that given gist-id is something that
// playground will understand
if (!matchExpectedFiles(data.files)) {
return callback(null, incorrectParams('invalid id provided'));
}
return callback(null, json(data));
}
// github can fork gists, but doesn't allow forking under the same account
// and it also doesn't allow forking specific revisions (commits). Hence
// we just fetch the gist, and submit it as a new one instead.
if (method === 'POST' && (action === 'fork' || version === 'fork')) {
const { files } = await getGist({ id, version });
// we still verify the files, as we can't be sure that the gist hasn't been
// updated externally (via github interface, or even git cli)
if (!matchExpectedFiles(files)) {
return callback(null, incorrectParams('unsupported files provided'));
}
const data = await saveGist({ files });
return callback(null, json(data));
}
// handle saving to gist, POST for creation and PATCH for updates
if (method === 'POST' || method === 'PATCH') {
const { files } = JSON.parse(event.body);
// check if the provided files match our expectations
if (!matchExpectedFiles(files)) {
return callback(null, incorrectParams('unsupported files provided'));
}
const data = await saveGist({ id, files });
return callback(null, json(data));
}
return callback(null, incorrectParams('What did you do? o.O'));
}
module.exports = { handler };