当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript msgpack-lite.decode函数代码示例

本文整理汇总了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]));
}
开发者ID:AbraaoAlves,项目名称:DefinitelyTyped,代码行数:11,代码来源:msgpack-lite-tests.ts

示例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]
   }
 }
开发者ID:josephg,项目名称:statecraft,代码行数:7,代码来源:lmdb.ts

示例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"}
}
开发者ID:AbraaoAlves,项目名称:DefinitelyTyped,代码行数:8,代码来源:msgpack-lite-tests.ts

示例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);
        }
      });

    });

  });
开发者ID:truonglvx,项目名称:colyseus,代码行数:46,代码来源:Process.ts

示例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
  }
}
开发者ID:AbraaoAlves,项目名称:DefinitelyTyped,代码行数:24,代码来源:msgpack-lite-tests.ts

示例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
//.........这里部分代码省略.........
开发者ID:josephg,项目名称:statecraft,代码行数:101,代码来源:lmdb.ts

示例7: myVectorUnpacker

 function myVectorUnpacker(buffer: Buffer | Uint8Array): MyVector {
   const array = msgpack.decode(buffer);
   return new MyVector(array[0], array[1]); // return Object deserialized
 }
开发者ID:AbraaoAlves,项目名称:DefinitelyTyped,代码行数:4,代码来源:msgpack-lite-tests.ts

示例8:

const unpackVersion = (data: Buffer): [Buffer, any] => [
  data.slice(0, 10),
  data.length > 10 ? msgpack.decode(data.slice(10)) : undefined
]
开发者ID:josephg,项目名称:statecraft,代码行数:4,代码来源:fdb.ts


注:本文中的msgpack-lite.decode函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。