buffer.js 3.4 KB

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