@toa.io/bridges.node 1.0.0-alpha.6 → 1.0.0-alpha.63

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.6",
3
+ "version": "1.0.0-alpha.63",
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": ">= 18.0.0"
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
- "@toa.io/core": "1.0.0-alpha.6",
30
- "@toa.io/filesystem": "1.0.0-alpha.6",
31
- "@toa.io/generic": "1.0.0-alpha.6",
29
+ "@babel/parser": "7.24.7",
30
+ "@toa.io/core": "1.0.0-alpha.63",
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": "f28d629a9477646e267a8af8479cc1bb10d62c80"
39
+ "gitHead": "9acf69d3135ea69bb9080c4f2f654d119f7b8a82"
39
40
  }
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
@@ -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(/** @type {toa.core.extensions.Aspect[]} */ this.#context.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) shortcuts[aspect.name](this, aspect)
47
+ if (aspect.name in shortcuts)
48
+ shortcuts[aspect.name](this, aspect)
58
49
  }
59
50
 
60
51
  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,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/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,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,17 @@
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
+
15
+ const CHANNELS = ['debug', 'info', 'warn', 'error']
16
+
17
+ 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
@@ -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
- }