Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat: add custom lookup option #134

Merged
merged 1 commit into from
Sep 22, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/function/event.d.ts
Original file line number Diff line number Diff line change
@@ -9,6 +9,10 @@ export interface Event {
multiValueQueryStringParameters: EventMultiValueQueryStringParameters | null
body: string | null
isBase64Encoded: boolean
streaming: {
callback_url: string
target_ipv4: string
}
}

interface EventHeaders {
17 changes: 11 additions & 6 deletions src/lib/streamer.js
Original file line number Diff line number Diff line change
@@ -13,12 +13,17 @@ class StreamingResponse extends PassThrough {
outgoingMessage
METADATA_BOUNDARY = `___x_nf-metadata_boundary-${Date.now()}`

constructor(url) {
constructor(url, ip) {
super()
// eslint-disable-next-line node/no-unsupported-features/node-builtins
const parsedUrl = new URL(url)
this.outgoingMessage =
parsedUrl.protocol === 'https:' ? https.request(url, { method: 'POST' }) : http.request(url, { method: 'POST' })
const options = {
lookup: (address, options, callback) => {
callback(null, ip)
},
method: 'POST',
}
this.outgoingMessage = parsedUrl.protocol === 'https:' ? https.request(url, options) : http.request(url, options)
this.outgoingMessage.setHeader('x-nf-metadata-boundary', this.METADATA_BOUNDARY)
this.pipe(this.outgoingMessage)
}
@@ -62,9 +67,9 @@ const wrapHandler =
*/
(event, context, callback) => {
console.log({ event })
const callbackUrl = event.queryStringParameters.callbackURL
const { callback_url: callbackUrl, target_ipv4: callbackIP } = event.streaming

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

try {
res = new StreamingResponse(callbackUrl)
res = new StreamingResponse(callbackUrl, callbackIP)
} catch (error) {
console.error(error)
return {