buffer.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // This is Buffer implementation used in SEA. Functionality is mostly
  2. // compatible with NodeJS 'safe-buffer' and is used for encoding conversions
  3. // between binary and 'hex' | 'utf8' | 'base64'
  4. // See documentation and validation for safe implementation in:
  5. // https://github.com/feross/safe-buffer#update
  6. var SeaArray = require('./array');
  7. function SafeBuffer(...props) {
  8. console.warn('new SafeBuffer() is depreciated, please use SafeBuffer.from()')
  9. return SafeBuffer.from(...props)
  10. }
  11. SafeBuffer.prototype = Object.create(Array.prototype)
  12. Object.assign(SafeBuffer, {
  13. // (data, enc) where typeof data === 'string' then enc === 'utf8'|'hex'|'base64'
  14. from() {
  15. if (!Object.keys(arguments).length) {
  16. throw new TypeError('First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object.')
  17. }
  18. const input = arguments[0]
  19. let buf
  20. if (typeof input === 'string') {
  21. const enc = arguments[1] || 'utf8'
  22. if (enc === 'hex') {
  23. const bytes = input.match(/([\da-fA-F]{2})/g)
  24. .map((byte) => parseInt(byte, 16))
  25. if (!bytes || !bytes.length) {
  26. throw new TypeError('Invalid first argument for type \'hex\'.')
  27. }
  28. buf = SeaArray.from(bytes)
  29. } else if (enc === 'utf8') {
  30. const length = input.length
  31. const words = new Uint16Array(length)
  32. Array.from({ length: length }, (_, i) => words[i] = input.charCodeAt(i))
  33. buf = SeaArray.from(words)
  34. } else if (enc === 'base64') {
  35. const dec = atob(input)
  36. const length = dec.length
  37. const bytes = new Uint8Array(length)
  38. Array.from({ length: length }, (_, i) => bytes[i] = dec.charCodeAt(i))
  39. buf = SeaArray.from(bytes)
  40. } else if (enc === 'binary') {
  41. buf = SeaArray.from(input)
  42. } else {
  43. console.info('SafeBuffer.from unknown encoding: '+enc)
  44. }
  45. return buf
  46. }
  47. const byteLength = input.byteLength
  48. const length = input.byteLength ? input.byteLength : input.length
  49. if (length) {
  50. let buf
  51. if (input instanceof ArrayBuffer) {
  52. buf = new Uint8Array(input)
  53. }
  54. return SeaArray.from(buf || input)
  55. }
  56. },
  57. // This is 'safe-buffer.alloc' sans encoding support
  58. alloc(length, fill = 0 /*, enc*/ ) {
  59. return SeaArray.from(new Uint8Array(Array.from({ length: length }, () => fill)))
  60. },
  61. // This is normal UNSAFE 'buffer.alloc' or 'new Buffer(length)' - don't use!
  62. allocUnsafe(length) {
  63. return SeaArray.from(new Uint8Array(Array.from({ length : length })))
  64. },
  65. // This puts together array of array like members
  66. concat(arr) { // octet array
  67. if (!Array.isArray(arr)) {
  68. throw new TypeError('First argument must be Array containing ArrayBuffer or Uint8Array instances.')
  69. }
  70. return SeaArray.from(arr.reduce((ret, item) => ret.concat(Array.from(item)), []))
  71. }
  72. })
  73. SafeBuffer.prototype.from = SafeBuffer.from
  74. SafeBuffer.prototype.toString = SeaArray.prototype.toString
  75. module.exports = SafeBuffer;