本文整理汇总了TypeScript中bser.dumpToBuffer函数的典型用法代码示例。如果您正苦于以下问题:TypeScript dumpToBuffer函数的具体用法?TypeScript dumpToBuffer怎么用?TypeScript dumpToBuffer使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了dumpToBuffer函数的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: sendSubscription
function sendSubscription(uid: string, root: string, files: FileChangeItem[]): void {
client.write(bser.dumpToBuffer({
subscription: uid,
root,
files
}))
}
示例2:
import * as bser from "bser";
import net from "net";
const obj = "sample data";
const bunser = new bser.BunserBuf();
bunser.on("value", obj => {
console.log("data from socket", obj);
});
const socket = net.connect("/socket");
socket.on("data", buf => {
bunser.append(buf);
});
socket.write(bser.dumpToBuffer(obj));
示例3: sendResponse
function sendResponse(data: any): void {
client.write(bser.dumpToBuffer(data))
}
示例4:
import * as bser from "bser";
const encoded = bser.dumpToBuffer(["hello"]);
console.log(bser.loadFromBuffer(encoded)); // ['hello']
示例5: roundtrip
function roundtrip(val: any) {
const encoded = bser.dumpToBuffer(val);
const decoded = bser.loadFromBuffer(encoded);
assert.deepStrictEqual(decoded, val);
}
示例6:
const acc = new bser.Accumulator(8);
acc.append("hello");
assert.equal(acc.readAvail(), 5);
assert.equal(acc.readOffset, 0);
assert.equal(acc.readString(3), "hel");
assert.equal(acc.readOffset, 3);
assert.equal(acc.readAvail(), 2);
assert.equal(acc.writeAvail(), 3);
// This should trigger a shunt and not make the buffer bigger
acc.reserve(5);
assert.equal(acc.readOffset, 0, "shunted");
assert.equal(acc.readAvail(), 2, "still have 2 available to read");
assert.equal(acc.writeAvail(), 6, "2 left to read out of 8 total space");
assert.equal(acc.peekString(2), "lo", "have the correct remainder");
// Don't include keys that have undefined values
const res = bser.dumpToBuffer({ expression: undefined });
assert.deepStrictEqual(bser.loadFromBuffer(res), {});
// Dump numbers without fraction to integers
let buffer;
buffer = bser.dumpToBuffer(1);
assert.equal(buffer.toString("hex"), "000105020000000301");
buffer = bser.dumpToBuffer(1.0);
assert.equal(buffer.toString("hex"), "000105020000000301");
// Dump numbers with fraction to double
buffer = bser.dumpToBuffer(1.1);
assert.equal(buffer.toString("hex"), "00010509000000079a9999999999f13f");