nl_tokens: create a buffer representing a segment . Different typeKey has different data type this is the detail : 0 - 20 1 - byte unsigned integer 21 - 40 2 - byte unsigned integer ( BE ) 41 - 90 4 - byte unsigned integer ( BE ) 91 - 155 string 156 - 165 uuid 166 - 180 byte string > 180 remained
pl_tokens: function createSegment ( typeKey , value ) { // 1-byte unsigned integer if ( typeKey <= 20 ) { return ( new Buffer ( [ typeKey , value ] ) ) ; } // 2-byte unsigned integer if ( typeKey <= 40 ) { let content = new Buffer ( 3 ) ; content . writeUInt8 ( typeKey , 0 ) ; <mask> . writeUInt16BE ( value , 1 ) ; return ( content ) ; } // 4-byte unsigned integer else if ( typeKey <= 90 ) { let content = new Buffer ( 5 ) ; content . writeUInt8 ( typeKey , 0 ) ; content . writeUInt32BE ( value , 1 ) ; return ( content ) ; } // string else if ( typeKey <= 155 ) { let content = Buffer . from ( value ) ; let header = new Buffer ( [ typeKey , content . length ] ) ; return ( Buffer . concat ( [ header , content ] ) ) ; } // uuid else if ( typeKey <= 165 ) { return ( Buffer . concat ( [ new Buffer ( [ typeKey ] ) , value ] ) ) ; } // byte string else if ( typeKey <= 180 ) { let content = value ; let header = new Buffer ( [ typeKey , content . length ] ) ; return ( Buffer . concat ( [ header , content ] ) ) ; } else { throw new Error ( "typeKey not supported" ) ; } }