@toa.io/extensions.origins 0.20.0-dev.4 → 0.20.0-dev.40

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.
Files changed (43) hide show
  1. package/package.json +18 -11
  2. package/readme.md +70 -67
  3. package/schemas/annotation.cos.yaml +3 -0
  4. package/schemas/manifest.cos.yaml +4 -0
  5. package/source/Factory.ts +88 -0
  6. package/source/annotation.test.ts +150 -0
  7. package/source/annotation.ts +83 -0
  8. package/source/extension.test.ts +161 -0
  9. package/source/extension.ts +60 -0
  10. package/source/index.ts +2 -0
  11. package/source/manifest.test.ts +30 -0
  12. package/source/manifest.ts +11 -0
  13. package/source/protocols/amqp/.test/aspect.fixtures.js +1 -1
  14. package/source/protocols/amqp/.test/mock.comq.js +2 -2
  15. package/source/protocols/amqp/aspect.js +17 -24
  16. package/source/protocols/amqp/deployment.js +8 -2
  17. package/source/protocols/http/.aspect/permissions.js +13 -10
  18. package/source/protocols/http/aspect.js +16 -37
  19. package/source/protocols/index.ts +16 -0
  20. package/tsconfig.json +12 -0
  21. package/source/.credentials.js +0 -14
  22. package/source/.deployment/index.js +0 -5
  23. package/source/.deployment/uris.js +0 -37
  24. package/source/.test/constants.js +0 -3
  25. package/source/.test/deployment.fixtures.js +0 -20
  26. package/source/.test/factory.fixtures.js +0 -13
  27. package/source/deployment.js +0 -41
  28. package/source/deployment.test.js +0 -185
  29. package/source/factory.js +0 -44
  30. package/source/factory.test.js +0 -140
  31. package/source/index.js +0 -9
  32. package/source/manifest.js +0 -41
  33. package/source/manifest.test.js +0 -82
  34. package/source/protocols/amqp/aspect.test.js +0 -119
  35. package/source/protocols/http/.aspect/permissions.test.js +0 -23
  36. package/source/protocols/http/aspect.test.js +0 -220
  37. package/source/protocols/index.js +0 -6
  38. package/source/schemas/annotations.cos.yaml +0 -1
  39. package/source/schemas/index.js +0 -8
  40. package/source/schemas/manifest.cos.yaml +0 -2
  41. package/types/amqp.d.ts +0 -9
  42. package/types/deployment.d.ts +0 -7
  43. package/types/http.d.ts +0 -28
@@ -1,220 +0,0 @@
1
- 'use strict'
2
-
3
- const clone = require('clone-deep')
4
- const { generate } = require('randomstring')
5
- const { random } = require('@toa.io/generic')
6
- const { Connector } = require('@toa.io/core')
7
-
8
- /** @type {string[]} */
9
- const protocols = require('../http/protocols')
10
-
11
- const fixtures = require('./.test/aspect.fixtures')
12
- const mock = fixtures.mock
13
-
14
- jest.mock('node-fetch', () => mock.fetch)
15
-
16
- const { create } = require('./aspect')
17
-
18
- /** @type {toa.origins.http.Aspect} */ let aspect
19
-
20
- beforeEach(() => {
21
- jest.clearAllMocks()
22
-
23
- aspect = create(fixtures.manifest)
24
- })
25
-
26
- it('should be instance of core.Connector', () => {
27
- expect(aspect).toBeInstanceOf(Connector)
28
- })
29
-
30
- it('should have name \'origins\'', () => {
31
- expect(aspect.name).toStrictEqual('http')
32
- })
33
-
34
- describe('invoke', () => {
35
- const path = '/' + generate()
36
- const headers = { [generate().toLowerCase()]: generate() }
37
- const body = generate()
38
-
39
- /** @type {import('node-fetch').RequestInit} */
40
- const request = { method: 'PATCH', headers, body }
41
- const name = 'foo'
42
- const response = { [generate()]: generate() }
43
-
44
- let call
45
- let args
46
- let result
47
-
48
- beforeEach(async () => {
49
- jest.clearAllMocks()
50
- mock.fetch.reset()
51
- mock.fetch.respond(200, response)
52
-
53
- result = await aspect.invoke(name, path, clone(request))
54
- call = mock.fetch.mock.calls[0]
55
- args = call?.[1]
56
- })
57
-
58
- it('should throw on unknown origin', async () => {
59
- await expect(() => aspect.invoke('bar', path, request)).rejects.toThrow(/is not defined/)
60
- })
61
-
62
- it('should resolve URL', async () => {
63
- jest.clearAllMocks()
64
- mock.fetch.respond(200, response)
65
-
66
- const path = 'ok'
67
-
68
- await aspect.invoke('deep', path, clone(request))
69
-
70
- expect(mock.fetch.mock.calls[0][0]).toStrictEqual(fixtures.manifest.deep + path)
71
- })
72
-
73
- it.each(protocols)('should throw on absolute URL (%s)',
74
- async (protocol) => {
75
- jest.clearAllMocks()
76
- mock.fetch.respond(200, response)
77
-
78
- await expect(aspect.invoke('deep', protocol + '//api.domain.com', clone(request)))
79
- .rejects.toThrow('Absolute URLs are forbidden')
80
- })
81
-
82
- it('should substitute wildcards', async () => {
83
- jest.clearAllMocks()
84
- mock.fetch.respond(200, response)
85
-
86
- const substitutions = ['foo', 'bar', 443]
87
-
88
- await aspect.invoke('amazon', path, clone(request), { substitutions })
89
-
90
- const url = mock.fetch.mock.calls[0][0]
91
-
92
- expect(url).toStrictEqual('https://foo.bar.amazon.com' + path)
93
- })
94
-
95
- it('should not lose query string', async () => {
96
- jest.clearAllMocks()
97
- mock.fetch.respond(200, response)
98
-
99
- const path = generate() + '?foo=' + generate()
100
-
101
- await aspect.invoke(name, path)
102
-
103
- const url = mock.fetch.mock.calls[0][0]
104
-
105
- expect(url).toStrictEqual(fixtures.manifest.foo + '/' + path)
106
- })
107
-
108
- it('should not throw if path is not defined', async () => {
109
- jest.clearAllMocks()
110
- mock.fetch.respond(200, response)
111
-
112
- // noinspection JSCheckFunctionSignatures
113
- expect(() => aspect.invoke(name)).not.toThrow()
114
- })
115
-
116
- describe('fetch', () => {
117
- it('should fetch', async () => {
118
- expect(mock.fetch).toHaveBeenCalledTimes(1)
119
- })
120
-
121
- it('should pass url', () => {
122
- expect(call[0]).toStrictEqual(fixtures.manifest.foo + path)
123
- })
124
-
125
- it('should pass request', () => {
126
- expect(args).toStrictEqual(request)
127
- })
128
-
129
- it('should return response', async () => {
130
- const body = await result.json()
131
-
132
- expect(body).toStrictEqual(response)
133
- })
134
- })
135
-
136
- describe('retry', () => {
137
- it('should retry', async () => {
138
- jest.clearAllMocks()
139
-
140
- const attempts = random(5) + 2
141
-
142
- for (let i = 1; i < attempts; i++) mock.fetch.respond(500)
143
-
144
- mock.fetch.respond(200, response)
145
-
146
- /** @type {toa.origins.http.invocation.Options} */
147
- const options = {
148
- retry: { base: 0, retries: attempts }
149
- }
150
-
151
- await aspect.invoke(name, path, clone(request), options)
152
-
153
- expect(mock.fetch).toHaveBeenCalledTimes(attempts)
154
- })
155
- })
156
- })
157
-
158
- describe.each(protocols)('absolute URL', (protocol) => {
159
- const response = { [generate()]: generate() }
160
-
161
- it('should request absolute URL', async () => {
162
- mock.fetch.respond(200, response)
163
-
164
- const properties = { null: true }
165
- const url = protocol + '//' + generate()
166
- const request = { method: 'POST' }
167
-
168
- aspect = create(fixtures.manifest, properties)
169
-
170
- await aspect.invoke(url, request)
171
-
172
- expect(mock.fetch).toHaveBeenCalledWith(url, request)
173
- })
174
-
175
- it('should throw if URL not allowed', async () => {
176
- mock.fetch.respond(200, response)
177
-
178
- const properties = {}
179
-
180
- aspect = create(fixtures.manifest, properties)
181
-
182
- const url = protocol + '//' + generate()
183
- const request = { method: 'POST' }
184
-
185
- await expect(aspect.invoke(url, request)).rejects.toThrow('is not allowed')
186
- })
187
-
188
- it.each([
189
- [String.raw`/^${protocol}\/\/api.\S+.com/`, `${protocol}//api.${generate()}.com/path/to`],
190
- [String.raw`/${protocol}\/\/api.\S+.com/`, `${protocol}//api.${generate()}.com/path/to`]
191
- ])('should allow requests %s', async (expression, url) => {
192
- mock.fetch.respond(200, response)
193
-
194
- const properties = { [expression]: true }
195
-
196
- aspect = create(fixtures.manifest, properties)
197
-
198
- await expect(aspect.invoke(url)).resolves.not.toThrow()
199
- })
200
-
201
- it.each([
202
- [String.raw`/^${protocol}\/\/api.\S+.com/`, `${protocol}//api.${generate()}.com/path/to`],
203
- [String.raw`/${protocol}\/\/api.\S+.com/`, `${protocol}//api.${generate()}.com/path/to`]
204
- ])('should allow requests except %s', async (expression, url) => {
205
- mock.fetch.respond(200, response)
206
-
207
- const properties = { null: true, [expression]: false }
208
-
209
- aspect = create(fixtures.manifest, properties)
210
-
211
- await expect(aspect.invoke(url)).rejects.toThrow('is not allowed')
212
- })
213
-
214
- it.each([
215
- ['starts', 'expression/'],
216
- ['ends', '/expression']
217
- ])('should throw if rule does not %s with /', async (_, expression) => {
218
- expect(() => create(fixtures.manifest, { [expression]: true })).toThrow('is not a regular expression')
219
- })
220
- })
@@ -1,6 +0,0 @@
1
- 'use strict'
2
-
3
- const http = require('./http')
4
- const amqp = require('./amqp')
5
-
6
- module.exports = [http, amqp]
@@ -1 +0,0 @@
1
- ~: ref:manifest
@@ -1,8 +0,0 @@
1
- 'use strict'
2
-
3
- const schemas = require('@toa.io/schemas')
4
-
5
- const namespace = schemas.namespace(__dirname)
6
-
7
- exports.manifest = namespace.schema('manifest')
8
- exports.annotations = namespace.schema('annotations')
@@ -1,2 +0,0 @@
1
- /^\./: <boolean>
2
- /^[^.]/: uri
package/types/amqp.d.ts DELETED
@@ -1,9 +0,0 @@
1
- import * as _extensions from '@toa.io/core/types/extensions'
2
-
3
- declare namespace toa.origins.amqp {
4
-
5
- interface Aspect extends _extensions.Aspect {
6
- invoke(origin: string, method: string, ...args: any[]): Promise<any>
7
- }
8
-
9
- }
@@ -1,7 +0,0 @@
1
- declare namespace toa.origins {
2
-
3
- type Manifest = Record<string, string>
4
-
5
- type Annotations = Record<string, Manifest>
6
-
7
- }
package/types/http.d.ts DELETED
@@ -1,28 +0,0 @@
1
- import * as fetch from 'node-fetch'
2
- import * as _extensions from '@toa.io/core/types/extensions'
3
- import * as _retry from '@toa.io/generic/types/retry'
4
-
5
- declare namespace toa.origins.http {
6
-
7
- namespace invocation {
8
- type Options = {
9
- substitutions?: string[]
10
- retry?: _retry.Options
11
- }
12
- }
13
-
14
- type Properties = Record<string | null, boolean>
15
-
16
- interface Permissions {
17
- test(url: string): boolean
18
- }
19
-
20
- interface Aspect extends _extensions.Aspect {
21
- invoke(origin: string, path: string, request?: fetch.RequestInit, options?: invocation.Options): Promise<fetch.Response>
22
-
23
- invoke(url: string, request?: fetch.RequestInit): Promise<fetch.Response>
24
- }
25
-
26
- }
27
-
28
- export type Aspect = toa.origins.http.Aspect