Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ importScripts(
'flows/kiro/background/desktop-client.js',
'flows/kiro/background/desktop-authorize-runner.js',
'flows/kiro/background/publisher-kiro-rs.js',
'background/email-local-part-helpers.js',
'background/generated-email-helpers.js',
'background/signup-flow-helpers.js',
'background/mail-rule-registry.js',
Expand Down
18 changes: 14 additions & 4 deletions background/cloudmail-provider.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(function cloudMailProviderModule(root, factory) {
root.MultiPageBackgroundCloudMailProvider = factory();
})(typeof self !== 'undefined' ? self : globalThis, function createCloudMailProviderModule() {
root.MultiPageBackgroundCloudMailProvider = factory(root);
})(typeof self !== 'undefined' ? self : globalThis, function createCloudMailProviderModule(root = globalThis) {
function createCloudMailProvider(deps = {}) {
const {
addLog = async () => {},
Expand All @@ -18,6 +18,7 @@
normalizeCloudMailDomains,
normalizeCloudMailMailApiMessages,
persistRegistrationEmailState = null,
buildRandomNameDateTimeLocalPart = root.MultiPageEmailLocalPartHelpers?.buildRandomNameDateTimeLocalPart,
pickVerificationMessageWithTimeFallback,
setEmailState = async () => {},
setPersistentSettings = async () => {},
Expand Down Expand Up @@ -172,6 +173,12 @@
return chars.join('');
}

function buildCloudMailNameDateTimeLocalPart(date = new Date()) {
return typeof buildRandomNameDateTimeLocalPart === 'function'
? buildRandomNameDateTimeLocalPart(date)
: '';
}

async function fetchCloudMailAddress(state, options = {}) {
throwIfStopped();
const latestState = state || await getState();
Expand All @@ -180,8 +187,10 @@
requireToken: true,
requireDomain: true,
});
const requestedLocal = String(options.localPart || options.name || '').trim().toLowerCase()
|| generateCloudMailAliasLocalPart();
const explicitLocalPart = String(options.localPart || '').trim().toLowerCase();
const legacyNameLocalPart = String(options.name || '').trim().toLowerCase();
const nameDateTimeLocalPart = buildCloudMailNameDateTimeLocalPart(options.date);
const requestedLocal = explicitLocalPart || legacyNameLocalPart || nameDateTimeLocalPart || generateCloudMailAliasLocalPart();
const address = `${requestedLocal}@${ensuredConfig.domain}`.toLowerCase();
const payload = { list: [{ email: address }] };
try {
Expand Down Expand Up @@ -318,6 +327,7 @@
}

return {
buildCloudMailNameDateTimeLocalPart,
ensureCloudMailConfig,
ensureCloudMailToken,
fetchCloudMailAddress,
Expand Down
51 changes: 51 additions & 0 deletions background/email-local-part-helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
(function attachEmailLocalPartHelpers(root, factory) {
root.MultiPageEmailLocalPartHelpers = factory();
})(typeof self !== 'undefined' ? self : globalThis, function createEmailLocalPartHelpersModule() {
const ENGLISH_NAME_PREFIXES = [
'james', 'john', 'robert', 'michael', 'william', 'david', 'richard', 'joseph',
'thomas', 'charles', 'mary', 'patricia', 'jennifer', 'linda', 'elizabeth',
'barbara', 'susan', 'jessica', 'sarah', 'karen', 'daniel', 'matthew',
'anthony', 'mark', 'donald', 'steven', 'paul', 'andrew', 'joshua', 'kevin',
'brian', 'george', 'edward', 'ronald', 'timothy', 'jason', 'jeffrey', 'ryan',
'jacob', 'gary', 'nicholas', 'eric', 'jonathan', 'stephen', 'larry', 'justin',
'scott', 'brandon', 'benjamin', 'samuel', 'gregory', 'alexander', 'patrick',
'frank', 'raymond', 'jack', 'dennis', 'jerry', 'tyler', 'aaron', 'henry',
'douglas', 'peter', 'adam', 'zachary', 'nathan', 'walter', 'harold', 'kyle',
'carl', 'arthur', 'gerald', 'roger', 'alice', 'emma', 'olivia', 'sophia',
'isabella', 'mia', 'amelia', 'harper', 'evelyn', 'abigail', 'emily', 'ella',
'scarlett', 'grace', 'chloe', 'victoria', 'riley', 'aria', 'lily', 'nora',
];

function pickRandomEnglishNamePrefix() {
return ENGLISH_NAME_PREFIXES[Math.floor(Math.random() * ENGLISH_NAME_PREFIXES.length)] || 'james';
}

function formatDateTimeDigits(date = new Date()) {
const current = new Date(date);
if (Number.isNaN(current.getTime())) {
return '';
}
const year = String(current.getFullYear());
const month = String(current.getMonth() + 1).padStart(2, '0');
const day = String(current.getDate()).padStart(2, '0');
const hour = String(current.getHours()).padStart(2, '0');
const minute = String(current.getMinutes()).padStart(2, '0');
const second = String(current.getSeconds()).padStart(2, '0');
const millisecond = String(current.getMilliseconds()).padStart(3, '0');
return `${year}${month}${day}${hour}${minute}${second}${millisecond}`;
}

function buildRandomNameDateTimeLocalPart(date = new Date()) {
const dateTimeDigits = formatDateTimeDigits(date);
if (!dateTimeDigits) {
return '';
}
return `${pickRandomEnglishNamePrefix()}${dateTimeDigits}`;
}

return {
buildRandomNameDateTimeLocalPart,
formatDateTimeDigits,
pickRandomEnglishNamePrefix,
};
});
10 changes: 7 additions & 3 deletions background/generated-email-helpers.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(function attachGeneratedEmailHelpers(root, factory) {
root.MultiPageGeneratedEmailHelpers = factory();
})(typeof self !== 'undefined' ? self : globalThis, function createGeneratedEmailHelpersModule() {
root.MultiPageGeneratedEmailHelpers = factory(root);
})(typeof self !== 'undefined' ? self : globalThis, function createGeneratedEmailHelpersModule(root = globalThis) {
function createGeneratedEmailHelpers(deps = {}) {
const {
addLog,
Expand All @@ -23,6 +23,7 @@
normalizeEmailGenerator,
isGeneratedAliasProvider,
persistRegistrationEmailState = null,
buildRandomNameDateTimeLocalPart = root.MultiPageEmailLocalPartHelpers?.buildRandomNameDateTimeLocalPart,
reuseOrCreateTab,
sendToContentScript,
setEmailState,
Expand Down Expand Up @@ -158,7 +159,9 @@
requireAdminAuth: true,
requireDomain: true,
});
const requestedName = String(options.localPart || options.name || '').trim().toLowerCase() || generateCloudflareAliasLocalPart();
const requestedName = String(options.localPart || options.name || '').trim().toLowerCase()
|| buildRandomNameDateTimeLocalPart(options.date)
|| generateCloudflareAliasLocalPart();
const payload = {
enablePrefix: true,
enableRandomSubdomain: Boolean(config.useRandomSubdomain),
Expand Down Expand Up @@ -365,6 +368,7 @@
fetchCloudflareTempEmailAddress,
fetchDuckEmail,
fetchGeneratedEmail,
buildRandomNameDateTimeLocalPart,
generateCloudflareAliasLocalPart,
requestCloudflareTempEmailJson,
};
Expand Down
6 changes: 4 additions & 2 deletions tests/background-generated-email-module.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ const assert = require('node:assert/strict');
const fs = require('node:fs');

function loadGeneratedEmailHelpersApi() {
const localPartHelpersSource = fs.readFileSync('background/email-local-part-helpers.js', 'utf8');
const source = fs.readFileSync('background/generated-email-helpers.js', 'utf8');
const globalScope = {};
return new Function('self', `${source}; return self.MultiPageGeneratedEmailHelpers;`)(globalScope);
return new Function('self', `${localPartHelpersSource}; ${source}; return self.MultiPageGeneratedEmailHelpers;`)(globalScope);
}

test('background imports generated email helper module', () => {
const source = fs.readFileSync('background.js', 'utf8');
assert.match(source, /importScripts\([\s\S]*'background\/email-local-part-helpers\.js'/);
assert.match(source, /importScripts\([\s\S]*'background\/generated-email-helpers\.js'/);
});

Expand Down Expand Up @@ -481,7 +483,7 @@ test('generated email helper uses the regular temp email domain when random subd
name: requests[0].body.name,
domain: 'mail.example.com',
});
assert.match(requests[0].body.name, /^[a-z0-9]+$/);
assert.match(requests[0].body.name, /^[a-z]+[0-9]{17}$/);
});

test('generated email helper requests random subdomain creation while preserving the returned address', async () => {
Expand Down
54 changes: 54 additions & 0 deletions tests/cloudmail-provider.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const test = require('node:test');
const assert = require('node:assert/strict');

require('../background/email-local-part-helpers.js');
require('../background/cloudmail-provider.js');

function createProviderApi(options = {}) {
Expand Down Expand Up @@ -228,3 +229,56 @@ test('fetchCloudMailAddress preserves phone identity through the shared persiste
},
]);
});

test('fetchCloudMailAddress builds random english name plus current date-time local part by default', async () => {
const api = globalThis.MultiPageBackgroundCloudMailProvider.createCloudMailProvider({
addLog: async () => {},
buildCloudMailHeaders: () => ({}),
CLOUD_MAIL_DEFAULT_PAGE_SIZE: 20,
CLOUD_MAIL_GENERATOR: 'cloudmail',
CLOUD_MAIL_PROVIDER: 'cloudmail',
fetchImpl: async (url) => {
if (String(url).includes('/api/public/addUser')) {
return {
ok: true,
text: async () => JSON.stringify({ code: 200 }),
};
}
return {
ok: true,
text: async () => JSON.stringify({ code: 200, data: { token: 'token' } }),
};
},
getCloudMailTokenFromResponse: () => 'token',
getState: async () => ({}),
joinCloudMailUrl: (baseUrl, path) => `${baseUrl}${path}`,
normalizeCloudMailAddress: (value) => String(value || '').trim().toLowerCase(),
normalizeCloudMailBaseUrl: (value) => String(value || '').trim(),
normalizeCloudMailDomain: (value) => String(value || '').trim(),
normalizeCloudMailDomains: (values) => values || [],
normalizeCloudMailMailApiMessages: () => [],
persistRegistrationEmailState: async () => {},
pickVerificationMessageWithTimeFallback: () => ({
match: null,
usedRelaxedFilters: false,
usedTimeFallback: false,
}),
setEmailState: async () => {},
setPersistentSettings: async () => {},
sleepWithStop: async () => {},
throwIfStopped: () => {},
});

const state = {
cloudMailBaseUrl: 'https://mail.example.com',
cloudMailAdminEmail: 'admin@example.com',
cloudMailAdminPassword: 'secret',
cloudMailToken: 'token',
cloudMailDomain: 'example.com',
};
const email = await api.fetchCloudMailAddress(state, {
date: '2026-05-17T08:09:10.123',
});

assert.match(email, /^[a-z]+20260517080910123@example\.com$/);
});