@bernierllc/email-mitm-masking 1.3.0 → 1.6.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/dist/EmailMaskingService.js +3 -0
- package/dist/errors.d.ts +11 -0
- package/dist/errors.js +24 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +3 -1
- package/package.json +10 -6
- package/.eslintrc.js +0 -21
- package/CHANGELOG.md +0 -157
- package/TESTING.md +0 -237
- package/__tests__/EmailMaskingService.test.ts +0 -470
- package/__tests__/__fixtures__/test-config.ts +0 -24
- package/__tests__/__mocks__/database.ts +0 -252
- package/__tests__/setup.ts +0 -16
- package/jest.config.cjs +0 -28
- package/src/EmailMaskingService.ts +0 -425
- package/src/database.ts +0 -69
- package/src/index.ts +0 -20
- package/src/types.ts +0 -200
- package/tsconfig.json +0 -22
|
@@ -1,252 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
Copyright (c) 2025 Bernier LLC
|
|
3
|
-
|
|
4
|
-
This file is licensed to the client under a limited-use license.
|
|
5
|
-
The client may use and modify this code *only within the scope of the project it was delivered for*.
|
|
6
|
-
Redistribution or use in other products or commercial offerings is not permitted without written consent from Bernier LLC.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import type { ProxyAddressRow, RoutingAuditRow } from '../../src/types.js';
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* In-memory database mock for testing email masking service
|
|
13
|
-
*/
|
|
14
|
-
class MockDatabase {
|
|
15
|
-
private proxyAddresses: Map<string, ProxyAddressRow> = new Map();
|
|
16
|
-
private routingAudits: Map<string, RoutingAuditRow> = new Map();
|
|
17
|
-
private proxyEmailIndex: Map<string, string> = new Map(); // proxy_email -> id
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Reset all data (called between tests)
|
|
21
|
-
*/
|
|
22
|
-
reset(): void {
|
|
23
|
-
this.proxyAddresses.clear();
|
|
24
|
-
this.routingAudits.clear();
|
|
25
|
-
this.proxyEmailIndex.clear();
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Execute a SQL query (mocked)
|
|
30
|
-
*/
|
|
31
|
-
async query<T = any>(sql: string, params: any[] = []): Promise<{ rows: T[] }> {
|
|
32
|
-
const normalizedSql = sql.trim().toLowerCase();
|
|
33
|
-
|
|
34
|
-
// CREATE TABLE statements (schema initialization)
|
|
35
|
-
if (normalizedSql.includes('create table') || normalizedSql.includes('create index')) {
|
|
36
|
-
return { rows: [] };
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
// INSERT INTO proxy_addresses
|
|
40
|
-
if (normalizedSql.includes('insert into proxy_addresses')) {
|
|
41
|
-
const [
|
|
42
|
-
proxyEmail,
|
|
43
|
-
realEmail,
|
|
44
|
-
userId,
|
|
45
|
-
externalEmail,
|
|
46
|
-
conversationId,
|
|
47
|
-
status,
|
|
48
|
-
expiresAt,
|
|
49
|
-
metadata
|
|
50
|
-
] = params;
|
|
51
|
-
|
|
52
|
-
const id = this.generateUUID();
|
|
53
|
-
const now = new Date().toISOString();
|
|
54
|
-
|
|
55
|
-
const row: ProxyAddressRow = {
|
|
56
|
-
id,
|
|
57
|
-
proxy_email: proxyEmail,
|
|
58
|
-
real_email: realEmail,
|
|
59
|
-
user_id: userId,
|
|
60
|
-
external_email: externalEmail,
|
|
61
|
-
conversation_id: conversationId,
|
|
62
|
-
status,
|
|
63
|
-
created_at: now,
|
|
64
|
-
activated_at: null,
|
|
65
|
-
expires_at: expiresAt ? new Date(expiresAt).toISOString() : null,
|
|
66
|
-
last_used_at: null,
|
|
67
|
-
routing_count: '0',
|
|
68
|
-
metadata: metadata || '{}'
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
this.proxyAddresses.set(id, row);
|
|
72
|
-
this.proxyEmailIndex.set(proxyEmail, id);
|
|
73
|
-
|
|
74
|
-
return { rows: [row] as T[] };
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
// INSERT INTO routing_audit
|
|
78
|
-
if (normalizedSql.includes('insert into routing_audit')) {
|
|
79
|
-
const [
|
|
80
|
-
proxyId,
|
|
81
|
-
direction,
|
|
82
|
-
fromAddress,
|
|
83
|
-
toAddress,
|
|
84
|
-
success,
|
|
85
|
-
error,
|
|
86
|
-
metadata
|
|
87
|
-
] = params;
|
|
88
|
-
|
|
89
|
-
const id = this.generateUUID();
|
|
90
|
-
const now = new Date().toISOString();
|
|
91
|
-
|
|
92
|
-
const row: RoutingAuditRow = {
|
|
93
|
-
id,
|
|
94
|
-
proxy_id: proxyId,
|
|
95
|
-
direction,
|
|
96
|
-
from_address: fromAddress,
|
|
97
|
-
to_address: toAddress,
|
|
98
|
-
routed_at: now,
|
|
99
|
-
success,
|
|
100
|
-
error: error || null,
|
|
101
|
-
metadata: metadata || '{}'
|
|
102
|
-
};
|
|
103
|
-
|
|
104
|
-
this.routingAudits.set(id, row);
|
|
105
|
-
|
|
106
|
-
return { rows: [{ id }] as T[] };
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
// SELECT * FROM proxy_addresses WHERE id = $1
|
|
110
|
-
if (normalizedSql.includes('select * from proxy_addresses') && normalizedSql.includes('where id')) {
|
|
111
|
-
const [id] = params;
|
|
112
|
-
const row = this.proxyAddresses.get(id);
|
|
113
|
-
return { rows: row ? [row] as T[] : [] };
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
// SELECT * FROM proxy_addresses WHERE proxy_email = $1
|
|
117
|
-
if (normalizedSql.includes('select * from proxy_addresses') && normalizedSql.includes('where proxy_email')) {
|
|
118
|
-
const [proxyEmail] = params;
|
|
119
|
-
const id = this.proxyEmailIndex.get(proxyEmail);
|
|
120
|
-
const row = id ? this.proxyAddresses.get(id) : undefined;
|
|
121
|
-
return { rows: row ? [row] as T[] : [] };
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
// SELECT * FROM proxy_addresses WHERE user_id = $1 AND real_email = $2 AND external_email = $3
|
|
125
|
-
if (normalizedSql.includes('select * from proxy_addresses') &&
|
|
126
|
-
normalizedSql.includes('where user_id') &&
|
|
127
|
-
normalizedSql.includes('real_email') &&
|
|
128
|
-
normalizedSql.includes('external_email')) {
|
|
129
|
-
const [userId, realEmail, externalEmail] = params;
|
|
130
|
-
|
|
131
|
-
let matches = Array.from(this.proxyAddresses.values()).filter(row =>
|
|
132
|
-
row.user_id === userId &&
|
|
133
|
-
row.real_email === realEmail &&
|
|
134
|
-
row.external_email === externalEmail
|
|
135
|
-
);
|
|
136
|
-
|
|
137
|
-
// Sort by created_at DESC if ORDER BY is present
|
|
138
|
-
if (normalizedSql.includes('order by created_at desc')) {
|
|
139
|
-
matches.sort((a, b) => {
|
|
140
|
-
const dateA = new Date(a.created_at).getTime();
|
|
141
|
-
const dateB = new Date(b.created_at).getTime();
|
|
142
|
-
return dateB - dateA; // DESC order
|
|
143
|
-
});
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
return { rows: matches.slice(0, 1) as T[] }; // LIMIT 1
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
// SELECT COUNT(*) FROM proxy_addresses WHERE user_id = $1 AND status = 'active'
|
|
150
|
-
if (normalizedSql.includes('select count(*)') && normalizedSql.includes('from proxy_addresses')) {
|
|
151
|
-
const [userId] = params;
|
|
152
|
-
|
|
153
|
-
const count = Array.from(this.proxyAddresses.values()).filter(row =>
|
|
154
|
-
row.user_id === userId && row.status === 'active'
|
|
155
|
-
).length;
|
|
156
|
-
|
|
157
|
-
return { rows: [{ count: count.toString() }] as T[] };
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
// UPDATE proxy_addresses SET status = $1 WHERE id = $2
|
|
161
|
-
if (normalizedSql.includes('update proxy_addresses') &&
|
|
162
|
-
normalizedSql.includes('set status') &&
|
|
163
|
-
!normalizedSql.includes('routing_count')) {
|
|
164
|
-
const [status, id] = params;
|
|
165
|
-
const row = this.proxyAddresses.get(id);
|
|
166
|
-
|
|
167
|
-
if (row) {
|
|
168
|
-
row.status = status;
|
|
169
|
-
this.proxyAddresses.set(id, row);
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
return { rows: [] };
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
// UPDATE proxy_addresses SET routing_count = routing_count + 1, last_used_at = NOW() WHERE id = $1
|
|
176
|
-
if (normalizedSql.includes('update proxy_addresses') && normalizedSql.includes('routing_count')) {
|
|
177
|
-
const [id] = params;
|
|
178
|
-
const row = this.proxyAddresses.get(id);
|
|
179
|
-
|
|
180
|
-
if (row) {
|
|
181
|
-
row.routing_count = (parseInt(row.routing_count) + 1).toString();
|
|
182
|
-
row.last_used_at = new Date().toISOString();
|
|
183
|
-
this.proxyAddresses.set(id, row);
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
return { rows: [] };
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
// UPDATE proxy_addresses SET status = 'expired' WHERE expires_at < NOW() AND status = 'active'
|
|
190
|
-
if (normalizedSql.includes('update proxy_addresses') && normalizedSql.includes('expires_at < now()')) {
|
|
191
|
-
const now = new Date();
|
|
192
|
-
|
|
193
|
-
Array.from(this.proxyAddresses.values()).forEach(row => {
|
|
194
|
-
if (row.status === 'active' && row.expires_at) {
|
|
195
|
-
const expiresAt = new Date(row.expires_at);
|
|
196
|
-
if (expiresAt < now) {
|
|
197
|
-
row.status = 'expired';
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
});
|
|
201
|
-
|
|
202
|
-
return { rows: [] };
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
// Fallback for unhandled queries
|
|
206
|
-
console.warn('Unhandled mock query:', normalizedSql, params);
|
|
207
|
-
return { rows: [] };
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
/**
|
|
211
|
-
* Generate a UUID v4
|
|
212
|
-
*/
|
|
213
|
-
private generateUUID(): string {
|
|
214
|
-
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
|
|
215
|
-
const r = Math.random() * 16 | 0;
|
|
216
|
-
const v = c === 'x' ? r : (r & 0x3 | 0x8);
|
|
217
|
-
return v.toString(16);
|
|
218
|
-
});
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
// Singleton instance
|
|
223
|
-
const mockDb = new MockDatabase();
|
|
224
|
-
|
|
225
|
-
/**
|
|
226
|
-
* Mock Pool class that mimics pg.Pool
|
|
227
|
-
*/
|
|
228
|
-
export class Pool {
|
|
229
|
-
async query<T = any>(sql: string, params: any[] = []): Promise<{ rows: T[] }> {
|
|
230
|
-
return mockDb.query<T>(sql, params);
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
async end(): Promise<void> {
|
|
234
|
-
// No-op for mock
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
/**
|
|
239
|
-
* Initialize database schema (no-op in mock)
|
|
240
|
-
*/
|
|
241
|
-
export async function initializeDatabase(_pool: Pool): Promise<void> {
|
|
242
|
-
// Schema is implicitly created in memory
|
|
243
|
-
await mockDb.query('CREATE TABLE proxy_addresses');
|
|
244
|
-
await mockDb.query('CREATE TABLE routing_audit');
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
/**
|
|
248
|
-
* Reset mock database between tests
|
|
249
|
-
*/
|
|
250
|
-
export function resetMockDatabase(): void {
|
|
251
|
-
mockDb.reset();
|
|
252
|
-
}
|
package/__tests__/setup.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
Copyright (c) 2025 Bernier LLC
|
|
3
|
-
|
|
4
|
-
This file is licensed to the client under a limited-use license.
|
|
5
|
-
The client may use and modify this code *only within the scope of the project it was delivered for*.
|
|
6
|
-
Redistribution or use in other products or commercial offerings is not permitted without written consent from Bernier LLC.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import { resetMockDatabase } from './__mocks__/database.js';
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Reset mock database before each test
|
|
13
|
-
*/
|
|
14
|
-
beforeEach(() => {
|
|
15
|
-
resetMockDatabase();
|
|
16
|
-
});
|
package/jest.config.cjs
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
module.exports = {
|
|
2
|
-
preset: 'ts-jest',
|
|
3
|
-
// Use fixed environment to handle Node.js v25+ localStorage issue
|
|
4
|
-
testEnvironment: '<rootDir>/../../../jest-environment-node-fixed.cjs',
|
|
5
|
-
roots: ['<rootDir>/__tests__'],
|
|
6
|
-
testMatch: ['**/__tests__/**/*.test.ts'],
|
|
7
|
-
setupFilesAfterEnv: ['<rootDir>/__tests__/setup.ts'],
|
|
8
|
-
collectCoverageFrom: [
|
|
9
|
-
'src/**/*.{ts,tsx}',
|
|
10
|
-
'!src/**/*.d.ts',
|
|
11
|
-
'!src/index.ts'
|
|
12
|
-
],
|
|
13
|
-
coverageThreshold: {
|
|
14
|
-
global: {
|
|
15
|
-
branches: 85,
|
|
16
|
-
functions: 85,
|
|
17
|
-
lines: 85,
|
|
18
|
-
statements: 85
|
|
19
|
-
}
|
|
20
|
-
},
|
|
21
|
-
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json'],
|
|
22
|
-
moduleNameMapper: {
|
|
23
|
-
'^(\\.{1,2}/.*)\\.js$': '$1',
|
|
24
|
-
'^pg$': '<rootDir>/__tests__/__mocks__/database.ts',
|
|
25
|
-
'^../src/database\\.js$': '<rootDir>/__tests__/__mocks__/database.ts'
|
|
26
|
-
},
|
|
27
|
-
verbose: true
|
|
28
|
-
};
|