See Overall_Message_Structure.txt for the specification implimented by class NmsPart. That spec describes dividing a network message into parts. NmsPart provides methods for sending and recieving the message parts. Each part is represented by an instance of class NmsPart.

NmsPart

Constructors

  • Creates an instance of NmsPart. To make an empty NmsPart ready to read from the network use new NmsPart(). To make NmsParts ready to write to the network pass the contents as the 1st parameter, and either a numeric byte length or a Buffer encoding (utf8, etc.) for the 2nd parameter. For example new NmsPart([ "data1", "data2" ], "utf8"). If the contents consists solely of node.js Buffer objects then the 2nd parameter can be ommitted. If the byte length is known in advance it can be passed as a number for the 2nd parameter. For example new NmsPart("gobledegook", 11). This saves the time the constructor would otherwise take calculating the byte length. If you change the contents you must simultaneously update the byte length. If the new byte length is not known use NmsPart's instance method setByteLenFromContents() to set the byte length directly from the contents.

    Parameters

    • contents: null | string | (string | Buffer)[] = null

      The contents of the message part.

    • byteLenOrEncoding: number | BufferEncoding = -1

      The byte length of the contents, or a buffer encoding for use in calculating the byte length.

    Returns NmsPart

    NmsPart

Properties

byteLen: number = 0

Contains the byte length of the contents, taking in to account the encoding to be used for strings. It does NOT include the length of the part header sent over the network in front of the contents. Note that this may be different from the contents length in string characters. If you change the contents then you must update the byteLen before sending the part out over the network. If the new byte length is not known use NmsPart's instance method setByteLenFromContents() to set byteLen directly from the contents.

NmsPart

contents: null | string | (string | Buffer)[] = null

Contains the data transmitted over the network. If you change the contents then you must update the byteLen before sending the part out over the network. If the new byte length is not known use NmsPart's instance method setByteLenFromContents() to set byteLen directly from the contents.

NmsPart

Methods

  • Read a part from the network. If chan.setEncoding() has been called to set a string encoding for the request/response then read() won't work correctly. After the read completes the NmsPart's contents property's set to the data read, and the byteLen property's set to its byte length. If the network's already delivered sufficient bytes then the read completes at once and read() returns true. If it must wait for more data from the network read() returns false, and indicates completion by calling doneCallback. Note that doneCallback is only called if read() returns false. doneCallback()'s 1st paraemter (e) is null if everything went OK, otherwise it contains an Error object. The 2nd parameter (msgEnded) is true if the end of the network message was detected. The 3rd parameter (immediateEnd) is true if the message ended right at the start, before reading even the 1st byte of the part header, otherwise it's false. Note that e is NOT null when immediateEnd is true.

    Parameters

    • chan: IncomingMessage

      Where to read from (an HTTP response object on a client, or an HTTP request object on a server).

    • encoding: null | BufferEncoding

      Pass an encoding to recieve the data as a string, or null for raw data as bytes in Buffers.

    • doneCallback: ((e: null | Error, msgEnded: boolean, immediateEnd: boolean) => void)

      The function to be called when the part has been read, if read() returned false.

        • (e, msgEnded, immediateEnd): void
        • Parameters

          • e: null | Error
          • msgEnded: boolean
          • immediateEnd: boolean

          Returns void

    Returns boolean

    Return true if the read's already completed and the data's present, false if you must wait for doneCallback() for completion.

    NmsPart

  • Set the NmsPart's byteLen property according to its contents property.

    Parameters

    • encoding: null | BufferEncoding = null

      The encoding to use when computing the byte length of strings, can be null if all the data is in node.js Buffer objects.

    Returns NmsPart

    The same NmsPart object as it's called on.

    NmsPart

  • Get the contents of the NmsPart object as a block of bytes in a node.js Buffer object.

    Parameters

    • encoding: null | BufferEncoding = null

      The string encoding to use, can be null if all the data is in node.js Buffer objects.

    Returns Buffer

    The contents of the NmsPart object as a block of bytes.

    NmsPart

  • Get the contents of the NmsPart object as a string.

    Parameters

    • encoding: null | BufferEncoding = null

      The encoding to use when converting a node.js Buffer object to a string, can be null if all the data is in strings.

    Returns string

    The contents of the NmsPart object as a string.

    NmsPart

  • Send the part over the network. write() requires that the NmsPart have the correct value for its byteLen property. If necessary the instance method setByteLenFromContents() can be used to set the byteLen directly from the contents. If the part can be sent imediately then write() returns true, and the write has completed when it returns. Otherwise write() returns false and indicates completion by calling doneCallback. Note that doneCallback is only called if write() returned false. If something went wrong then the Error object is passed as a parameter to doneCallback, otherwise, if all went well, then this parameter's null.

    Parameters

    • chan: ClientRequest | ServerResponse<IncomingMessage>

      Where to write to (an HTTP request object on a client, or an HTTP response object on a server).

    • encoding: null | BufferEncoding

      The encoding for writing strings, can be null if all the data is in node.js Buffer objects.

    • sendEnd: boolean

      If true complete the HTTP message after the last data has been written, otherwise leave the HTTP message open for more sending.

    • doneCallback: ((e: null | Error) => void)

      The function to be called when the part has been sent, if write() returned false.

        • (e): void
        • Parameters

          • e: null | Error

          Returns void

    Returns boolean

    True if the write completed before write() returned, and false if you must wait for doneCallback for completion.

    NmsPart

  • Return the total byte length of the part. The total byte length is the value required for the Content-Length HTTP header. It includes the byte length of the part's header, as well as the byte lengths of the part's contents taking in to account the encoding to be used for strings. writeLen() requires that the NmsPart have the correct value for its byteLen property. If necessary the instance method setByteLenFromContents() can be used to set the byteLen directly from the contents.

    Returns number

    The part's total byte length.

    NmsPart

  • Read 1 or more parts from the network, providing the data as an array of NmsPart objects in the order in which they're recieved. If chan.setEncoding() has been called to set a string encoding for the request/response then readMany() won't work correctly. If the OS has already read sufficient data from the network then readMany() returns the read as an array of NmsPart objects. Otherwise it returns null, and indicates completion by calling the doneCallback parameter. Note that doneCallback is only called if readMany() returns null. doneCallback()'s 1st paraemter (e) is null if everything went OK, otherwise it contains an Error object. The 2nd parameter (msgEnded) is true if the end of the network message was detected. The 3rd parameter contains the array of NmsPart objects read from the network.

    Parameters

    • chan: IncomingMessage

      Where to read from (an HTTP response object on a client, or an HTTP request object on a server).

    • partCount: number

      The number of parts to read, or 0 to read parts all the way to the end of the message.

    • encoding: null | BufferEncoding

      Pass an encoding to recieve each part's data as a string, or null for raw data as bytes in Buffers.

    • doneCallback: ((e: null | Error, data: NmsPart[], msgEnded: boolean) => void)

      The function to be called when all the parts have been read, if readMany() returned null.

        • (e, data, msgEnded): void
        • Parameters

          • e: null | Error
          • data: NmsPart[]
          • msgEnded: boolean

          Returns void

    Returns null | NmsPart[]

    If enough data has already been read from the network then return the array of NmsPart, otherwise return null.

    NmsPart

  • Send 1 or more parts over the network. writeMany() requires that all the NmsPart objects have the correct value for their byteLen properties. If necessary NmsPart's instance method setByteLenFromContents() can be used to set a part's byteLen directly from its contents. If all the parts can be sent imediately then writeMany() returns true, and the write has completed when it returns. Otherwise writeMany() returns false and indicates completion by calling doneCallback. Note that doneCallback is only called if writeMany() returned false. If something went wrong then the Error object is passed as a parameter to doneCallback, otherwise, if all went well, then this parameter's null.

    Parameters

    • chan: ClientRequest | ServerResponse<IncomingMessage>

      Where to write to (an HTTP request object on a client, or an HTTP response object on a server).

    • parts: NmsPart[]

      The array of NmsPart to send, in the order in which they should be sent.

    • encoding: null | BufferEncoding

      The encoding for writing strings, can be null if all the data is in node.js Buffer objects.

    • sendEnd: boolean

      If true complete the HTTP message after the last data has been written, otherwise leave the HTTP message open for more sending.

    • doneCallback: ((e: null | Error) => void)

      The function to be called when all the parts have been sent, if writeMany() returned false.

        • (e): void
        • Parameters

          • e: null | Error

          Returns void

    Returns boolean

    True if the write completed before writeMany() returned, and false if you must wait for doneCallback for completion.

    NmsPart

  • Return the total byte length of all the NmsPart in the parts array. The total byte length is the value required for the Content-Length HTTP header. It includes the byte length of each part's header, as well as the byte lengths of each part's contents taking in to account the encoding to be used for strings. writeManyLen() requires that all the NmsPart objects have the correct value for their byteLen properties. If necessary NmsPart's instance method setByteLenFromContents() can be used to set a part's byteLen directly from its contents.

    Parameters

    • parts: NmsPart[]

      The array of NmsPart for which to return the total byte length.

    Returns number

    The total byte length.

    NmsPart