-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpsina.js
163 lines (137 loc) · 3.77 KB
/
psina.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
const http = require('http');
const https = require('https');
async function fetch4(url, options = {}) {
const u = new URL(url);
return new Promise(async (resolve, reject) => {
const requestOptions = {
hostname: u.hostname,
port: 443,
path: u.pathname + u.search,
method: options.method,
family: 4,
headers: options.headers
};
const req = https.request(requestOptions, async (res) => {
try {
let responseText = '';
for await (const chunk of res) {
responseText += chunk;
}
resolve({
responseText,
headers: res.headers,
status: res.statusCode,
url
});
} catch (error) {
reject({
error,
headers: res.headers,
status: res.statusCode,
url
});
}
});
req.on('error', (error) => reject(error));
if (options.body) req.write(options.body);
req.end();
});
}
async function $fetch(request, response) {
if (!/post/i.test(request.method)) {
return response.writeHead(405).end();
}
const buffers = [];
for await (const chunk of request) {
buffers.push(chunk);
}
try {
const body = JSON.parse(Buffer.concat(buffers).toString());
const headers = body.headers ?? {};
const requestOptions = {
method: body.method?.toUpperCase() ?? request.method.toUpperCase(),
headers
};
if (typeof body.body === 'string') {
requestOptions.body = body.body;
} else if (body.body && typeof body.body === 'object') {
requestOptions.body = JSON.stringify(body.body);
}
if (
requestOptions.body &&
typeof requestOptions.headers['Content-Length'] === 'undefined'
)
requestOptions.headers['Content-Length'] = Buffer.byteLength(
requestOptions.body
);
const fetchResponse = await fetch4(body.url, requestOptions);
const ct = fetchResponse.headers['content-type'];
if (ct) response.setHeader('Content-Type', ct);
const headersToInclude = [];
if (Array.isArray(body.headersToInclude)) {
for (const header of body.headersToInclude) {
headersToInclude.push({
header,
value: fetchResponse.headers[header]
});
}
}
response.writeHead(fetchResponse.status);
if (headersToInclude.length) {
response.write(
JSON.stringify({
headers: headersToInclude,
response: fetchResponse.responseText
})
);
} else {
response.write(fetchResponse.responseText);
}
response.end();
} catch (e) {
console.error(e);
response.setHeader('Content-Type', 'application/json; charset=UTF-8');
response.writeHead(400);
response.write(
JSON.stringify({
error: {
message: e.message
}
})
);
response.end();
}
}
const server = http
.createServer(async (request, response) => {
response.setHeader('Access-Control-Allow-Origin', '*');
response.setHeader(
'Access-Control-Allow-Methods',
'GET, POST, OPTIONS, PUT, PATCH, DELETE'
);
response.setHeader('Access-Control-Allow-Headers', '*');
if (/options/i.test(request.method)) {
return response.writeHead(200).end();
}
switch (request.url) {
case '/':
response.setHeader('Content-Type', 'application/json;charset=UTF-8');
response.write(
JSON.stringify({
ok: true,
result: {
env: {
PPP_WORKER_ID: 'PSINA_PUBLIC_TOOLKIT'
}
}
})
);
response.end();
break;
case '/fetch':
return $fetch(request, response);
default:
response.writeHead(404).end();
}
})
.listen(process.env.PORT ?? 9998);