@atproto/xrpc-server 0.7.8 → 0.7.10
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 +27 -0
- package/dist/auth.d.ts.map +1 -1
- package/dist/auth.js +1 -1
- package/dist/auth.js.map +1 -1
- package/dist/rate-limiter.d.ts +1 -1
- package/dist/rate-limiter.d.ts.map +1 -1
- package/dist/server.d.ts +1 -1
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +55 -30
- package/dist/server.js.map +1 -1
- package/dist/stream/frames.d.ts +1 -1
- package/dist/stream/frames.d.ts.map +1 -1
- package/dist/stream/server.d.ts +2 -2
- package/dist/stream/server.d.ts.map +1 -1
- package/dist/stream/server.js +3 -6
- package/dist/stream/server.js.map +1 -1
- package/dist/stream/stream.d.ts +1 -1
- package/dist/stream/stream.d.ts.map +1 -1
- package/dist/stream/stream.js +1 -1
- package/dist/stream/stream.js.map +1 -1
- package/dist/stream/subscription.js +1 -1
- package/dist/stream/subscription.js.map +1 -1
- package/dist/stream/websocket-keepalive.d.ts +1 -1
- package/dist/stream/websocket-keepalive.d.ts.map +1 -1
- package/dist/stream/websocket-keepalive.js +1 -1
- package/dist/stream/websocket-keepalive.js.map +1 -1
- package/dist/types.d.ts +62 -50
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +26 -28
- package/dist/types.js.map +1 -1
- package/dist/util.d.ts +2 -2
- package/dist/util.d.ts.map +1 -1
- package/dist/util.js +1 -1
- package/dist/util.js.map +1 -1
- package/package.json +9 -6
- package/src/auth.ts +1 -1
- package/src/rate-limiter.ts +1 -1
- package/src/server.ts +49 -44
- package/src/stream/frames.ts +4 -4
- package/src/stream/server.ts +3 -3
- package/src/stream/stream.ts +3 -3
- package/src/stream/subscription.ts +1 -1
- package/src/stream/websocket-keepalive.ts +1 -1
- package/src/types.ts +46 -37
- package/src/util.ts +9 -10
- package/tests/_util.ts +1 -1
- package/tests/auth.test.ts +4 -4
- package/tests/bodies.test.ts +4 -4
- package/tests/errors.test.ts +1 -1
- package/tests/frames.test.ts +1 -1
- package/tests/ipld.test.ts +2 -2
- package/tests/parameters.test.ts +1 -1
- package/tests/procedures.test.ts +2 -2
- package/tests/queries.test.ts +1 -1
- package/tests/responses.test.ts +2 -2
- package/tests/stream.test.ts +3 -3
- package/tests/subscriptions.test.ts +5 -5
package/src/stream/frames.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import * as uint8arrays from 'uint8arrays'
|
|
2
|
-
import {
|
|
2
|
+
import { cborDecodeMulti, cborEncode } from '@atproto/common'
|
|
3
3
|
import {
|
|
4
|
-
|
|
4
|
+
ErrorFrameBody,
|
|
5
|
+
ErrorFrameHeader,
|
|
5
6
|
FrameHeader,
|
|
6
7
|
FrameType,
|
|
7
8
|
MessageFrameHeader,
|
|
8
|
-
ErrorFrameHeader,
|
|
9
|
-
ErrorFrameBody,
|
|
10
9
|
errorFrameBody,
|
|
10
|
+
frameHeader,
|
|
11
11
|
} from './types'
|
|
12
12
|
|
|
13
13
|
export abstract class Frame {
|
package/src/stream/server.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { IncomingMessage } from 'http'
|
|
2
|
-
import {
|
|
1
|
+
import { IncomingMessage } from 'node:http'
|
|
2
|
+
import { ServerOptions, WebSocket, WebSocketServer } from 'ws'
|
|
3
3
|
import { ErrorFrame, Frame } from './frames'
|
|
4
|
-
import logger from './logger'
|
|
4
|
+
import { logger } from './logger'
|
|
5
5
|
import { CloseCode, DisconnectError } from './types'
|
|
6
6
|
|
|
7
7
|
export class XrpcStreamServer {
|
package/src/stream/stream.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import { DuplexOptions } from 'node:stream'
|
|
2
|
+
import { WebSocket, createWebSocketStream } from 'ws'
|
|
3
|
+
import { ResponseType, XRPCError } from '@atproto/xrpc'
|
|
4
4
|
import { Frame, MessageFrame } from './frames'
|
|
5
5
|
|
|
6
6
|
export function streamByteChunks(ws: WebSocket, options?: DuplexOptions) {
|
package/src/types.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
+
import { IncomingMessage } from 'node:http'
|
|
2
|
+
import { Readable } from 'node:stream'
|
|
3
|
+
import express from 'express'
|
|
4
|
+
import { isHttpError } from 'http-errors'
|
|
5
|
+
import { z } from 'zod'
|
|
1
6
|
import {
|
|
2
7
|
ResponseType,
|
|
3
8
|
ResponseTypeNames,
|
|
4
9
|
ResponseTypeStrings,
|
|
5
10
|
XRPCError as XRPCClientError,
|
|
6
11
|
} from '@atproto/xrpc'
|
|
7
|
-
import express from 'express'
|
|
8
|
-
import { IncomingMessage } from 'http'
|
|
9
|
-
import { isHttpError } from 'http-errors'
|
|
10
|
-
import { Readable } from 'stream'
|
|
11
|
-
import zod from 'zod'
|
|
12
12
|
|
|
13
13
|
export type CatchallHandler = (
|
|
14
14
|
req: express.Request,
|
|
@@ -29,6 +29,17 @@ export type Options = {
|
|
|
29
29
|
global?: ServerRateLimitDescription[]
|
|
30
30
|
shared?: ServerRateLimitDescription[]
|
|
31
31
|
}
|
|
32
|
+
/**
|
|
33
|
+
* By default, errors are converted to {@link XRPCError} using
|
|
34
|
+
* {@link XRPCError.fromError} before being rendered. If method handlers throw
|
|
35
|
+
* error objects that are not properly rendered in the HTTP response, this
|
|
36
|
+
* function can be used to properly convert them to {@link XRPCError}. The
|
|
37
|
+
* provided function will typically fallback to the default error conversion
|
|
38
|
+
* (`return XRPCError.fromError(err)`) if the error is not recognized.
|
|
39
|
+
*
|
|
40
|
+
* @note This function should not throw errors.
|
|
41
|
+
*/
|
|
42
|
+
errorParser?: (err: unknown) => XRPCError
|
|
32
43
|
}
|
|
33
44
|
|
|
34
45
|
export type UndecodedParams = (typeof express.request)['query']
|
|
@@ -36,60 +47,56 @@ export type UndecodedParams = (typeof express.request)['query']
|
|
|
36
47
|
export type Primitive = string | number | boolean
|
|
37
48
|
export type Params = Record<string, Primitive | Primitive[] | undefined>
|
|
38
49
|
|
|
39
|
-
export const handlerInput =
|
|
40
|
-
encoding:
|
|
41
|
-
body:
|
|
50
|
+
export const handlerInput = z.object({
|
|
51
|
+
encoding: z.string(),
|
|
52
|
+
body: z.any(),
|
|
42
53
|
})
|
|
43
|
-
export type HandlerInput =
|
|
54
|
+
export type HandlerInput = z.infer<typeof handlerInput>
|
|
44
55
|
|
|
45
|
-
export const handlerAuth =
|
|
46
|
-
credentials:
|
|
47
|
-
artifacts:
|
|
56
|
+
export const handlerAuth = z.object({
|
|
57
|
+
credentials: z.any(),
|
|
58
|
+
artifacts: z.any(),
|
|
48
59
|
})
|
|
49
|
-
export type HandlerAuth =
|
|
60
|
+
export type HandlerAuth = z.infer<typeof handlerAuth>
|
|
50
61
|
|
|
51
|
-
export const headersSchema =
|
|
62
|
+
export const headersSchema = z.record(z.string())
|
|
52
63
|
|
|
53
|
-
export const handlerSuccess =
|
|
54
|
-
encoding:
|
|
55
|
-
body:
|
|
64
|
+
export const handlerSuccess = z.object({
|
|
65
|
+
encoding: z.string(),
|
|
66
|
+
body: z.any(),
|
|
56
67
|
headers: headersSchema.optional(),
|
|
57
68
|
})
|
|
58
|
-
export type HandlerSuccess =
|
|
69
|
+
export type HandlerSuccess = z.infer<typeof handlerSuccess>
|
|
59
70
|
|
|
60
|
-
export const handlerPipeThroughBuffer =
|
|
61
|
-
encoding:
|
|
62
|
-
buffer:
|
|
71
|
+
export const handlerPipeThroughBuffer = z.object({
|
|
72
|
+
encoding: z.string(),
|
|
73
|
+
buffer: z.instanceof(Buffer),
|
|
63
74
|
headers: headersSchema.optional(),
|
|
64
75
|
})
|
|
65
76
|
|
|
66
|
-
export type HandlerPipeThroughBuffer =
|
|
67
|
-
typeof handlerPipeThroughBuffer
|
|
68
|
-
>
|
|
77
|
+
export type HandlerPipeThroughBuffer = z.infer<typeof handlerPipeThroughBuffer>
|
|
69
78
|
|
|
70
|
-
export const handlerPipeThroughStream =
|
|
71
|
-
encoding:
|
|
72
|
-
stream:
|
|
79
|
+
export const handlerPipeThroughStream = z.object({
|
|
80
|
+
encoding: z.string(),
|
|
81
|
+
stream: z.instanceof(Readable),
|
|
73
82
|
headers: headersSchema.optional(),
|
|
74
83
|
})
|
|
75
84
|
|
|
76
|
-
export type HandlerPipeThroughStream =
|
|
77
|
-
typeof handlerPipeThroughStream
|
|
78
|
-
>
|
|
85
|
+
export type HandlerPipeThroughStream = z.infer<typeof handlerPipeThroughStream>
|
|
79
86
|
|
|
80
|
-
export const handlerPipeThrough =
|
|
87
|
+
export const handlerPipeThrough = z.union([
|
|
81
88
|
handlerPipeThroughBuffer,
|
|
82
89
|
handlerPipeThroughStream,
|
|
83
90
|
])
|
|
84
91
|
|
|
85
|
-
export type HandlerPipeThrough =
|
|
92
|
+
export type HandlerPipeThrough = z.infer<typeof handlerPipeThrough>
|
|
86
93
|
|
|
87
|
-
export const handlerError =
|
|
88
|
-
status:
|
|
89
|
-
error:
|
|
90
|
-
message:
|
|
94
|
+
export const handlerError = z.object({
|
|
95
|
+
status: z.number(),
|
|
96
|
+
error: z.string().optional(),
|
|
97
|
+
message: z.string().optional(),
|
|
91
98
|
})
|
|
92
|
-
export type HandlerError =
|
|
99
|
+
export type HandlerError = z.infer<typeof handlerError>
|
|
93
100
|
|
|
94
101
|
export type HandlerOutput = HandlerSuccess | HandlerPipeThrough | HandlerError
|
|
95
102
|
|
|
@@ -212,6 +219,8 @@ export type XRPCStreamHandlerConfig = {
|
|
|
212
219
|
handler: XRPCStreamHandler
|
|
213
220
|
}
|
|
214
221
|
|
|
222
|
+
export { ResponseType }
|
|
223
|
+
|
|
215
224
|
export class XRPCError extends Error {
|
|
216
225
|
constructor(
|
|
217
226
|
public type: ResponseType,
|
package/src/util.ts
CHANGED
|
@@ -1,28 +1,27 @@
|
|
|
1
1
|
import assert from 'node:assert'
|
|
2
|
-
import { Duplex, pipeline, Readable } from 'node:stream'
|
|
3
2
|
import { IncomingMessage } from 'node:http'
|
|
3
|
+
import { Duplex, Readable, pipeline } from 'node:stream'
|
|
4
4
|
import express from 'express'
|
|
5
5
|
import mime from 'mime-types'
|
|
6
|
+
import { MaxSizeChecker, createDecoders } from '@atproto/common'
|
|
6
7
|
import {
|
|
7
|
-
jsonToLex,
|
|
8
|
-
Lexicons,
|
|
9
8
|
LexXrpcProcedure,
|
|
10
9
|
LexXrpcQuery,
|
|
11
10
|
LexXrpcSubscription,
|
|
11
|
+
Lexicons,
|
|
12
|
+
jsonToLex,
|
|
12
13
|
} from '@atproto/lexicon'
|
|
13
|
-
import { createDecoders, MaxSizeChecker } from '@atproto/common'
|
|
14
14
|
import { ResponseType } from '@atproto/xrpc'
|
|
15
|
-
|
|
16
15
|
import {
|
|
17
|
-
UndecodedParams,
|
|
18
|
-
Params,
|
|
19
16
|
HandlerInput,
|
|
20
17
|
HandlerSuccess,
|
|
21
|
-
handlerSuccess,
|
|
22
|
-
InvalidRequestError,
|
|
23
18
|
InternalServerError,
|
|
24
|
-
|
|
19
|
+
InvalidRequestError,
|
|
20
|
+
Params,
|
|
25
21
|
RouteOpts,
|
|
22
|
+
UndecodedParams,
|
|
23
|
+
XRPCError,
|
|
24
|
+
handlerSuccess,
|
|
26
25
|
} from './types'
|
|
27
26
|
|
|
28
27
|
export function decodeQueryParams(
|
package/tests/_util.ts
CHANGED
package/tests/auth.test.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import * as http from 'node:http'
|
|
2
1
|
import { KeyObject, createPrivateKey } from 'node:crypto'
|
|
2
|
+
import * as http from 'node:http'
|
|
3
3
|
import { AddressInfo } from 'node:net'
|
|
4
4
|
import * as jose from 'jose'
|
|
5
5
|
import KeyEncoder from 'key-encoder'
|
|
@@ -7,13 +7,13 @@ import * as ui8 from 'uint8arrays'
|
|
|
7
7
|
import { MINUTE } from '@atproto/common'
|
|
8
8
|
import { Secp256k1Keypair } from '@atproto/crypto'
|
|
9
9
|
import { LexiconDoc } from '@atproto/lexicon'
|
|
10
|
-
import {
|
|
10
|
+
import { XRPCError, XrpcClient } from '@atproto/xrpc'
|
|
11
11
|
import * as xrpcServer from '../src'
|
|
12
12
|
import {
|
|
13
|
-
|
|
13
|
+
basicAuthHeaders,
|
|
14
14
|
closeServer,
|
|
15
15
|
createBasicAuth,
|
|
16
|
-
|
|
16
|
+
createServer,
|
|
17
17
|
} from './_util'
|
|
18
18
|
|
|
19
19
|
const LEXICONS: LexiconDoc[] = [
|
package/tests/bodies.test.ts
CHANGED
|
@@ -2,13 +2,13 @@ import * as http from 'node:http'
|
|
|
2
2
|
import { AddressInfo } from 'node:net'
|
|
3
3
|
import { Readable } from 'node:stream'
|
|
4
4
|
import { brotliCompressSync, deflateSync, gzipSync } from 'node:zlib'
|
|
5
|
-
import { LexiconDoc } from '@atproto/lexicon'
|
|
6
|
-
import { ResponseType, XrpcClient } from '@atproto/xrpc'
|
|
7
5
|
import { cidForCbor } from '@atproto/common'
|
|
8
6
|
import { randomBytes } from '@atproto/crypto'
|
|
9
|
-
import {
|
|
7
|
+
import { LexiconDoc } from '@atproto/lexicon'
|
|
8
|
+
import { ResponseType, XrpcClient } from '@atproto/xrpc'
|
|
10
9
|
import * as xrpcServer from '../src'
|
|
11
|
-
import logger from '../src/logger'
|
|
10
|
+
import { logger } from '../src/logger'
|
|
11
|
+
import { closeServer, createServer } from './_util'
|
|
12
12
|
|
|
13
13
|
const LEXICONS: LexiconDoc[] = [
|
|
14
14
|
{
|
package/tests/errors.test.ts
CHANGED
|
@@ -2,8 +2,8 @@ import * as http from 'node:http'
|
|
|
2
2
|
import { AddressInfo } from 'node:net'
|
|
3
3
|
import { LexiconDoc } from '@atproto/lexicon'
|
|
4
4
|
import { XRPCError, XRPCInvalidResponseError, XrpcClient } from '@atproto/xrpc'
|
|
5
|
-
import { createServer, closeServer } from './_util'
|
|
6
5
|
import * as xrpcServer from '../src'
|
|
6
|
+
import { closeServer, createServer } from './_util'
|
|
7
7
|
|
|
8
8
|
const LEXICONS: LexiconDoc[] = [
|
|
9
9
|
{
|
package/tests/frames.test.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as cborx from 'cbor-x'
|
|
2
2
|
import * as uint8arrays from 'uint8arrays'
|
|
3
|
-
import {
|
|
3
|
+
import { ErrorFrame, Frame, FrameType, MessageFrame } from '../src'
|
|
4
4
|
|
|
5
5
|
describe('Frames', () => {
|
|
6
6
|
it('creates and parses message frame.', async () => {
|
package/tests/ipld.test.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import * as http from 'node:http'
|
|
2
2
|
import { AddressInfo } from 'node:net'
|
|
3
|
+
import { CID } from 'multiformats/cid'
|
|
3
4
|
import { LexiconDoc } from '@atproto/lexicon'
|
|
4
5
|
import { XrpcClient } from '@atproto/xrpc'
|
|
5
|
-
import { CID } from 'multiformats/cid'
|
|
6
|
-
import { createServer, closeServer } from './_util'
|
|
7
6
|
import * as xrpcServer from '../src'
|
|
7
|
+
import { closeServer, createServer } from './_util'
|
|
8
8
|
|
|
9
9
|
const LEXICONS: LexiconDoc[] = [
|
|
10
10
|
{
|
package/tests/parameters.test.ts
CHANGED
|
@@ -2,8 +2,8 @@ import * as http from 'node:http'
|
|
|
2
2
|
import { AddressInfo } from 'node:net'
|
|
3
3
|
import { LexiconDoc } from '@atproto/lexicon'
|
|
4
4
|
import { XrpcClient } from '@atproto/xrpc'
|
|
5
|
-
import { createServer, closeServer } from './_util'
|
|
6
5
|
import * as xrpcServer from '../src'
|
|
6
|
+
import { closeServer, createServer } from './_util'
|
|
7
7
|
|
|
8
8
|
const LEXICONS: LexiconDoc[] = [
|
|
9
9
|
{
|
package/tests/procedures.test.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import * as http from 'node:http'
|
|
2
|
+
import { AddressInfo } from 'node:net'
|
|
2
3
|
import { Readable } from 'node:stream'
|
|
3
4
|
import { LexiconDoc } from '@atproto/lexicon'
|
|
4
5
|
import { XrpcClient } from '@atproto/xrpc'
|
|
5
|
-
import { createServer, closeServer } from './_util'
|
|
6
6
|
import * as xrpcServer from '../src'
|
|
7
|
-
import {
|
|
7
|
+
import { closeServer, createServer } from './_util'
|
|
8
8
|
|
|
9
9
|
const LEXICONS: LexiconDoc[] = [
|
|
10
10
|
{
|
package/tests/queries.test.ts
CHANGED
|
@@ -2,8 +2,8 @@ import * as http from 'node:http'
|
|
|
2
2
|
import { AddressInfo } from 'node:net'
|
|
3
3
|
import { LexiconDoc } from '@atproto/lexicon'
|
|
4
4
|
import { XrpcClient } from '@atproto/xrpc'
|
|
5
|
-
import { createServer, closeServer } from './_util'
|
|
6
5
|
import * as xrpcServer from '../src'
|
|
6
|
+
import { closeServer, createServer } from './_util'
|
|
7
7
|
|
|
8
8
|
const LEXICONS: LexiconDoc[] = [
|
|
9
9
|
{
|
package/tests/responses.test.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import * as http from 'node:http'
|
|
2
2
|
import { AddressInfo } from 'node:net'
|
|
3
|
+
import { byteIterableToStream } from '@atproto/common'
|
|
3
4
|
import { LexiconDoc } from '@atproto/lexicon'
|
|
4
5
|
import { XrpcClient } from '@atproto/xrpc'
|
|
5
|
-
import { byteIterableToStream } from '@atproto/common'
|
|
6
|
-
import { createServer, closeServer } from './_util'
|
|
7
6
|
import * as xrpcServer from '../src'
|
|
7
|
+
import { closeServer, createServer } from './_util'
|
|
8
8
|
|
|
9
9
|
const LEXICONS: LexiconDoc[] = [
|
|
10
10
|
{
|
package/tests/stream.test.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import { AddressInfo } from 'net'
|
|
1
|
+
import { once } from 'node:events'
|
|
2
|
+
import * as http from 'node:http'
|
|
3
|
+
import { AddressInfo } from 'node:net'
|
|
4
4
|
import { WebSocket } from 'ws'
|
|
5
5
|
import { XRPCError } from '@atproto/xrpc'
|
|
6
6
|
import {
|
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
import * as http from 'node:http'
|
|
2
2
|
import { AddressInfo } from 'node:net'
|
|
3
|
-
import { WebSocket, WebSocketServer, createWebSocketStream } from 'ws'
|
|
4
3
|
import getPort from 'get-port'
|
|
4
|
+
import { WebSocket, WebSocketServer, createWebSocketStream } from 'ws'
|
|
5
5
|
import { wait } from '@atproto/common'
|
|
6
6
|
import { LexiconDoc } from '@atproto/lexicon'
|
|
7
|
-
import {
|
|
7
|
+
import { ErrorFrame, Frame, MessageFrame, Subscription, byFrame } from '../src'
|
|
8
|
+
import * as xrpcServer from '../src'
|
|
8
9
|
import {
|
|
9
|
-
|
|
10
|
+
basicAuthHeaders,
|
|
10
11
|
closeServer,
|
|
11
12
|
createBasicAuth,
|
|
12
|
-
|
|
13
|
+
createServer,
|
|
13
14
|
} from './_util'
|
|
14
|
-
import * as xrpcServer from '../src'
|
|
15
15
|
|
|
16
16
|
const LEXICONS: LexiconDoc[] = [
|
|
17
17
|
{
|