|
14 | 14 | * a Uint8Array in any of our APIs that take a Ti.Buffer and eventually deprecating/removing Ti.Buffer.
|
15 | 15 | */
|
16 | 16 |
|
17 |
| -import{isInsideNodeModules}from'./internal/util'; |
| 17 | +import{ |
| 18 | +customInspectSymbol, |
| 19 | +getOwnNonIndexProperties, |
| 20 | +isBuffer, |
| 21 | +isInsideNodeModules, |
| 22 | +propertyFilter |
| 23 | +}from'./internal/util'; |
| 24 | + |
| 25 | +import{inspectasutilInspect}from'./internal/util/inspect'; |
| 26 | + |
| 27 | +const{ALL_PROPERTIES,ONLY_ENUMERABLE}=propertyFilter; |
18 | 28 |
|
19 | 29 | // https://nodejs.org/api/buffer.html#buffer_buffers_and_character_encodings
|
20 | 30 | constTI_CODEC_MAP=newMap();
|
@@ -51,6 +61,8 @@ const uint8DoubleArray = new Uint8Array(doubleArray.buffer);
|
51 | 61 | constfloatArray=newFloat32Array(1);
|
52 | 62 | constuint8FloatArray=newUint8Array(floatArray.buffer);
|
53 | 63 |
|
| 64 | +letINSPECT_MAX_BYTES=50; |
| 65 | + |
54 | 66 | classBuffer{
|
55 | 67 | /**
|
56 | 68 | * Constructs a new buffer.
|
@@ -80,17 +92,23 @@ class Buffer {
|
80 | 92 |
|
81 | 93 | consttiBuffer=arg;
|
82 | 94 | letstart=encodingOrOffset;
|
83 |
| -this._tiBuffer=tiBuffer; |
84 | 95 | if(start===undefined){
|
85 | 96 | start=0;
|
86 | 97 | }
|
87 |
| -this.byteOffset=start; |
88 | 98 | if(length===undefined){
|
89 |
| -this.length=tiBuffer.length-this.byteOffset; |
90 |
| -}else{ |
91 |
| -this.length=length; |
| 99 | +length=tiBuffer.length-start; |
92 | 100 | }
|
93 |
| -this._isBuffer=true; |
| 101 | +Object.defineProperties(this,{ |
| 102 | +byteOffset: { |
| 103 | +value: start |
| 104 | +}, |
| 105 | +length: { |
| 106 | +value: length |
| 107 | +}, |
| 108 | +_tiBuffer: { |
| 109 | +value: tiBuffer |
| 110 | +} |
| 111 | +}); |
94 | 112 | // FIXME: Support .buffer property that holds an ArrayBuffer!
|
95 | 113 | }
|
96 | 114 |
|
@@ -1405,10 +1423,46 @@ class Buffer {
|
1405 | 1423 | * @returns {boolean}
|
1406 | 1424 | */
|
1407 | 1425 | staticisBuffer(obj){
|
1408 |
| -returnobj!==null&&obj!==undefined&&obj._isBuffer===true; |
| 1426 | +returnobj!==null&&obj!==undefined&&obj[isBuffer]===true; |
| 1427 | +} |
| 1428 | + |
| 1429 | +// Override how buffers are presented by util.inspect(). |
| 1430 | +[customInspectSymbol](recurseTimes,ctx){ |
| 1431 | +constmax=INSPECT_MAX_BYTES; |
| 1432 | +constactualMax=Math.min(max,this.length); |
| 1433 | +constremaining=this.length-max; |
| 1434 | +letstr=this.slice(0,actualMax).toString('hex').replace(/(.{2})/g,'$1 ').trim(); |
| 1435 | +if(remaining>0){ |
| 1436 | +str+=` ... ${remaining} more byte${remaining>1 ? 's' : ''}`; |
| 1437 | +} |
| 1438 | +// Inspect special properties as well, if possible. |
| 1439 | +if(ctx){ |
| 1440 | +letextras=false; |
| 1441 | +constfilter=ctx.showHidden ? ALL_PROPERTIES : ONLY_ENUMERABLE; |
| 1442 | +constobj=getOwnNonIndexProperties(this,filter).reduce((obj,key)=>{ |
| 1443 | +extras=true; |
| 1444 | +obj[key]=this[key]; |
| 1445 | +returnobj; |
| 1446 | +},Object.create(null)); |
| 1447 | +if(extras){ |
| 1448 | +if(this.length!==0){ |
| 1449 | +str+=', '; |
| 1450 | +} |
| 1451 | +// '[Object: null prototype] {'.length === 26 |
| 1452 | +// This is guarded with a test. |
| 1453 | +str+=utilInspect(obj,{ |
| 1454 | + ...ctx, |
| 1455 | +breakLength: Infinity, |
| 1456 | +compact: true |
| 1457 | +}).slice(27,-2); |
| 1458 | +} |
| 1459 | +} |
| 1460 | +return`<${this.constructor.name}${str}>`; |
1409 | 1461 | }
|
1410 | 1462 | }
|
1411 | 1463 |
|
| 1464 | +Buffer.prototype.inspect=Buffer.prototype[customInspectSymbol]; |
| 1465 | + |
1412 | 1466 | Buffer.poolSize=8192;
|
1413 | 1467 |
|
1414 | 1468 | exportdefault{
|
@@ -1543,6 +1597,8 @@ const arrayIndexHandler = {
|
1543 | 1597 | if(Number.isSafeInteger(num)){
|
1544 | 1598 | returngetAdjustedIndex(target,num);
|
1545 | 1599 | }
|
| 1600 | +}elseif(propKey===isBuffer){ |
| 1601 | +returntrue; |
1546 | 1602 | }
|
1547 | 1603 | returnReflect.get(target,propKey,receiver);
|
1548 | 1604 | },
|
@@ -1589,7 +1645,7 @@ function setAdjustedIndex(buf, index, value) {
|
1589 | 1645 | * @returns {Buffer} wrapped inside a Proxy
|
1590 | 1646 | */
|
1591 | 1647 | functionnewBuffer(...args){
|
1592 |
| -returnnewProxy(newBuffer(...args),arrayIndexHandler);// eslint-disable-line security/detect-new-buffer |
| 1648 | +returnnewProxy(newBuffer(...args),arrayIndexHandler);// eslint-disable-line security/detect-new-buffer |
1593 | 1649 | }
|
1594 | 1650 |
|
1595 | 1651 | /**
|
|
0 commit comments