@toa.io/bridges.node 0.1.0-dev.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/LICENSE +22 -0
- package/package.json +40 -0
- package/src/context.js +21 -0
- package/src/define/events.js +15 -0
- package/src/define/index.js +12 -0
- package/src/define/operations/definition.js +40 -0
- package/src/define/operations.js +12 -0
- package/src/define/receivers.js +21 -0
- package/src/event.js +14 -0
- package/src/factory.js +28 -0
- package/src/index.js +7 -0
- package/src/load.js +29 -0
- package/src/operation.js +23 -0
- package/src/receiver.js +14 -0
- package/test/operation.fixtures.js +9 -0
- package/test/operation.test.js +39 -0
- package/test/operations.definition.test.js +40 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Copyright (c) 2020-present Artem Gurtovoi
|
|
2
|
+
|
|
3
|
+
MIT License
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
6
|
+
a copy of this software and associated documentation files (the
|
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
11
|
+
the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be
|
|
14
|
+
included in all copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@toa.io/bridges.node",
|
|
3
|
+
"version": "0.1.0-dev.0",
|
|
4
|
+
"description": "Toa Node Bridge (inproc)",
|
|
5
|
+
"homepage": "https://toa.io",
|
|
6
|
+
"author": {
|
|
7
|
+
"name": "Artem Gurtovoi",
|
|
8
|
+
"url": "https://github.com/temich"
|
|
9
|
+
},
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/toa-io/toa.git"
|
|
14
|
+
},
|
|
15
|
+
"bugs": {
|
|
16
|
+
"url": "https://github.com/toa-io/toa/issues"
|
|
17
|
+
},
|
|
18
|
+
"engines": {
|
|
19
|
+
"node": ">= 12.0.0"
|
|
20
|
+
},
|
|
21
|
+
"publishConfig": {
|
|
22
|
+
"access": "public"
|
|
23
|
+
},
|
|
24
|
+
"main": "src/index.js",
|
|
25
|
+
"scripts": {
|
|
26
|
+
"test": "echo \"Error: run tests from root\" && exit 1"
|
|
27
|
+
},
|
|
28
|
+
"peerDependencies": {
|
|
29
|
+
"@toa.io/core": "^0.0.0",
|
|
30
|
+
"@toa.io/gears": "^0.0.0"
|
|
31
|
+
},
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"@babel/parser": "7.16.2",
|
|
34
|
+
"fast-glob": "3.2.7"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"clone-deep": "4.0.1"
|
|
38
|
+
},
|
|
39
|
+
"gitHead": "632df6cead03909ad2cfb8852e178914ac4ab5d2"
|
|
40
|
+
}
|
package/src/context.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { Connector } = require('@toa.io/core')
|
|
4
|
+
const { underlay } = require('@toa.io/gears')
|
|
5
|
+
|
|
6
|
+
class Context extends Connector {
|
|
7
|
+
#context
|
|
8
|
+
|
|
9
|
+
constructor (context) {
|
|
10
|
+
super()
|
|
11
|
+
|
|
12
|
+
this.#context = context
|
|
13
|
+
|
|
14
|
+
this.depends(context)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
local = underlay(async (endpoint, request) => this.#context.apply(endpoint, request))
|
|
18
|
+
remote = underlay(async (domain, name, endpoint, request) => this.#context.call(domain, name, endpoint, request))
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
exports.Context = Context
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const load = require('../load')
|
|
4
|
+
|
|
5
|
+
const events = async (root) => {
|
|
6
|
+
const modules = await load.events(root)
|
|
7
|
+
return Object.fromEntries(modules.map(([name, module]) => [name, definition(module)]))
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const definition = (module) => ({
|
|
11
|
+
conditioned: module.condition !== undefined,
|
|
12
|
+
subjective: module.payload !== undefined
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
exports.events = events
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { events, event } = require('./events')
|
|
4
|
+
const { receivers, receiver } = require('./receivers')
|
|
5
|
+
const { operation, operations } = require('./operations')
|
|
6
|
+
|
|
7
|
+
exports.event = event
|
|
8
|
+
exports.events = events
|
|
9
|
+
exports.receiver = receiver
|
|
10
|
+
exports.receivers = receivers
|
|
11
|
+
exports.operation = operation
|
|
12
|
+
exports.operations = operations
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const parser = require('@babel/parser')
|
|
4
|
+
const { merge } = require('@toa.io/gears')
|
|
5
|
+
|
|
6
|
+
const definition = (module) => {
|
|
7
|
+
const definition = {}
|
|
8
|
+
|
|
9
|
+
if (typeof module.transition === 'function') definition.type = 'transition'
|
|
10
|
+
if (typeof module.observation === 'function') definition.type = 'observation'
|
|
11
|
+
if (typeof module.assignment === 'function') definition.type = 'assignment'
|
|
12
|
+
|
|
13
|
+
if (definition.type === undefined) {
|
|
14
|
+
throw new Error('Operation must export either transition, observation or assignment function')
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const func = module[definition.type]
|
|
18
|
+
const meta = parse(func)
|
|
19
|
+
|
|
20
|
+
return merge(definition, meta)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const parse = (func) => {
|
|
24
|
+
const ast = parser.parse(func.toString())
|
|
25
|
+
|
|
26
|
+
return node(ast.program.body[0])
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function node (node) {
|
|
30
|
+
if (node.type === 'ExpressionStatement') node = node.expression
|
|
31
|
+
if (node.async !== true) { throw new Error('Operation must export async function') }
|
|
32
|
+
|
|
33
|
+
const result = {}
|
|
34
|
+
|
|
35
|
+
if (node.params.length > 1) result.subject = node.params[1]?.name
|
|
36
|
+
|
|
37
|
+
return result
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
exports.definition = definition
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { definition } = require('./operations/definition')
|
|
4
|
+
const load = require('../load')
|
|
5
|
+
|
|
6
|
+
const operations = async (root) => {
|
|
7
|
+
const modules = await load.operations(root)
|
|
8
|
+
|
|
9
|
+
return Object.fromEntries(modules.map(([name, module]) => [name, definition(module)]))
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
exports.operations = operations
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const load = require('../load')
|
|
4
|
+
|
|
5
|
+
const receiver = async (root, label) => {
|
|
6
|
+
const module = await load.receiver(root, label)
|
|
7
|
+
return definition(module)
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const receivers = async (root) => {
|
|
11
|
+
const modules = await load.receivers(root)
|
|
12
|
+
return Object.fromEntries(modules.map(([name, module]) => [name, definition(module)]))
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const definition = (module) => ({
|
|
16
|
+
conditioned: module.condition !== undefined,
|
|
17
|
+
adaptive: module.request !== undefined
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
exports.receiver = receiver
|
|
21
|
+
exports.receivers = receivers
|
package/src/event.js
ADDED
package/src/factory.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const load = require('./load')
|
|
4
|
+
const { Operation } = require('./operation')
|
|
5
|
+
const { Event } = require('./event')
|
|
6
|
+
const { Receiver } = require('./receiver')
|
|
7
|
+
const { Context } = require('./context')
|
|
8
|
+
|
|
9
|
+
class Factory {
|
|
10
|
+
operation (root, name, context) {
|
|
11
|
+
const operation = load.operation(root, name)
|
|
12
|
+
const ctx = new Context(context)
|
|
13
|
+
|
|
14
|
+
return new Operation(operation, ctx)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
event (root, label) {
|
|
18
|
+
const event = load.event(root, label)
|
|
19
|
+
return new Event(event)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
receiver (root, label) {
|
|
23
|
+
const receiver = load.receiver(root, label)
|
|
24
|
+
return new Receiver(receiver)
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
exports.Factory = Factory
|
package/src/index.js
ADDED
package/src/load.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { basename, resolve } = require('path')
|
|
4
|
+
const glob = require('fast-glob')
|
|
5
|
+
|
|
6
|
+
const operation = (root, name) => load(root, OPERATIONS_DIRECTORY, name)
|
|
7
|
+
const event = (root, name) => load(root, EVENTS_DIRECTORY, name)
|
|
8
|
+
const receiver = (root, name) => load(root, RECEIVERS_DIRECTORY, name)
|
|
9
|
+
|
|
10
|
+
const scan = (directory) => async (root) => {
|
|
11
|
+
const pattern = resolve(root, directory, '*' + EXTENSION)
|
|
12
|
+
const modules = await glob(pattern)
|
|
13
|
+
|
|
14
|
+
return modules.map((module) => [basename(module, EXTENSION), require(module)])
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const load = (root, directory, name) => require(resolve(root, directory, name + EXTENSION))
|
|
18
|
+
|
|
19
|
+
const EXTENSION = '.js'
|
|
20
|
+
const EVENTS_DIRECTORY = 'events'
|
|
21
|
+
const RECEIVERS_DIRECTORY = 'receivers'
|
|
22
|
+
const OPERATIONS_DIRECTORY = 'operations'
|
|
23
|
+
|
|
24
|
+
exports.operation = operation
|
|
25
|
+
exports.event = event
|
|
26
|
+
exports.receiver = receiver
|
|
27
|
+
exports.operations = scan(OPERATIONS_DIRECTORY)
|
|
28
|
+
exports.events = scan(EVENTS_DIRECTORY)
|
|
29
|
+
exports.receivers = scan(RECEIVERS_DIRECTORY)
|
package/src/operation.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { Connector } = require('@toa.io/core')
|
|
4
|
+
|
|
5
|
+
class Operation extends Connector {
|
|
6
|
+
#operation
|
|
7
|
+
#context
|
|
8
|
+
|
|
9
|
+
constructor (operation, context) {
|
|
10
|
+
super()
|
|
11
|
+
|
|
12
|
+
this.#operation = operation.transition || operation.observation || operation.assignment
|
|
13
|
+
this.#context = context
|
|
14
|
+
|
|
15
|
+
this.depends(context)
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
async run (input, state) {
|
|
19
|
+
return this.#operation(input, state, this.#context)
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
exports.Operation = Operation
|
package/src/receiver.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
class Receiver {
|
|
4
|
+
#receiver
|
|
5
|
+
|
|
6
|
+
constructor (receiver) {
|
|
7
|
+
this.#receiver = receiver
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
condition = async (...args) => this.#receiver.condition(...args)
|
|
11
|
+
request = async (...args) => this.#receiver.request(...args)
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
exports.Receiver = Receiver
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { Connector } = require('@toa.io/core')
|
|
4
|
+
|
|
5
|
+
const { Operation } = require('../src/operation')
|
|
6
|
+
const fixtures = require('./operation.fixtures')
|
|
7
|
+
|
|
8
|
+
let operation
|
|
9
|
+
|
|
10
|
+
beforeEach(() => {
|
|
11
|
+
operation = new Operation(fixtures.operation, fixtures.context)
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
it('should extend Connector', () => {
|
|
15
|
+
expect(Operation.prototype).toBeInstanceOf(Connector)
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
it('should run algorithm', async () => {
|
|
19
|
+
await operation.run({ input: 1 })
|
|
20
|
+
|
|
21
|
+
expect(fixtures.operation.observation).toHaveBeenCalled()
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
it('should pass input, state, context', async () => {
|
|
25
|
+
const input = { a: 1 }
|
|
26
|
+
const state = { b: 2 }
|
|
27
|
+
|
|
28
|
+
await operation.run(input, state)
|
|
29
|
+
|
|
30
|
+
expect(fixtures.operation.observation).toHaveBeenCalledWith(input, state, fixtures.context)
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
it('should return output', async () => {
|
|
34
|
+
const reply = await operation.run()
|
|
35
|
+
const result = await fixtures.operation.observation.mock.results[0].value
|
|
36
|
+
|
|
37
|
+
expect(reply).toBeDefined()
|
|
38
|
+
expect(reply).toStrictEqual(result)
|
|
39
|
+
})
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { definition } = require('../src/define/operations/definition')
|
|
4
|
+
|
|
5
|
+
it('should throw if algorithm is not async function', () => {
|
|
6
|
+
const module = { observation: () => null }
|
|
7
|
+
expect(() => definition(module)).toThrow(/must export async function/)
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
describe('type', () => {
|
|
11
|
+
it('should return type', () => {
|
|
12
|
+
async function observe () {}
|
|
13
|
+
|
|
14
|
+
const observation = { observation: observe }
|
|
15
|
+
const transition = { transition: async () => null }
|
|
16
|
+
|
|
17
|
+
const o = definition(observation)
|
|
18
|
+
const t = definition(transition)
|
|
19
|
+
|
|
20
|
+
expect(o.type).toBe('observation')
|
|
21
|
+
expect(t.type).toBe('transition')
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
it('should throw on incorrect export', () => {
|
|
25
|
+
expect(() => definition({ _: async () => null }))
|
|
26
|
+
.toThrow(/transition, observation or assignment/)
|
|
27
|
+
|
|
28
|
+
expect(() => definition({ observation: 1 })).toThrow(/function/)
|
|
29
|
+
})
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
describe('subject', () => {
|
|
33
|
+
it('should return subject', () => {
|
|
34
|
+
const entity = { observation: async (input, entity) => Object.assign(entity, input) }
|
|
35
|
+
const set = { observation: async (input, set) => Object.assign(set, input) }
|
|
36
|
+
|
|
37
|
+
expect(definition(entity)).toMatchObject({ subject: 'entity' })
|
|
38
|
+
expect(definition(set)).toMatchObject({ subject: 'set' })
|
|
39
|
+
})
|
|
40
|
+
})
|