-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
125 lines (105 loc) · 3.56 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
import { readFile } from 'node:fs/promises'
import { join } from 'node:path'
import fp from 'fastify-plugin'
import AutoLoad from '@fastify/autoload'
import S from 'fluent-json-schema'
import Undici from 'undici'
export default fp(async function app (fastify, opts) {
await fastify.register(import('./plugins/config.no-load.js'), structuredClone(opts))
await fastify.register(AutoLoad, {
dir: join(import.meta.dirname, 'plugins'),
ignorePattern: /.*.no-load\.js/,
options: structuredClone(opts),
})
fastify.get('/', async (request, reply) => {
return reply.render('index.njk')
})
fastify.get('/request/:fingerprint', {
schema: {
params: S.object()
.prop('fingerprint', S.string()),
},
},
async (request, reply) => {
const file = fastify.fileIndex[request.params.fingerprint]
if (!file) {
return reply.render('404.njk', { file: request.params.fingerprint, message: 'not found' })
}
// get the file contents
const fullPath = join(import.meta.dirname, fastify.config.dirPath, file)
const fileContents = JSON.stringify(JSON.parse((await readFile(fullPath)).toString()), null, 2)
return reply.render('file.njk', { file, fileContents, fingerprint: request.params.fingerprint })
})
await fastify.register(import('@fastify/formbody'), opts)
fastify.post('/request/:fingerprint', {
schema: {
params: S.object()
.prop('fingerprint', S.string()),
},
},
async (request, reply) => {
const file = fastify.fileIndex[request.params.fingerprint]
if (!file) {
return reply.render('404.njk', { file: request.params.fingerprint, message: 'not found' })
}
// get the file contents
const fullPath = join(import.meta.dirname, fastify.config.dirPath, file)
const fileContents = JSON.parse((await readFile(fullPath)).toString())
// Prepare request
const requestConfig = {
method: '',
url: '',
maxRedirects: 0,
responseFormat: 'json',
}
requestConfig.method = fileContents.method
requestConfig.url = fileContents.url
requestConfig.responseFormat = fileContents.responseFormat
if (fileContents.followRedirects) {
request.log.debug(fileContents.followRedirects, 'Follow redirects')
if (fileContents.followRedirects === true) {
// Some arbitrary number of redirects, we don't want too many though
requestConfig.maxRedirects = 10
} else if (typeof fileContents.folowRedirects === 'number') {
requestConfig.maxRedirects = fileContents.folowRedirects
} else {
throw new Error('followRedirects invalid')
}
}
// perform request
const { statusCode, headers, trailers, body } = await Undici.request(
requestConfig.url,
{
method: requestConfig.method,
maxRedirections: requestConfig.maxRedirects,
}
)
let bodyData = ''
for await (const data of body) {
bodyData += data.toString()
}
if (requestConfig.responseFormat) {
switch (requestConfig.responseFormat) {
case 'json':
try {
console.log('test')
bodyData = JSON.parse(bodyData)
} catch (err) {
console.error(err)
console.info('Response body is not valid JSON')
}
}
}
return reply.render('file.njk', {
file,
fileContents: JSON.stringify(fileContents, null, 2),
fingerprint: request.params.fingerprint,
response: {
statusCode,
headers: JSON.stringify(headers, null, 2),
body: bodyData,
trailers,
},
})
})
})