@geekmidas/testkit 10.0.0-alpha.139 → 10.0.0-alpha.140
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/mailbox.cjs +20 -22
- package/dist/mailbox.cjs.map +1 -1
- package/dist/mailbox.d.cts +6 -12
- package/dist/mailbox.d.cts.map +1 -1
- package/dist/mailbox.d.mts +6 -12
- package/dist/mailbox.d.mts.map +1 -1
- package/dist/mailbox.mjs +20 -22
- package/dist/mailbox.mjs.map +1 -1
- package/package.json +13 -8
package/dist/mailbox.cjs
CHANGED
|
@@ -1,17 +1,5 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
2
|
//#region src/mailbox.ts
|
|
3
|
-
/**
|
|
4
|
-
* The mail a test sent, read from Mailpit.
|
|
5
|
-
*
|
|
6
|
-
* Locally, mail goes over real SMTP to Mailpit — the same client, the same
|
|
7
|
-
* message, only the host differs. A test reads what arrived through Mailpit's
|
|
8
|
-
* HTTP API, so a magic link is signed in with by opening the email that was
|
|
9
|
-
* actually sent rather than a token pulled from somewhere else.
|
|
10
|
-
*
|
|
11
|
-
* Mailpit has one inbox for everything, and transactions do not reach it, so a
|
|
12
|
-
* test keeps to its own mail by address: a unique recipient per test, read by
|
|
13
|
-
* `to:`, and cleared afterwards.
|
|
14
|
-
*/
|
|
15
3
|
/** How long `last()` waits for mail that is still on its way. */
|
|
16
4
|
const DEFAULT_TIMEOUT = 5e3;
|
|
17
5
|
const POLL_INTERVAL = 50;
|
|
@@ -24,7 +12,11 @@ function createMailbox(options) {
|
|
|
24
12
|
const query = `?query=${encodeURIComponent(`to:"${address}"`)}`;
|
|
25
13
|
const all = async () => {
|
|
26
14
|
const found = await json(await get(`/api/v1/search${query}`), inbox);
|
|
27
|
-
return Promise.all(found.messages.map(async ({ ID }) =>
|
|
15
|
+
return Promise.all(found.messages.map(async ({ ID }) => {
|
|
16
|
+
const response = await get(`/api/v1/message/${ID}/raw`);
|
|
17
|
+
if (!response.ok) throw new InboxUnreachable(inbox, response.status);
|
|
18
|
+
return email(ID, new Uint8Array(await response.arrayBuffer()));
|
|
19
|
+
}));
|
|
28
20
|
};
|
|
29
21
|
return {
|
|
30
22
|
all,
|
|
@@ -48,17 +40,23 @@ async function json(response, inbox) {
|
|
|
48
40
|
if (!response.ok) throw new InboxUnreachable(inbox, response.status);
|
|
49
41
|
return await response.json();
|
|
50
42
|
}
|
|
51
|
-
|
|
52
|
-
|
|
43
|
+
/** The message, through the one MIME parser everything that reads mail uses. */
|
|
44
|
+
async function email(id, raw) {
|
|
45
|
+
const { parseEmail } = await import("@geekmidas/emailkit/inbound");
|
|
46
|
+
const message = await parseEmail(raw);
|
|
47
|
+
const html = message.html ?? "";
|
|
48
|
+
const text = message.text ?? "";
|
|
49
|
+
const links = [...new Set([...html.matchAll(/href="(https?:\/\/[^"]+)"/g), ...text.matchAll(/(https?:\/\/[^\s<>"]+)/g)].map(([, url]) => decodeEntities(url)))];
|
|
53
50
|
return {
|
|
54
|
-
id
|
|
55
|
-
from: message.
|
|
56
|
-
to: message.
|
|
57
|
-
subject: message.
|
|
58
|
-
text
|
|
59
|
-
html
|
|
51
|
+
id,
|
|
52
|
+
from: message.from?.address ?? "",
|
|
53
|
+
to: message.to.map(({ address }) => address),
|
|
54
|
+
subject: message.subject ?? "",
|
|
55
|
+
text,
|
|
56
|
+
html,
|
|
60
57
|
links,
|
|
61
|
-
link: links[0]
|
|
58
|
+
link: links[0],
|
|
59
|
+
attachments: message.attachments
|
|
62
60
|
};
|
|
63
61
|
}
|
|
64
62
|
/** `&` in an HTML attribute is `&` in the URL it names. */
|
package/dist/mailbox.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mailbox.cjs","names":[],"sources":["../src/mailbox.ts"],"sourcesContent":["/**\n * The mail a test sent, read from Mailpit.\n *\n * Locally, mail goes over real SMTP to Mailpit — the same client, the same\n * message, only the host differs. A test reads what arrived through Mailpit's\n * HTTP API, so a magic link is signed in with by opening the email that was\n * actually sent rather than a token pulled from somewhere else.\n *\n * Mailpit has one inbox for everything, and transactions do not reach it, so a\n * test keeps to its own mail by address: a unique recipient per test, read by\n * `to:`, and cleared afterwards.\n */\n\n/** How long `last()` waits for mail that is still on its way. */\nconst DEFAULT_TIMEOUT = 5_000;\nconst POLL_INTERVAL = 50;\n\nexport interface MailboxOptions {\n\t/** Mailpit's HTTP address — what `gkm test` injects as `<ID>_INBOX_URL`. */\n\tinbox: string;\n\t/** The `fetch` to read it with. Defaults to the global one. */\n\tfetch?: typeof fetch;\n\t/** How long `last()` waits for mail to arrive. */\n\ttimeout?: number;\n}\n\n/** One email, as it arrived. */\nexport interface Email {\n\tid: string;\n\tfrom: string;\n\tto: string[];\n\tsubject: string;\n\ttext: string;\n\thtml: string;\n\t/** Every `http(s)` link in the email, in order. */\n\tlinks: string[];\n\t/** The first link — the one a sign-in or confirmation email is about. */\n\tlink: string | undefined;\n}\n\nexport interface Mailbox {\n\t/** The newest email to this address, waiting for one if none has arrived. */\n\tlast(): Promise<Email>;\n\t/** Every email to this address, newest first. */\n\tall(): Promise<Email[]>;\n\t/** Delete every email to this address. */\n\tclear(): Promise<void>;\n}\n\n/** A way to read the mail sent to one address. */\nexport function createMailbox(\n\toptions: MailboxOptions,\n): (address: string) => Mailbox {\n\tconst inbox = options.inbox.replace(/\\/$/, '');\n\tconst timeout = options.timeout ?? DEFAULT_TIMEOUT;\n\tconst get = (path: string, init?: RequestInit) =>\n\t\t(options.fetch ?? globalThis.fetch)(`${inbox}${path}`, init);\n\n\treturn (address) => {\n\t\tconst query = `?query=${encodeURIComponent(`to:\"${address}\"`)}`;\n\n\t\tconst all = async (): Promise<Email[]> => {\n\t\t\tconst found = await json<SearchResult>(\n\t\t\t\tawait get(`/api/v1/search${query}`),\n\t\t\t\tinbox,\n\t\t\t);\n\t\t\treturn Promise.all(\n\t\t\t\tfound.messages.map(async ({ ID })
|
|
1
|
+
{"version":3,"file":"mailbox.cjs","names":[],"sources":["../src/mailbox.ts"],"sourcesContent":["/**\n * The mail a test sent, read from Mailpit.\n *\n * Locally, mail goes over real SMTP to Mailpit — the same client, the same\n * message, only the host differs. A test reads what arrived through Mailpit's\n * HTTP API, so a magic link is signed in with by opening the email that was\n * actually sent rather than a token pulled from somewhere else.\n *\n * Mailpit has one inbox for everything, and transactions do not reach it, so a\n * test keeps to its own mail by address: a unique recipient per test, read by\n * `to:`, and cleared afterwards.\n */\n\nimport type { ReceivedAttachment } from '@geekmidas/emailkit/inbound';\n\n/** How long `last()` waits for mail that is still on its way. */\nconst DEFAULT_TIMEOUT = 5_000;\nconst POLL_INTERVAL = 50;\n\nexport interface MailboxOptions {\n\t/** Mailpit's HTTP address — what `gkm test` injects as `<ID>_INBOX_URL`. */\n\tinbox: string;\n\t/** The `fetch` to read it with. Defaults to the global one. */\n\tfetch?: typeof fetch;\n\t/** How long `last()` waits for mail to arrive. */\n\ttimeout?: number;\n}\n\n/** One email, as it arrived. */\nexport interface Email {\n\tid: string;\n\tfrom: string;\n\tto: string[];\n\tsubject: string;\n\ttext: string;\n\thtml: string;\n\t/** Every `http(s)` link in the email, in order. */\n\tlinks: string[];\n\t/** The first link — the one a sign-in or confirmation email is about. */\n\tlink: string | undefined;\n\t/**\n\t * Its files, read from the raw message by the parser an inbox's handler\n\t * uses — so what survives one survives the other.\n\t */\n\tattachments: ReceivedAttachment[];\n}\n\nexport interface Mailbox {\n\t/** The newest email to this address, waiting for one if none has arrived. */\n\tlast(): Promise<Email>;\n\t/** Every email to this address, newest first. */\n\tall(): Promise<Email[]>;\n\t/** Delete every email to this address. */\n\tclear(): Promise<void>;\n}\n\n/** A way to read the mail sent to one address. */\nexport function createMailbox(\n\toptions: MailboxOptions,\n): (address: string) => Mailbox {\n\tconst inbox = options.inbox.replace(/\\/$/, '');\n\tconst timeout = options.timeout ?? DEFAULT_TIMEOUT;\n\tconst get = (path: string, init?: RequestInit) =>\n\t\t(options.fetch ?? globalThis.fetch)(`${inbox}${path}`, init);\n\n\treturn (address) => {\n\t\tconst query = `?query=${encodeURIComponent(`to:\"${address}\"`)}`;\n\n\t\tconst all = async (): Promise<Email[]> => {\n\t\t\tconst found = await json<SearchResult>(\n\t\t\t\tawait get(`/api/v1/search${query}`),\n\t\t\t\tinbox,\n\t\t\t);\n\t\t\treturn Promise.all(\n\t\t\t\tfound.messages.map(async ({ ID }) => {\n\t\t\t\t\tconst response = await get(`/api/v1/message/${ID}/raw`);\n\t\t\t\t\tif (!response.ok) {\n\t\t\t\t\t\tthrow new InboxUnreachable(inbox, response.status);\n\t\t\t\t\t}\n\t\t\t\t\treturn email(ID, new Uint8Array(await response.arrayBuffer()));\n\t\t\t\t}),\n\t\t\t);\n\t\t};\n\n\t\treturn {\n\t\t\tall,\n\t\t\tasync last() {\n\t\t\t\tconst deadline = Date.now() + timeout;\n\t\t\t\tfor (;;) {\n\t\t\t\t\tconst [newest] = await all();\n\t\t\t\t\tif (newest) return newest;\n\t\t\t\t\tif (Date.now() >= deadline) throw new NoMail(address, timeout);\n\t\t\t\t\tawait new Promise((resolve) => setTimeout(resolve, POLL_INTERVAL));\n\t\t\t\t}\n\t\t\t},\n\t\t\tasync clear() {\n\t\t\t\tconst response = await get(`/api/v1/search${query}`, {\n\t\t\t\t\tmethod: 'DELETE',\n\t\t\t\t});\n\t\t\t\tif (!response.ok) {\n\t\t\t\t\tthrow new InboxUnreachable(inbox, response.status);\n\t\t\t\t}\n\t\t\t},\n\t\t};\n\t};\n}\n\ninterface SearchResult {\n\tmessages: { ID: string }[];\n}\n\nasync function json<T>(response: Response, inbox: string): Promise<T> {\n\tif (!response.ok) throw new InboxUnreachable(inbox, response.status);\n\treturn (await response.json()) as T;\n}\n\n/** The message, through the one MIME parser everything that reads mail uses. */\nasync function email(id: string, raw: Uint8Array): Promise<Email> {\n\tconst { parseEmail } = await import('@geekmidas/emailkit/inbound');\n\tconst message = await parseEmail(raw);\n\tconst html = message.html ?? '';\n\tconst text = message.text ?? '';\n\tconst links = [\n\t\t...new Set(\n\t\t\t[\n\t\t\t\t...html.matchAll(/href=\"(https?:\\/\\/[^\"]+)\"/g),\n\t\t\t\t...text.matchAll(/(https?:\\/\\/[^\\s<>\"]+)/g),\n\t\t\t].map(([, url]) => decodeEntities(url!)),\n\t\t),\n\t];\n\n\treturn {\n\t\tid,\n\t\tfrom: message.from?.address ?? '',\n\t\tto: message.to.map(({ address }) => address),\n\t\tsubject: message.subject ?? '',\n\t\ttext,\n\t\thtml,\n\t\tlinks,\n\t\tlink: links[0],\n\t\tattachments: message.attachments,\n\t};\n}\n\n/** `&` in an HTML attribute is `&` in the URL it names. */\nfunction decodeEntities(url: string): string {\n\treturn url.replaceAll('&', '&');\n}\n\n/** No email to an address arrived in time. */\nexport class NoMail extends Error {\n\tconstructor(\n\t\treadonly address: string,\n\t\treadonly timeout: number,\n\t) {\n\t\tsuper(\n\t\t\t`No email to ${address} arrived within ${timeout}ms. Check that the ` +\n\t\t\t\t`code under test sends it, and to that address.`,\n\t\t);\n\t\tthis.name = 'NoMail';\n\t}\n}\n\n/** Mailpit did not answer. */\nexport class InboxUnreachable extends Error {\n\tconstructor(\n\t\treadonly inbox: string,\n\t\treadonly status: number,\n\t) {\n\t\tsuper(\n\t\t\t`Mailpit at ${inbox} answered ${status}. Is it running? \\`gkm test\\` ` +\n\t\t\t\t`starts it when the app declares an Email construct.`,\n\t\t);\n\t\tthis.name = 'InboxUnreachable';\n\t}\n}\n"],"mappings":";;;AAgBA,MAAM,kBAAkB;AACxB,MAAM,gBAAgB;;AAwCtB,SAAgB,cACf,SAC+B;CAC/B,MAAM,QAAQ,QAAQ,MAAM,QAAQ,OAAO,EAAE;CAC7C,MAAM,UAAU,QAAQ,WAAW;CACnC,MAAM,OAAO,MAAc,UACzB,QAAQ,SAAS,WAAW,MAAA,CAAO,GAAG,QAAQ,QAAQ,IAAI;CAE5D,QAAQ,YAAY;EACnB,MAAM,QAAQ,UAAU,mBAAmB,O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,SAAS,yBAAyB,CAC3C,CAAC,CAAC,KAAK,GAAG,SAAS,eAAe,GAAI,CAAC,CACxC,CACD;CAEA,OAAO;EACN;EACA,MAAM,QAAQ,MAAM,WAAW;EAC/B,IAAI,QAAQ,GAAG,KAAK,EAAE,cAAc,OAAO;EAC3C,SAAS,QAAQ,WAAW;EAC5B;EACA;EACA;EACA,MAAM,MAAM;EACZ,aAAa,QAAQ;CACtB;AACD;;AAGA,SAAS,eAAe,KAAqB;CAC5C,OAAO,IAAI,WAAW,SAAS,GAAG;AACnC;;AAGA,IAAa,SAAb,cAA4B,MAAM;CAEvB;CACA;CAFV,YACC,SACA,SACC;EACD,MACC,eAAe,QAAQ,kBAAkB,QAAQ,kEAElD;EANS,KAAA,UAAA;EACA,KAAA,UAAA;EAMT,KAAK,OAAO;CACb;AACD;;AAGA,IAAa,mBAAb,cAAsC,MAAM;CAEjC;CACA;CAFV,YACC,OACA,QACC;EACD,MACC,cAAc,MAAM,YAAY,OAAO,kFAExC;EANS,KAAA,QAAA;EACA,KAAA,SAAA;EAMT,KAAK,OAAO;CACb;AACD"}
|
package/dist/mailbox.d.cts
CHANGED
|
@@ -1,16 +1,5 @@
|
|
|
1
|
+
import { ReceivedAttachment } from "@geekmidas/emailkit/inbound";
|
|
1
2
|
//#region src/mailbox.d.ts
|
|
2
|
-
/**
|
|
3
|
-
* The mail a test sent, read from Mailpit.
|
|
4
|
-
*
|
|
5
|
-
* Locally, mail goes over real SMTP to Mailpit — the same client, the same
|
|
6
|
-
* message, only the host differs. A test reads what arrived through Mailpit's
|
|
7
|
-
* HTTP API, so a magic link is signed in with by opening the email that was
|
|
8
|
-
* actually sent rather than a token pulled from somewhere else.
|
|
9
|
-
*
|
|
10
|
-
* Mailpit has one inbox for everything, and transactions do not reach it, so a
|
|
11
|
-
* test keeps to its own mail by address: a unique recipient per test, read by
|
|
12
|
-
* `to:`, and cleared afterwards.
|
|
13
|
-
*/
|
|
14
3
|
export interface MailboxOptions {
|
|
15
4
|
/** Mailpit's HTTP address — what `gkm test` injects as `<ID>_INBOX_URL`. */
|
|
16
5
|
inbox: string;
|
|
@@ -31,6 +20,11 @@ export interface Email {
|
|
|
31
20
|
links: string[];
|
|
32
21
|
/** The first link — the one a sign-in or confirmation email is about. */
|
|
33
22
|
link: string | undefined;
|
|
23
|
+
/**
|
|
24
|
+
* Its files, read from the raw message by the parser an inbox's handler
|
|
25
|
+
* uses — so what survives one survives the other.
|
|
26
|
+
*/
|
|
27
|
+
attachments: ReceivedAttachment[];
|
|
34
28
|
}
|
|
35
29
|
export interface Mailbox {
|
|
36
30
|
/** The newest email to this address, waiting for one if none has arrived. */
|
package/dist/mailbox.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mailbox.d.cts","names":[],"sources":["../src/mailbox.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"mailbox.d.cts","names":[],"sources":["../src/mailbox.ts"],"mappings":";;iBAmBiB;;EAEhB;;EAEA,eAAe;;EAEf;;;iBAIgB;EAChB;EACA;EACA;EACA;EACA;EACA;;EAEA;;EAEA;;;;;EAKA,aAAa;;iBAGG;;EAEhB,QAAQ,QAAQ;;EAEhB,OAAO,QAAQ;;EAEf,SAAS;;;wBAIM,cACf,SAAS,kBACN,oBAAoB;;qBA2FX,eAAe;WAEjB;WACA;EAFV,YACU,iBACA;;;qBAWE,yBAAyB;WAE3B;WACA;EAFV,YACU,eACA"}
|
package/dist/mailbox.d.mts
CHANGED
|
@@ -1,16 +1,5 @@
|
|
|
1
|
+
import { ReceivedAttachment } from "@geekmidas/emailkit/inbound";
|
|
1
2
|
//#region src/mailbox.d.ts
|
|
2
|
-
/**
|
|
3
|
-
* The mail a test sent, read from Mailpit.
|
|
4
|
-
*
|
|
5
|
-
* Locally, mail goes over real SMTP to Mailpit — the same client, the same
|
|
6
|
-
* message, only the host differs. A test reads what arrived through Mailpit's
|
|
7
|
-
* HTTP API, so a magic link is signed in with by opening the email that was
|
|
8
|
-
* actually sent rather than a token pulled from somewhere else.
|
|
9
|
-
*
|
|
10
|
-
* Mailpit has one inbox for everything, and transactions do not reach it, so a
|
|
11
|
-
* test keeps to its own mail by address: a unique recipient per test, read by
|
|
12
|
-
* `to:`, and cleared afterwards.
|
|
13
|
-
*/
|
|
14
3
|
export interface MailboxOptions {
|
|
15
4
|
/** Mailpit's HTTP address — what `gkm test` injects as `<ID>_INBOX_URL`. */
|
|
16
5
|
inbox: string;
|
|
@@ -31,6 +20,11 @@ export interface Email {
|
|
|
31
20
|
links: string[];
|
|
32
21
|
/** The first link — the one a sign-in or confirmation email is about. */
|
|
33
22
|
link: string | undefined;
|
|
23
|
+
/**
|
|
24
|
+
* Its files, read from the raw message by the parser an inbox's handler
|
|
25
|
+
* uses — so what survives one survives the other.
|
|
26
|
+
*/
|
|
27
|
+
attachments: ReceivedAttachment[];
|
|
34
28
|
}
|
|
35
29
|
export interface Mailbox {
|
|
36
30
|
/** The newest email to this address, waiting for one if none has arrived. */
|
package/dist/mailbox.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mailbox.d.mts","names":[],"sources":["../src/mailbox.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"mailbox.d.mts","names":[],"sources":["../src/mailbox.ts"],"mappings":";;iBAmBiB;;EAEhB;;EAEA,eAAe;;EAEf;;;iBAIgB;EAChB;EACA;EACA;EACA;EACA;EACA;;EAEA;;EAEA;;;;;EAKA,aAAa;;iBAGG;;EAEhB,QAAQ,QAAQ;;EAEhB,OAAO,QAAQ;;EAEf,SAAS;;;wBAIM,cACf,SAAS,kBACN,oBAAoB;;qBA2FX,eAAe;WAEjB;WACA;EAFV,YACU,iBACA;;;qBAWE,yBAAyB;WAE3B;WACA;EAFV,YACU,eACA"}
|
package/dist/mailbox.mjs
CHANGED
|
@@ -1,16 +1,4 @@
|
|
|
1
1
|
//#region src/mailbox.ts
|
|
2
|
-
/**
|
|
3
|
-
* The mail a test sent, read from Mailpit.
|
|
4
|
-
*
|
|
5
|
-
* Locally, mail goes over real SMTP to Mailpit — the same client, the same
|
|
6
|
-
* message, only the host differs. A test reads what arrived through Mailpit's
|
|
7
|
-
* HTTP API, so a magic link is signed in with by opening the email that was
|
|
8
|
-
* actually sent rather than a token pulled from somewhere else.
|
|
9
|
-
*
|
|
10
|
-
* Mailpit has one inbox for everything, and transactions do not reach it, so a
|
|
11
|
-
* test keeps to its own mail by address: a unique recipient per test, read by
|
|
12
|
-
* `to:`, and cleared afterwards.
|
|
13
|
-
*/
|
|
14
2
|
/** How long `last()` waits for mail that is still on its way. */
|
|
15
3
|
const DEFAULT_TIMEOUT = 5e3;
|
|
16
4
|
const POLL_INTERVAL = 50;
|
|
@@ -23,7 +11,11 @@ function createMailbox(options) {
|
|
|
23
11
|
const query = `?query=${encodeURIComponent(`to:"${address}"`)}`;
|
|
24
12
|
const all = async () => {
|
|
25
13
|
const found = await json(await get(`/api/v1/search${query}`), inbox);
|
|
26
|
-
return Promise.all(found.messages.map(async ({ ID }) =>
|
|
14
|
+
return Promise.all(found.messages.map(async ({ ID }) => {
|
|
15
|
+
const response = await get(`/api/v1/message/${ID}/raw`);
|
|
16
|
+
if (!response.ok) throw new InboxUnreachable(inbox, response.status);
|
|
17
|
+
return email(ID, new Uint8Array(await response.arrayBuffer()));
|
|
18
|
+
}));
|
|
27
19
|
};
|
|
28
20
|
return {
|
|
29
21
|
all,
|
|
@@ -47,17 +39,23 @@ async function json(response, inbox) {
|
|
|
47
39
|
if (!response.ok) throw new InboxUnreachable(inbox, response.status);
|
|
48
40
|
return await response.json();
|
|
49
41
|
}
|
|
50
|
-
|
|
51
|
-
|
|
42
|
+
/** The message, through the one MIME parser everything that reads mail uses. */
|
|
43
|
+
async function email(id, raw) {
|
|
44
|
+
const { parseEmail } = await import("@geekmidas/emailkit/inbound");
|
|
45
|
+
const message = await parseEmail(raw);
|
|
46
|
+
const html = message.html ?? "";
|
|
47
|
+
const text = message.text ?? "";
|
|
48
|
+
const links = [...new Set([...html.matchAll(/href="(https?:\/\/[^"]+)"/g), ...text.matchAll(/(https?:\/\/[^\s<>"]+)/g)].map(([, url]) => decodeEntities(url)))];
|
|
52
49
|
return {
|
|
53
|
-
id
|
|
54
|
-
from: message.
|
|
55
|
-
to: message.
|
|
56
|
-
subject: message.
|
|
57
|
-
text
|
|
58
|
-
html
|
|
50
|
+
id,
|
|
51
|
+
from: message.from?.address ?? "",
|
|
52
|
+
to: message.to.map(({ address }) => address),
|
|
53
|
+
subject: message.subject ?? "",
|
|
54
|
+
text,
|
|
55
|
+
html,
|
|
59
56
|
links,
|
|
60
|
-
link: links[0]
|
|
57
|
+
link: links[0],
|
|
58
|
+
attachments: message.attachments
|
|
61
59
|
};
|
|
62
60
|
}
|
|
63
61
|
/** `&` in an HTML attribute is `&` in the URL it names. */
|
package/dist/mailbox.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mailbox.mjs","names":[],"sources":["../src/mailbox.ts"],"sourcesContent":["/**\n * The mail a test sent, read from Mailpit.\n *\n * Locally, mail goes over real SMTP to Mailpit — the same client, the same\n * message, only the host differs. A test reads what arrived through Mailpit's\n * HTTP API, so a magic link is signed in with by opening the email that was\n * actually sent rather than a token pulled from somewhere else.\n *\n * Mailpit has one inbox for everything, and transactions do not reach it, so a\n * test keeps to its own mail by address: a unique recipient per test, read by\n * `to:`, and cleared afterwards.\n */\n\n/** How long `last()` waits for mail that is still on its way. */\nconst DEFAULT_TIMEOUT = 5_000;\nconst POLL_INTERVAL = 50;\n\nexport interface MailboxOptions {\n\t/** Mailpit's HTTP address — what `gkm test` injects as `<ID>_INBOX_URL`. */\n\tinbox: string;\n\t/** The `fetch` to read it with. Defaults to the global one. */\n\tfetch?: typeof fetch;\n\t/** How long `last()` waits for mail to arrive. */\n\ttimeout?: number;\n}\n\n/** One email, as it arrived. */\nexport interface Email {\n\tid: string;\n\tfrom: string;\n\tto: string[];\n\tsubject: string;\n\ttext: string;\n\thtml: string;\n\t/** Every `http(s)` link in the email, in order. */\n\tlinks: string[];\n\t/** The first link — the one a sign-in or confirmation email is about. */\n\tlink: string | undefined;\n}\n\nexport interface Mailbox {\n\t/** The newest email to this address, waiting for one if none has arrived. */\n\tlast(): Promise<Email>;\n\t/** Every email to this address, newest first. */\n\tall(): Promise<Email[]>;\n\t/** Delete every email to this address. */\n\tclear(): Promise<void>;\n}\n\n/** A way to read the mail sent to one address. */\nexport function createMailbox(\n\toptions: MailboxOptions,\n): (address: string) => Mailbox {\n\tconst inbox = options.inbox.replace(/\\/$/, '');\n\tconst timeout = options.timeout ?? DEFAULT_TIMEOUT;\n\tconst get = (path: string, init?: RequestInit) =>\n\t\t(options.fetch ?? globalThis.fetch)(`${inbox}${path}`, init);\n\n\treturn (address) => {\n\t\tconst query = `?query=${encodeURIComponent(`to:\"${address}\"`)}`;\n\n\t\tconst all = async (): Promise<Email[]> => {\n\t\t\tconst found = await json<SearchResult>(\n\t\t\t\tawait get(`/api/v1/search${query}`),\n\t\t\t\tinbox,\n\t\t\t);\n\t\t\treturn Promise.all(\n\t\t\t\tfound.messages.map(async ({ ID })
|
|
1
|
+
{"version":3,"file":"mailbox.mjs","names":[],"sources":["../src/mailbox.ts"],"sourcesContent":["/**\n * The mail a test sent, read from Mailpit.\n *\n * Locally, mail goes over real SMTP to Mailpit — the same client, the same\n * message, only the host differs. A test reads what arrived through Mailpit's\n * HTTP API, so a magic link is signed in with by opening the email that was\n * actually sent rather than a token pulled from somewhere else.\n *\n * Mailpit has one inbox for everything, and transactions do not reach it, so a\n * test keeps to its own mail by address: a unique recipient per test, read by\n * `to:`, and cleared afterwards.\n */\n\nimport type { ReceivedAttachment } from '@geekmidas/emailkit/inbound';\n\n/** How long `last()` waits for mail that is still on its way. */\nconst DEFAULT_TIMEOUT = 5_000;\nconst POLL_INTERVAL = 50;\n\nexport interface MailboxOptions {\n\t/** Mailpit's HTTP address — what `gkm test` injects as `<ID>_INBOX_URL`. */\n\tinbox: string;\n\t/** The `fetch` to read it with. Defaults to the global one. */\n\tfetch?: typeof fetch;\n\t/** How long `last()` waits for mail to arrive. */\n\ttimeout?: number;\n}\n\n/** One email, as it arrived. */\nexport interface Email {\n\tid: string;\n\tfrom: string;\n\tto: string[];\n\tsubject: string;\n\ttext: string;\n\thtml: string;\n\t/** Every `http(s)` link in the email, in order. */\n\tlinks: string[];\n\t/** The first link — the one a sign-in or confirmation email is about. */\n\tlink: string | undefined;\n\t/**\n\t * Its files, read from the raw message by the parser an inbox's handler\n\t * uses — so what survives one survives the other.\n\t */\n\tattachments: ReceivedAttachment[];\n}\n\nexport interface Mailbox {\n\t/** The newest email to this address, waiting for one if none has arrived. */\n\tlast(): Promise<Email>;\n\t/** Every email to this address, newest first. */\n\tall(): Promise<Email[]>;\n\t/** Delete every email to this address. */\n\tclear(): Promise<void>;\n}\n\n/** A way to read the mail sent to one address. */\nexport function createMailbox(\n\toptions: MailboxOptions,\n): (address: string) => Mailbox {\n\tconst inbox = options.inbox.replace(/\\/$/, '');\n\tconst timeout = options.timeout ?? DEFAULT_TIMEOUT;\n\tconst get = (path: string, init?: RequestInit) =>\n\t\t(options.fetch ?? globalThis.fetch)(`${inbox}${path}`, init);\n\n\treturn (address) => {\n\t\tconst query = `?query=${encodeURIComponent(`to:\"${address}\"`)}`;\n\n\t\tconst all = async (): Promise<Email[]> => {\n\t\t\tconst found = await json<SearchResult>(\n\t\t\t\tawait get(`/api/v1/search${query}`),\n\t\t\t\tinbox,\n\t\t\t);\n\t\t\treturn Promise.all(\n\t\t\t\tfound.messages.map(async ({ ID }) => {\n\t\t\t\t\tconst response = await get(`/api/v1/message/${ID}/raw`);\n\t\t\t\t\tif (!response.ok) {\n\t\t\t\t\t\tthrow new InboxUnreachable(inbox, response.status);\n\t\t\t\t\t}\n\t\t\t\t\treturn email(ID, new Uint8Array(await response.arrayBuffer()));\n\t\t\t\t}),\n\t\t\t);\n\t\t};\n\n\t\treturn {\n\t\t\tall,\n\t\t\tasync last() {\n\t\t\t\tconst deadline = Date.now() + timeout;\n\t\t\t\tfor (;;) {\n\t\t\t\t\tconst [newest] = await all();\n\t\t\t\t\tif (newest) return newest;\n\t\t\t\t\tif (Date.now() >= deadline) throw new NoMail(address, timeout);\n\t\t\t\t\tawait new Promise((resolve) => setTimeout(resolve, POLL_INTERVAL));\n\t\t\t\t}\n\t\t\t},\n\t\t\tasync clear() {\n\t\t\t\tconst response = await get(`/api/v1/search${query}`, {\n\t\t\t\t\tmethod: 'DELETE',\n\t\t\t\t});\n\t\t\t\tif (!response.ok) {\n\t\t\t\t\tthrow new InboxUnreachable(inbox, response.status);\n\t\t\t\t}\n\t\t\t},\n\t\t};\n\t};\n}\n\ninterface SearchResult {\n\tmessages: { ID: string }[];\n}\n\nasync function json<T>(response: Response, inbox: string): Promise<T> {\n\tif (!response.ok) throw new InboxUnreachable(inbox, response.status);\n\treturn (await response.json()) as T;\n}\n\n/** The message, through the one MIME parser everything that reads mail uses. */\nasync function email(id: string, raw: Uint8Array): Promise<Email> {\n\tconst { parseEmail } = await import('@geekmidas/emailkit/inbound');\n\tconst message = await parseEmail(raw);\n\tconst html = message.html ?? '';\n\tconst text = message.text ?? '';\n\tconst links = [\n\t\t...new Set(\n\t\t\t[\n\t\t\t\t...html.matchAll(/href=\"(https?:\\/\\/[^\"]+)\"/g),\n\t\t\t\t...text.matchAll(/(https?:\\/\\/[^\\s<>\"]+)/g),\n\t\t\t].map(([, url]) => decodeEntities(url!)),\n\t\t),\n\t];\n\n\treturn {\n\t\tid,\n\t\tfrom: message.from?.address ?? '',\n\t\tto: message.to.map(({ address }) => address),\n\t\tsubject: message.subject ?? '',\n\t\ttext,\n\t\thtml,\n\t\tlinks,\n\t\tlink: links[0],\n\t\tattachments: message.attachments,\n\t};\n}\n\n/** `&` in an HTML attribute is `&` in the URL it names. */\nfunction decodeEntities(url: string): string {\n\treturn url.replaceAll('&', '&');\n}\n\n/** No email to an address arrived in time. */\nexport class NoMail extends Error {\n\tconstructor(\n\t\treadonly address: string,\n\t\treadonly timeout: number,\n\t) {\n\t\tsuper(\n\t\t\t`No email to ${address} arrived within ${timeout}ms. Check that the ` +\n\t\t\t\t`code under test sends it, and to that address.`,\n\t\t);\n\t\tthis.name = 'NoMail';\n\t}\n}\n\n/** Mailpit did not answer. */\nexport class InboxUnreachable extends Error {\n\tconstructor(\n\t\treadonly inbox: string,\n\t\treadonly status: number,\n\t) {\n\t\tsuper(\n\t\t\t`Mailpit at ${inbox} answered ${status}. Is it running? \\`gkm test\\` ` +\n\t\t\t\t`starts it when the app declares an Email construct.`,\n\t\t);\n\t\tthis.name = 'InboxUnreachable';\n\t}\n}\n"],"mappings":";;AAgBA,MAAM,kBAAkB;AACxB,MAAM,gBAAgB;;AAwCtB,SAAgB,cACf,SAC+B;CAC/B,MAAM,QAAQ,QAAQ,MAAM,QAAQ,OAAO,EAAE;CAC7C,MAAM,UAAU,QAAQ,WAAW;CACnC,MAAM,OAAO,MAAc,UACzB,QAAQ,SAAS,WAAW,MAAA,CAAO,GAAG,QAAQ,QAAQ,IAAI;CAE5D,QAAQ,YAAY;EACnB,MAAM,QAAQ,UAAU,mBAAmB,OAAO,QAAQ,EAAE;EAE5D,MAAM,MAAM,YAA8B;GACzC,MAAM,QAAQ,MAAM,KACnB,MAAM,IAAI,iBAAiB,OAAO,GAClC,KACD;GACA,OAAO,QAAQ,IACd,MAAM,SAAS,IAAI,OAAO,EAAE,SAAS;IACpC,MAAM,WAAW,MAAM,IAAI,mBAAmB,GAAG,KAAK;IACtD,IAAI,CAAC,SAAS,IACb,MAAM,IAAI,iBAAiB,OAAO,SAAS,MAAM;IAElD,OAAO,MAAM,IAAI,IAAI,WAAW,MAAM,SAAS,YAAY,CAAC,CAAC;GAC9D,CAAC,CACF;EACD;EAEA,OAAO;GACN;GACA,MAAM,OAAO;IACZ,MAAM,WAAW,KAAK,IAAI,IAAI;IAC9B,SAAS;KACR,MAAM,CAAC,UAAU,MAAM,IAAI;KAC3B,IAAI,QAAQ,OAAO;K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mBAAb,cAAsC,MAAM;CAEjC;CACA;CAFV,YACC,OACA,QACC;EACD,MACC,cAAc,MAAM,YAAY,OAAO,kFAExC;EANS,KAAA,QAAA;EACA,KAAA,SAAA;EAMT,KAAK,OAAO;CACb;AACD"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@geekmidas/testkit",
|
|
3
|
-
"version": "10.0.0-alpha.
|
|
3
|
+
"version": "10.0.0-alpha.140",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -161,17 +161,18 @@
|
|
|
161
161
|
"dependencies": {
|
|
162
162
|
"@faker-js/faker": "~10.6.0",
|
|
163
163
|
"tough-cookie": "~6.0.2",
|
|
164
|
-
"@geekmidas/envkit": "^10.0.0-alpha.
|
|
165
|
-
"@geekmidas/logger": "^10.0.0-alpha.
|
|
166
|
-
"@geekmidas/services": "^10.0.0-alpha.
|
|
164
|
+
"@geekmidas/envkit": "^10.0.0-alpha.140",
|
|
165
|
+
"@geekmidas/logger": "^10.0.0-alpha.140",
|
|
166
|
+
"@geekmidas/services": "^10.0.0-alpha.140"
|
|
167
167
|
},
|
|
168
168
|
"devDependencies": {
|
|
169
169
|
"@better-auth/test-utils": "~1.7.6",
|
|
170
170
|
"@types/pg": "~8.23.1",
|
|
171
171
|
"better-auth": "~1.7.6",
|
|
172
|
-
"@geekmidas/envkit": "^10.0.0-alpha.
|
|
173
|
-
"@geekmidas/logger": "^10.0.0-alpha.
|
|
174
|
-
"@geekmidas/services": "^10.0.0-alpha.
|
|
172
|
+
"@geekmidas/envkit": "^10.0.0-alpha.140",
|
|
173
|
+
"@geekmidas/logger": "^10.0.0-alpha.140",
|
|
174
|
+
"@geekmidas/services": "^10.0.0-alpha.140",
|
|
175
|
+
"@geekmidas/emailkit": "^10.0.0-alpha.140"
|
|
175
176
|
},
|
|
176
177
|
"repository": {
|
|
177
178
|
"type": "git",
|
|
@@ -189,7 +190,8 @@
|
|
|
189
190
|
"db-errors": "~0.2.3",
|
|
190
191
|
"vitest": "~5.0.2",
|
|
191
192
|
"@types/aws-lambda": ">=8.10.163",
|
|
192
|
-
"better-auth": ">=1.7.5"
|
|
193
|
+
"better-auth": ">=1.7.5",
|
|
194
|
+
"@geekmidas/emailkit": "^10.0.0-alpha.140"
|
|
193
195
|
},
|
|
194
196
|
"peerDependenciesMeta": {
|
|
195
197
|
"kysely": {
|
|
@@ -215,6 +217,9 @@
|
|
|
215
217
|
},
|
|
216
218
|
"better-auth": {
|
|
217
219
|
"optional": true
|
|
220
|
+
},
|
|
221
|
+
"@geekmidas/emailkit": {
|
|
222
|
+
"optional": true
|
|
218
223
|
}
|
|
219
224
|
},
|
|
220
225
|
"scripts": {
|