FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
lambda-scraper/lambda/proxy-i.js at main · rev183/lambda-scraper · GitHub
rev183
/
lambda-scraper
Public
forked from
teticio/lambda-scraper
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
lambda-scraper
/
lambda
/
proxy-i.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
111 lines (102 loc) · 3.59 KB
Breadcrumbs
lambda-scraper
/
lambda
/
proxy-i.js
Copy path
File metadata and controls
111 lines (102 loc) · 3.59 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
if
(
typeof
(
awslambda
)
===
'undefined'
)
{
// For testing
global
.
awslambda
=
require
(
'./awslambda'
)
;
}
const
axios
=
require
(
'axios'
)
;
const
pipeline
=
require
(
'util'
)
.
promisify
(
require
(
'stream'
)
.
pipeline
)
;
const
{
Transform
}
=
require
(
'stream'
)
;
// awslambda.streamifyResponse does not send headers with an empty response
// so we have to patch it
class
patchEmptyResponse
extends
Transform
{
constructor
(
options
)
{
super
(
options
)
;
this
.
hasData
=
false
;
}
_transform
(
chunk
,
encoding
,
callback
)
{
this
.
hasData
=
true
;
this
.
push
(
chunk
)
;
callback
(
)
;
}
_final
(
callback
)
{
if
(
!
this
.
hasData
)
{
this
.
push
(
' '
)
;
}
callback
(
)
;
}
}
exports
.
lambdaHandler
=
awslambda
.
streamifyResponse
(
async
(
event
,
responseStream
,
context
)
=>
{
try
{
const
rawQueryString
=
event
.
headers
[
'lambda-scraper-raw-query-string'
]
?
event
.
headers
[
'lambda-scraper-raw-query-string'
]
:
event
.
rawQueryString
;
let
headers
=
Object
.
fromEntries
(
Object
.
entries
(
event
.
headers
)
.
filter
(
(
[
key
]
)
=>
!
key
.
toLowerCase
(
)
.
startsWith
(
'x-amz'
)
&&
!
key
.
toLowerCase
(
)
.
startsWith
(
'x-forwarded-'
)
&&
key
.
toLowerCase
(
)
!==
'host'
&&
key
!==
'lambda-scraper-raw-query-string'
)
.
map
(
(
[
key
,
value
]
)
=>
[
key
.
replace
(
/
^
l
a
m
b
d
a
-
s
c
r
a
p
e
r
-
/
,
''
)
,
value
]
)
)
;
let
url
=
event
.
rawPath
.
startsWith
(
'/http'
)
?
event
.
rawPath
.
substring
(
1
)
:
'https:/'
+
event
.
rawPath
;
if
(
rawQueryString
&&
rawQueryString
!==
''
)
{
url
+=
'?'
+
rawQueryString
;
}
const
httpRequest
=
{
method
:
event
.
requestContext
.
http
.
method
,
url
:
url
,
headers
:
headers
,
responseType
:
'stream'
,
timeout
:
600
*
1000
,
// 10 minutes
}
if
(
event
.
body
)
{
httpRequest
.
data
=
event
.
body
;
}
let
httpResponse
;
try
{
httpResponse
=
await
axios
(
httpRequest
)
;
}
catch
(
error
)
{
if
(
error
.
response
)
{
await
pipeline
(
error
.
response
.
data
,
awslambda
.
HttpResponseStream
.
from
(
responseStream
,
{
statusCode
:
error
.
response
.
status
,
headers
:
error
.
response
.
headers
,
}
)
,
)
;
return
;
}
throw
error
;
}
headers
=
Object
.
fromEntries
(
Object
.
entries
(
httpResponse
.
headers
)
.
map
(
(
[
key
,
value
]
)
=>
key
.
toLowerCase
(
)
.
startsWith
(
'x-amz'
)
?
[
'lambda-scraper-'
+
key
,
value
]
:
[
key
,
value
]
)
)
;
await
pipeline
(
httpResponse
.
data
,
new
patchEmptyResponse
(
)
,
awslambda
.
HttpResponseStream
.
from
(
responseStream
,
{
statusCode
:
httpResponse
.
status
,
headers
:
headers
,
}
)
,
)
;
}
catch
(
error
)
{
console
.
error
(
error
)
;
responseStream
=
awslambda
.
HttpResponseStream
.
from
(
responseStream
,
{
statusCode
:
500
,
headers
:
{
'Content-Type'
:
'application/json'
,
}
}
)
;
responseStream
.
write
(
JSON
.
stringify
(
{
message
:
error
.
message
,
stack
:
error
.
stack
}
)
)
;
responseStream
.
end
(
)
;
}
}
)
;
if
(
require
.
main
===
module
)
{
// For testing
const
event
=
{
rawPath
:
'/https://ipinfo.io/ip'
,
rawQueryString
:
''
,
headers
:
{
}
,
requestContext
:
{
http
:
{
method
:
'GET'
,
}
}
}
;
exports
.
lambdaHandler
(
event
,
{
}
)
.
then
(
)
.
catch
(
error
=>
console
.
error
(
error
)
)
;
}
Back
|
FazBrowse Home
|
New Git URL