本文整理汇总了TypeScript中msgpack-lite.decode函数的典型用法代码示例。如果您正苦于以下问题:TypeScript decode函数的具体用法?TypeScript decode怎么用?TypeScript decode使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了decode函数的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: decodingBytesArray
// https://github.com/kawanet/msgpack-lite#decoding-messagepack-bytes-array
function decodingBytesArray() {
// decode() accepts Buffer instance per default
msgpack.decode(new Buffer([0x81, 0xA3, 0x66, 0x6F, 0x6F, 0xA3, 0x62, 0x61, 0x72]));
// decode() also accepts Array instance
msgpack.decode([0x81, 0xA3, 0x66, 0x6F, 0x6F, 0xA3, 0x62, 0x61, 0x72]);
// decode() accepts raw Uint8Array instance as well
msgpack.decode(new Uint8Array([0x81, 0xA3, 0x66, 0x6F, 0x6F, 0xA3, 0x62, 0x61, 0x72]));
}
示例2:
const decode = (bytes: Buffer | null): [Uint8Array, any] => {
if (bytes == null) return [V_ZERO, null]
else {
const [vBuf, data] = msgpack.decode(bytes)
return [new Uint8Array(vBuf), data]
}
}
示例3: encodingAndDecoding
// https://github.com/kawanet/msgpack-lite#encoding-and-decoding-messagepack
function encodingAndDecoding() {
// encode from JS Object to MessagePack (Buffer)
const buffer = msgpack.encode({foo: "bar"});
// decode from MessagePack (Buffer) to JS Object
const data = msgpack.decode(buffer); // => {"foo": "bar"}
}
示例4: catch
client.on('message', (message) => {
// try to decode message received from client
try {
message = msgpack.decode(Buffer.from(message));
} catch (e) {
console.error("Couldn't decode message:", message, e.stack);
return;
}
if (message[0] !== Protocol.JOIN_ROOM) {
console.error("MatchMaking couldn't process message:", message);
return;
}
let roomName = message[1];
let joinOptions = message[2];
// has room handler avaialble?
memshared.lindex("handlers", roomName, (err, index) => {
if (index === null) {
send(client, [Protocol.JOIN_ERROR, roomName, `Error: no available handler for "${ roomName }"`]);
return;
}
// Request to join an existing sessions for requested handler
memshared.smembers(roomName, (err, availableWorkerIds) => {
//
// TODO:
// remove a room from match-making cache when it reaches maxClients.
//
joinOptions.clientId = client.id;
if (availableWorkerIds.length > 0) {
broadcastJoinRoomRequest(availableWorkerIds, client, roomName, joinOptions);
} else {
// retrieve active worker ids
requestCreateRoom(client, roomName, joinOptions);
}
});
});
});
示例5: customExtensionTypes
// https://github.com/kawanet/msgpack-lite#custom-extension-types-codecs
function customExtensionTypes() {
class MyVector {
constructor(public x: number, public y: number) {}
}
const codec = msgpack.createCodec();
codec.addExtPacker(0x3F, MyVector, myVectorPacker);
codec.addExtUnpacker(0x3F, myVectorUnpacker);
const data = new MyVector(1, 2);
const encoded = msgpack.encode(data, {codec});
const decoded = msgpack.decode(encoded, {codec});
function myVectorPacker(vector: MyVector) {
const array = [vector.x, vector.y];
return msgpack.encode(array); // return Buffer serialized
}
function myVectorUnpacker(buffer: Buffer | Uint8Array): MyVector {
const array = msgpack.decode(buffer);
return new MyVector(array[0], array[1]); // return Object deserialized
}
}
示例6: Error
const lmdbStore = <Val>(inner: I.Store<Val>, location: string): Promise<I.Store<Val>> => {
const env = new lmdb.Env()
// console.log('inner', inner)
if (inner.storeInfo.sources.length !== 1) {
// It would be trivial though.
throw new Error('LMDB store with multiple sources not implemented')
}
const source: I.Source = inner.storeInfo.sources[0]
// Check that the directory exists.
try { fs.mkdirSync(location) }
catch(e) { if (e.code !== 'EEXIST') throw e }
env.open({path: location, maxDbs: 2, noTls: true})
const dbi = env.openDbi({name: null, create: true})
// const configdb = env.openDbi({name: 'config', create: true})
// Note: I'm using 'native' Prozess version numbers, so the local store
// starts at version 0 and event 1 moves us to version 1.
let version: I.Version = new Uint8Array()
const setVersion = (txn: lmdb.Txn, v: I.Version) => {
version = v
txn.putBinary(dbi, VERSION_KEY, Buffer.from(version))
}
// Ok, first do catchup.
{
const txn = env.beginTxn()
const configBytes = txn.getBinary(dbi, CONFIG_KEY)
if (configBytes == null) {
// console.log('Database was created - no config!')
txn.putBinary(dbi, CONFIG_KEY, msgpack.encode({sc_ver: 1, source}))
setVersion(txn, new Uint8Array(8))
} else {
const {sc_ver, source:dbSource} = msgpack.decode(configBytes)
assert(sc_ver === 1, 'LDMB database was set up using invalid or old statecraft version.')
assert(dbSource === source, `LDMB database at ${location} is invalid. Delete and restart`)
version = new Uint8Array(txn.getBinary(dbi, VERSION_KEY))
}
txn.commit()
}
debug('Opened database at version', version)
const ready = resolvable()
// const ready = inner.start!([version])
// TODO: Generate these based on the opstore.
const capabilities = {
queryTypes: bitSet(I.QueryType.AllKV, I.QueryType.KV, I.QueryType.StaticRange, I.QueryType.Range),
mutationTypes: bitSet(I.ResultType.KV),
}
const decode = (bytes: Buffer | null): [Uint8Array, any] => {
if (bytes == null) return [V_ZERO, null]
else {
const [vBuf, data] = msgpack.decode(bytes)
return [new Uint8Array(vBuf), data]
}
}
const rawGet = (txn: lmdb.Txn, k: I.Key) => decode(txn.getBinaryUnsafe(dbi, k))
// TODO: Probably cleaner to write this as iterators? This is simpler / more
// understandable though.
const getKVResults = (dbTxn: lmdb.Txn, query: Iterable<I.Key>, opts: I.FetchOpts, resultsOut: Map<I.Key, Val>) => {
let maxVersion = V_ZERO
for (let k of query) {
const [lastMod, doc] = rawGet(dbTxn, k)
if (doc != null) resultsOut.set(k, opts.noDocs ? 1 : doc)
// Note we update maxVersion even if the document is null.
maxVersion = versionLib.vMax(maxVersion, lastMod)
}
return maxVersion
}
const getAllResults = (dbTxn: lmdb.Txn, opts: I.FetchOpts, resultsOut: Map<I.Key, Val>) => {
let maxVersion = V_ZERO
const cursor = new lmdb.Cursor(dbTxn, dbi)
let k = cursor.goToRange('\x02') // positioned right after config key
while (k != null) {
const bytes = cursor.getCurrentBinaryUnsafe()
const [lastMod, doc] = decode(bytes)
if (doc != null) resultsOut.set(k as string, opts.noDocs ? 1 : doc)
maxVersion = versionLib.vMax(maxVersion, lastMod)
k = cursor.goToNext()
}
cursor.close()
return maxVersion
}
const setCursor = (cursor: lmdb.Cursor, sel: I.StaticKeySelector) => {
let {k, isAfter} = sel
//.........这里部分代码省略.........
示例7: myVectorUnpacker
function myVectorUnpacker(buffer: Buffer | Uint8Array): MyVector {
const array = msgpack.decode(buffer);
return new MyVector(array[0], array[1]); // return Object deserialized
}
示例8:
const unpackVersion = (data: Buffer): [Buffer, any] => [
data.slice(0, 10),
data.length > 10 ? msgpack.decode(data.slice(10)) : undefined
]