BackendStack21 / restana

Restana is a lightweight and fast Node.js framework for building RESTful APIs.
MIT License
467 stars 27 forks source link

Fast-json-stringify #101

Closed schamberg97 closed 4 years ago

schamberg97 commented 4 years ago

Is your feature request related to a problem? Please describe. Hey there! I was lurking through the restana source codes (again) looking for ways to optimise it further, but stumbled upon a comment in response-extensions.js: // ToDo: fast json stringify ?. I do not currently use fast-json-stringify, so I might be biased regarding its importance, but I think this is something to be debated about by those who use restana?

Problems with including fast-json-stringify

The problem with fast-json-stringify is that it requires a schema. Without a proper schema, it errors out or produces {} string. However, the current res.send API does not leave any possibility to pass the schema in any convenient manner. I have tried to compile some possible options there could be.

Possible alternatives

  1. Including a separate res.send alternative in restana core just for the purpose of fast-json-stringify. Something like res.fastJson

    Pros: 1) solves the API problem; 2) no unnecessary checks Cons: 1) software bloat; 2) additional dependency; 3) violates the restana philosophy

  2. Allow to pass schema as res.fastJsonSchema (defaulting to null), which will be passed to stringify function. It could then look like this:

    const stringify = (obj, schema) => {
      if (schema) {
         return fastJson(schema)(obj)
      }
      return JSON.stringify(obj)
    }

    Pros: avoids the API problem Cons: 1) additional dependency; 2) additional check with every res.send response with a proper object, which will decrease the op/s count; 3) violates the restana philosophy

  3. Leave the issue to the restana users. Those who need fast-json-stringify should be perfectly capable of using it themselves, without any sugar in restana. However, the comment in question should be deleted to avoid future confusion.

    Pros: 1) API problem? What problem? 2) most compatible with restana philosophy Cons: none?

jkyberneees commented 4 years ago

Hi @schamberg97 thanks for taking your time to contribute and improve restana.

Totally valid concerns, as you describe, I clearly prefer your option 3 for the same reasons exposed: What problem? ☺️ this is restana's minimalist design.

I will refactor the lines in question ASAP.

Thanks.