@lodestar/api 1.20.0-dev.f6d3bce802 → 1.20.0-dev.ff253a7e4b

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.
Files changed (228) hide show
  1. package/README.md +1 -1
  2. package/lib/beacon/client/beacon.d.ts +4 -3
  3. package/lib/beacon/client/beacon.js +3 -36
  4. package/lib/beacon/client/beacon.js.map +1 -1
  5. package/lib/beacon/client/config.d.ts +4 -3
  6. package/lib/beacon/client/config.js +3 -6
  7. package/lib/beacon/client/config.js.map +1 -1
  8. package/lib/beacon/client/debug.d.ts +4 -3
  9. package/lib/beacon/client/debug.js +4 -45
  10. package/lib/beacon/client/debug.js.map +1 -1
  11. package/lib/beacon/client/events.d.ts +5 -2
  12. package/lib/beacon/client/events.js +32 -35
  13. package/lib/beacon/client/events.js.map +1 -1
  14. package/lib/beacon/client/index.d.ts +6 -3
  15. package/lib/beacon/client/index.js +2 -2
  16. package/lib/beacon/client/index.js.map +1 -1
  17. package/lib/beacon/client/lightclient.d.ts +4 -3
  18. package/lib/beacon/client/lightclient.js +4 -7
  19. package/lib/beacon/client/lightclient.js.map +1 -1
  20. package/lib/beacon/client/lodestar.d.ts +4 -3
  21. package/lib/beacon/client/lodestar.js +4 -7
  22. package/lib/beacon/client/lodestar.js.map +1 -1
  23. package/lib/beacon/client/node.d.ts +4 -3
  24. package/lib/beacon/client/node.js +4 -7
  25. package/lib/beacon/client/node.js.map +1 -1
  26. package/lib/beacon/client/proof.d.ts +5 -4
  27. package/lib/beacon/client/proof.js +5 -61
  28. package/lib/beacon/client/proof.js.map +1 -1
  29. package/lib/beacon/client/validator.d.ts +4 -3
  30. package/lib/beacon/client/validator.js +4 -7
  31. package/lib/beacon/client/validator.js.map +1 -1
  32. package/lib/beacon/index.d.ts +5 -5
  33. package/lib/beacon/index.js.map +1 -1
  34. package/lib/beacon/routes/beacon/block.d.ts +282 -164
  35. package/lib/beacon/routes/beacon/block.js +324 -116
  36. package/lib/beacon/routes/beacon/block.js.map +1 -1
  37. package/lib/beacon/routes/beacon/index.d.ts +6 -588
  38. package/lib/beacon/routes/beacon/index.js +16 -26
  39. package/lib/beacon/routes/beacon/index.js.map +1 -1
  40. package/lib/beacon/routes/beacon/pool.d.ts +155 -117
  41. package/lib/beacon/routes/beacon/pool.js +146 -42
  42. package/lib/beacon/routes/beacon/pool.js.map +1 -1
  43. package/lib/beacon/routes/beacon/rewards.d.ts +113 -101
  44. package/lib/beacon/routes/beacon/rewards.js +98 -63
  45. package/lib/beacon/routes/beacon/rewards.js.map +1 -1
  46. package/lib/beacon/routes/beacon/state.d.ts +172 -226
  47. package/lib/beacon/routes/beacon/state.js +204 -126
  48. package/lib/beacon/routes/beacon/state.js.map +1 -1
  49. package/lib/beacon/routes/config.d.ts +21 -36
  50. package/lib/beacon/routes/config.js +45 -24
  51. package/lib/beacon/routes/config.js.map +1 -1
  52. package/lib/beacon/routes/debug.d.ts +32 -95
  53. package/lib/beacon/routes/debug.js +87 -45
  54. package/lib/beacon/routes/debug.js.map +1 -1
  55. package/lib/beacon/routes/events.d.ts +20 -16
  56. package/lib/beacon/routes/events.js +39 -17
  57. package/lib/beacon/routes/events.js.map +1 -1
  58. package/lib/beacon/routes/index.d.ts +19 -19
  59. package/lib/beacon/routes/index.js +1 -0
  60. package/lib/beacon/routes/index.js.map +1 -1
  61. package/lib/beacon/routes/lightclient.d.ts +33 -55
  62. package/lib/beacon/routes/lightclient.js +107 -47
  63. package/lib/beacon/routes/lightclient.js.map +1 -1
  64. package/lib/beacon/routes/lodestar.d.ts +74 -148
  65. package/lib/beacon/routes/lodestar.js +126 -77
  66. package/lib/beacon/routes/lodestar.js.map +1 -1
  67. package/lib/beacon/routes/node.d.ts +48 -82
  68. package/lib/beacon/routes/node.js +90 -57
  69. package/lib/beacon/routes/node.js.map +1 -1
  70. package/lib/beacon/routes/proof.d.ts +25 -31
  71. package/lib/beacon/routes/proof.js +44 -24
  72. package/lib/beacon/routes/proof.js.map +1 -1
  73. package/lib/beacon/routes/validator.d.ts +547 -320
  74. package/lib/beacon/routes/validator.js +494 -213
  75. package/lib/beacon/routes/validator.js.map +1 -1
  76. package/lib/beacon/server/beacon.d.ts +3 -4
  77. package/lib/beacon/server/beacon.js +4 -57
  78. package/lib/beacon/server/beacon.js.map +1 -1
  79. package/lib/beacon/server/config.d.ts +3 -4
  80. package/lib/beacon/server/config.js +4 -5
  81. package/lib/beacon/server/config.js.map +1 -1
  82. package/lib/beacon/server/debug.d.ts +3 -4
  83. package/lib/beacon/server/debug.js +4 -54
  84. package/lib/beacon/server/debug.js.map +1 -1
  85. package/lib/beacon/server/events.d.ts +4 -4
  86. package/lib/beacon/server/events.js +17 -23
  87. package/lib/beacon/server/events.js.map +1 -1
  88. package/lib/beacon/server/index.d.ts +7 -8
  89. package/lib/beacon/server/index.js +13 -14
  90. package/lib/beacon/server/index.js.map +1 -1
  91. package/lib/beacon/server/lightclient.d.ts +3 -4
  92. package/lib/beacon/server/lightclient.js +4 -5
  93. package/lib/beacon/server/lightclient.js.map +1 -1
  94. package/lib/beacon/server/lodestar.d.ts +3 -4
  95. package/lib/beacon/server/lodestar.js +4 -5
  96. package/lib/beacon/server/lodestar.js.map +1 -1
  97. package/lib/beacon/server/node.d.ts +3 -4
  98. package/lib/beacon/server/node.js +4 -18
  99. package/lib/beacon/server/node.js.map +1 -1
  100. package/lib/beacon/server/proof.d.ts +3 -4
  101. package/lib/beacon/server/proof.js +4 -36
  102. package/lib/beacon/server/proof.js.map +1 -1
  103. package/lib/beacon/server/validator.d.ts +3 -4
  104. package/lib/beacon/server/validator.js +4 -21
  105. package/lib/beacon/server/validator.js.map +1 -1
  106. package/lib/builder/client.d.ts +4 -6
  107. package/lib/builder/client.js +3 -9
  108. package/lib/builder/client.js.map +1 -1
  109. package/lib/builder/index.d.ts +8 -5
  110. package/lib/builder/index.js +4 -1
  111. package/lib/builder/index.js.map +1 -1
  112. package/lib/builder/routes.d.ts +24 -38
  113. package/lib/builder/routes.js +77 -33
  114. package/lib/builder/routes.js.map +1 -1
  115. package/lib/builder/server/index.d.ts +6 -6
  116. package/lib/builder/server/index.js +7 -8
  117. package/lib/builder/server/index.js.map +1 -1
  118. package/lib/index.d.ts +8 -6
  119. package/lib/index.js +4 -4
  120. package/lib/index.js.map +1 -1
  121. package/lib/keymanager/client.d.ts +4 -3
  122. package/lib/keymanager/client.js +4 -7
  123. package/lib/keymanager/client.js.map +1 -1
  124. package/lib/keymanager/index.d.ts +5 -4
  125. package/lib/keymanager/index.js +1 -1
  126. package/lib/keymanager/index.js.map +1 -1
  127. package/lib/keymanager/routes.d.ts +155 -177
  128. package/lib/keymanager/routes.js +236 -123
  129. package/lib/keymanager/routes.js.map +1 -1
  130. package/lib/keymanager/server/index.d.ts +6 -6
  131. package/lib/keymanager/server/index.js +7 -8
  132. package/lib/keymanager/server/index.js.map +1 -1
  133. package/lib/server/index.d.ts +2 -0
  134. package/lib/server/index.js +3 -0
  135. package/lib/server/index.js.map +1 -0
  136. package/lib/utils/client/error.d.ts +6 -0
  137. package/lib/utils/client/error.js +8 -0
  138. package/lib/utils/client/error.js.map +1 -0
  139. package/lib/utils/client/httpClient.d.ts +35 -75
  140. package/lib/utils/client/httpClient.js +201 -153
  141. package/lib/utils/client/httpClient.js.map +1 -1
  142. package/lib/utils/client/index.d.ts +6 -2
  143. package/lib/utils/client/index.js +6 -2
  144. package/lib/utils/client/index.js.map +1 -1
  145. package/lib/utils/client/method.d.ts +11 -0
  146. package/lib/utils/client/method.js +27 -0
  147. package/lib/utils/client/method.js.map +1 -0
  148. package/lib/utils/client/request.d.ts +33 -0
  149. package/lib/utils/client/request.js +66 -0
  150. package/lib/utils/client/request.js.map +1 -0
  151. package/lib/utils/client/response.d.ts +35 -0
  152. package/lib/utils/client/response.js +166 -0
  153. package/lib/utils/client/response.js.map +1 -0
  154. package/lib/utils/codecs.d.ts +28 -0
  155. package/lib/utils/codecs.js +105 -0
  156. package/lib/utils/codecs.js.map +1 -0
  157. package/lib/utils/fork.d.ts +8 -0
  158. package/lib/utils/fork.js +35 -0
  159. package/lib/utils/fork.js.map +1 -0
  160. package/lib/utils/headers.d.ts +37 -0
  161. package/lib/utils/headers.js +135 -0
  162. package/lib/utils/headers.js.map +1 -0
  163. package/lib/utils/httpStatusCode.js.map +1 -0
  164. package/lib/utils/index.d.ts +1 -1
  165. package/lib/utils/index.js +1 -1
  166. package/lib/utils/index.js.map +1 -1
  167. package/lib/utils/metadata.d.ts +88 -0
  168. package/lib/utils/metadata.js +126 -0
  169. package/lib/utils/metadata.js.map +1 -0
  170. package/lib/utils/schema.d.ts +31 -17
  171. package/lib/utils/schema.js +18 -6
  172. package/lib/utils/schema.js.map +1 -1
  173. package/lib/utils/serdes.d.ts +3 -0
  174. package/lib/utils/serdes.js +13 -0
  175. package/lib/utils/serdes.js.map +1 -1
  176. package/lib/utils/server/{errors.d.ts → error.d.ts} +2 -2
  177. package/lib/utils/server/{errors.js → error.js} +1 -1
  178. package/lib/utils/server/error.js.map +1 -0
  179. package/lib/utils/server/handler.d.ts +11 -0
  180. package/lib/utils/server/handler.js +109 -0
  181. package/lib/utils/server/handler.js.map +1 -0
  182. package/lib/utils/server/index.d.ts +5 -4
  183. package/lib/utils/server/index.js +5 -4
  184. package/lib/utils/server/index.js.map +1 -1
  185. package/lib/utils/server/method.d.ts +35 -0
  186. package/lib/utils/server/method.js +2 -0
  187. package/lib/utils/server/method.js.map +1 -0
  188. package/lib/utils/server/parser.d.ts +3 -0
  189. package/lib/utils/server/parser.js +20 -0
  190. package/lib/utils/server/parser.js.map +1 -0
  191. package/lib/utils/server/route.d.ts +20 -0
  192. package/lib/utils/server/route.js +19 -0
  193. package/lib/utils/server/route.js.map +1 -0
  194. package/lib/utils/types.d.ts +116 -104
  195. package/lib/utils/types.js +2 -155
  196. package/lib/utils/types.js.map +1 -1
  197. package/lib/utils/urlFormat.d.ts +2 -2
  198. package/lib/utils/urlFormat.js +3 -3
  199. package/lib/utils/urlFormat.js.map +1 -1
  200. package/lib/utils/wireFormat.d.ts +8 -0
  201. package/lib/utils/wireFormat.js +23 -0
  202. package/lib/utils/wireFormat.js.map +1 -0
  203. package/package.json +9 -6
  204. package/lib/interfaces.d.ts +0 -44
  205. package/lib/interfaces.js +0 -2
  206. package/lib/interfaces.js.map +0 -1
  207. package/lib/utils/acceptHeader.d.ts +0 -9
  208. package/lib/utils/acceptHeader.js +0 -62
  209. package/lib/utils/acceptHeader.js.map +0 -1
  210. package/lib/utils/client/client.d.ts +0 -29
  211. package/lib/utils/client/client.js +0 -88
  212. package/lib/utils/client/client.js.map +0 -1
  213. package/lib/utils/client/httpStatusCode.js.map +0 -1
  214. package/lib/utils/routes.d.ts +0 -6
  215. package/lib/utils/routes.js +0 -31
  216. package/lib/utils/routes.js.map +0 -1
  217. package/lib/utils/server/errors.js.map +0 -1
  218. package/lib/utils/server/genericJsonServer.d.ts +0 -13
  219. package/lib/utils/server/genericJsonServer.js +0 -32
  220. package/lib/utils/server/genericJsonServer.js.map +0 -1
  221. package/lib/utils/server/registerRoute.d.ts +0 -3
  222. package/lib/utils/server/registerRoute.js +0 -13
  223. package/lib/utils/server/registerRoute.js.map +0 -1
  224. package/lib/utils/server/types.d.ts +0 -22
  225. package/lib/utils/server/types.js +0 -2
  226. package/lib/utils/server/types.js.map +0 -1
  227. /package/lib/utils/{client/httpStatusCode.d.ts → httpStatusCode.d.ts} +0 -0
  228. /package/lib/utils/{client/httpStatusCode.js → httpStatusCode.js} +0 -0
@@ -0,0 +1,109 @@
1
+ import { HttpHeader, MediaType, SUPPORTED_MEDIA_TYPES, parseAcceptHeader, parseContentTypeHeader } from "../headers.js";
2
+ import { isRequestWithoutBody, } from "../types.js";
3
+ import { WireFormat, fromWireFormat, getWireFormat } from "../wireFormat.js";
4
+ import { ApiError } from "./error.js";
5
+ export function createFastifyHandler(definition, method, _operationId) {
6
+ return async (req, resp) => {
7
+ // Determine response wire format first to inform application method
8
+ // about the preferable return type to avoid unnecessary serialization
9
+ let responseMediaType;
10
+ const acceptHeader = req.headers.accept;
11
+ if (definition.resp.isEmpty) {
12
+ // Ignore Accept header, the response will be sent without body
13
+ responseMediaType = null;
14
+ }
15
+ else if (acceptHeader === undefined || acceptHeader === "*/*") {
16
+ // Default to json to not force user to set header, e.g. when using curl
17
+ responseMediaType = MediaType.json;
18
+ }
19
+ else {
20
+ const { onlySupport } = definition.resp;
21
+ const supportedMediaTypes = onlySupport !== undefined ? [fromWireFormat(onlySupport)] : SUPPORTED_MEDIA_TYPES;
22
+ responseMediaType = parseAcceptHeader(acceptHeader, supportedMediaTypes);
23
+ if (responseMediaType === null) {
24
+ throw new ApiError(406, `Accepted media types not supported: ${acceptHeader}`);
25
+ }
26
+ }
27
+ const responseWireFormat = responseMediaType !== null ? getWireFormat(responseMediaType) : null;
28
+ let response;
29
+ try {
30
+ if (isRequestWithoutBody(definition)) {
31
+ response = await method(definition.req.parseReq(req), {
32
+ sszBytes: null,
33
+ returnBytes: responseWireFormat === WireFormat.ssz,
34
+ });
35
+ }
36
+ else {
37
+ const contentType = req.headers[HttpHeader.ContentType];
38
+ if (contentType === undefined) {
39
+ throw new ApiError(400, "Content-Type header is required");
40
+ }
41
+ const requestMediaType = parseContentTypeHeader(contentType);
42
+ if (requestMediaType === null) {
43
+ throw new ApiError(415, `Unsupported media type: ${contentType.split(";", 1)[0]}`);
44
+ }
45
+ const requestWireFormat = getWireFormat(requestMediaType);
46
+ const { onlySupport } = definition.req;
47
+ if (onlySupport !== undefined && onlySupport !== requestWireFormat) {
48
+ throw new ApiError(415, `Endpoint only supports ${onlySupport.toUpperCase()} requests`);
49
+ }
50
+ switch (requestWireFormat) {
51
+ case WireFormat.json:
52
+ response = await method(definition.req.parseReqJson(req), {
53
+ sszBytes: null,
54
+ returnBytes: responseWireFormat === WireFormat.ssz,
55
+ });
56
+ break;
57
+ case WireFormat.ssz:
58
+ response = await method(definition.req.parseReqSsz(req), {
59
+ sszBytes: req.body,
60
+ returnBytes: responseWireFormat === WireFormat.ssz,
61
+ });
62
+ break;
63
+ }
64
+ }
65
+ }
66
+ catch (e) {
67
+ if (e instanceof ApiError)
68
+ throw e;
69
+ // Errors related to parsing should return 400 status code
70
+ throw new ApiError(400, e.message);
71
+ }
72
+ if (response?.status !== undefined) {
73
+ resp.statusCode = response.status;
74
+ }
75
+ switch (responseWireFormat) {
76
+ case WireFormat.json: {
77
+ const metaHeaders = definition.resp.meta.toHeadersObject(response?.meta);
78
+ metaHeaders[HttpHeader.ContentType] = MediaType.json;
79
+ void resp.headers(metaHeaders);
80
+ const data = response?.data instanceof Uint8Array
81
+ ? definition.resp.data.toJson(definition.resp.data.deserialize(response.data, response.meta), response.meta)
82
+ : definition.resp.data.toJson(response?.data, response?.meta);
83
+ const metaJson = definition.resp.meta.toJson(response?.meta);
84
+ if (definition.resp.transform) {
85
+ return definition.resp.transform.toResponse(data, metaJson);
86
+ }
87
+ return {
88
+ data,
89
+ ...metaJson,
90
+ };
91
+ }
92
+ case WireFormat.ssz: {
93
+ const metaHeaders = definition.resp.meta.toHeadersObject(response?.meta);
94
+ metaHeaders[HttpHeader.ContentType] = MediaType.ssz;
95
+ void resp.headers(metaHeaders);
96
+ const data = response?.data instanceof Uint8Array
97
+ ? response.data
98
+ : definition.resp.data.serialize(response?.data, response?.meta);
99
+ // Fastify supports returning `Uint8Array` from handler and will efficiently
100
+ // convert it to a `Buffer` internally without copying the underlying `ArrayBuffer`
101
+ return data;
102
+ }
103
+ case null:
104
+ // Send response without body
105
+ return;
106
+ }
107
+ };
108
+ }
109
+ //# sourceMappingURL=handler.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"handler.js","sourceRoot":"","sources":["../../../src/utils/server/handler.ts"],"names":[],"mappings":"AAEA,OAAO,EAAC,UAAU,EAAE,SAAS,EAAE,qBAAqB,EAAE,iBAAiB,EAAE,sBAAsB,EAAC,MAAM,eAAe,CAAC;AACtH,OAAO,EASL,oBAAoB,GACrB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAC,U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M,CAAC,QAAQ,EAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,CAAC,CAAC;gBAClE,MAAM,QAAQ,GAAG,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAC;gBAC7D,IAAI,UAAU,CAAC,IAAI,CAAC,SAAS,EAAE,CAAC;oBAC9B,OAAO,UAAU,CAAC,IAAI,CAAC,SAAS,CAAC,UAAU,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC;gBAC9D,CAAC;gBACD,OAAO;oBACL,IAAI;oBACJ,GAAI,QAAmB;iBACxB,CAAC;YACJ,CAAC;YACD,KAAK,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC;gBACpB,MAAM,WAAW,GAAG,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,eAAe,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAC;gBACzE,WAAW,CAAC,UAAU,CAAC,WAAW,CAAC,GAAG,SAAS,CAAC,GAAG,CAAC;gBACpD,KAAK,IAAI,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC;gBAC/B,MAAM,IAAI,GACR,QAAQ,EAAE,IAAI,YAAY,UAAU;oBAClC,CAAC,CAAC,QAAQ,CAAC,IAAI;oBACf,CAAC,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,QAAQ,EAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,CAAC,CAAC;gBACrE,4EAA4E;gBAC5E,mFAAmF;gBACnF,OAAO,IAAI,CAAC;YACd,CAAC;YACD,KAAK,IAAI;gBACP,6BAA6B;gBAC7B,OAAO;QACX,CAAC;IACH,CAAC,CAAC;AACJ,CAAC"}
@@ -1,5 +1,6 @@
1
- export * from "./genericJsonServer.js";
2
- export * from "./registerRoute.js";
3
- export * from "./errors.js";
4
- export * from "./types.js";
1
+ export * from "./error.js";
2
+ export * from "./handler.js";
3
+ export * from "./method.js";
4
+ export * from "./parser.js";
5
+ export * from "./route.js";
5
6
  //# sourceMappingURL=index.d.ts.map
@@ -1,5 +1,6 @@
1
- export * from "./genericJsonServer.js";
2
- export * from "./registerRoute.js";
3
- export * from "./errors.js";
4
- export * from "./types.js";
1
+ export * from "./error.js";
2
+ export * from "./handler.js";
3
+ export * from "./method.js";
4
+ export * from "./parser.js";
5
+ export * from "./route.js";
5
6
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/utils/server/index.ts"],"names":[],"mappings":"AAAA,cAAc,wBAAwB,CAAC;AACvC,cAAc,oBAAoB,CAAC;AACnC,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/utils/server/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,cAAc,CAAC;AAC7B,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC"}
@@ -0,0 +1,35 @@
1
+ import { EmptyMeta, EmptyResponseData } from "../codecs.js";
2
+ import { HttpSuccessCodes } from "../httpStatusCode.js";
3
+ import { Endpoint, HasOnlyOptionalProps } from "../types.js";
4
+ type ApplicationResponseObject<E extends Endpoint> = {
5
+ /**
6
+ * Set non-200 success status code
7
+ */
8
+ status?: HttpSuccessCodes;
9
+ } & (E["return"] extends EmptyResponseData ? {
10
+ data?: never;
11
+ } : {
12
+ data: E["return"] | (E["return"] extends undefined ? undefined : Uint8Array);
13
+ }) & (E["meta"] extends EmptyMeta ? {
14
+ meta?: never;
15
+ } : {
16
+ meta: E["meta"];
17
+ });
18
+ export type ApplicationResponse<E extends Endpoint> = HasOnlyOptionalProps<ApplicationResponseObject<E>> extends true ? ApplicationResponseObject<E> | void : ApplicationResponseObject<E>;
19
+ export type ApiContext = {
20
+ /**
21
+ * Raw ssz bytes from request payload, only available for ssz requests
22
+ */
23
+ sszBytes?: Uint8Array | null;
24
+ /**
25
+ * Informs application method about preferable return type to avoid unnecessary serialization
26
+ */
27
+ returnBytes?: boolean;
28
+ };
29
+ type GenericOptions = Record<string, unknown>;
30
+ export type ApplicationMethod<E extends Endpoint> = (args: E["args"], context?: ApiContext, opts?: GenericOptions) => Promise<ApplicationResponse<E>>;
31
+ export type ApplicationMethods<Es extends Record<string, Endpoint>> = {
32
+ [K in keyof Es]: ApplicationMethod<Es[K]>;
33
+ };
34
+ export {};
35
+ //# sourceMappingURL=method.d.ts.map
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=method.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"method.js","sourceRoot":"","sources":["../../../src/utils/server/method.ts"],"names":[],"mappings":""}
@@ -0,0 +1,3 @@
1
+ import type * as fastify from "fastify";
2
+ export declare function addSszContentTypeParser(server: fastify.FastifyInstance): void;
3
+ //# sourceMappingURL=parser.d.ts.map
@@ -0,0 +1,20 @@
1
+ import { MediaType } from "../headers.js";
2
+ export function addSszContentTypeParser(server) {
3
+ // Cache body schema symbol, does not change per request
4
+ let bodySchemaSymbol;
5
+ server.addContentTypeParser(MediaType.ssz, { parseAs: "buffer" }, async (request, payload) => {
6
+ if (bodySchemaSymbol === undefined) {
7
+ // Get body schema symbol to be able to access validation function
8
+ // https://github.com/fastify/fastify/blob/af2ccb5ff681c1d0ac22eb7314c6fa803f73c873/lib/symbols.js#L25
9
+ bodySchemaSymbol = Object.getOwnPropertySymbols(request.context).find((s) => s.description === "body-schema");
10
+ }
11
+ // JSON schema validation will be applied to `Buffer` object, it is required to override validation function
12
+ // See https://github.com/fastify/help/issues/1012, it is not possible right now to define a schema per content type
13
+ request.context[bodySchemaSymbol] = () => true;
14
+ // We could just return the `Buffer` here which is a subclass of `Uint8Array` but downstream code does not require it
15
+ // and it's better to convert it here to avoid unexpected behavior such as `Buffer.prototype.slice` not copying memory
16
+ // See https://github.com/nodejs/node/issues/41588#issuecomment-1016269584
17
+ return new Uint8Array(payload.buffer, payload.byteOffset, payload.byteLength);
18
+ });
19
+ }
20
+ //# sourceMappingURL=parser.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parser.js","sourceRoot":"","sources":["../../../src/utils/server/parser.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,SAAS,EAAC,MAAM,eAAe,CAAC;AAExC,MAAM,UAAU,uBAAuB,CAAC,MAA+B;IACrE,wDAAwD;IACxD,IAAI,gBAAoC,CAAC;IAEzC,MAAM,CAAC,oBAAoB,CACzB,SAAS,CAAC,GAAG,EACb,EAAC,OAAO,EAAE,QAAQ,EAAC,EACnB,KAAK,EAAE,OAA+B,EAAE,OAAe,EAAE,EAAE;QACzD,IAAI,gBAAgB,KAAK,SAAS,EAAE,CAAC;YACnC,kEAAkE;YAClE,sGAAsG;YACtG,gBAAgB,GAAG,MAAM,CAAC,qBAAqB,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,WAAW,KAAK,aAAa,CAAC,CAAC;QAChH,CAAC;QACD,4GAA4G;QAC5G,oHAAoH;QACnH,OAAO,CAAC,OAA8C,CAAC,gBAA0B,CAAC,GAAG,GAAG,EAAE,CAAC,IAAI,CAAC;QAEjG,qHAAqH;QACrH,sHAAsH;QACtH,0EAA0E;QAC1E,OAAO,IAAI,UAAU,CAAC,OAAO,CAAC,MAAM,EAAE,OAAO,CAAC,UAAU,EAAE,OAAO,CAAC,UAAU,CAAC,CAAC;IAChF,CAAC,CACF,CAAC;AACJ,CAAC"}
@@ -0,0 +1,20 @@
1
+ import type * as fastify from "fastify";
2
+ import { Endpoint, RouteDefinition, RouteDefinitions } from "../types.js";
3
+ import { FastifyHandler } from "./handler.js";
4
+ import { ApplicationMethod, ApplicationMethods } from "./method.js";
5
+ export type FastifySchema = fastify.FastifySchema & {
6
+ operationId: string;
7
+ tags?: string[];
8
+ };
9
+ export type FastifyRoute<E extends Endpoint> = {
10
+ url: string;
11
+ method: fastify.HTTPMethods;
12
+ handler: FastifyHandler<E>;
13
+ schema: FastifySchema;
14
+ };
15
+ export type FastifyRoutes<Es extends Record<string, Endpoint>> = {
16
+ [K in keyof Es]: FastifyRoute<Es[K]>;
17
+ };
18
+ export declare function createFastifyRoute<E extends Endpoint>(definition: RouteDefinition<E>, method: ApplicationMethod<E>, operationId: string): FastifyRoute<E>;
19
+ export declare function createFastifyRoutes<Es extends Record<string, Endpoint>>(definitions: RouteDefinitions<Es>, methods: ApplicationMethods<Es>): FastifyRoutes<Es>;
20
+ //# sourceMappingURL=route.d.ts.map
@@ -0,0 +1,19 @@
1
+ import { mapValues } from "@lodestar/utils";
2
+ import { getFastifySchema } from "../schema.js";
3
+ import { toColonNotationPath } from "../urlFormat.js";
4
+ import { createFastifyHandler } from "./handler.js";
5
+ export function createFastifyRoute(definition, method, operationId) {
6
+ return {
7
+ url: toColonNotationPath(definition.url),
8
+ method: definition.method,
9
+ handler: createFastifyHandler(definition, method, operationId),
10
+ schema: {
11
+ ...getFastifySchema(definition.req.schema),
12
+ operationId,
13
+ },
14
+ };
15
+ }
16
+ export function createFastifyRoutes(definitions, methods) {
17
+ return mapValues(definitions, (definition, operationId) => createFastifyRoute(definition, methods?.[operationId]?.bind(methods), operationId));
18
+ }
19
+ //# sourceMappingURL=route.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"route.js","sourceRoot":"","sources":["../../../src/utils/server/route.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,SAAS,EAAC,MAAM,iBAAiB,CAAC;AAC1C,OAAO,EAAC,gBAAgB,EAAC,MAAM,cAAc,CAAC;AAE9C,OAAO,EAAC,mBAAmB,EAAC,MAAM,iBAAiB,CAAC;AACpD,OAAO,EAAiB,oBAAoB,EAAC,MAAM,cAAc,CAAC;AAgBlE,MAAM,UAAU,kBAAkB,CAChC,UAA8B,EAC9B,MAA4B,EAC5B,WAAmB;IAEnB,OAAO;QACL,GAAG,EAAE,mBAAmB,CAAC,UAAU,CAAC,GAAG,CAAC;QACxC,MAAM,EAAE,UAAU,CAAC,MAAM;QACzB,OAAO,EAAE,oBAAoB,CAAC,UAAU,EAAE,MAAM,EAAE,WAAW,CAAC;QAC9D,MAAM,EAAE;YACN,GAAG,gBAAgB,CAAC,UAAU,CAAC,GAAG,CAAC,MAAM,CAAC;YAC1C,WAAW;SACZ;KACF,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,mBAAmB,CACjC,WAAiC,EACjC,OAA+B;IAE/B,OAAO,SAAS,CAAC,WAAW,EAAE,CAAC,UAAU,EAAE,WAAW,EAAE,EAAE,CACxD,kBAAkB,CAAC,UAAU,EAAE,OAAO,EAAE,CAAC,WAAW,CAAC,EAAE,IAAI,CAAC,OAAO,CAAC,EAAE,WAAqB,CAAC,CAC7F,CAAC;AACJ,CAAC"}
@@ -1,117 +1,129 @@
1
- import { Type, ArrayType } from "@chainsafe/ssz";
2
- import { ForkName } from "@lodestar/params";
3
- import { ChainForkConfig } from "@lodestar/config";
4
- import { APIClientHandler, ApiClientResponseData, APIServerHandler, ClientApi } from "../interfaces.js";
5
- import { Schema, SchemaDefinition } from "./schema.js";
6
- export type RouteGroupDefinition<Api extends Record<string, APIServerHandler>, ReqTypes extends {
7
- [K in keyof Api]: ReqGeneric;
8
- }> = {
9
- routesData: RoutesData<Api>;
10
- getReqSerializers: (config: ChainForkConfig) => ReqSerializers<Api, ReqTypes>;
11
- getReturnTypes: (config: ChainForkConfig) => ReturnTypes<ClientApi<Api>>;
1
+ import { ExtraRequestInit } from "./client/request.js";
2
+ import { EmptyMeta } from "./codecs.js";
3
+ import { HeadersExtra } from "./headers.js";
4
+ import { SchemaDefinition } from "./schema.js";
5
+ import { WireFormat } from "./wireFormat.js";
6
+ export type HasOnlyOptionalProps<T> = {
7
+ [K in keyof T]-?: object extends Pick<T, K> ? never : K;
8
+ } extends {
9
+ [_ in keyof T]: never;
10
+ } ? true : false;
11
+ export type PathParams = Record<string, string | number>;
12
+ export type QueryParams = Record<string, string | number | boolean | (string | number)[]>;
13
+ export type HeaderParams = Record<string, string>;
14
+ export type RequestData<P extends PathParams = PathParams, Q extends QueryParams = QueryParams, H extends HeaderParams = HeaderParams> = {
15
+ params?: P;
16
+ query?: Q;
17
+ headers?: H;
12
18
  };
13
- export type RouteDef = {
14
- url: string;
15
- method: "GET" | "POST" | "DELETE";
16
- statusOk?: number;
19
+ export type JsonRequestData<B = unknown, P extends PathParams = PathParams, Q extends QueryParams = QueryParams, H extends HeaderParams = HeaderParams> = RequestData<P, Q, H> & {
20
+ body?: B;
17
21
  };
18
- export type ReqGeneric = {
19
- params?: Record<string, string | number>;
20
- query?: Record<string, string | number | boolean | (string | number)[]>;
21
- body?: any;
22
- headers?: Record<string, string[] | string | undefined>;
22
+ export type SszRequestData<P extends JsonRequestData> = Omit<P, "body"> & ("body" extends keyof P ? (P["body"] extends void ? {
23
+ body?: never;
24
+ } : {
25
+ body: Uint8Array;
26
+ }) : {
27
+ body?: never;
28
+ });
29
+ export type HttpMethod = "GET" | "POST" | "DELETE";
30
+ /**
31
+ * This type describes the general shape of a route
32
+ *
33
+ * This includes both http and application-level shape
34
+ * - The http method
35
+ * - Used to more strictly enforce the shape of the request
36
+ * - The application-level parameters
37
+ * - this enforces the shape of the input data passed by the client and to the route handler
38
+ * - The http request
39
+ * - this enforces the shape of the querystring, url params, request body
40
+ * - The application-level return data
41
+ * - this enforces the shape of the output data passed back to the client and returned by the route handler
42
+ * - The application-level return metadata
43
+ * - this enforces the shape of the returned metadata, used informationally and to help decode the return data
44
+ */
45
+ export type Endpoint<Method extends HttpMethod = HttpMethod, ArgsType = unknown, RequestType extends Method extends "GET" ? RequestData : JsonRequestData = JsonRequestData, ReturnType = unknown, Meta = unknown> = {
46
+ method: Method;
47
+ /** The parameters the client passes / server app code ingests */
48
+ args: ArgsType;
49
+ /** The parameters in the http request */
50
+ request: RequestType;
51
+ /** The return data */
52
+ return: ReturnType;
53
+ /** The return metadata */
54
+ meta: Meta;
23
55
  };
24
- export type ReqEmpty = ReqGeneric;
25
- export type Resolves<T extends (...args: any) => any> = Awaited<ReturnType<T>>;
26
- export type TypeJson<T> = {
27
- toJson(val: T): unknown;
28
- fromJson(json: unknown): T;
56
+ /** Encode / decode requests to & from function params, as well as schema definitions */
57
+ export type RequestWithoutBodyCodec<E extends Endpoint> = {
58
+ writeReq: (p: E["args"]) => E["request"];
59
+ parseReq: (r: E["request"]) => E["args"];
60
+ schema: SchemaDefinition<E["request"]>;
29
61
  };
30
- export type ReqSerializer<Fn extends (...args: any) => any, ReqType extends ReqGeneric> = {
31
- writeReq: (...args: Parameters<Fn>) => ReqType;
32
- parseReq: (arg: ReqType) => Parameters<Fn>;
33
- schema?: SchemaDefinition<ReqType>;
62
+ export type JsonRequestMethods<E extends Endpoint> = {
63
+ writeReqJson: (p: E["args"]) => E["request"];
64
+ parseReqJson: (r: E["request"]) => E["args"];
34
65
  };
35
- export type ReqSerializers<Api extends Record<string, APIServerHandler>, ReqTypes extends {
36
- [K in keyof Api]: ReqGeneric;
37
- }> = {
38
- [K in keyof Api]: ReqSerializer<Api[K], ReqTypes[K]>;
66
+ export type SszRequestMethods<E extends Endpoint> = {
67
+ writeReqSsz: (p: E["args"]) => SszRequestData<E["request"]>;
68
+ parseReqSsz: (r: SszRequestData<E["request"]>) => E["args"];
69
+ };
70
+ export type RequestWithBodyCodec<E extends Endpoint> = JsonRequestMethods<E> & SszRequestMethods<E> & {
71
+ schema: SchemaDefinition<E["request"]>;
72
+ /** Support ssz-only or json-only requests */
73
+ onlySupport?: WireFormat;
39
74
  };
40
- /** Curried definition to infer only one of the two generic types */
41
- export type ReqGenArg<Fn extends (...args: any) => any, ReqType extends ReqGeneric> = ReqSerializer<Fn, ReqType>;
42
- /** Shortcut for routes that have no params, query nor body */
43
- export declare const reqEmpty: ReqSerializer<() => void, ReqEmpty>;
44
- /** Shortcut for routes that have only body */
45
- export declare const reqOnlyBody: <T>(type: TypeJson<T>, bodySchema: Schema) => ReqGenArg<(arg: T) => Promise<void>, {
46
- body: unknown;
47
- }>;
48
- /** SSZ factory helper + typed. limit = 1e6 as a big enough random number */
49
- export declare function ArrayOf<T>(elementType: Type<T>): ArrayType<Type<T>, unknown, unknown>;
50
- /**
51
- * SSZ factory helper + typed to return responses of type
52
- * ```
53
- * data: T
54
- * ```
55
- */
56
- export declare function ContainerData<T>(dataType: TypeJson<T>): TypeJson<{
57
- data: T;
58
- }>;
59
- /**
60
- * SSZ factory helper + typed to return responses of type `{data: T; executionOptimistic: boolean}`
61
- */
62
- export declare function ContainerDataExecutionOptimistic<T>(dataType: TypeJson<T>): TypeJson<{
63
- data: T;
64
- executionOptimistic: boolean;
65
- }>;
66
- /**
67
- * SSZ factory helper + typed to return responses of type
68
- * ```
69
- * data: T
70
- * version: ForkName
71
- * ```
72
- */
73
- export declare function WithVersion<T>(getType: (fork: ForkName) => TypeJson<T>): TypeJson<{
74
- data: T;
75
- version: ForkName;
76
- }>;
77
- /**
78
- * SSZ factory helper to wrap an existing type with `{executionOptimistic: boolean}`
79
- */
80
- export declare function WithExecutionOptimistic<T extends {
81
- data: unknown;
82
- }>(type: TypeJson<T>): TypeJson<T & {
83
- executionOptimistic: boolean;
84
- }>;
85
75
  /**
86
- * SSZ factory helper to wrap an existing type with `{finalized: boolean}`
76
+ * Handles translation between `Endpoint["args"]` and `Endpoint["request"]`
87
77
  */
88
- export declare function WithFinalized<T extends {
89
- data: unknown;
90
- }>(type: TypeJson<T>): TypeJson<T & {
91
- finalized: boolean;
92
- }>;
78
+ export type RequestCodec<E extends Endpoint> = E["method"] extends "GET" ? RequestWithoutBodyCodec<E> : "body" extends keyof E["request"] ? RequestWithBodyCodec<E> : RequestWithoutBodyCodec<E>;
79
+ export declare function isRequestWithoutBody<E extends Endpoint>(definition: RouteDefinition<E>): definition is RouteDefinition<E> & {
80
+ req: RequestWithoutBodyCodec<E>;
81
+ };
82
+ export type ResponseDataCodec<T, M> = {
83
+ toJson: (data: T, meta: M) => unknown;
84
+ fromJson: (data: unknown, meta: M) => T;
85
+ serialize: (data: T, meta: M) => Uint8Array;
86
+ deserialize: (data: Uint8Array, meta: M) => T;
87
+ };
88
+ export type ResponseMetadataCodec<T> = {
89
+ toJson: (val: T) => unknown;
90
+ fromJson: (val: unknown) => T;
91
+ toHeadersObject: (val: T) => Record<string, string>;
92
+ fromHeaders: (headers: HeadersExtra) => T;
93
+ };
94
+ export type ResponseCodec<E extends Endpoint> = {
95
+ data: ResponseDataCodec<E["return"], E["meta"]>;
96
+ meta: ResponseMetadataCodec<E["meta"]>;
97
+ /** Occasionally, json responses require an extra transformation to separate the data from metadata */
98
+ transform?: {
99
+ toResponse: (data: unknown, meta: unknown) => unknown;
100
+ fromResponse: (resp: unknown) => {
101
+ data: E["return"];
102
+ } & (E["meta"] extends EmptyMeta ? {
103
+ meta?: never;
104
+ } : {
105
+ meta: E["meta"];
106
+ });
107
+ };
108
+ /** Support ssz-only or json-only responses */
109
+ onlySupport?: WireFormat;
110
+ /** Indicator used to handle empty responses */
111
+ isEmpty?: true;
112
+ };
93
113
  /**
94
- * SSZ factory helper to wrap an existing type with `{executionPayloadValue: Wei, consensusBlockValue: Wei}`
114
+ * Top-level definition of a route used by both the client and server
115
+ * - url and method
116
+ * - request and response codec
117
+ * - request json schema
95
118
  */
96
- export declare function WithBlockValues<T extends {
97
- data: unknown;
98
- }>(type: TypeJson<T>): TypeJson<T & {
99
- executionPayloadValue: bigint;
100
- consensusBlockValue: bigint;
101
- }>;
102
- type JsonCase = "snake" | "constant" | "camel" | "param" | "header" | "pascal" | "dot" | "notransform";
103
- /** Helper to only translate casing */
104
- export declare function jsonType<T extends Record<string, unknown> | Record<string, unknown>[] | unknown[]>(jsonCase: JsonCase): TypeJson<T>;
105
- /** Helper to not do any transformation with the type */
106
- export declare function sameType<T>(): TypeJson<T>;
107
- export type KeysOfNonVoidResolveValues<Api extends Record<string, APIClientHandler>> = {
108
- [K in keyof Api]: ApiClientResponseData<Resolves<Api[K]>> extends void ? never : K;
109
- }[keyof Api];
110
- export type ReturnTypes<Api extends Record<string, APIClientHandler>> = {
111
- [K in keyof Pick<Api, KeysOfNonVoidResolveValues<Api>>]: TypeJson<ApiClientResponseData<Resolves<Api[K]>>>;
119
+ export type RouteDefinition<E extends Endpoint> = {
120
+ url: string;
121
+ method: E["method"];
122
+ req: RequestCodec<E>;
123
+ resp: ResponseCodec<E>;
124
+ init?: ExtraRequestInit;
112
125
  };
113
- export type RoutesData<Api extends Record<string, APIServerHandler>> = {
114
- [K in keyof Api]: RouteDef;
126
+ export type RouteDefinitions<Es extends Record<string, Endpoint>> = {
127
+ [K in keyof Es]: RouteDefinition<Es[K]>;
115
128
  };
116
- export {};
117
129
  //# sourceMappingURL=types.d.ts.map