@atcute/xrpc-server 0.1.4 → 0.1.6

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.
@@ -2,6 +2,7 @@ export interface XRPCErrorOptions {
2
2
  status: number;
3
3
  error: string;
4
4
  description?: string;
5
+ headers?: HeadersInit;
5
6
  }
6
7
  export declare class XRPCError extends Error {
7
8
  /** response status */
@@ -10,32 +11,34 @@ export declare class XRPCError extends Error {
10
11
  readonly error: string;
11
12
  /** error message */
12
13
  readonly description?: string;
13
- constructor({ status, error, description }: XRPCErrorOptions);
14
+ /** response headers */
15
+ readonly headers?: HeadersInit;
16
+ constructor({ status, error, description, headers }: XRPCErrorOptions);
14
17
  toResponse(): Response;
15
18
  }
16
19
  export declare class InvalidRequestError extends XRPCError {
17
- constructor({ status, error, description }?: Partial<XRPCErrorOptions>);
20
+ constructor({ status, error, description, headers }?: Partial<XRPCErrorOptions>);
18
21
  }
19
22
  export declare class AuthRequiredError extends XRPCError {
20
- constructor({ status, error, description }?: Partial<XRPCErrorOptions>);
23
+ constructor({ status, error, description, headers }?: Partial<XRPCErrorOptions>);
21
24
  }
22
25
  export declare class ForbiddenError extends XRPCError {
23
- constructor({ status, error, description }?: Partial<XRPCErrorOptions>);
26
+ constructor({ status, error, description, headers }?: Partial<XRPCErrorOptions>);
24
27
  }
25
28
  export declare class RateLimitExceededError extends XRPCError {
26
- constructor({ status, error, description }?: Partial<XRPCErrorOptions>);
29
+ constructor({ status, error, description, headers }?: Partial<XRPCErrorOptions>);
27
30
  }
28
31
  export declare class InternalServerError extends XRPCError {
29
- constructor({ status, error, description }?: Partial<XRPCErrorOptions>);
32
+ constructor({ status, error, description, headers }?: Partial<XRPCErrorOptions>);
30
33
  }
31
34
  export declare class UpstreamFailureError extends XRPCError {
32
- constructor({ status, error, description }?: Partial<XRPCErrorOptions>);
35
+ constructor({ status, error, description, headers }?: Partial<XRPCErrorOptions>);
33
36
  }
34
37
  export declare class NotEnoughResourcesError extends XRPCError {
35
- constructor({ status, error, description }?: Partial<XRPCErrorOptions>);
38
+ constructor({ status, error, description, headers }?: Partial<XRPCErrorOptions>);
36
39
  }
37
40
  export declare class UpstreamTimeoutError extends XRPCError {
38
- constructor({ status, error, description }?: Partial<XRPCErrorOptions>);
41
+ constructor({ status, error, description, headers }?: Partial<XRPCErrorOptions>);
39
42
  }
40
43
  export interface XRPCSubscriptionErrorOptions {
41
44
  closeCode?: number;
@@ -1 +1 @@
1
- {"version":3,"file":"xrpc-error.d.ts","sourceRoot":"","sources":["../../lib/main/xrpc-error.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,gBAAgB;IAChC,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,MAAM,CAAC;IACd,WAAW,CAAC,EAAE,MAAM,CAAC;CACrB;AAED,qBAAa,SAAU,SAAQ,KAAK;IACnC,sBAAsB;IACtB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IAExB,iBAAiB;IACjB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,oBAAoB;IACpB,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM,CAAC;IAE9B,YAAY,EAAE,MAAM,EAAE,KAAK,EAAE,WAAW,EAAE,EAAE,gBAAgB,EAO3D;IAED,UAAU,IAAI,QAAQ,CAErB;CACD;AAED,qBAAa,mBAAoB,SAAQ,SAAS;IACjD,YAAY,EAAE,MAAY,EAAE,KAAwB,EAAE,WAAW,EAAE,GAAE,OAAO,CAAC,gBAAgB,CAAM,EAElG;CACD;AAED,qBAAa,iBAAkB,SAAQ,SAAS;IAC/C,YAAY,EACX,MAAY,EACZ,KAAgC,EAChC,WAAW,EACX,GAAE,OAAO,CAAC,gBAAgB,CAAM,EAEhC;CACD;AAED,qBAAa,cAAe,SAAQ,SAAS;IAC5C,YAAY,EAAE,MAAY,EAAE,KAAmB,EAAE,WAAW,EAAE,GAAE,OAAO,CAAC,gBAAgB,CAAM,EAE7F;CACD;AAED,qBA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Y,EACZ,KAAyB,EACzB,WAAW,EACX,OAAO,EACP,GAAE,OAAO,CAAC,gBAAgB,CAAM,EAEhC;CACD;AAED,qBAAa,uBAAwB,SAAQ,SAAS;IACrD,YAAY,EACX,MAAY,EACZ,KAA4B,EAC5B,WAAW,EACX,OAAO,EACP,GAAE,OAAO,CAAC,gBAAgB,CAAM,EAEhC;CACD;AAED,qBAAa,oBAAqB,SAAQ,SAAS;IAClD,YAAY,EACX,MAAY,EACZ,KAAyB,EACzB,WAAW,EACX,OAAO,EACP,GAAE,OAAO,CAAC,gBAAgB,CAAM,EAEhC;CACD;AAED,MAAM,WAAW,4BAA4B;IAC5C,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,KAAK,EAAE,MAAM,CAAC;IACd,WAAW,CAAC,EAAE,MAAM,CAAC;CACrB;AAED,qBAAa,qBAAsB,SAAQ,KAAK;IAC/C,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM,CAAC;IAE9B,YAAY,EAAE,SAAgB,EAAE,KAAK,EAAE,WAAW,EAAE,EAAE,4BAA4B,EAMjF;CACD"}
@@ -5,54 +5,57 @@ export class XRPCError extends Error {
5
5
  error;
6
6
  /** error message */
7
7
  description;
8
- constructor({ status, error, description }) {
8
+ /** response headers */
9
+ headers;
10
+ constructor({ status, error, description, headers }) {
9
11
  super(`${error} > ${description ?? '(unspecified description)'}`);
10
12
  this.status = status;
11
13
  this.error = error;
12
14
  this.description = description;
15
+ this.headers = headers;
13
16
  }
14
17
  toResponse() {
15
- return Response.json({ error: this.error, message: this.description }, { status: this.status });
18
+ return Response.json({ error: this.error, message: this.description }, { status: this.status, headers: this.headers });
16
19
  }
17
20
  }
18
21
  export class InvalidRequestError extends XRPCError {
19
- constructor({ status = 400, error = 'InvalidRequest', description } = {}) {
20
- super({ status, error, description });
22
+ constructor({ status = 400, error = 'InvalidRequest', description, headers, } = {}) {
23
+ super({ status, error, description, headers });
21
24
  }
22
25
  }
23
26
  export class AuthRequiredError extends XRPCError {
24
- constructor({ status = 401, error = 'AuthenticationRequired', description, } = {}) {
25
- super({ status, error, description });
27
+ constructor({ status = 401, error = 'AuthenticationRequired', description, headers, } = {}) {
28
+ super({ status, error, description, headers });
26
29
  }
27
30
  }
28
31
  export class ForbiddenError extends XRPCError {
29
- constructor({ status = 403, error = 'Forbidden', description } = {}) {
30
- super({ status, error, description });
32
+ constructor({ status = 403, error = 'Forbidden', description, headers } = {}) {
33
+ super({ status, error, description, headers });
31
34
  }
32
35
  }
33
36
  export class RateLimitExceededError extends XRPCError {
34
- constructor({ status = 429, error = 'RateLimitExceeded', description } = {}) {
35
- super({ status, error, description });
37
+ constructor({ status = 429, error = 'RateLimitExceeded', description, headers, } = {}) {
38
+ super({ status, error, description, headers });
36
39
  }
37
40
  }
38
41
  export class InternalServerError extends XRPCError {
39
- constructor({ status = 500, error = 'InternalServerError', description } = {}) {
40
- super({ status, error, description });
42
+ constructor({ status = 500, error = 'InternalServerError', description, headers, } = {}) {
43
+ super({ status, error, description, headers });
41
44
  }
42
45
  }
43
46
  export class UpstreamFailureError extends XRPCError {
44
- constructor({ status = 502, error = 'UpstreamFailure', description } = {}) {
45
- super({ status, error, description });
47
+ constructor({ status = 502, error = 'UpstreamFailure', description, headers, } = {}) {
48
+ super({ status, error, description, headers });
46
49
  }
47
50
  }
48
51
  export class NotEnoughResourcesError extends XRPCError {
49
- constructor({ status = 503, error = 'NotEnoughResources', description } = {}) {
50
- super({ status, error, description });
52
+ constructor({ status = 503, error = 'NotEnoughResources', description, headers, } = {}) {
53
+ super({ status, error, description, headers });
51
54
  }
52
55
  }
53
56
  export class UpstreamTimeoutError extends XRPCError {
54
- constructor({ status = 504, error = 'UpstreamTimeout', description } = {}) {
55
- super({ status, error, description });
57
+ constructor({ status = 504, error = 'UpstreamTimeout', description, headers, } = {}) {
58
+ super({ status, error, description, headers });
56
59
  }
57
60
  }
58
61
  export class XRPCSubscriptionError extends Error {
@@ -1 +1 @@
1
- {"version":3,"file":"xrpc-error.js","sourceRoot":"","sources":["../../lib/main/xrpc-error.ts"],"names":[],"mappings":"AAMA,MAAM,OAAO,SAAU,SAAQ,KAAK;IACnC,sBAAsB;IACb,MAAM,CAAS;IAExB,iBAAiB;IACR,KAAK,CAAS;IACvB,oBAAoB;IACX,WAAW,CAAU;IAE9B,YAAY,EAAE,MAAM,EAAE,KAAK,EAAE,WAAW,EAAoB,EAAE;QAC7D,KAAK,CAAC,GAAG,KAAK,MAAM,WAAW,IAAI,2BAA2B,EAAE,CAAC,CAAC;QAElE,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QAErB,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,IAAI,CAAC,WAAW,GAAG,WAAW,CAAC;IAAA,CAC/B;IAED,UAAU,GAAa;QACtB,OAAO,QAAQ,CAAC,IAAI,CAAC,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,EAAE,OAAO,EAAE,IAAI,CAAC,WAAW,EAAE,EAAE,EAAE,MAAM,EAAE,IAAI,CAAC,MAAM,EAAE,CAAC,CAAC;IAAA,CAChG;CACD;AAED,MAAM,OAAO,mBAAoB,SAAQ,SAAS;IACjD,YAAY,EAAE,MAAM,GAAG,GAAG,EAAE,KAAK,GAAG,gBAAgB,EAAE,WAAW,EAAE,GAA8B,EAAE,EAAE;QACpG,KAAK,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,WAAW,EAAE,CAAC,CAAC;IAAA,CACtC;CACD;AAED,MAAM,OAAO,iBAAkB,SAAQ,SAAS;IAC/C,YAAY,EACX,MAAM,GAAG,GAAG,EACZ,KAAK,GAAG,wBAAwB,EAChC,WAAW,GACX,GAA8B,EAAE,EAAE;QAClC,KAAK,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,WAAW,EAAE,CAAC,CAAC;IAAA,CACtC;CACD;AAED,MAAM,OAAO,cAAe,SAA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B,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,IAAI,CAAC,WAAW,GAAG,WAAW,CAAC;IAAA,CAC/B;CACD"}
@@ -4,6 +4,8 @@ export interface CORSOptions {
4
4
  exposedHeaders?: string[];
5
5
  /** Additional headers to allow */
6
6
  allowedHeaders?: string[];
7
+ /** NSID prefixes to exclude from CORS handling */
8
+ exclude?: string[];
7
9
  }
8
10
  export declare const cors: (options?: CORSOptions) => FetchMiddleware;
9
11
  //# sourceMappingURL=cors.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"cors.d.ts","sourceRoot":"","sources":["../../lib/middlewares/cors.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAEzD,MAAM,WAAW,WAAW;IAC3B,iDAAiD;IACjD,cAAc,CAAC,EAAE,MAAM,EAAE,CAAC;IAC1B,kCAAkC;IAClC,cAAc,CAAC,EAAE,MAAM,EAAE,CAAC;CAC1B;AAsBD,eAAO,MAAM,IAAI,4CAsChB,CAAC"}
1
+ {"version":3,"file":"cors.d.ts","sourceRoot":"","sources":["../../lib/middlewares/cors.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAEzD,MAAM,WAAW,WAAW;IAC3B,iDAAiD;IACjD,cAAc,CAAC,EAAE,MAAM,EAAE,CAAC;IAC1B,kCAAkC;IAClC,cAAc,CAAC,EAAE,MAAM,EAAE,CAAC;IAC1B,kDAAkD;IAClD,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;CACnB;AAwBD,eAAO,MAAM,IAAI,4CA4DhB,CAAC"}
@@ -13,12 +13,31 @@ const DEFAULT_ALLOWED_HEADERS = [
13
13
  'atproto-accept-labelers',
14
14
  'atproto-proxy',
15
15
  ];
16
+ const RE_XRPC_NSID = /^\/xrpc\/([^?]*)/;
16
17
  export const cors = (options = {}) => {
17
18
  const exposedHeaders = Array.from(new Set([...DEFAULT_EXPOSED_HEADERS, ...(options.exposedHeaders?.map((h) => h.toLowerCase()) || [])])).sort();
18
19
  const allowedHeaders = Array.from(new Set([...DEFAULT_ALLOWED_HEADERS, ...(options.allowedHeaders?.map((h) => h.toLowerCase()) || [])]))
19
20
  .sort()
20
21
  .join(',');
22
+ const exclude = options.exclude;
21
23
  return async (request, next) => {
24
+ // check if this NSID should be excluded from CORS handling
25
+ if (exclude) {
26
+ const url = new URL(request.url);
27
+ const match = RE_XRPC_NSID.exec(url.pathname);
28
+ if (match) {
29
+ const nsid = match[1];
30
+ const excluded = exclude.some((pattern) => {
31
+ if (pattern.endsWith('.*')) {
32
+ return nsid.startsWith(pattern.slice(0, -1));
33
+ }
34
+ return nsid === pattern;
35
+ });
36
+ if (excluded) {
37
+ return next(request);
38
+ }
39
+ }
40
+ }
22
41
  const origin = request.headers.get('origin') || '*';
23
42
  // Handle preflight requests
24
43
  if (request.method === 'OPTIONS') {
@@ -1 +1 @@
1
- {"version":3,"file":"cors.js","sourceRoot":"","sources":["../../lib/middlewares/cors.ts"],"names":[],"mappings":"AASA,MAAM,uBAAuB,GAAG;IAC/B,YAAY;IACZ,kBAAkB;IAElB,iBAAiB;IACjB,kBAAkB;IAClB,qBAAqB;IACrB,iBAAiB;CACjB,CAAC;AAEF,MAAM,uBAAuB,GAAG;IAC/B,cAAc;IAEd,eAAe;IACf,MAAM;IAEN,yBAAyB;IACzB,eAAe;CACf,CAAC;AAEF,MAAM,CAAC,MAAM,IAAI,GAAG,CAAC,OAAO,GAAgB,EAAE,EAAmB,EAAE,CAAC;IACnE,MAAM,cAAc,GAAG,KAAK,CAAC,IAAI,CAChC,IAAI,GAAG,CAAC,CAAC,GAAG,uBAAuB,EAAE,GAAG,CAAC,OAAO,CAAC,cAAc,EAAE,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,WAAW,EAAE,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,CACrG,CAAC,IAAI,EAAE,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CAAC,IAAI,EAAE,EAAE,MAAM,EAAE,GAAG,EAAE,OAAO,EAAE,OAAO,EAAE,CAAC,CAAC;QAC9D,CAAC;QAED,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,CAAC;QAErC,MAAM,MAAM,GAAG,cAAc,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QAE/E,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,6BAA6B,EAAE,MAAM,CAAC,CAAC;QAC5D,IAAI,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YACvB,QAAQ,CAAC,OAAO,CAAC,MAAM,CAAC,+BAA+B,EAAE,MAAM,CAAC,CAAC;QAClE,CAAC;QAED,OAAO,QAAQ,CAAC;IAAA,CAChB,CAAC;AAAA,CACF,CAAC"}
@@ -2,6 +2,7 @@ export interface XRPCErrorOptions {
2
2
  status: number;
3
3
  error: string;
4
4
  description?: string;
5
+ headers?: HeadersInit;
5
6
  }
6
7
 
7
8
  export class XRPCError extends Error {
@@ -12,24 +13,35 @@ export class XRPCError extends Error {
12
13
  readonly error: string;
13
14
  /** error message */
14
15
  readonly description?: string;
16
+ /** response headers */
17
+ readonly headers?: HeadersInit;
15
18
 
16
- constructor({ status, error, description }: XRPCErrorOptions) {
19
+ constructor({ status, error, description, headers }: XRPCErrorOptions) {
17
20
  super(`${error} > ${description ?? '(unspecified description)'}`);
18
21
 
19
22
  this.status = status;
20
23
 
21
24
  this.error = error;
22
25
  this.description = description;
26
+ this.headers = headers;
23
27
  }
24
28
 
25
29
  toResponse(): Response {
26
- return Response.json({ error: this.error, message: this.description }, { status: this.status });
30
+ return Response.json(
31
+ { error: this.error, message: this.description },
32
+ { status: this.status, headers: this.headers },
33
+ );
27
34
  }
28
35
  }
29
36
 
30
37
  export class InvalidRequestError extends XRPCError {
31
- constructor({ status = 400, error = 'InvalidRequest', description }: Partial<XRPCErrorOptions> = {}) {
32
- super({ status, error, description });
38
+ constructor({
39
+ status = 400,
40
+ error = 'InvalidRequest',
41
+ description,
42
+ headers,
43
+ }: Partial<XRPCErrorOptions> = {}) {
44
+ super({ status, error, description, headers });
33
45
  }
34
46
  }
35
47
 
@@ -38,44 +50,70 @@ export class AuthRequiredError extends XRPCError {
38
50
  status = 401,
39
51
  error = 'AuthenticationRequired',
40
52
  description,
53
+ headers,
41
54
  }: Partial<XRPCErrorOptions> = {}) {
42
- super({ status, error, description });
55
+ super({ status, error, description, headers });
43
56
  }
44
57
  }
45
58
 
46
59
  export class ForbiddenError extends XRPCError {
47
- constructor({ status = 403, error = 'Forbidden', description }: Partial<XRPCErrorOptions> = {}) {
48
- super({ status, error, description });
60
+ constructor({ status = 403, error = 'Forbidden', description, headers }: Partial<XRPCErrorOptions> = {}) {
61
+ super({ status, error, description, headers });
49
62
  }
50
63
  }
51
64
 
52
65
  export class RateLimitExceededError extends XRPCError {
53
- constructor({ status = 429, error = 'RateLimitExceeded', description }: Partial<XRPCErrorOptions> = {}) {
54
- super({ status, error, description });
66
+ constructor({
67
+ status = 429,
68
+ error = 'RateLimitExceeded',
69
+ description,
70
+ headers,
71
+ }: Partial<XRPCErrorOptions> = {}) {
72
+ super({ status, error, description, headers });
55
73
  }
56
74
  }
57
75
 
58
76
  export class InternalServerError extends XRPCError {
59
- constructor({ status = 500, error = 'InternalServerError', description }: Partial<XRPCErrorOptions> = {}) {
60
- super({ status, error, description });
77
+ constructor({
78
+ status = 500,
79
+ error = 'InternalServerError',
80
+ description,
81
+ headers,
82
+ }: Partial<XRPCErrorOptions> = {}) {
83
+ super({ status, error, description, headers });
61
84
  }
62
85
  }
63
86
 
64
87
  export class UpstreamFailureError extends XRPCError {
65
- constructor({ status = 502, error = 'UpstreamFailure', description }: Partial<XRPCErrorOptions> = {}) {
66
- super({ status, error, description });
88
+ constructor({
89
+ status = 502,
90
+ error = 'UpstreamFailure',
91
+ description,
92
+ headers,
93
+ }: Partial<XRPCErrorOptions> = {}) {
94
+ super({ status, error, description, headers });
67
95
  }
68
96
  }
69
97
 
70
98
  export class NotEnoughResourcesError extends XRPCError {
71
- constructor({ status = 503, error = 'NotEnoughResources', description }: Partial<XRPCErrorOptions> = {}) {
72
- super({ status, error, description });
99
+ constructor({
100
+ status = 503,
101
+ error = 'NotEnoughResources',
102
+ description,
103
+ headers,
104
+ }: Partial<XRPCErrorOptions> = {}) {
105
+ super({ status, error, description, headers });
73
106
  }
74
107
  }
75
108
 
76
109
  export class UpstreamTimeoutError extends XRPCError {
77
- constructor({ status = 504, error = 'UpstreamTimeout', description }: Partial<XRPCErrorOptions> = {}) {
78
- super({ status, error, description });
110
+ constructor({
111
+ status = 504,
112
+ error = 'UpstreamTimeout',
113
+ description,
114
+ headers,
115
+ }: Partial<XRPCErrorOptions> = {}) {
116
+ super({ status, error, description, headers });
79
117
  }
80
118
  }
81
119
 
@@ -5,6 +5,8 @@ export interface CORSOptions {
5
5
  exposedHeaders?: string[];
6
6
  /** Additional headers to allow */
7
7
  allowedHeaders?: string[];
8
+ /** NSID prefixes to exclude from CORS handling */
9
+ exclude?: string[];
8
10
  }
9
11
 
10
12
  const DEFAULT_EXPOSED_HEADERS = [
@@ -27,6 +29,8 @@ const DEFAULT_ALLOWED_HEADERS = [
27
29
  'atproto-proxy',
28
30
  ];
29
31
 
32
+ const RE_XRPC_NSID = /^\/xrpc\/([^?]*)/;
33
+
30
34
  export const cors = (options: CORSOptions = {}): FetchMiddleware => {
31
35
  const exposedHeaders = Array.from(
32
36
  new Set([...DEFAULT_EXPOSED_HEADERS, ...(options.exposedHeaders?.map((h) => h.toLowerCase()) || [])]),
@@ -38,7 +42,29 @@ export const cors = (options: CORSOptions = {}): FetchMiddleware => {
38
42
  .sort()
39
43
  .join(',');
40
44
 
45
+ const exclude = options.exclude;
46
+
41
47
  return async (request, next) => {
48
+ // check if this NSID should be excluded from CORS handling
49
+ if (exclude) {
50
+ const url = new URL(request.url);
51
+ const match = RE_XRPC_NSID.exec(url.pathname);
52
+
53
+ if (match) {
54
+ const nsid = match[1];
55
+ const excluded = exclude.some((pattern) => {
56
+ if (pattern.endsWith('.*')) {
57
+ return nsid.startsWith(pattern.slice(0, -1));
58
+ }
59
+ return nsid === pattern;
60
+ });
61
+
62
+ if (excluded) {
63
+ return next(request);
64
+ }
65
+ }
66
+ }
67
+
42
68
  const origin = request.headers.get('origin') || '*';
43
69
 
44
70
  // Handle preflight requests
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "@atcute/xrpc-server",
4
- "version": "0.1.4",
4
+ "version": "0.1.6",
5
5
  "description": "a small web framework for handling XRPC operations",
6
6
  "license": "0BSD",
7
7
  "repository": {
@@ -25,12 +25,12 @@
25
25
  "dependencies": {
26
26
  "@badrap/valita": "^0.4.6",
27
27
  "nanoid": "^5.1.6",
28
- "@atcute/cbor": "^2.2.8",
29
28
  "@atcute/crypto": "^2.3.0",
29
+ "@atcute/cbor": "^2.2.8",
30
30
  "@atcute/identity": "^1.1.3",
31
31
  "@atcute/identity-resolver": "^1.2.1",
32
+ "@atcute/lexicons": "^1.2.6",
32
33
  "@atcute/multibase": "^1.1.6",
33
- "@atcute/lexicons": "^1.2.5",
34
34
  "@atcute/uint8array": "^1.0.6"
35
35
  },
36
36
  "devDependencies": {