@atproto/xrpc-server 0.11.5 → 0.11.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.
- package/CHANGELOG.md +20 -0
- package/package.json +26 -21
- package/jest.config.cjs +0 -21
- package/src/auth.ts +0 -235
- package/src/errors.ts +0 -312
- package/src/index.ts +0 -14
- package/src/logger.ts +0 -8
- package/src/rate-limiter-http.ts +0 -82
- package/src/rate-limiter.ts +0 -279
- package/src/server.ts +0 -858
- package/src/stream/frames.ts +0 -125
- package/src/stream/index.ts +0 -5
- package/src/stream/logger.ts +0 -6
- package/src/stream/server.ts +0 -66
- package/src/stream/stream.ts +0 -39
- package/src/stream/subscription.ts +0 -96
- package/src/stream/types.ts +0 -27
- package/src/types.ts +0 -330
- package/src/util.ts +0 -708
- package/tests/_util.ts +0 -124
- package/tests/auth.test.ts +0 -333
- package/tests/bodies.test.ts +0 -608
- package/tests/errors.test.ts +0 -299
- package/tests/frames.test.ts +0 -135
- package/tests/ipld.test.ts +0 -97
- package/tests/parameters.test.ts +0 -331
- package/tests/parsing.test.ts +0 -89
- package/tests/procedures.test.ts +0 -176
- package/tests/queries.test.ts +0 -140
- package/tests/rate-limiter.test.ts +0 -312
- package/tests/responses.test.ts +0 -72
- package/tests/stream.test.ts +0 -169
- package/tests/subscriptions.test.ts +0 -398
- package/tsconfig.build.json +0 -8
- package/tsconfig.build.tsbuildinfo +0 -1
- package/tsconfig.json +0 -7
- package/tsconfig.tests.json +0 -7
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,25 @@
|
|
|
1
1
|
# @atproto/xrpc-server
|
|
2
2
|
|
|
3
|
+
## 0.11.6
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#5099](https://github.com/bluesky-social/atproto/pull/5099) [`b43ec31`](https://github.com/bluesky-social/atproto/commit/b43ec31f247f4461725b01226885f88bd430ca07) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Update TypeScript build to rely on references to composite internal projects
|
|
8
|
+
|
|
9
|
+
- [#5099](https://github.com/bluesky-social/atproto/pull/5099) [`b43ec31`](https://github.com/bluesky-social/atproto/commit/b43ec31f247f4461725b01226885f88bd430ca07) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Bundle only necessary files in the NPM tarball, including the `CHANGELOG.md` and `README.md` files (if present).
|
|
10
|
+
|
|
11
|
+
- Updated dependencies [[`b43ec31`](https://github.com/bluesky-social/atproto/commit/b43ec31f247f4461725b01226885f88bd430ca07), [`b43ec31`](https://github.com/bluesky-social/atproto/commit/b43ec31f247f4461725b01226885f88bd430ca07), [`b43ec31`](https://github.com/bluesky-social/atproto/commit/b43ec31f247f4461725b01226885f88bd430ca07), [`b43ec31`](https://github.com/bluesky-social/atproto/commit/b43ec31f247f4461725b01226885f88bd430ca07)]:
|
|
12
|
+
- @atproto/lex-client@0.2.1
|
|
13
|
+
- @atproto/lex-schema@0.1.6
|
|
14
|
+
- @atproto/lex-cbor@0.1.3
|
|
15
|
+
- @atproto/lex-data@0.1.4
|
|
16
|
+
- @atproto/lex-json@0.1.3
|
|
17
|
+
- @atproto/ws-client@0.1.4
|
|
18
|
+
- @atproto/lexicon@0.7.4
|
|
19
|
+
- @atproto/common@0.6.5
|
|
20
|
+
- @atproto/crypto@0.5.3
|
|
21
|
+
- @atproto/xrpc@0.8.3
|
|
22
|
+
|
|
3
23
|
## 0.11.5
|
|
4
24
|
|
|
5
25
|
### Patch Changes
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atproto/xrpc-server",
|
|
3
|
-
"version": "0.11.
|
|
3
|
+
"version": "0.11.6",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "atproto HTTP API (XRPC) server library",
|
|
6
6
|
"keywords": [
|
|
@@ -13,6 +13,18 @@
|
|
|
13
13
|
"url": "https://github.com/bluesky-social/atproto",
|
|
14
14
|
"directory": "packages/xrpc-server"
|
|
15
15
|
},
|
|
16
|
+
"files": [
|
|
17
|
+
"./dist",
|
|
18
|
+
"./README.md",
|
|
19
|
+
"./CHANGELOG.md"
|
|
20
|
+
],
|
|
21
|
+
"type": "module",
|
|
22
|
+
"exports": {
|
|
23
|
+
".": {
|
|
24
|
+
"types": "./dist/index.d.ts",
|
|
25
|
+
"default": "./dist/index.js"
|
|
26
|
+
}
|
|
27
|
+
},
|
|
16
28
|
"engines": {
|
|
17
29
|
"node": ">=22"
|
|
18
30
|
},
|
|
@@ -22,16 +34,16 @@
|
|
|
22
34
|
"mime-types": "^2.1.35",
|
|
23
35
|
"rate-limiter-flexible": "^2.4.1",
|
|
24
36
|
"ws": "^8.12.0",
|
|
25
|
-
"@atproto/common": "^0.6.
|
|
26
|
-
"@atproto/
|
|
27
|
-
"@atproto/
|
|
28
|
-
"@atproto/lex-
|
|
29
|
-
"@atproto/lex-
|
|
30
|
-
"@atproto/lex-
|
|
31
|
-
"@atproto/
|
|
32
|
-
"@atproto/
|
|
33
|
-
"@atproto/
|
|
34
|
-
"@atproto/
|
|
37
|
+
"@atproto/common": "^0.6.5",
|
|
38
|
+
"@atproto/lex-cbor": "^0.1.3",
|
|
39
|
+
"@atproto/crypto": "^0.5.3",
|
|
40
|
+
"@atproto/lex-data": "^0.1.4",
|
|
41
|
+
"@atproto/lex-client": "^0.2.1",
|
|
42
|
+
"@atproto/lex-schema": "^0.1.6",
|
|
43
|
+
"@atproto/lexicon": "^0.7.4",
|
|
44
|
+
"@atproto/lex-json": "^0.1.3",
|
|
45
|
+
"@atproto/xrpc": "^0.8.3",
|
|
46
|
+
"@atproto/ws-client": "^0.1.4"
|
|
35
47
|
},
|
|
36
48
|
"devDependencies": {
|
|
37
49
|
"@jest/globals": "^30.0.0",
|
|
@@ -45,16 +57,9 @@
|
|
|
45
57
|
"jose": "^5.0.1",
|
|
46
58
|
"key-encoder": "^2.0.3",
|
|
47
59
|
"multiformats": "^13.0.0",
|
|
48
|
-
"@atproto/crypto": "^0.5.
|
|
49
|
-
"@atproto/lex": "^0.1.
|
|
50
|
-
"@atproto/lex-document": "^0.1.
|
|
51
|
-
},
|
|
52
|
-
"type": "module",
|
|
53
|
-
"exports": {
|
|
54
|
-
".": {
|
|
55
|
-
"types": "./dist/index.d.ts",
|
|
56
|
-
"default": "./dist/index.js"
|
|
57
|
-
}
|
|
60
|
+
"@atproto/crypto": "^0.5.3",
|
|
61
|
+
"@atproto/lex": "^0.1.7",
|
|
62
|
+
"@atproto/lex-document": "^0.1.3"
|
|
58
63
|
},
|
|
59
64
|
"scripts": {
|
|
60
65
|
"test": "NODE_OPTIONS=--experimental-vm-modules jest",
|
package/jest.config.cjs
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
/** @type {import('jest').Config} */
|
|
2
|
-
module.exports = {
|
|
3
|
-
displayName: 'XRPC Server',
|
|
4
|
-
transform: {
|
|
5
|
-
'^.+\\.(t|j)s$': [
|
|
6
|
-
'@swc/jest',
|
|
7
|
-
{
|
|
8
|
-
jsc: {
|
|
9
|
-
parser: { syntax: 'typescript', importAttributes: true },
|
|
10
|
-
experimental: { keepImportAttributes: true },
|
|
11
|
-
transform: {},
|
|
12
|
-
},
|
|
13
|
-
module: { type: 'es6' },
|
|
14
|
-
},
|
|
15
|
-
],
|
|
16
|
-
},
|
|
17
|
-
extensionsToTreatAsEsm: ['.ts'],
|
|
18
|
-
transformIgnorePatterns: [],
|
|
19
|
-
setupFiles: ['<rootDir>/../../test.setup.ts'],
|
|
20
|
-
moduleNameMapper: { '^(\\.\\.?\\/.+)\\.js$': ['$1.ts', '$1.js'] },
|
|
21
|
-
}
|
package/src/auth.ts
DELETED
|
@@ -1,235 +0,0 @@
|
|
|
1
|
-
import * as common from '@atproto/common'
|
|
2
|
-
import { MINUTE } from '@atproto/common'
|
|
3
|
-
import * as crypto from '@atproto/crypto'
|
|
4
|
-
import { DidString, isDidString } from '@atproto/lex-schema'
|
|
5
|
-
import { AuthRequiredError } from './errors.js'
|
|
6
|
-
|
|
7
|
-
type ServiceJwtParams = {
|
|
8
|
-
iss: string
|
|
9
|
-
aud: string
|
|
10
|
-
iat?: number
|
|
11
|
-
exp?: number
|
|
12
|
-
lxm: string | null
|
|
13
|
-
keypair: crypto.Keypair
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
type ServiceJwtHeaders = {
|
|
17
|
-
alg: string
|
|
18
|
-
} & Record<string, unknown>
|
|
19
|
-
|
|
20
|
-
type ServiceJwtPayload = {
|
|
21
|
-
iss: DidString | `${DidString}#${string}`
|
|
22
|
-
aud: string
|
|
23
|
-
exp: number
|
|
24
|
-
lxm?: string
|
|
25
|
-
jti?: string
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export const createServiceJwt = async (
|
|
29
|
-
params: ServiceJwtParams,
|
|
30
|
-
): Promise<string> => {
|
|
31
|
-
const { iss, aud, keypair } = params
|
|
32
|
-
const iat = params.iat ?? Math.floor(Date.now() / 1e3)
|
|
33
|
-
const exp = params.exp ?? iat + MINUTE / 1e3
|
|
34
|
-
const lxm = params.lxm ?? undefined
|
|
35
|
-
const jti = await crypto.randomStr(16, 'hex')
|
|
36
|
-
const header = {
|
|
37
|
-
typ: 'JWT',
|
|
38
|
-
alg: keypair.jwtAlg,
|
|
39
|
-
}
|
|
40
|
-
const payload = common.noUndefinedVals({
|
|
41
|
-
iat,
|
|
42
|
-
iss,
|
|
43
|
-
aud,
|
|
44
|
-
exp,
|
|
45
|
-
lxm,
|
|
46
|
-
jti,
|
|
47
|
-
})
|
|
48
|
-
const toSignStr = `${jsonToB64Url(header)}.${jsonToB64Url(payload)}`
|
|
49
|
-
const toSign = Buffer.from(toSignStr, 'utf8')
|
|
50
|
-
const sig = Buffer.from(await keypair.sign(toSign))
|
|
51
|
-
return `${toSignStr}.${sig.toString('base64url')}`
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
export const createServiceAuthHeaders = async (params: ServiceJwtParams) => {
|
|
55
|
-
const jwt = await createServiceJwt(params)
|
|
56
|
-
return {
|
|
57
|
-
headers: { authorization: `Bearer ${jwt}` },
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
const jsonToB64Url = (json: Record<string, unknown>): string => {
|
|
62
|
-
return Buffer.from(JSON.stringify(json)).toString('base64url')
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
export type VerifySignatureWithKeyFn = (
|
|
66
|
-
key: string,
|
|
67
|
-
msgBytes: Uint8Array,
|
|
68
|
-
sigBytes: Uint8Array,
|
|
69
|
-
alg: string,
|
|
70
|
-
) => Promise<boolean>
|
|
71
|
-
|
|
72
|
-
export const verifyJwt = async (
|
|
73
|
-
jwtStr: string,
|
|
74
|
-
ownDid: string | null, // null indicates to skip the audience check
|
|
75
|
-
lxm: string | null, // null indicates to skip the lxm check
|
|
76
|
-
getSigningKey: (
|
|
77
|
-
iss: DidString | `${DidString}#${string}`,
|
|
78
|
-
forceRefresh: boolean,
|
|
79
|
-
) => Promise<string>,
|
|
80
|
-
verifySignatureWithKey: VerifySignatureWithKeyFn = cryptoVerifySignatureWithKey,
|
|
81
|
-
): Promise<ServiceJwtPayload> => {
|
|
82
|
-
const parts = jwtStr.split('.')
|
|
83
|
-
if (parts.length !== 3) {
|
|
84
|
-
throw new AuthRequiredError('poorly formatted jwt', 'BadJwt')
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
const header = parseHeader(parts[0])
|
|
88
|
-
|
|
89
|
-
// The spec does not describe what to do with the "typ" claim. We can,
|
|
90
|
-
// however, forbid some values that are not compatible with our use case.
|
|
91
|
-
if (
|
|
92
|
-
// service tokens are not OAuth 2.0 access tokens
|
|
93
|
-
// https://datatracker.ietf.org/doc/html/rfc9068
|
|
94
|
-
header['typ'] === 'at+jwt' ||
|
|
95
|
-
// "refresh+jwt" is a non-standard type used by the @atproto packages
|
|
96
|
-
header['typ'] === 'refresh+jwt' ||
|
|
97
|
-
// "DPoP" proofs are not meant to be used as service tokens
|
|
98
|
-
// https://datatracker.ietf.org/doc/html/rfc9449
|
|
99
|
-
header['typ'] === 'dpop+jwt'
|
|
100
|
-
) {
|
|
101
|
-
throw new AuthRequiredError(
|
|
102
|
-
`Invalid jwt type "${header['typ']}"`,
|
|
103
|
-
'BadJwtType',
|
|
104
|
-
)
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
const payload = parsePayload(parts[1])
|
|
108
|
-
const sig = parts[2]
|
|
109
|
-
|
|
110
|
-
if (Date.now() / 1000 > payload.exp) {
|
|
111
|
-
throw new AuthRequiredError('jwt expired', 'JwtExpired')
|
|
112
|
-
}
|
|
113
|
-
if (ownDid !== null && payload.aud !== ownDid) {
|
|
114
|
-
throw new AuthRequiredError(
|
|
115
|
-
'jwt audience does not match service did',
|
|
116
|
-
'BadJwtAudience',
|
|
117
|
-
)
|
|
118
|
-
}
|
|
119
|
-
if (lxm !== null && payload.lxm !== lxm) {
|
|
120
|
-
throw new AuthRequiredError(
|
|
121
|
-
payload.lxm !== undefined
|
|
122
|
-
? `bad jwt lexicon method ("lxm"). must match: ${lxm}`
|
|
123
|
-
: `missing jwt lexicon method ("lxm"). must match: ${lxm}`,
|
|
124
|
-
'BadJwtLexiconMethod',
|
|
125
|
-
)
|
|
126
|
-
}
|
|
127
|
-
if (!payload.iss || !isDidStringOrService(payload.iss)) {
|
|
128
|
-
throw new AuthRequiredError('jwt iss is not a valid did', 'BadJwtIss')
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
const msgBytes = Buffer.from(parts.slice(0, 2).join('.'), 'utf8')
|
|
132
|
-
const sigBytes = Buffer.from(sig, 'base64url')
|
|
133
|
-
|
|
134
|
-
const signingKey = await getSigningKey(payload.iss, false)
|
|
135
|
-
const { alg } = header
|
|
136
|
-
|
|
137
|
-
let validSig: boolean
|
|
138
|
-
try {
|
|
139
|
-
validSig = await verifySignatureWithKey(signingKey, msgBytes, sigBytes, alg)
|
|
140
|
-
} catch (err) {
|
|
141
|
-
throw new AuthRequiredError(
|
|
142
|
-
'could not verify jwt signature',
|
|
143
|
-
'BadJwtSignature',
|
|
144
|
-
)
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
if (!validSig) {
|
|
148
|
-
// get fresh signing key in case it failed due to a recent rotation
|
|
149
|
-
const freshSigningKey = await getSigningKey(payload.iss, true)
|
|
150
|
-
try {
|
|
151
|
-
validSig =
|
|
152
|
-
freshSigningKey !== signingKey
|
|
153
|
-
? await verifySignatureWithKey(
|
|
154
|
-
freshSigningKey,
|
|
155
|
-
msgBytes,
|
|
156
|
-
sigBytes,
|
|
157
|
-
alg,
|
|
158
|
-
)
|
|
159
|
-
: false
|
|
160
|
-
} catch (err) {
|
|
161
|
-
throw new AuthRequiredError(
|
|
162
|
-
'could not verify jwt signature',
|
|
163
|
-
'BadJwtSignature',
|
|
164
|
-
)
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
if (!validSig) {
|
|
169
|
-
throw new AuthRequiredError(
|
|
170
|
-
'jwt signature does not match jwt issuer',
|
|
171
|
-
'BadJwtSignature',
|
|
172
|
-
)
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
return payload
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
export const cryptoVerifySignatureWithKey: VerifySignatureWithKeyFn = async (
|
|
179
|
-
key: string,
|
|
180
|
-
msgBytes: Uint8Array,
|
|
181
|
-
sigBytes: Uint8Array,
|
|
182
|
-
alg: string,
|
|
183
|
-
) => {
|
|
184
|
-
return crypto.verifySignature(key, msgBytes, sigBytes, {
|
|
185
|
-
jwtAlg: alg,
|
|
186
|
-
allowMalleableSig: true,
|
|
187
|
-
})
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
const parseB64UrlToJson = (b64: string) => {
|
|
191
|
-
return JSON.parse(Buffer.from(b64, 'base64url').toString('utf8'))
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
const parseHeader = (b64: string): ServiceJwtHeaders => {
|
|
195
|
-
const header = parseB64UrlToJson(b64)
|
|
196
|
-
if (!header || typeof header !== 'object' || typeof header.alg !== 'string') {
|
|
197
|
-
throw new AuthRequiredError('poorly formatted jwt', 'BadJwt')
|
|
198
|
-
}
|
|
199
|
-
return header
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
const parsePayload = (b64: string): ServiceJwtPayload => {
|
|
203
|
-
const payload = parseB64UrlToJson(b64)
|
|
204
|
-
if (
|
|
205
|
-
!payload ||
|
|
206
|
-
typeof payload !== 'object' ||
|
|
207
|
-
typeof payload.iss !== 'string' ||
|
|
208
|
-
typeof payload.aud !== 'string' ||
|
|
209
|
-
typeof payload.exp !== 'number' ||
|
|
210
|
-
(payload.lxm && typeof payload.lxm !== 'string') ||
|
|
211
|
-
(payload.nonce && typeof payload.nonce !== 'string')
|
|
212
|
-
) {
|
|
213
|
-
throw new AuthRequiredError('poorly formatted jwt', 'BadJwt')
|
|
214
|
-
}
|
|
215
|
-
return payload
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
function isDidStringOrService(
|
|
219
|
-
value: string,
|
|
220
|
-
): value is DidString | `${DidString}#${string}` {
|
|
221
|
-
const hashIdx = value.indexOf('#')
|
|
222
|
-
if (hashIdx === -1) {
|
|
223
|
-
return isDidString(value)
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
// basic validation of the fragment part
|
|
227
|
-
const fragmentLen = value.length - hashIdx - 1
|
|
228
|
-
if (fragmentLen < 1 || value.includes('#', hashIdx + 1)) {
|
|
229
|
-
return false
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
// Validate the did part
|
|
233
|
-
const didPart = value.slice(0, hashIdx)
|
|
234
|
-
return isDidString(didPart)
|
|
235
|
-
}
|
package/src/errors.ts
DELETED
|
@@ -1,312 +0,0 @@
|
|
|
1
|
-
// eslint-disable-next-line import/default, import/no-named-as-default-member
|
|
2
|
-
import httpErrors from 'http-errors'
|
|
3
|
-
// eslint-disable-next-line import/no-named-as-default-member
|
|
4
|
-
const { isHttpError } = httpErrors
|
|
5
|
-
import { LexError, XrpcError } from '@atproto/lex-client'
|
|
6
|
-
import { l } from '@atproto/lex-schema'
|
|
7
|
-
import {
|
|
8
|
-
ResponseType,
|
|
9
|
-
ResponseTypeStrings,
|
|
10
|
-
XRPCError as XRPCClientError,
|
|
11
|
-
httpResponseCodeToName,
|
|
12
|
-
httpResponseCodeToString,
|
|
13
|
-
} from '@atproto/xrpc'
|
|
14
|
-
|
|
15
|
-
// @NOTE Do not depend (directly or indirectly) on "./types" here, as it would
|
|
16
|
-
// create a circular dependency.
|
|
17
|
-
|
|
18
|
-
export const errorResult = l.object({
|
|
19
|
-
status: l.integer({ minimum: 400 }),
|
|
20
|
-
error: l.optional(l.string()),
|
|
21
|
-
message: l.optional(l.string()),
|
|
22
|
-
})
|
|
23
|
-
export type ErrorResult = l.Infer<typeof errorResult>
|
|
24
|
-
|
|
25
|
-
export function isErrorResult(v: unknown): v is ErrorResult {
|
|
26
|
-
return errorResult.safeParse(v).success
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export function excludeErrorResult<V>(v: V) {
|
|
30
|
-
if (isErrorResult(v)) throw XRPCError.fromErrorResult(v)
|
|
31
|
-
return v as Exclude<V, ErrorResult>
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export { ResponseType }
|
|
35
|
-
|
|
36
|
-
export class XRPCError extends Error {
|
|
37
|
-
constructor(
|
|
38
|
-
public type: ResponseType,
|
|
39
|
-
public errorMessage?: string,
|
|
40
|
-
public customErrorName?: string,
|
|
41
|
-
options?: ErrorOptions,
|
|
42
|
-
) {
|
|
43
|
-
super(errorMessage, options)
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
get statusCode(): number {
|
|
47
|
-
const { type } = this
|
|
48
|
-
|
|
49
|
-
// Fool-proofing. `new XRPCError(123.5 as number, '')` does not generate a TypeScript error.
|
|
50
|
-
// Because of this, we can end-up with any numeric value instead of an actual `ResponseType`.
|
|
51
|
-
// For legacy reasons, the `type` argument is not checked in the constructor, so we check it here.
|
|
52
|
-
if (type < 400 || type >= 600 || !Number.isFinite(type)) {
|
|
53
|
-
return 500
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
return type
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
get error(): string | undefined {
|
|
60
|
-
return this.customErrorName ?? this.typeName
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
get payload() {
|
|
64
|
-
return {
|
|
65
|
-
error: this.error,
|
|
66
|
-
message:
|
|
67
|
-
this.type === ResponseType.InternalServerError
|
|
68
|
-
? this.typeStr // Do not respond with error details for 500s
|
|
69
|
-
: this.errorMessage || this.typeStr,
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
get typeName(): string | undefined {
|
|
74
|
-
return ResponseType[this.type]
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
get typeStr(): string | undefined {
|
|
78
|
-
return ResponseTypeStrings[this.type]
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
static fromError(cause: unknown): XRPCError {
|
|
82
|
-
if (cause instanceof XRPCError) {
|
|
83
|
-
return cause
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
if (cause instanceof XRPCClientError) {
|
|
87
|
-
const { error, message, type } = mapFromClientError(cause)
|
|
88
|
-
return new XRPCError(type, message, error, { cause })
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
if (cause instanceof XrpcError) {
|
|
92
|
-
const { status, body } = cause.toDownstreamError()
|
|
93
|
-
return new XRPCError(status, body.message, body.error, { cause })
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
if (cause instanceof LexError) {
|
|
97
|
-
const data = cause.toJSON()
|
|
98
|
-
const type = ResponseType.InternalServerError
|
|
99
|
-
return new XRPCError(type, data.message, data.error, { cause })
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
if (isHttpError(cause)) {
|
|
103
|
-
return new XRPCError(cause.status, cause.message, cause.name, { cause })
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
if (isErrorResult(cause)) {
|
|
107
|
-
return this.fromErrorResult(cause)
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
if (cause instanceof Error) {
|
|
111
|
-
return new InternalServerError(cause.message, undefined, { cause })
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
return new InternalServerError(
|
|
115
|
-
'Unexpected internal server error',
|
|
116
|
-
undefined,
|
|
117
|
-
{ cause },
|
|
118
|
-
)
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
static fromErrorResult(err: ErrorResult): XRPCError {
|
|
122
|
-
return new XRPCError(err.status, err.message, err.error, { cause: err })
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
export class InvalidRequestError extends XRPCError {
|
|
127
|
-
constructor(
|
|
128
|
-
errorMessage?: string,
|
|
129
|
-
customErrorName?: string,
|
|
130
|
-
options?: ErrorOptions,
|
|
131
|
-
) {
|
|
132
|
-
super(ResponseType.InvalidRequest, errorMessage, customErrorName, options)
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
[Symbol.hasInstance](instance: unknown): boolean {
|
|
136
|
-
return (
|
|
137
|
-
instance instanceof XRPCError &&
|
|
138
|
-
instance.type === ResponseType.InvalidRequest
|
|
139
|
-
)
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
export class AuthRequiredError extends XRPCError {
|
|
144
|
-
constructor(
|
|
145
|
-
errorMessage?: string,
|
|
146
|
-
customErrorName?: string,
|
|
147
|
-
options?: ErrorOptions,
|
|
148
|
-
) {
|
|
149
|
-
super(
|
|
150
|
-
ResponseType.AuthenticationRequired,
|
|
151
|
-
errorMessage,
|
|
152
|
-
customErrorName,
|
|
153
|
-
options,
|
|
154
|
-
)
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
[Symbol.hasInstance](instance: unknown): boolean {
|
|
158
|
-
return (
|
|
159
|
-
instance instanceof XRPCError &&
|
|
160
|
-
instance.type === ResponseType.AuthenticationRequired
|
|
161
|
-
)
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
export class ForbiddenError extends XRPCError {
|
|
166
|
-
constructor(
|
|
167
|
-
errorMessage?: string,
|
|
168
|
-
customErrorName?: string,
|
|
169
|
-
options?: ErrorOptions,
|
|
170
|
-
) {
|
|
171
|
-
super(ResponseType.Forbidden, errorMessage, customErrorName, options)
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
[Symbol.hasInstance](instance: unknown): boolean {
|
|
175
|
-
return (
|
|
176
|
-
instance instanceof XRPCError && instance.type === ResponseType.Forbidden
|
|
177
|
-
)
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
export class InternalServerError extends XRPCError {
|
|
182
|
-
constructor(
|
|
183
|
-
errorMessage?: string,
|
|
184
|
-
customErrorName?: string,
|
|
185
|
-
options?: ErrorOptions,
|
|
186
|
-
) {
|
|
187
|
-
super(
|
|
188
|
-
ResponseType.InternalServerError,
|
|
189
|
-
errorMessage,
|
|
190
|
-
customErrorName,
|
|
191
|
-
options,
|
|
192
|
-
)
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
[Symbol.hasInstance](instance: unknown): boolean {
|
|
196
|
-
return (
|
|
197
|
-
instance instanceof XRPCError &&
|
|
198
|
-
instance.type === ResponseType.InternalServerError
|
|
199
|
-
)
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
export class UpstreamFailureError extends XRPCError {
|
|
204
|
-
constructor(
|
|
205
|
-
errorMessage?: string,
|
|
206
|
-
customErrorName?: string,
|
|
207
|
-
options?: ErrorOptions,
|
|
208
|
-
) {
|
|
209
|
-
super(ResponseType.UpstreamFailure, errorMessage, customErrorName, options)
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
[Symbol.hasInstance](instance: unknown): boolean {
|
|
213
|
-
return (
|
|
214
|
-
instance instanceof XRPCError &&
|
|
215
|
-
instance.type === ResponseType.UpstreamFailure
|
|
216
|
-
)
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
export class NotEnoughResourcesError extends XRPCError {
|
|
221
|
-
constructor(
|
|
222
|
-
errorMessage?: string,
|
|
223
|
-
customErrorName?: string,
|
|
224
|
-
options?: ErrorOptions,
|
|
225
|
-
) {
|
|
226
|
-
super(
|
|
227
|
-
ResponseType.NotEnoughResources,
|
|
228
|
-
errorMessage,
|
|
229
|
-
customErrorName,
|
|
230
|
-
options,
|
|
231
|
-
)
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
[Symbol.hasInstance](instance: unknown): boolean {
|
|
235
|
-
return (
|
|
236
|
-
instance instanceof XRPCError &&
|
|
237
|
-
instance.type === ResponseType.NotEnoughResources
|
|
238
|
-
)
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
export class UpstreamTimeoutError extends XRPCError {
|
|
243
|
-
constructor(
|
|
244
|
-
errorMessage?: string,
|
|
245
|
-
customErrorName?: string,
|
|
246
|
-
options?: ErrorOptions,
|
|
247
|
-
) {
|
|
248
|
-
super(ResponseType.UpstreamTimeout, errorMessage, customErrorName, options)
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
[Symbol.hasInstance](instance: unknown): boolean {
|
|
252
|
-
return (
|
|
253
|
-
instance instanceof XRPCError &&
|
|
254
|
-
instance.type === ResponseType.UpstreamTimeout
|
|
255
|
-
)
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
export class MethodNotImplementedError extends XRPCError {
|
|
260
|
-
constructor(
|
|
261
|
-
errorMessage?: string,
|
|
262
|
-
customErrorName?: string,
|
|
263
|
-
options?: ErrorOptions,
|
|
264
|
-
) {
|
|
265
|
-
super(
|
|
266
|
-
ResponseType.MethodNotImplemented,
|
|
267
|
-
errorMessage,
|
|
268
|
-
customErrorName,
|
|
269
|
-
options,
|
|
270
|
-
)
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
[Symbol.hasInstance](instance: unknown): boolean {
|
|
274
|
-
return (
|
|
275
|
-
instance instanceof XRPCError &&
|
|
276
|
-
instance.type === ResponseType.MethodNotImplemented
|
|
277
|
-
)
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
/**
|
|
282
|
-
* Converts an upstream XRPC {@link ResponseType} into a downstream {@link ResponseType}.
|
|
283
|
-
*/
|
|
284
|
-
function mapFromClientError(error: XRPCClientError): {
|
|
285
|
-
error: string
|
|
286
|
-
message: string
|
|
287
|
-
type: ResponseType
|
|
288
|
-
} {
|
|
289
|
-
switch (error.status) {
|
|
290
|
-
case ResponseType.InvalidResponse:
|
|
291
|
-
// Upstream server returned an XRPC response that is not compatible with our internal lexicon definitions for that XRPC method.
|
|
292
|
-
// @NOTE This could be reflected as both a 500 ("we" are at fault) and 502 ("they" are at fault). Let's be gents about it.
|
|
293
|
-
return {
|
|
294
|
-
error: httpResponseCodeToName(ResponseType.InternalServerError),
|
|
295
|
-
message: httpResponseCodeToString(ResponseType.InternalServerError),
|
|
296
|
-
type: ResponseType.InternalServerError,
|
|
297
|
-
}
|
|
298
|
-
case ResponseType.Unknown:
|
|
299
|
-
// Typically a network error / unknown host
|
|
300
|
-
return {
|
|
301
|
-
error: httpResponseCodeToName(ResponseType.InternalServerError),
|
|
302
|
-
message: httpResponseCodeToString(ResponseType.InternalServerError),
|
|
303
|
-
type: ResponseType.InternalServerError,
|
|
304
|
-
}
|
|
305
|
-
default:
|
|
306
|
-
return {
|
|
307
|
-
error: error.error,
|
|
308
|
-
message: error.message,
|
|
309
|
-
type: error.status,
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
export * from './auth.js'
|
|
2
|
-
export * from './errors.js'
|
|
3
|
-
export * from './rate-limiter.js'
|
|
4
|
-
export * from './server.js'
|
|
5
|
-
export * from './stream/index.js'
|
|
6
|
-
export * from './types.js'
|
|
7
|
-
|
|
8
|
-
export {
|
|
9
|
-
ServerTimer,
|
|
10
|
-
parseReqEncoding,
|
|
11
|
-
parseReqNsid,
|
|
12
|
-
serverTimingHeader,
|
|
13
|
-
} from './util.js'
|
|
14
|
-
export type { ServerTiming } from './util.js'
|