@toa.io/bindings.loop 1.0.0-alpha.29 → 1.0.0-alpha.292
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/CHANGELOG.md +39 -0
- package/package.json +5 -3
- package/src/consumer.js +4 -8
- package/src/factory.js +5 -9
- package/src/index.js +1 -6
- package/src/producer.js +8 -11
- package/test/fixtures.js +7 -11
- package/test/loop.test.js +88 -0
- package/test/test.js +0 -68
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# Change Log
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
|
+
|
|
6
|
+
# [1.0.0-alpha.292](https://github.com/toa-io/toa/compare/v1.0.0-alpha.291...v1.0.0-alpha.292) (2026-09-07)
|
|
7
|
+
|
|
8
|
+
### Features
|
|
9
|
+
|
|
10
|
+
* **definitions:** what a package declares is read from one package ([7052341](https://github.com/toa-io/toa/commit/70523411b5d9e2b204c999aa02365173f3bb518d))
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
# [1.0.0-alpha.291](https://github.com/toa-io/toa/compare/v1.0.0-alpha.290...v1.0.0-alpha.291) (2026-09-07)
|
|
14
|
+
|
|
15
|
+
**Note:** Version bump only for package @toa.io/bindings.loop
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
# [1.0.0-alpha.289](https://github.com/toa-io/toa/compare/v1.0.0-alpha.288...v1.0.0-alpha.289) (2026-09-07)
|
|
22
|
+
|
|
23
|
+
**Note:** Version bump only for package @toa.io/bindings.loop
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
# [1.0.0-alpha.288](https://github.com/toa-io/toa/compare/v1.0.0-alpha.287...v1.0.0-alpha.288) (2026-09-06)
|
|
30
|
+
|
|
31
|
+
**Note:** Version bump only for package @toa.io/bindings.loop
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
# [1.0.0-alpha.287](https://github.com/toa-io/toa/compare/v1.0.0-alpha.286...v1.0.0-alpha.287) (2026-09-06)
|
|
38
|
+
|
|
39
|
+
**Note:** Version bump only for package @toa.io/bindings.loop
|
package/package.json
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@toa.io/bindings.loop",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
3
|
+
"version": "1.0.0-alpha.292",
|
|
4
|
+
"type": "module",
|
|
4
5
|
"description": "Toa Loop Binding",
|
|
5
6
|
"author": "temich <tema.gurtovoy@gmail.com>",
|
|
6
7
|
"homepage": "https://github.com/toa-io/toa#readme",
|
|
@@ -19,7 +20,8 @@
|
|
|
19
20
|
"test": "echo \"Error: run tests from root\" && exit 1"
|
|
20
21
|
},
|
|
21
22
|
"dependencies": {
|
|
22
|
-
"@toa.io/
|
|
23
|
+
"@toa.io/core": "1.0.0-alpha.292",
|
|
24
|
+
"@toa.io/generic": "1.0.0-alpha.292"
|
|
23
25
|
},
|
|
24
|
-
"gitHead": "
|
|
26
|
+
"gitHead": "0f6aa3ad9dbec86bb632a7975ea61d1dde1d8fc5"
|
|
25
27
|
}
|
package/src/consumer.js
CHANGED
|
@@ -1,13 +1,11 @@
|
|
|
1
|
-
|
|
1
|
+
import { Connector } from '@toa.io/core'
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
class Consumer extends Connector {
|
|
3
|
+
export class Consumer extends Connector {
|
|
6
4
|
#bindings
|
|
7
5
|
#locator
|
|
8
6
|
#endpoint
|
|
9
7
|
|
|
10
|
-
constructor
|
|
8
|
+
constructor(bindings, locator, endpoint) {
|
|
11
9
|
super()
|
|
12
10
|
|
|
13
11
|
this.#bindings = bindings
|
|
@@ -15,12 +13,10 @@ class Consumer extends Connector {
|
|
|
15
13
|
this.#endpoint = endpoint
|
|
16
14
|
}
|
|
17
15
|
|
|
18
|
-
async request
|
|
16
|
+
async request(request) {
|
|
19
17
|
const invoke = this.#bindings[this.#locator.id]?.[this.#endpoint]
|
|
20
18
|
|
|
21
19
|
if (invoke === undefined) return false
|
|
22
20
|
else return invoke(request)
|
|
23
21
|
}
|
|
24
22
|
}
|
|
25
|
-
|
|
26
|
-
exports.Consumer = Consumer
|
package/src/factory.js
CHANGED
|
@@ -1,18 +1,14 @@
|
|
|
1
|
-
|
|
1
|
+
import { Producer } from './producer.js'
|
|
2
|
+
import { Consumer } from './consumer.js'
|
|
2
3
|
|
|
3
|
-
|
|
4
|
-
const { Consumer } = require('./consumer')
|
|
5
|
-
|
|
6
|
-
class Factory {
|
|
4
|
+
export class Factory {
|
|
7
5
|
#bindings = {}
|
|
8
6
|
|
|
9
|
-
producer
|
|
7
|
+
producer(locator, endpoints, producer) {
|
|
10
8
|
return new Producer(this.#bindings, locator, endpoints, producer)
|
|
11
9
|
}
|
|
12
10
|
|
|
13
|
-
consumer
|
|
11
|
+
consumer(locator, endpoint) {
|
|
14
12
|
return new Consumer(this.#bindings, locator, endpoint)
|
|
15
13
|
}
|
|
16
14
|
}
|
|
17
|
-
|
|
18
|
-
exports.Factory = Factory
|
package/src/index.js
CHANGED
package/src/producer.js
CHANGED
|
@@ -1,13 +1,11 @@
|
|
|
1
|
-
|
|
1
|
+
import { Connector } from '@toa.io/core'
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
class Producer extends Connector {
|
|
3
|
+
export class Producer extends Connector {
|
|
6
4
|
#binding
|
|
7
5
|
#endpoints
|
|
8
6
|
#producer
|
|
9
7
|
|
|
10
|
-
constructor
|
|
8
|
+
constructor(bindings, locator, endpoints, producer) {
|
|
11
9
|
super()
|
|
12
10
|
|
|
13
11
|
if (bindings[locator.id] === undefined) bindings[locator.id] = {}
|
|
@@ -19,19 +17,18 @@ class Producer extends Connector {
|
|
|
19
17
|
this.depends(producer)
|
|
20
18
|
}
|
|
21
19
|
|
|
22
|
-
async open
|
|
20
|
+
async open() {
|
|
23
21
|
this.#endpoints.forEach((endpoint) => this.#operation(endpoint))
|
|
24
22
|
}
|
|
25
23
|
|
|
26
|
-
async close
|
|
24
|
+
async close() {
|
|
27
25
|
this.#endpoints.forEach((endpoint) => delete this.#binding[endpoint])
|
|
28
26
|
}
|
|
29
27
|
|
|
30
|
-
#operation
|
|
31
|
-
if (this.#binding[endpoint] !== undefined)
|
|
28
|
+
#operation(endpoint) {
|
|
29
|
+
if (this.#binding[endpoint] !== undefined)
|
|
30
|
+
throw new Error(`Loop binding endpoint '${endpoint}' already exists`)
|
|
32
31
|
|
|
33
32
|
this.#binding[endpoint] = async (request) => this.#producer.invoke(endpoint, request)
|
|
34
33
|
}
|
|
35
34
|
}
|
|
36
|
-
|
|
37
|
-
exports.Producer = Producer
|
package/test/fixtures.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
|
|
1
|
+
import { mock } from 'node:test'
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
import { generate } from 'randomstring'
|
|
4
4
|
|
|
5
|
-
const component = {
|
|
5
|
+
export const component = {
|
|
6
6
|
locator: {
|
|
7
7
|
id: 'foo.bar',
|
|
8
8
|
operations: [
|
|
@@ -17,21 +17,17 @@ const component = {
|
|
|
17
17
|
}
|
|
18
18
|
]
|
|
19
19
|
},
|
|
20
|
-
invoke:
|
|
20
|
+
invoke: mock.fn(async () => generate()),
|
|
21
21
|
link: () => null,
|
|
22
22
|
connect: () => null,
|
|
23
23
|
disconnect: () => null
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
-
const endpoints = ['get', 'add', 'discover']
|
|
26
|
+
export const endpoints = ['get', 'add', 'discover']
|
|
27
27
|
|
|
28
|
-
const exposition = {
|
|
28
|
+
export const exposition = {
|
|
29
29
|
locator: {
|
|
30
30
|
id: 'foo.bar'
|
|
31
31
|
},
|
|
32
|
-
invoke:
|
|
32
|
+
invoke: mock.fn(async () => generate())
|
|
33
33
|
}
|
|
34
|
-
|
|
35
|
-
exports.component = component
|
|
36
|
-
exports.endpoints = endpoints
|
|
37
|
-
exports.exposition = exposition
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { it, before, after, beforeEach, afterEach } from 'node:test'
|
|
2
|
+
import assert from 'node:assert/strict'
|
|
3
|
+
|
|
4
|
+
import clone from 'clone-deep'
|
|
5
|
+
import { sample } from '@toa.io/generic'
|
|
6
|
+
|
|
7
|
+
import { Factory } from '../src/factory.js'
|
|
8
|
+
import * as fixtures from './fixtures.js'
|
|
9
|
+
|
|
10
|
+
const factory = new Factory()
|
|
11
|
+
const producer = factory.producer(
|
|
12
|
+
fixtures.component.locator,
|
|
13
|
+
fixtures.endpoints,
|
|
14
|
+
fixtures.component
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
let consumer, endpoint
|
|
18
|
+
|
|
19
|
+
before(async () => {
|
|
20
|
+
await producer.connect()
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
after(async () => {
|
|
24
|
+
await producer.disconnect()
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
beforeEach(async () => {
|
|
28
|
+
resetCalls()
|
|
29
|
+
|
|
30
|
+
endpoint = sample(fixtures.endpoints)
|
|
31
|
+
consumer = factory.consumer(fixtures.component.locator, endpoint)
|
|
32
|
+
|
|
33
|
+
await consumer.connect()
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
afterEach(async () => {
|
|
37
|
+
await consumer.disconnect()
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
it('should bind', async () => {
|
|
41
|
+
const r1 = await consumer.request(1)
|
|
42
|
+
const r2 = await consumer.request(2)
|
|
43
|
+
|
|
44
|
+
const calls = fixtures.component.invoke.mock.calls
|
|
45
|
+
|
|
46
|
+
assert.strictEqual(calls.length, 2)
|
|
47
|
+
assert.deepStrictEqual(calls[0].arguments, [endpoint, 1])
|
|
48
|
+
assert.deepStrictEqual(calls[1].arguments, [endpoint, 2])
|
|
49
|
+
|
|
50
|
+
assert.strictEqual(r1, await calls[0].result)
|
|
51
|
+
assert.strictEqual(r2, await calls[1].result)
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
it('should return false if no binding', async () => {
|
|
55
|
+
const consumer = factory.consumer({ id: 'not.existent' }, endpoint)
|
|
56
|
+
const result = await consumer.request()
|
|
57
|
+
|
|
58
|
+
assert.strictEqual(result, false)
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
it('should not depend on initialization order', async () => {
|
|
62
|
+
const component = clone(fixtures.component)
|
|
63
|
+
|
|
64
|
+
component.locator.id = 'other.name'
|
|
65
|
+
|
|
66
|
+
const consumer = factory.consumer(component.locator, endpoint)
|
|
67
|
+
await consumer.connect()
|
|
68
|
+
|
|
69
|
+
assert.strictEqual(await consumer.request(), false)
|
|
70
|
+
|
|
71
|
+
const producer = factory.producer(component.locator, fixtures.endpoints, component)
|
|
72
|
+
await producer.connect()
|
|
73
|
+
|
|
74
|
+
assert.strictEqual(
|
|
75
|
+
await consumer.request(),
|
|
76
|
+
await component.invoke.mock.calls[0].result
|
|
77
|
+
)
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
function resetCalls(target = [fixtures], seen = new Set()) {
|
|
81
|
+
if (target === null || typeof target !== 'object' || seen.has(target)) return
|
|
82
|
+
|
|
83
|
+
seen.add(target)
|
|
84
|
+
|
|
85
|
+
for (const value of Object.values(target))
|
|
86
|
+
if (typeof value === 'function' && value.mock !== undefined) value.mock.resetCalls()
|
|
87
|
+
else resetCalls(value, seen)
|
|
88
|
+
}
|
package/test/test.js
DELETED
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const clone = require('clone-deep')
|
|
4
|
-
const { sample } = require('@toa.io/generic')
|
|
5
|
-
|
|
6
|
-
const { Factory } = require('../src/factory')
|
|
7
|
-
const fixtures = require('./fixtures')
|
|
8
|
-
|
|
9
|
-
const factory = new Factory()
|
|
10
|
-
const producer = factory.producer(fixtures.component.locator, fixtures.endpoints, fixtures.component)
|
|
11
|
-
|
|
12
|
-
let consumer, endpoint
|
|
13
|
-
|
|
14
|
-
beforeAll(async () => {
|
|
15
|
-
await producer.connect()
|
|
16
|
-
})
|
|
17
|
-
|
|
18
|
-
afterAll(async () => {
|
|
19
|
-
await producer.disconnect()
|
|
20
|
-
})
|
|
21
|
-
|
|
22
|
-
beforeEach(async () => {
|
|
23
|
-
jest.clearAllMocks()
|
|
24
|
-
|
|
25
|
-
endpoint = sample(fixtures.endpoints)
|
|
26
|
-
consumer = factory.consumer(fixtures.component.locator, endpoint)
|
|
27
|
-
|
|
28
|
-
await consumer.connect()
|
|
29
|
-
})
|
|
30
|
-
|
|
31
|
-
afterEach(async () => {
|
|
32
|
-
await consumer.disconnect()
|
|
33
|
-
})
|
|
34
|
-
|
|
35
|
-
it('should bind', async () => {
|
|
36
|
-
const r1 = await consumer.request(1)
|
|
37
|
-
const r2 = await consumer.request(2)
|
|
38
|
-
|
|
39
|
-
expect(fixtures.component.invoke).toHaveBeenCalledTimes(2)
|
|
40
|
-
expect(fixtures.component.invoke).toHaveBeenNthCalledWith(1, endpoint, 1)
|
|
41
|
-
expect(fixtures.component.invoke).toHaveBeenNthCalledWith(2, endpoint, 2)
|
|
42
|
-
|
|
43
|
-
expect(r1).toBe(await fixtures.component.invoke.mock.results[0].value)
|
|
44
|
-
expect(r2).toBe(await fixtures.component.invoke.mock.results[1].value)
|
|
45
|
-
})
|
|
46
|
-
|
|
47
|
-
it('should return false if no binding', async () => {
|
|
48
|
-
const consumer = factory.consumer({ id: 'not.existent' }, endpoint)
|
|
49
|
-
const result = await consumer.request()
|
|
50
|
-
|
|
51
|
-
expect(result).toBe(false)
|
|
52
|
-
})
|
|
53
|
-
|
|
54
|
-
it('should not depend on initialization order', async () => {
|
|
55
|
-
const component = clone(fixtures.component)
|
|
56
|
-
|
|
57
|
-
component.locator.id = 'other.name'
|
|
58
|
-
|
|
59
|
-
const consumer = factory.consumer(component.locator, endpoint)
|
|
60
|
-
await consumer.connect()
|
|
61
|
-
|
|
62
|
-
expect(await consumer.request()).toBe(false)
|
|
63
|
-
|
|
64
|
-
const producer = factory.producer(component.locator, fixtures.endpoints, component)
|
|
65
|
-
await producer.connect()
|
|
66
|
-
|
|
67
|
-
expect(await consumer.request()).toBe(await component.invoke.mock.results[0].value)
|
|
68
|
-
})
|