2

I'd like to map an API request 1::1 with a file name.

This means, among other things, serializing query params into something safe for a filename, ideally on both Windows and *nix.

If the api request is this:

api/eggs/fresh?search=brown&order=asc

Then the filename might be:

api/eggs/fresh?search=brown&order=asc.json

But the ? char and maybe the & are unsafe, so I have to encode them I think.

The serialization must be bi-directionally deterministic. I have to be able to get the data back out. Converting all illegal characters to _, for example, wouldn't meet this criteria.

Is the right method querystring.encode? This is for encoding URLs but might work if % is file system safe. How do I URl encode something in Node.js?? Or is there something better suited to a file system?

Community
  • 1
  • 1
SimplGy
  • 20,079
  • 15
  • 107
  • 144

1 Answers1

0

I'm following this process:

  1. Get folder + resource name
  2. Get Array of params in the request
  3. Alphabetize params (so order doesn't matter)
  4. Join params with & (I think this is safe in modern file systems)
  5. Concat folder/resource + __&__ + paramString + .json

This fails if someone uses __&__ as part of an actual query, but I think that's unlikely. Not the cleanest solution, I'd love a better one!

SimplGy
  • 20,079
  • 15
  • 107
  • 144