-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
142 lines (122 loc) · 3.65 KB
/
index.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
/* global addEventListener */
/* global Response */
import { createRouter } from 'radix3';
import FastifyEdgeRequest, { readBody } from './request.js';
import FastifyEdgeReply, { kBody, kResponse, kRedirect } from './reply.js';
const kHooks = Symbol('kHooks');
const handleRequest = Symbol('handleRequest');
const getRoute = Symbol('getRoute');
const runHooks = Symbol('runHooks');
const runOnSendHooks = Symbol('runOnSendHooks');
const kRouter = Symbol('kRrouter');
const sendResponse = Symbol('sendResponse');
export class FastifyEdge {
[kHooks] = {
onRequest: [],
onSend: [],
onResponse: [],
};
[kRouter] = null;
constructor () {
this[kRouter] = createRouter();
this.setup();
}
setup () {
if (process.env.bun) {
this.fetch = request => this[sendResponse](request);
} else {
addEventListener('fetch', this[handleRequest].bind(this));
}
}
[handleRequest] (event) {
event.respondWith(this[sendResponse](event.request));
}
async [sendResponse] (request) {
const url = new URL(request.url);
const route = this[kRouter].lookup(url.pathname);
if (!route) {
return new Response('Not found', {
headers: { 'content-type': 'text/plain' },
status: 404,
});
}
const req = new FastifyEdgeRequest(request, url, route);
const reply = new FastifyEdgeReply(req);
await req[readBody]();
await this[runHooks](this[kHooks].onRequest, req, reply);
await this[runHooks](route.onRequest, req, reply);
await route.handler(req, reply);
await this[runOnSendHooks](this[kHooks].onSend, req, reply);
await this[runOnSendHooks](route.onSend, req, reply);
await this[runHooks](this[kHooks].onResponse, req, reply);
await this[runHooks](route.onResponse, req, reply);
if (reply[kRedirect]) {
return Response.redirect(...reply[kRedirect]);
} else {
return new Response(reply[kBody], reply[kResponse]);
}
}
addHook (hook, func) {
this[kHooks][hook].push(func);
}
route (settings) {
const route = this[getRoute](settings.method, settings.path, settings);
this[kRouter].insert(route.path, route);
}
get (path, settings) {
const route = this[getRoute]('get', path, settings);
this[kRouter].insert(path, route);
}
post (path, settings) {
const route = this[getRoute]('post', path, settings);
this[kRouter].insert(path, route);
}
put (path, settings) {
const route = this[getRoute]('put', path, settings);
this[kRouter].insert(path, route);
}
delete (path, settings) {
const route = this[getRoute]('delete', path, settings);
this[kRouter].insert(path, route);
}
options (path, settings) {
const route = this[getRoute]('options', path, settings);
this[kRouter].insert(path, route);
}
async [runHooks] (run, ...args) {
if (typeof run === 'function') {
run = [run];
}
if (Array.isArray(run)) {
for (const hook of run) {
await hook(...args);
}
}
}
async [runOnSendHooks] (run, req, reply) {
let altered;
let payload;
if (typeof run === 'function') {
run = [run];
}
if (Array.isArray(run)) {
for (const hook of run) {
payload = reply[kBody];
// eslint-disable-next-line no-cond-assign
if (altered = await hook(req, reply, payload) ?? false) {
reply[kBody] = altered;
}
}
}
}
[getRoute] (method, path, settings = {}) {
const route = { method, path };
if (typeof settings === 'function') {
route.handler = settings;
} else {
Object.assign(route, settings);
}
return route;
}
}
export default (...args) => new FastifyEdge(...args);