@supatype/client 0.1.0-alpha.10
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/.turbo/turbo-build.log +4 -0
- package/.turbo/turbo-test.log +23 -0
- package/.turbo/turbo-typecheck.log +4 -0
- package/dist/auth.d.ts +279 -0
- package/dist/auth.d.ts.map +1 -0
- package/dist/auth.js +848 -0
- package/dist/auth.js.map +1 -0
- package/dist/error-codes-doc.d.ts +31 -0
- package/dist/error-codes-doc.d.ts.map +1 -0
- package/dist/error-codes-doc.js +294 -0
- package/dist/error-codes-doc.js.map +1 -0
- package/dist/errors.d.ts +197 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +162 -0
- package/dist/errors.js.map +1 -0
- package/dist/fetch-with-retry.d.ts +49 -0
- package/dist/fetch-with-retry.d.ts.map +1 -0
- package/dist/fetch-with-retry.js +150 -0
- package/dist/fetch-with-retry.js.map +1 -0
- package/dist/index.d.ts +151 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +285 -0
- package/dist/index.js.map +1 -0
- package/dist/query.d.ts +55 -0
- package/dist/query.d.ts.map +1 -0
- package/dist/query.js +264 -0
- package/dist/query.js.map +1 -0
- package/dist/realtime.d.ts +69 -0
- package/dist/realtime.d.ts.map +1 -0
- package/dist/realtime.js +279 -0
- package/dist/realtime.js.map +1 -0
- package/dist/retry.d.ts +24 -0
- package/dist/retry.d.ts.map +1 -0
- package/dist/retry.js +25 -0
- package/dist/retry.js.map +1 -0
- package/dist/serverless-docs.d.ts +38 -0
- package/dist/serverless-docs.d.ts.map +1 -0
- package/dist/serverless-docs.js +83 -0
- package/dist/serverless-docs.js.map +1 -0
- package/dist/storage.d.ts +102 -0
- package/dist/storage.d.ts.map +1 -0
- package/dist/storage.js +169 -0
- package/dist/storage.js.map +1 -0
- package/dist/types.d.ts +212 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/package.json +26 -0
- package/src/auth.ts +1024 -0
- package/src/error-codes-doc.ts +315 -0
- package/src/errors.ts +201 -0
- package/src/fetch-with-retry.ts +200 -0
- package/src/index.ts +518 -0
- package/src/query.ts +331 -0
- package/src/realtime.ts +418 -0
- package/src/retry.ts +35 -0
- package/src/serverless-docs.ts +107 -0
- package/src/storage.ts +234 -0
- package/src/types.ts +264 -0
- package/tests/auth.test.ts +351 -0
- package/tests/mfa-totp-integration.test.ts +359 -0
- package/tests/query.test.ts +286 -0
- package/tests/rate-limit-integration.test.ts +185 -0
- package/tests/realtime-multi-tenant-integration.test.ts +372 -0
- package/tests/retry-integration.test.ts +212 -0
- package/tests/serverless.test.ts +115 -0
- package/tests/storage-quota-integration.test.ts +189 -0
- package/tests/storage-upload-limit-integration.test.ts +185 -0
- package/tests/types.test.ts +137 -0
- package/tsconfig.json +11 -0
- package/tsconfig.tsbuildinfo +1 -0
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Fetch wrapper with automatic retry, rate limit handling, and timeout.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { NetworkError, RateLimitError, SupatypeError, NetworkErrorCodes } from "./errors.js"
|
|
6
|
+
|
|
7
|
+
export interface FetchOptions {
|
|
8
|
+
/** Maximum number of retries for transient errors. Default: 3. */
|
|
9
|
+
maxRetries?: number | undefined
|
|
10
|
+
/** Request timeout in milliseconds. Default: 30000. */
|
|
11
|
+
timeout?: number | undefined
|
|
12
|
+
/** Disable automatic retry. */
|
|
13
|
+
retry?: boolean | undefined
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const DEFAULT_MAX_RETRIES = 3
|
|
17
|
+
const DEFAULT_TIMEOUT = 30000
|
|
18
|
+
const RETRY_DELAYS = [200, 1000, 3000]
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Fetch with automatic retry for transient errors and rate limit handling.
|
|
22
|
+
*
|
|
23
|
+
* Retries on:
|
|
24
|
+
* - Network errors (connection refused, timeout, DNS)
|
|
25
|
+
* - 5xx server errors
|
|
26
|
+
*
|
|
27
|
+
* Does NOT retry:
|
|
28
|
+
* - 4xx client errors (except 429 which is handled specially)
|
|
29
|
+
*/
|
|
30
|
+
export async function fetchWithRetry(
|
|
31
|
+
url: string,
|
|
32
|
+
init?: RequestInit & FetchOptions,
|
|
33
|
+
): Promise<Response> {
|
|
34
|
+
const maxRetries = init?.retry === false ? 0 : (init?.maxRetries ?? DEFAULT_MAX_RETRIES)
|
|
35
|
+
const timeout = init?.timeout ?? DEFAULT_TIMEOUT
|
|
36
|
+
let lastError: Error | undefined
|
|
37
|
+
|
|
38
|
+
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
|
39
|
+
try {
|
|
40
|
+
const controller = new AbortController()
|
|
41
|
+
const timeoutId = setTimeout(() => controller.abort(), timeout)
|
|
42
|
+
|
|
43
|
+
const response = await fetch(url, {
|
|
44
|
+
...init,
|
|
45
|
+
signal: controller.signal,
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
clearTimeout(timeoutId)
|
|
49
|
+
|
|
50
|
+
// Handle rate limiting
|
|
51
|
+
if (response.status === 429) {
|
|
52
|
+
const retryAfter = parseInt(response.headers.get("Retry-After") || "5", 10)
|
|
53
|
+
if (attempt < maxRetries) {
|
|
54
|
+
await sleep(retryAfter * 1000)
|
|
55
|
+
continue
|
|
56
|
+
}
|
|
57
|
+
throw new RateLimitError(retryAfter)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// Retry on 5xx server errors
|
|
61
|
+
if (response.status >= 500 && attempt < maxRetries) {
|
|
62
|
+
await sleep(RETRY_DELAYS[attempt] ?? 3000)
|
|
63
|
+
continue
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return response
|
|
67
|
+
} catch (err) {
|
|
68
|
+
if (err instanceof RateLimitError) throw err
|
|
69
|
+
if (err instanceof SupatypeError) throw err
|
|
70
|
+
|
|
71
|
+
lastError = err instanceof Error ? err : new Error(String(err))
|
|
72
|
+
|
|
73
|
+
// Classify the error
|
|
74
|
+
if (lastError.name === "AbortError") {
|
|
75
|
+
throw new NetworkError(
|
|
76
|
+
`Request timed out after ${timeout}ms`,
|
|
77
|
+
NetworkErrorCodes.TIMEOUT,
|
|
78
|
+
)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Retry on network errors
|
|
82
|
+
if (attempt < maxRetries) {
|
|
83
|
+
await sleep(RETRY_DELAYS[attempt] ?? 3000)
|
|
84
|
+
continue
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// Classify and throw
|
|
88
|
+
const message = lastError.message.toLowerCase()
|
|
89
|
+
if (message.includes("econnrefused")) {
|
|
90
|
+
throw new NetworkError(
|
|
91
|
+
`Connection refused: ${url}`,
|
|
92
|
+
NetworkErrorCodes.CONNECTION_REFUSED,
|
|
93
|
+
)
|
|
94
|
+
}
|
|
95
|
+
if (message.includes("enotfound") || message.includes("dns")) {
|
|
96
|
+
throw new NetworkError(
|
|
97
|
+
`DNS lookup failed: ${url}`,
|
|
98
|
+
NetworkErrorCodes.DNS_FAILURE,
|
|
99
|
+
)
|
|
100
|
+
}
|
|
101
|
+
throw new NetworkError(lastError.message)
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
throw new NetworkError(lastError?.message || "Request failed after retries")
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function sleep(ms: number): Promise<void> {
|
|
109
|
+
return new Promise((resolve) => setTimeout(resolve, ms))
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// ─── Serverless environment detection ────────────────────────────────────────
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Known serverless environment indicators keyed by platform name.
|
|
116
|
+
* Each entry maps to an env-var check: either the var must exist (value `undefined`)
|
|
117
|
+
* or it must equal the specified string.
|
|
118
|
+
*/
|
|
119
|
+
const SERVERLESS_ENV_VARS: ReadonlyArray<{ name: string; envVar: string; expected?: string }> = [
|
|
120
|
+
{ name: "Vercel", envVar: "VERCEL", expected: "1" },
|
|
121
|
+
{ name: "AWS Lambda", envVar: "AWS_LAMBDA_FUNCTION_NAME" },
|
|
122
|
+
{ name: "Netlify", envVar: "NETLIFY", expected: "true" },
|
|
123
|
+
{ name: "Cloudflare Workers", envVar: "CF_PAGES" },
|
|
124
|
+
{ name: "Google Cloud Functions", envVar: "FUNCTION_TARGET" },
|
|
125
|
+
{ name: "Azure Functions", envVar: "FUNCTIONS_WORKER_RUNTIME" },
|
|
126
|
+
{ name: "Deno Deploy", envVar: "DENO_DEPLOYMENT_ID" },
|
|
127
|
+
{ name: "Railway", envVar: "RAILWAY_ENVIRONMENT" },
|
|
128
|
+
{ name: "Render", envVar: "RENDER" },
|
|
129
|
+
{ name: "Fly.io", envVar: "FLY_APP_NAME" },
|
|
130
|
+
]
|
|
131
|
+
|
|
132
|
+
export interface ServerlessDetectionResult {
|
|
133
|
+
/** Whether a serverless environment was detected. */
|
|
134
|
+
detected: boolean
|
|
135
|
+
/** Name of the detected platform, or `null` when not detected. */
|
|
136
|
+
platform: string | null
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Detect whether the SDK is running inside a known serverless environment by
|
|
141
|
+
* inspecting well-known environment variables.
|
|
142
|
+
*
|
|
143
|
+
* Returns the platform name when detected so callers can include it in
|
|
144
|
+
* diagnostics or warnings.
|
|
145
|
+
*/
|
|
146
|
+
export function detectServerlessEnvironment(): ServerlessDetectionResult {
|
|
147
|
+
// Guard: `process` may not exist in browser/edge contexts.
|
|
148
|
+
if (typeof process === "undefined" || process.env === undefined) {
|
|
149
|
+
return { detected: false, platform: null }
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
for (const entry of SERVERLESS_ENV_VARS) {
|
|
153
|
+
const value = process.env[entry.envVar]
|
|
154
|
+
if (value === undefined) continue
|
|
155
|
+
if (entry.expected === undefined || value === entry.expected) {
|
|
156
|
+
return { detected: true, platform: entry.name }
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
return { detected: false, platform: null }
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const SERVERLESS_WARNING_ISSUED = { value: false }
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Log a one-time warning when the SDK detects a serverless environment **and**
|
|
167
|
+
* the provided URL looks like a direct Postgres connection string rather than
|
|
168
|
+
* the Supatype HTTP gateway.
|
|
169
|
+
*
|
|
170
|
+
* Direct database connections are problematic in serverless contexts because
|
|
171
|
+
* each invocation may open a new TCP connection, quickly exhausting the
|
|
172
|
+
* database's connection limit.
|
|
173
|
+
*
|
|
174
|
+
* The warning is emitted at most once per process to avoid log noise.
|
|
175
|
+
*/
|
|
176
|
+
export function warnIfServerlessDirectConnection(url: string): void {
|
|
177
|
+
if (SERVERLESS_WARNING_ISSUED.value) return
|
|
178
|
+
|
|
179
|
+
const { detected, platform } = detectServerlessEnvironment()
|
|
180
|
+
if (!detected) return
|
|
181
|
+
|
|
182
|
+
// Check if the URL resembles a direct Postgres connection string
|
|
183
|
+
const lowerUrl = url.toLowerCase()
|
|
184
|
+
const isDirectConnection =
|
|
185
|
+
lowerUrl.startsWith("postgres://") ||
|
|
186
|
+
lowerUrl.startsWith("postgresql://") ||
|
|
187
|
+
// Port 5432 or 6543 (pgbouncer) on a bare hostname hints at direct PG access
|
|
188
|
+
/:\d{4,5}\/\w/.test(url)
|
|
189
|
+
|
|
190
|
+
if (!isDirectConnection) return
|
|
191
|
+
|
|
192
|
+
SERVERLESS_WARNING_ISSUED.value = true
|
|
193
|
+
|
|
194
|
+
const prefix = platform !== null ? `[Supatype] Serverless environment detected (${platform}). ` : "[Supatype] "
|
|
195
|
+
console.warn(
|
|
196
|
+
prefix +
|
|
197
|
+
"Direct database connections are not recommended in serverless environments. " +
|
|
198
|
+
"Use the Supatype client SDK instead."
|
|
199
|
+
)
|
|
200
|
+
}
|