@atproto/lex-client 0.0.17 → 0.0.19

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/errors.js CHANGED
@@ -1,12 +1,33 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.XrpcFetchError = exports.XrpcInternalError = exports.XrpcInvalidResponseError = exports.XrpcUpstreamError = exports.XrpcAuthenticationError = exports.XrpcResponseError = exports.XrpcError = exports.LexError = exports.RETRYABLE_HTTP_STATUS_CODES = void 0;
3
+ exports.XrpcFetchError = exports.XrpcInternalError = exports.XrpcResponseValidationError = exports.XrpcInvalidResponseError = exports.XrpcAuthenticationError = exports.XrpcResponseError = exports.XrpcError = exports.LexError = exports.RETRYABLE_HTTP_STATUS_CODES = void 0;
4
4
  exports.isXrpcErrorPayload = isXrpcErrorPayload;
5
5
  exports.asXrpcFailure = asXrpcFailure;
6
6
  const lex_data_1 = require("@atproto/lex-data");
7
7
  Object.defineProperty(exports, "LexError", { enumerable: true, get: function () { return lex_data_1.LexError; } });
8
8
  const lex_schema_1 = require("@atproto/lex-schema");
9
9
  const www_authenticate_js_1 = require("./www-authenticate.js");
10
+ /**
11
+ * Mapping that allows generating an XRPC error code from an HTTP status code
12
+ * when the response does not contain a valid XRPC error payload. This is used
13
+ * to convert non-XRPC error responses from upstream servers into a standardized
14
+ * XRPC error for downstream clients.
15
+ */
16
+ const StatusErrorCodes = new Map([
17
+ [400, 'InvalidRequest'],
18
+ [401, 'AuthenticationRequired'],
19
+ [403, 'Forbidden'],
20
+ [404, 'XRPCNotSupported'],
21
+ [406, 'NotAcceptable'],
22
+ [413, 'PayloadTooLarge'],
23
+ [415, 'UnsupportedMediaType'],
24
+ [429, 'RateLimitExceeded'],
25
+ [500, 'InternalServerError'],
26
+ [501, 'MethodNotImplemented'],
27
+ [502, 'UpstreamFailure'],
28
+ [503, 'NotEnoughResources'],
29
+ [504, 'UpstreamTimeout'],
30
+ ]);
10
31
  /**
11
32
  * HTTP status codes that indicate a transient error that may succeed on retry.
12
33
  *
@@ -52,7 +73,7 @@ function isXrpcErrorPayload(payload) {
52
73
  * @typeParam TReason - The reason type for ResultFailure
53
74
  *
54
75
  * @see {@link XrpcResponseError} - For valid XRPC error responses
55
- * @see {@link XrpcUpstreamError} - For invalid/unexpected responses
76
+ * @see {@link XrpcInvalidResponseError} - For invalid/unexpected responses
56
77
  * @see {@link XrpcInternalError} - For network/internal errors
57
78
  */
58
79
  class XrpcError extends lex_data_1.LexError {
@@ -100,7 +121,13 @@ class XrpcResponseError extends XrpcError {
100
121
  payload;
101
122
  name = 'XrpcResponseError';
102
123
  constructor(method, response, payload, options) {
103
- const { error, message } = payload.body;
124
+ const { error, message } = isXrpcErrorPayload(payload)
125
+ ? payload.body
126
+ : {
127
+ error: StatusErrorCodes.get(response.status) ??
128
+ (response.status >= 500 ? 'UpstreamFailure' : 'InvalidRequest'),
129
+ message: buildResponseOverviewMessage(response),
130
+ };
104
131
  super(method, error, message, options);
105
132
  this.response = response;
106
133
  this.payload = payload;
@@ -112,17 +139,24 @@ class XrpcResponseError extends XrpcError {
112
139
  return exports.RETRYABLE_HTTP_STATUS_CODES.has(this.response.status);
113
140
  }
114
141
  toJSON() {
115
- return this.payload.body;
142
+ // Return the original error payload if it's a valid XRPC error, otherwise
143
+ // convert to an XRPC error format.
144
+ const { payload } = this;
145
+ if (isXrpcErrorPayload(payload)) {
146
+ return payload.body;
147
+ }
148
+ return super.toJSON();
116
149
  }
117
150
  toDownstreamError() {
118
- // If the upstream server returned a 5xx error, we want to return a 502 Bad
151
+ const { status, headers } = this.response;
152
+ // If the upstream server returned a 500 error, we want to return a 502 Bad
119
153
  // Gateway to downstream clients, as the issue is with the upstream server,
120
154
  // not us. We still return the original error code and message in the body
121
155
  // for transparency, but we do not want to expose internal server errors
122
156
  // from the upstream server as-is to downstream clients.
123
157
  return {
124
- status: this.response.status === 500 ? 502 : this.status,
125
- headers: stripHopByHopHeaders(this.headers),
158
+ status: status === 500 ? 502 : status,
159
+ headers: stripHopByHopHeaders(headers),
126
160
  body: this.toJSON(),
127
161
  };
128
162
  }
@@ -133,7 +167,7 @@ class XrpcResponseError extends XrpcError {
133
167
  return this.response.headers;
134
168
  }
135
169
  get body() {
136
- return this.payload.body;
170
+ return this.payload?.body;
137
171
  }
138
172
  }
139
173
  exports.XrpcResponseError = XrpcResponseError;
@@ -177,13 +211,6 @@ class XrpcAuthenticationError extends XrpcResponseError {
177
211
  return (this.#wwwAuthenticateCached ??=
178
212
  (0, www_authenticate_js_1.parseWWWAuthenticateHeader)(this.response.headers.get('www-authenticate')) ?? {});
179
213
  }
180
- toDownstreamError() {
181
- return {
182
- status: 401,
183
- headers: stripHopByHopHeaders(this.headers),
184
- body: this.toJSON(),
185
- };
186
- }
187
214
  }
188
215
  exports.XrpcAuthenticationError = XrpcAuthenticationError;
189
216
  /**
@@ -196,17 +223,18 @@ exports.XrpcAuthenticationError = XrpcAuthenticationError;
196
223
  * - Non-JSON error responses
197
224
  * - Responses from non-XRPC endpoints
198
225
  *
199
- * The error code is always 'UpstreamFailure' and maps to HTTP 502 Bad Gateway
200
- * when converted to a response.
226
+ * The error code is always 'InvalidResponse' and maps to HTTP 502 Bad Gateway
227
+ * when converted to a response. This should allow downstream clients to
228
+ * determine at which boundary the error occurred.
201
229
  *
202
230
  * @typeParam M - The XRPC method type
203
231
  */
204
- class XrpcUpstreamError extends XrpcError {
232
+ class XrpcInvalidResponseError extends XrpcError {
205
233
  response;
206
234
  payload;
207
- name = 'XrpcUpstreamError';
208
- constructor(method, response, payload = null, message = `Unexpected upstream XRPC response`, options) {
209
- super(method, 'UpstreamFailure', message, options);
235
+ name = 'XrpcInvalidResponseError';
236
+ constructor(method, response, payload, message = buildResponseOverviewMessage(response), options) {
237
+ super(method, 'InvalidResponse', message, options);
210
238
  this.response = response;
211
239
  this.payload = payload;
212
240
  }
@@ -220,11 +248,11 @@ class XrpcUpstreamError extends XrpcError {
220
248
  return { status: 502, body: this.toJSON() };
221
249
  }
222
250
  }
223
- exports.XrpcUpstreamError = XrpcUpstreamError;
251
+ exports.XrpcInvalidResponseError = XrpcInvalidResponseError;
224
252
  /**
225
253
  * Error class for invalid XRPC responses that fail schema validation.
226
254
  *
227
- * This is a specific type of {@link XrpcUpstreamError} that indicates the
255
+ * This is a specific type of {@link XrpcInvalidResponseError} that indicates the
228
256
  * upstream server returned a response that was structurally valid but did not
229
257
  * conform to the expected schema for the method. This likely indicates a
230
258
  * mismatch between client and server versions or an issue with the server's
@@ -232,24 +260,15 @@ exports.XrpcUpstreamError = XrpcUpstreamError;
232
260
  *
233
261
  * @typeParam M - The XRPC method type
234
262
  */
235
- class XrpcInvalidResponseError extends XrpcUpstreamError {
263
+ class XrpcResponseValidationError extends XrpcInvalidResponseError {
236
264
  cause;
237
- name = 'XrpcInvalidResponseError';
265
+ name = 'XrpcResponseValidationError';
238
266
  constructor(method, response, payload, cause) {
239
- super(method, response, payload, `Invalid response: ${cause.message}`, {
240
- cause,
241
- });
267
+ super(method, response, payload, `Invalid response payload: ${cause.message}`, { cause });
242
268
  this.cause = cause;
243
269
  }
244
- toDownstreamError() {
245
- // @NOTE This could be reflected as both a 500 ("we" are at fault) and 502
246
- // ("they" are at fault). We are using 502 here to allow downstream clients
247
- // to determine that the issue lies at the interface between us and the
248
- // upstream server, rather than an issue with our internal processing.
249
- return { status: 502, body: this.toJSON() };
250
- }
251
270
  }
252
- exports.XrpcInvalidResponseError = XrpcInvalidResponseError;
271
+ exports.XrpcResponseValidationError = XrpcResponseValidationError;
253
272
  /**
254
273
  * Error class for unexpected internal/client-side errors during XRPC requests.
255
274
  *
@@ -338,7 +357,7 @@ exports.XrpcFetchError = XrpcFetchError;
338
357
  */
339
358
  function asXrpcFailure(method, cause) {
340
359
  if (cause instanceof XrpcResponseError ||
341
- cause instanceof XrpcUpstreamError ||
360
+ cause instanceof XrpcInvalidResponseError ||
342
361
  cause instanceof XrpcInternalError) {
343
362
  if (cause.method === method)
344
363
  return cause;
@@ -375,4 +394,10 @@ function stripHopByHopHeaders(headers) {
375
394
  result.delete('content-encoding');
376
395
  return result;
377
396
  }
397
+ function buildResponseOverviewMessage(response) {
398
+ if (response.status < 400) {
399
+ return `Upstream server responded with an invalid status code (${response.status})`;
400
+ }
401
+ return `Upstream server responded with a ${response.status} error`;
402
+ }
378
403
  //# sourceMappingURL=errors.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":";;;AAuEA,gDAQC;AAoZD,sCAaC;AAhfD,gDAAwE;AA4C/D,yFA5CA,mBAAQ,OA4CA;AA3CjB,oDAO4B;AAI5B,+DAG8B;AAW9B;;;;;;;;;;;;;GAaG;AACU,QAAA,2BAA2B,GAAwB,IAAI,GAAG,CAAC;IACtE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG;CAC5C,CAAC,CAAA;AAiBF;;;;;;;;;;;GAWG;AACH,SAAgB,kBAAkB,CAChC,OAAsD;IAEtD,OAAO,CACL,OAAO,IAAI,IAAI;QACf,OAAO,CAAC,QAAQ,KAAK,kBAAkB;QACvC,+BAAkB,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,CACzC,CAAA;AACH,CAAC;AAED;;;;;;;;;;;;;GAaG;AACH,MAAsB,SAKpB,SAAQ,mBAAW;IAMR;IAHX,IAAI,GAAG,WAAW,CAAA;IAElB,YACW,MAAS,EAClB,KAAQ,EACR,UAAkB,GAAG,KAAK,oBAAoB,EAC9C,OAAsB;QAEtB,KAAK,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,CAAC,CAAA;QALrB,WAAM,GAAN,MAAM,CAAG;IAMpB,CAAC;IAED;;OAEG;IACM,OAAO,GAAG,KAAc,CAAA;IAcjC,mBAAmB;QACjB,OAAO,IAAI,CAAC,MAAM,CAAC,MAAM,E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S;CACV,CAAC,CAAA;AAEF,SAAS,oBAAoB,CAAC,OAAgB;IAC5C,MAAM,MAAM,GAAG,IAAI,OAAO,CAAC,OAAO,CAAC,CAAA;IAEnC,6CAA6C;IAC7C,KAAK,MAAM,IAAI,IAAI,kBAAkB,EAAE,CAAC;QACtC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;IACrB,CAAC;IAED,mDAAmD;IACnD,MAAM,UAAU,GAAG,OAAO,CAAC,GAAG,CAAC,YAAY,CAAC,CAAA;IAC5C,IAAI,UAAU,EAAE,CAAC;QACf,KAAK,MAAM,IAAI,IAAI,UAAU,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE,CAAC;YACzC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC,CAAA;QAC5B,CAAC;IACH,CAAC;IAED,4EAA4E;IAC5E,wEAAwE;IACxE,sCAAsC;IACtC,MAAM,CAAC,MAAM,CAAC,gBAAgB,CAAC,CAAA;IAC/B,MAAM,CAAC,MAAM,CAAC,kBAAkB,CAAC,CAAA;IAEjC,OAAO,MAAM,CAAA;AACf,CAAC","sourcesContent":["import { LexError, LexErrorCode, LexErrorData } from '@atproto/lex-data'\nimport {\n InferMethodError,\n LexValidationError,\n Procedure,\n Query,\n ResultFailure,\n lexErrorDataSchema,\n} from '@atproto/lex-schema'\n// eslint-disable-next-line @typescript-eslint/no-unused-vars\nimport { Agent } from './agent.js'\nimport { XrpcUnknownResponsePayload } from './types.js'\nimport {\n WWWAuthenticate,\n parseWWWAuthenticateHeader,\n} from './www-authenticate.js'\n\nexport type { XrpcUnknownResponsePayload }\n\nexport type DownstreamError<N extends LexErrorCode = LexErrorCode> = {\n status: number\n headers?: Headers\n encoding?: 'application/json'\n body: LexErrorData<N>\n}\n\n/**\n * HTTP status codes that indicate a transient error that may succeed on retry.\n *\n * Includes:\n * - 408 Request Timeout\n * - 425 Too Early\n * - 429 Too Many Requests (rate limited)\n * - 500 Internal Server Error\n * - 502 Bad Gateway\n * - 503 Service Unavailable\n * - 504 Gateway Timeout\n * - 522 Connection Timed Out (Cloudflare)\n * - 524 A Timeout Occurred (Cloudflare)\n */\nexport const RETRYABLE_HTTP_STATUS_CODES: ReadonlySet<number> = new Set([\n 408, 425, 429, 500, 502, 503, 504, 522, 524,\n])\n\nexport { LexError }\nexport type { LexErrorCode, LexErrorData }\n\n/**\n * The payload structure for XRPC error responses.\n *\n * All XRPC errors return JSON with an `error` code and optional `message`.\n *\n * @typeParam N - The specific error code type\n */\nexport type XrpcErrorPayload<N extends LexErrorCode = LexErrorCode> = {\n body: LexErrorData<N>\n encoding: 'application/json'\n}\n\n/**\n * All unsuccessful responses should follow a standard error response\n * schema. The Content-Type should be application/json, and the payload\n * should be a JSON object with the following fields:\n *\n * - `error` (string, required): type name of the error (generic ASCII\n * constant, no whitespace)\n * - `message` (string, optional): description of the error, appropriate for\n * display to humans\n *\n * This function checks whether a given payload matches this schema.\n */\nexport function isXrpcErrorPayload(\n payload: XrpcUnknownResponsePayload | null | undefined,\n): payload is XrpcErrorPayload {\n return (\n payload != null &&\n payload.encoding === 'application/json' &&\n lexErrorDataSchema.matches(payload.body)\n )\n}\n\n/**\n * Abstract base class for all XRPC errors.\n *\n * Extends {@link LexError} and implements {@link ResultFailure} for use with\n * safe/result-based error handling patterns.\n *\n * @typeParam M - The XRPC method type (Procedure or Query)\n * @typeParam N - The error code type\n * @typeParam TReason - The reason type for ResultFailure\n *\n * @see {@link XrpcResponseError} - For valid XRPC error responses\n * @see {@link XrpcUpstreamError} - For invalid/unexpected responses\n * @see {@link XrpcInternalError} - For network/internal errors\n */\nexport abstract class XrpcError<\n M extends Procedure | Query = Procedure | Query,\n N extends LexErrorCode = LexErrorCode,\n TReason = unknown,\n >\n extends LexError<N>\n implements ResultFailure<TReason>\n{\n name = 'XrpcError'\n\n constructor(\n readonly method: M,\n error: N,\n message: string = `${error} Lexicon RPC error`,\n options?: ErrorOptions,\n ) {\n super(error, message, options)\n }\n\n /**\n * @see {@link ResultFailure.success}\n */\n readonly success = false as const\n\n /**\n * @see {@link ResultFailure.reason}\n */\n abstract readonly reason: TReason\n\n /**\n * Indicates whether the error is transient and can be retried.\n */\n abstract shouldRetry(): boolean\n\n abstract toDownstreamError(): DownstreamError\n\n matchesSchemaErrors(): this is XrpcError<M, InferMethodError<M>> {\n return this.method.errors?.includes(this.error) ?? false\n }\n}\n\n/**\n * Error class for valid XRPC error responses from the server.\n *\n * This represents a properly formatted XRPC error where the server returned\n * a non-2xx status with a valid JSON error payload containing `error` and\n * optional `message` fields.\n *\n * Use {@link matchesSchemaErrors} to check if the error matches the method's declared\n * error types for type-safe error handling.\n *\n * @typeParam M - The XRPC method type\n * @typeParam N - The error code type (inferred from method or generic)\n *\n * @example Handling specific errors\n * ```typescript\n * try {\n * await client.xrpc(someMethod, options)\n * } catch (err) {\n * if (err instanceof XrpcResponseError && err.error === 'RecordNotFound') {\n * // Handle not found case\n * }\n * }\n * ```\n */\nexport class XrpcResponseError<\n M extends Procedure | Query = Procedure | Query,\n N extends LexErrorCode = InferMethodError<M> | LexErrorCode,\n> extends XrpcError<M, N, XrpcResponseError<M, N>> {\n name = 'XrpcResponseError'\n\n constructor(\n method: M,\n readonly response: Response,\n readonly payload: XrpcErrorPayload<N>,\n options?: ErrorOptions,\n ) {\n const { error, message } = payload.body\n super(method, error, message, options)\n }\n\n override get reason(): this {\n return this\n }\n\n override shouldRetry(): boolean {\n return RETRYABLE_HTTP_STATUS_CODES.has(this.response.status)\n }\n\n override toJSON(): LexErrorData<N> {\n return this.payload.body\n }\n\n override toDownstreamError(): DownstreamError {\n // If the upstream server returned a 5xx error, we want to return a 502 Bad\n // Gateway to downstream clients, as the issue is with the upstream server,\n // not us. We still return the original error code and message in the body\n // for transparency, but we do not want to expose internal server errors\n // from the upstream server as-is to downstream clients.\n return {\n status: this.response.status === 500 ? 502 : this.status,\n headers: stripHopByHopHeaders(this.headers),\n body: this.toJSON(),\n }\n }\n\n get status(): number {\n return this.response.status\n }\n\n get headers(): Headers {\n return this.response.headers\n }\n\n get body(): LexErrorData<N> {\n return this.payload.body\n }\n}\n\nexport type { WWWAuthenticate }\n\n/**\n * Error class for 401 Unauthorized XRPC responses.\n *\n * Extends {@link XrpcResponseError} with access to parsed WWW-Authenticate header\n * information, useful for implementing authentication flows.\n *\n * Authentication errors are never retryable as they require user intervention\n * (e.g., re-authentication, token refresh).\n *\n * @typeParam M - The XRPC method type\n * @typeParam N - The error code type\n *\n * @example Handling authentication errors\n * ```typescript\n * try {\n * await client.xrpc(someMethod, options)\n * } catch (err) {\n * if (err instanceof XrpcAuthenticationError) {\n * const { DPoP } = err.wwwAuthenticate\n * if (DPoP?.error === 'use_dpop_nonce') {\n * // Handle DPoP nonce requirement\n * }\n * }\n * }\n * ```\n */\nexport class XrpcAuthenticationError<\n M extends Procedure | Query = Procedure | Query,\n N extends LexErrorCode = LexErrorCode,\n> extends XrpcResponseError<M, N> {\n name = 'XrpcAuthenticationError'\n\n override shouldRetry(): boolean {\n return false\n }\n\n #wwwAuthenticateCached?: WWWAuthenticate\n /**\n * Parsed WWW-Authenticate header from the response.\n * Contains authentication scheme parameters (e.g., Bearer realm, DPoP nonce).\n */\n get wwwAuthenticate(): WWWAuthenticate {\n return (this.#wwwAuthenticateCached ??=\n parseWWWAuthenticateHeader(\n this.response.headers.get('www-authenticate'),\n ) ?? {})\n }\n\n override toDownstreamError(): DownstreamError {\n return {\n status: 401,\n headers: stripHopByHopHeaders(this.headers),\n body: this.toJSON(),\n }\n }\n}\n\n/**\n * Error class for invalid or unprocessable XRPC responses from upstream servers.\n *\n * This occurs when the server returns a response that doesn't conform to the\n * XRPC protocol, such as:\n * - Missing or invalid Content-Type header\n * - Response body that doesn't match the method's output schema\n * - Non-JSON error responses\n * - Responses from non-XRPC endpoints\n *\n * The error code is always 'UpstreamFailure' and maps to HTTP 502 Bad Gateway\n * when converted to a response.\n *\n * @typeParam M - The XRPC method type\n */\nexport class XrpcUpstreamError<\n M extends Procedure | Query = Procedure | Query,\n> extends XrpcError<M, 'UpstreamFailure', XrpcUpstreamError<M>> {\n name = 'XrpcUpstreamError'\n\n constructor(\n method: M,\n readonly response: Response,\n readonly payload: XrpcUnknownResponsePayload | null = null,\n message: string = `Unexpected upstream XRPC response`,\n options?: ErrorOptions,\n ) {\n super(method, 'UpstreamFailure', message, options)\n }\n\n override get reason(): this {\n return this\n }\n\n override shouldRetry(): boolean {\n return RETRYABLE_HTTP_STATUS_CODES.has(this.response.status)\n }\n\n override toDownstreamError(): DownstreamError {\n return { status: 502, body: this.toJSON() }\n }\n}\n\n/**\n * Error class for invalid XRPC responses that fail schema validation.\n *\n * This is a specific type of {@link XrpcUpstreamError} that indicates the\n * upstream server returned a response that was structurally valid but did not\n * conform to the expected schema for the method. This likely indicates a\n * mismatch between client and server versions or an issue with the server's\n * XRPC implementation.\n *\n * @typeParam M - The XRPC method type\n */\nexport class XrpcInvalidResponseError<\n M extends Procedure | Query = Procedure | Query,\n> extends XrpcUpstreamError<M> {\n name = 'XrpcInvalidResponseError'\n\n constructor(\n method: M,\n response: Response,\n payload: XrpcUnknownResponsePayload,\n readonly cause: LexValidationError,\n ) {\n super(method, response, payload, `Invalid response: ${cause.message}`, {\n cause,\n })\n }\n\n override toDownstreamError(): DownstreamError {\n // @NOTE This could be reflected as both a 500 (\"we\" are at fault) and 502\n // (\"they\" are at fault). We are using 502 here to allow downstream clients\n // to determine that the issue lies at the interface between us and the\n // upstream server, rather than an issue with our internal processing.\n return { status: 502, body: this.toJSON() }\n }\n}\n\n/**\n * Error class for unexpected internal/client-side errors during XRPC requests.\n *\n * The error code is always 'InternalServerError' and these errors not\n * considered retryable as they stem from unforeseen issues in the\n * implementation.\n *\n * @typeParam M - The XRPC method type\n */\nexport class XrpcInternalError<\n M extends Procedure | Query = Procedure | Query,\n> extends XrpcError<M, 'InternalServerError', XrpcInternalError<M>> {\n name = 'XrpcInternalError'\n\n constructor(method: M, message?: string, options?: ErrorOptions) {\n super(\n method,\n 'InternalServerError',\n message ?? 'Unable to fulfill XRPC request',\n options,\n )\n }\n\n override get reason(): this {\n return this\n }\n\n override shouldRetry(): boolean {\n return false\n }\n\n override toJSON(): LexErrorData {\n // @NOTE Do not expose internal error details to downstream clients\n return { error: this.error, message: 'Internal Server Error' }\n }\n\n override toDownstreamError(): DownstreamError {\n return { status: 500, body: this.toJSON() }\n }\n}\n\n/**\n * Special case of XrpcInternalError that specifically represents errors thrown\n * by {@link Agent.fetchHandler} during the XRPC request. This includes:\n * - Network errors (connection refused, DNS failure)\n * - Request timeouts\n * - Request aborted via AbortSignal\n *\n * These errors are optimistically considered retryable, as many fetch errors\n * are transient and may succeed on retry.\n */\nexport class XrpcFetchError<\n M extends Procedure | Query = Procedure | Query,\n> extends XrpcInternalError<M> {\n name = 'XrpcFetchError'\n\n constructor(method: M, cause: unknown) {\n const message = cause instanceof Error ? cause.message : String(cause)\n super(method, `Unexpected fetchHandler() error: ${message}`, { cause })\n }\n\n override shouldRetry(): boolean {\n // Ideally, we would inspect the reason to determine if it's retryable (by\n // detecting network errors, timeouts, etc.). Since these cases are highly\n // platform-dependent, we optimistically assume all fetch errors are\n // transient and retryable.\n return true\n }\n\n override toJSON(): LexErrorData {\n // @NOTE Do not expose internal error details to downstream clients\n return { error: this.error, message: 'Failed to perform upstream request' }\n }\n\n override toDownstreamError(): DownstreamError {\n // While it might technically be a 500 error, we use 502 Bad Gateway here to\n // indicate that the error occurred while communicating with the upstream\n // server, allowing downstream clients to distinguish between errors in our\n // internal processing (500) and errors in the upstream server or network\n // (502).\n return { status: 502, body: this.toJSON() }\n }\n}\n\n/**\n * Union type of all possible XRPC failure types.\n *\n * Used as the return type for safe/non-throwing XRPC methods. Check the\n * `success` property to distinguish between success and failure:\n *\n * @typeParam M - The XRPC method type\n *\n * @example\n * ```typescript\n * const result = await client.xrpcSafe(someMethod, options)\n * if (result.success) {\n * console.log(result.body) // XrpcResponse\n * } else {\n * // result is XrpcFailure (XrpcResponseError | XrpcUpstreamError | XrpcInternalError)\n * console.error(result.error, result.message)\n * }\n * ```\n */\nexport type XrpcFailure<M extends Procedure | Query = Procedure | Query> =\n // The server returned a valid XRPC error response\n | XrpcResponseError<M>\n // The response was not a valid XRPC response, or it does not match the schema\n | XrpcUpstreamError<M>\n // Something went wrong (network error, etc.)\n | XrpcInternalError<M>\n\n/**\n * Converts an unknown error into an appropriate {@link XrpcFailure} type.\n *\n * If the error is already an XrpcFailure for the given method, returns it as-is.\n * Otherwise, wraps it in an {@link XrpcInternalError}.\n *\n * @param method - The XRPC method that was called\n * @param cause - The error to convert\n * @returns An XrpcFailure instance\n *\n * @example\n * ```typescript\n * try {\n * const response = await fetch(...)\n * // ... process response\n * } catch (err) {\n * return asXrpcFailure(method, err)\n * }\n * ```\n */\nexport function asXrpcFailure<M extends Procedure | Query>(\n method: M,\n cause: unknown,\n): XrpcFailure<M> {\n if (\n cause instanceof XrpcResponseError ||\n cause instanceof XrpcUpstreamError ||\n cause instanceof XrpcInternalError\n ) {\n if (cause.method === method) return cause\n }\n\n return new XrpcInternalError(method, undefined, { cause })\n}\n\nconst HOP_BY_HOP_HEADERS = new Set([\n 'connection',\n 'keep-alive',\n 'proxy-authenticate',\n 'proxy-authorization',\n 'te',\n 'trailer',\n 'transfer-encoding',\n 'upgrade',\n])\n\nfunction stripHopByHopHeaders(headers: Headers): Headers {\n const result = new Headers(headers)\n\n // Remove statically known hop-by-hop headers\n for (const name of HOP_BY_HOP_HEADERS) {\n result.delete(name)\n }\n\n // Remove headers listed in the \"Connection\" header\n const connection = headers.get('connection')\n if (connection) {\n for (const name of connection.split(',')) {\n result.delete(name.trim())\n }\n }\n\n // These are not actually hop-by-hop headers, but we remove them because the\n // upstream payload gets parsed and re-serialized, so content length and\n // encoding may no longer be accurate.\n result.delete('content-length')\n result.delete('content-encoding')\n\n return result\n}\n"]}
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":";;;AAkGA,gDAQC;AAsZD,sCA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C,QAAQ,CAAC,MAAM,QAAQ,CAAA;AACpE,CAAC","sourcesContent":["import {\n LexError,\n LexErrorCode,\n LexErrorData,\n LexValue,\n} from '@atproto/lex-data'\nimport {\n InferMethodError,\n LexValidationError,\n Procedure,\n Query,\n ResultFailure,\n lexErrorDataSchema,\n} from '@atproto/lex-schema'\n// eslint-disable-next-line @typescript-eslint/no-unused-vars\nimport { Agent } from './agent.js'\nimport { XrpcUnknownResponsePayload } from './types.js'\nimport {\n WWWAuthenticate,\n parseWWWAuthenticateHeader,\n} from './www-authenticate.js'\n\n/**\n * Mapping that allows generating an XRPC error code from an HTTP status code\n * when the response does not contain a valid XRPC error payload. This is used\n * to convert non-XRPC error responses from upstream servers into a standardized\n * XRPC error for downstream clients.\n */\nconst StatusErrorCodes = new Map<number, LexErrorCode>([\n [400, 'InvalidRequest'],\n [401, 'AuthenticationRequired'],\n [403, 'Forbidden'],\n [404, 'XRPCNotSupported'],\n [406, 'NotAcceptable'],\n [413, 'PayloadTooLarge'],\n [415, 'UnsupportedMediaType'],\n [429, 'RateLimitExceeded'],\n [500, 'InternalServerError'],\n [501, 'MethodNotImplemented'],\n [502, 'UpstreamFailure'],\n [503, 'NotEnoughResources'],\n [504, 'UpstreamTimeout'],\n])\n\nexport type { XrpcUnknownResponsePayload }\n\nexport type DownstreamError<N extends LexErrorCode = LexErrorCode> = {\n status: number\n headers?: Headers\n encoding?: 'application/json'\n body: LexErrorData<N>\n}\n\n/**\n * HTTP status codes that indicate a transient error that may succeed on retry.\n *\n * Includes:\n * - 408 Request Timeout\n * - 425 Too Early\n * - 429 Too Many Requests (rate limited)\n * - 500 Internal Server Error\n * - 502 Bad Gateway\n * - 503 Service Unavailable\n * - 504 Gateway Timeout\n * - 522 Connection Timed Out (Cloudflare)\n * - 524 A Timeout Occurred (Cloudflare)\n */\nexport const RETRYABLE_HTTP_STATUS_CODES: ReadonlySet<number> = new Set([\n 408, 425, 429, 500, 502, 503, 504, 522, 524,\n])\n\nexport { LexError }\nexport type { LexErrorCode, LexErrorData }\n\n/**\n * The payload structure for XRPC error responses.\n *\n * All XRPC errors return JSON with an `error` code and optional `message`.\n *\n * @typeParam N - The specific error code type\n */\nexport type XrpcErrorPayload<N extends LexErrorCode = LexErrorCode> = {\n body: LexErrorData<N>\n encoding: 'application/json'\n}\n\n/**\n * All unsuccessful responses should follow a standard error response\n * schema. The Content-Type should be application/json, and the payload\n * should be a JSON object with the following fields:\n *\n * - `error` (string, required): type name of the error (generic ASCII\n * constant, no whitespace)\n * - `message` (string, optional): description of the error, appropriate for\n * display to humans\n *\n * This function checks whether a given payload matches this schema.\n */\nexport function isXrpcErrorPayload(\n payload: XrpcUnknownResponsePayload | null | undefined,\n): payload is XrpcErrorPayload {\n return (\n payload != null &&\n payload.encoding === 'application/json' &&\n lexErrorDataSchema.matches(payload.body)\n )\n}\n\n/**\n * Abstract base class for all XRPC errors.\n *\n * Extends {@link LexError} and implements {@link ResultFailure} for use with\n * safe/result-based error handling patterns.\n *\n * @typeParam M - The XRPC method type (Procedure or Query)\n * @typeParam N - The error code type\n * @typeParam TReason - The reason type for ResultFailure\n *\n * @see {@link XrpcResponseError} - For valid XRPC error responses\n * @see {@link XrpcInvalidResponseError} - For invalid/unexpected responses\n * @see {@link XrpcInternalError} - For network/internal errors\n */\nexport abstract class XrpcError<\n M extends Procedure | Query = Procedure | Query,\n N extends LexErrorCode = LexErrorCode,\n TReason = unknown,\n >\n extends LexError<N>\n implements ResultFailure<TReason>\n{\n name = 'XrpcError'\n\n constructor(\n readonly method: M,\n error: N,\n message: string = `${error} Lexicon RPC error`,\n options?: ErrorOptions,\n ) {\n super(error, message, options)\n }\n\n /**\n * @see {@link ResultFailure.success}\n */\n readonly success = false as const\n\n /**\n * @see {@link ResultFailure.reason}\n */\n abstract readonly reason: TReason\n\n /**\n * Indicates whether the error is transient and can be retried.\n */\n abstract shouldRetry(): boolean\n\n abstract toDownstreamError(): DownstreamError\n\n matchesSchemaErrors(): this is XrpcError<M, InferMethodError<M>> {\n return this.method.errors?.includes(this.error) ?? false\n }\n}\n\n/**\n * Error class for valid XRPC error responses from the server.\n *\n * This represents a properly formatted XRPC error where the server returned\n * a non-2xx status with a valid JSON error payload containing `error` and\n * optional `message` fields.\n *\n * Use {@link matchesSchemaErrors} to check if the error matches the method's declared\n * error types for type-safe error handling.\n *\n * @typeParam M - The XRPC method type\n * @typeParam N - The error code type (inferred from method or generic)\n *\n * @example Handling specific errors\n * ```typescript\n * try {\n * await client.xrpc(someMethod, options)\n * } catch (err) {\n * if (err instanceof XrpcResponseError && err.error === 'RecordNotFound') {\n * // Handle not found case\n * }\n * }\n * ```\n */\nexport class XrpcResponseError<\n M extends Procedure | Query = Procedure | Query,\n> extends XrpcError<M, LexErrorCode, XrpcResponseError<M>> {\n name = 'XrpcResponseError'\n\n constructor(\n method: M,\n readonly response: Response,\n readonly payload?: XrpcUnknownResponsePayload,\n options?: ErrorOptions,\n ) {\n const { error, message } = isXrpcErrorPayload(payload)\n ? payload.body\n : {\n error:\n StatusErrorCodes.get(response.status) ??\n (response.status >= 500 ? 'UpstreamFailure' : 'InvalidRequest'),\n message: buildResponseOverviewMessage(response),\n }\n super(method, error, message, options)\n }\n\n override get reason(): this {\n return this\n }\n\n override shouldRetry(): boolean {\n return RETRYABLE_HTTP_STATUS_CODES.has(this.response.status)\n }\n\n override toJSON(): LexErrorData {\n // Return the original error payload if it's a valid XRPC error, otherwise\n // convert to an XRPC error format.\n const { payload } = this\n if (isXrpcErrorPayload(payload)) {\n return payload.body\n }\n\n return super.toJSON()\n }\n\n override toDownstreamError(): DownstreamError {\n const { status, headers } = this.response\n // If the upstream server returned a 500 error, we want to return a 502 Bad\n // Gateway to downstream clients, as the issue is with the upstream server,\n // not us. We still return the original error code and message in the body\n // for transparency, but we do not want to expose internal server errors\n // from the upstream server as-is to downstream clients.\n return {\n status: status === 500 ? 502 : status,\n headers: stripHopByHopHeaders(headers),\n body: this.toJSON(),\n }\n }\n\n get status(): number {\n return this.response.status\n }\n\n get headers(): Headers {\n return this.response.headers\n }\n\n get body(): undefined | Uint8Array | LexValue {\n return this.payload?.body\n }\n}\n\nexport type { WWWAuthenticate }\n\n/**\n * Error class for 401 Unauthorized XRPC responses.\n *\n * Extends {@link XrpcResponseError} with access to parsed WWW-Authenticate header\n * information, useful for implementing authentication flows.\n *\n * Authentication errors are never retryable as they require user intervention\n * (e.g., re-authentication, token refresh).\n *\n * @typeParam M - The XRPC method type\n * @typeParam N - The error code type\n *\n * @example Handling authentication errors\n * ```typescript\n * try {\n * await client.xrpc(someMethod, options)\n * } catch (err) {\n * if (err instanceof XrpcAuthenticationError) {\n * const { DPoP } = err.wwwAuthenticate\n * if (DPoP?.error === 'use_dpop_nonce') {\n * // Handle DPoP nonce requirement\n * }\n * }\n * }\n * ```\n */\nexport class XrpcAuthenticationError<\n M extends Procedure | Query = Procedure | Query,\n> extends XrpcResponseError<M> {\n name = 'XrpcAuthenticationError'\n\n override shouldRetry(): boolean {\n return false\n }\n\n #wwwAuthenticateCached?: WWWAuthenticate\n /**\n * Parsed WWW-Authenticate header from the response.\n * Contains authentication scheme parameters (e.g., Bearer realm, DPoP nonce).\n */\n get wwwAuthenticate(): WWWAuthenticate {\n return (this.#wwwAuthenticateCached ??=\n parseWWWAuthenticateHeader(\n this.response.headers.get('www-authenticate'),\n ) ?? {})\n }\n}\n\n/**\n * Error class for invalid or unprocessable XRPC responses from upstream servers.\n *\n * This occurs when the server returns a response that doesn't conform to the\n * XRPC protocol, such as:\n * - Missing or invalid Content-Type header\n * - Response body that doesn't match the method's output schema\n * - Non-JSON error responses\n * - Responses from non-XRPC endpoints\n *\n * The error code is always 'InvalidResponse' and maps to HTTP 502 Bad Gateway\n * when converted to a response. This should allow downstream clients to\n * determine at which boundary the error occurred.\n *\n * @typeParam M - The XRPC method type\n */\nexport class XrpcInvalidResponseError<\n M extends Procedure | Query = Procedure | Query,\n> extends XrpcError<M, 'InvalidResponse', XrpcInvalidResponseError<M>> {\n name = 'XrpcInvalidResponseError'\n\n constructor(\n method: M,\n readonly response: Response,\n readonly payload?: XrpcUnknownResponsePayload,\n message: string = buildResponseOverviewMessage(response),\n options?: ErrorOptions,\n ) {\n super(method, 'InvalidResponse', message, options)\n }\n\n override get reason(): this {\n return this\n }\n\n override shouldRetry(): boolean {\n return RETRYABLE_HTTP_STATUS_CODES.has(this.response.status)\n }\n\n override toDownstreamError(): DownstreamError {\n return { status: 502, body: this.toJSON() }\n }\n}\n\n/**\n * Error class for invalid XRPC responses that fail schema validation.\n *\n * This is a specific type of {@link XrpcInvalidResponseError} that indicates the\n * upstream server returned a response that was structurally valid but did not\n * conform to the expected schema for the method. This likely indicates a\n * mismatch between client and server versions or an issue with the server's\n * XRPC implementation.\n *\n * @typeParam M - The XRPC method type\n */\nexport class XrpcResponseValidationError<\n M extends Procedure | Query = Procedure | Query,\n> extends XrpcInvalidResponseError<M> {\n name = 'XrpcResponseValidationError'\n\n constructor(\n method: M,\n response: Response,\n payload: XrpcUnknownResponsePayload,\n readonly cause: LexValidationError,\n ) {\n super(\n method,\n response,\n payload,\n `Invalid response payload: ${cause.message}`,\n { cause },\n )\n }\n}\n\n/**\n * Error class for unexpected internal/client-side errors during XRPC requests.\n *\n * The error code is always 'InternalServerError' and these errors not\n * considered retryable as they stem from unforeseen issues in the\n * implementation.\n *\n * @typeParam M - The XRPC method type\n */\nexport class XrpcInternalError<\n M extends Procedure | Query = Procedure | Query,\n> extends XrpcError<M, 'InternalServerError', XrpcInternalError<M>> {\n name = 'XrpcInternalError'\n\n constructor(method: M, message?: string, options?: ErrorOptions) {\n super(\n method,\n 'InternalServerError',\n message ?? 'Unable to fulfill XRPC request',\n options,\n )\n }\n\n override get reason(): this {\n return this\n }\n\n override shouldRetry(): boolean {\n return false\n }\n\n override toJSON(): LexErrorData {\n // @NOTE Do not expose internal error details to downstream clients\n return { error: this.error, message: 'Internal Server Error' }\n }\n\n override toDownstreamError(): DownstreamError {\n return { status: 500, body: this.toJSON() }\n }\n}\n\n/**\n * Special case of XrpcInternalError that specifically represents errors thrown\n * by {@link Agent.fetchHandler} during the XRPC request. This includes:\n * - Network errors (connection refused, DNS failure)\n * - Request timeouts\n * - Request aborted via AbortSignal\n *\n * These errors are optimistically considered retryable, as many fetch errors\n * are transient and may succeed on retry.\n */\nexport class XrpcFetchError<\n M extends Procedure | Query = Procedure | Query,\n> extends XrpcInternalError<M> {\n name = 'XrpcFetchError'\n\n constructor(method: M, cause: unknown) {\n const message = cause instanceof Error ? cause.message : String(cause)\n super(method, `Unexpected fetchHandler() error: ${message}`, { cause })\n }\n\n override shouldRetry(): boolean {\n // Ideally, we would inspect the reason to determine if it's retryable (by\n // detecting network errors, timeouts, etc.). Since these cases are highly\n // platform-dependent, we optimistically assume all fetch errors are\n // transient and retryable.\n return true\n }\n\n override toJSON(): LexErrorData {\n // @NOTE Do not expose internal error details to downstream clients\n return { error: this.error, message: 'Failed to perform upstream request' }\n }\n\n override toDownstreamError(): DownstreamError {\n // While it might technically be a 500 error, we use 502 Bad Gateway here to\n // indicate that the error occurred while communicating with the upstream\n // server, allowing downstream clients to distinguish between errors in our\n // internal processing (500) and errors in the upstream server or network\n // (502).\n return { status: 502, body: this.toJSON() }\n }\n}\n\n/**\n * Union type of all possible XRPC failure types.\n *\n * Used as the return type for safe/non-throwing XRPC methods. Check the\n * `success` property to distinguish between success and failure:\n *\n * @typeParam M - The XRPC method type\n *\n * @example\n * ```typescript\n * const result = await client.xrpcSafe(someMethod, options)\n * if (result.success) {\n * console.log(result.body) // XrpcResponse\n * } else {\n * // result is XrpcFailure (XrpcResponseError | XrpcInvalidResponseError | XrpcInternalError)\n * console.error(result.error, result.message)\n * }\n * ```\n */\nexport type XrpcFailure<M extends Procedure | Query = Procedure | Query> =\n // The server returned a valid XRPC error response\n | XrpcResponseError<M>\n // The response was not a valid XRPC response, or it does not match the schema\n | XrpcInvalidResponseError<M>\n // Something went wrong (network error, etc.)\n | XrpcInternalError<M>\n\n/**\n * Converts an unknown error into an appropriate {@link XrpcFailure} type.\n *\n * If the error is already an XrpcFailure for the given method, returns it as-is.\n * Otherwise, wraps it in an {@link XrpcInternalError}.\n *\n * @param method - The XRPC method that was called\n * @param cause - The error to convert\n * @returns An XrpcFailure instance\n *\n * @example\n * ```typescript\n * try {\n * const response = await fetch(...)\n * // ... process response\n * } catch (err) {\n * return asXrpcFailure(method, err)\n * }\n * ```\n */\nexport function asXrpcFailure<M extends Procedure | Query>(\n method: M,\n cause: unknown,\n): XrpcFailure<M> {\n if (\n cause instanceof XrpcResponseError ||\n cause instanceof XrpcInvalidResponseError ||\n cause instanceof XrpcInternalError\n ) {\n if (cause.method === method) return cause\n }\n\n return new XrpcInternalError(method, undefined, { cause })\n}\n\nconst HOP_BY_HOP_HEADERS = new Set([\n 'connection',\n 'keep-alive',\n 'proxy-authenticate',\n 'proxy-authorization',\n 'te',\n 'trailer',\n 'transfer-encoding',\n 'upgrade',\n])\n\nfunction stripHopByHopHeaders(headers: Headers): Headers {\n const result = new Headers(headers)\n\n // Remove statically known hop-by-hop headers\n for (const name of HOP_BY_HOP_HEADERS) {\n result.delete(name)\n }\n\n // Remove headers listed in the \"Connection\" header\n const connection = headers.get('connection')\n if (connection) {\n for (const name of connection.split(',')) {\n result.delete(name.trim())\n }\n }\n\n // These are not actually hop-by-hop headers, but we remove them because the\n // upstream payload gets parsed and re-serialized, so content length and\n // encoding may no longer be accurate.\n result.delete('content-length')\n result.delete('content-encoding')\n\n return result\n}\n\nfunction buildResponseOverviewMessage(response: Response): string {\n if (response.status < 400) {\n return `Upstream server responded with an invalid status code (${response.status})`\n }\n\n return `Upstream server responded with a ${response.status} error`\n}\n"]}
@@ -3,9 +3,7 @@ declare const $nsid = "com.atproto.repo.uploadBlob";
3
3
  export { $nsid };
4
4
  /** Upload a new blob, to be referenced from a repository record. The blob will be deleted if it is not referenced within a time window (eg, minutes). Blob restrictions (mimetype, size, etc) are enforced when the reference is created. Requires auth, implemented by PDS. */
5
5
  declare const main: l.Procedure<"com.atproto.repo.uploadBlob", l.ParamsSchema<{}>, l.Payload<"*/*", undefined>, l.Payload<"application/json", l.ObjectSchema<{
6
- blob: l.BlobSchema<{
7
- allowLegacy: false;
8
- }>;
6
+ blob: l.BlobSchema<{}>;
9
7
  }>>, undefined>;
10
8
  export { main };
11
9
  export type $Params = l.InferMethodParams<typeof main>;
@@ -14,8 +12,6 @@ export type $InputBody<B = l.BinaryData> = l.InferMethodInputBody<typeof main, B
14
12
  export type $Output<B = l.BinaryData> = l.InferMethodOutput<typeof main, B>;
15
13
  export type $OutputBody<B = l.BinaryData> = l.InferMethodOutputBody<typeof main, B>;
16
14
  export declare const $lxm: "com.atproto.repo.uploadBlob", $params: l.ParamsSchema<{}>, $input: l.Payload<"*/*", undefined>, $output: l.Payload<"application/json", l.ObjectSchema<{
17
- blob: l.BlobSchema<{
18
- allowLegacy: false;
19
- }>;
15
+ blob: l.BlobSchema<{}>;
20
16
  }>>;
21
17
  //# sourceMappingURL=uploadBlob.defs.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"uploadBlob.defs.d.ts","sourceRoot":"","sources":["../../../../../src/lexicons/com/atproto/repo/uploadBlob.defs.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,CAAC,EAAE,MAAM,qBAAqB,CAAA;AAEvC,QAAA,MAAM,KAAK,gCAAgC,CAAA;AAE3C,OAAO,EAAE,KAAK,EAAE,CAAA;AAEhB,gRAAgR;AAChR,QAAA,MAAM,IAAI;;;;eASP,CAAA;AACH,OAAO,EAAE,IAAI,EAAE,CAAA;AAEf,MAAM,MAAM,OAAO,GAAG,CAAC,CAAC,iBAAiB,CAAC,OAAO,IAAI,CAAC,CAAA;AACtD,MAAM,MAAM,MAAM,CAAC,CAAC,GAAG,CAAC,CAAC,UAAU,IAAI,CAAC,CAAC,gBAAgB,CAAC,OAAO,IAAI,EAAE,CAAC,CAAC,CAAA;AACzE,MAAM,MAAM,UAAU,CAAC,CAAC,GAAG,CAAC,CAAC,UAAU,IAAI,CAAC,CAAC,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AAG,CAAC,CAAC,UAAU,IAAI,CAAC,CAAC,qBAAqB,CACjE,OAAO,IAAI,EACX,CAAC,CACF,CAAA;AAED,eAAO,MAAM,IAAI,+BAA0B,EACzC,OAAO,oBAAgC,EACvC,MAAM,6BAA2B,EACjC,OAAO;;GAA4B,CAAA"}
@@ -13,9 +13,7 @@ const main =
13
13
  lex_schema_1.l.procedure($nsid,
14
14
  /*#__PURE__*/ lex_schema_1.l.params(),
15
15
  /*#__PURE__*/ lex_schema_1.l.payload('*/*'),
16
- /*#__PURE__*/ lex_schema_1.l.jsonPayload({
17
- blob: /*#__PURE__*/ lex_schema_1.l.blob({ allowLegacy: false }),
18
- }));
16
+ /*#__PURE__*/ lex_schema_1.l.jsonPayload({ blob: /*#__PURE__*/ lex_schema_1.l.blob() }));
19
17
  exports.main = main;
20
18
  exports.$lxm = main.nsid, exports.$params = main.parameters, exports.$input = main.input, exports.$output = main.output;
21
19
  //# sourceMappingURL=uploadBlob.defs.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"uploadBlob.defs.js","sourceRoot":"","sources":["../../../../../src/lexicons/com/atproto/repo/uploadBlob.defs.ts"],"names":[],"mappings":";AAAA;;GAEG;;;AAEH,oDAAuC;AAEvC,MAAM,KAAK,GAAG,6BAA6B,CAAA;AAElC,sBAAK;AAEd,gRAAgR;AAChR,MAAM,IAAI;AACR,aAAa;AACb,cAAC,CAAC,SAAS,CACT,KAAK;AACL,aAAa,CAAC,cAAC,CAAC,MAAM,EAAE;AACxB,aAAa,CAAC,cAAC,CAAC,OAAO,CAAC,KAAK,CAAC;AAC9B,aAAa,CAAC,cAAC,CAAC,WAAW,CAAC;IAC1B,IAAI,EAAE,aAAa,CAAC,cAAC,CAAC,IAAI,CAAC,EAAE,WAAW,EAAE,KAAK,EAAE,CAAC;CACnD,CAAC,CACH,CAAA;AACM,oBAAI;AAcA,QAAA,IAAI,GAAiB,IAAI,CAAC,IAAI,EACzC,QAAA,OAAO,GAAiB,IAAI,CAAC,UAAU,EACvC,QAAA,MAAM,GAAiB,IAAI,CAAC,KAAK,EACjC,QAAA,OAAO,GAAiB,IAAI,CAAC,MAAM,CAAA","sourcesContent":["/*\n * THIS FILE WAS GENERATED BY \"@atproto/lex\". DO NOT EDIT.\n */\n\nimport { l } from '@atproto/lex-schema'\n\nconst $nsid = 'com.atproto.repo.uploadBlob'\n\nexport { $nsid }\n\n/** Upload a new blob, to be referenced from a repository record. The blob will be deleted if it is not referenced within a time window (eg, minutes). Blob restrictions (mimetype, size, etc) are enforced when the reference is created. Requires auth, implemented by PDS. */\nconst main =\n /*#__PURE__*/\n l.procedure(\n $nsid,\n /*#__PURE__*/ l.params(),\n /*#__PURE__*/ l.payload('*/*'),\n /*#__PURE__*/ l.jsonPayload({\n blob: /*#__PURE__*/ l.blob({ allowLegacy: false }),\n }),\n )\nexport { main }\n\nexport type $Params = l.InferMethodParams<typeof main>\nexport type $Input<B = l.BinaryData> = l.InferMethodInput<typeof main, B>\nexport type $InputBody<B = l.BinaryData> = l.InferMethodInputBody<\n typeof main,\n B\n>\nexport type $Output<B = l.BinaryData> = l.InferMethodOutput<typeof main, B>\nexport type $OutputBody<B = l.BinaryData> = l.InferMethodOutputBody<\n typeof main,\n B\n>\n\nexport const $lxm = /*#__PURE__*/ main.nsid,\n $params = /*#__PURE__*/ main.parameters,\n $input = /*#__PURE__*/ main.input,\n $output = /*#__PURE__*/ main.output\n"]}
1
+ {"version":3,"file":"uploadBlob.defs.js","sourceRoot":"","sources":["../../../../../src/lexicons/com/atproto/repo/uploadBlob.defs.ts"],"names":[],"mappings":";AAAA;;GAEG;;;AAEH,oDAAuC;AAEvC,MAAM,KAAK,GAAG,6BAA6B,CAAA;AAElC,sBAAK;AAEd,gRAAgR;AAChR,MAAM,IAAI;AACR,aAAa;AACb,cAAC,CAAC,SAAS,CACT,KAAK;AACL,aAAa,CAAC,cAAC,CAAC,MAAM,EAAE;AACxB,aAAa,CAAC,cAAC,CAAC,OAAO,CAAC,KAAK,CAAC;AAC9B,aAAa,CAAC,cAAC,CAAC,WAAW,CAAC,EAAE,IAAI,EAAE,aAAa,CAAC,cAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAC9D,CAAA;AACM,oBAAI;AAcA,QAAA,IAAI,GAAiB,IAAI,CAAC,IAAI,EACzC,QAAA,OAAO,GAAiB,IAAI,CAAC,UAAU,EACvC,QAAA,MAAM,GAAiB,IAAI,CAAC,KAAK,EACjC,QAAA,OAAO,GAAiB,IAAI,CAAC,MAAM,CAAA","sourcesContent":["/*\n * THIS FILE WAS GENERATED BY \"@atproto/lex\". DO NOT EDIT.\n */\n\nimport { l } from '@atproto/lex-schema'\n\nconst $nsid = 'com.atproto.repo.uploadBlob'\n\nexport { $nsid }\n\n/** Upload a new blob, to be referenced from a repository record. The blob will be deleted if it is not referenced within a time window (eg, minutes). Blob restrictions (mimetype, size, etc) are enforced when the reference is created. Requires auth, implemented by PDS. */\nconst main =\n /*#__PURE__*/\n l.procedure(\n $nsid,\n /*#__PURE__*/ l.params(),\n /*#__PURE__*/ l.payload('*/*'),\n /*#__PURE__*/ l.jsonPayload({ blob: /*#__PURE__*/ l.blob() }),\n )\nexport { main }\n\nexport type $Params = l.InferMethodParams<typeof main>\nexport type $Input<B = l.BinaryData> = l.InferMethodInput<typeof main, B>\nexport type $InputBody<B = l.BinaryData> = l.InferMethodInputBody<\n typeof main,\n B\n>\nexport type $Output<B = l.BinaryData> = l.InferMethodOutput<typeof main, B>\nexport type $OutputBody<B = l.BinaryData> = l.InferMethodOutputBody<\n typeof main,\n B\n>\n\nexport const $lxm = /*#__PURE__*/ main.nsid,\n $params = /*#__PURE__*/ main.parameters,\n $input = /*#__PURE__*/ main.input,\n $output = /*#__PURE__*/ main.output\n"]}
@@ -10,7 +10,7 @@ type InferBodyType<TEncoding extends string, TSchema> = TSchema extends Validato
10
10
  *
11
11
  * @typeParam M - The XRPC method type (Procedure or Query)
12
12
  */
13
- export type XrpcResponseBody<M extends Procedure | Query> = M['output'] extends Payload<infer TEncoding, infer TSchema> ? TEncoding extends string ? InferBodyType<TEncoding, TSchema> : undefined : never;
13
+ export type XrpcResponseBody<M extends Procedure | Query> = M['output'] extends Payload<infer TEncoding, infer TSchema> ? TEncoding extends string ? InferBodyType<TEncoding, TSchema> : undefined | LexValue | Uint8Array : never;
14
14
  /**
15
15
  * The full payload type of an XRPC response, including body and encoding.
16
16
  *
@@ -21,7 +21,11 @@ export type XrpcResponseBody<M extends Procedure | Query> = M['output'] extends
21
21
  export type XrpcResponsePayload<M extends Procedure | Query> = M['output'] extends Payload<infer TEncoding, infer TSchema> ? TEncoding extends string ? {
22
22
  encoding: InferEncodingType<TEncoding>;
23
23
  body: InferBodyType<TEncoding, TSchema>;
24
- } : undefined : never;
24
+ } : // If the schema does not specify an output encoding, anything could be
25
+ undefined | {
26
+ body: LexValue | Uint8Array;
27
+ encoding: string;
28
+ } : never;
25
29
  export type XrpcResponseOptions = {
26
30
  /**
27
31
  * Whether to validate the response against the method's output schema.
@@ -95,7 +99,7 @@ export declare class XrpcResponse<M extends Procedure | Query> implements Result
95
99
  * {@link XrpcResponseError.matchesSchemaErrors} to narrow the error type based on
96
100
  * the method's declared error schema. This can be narrowed further as a
97
101
  * {@link XrpcAuthenticationError} if the error is an authentication error.
98
- * @throws {XrpcUpstreamError} when the response is not a valid XRPC
102
+ * @throws {XrpcInvalidResponseError} when the response is not a valid XRPC
99
103
  * response, or if the response does not conform to the method's schema.
100
104
  */
101
105
  static fromFetchResponse<const M extends Procedure | Query>(method: M, response: Response, options?: XrpcResponseOptions): Promise<XrpcResponse<M>>;
@@ -1 +1 @@
1
- {"version":3,"file":"response.d.ts","sourceRoot":"","sources":["../src/response.ts"],"names":[],"mappings":"AACA,OAAO,EACL,yBAAyB,EACzB,WAAW,EACX,QAAQ,EACR,OAAO,EACP,SAAS,EACT,KAAK,EACL,aAAa,EACb,SAAS,EACV,MAAM,qBAAqB,CAAA;AAQ5B,OAAO,EACL,cAAc,EAGf,MAAM,YAAY,CAA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C;IAWvC,QAAQ,CAAC,MAAM,EAAE,CAAC;IAClB,QAAQ,CAAC,MAAM,EAAE,MAAM;IACvB,QAAQ,CAAC,OAAO,EAAE,OAAO;IACzB,QAAQ,CAAC,OAAO,EAAE,mBAAmB,CAAC,CAAC,CAAC;IAZ1C,yCAAyC;IACzC,QAAQ,CAAC,OAAO,EAAG,IAAI,CAAS;IAEhC,uCAAuC;IACvC,IAAI,KAAK,IAAI,IAAI,CAEhB;gBAGU,MAAM,EAAE,CAAC,EACT,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,OAAO,EAChB,OAAO,EAAE,mBAAmB,CAAC,CAAC,CAAC;IAG1C;;;OAGG;IACH,IAAI,QAAQ,YAEX;IAED;;;OAGG;IACH,IAAI,QAAQ,IACuB,yBAAyB,CAAC,CAAC,CAAC,CAC9D;IAED;;;;;;OAMG;IACH,IAAI,IAAI,IACuB,gBAAgB,CAAC,CAAC,CAAC,CACjD;IAED;;;;;;;OAOG;WACU,iBAAiB,CAAC,KAAK,CAAC,CAAC,SAAS,SAAS,GAAG,KAAK,EAC9D,MAAM,EAAE,CAAC,EACT,QAAQ,EAAE,QAAQ,EAClB,OAAO,CAAC,EAAE,mBAAmB,GAC5B,OAAO,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC;CAkG5B"}
package/dist/response.js CHANGED
@@ -57,59 +57,58 @@ class XrpcResponse {
57
57
  * {@link XrpcResponseError.matchesSchemaErrors} to narrow the error type based on
58
58
  * the method's declared error schema. This can be narrowed further as a
59
59
  * {@link XrpcAuthenticationError} if the error is an authentication error.
60
- * @throws {XrpcUpstreamError} when the response is not a valid XRPC
60
+ * @throws {XrpcInvalidResponseError} when the response is not a valid XRPC
61
61
  * response, or if the response does not conform to the method's schema.
62
62
  */
63
63
  static async fromFetchResponse(method, response, options) {
64
64
  // @NOTE The body MUST either be read or canceled to avoid resource leaks.
65
65
  // Since nothing should cause an exception before "readPayload" is
66
66
  // called, we can safely not use a try/finally here.
67
- // @NOTE redirect is set to 'follow', so we shouldn't get 3xx responses here
68
- if (response.status < 200 || response.status >= 300) {
69
- // Always parse json for error responses
67
+ // Always turn 4xx/5xx responses into XrpcResponseError
68
+ if (response.status >= 400) {
70
69
  const payload = await readPayload(method, response, {
71
- parse: { strict: options?.strictResponseProcessing ?? true },
70
+ // Always parse errors in non-strict mode
71
+ parse: { strict: false },
72
72
  });
73
- // Properly formatted XRPC error response ?
74
- if (response.status >= 400 && (0, errors_js_1.isXrpcErrorPayload)(payload)) {
75
- throw response.status === 401
76
- ? new errors_js_1.XrpcAuthenticationError(method, response, payload)
77
- : new errors_js_1.XrpcResponseError(method, response, payload);
73
+ if (response.status === 401) {
74
+ throw new errors_js_1.XrpcAuthenticationError(method, response, payload);
78
75
  }
79
- // Invalid XRPC response (we probably did not hit an XRPC implementation)
80
- throw new errors_js_1.XrpcUpstreamError(method, response, payload, response.status >= 500
81
- ? 'Upstream server encountered an error'
82
- : response.status >= 400
83
- ? 'Invalid response payload'
84
- : 'Invalid response status code');
76
+ throw new errors_js_1.XrpcResponseError(method, response, payload);
77
+ }
78
+ // @NOTE redirect is set to 'follow', so we shouldn't get 3xx responses here
79
+ if (response.status < 200 || response.status >= 300) {
80
+ await response.body?.cancel();
81
+ throw new errors_js_1.XrpcInvalidResponseError(method, response, undefined, `Unexpected status code ${response.status}`);
85
82
  }
86
83
  const payload = await readPayload(method, response, {
87
- // Only parse json if the schema expects it
88
- parse: method.output.encoding === CONTENT_TYPE_JSON && {
89
- strict: options?.strictResponseProcessing ?? true,
90
- },
84
+ // Parse response if there is a schema, or if the encoding is
85
+ // "application/json"
86
+ parse: method.output.schema || method.output.encoding === CONTENT_TYPE_JSON
87
+ ? { strict: options?.strictResponseProcessing ?? true }
88
+ : // If there is no declared output encoding, we'll parse the output (in loose mode)
89
+ method.output.encoding == null
90
+ ? { strict: false }
91
+ : false,
91
92
  });
92
- // Response is successful (2xx). Validate payload (data and encoding) against schema.
93
- if (method.output.encoding == null) {
94
- // Schema expects no payload
95
- if (payload) {
96
- throw new errors_js_1.XrpcUpstreamError(method, response, payload, `Expected response with no body, got ${payload.encoding}`);
97
- }
93
+ if (!method.output.matchesEncoding(payload?.encoding)) {
94
+ throw new errors_js_1.XrpcInvalidResponseError(method, response, payload, `Expected ${stringifyEncoding(method.output.encoding)} response (got ${stringifyEncoding(payload?.encoding)})`);
98
95
  }
99
- else {
100
- // Schema expects a payload
101
- if (!payload || !method.output.matchesEncoding(payload.encoding)) {
102
- throw new errors_js_1.XrpcUpstreamError(method, response, payload, payload
103
- ? `Expected ${method.output.encoding} response, got ${payload.encoding}`
104
- : `Expected non-empty response with content-type ${method.output.encoding}`);
105
- }
96
+ // Response is successful (2xx). Validate payload (data and encoding) against schema.
97
+ if (method.output.encoding != null) {
98
+ // If the schema specifies an output, verify that the response properly
99
+ // matches the expected format (encoding and schema, if present). If no
100
+ // output is specified, any payload could be returned.
101
+ // Needed for type safety. Should never happen since matchesEncoding()
102
+ // should return not succeed if there is a schema encoding but no payload.
103
+ if (!payload)
104
+ throw new Error('Expected payload');
106
105
  // Assert valid response body.
107
106
  if (method.output.schema && options?.validateResponse !== false) {
108
107
  const result = method.output.schema.safeParse(payload.body, {
109
108
  strict: options?.strictResponseProcessing ?? true,
110
109
  });
111
110
  if (!result.success) {
112
- throw new errors_js_1.XrpcInvalidResponseError(method, response, payload, result.reason);
111
+ throw new errors_js_1.XrpcResponseValidationError(method, response, payload, result.reason);
113
112
  }
114
113
  const parsedPayload = {
115
114
  body: result.value,
@@ -150,17 +149,10 @@ async function readPayload(method, response, options) {
150
149
  throw new TypeError(`Invalid content-type "${encoding}" in response`);
151
150
  }
152
151
  if (options?.parse && encoding === CONTENT_TYPE_JSON) {
153
- // @NOTE It might be worth returning the raw bytes here (Uint8Array) and
154
- // perform the lex parsing using cborg/json, allowing to do
155
- // bytes->LexValue in one step instead of bytes->text->JSON->LexValue.
156
- // This would require adding encode/decode utilities to lex-json (similar
157
- // to @ipld/dag-json)
158
- const text = await response.text();
159
- // @NOTE Using `lexParse(text)` (instead of `jsonToLex(json)`) here as
160
- // using a reviver function during JSON.parse should be faster than
161
- // parsing to JSON then converting to Lex (?)
162
- // @TODO verify statement above
163
- return { encoding, body: (0, lex_json_1.lexParse)(text, options.parse) };
152
+ const arrayBuffer = await response.arrayBuffer();
153
+ const bytes = new Uint8Array(arrayBuffer);
154
+ const body = (0, lex_json_1.lexParseJsonBytes)(bytes, options.parse);
155
+ return { encoding, body };
164
156
  }
165
157
  const arrayBuffer = await response.arrayBuffer();
166
158
  return { encoding, body: new Uint8Array(arrayBuffer) };
@@ -168,7 +160,10 @@ async function readPayload(method, response, options) {
168
160
  catch (cause) {
169
161
  const message = 'Unable to parse response payload';
170
162
  const messageDetail = cause instanceof TypeError ? cause.message : undefined;
171
- throw new errors_js_1.XrpcUpstreamError(method, response, null, messageDetail ? `${message}: ${messageDetail}` : message, { cause });
163
+ throw new errors_js_1.XrpcInvalidResponseError(method, response, undefined, messageDetail ? `${message}: ${messageDetail}` : message, { cause });
172
164
  }
173
165
  }
166
+ function stringifyEncoding(encoding) {
167
+ return encoding ? `"${encoding}"` : 'no payload';
168
+ }
174
169
  //# sourceMappingURL=response.js.map