Skip to content
Related Articles

Related Articles

Node.js v8.Deserializer.readHeader() Method

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 22 Jul, 2020

The v8.Deserializer.readHeader() method is an inbuilt application programming interface of the v8.Deserializer module which is used to read the header and validate it, to ensure that contains a valid serialization format version.

Syntax:

v8.Deserializer.readHeader();

Parameters: This method does not accept any parameters.

Return Value: This method reads the header buffer at deserializer and validates it. It returns true on the valid header, otherwise throws an error.

Below examples illustrate the use of v8.Deserializer.readHeader() method in Node.js:

Example 1: Filename: index.js




// Accessing v8 module
const v8 = require('v8');
const serializer = new v8.Serializer();
  
// Calling v8.Deserializer.readHeader() 
console.log(serializer.releaseBuffer());
serializer.writeHeader();
  
const deserializer = new v8.Deserializer(
            serializer.releaseBuffer());
console.log(deserializer.readHeader());
  
console.log(serializer.releaseBuffer());

Run index.js file using the following command:

node index.js

Output:

<Buffer >
true
<Buffer >

Example 2: Filename: index.js




// Accessing v8 module
const v8 = require('v8');
const serializer = new v8.Serializer();
  
// Calling v8.serializer.writeHeader() 
serializer.writeHeader();
  
// Calling v8.deserializer.readHeader() 
const deserializer = new v8.Deserializer(
        serializer.releaseBuffer());
          
if (deserializer.readHeader()) {
    console.log("It is a valid header!");
} else {
    console.log("It is not a valid header!");
}

Run index.js file using the following command:

node index.js

Output:

It is a valid header!

Reference: https://nodejs.org/api/v8.html#v8_deserializer_readheader


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!