UNPKG

cf-imap

Version:

IMAP (v4) client for the Cloudflare Workers platform.

85 lines (84 loc) 3.12 kB
/// <reference types="@cloudflare/workers-types" /> import { connect } from "cloudflare:sockets"; import type { Email, FetchEmailsProps, SearchEmailsProps } from "./types/emails"; export type Options = { host: string; port: number; tls: boolean; auth: { username: string; password: string; }; }; export declare class CFImap { private options; constructor({ host, port, tls, auth }: Options); /** * Raw socket used to communicate with the IMAP server. Null if connect function not run yet. */ socket: ReturnType<typeof connect> | null; session: { id?: string; protocol?: string; }; /** * Only used to determine if a folder is selected */ selectedFolder: string; encoder: TextEncoder; decoder: TextDecoder; writer: WritableStreamDefaultWriter<any> | null; reader: ReadableStreamDefaultReader<any> | null; /** * Connects to the IMAP server. Must be run after initialising the CFImap class, otherwise nothing will work. * * @async * @returns {void} */ connect: () => Promise<void>; /** * Returns the prefix and hierarchy delimiter to personal and shared namespaces that the logged in user has access to. Should be the second ran function. */ getNamespaces: () => Promise<string[]>; /** * Returns all folders in the specified namespace along with any flags. * @param {string} namespace - From which namespace to list folders * @param {string} filter - String filter */ getFolders: (namespace: string, filter?: string) => Promise<{ name: string; delimiter: string; attributes: string[]; }[]>; /** * Selects a folder for use in the email GET & FETCH functions. Must be run before those commands, otherwise those commands will throw an error. * @param folder - Selectable folder */ selectFolder: (folder: string) => Promise<{ [key: string]: any; }>; /** * Fetches emails from a folder specified by the selectFolder() function. * * @async * @param {Object} props - Props * @param {number} [props.byteLimit] - Maximum size of the emails to fetch (optional, not recommended) * @param {[ number, number ]} props.limit - Range of emails to fetch. * @param {boolean} [props.peek=true] - If true (optional, defaults to true), upon fetch the emails won't get the \Seen flag set. */ fetchEmails: ({ folder, byteLimit, limit, peek, fetchBody }: FetchEmailsProps) => Promise<Email[]>; /** * Searches emails based on the props given. */ searchEmails: (props: SearchEmailsProps) => Promise<number[]>; /** * Requests a "checkpoint" on the server, a.k.a requests that the server does some houskeeping. * Almost never used, but exists in the RFC 3501 spec. * Removed in the RFC 9051 spec, however most providers still support it. */ check: () => Promise<string[]>; /** * Logs the user out of the IMAP session and closes the socket. */ logout: () => Promise<boolean>; }