-
Notifications
You must be signed in to change notification settings - Fork 8
/
worker.js
311 lines (263 loc) · 7.91 KB
/
worker.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
/**
* Helper functions that when passed a request will return a
* boolean indicating if the request uses that HTTP method,
* header, host or referrer.
*/
const Method = (method) => (req) => req.method.toLowerCase() === method.toLowerCase();
const Connect = Method("connect");
const Delete = Method("delete");
const Get = Method("get");
const Head = Method("head");
const Options = Method("options");
const Patch = Method("patch");
const Post = Method("post");
const Put = Method("put");
const Trace = Method("trace");
const Header = (header, val) => (req) => req.headers.get(header) === val;
const Host = (host) => Header("host", host.toLowerCase());
const Referrer = (host) => Header("referrer", host.toLowerCase());
const Path = (regExp) => (req) => {
const url = new URL(req.url);
const path = url.pathname;
const match = path.match(regExp) || [];
return match[0] === path;
};
/**
* The Router handles determines which handler is matched given the
* conditions present for each request.
*/
class Router {
constructor() {
this.routes = [];
}
handle(conditions, handler) {
this.routes.push({
conditions,
handler,
});
return this;
}
connect(url, handler) {
return this.handle([Connect, Path(url)], handler);
}
delete(url, handler) {
return this.handle([Delete, Path(url)], handler);
}
get(url, handler) {
return this.handle([Get, Path(url)], handler);
}
head(url, handler) {
return this.handle([Head, Path(url)], handler);
}
options(url, handler) {
return this.handle([Options, Path(url)], handler);
}
patch(url, handler) {
return this.handle([Patch, Path(url)], handler);
}
post(url, handler) {
return this.handle([Post, Path(url)], handler);
}
put(url, handler) {
return this.handle([Put, Path(url)], handler);
}
trace(url, handler) {
return this.handle([Trace, Path(url)], handler);
}
all(handler) {
return this.handle([], handler);
}
route(req) {
const route = this.resolve(req);
if (route) {
return route.handler(req);
}
return new Response("resource not found", {
status: 404,
statusText: "not found",
headers: {
"content-type": "text/plain",
},
});
}
/**
* resolve returns the matching route for a request that returns
* true for all conditions (if any).
*/
resolve(req) {
return this.routes.find((r) => {
if (!r.conditions || (Array.isArray(r) && !r.conditions.length)) {
return true;
}
if (typeof r.conditions === "function") {
return r.conditions(req);
}
return r.conditions.every((c) => c(req));
});
}
}
const headers = new Headers({
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
});
const corsHeaders = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET,POST,OPTIONS",
"Access-Control-Max-Age": "86400",
};
addEventListener("fetch", (event) => {
const request = event.request;
if (request.method === "OPTIONS") {
// Handle CORS preflight requests
event.respondWith(handleOptions(request));
} else {
event.respondWith(handleRequest(request));
}
});
function handleOptions(request) {
// Make sure the necessary headers are present
// for this to be a valid pre-flight request
let headers = request.headers;
if (headers.get("Origin") !== null && headers.get("Access-Control-Request-Method") !== null && headers.get("Access-Control-Request-Headers") !== null) {
// Handle CORS pre-flight request.
// If you want to check or reject the requested method + headers
// you can do that here.
let respHeaders = {
...corsHeaders,
// Allow all future content Request headers to go back to browser
// such as Authorization (Bearer) or X-Client-Name-Version
"Access-Control-Allow-Headers": request.headers.get("Access-Control-Request-Headers"),
};
return new Response(null, {
headers: respHeaders,
});
} else {
// Handle standard OPTIONS request.
// If you want to allow other HTTP Methods, you can do that here.
return new Response(null, {
headers: {
Allow: "GET, POST, OPTIONS",
},
});
}
}
function witnessOpts() {
let o = {};
o.dns = {};
o.reddit = {};
o.same = {};
if (GITHUB_USER_AGENT) {
o.github = {
user_agent: GITHUB_USER_AGENT,
delimitor: "\n\n"
}
}
if (SOUNDCLOUD_CLIENT_ID) {
let limit = parseInt(SOUNDCLOUD_LIMIT);
let offset = parseInt(SOUNDCLOUD_MAX_OFFSET);
if (!isNaN(limit) && !isNaN(offset)) {
o.soundcloud = {
client_id: SOUNDCLOUD_CLIENT_ID,
limit,
max_offset: offset,
}
}
}
if (TWITTER_BEARER_TOKEN) {
o.twitter = {
api_key: TWITTER_BEARER_TOKEN,
delimitor: "\n\n"
}
}
let useSendGrid = SENDGRID_BEARER_TOKEN
&& SENDGRID_FROM_ADDRESS
&& SENDGRID_FROM_NAME
&& SENDGRID_SUBJECT_NAME
&& SENDGRID_MAX_ELAPSED_MINS
&& !isNaN(parseInt(SENDGRID_MAX_ELAPSED_MINS));
if (useSendGrid) {
o.email = {
api_key: SENDGRID_BEARER_TOKEN,
from_addr: SENDGRID_FROM_ADDRESS,
from_name: SENDGRID_FROM_NAME,
max_elapsed_minutes: parseInt(SENDGRID_MAX_ELAPSED_MINS),
subject_name: SENDGRID_SUBJECT_NAME,
}
}
let useAlchemy = ALCHEMY_API_KEY
&& ALCHEMY_MAX_ELAPSED_MINS
&& !isNaN(parseInt(ALCHEMY_MAX_ELAPSED_MINS));
if (useAlchemy) {
o.nft_ownership = {
api_key: ALCHEMY_API_KEY,
max_elapsed_minutes: parseInt(ALCHEMY_MAX_ELAPSED_MINS)
}
}
return o
};
const opts = {
witness: witnessOpts(),
did: DID_WEB
}
const {statement, witness, instructions} = wasm_bindgen;
const instance = wasm_bindgen(wasm);
async function stmt(request) {
try {
await instance;
const h = request.headers;
const contentType = h.get('content-type') || '';
if (contentType.includes('application/json')) {
const body_str = JSON.stringify(await request.json());
const res = await statement(REBASE_SK, body_str, JSON.stringify(opts));
return new Response(res, {status: 200, headers: headers});
} else {
return new Response(JSON.stringify(new Error(`Expected content-type application/json, got: ${contentType}`)), {
status: 400,
headers: headers
});
}
} catch (e) {
return new Response(JSON.stringify(e), { status: 400, headers: headers});
}
}
async function wtns(request) {
try {
await instance;
const h = request.headers;
const contentType = h.get('content-type') || '';
if (contentType.includes('application/json')) {
let body = await request.json();
const credential = await witness(REBASE_SK, JSON.stringify(body), JSON.stringify(opts));
return new Response(credential, {status: 200, headers: headers});
} else {
throw new Error(`Expected content-type application/json, got: ${contentType}`)
}
} catch (e) {
return new Response(JSON.stringify({error: e?.message ? e.message : e}), { status: 400, headers: headers});
}
}
async function inst(request) {
try {
await instance;
const h = request.headers;
const contentType = h.get('content-type') || '';
if (contentType.includes('application/json')) {
let body = await request.json();
const res = await instructions(JSON.stringify(body), JSON.stringify(opts));
return new Response(res, {status: 200, headers: headers});
} else {
throw new Error(`Expected content-type application/json, got: ${contentType}`)
}
} catch (e) {
return new Response(JSON.stringify({error: e?.message ? e.message : e}), { status: 400, headers: headers});
}
}
async function handleRequest(request) {
const r = new Router();
r.post("/statement", (request) => stmt(request));
r.post("/witness", (request) => wtns(request));
r.post("/instructions", (request) => inst(request));
// r.post("/verify", (request) => verify(request))
const resp = await r.route(request);
return resp;
}