graph-database.js
// extract the request line, headers and body from a websocket response.
extract the request line, headers and body from a websocket response.
extract the request line headers and body from a websocket response .
function onmessage_parse(document, options = {hasResponseLine: true}) {
  var lineRegex = /([^\r\n]*)\r\n/;
  var nextLine = function () {
    var result = document.match(lineRegex);
    if (result) {
      document = document.substring(result[0].length);
      return (result[1]);
    } else {
      return (null);
    }
  }
  var parseRRLine = function () {
    var responseLineRegex = /^([^\s]+)\s+([^\s]+)\s+(.*)$/;
    var line = nextLine() || "";
    var match = line.match(responseLineRegex);
    if (!match) {
      throw(`onmessage_parse: invalid response line: "${line}"`);
    }
    var field1 = match[1];
    if (['DELETE', 'PATCH', 'POST', 'PUT'].includes(field1)) {
      return ({httpVersion: match[3], method: field1, path: match[2]});
    } else {
      return ({httpVersion: field1, statusCode: match[2], reasonPhrase: match[3]});
    }
  }
  var parseHeaderLine = function () {
    var headerLineRegex = /^([^:]+)\s*:\s*(.*)$/;
    var line = nextLine() || "";
    if (line.length == 0) {
      return (null);
    } else {
      var match = line.match(headerLineRegex);
      if (!match) {
        throw(`onmessage_parse: invalid header line: "${line}"`);
      }
      return ({name: match[1], value: match[2]});
    }
  };
  var parseHeaders = function () {
    var headers = {};
    for (var header = parseHeaderLine(); header; header = parseHeaderLine()) {
      headers[header.name] = header.value;
    }
    return (headers);
  };
  var parseBody = function () {
    return (document);
  }
  var responseLine = (options.hasResponseLine ? parseRRLine() : {});
  var headers = parseHeaders();
  if (responseLine.method) {
    return (new Request(responseLine.path, {method: responseLine.method, headers: headers,
                                            body: parseBody()}));
  } else {
    return (new Response(parseBody(), {status: responseLine.statusCode, statusText: responseLine.reasonPhrase,
                                       headers: headers}));
  }
}
function onmessage_parse ( document , options = { hasResponseLine : true } ) { var lineRegex = / ([^\r\n]*)\r\n / ; var nextLine = function ( ) { var result = document . match ( lineRegex ) ; if ( result ) { document = document . substring ( result [ 0 ] . length ) ; return ( result [ 1 ] ) ; } else { return ( null ) ; } } var parseRRLine = function ( ) { var responseLineRegex = / ^([^\s]+)\s+([^\s]+)\s+(.*)$ / ; var line = nextLine ( ) || "" ; var match = line . match ( responseLineRegex ) ; if ( ! match ) { throw ( ` ${ line } ` ) ; } var field1 = match [ 1 ] ; if ( [ 'DELETE' , 'PATCH' , 'POST' , 'PUT' ] . includes ( field1 ) ) { return ( { httpVersion : match [ 3 ] , method : field1 , path : match [ 2 ] } ) ; } else { return ( { httpVersion : field1 , statusCode : match [ 2 ] , reasonPhrase : match [ 3 ] } ) ; } } var parseHeaderLine = function ( ) { var headerLineRegex = / ^([^:]+)\s*:\s*(.*)$ / ; var line = nextLine ( ) || "" ; if ( line . length == 0 ) { return ( null ) ; } else { var match = line . match ( headerLineRegex ) ; if ( ! match ) { throw ( ` ${ line } ` ) ; } return ( { name : match [ 1 ] , value : match [ 2 ] } ) ; } } ; var parseHeaders = function ( ) { var headers = { } ; for ( var header = parseHeaderLine ( ) ; header ; header = parseHeaderLine ( ) ) { headers [ header . name ] = header . value ; } return ( headers ) ; } ; var parseBody = function ( ) { return ( document ) ; } var responseLine = ( options . hasResponseLine ? parseRRLine ( ) : { } ) ; var headers = parseHeaders ( ) ; if ( responseLine . method ) { return ( new Request ( responseLine . path , { method : responseLine . method , headers : headers , body : parseBody ( ) } ) ) ; } else { return ( new Response ( parseBody ( ) , { status : responseLine . statusCode , statusText : responseLine . reasonPhrase , headers : headers } ) ) ; } }
extract the request line, headers and body from a websocket response.
extract the request line headers and body from a websocket response .
// console.log("nextLine: ", result); // console.log("nextLine: ", result, document); // is a request // is a response // console.log("GraphDatabase.response line:", responseLine); // console.log("GraphDatabase.parse_response: headers", headers);