本文整理汇总了TypeScript中msgpack-lite.encode函数的典型用法代码示例。如果您正苦于以下问题:TypeScript encode函数的具体用法?TypeScript encode怎么用?TypeScript encode使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了encode函数的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: if
}> (elementType: Form.Element.Types) => (o?: T) => {
const element: Form.IElement = {
fileName: o && o.fileName,
id: o && o.id,
label: o && o.label,
mask: o && o.mask && msgpack.encode(o.mask),
max: o && o.max,
mediaType: o && o.mediaType,
min: o && o.min,
noGrow: o && o.noGrow === true,
options: o && o.options,
required: o && o.required,
step: o && o.step,
type: elementType,
width: o && o.width
};
if (o && typeof o.value === 'boolean') {
element.valueBoolean = o.value;
}
else if (o && o.value instanceof Uint8Array) {
element.valueBytes = o.value;
}
else if (o && typeof o.value === 'number') {
element.valueNumber = o.value;
}
else if (o && typeof o.value === 'string') {
element.valueString = o.value;
}
return element;
};
示例2: 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"}
}
示例3: await
;(async () => {
for await (const cu of inner.subscribe({type: I.QueryType.AllKV, q: true}, {fromVersion: [version]})) {
if (cu.replace) throw new Error('LMDB stores inner store replacing data is not implemented')
// console.log('lmdb store got update', cu)
let evtTxn = env.beginTxn()
let nextVersion = new Uint8Array()
for (let i = 0; i < cu.txns.length; i++) {
const {txn, meta, versions} = cu.txns[i]
const toV = versions[0]
if (toV == null) throw new Error('Invalid catchup data - null version in txn')
const txn_ = txn as I.KVTxn<Val>
for (const [k, op] of txn_) {
// const oldData = fieldOps.create(rawGet(dbTxn, k)[0], op)
const oldData = rawGet(evtTxn, k)[1]
const newData = fieldOps.apply(oldData, op)
// console.log('updated key', k, 'from', oldData, 'to', newData)
// I'm leaving an empty entry in the lmdb database even if newData is
// null so fetch will correctly report last modified versions.
// This can be stripped with a periodically updating baseVersion if
// thats useful.
evtTxn.putBinary(dbi, k, msgpack.encode([Buffer.from(toV), newData]))
}
// nextVersion = toV
}
nextVersion = cu.toVersion[0]!
// TODO: Consider setting subgroup minversion here.
subGroup.onOp(0, version, cu.txns)
// console.log('setversion', nextVersion)
setVersion(evtTxn, nextVersion)
evtTxn.commit()
if (cu.caughtUp) ready.resolve()
}
})()
示例4: 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
}
}
示例5: 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
//.........这里部分代码省略.........
示例6: myVectorPacker
function myVectorPacker(vector: MyVector) {
const array = [vector.x, vector.y];
return msgpack.encode(array); // return Buffer serialized
}
示例7:
export const encodeTxn = (txn: I.KVTxn<any>, meta: I.Metadata) => msgpack.encode([Array.from(txn), meta])
示例8: pack
pack(data: serialize.TUnpacked): serialize.TPacked {
var msgpack = require('msgpack-lite');
return msgpack.encode(data);
}
示例9:
export const serializeBinary = <T>(object: T): Buffer =>
msgpack.encode(object);
示例10:
/// <reference path="msgpack-lite.d.ts" />
import * as msgpack from "msgpack-lite";
var encoded = msgpack.encode("");
msgpack.decode(encoded);