diia-open-source / be-pkg-utils

European Union Public License 1.2
14 stars 10 forks source link

Better use js native utilities or implement v8-isolation compatible colution #12

Open tshemsedinov opened 4 months ago

tshemsedinov commented 4 months ago
  1. JavaScript has native tools to do that: Array.isArray, Buffer.isBuffer
  2. This will not work for v8 sandboxes
  3. But you can't use it in all cases, better solution is to check instance.constructor.name, this will work also for isolated contexts in v8
  4. Utility class is well-known antipattern, see Clean code

https://github.com/diia-open-source/be-pkg-utils/blob/e969374fb769e59ab6bbdb709caeba0199e14a8c/src/typeUtils.ts#L1-L13

vird commented 4 months ago

Problem with value.type === 'Buffer' Than it will also say that it's valid buffer if it's not a buffer, but serialized buffer (or better say under-deserialized) e.g. this way https://nodejs.org/api/buffer.html

const copy = JSON.parse(json, (key, value) => {
  return value && value.type === 'Buffer' ?
    Buffer.from(value) :
    value;
});