@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.
- package/components/identity.basic/source/authenticate.ts +3 -2
- package/components/identity.basic/source/transit.ts +4 -3
- package/components/octets.storage/manifest.toa.yaml +26 -0
- package/components/octets.storage/operations/delete.js +7 -0
- package/components/octets.storage/operations/fetch.js +46 -0
- package/components/octets.storage/operations/get.js +7 -0
- package/components/octets.storage/operations/list.js +7 -0
- package/components/octets.storage/operations/permute.js +7 -0
- package/components/octets.storage/operations/store.js +11 -0
- package/cucumber.js +0 -1
- package/documentation/access.md +2 -3
- package/documentation/cache.md +42 -0
- package/documentation/octets.md +196 -0
- package/documentation/protocol.md +49 -5
- package/documentation/tree.md +1 -5
- package/features/access.feature +1 -0
- package/features/cache.feature +160 -0
- package/features/errors.feature +18 -0
- package/features/identity.basic.feature +2 -0
- package/features/octets.feature +295 -0
- package/features/octets.workflows.feature +114 -0
- package/features/routes.feature +40 -0
- package/features/steps/HTTP.ts +56 -6
- package/features/steps/Parameters.ts +5 -2
- package/features/steps/Workspace.ts +8 -5
- package/features/steps/components/octets.tester/manifest.toa.yaml +15 -0
- package/features/steps/components/octets.tester/operations/bar.js +12 -0
- package/features/steps/components/octets.tester/operations/baz.js +11 -0
- package/features/steps/components/octets.tester/operations/diversify.js +14 -0
- package/features/steps/components/octets.tester/operations/err.js +16 -0
- package/features/steps/components/octets.tester/operations/foo.js +7 -0
- package/features/steps/components/octets.tester/operations/lenna.png +0 -0
- package/features/steps/components/pots/manifest.toa.yaml +1 -1
- package/features/streams.feature +5 -1
- package/package.json +16 -9
- package/readme.md +8 -5
- package/schemas/octets/context.cos.yaml +1 -0
- package/schemas/octets/delete.cos.yaml +1 -0
- package/schemas/octets/fetch.cos.yaml +3 -0
- package/schemas/octets/list.cos.yaml +1 -0
- package/schemas/octets/permute.cos.yaml +1 -0
- package/schemas/octets/store.cos.yaml +3 -0
- package/source/Gateway.ts +9 -4
- package/source/HTTP/Server.fixtures.ts +2 -6
- package/source/HTTP/Server.test.ts +9 -31
- package/source/HTTP/Server.ts +33 -19
- package/source/HTTP/exceptions.ts +2 -12
- package/source/HTTP/formats/index.ts +7 -4
- package/source/HTTP/formats/json.ts +3 -0
- package/source/HTTP/formats/msgpack.ts +3 -0
- package/source/HTTP/formats/text.ts +3 -0
- package/source/HTTP/formats/yaml.ts +3 -0
- package/source/HTTP/messages.test.ts +3 -49
- package/source/HTTP/messages.ts +60 -35
- package/source/RTD/Route.ts +1 -1
- package/source/RTD/segment.ts +2 -1
- package/source/RTD/syntax/parse.ts +2 -1
- package/source/Remotes.ts +8 -0
- package/source/Tenant.ts +5 -0
- package/source/directives/auth/Family.ts +26 -22
- package/source/directives/auth/Rule.ts +1 -1
- package/source/directives/cache/Control.ts +59 -0
- package/source/directives/cache/Exact.ts +7 -0
- package/source/directives/cache/Family.ts +36 -0
- package/source/directives/cache/index.ts +3 -0
- package/source/directives/cache/types.ts +9 -0
- package/source/directives/index.ts +3 -1
- package/source/directives/octets/Context.ts +18 -0
- package/source/directives/octets/Delete.ts +32 -0
- package/source/directives/octets/Family.ts +68 -0
- package/source/directives/octets/Fetch.ts +85 -0
- package/source/directives/octets/List.ts +32 -0
- package/source/directives/octets/Permute.ts +37 -0
- package/source/directives/octets/Store.ts +158 -0
- package/source/directives/octets/index.ts +3 -0
- package/source/directives/octets/schemas.ts +12 -0
- 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,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
|