@toa.io/extensions.origins 0.8.0-dev.1 → 0.8.0-dev.2

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/extensions.origins",
3
- "version": "0.8.0-dev.1",
3
+ "version": "0.8.0-dev.2",
4
4
  "description": "Toa Origins",
5
5
  "author": "temich <tema.gurtovoy@gmail.com>",
6
6
  "homepage": "https://github.com/toa-io/toa#readme",
@@ -19,15 +19,15 @@
19
19
  "test": "echo \"Error: run tests from root\" && exit 1"
20
20
  },
21
21
  "dependencies": {
22
- "@toa.io/core": "0.8.0-dev.1",
23
- "@toa.io/generic": "0.8.0-dev.1",
24
- "@toa.io/schemas": "0.8.0-dev.1",
25
- "@toa.io/yaml": "0.7.2-dev.2",
22
+ "@toa.io/core": "0.8.0-dev.2",
23
+ "@toa.io/generic": "0.8.0-dev.2",
24
+ "@toa.io/schemas": "0.8.0-dev.2",
25
+ "@toa.io/yaml": "0.7.2-dev.3",
26
26
  "comq": "0.6.0",
27
27
  "node-fetch": "2.6.7"
28
28
  },
29
29
  "devDependencies": {
30
30
  "@types/node-fetch": "2.6.2"
31
31
  },
32
- "gitHead": "9bb2f372c556440ee3e2a42e10eb415220ba1cb0"
32
+ "gitHead": "85a6e6ce4f5d90af087507e3db65b94f32e9a818"
33
33
  }
package/readme.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Toa Origins
2
2
 
3
- `origins` extension enables external communications over supported protocols (HTTP and AMQP).
3
+ Enables external communications over supported protocols (HTTP and AMQP).
4
4
 
5
5
  ## TL;DR
6
6
 
@@ -37,6 +37,21 @@ origins:
37
37
  ## Manifest
38
38
 
39
39
  `origins` manifest is an object conforming declaring origin names as keys an origin URLs as values.
40
+ Component's `origins` manifest can be overridden by the Context `origins` annotation.
41
+
42
+ ### Sharded Connections
43
+
44
+ Origin value may contain [shards](/libraries/generic/readme.md#shards) placeholders.
45
+
46
+ ### Environment Variables
47
+
48
+ Origin value may contain environment variable placeholders.
49
+
50
+ ```yaml
51
+ # manifest.toa.yaml
52
+ origins:
53
+ foo@dev: stage${STAGE_NUMBER}.stages.com
54
+ ```
40
55
 
41
56
  ## HTTP Aspect
42
57
 
@@ -1,5 +1,6 @@
1
1
  'use strict'
2
2
 
3
+ const { remap, echo, shards } = require('@toa.io/generic')
3
4
  const schemas = require('./schemas')
4
5
  const protocols = require('./protocols')
5
6
 
@@ -10,16 +11,28 @@ const protocols = require('./protocols')
10
11
  function manifest (manifest) {
11
12
  if (manifest === null) return {}
12
13
 
13
- schemas.manifest.validate(manifest)
14
+ manifest = remap(manifest, echo)
15
+ validate(manifest)
14
16
 
15
- for (const uri of Object.values(manifest)) {
16
- const protocol = new URL(uri).protocol
17
- const supported = protocols.find((provider) => provider.protocols.includes(protocol))
17
+ for (const url of Object.values(manifest)) {
18
+ const supported = protocols.find((provider) => supports(provider, url))
18
19
 
19
- if (supported === undefined) throw new Error(`'${protocol}' protocol is not supported`)
20
+ if (supported === undefined) throw new Error(`'${url}' protocol is not supported`)
20
21
  }
21
22
 
22
23
  return manifest
23
24
  }
24
25
 
26
+ /**
27
+ * @param {toa.origins.Manifest} manifest
28
+ */
29
+ function validate (manifest) {
30
+ manifest = remap(manifest, (value) => shards(value)[0])
31
+ schemas.manifest.validate(manifest)
32
+ }
33
+
34
+ function supports (provider, url) {
35
+ return provider.protocols.findIndex((protocol) => url.substring(0, protocol.length) === protocol) !== -1
36
+ }
37
+
25
38
  exports.manifest = manifest
@@ -55,3 +55,21 @@ it.each(PROTOCOLS)('should support %s protocol', async (protocol) => {
55
55
 
56
56
  expect(() => manifest(input)).not.toThrow()
57
57
  })
58
+
59
+ it('should handle placeholders', async () => {
60
+ const input = { foo: 'http://${FOO}' + generate() + ':${BAR}/' } // eslint-disable-line no-template-curly-in-string
61
+
62
+ expect(() => manifest(input)).not.toThrow()
63
+ })
64
+
65
+ it('should handle host shards', async () => {
66
+ const input = { foo: 'http://{0-3}' + generate() }
67
+
68
+ expect(() => manifest(input)).not.toThrow()
69
+ })
70
+
71
+ it('should handle port shards', async () => {
72
+ const input = { foo: 'http://' + generate() + ':888{0-9}' }
73
+
74
+ expect(() => manifest(input)).not.toThrow()
75
+ })