Skip to content

SocketFetch class

Paweł Psztyć edited this page Feb 19, 2016 · 3 revisions

SocketFetch

Contents

Description

A SocketFetch class is similar to fetch API but it uses chrome.socket as a transport.

The class is making use of https://github.com/medialize/URI.js.

Usage example

let request = SocketFetch('http://domain.com').fetch().then((response) => {
   if (response.ok) {
     return response.json();
   }
});

Relevant properties

readyState

A boolean property state represents the socket read status. It can be either:

  • STATUS (0) - expecting the message is contain a status line
  • HEADERS (1) - expecting the message is containing headers part of the message (headers are optional)
  • BODY (2) - expecting to read a message body
  • DONE (3) - message has been fully read. This status can be set by readSocketError function when server closes the connection.

status

A integer representing status code of the response.

statusMessage

An optional string representing response status message

headers

A read headers string. It may be incomplete if readyState equals HEADERS or STATUS.

body

A read response body. It may be incomplete if readyState does not equals DONE.

contentType

As a shortcut for finding content-type header in a headers list. It can be either a getter function that is looking for a Content-Type header or a value set after headers are parsed.

contentLength

As a shortcut for finding Content-Length header in a headers list. It can be either a getter function that is looking for a Content-Length header or a value set after headers are parsed.

chunked

A flag determining that the response is chunked Transfer-Encoding. When Transfer-Encoding header is set to "chunked" then the response will be split in chunks. Every chunk starts with hex number of length in chunk followed by new line character (\r\n or CR or 13|10). Because message received by the socket most probably will have different buffer size, the readSocketData() function may contain more than one part of chunk or incomplete part of chunk.

chunkSize

Set only if chunked is true and chunk size has been read. It is a number of bytes left to read until end of the chunk. While parser is consuming next bytes this number will decrease.

onResponseRead() function

A function that should be called when the message has been entirely read and can produce the result to the user.

Processing socket message

When message from the socket is received the implementation runs following steps

  1. If readyState equals DONE exit.
  2. If readyState equals STATUS consume first line (until new line character) and then:
    • parse read data with status parse function
    • set status to parsed status code
    • if parsed status message is not empty
    • set readyState to HEADERS
    • Continue to point 3
  3. If readyState equals HEADERS follow this steps:
    • set temporary array data
    • read data and copy byte by byte to the data array until:
      • CRCR detected (or 13|10|13|10 sequence in Uint8Array) then follow this steps
        • convert data to string
        • fill headers with converted data
        • use headers parse function to parse headers data
        • if Content-Type header is available set contentType to the value
        • if Content-Length header is available set contentLength to the value
        • if Transfer-Encoding header is available and equals chunked set chunked to true
        • set readyState to BODY
        • consume CRCR characters and continue to step 4
      • End of stream detected
        • convert data to string
        • concatenate headers with converted data
        • exit
  4. If readyState equals BODY
    • if chunked is true and chunkSize is undefined or equal 0
      • read line and parse contents as a hex number
      • set result to chunkSize
      • if chunkSize equals 0 call onResponseRead() function, set readyState to DONE and exit.
      • otherwise continue to the next point
    • if chunked is true and chunkSize is grater than 0 follow this steps:
      • if has no more data to read concatenate read temp data with body array and exit
      • otherwise complete this steps
        • Read next byte and copy it to the temp array
        • Decrement chunkSize by one
        • repeat steps from point 4
    • if chunked is false copy remaining data to the body array and:
      • if last characters was CRCR (two new line characters) call onResponseRead() function and set readyState to DONE
      • exit
  5. Otherwise throw InvalidState exception and exit.

There is a flowchart for reading data from socket: Reading data from socket flowchart