@toa.io/bridges.node 1.0.0-alpha.5 → 1.0.0-alpha.50

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": "1.0.0-alpha.5",
3
+ "version": "1.0.0-alpha.50",
4
4
  "description": "Toa Node Bridge (inproc)",
5
5
  "homepage": "https://toa.io",
6
6
  "author": {
@@ -26,14 +26,14 @@
26
26
  "test": "echo \"Error: run tests from root\" && exit 1"
27
27
  },
28
28
  "dependencies": {
29
- "@toa.io/core": "1.0.0-alpha.5",
30
- "@toa.io/filesystem": "1.0.0-alpha.5",
31
- "@toa.io/generic": "1.0.0-alpha.5",
29
+ "@toa.io/core": "1.0.0-alpha.50",
30
+ "@toa.io/filesystem": "1.0.0-alpha.50",
31
+ "@toa.io/generic": "1.0.0-alpha.50",
32
32
  "fast-glob": "3.2.7",
33
33
  "matchacho": "0.3.5"
34
34
  },
35
35
  "devDependencies": {
36
36
  "clone-deep": "4.0.1"
37
37
  },
38
- "gitHead": "1e4bb4ac28a6dddff1f4b8c5be7224fcdc47b847"
38
+ "gitHead": "514269d4b481150c8cd0db2e5971da6e9fe80ad9"
39
39
  }
package/readme.md CHANGED
@@ -72,7 +72,7 @@ it defines operation's `scope`.
72
72
 
73
73
  ```javascript
74
74
  class ObjectTransitionFactory {
75
- create () {
75
+ async create () {
76
76
  // ...
77
77
  }
78
78
  }
@@ -24,6 +24,10 @@ class Runner extends Connector {
24
24
  await this.#algorithm.mount?.(this.#context)
25
25
  }
26
26
 
27
+ async close () {
28
+ await this.#algorithm.unmount?.()
29
+ }
30
+
27
31
  async execute (input, state) {
28
32
  const reply = await this.#algorithm.execute(input, state)
29
33
 
package/src/context.js CHANGED
@@ -54,7 +54,8 @@ class Context extends Connector {
54
54
 
55
55
  map[aspect.name] = aspect.invoke.bind(aspect)
56
56
 
57
- if (aspect.name in shortcuts) shortcuts[aspect.name](this, aspect)
57
+ if (aspect.name in shortcuts)
58
+ shortcuts[aspect.name](this, aspect)
58
59
  }
59
60
 
60
61
  return map
@@ -1,4 +1,4 @@
1
1
  'use strict'
2
2
 
3
3
  exports.types = ['transition', 'observation', 'assignment', 'computation', 'effect']
4
- exports.scopes = ['object', 'objects', 'changeset']
4
+ exports.scopes = ['object', 'objects', 'changeset', 'stream']
@@ -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
- * @param {string} name
32
- * @returns {string}
33
- */
34
- const scope = (name) => scopes.includes(name) ? name : undefined
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,7 +8,7 @@ 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
13
  const ctx = new Context(context)
14
14
 
@@ -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/index.js CHANGED
@@ -1,8 +1,6 @@
1
1
  'use strict'
2
2
 
3
3
  const { Factory } = require('./factory')
4
- const { version } = require('./version')
5
4
 
6
5
  exports.define = require('./define')
7
6
  exports.Factory = Factory
8
- exports.version = version
@@ -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
- const amqp = (context, aspect) => {
7
+ function amqp (context, aspect) {
7
8
  context.amqp = underlay(async (segs, args) => {
8
- if (segs.length !== 2) throw new Error(`AMQP aspect call should have 2 segments [${segs.join(', ')}] given`)
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
 
@@ -1,7 +1,7 @@
1
1
  'use strict'
2
2
 
3
3
  /** @type {toa.node.shortcut} */
4
- const configuration = (context, aspect) => {
4
+ function configuration (context, aspect) {
5
5
  Object.defineProperty(context, 'configuration', {
6
6
  get: () => aspect.invoke()
7
7
  })
@@ -3,7 +3,7 @@
3
3
  const { underlay } = require('@toa.io/generic')
4
4
 
5
5
  /** @type {toa.node.shortcut} */
6
- const http = (context, aspect) => {
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
 
@@ -6,6 +6,7 @@ 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')
9
10
 
10
11
  exports.http = http
11
12
  exports.amqp = amqp
@@ -13,3 +14,4 @@ exports.configuration = configuration
13
14
  exports.state = state
14
15
  exports.stash = stash
15
16
  exports.storages = storages
17
+ exports.pubsub = pubsub
@@ -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
@@ -3,7 +3,7 @@
3
3
  const { underlay } = require('@toa.io/generic')
4
4
 
5
5
  /** @type {toa.node.shortcut} */
6
- const stash = (context, aspect) => {
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`)
@@ -3,7 +3,7 @@
3
3
  const { generate } = require('@toa.io/generic')
4
4
 
5
5
  /** @type {toa.node.shortcut} */
6
- const state = (context, aspect) => {
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: undefined })
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: undefined })
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 not define default scope', async () => {
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).toBeUndefined()
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
 
@@ -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>
package/src/version.js DELETED
@@ -1,15 +0,0 @@
1
- 'use strict'
2
-
3
- const { join } = require('node:path')
4
-
5
- exports.version = function(manifest) {
6
- const pkgPath = join(manifest.path, 'package.json')
7
-
8
- try {
9
- const pkg = require(pkgPath)
10
-
11
- return pkg.version
12
- } catch {
13
- return undefined
14
- }
15
- }