@atproto/xrpc 0.4.3 → 0.6.0-rc.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/types.d.ts CHANGED
@@ -1,17 +1,15 @@
1
1
  import { z } from 'zod';
2
2
  import { ValidationError } from '@atproto/lexicon';
3
3
  export type QueryParams = Record<string, any>;
4
- export type Headers = Record<string, string>;
4
+ export type HeadersMap = Record<string, string>;
5
+ /** @deprecated not to be confused with the WHATWG Headers constructor */
6
+ export type Headers = HeadersMap;
7
+ export type Gettable<T> = T | (() => T);
5
8
  export interface CallOptions {
6
9
  encoding?: string;
7
- headers?: Headers;
10
+ signal?: AbortSignal;
11
+ headers?: HeadersMap;
8
12
  }
9
- export interface FetchHandlerResponse {
10
- status: number;
11
- headers: Headers;
12
- body: ArrayBuffer | undefined;
13
- }
14
- export type FetchHandler = (httpUri: string, httpMethod: string, httpHeaders: Headers, httpReqBody: any) => Promise<FetchHandlerResponse>;
15
13
  export declare const errorResponseBody: z.ZodObject<{
16
14
  error: z.ZodOptional<z.ZodString>;
17
15
  message: z.ZodOptional<z.ZodString>;
@@ -39,7 +37,9 @@ export declare enum ResponseType {
39
37
  NotEnoughResources = 503,
40
38
  UpstreamTimeout = 504
41
39
  }
40
+ export declare function httpResponseCodeToEnum(status: number): ResponseType;
42
41
  export declare const ResponseTypeNames: {
42
+ 1: string;
43
43
  2: string;
44
44
  200: string;
45
45
  400: string;
@@ -54,7 +54,9 @@ export declare const ResponseTypeNames: {
54
54
  503: string;
55
55
  504: string;
56
56
  };
57
+ export declare function httpResponseCodeToName(status: number): string;
57
58
  export declare const ResponseTypeStrings: {
59
+ 1: string;
58
60
  2: string;
59
61
  200: string;
60
62
  400: string;
@@ -69,6 +71,7 @@ export declare const ResponseTypeStrings: {
69
71
  503: string;
70
72
  504: string;
71
73
  };
74
+ export declare function httpResponseCodeToString(status: number): string;
72
75
  export declare class XRPCResponse {
73
76
  data: any;
74
77
  headers: Headers;
@@ -76,11 +79,12 @@ export declare class XRPCResponse {
76
79
  constructor(data: any, headers: Headers);
77
80
  }
78
81
  export declare class XRPCError extends Error {
79
- status: ResponseType;
80
- error?: string | undefined;
82
+ error: string;
83
+ headers?: HeadersMap | undefined;
81
84
  success: boolean;
82
- headers?: Headers;
83
- constructor(status: ResponseType, error?: string | undefined, message?: string, headers?: Headers);
85
+ status: ResponseType;
86
+ constructor(statusCode: number, error?: string, message?: string, headers?: HeadersMap | undefined, options?: ErrorOptions);
87
+ static from(cause: unknown, fallbackStatus?: ResponseType): XRPCError;
84
88
  }
85
89
  export declare class XRPCInvalidResponseError extends XRPCError {
86
90
  lexiconNsid: string;
@@ -88,3 +92,4 @@ export declare class XRPCInvalidResponseError extends XRPCError {
88
92
  responseBody: unknown;
89
93
  constructor(lexiconNsid: string, validationError: ValidationError, responseBody: unknown);
90
94
  }
95
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AACvB,OAAO,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAA;AAElD,MAAM,MAAM,WAAW,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;AAC7C,MAAM,MAAM,UAAU,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;AAE/C,yEAAyE;AACzE,MAAM,MAAM,OAAO,GAAG,UAAU,CAAA;AAEhC,MAAM,MAAM,QAAQ,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,CAAA;AAEvC,MAAM,WAAW,WAAW;IAC1B,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,MAAM,CAAC,EAAE,WAAW,CAAA;IACpB,OAAO,CAAC,EAAE,UAAU,CAAA;CACrB;AAED,eAAO,MAAM,iBAAiB;;;;;;;;;EAG5B,CAAA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IACnB,eAAe,EAAE,eAAe;IAChC,YAAY,EAAE,OAAO;gBAFrB,WAAW,EAAE,MAAM,EACnB,eAAe,EAAE,eAAe,EAChC,YAAY,EAAE,OAAO;CAU/B"}
package/dist/types.js ADDED
@@ -0,0 +1,186 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.XRPCInvalidResponseError = exports.XRPCError = exports.XRPCResponse = exports.httpResponseCodeToString = exports.ResponseTypeStrings = exports.httpResponseCodeToName = exports.ResponseTypeNames = exports.httpResponseCodeToEnum = exports.ResponseType = exports.errorResponseBody = void 0;
4
+ const zod_1 = require("zod");
5
+ exports.errorResponseBody = zod_1.z.object({
6
+ error: zod_1.z.string().optional(),
7
+ message: zod_1.z.string().optional(),
8
+ });
9
+ var ResponseType;
10
+ (function (ResponseType) {
11
+ ResponseType[ResponseType["Unknown"] = 1] = "Unknown";
12
+ ResponseType[ResponseType["InvalidResponse"] = 2] = "InvalidResponse";
13
+ ResponseType[ResponseType["Success"] = 200] = "Success";
14
+ ResponseType[ResponseType["InvalidRequest"] = 400] = "InvalidRequest";
15
+ ResponseType[ResponseType["AuthRequired"] = 401] = "AuthRequired";
16
+ ResponseType[ResponseType["Forbidden"] = 403] = "Forbidden";
17
+ ResponseType[ResponseType["XRPCNotSupported"] = 404] = "XRPCNotSupported";
18
+ ResponseType[ResponseType["PayloadTooLarge"] = 413] = "PayloadTooLarge";
19
+ ResponseType[ResponseType["RateLimitExceeded"] = 429] = "RateLimitExceeded";
20
+ ResponseType[ResponseType["InternalServerError"] = 500] = "InternalServerError";
21
+ ResponseType[ResponseType["MethodNotImplemented"] = 501] = "MethodNotImplemented";
22
+ ResponseType[ResponseType["UpstreamFailure"] = 502] = "UpstreamFailure";
23
+ ResponseType[ResponseType["NotEnoughResources"] = 503] = "NotEnoughResources";
24
+ ResponseType[ResponseType["UpstreamTimeout"] = 504] = "UpstreamTimeout";
25
+ })(ResponseType || (exports.ResponseType = ResponseType = {}));
26
+ function httpResponseCodeToEnum(status) {
27
+ if (status in ResponseType) {
28
+ return status;
29
+ }
30
+ else if (status >= 100 && status < 200) {
31
+ return ResponseType.XRPCNotSupported;
32
+ }
33
+ else if (status >= 200 && status < 300) {
34
+ return ResponseType.Success;
35
+ }
36
+ else if (status >= 300 && status < 400) {
37
+ return ResponseType.XRPCNotSupported;
38
+ }
39
+ else if (status >= 400 && status < 500) {
40
+ return ResponseType.InvalidRequest;
41
+ }
42
+ else {
43
+ return ResponseType.InternalServerError;
44
+ }
45
+ }
46
+ exports.httpResponseCodeToEnum = httpResponseCodeToEnum;
47
+ exports.ResponseTypeNames = {
48
+ [ResponseType.Unknown]: 'Unknown',
49
+ [ResponseType.InvalidResponse]: 'InvalidResponse',
50
+ [ResponseType.Success]: 'Success',
51
+ [ResponseType.InvalidRequest]: 'InvalidRequest',
52
+ [ResponseType.AuthRequired]: 'AuthenticationRequired',
53
+ [ResponseType.Forbidden]: 'Forbidden',
54
+ [ResponseType.XRPCNotSupported]: 'XRPCNotSupported',
55
+ [ResponseType.PayloadTooLarge]: 'PayloadTooLarge',
56
+ [ResponseType.RateLimitExceeded]: 'RateLimitExceeded',
57
+ [ResponseType.InternalServerError]: 'InternalServerError',
58
+ [ResponseType.MethodNotImplemented]: 'MethodNotImplemented',
59
+ [ResponseType.UpstreamFailure]: 'UpstreamFailure',
60
+ [ResponseType.NotEnoughResources]: 'NotEnoughResources',
61
+ [ResponseType.UpstreamTimeout]: 'UpstreamTimeout',
62
+ };
63
+ function httpResponseCodeToName(status) {
64
+ return exports.ResponseTypeNames[httpResponseCodeToEnum(status)];
65
+ }
66
+ exports.httpResponseCodeToName = httpResponseCodeToName;
67
+ exports.ResponseTypeStrings = {
68
+ [ResponseType.Unknown]: 'Unknown',
69
+ [ResponseType.InvalidResponse]: 'Invalid Response',
70
+ [ResponseType.Success]: 'Success',
71
+ [ResponseType.InvalidRequest]: 'Invalid Request',
72
+ [ResponseType.AuthRequired]: 'Authentication Required',
73
+ [ResponseType.Forbidden]: 'Forbidden',
74
+ [ResponseType.XRPCNotSupported]: 'XRPC Not Supported',
75
+ [ResponseType.PayloadTooLarge]: 'Payload Too Large',
76
+ [ResponseType.RateLimitExceeded]: 'Rate Limit Exceeded',
77
+ [ResponseType.InternalServerError]: 'Internal Server Error',
78
+ [ResponseType.MethodNotImplemented]: 'Method Not Implemented',
79
+ [ResponseType.UpstreamFailure]: 'Upstream Failure',
80
+ [ResponseType.NotEnoughResources]: 'Not Enough Resources',
81
+ [ResponseType.UpstreamTimeout]: 'Upstream Timeout',
82
+ };
83
+ function httpResponseCodeToString(status) {
84
+ return exports.ResponseTypeStrings[httpResponseCodeToEnum(status)];
85
+ }
86
+ exports.httpResponseCodeToString = httpResponseCodeToString;
87
+ class XRPCResponse {
88
+ constructor(data, headers) {
89
+ Object.defineProperty(this, "data", {
90
+ enumerable: true,
91
+ configurable: true,
92
+ writable: true,
93
+ value: data
94
+ });
95
+ Object.defineProperty(this, "headers", {
96
+ enumerable: true,
97
+ configurable: true,
98
+ writable: true,
99
+ value: headers
100
+ });
101
+ Object.defineProperty(this, "success", {
102
+ enumerable: true,
103
+ configurable: true,
104
+ writable: true,
105
+ value: true
106
+ });
107
+ }
108
+ }
109
+ exports.XRPCResponse = XRPCResponse;
110
+ class XRPCError extends Error {
111
+ constructor(statusCode, error = httpResponseCodeToName(statusCode), message, headers, options) {
112
+ super(message || error || httpResponseCodeToString(statusCode), options);
113
+ Object.defineProperty(this, "error", {
114
+ enumerable: true,
115
+ configurable: true,
116
+ writable: true,
117
+ value: error
118
+ });
119
+ Object.defineProperty(this, "headers", {
120
+ enumerable: true,
121
+ configurable: true,
122
+ writable: true,
123
+ value: headers
124
+ });
125
+ Object.defineProperty(this, "success", {
126
+ enumerable: true,
127
+ configurable: true,
128
+ writable: true,
129
+ value: false
130
+ });
131
+ Object.defineProperty(this, "status", {
132
+ enumerable: true,
133
+ configurable: true,
134
+ writable: true,
135
+ value: void 0
136
+ });
137
+ this.status = httpResponseCodeToEnum(statusCode);
138
+ // Pre 2022 runtimes won't handle the "options" constructor argument
139
+ const cause = options?.cause;
140
+ if (this.cause === undefined && cause !== undefined) {
141
+ this.cause = cause;
142
+ }
143
+ }
144
+ static from(cause, fallbackStatus) {
145
+ if (cause instanceof XRPCError) {
146
+ return cause;
147
+ }
148
+ // Extract status code from "http-errors" like errors
149
+ const statusCode = cause instanceof Error
150
+ ? ('statusCode' in cause ? cause.statusCode : undefined) ??
151
+ ('status' in cause ? cause.status : undefined)
152
+ : undefined;
153
+ const status = typeof statusCode === 'number'
154
+ ? httpResponseCodeToEnum(statusCode)
155
+ : fallbackStatus ?? ResponseType.Unknown;
156
+ const error = exports.ResponseTypeNames[status];
157
+ const message = cause instanceof Error ? cause.message : String(cause);
158
+ return new XRPCError(status, error, message, undefined, { cause });
159
+ }
160
+ }
161
+ exports.XRPCError = XRPCError;
162
+ class XRPCInvalidResponseError extends XRPCError {
163
+ constructor(lexiconNsid, validationError, responseBody) {
164
+ super(ResponseType.InvalidResponse, exports.ResponseTypeStrings[ResponseType.InvalidResponse], `The server gave an invalid response and may be out of date.`, undefined, { cause: validationError });
165
+ Object.defineProperty(this, "lexiconNsid", {
166
+ enumerable: true,
167
+ configurable: true,
168
+ writable: true,
169
+ value: lexiconNsid
170
+ });
171
+ Object.defineProperty(this, "validationError", {
172
+ enumerable: true,
173
+ configurable: true,
174
+ writable: true,
175
+ value: validationError
176
+ });
177
+ Object.defineProperty(this, "responseBody", {
178
+ enumerable: true,
179
+ configurable: true,
180
+ writable: true,
181
+ value: responseBody
182
+ });
183
+ }
184
+ }
185
+ exports.XRPCInvalidResponseError = XRPCInvalidResponseError;
186
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":";;;AAAA,6BAAuB;AAiBV,QAAA,iBAAiB,GAAG,OAAC,CAAC,MAAM,CAAC;IACxC,KAAK,EAAE,OAAC,CAAC,MAAM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+1,14 @@
1
1
  import { LexXrpcProcedure, LexXrpcQuery } from '@atproto/lexicon';
2
- import { CallOptions, Headers, QueryParams, ResponseType } from './types';
2
+ import { CallOptions, ErrorResponseBody, Gettable, QueryParams } from './types';
3
+ export declare function isErrorResponseBody(v: unknown): v is ErrorResponseBody;
3
4
  export declare function getMethodSchemaHTTPMethod(schema: LexXrpcProcedure | LexXrpcQuery): "post" | "get";
4
5
  export declare function constructMethodCallUri(nsid: string, schema: LexXrpcProcedure | LexXrpcQuery, serviceUri: URL, params?: QueryParams): string;
6
+ export declare function constructMethodCallUrl(nsid: string, schema: LexXrpcProcedure | LexXrpcQuery, params?: QueryParams): string;
5
7
  export declare function encodeQueryParam(type: 'string' | 'float' | 'integer' | 'boolean' | 'datetime' | 'array' | 'unknown', value: any): string;
6
- export declare function normalizeHeaders(headers: Headers): Headers;
7
- export declare function constructMethodCallHeaders(schema: LexXrpcProcedure | LexXrpcQuery, data?: any, opts?: CallOptions): Headers;
8
- export declare function encodeMethodCallBody(headers: Headers, data?: any): ArrayBuffer | undefined;
9
- export declare function httpResponseCodeToEnum(status: number): ResponseType;
8
+ export declare function constructMethodCallHeaders(schema: LexXrpcProcedure | LexXrpcQuery, data?: unknown, opts?: CallOptions): Headers;
9
+ export declare function combineHeaders(headersInit: undefined | HeadersInit, defaultHeaders?: Iterable<[string, undefined | Gettable<null | string>]>): undefined | HeadersInit;
10
+ export declare function isBodyInit(value: unknown): value is BodyInit;
11
+ export declare function isIterable(value: unknown): value is Iterable<unknown> | AsyncIterable<unknown>;
12
+ export declare function encodeMethodCallBody(headers: Headers, data?: unknown): BodyInit | undefined;
10
13
  export declare function httpResponseBodyParse(mimeType: string | null, data: ArrayBuffer | undefined): any;
14
+ //# sourceMappingURL=util.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../src/util.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,gBAAgB,EAChB,YAAY,EAEb,MAAM,kBAA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;AAED,wBAAgB,oBAAoB,CAClC,OAAO,EAAE,OAAO,EAChB,IAAI,CAAC,EAAE,OAAO,GACb,QAAQ,GAAG,SAAS,CAqEtB;AA6FD,wBAAgB,qBAAqB,CACnC,QAAQ,EAAE,MAAM,GAAG,IAAI,EACvB,IAAI,EAAE,WAAW,GAAG,SAAS,GAC5B,GAAG,CAwBL"}
package/dist/util.js ADDED
@@ -0,0 +1,362 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.httpResponseBodyParse = exports.encodeMethodCallBody = exports.isIterable = exports.isBodyInit = exports.combineHeaders = exports.constructMethodCallHeaders = exports.encodeQueryParam = exports.constructMethodCallUrl = exports.constructMethodCallUri = exports.getMethodSchemaHTTPMethod = exports.isErrorResponseBody = void 0;
4
+ const lexicon_1 = require("@atproto/lexicon");
5
+ const types_1 = require("./types");
6
+ const ReadableStream = globalThis.ReadableStream ||
7
+ class {
8
+ constructor() {
9
+ // This anonymous class will never pass any "instanceof" check and cannot
10
+ // be instantiated.
11
+ throw new Error('ReadableStream is not supported in this environment');
12
+ }
13
+ };
14
+ function isErrorResponseBody(v) {
15
+ return types_1.errorResponseBody.safeParse(v).success;
16
+ }
17
+ exports.isErrorResponseBody = isErrorResponseBody;
18
+ function getMethodSchemaHTTPMethod(schema) {
19
+ if (schema.type === 'procedure') {
20
+ return 'post';
21
+ }
22
+ return 'get';
23
+ }
24
+ exports.getMethodSchemaHTTPMethod = getMethodSchemaHTTPMethod;
25
+ function constructMethodCallUri(nsid, schema, serviceUri, params) {
26
+ const uri = new URL(constructMethodCallUrl(nsid, schema, params), serviceUri);
27
+ return uri.toString();
28
+ }
29
+ exports.constructMethodCallUri = constructMethodCallUri;
30
+ function constructMethodCallUrl(nsid, schema, params) {
31
+ const pathname = `/xrpc/${encodeURIComponent(nsid)}`;
32
+ if (!params)
33
+ return pathname;
34
+ const searchParams = [];
35
+ for (const [key, value] of Object.entries(params)) {
36
+ const paramSchema = schema.parameters?.properties?.[key];
37
+ if (!paramSchema) {
38
+ throw new Error(`Invalid query parameter: ${key}`);
39
+ }
40
+ if (value !== undefined) {
41
+ if (paramSchema.type === 'array') {
42
+ const values = Array.isArray(value) ? value : [value];
43
+ for (const val of values) {
44
+ searchParams.push([
45
+ key,
46
+ encodeQueryParam(paramSchema.items.type, val),
47
+ ]);
48
+ }
49
+ }
50
+ else {
51
+ searchParams.push([key, encodeQueryParam(paramSchema.type, value)]);
52
+ }
53
+ }
54
+ }
55
+ if (!searchParams.length)
56
+ return pathname;
57
+ return `${pathname}?${new URLSearchParams(searchParams).toString()}`;
58
+ }
59
+ exports.constructMethodCallUrl = constructMethodCallUrl;
60
+ function encodeQueryParam(type, value) {
61
+ if (type === 'string' || type === 'unknown') {
62
+ return String(value);
63
+ }
64
+ if (type === 'float') {
65
+ return String(Number(value));
66
+ }
67
+ else if (type === 'integer') {
68
+ return String(Number(value) | 0);
69
+ }
70
+ else if (type === 'boolean') {
71
+ return value ? 'true' : 'false';
72
+ }
73
+ else if (type === 'datetime') {
74
+ if (value instanceof Date) {
75
+ return value.toISOString();
76
+ }
77
+ return String(value);
78
+ }
79
+ throw new Error(`Unsupported query param type: ${type}`);
80
+ }
81
+ exports.encodeQueryParam = encodeQueryParam;
82
+ function constructMethodCallHeaders(schema, data, opts) {
83
+ // Not using `new Headers(opts?.headers)` to avoid duplicating headers values
84
+ // due to inconsistent casing in headers name. In case of multiple headers
85
+ // with the same name (but using a different case), the last one will be used.
86
+ // new Headers({ 'content-type': 'foo', 'Content-Type': 'bar' }).get('content-type')
87
+ // => 'foo, bar'
88
+ const headers = new Headers();
89
+ if (opts?.headers) {
90
+ for (const name in opts.headers) {
91
+ if (headers.has(name)) {
92
+ throw new TypeError(`Duplicate header: ${name}`);
93
+ }
94
+ const value = opts.headers[name];
95
+ if (value != null) {
96
+ headers.set(name, value);
97
+ }
98
+ }
99
+ }
100
+ if (schema.type === 'procedure') {
101
+ if (opts?.encoding) {
102
+ headers.set('content-type', opts.encoding);
103
+ }
104
+ else if (!headers.has('content-type') && typeof data !== 'undefined') {
105
+ // Special handling of BodyInit types before falling back to JSON encoding
106
+ if (data instanceof ArrayBuffer ||
107
+ data instanceof ReadableStream ||
108
+ ArrayBuffer.isView(data)) {
109
+ headers.set('content-type', 'application/octet-stream');
110
+ }
111
+ else if (data instanceof FormData) {
112
+ // Note: The multipart form data boundary is missing from the header
113
+ // we set here, making that header invalid. This special case will be
114
+ // handled in encodeMethodCallBody()
115
+ headers.set('content-type', 'multipart/form-data');
116
+ }
117
+ else if (data instanceof URLSearchParams) {
118
+ headers.set('content-type', 'application/x-www-form-urlencoded;charset=UTF-8');
119
+ }
120
+ else if (isBlobLike(data)) {
121
+ headers.set('content-type', data.type || 'application/octet-stream');
122
+ }
123
+ else if (typeof data === 'string') {
124
+ headers.set('content-type', 'text/plain;charset=UTF-8');
125
+ }
126
+ // At this point, data is not a valid BodyInit type.
127
+ else if (isIterable(data)) {
128
+ headers.set('content-type', 'application/octet-stream');
129
+ }
130
+ else if (typeof data === 'boolean' ||
131
+ typeof data === 'number' ||
132
+ typeof data === 'string' ||
133
+ typeof data === 'object' // covers "null"
134
+ ) {
135
+ headers.set('content-type', 'application/json');
136
+ }
137
+ else {
138
+ // symbol, function, bigint
139
+ throw new types_1.XRPCError(types_1.ResponseType.InvalidRequest, `Unsupported data type: ${typeof data}`);
140
+ }
141
+ }
142
+ }
143
+ return headers;
144
+ }
145
+ exports.constructMethodCallHeaders = constructMethodCallHeaders;
146
+ function combineHeaders(headersInit, defaultHeaders) {
147
+ if (!defaultHeaders)
148
+ return headersInit;
149
+ let headers = undefined;
150
+ for (const [key, getter] of defaultHeaders) {
151
+ // Ignore undefined values (allowed for convenience when using
152
+ // Object.entries).
153
+ if (getter === undefined)
154
+ continue;
155
+ // Lazy initialization of the headers object
156
+ headers ?? (headers = new Headers(headersInit));
157
+ if (headers.has(key))
158
+ continue;
159
+ const value = typeof getter === 'function' ? getter() : getter;
160
+ if (typeof value === 'string')
161
+ headers.set(key, value);
162
+ else if (value === null)
163
+ headers.delete(key);
164
+ else
165
+ throw new TypeError(`Invalid "${key}" header value: ${typeof value}`);
166
+ }
167
+ return headers ?? headersInit;
168
+ }
169
+ exports.combineHeaders = combineHeaders;
170
+ function isBlobLike(value) {
171
+ if (value == null)
172
+ return false;
173
+ if (typeof value !== 'object')
174
+ return false;
175
+ if (typeof Blob === 'function' && value instanceof Blob)
176
+ return true;
177
+ // Support for Blobs provided by libraries that don't use the native Blob
178
+ // (e.g. fetch-blob from node-fetch).
179
+ // https://github.com/node-fetch/fetch-blob/blob/a1a182e5978811407bef4ea1632b517567dda01f/index.js#L233-L244
180
+ const tag = value[Symbol.toStringTag];
181
+ if (tag === 'Blob' || tag === 'File') {
182
+ return 'stream' in value && typeof value.stream === 'function';
183
+ }
184
+ return false;
185
+ }
186
+ function isBodyInit(value) {
187
+ switch (typeof value) {
188
+ case 'string':
189
+ return true;
190
+ case 'object':
191
+ return (value instanceof ArrayBuffer ||
192
+ value instanceof FormData ||
193
+ value instanceof URLSearchParams ||
194
+ value instanceof ReadableStream ||
195
+ ArrayBuffer.isView(value) ||
196
+ isBlobLike(value));
197
+ default:
198
+ return false;
199
+ }
200
+ }
201
+ exports.isBodyInit = isBodyInit;
202
+ function isIterable(value) {
203
+ return (value != null &&
204
+ typeof value === 'object' &&
205
+ (Symbol.iterator in value || Symbol.asyncIterator in value));
206
+ }
207
+ exports.isIterable = isIterable;
208
+ function encodeMethodCallBody(headers, data) {
209
+ // Silently ignore the body if there is no content-type header.
210
+ const contentType = headers.get('content-type');
211
+ if (!contentType) {
212
+ return undefined;
213
+ }
214
+ if (typeof data === 'undefined') {
215
+ // This error would be returned by the server, but we can catch it earlier
216
+ // to avoid un-necessary requests. Note that a content-length of 0 does not
217
+ // necessary mean that the body is "empty" (e.g. an empty txt file).
218
+ throw new types_1.XRPCError(types_1.ResponseType.InvalidRequest, `A request body is expected but none was provided`);
219
+ }
220
+ if (isBodyInit(data)) {
221
+ if (data instanceof FormData && contentType === 'multipart/form-data') {
222
+ // fetch() will encode FormData payload itself, but it won't override the
223
+ // content-type header if already present. This would cause the boundary
224
+ // to be missing from the content-type header, resulting in a 400 error.
225
+ // Deleting the content-type header here to let fetch() re-create it.
226
+ headers.delete('content-type');
227
+ }
228
+ // Will be encoded by the fetch API.
229
+ return data;
230
+ }
231
+ if (isIterable(data)) {
232
+ // Note that some environments support using Iterable & AsyncIterable as the
233
+ // body (e.g. Node's fetch), but not all of them do (browsers).
234
+ return iterableToReadableStream(data);
235
+ }
236
+ if (contentType.startsWith('text/')) {
237
+ return new TextEncoder().encode(String(data));
238
+ }
239
+ if (contentType.startsWith('application/json')) {
240
+ const json = (0, lexicon_1.stringifyLex)(data);
241
+ // Server would return a 400 error if the JSON is invalid (e.g. trying to
242
+ // JSONify a function, or an object that implements toJSON() poorly).
243
+ if (json === undefined) {
244
+ throw new types_1.XRPCError(types_1.ResponseType.InvalidRequest, `Failed to encode request body as JSON`);
245
+ }
246
+ return new TextEncoder().encode(json);
247
+ }
248
+ // At this point, "data" is not a valid BodyInit value, and we don't know how
249
+ // to encode it into one. Passing it to fetch would result in an error. Let's
250
+ // throw our own error instead.
251
+ const type = !data || typeof data !== 'object'
252
+ ? typeof data
253
+ : data.constructor !== Object &&
254
+ typeof data.constructor === 'function' &&
255
+ typeof data.constructor?.name === 'string'
256
+ ? data.constructor.name
257
+ : 'object';
258
+ throw new types_1.XRPCError(types_1.ResponseType.InvalidRequest, `Unable to encode ${type} as ${contentType} data`);
259
+ }
260
+ exports.encodeMethodCallBody = encodeMethodCallBody;
261
+ /**
262
+ * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream/from_static}
263
+ */
264
+ function iterableToReadableStream(iterable) {
265
+ // Use the native ReadableStream.from() if available.
266
+ if ('from' in ReadableStream && typeof ReadableStream.from === 'function') {
267
+ return ReadableStream.from(iterable);
268
+ }
269
+ // Note, in environments where ReadableStream is not available either, we
270
+ // *could* load the iterable into memory and create an Arraybuffer from it.
271
+ // However, this would be a bad idea for large iterables. In order to keep
272
+ // things simple, we'll just allow the anonymous ReadableStream constructor
273
+ // to throw an error in those environments, hinting the user of the lib to find
274
+ // an alternate solution in that case (e.g. use a Blob if available).
275
+ let generator;
276
+ return new ReadableStream({
277
+ type: 'bytes',
278
+ start() {
279
+ // Wrap the iterable in an async generator to handle both sync and async
280
+ // iterables, and make sure that the return() method exists.
281
+ generator = (async function* () {
282
+ yield* iterable;
283
+ })();
284
+ },
285
+ async pull(controller) {
286
+ const { done, value } = await generator.next();
287
+ if (done) {
288
+ controller.close();
289
+ }
290
+ else {
291
+ try {
292
+ const buf = toUint8Array(value);
293
+ if (buf)
294
+ controller.enqueue(buf);
295
+ }
296
+ catch (cause) {
297
+ // ReadableStream won't call cancel() if the stream is errored.
298
+ await generator.return();
299
+ controller.error(new TypeError('Converting iterable body to ReadableStream requires Buffer, ArrayBuffer or string values', { cause }));
300
+ }
301
+ }
302
+ },
303
+ async cancel() {
304
+ await generator.return();
305
+ },
306
+ });
307
+ }
308
+ // Browsers don't have Buffer. This syntax is to avoid bundlers from including
309
+ // a Buffer polyfill in the bundle if it's not used elsewhere.
310
+ const globalName = `${{ toString: () => 'Buf' }}fer`;
311
+ const Buffer = typeof globalThis[globalName] === 'function'
312
+ ? globalThis[globalName]
313
+ : undefined;
314
+ const toUint8Array = Buffer
315
+ ? (value) => {
316
+ // @ts-expect-error Buffer.from will throw if value is not a valid input
317
+ const buf = Buffer.isBuffer(value) ? value : Buffer.from(value);
318
+ return buf.byteLength ? new Uint8Array(buf) : undefined;
319
+ }
320
+ : (value) => {
321
+ if (value instanceof ArrayBuffer) {
322
+ const buf = new Uint8Array(value);
323
+ return buf.byteLength ? buf : undefined;
324
+ }
325
+ // Simulate Buffer.from() behavior for strings and and coercion
326
+ if (typeof value === 'string') {
327
+ return value.length ? new TextEncoder().encode(value) : undefined;
328
+ }
329
+ else if (typeof value?.valueOf === 'function') {
330
+ const coerced = value.valueOf();
331
+ if (coerced instanceof ArrayBuffer) {
332
+ const buf = new Uint8Array(coerced);
333
+ return buf.byteLength ? buf : undefined;
334
+ }
335
+ else if (typeof coerced === 'string') {
336
+ return coerced.length ? new TextEncoder().encode(coerced) : undefined;
337
+ }
338
+ }
339
+ throw new TypeError(`Unable to convert "${typeof value}" to Uint8Array`);
340
+ };
341
+ function httpResponseBodyParse(mimeType, data) {
342
+ try {
343
+ if (mimeType) {
344
+ if (mimeType.includes('application/json')) {
345
+ const str = new TextDecoder().decode(data);
346
+ return (0, lexicon_1.jsonStringToLex)(str);
347
+ }
348
+ if (mimeType.startsWith('text/')) {
349
+ return new TextDecoder().decode(data);
350
+ }
351
+ }
352
+ if (data instanceof ArrayBuffer) {
353
+ return new Uint8Array(data);
354
+ }
355
+ return data;
356
+ }
357
+ catch (cause) {
358
+ throw new types_1.XRPCError(types_1.ResponseType.InvalidResponse, undefined, `Failed to parse response body: ${String(cause)}`, undefined, { cause });
359
+ }
360
+ }
361
+ exports.httpResponseBodyParse = httpResponseBodyParse;
362
+ //# sourceMappingURL=util.js.map