当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Node.js v8.deserializer.readHeader()用法及代码示例


这个v8.deserializer.readHeader()方法用于读取和验证标头。它还检查格式版本。遇到无效或不受支持的有线格式时会引发错误。

用法

v8.deserializer.readHeader()

参数

由于它从内部缓冲区读取标头,因此不需要任何输入参数。反序列化标头后,它将返回 True。

Example 1

创建一个文件名"readHeader.js"并复制以下代码片段。创建文件后,使用命令“node readHeader.js”运行此代码。

// v8.deserializer.readHeader() Demo Example

// Importing the v8 module
const v8 = require('v8');

// Defining the serializer object
const serializer = new v8.Serializer();

// Writing the headers to the internal buffer
serializer.writeHeader();

// Defining the deserializer object
const deserializer = new v8.Deserializer(serializer.releaseBuffer());

// Reading the headers from internal buffer
console.log(deserializer.readHeader());

// Printing the header values from internal buffer
console.log(serializer.releaseBuffer());

输出

C:\home\node>> node readHeader.js
undefined
<Buffer ff 0d 22 1a 57 65 6c 63 6f 6d 65 20 74 6f 20 54 75 74 6f 72 69 61 6c 73 20 50 6f 69 6e 74>

Example 2

让我们再看一个例子

// v8.serializer.readHeader() Demo Example

// Importing the v8 module
const v8 = require('v8');

// Defining the serializer object
const serializer = new v8.Serializer();

// Writing some value using writeUint32
console.log(serializer.writeUint32(7869));

// Writing the headers to the internal buffer
serializer.writeHeader();

// Defining the deserializer object
const deserializer = new
v8.Deserializer(serializer.releaseBuffer());

// Reading the headers from internal buffer
console.log(deserializer.readHeader());

// Printing the header values from internal buffer
console.log(serializer.releaseBuffer());

输出

C:\home\node>> node readHeader.js
undefined
/home/mayankaggarwal/mysql-test/util.js:18
console.log(deserializer.readHeader());
                        ^
Error:Unable to deserialize cloned data due to invalid or
unsupported version.
   at Object. (/home/node/readHeader .js:18:26)
   at Module._compile (internal/modules/cjs/loader.js:778:30)
   at Object.Module._extensions..js
(internal/modules/cjs/loader.js:789:10)
   at Module.load (internal/modules/cjs/loader.js:653:32)
   at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
   at Function.Module._load
(internal/modules/cjs/loader.js:585:3)
   at Function.Module.runMain
(internal/modules/cjs/loader.js:831:12)
   at startup (internal/bootstrap/node.js:283:19)
   at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)

相关用法


注:本文由纯净天空筛选整理自Mayank Agarwal大神的英文原创作品 Node.js – v8.deserializer.readHeader() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。