buffer.js 3.3 KB

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