|
1 | 1 | /// <reference types="node" /> |
2 | 2 |
|
3 | | -export type MimeWordEncoding = "Q" | "B"; |
4 | | - |
5 | | -/** |
6 | | - * Encodes a string into mime [encoded word](http://en.wikipedia.org/wiki/MIME#Encoded-Word) format. |
7 | | - * |
8 | | - * @param data Value to be encoded. |
9 | | - * @param mimeWordEncoding Encoding for the mime word. Defaults to `'Q'`. |
10 | | - * @param maxLength If set, split mime words into several chunks if needed. Defaults to `0`. |
11 | | - * @returns Single or several mime words joined together. |
12 | | - * |
13 | | - * @example |
14 | | - * import * as libmime from 'libmime'; |
15 | | - * |
16 | | - * libmime.encodeWord('See on õhin test', 'Q'); |
17 | | - * // --> =?UTF-8?Q?See_on_=C3=B5hin_test?= |
18 | | - */ |
19 | | -export function encodeWord(data: string | Buffer, mimeWordEncoding?: MimeWordEncoding, maxLength?: number): string; |
20 | | - |
21 | | -/** |
22 | | - * Encodes non ascii sequences in a string to mime words. |
23 | | - * |
24 | | - * @param data Value to be encoded. |
25 | | - * @param mimeWordEncoding Encoding for the mime word. Defaults to `'Q'`. |
26 | | - * @param maxLength If set, split mime words into several chunks if needed. |
27 | | - * @param fromCharset Source sharacter set. Defaults to `'UTF-8'`. |
28 | | - * @returns String with possible mime words. |
29 | | - */ |
30 | | -export function encodeWords( |
31 | | - data: string | Buffer, |
32 | | - mimeWordEncoding?: MimeWordEncoding, |
33 | | - maxLength?: number, |
34 | | - fromCharset?: string, |
35 | | -): string; |
36 | | -export function encodeWords(data: string | Buffer, mimeWordEncoding?: MimeWordEncoding, fromCharset?: string): string; |
37 | | - |
38 | | -/** |
39 | | - * Decode a complete mime word encoded string. |
40 | | - * |
41 | | - * @param charset Character set for the string. |
42 | | - * @param mimeWordEncoding Mime encoding for the string. |
43 | | - * @param str Mime word encoded string. |
44 | | - * @returns Decoded unicode string. |
45 | | - */ |
46 | | -export function decodeWord(charset: string, mimeWordEncoding: MimeWordEncoding, str: string): string; |
47 | | - |
48 | | -/** |
49 | | - * Decodes a string that might include one or several mime words. If no mime words are found from the string, |
50 | | - * the original string is returned. |
51 | | - * |
52 | | - * @param str String to be decoded. |
53 | | - * @returns Decoded unicode string. |
54 | | - */ |
55 | | -export function decodeWords(str: string): string; |
56 | | - |
57 | | -/** |
58 | | - * Folds a long line according to the [RFC 5322](http://tools.ietf.org/html/rfc5322#section-2.1.1). |
59 | | - * Mostly needed for folding header lines. |
60 | | - * |
61 | | - * @param str String to be folded. |
62 | | - * @param lineLength Maximum length of a line. Defaults to `76`. |
63 | | - * @param afterSpace If `true`, leave a space in the end of a line. |
64 | | - * @returns String with folded lines. |
65 | | - * |
66 | | - * @example |
67 | | - * import * as libmime from 'libmime'; |
68 | | - * |
69 | | - * libmime.foldLines('Content-Type: multipart/alternative; boundary="----zzzz----"'); |
70 | | - * // --> |
71 | | - * // Content-Type: multipart/alternative; |
72 | | - * // boundary="----zzzz----" |
73 | | - */ |
74 | | -export function foldLines(str: string, lineLength?: number, afterSpace?: boolean): string; |
75 | | - |
76 | | -/** |
77 | | - * Adds soft line breaks to content marked with `format=flowed` to ensure that no line |
78 | | - * in the message is longer than `lineLength`. |
79 | | - * |
80 | | - * @param str Plaintext string that requires wrapping. |
81 | | - * @param lineLength Maximum length of a line. Defaults to `76`. |
82 | | - * @returns String with forced line breaks. |
83 | | - */ |
84 | | -export function encodeFlowed(str: string, lineLength?: number): string; |
85 | | - |
86 | | -/** |
87 | | - * Unwraps a plaintext string in `format=flowed` soft wrapping. |
88 | | - * |
89 | | - * @param str Plaintext string with `format=flowed` to decode. |
90 | | - * @param delSp If `true`, delete leading spaces. Defaults to `false`. |
91 | | - * @returns Mime decoded string. |
92 | | - */ |
93 | | -export function decodeFlowed(str: string, delSp?: boolean): string; |
94 | | - |
95 | | -/** |
96 | | - * Unfolds a header line and splits it to key and value pair. The value is not mime word decoded, |
97 | | - * you need to do your own decoding based on the rules for the specific header key. |
98 | | - * |
99 | | - * @param headerLine Single header line, might include linebreaks as well if folded. |
100 | | - */ |
101 | | -export function decodeHeader(headerLine: string): { key: string; value: string }; |
102 | | - |
103 | | -/** |
104 | | - * Parses a block of header lines. Does not decode mime words as every header might have its own |
105 | | - * rules (eg. formatted email addresses and such). |
106 | | - * |
107 | | - * @param headers Headers string. |
108 | | - */ |
109 | | -export function decodeHeaders(headers: string): { [key: string]: string[] }; |
110 | | - |
111 | | -export interface StructuredHeader { |
112 | | - value: string; |
113 | | - params: { [key: string]: string }; |
| 3 | +declare namespace libmime { |
| 4 | + type MimeWordEncoding = "Q" | "B"; |
| 5 | + |
| 6 | + interface StructuredHeader { |
| 7 | + value: string; |
| 8 | + params: { [key: string]: string }; |
| 9 | + } |
| 10 | +} |
| 11 | + |
| 12 | +declare class Libmime { |
| 13 | + /** |
| 14 | + * Encodes a string into mime [encoded word](http://en.wikipedia.org/wiki/MIME#Encoded-Word) format. |
| 15 | + * |
| 16 | + * @param data Value to be encoded. |
| 17 | + * @param mimeWordEncoding Encoding for the mime word. Defaults to `'Q'`. |
| 18 | + * @param maxLength If set, split mime words into several chunks if needed. Defaults to `0`. |
| 19 | + * @returns Single or several mime words joined together. |
| 20 | + * |
| 21 | + * @example |
| 22 | + * import * as libmime from 'libmime'; |
| 23 | + * |
| 24 | + * libmime.encodeWord('See on õhin test', 'Q'); |
| 25 | + * // --> =?UTF-8?Q?See_on_=C3=B5hin_test?= |
| 26 | + */ |
| 27 | + encodeWord(data: string | Buffer, mimeWordEncoding?: libmime.MimeWordEncoding, maxLength?: number): string; |
| 28 | + |
| 29 | + /** |
| 30 | + * Encodes non ascii sequences in a string to mime words. |
| 31 | + * |
| 32 | + * @param data Value to be encoded. |
| 33 | + * @param mimeWordEncoding Encoding for the mime word. Defaults to `'Q'`. |
| 34 | + * @param maxLength If set, split mime words into several chunks if needed. |
| 35 | + * @param fromCharset Source sharacter set. Defaults to `'UTF-8'`. |
| 36 | + * @returns String with possible mime words. |
| 37 | + */ |
| 38 | + encodeWords( |
| 39 | + data: string | Buffer, |
| 40 | + mimeWordEncoding?: libmime.MimeWordEncoding, |
| 41 | + maxLength?: number, |
| 42 | + fromCharset?: string, |
| 43 | + ): string; |
| 44 | + encodeWords(data: string | Buffer, mimeWordEncoding?: libmime.MimeWordEncoding, fromCharset?: string): string; |
| 45 | + |
| 46 | + /** |
| 47 | + * Decode a complete mime word encoded string. |
| 48 | + * |
| 49 | + * @param charset Character set for the string. |
| 50 | + * @param mimeWordEncoding Mime encoding for the string. |
| 51 | + * @param str Mime word encoded string. |
| 52 | + * @returns Decoded unicode string. |
| 53 | + */ |
| 54 | + decodeWord(charset: string, mimeWordEncoding: libmime.MimeWordEncoding, str: string): string; |
| 55 | + |
| 56 | + /** |
| 57 | + * Decodes a string that might include one or several mime words. If no mime words are found from the string, |
| 58 | + * the original string is returned. |
| 59 | + * |
| 60 | + * @param str String to be decoded. |
| 61 | + * @returns Decoded unicode string. |
| 62 | + */ |
| 63 | + decodeWords(str: string): string; |
| 64 | + |
| 65 | + /** |
| 66 | + * Folds a long line according to the [RFC 5322](http://tools.ietf.org/html/rfc5322#section-2.1.1). |
| 67 | + * Mostly needed for folding header lines. |
| 68 | + * |
| 69 | + * @param str String to be folded. |
| 70 | + * @param lineLength Maximum length of a line. Defaults to `76`. |
| 71 | + * @param afterSpace If `true`, leave a space in the end of a line. |
| 72 | + * @returns String with folded lines. |
| 73 | + * |
| 74 | + * @example |
| 75 | + * import * as libmime from 'libmime'; |
| 76 | + * |
| 77 | + * libmime.foldLines('Content-Type: multipart/alternative; boundary="----zzzz----"'); |
| 78 | + * // --> |
| 79 | + * // Content-Type: multipart/alternative; |
| 80 | + * // boundary="----zzzz----" |
| 81 | + */ |
| 82 | + foldLines(str: string, lineLength?: number, afterSpace?: boolean): string; |
| 83 | + |
| 84 | + /** |
| 85 | + * Adds soft line breaks to content marked with `format=flowed` to ensure that no line |
| 86 | + * in the message is longer than `lineLength`. |
| 87 | + * |
| 88 | + * @param str Plaintext string that requires wrapping. |
| 89 | + * @param lineLength Maximum length of a line. Defaults to `76`. |
| 90 | + * @returns String with forced line breaks. |
| 91 | + */ |
| 92 | + encodeFlowed(str: string, lineLength?: number): string; |
| 93 | + |
| 94 | + /** |
| 95 | + * Unwraps a plaintext string in `format=flowed` soft wrapping. |
| 96 | + * |
| 97 | + * @param str Plaintext string with `format=flowed` to decode. |
| 98 | + * @param delSp If `true`, delete leading spaces. Defaults to `false`. |
| 99 | + * @returns Mime decoded string. |
| 100 | + */ |
| 101 | + decodeFlowed(str: string, delSp?: boolean): string; |
| 102 | + |
| 103 | + /** |
| 104 | + * Unfolds a header line and splits it to key and value pair. The value is not mime word decoded, |
| 105 | + * you need to do your own decoding based on the rules for the specific header key. |
| 106 | + * |
| 107 | + * @param headerLine Single header line, might include linebreaks as well if folded. |
| 108 | + */ |
| 109 | + decodeHeader(headerLine: string): { key: string; value: string }; |
| 110 | + |
| 111 | + /** |
| 112 | + * Parses a block of header lines. Does not decode mime words as every header might have its own |
| 113 | + * rules (eg. formatted email addresses and such). |
| 114 | + * |
| 115 | + * @param headers Headers string. |
| 116 | + */ |
| 117 | + decodeHeaders(headers: string): { [key: string]: string[] }; |
| 118 | + |
| 119 | + /** |
| 120 | + * Parses a header value with `key=value` arguments into a structured object. Useful when dealing |
| 121 | + * with `content-type` and such. Continuation encoded params are joined into mime encoded words. |
| 122 | + * |
| 123 | + * @param valueString A header value without the key. |
| 124 | + * |
| 125 | + * @example |
| 126 | + * import * as libmime from 'libmime'; |
| 127 | + * |
| 128 | + * libmime.parseHeaderValue('content-type: text/plain; CHARSET="UTF-8"'); |
| 129 | + * // --> |
| 130 | + * // { |
| 131 | + * // "value": "text/plain", |
| 132 | + * // "params": { |
| 133 | + * // "charset": "UTF-8" |
| 134 | + * // } |
| 135 | + * // } |
| 136 | + */ |
| 137 | + parseHeaderValue(valueString: string): libmime.StructuredHeader; |
| 138 | + |
| 139 | + /** |
| 140 | + * Joins structured header value together as `'value; param1=value1; param2=value2'`. |
| 141 | + * |
| 142 | + * @param structuredHeader A header value formatted with `parseHeaderValue`. |
| 143 | + * `filename` argument is encoded with continuation encoding if needed. |
| 144 | + */ |
| 145 | + buildHeaderValue(structuredHeader: libmime.StructuredHeader): string; |
| 146 | + |
| 147 | + /** |
| 148 | + * Encodes and splits a header param value according to [RFC2231](https://tools.ietf.org/html/rfc2231#section-3) |
| 149 | + * Parameter Value Continuations. |
| 150 | + * |
| 151 | + * @param key Parameter key (eg. `filename`). |
| 152 | + * @param data Value to encode. |
| 153 | + * @param maxLength Maximum length of the encoded string part (not line length). Defaults to `50`. |
| 154 | + * @param fromCharset Source sharacter set. Defaults to `'UTF-8'`. |
| 155 | + * @returns A list of encoded keys and headers. |
| 156 | + * |
| 157 | + * @example |
| 158 | + * import * as libmime from 'libmime'; |
| 159 | + * |
| 160 | + * libmime.buildHeaderParam('filename', 'filename õäöü.txt', 20); |
| 161 | + * // --> |
| 162 | + * // [ |
| 163 | + * // { key: 'filename*0*', value: 'utf-8\'\'filename%20' }, |
| 164 | + * // { key: 'filename*1*', value: '%C3%B5%C3%A4%C3%B6' }, |
| 165 | + * // { key: 'filename*2*', value: '%C3%BC.txt' } |
| 166 | + * // ] |
| 167 | + */ |
| 168 | + buildHeaderParam( |
| 169 | + key: string, |
| 170 | + data: string | Buffer, |
| 171 | + maxLength?: number, |
| 172 | + fromCharset?: string, |
| 173 | + ): Array<{ key: string; value: string }>; |
| 174 | + |
| 175 | + /** |
| 176 | + * @param mimeType Content type to be checked for. |
| 177 | + * @returns File extension for a content type string. If no suitable extensions are found, |
| 178 | + * `'bin'` is used as the default extension. |
| 179 | + * |
| 180 | + * @example |
| 181 | + * import * as libmime from 'libmime'; |
| 182 | + * |
| 183 | + * libmime.detectExtension('image/jpeg'); |
| 184 | + * // --> 'jpeg' |
| 185 | + */ |
| 186 | + detectExtension(mimeType: string): string; |
| 187 | + |
| 188 | + /** |
| 189 | + * @param extension Extension (or filename) to be checked for. |
| 190 | + * @returns Content type for a file extension. If no suitable content types are found, |
| 191 | + * `'application/octet-stream'` is used as the default content type. |
| 192 | + * |
| 193 | + * @example |
| 194 | + * import * as libmime from 'libmime'; |
| 195 | + * |
| 196 | + * libmime.detectMimeType('logo.jpg'); |
| 197 | + * // --> 'image/jpeg' |
| 198 | + */ |
| 199 | + detectMimeType(extension: string): string; |
114 | 200 | } |
115 | | -/** |
116 | | - * Parses a header value with `key=value` arguments into a structured object. Useful when dealing |
117 | | - * with `content-type` and such. Continuation encoded params are joined into mime encoded words. |
118 | | - * |
119 | | - * @param valueString A header value without the key. |
120 | | - * |
121 | | - * @example |
122 | | - * import * as libmime from 'libmime'; |
123 | | - * |
124 | | - * libmime.parseHeaderValue('content-type: text/plain; CHARSET="UTF-8"'); |
125 | | - * // --> |
126 | | - * // { |
127 | | - * // "value": "text/plain", |
128 | | - * // "params": { |
129 | | - * // "charset": "UTF-8" |
130 | | - * // } |
131 | | - * // } |
132 | | - */ |
133 | | -export function parseHeaderValue(valueString: string): StructuredHeader; |
134 | | - |
135 | | -/** |
136 | | - * Joins structured header value together as `'value; param1=value1; param2=value2'`. |
137 | | - * |
138 | | - * @param structuredHeader A header value formatted with `parseHeaderValue`. |
139 | | - * `filename` argument is encoded with continuation encoding if needed. |
140 | | - */ |
141 | | -export function buildHeaderValue(structuredHeader: StructuredHeader): string; |
142 | | - |
143 | | -/** |
144 | | - * Encodes and splits a header param value according to [RFC2231](https://tools.ietf.org/html/rfc2231#section-3) |
145 | | - * Parameter Value Continuations. |
146 | | - * |
147 | | - * @param key Parameter key (eg. `filename`). |
148 | | - * @param data Value to encode. |
149 | | - * @param maxLength Maximum length of the encoded string part (not line length). Defaults to `50`. |
150 | | - * @param fromCharset Source sharacter set. Defaults to `'UTF-8'`. |
151 | | - * @returns A list of encoded keys and headers. |
152 | | - * |
153 | | - * @example |
154 | | - * import * as libmime from 'libmime'; |
155 | | - * |
156 | | - * libmime.buildHeaderParam('filename', 'filename õäöü.txt', 20); |
157 | | - * // --> |
158 | | - * // [ |
159 | | - * // { key: 'filename*0*', value: 'utf-8\'\'filename%20' }, |
160 | | - * // { key: 'filename*1*', value: '%C3%B5%C3%A4%C3%B6' }, |
161 | | - * // { key: 'filename*2*', value: '%C3%BC.txt' } |
162 | | - * // ] |
163 | | - */ |
164 | | -export function buildHeaderParam( |
165 | | - key: string, |
166 | | - data: string | Buffer, |
167 | | - maxLength?: number, |
168 | | - fromCharset?: string, |
169 | | -): Array<{ key: string; value: string }>; |
170 | | - |
171 | | -/** |
172 | | - * @param mimeType Content type to be checked for. |
173 | | - * @returns File extension for a content type string. If no suitable extensions are found, |
174 | | - * `'bin'` is used as the default extension. |
175 | | - * |
176 | | - * @example |
177 | | - * import * as libmime from 'libmime'; |
178 | | - * |
179 | | - * libmime.detectExtension('image/jpeg'); |
180 | | - * // --> 'jpeg' |
181 | | - */ |
182 | | -export function detectExtension(mimeType: string): string; |
183 | | - |
184 | | -/** |
185 | | - * @param extension Extension (or filename) to be checked for. |
186 | | - * @returns Content type for a file extension. If no suitable content types are found, |
187 | | - * `'application/octet-stream'` is used as the default content type. |
188 | | - * |
189 | | - * @example |
190 | | - * import * as libmime from 'libmime'; |
191 | | - * |
192 | | - * libmime.detectMimeType('logo.jpg'); |
193 | | - * // --> 'image/jpeg' |
194 | | - */ |
195 | | -export function detectMimeType(extension: string): string; |
| 201 | + |
| 202 | +declare const libmime: Libmime & { Libmime: typeof Libmime }; |
| 203 | + |
| 204 | +export = libmime; |
0 commit comments