runline 0.36.0 → 0.38.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.
@@ -1,9 +1,7 @@
1
- import { basename } from "node:path";
2
1
  import { AuthError, credentialRequest, jsonAnswer, pathSegment, responseFailed, } from "runline";
3
2
  import * as t from "typebox";
4
3
  import { Check } from "typebox/value";
5
- import { standardBase64 } from "../../_shared/base64.js";
6
- import { readLocalFile } from "../../_shared/localFile.js";
4
+ import { attachmentFile } from "../../_shared/attachments.js";
7
5
  import { obj } from "../../_shared/provider.js";
8
6
  import { agentMailCredential } from "./credentials.js";
9
7
  import { id, MAX_REQUEST_BYTES } from "./schemas.js";
@@ -32,59 +30,39 @@ function wireFields(fields) {
32
30
  }
33
31
  /** The bytes a request body can still hold once base64 inflates them by 4/3. */
34
32
  const MAX_FILE_BYTES = Math.floor((MAX_REQUEST_BYTES * 3) / 4);
35
- function base64Content(value, index) {
36
- const content = standardBase64(value);
37
- if (content === undefined)
38
- throw new Error(`agentMail: attachment ${index} contentBase64 is not valid base64`);
39
- return content;
40
- }
41
- /** One attachment's base64 bytes, or its URL for AgentMail to fetch. */
42
- async function attachmentSource(input, index) {
43
- if ("contentBase64" in input) {
44
- const value = input.contentBase64;
45
- return {
46
- content: base64Content(typeof value === "string" ? value : value.contentBase64, index),
47
- };
33
+ /**
34
+ * One attachment as AgentMail's SendAttachment: its bytes as base64, or
35
+ * its URL for AgentMail to fetch.
36
+ */
37
+ async function wireAttachment(input, index) {
38
+ let source;
39
+ let file;
40
+ if ("url" in input) {
41
+ try {
42
+ const url = new URL(input.url);
43
+ if (url.protocol !== "https:" || url.username || url.password)
44
+ throw new Error();
45
+ }
46
+ catch {
47
+ throw new Error(`agentMail: attachment ${index} url requires HTTPS without embedded credentials`);
48
+ }
49
+ source = { url: input.url };
50
+ file = { name: input.name, mimeType: input.mimeType };
48
51
  }
49
- if ("contentPath" in input) {
50
- const bytes = await readLocalFile(input.contentPath, {
51
- subject: `agentMail: attachment ${index} contentPath`,
52
+ else {
53
+ const resolved = await attachmentFile(input, index, {
54
+ plugin: "agentMail",
52
55
  maxBytes: MAX_FILE_BYTES,
53
56
  oversize: `agentMail: attachment ${index} exceeds the 6 MB request limit once encoded; use url`,
54
57
  });
55
- return { content: bytes.toString("base64") };
56
- }
57
- if ("content" in input)
58
- return { content: Buffer.from(input.content, "utf8").toString("base64") };
59
- try {
60
- const url = new URL(input.url);
61
- if (url.protocol !== "https:" || url.username || url.password)
62
- throw new Error();
58
+ if (!resolved.bytes.length)
59
+ throw new Error(`agentMail: attachment ${index} is empty`);
60
+ source = { content: resolved.bytes.toString("base64") };
61
+ file = resolved;
63
62
  }
64
- catch {
65
- throw new Error(`agentMail: attachment ${index} url requires HTTPS without embedded credentials`);
66
- }
67
- return { url: input.url };
68
- }
69
- /**
70
- * One attachment as AgentMail's SendAttachment. A download result passed
71
- * as contentBase64 supplies the name and type the input leaves out; a
72
- * local file is named for its path; text defaults to text/plain.
73
- */
74
- async function wireAttachment(input, index) {
75
- const source = await attachmentSource(input, index);
76
- if ("content" in source && !source.content)
77
- throw new Error(`agentMail: attachment ${index} is empty`);
78
- const result = "contentBase64" in input && typeof input.contentBase64 !== "string"
79
- ? input.contentBase64
80
- : undefined;
81
63
  return Object.fromEntries(Object.entries({
82
- filename: input.name ??
83
- (result?.name || undefined) ??
84
- ("contentPath" in input ? basename(input.contentPath) : undefined),
85
- content_type: input.mimeType ??
86
- (result?.mimeType || undefined) ??
87
- ("content" in input ? "text/plain" : undefined),
64
+ filename: file.name,
65
+ content_type: file.mimeType,
88
66
  content_disposition: input.inline === undefined
89
67
  ? undefined
90
68
  : input.inline
@@ -1,5 +1,6 @@
1
1
  import * as t from "typebox";
2
- import { atLeastOne, id, inboxFields, labelFields, mailListFields, pageFields, STRICT, searchFields, strings, threadFields, } from "./schemas.js";
2
+ import { atLeastOne } from "../../_shared/schemas.js";
3
+ import { id, inboxFields, labelFields, mailListFields, pageFields, STRICT, searchFields, strings, threadFields, } from "./schemas.js";
3
4
  import { attachmentPath, inboxPath, request, threadPath } from "./shared.js";
4
5
  const list = t.Object({
5
6
  ...inboxFields,
@@ -19,7 +19,8 @@
19
19
  */
20
20
  import { pathSegment, } from "runline";
21
21
  import * as t from "typebox";
22
- import { DownloadedFile, standardBase64 } from "../../_shared/base64.js";
22
+ import { base64File } from "../../_shared/attachments.js";
23
+ import { DownloadedFile } from "../../_shared/base64.js";
23
24
  import { renderEmailJsx } from "../../_shared/emailJsx.js";
24
25
  import { googleCredential, googleJsonRequest, googleOAuth, } from "../../_shared/googleAuth.js";
25
26
  import { GoogleTimestamp, Id, NonEmptyString, stringEnum, } from "../../_shared/googleSchemas.js";
@@ -111,30 +112,17 @@ function normalizeAttachment(input, index) {
111
112
  throw new Error(`gmail: attachment ${index} must be an object`);
112
113
  }
113
114
  const content = input.contentBase64;
114
- const contentBase64 = typeof content === "string"
115
- ? content
116
- : content &&
117
- typeof content === "object" &&
118
- typeof content.contentBase64 === "string"
119
- ? content.contentBase64
120
- : undefined;
121
- if (typeof contentBase64 !== "string") {
115
+ if (typeof content !== "string" &&
116
+ (typeof content !== "object" ||
117
+ !content ||
118
+ typeof content.contentBase64 !== "string")) {
122
119
  throw new Error(`gmail: attachment ${index} contentBase64 must be a base64 string`);
123
120
  }
124
- const normalized = standardBase64(contentBase64);
125
- if (normalized === undefined) {
126
- throw new Error(`gmail: attachment ${index} contentBase64 is not valid base64`);
127
- }
128
- // A download result passed as contentBase64 names its own file and type.
129
- const result = typeof content === "object" && content ? content : {};
130
- const named = (value) => typeof value === "string" && value ? value : undefined;
121
+ const file = base64File(content, `gmail: attachment ${index}`);
131
122
  return {
132
- name: input.name ??
133
- input.filename ??
134
- named(result.name) ??
135
- `attachment-${index + 1}`,
136
- mimeType: input.mimeType ?? named(result.mimeType) ?? "application/octet-stream",
137
- contentBase64: normalized,
123
+ name: input.name ?? input.filename ?? file.name ?? `attachment-${index + 1}`,
124
+ mimeType: input.mimeType ?? file.mimeType ?? "application/octet-stream",
125
+ contentBase64: file.bytes.toString("base64"),
138
126
  };
139
127
  }
140
128
  function attachmentPart(att) {