本文整理汇总了TypeScript中buffer.Buffer类的典型用法代码示例。如果您正苦于以下问题:TypeScript Buffer类的具体用法?TypeScript Buffer怎么用?TypeScript Buffer使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Buffer类的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: 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);
}
示例4: decode
export function decode(data: Buffer, encoding?: string): any {
let value: string;
let rest: Buffer
({ value, rest } = _decode(data, encoding));
if (rest.length) {
throw new Error(`Unexpected continuation: "${rest.toString()}"`)
}
return value;
}
示例5: bufferTests
function bufferTests() {
var utf8Buffer = new Buffer('test');
var base64Buffer = new Buffer('','base64');
var octets: Uint8Array = null;
var octetBuffer = new Buffer(octets);
var sharedBuffer = new Buffer(octets.buffer);
var copiedBuffer = new Buffer(utf8Buffer);
console.log(Buffer.isBuffer(octetBuffer));
console.log(Buffer.isEncoding('utf8'));
console.log(Buffer.byteLength('xyz123'));
console.log(Buffer.byteLength('xyz123', 'ascii'));
var result1 = Buffer.concat([utf8Buffer, base64Buffer]);
var result2 = Buffer.concat([utf8Buffer, base64Buffer], 9999999);
// Test that TS 1.6 works with the 'as Buffer' annotation
// on isBuffer.
var a: Buffer | number;
a = new Buffer(10);
if (Buffer.isBuffer(a)) {
a.writeUInt8(3, 4);
}
// write* methods return offsets.
var b = new Buffer(16);
var result: number = b.writeUInt32LE(0, 0);
result = b.writeUInt16LE(0, 4);
result = b.writeUInt8(0, 6);
result = b.writeInt8(0, 7);
result = b.writeDoubleLE(0, 8);
// fill returns the input buffer.
b.fill('a').fill('b');
{
let buffer = new Buffer('123');
let index: number;
index = buffer.indexOf("23");
index = buffer.indexOf("23", 1);
index = buffer.indexOf(23);
index = buffer.indexOf(buffer);
}
// Imported Buffer from buffer module works properly
{
let b = new ImportedBuffer('123');
b.writeUInt8(0, 6);
let sb = new ImportedSlowBuffer(43);
b.writeUInt8(0, 6);
}
// Buffer has Uint8Array's buffer field (an ArrayBuffer).
{
let buffer = new Buffer('123');
let octets = new Uint8Array(buffer.buffer);
}
}
示例6: function
const streamWrite: NodeJS.WritableStream['write'] = function (chunk: Buffer | string, encoding?: any, callback?: Function) {
if (Buffer.isBuffer(chunk)) {
chunk = chunk.toString(encoding)
}
process.log(chunk)
if (callback) {
callback()
}
return true
}
示例7: 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);
}
示例8: FakeTransaction
t.test('should validate', st => {
const tx = new FakeTransaction(txData)
const txWithWrongSignature = new FakeTransaction({
...txData,
r: Buffer.from('abcd1558260ac737ea6d800906c6d085a801e5e0f0952bf93978d6fa468fbdff', 'hex'),
})
const txWithLowLimit = new FakeTransaction({
...txData,
gasLimit: '0x1',
})
st.plan(6)
st.true(tx.validate(), 'tx should be valid')
st.false(txWithWrongSignature.validate(), 'tx should be invalid')
st.false(txWithLowLimit.validate(), 'tx should be invalid')
st.equal(tx.validate(true), '', 'tx should return no errors')
st.equal(
txWithWrongSignature.validate(true),
'Invalid Signature',
'tx should return correct error',
)
st.equal(
txWithLowLimit.validate(true),
'gas limit is too low. Need at least 21464',
'tx should return correct error',
)
})
示例9: function
t.test('Verify EIP155 Signature before and after signing with private key', function(st) {
// Inputs and expected results for this test are taken directly from the example in https://github.com/ethereum/EIPs/blob/master/EIPS/eip-155.md
const txRaw = [
'0x09',
'0x4a817c800',
'0x5208',
'0x3535353535353535353535353535353535353535',
'0x0de0b6b3a7640000',
'0x',
]
const privateKey = Buffer.from(
'4646464646464646464646464646464646464646464646464646464646464646',
'hex',
)
const pt = new Transaction(txRaw, { chain: 1 })
st.equal(
pt.serialize().toString('hex'),
'ec098504a817c800825208943535353535353535353535353535353535353535880de0b6b3a764000080018080',
)
pt.sign(privateKey)
st.equal(
pt.hash(false).toString('hex'),
'daf5a779ae972f972197303d7b574746c7ef83eadac0f2791ad23db92e4c8e53',
)
st.equal(
pt.serialize().toString('hex'),
'f86c098504a817c800825208943535353535353535353535353535353535353535880de0b6b3a76400008025a028ef61340bd939bc2195fe537567866003e1a15d3c71ff63e1590620aa636276a067cbe9d8997f761aecb703304b3800ccf555c9f3dc64214b297fb1966a3b6d83',
)
st.end()
})
示例10: it
it('test receive right data', () => {
const buffer = Buffer.from([
0xda,
0xbb,
0x02,
0x14,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x02,
0x00,
0x00,
0x00,
0x06,
0x91,
0x04,
0x70,
0x61,
0x6e,
0x67,
]);
const dBuff = DecodeBuffer.from(1).subscribe(data => {
const {requestId, res, err} = decode(data);
expect(requestId).toEqual(2);
expect(res).toEqual('pang');
expect(err).toEqual(null);
});
dBuff.receive(buffer);
});