imapflow 2.4.2 → 2.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +17 -0
- package/README.md +59 -1
- package/dist/cjs/errors.d.ts +2 -0
- package/dist/cjs/errors.js +3 -0
- package/dist/cjs/imap-flow.d.ts +9 -9
- package/dist/cjs/imap-flow.js +100 -15
- package/dist/cjs/package-info.d.ts +1 -1
- package/dist/cjs/package-info.js +1 -1
- package/dist/cjs/sockets-transport.d.ts +44 -0
- package/dist/cjs/sockets-transport.js +89 -0
- package/dist/cjs/streams-transport.d.ts +84 -0
- package/dist/cjs/streams-transport.js +281 -0
- package/dist/cjs/tools.d.ts +9 -0
- package/dist/cjs/tools.js +12 -0
- package/dist/cjs/types.d.ts +76 -1
- package/dist/esm/errors.d.ts +2 -0
- package/dist/esm/errors.js +3 -0
- package/dist/esm/imap-flow.d.ts +9 -9
- package/dist/esm/imap-flow.js +98 -15
- package/dist/esm/package-info.d.ts +1 -1
- package/dist/esm/package-info.js +1 -1
- package/dist/esm/sockets-transport.d.ts +44 -0
- package/dist/esm/sockets-transport.js +86 -0
- package/dist/esm/streams-transport.d.ts +84 -0
- package/dist/esm/streams-transport.js +278 -0
- package/dist/esm/tools.d.ts +9 -0
- package/dist/esm/tools.js +11 -0
- package/dist/esm/types.d.ts +76 -1
- package/package.json +16 -5
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import type { ImapTransport, ImapTransportConnectOptions, ImapTransportOptions } from './types.js';
|
|
2
|
+
/** The reader of {@link StreamsSocketInfo.readable} */
|
|
3
|
+
export interface StreamsReader {
|
|
4
|
+
read(): Promise<{
|
|
5
|
+
done: boolean;
|
|
6
|
+
value?: Uint8Array | undefined;
|
|
7
|
+
}>;
|
|
8
|
+
releaseLock(): void;
|
|
9
|
+
}
|
|
10
|
+
/** The writer of {@link StreamsSocketInfo.writable} */
|
|
11
|
+
export interface StreamsWriter {
|
|
12
|
+
write(chunk: Uint8Array): Promise<void>;
|
|
13
|
+
releaseLock(): void;
|
|
14
|
+
}
|
|
15
|
+
/** What a {@link StreamsSocket} resolves with once it is open */
|
|
16
|
+
export interface StreamsSocketInfo {
|
|
17
|
+
/** Bytes from the server, a ReadableStream of Uint8Array */
|
|
18
|
+
readable: {
|
|
19
|
+
getReader(): StreamsReader;
|
|
20
|
+
};
|
|
21
|
+
/** Bytes to the server, a WritableStream of Uint8Array */
|
|
22
|
+
writable: {
|
|
23
|
+
getWriter(): StreamsWriter;
|
|
24
|
+
};
|
|
25
|
+
remoteAddress?: string | undefined;
|
|
26
|
+
remotePort?: number | undefined;
|
|
27
|
+
localAddress?: string | undefined;
|
|
28
|
+
localPort?: number | undefined;
|
|
29
|
+
/** Details of a TLS connection, reported as `client.tls`. Left out when the API has none */
|
|
30
|
+
tls?: StreamsTlsInfo | undefined;
|
|
31
|
+
}
|
|
32
|
+
/** What a socket API knows about a TLS connection */
|
|
33
|
+
export interface StreamsTlsInfo {
|
|
34
|
+
/** Whether the server certificate was verified */
|
|
35
|
+
authorized: boolean;
|
|
36
|
+
/** Cipher name, e.g. `TLS_AES_256_GCM_SHA384` */
|
|
37
|
+
name?: string | undefined;
|
|
38
|
+
standardName?: string | undefined;
|
|
39
|
+
/** Protocol version, e.g. `TLSv1.3` */
|
|
40
|
+
version?: string | undefined;
|
|
41
|
+
}
|
|
42
|
+
/** A connection of a web streams socket API, as {@link StreamsTransportApi} opens it */
|
|
43
|
+
export interface StreamsSocket {
|
|
44
|
+
/** Resolves once the connection (and the TLS handshake, if any) is established, rejects if it fails */
|
|
45
|
+
opened: Promise<StreamsSocketInfo>;
|
|
46
|
+
/** Rejects if the connection fails after it was opened */
|
|
47
|
+
closed?: Promise<unknown> | undefined;
|
|
48
|
+
/** Closes the connection */
|
|
49
|
+
close(): unknown;
|
|
50
|
+
}
|
|
51
|
+
/** The socket API that {@link createStreamsTransport} drives */
|
|
52
|
+
export interface StreamsTransportApi<S extends StreamsSocket = StreamsSocket> {
|
|
53
|
+
/**
|
|
54
|
+
* Opens a connection, cleartext or TLS as `options.secure` says. Throws to refuse it, for
|
|
55
|
+
* example with an `UnsupportedTransportOption` error for TLS options the API can not honor.
|
|
56
|
+
*/
|
|
57
|
+
open(options: ImapTransportConnectOptions): S;
|
|
58
|
+
/**
|
|
59
|
+
* Starts TLS on an open cleartext connection after STARTTLS and returns the TLS connection,
|
|
60
|
+
* the old one stays open as its transport. Throws to refuse the upgrade. Leave it out when
|
|
61
|
+
* the API can not upgrade a connection: ImapFlow then does not try STARTTLS.
|
|
62
|
+
*/
|
|
63
|
+
upgrade?: ((socket: S, options: ImapTransportOptions) => S) | undefined;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Builds a transport for the `transport` option over a socket API built on web streams. A new
|
|
67
|
+
* API needs only `open()`, and `upgrade()` if it can start TLS on an open connection:
|
|
68
|
+
*
|
|
69
|
+
* ```js
|
|
70
|
+
* const transport = createStreamsTransport({
|
|
71
|
+
* open: ({ host, port, secure }) => {
|
|
72
|
+
* const socket = openSomeSocket(host, port, secure);
|
|
73
|
+
* return { opened: socket.ready.then(() => ({ readable: socket.readable, writable: socket.writable })), close: () => socket.close() };
|
|
74
|
+
* }
|
|
75
|
+
* });
|
|
76
|
+
* ```
|
|
77
|
+
*
|
|
78
|
+
* The transport stops reading once the server accepted a STARTTLS, so the API never has to start
|
|
79
|
+
* TLS with a read in flight, and releases the stream locks before `upgrade()` is called.
|
|
80
|
+
*
|
|
81
|
+
* @param api - Opens (and upgrades) connections
|
|
82
|
+
* @returns A transport for the `transport` option
|
|
83
|
+
*/
|
|
84
|
+
export declare function createStreamsTransport<S extends StreamsSocket>(api: StreamsTransportApi<S>): ImapTransport;
|
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// The `transport` option over a socket API built on web streams: the socket opens with a promise
|
|
3
|
+
// that resolves to a ReadableStream and a WritableStream of bytes. `cloudflare:sockets` (celld,
|
|
4
|
+
// Cloudflare Workers) is one, see sockets-transport.ts; Chrome's Direct Sockets and in-memory
|
|
5
|
+
// connections fit the same shape. The streams are wrapped into a Duplex that behaves like the
|
|
6
|
+
// net.Socket ImapFlow drives.
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.createStreamsTransport = createStreamsTransport;
|
|
9
|
+
const node_stream_1 = require("node:stream");
|
|
10
|
+
const tools_js_1 = require("./tools.js");
|
|
11
|
+
function closeQuietly(socket) {
|
|
12
|
+
try {
|
|
13
|
+
Promise.resolve(socket.close()).catch(tools_js_1.noop);
|
|
14
|
+
}
|
|
15
|
+
catch {
|
|
16
|
+
// already closed
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
class StreamsDuplex extends node_stream_1.Duplex {
|
|
20
|
+
constructor(api, socket, onConnect, plain) {
|
|
21
|
+
super({ allowHalfOpen: false });
|
|
22
|
+
// Taken once the socket is open: locking the streams of a socket returned by startTls()
|
|
23
|
+
// before its handshake completes stalls the handshake on celld
|
|
24
|
+
this._reader = null;
|
|
25
|
+
this._writer = null;
|
|
26
|
+
this._pumping = false;
|
|
27
|
+
this._readRequested = false;
|
|
28
|
+
// A runtime may only start TLS on a socket without a read in flight (with one, celld never
|
|
29
|
+
// completes the handshake), while the read loop always keeps one. So once the client
|
|
30
|
+
// announced a STARTTLS (holdAfterStarttls), reading stops after the tagged line that answers
|
|
31
|
+
// it, the only command in flight then, and resumes only when that line is not an OK.
|
|
32
|
+
this._awaitingStarttls = false;
|
|
33
|
+
this._lineTail = '';
|
|
34
|
+
// Set when the partial line got too long to be the one looked for and was dropped: the rest
|
|
35
|
+
// of it, up to the next line break, is skipped too
|
|
36
|
+
this._tailDropped = false;
|
|
37
|
+
this._readsHeld = false;
|
|
38
|
+
// Set once startTls() handed the connection to a TLS socket: the read that ends then is not
|
|
39
|
+
// the end of the connection, and closing this socket would close the TLS one
|
|
40
|
+
this._upgraded = false;
|
|
41
|
+
this._timeout = 0;
|
|
42
|
+
this._timer = null;
|
|
43
|
+
// Traffic only records the time, the one timer re-arms itself for the rest of the idle period
|
|
44
|
+
this._lastActivity = 0;
|
|
45
|
+
this._api = api;
|
|
46
|
+
this._socket = socket;
|
|
47
|
+
this._plain = plain || null;
|
|
48
|
+
socket.opened.then(info => {
|
|
49
|
+
if (this.destroyed) {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
try {
|
|
53
|
+
this._reader = info.readable.getReader();
|
|
54
|
+
this._writer = info.writable.getWriter();
|
|
55
|
+
}
|
|
56
|
+
catch (err) {
|
|
57
|
+
// streams already locked by someone else
|
|
58
|
+
this.destroy(err);
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
this.remoteAddress = info.remoteAddress;
|
|
62
|
+
this.remotePort = info.remotePort;
|
|
63
|
+
this.localAddress = info.localAddress;
|
|
64
|
+
this.localPort = info.localPort;
|
|
65
|
+
const tls = info.tls;
|
|
66
|
+
if (tls) {
|
|
67
|
+
this.authorized = tls.authorized;
|
|
68
|
+
this.getCipher = () => ({ name: tls.name || '', standardName: tls.standardName || tls.name || '', version: tls.version || '' });
|
|
69
|
+
}
|
|
70
|
+
this._touch();
|
|
71
|
+
onConnect();
|
|
72
|
+
if (this._readRequested) {
|
|
73
|
+
void this._pump();
|
|
74
|
+
}
|
|
75
|
+
}, (err) => this.destroy(err));
|
|
76
|
+
socket.closed?.catch((err) => {
|
|
77
|
+
if (!this._upgraded) {
|
|
78
|
+
this.destroy(err);
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
_read() {
|
|
83
|
+
if (this._readsHeld) {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
if (!this._reader) {
|
|
87
|
+
this._readRequested = true;
|
|
88
|
+
}
|
|
89
|
+
else if (!this._pumping) {
|
|
90
|
+
void this._pump();
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
async _pump() {
|
|
94
|
+
const reader = this._reader;
|
|
95
|
+
this._pumping = true;
|
|
96
|
+
try {
|
|
97
|
+
for (;;) {
|
|
98
|
+
const { done, value } = await reader.read();
|
|
99
|
+
if (this._upgraded || this.destroyed) {
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
if (done || !value) {
|
|
103
|
+
this.push(null);
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
this._touch();
|
|
107
|
+
const chunk = Buffer.from(value.buffer, value.byteOffset, value.byteLength);
|
|
108
|
+
const pushed = this.push(chunk);
|
|
109
|
+
if (this._awaitingStarttls && this._starttlsCompleted(chunk)) {
|
|
110
|
+
this._readsHeld = true;
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
if (!pushed) {
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
catch (err) {
|
|
119
|
+
if (!this._upgraded) {
|
|
120
|
+
this.destroy(err);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
finally {
|
|
124
|
+
this._pumping = false;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
// Whether the chunk holds the tagged response of the STARTTLS command and it is an OK. Lines
|
|
128
|
+
// end with LF, CR is optional, as for the parser
|
|
129
|
+
_starttlsCompleted(chunk) {
|
|
130
|
+
const skipFirst = this._tailDropped;
|
|
131
|
+
const lines = (this._lineTail + chunk.toString('latin1')).split('\n');
|
|
132
|
+
const tail = lines.pop();
|
|
133
|
+
if (skipFirst && !lines.length) {
|
|
134
|
+
// still inside the dropped line
|
|
135
|
+
return false;
|
|
136
|
+
}
|
|
137
|
+
this._tailDropped = tail.length > 1024;
|
|
138
|
+
this._lineTail = this._tailDropped ? '' : tail;
|
|
139
|
+
for (let i = skipFirst ? 1 : 0; i < lines.length; i++) {
|
|
140
|
+
const tagged = /^(?![*+] )\S+ (OK|NO|BAD)\b/i.exec(lines[i]);
|
|
141
|
+
if (tagged) {
|
|
142
|
+
this._awaitingStarttls = false;
|
|
143
|
+
return tagged[1].toUpperCase() === 'OK';
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return false;
|
|
147
|
+
}
|
|
148
|
+
/** Stops reading after the tagged response of the STARTTLS command about to be sent, if it is an OK */
|
|
149
|
+
holdAfterStarttls() {
|
|
150
|
+
this._awaitingStarttls = true;
|
|
151
|
+
this._lineTail = '';
|
|
152
|
+
this._tailDropped = false;
|
|
153
|
+
}
|
|
154
|
+
_write(chunk, _encoding, callback) {
|
|
155
|
+
if (!this._writer) {
|
|
156
|
+
// ImapFlow writes only after onConnect, this is a misuse
|
|
157
|
+
callback(new Error('Socket is not open yet'));
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
this._touch();
|
|
161
|
+
this._writer.write(chunk).then(() => callback(), callback);
|
|
162
|
+
}
|
|
163
|
+
_destroy(err, callback) {
|
|
164
|
+
this._clearTimer();
|
|
165
|
+
if (!this._upgraded) {
|
|
166
|
+
this._close();
|
|
167
|
+
}
|
|
168
|
+
if (this._plain) {
|
|
169
|
+
// the plain connection went with the TLS one, unless the API keeps it open
|
|
170
|
+
this._plain._close();
|
|
171
|
+
this._plain.destroy();
|
|
172
|
+
}
|
|
173
|
+
callback(err);
|
|
174
|
+
}
|
|
175
|
+
// Closes the connection. An API that refuses to close while its streams are locked (Direct
|
|
176
|
+
// Sockets) gets a second attempt with the locks released; releasing them right away would
|
|
177
|
+
// fail the read in flight, which ends the request on celld
|
|
178
|
+
_close() {
|
|
179
|
+
const retry = () => {
|
|
180
|
+
for (const stream of [this._reader, this._writer]) {
|
|
181
|
+
try {
|
|
182
|
+
stream?.releaseLock();
|
|
183
|
+
}
|
|
184
|
+
catch {
|
|
185
|
+
// an older streams implementation refuses with a read in flight
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
closeQuietly(this._socket);
|
|
189
|
+
};
|
|
190
|
+
try {
|
|
191
|
+
const closing = this._socket.close();
|
|
192
|
+
if (typeof closing?.then === 'function') {
|
|
193
|
+
closing.then(undefined, retry);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
catch {
|
|
197
|
+
retry();
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
/** Like net.Socket#setTimeout(): emits 'timeout' after `ms` without traffic, 0 disables */
|
|
201
|
+
setTimeout(ms) {
|
|
202
|
+
this._timeout = ms;
|
|
203
|
+
this._clearTimer();
|
|
204
|
+
this._touch();
|
|
205
|
+
return this;
|
|
206
|
+
}
|
|
207
|
+
setKeepAlive() {
|
|
208
|
+
return this;
|
|
209
|
+
}
|
|
210
|
+
/** Hands the connection over to a TLS socket, this one only stays as its transport */
|
|
211
|
+
startTls(options, onSecure) {
|
|
212
|
+
this._upgraded = true;
|
|
213
|
+
this._clearTimer();
|
|
214
|
+
// workerd refuses to start TLS while the streams are locked, nothing is in flight on them
|
|
215
|
+
this._reader?.releaseLock();
|
|
216
|
+
this._writer?.releaseLock();
|
|
217
|
+
let tlsSocket;
|
|
218
|
+
try {
|
|
219
|
+
tlsSocket = this._api.upgrade(this._socket, options);
|
|
220
|
+
}
|
|
221
|
+
catch (err) {
|
|
222
|
+
// refused before any handshake, the connection is closed with this socket
|
|
223
|
+
this._upgraded = false;
|
|
224
|
+
throw err;
|
|
225
|
+
}
|
|
226
|
+
return new StreamsDuplex(this._api, tlsSocket, onSecure, this);
|
|
227
|
+
}
|
|
228
|
+
_touch() {
|
|
229
|
+
this._lastActivity = Date.now();
|
|
230
|
+
if (!this._timer) {
|
|
231
|
+
this._arm(this._timeout);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
_arm(ms) {
|
|
235
|
+
if (this._timeout > 0 && !this.destroyed && !this._upgraded) {
|
|
236
|
+
this._timer = (0, tools_js_1.unrefTimer)(setTimeout(() => {
|
|
237
|
+
this._timer = null;
|
|
238
|
+
const idle = Date.now() - this._lastActivity;
|
|
239
|
+
if (idle >= this._timeout) {
|
|
240
|
+
this.emit('timeout');
|
|
241
|
+
}
|
|
242
|
+
else {
|
|
243
|
+
this._arm(this._timeout - idle);
|
|
244
|
+
}
|
|
245
|
+
}, ms));
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
_clearTimer() {
|
|
249
|
+
(0, tools_js_1.clearTimer)(this._timer);
|
|
250
|
+
this._timer = null;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Builds a transport for the `transport` option over a socket API built on web streams. A new
|
|
255
|
+
* API needs only `open()`, and `upgrade()` if it can start TLS on an open connection:
|
|
256
|
+
*
|
|
257
|
+
* ```js
|
|
258
|
+
* const transport = createStreamsTransport({
|
|
259
|
+
* open: ({ host, port, secure }) => {
|
|
260
|
+
* const socket = openSomeSocket(host, port, secure);
|
|
261
|
+
* return { opened: socket.ready.then(() => ({ readable: socket.readable, writable: socket.writable })), close: () => socket.close() };
|
|
262
|
+
* }
|
|
263
|
+
* });
|
|
264
|
+
* ```
|
|
265
|
+
*
|
|
266
|
+
* The transport stops reading once the server accepted a STARTTLS, so the API never has to start
|
|
267
|
+
* TLS with a read in flight, and releases the stream locks before `upgrade()` is called.
|
|
268
|
+
*
|
|
269
|
+
* @param api - Opens (and upgrades) connections
|
|
270
|
+
* @returns A transport for the `transport` option
|
|
271
|
+
*/
|
|
272
|
+
function createStreamsTransport(api) {
|
|
273
|
+
const transport = {
|
|
274
|
+
connect: (options, onConnect) => new StreamsDuplex(api, api.open(options), onConnect)
|
|
275
|
+
};
|
|
276
|
+
if (api.upgrade) {
|
|
277
|
+
transport.prepareUpgrade = socket => socket.holdAfterStarttls();
|
|
278
|
+
transport.upgrade = (socket, options, onSecure) => socket.startTls(options, onSecure);
|
|
279
|
+
}
|
|
280
|
+
return transport;
|
|
281
|
+
}
|
package/dist/cjs/tools.d.ts
CHANGED
|
@@ -7,6 +7,15 @@ export { AuthenticationFailure } from './errors.js';
|
|
|
7
7
|
export declare const EXPANDED_RANGE_LIMIT = 16777216;
|
|
8
8
|
export declare const MAX_UINT32_DIGITS = 10;
|
|
9
9
|
export declare const noop: () => void;
|
|
10
|
+
/**
|
|
11
|
+
* Tells an IP literal from a hostname without `node:net`, which some runtimes do not implement.
|
|
12
|
+
* Only used to decide whether a host can be a TLS servername: a dotted quad, or anything with a
|
|
13
|
+
* colon (an IPv6 literal, a hostname never contains one).
|
|
14
|
+
*
|
|
15
|
+
* @param host - Hostname or IP address
|
|
16
|
+
* @returns true for an IP literal
|
|
17
|
+
*/
|
|
18
|
+
export declare function isIpLiteral(host: string): boolean;
|
|
10
19
|
/**
|
|
11
20
|
* A stream decoder returned by getDecoder(). The Japanese decoder reports the `limited`
|
|
12
21
|
* flag once it has buffered all it will accept; a streaming iconv decoder never sets it.
|
package/dist/cjs/tools.js
CHANGED
|
@@ -5,6 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
};
|
|
6
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
7
|
exports.noop = exports.MAX_UINT32_DIGITS = exports.EXPANDED_RANGE_LIMIT = exports.AuthenticationFailure = void 0;
|
|
8
|
+
exports.isIpLiteral = isIpLiteral;
|
|
8
9
|
exports.buildConnectionError = buildConnectionError;
|
|
9
10
|
exports.restampConnectionError = restampConnectionError;
|
|
10
11
|
exports.guardedPromise = guardedPromise;
|
|
@@ -122,6 +123,17 @@ const IMAP4REV2_FOLDED_CAPABILITIES = new Set([
|
|
|
122
123
|
// Deliberate no-op, used as the observer a guarded promise attaches to its own rejection.
|
|
123
124
|
const noop = () => { };
|
|
124
125
|
exports.noop = noop;
|
|
126
|
+
/**
|
|
127
|
+
* Tells an IP literal from a hostname without `node:net`, which some runtimes do not implement.
|
|
128
|
+
* Only used to decide whether a host can be a TLS servername: a dotted quad, or anything with a
|
|
129
|
+
* colon (an IPv6 literal, a hostname never contains one).
|
|
130
|
+
*
|
|
131
|
+
* @param host - Hostname or IP address
|
|
132
|
+
* @returns true for an IP literal
|
|
133
|
+
*/
|
|
134
|
+
function isIpLiteral(host) {
|
|
135
|
+
return /^\d{1,3}(\.\d{1,3}){3}$/.test(host) || host.includes(':');
|
|
136
|
+
}
|
|
125
137
|
// The fields buildConnectionError() stamps to say *where* a connection error was rejected, as
|
|
126
138
|
// opposed to what went wrong. restampConnectionError() clears them, because they belong to the
|
|
127
139
|
// site that built the error rather than to the failure it describes.
|
package/dist/cjs/types.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { ConnectionOptions, CipherNameAndProtocol } from 'node:tls';
|
|
2
|
-
import type { Readable } from 'node:stream';
|
|
2
|
+
import type { Duplex, Readable } from 'node:stream';
|
|
3
3
|
/**
|
|
4
4
|
* Logger interface. Any object with `debug`, `info`, `warn` and `error` methods can be used,
|
|
5
5
|
* for example a pino or bunyan instance. `trace` and `fatal` are used when present.
|
|
@@ -95,6 +95,14 @@ export interface ImapFlowOptions {
|
|
|
95
95
|
* Node as-is. Proxy DNS and negotiation run inside `connectionTimeout`.
|
|
96
96
|
*/
|
|
97
97
|
proxy?: string | undefined;
|
|
98
|
+
/**
|
|
99
|
+
* Opens the connection through this transport instead of `node:net` and `node:tls`, for
|
|
100
|
+
* runtimes without those modules. `createSocketsTransport()` builds one from the
|
|
101
|
+
* `connect()` of the `cloudflare:sockets` API (celld, Cloudflare Workers),
|
|
102
|
+
* `createStreamsTransport()` from any socket API built on web streams. Can not be combined
|
|
103
|
+
* with `proxy`.
|
|
104
|
+
*/
|
|
105
|
+
transport?: ImapTransport | undefined;
|
|
98
106
|
/** If true, then use QRESYNC instead of CONDSTORE. EXPUNGE notifications will include UID instead of sequence number */
|
|
99
107
|
qresync?: boolean | undefined;
|
|
100
108
|
/** If set, then breaks and restarts IDLE every maxIdleTime ms */
|
|
@@ -882,3 +890,70 @@ export interface ImapFlowEvents {
|
|
|
882
890
|
/** The server sent an ALERT: text meant for the user, such as a planned shutdown or an account problem */
|
|
883
891
|
alert: [alert: AlertEvent];
|
|
884
892
|
}
|
|
893
|
+
/** Where {@link ImapTransport} connects to, and the `tls` options of the client */
|
|
894
|
+
export interface ImapTransportOptions {
|
|
895
|
+
host: string;
|
|
896
|
+
port: number;
|
|
897
|
+
/** The SNI name, absent for an IP literal host without the `servername` option */
|
|
898
|
+
servername?: string | undefined;
|
|
899
|
+
/** The `tls` option of the client */
|
|
900
|
+
tls?: ConnectionOptions | undefined;
|
|
901
|
+
}
|
|
902
|
+
/** What {@link ImapTransport.connect} gets: the destination, and whether to open a TLS connection */
|
|
903
|
+
export interface ImapTransportConnectOptions extends ImapTransportOptions {
|
|
904
|
+
/** Open a TLS connection (the `secure` option), otherwise a cleartext one */
|
|
905
|
+
secure: boolean;
|
|
906
|
+
}
|
|
907
|
+
/**
|
|
908
|
+
* The socket a {@link ImapTransport} returns: a Duplex stream of Buffers that emits `error`,
|
|
909
|
+
* `end` and `close`, and `timeout` once `setTimeout(ms)` was called and `ms` passed without
|
|
910
|
+
* traffic (0 disables it, like `net.Socket#setTimeout()`), the watchdog behind the
|
|
911
|
+
* `socketTimeout` option. The rest of what a `net.Socket` has is optional; ImapFlow uses it when
|
|
912
|
+
* it is there (the addresses for logging, the cipher of a TLS connection for `client.tls`).
|
|
913
|
+
*/
|
|
914
|
+
export type ImapTransportSocket = Duplex & {
|
|
915
|
+
setTimeout(ms: number): unknown;
|
|
916
|
+
setKeepAlive?: ((enable?: boolean, initialDelay?: number) => unknown) | undefined;
|
|
917
|
+
remoteAddress?: string | undefined;
|
|
918
|
+
remotePort?: number | undefined;
|
|
919
|
+
localAddress?: string | undefined;
|
|
920
|
+
localPort?: number | undefined;
|
|
921
|
+
getCipher?: (() => CipherNameAndProtocol | null) | undefined;
|
|
922
|
+
authorized?: boolean | undefined;
|
|
923
|
+
};
|
|
924
|
+
/**
|
|
925
|
+
* Opens and upgrades the socket carrying the session, see the `transport` option.
|
|
926
|
+
* `createStreamsTransport()` builds one over any socket API built on web streams,
|
|
927
|
+
* `createSocketsTransport()` over `cloudflare:sockets`.
|
|
928
|
+
*/
|
|
929
|
+
export interface ImapTransport {
|
|
930
|
+
/**
|
|
931
|
+
* Opens a connection, cleartext or TLS. Connection errors are emitted on the returned
|
|
932
|
+
* socket; a connection refused up front (an option the transport can not honor) may throw.
|
|
933
|
+
*
|
|
934
|
+
* @param options - Destination, TLS options and whether to open a TLS connection
|
|
935
|
+
* @param onConnect - Called once the connection (and the TLS handshake) is established
|
|
936
|
+
*/
|
|
937
|
+
connect(options: ImapTransportConnectOptions, onConnect: () => void): ImapTransportSocket;
|
|
938
|
+
/**
|
|
939
|
+
* Called right before the STARTTLS command is sent on the cleartext socket, so a transport
|
|
940
|
+
* that can only start TLS without a read in flight can stop reading once it is answered.
|
|
941
|
+
* STARTTLS is the only command in flight then: the next tagged response line (one that does
|
|
942
|
+
* not start with `* ` or `+ `) answers it, and if it is an OK, the server sends nothing more
|
|
943
|
+
* before the TLS handshake.
|
|
944
|
+
*
|
|
945
|
+
* @param socket - The cleartext socket returned by connect()
|
|
946
|
+
*/
|
|
947
|
+
prepareUpgrade?: ((socket: ImapTransportSocket) => void) | undefined;
|
|
948
|
+
/**
|
|
949
|
+
* Starts TLS on a connected cleartext socket after STARTTLS. May throw if the upgrade is
|
|
950
|
+
* refused before any handshake, handshake errors are emitted on the returned socket. A
|
|
951
|
+
* transport that can not start TLS leaves it out: ImapFlow then treats the server as one
|
|
952
|
+
* without STARTTLS (see the `doSTARTTLS` option).
|
|
953
|
+
*
|
|
954
|
+
* @param socket - The cleartext socket returned by connect()
|
|
955
|
+
* @param options - Destination and TLS options
|
|
956
|
+
* @param onSecure - Called once the TLS handshake is complete
|
|
957
|
+
*/
|
|
958
|
+
upgrade?: ((socket: ImapTransportSocket, options: ImapTransportOptions, onSecure: () => void) => ImapTransportSocket) | undefined;
|
|
959
|
+
}
|
package/dist/esm/errors.d.ts
CHANGED
|
@@ -20,6 +20,7 @@ export declare const ImapFlowErrorCode: {
|
|
|
20
20
|
readonly LockTimeout: "LockTimeout";
|
|
21
21
|
readonly ETHROTTLE: "ETHROTTLE";
|
|
22
22
|
readonly UnexpectedTag: "UnexpectedTag";
|
|
23
|
+
readonly MissingTag: "MissingTag";
|
|
23
24
|
readonly InvalidResponse: "InvalidResponse";
|
|
24
25
|
readonly ResponseProcessingFailed: "ResponseProcessingFailed";
|
|
25
26
|
readonly STARTTLS_INJECTION: "STARTTLS_INJECTION";
|
|
@@ -46,6 +47,7 @@ export declare const ImapFlowErrorCode: {
|
|
|
46
47
|
readonly EPROXY: "EPROXY";
|
|
47
48
|
readonly UnsupportedProxyAddress: "UnsupportedProxyAddress";
|
|
48
49
|
readonly ERR_INVALID_URL: "ERR_INVALID_URL";
|
|
50
|
+
readonly UnsupportedTransportOption: "UnsupportedTransportOption";
|
|
49
51
|
readonly InstanceReused: "InstanceReused";
|
|
50
52
|
};
|
|
51
53
|
/** One of the {@link ImapFlowErrorCode} values */
|
package/dist/esm/errors.js
CHANGED
|
@@ -22,6 +22,7 @@ export const ImapFlowErrorCode = {
|
|
|
22
22
|
// the server
|
|
23
23
|
ETHROTTLE: 'ETHROTTLE',
|
|
24
24
|
UnexpectedTag: 'UnexpectedTag',
|
|
25
|
+
MissingTag: 'MissingTag',
|
|
25
26
|
InvalidResponse: 'InvalidResponse',
|
|
26
27
|
ResponseProcessingFailed: 'ResponseProcessingFailed',
|
|
27
28
|
STARTTLS_INJECTION: 'STARTTLS_INJECTION',
|
|
@@ -53,6 +54,8 @@ export const ImapFlowErrorCode = {
|
|
|
53
54
|
EPROXY: 'EPROXY',
|
|
54
55
|
UnsupportedProxyAddress: 'UnsupportedProxyAddress',
|
|
55
56
|
ERR_INVALID_URL: 'ERR_INVALID_URL',
|
|
57
|
+
// the transport option
|
|
58
|
+
UnsupportedTransportOption: 'UnsupportedTransportOption',
|
|
56
59
|
// API misuse
|
|
57
60
|
InstanceReused: 'InstanceReused'
|
|
58
61
|
};
|
package/dist/esm/imap-flow.d.ts
CHANGED
|
@@ -1,15 +1,17 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @module imapflow
|
|
3
3
|
*/
|
|
4
|
-
import tls from 'node:tls';
|
|
5
|
-
import net from 'node:net';
|
|
6
4
|
import { EventEmitter } from 'node:events';
|
|
7
5
|
import { PassThrough, type Readable } from 'node:stream';
|
|
8
6
|
import { AuthenticationFailure } from './errors.js';
|
|
9
|
-
import type { AppendResponseObject, CopyResponseObject, DownloadManyOptions, DownloadManyResult, DownloadObject, DownloadNotFound, DownloadOptions, ESearchResult, FetchMessageObject, FetchOptions, FetchQueryObject, IdInfoObject, ImapFlowEvents, ImapFlowOptions, InternalLogger, ListOptions, ListResponse, ListTreeResponse, MailboxCreateResponse, MailboxDeleteResponse, MailboxLockObject, MailboxLockOptions, MailboxObject, MailboxOpenOptions, MailboxRenameResponse, MessageRange, MessageRangeOptions, NamespaceObject, NamespacesObject, QuotaResponse, SearchObject, SearchOptions, SearchReturnOption, SequenceString, StatusObject, StatusQuery, StoreOptions, TlsInfo } from './types.js';
|
|
7
|
+
import type { AppendResponseObject, CopyResponseObject, DownloadManyOptions, DownloadManyResult, DownloadObject, DownloadNotFound, DownloadOptions, ESearchResult, FetchMessageObject, FetchOptions, FetchQueryObject, IdInfoObject, ImapFlowEvents, ImapFlowOptions, InternalLogger, ListOptions, ListResponse, ListTreeResponse, MailboxCreateResponse, MailboxDeleteResponse, MailboxLockObject, MailboxLockOptions, MailboxObject, MailboxOpenOptions, MailboxRenameResponse, MessageRange, MessageRangeOptions, NamespaceObject, NamespacesObject, QuotaResponse, SearchObject, SearchOptions, SearchReturnOption, SequenceString, StatusObject, StatusQuery, StoreOptions, TlsInfo, ImapTransportSocket } from './types.js';
|
|
10
8
|
export type * from './types.js';
|
|
11
9
|
export type { ImapFlowError } from './errors.js';
|
|
12
10
|
export { AuthenticationFailure, ImapFlowErrorCode } from './errors.js';
|
|
11
|
+
export { createSocketsTransport } from './sockets-transport.js';
|
|
12
|
+
export type { SocketsApiSocket, SocketsConnect } from './sockets-transport.js';
|
|
13
|
+
export { createStreamsTransport } from './streams-transport.js';
|
|
14
|
+
export type { StreamsReader, StreamsSocket, StreamsSocketInfo, StreamsTlsInfo, StreamsTransportApi, StreamsWriter } from './streams-transport.js';
|
|
13
15
|
export type { ImapAttribute, ImapAttributeList, ImapAttributeNode, ImapResponse } from './handler/types.js';
|
|
14
16
|
declare const stateValues: {
|
|
15
17
|
readonly NOT_AUTHENTICATED: 1;
|
|
@@ -27,13 +29,11 @@ export type ConnectionStates = {
|
|
|
27
29
|
readonly [K in keyof typeof stateValues]: ConnectionState;
|
|
28
30
|
};
|
|
29
31
|
/**
|
|
30
|
-
* The socket carrying the IMAP session
|
|
31
|
-
*
|
|
32
|
+
* The socket carrying the IMAP session: a `net.Socket` or `tls.TLSSocket` on Node.js, or what a
|
|
33
|
+
* transport returns. Typed as {@link ImapTransportSocket}, so the client only relies on what
|
|
34
|
+
* every transport provides and checks for the optional members at runtime.
|
|
32
35
|
*/
|
|
33
|
-
export type ImapSocket =
|
|
34
|
-
getCipher?: (() => tls.CipherNameAndProtocol) | undefined;
|
|
35
|
-
authorized?: boolean | undefined;
|
|
36
|
-
};
|
|
36
|
+
export type ImapSocket = ImapTransportSocket;
|
|
37
37
|
/**
|
|
38
38
|
* Where outgoing bytes are written: the socket itself, or the PassThrough that feeds the
|
|
39
39
|
* DEFLATE stream once compression is active
|