Class: MessageParser
@divine/uri.MessageParser
The message/*
parser handles all kinds of messages, including message/rfc822
.
The parsed data is a MimeMessage, but the serializer can handle any MimeMessageLike object.
Hierarchy
↳
MessageParser
Constructors
constructor
• Protected
new MessageParser(contentType
)
Constructs a new Parser instance.
Parameters
Name | Type | Description |
---|---|---|
contentType | ContentType | The media type this parser object was instanciated for. |
Inherited from
Defined in
Properties
contentType
• Readonly
contentType: ContentType
The media type this parser object was instanciated for.
Inherited from
Defined in
Methods
_assertSerializebleData
▸ Protected
_assertSerializebleData(condition
, data
, cause?
): asserts condition
A helper method used by parser subclasses to report invalid input.
Parameters
Name | Type | Description |
---|---|---|
condition | boolean | Must be true , or else a ParserError will be raised. |
data | unknown | Some kind of extra information that will be provided in data. |
cause? | unknown | If this error was caused by some other kind of failure, the original error will be available as cause. |
Returns
asserts condition
Inherited from
Parser._assertSerializebleData
Defined in
parse
▸ parse(stream
): Promise
<MimeMessage
>
Parses a stream and returns the result as a parser-specific representation.
This method must be implemented by the actual subclass.
Throws
ParserError On parser errors.
Parameters
Name | Type | Description |
---|---|---|
stream | AsyncIterable <Buffer > | The stream to parse. |
Returns
Promise
<MimeMessage
>
The parsed stream.
Overrides
Defined in
serialize
▸ serialize(data
): AsyncIterable
<Buffer
>
Serializes a parsed or manually constructed object back into a Buffer or byte stream.
This method must be implemented by the actual subclass.
Throws
ParserError On serialization errors.
Parameters
Name | Type | Description |
---|---|---|
data | MimeMessageLike | A parser-specific representation that is to be serialized. |
Returns
AsyncIterable
<Buffer
>
A Buffer or a byte stream.
Overrides
Defined in
parse
▸ Static
parse<T
>(stream
, contentType
): Promise
<T
& Finalizable
>
Parses a given string, Buffer or byte stream using a parser registered for a specific media type.
NOTE: This method always returns an object. Primitives are never returned. This means that text, for instance
will be returned as a String object, null
as Object(NULL) and undefined
as Object(VOID). You
may use toPrimitive to return the original value, or use .valueOf()
and test the result against the
NULL and VOID symbols.
Throws
ParserError On parser errors or if the media type is not recognized.
Type parameters
Name | Type | Description |
---|---|---|
T | extends object | The type of the returned object. |
Parameters
Name | Type | Description |
---|---|---|
stream | string | Buffer | AsyncIterable <string | Buffer > | The source that should be parsed. |
contentType | string | ContentType | The media type that specifies what parser to use. |
Returns
Promise
<T
& Finalizable
>
An object (always an object) that represents the original source after parsing. It's possible that the Parser subclass allocated temporary resources as part of the process. These resources may be cleaned up by calling FINALIZE.
Inherited from
Defined in
register
▸ Static
register(type
, parser
): typeof Parser
Registers a new parser/serializer. All subclasses must register their MIME media type support with this method.
Parameters
Name | Type | Description |
---|---|---|
type | string | RegExp | The content/media type the parser can handle. |
parser | typeof Parser | The Parser subclass to register. |
Returns
typeof Parser
The Parser base class (for method chaining).
Inherited from
Defined in
serialize
▸ Static
serialize<T
>(data
, contentType?
): [Buffer
| Readable
& AsyncIterable
<Buffer
>, ContentType
]
Converts a parsed (or manually constructed) object back into a byte stream representation.
Buffers and ReadableStream will be passed through as-is. Strings will just be encoded using the charset
param
from contentType
(or UTF-8 if not present). Everything else is serialized using a Parser subclass.
Throws
ParserError On serialization errors or if the media type is not recognized.
Type parameters
Name | Type | Description |
---|---|---|
T | unknown | The type of the object that is to be serialized. |
Parameters
Name | Type | Description |
---|---|---|
data | T | The object that is to be serialized. |
contentType? | string | ContentType | The media type that specifies what parser to use. |
Returns
[Buffer
| Readable
& AsyncIterable
<Buffer
>, ContentType
]
A tuple containing the Buffer/byte stream and the actual media type. Note that the parser may return a slightly different media type than was given (for instance, MultiPartParser might add a boundary param if none was given).
Inherited from
Defined in
serializeToBuffer
▸ Static
serializeToBuffer<T
>(data
, contentType?
): Promise
<[Buffer
, ContentType
]>
Converts a parsed (or manually constructed) object into a Buffer.
This is a convenience method that just invokes parse and then converts the byte stream into a single Buffer.
Throws
ParserError On serialization errors or if the media type is not recognized.
Type parameters
Name | Type |
---|---|
T | unknown |
Parameters
Name | Type | Description |
---|---|---|
data | T | The object that is to be serialized. |
contentType? | string | ContentType | The media type that specifies what parser to use. |
Returns
Promise
<[Buffer
, ContentType
]>
A tuple containing the Buffer and the actual media type. Note that the parser may return a slightly different media type than was given (for instance, MultiPartParser might add a boundary param if none was given).