4 Commits

Author SHA1 Message Date
jkunz 4b694bd33a v1.2.1
Default (tags) / security (push) Failing after 1s
Default (tags) / test (push) Failing after 1s
Default (tags) / release (push) Has been skipped
Default (tags) / metadata (push) Has been skipped
2026-05-01 16:59:04 +00:00
jkunz 13ca27db86 fix(imap client): harden IMAP client message processing and modernize test tooling 2026-05-01 16:59:04 +00:00
philkunz 98263058bf 1.2.0
Default (tags) / security (push) Failing after 1s
Default (tags) / test (push) Failing after 0s
Default (tags) / release (push) Has been skipped
Default (tags) / metadata (push) Has been skipped
2024-11-26 22:58:27 +01:00
philkunz 9c42210acd feat(core): Introduce ImapClient and ImapServer classes for enhanced IMAP support 2024-11-26 22:58:26 +01:00
14 changed files with 5301 additions and 3875 deletions
+44
View File
@@ -0,0 +1,44 @@
{
"@git.zone/cli": {
"projectType": "npm",
"module": {
"githost": "code.foss.global",
"gitscope": "push.rocks",
"gitrepo": "smartimap",
"shortDescription": "event-driven IMAP message parser",
"description": "A Node.js library for event-driven streaming and parsing of IMAP email messages.",
"npmPackagename": "@push.rocks/smartimap",
"license": "MIT",
"projectDomain": "push.rocks",
"keywords": [
"imap",
"email",
"streaming",
"mailparser",
"email-client",
"nodejs",
"event-driven",
"automation",
"message-stream",
"filter",
"inbox",
"connection",
"message processing"
]
},
"release": {
"registries": [
"https://verdaccio.lossless.digital",
"https://registry.npmjs.org"
],
"accessLevel": "public"
}
},
"@ship.zone/szci": {
"npmGlobalTools": [],
"npmRegistryUrl": "registry.npmjs.org"
},
"@git.zone/tsdoc": {
"legal": "\n## License and Legal Information\n\nThis repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository. \n\n**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.\n\n### Trademarks\n\nThis project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.\n\n### Company Information\n\nTask Venture Capital GmbH \nRegistered at District court Bremen HRB 35230 HB, Germany\n\nFor any legal inquiries or if you require further information, please contact us via email at hello@task.vc.\n\nBy using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.\n"
}
}
+16
View File
@@ -1,5 +1,21 @@
# Changelog
## 2026-05-01 - 1.2.1 - fix(imap client)
harden IMAP client message processing and modernize test tooling
- Guard against empty search results and missing message sources before parsing fetched messages.
- Handle non-Error exceptions safely in the IMAP server command response path.
- Migrate tests to @git.zone/tstest with node-specific test files and make the IMAP integration test opt-in via environment flag.
- Update package metadata, exports, TypeScript settings, and dependency versions to align with the current build setup.
## 2024-11-26 - 1.2.0 - feat(core)
Introduce ImapClient and ImapServer classes for enhanced IMAP support
- Implemented ImapClient class for managing IMAP connections and message retrieval.
- Implemented ImapServer class to simulate an IMAP server for testing.
- Added new tests for ImapClient and ImapServer to ensure reliability.
- Updated dependencies in package.json to latest versions.
## 2024-09-19 - 1.1.0 - feat(core)
Enhance package with detailed documentation and updated npm metadata
+19
View File
@@ -0,0 +1,19 @@
Copyright (c) 2026 Task Venture Capital GmbH <hello@task.vc>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
+13 -5
View File
@@ -1,10 +1,11 @@
{
"gitzone": {
"@git.zone/cli": {
"projectType": "npm",
"module": {
"githost": "code.foss.global",
"gitscope": "push.rocks",
"gitrepo": "smartimap",
"shortDescription": "event-driven IMAP message parser",
"description": "A Node.js library for event-driven streaming and parsing of IMAP email messages.",
"npmPackagename": "@push.rocks/smartimap",
"license": "MIT",
@@ -24,13 +25,20 @@
"connection",
"message processing"
]
},
"release": {
"registries": [
"https://verdaccio.lossless.digital",
"https://registry.npmjs.org"
],
"accessLevel": "public"
}
},
"npmci": {
"@ship.zone/szci": {
"npmGlobalTools": [],
"npmAccessLevel": "public"
"npmRegistryUrl": "registry.npmjs.org"
},
"tsdoc": {
"@git.zone/tsdoc": {
"legal": "\n## License and Legal Information\n\nThis repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository. \n\n**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.\n\n### Trademarks\n\nThis project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.\n\n### Company Information\n\nTask Venture Capital GmbH \nRegistered at District court Bremen HRB 35230 HB, Germany\n\nFor any legal inquiries or if you require further information, please contact us via email at hello@task.vc.\n\nBy using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.\n"
}
}
}
+17 -15
View File
@@ -1,31 +1,31 @@
{
"name": "@push.rocks/smartimap",
"version": "1.1.0",
"version": "1.2.1",
"private": false,
"description": "A Node.js library for event-driven streaming and parsing of IMAP email messages.",
"exports": {
".": "./dist_ts/index.js"
},
"main": "dist_ts/index.js",
"typings": "dist_ts/index.d.ts",
"type": "module",
"author": "Task Venture Capital GmbH",
"author": "Task Venture Capital GmbH <hello@task.vc>",
"license": "MIT",
"scripts": {
"test": "(tstest test/ --web)",
"build": "(tsbuild --web --allowimplicitany)",
"buildDocs": "(tsdoc)"
"test": "tstest test/",
"build": "tsbuild --web",
"buildDocs": "tsdoc"
},
"devDependencies": {
"@git.zone/tsbuild": "^2.1.25",
"@git.zone/tsbundle": "^2.0.5",
"@git.zone/tsrun": "^1.2.46",
"@git.zone/tstest": "^1.0.44",
"@push.rocks/tapbundle": "^5.3.0",
"@types/node": "^22.5.5"
"@git.zone/tsbuild": "^4.4.0",
"@git.zone/tsrun": "^2.0.3",
"@git.zone/tstest": "^3.6.3",
"@types/node": "^25.6.0"
},
"dependencies": {
"@types/imapflow": "^1.0.19",
"@types/mailparser": "^3.4.4",
"imapflow": "^1.0.164",
"mailparser": "^3.7.1"
"@types/mailparser": "^3.4.6",
"imapflow": "^1.3.3",
"mailparser": "^3.9.8"
},
"repository": {
"type": "git",
@@ -47,6 +47,8 @@
"dist_ts_web/**/*",
"assets/**/*",
"cli.js",
".smartconfig.json",
"license",
"npmextra.json",
"readme.md"
],
+4937 -3809
View File
File diff suppressed because it is too large Load Diff
+46
View File
@@ -0,0 +1,46 @@
import { tap } from '@git.zone/tstest/tapbundle';
import { TapNodeTools } from '@git.zone/tstest/tapbundle_serverside';
import * as smartimap from '../ts/index.js';
let testSmartImap: smartimap.ImapClient;
const tapNodeTools = new TapNodeTools(tap);
tap.test('smartimap', async () => {
if (process.env.SMARTIMAP_RUN_INTEGRATION_TESTS !== 'true') {
console.log('Skipping IMAP integration test. Set SMARTIMAP_RUN_INTEGRATION_TESTS=true to run it.');
return;
}
testSmartImap = new smartimap.ImapClient({
host: await tapNodeTools.getEnvVarOnDemand('IMAP_URL'),
port: 993,
secure: true,
auth: {
user: await tapNodeTools.getEnvVarOnDemand('IMAP_USER'),
pass: await tapNodeTools.getEnvVarOnDemand('IMAP_PASSWORD'),
},
mailbox: 'INBOX',
filter: { seen: true, to: await tapNodeTools.getEnvVarOnDemand('IMAP_USER'), },
});
testSmartImap.on('message', (message: smartimap.SmartImapMessage) => {
console.log(message.subject);
});
testSmartImap.on('error', (error: Error) => {
console.error(error);
});
testSmartImap.on('connected', () => {
console.log('Connected');
});
testSmartImap.on('disconnected', () => {
console.log('Disconnected');
});
await testSmartImap.connect();
await testSmartImap.disconnect();
});
export default tap.start();
+28
View File
@@ -0,0 +1,28 @@
import { tap } from '@git.zone/tstest/tapbundle';
import { ImapServer } from '../ts/classes.imapserver.js';
tap.test('imapserver', async () => {
// Example usage
const imapServer = new ImapServer();
imapServer.addUser('testuser', 'password');
imapServer.createInbox('testuser', 'INBOX');
imapServer.createInbox('testuser', 'Sent');
// Add a sample message
const testUser = imapServer.users.get('testuser')!;
const inbox = testUser.inboxes.get('INBOX')!;
inbox.messages.push({
id: '1',
subject: 'Welcome',
sender: 'no-reply@example.com',
recipient: 'testuser@example.com',
date: new Date(),
body: 'Welcome to your new IMAP inbox!',
});
// Start the server on port 143 (commonly used for IMAP)
// imapServer.start(143);
});
export default tap.start();
-39
View File
@@ -1,39 +0,0 @@
import { expect, expectAsync, tap } from '@push.rocks/tapbundle';
import { tapNodeTools } from '@push.rocks/tapbundle/node';
import * as smartimap from '../ts/index.js';
let testSmartImap: smartimap.SmartImap;
tap.test('smartimap', async () => {
testSmartImap = new smartimap.SmartImap({
host: await tapNodeTools.getEnvVarOnDemand('IMAP_URL'),
port: 993,
secure: true,
auth: {
user: await tapNodeTools.getEnvVarOnDemand('IMAP_USER'),
pass: await tapNodeTools.getEnvVarOnDemand('IMAP_PASSWORD'),
},
mailbox: 'buchhaltung',
filter: { seen: true, to: await tapNodeTools.getEnvVarOnDemand('IMAP_USER'), },
});
await testSmartImap.connect();
testSmartImap.on('message', (message) => {
console.log(message);
});
testSmartImap.on('error', (error) => {
console.error(error);
});
testSmartImap.on('connected', () => {
console.log('Connected');
});
testSmartImap.on('disconnected', () => {
console.log('Disconnected');
});
});
tap.start();
+1 -1
View File
@@ -3,6 +3,6 @@
*/
export const commitinfo = {
name: '@push.rocks/smartimap',
version: '1.1.0',
version: '1.2.1',
description: 'A Node.js library for event-driven streaming and parsing of IMAP email messages.'
}
@@ -1,6 +1,6 @@
import * as plugins from './smartimap.plugins.js';
export interface SmartImapConfig {
export interface ImapClientConfig {
host: string;
port?: number; // Defaults to 993 if secure, else 143
secure?: boolean; // Defaults to true
@@ -12,7 +12,9 @@ export interface SmartImapConfig {
filter?: plugins.imapflow.SearchObject; // IMAP search criteria object
}
export class SmartImap extends plugins.events.EventEmitter {
export type SmartImapMessage = plugins.mailparser.ParsedMail;
export class ImapClient extends plugins.events.EventEmitter {
private client: plugins.imapflow.ImapFlow;
private mailbox: string;
private filter: plugins.imapflow.SearchObject;
@@ -20,7 +22,7 @@ export class SmartImap extends plugins.events.EventEmitter {
private processing: boolean = false;
private seenUids: Set<number> = new Set();
constructor(private config: SmartImapConfig) {
constructor(private config: ImapClientConfig) {
super();
this.mailbox = config.mailbox || 'INBOX';
@@ -66,6 +68,9 @@ export class SmartImap extends plugins.events.EventEmitter {
try {
const searchResult = await this.client.search(this.filter);
if (!searchResult) {
return;
}
// Convert searchResult to a regular array
const searchResultArray = Array.from(searchResult);
@@ -78,6 +83,9 @@ export class SmartImap extends plugins.events.EventEmitter {
// Fetch messages
for await (const message of this.client.fetch(newUids, { envelope: true, source: true })) {
if (!message.source) {
continue;
}
const parsed = await plugins.mailparser.simpleParser(message.source);
this.emit('message', parsed);
this.seenUids.add(message.uid);
@@ -100,4 +108,4 @@ export class SmartImap extends plugins.events.EventEmitter {
this.emit('disconnected');
}
}
}
}
+163
View File
@@ -0,0 +1,163 @@
import * as net from "net";
export interface IImapServerMessage {
id: string;
subject: string;
sender: string;
recipient: string;
date: Date;
body: string;
}
export interface IImapServerInbox {
name: string;
messages: IImapServerMessage[];
}
export interface IImapServerUser {
username: string;
password: string;
inboxes: Map<string, IImapServerInbox>;
}
export class ImapServer {
public users: Map<string, IImapServerUser>;
private server: net.Server;
constructor() {
this.users = new Map();
this.server = net.createServer(this.handleConnection.bind(this));
}
// Add a user for authentication
public addUser(username: string, password: string): void {
if (this.users.has(username)) {
throw new Error(`User "${username}" already exists.`);
}
this.users.set(username, { username, password, inboxes: new Map() });
}
// Add an inbox for a user
public createInbox(username: string, inboxName: string): void {
const user = this.users.get(username);
if (!user) {
throw new Error(`User "${username}" does not exist.`);
}
if (user.inboxes.has(inboxName)) {
throw new Error(`Inbox "${inboxName}" already exists for user "${username}".`);
}
user.inboxes.set(inboxName, { name: inboxName, messages: [] });
}
// Start the server
public start(port: number): void {
this.server.listen(port, () => {
console.log(`IMAP Server started on port ${port}`);
});
}
// Stop the server
public stop(): void {
this.server.close(() => {
console.log("IMAP Server stopped.");
});
}
// Handle a new client connection
private handleConnection(socket: net.Socket): void {
let currentUser: IImapServerUser | null = null;
let selectedInbox: IImapServerInbox | null = null;
socket.write("* OK IMAP4rev1 Service Ready\r\n");
socket.on("data", (data) => {
const command = data.toString().trim();
console.log(`Received command: ${command}`);
const [tag, keyword, ...args] = command.split(" ");
let response = "";
try {
switch (keyword.toUpperCase()) {
case "LOGIN": {
const [username, password] = args;
const user = this.users.get(username);
if (user && user.password === password) {
currentUser = user;
response = `${tag} OK LOGIN completed`;
} else {
response = `${tag} NO LOGIN failed`;
}
break;
}
case "LIST": {
if (!currentUser) {
response = `${tag} NO Not authenticated`;
break;
}
const inboxNames = Array.from(currentUser.inboxes.keys()).map((inbox) => `* LIST () "/" ${inbox}`);
response = `${inboxNames.join("\r\n")}\r\n${tag} OK LIST completed`;
break;
}
case "SELECT": {
if (!currentUser) {
response = `${tag} NO Not authenticated`;
break;
}
const inboxName = args[0];
const inbox = currentUser.inboxes.get(inboxName);
if (inbox) {
selectedInbox = inbox;
response = `* FLAGS (\\Answered \\Flagged \\Deleted \\Seen \\Draft)\r\n* EXISTS ${inbox.messages.length}\r\n${tag} OK [READ-WRITE] SELECT completed`;
} else {
response = `${tag} NO SELECT failed: No such mailbox`;
}
break;
}
case "FETCH": {
if (!selectedInbox) {
response = `${tag} NO No mailbox selected`;
break;
}
const [id] = args;
const message = selectedInbox.messages.find((msg) => msg.id === id);
if (message) {
response = `* ${id} FETCH (BODY[TEXT] {${message.body.length}}\r\n${message.body}\r\n)\r\n${tag} OK FETCH completed`;
} else {
response = `${tag} NO FETCH failed: No such message`;
}
break;
}
case "LOGOUT": {
response = `* BYE IMAP4rev1 Server logging out\r\n${tag} OK LOGOUT completed`;
socket.write(response + "\r\n");
socket.end();
return;
}
default: {
response = `${tag} BAD Unknown command`;
break;
}
}
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
response = `${tag} BAD Error: ${errorMessage}`;
}
socket.write(response + "\r\n");
});
socket.on("close", () => {
console.log("Client disconnected.");
});
socket.on("error", (err) => {
console.error("Socket error:", err);
});
}
}
+2 -1
View File
@@ -1 +1,2 @@
export * from './classes.smartimap.js';
export * from './classes.imapclient.js';
export * from './classes.imapserver.js';
+3 -1
View File
@@ -5,8 +5,10 @@
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"noImplicitAny": true,
"esModuleInterop": true,
"verbatimModuleSyntax": true
"verbatimModuleSyntax": true,
"types": ["node"]
},
"exclude": [
"dist_*/**/*.d.ts"