posthog-node 5.70.0 → 5.72.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/client.d.ts +3 -2
- package/dist/client.d.ts.map +1 -1
- package/dist/emails/attachments.d.ts +23 -0
- package/dist/emails/attachments.d.ts.map +1 -0
- package/dist/emails/attachments.js +170 -0
- package/dist/emails/attachments.mjs +123 -0
- package/dist/emails/emails.d.ts +7 -2
- package/dist/emails/emails.d.ts.map +1 -1
- package/dist/emails/emails.js +202 -36
- package/dist/emails/emails.mjs +202 -36
- package/dist/emails/errors.d.ts +1 -1
- package/dist/emails/errors.d.ts.map +1 -1
- package/dist/emails/errors.js +6 -1
- package/dist/emails/errors.mjs +6 -1
- package/dist/emails/types.d.ts +66 -0
- package/dist/emails/types.d.ts.map +1 -1
- package/dist/exports.d.ts +1 -1
- package/dist/exports.d.ts.map +1 -1
- package/dist/types.d.ts +2 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/dist/version.mjs +1 -1
- package/package.json +4 -4
- package/src/client.ts +3 -2
- package/src/emails/attachments.ts +209 -0
- package/src/emails/emails.ts +248 -36
- package/src/emails/errors.ts +7 -2
- package/src/emails/types.ts +68 -0
- package/src/exports.ts +2 -0
- package/src/types.ts +2 -1
- package/src/version.ts +1 -1
package/dist/version.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
const version = "5.
|
|
1
|
+
const version = "5.72.0";
|
|
2
2
|
export { version };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "posthog-node",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.72.0",
|
|
4
4
|
"bugs": {
|
|
5
5
|
"url": "https://github.com/PostHog/posthog-js/issues"
|
|
6
6
|
},
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
"module": "dist/entrypoints/index.node.mjs",
|
|
30
30
|
"types": "dist/entrypoints/index.node.d.ts",
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@posthog/core": "^1.67.
|
|
32
|
+
"@posthog/core": "^1.67.2"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
35
|
"@edge-runtime/vm": "^5.0.0",
|
|
@@ -48,8 +48,8 @@
|
|
|
48
48
|
"rxjs": "^7.8.0",
|
|
49
49
|
"typescript": "7.0.2",
|
|
50
50
|
"vitest": "^1.6.1",
|
|
51
|
-
"@posthog
|
|
52
|
-
"@posthog/
|
|
51
|
+
"@posthog/types": "^1.425.0",
|
|
52
|
+
"@posthog-tooling/tsconfig-base": "1.1.1"
|
|
53
53
|
},
|
|
54
54
|
"peerDependencies": {
|
|
55
55
|
"@opentelemetry/api": "^1.2.0",
|
package/src/client.ts
CHANGED
|
@@ -3531,8 +3531,9 @@ export abstract class PostHogBackendClient extends PostHogCoreStateless implemen
|
|
|
3531
3531
|
* The configured service name, stamped on events as `service_name` so events
|
|
3532
3532
|
* from several services sharing one project can be told apart. Resolved with
|
|
3533
3533
|
* the same resolvers as the traces and metrics `service.name`, so the event
|
|
3534
|
-
* carries the name the telemetry does: traces first (
|
|
3535
|
-
*
|
|
3534
|
+
* carries the name the telemetry does: traces first (`resourceAttributes`,
|
|
3535
|
+
* then `serviceName`), then metrics the same way. Host resource attributes
|
|
3536
|
+
* are only `os.*`, so they never supply the name. Lowest precedence: a
|
|
3536
3537
|
* property set by the caller, the context or `register` wins.
|
|
3537
3538
|
*/
|
|
3538
3539
|
private _serviceNameProperties(): PostHogEventProperties {
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import { EmailError } from './errors'
|
|
2
|
+
import type { EmailAttachment } from './types'
|
|
3
|
+
|
|
4
|
+
const MAX_ATTACHMENTS = 10
|
|
5
|
+
const MAX_MESSAGE_BYTES = 40_000_000
|
|
6
|
+
const MESSAGE_HEADERS_BYTES = 64 * 1024
|
|
7
|
+
const SUBJECT_BYTES_PER_ENCODED_WORD = 42
|
|
8
|
+
const ENCODED_WORD_BYTES = 75
|
|
9
|
+
const MESSAGE_LIMIT = '40 MB after Base64 encoding, about 29 MB of files'
|
|
10
|
+
const BASE64_PATTERN = /^[A-Za-z0-9+/]*={0,2}$/
|
|
11
|
+
|
|
12
|
+
export interface AttachmentFile {
|
|
13
|
+
filename: string
|
|
14
|
+
bytes: Uint8Array<ArrayBuffer>
|
|
15
|
+
contentType?: string
|
|
16
|
+
contentId?: string
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface HashedFile extends AttachmentFile {
|
|
20
|
+
sha256: string
|
|
21
|
+
field: string
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
interface CheckedAttachment {
|
|
25
|
+
filename: string
|
|
26
|
+
content: string | Uint8Array
|
|
27
|
+
size: number
|
|
28
|
+
contentType?: string
|
|
29
|
+
contentId?: string
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function attachmentFiles(
|
|
33
|
+
attachments: unknown,
|
|
34
|
+
{ html, text, subject }: { html?: string; text?: string; subject: string }
|
|
35
|
+
): AttachmentFile[] {
|
|
36
|
+
if (!Array.isArray(attachments)) {
|
|
37
|
+
throw invalid('attachments must be a list of files.', 'attachments')
|
|
38
|
+
}
|
|
39
|
+
if (attachments.length > MAX_ATTACHMENTS) {
|
|
40
|
+
throw new EmailError({
|
|
41
|
+
code: 'too_many_attachments',
|
|
42
|
+
message: `An email carries at most ${MAX_ATTACHMENTS} files, so no email was sent.`,
|
|
43
|
+
field: 'attachments',
|
|
44
|
+
fix: `Attach at most ${MAX_ATTACHMENTS} files, and link to any others from the email body.`,
|
|
45
|
+
})
|
|
46
|
+
}
|
|
47
|
+
const checked = Array.from(attachments, checkedAttachment)
|
|
48
|
+
refuseMessageOverLimit(
|
|
49
|
+
checked.map(({ size }) => size),
|
|
50
|
+
[html ?? '', text || html || ''],
|
|
51
|
+
subject
|
|
52
|
+
)
|
|
53
|
+
return checked.map(({ content, size, ...file }) => ({
|
|
54
|
+
...file,
|
|
55
|
+
bytes: typeof content === 'string' ? bytesOfBase64(content) : new Uint8Array(content),
|
|
56
|
+
}))
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Checks the email's size once a react element is rendered, since its html and the text derived
|
|
61
|
+
* from it were unknown when the files were checked.
|
|
62
|
+
*/
|
|
63
|
+
export function refuseRenderedMessageOverLimit(files: AttachmentFile[], content: Record<string, unknown>): void {
|
|
64
|
+
const html = typeof content.html === 'string' ? content.html : ''
|
|
65
|
+
const text = typeof content.text === 'string' ? content.text : ''
|
|
66
|
+
refuseMessageOverLimit(
|
|
67
|
+
files.map(({ bytes }) => bytes.byteLength),
|
|
68
|
+
[html, text || html],
|
|
69
|
+
typeof content.subject === 'string' ? content.subject : ''
|
|
70
|
+
)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export async function hashedFiles(files: AttachmentFile[]): Promise<HashedFile[]> {
|
|
74
|
+
return Promise.all(
|
|
75
|
+
files.map(async (file, index) => ({
|
|
76
|
+
...file,
|
|
77
|
+
sha256: await sha256Hex(file.bytes),
|
|
78
|
+
field: `attachments__${index}`,
|
|
79
|
+
}))
|
|
80
|
+
)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export function attachmentReference({ sha256, filename, contentType, contentId }: HashedFile): Record<string, unknown> {
|
|
84
|
+
return {
|
|
85
|
+
sha256,
|
|
86
|
+
filename,
|
|
87
|
+
...(contentType !== undefined && { content_type: contentType }),
|
|
88
|
+
...(contentId !== undefined && { content_id: contentId }),
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function checkedAttachment(attachment: unknown, index: number): CheckedAttachment {
|
|
93
|
+
if (
|
|
94
|
+
typeof attachment !== 'object' ||
|
|
95
|
+
attachment === null ||
|
|
96
|
+
Array.isArray(attachment) ||
|
|
97
|
+
ArrayBuffer.isView(attachment)
|
|
98
|
+
) {
|
|
99
|
+
throw invalid(
|
|
100
|
+
"An attachment is an object with filename and content. Pass the file's bytes as its content.",
|
|
101
|
+
`attachments__${index}`
|
|
102
|
+
)
|
|
103
|
+
}
|
|
104
|
+
const { filename, content, contentType, contentId, path, ...unsupported } = attachment as Partial<
|
|
105
|
+
Record<keyof EmailAttachment, unknown>
|
|
106
|
+
>
|
|
107
|
+
if (path !== undefined) {
|
|
108
|
+
throw invalid(
|
|
109
|
+
"PostHog can't fetch an attachment from a path or URL. Read the file and pass its content instead.",
|
|
110
|
+
`attachments__${index}__path`
|
|
111
|
+
)
|
|
112
|
+
}
|
|
113
|
+
const unsupportedKey = Object.entries(unsupported).find(([, value]) => value !== undefined)?.[0]
|
|
114
|
+
if (unsupportedKey !== undefined) {
|
|
115
|
+
throw invalid(
|
|
116
|
+
`An attachment takes filename, content, contentType and contentId, not ${unsupportedKey}.`,
|
|
117
|
+
`attachments__${index}__${unsupportedKey}`
|
|
118
|
+
)
|
|
119
|
+
}
|
|
120
|
+
if (typeof filename !== 'string' || filename === '') {
|
|
121
|
+
throw invalid("An attachment needs a filename, such as 'invoice.pdf'.", `attachments__${index}__filename`)
|
|
122
|
+
}
|
|
123
|
+
if (typeof content !== 'string' && !(content instanceof Uint8Array)) {
|
|
124
|
+
throw invalid(
|
|
125
|
+
'An attachment needs content: a Buffer, a Uint8Array or a Base64 string.',
|
|
126
|
+
`attachments__${index}__content`
|
|
127
|
+
)
|
|
128
|
+
}
|
|
129
|
+
if (typeof content === 'string' && !isBase64(content)) {
|
|
130
|
+
throw invalid(
|
|
131
|
+
"An attachment's content string must be Base64. Pass text as a Buffer or a Uint8Array.",
|
|
132
|
+
`attachments__${index}__content`
|
|
133
|
+
)
|
|
134
|
+
}
|
|
135
|
+
if (contentType !== undefined && typeof contentType !== 'string') {
|
|
136
|
+
throw invalid(
|
|
137
|
+
"An attachment's contentType must be a string, such as 'application/pdf'.",
|
|
138
|
+
`attachments__${index}__contentType`
|
|
139
|
+
)
|
|
140
|
+
}
|
|
141
|
+
if (contentId !== undefined && typeof contentId !== 'string') {
|
|
142
|
+
throw invalid("An attachment's contentId must be a string.", `attachments__${index}__contentId`)
|
|
143
|
+
}
|
|
144
|
+
const size = typeof content === 'string' ? base64DecodedBytes(content) : content.byteLength
|
|
145
|
+
if (size === 0) {
|
|
146
|
+
throw invalid('An attachment needs at least one byte.', `attachments__${index}__content`)
|
|
147
|
+
}
|
|
148
|
+
return {
|
|
149
|
+
filename,
|
|
150
|
+
content,
|
|
151
|
+
size,
|
|
152
|
+
...(contentType !== undefined && { contentType }),
|
|
153
|
+
...(contentId !== undefined && { contentId }),
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function isBase64(text: string): boolean {
|
|
158
|
+
return text.length % 4 === 0 && BASE64_PATTERN.test(text)
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function base64DecodedBytes(text: string): number {
|
|
162
|
+
const padding = text.endsWith('==') ? 2 : text.endsWith('=') ? 1 : 0
|
|
163
|
+
return (text.length / 4) * 3 - padding
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function bytesOfBase64(text: string): Uint8Array<ArrayBuffer> {
|
|
167
|
+
if (typeof Buffer !== 'undefined') {
|
|
168
|
+
return Buffer.from(text, 'base64')
|
|
169
|
+
}
|
|
170
|
+
const binary = atob(text)
|
|
171
|
+
const bytes = new Uint8Array(binary.length)
|
|
172
|
+
for (let index = 0; index < binary.length; index++) {
|
|
173
|
+
bytes[index] = binary.charCodeAt(index)
|
|
174
|
+
}
|
|
175
|
+
return bytes
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function refuseMessageOverLimit(fileSizes: number[], bodies: string[], subject: string): void {
|
|
179
|
+
if (fileSizes.length === 0) {
|
|
180
|
+
return
|
|
181
|
+
}
|
|
182
|
+
const encoder = new TextEncoder()
|
|
183
|
+
const sizes = [...fileSizes, ...bodies.map((body) => encoder.encode(body).byteLength)]
|
|
184
|
+
const headersBytes =
|
|
185
|
+
MESSAGE_HEADERS_BYTES +
|
|
186
|
+
Math.ceil(encoder.encode(subject).byteLength / SUBJECT_BYTES_PER_ENCODED_WORD) * ENCODED_WORD_BYTES
|
|
187
|
+
if (sizes.reduce((total, size) => total + base64LinesBytes(size), headersBytes) > MAX_MESSAGE_BYTES) {
|
|
188
|
+
throw new EmailError({
|
|
189
|
+
code: 'message_too_large',
|
|
190
|
+
message: `These files make the email larger than ${MESSAGE_LIMIT}, so no email was sent.`,
|
|
191
|
+
field: 'attachments',
|
|
192
|
+
why: "PostHog's email provider takes emails up to 40 MB, and Base64 encoding makes each file about a third larger.",
|
|
193
|
+
fix: 'Send fewer or smaller files, or link to large files from the email body.',
|
|
194
|
+
})
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function base64LinesBytes(size: number): number {
|
|
199
|
+
return Math.ceil((Math.ceil(size / 3) * 4 * 78) / 76)
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
async function sha256Hex(bytes: Uint8Array<ArrayBuffer>): Promise<string> {
|
|
203
|
+
const digest = new Uint8Array(await globalThis.crypto.subtle.digest('SHA-256', bytes))
|
|
204
|
+
return Array.from(digest, (byte) => byte.toString(16).padStart(2, '0')).join('')
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
function invalid(message: string, field: string): EmailError {
|
|
208
|
+
return new EmailError({ code: 'invalid_attachment', message, field })
|
|
209
|
+
}
|