This avoids using Buffer constructor API on newer Node.js versions.
To achieve that, Buffer.from presence is checked, with validation that it's not the same method as Uint8Array.from.
Also an additional type-guard is added in the fallback code path to ensure that typed numbers are never accidently fed into new Buffer input.
Given this module popularity and the fact that old Buffer constructor API was used in a single place, I decided to just feature-detect it in-place instead of pulling in polyfill deps.
Another way would be to just use Buffer.from and do var Buffer = require('safe-buffer');
This avoids using Buffer constructor API on newer Node.js versions.
To achieve that,
Buffer.from
presence is checked, with validation that it's not the same method as Uint8Array.from.Also an additional type-guard is added in the fallback code path to ensure that typed numbers are never accidently fed into
new Buffer
input.Given this module popularity and the fact that old Buffer constructor API was used in a single place, I decided to just feature-detect it in-place instead of pulling in polyfill deps. Another way would be to just use
Buffer.from
and dovar Buffer = require('safe-buffer')
;Refs: https://nodejs.org/api/deprecations.html#deprecations_dep0005_buffer_constructor Tracking: https://github.com/nodejs/node/issues/19079