當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。