@toa.io/extensions.exposition 1.0.0-alpha.287 → 1.0.0-alpha.289

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 (277) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/components/identity.basic/tsconfig.tsbuildinfo +1 -1
  3. package/components/identity.clients/tsconfig.tsbuildinfo +1 -1
  4. package/components/identity.credentials/tsconfig.tsbuildinfo +1 -1
  5. package/components/identity.federation/tsconfig.tsbuildinfo +1 -1
  6. package/components/identity.grants/tsconfig.tsbuildinfo +1 -1
  7. package/components/identity.keys/tsconfig.tsbuildinfo +1 -1
  8. package/components/identity.otp/tsconfig.tsbuildinfo +1 -1
  9. package/components/identity.passkeys/manifest.toa.yaml +2 -1
  10. package/components/identity.passkeys/source/errors.test.ts +34 -0
  11. package/components/identity.passkeys/source/webauthn.test.ts +26 -0
  12. package/components/identity.passkeys/tsconfig.tsbuildinfo +1 -1
  13. package/components/identity.passkeys/types/toa.d.ts +1 -1
  14. package/components/identity.roles/tsconfig.tsbuildinfo +1 -1
  15. package/components/identity.tokens/tsconfig.tsbuildinfo +1 -1
  16. package/documentation/access.md +23 -4
  17. package/documentation/discovery.md +81 -0
  18. package/documentation/help.md +122 -0
  19. package/documentation/introspection.md +42 -9
  20. package/documentation/mcp.md +13 -16
  21. package/documentation/octets.md +20 -0
  22. package/features/cors.feature +31 -1
  23. package/features/dev.feature +2 -0
  24. package/features/discovery.feature +213 -0
  25. package/features/discovery.ui.feature +213 -0
  26. package/features/help.feature +512 -0
  27. package/features/introspection.feature +94 -8
  28. package/features/map.feature +1 -0
  29. package/features/mcp.feature +74 -12
  30. package/features/methods.feature +2 -1
  31. package/features/oauth.grants.feature +3 -1
  32. package/features/octets.download.feature +1 -0
  33. package/features/octets.feature +59 -0
  34. package/features/octets.meta.feature +1 -0
  35. package/features/site/_app/immutable/asset.js +1 -0
  36. package/features/site/favicon.ico +0 -0
  37. package/features/site/index.html +10 -0
  38. package/features/steps/Components.ts +4 -2
  39. package/features/steps/Database.ts +7 -1
  40. package/features/steps/Gateway.ts +23 -21
  41. package/features/steps/Parameters.ts +37 -16
  42. package/features/steps/Realtime.ts +3 -3
  43. package/package.json +7 -5
  44. package/readme.md +3 -0
  45. package/source/Directive.ts +4 -11
  46. package/source/Discovery/Explorer.ts +34 -0
  47. package/source/Discovery/Site.test.ts +147 -0
  48. package/source/Discovery/Site.ts +202 -0
  49. package/source/Discovery/index.ts +3 -0
  50. package/source/Discovery/tree.test.ts +175 -0
  51. package/source/Discovery/tree.ts +77 -0
  52. package/source/Discovery/trunk.ts +33 -0
  53. package/source/Endpoint.ts +4 -0
  54. package/source/Factory.ts +3 -86
  55. package/source/Gateway.ts +34 -4
  56. package/source/HTTP/Context.ts +7 -0
  57. package/source/HTTP/Probe.ts +0 -11
  58. package/source/HTTP/Server.ts +2 -3
  59. package/source/HTTP/messages.ts +4 -2
  60. package/source/Introspection.ts +58 -7
  61. package/source/MCP/schema.ts +16 -5
  62. package/source/MCP/tools.ts +14 -32
  63. package/source/Mapping.ts +5 -0
  64. package/source/Query.ts +17 -2
  65. package/source/RTD/Directives.ts +10 -0
  66. package/source/RTD/Endpoint.ts +8 -1
  67. package/source/RTD/Node.ts +20 -6
  68. package/source/RTD/Tree.ts +19 -9
  69. package/source/RTD/factory.ts +37 -1
  70. package/source/RTD/segment.ts +21 -0
  71. package/source/const.ts +26 -0
  72. package/source/deployment.ts +2 -2
  73. package/source/directives/auth/Anonymous.test.ts +31 -5
  74. package/source/directives/auth/Anonymous.ts +15 -3
  75. package/source/directives/auth/Anyone.ts +6 -0
  76. package/source/directives/auth/Assert.ts +5 -0
  77. package/source/directives/auth/Delegate.ts +5 -2
  78. package/source/directives/auth/Federation.ts +11 -0
  79. package/source/directives/auth/Id.ts +15 -0
  80. package/source/directives/auth/Input.ts +5 -0
  81. package/source/directives/auth/Role.ts +17 -0
  82. package/source/directives/cors/CORS.test.ts +75 -0
  83. package/source/directives/cors/CORS.ts +10 -3
  84. package/source/directives/help/Family.ts +122 -0
  85. package/source/directives/help/Help.test.ts +227 -0
  86. package/source/directives/help/Help.ts +47 -0
  87. package/source/directives/help/Parameters.ts +47 -0
  88. package/source/directives/help/described.ts +57 -0
  89. package/source/directives/help/index.ts +45 -0
  90. package/source/directives/index.ts +8 -1
  91. package/source/directives/map/Headers.ts +6 -6
  92. package/source/directives/mcp/MCP.ts +12 -21
  93. package/source/directives/mcp/Tool.test.ts +22 -68
  94. package/source/directives/mcp/Tool.ts +14 -41
  95. package/source/directives/octets/Directive.ts +7 -0
  96. package/source/directives/octets/Octets.ts +8 -0
  97. package/source/directives/octets/Put.ts +10 -0
  98. package/source/manifest.ts +1 -1
  99. package/source/root.ts +2 -2
  100. package/source/service.ts +94 -0
  101. package/source/shortcuts.ts +14 -0
  102. package/transpiled/Directive.d.ts +1 -1
  103. package/transpiled/Directive.js +3 -11
  104. package/transpiled/Directive.js.map +1 -1
  105. package/transpiled/Discovery/Explorer.d.ts +11 -0
  106. package/transpiled/Discovery/Explorer.js +29 -0
  107. package/transpiled/Discovery/Explorer.js.map +1 -0
  108. package/transpiled/Discovery/Site.d.ts +43 -0
  109. package/transpiled/Discovery/Site.js +171 -0
  110. package/transpiled/Discovery/Site.js.map +1 -0
  111. package/transpiled/Discovery/index.d.ts +3 -0
  112. package/transpiled/Discovery/index.js +4 -0
  113. package/transpiled/Discovery/index.js.map +1 -0
  114. package/transpiled/Discovery/tree.d.ts +26 -0
  115. package/transpiled/Discovery/tree.js +50 -0
  116. package/transpiled/Discovery/tree.js.map +1 -0
  117. package/transpiled/Discovery/trunk.d.ts +13 -0
  118. package/transpiled/Discovery/trunk.js +29 -0
  119. package/transpiled/Discovery/trunk.js.map +1 -0
  120. package/transpiled/Endpoint.d.ts +2 -1
  121. package/transpiled/Endpoint.js +3 -0
  122. package/transpiled/Endpoint.js.map +1 -1
  123. package/transpiled/Factory.js +3 -61
  124. package/transpiled/Factory.js.map +1 -1
  125. package/transpiled/Gateway.d.ts +4 -0
  126. package/transpiled/Gateway.js +29 -4
  127. package/transpiled/Gateway.js.map +1 -1
  128. package/transpiled/HTTP/Context.d.ts +6 -0
  129. package/transpiled/HTTP/Context.js +6 -0
  130. package/transpiled/HTTP/Context.js.map +1 -1
  131. package/transpiled/HTTP/Probe.d.ts +0 -10
  132. package/transpiled/HTTP/Probe.js +0 -10
  133. package/transpiled/HTTP/Probe.js.map +1 -1
  134. package/transpiled/HTTP/Server.d.ts +0 -1
  135. package/transpiled/HTTP/Server.js +2 -2
  136. package/transpiled/HTTP/Server.js.map +1 -1
  137. package/transpiled/HTTP/messages.js +4 -2
  138. package/transpiled/HTTP/messages.js.map +1 -1
  139. package/transpiled/Introspection.d.ts +38 -6
  140. package/transpiled/Introspection.js +18 -1
  141. package/transpiled/Introspection.js.map +1 -1
  142. package/transpiled/MCP/schema.d.ts +2 -2
  143. package/transpiled/MCP/schema.js +11 -5
  144. package/transpiled/MCP/schema.js.map +1 -1
  145. package/transpiled/MCP/tools.d.ts +1 -1
  146. package/transpiled/MCP/tools.js +12 -28
  147. package/transpiled/MCP/tools.js.map +1 -1
  148. package/transpiled/Mapping.d.ts +2 -0
  149. package/transpiled/Mapping.js +4 -0
  150. package/transpiled/Mapping.js.map +1 -1
  151. package/transpiled/Query.d.ts +12 -0
  152. package/transpiled/Query.js +16 -2
  153. package/transpiled/Query.js.map +1 -1
  154. package/transpiled/RTD/Directives.d.ts +8 -0
  155. package/transpiled/RTD/Endpoint.d.ts +7 -1
  156. package/transpiled/RTD/Node.d.ts +11 -2
  157. package/transpiled/RTD/Node.js +12 -2
  158. package/transpiled/RTD/Node.js.map +1 -1
  159. package/transpiled/RTD/Tree.js +15 -8
  160. package/transpiled/RTD/Tree.js.map +1 -1
  161. package/transpiled/RTD/factory.js +27 -1
  162. package/transpiled/RTD/factory.js.map +1 -1
  163. package/transpiled/RTD/segment.d.ts +7 -0
  164. package/transpiled/RTD/segment.js +17 -0
  165. package/transpiled/RTD/segment.js.map +1 -1
  166. package/transpiled/const.d.ts +21 -0
  167. package/transpiled/const.js +21 -0
  168. package/transpiled/const.js.map +1 -1
  169. package/transpiled/deployment.js +2 -2
  170. package/transpiled/deployment.js.map +1 -1
  171. package/transpiled/directives/auth/Anonymous.d.ts +11 -2
  172. package/transpiled/directives/auth/Anonymous.js +13 -3
  173. package/transpiled/directives/auth/Anonymous.js.map +1 -1
  174. package/transpiled/directives/auth/Anyone.d.ts +3 -0
  175. package/transpiled/directives/auth/Anyone.js +4 -0
  176. package/transpiled/directives/auth/Anyone.js.map +1 -1
  177. package/transpiled/directives/auth/Assert.d.ts +2 -0
  178. package/transpiled/directives/auth/Assert.js +4 -0
  179. package/transpiled/directives/auth/Assert.js.map +1 -1
  180. package/transpiled/directives/auth/Delegate.d.ts +4 -1
  181. package/transpiled/directives/auth/Delegate.js +5 -2
  182. package/transpiled/directives/auth/Delegate.js.map +1 -1
  183. package/transpiled/directives/auth/Federation.d.ts +5 -0
  184. package/transpiled/directives/auth/Federation.js +8 -0
  185. package/transpiled/directives/auth/Federation.js.map +1 -1
  186. package/transpiled/directives/auth/Id.d.ts +9 -0
  187. package/transpiled/directives/auth/Id.js +12 -0
  188. package/transpiled/directives/auth/Id.js.map +1 -1
  189. package/transpiled/directives/auth/Input.d.ts +2 -0
  190. package/transpiled/directives/auth/Input.js +4 -0
  191. package/transpiled/directives/auth/Input.js.map +1 -1
  192. package/transpiled/directives/auth/Role.d.ts +7 -0
  193. package/transpiled/directives/auth/Role.js +11 -0
  194. package/transpiled/directives/auth/Role.js.map +1 -1
  195. package/transpiled/directives/cors/CORS.js +9 -2
  196. package/transpiled/directives/cors/CORS.js.map +1 -1
  197. package/transpiled/directives/help/Family.d.ts +28 -0
  198. package/transpiled/directives/help/Family.js +83 -0
  199. package/transpiled/directives/help/Family.js.map +1 -0
  200. package/transpiled/directives/help/Help.d.ts +22 -0
  201. package/transpiled/directives/help/Help.js +36 -0
  202. package/transpiled/directives/help/Help.js.map +1 -0
  203. package/transpiled/directives/help/Parameters.d.ts +18 -0
  204. package/transpiled/directives/help/Parameters.js +34 -0
  205. package/transpiled/directives/help/Parameters.js.map +1 -0
  206. package/transpiled/directives/help/described.d.ts +16 -0
  207. package/transpiled/directives/help/described.js +23 -0
  208. package/transpiled/directives/help/described.js.map +1 -0
  209. package/transpiled/directives/help/index.d.ts +22 -0
  210. package/transpiled/directives/help/index.js +34 -0
  211. package/transpiled/directives/help/index.js.map +1 -0
  212. package/transpiled/directives/index.d.ts +4 -0
  213. package/transpiled/directives/index.js +8 -1
  214. package/transpiled/directives/index.js.map +1 -1
  215. package/transpiled/directives/map/Headers.d.ts +5 -0
  216. package/transpiled/directives/map/Headers.js +7 -5
  217. package/transpiled/directives/map/Headers.js.map +1 -1
  218. package/transpiled/directives/mcp/MCP.d.ts +8 -5
  219. package/transpiled/directives/mcp/MCP.js +9 -13
  220. package/transpiled/directives/mcp/MCP.js.map +1 -1
  221. package/transpiled/directives/mcp/Tool.d.ts +6 -12
  222. package/transpiled/directives/mcp/Tool.js +10 -23
  223. package/transpiled/directives/mcp/Tool.js.map +1 -1
  224. package/transpiled/directives/octets/Directive.d.ts +6 -0
  225. package/transpiled/directives/octets/Directive.js.map +1 -1
  226. package/transpiled/directives/octets/Octets.d.ts +3 -0
  227. package/transpiled/directives/octets/Octets.js +6 -0
  228. package/transpiled/directives/octets/Octets.js.map +1 -1
  229. package/transpiled/directives/octets/Put.d.ts +3 -0
  230. package/transpiled/directives/octets/Put.js +8 -0
  231. package/transpiled/directives/octets/Put.js.map +1 -1
  232. package/transpiled/manifest.js +1 -1
  233. package/transpiled/root.js +2 -2
  234. package/transpiled/root.js.map +1 -1
  235. package/transpiled/service.d.ts +7 -0
  236. package/transpiled/service.js +74 -0
  237. package/transpiled/service.js.map +1 -0
  238. package/transpiled/shortcuts.d.ts +3 -0
  239. package/transpiled/shortcuts.js +13 -0
  240. package/transpiled/shortcuts.js.map +1 -0
  241. package/tsconfig.tsbuildinfo +1 -1
  242. package/ui/dist/_app/env.js +1 -0
  243. package/ui/dist/_app/immutable/assets/0.Bl7vLmIK.css +2 -0
  244. package/ui/dist/_app/immutable/assets/2.BC0-Jhqu.css +1 -0
  245. package/ui/dist/_app/immutable/assets/inter-cyrillic-ext-wght-normal.BOeWTOD4.woff2 +0 -0
  246. package/ui/dist/_app/immutable/assets/inter-cyrillic-wght-normal.DqGufNeO.woff2 +0 -0
  247. package/ui/dist/_app/immutable/assets/inter-greek-ext-wght-normal.DlzME5K_.woff2 +0 -0
  248. package/ui/dist/_app/immutable/assets/inter-greek-wght-normal.CkhJZR-_.woff2 +0 -0
  249. package/ui/dist/_app/immutable/assets/inter-latin-ext-wght-normal.DO1Apj_S.woff2 +0 -0
  250. package/ui/dist/_app/immutable/assets/inter-latin-wght-normal.Dx4kXJAl.woff2 +0 -0
  251. package/ui/dist/_app/immutable/assets/inter-vietnamese-wght-normal.CBcvBZtf.woff2 +0 -0
  252. package/ui/dist/_app/immutable/chunks/Bjy-W4x2.js +81 -0
  253. package/ui/dist/_app/immutable/chunks/CDMRPWCx.js +6 -0
  254. package/ui/dist/_app/immutable/chunks/DJTKIUs8.js +1 -0
  255. package/ui/dist/_app/immutable/chunks/DjIYL2zC.js +3 -0
  256. package/ui/dist/_app/immutable/chunks/xihTtKlq.js +1 -0
  257. package/ui/dist/_app/immutable/chunks/z-DVUbQC.js +1 -0
  258. package/ui/dist/_app/immutable/entry/app.5S2Jtv46.js +2 -0
  259. package/ui/dist/_app/immutable/entry/start.Bqi6IBmH.js +1 -0
  260. package/ui/dist/_app/immutable/nodes/0.D4LwNilH.js +1 -0
  261. package/ui/dist/_app/immutable/nodes/1.CSRya9So.js +1 -0
  262. package/ui/dist/_app/immutable/nodes/2.BtiGmR6I.js +14 -0
  263. package/ui/dist/_app/version.json +1 -0
  264. package/ui/dist/apple-touch-icon.png +0 -0
  265. package/ui/dist/favicon-96x96.png +0 -0
  266. package/ui/dist/favicon.ico +0 -0
  267. package/ui/dist/index.html +66 -0
  268. package/components/identity.bans/migrations/0001-system-properties.yaml +0 -14
  269. package/components/identity.basic/migrations/0001-system-properties.yaml +0 -14
  270. package/components/identity.clients/migrations/0001-system-properties.yaml +0 -14
  271. package/components/identity.federation/migrations/0001-system-properties.yaml +0 -14
  272. package/components/identity.grants/migrations/0001-system-properties.yaml +0 -14
  273. package/components/identity.keys/migrations/0001-system-properties.yaml +0 -14
  274. package/components/identity.otp/migrations/0001-system-properties.yaml +0 -14
  275. package/components/identity.passkeys/migrations/0001-system-properties.yaml +0 -14
  276. package/components/identity.roles/migrations/0001-system-properties.yaml +0 -14
  277. package/components/identity.tokens/migrations/0001-system-properties.yaml +0 -14
@@ -4,7 +4,7 @@ import tsflow from 'cucumber-tsflow'
4
4
 
5
5
  import { Factory } from '@toa.io/extensions.realtime'
6
6
  import * as boot from '@toa.io/boot'
7
- import { match, timeout } from '@toa.io/generic'
7
+ import { environment, match, timeout } from '@toa.io/generic'
8
8
  import { load as parse } from 'js-yaml'
9
9
  import { Agent } from '@toa.io/agent'
10
10
  import { Parameters } from './Parameters.js'
@@ -38,7 +38,7 @@ export class Realtime {
38
38
  for (const [event, property] of Object.entries(annotation))
39
39
  routes.push({ event, properties: [property] })
40
40
 
41
- process.env.TOA_REALTIME = JSON.stringify(routes)
41
+ environment.set('TOA_REALTIME', JSON.stringify(routes))
42
42
 
43
43
  const factory = new Factory(boot.host())
44
44
 
@@ -93,7 +93,7 @@ export class Realtime {
93
93
  await this.instance.disconnect()
94
94
 
95
95
  this.instance = null
96
- process.env.TOA_REALTIME = undefined
96
+ environment.delete('TOA_REALTIME')
97
97
  }
98
98
 
99
99
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@toa.io/extensions.exposition",
3
- "version": "1.0.0-alpha.287",
3
+ "version": "1.0.0-alpha.289",
4
4
  "type": "module",
5
5
  "description": "Toa Exposition",
6
6
  "author": "temich <tema.gurtovoy@gmail.com>",
@@ -20,9 +20,9 @@
20
20
  "dependencies": {
21
21
  "@node-rs/bcrypt": "1.10.8",
22
22
  "@simplewebauthn/server": "14.0.0",
23
- "@toa.io/core": "1.0.0-alpha.287",
24
- "@toa.io/generic": "1.0.0-alpha.286",
25
- "@toa.io/schemas": "1.0.0-alpha.287",
23
+ "@toa.io/core": "1.0.0-alpha.289",
24
+ "@toa.io/generic": "1.0.0-alpha.289",
25
+ "@toa.io/schemas": "1.0.0-alpha.289",
26
26
  "content-type": "3.0.0",
27
27
  "jose": "6.2.10",
28
28
  "js-yaml": "5.4.1",
@@ -48,6 +48,8 @@
48
48
  "transpile:otp": "tsc -p ./components/identity.otp",
49
49
  "transpile:clients": "tsc -p ./components/identity.clients",
50
50
  "transpile:grants": "tsc -p ./components/identity.grants",
51
+ "build:ui": "npm --prefix ui ci && npm --prefix ui run build",
52
+ "prepack": "npm run build:ui",
51
53
  "features": "TSX_TSCONFIG_PATH=features/steps/tsconfig.json NODE_OPTIONS=--import=tsx cucumber-js",
52
54
  "features:h2c": "TOA_EXPOSITION_PROTOCOL=h2c TOA_AGENT_PROTOCOL=h2c TSX_TSCONFIG_PATH=features/steps/tsconfig.json NODE_OPTIONS=--import=tsx cucumber-js",
53
55
  "features:security": "TSX_TSCONFIG_PATH=features/steps/tsconfig.json NODE_OPTIONS=--import=tsx cucumber-js --tags @security",
@@ -58,5 +60,5 @@
58
60
  "@types/cors": "2.8.19",
59
61
  "@types/negotiator": "0.6.5"
60
62
  },
61
- "gitHead": "5c7944a70c16065ab6372072385cc1f02f5db77c"
63
+ "gitHead": "4fcb482beac5896305d8084cdbc1d5fcf372d108"
62
64
  }
package/readme.md CHANGED
@@ -211,6 +211,9 @@ See also [telemetry ready probe](../telemetry/readme.md#ready-probe) and
211
211
  - [Resource Tree Definition](documentation/tree.md)
212
212
  - [Identity authentication](documentation/identity.md)
213
213
  - [Access authorization](documentation/access.md)
214
+ - [Resource introspection](documentation/introspection.md)
215
+ - [Help](documentation/help.md)
216
+ - [Resource discovery](documentation/discovery.md)
214
217
  - [OAuth authorization server](documentation/oauth.md)
215
218
  - [Consent](documentation/consent.md)
216
219
  - [BLOBs](documentation/octets.md)
@@ -206,6 +206,10 @@ export class DirectivesFactory implements RTD.DirectiveFactory {
206
206
  return directives
207
207
  }
208
208
 
209
+ public inheritable(directive: RTD.syntax.Directive): boolean {
210
+ return this.families[directive.family]?.inherited !== false
211
+ }
212
+
209
213
  public dispose(): void {
210
214
  for (const directives of this.instances) directives.dispose()
211
215
  }
@@ -242,14 +246,3 @@ interface Stage {
242
246
  depart: SpanOptions
243
247
  }
244
248
 
245
- export const shortcuts: RTD.syntax.Shortcuts = new Map([
246
- ['anonymous', 'auth:anonymous'],
247
- ['anyone', 'auth:anyone'],
248
- ['id', 'auth:id'],
249
- ['role', 'auth:role'],
250
- ['rule', 'auth:rule'],
251
- ['incept', 'auth:incept'],
252
- ['input', 'io:input'],
253
- ['output', 'io:output'],
254
- ['languages', 'map:languages']
255
- ])
@@ -0,0 +1,34 @@
1
+ import * as http from '../HTTP/index.js'
2
+ import { BRANCH_TTL } from '../const.js'
3
+ import { describe } from './tree.js'
4
+ import type { Tree } from '../RTD/index.js'
5
+
6
+ /**
7
+ * What an application serves, for every route at once. Each entry is what `OPTIONS` on that
8
+ * path answers, so a key taken from here is a request that can be made.
9
+ */
10
+ export class Explorer {
11
+ private readonly tree: Tree
12
+
13
+ public constructor(tree: Tree) {
14
+ this.tree = tree
15
+ }
16
+
17
+ public async process(context: http.Context): Promise<http.OutgoingMessage> {
18
+ if (context.request.method !== 'OPTIONS')
19
+ throw new http.MethodNotAllowed(new Headers({ allow: ALLOW }))
20
+
21
+ return {
22
+ body: await describe(this.tree, context),
23
+ headers: new Headers({
24
+ allow: ALLOW,
25
+ // it stands until a branch expires, and it is filtered by who asked
26
+ 'cache-control': `private, max-age=${BRANCH_TTL / 1000}`,
27
+ vary: 'authorization'
28
+ })
29
+ }
30
+ }
31
+ }
32
+
33
+ /** The verbs of this path, which are not the verbs of the tree it answers with. */
34
+ const ALLOW = 'GET, HEAD, OPTIONS'
@@ -0,0 +1,147 @@
1
+ import { describe, it } from 'node:test'
2
+ import assert from 'node:assert/strict'
3
+ import { Readable } from 'node:stream'
4
+ import * as path from 'node:path'
5
+ import * as fs from 'node:fs'
6
+ import * as os from 'node:os'
7
+
8
+ import { Site } from './Site.js'
9
+ import * as http from '../HTTP/index.js'
10
+ import type { Input, Output } from '../io.js'
11
+
12
+ const root = fs.mkdtempSync(path.join(os.tmpdir(), 'discovery-'))
13
+
14
+ fs.mkdirSync(path.join(root, '_app', 'immutable'), { recursive: true })
15
+ fs.writeFileSync(path.join(root, 'index.html'), 'the page\n')
16
+ fs.writeFileSync(path.join(root, 'favicon.ico'), 'icon')
17
+ fs.writeFileSync(path.join(root, '_app', 'immutable', 'asset.js'), 'export {}\n')
18
+
19
+ const empty = fs.mkdtempSync(path.join(os.tmpdir(), 'discovery-empty-'))
20
+
21
+ const site = new Site(root)
22
+
23
+ const input = (url: string, method = 'GET'): Input =>
24
+ ({
25
+ request: { method, headers: {} },
26
+ url: new URL(url, 'http://nex.toa.io')
27
+ }) as unknown as Input
28
+
29
+ const header = (output: Output, name: string): string | null =>
30
+ output?.headers?.get(name) ?? null
31
+
32
+ describe('discovery site', () => {
33
+ it('should leave a path outside its prefix alone', () => {
34
+ assert.equal(site.intercept(input('/')), null)
35
+ assert.equal(site.intercept(input('/pots/')), null)
36
+ })
37
+
38
+ it('should not claim a path that merely starts like its own', () => {
39
+ assert.equal(site.intercept(input('/.discoveryable/')), null)
40
+ })
41
+
42
+ it('should send the bare path to the directory it is', () => {
43
+ const output = site.intercept(input('/.discovery'))
44
+
45
+ assert.equal(output?.status, 302)
46
+ assert.equal(header(output, 'location'), '/.discovery/')
47
+ })
48
+
49
+ it('should keep the query when it redirects', () => {
50
+ const output = site.intercept(input('/.discovery?at=/pots'))
51
+
52
+ assert.equal(header(output, 'location'), '/.discovery/?at=/pots')
53
+ })
54
+
55
+ it('should serve the page', () => {
56
+ const output = site.intercept(input('/.discovery/'))
57
+
58
+ assert.equal(output?.status, 200)
59
+ assert.equal(header(output, 'content-type'), 'text/html; charset=utf-8')
60
+ assert.equal(header(output, 'cache-control'), 'no-cache')
61
+ })
62
+
63
+ it('should always carry a content-type and a stream', () => {
64
+ // a stream without one is framed as `multipart/*`, and the page arrives as an envelope
65
+ for (const url of ['/.discovery/', '/.discovery/_app/immutable/asset.js']) {
66
+ const output = site.intercept(input(url))
67
+
68
+ assert.ok(output?.headers?.has('content-type'), url)
69
+ assert.ok(output?.body instanceof Readable, url)
70
+ }
71
+ })
72
+
73
+ it('should hold a hashed asset forever and an icon for a day', () => {
74
+ assert.equal(
75
+ header(site.intercept(input('/.discovery/_app/immutable/asset.js')), 'cache-control'),
76
+ 'public, max-age=31536000, immutable'
77
+ )
78
+ assert.equal(
79
+ header(site.intercept(input('/.discovery/favicon.ico')), 'cache-control'),
80
+ 'public, max-age=86400'
81
+ )
82
+ })
83
+
84
+ it('should fall back to the page for anything that could be a route', () => {
85
+ const output = site.intercept(input('/.discovery/pots/:id'))
86
+
87
+ assert.equal(header(output, 'content-type'), 'text/html; charset=utf-8')
88
+ })
89
+
90
+ it('should refuse an asset that is not there', () => {
91
+ // a missing asset is missing, however much a route can look like one
92
+ assert.throws(
93
+ () => site.intercept(input('/.discovery/_app/immutable/gone.js')),
94
+ http.NotFound
95
+ )
96
+ })
97
+
98
+ it('should refuse a path that climbs out of the root', () => {
99
+ /*
100
+ * An encoded separator is what reaches here: `..` and `%2e%2e` are dot segments and the
101
+ * URL parser has already collapsed them, but `%2f` keeps a segment whole through it and
102
+ * decodes to one afterwards.
103
+ */
104
+ for (const url of [
105
+ '/.discovery/%2f%2e%2e%2f%2e%2e%2fpackage.json',
106
+ '/.discovery/a%2f..%2f..%2fpackage.json',
107
+ '/.discovery/%2e%2e%2fpackage.json'
108
+ ])
109
+ assert.throws(() => site.intercept(input(url)), http.NotFound, url)
110
+ })
111
+
112
+ it('should never see a dot segment, which the URL parser collapses first', () => {
113
+ // `/.discovery/%2e%2e/x` is `/x`, which is not this prefix and not ours to answer
114
+ assert.equal(site.intercept(input('/.discovery/%2e%2e/%2e%2e/package.json')), null)
115
+ assert.equal(site.intercept(input('/.discovery/../../package.json')), null)
116
+ })
117
+
118
+ it('should report the length a GET would have returned, and open no stream', () => {
119
+ const output = site.intercept(input('/.discovery/', 'HEAD'))
120
+
121
+ assert.equal(output?.status, 200)
122
+ assert.equal(header(output, 'content-length'), '9')
123
+ assert.equal(output?.body, undefined)
124
+ })
125
+
126
+ it('should leave OPTIONS on the prefix to the tree', () => {
127
+ // it needs the identity, which is resolved after this stage
128
+ assert.equal(site.intercept(input('/.discovery', 'OPTIONS')), null)
129
+ assert.equal(site.intercept(input('/.discovery/', 'OPTIONS')), null)
130
+ })
131
+
132
+ it('should refuse every other method', () => {
133
+ assert.throws(() => site.intercept(input('/.discovery/', 'POST')), http.MethodNotAllowed)
134
+ assert.throws(
135
+ () => site.intercept(input('/.discovery/favicon.ico', 'OPTIONS')),
136
+ http.MethodNotAllowed
137
+ )
138
+ })
139
+
140
+ it('should say so where the page is not built', () => {
141
+ const output = new Site(empty).intercept(input('/.discovery/'))
142
+
143
+ assert.equal(output?.status, 503)
144
+ assert.equal(header(output, 'content-type'), 'text/plain; charset=utf-8')
145
+ assert.ok(output?.body instanceof Readable)
146
+ })
147
+ })
@@ -0,0 +1,202 @@
1
+ import * as fs from 'node:fs'
2
+ import * as path from 'node:path'
3
+ import { Readable } from 'node:stream'
4
+ import { console } from 'openspan'
5
+ import * as http from '../HTTP/index.js'
6
+ import { DISCOVERY } from '../const.js'
7
+ import type { Input, Output } from '../io.js'
8
+ import type { Interceptor } from '../Interception.js'
9
+
10
+ /**
11
+ * The page that reads the tree: the directory `ui` builds, and nothing else.
12
+ *
13
+ * An interceptor rather than an endpoint, for the reason the OAuth documents are one — it
14
+ * runs before a credential is resolved. A page is public, and one served after `auth` would
15
+ * refuse the client holding a stale token, who is the client most likely to have opened it.
16
+ * It is also what keeps `depart` off these replies: a re-issued credential sets `no-store`,
17
+ * which would take the build hash off every asset that carries one.
18
+ *
19
+ * It claims the whole prefix, so that nothing under it reaches the router — a crawler
20
+ * asking for assets that are not there would otherwise have every one of them counted as a
21
+ * route that has gone missing, and ping discovery about it.
22
+ *
23
+ * `OPTIONS` on the prefix itself is the exception it lets through: the tree is what answers
24
+ * there, and that needs the identity this stage does not have yet.
25
+ */
26
+ export class Site implements Interceptor {
27
+ public readonly name = 'discovery'
28
+
29
+ /** What a test points at a fixture; the build otherwise. */
30
+ private readonly override: string | undefined
31
+ private root: string
32
+
33
+ public constructor(root?: string) {
34
+ this.override = root
35
+ this.root = root ?? site()
36
+ }
37
+
38
+ /**
39
+ * Resolved here rather than at construction because this is a module singleton, built
40
+ * when the module loads and before anything has said where the page is. Said once, too,
41
+ * because a page that is not built is otherwise a `503` per request and nothing else.
42
+ */
43
+ public mount(): void {
44
+ this.root = this.override ?? site()
45
+
46
+ if (!isFile(path.join(this.root, 'index.html')))
47
+ console.warn('Discovery UI is not built', { root: this.root })
48
+ }
49
+
50
+ public intercept(input: Input): Output {
51
+ const { pathname } = input.url
52
+
53
+ if (pathname !== DISCOVERY && !pathname.startsWith(DISCOVERY + '/')) return null
54
+
55
+ // the prefix itself, as opposed to something under it
56
+ const bare = pathname === DISCOVERY || pathname === DISCOVERY + '/'
57
+ const method = input.request.method
58
+
59
+ if (method !== 'GET' && method !== 'HEAD') {
60
+ // the tree answers here, and it needs the identity this stage does not have yet
61
+ if (bare && method === 'OPTIONS') return null
62
+
63
+ throw new http.MethodNotAllowed(
64
+ new Headers({ allow: bare ? 'GET, HEAD, OPTIONS' : 'GET, HEAD' })
65
+ )
66
+ }
67
+
68
+ // the page is a directory, and every asset it names is relative to it
69
+ if (pathname === DISCOVERY)
70
+ return {
71
+ status: 302,
72
+ headers: new Headers({ location: DISCOVERY + '/' + input.url.search })
73
+ }
74
+
75
+ const file = this.resolve(pathname)
76
+
77
+ if (file === null) throw new http.NotFound()
78
+
79
+ return this.send(file, method)
80
+ }
81
+
82
+ /**
83
+ * The file a request lands on, or `null` when nothing does. A path that exists is served
84
+ * as it is; anything else that could be a route falls back to the page, because the
85
+ * client router — not this server — knows what routes there are.
86
+ */
87
+ private resolve(pathname: string): string | null {
88
+ /*
89
+ * Decoded after the prefix is off, so that an encoded separator cannot smuggle a
90
+ * segment past the test that put us here. A dot segment never arrives — `..` and
91
+ * `%2e%2e` alike are collapsed while the URL is parsed — but `%2f` survives that and
92
+ * decodes to a separator here, which is what the guard below is for.
93
+ */
94
+ const relative = decode(pathname.slice(DISCOVERY.length))
95
+
96
+ if (relative === null || relative.includes('\0')) return null
97
+
98
+ const file = path.join(this.root, relative)
99
+
100
+ if (file !== this.root && !file.startsWith(this.root + path.sep)) return null
101
+
102
+ if (isFile(file)) return file
103
+
104
+ /*
105
+ * A missing asset is missing, but a route can look like one. What this server would
106
+ * have served is what it knows how to serve, so anything else is a route and falls
107
+ * back to the page — as does anything ending in a slash, which is no name for a file.
108
+ */
109
+ const asset = !relative.endsWith('/') && path.extname(relative) in TYPES
110
+
111
+ return asset ? null : path.join(this.root, 'index.html')
112
+ }
113
+
114
+ /**
115
+ * Always with a `content-type` of its own: a stream without one is framed as
116
+ * `multipart/*` and the page arrives as an envelope of JSON-encoded buffers.
117
+ */
118
+ private send(file: string, method: string): Output {
119
+ const stats = fs.statSync(file, { throwIfNoEntry: false })
120
+
121
+ if (stats === undefined)
122
+ return {
123
+ status: 503,
124
+ headers: new Headers({ 'content-type': 'text/plain; charset=utf-8' }),
125
+ body: Readable.from([Buffer.from(NOT_BUILT)])
126
+ }
127
+
128
+ const headers = new Headers({
129
+ 'content-type': TYPES[path.extname(file)] ?? 'application/octet-stream',
130
+ 'content-length': String(stats.size),
131
+ 'cache-control': caching(path.relative(this.root, file))
132
+ })
133
+
134
+ // a HEAD reply carries no body but reports the length a GET would have returned; the
135
+ // HTTP/2 layer does not drop one written to it, so none is opened
136
+ if (method === 'HEAD') return { status: 200, headers }
137
+
138
+ return { status: 200, headers, body: fs.createReadStream(file) }
139
+ }
140
+ }
141
+
142
+ const NOT_BUILT = 'The discovery UI is not built. Run `npm run build:ui`.\n'
143
+
144
+ function decode(pathname: string): string | null {
145
+ try {
146
+ return decodeURIComponent(pathname)
147
+ } catch {
148
+ return null
149
+ }
150
+ }
151
+
152
+ function isFile(file: string): boolean {
153
+ return fs.statSync(file, { throwIfNoEntry: false })?.isFile() === true
154
+ }
155
+
156
+ /**
157
+ * Where `npm run build:ui` puts the page. Two levels up because this module is a directory
158
+ * deeper than `source` itself, and `transpiled` mirrors that — so it resolves the same from
159
+ * either. The variable is the features', which run against a fixture rather than a build.
160
+ */
161
+ function site(): string {
162
+ return (
163
+ process.env.__TESTING_EXPOSITION_DISCOVERY_ROOT ??
164
+ path.resolve(import.meta.dirname, '..', '..', 'ui', 'dist')
165
+ )
166
+ }
167
+
168
+ /** Assets under this prefix carry their build hash in the name. */
169
+ const IMMUTABLE = path.join('_app', 'immutable')
170
+ const FOREVER = 'public, max-age=31536000, immutable'
171
+
172
+ /** The icons: named without a hash, so they are re-read, but rarely. */
173
+ const ICONS = new Set(['favicon.ico', 'favicon-96x96.png', 'apple-touch-icon.png'])
174
+ const DAY = 'public, max-age=86400'
175
+
176
+ /**
177
+ * How long what is served may be held. The page is never cached — it names the assets, and
178
+ * their names carry the build. An icon is named for what it is rather than for its content,
179
+ * so it is asked about again, but not on every page load.
180
+ */
181
+ function caching(relative: string): string {
182
+ if (relative.startsWith(IMMUTABLE)) return FOREVER
183
+
184
+ return ICONS.has(relative) ? DAY : 'no-cache'
185
+ }
186
+
187
+ const TYPES: Record<string, string> = {
188
+ '.css': 'text/css; charset=utf-8',
189
+ '.html': 'text/html; charset=utf-8',
190
+ '.ico': 'image/x-icon',
191
+ '.jpg': 'image/jpeg',
192
+ '.js': 'text/javascript; charset=utf-8',
193
+ '.json': 'application/json; charset=utf-8',
194
+ '.map': 'application/json; charset=utf-8',
195
+ '.png': 'image/png',
196
+ '.svg': 'image/svg+xml',
197
+ '.txt': 'text/plain; charset=utf-8',
198
+ '.webmanifest': 'application/manifest+json',
199
+ '.webp': 'image/webp',
200
+ '.woff': 'font/woff',
201
+ '.woff2': 'font/woff2'
202
+ }
@@ -0,0 +1,3 @@
1
+ export { Explorer } from './Explorer.js'
2
+ export { Site } from './Site.js'
3
+ export { looking } from './trunk.js'
@@ -0,0 +1,175 @@
1
+ import { describe as suite, it, afterEach } from 'node:test'
2
+ import assert from 'node:assert/strict'
3
+
4
+ import { describe } from './tree.js'
5
+ import { Tree } from '../RTD/Tree.js'
6
+ import type * as http from '../HTTP/index.js'
7
+ import type { Introspection } from '../Introspection.js'
8
+ import type { EndpointsFactory } from '../Endpoint.js'
9
+ import type { DirectiveFactory } from '../RTD/Directives.js'
10
+ import type * as syntax from '../RTD/syntax/index.js'
11
+
12
+ /** A method with no mapping has no endpoint, so what it says is its directives' alone. */
13
+ const endpoints = {} as unknown as EndpointsFactory
14
+
15
+ /** What a method marked with this describes as, standing for a directive that refuses. */
16
+ const REFUSE = 'refuse'
17
+
18
+ const directives = {
19
+ create: (stack: syntax.Directive[]) => ({
20
+ declared: () => undefined,
21
+ precall: async () => null,
22
+ settle: async () => undefined,
23
+ dispose: () => undefined,
24
+ explain: async (_: unknown, introspection: Introspection) => {
25
+ const mark = stack.find((directive) => directive.family === 'test')?.value
26
+
27
+ if (mark === REFUSE) return null
28
+
29
+ return mark === undefined ? introspection : { ...introspection, description: mark }
30
+ }
31
+ }),
32
+ preflight: async () => undefined,
33
+ depart: async () => undefined,
34
+ dispose: () => undefined
35
+ } as unknown as DirectiveFactory
36
+
37
+ const method = (verb: string, mark?: string): syntax.Method => ({
38
+ verb,
39
+ directives: mark === undefined ? [] : [{ family: 'test', name: 'mark', value: mark }]
40
+ })
41
+
42
+ const node = (
43
+ path: string,
44
+ methods: syntax.Method[] = [],
45
+ routes: syntax.Route[] = []
46
+ ): syntax.Route => ({ path, node: { routes, methods, directives: [] } })
47
+
48
+ const trunk = (routes: syntax.Route[], methods: syntax.Method[] = []): syntax.Node => ({
49
+ routes,
50
+ methods,
51
+ directives: []
52
+ })
53
+
54
+ const request = {} as unknown as http.Context
55
+
56
+ afterEach(() => {
57
+ delete process.env.__TESTING_EXPOSITION_BRANCH_TTL
58
+ })
59
+
60
+ suite('discovery tree', () => {
61
+ it('should key the trunk by the root', async () => {
62
+ const tree = new Tree(trunk([], [method('GET')]), endpoints, directives)
63
+
64
+ assert.deepEqual(Object.keys((await describe(tree, request)).routes), ['/'])
65
+ })
66
+
67
+ it('should key every route by the template it answers at', async () => {
68
+ const tree = new Tree(
69
+ trunk([
70
+ node('/pots', [method('GET'), method('POST')], [node('/:id', [method('GET')])])
71
+ ]),
72
+ endpoints,
73
+ directives
74
+ )
75
+
76
+ const { routes: resources } = await describe(tree, request)
77
+
78
+ assert.deepEqual(Object.keys(resources), ['/pots', '/pots/:id'])
79
+ assert.deepEqual(Object.keys(resources['/pots']), ['GET', 'POST'])
80
+ })
81
+
82
+ it('should include a route no name can spell', async () => {
83
+ // `/.rpc` and `/.mcp` leave these out; HTTP addresses a route by its path
84
+ const tree = new Tree(
85
+ trunk([
86
+ node('/v1.0', [method('GET')]),
87
+ node('/files', [], [node('/**', [method('GET')])]),
88
+ node('/any', [], [node('/*', [method('GET')])])
89
+ ]),
90
+ endpoints,
91
+ directives
92
+ )
93
+
94
+ assert.deepEqual(Object.keys((await describe(tree, request)).routes), [
95
+ '/any/*',
96
+ '/files/**',
97
+ '/v1.0'
98
+ ])
99
+ })
100
+
101
+ it('should skip an intermediate node, whose route answers in its place', async () => {
102
+ const tree = new Tree(
103
+ trunk([node('/posts', [method('PATCH')], [node('/', [method('PUT')])])]),
104
+ endpoints,
105
+ directives
106
+ )
107
+
108
+ assert.deepEqual(Object.keys((await describe(tree, request)).routes), ['/posts'])
109
+ })
110
+
111
+ it('should omit a resource this caller may reach no method of', async () => {
112
+ const tree = new Tree(
113
+ trunk([
114
+ node('/open', [method('GET')]),
115
+ node('/closed', [method('GET', REFUSE), method('POST', REFUSE)])
116
+ ]),
117
+ endpoints,
118
+ directives
119
+ )
120
+
121
+ assert.deepEqual(Object.keys((await describe(tree, request)).routes), ['/open'])
122
+ })
123
+
124
+ it('should carry only the verbs this caller may reach', async () => {
125
+ const tree = new Tree(
126
+ trunk([node('/pots', [method('GET'), method('POST', REFUSE)])]),
127
+ endpoints,
128
+ directives
129
+ )
130
+
131
+ const { routes: resources } = await describe(tree, request)
132
+
133
+ assert.deepEqual(Object.keys(resources['/pots']), ['GET'])
134
+ })
135
+
136
+ it('should answer the declaration a request would reach where two share a template', async () => {
137
+ // `/a/b` and `/a: { /b: }` are two routes and one template; the more specific matches
138
+ const tree = new Tree(
139
+ trunk([
140
+ node('/a/b', [method('GET', 'nested')]),
141
+ node('/a', [], [node('/b', [method('GET', 'adjacent')])])
142
+ ]),
143
+ endpoints,
144
+ directives
145
+ )
146
+
147
+ const { routes: resources } = await describe(tree, request)
148
+
149
+ assert.deepEqual(Object.keys(resources), ['/a/b'])
150
+ assert.equal(resources['/a/b'].GET.description, 'nested')
151
+ })
152
+
153
+ it('should drop a branch that has expired', async () => {
154
+ process.env.__TESTING_EXPOSITION_BRANCH_TTL = '-1'
155
+
156
+ const tree = new Tree(trunk([]), endpoints, directives)
157
+
158
+ tree.merge(trunk([node('/pots', [method('GET')])]), {
159
+ namespace: 'default',
160
+ component: 'pots'
161
+ })
162
+
163
+ assert.deepEqual(Object.keys((await describe(tree, request)).routes), [])
164
+ })
165
+
166
+ it('should sort, because branches merge in the order their tenants answer', async () => {
167
+ const tree = new Tree(
168
+ trunk([node('/zed', [method('GET')]), node('/alpha', [method('GET')])]),
169
+ endpoints,
170
+ directives
171
+ )
172
+
173
+ assert.deepEqual(Object.keys((await describe(tree, request)).routes), ['/alpha', '/zed'])
174
+ })
175
+ })