workers-start 0.10.0 → 0.13.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/CHANGELOG.md +18 -0
- package/README.md +51 -4
- package/dist/cache.d.ts +56 -0
- package/dist/cache.d.ts.map +1 -1
- package/dist/cache.js +101 -0
- package/dist/cache.js.map +1 -1
- package/dist/google.d.ts +4 -2
- package/dist/google.d.ts.map +1 -1
- package/dist/google.js +6 -3
- package/dist/google.js.map +1 -1
- package/dist/index.d.ts +4 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/mail.d.ts.map +1 -1
- package/dist/mail.js +18 -12
- package/dist/mail.js.map +1 -1
- package/dist/rate-limit.d.ts +71 -0
- package/dist/rate-limit.d.ts.map +1 -0
- package/dist/rate-limit.js +102 -0
- package/dist/rate-limit.js.map +1 -0
- package/dist/sentry.d.ts +9 -3
- package/dist/sentry.d.ts.map +1 -1
- package/dist/sentry.js +26 -8
- package/dist/sentry.js.map +1 -1
- package/package.json +9 -3
- package/src/cache.ts +161 -0
- package/src/google.ts +5 -2
- package/src/index.ts +20 -1
- package/src/mail.ts +19 -12
- package/src/rate-limit.ts +158 -0
- package/src/sentry.ts +31 -16
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Rate limiting on the Workers rate limiting binding, with one answer for
|
|
3
|
+
* every app: the client is keyed by a salted hash of its IP (never the raw
|
|
4
|
+
* address) or by an API key the app names, a missing binding fails open and
|
|
5
|
+
* says so once per isolate, and a limited request gets a 429 problem
|
|
6
|
+
* (RFC 9457) with `Retry-After`, the IETF `RateLimit-Policy` and `RateLimit`
|
|
7
|
+
* headers, and `cache-control: no-store`.
|
|
8
|
+
*
|
|
9
|
+
* The binding counts per Cloudflare location over 10 or 60 seconds. Quotas
|
|
10
|
+
* over hours or days (a plan's daily allowance) are product rules, not abuse
|
|
11
|
+
* limits; keep those in the app's database.
|
|
12
|
+
*/
|
|
13
|
+
/** The `ratelimits` binding, structurally. */
|
|
14
|
+
export interface RateLimitBinding {
|
|
15
|
+
limit(options: {
|
|
16
|
+
key: string;
|
|
17
|
+
}): Promise<{
|
|
18
|
+
success: boolean;
|
|
19
|
+
}>;
|
|
20
|
+
}
|
|
21
|
+
export interface RateLimitPolicy {
|
|
22
|
+
/** Names the limit in the headers, such as `api` or `join`. */
|
|
23
|
+
name: string;
|
|
24
|
+
/** Requests allowed per period. Must match the binding's wrangler config. */
|
|
25
|
+
limit: number;
|
|
26
|
+
/** The binding's period: 10 or 60 seconds. */
|
|
27
|
+
periodSeconds: 10 | 60;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* A stable, anonymous key for the client: the SHA-256 of `salt` and the
|
|
31
|
+
* client IP (`cf-connecting-ip`), hex, 32 characters. Without an IP (a
|
|
32
|
+
* request from a test or a service binding) the key is `anon`.
|
|
33
|
+
*/
|
|
34
|
+
export declare function clientKey(request: Request, { salt }?: {
|
|
35
|
+
salt?: string;
|
|
36
|
+
}): Promise<string>;
|
|
37
|
+
/**
|
|
38
|
+
* Whether the request may go ahead. A missing binding allows it (failing
|
|
39
|
+
* closed would take the site down over a config slip) and calls `onUnbound`
|
|
40
|
+
* once per isolate so the slip is seen. So does a binding that throws.
|
|
41
|
+
*/
|
|
42
|
+
export declare function checkRateLimit(binding: RateLimitBinding | undefined | null, key: string, { onUnbound }?: {
|
|
43
|
+
onUnbound?: (error?: unknown) => void;
|
|
44
|
+
}): Promise<boolean>;
|
|
45
|
+
/** The IETF rate limit headers for a limited response, with `Retry-After`. */
|
|
46
|
+
export declare function rateLimitHeaders(policy: RateLimitPolicy): Record<string, string>;
|
|
47
|
+
/** The 429 problem for `policy`. `detail` replaces the default sentence. */
|
|
48
|
+
export declare function tooManyRequests(policy: RateLimitPolicy, { detail }?: {
|
|
49
|
+
detail?: string;
|
|
50
|
+
}): Response;
|
|
51
|
+
export interface RateLimitOptions {
|
|
52
|
+
/** The key to count by. Defaults to `clientKey(request, { salt })`. */
|
|
53
|
+
key?: string;
|
|
54
|
+
/** Salt for the default IP key, such as a secret from the env. */
|
|
55
|
+
salt?: string;
|
|
56
|
+
/** Let verified bots (`request.cf.verifiedBotCategory`) through uncounted. */
|
|
57
|
+
allowVerifiedBots?: boolean;
|
|
58
|
+
/** Called once per isolate when the binding is missing or fails. */
|
|
59
|
+
onUnbound?: (error?: unknown) => void;
|
|
60
|
+
/** Detail for the 429. */
|
|
61
|
+
detail?: string;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Counts the request against `policy` and returns the 429 to send, or null
|
|
65
|
+
* to go ahead.
|
|
66
|
+
*
|
|
67
|
+
* const limited = await rateLimit(request, env.API_LIMIT, API_POLICY);
|
|
68
|
+
* if (limited) return limited;
|
|
69
|
+
*/
|
|
70
|
+
export declare function rateLimit(request: Request, binding: RateLimitBinding | undefined | null, policy: RateLimitPolicy, options?: RateLimitOptions): Promise<Response | null>;
|
|
71
|
+
//# sourceMappingURL=rate-limit.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rate-limit.d.ts","sourceRoot":"","sources":["../src/rate-limit.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,8CAA8C;AAC9C,MAAM,WAAW,gBAAgB;IAC/B,KAAK,CAAC,OAAO,EAAE;QAAE,GAAG,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC;QAAE,OAAO,EAAE,OAAO,CAAA;KAAE,CAAC,CAAC;CAChE;AAED,MAAM,WAAW,eAAe;IAC9B,+DAA+D;IAC/D,IAAI,EAAE,MAAM,CAAC;IACb,6EAA6E;IAC7E,KAAK,EAAE,MAAM,CAAC;IACd,8CAA8C;IAC9C,aAAa,EAAE,EAAE,GAAG,EAAE,CAAC;CACxB;AAID;;;;GAIG;AACH,wBAAsB,SAAS,CAC7B,OAAO,EAAE,OAAO,EAChB,EAAE,IAAS,EAAE,GAA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
|
|
3
|
+
* every app: the client is keyed by a salted hash of its IP (never the raw
|
|
4
|
+
* address) or by an API key the app names, a missing binding fails open and
|
|
5
|
+
* says so once per isolate, and a limited request gets a 429 problem
|
|
6
|
+
* (RFC 9457) with `Retry-After`, the IETF `RateLimit-Policy` and `RateLimit`
|
|
7
|
+
* headers, and `cache-control: no-store`.
|
|
8
|
+
*
|
|
9
|
+
* The binding counts per Cloudflare location over 10 or 60 seconds. Quotas
|
|
10
|
+
* over hours or days (a plan's daily allowance) are product rules, not abuse
|
|
11
|
+
* limits; keep those in the app's database.
|
|
12
|
+
*/
|
|
13
|
+
const encoder = new TextEncoder();
|
|
14
|
+
/**
|
|
15
|
+
* A stable, anonymous key for the client: the SHA-256 of `salt` and the
|
|
16
|
+
* client IP (`cf-connecting-ip`), hex, 32 characters. Without an IP (a
|
|
17
|
+
* request from a test or a service binding) the key is `anon`.
|
|
18
|
+
*/
|
|
19
|
+
export async function clientKey(request, { salt = "" } = {}) {
|
|
20
|
+
const ip = request.headers.get("cf-connecting-ip")?.trim();
|
|
21
|
+
if (!ip)
|
|
22
|
+
return "anon";
|
|
23
|
+
const digest = await crypto.subtle.digest("SHA-256", encoder.encode(`${salt}\n${ip}`));
|
|
24
|
+
return [...new Uint8Array(digest).slice(0, 16)]
|
|
25
|
+
.map((b) => b.toString(16).padStart(2, "0"))
|
|
26
|
+
.join("");
|
|
27
|
+
}
|
|
28
|
+
const reported = new WeakSet();
|
|
29
|
+
let reportedUnbound = false;
|
|
30
|
+
/**
|
|
31
|
+
* Whether the request may go ahead. A missing binding allows it (failing
|
|
32
|
+
* closed would take the site down over a config slip) and calls `onUnbound`
|
|
33
|
+
* once per isolate so the slip is seen. So does a binding that throws.
|
|
34
|
+
*/
|
|
35
|
+
export async function checkRateLimit(binding, key, { onUnbound } = {}) {
|
|
36
|
+
if (!binding) {
|
|
37
|
+
if (!reportedUnbound) {
|
|
38
|
+
reportedUnbound = true;
|
|
39
|
+
onUnbound?.();
|
|
40
|
+
}
|
|
41
|
+
return true;
|
|
42
|
+
}
|
|
43
|
+
try {
|
|
44
|
+
return (await binding.limit({ key })).success;
|
|
45
|
+
}
|
|
46
|
+
catch (error) {
|
|
47
|
+
if (!reported.has(binding)) {
|
|
48
|
+
reported.add(binding);
|
|
49
|
+
onUnbound?.(error);
|
|
50
|
+
}
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
/** The IETF rate limit headers for a limited response, with `Retry-After`. */
|
|
55
|
+
export function rateLimitHeaders(policy) {
|
|
56
|
+
const name = JSON.stringify(policy.name);
|
|
57
|
+
return {
|
|
58
|
+
"retry-after": String(policy.periodSeconds),
|
|
59
|
+
"ratelimit-policy": `${name};q=${policy.limit};w=${policy.periodSeconds}`,
|
|
60
|
+
ratelimit: `${name};r=0;t=${policy.periodSeconds}`,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
/** The 429 problem for `policy`. `detail` replaces the default sentence. */
|
|
64
|
+
export function tooManyRequests(policy, { detail } = {}) {
|
|
65
|
+
const body = {
|
|
66
|
+
type: "about:blank",
|
|
67
|
+
title: "Too Many Requests",
|
|
68
|
+
status: 429,
|
|
69
|
+
code: "rate_limited",
|
|
70
|
+
detail: detail ??
|
|
71
|
+
`Too many requests. Try again in ${policy.periodSeconds} seconds.`,
|
|
72
|
+
};
|
|
73
|
+
return new Response(JSON.stringify(body), {
|
|
74
|
+
status: 429,
|
|
75
|
+
headers: {
|
|
76
|
+
"content-type": "application/problem+json",
|
|
77
|
+
"cache-control": "no-store",
|
|
78
|
+
...rateLimitHeaders(policy),
|
|
79
|
+
},
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Counts the request against `policy` and returns the 429 to send, or null
|
|
84
|
+
* to go ahead.
|
|
85
|
+
*
|
|
86
|
+
* const limited = await rateLimit(request, env.API_LIMIT, API_POLICY);
|
|
87
|
+
* if (limited) return limited;
|
|
88
|
+
*/
|
|
89
|
+
export async function rateLimit(request, binding, policy, options = {}) {
|
|
90
|
+
if (options.allowVerifiedBots) {
|
|
91
|
+
const cf = request.cf;
|
|
92
|
+
if (cf?.verifiedBotCategory)
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
const key = options.key ??
|
|
96
|
+
(await clientKey(request, options.salt ? { salt: options.salt } : {}));
|
|
97
|
+
const allowed = await checkRateLimit(binding, `${policy.name}:${key}`, options.onUnbound ? { onUnbound: options.onUnbound } : {});
|
|
98
|
+
return allowed
|
|
99
|
+
? null
|
|
100
|
+
: tooManyRequests(policy, options.detail ? { detail: options.detail } : {});
|
|
101
|
+
}
|
|
102
|
+
//# sourceMappingURL=rate-limit.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rate-limit.js","sourceRoot":"","sources":["../src/rate-limit.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAgBH,MAAM,OAAO,GAAG,IAAI,WAAW,EAAE,CAAC;AAElC;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,SAAS,CAC7B,OAAgB,EAChB,EAAE,IAAI,GAAG,EAAE,EAAE,GAAsB,EAAE;IAErC,MAAM,EAAE,GAAG,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,kBAAkB,CAAC,EAAE,IAAI,EAAE,CAAC;IAC3D,IAAI,CAAC,EAAE;QAAE,OAAO,MAAM,CAAC;IACvB,MAAM,MAAM,GAAG,MAAM,MAAM,CAAC,MAAM,CAAC,MAAM,CACvC,SAAS,EACT,OAAO,CAAC,MAAM,CAAC,GAAG,IAAI,KAAK,EAAE,EAAE,CAAC,CACjC,CAAC;IACF,OAAO,CAAC,GAAG,IAAI,UAAU,CAAC,MAAM,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;SAC5C,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,E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AAC,CAAC,eAAe,CAAC,MAAM,EAAE,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;AAChF,CAAC"}
|
package/dist/sentry.d.ts
CHANGED
|
@@ -20,6 +20,10 @@ export interface SentryEventLike {
|
|
|
20
20
|
request?: {
|
|
21
21
|
url?: string;
|
|
22
22
|
};
|
|
23
|
+
message?: string;
|
|
24
|
+
logentry?: {
|
|
25
|
+
message?: string;
|
|
26
|
+
};
|
|
23
27
|
exception?: {
|
|
24
28
|
values?: {
|
|
25
29
|
value?: string;
|
|
@@ -41,14 +45,14 @@ export declare const EXTENSION_ERROR: RegExp;
|
|
|
41
45
|
export declare const USER_REJECTION: RegExp;
|
|
42
46
|
/** Scripts other people inject into the page: extensions, wallets and Google Translate's proxy. */
|
|
43
47
|
export declare const BROWSER_DENY_URLS: readonly RegExp[];
|
|
44
|
-
/** Whether a browser event is noise that is not the app's to fix. `extra` adds patterns matched against
|
|
48
|
+
/** Whether a browser event is noise that is not the app's to fix. `extra` adds patterns matched against its messages. */
|
|
45
49
|
export declare function isBrowserNoise(event: SentryEventLike, extra?: readonly RegExp[]): boolean;
|
|
46
50
|
/** A DSN worth using: an `https://` URL. Anything else (empty, `off`, a placeholder) turns Sentry off. */
|
|
47
51
|
export declare function validDsn(dsn: string | null | undefined): string | undefined;
|
|
48
52
|
interface CommonOptions {
|
|
49
53
|
/** Overrides the DSN read from the env. */
|
|
50
54
|
dsn?: string | undefined;
|
|
51
|
-
/**
|
|
55
|
+
/** Left unset by default: an unset rate means no tracing at all, where 0 still creates unsampled spans. */
|
|
52
56
|
tracesSampleRate?: number;
|
|
53
57
|
/** Runs after the built-in filters. */
|
|
54
58
|
beforeSend?: BeforeSend;
|
|
@@ -60,7 +64,7 @@ interface CommonOptions {
|
|
|
60
64
|
export interface WorkerSentryOptions {
|
|
61
65
|
dsn: string | undefined;
|
|
62
66
|
enabled: boolean;
|
|
63
|
-
tracesSampleRate
|
|
67
|
+
tracesSampleRate?: number;
|
|
64
68
|
beforeSend: BeforeSend;
|
|
65
69
|
beforeSendTransaction: BeforeSend;
|
|
66
70
|
initialScope?: {
|
|
@@ -91,6 +95,8 @@ export declare function browserSentryOptions(options?: CommonOptions & {
|
|
|
91
95
|
denyUrls?: readonly RegExp[];
|
|
92
96
|
/** More messages that are noise. */
|
|
93
97
|
noise?: readonly RegExp[];
|
|
98
|
+
/** Events to keep even when they read like noise, such as handled captures from a wallet flow. */
|
|
99
|
+
keep?: (event: AnyEvent) => boolean;
|
|
94
100
|
}): BrowserSentryOptions;
|
|
95
101
|
export {};
|
|
96
102
|
//# sourceMappingURL=sentry.d.ts.map
|
package/dist/sentry.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sentry.d.ts","sourceRoot":"","sources":["../src/sentry.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAEH,mDAAmD;AACnD,MAAM,WAAW,eAAe;IAC9B,OAAO,CAAC,EAAE;QAAE,GAAG,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;IAC3B,SAAS,CAAC,EAAE;QAAE,MAAM,CAAC,EAAE;YAAE,KAAK,CAAC,EAAE,MAAM,CAAC;YAAC,IAAI,CAAC,EAAE,MAAM,CAAA;SAAE,EAAE,CAAA;KAAE,CAAC;CAC9D;AAGD,KAAK,QAAQ,GAAG,GAAG,CAAC;AACpB,KAAK,UAAU,GAAG,CAAC,KAAK,EAAE,QAAQ,EAAE,IAAI,EAAE,QAAQ,KAAK,QAAQ,CAAC;AAIhE,qIAAqI;AACrI,wBAAgB,YAAY,CAAC,KAAK,EAAE,eAAe,GAAG,OAAO,CAS5D;AAED,iEAAiE;AACjE,wBAAgB,eAAe,CAAC,CAAC,SAAS,eAAe,EAAE,KAAK,EAAE,CAAC,GAAG,CAAC,GAAG,IAAI,CAE7E;AAED,yFAAyF;AACz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SAAS,MAAM,EAAE,CAAC;IAC1B,kGAAkG;IAClG,IAAI,CAAC,EAAE,CAAC,KAAK,EAAE,QAAQ,KAAK,OAAO,CAAC;CAChC,GACL,oBAAoB,CAoBtB"}
|
package/dist/sentry.js
CHANGED
|
@@ -34,9 +34,9 @@ export function dropLocalEvents(event) {
|
|
|
34
34
|
return isLocalEvent(event) ? null : event;
|
|
35
35
|
}
|
|
36
36
|
/** Browser wording for a request that never left the device: Chrome, Safari, Firefox. */
|
|
37
|
-
export const NETWORK_ERROR = /failed to fetch|load failed|
|
|
37
|
+
export const NETWORK_ERROR = /failed to fetch|load failed|network ?error/i;
|
|
38
38
|
/** Errors thrown by extension content scripts and injected wallets. */
|
|
39
|
-
export const EXTENSION_ERROR = /func \w+ not found|tronlink|trap returned falsish|origin not allowed|object captured as promise rejection with keys:
|
|
39
|
+
export const EXTENSION_ERROR = /func \w+ not found|tronlink|trap returned falsish|origin not allowed|object captured as promise rejection with keys: (?=[^\n]*\bcode\b)(?=[^\n]*\bmessage\b)/i;
|
|
40
40
|
/** A person declining a wallet prompt. */
|
|
41
41
|
export const USER_REJECTION = /user rejected|user denied|rejected the request/i;
|
|
42
42
|
/** Scripts other people inject into the page: extensions, wallets and Google Translate's proxy. */
|
|
@@ -48,11 +48,21 @@ export const BROWSER_DENY_URLS = [
|
|
|
48
48
|
/chrome-extension:\/\//i,
|
|
49
49
|
/moz-extension:\/\//i,
|
|
50
50
|
/safari-(web-)?extension:\/\//i,
|
|
51
|
+
/ms-browser-extension:\/\//i,
|
|
52
|
+
/webkit-masked-url:/i,
|
|
51
53
|
];
|
|
52
|
-
/**
|
|
54
|
+
/** The texts the noise check reads: the exception value, the message and the log entry. */
|
|
55
|
+
function messagesOf(event) {
|
|
56
|
+
return [
|
|
57
|
+
event.exception?.values?.[0]?.value,
|
|
58
|
+
event.message,
|
|
59
|
+
event.logentry?.message,
|
|
60
|
+
].filter((m) => typeof m === "string" && m !== "");
|
|
61
|
+
}
|
|
62
|
+
/** Whether a browser event is noise that is not the app's to fix. `extra` adds patterns matched against its messages. */
|
|
53
63
|
export function isBrowserNoise(event, extra = []) {
|
|
54
|
-
const
|
|
55
|
-
return
|
|
64
|
+
const patterns = [NETWORK_ERROR, EXTENSION_ERROR, USER_REJECTION, ...extra];
|
|
65
|
+
return messagesOf(event).some((m) => patterns.some((re) => re.test(m)));
|
|
56
66
|
}
|
|
57
67
|
/** A DSN worth using: an `https://` URL. Anything else (empty, `off`, a placeholder) turns Sentry off. */
|
|
58
68
|
export function validDsn(dsn) {
|
|
@@ -80,10 +90,11 @@ export function workerSentryOptions(env, options = {}) {
|
|
|
80
90
|
const out = {
|
|
81
91
|
dsn,
|
|
82
92
|
enabled: dsn !== undefined,
|
|
83
|
-
tracesSampleRate: options.tracesSampleRate ?? 0,
|
|
84
93
|
beforeSend: chain(dropLocalEvents, options.beforeSend),
|
|
85
94
|
beforeSendTransaction: dropLocalEvents,
|
|
86
95
|
};
|
|
96
|
+
if (options.tracesSampleRate !== undefined)
|
|
97
|
+
out.tracesSampleRate = options.tracesSampleRate;
|
|
87
98
|
if (options.tags)
|
|
88
99
|
out.initialScope = { tags: options.tags };
|
|
89
100
|
if (options.release)
|
|
@@ -103,11 +114,18 @@ export function browserSentryOptions(options = {}) {
|
|
|
103
114
|
const out = {
|
|
104
115
|
dsn,
|
|
105
116
|
enabled: dsn !== undefined && options.enabled !== false,
|
|
106
|
-
tracesSampleRate: options.tracesSampleRate ?? 0,
|
|
107
117
|
denyUrls: [...BROWSER_DENY_URLS, ...(options.denyUrls ?? [])],
|
|
108
|
-
beforeSend: chain((event) =>
|
|
118
|
+
beforeSend: chain((event) => {
|
|
119
|
+
if (isLocalEvent(event))
|
|
120
|
+
return null;
|
|
121
|
+
if (options.keep?.(event))
|
|
122
|
+
return event;
|
|
123
|
+
return isBrowserNoise(event, noise) ? null : event;
|
|
124
|
+
}, options.beforeSend),
|
|
109
125
|
beforeSendTransaction: dropLocalEvents,
|
|
110
126
|
};
|
|
127
|
+
if (options.tracesSampleRate !== undefined)
|
|
128
|
+
out.tracesSampleRate = options.tracesSampleRate;
|
|
111
129
|
if (options.tags)
|
|
112
130
|
out.initialScope = { tags: options.tags };
|
|
113
131
|
if (options.release)
|
package/dist/sentry.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sentry.js","sourceRoot":"","sources":["../src/sentry.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;
|
|
1
|
+
{"version":3,"file":"sentry.js","sourceRoot":"","sources":["../src/sentry.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAcH,MAAM,WAAW,GAAG,IAAI,GAAG,CAAC,CAAC,WAAW,EAAE,WAAW,EAAE,OAAO,EAAE,KAAK,CAAC,CAAC,CAAC;AAExE,qIAAqI;AACrI,MAAM,UAAU,YAAY,CAAC,KAAsB;IACjD,MAAM,GAAG,GAAG,KAAK,CAAC,OAAO,EAAE,GAAG,CAAC;IAC/B,IAAI,CAAC,GAAG;QAAE,OAAO,KAAK,CAAC;IACvB,IAAI,CAAC;QACH,MAAM,IAAI,GAAG,IAAI,GAAG,CAAC,GAAG,CAAC,CAAC,QAAQ,CAAC;QACnC,OAAO,WAAW,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC,QAAQ,CAAC,YAAY,CAAC,CAAC;IAC9D,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,KAAK,CAAC;IACf,CAAC;AACH,CAAC;AAED,iEAAiE;AACjE,MAAM,UAAU,eAAe,CAA4B,KAAQ;IACjE,OAAO,YAAY,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC;AAC5C,CAAC;AAED,yFAAyF;AACzF,MAAM,CAAC,MAAM,aAAa,GAAG,6CAA6C,CAAC;AAC3E,uEAAuE;AACvE,MAAM,CAAC,MAAM,eAAe,GAC1B,+JAA+J,CAAC;AAClK,0CAA0C;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AI,EAAE,CAAC;IAClC,MAAM,GAAG,GAAyB;QAChC,GAAG;QACH,OAAO,EAAE,GAAG,KAAK,SAAS,IAAI,OAAO,CAAC,OAAO,KAAK,KAAK;QACvD,QAAQ,EAAE,CAAC,GAAG,iBAAiB,EAAE,GAAG,CAAC,OAAO,CAAC,QAAQ,IAAI,EAAE,CAAC,CAAC;QAC7D,UAAU,EAAE,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE;YAC1B,IAAI,YAAY,CAAC,KAAK,CAAC;gBAAE,OAAO,IAAI,CAAC;YACrC,IAAI,OAAO,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC;gBAAE,OAAO,KAAK,CAAC;YACxC,OAAO,cAAc,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC;QACrD,CAAC,EAAE,OAAO,CAAC,UAAU,CAAC;QACtB,qBAAqB,EAAE,eAAe;KACvC,CAAC;IACF,IAAI,OAAO,CAAC,gBAAgB,KAAK,SAAS;QACxC,GAAG,CAAC,gBAAgB,GAAG,OAAO,CAAC,gBAAgB,CAAC;IAClD,IAAI,OAAO,CAAC,IAAI;QAAE,GAAG,CAAC,YAAY,GAAG,EAAE,IAAI,EAAE,OAAO,CAAC,IAAI,EAAE,CAAC;IAC5D,IAAI,OAAO,CAAC,OAAO;QAAE,GAAG,CAAC,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC;IACnD,IAAI,OAAO,CAAC,WAAW;QAAE,GAAG,CAAC,WAAW,GAAG,OAAO,CAAC,WAAW,CAAC;IAC/D,OAAO,GAAG,CAAC;AACb,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "workers-start",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "The parts every
|
|
3
|
+
"version": "0.13.0",
|
|
4
|
+
"description": "The parts every app on Cloudflare Workers ends up writing: the TanStack Start entry with Markdown for agents, head tags, bindings, edge and value caches, Cloudflare Access checks, Google service-account calls, Sentry options, mail, rate limiting, and a check that no local secret ships in the build.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Ryan Ghods",
|
|
7
7
|
"repository": {
|
|
@@ -26,7 +26,8 @@
|
|
|
26
26
|
"search-console",
|
|
27
27
|
"sentry",
|
|
28
28
|
"email",
|
|
29
|
-
"resend"
|
|
29
|
+
"resend",
|
|
30
|
+
"rate-limit"
|
|
30
31
|
],
|
|
31
32
|
"type": "module",
|
|
32
33
|
"sideEffects": false,
|
|
@@ -101,6 +102,11 @@
|
|
|
101
102
|
"import": "./dist/mail.js",
|
|
102
103
|
"default": "./dist/mail.js"
|
|
103
104
|
},
|
|
105
|
+
"./rate-limit": {
|
|
106
|
+
"types": "./dist/rate-limit.d.ts",
|
|
107
|
+
"import": "./dist/rate-limit.js",
|
|
108
|
+
"default": "./dist/rate-limit.js"
|
|
109
|
+
},
|
|
104
110
|
"./package.json": "./package.json"
|
|
105
111
|
},
|
|
106
112
|
"bin": {
|
package/src/cache.ts
CHANGED
|
@@ -93,3 +93,164 @@ export async function withEdgeCache(
|
|
|
93
93
|
}
|
|
94
94
|
return response;
|
|
95
95
|
}
|
|
96
|
+
|
|
97
|
+
/** A KV namespace, structurally. */
|
|
98
|
+
export interface CachedKv {
|
|
99
|
+
get(key: string): Promise<string | null>;
|
|
100
|
+
put(
|
|
101
|
+
key: string,
|
|
102
|
+
value: string,
|
|
103
|
+
options?: { expirationTtl?: number },
|
|
104
|
+
): Promise<void>;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export interface CachedOptions {
|
|
108
|
+
/** Served as is for this long after it was computed. */
|
|
109
|
+
freshSeconds: number;
|
|
110
|
+
/**
|
|
111
|
+
* After that, served for this much longer while one refresh runs after the
|
|
112
|
+
* response. Default 0: a value past fresh is recomputed before answering.
|
|
113
|
+
*/
|
|
114
|
+
staleSeconds?: number;
|
|
115
|
+
/**
|
|
116
|
+
* A KV namespace to share values across locations, for upstreams too slow
|
|
117
|
+
* or rate limited to call once per location. Without it, values live in
|
|
118
|
+
* each location's Cache API, which has no write limit or cost.
|
|
119
|
+
*/
|
|
120
|
+
kv?: CachedKv;
|
|
121
|
+
/** Added to every key, so a deploy that changes the value's shape starts fresh. */
|
|
122
|
+
version?: string;
|
|
123
|
+
/** The Cache to use. Default `caches.default`; none outside workerd. */
|
|
124
|
+
cache?: Pick<Cache, "match" | "put">;
|
|
125
|
+
/** Schedules background refreshes and stores. Default `afterResponse`. */
|
|
126
|
+
waitUntil?: (work: Promise<unknown>) => void;
|
|
127
|
+
/** Hears a failed compute that was answered with a stale value instead, or a failed store. */
|
|
128
|
+
onError?: (error: unknown) => void;
|
|
129
|
+
/** Milliseconds since the epoch. */
|
|
130
|
+
now?: () => number;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export interface CachedEntry<T> {
|
|
134
|
+
value: T;
|
|
135
|
+
/** When the value was computed, in milliseconds since the epoch. */
|
|
136
|
+
at: number;
|
|
137
|
+
/** True when the value is past `freshSeconds`. */
|
|
138
|
+
stale: boolean;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
interface Envelope<T> {
|
|
142
|
+
at: number;
|
|
143
|
+
value: T;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const refreshing = new Map<string, Promise<unknown>>();
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* A value computed at most once per `freshSeconds`, kept in the Cache API
|
|
150
|
+
* (and in KV, when given), with stale-while-revalidate and stale-if-error:
|
|
151
|
+
*
|
|
152
|
+
* - fresh: returned as is;
|
|
153
|
+
* - within `staleSeconds` after that: returned, and recomputed after the
|
|
154
|
+
* response (one refresh per key per isolate at a time);
|
|
155
|
+
* - older, or missing: recomputed before answering. If that fails and any
|
|
156
|
+
* earlier value is still stored, the earlier value is returned (with
|
|
157
|
+
* `stale: true`) and the error goes to `onError`; with nothing stored, the
|
|
158
|
+
* error is thrown.
|
|
159
|
+
*
|
|
160
|
+
* Values must be JSON. Keys are any string.
|
|
161
|
+
*/
|
|
162
|
+
export async function cachedEntry<T>(
|
|
163
|
+
key: string,
|
|
164
|
+
compute: () => Promise<T>,
|
|
165
|
+
options: CachedOptions,
|
|
166
|
+
): Promise<CachedEntry<T>> {
|
|
167
|
+
const now = options.now ?? Date.now;
|
|
168
|
+
const freshMs = options.freshSeconds * 1000;
|
|
169
|
+
const staleMs = (options.staleSeconds ?? 0) * 1000;
|
|
170
|
+
const keepSeconds = Math.max(
|
|
171
|
+
60,
|
|
172
|
+
Math.ceil(options.freshSeconds + (options.staleSeconds ?? 0)) * 4,
|
|
173
|
+
);
|
|
174
|
+
const fullKey = options.version ? `${options.version}:${key}` : key;
|
|
175
|
+
const cache = options.cache ?? defaultCache();
|
|
176
|
+
const cacheKey = new Request(
|
|
177
|
+
`https://workers-start.cache/${encodeURIComponent(fullKey)}`,
|
|
178
|
+
);
|
|
179
|
+
const later = (work: Promise<unknown>) => {
|
|
180
|
+
const guarded = work.catch((error) => options.onError?.(error));
|
|
181
|
+
if (options.waitUntil) options.waitUntil(guarded);
|
|
182
|
+
else void afterResponse(guarded);
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
const read = async (): Promise<Envelope<T> | null> => {
|
|
186
|
+
const hit = cache ? await cache.match(cacheKey) : undefined;
|
|
187
|
+
if (hit) return (await hit.json()) as Envelope<T>;
|
|
188
|
+
if (!options.kv) return null;
|
|
189
|
+
const raw = await options.kv.get(fullKey);
|
|
190
|
+
if (!raw) return null;
|
|
191
|
+
const envelope = JSON.parse(raw) as Envelope<T>;
|
|
192
|
+
if (cache) later(cache.put(cacheKey, toResponse(envelope, keepSeconds)));
|
|
193
|
+
return envelope;
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
const store = async (envelope: Envelope<T>) => {
|
|
197
|
+
const text = JSON.stringify(envelope);
|
|
198
|
+
await Promise.all([
|
|
199
|
+
cache?.put(cacheKey, toResponse(envelope, keepSeconds)),
|
|
200
|
+
options.kv?.put(fullKey, text, { expirationTtl: keepSeconds }),
|
|
201
|
+
]);
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
const refresh = (): Promise<Envelope<T>> => {
|
|
205
|
+
const running = refreshing.get(fullKey) as Promise<Envelope<T>> | undefined;
|
|
206
|
+
if (running) return running;
|
|
207
|
+
const next = (async () => {
|
|
208
|
+
const envelope = { at: now(), value: await compute() };
|
|
209
|
+
later(store(envelope));
|
|
210
|
+
return envelope;
|
|
211
|
+
})().finally(() => refreshing.delete(fullKey));
|
|
212
|
+
refreshing.set(fullKey, next);
|
|
213
|
+
return next;
|
|
214
|
+
};
|
|
215
|
+
|
|
216
|
+
let current: Envelope<T> | null = null;
|
|
217
|
+
try {
|
|
218
|
+
current = await read();
|
|
219
|
+
} catch (error) {
|
|
220
|
+
options.onError?.(error);
|
|
221
|
+
}
|
|
222
|
+
const age = current ? now() - current.at : Number.POSITIVE_INFINITY;
|
|
223
|
+
if (current && age <= freshMs) {
|
|
224
|
+
return { value: current.value, at: current.at, stale: false };
|
|
225
|
+
}
|
|
226
|
+
if (current && age <= freshMs + staleMs) {
|
|
227
|
+
later(refresh());
|
|
228
|
+
return { value: current.value, at: current.at, stale: true };
|
|
229
|
+
}
|
|
230
|
+
try {
|
|
231
|
+
const fresh = await refresh();
|
|
232
|
+
return { value: fresh.value, at: fresh.at, stale: false };
|
|
233
|
+
} catch (error) {
|
|
234
|
+
if (!current) throw error;
|
|
235
|
+
options.onError?.(error);
|
|
236
|
+
return { value: current.value, at: current.at, stale: true };
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/** `cachedEntry`, returning only the value. */
|
|
241
|
+
export async function cached<T>(
|
|
242
|
+
key: string,
|
|
243
|
+
compute: () => Promise<T>,
|
|
244
|
+
options: CachedOptions,
|
|
245
|
+
): Promise<T> {
|
|
246
|
+
return (await cachedEntry(key, compute, options)).value;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
function toResponse(envelope: Envelope<unknown>, seconds: number): Response {
|
|
250
|
+
return new Response(JSON.stringify(envelope), {
|
|
251
|
+
headers: {
|
|
252
|
+
"content-type": "application/json",
|
|
253
|
+
"cache-control": `public, s-maxage=${seconds}, max-age=${seconds}`,
|
|
254
|
+
},
|
|
255
|
+
});
|
|
256
|
+
}
|
package/src/google.ts
CHANGED
|
@@ -344,12 +344,15 @@ export function ga4Rows(
|
|
|
344
344
|
});
|
|
345
345
|
}
|
|
346
346
|
|
|
347
|
-
/** A filter matching a dimension against a list of values. */
|
|
347
|
+
/** A filter matching a dimension against a list of values. Google matches case-sensitively unless `caseSensitive: false`. */
|
|
348
348
|
export function inListFilter(
|
|
349
349
|
fieldName: string,
|
|
350
350
|
values: readonly string[],
|
|
351
|
+
{ caseSensitive }: { caseSensitive?: boolean } = {},
|
|
351
352
|
): Ga4FilterExpression {
|
|
352
|
-
|
|
353
|
+
const inList: Record<string, unknown> = { values: [...values] };
|
|
354
|
+
if (caseSensitive !== undefined) inList.caseSensitive = caseSensitive;
|
|
355
|
+
return { filter: { fieldName, inListFilter: inList } };
|
|
353
356
|
}
|
|
354
357
|
|
|
355
358
|
// Search Console
|
package/src/index.ts
CHANGED
|
@@ -14,8 +14,15 @@ export {
|
|
|
14
14
|
parseAccessConfig,
|
|
15
15
|
verifyAccessToken,
|
|
16
16
|
} from "./access.js";
|
|
17
|
-
export type {
|
|
17
|
+
export type {
|
|
18
|
+
CachedEntry,
|
|
19
|
+
CachedKv,
|
|
20
|
+
CachedOptions,
|
|
21
|
+
EdgeCacheOptions,
|
|
22
|
+
} from "./cache.js";
|
|
18
23
|
export {
|
|
24
|
+
cached,
|
|
25
|
+
cachedEntry,
|
|
19
26
|
sharedMaxAge,
|
|
20
27
|
withEdgeCache,
|
|
21
28
|
withoutBrowserMaxAge,
|
|
@@ -93,6 +100,18 @@ export {
|
|
|
93
100
|
recordingMailer,
|
|
94
101
|
resendMailer,
|
|
95
102
|
} from "./mail.js";
|
|
103
|
+
export type {
|
|
104
|
+
RateLimitBinding,
|
|
105
|
+
RateLimitOptions,
|
|
106
|
+
RateLimitPolicy,
|
|
107
|
+
} from "./rate-limit.js";
|
|
108
|
+
export {
|
|
109
|
+
checkRateLimit,
|
|
110
|
+
clientKey,
|
|
111
|
+
rateLimit,
|
|
112
|
+
rateLimitHeaders,
|
|
113
|
+
tooManyRequests,
|
|
114
|
+
} from "./rate-limit.js";
|
|
96
115
|
export type {
|
|
97
116
|
AnalyticsScriptOptions,
|
|
98
117
|
GaEventParams,
|
package/src/mail.ts
CHANGED
|
@@ -174,18 +174,25 @@ export function resendMailer(options: ResendOptions): Mailer {
|
|
|
174
174
|
...(a.contentType ? { content_type: a.contentType } : {}),
|
|
175
175
|
}));
|
|
176
176
|
}
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
177
|
+
let res: Response;
|
|
178
|
+
try {
|
|
179
|
+
res = await fetcher("https://api.resend.com/emails", {
|
|
180
|
+
method: "POST",
|
|
181
|
+
headers: {
|
|
182
|
+
authorization: `Bearer ${options.apiKey}`,
|
|
183
|
+
"content-type": "application/json",
|
|
184
|
+
...(message.idempotencyKey
|
|
185
|
+
? { "idempotency-key": message.idempotencyKey }
|
|
186
|
+
: {}),
|
|
187
|
+
},
|
|
188
|
+
body: JSON.stringify(body),
|
|
189
|
+
signal: AbortSignal.timeout(options.timeoutMs ?? 10_000),
|
|
190
|
+
});
|
|
191
|
+
} catch (err) {
|
|
192
|
+
throw new MailError(
|
|
193
|
+
`Resend did not answer: ${err instanceof Error ? err.message : String(err)}`,
|
|
194
|
+
);
|
|
195
|
+
}
|
|
189
196
|
const json = (await res.json().catch(() => null)) as {
|
|
190
197
|
id?: string;
|
|
191
198
|
message?: string;
|