11cb0ef41Sopenharmony_ci/* eslint-disable node/no-deprecated-api */ 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ci'use strict' 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_civar buffer = require('buffer') 61cb0ef41Sopenharmony_civar Buffer = buffer.Buffer 71cb0ef41Sopenharmony_ci 81cb0ef41Sopenharmony_civar safer = {} 91cb0ef41Sopenharmony_ci 101cb0ef41Sopenharmony_civar key 111cb0ef41Sopenharmony_ci 121cb0ef41Sopenharmony_cifor (key in buffer) { 131cb0ef41Sopenharmony_ci if (!buffer.hasOwnProperty(key)) continue 141cb0ef41Sopenharmony_ci if (key === 'SlowBuffer' || key === 'Buffer') continue 151cb0ef41Sopenharmony_ci safer[key] = buffer[key] 161cb0ef41Sopenharmony_ci} 171cb0ef41Sopenharmony_ci 181cb0ef41Sopenharmony_civar Safer = safer.Buffer = {} 191cb0ef41Sopenharmony_cifor (key in Buffer) { 201cb0ef41Sopenharmony_ci if (!Buffer.hasOwnProperty(key)) continue 211cb0ef41Sopenharmony_ci if (key === 'allocUnsafe' || key === 'allocUnsafeSlow') continue 221cb0ef41Sopenharmony_ci Safer[key] = Buffer[key] 231cb0ef41Sopenharmony_ci} 241cb0ef41Sopenharmony_ci 251cb0ef41Sopenharmony_cisafer.Buffer.prototype = Buffer.prototype 261cb0ef41Sopenharmony_ci 271cb0ef41Sopenharmony_ciif (!Safer.from || Safer.from === Uint8Array.from) { 281cb0ef41Sopenharmony_ci Safer.from = function (value, encodingOrOffset, length) { 291cb0ef41Sopenharmony_ci if (typeof value === 'number') { 301cb0ef41Sopenharmony_ci throw new TypeError('The "value" argument must not be of type number. Received type ' + typeof value) 311cb0ef41Sopenharmony_ci } 321cb0ef41Sopenharmony_ci if (value && typeof value.length === 'undefined') { 331cb0ef41Sopenharmony_ci throw new TypeError('The first argument must be one of type string, Buffer, ArrayBuffer, Array, or Array-like Object. Received type ' + typeof value) 341cb0ef41Sopenharmony_ci } 351cb0ef41Sopenharmony_ci return Buffer(value, encodingOrOffset, length) 361cb0ef41Sopenharmony_ci } 371cb0ef41Sopenharmony_ci} 381cb0ef41Sopenharmony_ci 391cb0ef41Sopenharmony_ciif (!Safer.alloc) { 401cb0ef41Sopenharmony_ci Safer.alloc = function (size, fill, encoding) { 411cb0ef41Sopenharmony_ci if (typeof size !== 'number') { 421cb0ef41Sopenharmony_ci throw new TypeError('The "size" argument must be of type number. Received type ' + typeof size) 431cb0ef41Sopenharmony_ci } 441cb0ef41Sopenharmony_ci if (size < 0 || size >= 2 * (1 << 30)) { 451cb0ef41Sopenharmony_ci throw new RangeError('The value "' + size + '" is invalid for option "size"') 461cb0ef41Sopenharmony_ci } 471cb0ef41Sopenharmony_ci var buf = Buffer(size) 481cb0ef41Sopenharmony_ci if (!fill || fill.length === 0) { 491cb0ef41Sopenharmony_ci buf.fill(0) 501cb0ef41Sopenharmony_ci } else if (typeof encoding === 'string') { 511cb0ef41Sopenharmony_ci buf.fill(fill, encoding) 521cb0ef41Sopenharmony_ci } else { 531cb0ef41Sopenharmony_ci buf.fill(fill) 541cb0ef41Sopenharmony_ci } 551cb0ef41Sopenharmony_ci return buf 561cb0ef41Sopenharmony_ci } 571cb0ef41Sopenharmony_ci} 581cb0ef41Sopenharmony_ci 591cb0ef41Sopenharmony_ciif (!safer.kStringMaxLength) { 601cb0ef41Sopenharmony_ci try { 611cb0ef41Sopenharmony_ci safer.kStringMaxLength = process.binding('buffer').kStringMaxLength 621cb0ef41Sopenharmony_ci } catch (e) { 631cb0ef41Sopenharmony_ci // we can't determine kStringMaxLength in environments where process.binding 641cb0ef41Sopenharmony_ci // is unsupported, so let's not set it 651cb0ef41Sopenharmony_ci } 661cb0ef41Sopenharmony_ci} 671cb0ef41Sopenharmony_ci 681cb0ef41Sopenharmony_ciif (!safer.constants) { 691cb0ef41Sopenharmony_ci safer.constants = { 701cb0ef41Sopenharmony_ci MAX_LENGTH: safer.kMaxLength 711cb0ef41Sopenharmony_ci } 721cb0ef41Sopenharmony_ci if (safer.kStringMaxLength) { 731cb0ef41Sopenharmony_ci safer.constants.MAX_STRING_LENGTH = safer.kStringMaxLength 741cb0ef41Sopenharmony_ci } 751cb0ef41Sopenharmony_ci} 761cb0ef41Sopenharmony_ci 771cb0ef41Sopenharmony_cimodule.exports = safer 78