@1mill/cloudevents 0.11.0 → 2.0.0

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 (52) hide show
  1. package/README.md +76 -51
  2. package/dist/index.js +2 -0
  3. package/dist/index.js.map +1 -0
  4. package/dist/index.modern.js +2 -0
  5. package/dist/index.modern.js.map +1 -0
  6. package/dist/index.module.js +2 -0
  7. package/dist/index.module.js.map +1 -0
  8. package/dist/index.umd.js +2 -0
  9. package/dist/index.umd.js.map +1 -0
  10. package/package.json +21 -8
  11. package/src/index.js +71 -0
  12. package/.editorconfig +0 -21
  13. package/index.js +0 -6
  14. package/v3/createCloudevent.js +0 -51
  15. package/v3/createEventStream.js +0 -36
  16. package/v3/enrichCloudevent.js +0 -15
  17. package/v3/index.js +0 -16
  18. package/v3/isEnriched.js +0 -5
  19. package/v3/kafka/convertFrom.js +0 -6
  20. package/v3/kafka/convertTo.js +0 -12
  21. package/v3/kafka/createAuthentication.js +0 -19
  22. package/v3/kafka/createEmit.js +0 -33
  23. package/v3/kafka/createListen.js +0 -75
  24. package/v3/kafka/index.js +0 -28
  25. package/v3/lambda/handler.js +0 -29
  26. package/v3/lambda/index.js +0 -7
  27. package/v3/lib/constants.js +0 -14
  28. package/v4/createCloudevent.js +0 -47
  29. package/v4/createEventStream.js +0 -36
  30. package/v4/index.js +0 -12
  31. package/v4/kafka/convertFrom.js +0 -6
  32. package/v4/kafka/convertTo.js +0 -12
  33. package/v4/kafka/createAuthentication.js +0 -19
  34. package/v4/kafka/createEmit.js +0 -33
  35. package/v4/kafka/createListen.js +0 -64
  36. package/v4/kafka/index.js +0 -28
  37. package/v4/lambda/handler.js +0 -9
  38. package/v4/lambda/index.js +0 -7
  39. package/v4/lib/constants.js +0 -14
  40. package/v5/createCloudevent.js +0 -47
  41. package/v5/createCloudeventStream.js +0 -42
  42. package/v5/index.js +0 -12
  43. package/v5/kafka/convertFrom.js +0 -6
  44. package/v5/kafka/convertTo.js +0 -12
  45. package/v5/kafka/createAuthentication.js +0 -19
  46. package/v5/kafka/createEmit.js +0 -33
  47. package/v5/kafka/createListen.js +0 -73
  48. package/v5/kafka/index.js +0 -28
  49. package/v5/lambda/handler.js +0 -9
  50. package/v5/lambda/index.js +0 -7
  51. package/v5/lib/constants.js +0 -14
  52. package/v6/index.js +0 -41
@@ -1,75 +0,0 @@
1
- const { ERROR_TYPES, SIGNAL_TRAPS } = require('../lib/constants')
2
- const { Kafka } = require('kafkajs')
3
- const { convertFrom } = require('./convertFrom')
4
- const { createAuthentication } = require('./createAuthentication')
5
- const { isEnriched } = require('../isEnriched')
6
-
7
- const createListen = ({
8
- id,
9
- mechanism,
10
- password,
11
- urls,
12
- username,
13
- }) => {
14
- const authentication = createAuthentication({
15
- mechanism,
16
- password,
17
- username,
18
- })
19
- const kafka = new Kafka({
20
- ...authentication,
21
- brokers: urls,
22
- clientId: id,
23
- })
24
-
25
- const { connect, disconnect, run, subscribe } = kafka.consumer({ groupId: id })
26
- const listen = async ({ handler, types }) => {
27
- await connect()
28
- await Promise.all(types.map(type => consumer.subscribe({
29
- fromBeginning: true,
30
- topic: type,
31
- })))
32
- await run({
33
- eachMessage: async (event) => {
34
- const { cloudevent } = convertFrom({ event })
35
- await handler({
36
- ...cloudevent,
37
- cloudevent,
38
- data: cloudevent.data
39
- ? JSON.parse(cloudevent.data)
40
- : undefined,
41
- enrichmentdata: isEnriched({ cloudevent })
42
- ? JSON.parse(cloudevent.enrichmentdata)
43
- : undefined,
44
- isEnriched: isEnriched({ cloudevent }),
45
- })
46
- }
47
- })
48
-
49
- // Before server stop, close subscription connections to kafka
50
- ERROR_TYPES.map(errorType => {
51
- process.on(errorType, async (err) => {
52
- try {
53
- console.log(`process.on ${errorType}`)
54
- console.error(err)
55
- await disconnect()
56
- process.exit(0)
57
- } catch (_err) {
58
- process.exit(1)
59
- }
60
- })
61
- })
62
- SIGNAL_TRAPS.map(signalTrap => {
63
- process.once(signalTrap, async () => {
64
- try {
65
- await disconnect()
66
- } finally {
67
- process.kill(process.pid, signalTrap)
68
- }
69
- })
70
- })
71
- }
72
- return { listen }
73
- }
74
-
75
- module.exports = { createListen }
package/v3/kafka/index.js DELETED
@@ -1,28 +0,0 @@
1
- const { createEmit } = require('./createEmit')
2
- const { createListen } = require('./createListen')
3
-
4
- const createInstance = ({
5
- id,
6
- mechanism,
7
- password,
8
- urls,
9
- username,
10
- }) => {
11
- const { emit } = createEmit({
12
- id,
13
- mechanism,
14
- password,
15
- urls,
16
- username,
17
- })
18
- const { listen } = createListen({
19
- id,
20
- mechanism,
21
- password,
22
- urls,
23
- username,
24
- });
25
- return { emit, listen }
26
- }
27
-
28
- module.exports = { createInstance }
@@ -1,29 +0,0 @@
1
- const { isEnriched } = require('../isEnriched')
2
-
3
- const handler = (func) => {
4
- return async (event, _context, _callback) => {
5
- const cloudevent = event
6
-
7
- const {
8
- data,
9
- datacontenttype,
10
- enrichmentdata,
11
- enrichmentdatacontenttype,
12
- } = cloudevent
13
-
14
- const payload = await func({
15
- ...cloudevent,
16
- cloudevent,
17
- data: datacontenttype === 'application/json'
18
- ? JSON.parse(data)
19
- : data,
20
- enrichmentdata: enrichmentdatacontenttype === 'application/json'
21
- ? JSON.parse(enrichmentdata)
22
- : enrichmentdata,
23
- isEnriched: isEnriched({ cloudevent })
24
- })
25
- return payload // * Returned to AWS invoker if 'RequestResponse' type
26
- }
27
- }
28
-
29
- module.exports = { handler }
@@ -1,7 +0,0 @@
1
- const { handler } = require('./handler');
2
-
3
- const createInstance = ({}) => {
4
- return { handler }
5
- }
6
-
7
- module.exports = { createInstance }
@@ -1,14 +0,0 @@
1
- const PROTOCOL_KAFKA = 'kafka'
2
- const PROTOCOL_LAMBDA = 'lambda'
3
-
4
- const ERROR_TYPES = ['unhandledRejection', 'uncaughtException']
5
- const SIGNAL_TRAPS = ['SIGTERM', 'SIGINT', 'SIGUSR2']
6
-
7
- module.exports = {
8
- // Supported protocols
9
- PROTOCOL_KAFKA,
10
- PROTOCOL_LAMBDA,
11
- // Other constants
12
- ERROR_TYPES,
13
- SIGNAL_TRAPS,
14
- };
@@ -1,47 +0,0 @@
1
- const createCloudevent = ({
2
- data,
3
- datacontenttype,
4
- dataschema,
5
- dlx = 'dlx',
6
- id,
7
- originid,
8
- originsource,
9
- origintype,
10
- source,
11
- specversion = '1.0',
12
- subject,
13
- type,
14
- }) => {
15
- if (!dlx) { throw new Error('Cloudevent "dlx" is as required') }
16
- if (!id) { throw new Error('Cloudevent "id" is as required') }
17
- if (!source) { throw new Error('Cloudevent "source" is as required') }
18
- if (!type) { throw new Error('Cloudevent "type" is as required') }
19
-
20
- const cloudevent = {
21
- // * Defined in cloudevents specification
22
- // Required defaults
23
- id,
24
- source,
25
- specversion,
26
- time: new Date().toISOString(),
27
- type,
28
-
29
- // Optional data
30
- data,
31
- datacontenttype,
32
- dataschema,
33
- subject,
34
-
35
- // * In-house attribute extensions
36
- // Required defaults
37
- dlx,
38
-
39
- // Origin data
40
- originid: originid || id,
41
- originsource: originsource || source,
42
- origintype: origintype || type,
43
- }
44
- return cloudevent
45
- }
46
-
47
- module.exports = { createCloudevent }
@@ -1,36 +0,0 @@
1
- const { PROTOCOL_KAFKA, PROTOCOL_LAMBDA } = require('./lib/constants')
2
- const { createInstance: kafkaCreateInstance } = require('./kafka')
3
- const { createInstance: lambdaCreateInstance } = require('./lambda')
4
-
5
- const SUPPORTED_PROTOCOLS = [
6
- PROTOCOL_KAFKA,
7
- PROTOCOL_LAMBDA,
8
- ]
9
-
10
- const createEventStream = ({
11
- id,
12
- mechanism,
13
- password,
14
- protocol,
15
- urls,
16
- username,
17
- }) => {
18
- if (!protocol) { throw new Error('The "protocol" argument is required') }
19
- if (!SUPPORTED_PROTOCOLS.includes(protocol)) { throw new Error(`The "${protocol}" protocol is not supported`) }
20
-
21
- if (protocol === PROTOCOL_KAFKA) {
22
- return kafkaCreateInstance({
23
- id,
24
- mechanism,
25
- password,
26
- urls,
27
- username,
28
- })
29
- }
30
-
31
- if (protocol === PROTOCOL_LAMBDA) {
32
- return lambdaCreateInstance({})
33
- }
34
- }
35
-
36
- module.exports = { createEventStream }
package/v4/index.js DELETED
@@ -1,12 +0,0 @@
1
- const { PROTOCOL_KAFKA, PROTOCOL_LAMBDA } = require('./lib/constants')
2
- const { createCloudevent } = require('./createCloudevent')
3
- const { createEventStream } = require('./createEventStream')
4
-
5
- const v4 = {
6
- PROTOCOL_KAFKA,
7
- PROTOCOL_LAMBDA,
8
- createCloudevent,
9
- createEventStream,
10
- }
11
-
12
- module.exports = { v4 }
@@ -1,6 +0,0 @@
1
- const convertFrom = ({ event }) => {
2
- const cloudevent = JSON.parse(event.message.value)
3
- return { cloudevent }
4
- }
5
-
6
- module.exports = { convertFrom }
@@ -1,12 +0,0 @@
1
- const convertTo = ({ cloudevent }) => {
2
- const message = {
3
- headers: {
4
- contentType: 'application/cloudevents+json;charset=UTF-8'
5
- },
6
- value: JSON.stringify(cloudevent),
7
- }
8
- const event = { messages: [message], topic: cloudevent.type }
9
- return { event }
10
- }
11
-
12
- module.exports = { convertTo }
@@ -1,19 +0,0 @@
1
- // Client Configuration for KafkaJS: https://kafka.js.org/docs/configuration
2
- const SCRAM_MECHANISMS = [
3
- 'plain',
4
- 'scram-sha-256',
5
- 'scram-sha-512',
6
- ]
7
-
8
- const createAuthentication = ({ mechanism, password, username }) => {
9
- if (SCRAM_MECHANISMS.includes(mechanism)) {
10
- return {
11
- sasl: { mechanism, password, username },
12
- ssl: true,
13
- }
14
- }
15
-
16
- return {}
17
- }
18
-
19
- module.exports = { createAuthentication }
@@ -1,33 +0,0 @@
1
- const { Kafka } = require('kafkajs')
2
- const { convertTo } = require('./convertTo')
3
- const { createAuthentication } = require('./createAuthentication')
4
-
5
- const createEmit = ({
6
- id,
7
- mechanism,
8
- password,
9
- urls,
10
- username,
11
- }) => {
12
- const authentication = createAuthentication({
13
- mechanism,
14
- password,
15
- username,
16
- })
17
- const kafka = new Kafka({
18
- ...authentication,
19
- brokers: urls,
20
- clientId: id,
21
- })
22
-
23
- const emit = async ({ cloudevent }) => {
24
- const producer = kafka.producer()
25
- await producer.connect()
26
- const { event } = convertTo({ cloudevent })
27
- await producer.send(event)
28
- await producer.disconnect()
29
- }
30
- return { emit }
31
- }
32
-
33
- module.exports = { createEmit }
@@ -1,64 +0,0 @@
1
- const { ERROR_TYPES, SIGNAL_TRAPS } = require('../lib/constants')
2
- const { Kafka } = require('kafkajs')
3
- const { convertFrom } = require('./convertFrom')
4
- const { createAuthentication } = require('./createAuthentication')
5
-
6
- const createListen = ({
7
- id,
8
- mechanism,
9
- password,
10
- urls,
11
- username,
12
- }) => {
13
- const authentication = createAuthentication({
14
- mechanism,
15
- password,
16
- username,
17
- })
18
- const kafka = new Kafka({
19
- ...authentication,
20
- brokers: urls,
21
- clientId: id,
22
- })
23
-
24
- const consumer = kafka.consumer({ groupId: id });
25
- const listen = async ({ handler, types }) => {
26
- await consumer.connect()
27
- await Promise.all(types.map(type => consumer.subscribe({
28
- fromBeginning: true,
29
- topic: type,
30
- })))
31
- await consumer.run({
32
- eachMessage: async (event) => {
33
- const { cloudevent } = convertFrom({ event })
34
- await handler({ ...cloudevent, cloudevent })
35
- }
36
- })
37
-
38
- // Before server stop, close subscription connections to kafka
39
- ERROR_TYPES.map(errorType => {
40
- process.on(errorType, async (err) => {
41
- try {
42
- console.log(`process.on ${errorType}`)
43
- console.error(err)
44
- await consumer.disconnect()
45
- process.exit(0)
46
- } catch (_err) {
47
- process.exit(1)
48
- }
49
- })
50
- })
51
- SIGNAL_TRAPS.map(signalTrap => {
52
- process.once(signalTrap, async () => {
53
- try {
54
- await consumer.disconnect()
55
- } finally {
56
- process.kill(process.pid, signalTrap)
57
- }
58
- })
59
- })
60
- }
61
- return { listen }
62
- }
63
-
64
- module.exports = { createListen }
package/v4/kafka/index.js DELETED
@@ -1,28 +0,0 @@
1
- const { createEmit } = require('./createEmit')
2
- const { createListen } = require('./createListen')
3
-
4
- const createInstance = ({
5
- id,
6
- mechanism,
7
- password,
8
- urls,
9
- username,
10
- }) => {
11
- const { emit } = createEmit({
12
- id,
13
- mechanism,
14
- password,
15
- urls,
16
- username,
17
- })
18
- const { listen } = createListen({
19
- id,
20
- mechanism,
21
- password,
22
- urls,
23
- username,
24
- });
25
- return { emit, listen }
26
- }
27
-
28
- module.exports = { createInstance }
@@ -1,9 +0,0 @@
1
- const handler = (func) => {
2
- return async (event, _context, _callback) => {
3
- const cloudevent = event
4
- const payload = await func ({ ...cloudevent, cloudevent })
5
- return payload // * Returned to AWS invoker if 'RequestResponse' type
6
- }
7
- }
8
-
9
- module.exports = { handler }
@@ -1,7 +0,0 @@
1
- const { handler } = require('./handler');
2
-
3
- const createInstance = ({}) => {
4
- return { handler }
5
- }
6
-
7
- module.exports = { createInstance }
@@ -1,14 +0,0 @@
1
- const PROTOCOL_KAFKA = 'kafka'
2
- const PROTOCOL_LAMBDA = 'lambda'
3
-
4
- const ERROR_TYPES = ['unhandledRejection', 'uncaughtException']
5
- const SIGNAL_TRAPS = ['SIGTERM', 'SIGINT', 'SIGUSR2']
6
-
7
- module.exports = {
8
- // Supported protocols
9
- PROTOCOL_KAFKA,
10
- PROTOCOL_LAMBDA,
11
- // Other constants
12
- ERROR_TYPES,
13
- SIGNAL_TRAPS,
14
- };
@@ -1,47 +0,0 @@
1
- const createCloudevent = ({
2
- data,
3
- datacontenttype,
4
- dataschema,
5
- dlx = 'dlx',
6
- id,
7
- originid,
8
- originsource,
9
- origintype,
10
- source,
11
- specversion = '1.0',
12
- subject,
13
- type,
14
- }) => {
15
- if (!dlx) { throw new Error('Cloudevent "dlx" is as required') }
16
- if (!id) { throw new Error('Cloudevent "id" is as required') }
17
- if (!source) { throw new Error('Cloudevent "source" is as required') }
18
- if (!type) { throw new Error('Cloudevent "type" is as required') }
19
-
20
- const cloudevent = {
21
- // * Defined in cloudevents specification
22
- // Required defaults
23
- id,
24
- source,
25
- specversion,
26
- time: new Date().toISOString(),
27
- type,
28
-
29
- // Optional data
30
- data,
31
- datacontenttype,
32
- dataschema,
33
- subject,
34
-
35
- // * In-house attribute extensions
36
- // Required defaults
37
- dlx,
38
-
39
- // Origin data
40
- originid: originid || id,
41
- originsource: originsource || source,
42
- origintype: origintype || type,
43
- }
44
- return cloudevent
45
- }
46
-
47
- module.exports = { createCloudevent }
@@ -1,42 +0,0 @@
1
- const { PROTOCOL_KAFKA, PROTOCOL_LAMBDA } = require('./lib/constants')
2
- const { createInstance: kafkaCreateInstance } = require('./kafka')
3
- const { createInstance: lambdaCreateInstance } = require('./lambda')
4
-
5
- const SUPPORTED_PROTOCOLS = [
6
- PROTOCOL_KAFKA,
7
- PROTOCOL_LAMBDA,
8
- ]
9
-
10
- const _throwRequired = (attribute) => {
11
- throw new Error(`The ${attribute} argument is required`)
12
- }
13
-
14
- const createCloudeventStream = ({
15
- id = process.env.CLOUDEVENT_STREAM_ID,
16
- mechanism = process.env.CLOUDEVENT_STREAM_MECHANISM,
17
- password = process.env.CLOUDEVENT_STREAM_PASSWORD,
18
- protocol = process.env.CLOUDEVENT_STREAM_PROTOCOL,
19
- urls = (process.env.CLOUDEVENT_STREAM_URLS || '').split(','),
20
- username = process.env.CLOUDEVENT_STREAM_USERNAME,
21
- }) => {
22
- if (!protocol) { _throwRequired('protocol') }
23
- if (!SUPPORTED_PROTOCOLS.includes(protocol)) { throw new Error(`The "${protocol}" protocol is not supported`) }
24
-
25
- if (protocol === PROTOCOL_KAFKA) {
26
- if (!id) { _throwRequired('id') }
27
- if (!urls.length === 0) { _throwRequired('urls') }
28
- return kafkaCreateInstance({
29
- id,
30
- mechanism,
31
- password,
32
- urls,
33
- username,
34
- })
35
- }
36
-
37
- if (protocol === PROTOCOL_LAMBDA) {
38
- return lambdaCreateInstance({})
39
- }
40
- }
41
-
42
- module.exports = { createCloudeventStream }
package/v5/index.js DELETED
@@ -1,12 +0,0 @@
1
- const { PROTOCOL_KAFKA, PROTOCOL_LAMBDA } = require('./lib/constants')
2
- const { createCloudevent } = require('./createCloudevent')
3
- const { createCloudeventStream } = require('./createCloudeventStream')
4
-
5
- const v5 = {
6
- PROTOCOL_KAFKA,
7
- PROTOCOL_LAMBDA,
8
- createCloudevent,
9
- createCloudeventStream,
10
- }
11
-
12
- module.exports = { v5 }
@@ -1,6 +0,0 @@
1
- const convertFrom = ({ event }) => {
2
- const cloudevent = JSON.parse(event.message.value)
3
- return { cloudevent }
4
- }
5
-
6
- module.exports = { convertFrom }
@@ -1,12 +0,0 @@
1
- const convertTo = ({ cloudevent }) => {
2
- const message = {
3
- headers: {
4
- contentType: 'application/cloudevents+json;charset=UTF-8'
5
- },
6
- value: JSON.stringify(cloudevent),
7
- }
8
- const event = { messages: [message], topic: cloudevent.type }
9
- return { event }
10
- }
11
-
12
- module.exports = { convertTo }
@@ -1,19 +0,0 @@
1
- // Client Configuration for KafkaJS: https://kafka.js.org/docs/configuration
2
- const SCRAM_MECHANISMS = [
3
- 'plain',
4
- 'scram-sha-256',
5
- 'scram-sha-512',
6
- ]
7
-
8
- const createAuthentication = ({ mechanism, password, username }) => {
9
- if (SCRAM_MECHANISMS.includes(mechanism)) {
10
- return {
11
- sasl: { mechanism, password, username },
12
- ssl: true,
13
- }
14
- }
15
-
16
- return {}
17
- }
18
-
19
- module.exports = { createAuthentication }
@@ -1,33 +0,0 @@
1
- const { Kafka } = require('kafkajs')
2
- const { convertTo } = require('./convertTo')
3
- const { createAuthentication } = require('./createAuthentication')
4
-
5
- const createEmit = ({
6
- id,
7
- mechanism,
8
- password,
9
- urls,
10
- username,
11
- }) => {
12
- const authentication = createAuthentication({
13
- mechanism,
14
- password,
15
- username,
16
- })
17
- const kafka = new Kafka({
18
- ...authentication,
19
- brokers: urls,
20
- clientId: id,
21
- })
22
-
23
- const emit = async ({ cloudevent }) => {
24
- const producer = kafka.producer()
25
- await producer.connect()
26
- const { event } = convertTo({ cloudevent })
27
- await producer.send(event)
28
- await producer.disconnect()
29
- }
30
- return { emit }
31
- }
32
-
33
- module.exports = { createEmit }