Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

feat(Worker): add cf-* headers to request #21

Closed
wants to merge 3 commits into from
Closed

feat(Worker): add cf-* headers to request #21

wants to merge 3 commits into from

Conversation

jdanyow
Copy link
Collaborator

@jdanyow jdanyow commented Dec 16, 2018

#17

app/worker.js Outdated Show resolved Hide resolved
app/worker.js Outdated Show resolved Hide resolved
package.json Outdated Show resolved Hide resolved
@gja
Copy link
Owner

gja commented Dec 16, 2018

Hi, thanks for the pr. If you can fix the IP address thing I’ll merge it :-). Any thoughts on how to do https?

@jdanyow
Copy link
Collaborator Author

jdanyow commented Dec 16, 2018

@gja thanks for the feedback. I don't know if I have this quite right yet. I've added a buildRequest function in server.js and moved all the request building logic in server.js and worker.js to that method. The function takes the express request as it's only argument.

It's a tricky refactor because of the dependency shift. Building the request for the fetch event needs the ip, etc. I'm wondering if the worker spec should take a dependency on buildRequest rather than calling new Request, or, would your rather I revert and move the request building logic to the worker?

headers: req.headers,
function buildRequest(req) {
const url = `${req.protocol}://${req.get('host')}${req.originalUrl}`;
const ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress.split(':').pop();
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

req.connection.remoteAddress.split(':').pop()

pro: no additional dependencies
con: there are more robust options

@gja
Copy link
Owner

gja commented Dec 17, 2018

Good points. I think it does make sense to keep the logic in the worker instead of in the server. Primarily because the worker should be more reusable.

Is it possible to keep the API signature of worker the same? (taking url and opts). But the Headers are added by the worker. opts.remoteAddress can have the value of node.connection.remoteAddress.

Let me know if you'd like me to pick this up as well, I have a bit of bandwidth tonight.

@jdanyow
Copy link
Collaborator Author

jdanyow commented Dec 17, 2018

Thanks @gja, I'm happy to keep plugging away at it but if you have bandwidth and want to pick this up, please go ahead. I think you have a clearer vision for how this should be implemented. I'll probably be able to get back to this in the next few days.

gja added a commit that referenced this pull request Dec 18, 2018
@gja gja closed this in #22 Dec 18, 2018
gja added a commit that referenced this pull request Dec 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants