@squasher-ai/nextjs 0.3.0 → 0.4.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/_vendor/node-sdk/actionable-telemetry.d.ts +4 -0
- package/dist/_vendor/node-sdk/actionable-telemetry.js +32 -0
- package/dist/_vendor/node-sdk/aws-lambda.d.ts +19 -0
- package/dist/_vendor/node-sdk/aws-lambda.js +81 -0
- package/dist/_vendor/node-sdk/client.d.ts +54 -0
- package/dist/_vendor/node-sdk/client.js +479 -0
- package/dist/_vendor/node-sdk/delivery-stats.d.ts +40 -0
- package/dist/_vendor/node-sdk/delivery-stats.js +57 -0
- package/dist/_vendor/node-sdk/http.d.ts +22 -0
- package/dist/_vendor/node-sdk/http.js +139 -0
- package/dist/_vendor/node-sdk/index.d.ts +36 -0
- package/dist/_vendor/node-sdk/index.js +84 -0
- package/dist/_vendor/node-sdk/local-events/sink.d.ts +6 -0
- package/dist/_vendor/node-sdk/local-events/sink.js +43 -0
- package/dist/_vendor/node-sdk/trace-context.d.ts +8 -0
- package/dist/_vendor/node-sdk/trace-context.js +13 -0
- package/dist/_vendor/node-sdk/types.d.ts +65 -0
- package/dist/_vendor/node-sdk/types.js +1 -0
- package/dist/_vendor/sdk-runtime/batching/index.d.ts +42 -0
- package/dist/_vendor/sdk-runtime/batching/index.js +48 -0
- package/dist/_vendor/sdk-runtime/errors/headers.d.ts +6 -0
- package/dist/_vendor/sdk-runtime/errors/headers.js +44 -0
- package/dist/_vendor/sdk-runtime/errors/index.d.ts +95 -0
- package/dist/_vendor/sdk-runtime/errors/index.js +157 -0
- package/dist/_vendor/sdk-runtime/headers.d.ts +48 -0
- package/dist/_vendor/sdk-runtime/headers.js +67 -0
- package/dist/_vendor/sdk-runtime/platform.d.ts +33 -0
- package/dist/_vendor/sdk-runtime/platform.js +173 -0
- package/dist/_vendor/sdk-runtime/retry.d.ts +50 -0
- package/dist/_vendor/sdk-runtime/retry.js +104 -0
- package/dist/_vendor/sdk-runtime/runtime/actionable-error.d.ts +37 -0
- package/dist/_vendor/sdk-runtime/runtime/actionable-error.js +123 -0
- package/dist/_vendor/sdk-runtime/runtime/environment.d.ts +18 -0
- package/dist/_vendor/sdk-runtime/runtime/environment.js +81 -0
- package/dist/_vendor/sdk-runtime/runtime/public-sdk-runtime.d.ts +12 -0
- package/dist/_vendor/sdk-runtime/runtime/public-sdk-runtime.js +38 -0
- package/dist/_vendor/sdk-runtime/runtime/redaction.d.ts +14 -0
- package/dist/_vendor/sdk-runtime/runtime/redaction.js +120 -0
- package/dist/_vendor/sdk-runtime/runtime/release.d.ts +35 -0
- package/dist/_vendor/sdk-runtime/runtime/release.js +120 -0
- package/dist/_vendor/sdk-runtime/sampling/index.d.ts +43 -0
- package/dist/_vendor/sdk-runtime/sampling/index.js +70 -0
- package/dist/api-handler.d.ts +1 -1
- package/dist/api-handler.js +1 -1
- package/dist/client.d.ts +1 -1
- package/dist/client.js +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/middleware.js +1 -1
- package/package.json +1 -4
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Typed error hierarchy for Squasher SDKs. Status-specific subclasses retain
|
|
3
|
+
* `SquasherApiError` as their common parent.
|
|
4
|
+
*/
|
|
5
|
+
import { type HeaderSource } from "./headers.js";
|
|
6
|
+
export type { HeaderLike } from "./headers.js";
|
|
7
|
+
export interface ParsedErrorBody {
|
|
8
|
+
/** Squasher API convention: `{ error: "human readable message" }`. */
|
|
9
|
+
error?: string;
|
|
10
|
+
/** Optional structured message supplied by an API response. */
|
|
11
|
+
message?: string;
|
|
12
|
+
/** Optional structured error code supplied by an API response. */
|
|
13
|
+
code?: string;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Lookup that works for both the WHATWG `Headers` instance and a plain
|
|
17
|
+
* `Record<string, string>`. The api-client used to be Headers-only; SDK code
|
|
18
|
+
* sometimes hands us a plain object, so accept both.
|
|
19
|
+
*/
|
|
20
|
+
/** Root of every Squasher SDK error. Lets callers `catch (e: SquasherError)`. */
|
|
21
|
+
export declare class SquasherError extends Error {
|
|
22
|
+
constructor(message: string);
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Base class for every error returned by the Squasher API. Status-specific
|
|
26
|
+
* subclasses extend this; checks against `SquasherApiError` continue to match
|
|
27
|
+
* any of them.
|
|
28
|
+
*/
|
|
29
|
+
export declare class SquasherApiError extends SquasherError {
|
|
30
|
+
readonly status: number;
|
|
31
|
+
readonly body: ParsedErrorBody | null;
|
|
32
|
+
readonly requestId: string | undefined;
|
|
33
|
+
constructor(status: number, body: ParsedErrorBody | null, message: string, headers?: HeaderSource);
|
|
34
|
+
/**
|
|
35
|
+
* Map an HTTP status to the most specific subclass. Falls back to the
|
|
36
|
+
* generic `SquasherApiError` for unrecognised statuses (e.g. 418).
|
|
37
|
+
* Network/abort failures throw `SquasherConnectionError` /
|
|
38
|
+
* `SquasherUserAbortError` instead — those never go through here.
|
|
39
|
+
*/
|
|
40
|
+
static generate(status: number, body: ParsedErrorBody | null, fallbackMessage: string, headers?: HeaderSource): SquasherApiError;
|
|
41
|
+
}
|
|
42
|
+
export declare class BadRequestError extends SquasherApiError {
|
|
43
|
+
readonly status: 400;
|
|
44
|
+
constructor(body: ParsedErrorBody | null, message: string, headers?: HeaderSource);
|
|
45
|
+
}
|
|
46
|
+
export declare class AuthenticationError extends SquasherApiError {
|
|
47
|
+
readonly status: 401;
|
|
48
|
+
constructor(body: ParsedErrorBody | null, message: string, headers?: HeaderSource);
|
|
49
|
+
}
|
|
50
|
+
export declare class PermissionDeniedError extends SquasherApiError {
|
|
51
|
+
readonly status: 403;
|
|
52
|
+
constructor(body: ParsedErrorBody | null, message: string, headers?: HeaderSource);
|
|
53
|
+
}
|
|
54
|
+
export declare class NotFoundError extends SquasherApiError {
|
|
55
|
+
readonly status: 404;
|
|
56
|
+
constructor(body: ParsedErrorBody | null, message: string, headers?: HeaderSource);
|
|
57
|
+
}
|
|
58
|
+
export declare class ConflictError extends SquasherApiError {
|
|
59
|
+
readonly status: 409;
|
|
60
|
+
constructor(body: ParsedErrorBody | null, message: string, headers?: HeaderSource);
|
|
61
|
+
}
|
|
62
|
+
export declare class UnprocessableEntityError extends SquasherApiError {
|
|
63
|
+
readonly status: 422;
|
|
64
|
+
constructor(body: ParsedErrorBody | null, message: string, headers?: HeaderSource);
|
|
65
|
+
}
|
|
66
|
+
export declare class RateLimitError extends SquasherApiError {
|
|
67
|
+
readonly status: 429;
|
|
68
|
+
constructor(body: ParsedErrorBody | null, message: string, headers?: HeaderSource);
|
|
69
|
+
}
|
|
70
|
+
/** 5xx — keeps the actual status because the spread is too wide for a const. */
|
|
71
|
+
export declare class InternalServerError extends SquasherApiError {
|
|
72
|
+
constructor(status: number, body: ParsedErrorBody | null, message: string, headers?: HeaderSource);
|
|
73
|
+
}
|
|
74
|
+
/** Network-level failure (DNS, connection refused, TLS). */
|
|
75
|
+
export declare class SquasherConnectionError extends SquasherError {
|
|
76
|
+
readonly cause?: Error | undefined;
|
|
77
|
+
constructor(message: string, cause?: Error | undefined);
|
|
78
|
+
}
|
|
79
|
+
/** Specifically a connect-or-read timeout. Subclasses ConnectionError on purpose. */
|
|
80
|
+
export declare class SquasherConnectionTimeoutError extends SquasherConnectionError {
|
|
81
|
+
constructor(message?: string);
|
|
82
|
+
}
|
|
83
|
+
/** AbortController.abort() raised; surfaced separately so retry loops skip it. */
|
|
84
|
+
export declare class SquasherUserAbortError extends SquasherError {
|
|
85
|
+
constructor(message?: string);
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Thrown when a Standard Webhooks payload fails signature verification —
|
|
89
|
+
* unknown or rotated secret, tampered body, replayed/expired timestamp,
|
|
90
|
+
* or malformed signature header. Extends SquasherError directly (not
|
|
91
|
+
* SquasherApiError) because webhook failures are not HTTP responses.
|
|
92
|
+
*/
|
|
93
|
+
export declare class InvalidWebhookSignatureError extends SquasherError {
|
|
94
|
+
constructor(message: string);
|
|
95
|
+
}
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Typed error hierarchy for Squasher SDKs. Status-specific subclasses retain
|
|
3
|
+
* `SquasherApiError` as their common parent.
|
|
4
|
+
*/
|
|
5
|
+
import { readHeader } from "./headers.js";
|
|
6
|
+
/**
|
|
7
|
+
* Lookup that works for both the WHATWG `Headers` instance and a plain
|
|
8
|
+
* `Record<string, string>`. The api-client used to be Headers-only; SDK code
|
|
9
|
+
* sometimes hands us a plain object, so accept both.
|
|
10
|
+
*/
|
|
11
|
+
/** Root of every Squasher SDK error. Lets callers `catch (e: SquasherError)`. */
|
|
12
|
+
export class SquasherError extends Error {
|
|
13
|
+
constructor(message) {
|
|
14
|
+
super(message);
|
|
15
|
+
this.name = "SquasherError";
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Base class for every error returned by the Squasher API. Status-specific
|
|
20
|
+
* subclasses extend this; checks against `SquasherApiError` continue to match
|
|
21
|
+
* any of them.
|
|
22
|
+
*/
|
|
23
|
+
export class SquasherApiError extends SquasherError {
|
|
24
|
+
status;
|
|
25
|
+
body;
|
|
26
|
+
requestId;
|
|
27
|
+
constructor(status, body, message, headers) {
|
|
28
|
+
super(buildMessage(body, message));
|
|
29
|
+
this.name = "SquasherApiError";
|
|
30
|
+
this.status = status;
|
|
31
|
+
this.body = body;
|
|
32
|
+
this.requestId = readHeader(headers, "x-request-id");
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Map an HTTP status to the most specific subclass. Falls back to the
|
|
36
|
+
* generic `SquasherApiError` for unrecognised statuses (e.g. 418).
|
|
37
|
+
* Network/abort failures throw `SquasherConnectionError` /
|
|
38
|
+
* `SquasherUserAbortError` instead — those never go through here.
|
|
39
|
+
*/
|
|
40
|
+
static generate(status, body, fallbackMessage, headers) {
|
|
41
|
+
if (status === 400)
|
|
42
|
+
return new BadRequestError(body, fallbackMessage, headers);
|
|
43
|
+
if (status === 401)
|
|
44
|
+
return new AuthenticationError(body, fallbackMessage, headers);
|
|
45
|
+
if (status === 403)
|
|
46
|
+
return new PermissionDeniedError(body, fallbackMessage, headers);
|
|
47
|
+
if (status === 404)
|
|
48
|
+
return new NotFoundError(body, fallbackMessage, headers);
|
|
49
|
+
if (status === 409)
|
|
50
|
+
return new ConflictError(body, fallbackMessage, headers);
|
|
51
|
+
if (status === 422)
|
|
52
|
+
return new UnprocessableEntityError(body, fallbackMessage, headers);
|
|
53
|
+
if (status === 429)
|
|
54
|
+
return new RateLimitError(body, fallbackMessage, headers);
|
|
55
|
+
if (status >= 500)
|
|
56
|
+
return new InternalServerError(status, body, fallbackMessage, headers);
|
|
57
|
+
return new SquasherApiError(status, body, fallbackMessage, headers);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
function buildMessage(body, fallback) {
|
|
61
|
+
if (body?.error)
|
|
62
|
+
return body.error;
|
|
63
|
+
if (body?.message)
|
|
64
|
+
return body.message;
|
|
65
|
+
return fallback;
|
|
66
|
+
}
|
|
67
|
+
export class BadRequestError extends SquasherApiError {
|
|
68
|
+
status = 400;
|
|
69
|
+
constructor(body, message, headers) {
|
|
70
|
+
super(400, body, message, headers);
|
|
71
|
+
this.name = "BadRequestError";
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
export class AuthenticationError extends SquasherApiError {
|
|
75
|
+
status = 401;
|
|
76
|
+
constructor(body, message, headers) {
|
|
77
|
+
super(401, body, message, headers);
|
|
78
|
+
this.name = "AuthenticationError";
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
export class PermissionDeniedError extends SquasherApiError {
|
|
82
|
+
status = 403;
|
|
83
|
+
constructor(body, message, headers) {
|
|
84
|
+
super(403, body, message, headers);
|
|
85
|
+
this.name = "PermissionDeniedError";
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
export class NotFoundError extends SquasherApiError {
|
|
89
|
+
status = 404;
|
|
90
|
+
constructor(body, message, headers) {
|
|
91
|
+
super(404, body, message, headers);
|
|
92
|
+
this.name = "NotFoundError";
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
export class ConflictError extends SquasherApiError {
|
|
96
|
+
status = 409;
|
|
97
|
+
constructor(body, message, headers) {
|
|
98
|
+
super(409, body, message, headers);
|
|
99
|
+
this.name = "ConflictError";
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
export class UnprocessableEntityError extends SquasherApiError {
|
|
103
|
+
status = 422;
|
|
104
|
+
constructor(body, message, headers) {
|
|
105
|
+
super(422, body, message, headers);
|
|
106
|
+
this.name = "UnprocessableEntityError";
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
export class RateLimitError extends SquasherApiError {
|
|
110
|
+
status = 429;
|
|
111
|
+
constructor(body, message, headers) {
|
|
112
|
+
super(429, body, message, headers);
|
|
113
|
+
this.name = "RateLimitError";
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
/** 5xx — keeps the actual status because the spread is too wide for a const. */
|
|
117
|
+
export class InternalServerError extends SquasherApiError {
|
|
118
|
+
constructor(status, body, message, headers) {
|
|
119
|
+
super(status, body, message, headers);
|
|
120
|
+
this.name = "InternalServerError";
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
/** Network-level failure (DNS, connection refused, TLS). */
|
|
124
|
+
export class SquasherConnectionError extends SquasherError {
|
|
125
|
+
cause;
|
|
126
|
+
constructor(message, cause) {
|
|
127
|
+
super(message);
|
|
128
|
+
this.cause = cause;
|
|
129
|
+
this.name = "SquasherConnectionError";
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
/** Specifically a connect-or-read timeout. Subclasses ConnectionError on purpose. */
|
|
133
|
+
export class SquasherConnectionTimeoutError extends SquasherConnectionError {
|
|
134
|
+
constructor(message = "Request timed out") {
|
|
135
|
+
super(message);
|
|
136
|
+
this.name = "SquasherConnectionTimeoutError";
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
/** AbortController.abort() raised; surfaced separately so retry loops skip it. */
|
|
140
|
+
export class SquasherUserAbortError extends SquasherError {
|
|
141
|
+
constructor(message = "Request was aborted") {
|
|
142
|
+
super(message);
|
|
143
|
+
this.name = "SquasherUserAbortError";
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Thrown when a Standard Webhooks payload fails signature verification —
|
|
148
|
+
* unknown or rotated secret, tampered body, replayed/expired timestamp,
|
|
149
|
+
* or malformed signature header. Extends SquasherError directly (not
|
|
150
|
+
* SquasherApiError) because webhook failures are not HTTP responses.
|
|
151
|
+
*/
|
|
152
|
+
export class InvalidWebhookSignatureError extends SquasherError {
|
|
153
|
+
constructor(message) {
|
|
154
|
+
super(message);
|
|
155
|
+
this.name = "InvalidWebhookSignatureError";
|
|
156
|
+
}
|
|
157
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Default telemetry headers attached to every SDK-originated HTTP request.
|
|
3
|
+
*
|
|
4
|
+
* Every request identifies the language, package, package version, OS,
|
|
5
|
+
* architecture, and runtime. Retry count and timeout are added per request.
|
|
6
|
+
*/
|
|
7
|
+
export interface DefaultHeadersOptions {
|
|
8
|
+
/** npm package name (e.g. "@squasher-ai/client", "@squasher-ai/node"). */
|
|
9
|
+
packageName: string;
|
|
10
|
+
/** Semver of the package shipping the request. */
|
|
11
|
+
packageVersion: string;
|
|
12
|
+
}
|
|
13
|
+
export interface RequestHeaderOptions {
|
|
14
|
+
/** Retry attempt count. Set to 0 on first attempt; incremented per retry. */
|
|
15
|
+
retryCount?: number;
|
|
16
|
+
/** Per-request timeout in milliseconds, surfaced as seconds in the header. */
|
|
17
|
+
timeoutMs?: number;
|
|
18
|
+
}
|
|
19
|
+
export interface TelemetryHeaders {
|
|
20
|
+
[name: string]: string;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Build the constant-per-process header set: lang, package, OS, arch, runtime.
|
|
24
|
+
* Cache the return value at the SDK-construction site rather than calling per
|
|
25
|
+
* request — platform detection is cached but header construction still
|
|
26
|
+
* allocates a fresh object.
|
|
27
|
+
*/
|
|
28
|
+
export declare function getDefaultHeaders(opts: DefaultHeadersOptions): TelemetryHeaders;
|
|
29
|
+
/**
|
|
30
|
+
* Build the per-request header overlay: retry count and timeout. Returned as
|
|
31
|
+
* a separate object so callers can spread it on top of `getDefaultHeaders()`.
|
|
32
|
+
* Omits any field whose source is undefined so we don't ship empty headers.
|
|
33
|
+
*/
|
|
34
|
+
export declare function getRequestHeaders(opts: RequestHeaderOptions): TelemetryHeaders;
|
|
35
|
+
/** Build the SDK User-Agent string used by standard HTTP access logs. */
|
|
36
|
+
export declare function getUserAgent(opts: DefaultHeadersOptions): string;
|
|
37
|
+
/** Header names used by SDK tests and request integrations. */
|
|
38
|
+
export declare const HEADER_NAMES: {
|
|
39
|
+
readonly lang: "X-Squasher-Lang";
|
|
40
|
+
readonly package: "X-Squasher-Package";
|
|
41
|
+
readonly packageVersion: "X-Squasher-Package-Version";
|
|
42
|
+
readonly os: "X-Squasher-OS";
|
|
43
|
+
readonly arch: "X-Squasher-Arch";
|
|
44
|
+
readonly runtime: "X-Squasher-Runtime";
|
|
45
|
+
readonly runtimeVersion: "X-Squasher-Runtime-Version";
|
|
46
|
+
readonly retryCount: "X-Squasher-Retry-Count";
|
|
47
|
+
readonly timeout: "X-Squasher-Timeout";
|
|
48
|
+
};
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Default telemetry headers attached to every SDK-originated HTTP request.
|
|
3
|
+
*
|
|
4
|
+
* Every request identifies the language, package, package version, OS,
|
|
5
|
+
* architecture, and runtime. Retry count and timeout are added per request.
|
|
6
|
+
*/
|
|
7
|
+
import { getPlatform } from "./platform.js";
|
|
8
|
+
const HEADER_LANG = "X-Squasher-Lang";
|
|
9
|
+
const HEADER_PACKAGE = "X-Squasher-Package";
|
|
10
|
+
const HEADER_PACKAGE_VERSION = "X-Squasher-Package-Version";
|
|
11
|
+
const HEADER_OS = "X-Squasher-OS";
|
|
12
|
+
const HEADER_ARCH = "X-Squasher-Arch";
|
|
13
|
+
const HEADER_RUNTIME = "X-Squasher-Runtime";
|
|
14
|
+
const HEADER_RUNTIME_VERSION = "X-Squasher-Runtime-Version";
|
|
15
|
+
const HEADER_RETRY_COUNT = "X-Squasher-Retry-Count";
|
|
16
|
+
const HEADER_TIMEOUT = "X-Squasher-Timeout";
|
|
17
|
+
/**
|
|
18
|
+
* Build the constant-per-process header set: lang, package, OS, arch, runtime.
|
|
19
|
+
* Cache the return value at the SDK-construction site rather than calling per
|
|
20
|
+
* request — platform detection is cached but header construction still
|
|
21
|
+
* allocates a fresh object.
|
|
22
|
+
*/
|
|
23
|
+
export function getDefaultHeaders(opts) {
|
|
24
|
+
const platform = getPlatform();
|
|
25
|
+
return {
|
|
26
|
+
[HEADER_LANG]: platform.lang,
|
|
27
|
+
[HEADER_PACKAGE]: opts.packageName,
|
|
28
|
+
[HEADER_PACKAGE_VERSION]: opts.packageVersion,
|
|
29
|
+
[HEADER_OS]: platform.os,
|
|
30
|
+
[HEADER_ARCH]: platform.arch,
|
|
31
|
+
[HEADER_RUNTIME]: platform.runtime,
|
|
32
|
+
[HEADER_RUNTIME_VERSION]: platform.runtimeVersion,
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Build the per-request header overlay: retry count and timeout. Returned as
|
|
37
|
+
* a separate object so callers can spread it on top of `getDefaultHeaders()`.
|
|
38
|
+
* Omits any field whose source is undefined so we don't ship empty headers.
|
|
39
|
+
*/
|
|
40
|
+
export function getRequestHeaders(opts) {
|
|
41
|
+
const out = {};
|
|
42
|
+
if (opts.retryCount !== undefined && opts.retryCount > 0) {
|
|
43
|
+
out[HEADER_RETRY_COUNT] = String(opts.retryCount);
|
|
44
|
+
}
|
|
45
|
+
if (opts.timeoutMs !== undefined && opts.timeoutMs > 0) {
|
|
46
|
+
// Express the timeout in seconds with millisecond precision.
|
|
47
|
+
out[HEADER_TIMEOUT] = (opts.timeoutMs / 1000).toString();
|
|
48
|
+
}
|
|
49
|
+
return out;
|
|
50
|
+
}
|
|
51
|
+
/** Build the SDK User-Agent string used by standard HTTP access logs. */
|
|
52
|
+
export function getUserAgent(opts) {
|
|
53
|
+
const { runtime, runtimeVersion, os, arch } = getPlatform();
|
|
54
|
+
return `${opts.packageName}/${opts.packageVersion} (${runtime}/${runtimeVersion}; ${os} ${arch})`;
|
|
55
|
+
}
|
|
56
|
+
/** Header names used by SDK tests and request integrations. */
|
|
57
|
+
export const HEADER_NAMES = {
|
|
58
|
+
lang: HEADER_LANG,
|
|
59
|
+
package: HEADER_PACKAGE,
|
|
60
|
+
packageVersion: HEADER_PACKAGE_VERSION,
|
|
61
|
+
os: HEADER_OS,
|
|
62
|
+
arch: HEADER_ARCH,
|
|
63
|
+
runtime: HEADER_RUNTIME,
|
|
64
|
+
runtimeVersion: HEADER_RUNTIME_VERSION,
|
|
65
|
+
retryCount: HEADER_RETRY_COUNT,
|
|
66
|
+
timeout: HEADER_TIMEOUT,
|
|
67
|
+
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Runtime, OS, and architecture detection used by SDK request headers.
|
|
3
|
+
*
|
|
4
|
+
* Probes are wrapped in try/catch because locked-down runtimes can throw when
|
|
5
|
+
* global properties are read. Unknown values remain explicit.
|
|
6
|
+
*/
|
|
7
|
+
export type PlatformRuntime = "node" | "deno" | "bun" | "edge" | "workerd" | `browser:${string}` | "unknown";
|
|
8
|
+
export type PlatformOs = "MacOS" | "Linux" | "Windows" | "FreeBSD" | "OpenBSD" | "iOS" | "Android" | "Unknown";
|
|
9
|
+
export type PlatformArch = "x32" | "x64" | "arm" | "arm64" | "Unknown";
|
|
10
|
+
export interface PlatformProperties {
|
|
11
|
+
/** Language family. Always "js" for any TypeScript/JavaScript SDK. */
|
|
12
|
+
lang: "js";
|
|
13
|
+
/** Concrete runtime identifier. */
|
|
14
|
+
runtime: PlatformRuntime;
|
|
15
|
+
/** Best-effort runtime version string ("20.10.0", "1.0.30", etc.) or "unknown". */
|
|
16
|
+
runtimeVersion: string;
|
|
17
|
+
/** Operating system. */
|
|
18
|
+
os: PlatformOs;
|
|
19
|
+
/** CPU architecture. */
|
|
20
|
+
arch: PlatformArch;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Detect the runtime, OS, arch, and version of the host JS environment.
|
|
24
|
+
*
|
|
25
|
+
* Detection order: Deno → Bun → Cloudflare workerd → generic Edge → Node →
|
|
26
|
+
* Browser → unknown. Earlier checks take precedence because some runtimes
|
|
27
|
+
* (Bun, Deno) also expose a `process` global for compatibility — we want the
|
|
28
|
+
* native identifier to win.
|
|
29
|
+
*/
|
|
30
|
+
export declare function detectPlatform(): PlatformProperties;
|
|
31
|
+
export declare function getPlatform(): PlatformProperties;
|
|
32
|
+
/** Reset cached detection for deterministic tests. */
|
|
33
|
+
export declare function resetPlatformCacheForTesting(): void;
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Runtime, OS, and architecture detection used by SDK request headers.
|
|
3
|
+
*
|
|
4
|
+
* Probes are wrapped in try/catch because locked-down runtimes can throw when
|
|
5
|
+
* global properties are read. Unknown values remain explicit.
|
|
6
|
+
*/
|
|
7
|
+
import { readGlobalProperty, readRuntimeProperty, readRuntimeString, } from "./runtime/environment.js";
|
|
8
|
+
function nestedString(value, ...path) {
|
|
9
|
+
let current = value;
|
|
10
|
+
for (const name of path)
|
|
11
|
+
current = readRuntimeProperty(current, name);
|
|
12
|
+
return readRuntimeString(current);
|
|
13
|
+
}
|
|
14
|
+
function normalizeOs(platform) {
|
|
15
|
+
if (!platform)
|
|
16
|
+
return "Unknown";
|
|
17
|
+
if (platform === "darwin")
|
|
18
|
+
return "MacOS";
|
|
19
|
+
if (platform === "linux")
|
|
20
|
+
return "Linux";
|
|
21
|
+
if (platform === "win32")
|
|
22
|
+
return "Windows";
|
|
23
|
+
if (platform === "freebsd")
|
|
24
|
+
return "FreeBSD";
|
|
25
|
+
if (platform === "openbsd")
|
|
26
|
+
return "OpenBSD";
|
|
27
|
+
if (platform === "android")
|
|
28
|
+
return "Android";
|
|
29
|
+
return "Unknown";
|
|
30
|
+
}
|
|
31
|
+
function normalizeArch(arch) {
|
|
32
|
+
if (!arch)
|
|
33
|
+
return "Unknown";
|
|
34
|
+
if (arch === "x32" || arch === "ia32")
|
|
35
|
+
return "x32";
|
|
36
|
+
if (arch === "x64" || arch === "amd64")
|
|
37
|
+
return "x64";
|
|
38
|
+
if (arch === "arm")
|
|
39
|
+
return "arm";
|
|
40
|
+
if (arch === "arm64" || arch === "aarch64")
|
|
41
|
+
return "arm64";
|
|
42
|
+
return "Unknown";
|
|
43
|
+
}
|
|
44
|
+
const BROWSER_UA_PATTERNS = [
|
|
45
|
+
{ key: "edge", re: /Edg(?:e|A|iOS)?\/(\d+(?:\.\d+)?)/ },
|
|
46
|
+
{ key: "ie", re: /MSIE (\d+(?:\.\d+)?)/ },
|
|
47
|
+
{ key: "ie", re: /Trident\/.*; rv:(\d+(?:\.\d+)?)/ },
|
|
48
|
+
{ key: "chrome", re: /Chrome\/(\d+(?:\.\d+)?)/ },
|
|
49
|
+
{ key: "firefox", re: /Firefox\/(\d+(?:\.\d+)?)/ },
|
|
50
|
+
{ key: "safari", re: /Version\/(\d+(?:\.\d+)?).*Safari/ },
|
|
51
|
+
];
|
|
52
|
+
function detectBrowserFromUserAgent(ua) {
|
|
53
|
+
let os = "Unknown";
|
|
54
|
+
if (/Mac OS X/.test(ua))
|
|
55
|
+
os = "MacOS";
|
|
56
|
+
else if (/Windows/.test(ua))
|
|
57
|
+
os = "Windows";
|
|
58
|
+
else if (/Linux/.test(ua))
|
|
59
|
+
os = "Linux";
|
|
60
|
+
else if (/Android/.test(ua))
|
|
61
|
+
os = "Android";
|
|
62
|
+
else if (/iP(hone|ad|od)/.test(ua))
|
|
63
|
+
os = "iOS";
|
|
64
|
+
for (const { key, re } of BROWSER_UA_PATTERNS) {
|
|
65
|
+
const match = re.exec(ua);
|
|
66
|
+
if (match) {
|
|
67
|
+
return { runtime: `browser:${key}`, version: match[1] ?? "unknown", os };
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return undefined;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Detect the runtime, OS, arch, and version of the host JS environment.
|
|
74
|
+
*
|
|
75
|
+
* Detection order: Deno → Bun → Cloudflare workerd → generic Edge → Node →
|
|
76
|
+
* Browser → unknown. Earlier checks take precedence because some runtimes
|
|
77
|
+
* (Bun, Deno) also expose a `process` global for compatibility — we want the
|
|
78
|
+
* native identifier to win.
|
|
79
|
+
*/
|
|
80
|
+
export function detectPlatform() {
|
|
81
|
+
// Deno
|
|
82
|
+
const deno = readGlobalProperty("Deno");
|
|
83
|
+
if (deno !== undefined && deno !== null) {
|
|
84
|
+
return {
|
|
85
|
+
lang: "js",
|
|
86
|
+
runtime: "deno",
|
|
87
|
+
runtimeVersion: nestedString(deno, "version", "deno") ?? "unknown",
|
|
88
|
+
os: normalizeOs(nestedString(deno, "build", "os")),
|
|
89
|
+
arch: normalizeArch(nestedString(deno, "build", "arch")),
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
// Bun
|
|
93
|
+
const bun = readGlobalProperty("Bun");
|
|
94
|
+
if (bun !== undefined && bun !== null) {
|
|
95
|
+
const process = readGlobalProperty("process");
|
|
96
|
+
return {
|
|
97
|
+
lang: "js",
|
|
98
|
+
runtime: "bun",
|
|
99
|
+
runtimeVersion: nestedString(bun, "version") ?? "unknown",
|
|
100
|
+
os: normalizeOs(nestedString(process, "platform")),
|
|
101
|
+
arch: normalizeArch(nestedString(process, "arch")),
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
// Cloudflare workerd (preferred over generic edge — gives us version when present)
|
|
105
|
+
const navigatorUserAgent = nestedString(readGlobalProperty("navigator"), "userAgent");
|
|
106
|
+
if (navigatorUserAgent && /Cloudflare-Workers/.test(navigatorUserAgent)) {
|
|
107
|
+
return {
|
|
108
|
+
lang: "js",
|
|
109
|
+
runtime: "workerd",
|
|
110
|
+
runtimeVersion: "unknown",
|
|
111
|
+
os: "Unknown",
|
|
112
|
+
arch: "Unknown",
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
// Generic edge (Vercel Edge, Next.js edge runtime, etc.)
|
|
116
|
+
const edgeRuntime = readGlobalProperty("EdgeRuntime");
|
|
117
|
+
if (edgeRuntime !== undefined) {
|
|
118
|
+
return {
|
|
119
|
+
lang: "js",
|
|
120
|
+
runtime: "edge",
|
|
121
|
+
runtimeVersion: readRuntimeString(edgeRuntime) ?? "unknown",
|
|
122
|
+
os: "Unknown",
|
|
123
|
+
arch: "Unknown",
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
// Node.js — duck-typed via release.name to avoid false positives on
|
|
127
|
+
// bundlers that polyfill `process` for browser code.
|
|
128
|
+
const process = readGlobalProperty("process");
|
|
129
|
+
if (nestedString(process, "release", "name") === "node") {
|
|
130
|
+
return {
|
|
131
|
+
lang: "js",
|
|
132
|
+
runtime: "node",
|
|
133
|
+
runtimeVersion: nestedString(process, "versions", "node") ?? nestedString(process, "version") ?? "unknown",
|
|
134
|
+
os: normalizeOs(nestedString(process, "platform")),
|
|
135
|
+
arch: normalizeArch(nestedString(process, "arch")),
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
// Browser
|
|
139
|
+
if (navigatorUserAgent) {
|
|
140
|
+
const match = detectBrowserFromUserAgent(navigatorUserAgent);
|
|
141
|
+
if (match) {
|
|
142
|
+
return {
|
|
143
|
+
lang: "js",
|
|
144
|
+
runtime: match.runtime,
|
|
145
|
+
runtimeVersion: match.version,
|
|
146
|
+
os: match.os,
|
|
147
|
+
arch: "Unknown",
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
return {
|
|
152
|
+
lang: "js",
|
|
153
|
+
runtime: "unknown",
|
|
154
|
+
runtimeVersion: "unknown",
|
|
155
|
+
os: "Unknown",
|
|
156
|
+
arch: "Unknown",
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Cached platform properties — detection is stable across a process lifetime,
|
|
161
|
+
* so probing once and reusing the result keeps the per-request cost at zero.
|
|
162
|
+
* Tests that need a fresh probe can call `resetPlatformCacheForTesting()`.
|
|
163
|
+
*/
|
|
164
|
+
let cached;
|
|
165
|
+
export function getPlatform() {
|
|
166
|
+
if (!cached)
|
|
167
|
+
cached = detectPlatform();
|
|
168
|
+
return cached;
|
|
169
|
+
}
|
|
170
|
+
/** Reset cached detection for deterministic tests. */
|
|
171
|
+
export function resetPlatformCacheForTesting() {
|
|
172
|
+
cached = undefined;
|
|
173
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Retry primitives shared across Squasher SDKs.
|
|
3
|
+
*
|
|
4
|
+
* Server retry hints take precedence over exponential backoff with downward
|
|
5
|
+
* jitter. Aborts are never retried; network failures, timeouts, 408, 429, and
|
|
6
|
+
* 5xx responses are retryable by default.
|
|
7
|
+
*/
|
|
8
|
+
import { type HeaderSource } from "./errors/headers.js";
|
|
9
|
+
export interface RetryDecisionInput {
|
|
10
|
+
/** HTTP response status, if a response was received. */
|
|
11
|
+
status?: number;
|
|
12
|
+
/** Response headers — supports either a fetch `Headers` or plain object. */
|
|
13
|
+
headers?: HeaderSource;
|
|
14
|
+
/** Error thrown by `fetch` itself (network failure / abort). */
|
|
15
|
+
thrown?: Error;
|
|
16
|
+
}
|
|
17
|
+
export type HeadersLike = Headers;
|
|
18
|
+
/**
|
|
19
|
+
* Decide whether a response (or thrown error) should trigger a retry.
|
|
20
|
+
*
|
|
21
|
+
* Aborts always opt out — `AbortController.abort()` is user intent, not a
|
|
22
|
+
* transient failure. Connection errors always opt in. Status-code logic
|
|
23
|
+
* mirrors Stainless: 408/409/429 plus any 5xx, with `X-Should-Retry`
|
|
24
|
+
* override.
|
|
25
|
+
*/
|
|
26
|
+
export declare function shouldRetry(input: RetryDecisionInput): boolean;
|
|
27
|
+
export interface BackoffOptions {
|
|
28
|
+
/** Zero-indexed retry attempt number. The first retry uses attempt = 0. */
|
|
29
|
+
attempt: number;
|
|
30
|
+
/** Response headers, used to honor Retry-After / Retry-After-Ms. */
|
|
31
|
+
headers?: HeaderSource;
|
|
32
|
+
/** Random source — injected for deterministic tests. Defaults to Math.random. */
|
|
33
|
+
random?: () => number;
|
|
34
|
+
/** Max sleep in ms. Defaults to 8000 to match Stainless. */
|
|
35
|
+
maxSleepMs?: number;
|
|
36
|
+
/** Reference time used to interpret HTTP-date `Retry-After` values. Tests inject. */
|
|
37
|
+
now?: () => number;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Compute the delay in milliseconds before the next attempt. Server-supplied
|
|
41
|
+
* hints win when present, otherwise falls back to exponential backoff with
|
|
42
|
+
* downward jitter.
|
|
43
|
+
*
|
|
44
|
+
* Returned value is always non-negative. Capped at `maxSleepMs` (default 8s)
|
|
45
|
+
* even when the server requests a longer retry — long sleeps almost always
|
|
46
|
+
* indicate the caller should reach for queueing instead of in-process retries.
|
|
47
|
+
*/
|
|
48
|
+
export declare function nextBackoffMs(opts: BackoffOptions): number;
|
|
49
|
+
/** Promise-friendly setTimeout. Resolves after `ms` milliseconds. */
|
|
50
|
+
export declare function sleep(ms: number, signal?: AbortSignal): Promise<void>;
|