GoogleChromeLabs / carlo

Web rendering surface for Node applications
Apache License 2.0
9.31k stars 309 forks source link

Serve files from filesystem and wwwOrigin #78

Closed vladimir-shilin closed 5 years ago

vladimir-shilin commented 5 years ago

I need to serve application from wwwOrigin in development mode, but app need to have access to filesystem. At first this code searches files by prefixes in filesystem, then tries to get it from wwwOrigin. Also there is fix which converts url to path for searching in filesystem with decodeURIComponent function.

googlebot commented 5 years ago

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

:memo: Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here (e.g. I signed it!) and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers
vladimir-shilin commented 5 years ago

I signed it!

googlebot commented 5 years ago

CLAs look good, thanks!

pavelfeldman commented 5 years ago

I've seen the opposite requests as well where dev workflow was supposed to override the locally available files. I.e. order matters. I wonder if we should deprecate serveOrigin in favor of a smarter serveFolder:

app.serveFolder('http://localhost', 'prefix');
app.serveFolder('www');

These would be matched in the specified order. Would that work for you?

vladimir-shilin commented 5 years ago

Yes. That would be great.

pavelfeldman commented 5 years ago

Do you want to do it or should I?

vladimir-shilin commented 5 years ago

I can try.

pavelfeldman commented 5 years ago

Sounds good! I was thinking of keeping serveOrigin for backwards compat:

serveOrigin(origin) {
  this.serveFolder(origin);
}

and then in the for (const [prefix, folders] of this.www_) loop detect those that are origins and serve files respectively. We can assume that serving from the origin always succeeds.

vladimir-shilin commented 5 years ago

I see the problem that search of file will stop at first folder which is url. I think we need make request in that case and check response code. Can I use some library for that or use node http and https libraries instead?

pavelfeldman commented 5 years ago

I thought your prefix was sufficient to match deterministically. Could you paste your filsystem structure and which files you would like to serve over http for me to understand it better?

vladimir-shilin commented 5 years ago

In my case I'll have index.html and app.js on http://locahost and other files in filesystem by prefixes. For example app.serveFolder('http://locahost'); app.serveFolder('somefolder/images', 'images'); Even in this case request /images/1.jpg will match prefix '' and get 404 error.

pavelfeldman commented 5 years ago

This would do what you want though, right? (Order matters)

app.serveFolder('somefolder/images', 'images');
app.serveFolder('http://locahost');
vladimir-shilin commented 5 years ago

Not exactly. At first app opens, then user choose folder with images.

pavelfeldman commented 5 years ago

Got it. Let me take it offline. You might need manual request interception to resolve this. I was planning on adding it anyways.

vladimir-shilin commented 5 years ago

Thanks. Good luck!