@toa.io/bridges.node 0.6.0 → 0.7.0-dev.1

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@toa.io/bridges.node",
3
- "version": "0.6.0",
3
+ "version": "0.7.0-dev.1",
4
4
  "description": "Toa Node Bridge (inproc)",
5
5
  "homepage": "https://toa.io",
6
6
  "author": {
@@ -26,13 +26,13 @@
26
26
  "test": "echo \"Error: run tests from root\" && exit 1"
27
27
  },
28
28
  "dependencies": {
29
- "@toa.io/core": "0.6.0",
30
- "@toa.io/filesystem": "0.6.0",
31
- "@toa.io/generic": "0.6.0",
29
+ "@toa.io/core": "0.7.0-dev.1",
30
+ "@toa.io/filesystem": "0.7.0-dev.1",
31
+ "@toa.io/generic": "0.7.0-dev.1",
32
32
  "fast-glob": "3.2.7"
33
33
  },
34
34
  "devDependencies": {
35
35
  "clone-deep": "4.0.1"
36
36
  },
37
- "gitHead": "a807d9906db194f5851613a1ead5450d277f4939"
37
+ "gitHead": "1ebdef6733499671422b624ee4e11ab4aa5dd21a"
38
38
  }
package/src/context.js CHANGED
@@ -3,6 +3,8 @@
3
3
  const { Connector } = require('@toa.io/core')
4
4
  const { underlay } = require('@toa.io/generic')
5
5
 
6
+ const shortcuts = require('./shortcuts')
7
+
6
8
  /**
7
9
  * @implements {toa.node.Context}
8
10
  */
@@ -48,39 +50,11 @@ class Context extends Connector {
48
50
 
49
51
  map[aspect.name] = aspect.invoke.bind(aspect)
50
52
 
51
- // well-known aspects
52
- if (aspect.name === 'configuration') this.#configuration(aspect)
53
- if (aspect.name === 'origins') this.origins = this.#origins(aspect)
53
+ if (aspect.name in shortcuts) shortcuts[aspect.name](this, aspect)
54
54
  }
55
55
 
56
56
  return map
57
57
  }
58
-
59
- /**
60
- * @param {toa.core.extensions.Aspect} aspect
61
- */
62
- #origins (aspect) {
63
- return underlay(async (segs, args) => {
64
- if (segs.length < 2) throw new Error(`Origins call requires at least 2 arguments, ${segs.length} given`)
65
-
66
- const name = segs.shift()
67
- const method = segs.pop().toUpperCase()
68
- const path = segs.join('/')
69
- const request = { method, ...args[0] }
70
- const options = args[1]
71
-
72
- return await aspect.invoke(name, path, request, options)
73
- })
74
- }
75
-
76
- /**
77
- * @param {toa.core.extensions.Aspect} aspect
78
- */
79
- #configuration (aspect) {
80
- Object.defineProperty(this, 'configuration', {
81
- get: () => aspect.invoke()
82
- })
83
- }
84
58
  }
85
59
 
86
60
  exports.Context = Context
@@ -0,0 +1,10 @@
1
+ 'use strict'
2
+
3
+ /** @type {toa.node.shortcut} */
4
+ const configuration = (context, aspect) => {
5
+ Object.defineProperty(context, 'configuration', {
6
+ get: () => aspect.invoke()
7
+ })
8
+ }
9
+
10
+ exports.configuration = configuration
@@ -0,0 +1,9 @@
1
+ 'use strict'
2
+
3
+ const { origins } = require('./origins')
4
+ const { configuration } = require('./configuration')
5
+ const { state } = require('./state')
6
+
7
+ exports.origins = origins
8
+ exports.configuration = configuration
9
+ exports.state = state
@@ -0,0 +1,20 @@
1
+ 'use strict'
2
+
3
+ const { underlay } = require('@toa.io/generic')
4
+
5
+ /** @type {toa.node.shortcut} */
6
+ const origins = (context, aspect) => {
7
+ context.origins = underlay(async (segs, args) => {
8
+ if (segs.length < 2) throw new Error(`Origins call requires at least 2 arguments, ${segs.length} given`)
9
+
10
+ const name = segs.shift()
11
+ const method = segs.pop().toUpperCase()
12
+ const path = segs.join('/')
13
+ const request = { method, ...args[0] }
14
+ const options = args[1]
15
+
16
+ return await aspect.invoke(name, path, request, options)
17
+ })
18
+ }
19
+
20
+ exports.origins = origins
@@ -0,0 +1,54 @@
1
+ 'use strict'
2
+
3
+ const { generate } = require('@toa.io/generic')
4
+
5
+ /** @type {toa.node.shortcut} */
6
+ const state = (context, aspect) => {
7
+ context.state = generate((segs, value) => {
8
+ if (value === undefined) return get(aspect, segs)
9
+ else set(aspect, segs, value)
10
+ })
11
+ }
12
+
13
+ /**
14
+ * @param {toa.core.extensions.Aspect} aspect
15
+ * @param {string[]} segs
16
+ * @return {any}
17
+ */
18
+ function get (aspect, segs) {
19
+ let cursor = aspect.invoke()
20
+
21
+ for (const seg of segs) cursor = cursor[seg]
22
+
23
+ return cursor
24
+ }
25
+
26
+ /**
27
+ * @param {toa.core.extensions.Aspect} aspect
28
+ * @param {string[]} segs
29
+ * @param {any} value
30
+ */
31
+ function set (aspect, segs, value) {
32
+ const object = build(segs, value)
33
+
34
+ aspect.invoke(object)
35
+ }
36
+
37
+ /**
38
+ * @param {string[]} segs
39
+ * @param {any} value
40
+ */
41
+ function build (segs, value) {
42
+ const object = {}
43
+ let cursor = object
44
+ const key = segs.pop()
45
+
46
+ for (const seg of segs) cursor = cursor[seg] = {}
47
+
48
+ if (value !== undefined) cursor[key] = value
49
+ else cursor[key] = {}
50
+
51
+ return object
52
+ }
53
+
54
+ exports.state = state
@@ -0,0 +1,16 @@
1
+ 'use strict'
2
+
3
+ const context = {
4
+ apply: jest.fn(),
5
+ call: jest.fn(),
6
+ aspects: [
7
+ {
8
+ name: 'state',
9
+ invoke: jest.fn()
10
+ }
11
+ ],
12
+ link: jest.fn(),
13
+ connect: jest.fn()
14
+ }
15
+
16
+ exports.context = context
@@ -0,0 +1,40 @@
1
+ 'use strict'
2
+
3
+ const fixtures = require('./context.state.fixtures')
4
+ const { Context } = require('../src/context')
5
+ const { generate } = require('randomstring')
6
+
7
+ const state = fixtures.context.aspects[0]
8
+
9
+ /** @type {Context} */
10
+ let context
11
+
12
+ beforeEach(async () => {
13
+ jest.clearAllMocks()
14
+
15
+ context = new Context(fixtures.context)
16
+
17
+ await context.connect()
18
+ })
19
+
20
+ it('should expose aspect', async () => {
21
+ expect(context.aspects.state).toBeDefined()
22
+ })
23
+
24
+ it('should set value', async () => {
25
+ context.state.a = 1
26
+
27
+ expect(state.invoke).toHaveBeenCalledWith({ a: 1 })
28
+ })
29
+
30
+ it('should get value', async () => {
31
+ const b = generate()
32
+
33
+ state.invoke.mockImplementation(() => ({ a: { b } }))
34
+
35
+ const value = context.state.a.b
36
+
37
+ expect(value).toStrictEqual(b)
38
+
39
+ state.invoke.mockClear()
40
+ })
@@ -1,22 +1,22 @@
1
1
  import { Underlay } from '@toa.io/generic/types'
2
- import { Connector } from "@toa.io/core/types";
2
+ import { Connector } from '@toa.io/core/types'
3
+ import { Aspect } from '@toa.io/core/types/extensions'
3
4
 
4
5
  declare namespace toa.node {
5
6
 
6
- interface Aspectes {
7
- [key: string]: Function
8
- }
9
-
10
7
  interface Context extends Connector {
11
8
  local: Underlay
12
9
  remote: Underlay
13
- aspects: Aspectes
10
+ aspects: Record<string, Function>
14
11
 
15
12
  // known extensions
16
13
  origins?: Underlay
17
- configuration?: Underlay
14
+ configuration?: object
15
+ state?: object
18
16
  }
19
17
 
18
+ type shortcut = (context: Context, aspect: Aspect) => void
19
+
20
20
  }
21
21
 
22
22
  export type Context = toa.node.Context