@squasher-ai/browser-logger 0.2.0 → 0.3.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/result-runtime/attempt.d.ts +17 -0
- package/dist/_vendor/result-runtime/attempt.js +62 -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/browser-logger.d.ts +0 -1
- package/dist/browser-logger.js +1 -1
- package/dist/browser-logger.umd.js +1 -1
- package/dist/index.d.ts +0 -1
- package/dist/serializer.d.ts +0 -1
- package/dist/serializer.js +1 -1
- package/dist/transport.d.ts +0 -1
- package/dist/transport.js +2 -2
- package/dist/types.d.ts +0 -1
- package/package.json +2 -14
- package/dist/browser-logger.d.ts.map +0 -1
- package/dist/global.d.ts +0 -14
- package/dist/global.d.ts.map +0 -1
- package/dist/global.js +0 -19
- package/dist/index.d.ts.map +0 -1
- package/dist/serializer.d.ts.map +0 -1
- package/dist/transport.d.ts.map +0 -1
- package/dist/types.d.ts.map +0 -1
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export interface AttemptFailure<TCause = Error> {
|
|
2
|
+
cause: TCause;
|
|
3
|
+
error: Error;
|
|
4
|
+
}
|
|
5
|
+
export interface AttemptOptions<TTry, TCatch = TTry, TCause = Error> {
|
|
6
|
+
try: () => TTry;
|
|
7
|
+
catch: (failure: AttemptFailure<TCause>) => TCatch;
|
|
8
|
+
finally?: () => void;
|
|
9
|
+
}
|
|
10
|
+
export interface AttemptAsyncOptions<TTry, TCatch = TTry, TCause = Error> {
|
|
11
|
+
try: () => Promise<TTry>;
|
|
12
|
+
catch: (failure: AttemptFailure<TCause>) => Promise<TCatch> | TCatch;
|
|
13
|
+
finally?: () => Promise<void> | void;
|
|
14
|
+
}
|
|
15
|
+
export declare function toError<T>(cause: T, fallbackMessage?: string): Error;
|
|
16
|
+
export declare function attempt<TTry, TCatch = TTry, TCause = Error>(options: AttemptOptions<TTry, TCatch, TCause>): TTry | TCatch;
|
|
17
|
+
export declare function attemptAsync<TTry, TCatch = TTry, TCause = Error>(options: AttemptAsyncOptions<TTry, TCatch, TCause>): Promise<TTry | TCatch>;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
export function toError(cause, fallbackMessage = "Unexpected error") {
|
|
2
|
+
if (cause instanceof Error)
|
|
3
|
+
return cause;
|
|
4
|
+
const nestedError = readProperty(cause, "error");
|
|
5
|
+
if (nestedError instanceof Error)
|
|
6
|
+
return nestedError;
|
|
7
|
+
const messageValue = readProperty(cause, "message");
|
|
8
|
+
if (Object.prototype.toString.call(messageValue) === "[object String]") {
|
|
9
|
+
const message = String(messageValue);
|
|
10
|
+
if (message.length > 0)
|
|
11
|
+
return new Error(message);
|
|
12
|
+
}
|
|
13
|
+
if (Object.prototype.toString.call(cause) === "[object String]") {
|
|
14
|
+
const message = String(cause);
|
|
15
|
+
if (message.length > 0)
|
|
16
|
+
return new Error(message);
|
|
17
|
+
}
|
|
18
|
+
return new Error(fallbackMessage, { cause });
|
|
19
|
+
}
|
|
20
|
+
function readProperty(owner, key) {
|
|
21
|
+
try {
|
|
22
|
+
const target = Object(owner);
|
|
23
|
+
let cursor = target;
|
|
24
|
+
while (cursor) {
|
|
25
|
+
const descriptor = Object.getOwnPropertyDescriptor(cursor, key);
|
|
26
|
+
if (descriptor) {
|
|
27
|
+
return "value" in descriptor ? descriptor.value : descriptor.get?.call(target);
|
|
28
|
+
}
|
|
29
|
+
cursor = Object.getPrototypeOf(cursor);
|
|
30
|
+
}
|
|
31
|
+
return undefined;
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
return undefined;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
export function attempt(options) {
|
|
38
|
+
try {
|
|
39
|
+
return options.try();
|
|
40
|
+
}
|
|
41
|
+
catch (cause) {
|
|
42
|
+
// SAFETY: `TCause` is the caller-selected type for the catch channel.
|
|
43
|
+
const preservedCause = cause;
|
|
44
|
+
return options.catch({ cause: preservedCause, error: toError(preservedCause) });
|
|
45
|
+
}
|
|
46
|
+
finally {
|
|
47
|
+
options.finally?.();
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
export async function attemptAsync(options) {
|
|
51
|
+
try {
|
|
52
|
+
return await options.try();
|
|
53
|
+
}
|
|
54
|
+
catch (cause) {
|
|
55
|
+
// SAFETY: `TCause` is the caller-selected type for the catch channel.
|
|
56
|
+
const preservedCause = cause;
|
|
57
|
+
return await options.catch({ cause: preservedCause, error: toError(preservedCause) });
|
|
58
|
+
}
|
|
59
|
+
finally {
|
|
60
|
+
await options.finally?.();
|
|
61
|
+
}
|
|
62
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Batching primitives used by Squasher SDKs.
|
|
3
|
+
*
|
|
4
|
+
* The 1 MB default keeps request bodies bounded while amortizing per-request
|
|
5
|
+
* overhead. Callers may choose a lower limit when their runtime requires one.
|
|
6
|
+
*/
|
|
7
|
+
/** Maximum serialized SDK batch body, in bytes. */
|
|
8
|
+
export declare const MAX_BATCH_BODY_BYTES = 1000000;
|
|
9
|
+
export interface BuildBatchesOptions<TItem> {
|
|
10
|
+
/**
|
|
11
|
+
* Static JSON immediately before the comma-separated serialized items.
|
|
12
|
+
* Keeping the envelope split lets the builder serialize each item once
|
|
13
|
+
* instead of repeatedly serializing a growing candidate array.
|
|
14
|
+
*/
|
|
15
|
+
batchPrefix: string;
|
|
16
|
+
/** Static JSON immediately after the serialized items. */
|
|
17
|
+
batchSuffix: string;
|
|
18
|
+
/**
|
|
19
|
+
* Serialize a single item (no wrapper). Called when a batch of size 1 is
|
|
20
|
+
* flushed. Lets SDKs send `{...event}` instead of `{events: [{...}]}` for
|
|
21
|
+
* single-event flushes — half the bytes for the common case.
|
|
22
|
+
*/
|
|
23
|
+
serializeSingle: (item: TItem) => string;
|
|
24
|
+
/** Override the default 1_000_000-byte ceiling (for tests). */
|
|
25
|
+
maxBatchBodyBytes?: number;
|
|
26
|
+
}
|
|
27
|
+
export interface BuiltBatch {
|
|
28
|
+
/** The exact request body (already serialized). */
|
|
29
|
+
body: string;
|
|
30
|
+
/** Number of items in this batch. */
|
|
31
|
+
itemCount: number;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Greedy batching: walk the items in order, adding each to the current batch
|
|
35
|
+
* unless doing so would exceed `MAX_BATCH_BODY_BYTES`. When it would, flush
|
|
36
|
+
* the current batch and start a new one with the offending item.
|
|
37
|
+
*
|
|
38
|
+
* An item that is itself larger than the limit becomes a single-item batch
|
|
39
|
+
* regardless — callers are expected to drop or split oversized items at a
|
|
40
|
+
* higher layer; this function does not silently drop data.
|
|
41
|
+
*/
|
|
42
|
+
export declare function buildBatches<TItem>(items: TItem[], options: BuildBatchesOptions<TItem>): BuiltBatch[];
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Batching primitives used by Squasher SDKs.
|
|
3
|
+
*
|
|
4
|
+
* The 1 MB default keeps request bodies bounded while amortizing per-request
|
|
5
|
+
* overhead. Callers may choose a lower limit when their runtime requires one.
|
|
6
|
+
*/
|
|
7
|
+
/** Maximum serialized SDK batch body, in bytes. */
|
|
8
|
+
export const MAX_BATCH_BODY_BYTES = 1_000_000;
|
|
9
|
+
const BODY_ENCODER = new TextEncoder();
|
|
10
|
+
/**
|
|
11
|
+
* Greedy batching: walk the items in order, adding each to the current batch
|
|
12
|
+
* unless doing so would exceed `MAX_BATCH_BODY_BYTES`. When it would, flush
|
|
13
|
+
* the current batch and start a new one with the offending item.
|
|
14
|
+
*
|
|
15
|
+
* An item that is itself larger than the limit becomes a single-item batch
|
|
16
|
+
* regardless — callers are expected to drop or split oversized items at a
|
|
17
|
+
* higher layer; this function does not silently drop data.
|
|
18
|
+
*/
|
|
19
|
+
export function buildBatches(items, options) {
|
|
20
|
+
const max = options.maxBatchBodyBytes ?? MAX_BATCH_BODY_BYTES;
|
|
21
|
+
const batches = [];
|
|
22
|
+
let current = [];
|
|
23
|
+
const envelopeBytes = BODY_ENCODER.encode(options.batchPrefix).length +
|
|
24
|
+
BODY_ENCODER.encode(options.batchSuffix).length;
|
|
25
|
+
let currentBodyBytes = envelopeBytes;
|
|
26
|
+
const flush = () => {
|
|
27
|
+
if (current.length === 0)
|
|
28
|
+
return;
|
|
29
|
+
const body = current.length === 1
|
|
30
|
+
? current[0]
|
|
31
|
+
: `${options.batchPrefix}${current.join(",")}${options.batchSuffix}`;
|
|
32
|
+
batches.push({ body, itemCount: current.length });
|
|
33
|
+
current = [];
|
|
34
|
+
currentBodyBytes = envelopeBytes;
|
|
35
|
+
};
|
|
36
|
+
for (const item of items) {
|
|
37
|
+
const serialized = options.serializeSingle(item);
|
|
38
|
+
const serializedBytes = BODY_ENCODER.encode(serialized).length;
|
|
39
|
+
const separatorBytes = current.length === 0 ? 0 : 1;
|
|
40
|
+
if (current.length > 0 && currentBodyBytes + separatorBytes + serializedBytes > max) {
|
|
41
|
+
flush();
|
|
42
|
+
}
|
|
43
|
+
currentBodyBytes += (current.length === 0 ? 0 : 1) + serializedBytes;
|
|
44
|
+
current.push(serialized);
|
|
45
|
+
}
|
|
46
|
+
flush();
|
|
47
|
+
return batches;
|
|
48
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export type HeaderMap = Record<string, string>;
|
|
2
|
+
export interface HeaderLike {
|
|
3
|
+
get(name: string): string | null;
|
|
4
|
+
}
|
|
5
|
+
export type HeaderSource = HeaderLike | HeaderMap;
|
|
6
|
+
export declare function readHeader(headers: HeaderSource | undefined, name: string): string | undefined;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
export function readHeader(headers, name) {
|
|
2
|
+
if (!headers)
|
|
3
|
+
return undefined;
|
|
4
|
+
const get = readProperty(headers, "get");
|
|
5
|
+
if (isHeaderGetter(get)) {
|
|
6
|
+
try {
|
|
7
|
+
const value = get.call(headers, name);
|
|
8
|
+
return Object.prototype.toString.call(value) === "[object String]"
|
|
9
|
+
? String(value)
|
|
10
|
+
: undefined;
|
|
11
|
+
}
|
|
12
|
+
catch {
|
|
13
|
+
return undefined;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
const normalizedName = name.toLowerCase();
|
|
17
|
+
for (const [key, value] of Object.entries(Object(headers))) {
|
|
18
|
+
if (key.toLowerCase() === normalizedName &&
|
|
19
|
+
Object.prototype.toString.call(value) === "[object String]") {
|
|
20
|
+
return String(value);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return undefined;
|
|
24
|
+
}
|
|
25
|
+
function isHeaderGetter(value) {
|
|
26
|
+
return Object.prototype.toString.call(value) === "[object Function]";
|
|
27
|
+
}
|
|
28
|
+
function readProperty(owner, key) {
|
|
29
|
+
try {
|
|
30
|
+
const target = Object(owner);
|
|
31
|
+
let cursor = target;
|
|
32
|
+
while (cursor) {
|
|
33
|
+
const descriptor = Object.getOwnPropertyDescriptor(cursor, key);
|
|
34
|
+
if (descriptor) {
|
|
35
|
+
return "value" in descriptor ? descriptor.value : descriptor.get?.call(target);
|
|
36
|
+
}
|
|
37
|
+
cursor = Object.getPrototypeOf(cursor);
|
|
38
|
+
}
|
|
39
|
+
return undefined;
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
return undefined;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -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;
|