@open-workspace/email 0.4.1 → 0.5.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/README.md +9 -4
- package/alchemy/index.ts +130 -2
- package/drizzle/mailbox/0002_good_komodo.sql +2 -0
- package/drizzle/mailbox/0003_after_0_4_2.sql +180 -0
- package/drizzle/mailbox/meta/0002_snapshot.json +1017 -0
- package/drizzle/mailbox/meta/0003_snapshot.json +1507 -0
- package/drizzle/mailbox/meta/_journal.json +14 -0
- package/drizzle/mailbox/migrations.js +5 -1
- package/migrations/0001_overrated_ricochet.sql +2 -0
- package/migrations/0002_after_0_4_2.sql +35 -0
- package/migrations/meta/0001_snapshot.json +564 -0
- package/migrations/meta/0002_snapshot.json +807 -0
- package/migrations/meta/_journal.json +14 -0
- package/package.json +23 -7
- package/shared/attachment-preview.ts +18 -8
- package/shared/folder-order.ts +31 -6
- package/shared/mail-headers.ts +3 -3
- package/shared/mail-html.ts +25 -0
- package/shared/mail-import.ts +0 -7
- package/shared/mail-received.ts +4 -4
- package/shared/mail-rule-actions.ts +47 -0
- package/shared/mail.ts +70 -4
- package/shared/office-preview.ts +0 -4
- package/shared/pdfjs-assets.ts +1 -3
- package/shared/rich-content.ts +35 -0
- package/shared/signature.ts +15 -0
- package/src/admin/address-auto-reply.tsx +49 -0
- package/src/admin/blocked-senders.tsx +86 -66
- package/src/admin/forward-destinations.tsx +140 -0
- package/src/admin/mail-archive.tsx +134 -0
- package/src/admin/team-signature.tsx +90 -0
- package/src/client.tsx +130 -16
- package/src/mail/attachment-preview-dialog.tsx +13 -5
- package/src/mail/auto-reply-banner.tsx +61 -0
- package/src/mail/blocked-senders.tsx +124 -91
- package/src/mail/compose-window-chrome.tsx +20 -8
- package/src/mail/compose-window.tsx +387 -365
- package/src/mail/composer-asset-bookmarks.ts +47 -12
- package/src/mail/composer-attachment-list.tsx +42 -17
- package/src/mail/composer-editor-document.ts +54 -10
- package/src/mail/composer-editor.tsx +44 -44
- package/src/mail/composer-link-text.ts +42 -0
- package/src/mail/composer-recipient-fields.tsx +25 -16
- package/src/mail/composer-recipients.ts +62 -16
- package/src/mail/composer-session.ts +107 -23
- package/src/mail/composer-toolbar.tsx +15 -1
- package/src/mail/composer-upload-client.ts +29 -1
- package/src/mail/composer-upload-progress.ts +2 -2
- package/src/mail/conversation-actions-model.ts +56 -0
- package/src/mail/conversation-list.tsx +263 -128
- package/src/mail/conversation-loading-skeleton.tsx +165 -90
- package/src/mail/conversation-message.tsx +257 -102
- package/src/mail/conversation-order.ts +12 -0
- package/src/mail/conversation-reply.tsx +313 -189
- package/src/mail/conversation-selection-toolbar.tsx +188 -143
- package/src/mail/conversation-view.tsx +320 -85
- package/src/mail/delivery-indicator.tsx +33 -12
- package/src/mail/draft-attachments.ts +194 -0
- package/src/mail/email-html-body.tsx +97 -53
- package/src/mail/folder-cleanup-bar.tsx +193 -0
- package/src/mail/folder-glyph.tsx +5 -0
- package/src/mail/folder-manager.tsx +71 -38
- package/src/mail/mail-app-bar.tsx +15 -3
- package/src/mail/mail-dock.tsx +19 -3
- package/src/mail/mail-focus.ts +48 -0
- package/src/mail/mail-folders.ts +14 -2
- package/src/mail/mail-layout.css +20 -0
- package/src/mail/mail-mailbox-sheet.tsx +22 -69
- package/src/mail/mail-nav-rail.tsx +7 -4
- package/src/mail/mail-nav.tsx +66 -46
- package/src/mail/mail-prefetch.ts +30 -9
- package/src/mail/mail-primitives.tsx +6 -4
- package/src/mail/mail-queries.ts +122 -20
- package/src/mail/mail-query-cache.ts +1085 -173
- package/src/mail/mail-search-params.ts +35 -0
- package/src/mail/mail-shell.tsx +32 -9
- package/src/mail/mailbox-ai-settings.tsx +56 -42
- package/src/mail/mailbox-realtime.tsx +4 -12
- package/src/mail/mailbox-rules.tsx +898 -0
- package/src/mail/message-actions.tsx +38 -26
- package/src/mail/message-attachments.tsx +20 -12
- package/src/mail/message-body.ts +53 -0
- package/src/mail/message-format.ts +8 -2
- package/src/mail/message-header.tsx +3 -1
- package/src/mail/message-translation.tsx +34 -67
- package/src/mail/original-message-sheet.tsx +63 -50
- package/src/mail/original-message.ts +6 -6
- package/src/mail/pane-splitter.tsx +3 -3
- package/src/mail/pdf-preview.tsx +70 -62
- package/src/mail/plain-message-text.tsx +28 -0
- package/src/mail/print-message.ts +31 -36
- package/src/mail/reader-pane.tsx +134 -102
- package/src/mail/recipient-combobox.tsx +52 -29
- package/src/mail/recipient-field-row.tsx +3 -0
- package/src/mail/rich-content.ts +128 -0
- package/src/mail/sanitize-email-html.ts +57 -33
- package/src/mail/sender-actions.tsx +49 -39
- package/src/mail/sender-authentication.tsx +1 -1
- package/src/mail/sender-dialogs.tsx +67 -43
- package/src/mail/sender-security.tsx +9 -10
- package/src/mail/signature.ts +43 -0
- package/src/mail/translation-html.ts +3 -3
- package/src/mail/types.ts +12 -0
- package/src/mail/use-composer-attachment-preflight.ts +23 -60
- package/src/mail/use-composer-core.ts +286 -0
- package/src/mail/use-composer-navigation.ts +120 -0
- package/src/mail/use-composer-submission.ts +154 -19
- package/src/mail/use-conversation-actions.ts +292 -0
- package/src/mail/use-conversation-position.ts +51 -0
- package/src/mail/use-conversation-selection.ts +192 -89
- package/src/mail/use-draft-autosave.ts +394 -0
- package/src/mail/use-drafts.ts +194 -0
- package/src/mail/use-mail-controller.ts +754 -213
- package/src/mail/use-mail-data.ts +210 -100
- package/src/mail/use-mail-safety.ts +19 -38
- package/src/mail/use-mail-send.ts +18 -6
- package/src/mail/use-pdf-translation.ts +1 -1
- package/src/mail/use-reply-cc.ts +61 -0
- package/src/mail/use-visible-message-read.ts +128 -64
- package/src/pwa/push-key.ts +18 -0
- package/src/pwa/push-subscription.ts +88 -26
- package/src/settings/auto-reply-editor.tsx +454 -0
- package/src/settings/auto-reply-page.tsx +59 -0
- package/src/settings/mail-apps-page.tsx +52 -43
- package/src/settings/notifications-page.tsx +82 -67
- package/src/settings/rich-content-editor.tsx +182 -0
- package/src/settings/settings-queries.ts +57 -0
- package/src/settings/signature-editor.tsx +142 -0
- package/src/settings/signature-page.tsx +70 -0
- package/worker/admin/blocked-sender-routes.ts +10 -16
- package/worker/backups/eml.ts +185 -0
- package/worker/backups/file-parts.ts +140 -0
- package/worker/backups/mail-backup.ts +644 -0
- package/worker/backups/settings-backup.ts +866 -0
- package/worker/backups/stream-eml.ts +73 -0
- package/worker/db/schema.ts +98 -2
- package/worker/jmap/attachment-blobs.ts +113 -0
- package/worker/jmap/blobs.ts +162 -0
- package/worker/jmap/draft-content.ts +42 -0
- package/worker/jmap/drafts.ts +70 -0
- package/worker/jmap/email-body.ts +152 -0
- package/worker/jmap/email-methods.ts +1223 -0
- package/worker/jmap/eventsource.ts +9 -8
- package/worker/jmap/import-methods.ts +205 -0
- package/worker/jmap/mailbox-methods.ts +187 -0
- package/worker/jmap/method-core.ts +379 -0
- package/worker/jmap/methods.ts +108 -1815
- package/worker/jmap/push-subscription-methods.ts +373 -0
- package/worker/jmap/push-types.ts +46 -2
- package/worker/jmap/push.ts +24 -31
- package/worker/jmap/routes.ts +144 -153
- package/worker/jmap/submission-methods.ts +395 -0
- package/worker/mail/access.ts +35 -12
- package/worker/mail/ai-classification.ts +2 -2
- package/worker/mail/archive-object.ts +96 -0
- package/worker/mail/archive-routes.ts +35 -0
- package/worker/mail/archive.ts +682 -0
- package/worker/mail/auth-headers.ts +48 -40
- package/worker/mail/auto-reply-routes.ts +255 -0
- package/worker/mail/auto-reply.ts +243 -0
- package/worker/mail/bimi.ts +53 -36
- package/worker/mail/bootstrap.ts +11 -3
- package/worker/mail/capabilities.ts +69 -0
- package/worker/mail/classification-signals.ts +1 -1
- package/worker/mail/confusables-data.ts +9 -0
- package/worker/mail/conversation-cursor.ts +6 -15
- package/worker/mail/delivery-events.ts +9 -7
- package/worker/mail/delivery-facts.ts +1 -1
- package/worker/mail/demo-routes.ts +3 -3
- package/worker/mail/doh.ts +3 -3
- package/worker/mail/download-links.ts +153 -0
- package/worker/mail/downloads.ts +2 -6
- package/worker/mail/draft-routes.ts +473 -0
- package/worker/mail/draft-sender.ts +54 -0
- package/worker/mail/edge-cache.ts +41 -55
- package/worker/mail/email-routing.ts +123 -0
- package/worker/mail/forward-destinations.ts +114 -0
- package/worker/mail/forward-downloads.ts +115 -0
- package/worker/mail/forward-html.ts +85 -0
- package/worker/mail/header-metadata.ts +2 -5
- package/worker/mail/impersonation.ts +102 -101
- package/worker/mail/import-routes.ts +29 -82
- package/worker/mail/import.ts +130 -0
- package/worker/mail/inbound-delivery.ts +6 -5
- package/worker/mail/inbound.ts +58 -28
- package/worker/mail/ip-intel.ts +8 -17
- package/worker/mail/isolate-cache.ts +1 -1
- package/worker/mail/mailbox-deletion.ts +33 -12
- package/worker/mail/mailbox-directory.ts +39 -65
- package/worker/mail/mailing-list.ts +29 -58
- package/worker/mail/message-security.ts +3 -3
- package/worker/mail/message-view.ts +12 -10
- package/worker/mail/office-conversion.ts +3 -5
- package/worker/mail/office-preview-container.ts +3 -8
- package/worker/mail/outbound-content.ts +139 -65
- package/worker/mail/outbound-delivery.ts +20 -17
- package/worker/mail/outbound-service.ts +34 -9
- package/worker/mail/outbound.ts +146 -75
- package/worker/mail/outgoing-context.ts +60 -30
- package/worker/mail/participants.ts +10 -15
- package/worker/mail/plain-body.ts +39 -0
- package/worker/mail/push-endpoint.ts +17 -0
- package/worker/mail/push-notifications.ts +87 -43
- package/worker/mail/quote-parser.ts +107 -25
- package/worker/mail/r2-presigned-urls.ts +7 -0
- package/worker/mail/receive.ts +146 -39
- package/worker/mail/recipients.ts +1 -1
- package/worker/mail/remote-proxy.ts +3 -3
- package/worker/mail/repeat.ts +1 -1
- package/worker/mail/reply-plan.ts +124 -26
- package/worker/mail/rfc.ts +15 -7
- package/worker/mail/rich-content.ts +207 -0
- package/worker/mail/routes.ts +696 -304
- package/worker/mail/routing.ts +274 -0
- package/worker/mail/rules.ts +230 -0
- package/worker/mail/safety-routes.ts +88 -80
- package/worker/mail/schemas.ts +222 -31
- package/worker/mail/search.ts +25 -2
- package/worker/mail/security-settings.ts +4 -10
- package/worker/mail/send-message.ts +238 -0
- package/worker/mail/sender-intel.ts +12 -20
- package/worker/mail/signature-routes.ts +201 -0
- package/worker/mail/signatures.ts +195 -0
- package/worker/mail/spamhaus.ts +28 -16
- package/worker/mail/storage-keys.ts +35 -0
- package/worker/mail/unsubscribe.ts +19 -41
- package/worker/mail/uploads.ts +193 -80
- package/worker/mail/webhook-source.ts +3 -2
- package/worker/mail/workspace-safety.ts +194 -60
- package/worker/mailbox/demo.ts +13 -15
- package/worker/mailbox/email-index.ts +92 -13
- package/worker/mailbox/folder-model.ts +0 -20
- package/worker/mailbox/import-placement.ts +21 -36
- package/worker/mailbox/inbound-decision.ts +114 -0
- package/worker/mailbox/inbox-counts.ts +45 -25
- package/worker/mailbox/index.ts +843 -2888
- package/worker/mailbox/jmap-change-log.ts +195 -0
- package/worker/mailbox/jmap-email-rows.ts +186 -0
- package/worker/mailbox/jmap-mailboxes.ts +116 -0
- package/worker/mailbox/jmap-placement.ts +139 -0
- package/worker/mailbox/jmap-query-filter.ts +231 -0
- package/worker/mailbox/jmap-store.ts +167 -299
- package/worker/mailbox/mailbox-auto-reply.ts +301 -0
- package/worker/mailbox/mailbox-backup.ts +538 -0
- package/worker/mailbox/mailbox-drafts.ts +417 -0
- package/worker/mailbox/mailbox-folders.ts +242 -0
- package/worker/mailbox/mailbox-import.ts +450 -0
- package/worker/mailbox/mailbox-inbound.ts +934 -0
- package/worker/mailbox/mailbox-jmap.ts +294 -0
- package/worker/mailbox/mailbox-outbound.ts +413 -0
- package/worker/mailbox/mailbox-rules.ts +336 -0
- package/worker/mailbox/mailbox-senders.ts +294 -0
- package/worker/mailbox/mailbox-storage.ts +438 -0
- package/worker/mailbox/mailbox-views.ts +891 -0
- package/worker/mailbox/model.ts +1 -0
- package/worker/mailbox/office-preview-jobs.ts +59 -47
- package/worker/mailbox/placement.ts +933 -0
- package/worker/mailbox/restore-thread.ts +52 -0
- package/worker/mailbox/retention.ts +36 -0
- package/worker/mailbox/schema.ts +211 -1
- package/worker/mailbox/senders.ts +216 -178
- package/worker/mailbox/spam-undo-filing.ts +138 -0
- package/worker/mailbox/spam-undo.ts +240 -0
- package/worker/mailbox/support.ts +416 -0
- package/worker/mcp/mail-action-tools.ts +156 -0
- package/worker/mcp/mail-compose-tools.ts +8 -2
- package/worker/mcp/mail-safety-tools.ts +4 -4
- package/worker/module.ts +23 -2
- package/src/mail/use-compose-submission.ts +0 -208
- package/src/mail/use-reply-submission.ts +0 -114
- package/worker/mail/capped-body.ts +0 -36
|
@@ -0,0 +1,379 @@
|
|
|
1
|
+
/** What every JMAP method shares: the request and its accounts, back-references, argument parsing and method errors. */
|
|
2
|
+
import {
|
|
3
|
+
describeIssues,
|
|
4
|
+
hashToken,
|
|
5
|
+
issueProperties,
|
|
6
|
+
} from "@open-workspace/core/module";
|
|
7
|
+
import type { Issue } from "@open-workspace/core/module";
|
|
8
|
+
import { z } from "zod";
|
|
9
|
+
|
|
10
|
+
import { MAIL_ORGANIZE_SCOPE, MAIL_SEND_SCOPE } from "../../shared/permissions";
|
|
11
|
+
import type { composeSchema } from "../mail/schemas";
|
|
12
|
+
import type { PushVapid } from "./push";
|
|
13
|
+
|
|
14
|
+
export const CORE = "urn:ietf:params:jmap:core";
|
|
15
|
+
export const MAX_OBJECTS = 500;
|
|
16
|
+
export const MAX_CALLS = 32;
|
|
17
|
+
|
|
18
|
+
export type JmapAccount = {
|
|
19
|
+
id: string;
|
|
20
|
+
address: string;
|
|
21
|
+
displayName: string;
|
|
22
|
+
ownerUserId: string | null;
|
|
23
|
+
isPrimary: boolean;
|
|
24
|
+
canSend: boolean;
|
|
25
|
+
isPersonal: boolean;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export function accountRights(owner: JmapAccount, scopes: readonly string[]) {
|
|
29
|
+
return {
|
|
30
|
+
organize: scopes.includes(MAIL_ORGANIZE_SCOPE),
|
|
31
|
+
submit: owner.canSend && scopes.includes(MAIL_SEND_SCOPE),
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Clients refresh the session when the accounts or their advertised rights change. */
|
|
36
|
+
export function sessionState(
|
|
37
|
+
accounts: JmapAccount[],
|
|
38
|
+
scopes: readonly string[],
|
|
39
|
+
vapidKey: string | null
|
|
40
|
+
) {
|
|
41
|
+
return hashToken(JSON.stringify([accounts, [...scopes].sort(), vapidKey]));
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export type JmapContext = {
|
|
45
|
+
env: Env;
|
|
46
|
+
user: { id: string; name: string };
|
|
47
|
+
/** The OAuth client the token was issued to; push subscriptions belong to it. */
|
|
48
|
+
clientId: string;
|
|
49
|
+
/** The token's mail scopes; each method needs its own (`scopes` of its definition). */
|
|
50
|
+
scopes: readonly string[];
|
|
51
|
+
/** Signs pushes to Web Push services; null when push is not configured. */
|
|
52
|
+
vapid: PushVapid | null;
|
|
53
|
+
accounts: JmapAccount[];
|
|
54
|
+
requestUrl: string;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
export type Json = z.core.util.JSONType;
|
|
58
|
+
|
|
59
|
+
export type JsonObject = Record<string, Json>;
|
|
60
|
+
|
|
61
|
+
/** RFC 8620 §3.3: a request is the capabilities it uses and its method calls. */
|
|
62
|
+
export const jmapRequestSchema = z.object({
|
|
63
|
+
using: z.array(z.string()),
|
|
64
|
+
methodCalls: z.array(
|
|
65
|
+
z.tuple([z.string(), z.record(z.string(), z.json()), z.string()])
|
|
66
|
+
),
|
|
67
|
+
createdIds: z.record(z.string(), z.string()).optional(),
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
export type JmapRequest = z.output<typeof jmapRequestSchema>;
|
|
71
|
+
|
|
72
|
+
export type Invocation = [string, JsonObject, string];
|
|
73
|
+
|
|
74
|
+
/** A request-level error (RFC 8620 §3.6.1), answered as a problem document. */
|
|
75
|
+
export class JmapError extends Error {
|
|
76
|
+
constructor(
|
|
77
|
+
readonly type: string,
|
|
78
|
+
message: string
|
|
79
|
+
) {
|
|
80
|
+
super(message);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** The method-level errors (RFC 8620 §3.6.2, RFC 8621) these methods answer with. */
|
|
85
|
+
const methodErrorTypes = z.enum([
|
|
86
|
+
"accountNotFound",
|
|
87
|
+
"anchorNotFound",
|
|
88
|
+
"cannotCalculateChanges",
|
|
89
|
+
"forbidden",
|
|
90
|
+
"invalidArguments",
|
|
91
|
+
"invalidResultReference",
|
|
92
|
+
"requestTooLarge",
|
|
93
|
+
"stateMismatch",
|
|
94
|
+
"tooManyChanges",
|
|
95
|
+
"unknownMethod",
|
|
96
|
+
"unsupportedFilter",
|
|
97
|
+
"unsupportedSort",
|
|
98
|
+
]);
|
|
99
|
+
|
|
100
|
+
type MethodErrorType = z.output<typeof methodErrorTypes>;
|
|
101
|
+
|
|
102
|
+
/** A method-level error, answered in place of the method's response. */
|
|
103
|
+
export class MethodError extends Error {
|
|
104
|
+
constructor(
|
|
105
|
+
readonly type: MethodErrorType,
|
|
106
|
+
message?: string
|
|
107
|
+
) {
|
|
108
|
+
super(message ?? type);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** Uploaded blobs are named after their upload: `upl~<upload id>`. */
|
|
113
|
+
export const UPLOAD_BLOB = /^upl~(upl_[a-f0-9]{32})$/u;
|
|
114
|
+
|
|
115
|
+
export const ATTACHMENT_BLOB = /^att~([^~]+)~([^~]+)$/u;
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* A draft from Email/set create; it lives only for the request that sends
|
|
119
|
+
* it. It holds the message as the mail app's composer would send it.
|
|
120
|
+
*/
|
|
121
|
+
export type Draft = {
|
|
122
|
+
accountId: string;
|
|
123
|
+
compose: z.output<typeof composeSchema>;
|
|
124
|
+
/** The message it answers, as Message-IDs (inReplyTo) or a kept draft's source. */
|
|
125
|
+
answers: { messageIds: string[]; emailId: string | null };
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
/** The Email property a composer field comes from. */
|
|
129
|
+
function draftProperty(field: string) {
|
|
130
|
+
if (field === "bodyText") return "textBody";
|
|
131
|
+
|
|
132
|
+
if (field === "bodyHtml") return "htmlBody";
|
|
133
|
+
|
|
134
|
+
return field;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* A composer issue, at the Email property it is about. Issues about the
|
|
139
|
+
* message as a whole are about its recipients.
|
|
140
|
+
*/
|
|
141
|
+
export function draftIssue(issue: Issue) {
|
|
142
|
+
const [field = "to", ...rest] = issue.path;
|
|
143
|
+
|
|
144
|
+
return { ...issue, path: [draftProperty(String(field)), ...rest] };
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export type RequestState = {
|
|
148
|
+
context: JmapContext;
|
|
149
|
+
responses: Invocation[];
|
|
150
|
+
createdIds: Map<string, string>;
|
|
151
|
+
drafts: Map<string, Draft>;
|
|
152
|
+
/** Responses a method adds after its own, e.g. an implicit Email/set. */
|
|
153
|
+
after: Invocation[];
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
/** Arrays and objects are the JSON values a pointer steps into. */
|
|
157
|
+
export function isJsonObject(value: Json): value is JsonObject {
|
|
158
|
+
return Object(value) === value && !Array.isArray(value);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// ---------------------------------------------------------------- references
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* RFC 8620 §3.7: a JSON Pointer where `*` maps over an array. A typed
|
|
165
|
+
* variable, as the recursion needs its result type.
|
|
166
|
+
*/
|
|
167
|
+
const evaluatePointer: (value: Json, path: string) => Json = (value, path) => {
|
|
168
|
+
if (path === "") return value;
|
|
169
|
+
|
|
170
|
+
const [head = "", ...rest] = path
|
|
171
|
+
.slice(1)
|
|
172
|
+
.split("/")
|
|
173
|
+
.map((part) => part.replaceAll("~1", "/").replaceAll("~0", "~"));
|
|
174
|
+
|
|
175
|
+
const tail = rest.length ? `/${rest.join("/")}` : "";
|
|
176
|
+
|
|
177
|
+
if (Array.isArray(value)) {
|
|
178
|
+
if (head === "*") {
|
|
179
|
+
return value.flatMap((item) => {
|
|
180
|
+
const result = evaluatePointer(item, tail);
|
|
181
|
+
|
|
182
|
+
return Array.isArray(result) ? result : [result];
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const item = /^\d+$/u.test(head) ? value[Number(head)] : undefined;
|
|
187
|
+
|
|
188
|
+
if (item !== undefined) return evaluatePointer(item, tail);
|
|
189
|
+
} else if (isJsonObject(value) && Object.hasOwn(value, head)) {
|
|
190
|
+
const property = value[head];
|
|
191
|
+
|
|
192
|
+
if (property !== undefined) return evaluatePointer(property, tail);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
throw new MethodError(
|
|
196
|
+
"invalidResultReference",
|
|
197
|
+
`Path ${path} does not resolve`
|
|
198
|
+
);
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
const resultReference = z.object({
|
|
202
|
+
resultOf: z.string(),
|
|
203
|
+
name: z.string(),
|
|
204
|
+
path: z.string(),
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
export function resolveReferences(state: RequestState, args: JsonObject) {
|
|
208
|
+
const resolved: JsonObject = {};
|
|
209
|
+
|
|
210
|
+
for (const [key, value] of Object.entries(args)) {
|
|
211
|
+
if (!key.startsWith("#")) {
|
|
212
|
+
resolved[key] = value;
|
|
213
|
+
continue;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
const name = key.slice(1);
|
|
217
|
+
|
|
218
|
+
if (Object.hasOwn(args, name)) {
|
|
219
|
+
throw new MethodError(
|
|
220
|
+
"invalidArguments",
|
|
221
|
+
`Both ${name} and #${name} given`
|
|
222
|
+
);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
const reference = resultReference.safeParse(value);
|
|
226
|
+
|
|
227
|
+
if (!reference.success) {
|
|
228
|
+
throw new MethodError(
|
|
229
|
+
"invalidResultReference",
|
|
230
|
+
describeIssues(reference.error.issues)
|
|
231
|
+
);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
const { resultOf, name: methodName, path } = reference.data;
|
|
235
|
+
|
|
236
|
+
const source = state.responses.find(
|
|
237
|
+
([response, , callId]) => callId === resultOf && response === methodName
|
|
238
|
+
);
|
|
239
|
+
|
|
240
|
+
if (!source) {
|
|
241
|
+
throw new MethodError(
|
|
242
|
+
"invalidResultReference",
|
|
243
|
+
`No ${methodName} result for ${resultOf}`
|
|
244
|
+
);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
resolved[name] = evaluatePointer(source[1], path);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
return resolved;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// ---------------------------------------------------------------- arguments
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Adds an issue that answers the method with `type` instead of
|
|
257
|
+
* invalidArguments: the arguments are well formed, but ask for something
|
|
258
|
+
* this server does not do.
|
|
259
|
+
*/
|
|
260
|
+
export function methodIssue(
|
|
261
|
+
context: z.RefinementCtx,
|
|
262
|
+
type: MethodErrorType,
|
|
263
|
+
message: string
|
|
264
|
+
) {
|
|
265
|
+
context.addIssue({ code: "custom", message, params: { methodError: type } });
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/** The method error an issue asks for, from `methodIssue`. */
|
|
269
|
+
function issueMethodError(issue: Issue) {
|
|
270
|
+
if (issue.code !== "custom") return null;
|
|
271
|
+
const type = methodErrorTypes.safeParse(issue.params?.methodError);
|
|
272
|
+
|
|
273
|
+
return type.success ? type.data : null;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/** A method's arguments, or the method error that answers them (RFC 8620 §3.6.2). */
|
|
277
|
+
export function parseArguments<Schema extends z.ZodType>(
|
|
278
|
+
schema: Schema,
|
|
279
|
+
args: JsonObject
|
|
280
|
+
) {
|
|
281
|
+
const parsed = schema.safeParse(args);
|
|
282
|
+
|
|
283
|
+
if (parsed.success) return parsed.data;
|
|
284
|
+
const { issues } = parsed.error;
|
|
285
|
+
|
|
286
|
+
for (const issue of issues) {
|
|
287
|
+
const type = issueMethodError(issue);
|
|
288
|
+
|
|
289
|
+
if (type) throw new MethodError(type, issue.message);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
throw new MethodError("invalidArguments", describeIssues(issues));
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
export const accountId = z.string();
|
|
296
|
+
|
|
297
|
+
/** Ids of objects to fetch or change; more than a request may hold is requestTooLarge. */
|
|
298
|
+
export const idList = z.array(z.string()).superRefine((ids, context) => {
|
|
299
|
+
if (ids.length > MAX_OBJECTS) {
|
|
300
|
+
methodIssue(context, "requestTooLarge", `At most ${MAX_OBJECTS} ids`);
|
|
301
|
+
}
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
export const properties = z.array(z.string()).nullish();
|
|
305
|
+
|
|
306
|
+
/** RFC 8620 §1.4 UTCDate; an offset is accepted too. */
|
|
307
|
+
export const utcDate = z.iso.datetime({ offset: true });
|
|
308
|
+
|
|
309
|
+
/** Objects to create or patches to apply, each checked on its own (SetError per id). */
|
|
310
|
+
export const setObjects = z.record(z.string(), z.json()).nullish();
|
|
311
|
+
|
|
312
|
+
export const changesArguments = z.object({
|
|
313
|
+
accountId,
|
|
314
|
+
sinceState: z.string(),
|
|
315
|
+
maxChanges: z.number().int().positive().nullish(),
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
/** A SetError for an object whose properties failed `issues` (RFC 8620 §5.3). */
|
|
319
|
+
export function invalidProperties(
|
|
320
|
+
issues: readonly Issue[],
|
|
321
|
+
options: { topLevel?: boolean } = {}
|
|
322
|
+
) {
|
|
323
|
+
const names = issueProperties(issues, options);
|
|
324
|
+
|
|
325
|
+
for (const issue of issues) {
|
|
326
|
+
if (issue.code === "unrecognized_keys") names.push(...issue.keys);
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
return {
|
|
330
|
+
type: "invalidProperties",
|
|
331
|
+
properties: [...new Set(names)],
|
|
332
|
+
description: describeIssues(issues),
|
|
333
|
+
};
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/** A failed step's SetError, beside the results it would have given. */
|
|
337
|
+
export function setError(error: JsonObject) {
|
|
338
|
+
return { error };
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/** The `created`/`updated`/`not…` map of a /set response: null when empty. */
|
|
342
|
+
export function setResult<Value extends Json>(entries: Map<string, Value>) {
|
|
343
|
+
return entries.size ? Object.fromEntries(entries) : null;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
// ------------------------------------------------------------------ helpers
|
|
347
|
+
|
|
348
|
+
/** The id a creation id stands for. */
|
|
349
|
+
export function createdId(state: RequestState, creationId: string) {
|
|
350
|
+
return state.createdIds.get(creationId);
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
export function account(state: RequestState, id: string) {
|
|
354
|
+
const found = state.context.accounts.find((item) => item.id === id);
|
|
355
|
+
|
|
356
|
+
if (!found) throw new MethodError("accountNotFound");
|
|
357
|
+
|
|
358
|
+
return found;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
export function pick<Value extends Json>(
|
|
362
|
+
object: Record<string, Value>,
|
|
363
|
+
wanted: readonly string[] | null | undefined
|
|
364
|
+
) {
|
|
365
|
+
if (!wanted) return object;
|
|
366
|
+
|
|
367
|
+
return Object.fromEntries(
|
|
368
|
+
Object.entries(object).filter(
|
|
369
|
+
([key]) => key === "id" || wanted.includes(key)
|
|
370
|
+
)
|
|
371
|
+
);
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
export const addresses = (
|
|
375
|
+
list: { address: string; name?: string | null }[] | null | undefined
|
|
376
|
+
) =>
|
|
377
|
+
list?.length
|
|
378
|
+
? list.map((item) => ({ name: item.name || null, email: item.address }))
|
|
379
|
+
: null;
|