@toa.io/bridges.node 1.0.0-alpha.8 → 1.0.0-alpha.83
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/package.json +7 -6
- package/readme.md +1 -1
- package/src/algorithms/runner.js +4 -0
- package/src/context.js +6 -15
- package/src/define/.operations/syntaxes/constants.js +1 -1
- package/src/define/.operations/syntaxes/function.js +11 -5
- package/src/factory.js +4 -4
- package/src/shortcuts/amqp.js +3 -2
- package/src/shortcuts/configuration.js +1 -1
- package/src/shortcuts/http.js +1 -1
- package/src/shortcuts/index.js +4 -0
- package/src/shortcuts/logs.js +19 -0
- package/src/shortcuts/pubsub.js +16 -0
- package/src/shortcuts/stash.js +1 -1
- package/src/shortcuts/state.js +1 -1
- package/test/define.operations.define.test.js +6 -6
- package/test/factory.algorithm.test.js +1 -1
- package/types/algorithms.d.ts +6 -4
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@toa.io/bridges.node",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
3
|
+
"version": "1.0.0-alpha.83",
|
|
4
4
|
"description": "Toa Node Bridge (inproc)",
|
|
5
5
|
"homepage": "https://toa.io",
|
|
6
6
|
"author": {
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
"url": "https://github.com/toa-io/toa/issues"
|
|
17
17
|
},
|
|
18
18
|
"engines": {
|
|
19
|
-
"node": ">=
|
|
19
|
+
"node": ">= 20.0.0"
|
|
20
20
|
},
|
|
21
21
|
"publishConfig": {
|
|
22
22
|
"access": "public"
|
|
@@ -26,14 +26,15 @@
|
|
|
26
26
|
"test": "echo \"Error: run tests from root\" && exit 1"
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@
|
|
30
|
-
"@toa.io/
|
|
31
|
-
"@toa.io/
|
|
29
|
+
"@babel/parser": "7.24.7",
|
|
30
|
+
"@toa.io/core": "1.0.0-alpha.81",
|
|
31
|
+
"@toa.io/filesystem": "1.0.0-alpha.63",
|
|
32
|
+
"@toa.io/generic": "1.0.0-alpha.63",
|
|
32
33
|
"fast-glob": "3.2.7",
|
|
33
34
|
"matchacho": "0.3.5"
|
|
34
35
|
},
|
|
35
36
|
"devDependencies": {
|
|
36
37
|
"clone-deep": "4.0.1"
|
|
37
38
|
},
|
|
38
|
-
"gitHead": "
|
|
39
|
+
"gitHead": "d050d114ada42d94aecc927b51e9be68026a3a33"
|
|
39
40
|
}
|
package/readme.md
CHANGED
package/src/algorithms/runner.js
CHANGED
package/src/context.js
CHANGED
|
@@ -5,29 +5,23 @@ const { underlay } = require('@toa.io/generic')
|
|
|
5
5
|
|
|
6
6
|
const shortcuts = require('./shortcuts')
|
|
7
7
|
|
|
8
|
-
/**
|
|
9
|
-
* @implements {toa.node.Context}
|
|
10
|
-
*/
|
|
11
8
|
class Context extends Connector {
|
|
9
|
+
operation
|
|
12
10
|
aspects
|
|
13
|
-
configuration
|
|
14
|
-
origins
|
|
15
11
|
|
|
16
12
|
#context
|
|
17
13
|
|
|
18
|
-
|
|
19
|
-
* @param {toa.core.Context} context
|
|
20
|
-
*/
|
|
21
|
-
constructor (context) {
|
|
14
|
+
constructor (context, operation) {
|
|
22
15
|
super()
|
|
23
16
|
|
|
17
|
+
this.operation = operation
|
|
24
18
|
this.#context = context
|
|
25
19
|
|
|
26
20
|
this.depends(context)
|
|
27
21
|
}
|
|
28
22
|
|
|
29
23
|
async open () {
|
|
30
|
-
this.aspects = this.#aspects(
|
|
24
|
+
this.aspects = this.#aspects(this.#context.aspects)
|
|
31
25
|
}
|
|
32
26
|
|
|
33
27
|
local = underlay(async ([endpoint], [request]) => {
|
|
@@ -42,10 +36,6 @@ class Context extends Connector {
|
|
|
42
36
|
return this.#context.call(namespace, name, endpoint, request)
|
|
43
37
|
})
|
|
44
38
|
|
|
45
|
-
/**
|
|
46
|
-
* @param {toa.core.extensions.Aspect[]} aspects
|
|
47
|
-
* @returns {{ [key: string]: Function}}
|
|
48
|
-
*/
|
|
49
39
|
#aspects (aspects) {
|
|
50
40
|
const map = {}
|
|
51
41
|
|
|
@@ -54,7 +44,8 @@ class Context extends Connector {
|
|
|
54
44
|
|
|
55
45
|
map[aspect.name] = aspect.invoke.bind(aspect)
|
|
56
46
|
|
|
57
|
-
if (aspect.name in shortcuts)
|
|
47
|
+
if (aspect.name in shortcuts)
|
|
48
|
+
shortcuts[aspect.name](this, aspect)
|
|
58
49
|
}
|
|
59
50
|
|
|
60
51
|
return map
|
|
@@ -13,6 +13,8 @@ const define = (descriptor) => {
|
|
|
13
13
|
definition.type = name
|
|
14
14
|
|
|
15
15
|
if (node.params.length > 1) definition.scope = scope(node.params[1].name)
|
|
16
|
+
else definition.scope = 'none'
|
|
17
|
+
|
|
16
18
|
if (node.params.length === 0) definition.input = null
|
|
17
19
|
|
|
18
20
|
return definition
|
|
@@ -27,11 +29,15 @@ const test = (statement, type) => {
|
|
|
27
29
|
return func && known
|
|
28
30
|
}
|
|
29
31
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
32
|
+
function scope (name) {
|
|
33
|
+
if (scopes.includes(name))
|
|
34
|
+
return name
|
|
35
|
+
|
|
36
|
+
if (name === 'context')
|
|
37
|
+
return 'none'
|
|
38
|
+
|
|
39
|
+
return undefined
|
|
40
|
+
}
|
|
35
41
|
|
|
36
42
|
const nodes = ['FunctionDeclaration', 'ArrowFunctionExpression', 'ClassMethod']
|
|
37
43
|
|
package/src/factory.js
CHANGED
|
@@ -8,9 +8,9 @@ const { Context } = require('./context')
|
|
|
8
8
|
const { extract } = require('./define/operations')
|
|
9
9
|
|
|
10
10
|
class Factory {
|
|
11
|
-
algorithm (root, name, context) {
|
|
11
|
+
async algorithm (root, name, context) {
|
|
12
12
|
const module = load.operation(root, name)
|
|
13
|
-
const ctx = new Context(context)
|
|
13
|
+
const ctx = new Context(context, name)
|
|
14
14
|
|
|
15
15
|
return runner(module, ctx)
|
|
16
16
|
}
|
|
@@ -36,11 +36,11 @@ class Factory {
|
|
|
36
36
|
* @param {toa.node.Context} context
|
|
37
37
|
* @returns {Runner}
|
|
38
38
|
*/
|
|
39
|
-
function runner (module, context) {
|
|
39
|
+
async function runner (module, context) {
|
|
40
40
|
const descriptor = extract(module)
|
|
41
41
|
const func = module[descriptor.name]
|
|
42
42
|
const factory = require('./algorithms/' + descriptor.syntax)
|
|
43
|
-
const instance = factory.create(func)
|
|
43
|
+
const instance = await factory.create(func)
|
|
44
44
|
|
|
45
45
|
return new Runner(instance, context)
|
|
46
46
|
}
|
package/src/shortcuts/amqp.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
3
|
const { underlay } = require('@toa.io/generic')
|
|
4
|
+
const assert = require('node:assert')
|
|
4
5
|
|
|
5
6
|
/** @type {toa.node.shortcut} */
|
|
6
|
-
|
|
7
|
+
function amqp (context, aspect) {
|
|
7
8
|
context.amqp = underlay(async (segs, args) => {
|
|
8
|
-
|
|
9
|
+
assert(segs.length === 2, `AMQP aspect call should have 2 segments [${segs.join(', ')}] given`)
|
|
9
10
|
|
|
10
11
|
const [origin, method] = segs
|
|
11
12
|
|
package/src/shortcuts/http.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
const { underlay } = require('@toa.io/generic')
|
|
4
4
|
|
|
5
5
|
/** @type {toa.node.shortcut} */
|
|
6
|
-
|
|
6
|
+
function http (context, aspect) {
|
|
7
7
|
context.http = underlay(async (segs, args) => {
|
|
8
8
|
if (segs.length < 2) throw new Error(`Origins call requires at least 2 arguments, ${segs.length} given`)
|
|
9
9
|
|
package/src/shortcuts/index.js
CHANGED
|
@@ -6,6 +6,8 @@ const { configuration } = require('./configuration')
|
|
|
6
6
|
const { state } = require('./state')
|
|
7
7
|
const { stash } = require('./stash')
|
|
8
8
|
const { storages } = require('./storages')
|
|
9
|
+
const { pubsub } = require('./pubsub')
|
|
10
|
+
const { logs } = require('./logs')
|
|
9
11
|
|
|
10
12
|
exports.http = http
|
|
11
13
|
exports.amqp = amqp
|
|
@@ -13,3 +15,5 @@ exports.configuration = configuration
|
|
|
13
15
|
exports.state = state
|
|
14
16
|
exports.stash = stash
|
|
15
17
|
exports.storages = storages
|
|
18
|
+
exports.pubsub = pubsub
|
|
19
|
+
exports.logs = logs
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
function logs (context, aspect) {
|
|
4
|
+
function invoke (severity) {
|
|
5
|
+
return (message, attributes) => aspect.invoke(context.operation, severity, message, attributes)
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
context.logs = CHANNELS.reduce((logs, channel) => {
|
|
9
|
+
logs[channel] = invoke(channel)
|
|
10
|
+
|
|
11
|
+
return logs
|
|
12
|
+
}, {})
|
|
13
|
+
|
|
14
|
+
context.logs.fork = (ctx) => aspect.invoke(context.operation, 'fork', ctx)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const CHANNELS = ['debug', 'info', 'warn', 'error']
|
|
18
|
+
|
|
19
|
+
exports.logs = logs
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { underlay } = require('@toa.io/generic')
|
|
4
|
+
const assert = require('node:assert')
|
|
5
|
+
|
|
6
|
+
function pubsub (context, aspect) {
|
|
7
|
+
context.pubsub = underlay(async (segs, args) => {
|
|
8
|
+
assert(segs.length === 2, `Pub/Sub aspect call should have 2 segments [${segs.join(', ')}] given`)
|
|
9
|
+
|
|
10
|
+
const [origin, method] = segs
|
|
11
|
+
|
|
12
|
+
return aspect.invoke(method, origin, ...args)
|
|
13
|
+
})
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
exports.pubsub = pubsub
|
package/src/shortcuts/stash.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
const { underlay } = require('@toa.io/generic')
|
|
4
4
|
|
|
5
5
|
/** @type {toa.node.shortcut} */
|
|
6
|
-
|
|
6
|
+
function stash (context, aspect) {
|
|
7
7
|
context.stash = underlay(async (segs, args) => {
|
|
8
8
|
if (segs.length !== 1)
|
|
9
9
|
throw new Error(`Stash aspect call should have 1 segment, [${segs.join(', ')}] given`)
|
package/src/shortcuts/state.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
const { generate } = require('@toa.io/generic')
|
|
4
4
|
|
|
5
5
|
/** @type {toa.node.shortcut} */
|
|
6
|
-
|
|
6
|
+
function state (context, aspect) {
|
|
7
7
|
context.state = generate((segs, value) => {
|
|
8
8
|
if (value === undefined) return get(aspect, segs)
|
|
9
9
|
else set(aspect, segs, value)
|
|
@@ -67,7 +67,7 @@ describe('function', () => {
|
|
|
67
67
|
const module = { computation }
|
|
68
68
|
const definition = define(module)
|
|
69
69
|
|
|
70
|
-
expect(definition).toMatchObject({ type: 'computation', scope:
|
|
70
|
+
expect(definition).toMatchObject({ type: 'computation', scope: 'none' })
|
|
71
71
|
})
|
|
72
72
|
|
|
73
73
|
it('should parse effect declaration', () => {
|
|
@@ -76,7 +76,7 @@ describe('function', () => {
|
|
|
76
76
|
const module = { effect }
|
|
77
77
|
const definition = define(module)
|
|
78
78
|
|
|
79
|
-
expect(definition).toMatchObject({ type: 'effect', scope:
|
|
79
|
+
expect(definition).toMatchObject({ type: 'effect', scope: 'none' })
|
|
80
80
|
})
|
|
81
81
|
|
|
82
82
|
it('should parse expression', () => {
|
|
@@ -103,8 +103,8 @@ describe('function', () => {
|
|
|
103
103
|
expect(definition.scope).toStrictEqual(undefined)
|
|
104
104
|
})
|
|
105
105
|
|
|
106
|
-
it('should not define scope', async () => {
|
|
107
|
-
const observation = (input) => null
|
|
106
|
+
it('should not define unknown scope', async () => {
|
|
107
|
+
const observation = (input, message) => null
|
|
108
108
|
const module = { observation }
|
|
109
109
|
const definition = define(module)
|
|
110
110
|
|
|
@@ -168,7 +168,7 @@ describe('class', () => {
|
|
|
168
168
|
expect(() => define(module)).toThrow('does not match conventions')
|
|
169
169
|
})
|
|
170
170
|
|
|
171
|
-
it('should define
|
|
171
|
+
it('should define none scope', async () => {
|
|
172
172
|
class Observation {
|
|
173
173
|
execute (input) {}
|
|
174
174
|
}
|
|
@@ -176,7 +176,7 @@ describe('class', () => {
|
|
|
176
176
|
const module = { Observation }
|
|
177
177
|
const definition = define(module)
|
|
178
178
|
|
|
179
|
-
expect(definition.scope).
|
|
179
|
+
expect(definition.scope).toBe('none')
|
|
180
180
|
})
|
|
181
181
|
|
|
182
182
|
it('should define null input', async () => {
|
|
@@ -26,7 +26,7 @@ it('should be', () => {
|
|
|
26
26
|
|
|
27
27
|
for (const sample of ['fn', 'cls', 'fct']) {
|
|
28
28
|
it(`should create '${sample}' operation`, async () => {
|
|
29
|
-
const algorithm = factory.algorithm(root, sample, context)
|
|
29
|
+
const algorithm = await factory.algorithm(root, sample, context)
|
|
30
30
|
|
|
31
31
|
expect(algorithm).toBeDefined()
|
|
32
32
|
|
package/types/algorithms.d.ts
CHANGED
|
@@ -2,22 +2,24 @@ import { bridges } from '@toa.io/core'
|
|
|
2
2
|
import * as _context from './context'
|
|
3
3
|
import * as _core from '@toa.io/core'
|
|
4
4
|
|
|
5
|
-
declare namespace toa.node{
|
|
5
|
+
declare namespace toa.node {
|
|
6
6
|
|
|
7
|
-
namespace algorithms{
|
|
7
|
+
namespace algorithms {
|
|
8
8
|
|
|
9
9
|
type Constructor = () => bridges.Algorithm
|
|
10
10
|
|
|
11
|
-
interface Factory{
|
|
11
|
+
interface Factory {
|
|
12
12
|
create: Constructor
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
type func = (input?: any, scope?: object | object[], context?: _context.Context) => Promise<_core.Reply>
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
interface Algorithm{
|
|
18
|
+
interface Algorithm {
|
|
19
19
|
mount? (context: _context.Context): Promise<void> | void
|
|
20
20
|
|
|
21
|
+
unmount? (): Promise<void> | void
|
|
22
|
+
|
|
21
23
|
execute (input: any, scope: object | object[]): Promise<any>
|
|
22
24
|
|
|
23
25
|
execute (input: any): Promise<_core.Reply>
|