本文整理汇总了TypeScript中buffer.Buffer.concat方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Buffer.concat方法的具体用法?TypeScript Buffer.concat怎么用?TypeScript Buffer.concat使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类buffer.Buffer
的用法示例。
在下文中一共展示了Buffer.concat方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: encodeString
function encodeString(s: string): Buffer {
let bytes = Buffer.from(s);
return Buffer.concat([
Buffer.from(bytes.length.toString()),
Buffer.from(':'),
bytes]);
}
示例2: encodeArray
function encodeArray(l: Array<any>): Buffer {
let result: Array<Buffer> = [Buffer.from('l')];
l.forEach(element => {
result.push(_encode(element));
});
result.push(Buffer.from('e'))
return Buffer.concat(result);
}
示例3: hash
/**
* Computes a sha3-256 hash of the serialized tx, using the sender address to generate a fake
* signature.
*
* @param includeSignature - Whether or not to include the signature
*/
hash(includeSignature = true): Buffer {
if (includeSignature && this._from && this._from.toString('hex') !== '') {
// include a fake signature using the from address as a private key
const fakeKey = Buffer.concat([this._from, this._from.slice(0, 12)])
this.sign(fakeKey)
}
return super.hash(includeSignature)
}
示例4: encode
export function encode({ opt, data }) {
const bufList: Buffer[] = [];
let byteLen = 0;
let offset = 0;
data = Array.isArray(data) ? data : [data];
let buf = Buffer.alloc(allocLen);
bufList.push(buf);
const writeUInt = (value, byteLength) => {
byteLen += byteLength;
const less = allocLen - offset;
if (less < byteLength) {
if (less) {
// split buffer
byteLength -= less;
const i = Math.pow(2, byteLength * 8);
const l = (value / i) >>> 0;
value -= l * i;
buf.writeUIntBE(l, offset, less);
}
buf = Buffer.alloc(allocLen);
bufList.push(buf);
offset = 0;
}
buf.writeUIntBE(value, offset, byteLength);
offset += byteLength;
};
writeUInt(opt, OPT_LEN);
// set buffer
data.forEach((item) => {
const packetType = packetTypes[item.type];
writeUInt(item.type, PACKET_TYPE_LEN);
for (const key in packetType) {
if (packetType.hasOwnProperty(key)) {
let value = +item.packet[key] || 0;
const packetItem = packetType[key];
const byteLength = packetItem.byteLen;
if (packetItem.encode) {
value = packetItem.encode(value);
}
writeUInt(value, byteLength);
}
}
});
if (bufList.length > 1) {
buf = Buffer.concat(bufList, bufList.length * allocLen);
}
return buf.buffer.slice(buf.byteOffset, buf.byteOffset + byteLen);
}
示例5: encodeDict
function encodeDict(d: any): Buffer {
let result: Array<Buffer> = [Buffer.from('d')];
let keys = Object.keys(d).sort();
keys.forEach(k => {
result.push(encodeString(k));
result.push(encode(d[k]))
});
result.push(Buffer.from('e'));
return Buffer.concat(result);
}
示例6: resolve
client.on('data', data => {
nreplResp = Buffer.concat([nreplResp, data]);
const { decodedObjects, rest } = bencodeUtil.decodeObjects(nreplResp);
nreplResp = rest;
const validDecodedObjects = decodedObjects.reduce((objs, obj) => {
if (!isLastNreplObject(objs))
objs.push(obj);
return objs;
}, []);
respObjects.push(...validDecodedObjects);
if (isLastNreplObject(respObjects)) {
client.end();
client.removeAllListeners();
resolve(respObjects);
}
});
示例7:
.on('end', () => {
const buffer = Buffer.concat(chunks);
res(buffer.toString('hex'));
});
示例8:
res.on("data", (d) => {
buffer = Buffer.concat([buffer, d]);
});
示例9:
}).on('end', () => {
res.end('You posted me this: ' + Buffer.concat(body).toString());
});