@toa.io/extensions.exposition 0.22.1 → 1.0.0-alpha.0

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 (77) hide show
  1. package/components/identity.basic/source/authenticate.ts +3 -2
  2. package/components/identity.basic/source/transit.ts +4 -3
  3. package/components/octets.storage/manifest.toa.yaml +26 -0
  4. package/components/octets.storage/operations/delete.js +7 -0
  5. package/components/octets.storage/operations/fetch.js +46 -0
  6. package/components/octets.storage/operations/get.js +7 -0
  7. package/components/octets.storage/operations/list.js +7 -0
  8. package/components/octets.storage/operations/permute.js +7 -0
  9. package/components/octets.storage/operations/store.js +11 -0
  10. package/cucumber.js +0 -1
  11. package/documentation/access.md +2 -3
  12. package/documentation/cache.md +42 -0
  13. package/documentation/octets.md +196 -0
  14. package/documentation/protocol.md +49 -5
  15. package/documentation/tree.md +1 -5
  16. package/features/access.feature +1 -0
  17. package/features/cache.feature +160 -0
  18. package/features/errors.feature +18 -0
  19. package/features/identity.basic.feature +2 -0
  20. package/features/octets.feature +295 -0
  21. package/features/octets.workflows.feature +114 -0
  22. package/features/routes.feature +40 -0
  23. package/features/steps/HTTP.ts +56 -6
  24. package/features/steps/Parameters.ts +5 -2
  25. package/features/steps/Workspace.ts +8 -5
  26. package/features/steps/components/octets.tester/manifest.toa.yaml +15 -0
  27. package/features/steps/components/octets.tester/operations/bar.js +12 -0
  28. package/features/steps/components/octets.tester/operations/baz.js +11 -0
  29. package/features/steps/components/octets.tester/operations/diversify.js +14 -0
  30. package/features/steps/components/octets.tester/operations/err.js +16 -0
  31. package/features/steps/components/octets.tester/operations/foo.js +7 -0
  32. package/features/steps/components/octets.tester/operations/lenna.png +0 -0
  33. package/features/steps/components/pots/manifest.toa.yaml +1 -1
  34. package/features/streams.feature +5 -1
  35. package/package.json +16 -9
  36. package/readme.md +8 -5
  37. package/schemas/octets/context.cos.yaml +1 -0
  38. package/schemas/octets/delete.cos.yaml +1 -0
  39. package/schemas/octets/fetch.cos.yaml +3 -0
  40. package/schemas/octets/list.cos.yaml +1 -0
  41. package/schemas/octets/permute.cos.yaml +1 -0
  42. package/schemas/octets/store.cos.yaml +3 -0
  43. package/source/Gateway.ts +9 -4
  44. package/source/HTTP/Server.fixtures.ts +2 -6
  45. package/source/HTTP/Server.test.ts +9 -31
  46. package/source/HTTP/Server.ts +33 -19
  47. package/source/HTTP/exceptions.ts +2 -12
  48. package/source/HTTP/formats/index.ts +7 -4
  49. package/source/HTTP/formats/json.ts +3 -0
  50. package/source/HTTP/formats/msgpack.ts +3 -0
  51. package/source/HTTP/formats/text.ts +3 -0
  52. package/source/HTTP/formats/yaml.ts +3 -0
  53. package/source/HTTP/messages.test.ts +3 -49
  54. package/source/HTTP/messages.ts +60 -35
  55. package/source/RTD/Route.ts +1 -1
  56. package/source/RTD/segment.ts +2 -1
  57. package/source/RTD/syntax/parse.ts +2 -1
  58. package/source/Remotes.ts +8 -0
  59. package/source/Tenant.ts +5 -0
  60. package/source/directives/auth/Family.ts +26 -22
  61. package/source/directives/auth/Rule.ts +1 -1
  62. package/source/directives/cache/Control.ts +59 -0
  63. package/source/directives/cache/Exact.ts +7 -0
  64. package/source/directives/cache/Family.ts +36 -0
  65. package/source/directives/cache/index.ts +3 -0
  66. package/source/directives/cache/types.ts +9 -0
  67. package/source/directives/index.ts +3 -1
  68. package/source/directives/octets/Context.ts +18 -0
  69. package/source/directives/octets/Delete.ts +32 -0
  70. package/source/directives/octets/Family.ts +68 -0
  71. package/source/directives/octets/Fetch.ts +85 -0
  72. package/source/directives/octets/List.ts +32 -0
  73. package/source/directives/octets/Permute.ts +37 -0
  74. package/source/directives/octets/Store.ts +158 -0
  75. package/source/directives/octets/index.ts +3 -0
  76. package/source/directives/octets/schemas.ts +12 -0
  77. package/source/directives/octets/types.ts +13 -0
@@ -0,0 +1,37 @@
1
+ import { NotAcceptable, NotFound } from '../../HTTP'
2
+ import * as schemas from './schemas'
3
+ import type { Maybe } from '@toa.io/types'
4
+ import type { Component } from '@toa.io/core'
5
+ import type { Output } from '../../Directive'
6
+
7
+ import type { Directive, Input } from './types'
8
+
9
+ export class Permute implements Directive {
10
+ public readonly targeted = false
11
+
12
+ private readonly discovery: Promise<Component>
13
+ private storage: Component | null = null
14
+
15
+ public constructor (value: null, discovery: Promise<Component>) {
16
+ schemas.remove.validate(value)
17
+
18
+ this.discovery = discovery
19
+ }
20
+
21
+ public async apply (storage: string, request: Input): Promise<Output> {
22
+ this.storage ??= await this.discovery
23
+
24
+ if (request.encoder === null)
25
+ throw new NotAcceptable()
26
+
27
+ const path = request.path
28
+ const list = await request.parse()
29
+ const input = { storage, path, list }
30
+ const error = await this.storage.invoke<Maybe<unknown>>('permute', { input })
31
+
32
+ if (error instanceof Error)
33
+ throw new NotFound()
34
+
35
+ return {}
36
+ }
37
+ }
@@ -0,0 +1,158 @@
1
+ import { Readable } from 'node:stream'
2
+ import { posix } from 'node:path'
3
+ import { match } from 'matchacho'
4
+ import { promex } from '@toa.io/generic'
5
+ import { BadRequest, UnsupportedMediaType } from '../../HTTP'
6
+ import * as schemas from './schemas'
7
+ import type { Maybe } from '@toa.io/types'
8
+ import type { Entry } from '@toa.io/extensions.storages'
9
+ import type { Remotes } from '../../Remotes'
10
+ import type { ErrorType } from 'error-value'
11
+ import type { Component } from '@toa.io/core'
12
+ import type { Output } from '../../Directive'
13
+ import type { Directive, Input } from './types'
14
+
15
+ export class Store implements Directive {
16
+ public readonly targeted = false
17
+
18
+ private readonly accept: string | undefined
19
+ private readonly workflow: Workflow | undefined
20
+ private readonly discovery: Record<string, Promise<Component>> = {}
21
+ private readonly remotes: Remotes
22
+ private readonly components: Record<string, Component> = {}
23
+ private storage: Component | null = null
24
+
25
+ public constructor
26
+ (options: Options | null, discovery: Promise<Component>, remotes: Remotes) {
27
+ schemas.store.validate(options)
28
+
29
+ this.accept = match(options?.accept,
30
+ String, (value: string) => value,
31
+ Array, (types: string[]) => types.join(','),
32
+ undefined)
33
+
34
+ this.workflow = match(options?.workflow,
35
+ Array, (units: Unit[]) => units,
36
+ Object, (unit: Unit) => [unit],
37
+ undefined)
38
+
39
+ this.discovery.storage = discovery
40
+ this.remotes = remotes
41
+ }
42
+
43
+ public async apply (storage: string, request: Input): Promise<Output> {
44
+ this.storage ??= await this.discovery.storage
45
+
46
+ const input = { storage, request, accept: this.accept }
47
+ const entry = await this.storage.invoke('store', { input })
48
+
49
+ return match<Output>(entry,
50
+ Error, (error: ErrorType) => this.throw(error),
51
+ () => this.reply(request, storage, entry))
52
+ }
53
+
54
+ private reply (request: Input, storage: string, entry: Entry): Output {
55
+ const body = this.workflow === undefined
56
+ ? entry
57
+ : Readable.from(this.execute(request, storage, entry))
58
+
59
+ return { body }
60
+ }
61
+
62
+ private throw (error: ErrorType): never {
63
+ throw match(error.code,
64
+ 'NOT_ACCEPTABLE', () => new UnsupportedMediaType(),
65
+ 'TYPE_MISMATCH', () => new BadRequest(),
66
+ error)
67
+ }
68
+
69
+ /* eslint-disable no-useless-return, max-depth */
70
+
71
+ /**
72
+ * Execute workflow units sequentially, steps within a unit in parallel.
73
+ * Yield results as soon as they come.
74
+ *
75
+ * If you need to change this, it may take a while.
76
+ */
77
+ private async * execute (request: Input, storage: string, entry: Entry): AsyncGenerator {
78
+ yield entry
79
+
80
+ const path = posix.join(request.path, entry.id)
81
+ let interrupted = false
82
+
83
+ for (const unit of this.workflow as Workflow) {
84
+ if (interrupted)
85
+ break
86
+
87
+ const steps = Object.keys(unit)
88
+
89
+ // unit result promises queue
90
+ const results = Array.from(steps, promex<unknown>)
91
+ let next = 0
92
+
93
+ // execute steps in parallel
94
+ for (const step of steps)
95
+ // these promises are indirectly awaited in the yield loop
96
+ void (async () => {
97
+ const endpoint = unit[step]
98
+ const context: Context = { storage, path, entry }
99
+ const result = await this.call(endpoint, context)
100
+
101
+ if (interrupted)
102
+ return
103
+
104
+ // as a result is received, resolve the next promise from the queue
105
+ const promise = results[next++]
106
+
107
+ if (result instanceof Error) {
108
+ interrupted = true
109
+ promise.resolve({ error: { step, ...result } })
110
+
111
+ // cancel pending promises
112
+ results[next].resolve(null)
113
+ } else
114
+ promise.resolve({ [step]: result ?? null })
115
+ })().catch((e) => results[next].reject(e))
116
+
117
+ // yield results from the queue as they come
118
+ for (const promise of results) {
119
+ const result = await promise
120
+
121
+ if (result === null) // canceled promise
122
+ break
123
+ else
124
+ yield result
125
+ }
126
+ }
127
+ }
128
+
129
+ private async call (endpoint: string, context: Context): Promise<Maybe<unknown>> {
130
+ const [operation, component, namespace = 'default'] = endpoint.split('.').reverse()
131
+ const key = `${namespace}.${component}`
132
+
133
+ this.components[key] ??= await this.discover(key, namespace, component)
134
+
135
+ return await this.components[key].invoke(operation, { input: context })
136
+ }
137
+
138
+ private async discover (key: string, namespace: string, component: string): Promise<Component> {
139
+ if (this.discovery[key] === undefined)
140
+ this.discovery[key] = this.remotes.discover(namespace, component)
141
+
142
+ return await this.discovery[key]
143
+ }
144
+ }
145
+
146
+ type Unit = Record<string, string>
147
+ type Workflow = Unit[]
148
+
149
+ interface Options {
150
+ accept: string | string[]
151
+ workflow: Workflow | Unit
152
+ }
153
+
154
+ interface Context {
155
+ storage: string
156
+ path: string
157
+ entry: Entry
158
+ }
@@ -0,0 +1,3 @@
1
+ import Family from './Family'
2
+
3
+ export = Family
@@ -0,0 +1,12 @@
1
+ import { resolve } from 'node:path'
2
+ import schemas from '@toa.io/schemas'
3
+
4
+ const path = resolve(__dirname, '../../../schemas/octets')
5
+ const namespace = schemas.namespace(path)
6
+
7
+ export const context = namespace.schema('context')
8
+ export const store = namespace.schema('store')
9
+ export const fetch = namespace.schema('fetch')
10
+ export const remove = namespace.schema('delete')
11
+ export const list = namespace.schema('list')
12
+ export const permute = namespace.schema('permute')
@@ -0,0 +1,13 @@
1
+ import type * as directive from '../../Directive'
2
+
3
+ export interface Directive {
4
+ readonly targeted: boolean
5
+
6
+ apply: (storage: string, input: Input) => directive.Output | Promise<directive.Output>
7
+ }
8
+
9
+ export interface Extension {
10
+ octets?: string
11
+ }
12
+
13
+ export type Input = directive.Input & Extension