recycleactor commited on
Commit
bfb56ae
·
verified ·
1 Parent(s): d14d270

Upload server.js

Browse files
Files changed (1) hide show
  1. server.js +10 -7
server.js CHANGED
@@ -24,6 +24,9 @@ function normalizeUrl(u) {
24
  if (!u) return null
25
  let s = String(u).trim()
26
  if (!s) return null
 
 
 
27
  try {
28
  const url = new URL(s)
29
  if (url.protocol !== 'http:' && url.protocol !== 'https:') return null
@@ -34,7 +37,7 @@ function normalizeUrl(u) {
34
  }
35
  }
36
 
37
- async function fetchText(url, timeoutMs) {
38
  const controller = new AbortController()
39
  const t = setTimeout(() => controller.abort(), timeoutMs)
40
 
@@ -43,7 +46,7 @@ async function fetchText(url, timeoutMs) {
43
  redirect: 'follow',
44
  signal: controller.signal,
45
  headers: {
46
- 'user-agent': 'lampa-es5-proxy/0.1'
47
  }
48
  })
49
 
@@ -136,9 +139,9 @@ function jsWrapper(serverBase, baseUrl, transpiledCode, needRegenerator) {
136
  return parts.join('\n')
137
  }
138
 
139
- async function cssInline(url, depth) {
140
  if (depth > MAX_CSS_DEPTH) return ''
141
- const { text } = await fetchText(url, 25_000)
142
  const base = url.replace(/[?#].*$/, '').replace(/[^/]*$/, '')
143
  let css = text
144
 
@@ -160,7 +163,7 @@ async function cssInline(url, depth) {
160
  for (let i = 0; i < imports.length; i++) {
161
  let inlined = ''
162
  try {
163
- inlined = await cssInline(imports[i], depth + 1)
164
  } catch (e) {
165
  inlined = ''
166
  }
@@ -197,7 +200,7 @@ app.get('/s', async (req, res) => {
197
  if (!source) return res.status(400).type('text/plain').send('Bad url')
198
 
199
  try {
200
- const css = await cssInline(source, 0)
201
  res.setHeader('content-type', 'text/css; charset=utf-8')
202
  res.setHeader('cache-control', 'public, max-age=600')
203
  res.status(200).send(css)
@@ -212,7 +215,7 @@ app.get('/p', async (req, res) => {
212
  if (!source) return res.status(400).type('text/plain').send('Bad url')
213
 
214
  try {
215
- const { text } = await fetchText(source, 25_000)
216
  const result = await babel.transformAsync(text, {
217
  babelrc: false,
218
  configFile: false,
 
24
  if (!u) return null
25
  let s = String(u).trim()
26
  if (!s) return null
27
+ if (!/^https?:\/\//i.test(s)) {
28
+ s = 'https://' + s
29
+ }
30
  try {
31
  const url = new URL(s)
32
  if (url.protocol !== 'http:' && url.protocol !== 'https:') return null
 
37
  }
38
  }
39
 
40
+ async function fetchText(url, timeoutMs, clientUA) {
41
  const controller = new AbortController()
42
  const t = setTimeout(() => controller.abort(), timeoutMs)
43
 
 
46
  redirect: 'follow',
47
  signal: controller.signal,
48
  headers: {
49
+ 'user-agent': clientUA ? (clientUA + ' Lampa') : 'Mozilla/5.0 (Web0S; Linux/SmartTV) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36 Lampa'
50
  }
51
  })
52
 
 
139
  return parts.join('\n')
140
  }
141
 
142
+ async function cssInline(url, depth, clientUA) {
143
  if (depth > MAX_CSS_DEPTH) return ''
144
+ const { text } = await fetchText(url, 25_000, clientUA)
145
  const base = url.replace(/[?#].*$/, '').replace(/[^/]*$/, '')
146
  let css = text
147
 
 
163
  for (let i = 0; i < imports.length; i++) {
164
  let inlined = ''
165
  try {
166
+ inlined = await cssInline(imports[i], depth + 1, clientUA)
167
  } catch (e) {
168
  inlined = ''
169
  }
 
200
  if (!source) return res.status(400).type('text/plain').send('Bad url')
201
 
202
  try {
203
+ const css = await cssInline(source, 0, req.headers['user-agent'])
204
  res.setHeader('content-type', 'text/css; charset=utf-8')
205
  res.setHeader('cache-control', 'public, max-age=600')
206
  res.status(200).send(css)
 
215
  if (!source) return res.status(400).type('text/plain').send('Bad url')
216
 
217
  try {
218
+ const { text } = await fetchText(source, 25_000, req.headers['user-agent'])
219
  const result = await babel.transformAsync(text, {
220
  babelrc: false,
221
  configFile: false,