Skip to content

Commit bcf6946

Browse files
feat: add custom lookup option (#134)
1 parent 31dea42 commit bcf6946

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

src/function/event.d.ts

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ export interface Event {
99
multiValueQueryStringParameters: EventMultiValueQueryStringParameters | null
1010
body: string | null
1111
isBase64Encoded: boolean
12+
streaming: {
13+
callback_url: string
14+
target_ipv4: string
15+
}
1216
}
1317

1418
interface EventHeaders {

src/lib/streamer.js

+11-6
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,17 @@ class StreamingResponse extends PassThrough {
1313
outgoingMessage
1414
METADATA_BOUNDARY = `___x_nf-metadata_boundary-${Date.now()}`
1515

16-
constructor(url) {
16+
constructor(url, ip) {
1717
super()
1818
// eslint-disable-next-line node/no-unsupported-features/node-builtins
1919
const parsedUrl = new URL(url)
20-
this.outgoingMessage =
21-
parsedUrl.protocol === 'https:' ? https.request(url, { method: 'POST' }) : http.request(url, { method: 'POST' })
20+
const options = {
21+
lookup: (address, options, callback) => {
22+
callback(null, ip)
23+
},
24+
method: 'POST',
25+
}
26+
this.outgoingMessage = parsedUrl.protocol === 'https:' ? https.request(url, options) : http.request(url, options)
2227
this.outgoingMessage.setHeader('x-nf-metadata-boundary', this.METADATA_BOUNDARY)
2328
this.pipe(this.outgoingMessage)
2429
}
@@ -62,9 +67,9 @@ const wrapHandler =
6267
*/
6368
(event, context, callback) => {
6469
console.log({ event })
65-
const callbackUrl = event.queryStringParameters.callbackURL
70+
const { callback_url: callbackUrl, target_ipv4: callbackIP } = event.streaming
6671

67-
if (!callbackUrl) {
72+
if (!callbackUrl || !callbackIP) {
6873
return {
6974
statusCode: 422,
7075
body: 'Missing callback URL',
@@ -75,7 +80,7 @@ const wrapHandler =
7580
let res
7681

7782
try {
78-
res = new StreamingResponse(callbackUrl)
83+
res = new StreamingResponse(callbackUrl, callbackIP)
7984
} catch (error) {
8085
console.error(error)
8186
return {

0 commit comments

Comments
 (0)