@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
package/README.md CHANGED
@@ -2,76 +2,101 @@
2
2
 
3
3
  ## Introduction
4
4
 
5
- This package is an implementation and extention of the CloudEvents v1 specification to easily communicate with event streaming applications.
6
- Currently only Kafka is suppported, but the intention is to make this library as agnostic as possible.
5
+ This is an implementation and extention of the CloudEvents v1 specification to easily build cloudevents.
7
6
 
8
7
  ## How to install
9
8
 
10
9
  ```bash
11
- npm install @1mill/cloudevents@^0.10
10
+ npm install @1mill/cloudevents@^0.11
12
11
  ```
13
12
 
14
- ## Example
15
-
16
- ### Publish CloudEvent to event stream
13
+ ## Example usage
17
14
 
18
15
  ```js
19
- const { v4: { createCloudevent, createEventStream } } = require('@1mill/cloudevents')
20
- const stream = createEventStream({
21
- protocol: 'kafka',
22
- urls: 'my-kafka-url:9092',
23
- })
16
+ const { Cloudevent } = require('@1mill/cloudevents')
24
17
 
25
- const cloudevent = createCloudevent({
18
+ const cloudevent = new Cloudevent({
26
19
  data: JSON.stringify({
27
- my: true,
28
- payload: { something: true },
20
+ someAttribute: 'yes',
21
+ someOtherAttribute: { thing: true },
29
22
  }),
30
- datacontenttype: 'application/json',
31
- id: 'my-id',
32
- source: 'my-source',
33
- type: 'my-type.version.modifier',
23
+ source: 'https://www.my-website.com/my/page/123', // * Required
24
+ subject: '123',
25
+ type: 'cmd.do-this-command.v0', // * Required
34
26
  })
35
- stream.emit({ cloudevent })
27
+
28
+ console.log(cloudevent)
29
+
30
+ // Cloudevent {
31
+ // id: '-of0T1jfpvD7_lOXtynbb',
32
+ // source: 'https://www.my-website.come/my/page/123',
33
+ // type: 'cmd.do-this-command.v0',
34
+ // specversion: '1.0',
35
+ // data: '{"someAttribute":"yes","someOtherAttribute":{"thing":true}}',
36
+ // datacontenttype: 'application/json',
37
+ // dataschema: undefined,
38
+ // subject: '123',
39
+ // time: '2021-09-06T16:29:26.527Z',
40
+ // originid: '-of0T1jfpvD7_lOXtynbb',
41
+ // originsource: 'https://www.my-website.come/my/page/123',
42
+ // origintype: 'cmd.do-this-command.v0'
43
+ // }
36
44
  ```
37
45
 
38
- ### Subscribe to event stream
46
+ ### Example of Chaining Cloudevents
39
47
 
40
48
  ```js
41
- const { v4: { createEventStream } } = require('@1mill/cloudevents')
42
- const stream = createEventStream({
43
- protocol: 'kafka',
44
- urls: 'my-kafka-urls:9092',
45
- })
46
- stream.listen({
47
- handler: ({ cloudevent }) => {
48
- const { my, payload } = JSON.parse(cloudevent.data)
49
- console.log(my + payload)
50
- },
51
- types: [
52
- 'my-other-type.version.modifier',
53
- 'my-type.version.modifier',
54
- ]
49
+ // 1. Create command
50
+ const cmdCloudevent = new Cloudevent({
51
+ source: 'https://my-ui.com/my/feature/page/123',
52
+ subject: '123',
53
+ type: 'cmd.do-this-command.v0',
55
54
  })
56
- ```
57
55
 
58
- ### Lambda handler
56
+ console.log(cmdCloudevent)
57
+ // Cloudevent {
58
+ // id: 'pDScxm45M2-BnnIYHw4P3',
59
+ // source: 'https://www.my-website.come/my/page/123',
60
+ // type: 'cmd.do-this-command.v0',
61
+ // specversion: '1.0',
62
+ // data: '{"someAttribute":"yes","someOtherAttribute":{"thing":true}}',
63
+ // datacontenttype: 'application/json',
64
+ // dataschema: undefined,
65
+ // subject: '123',
66
+ // time: '2021-09-06T16:38:49.717Z',
67
+ // originid: 'pDScxm45M2-BnnIYHw4P3',
68
+ // originsource: 'https://www.my-website.come/my/page/123',
69
+ // origintype: 'cmd.do-this-command.v0'
70
+ // }
71
+
72
+ // 2. Publish command to Enterprise Event Bus (e.g. Kafka)
73
+
74
+ // 3. Subscribe to cloudevent type on Enterprise Event Buss
75
+
76
+ // 4. React to cloudevent command
77
+ const enrichment = dataFromMyBusinessProcess()
78
+ const fctCloudevent = new Cloudevent({
79
+ ...cmdCloudevent,
80
+ data: JSON.stringify(enrichment),
81
+ source: 'arn:aws:lambda:us-east-1:123456789012:function:my-function',
82
+ type: 'fct.this-thing-happened.v0',
83
+ })
59
84
 
60
- ```js
61
- const { v4: { createEventStream } } = require('@1mill/cloudevents')
62
-
63
- const perform = async ({ cloudevent }) => {
64
- const { my, payload } = JSON.parse(cloudevent.data)
65
- await new Promise((resolve, _reject) => {
66
- setTimeout(() => {
67
- console.log(my + payload)
68
- resolve()
69
- }, 1000)
70
- })
71
- }
72
-
73
- const lambda = createEventStream({ protocol: 'lambda' })
74
- exports.handler = lambda.handler(perform)
85
+ console.log(fctCloudevent)
86
+ // Cloudevent {
87
+ // id: 'N02yLAd_bZeZLGRUl78AS',
88
+ // source: 'arn:aws:lambda:us-east-1:123456789012:function:my-function',
89
+ // type: 'fct.this-thing-happened.v0',
90
+ // specversion: '1.0',
91
+ // data: '{...}',
92
+ // datacontenttype: 'application/json',
93
+ // dataschema: undefined,
94
+ // subject: '123',
95
+ // time: '2021-09-06T16:38:49.717Z',
96
+ // originid: 'pDScxm45M2-BnnIYHw4P3',
97
+ // originsource: 'https://www.my-website.come/my/page/123',
98
+ // origintype: 'cmd.do-this-command.v0'
99
+ // }
75
100
  ```
76
101
 
77
102
  ## Release new version
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ var t=require("nanoid").nanoid,i=function(t){return process&&process.env&&process.env[t]};module.exports=Object.freeze({Cloudevent:function(e){var r=e.data,o=e.datacontenttype,s=e.dataschema,n=e.originid,h=e.originsource,u=e.origintime,d=e.origintype,a=e.source,g=e.specversion,c=e.subject,p=e.type;if(this.id=t(i("1MILL_CLOUDEVENTS_NANOID_LENGTH")||21),!this.id)throw new Error('Cloudevent "id" is required');if("string"!=typeof this.id)throw new Error('Cloudevent "id" must be a string');if(this.source=a||i("1MILL_CLOUDEVENTS_SOURCE"),!this.source)throw new Error('Cloudevent "source" is required');if("string"!=typeof this.source)throw new Error('Cloudevent "source" must be a string');if(this.type=p,!this.type)throw new Error('Cloudevent "type" is required');if("string"!=typeof this.type)throw new Error('Cloudevent "type" must be a string');if(this.specversion=g||"1.0",!this.specversion)throw new Error('Cloudevent "specversion" is required');if("string"!=typeof this.specversion)throw new Error('Cloudevent "specversion" must be a string');if(this.data=r,this.datacontenttype=void 0!==this.data?o||"application/json":o,this.datacontenttype&&"string"!=typeof this.datacontenttype)throw new Error('Cloudevent "datacontenttype" must be a string');if(this.dataschema=s,this.dataschema&&"string"!=typeof this.dataschema)throw new Error('Cloudevent "dataschema" must be a string');if(this.subject=c,this.subject&&"string"!=typeof this.subject)throw new Error('Cloudevent "subject" must be a string');if(this.time=(new Date).toISOString(),this.originid=n||this.id,!this.originid)throw new Error('Cloudevent "originid" is required');if("string"!=typeof this.originid)throw new Error('Cloudevent "originid" must be a string');if(this.originsource=h||this.source,!this.originsource)throw new Error('Cloudevent "originsource" is required');if("string"!=typeof this.originsource)throw new Error('Cloudevent "originsource" must be a string');if(this.origintime=u||this.time,!this.origintime)throw new Error('Cloudevent "origintime" is required');if("string"!=typeof this.origintime)throw new Error('Cloudevent "origintime" must be a string');if(this.origintype=d||this.type,!this.origintype)throw new Error('Cloudevent "origintype" is required');if("string"!=typeof this.origintype)throw new Error('Cloudevent "origintype" must be a string')}});
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sources":["../src/index.js"],"sourcesContent":["const { nanoid } = require('nanoid')\r\n\r\nconst fetchNodeEnv = name => process && process.env && process.env[name]\r\n\r\nclass Cloudevent {\r\n\tconstructor({\r\n\t\tdata,\r\n\t\tdatacontenttype,\r\n\t\tdataschema,\r\n\t\toriginid,\r\n\t\toriginsource,\r\n\t\torigintime,\r\n\t\torigintype,\r\n\t\tsource,\r\n\t\tspecversion,\r\n\t\tsubject,\r\n\t\ttype,\r\n\t}) {\r\n\t\t// * Required fields by CloudEvent specification\r\n\t\tthis.id = nanoid(fetchNodeEnv('1MILL_CLOUDEVENTS_NANOID_LENGTH') || 21)\r\n\t\tif (!this.id) throw new Error('Cloudevent \"id\" is required')\r\n\t\tif (typeof this.id !== 'string') throw new Error('Cloudevent \"id\" must be a string')\r\n\r\n\t\tthis.source = source || fetchNodeEnv('1MILL_CLOUDEVENTS_SOURCE')\r\n\t\tif (!this.source) throw new Error('Cloudevent \"source\" is required')\r\n\t\tif (typeof this.source !== 'string') throw new Error('Cloudevent \"source\" must be a string')\r\n\r\n\t\tthis.type = type\r\n\t\tif (!this.type) throw new Error('Cloudevent \"type\" is required')\r\n\t\tif (typeof this.type !== 'string') throw new Error('Cloudevent \"type\" must be a string')\r\n\r\n\t\tthis.specversion = specversion || '1.0'\r\n\t\tif (!this.specversion) throw new Error('Cloudevent \"specversion\" is required')\r\n\t\tif (typeof this.specversion !== 'string') throw new Error('Cloudevent \"specversion\" must be a string')\r\n\r\n\t\t// * Optional fields by CloudEvent specification\r\n\t\tthis.data = data\r\n\r\n\t\tthis.datacontenttype = typeof this.data !== 'undefined'\r\n\t\t\t? datacontenttype || 'application/json'\r\n\t\t\t: datacontenttype\r\n\t\tif (this.datacontenttype && typeof this.datacontenttype !== 'string') throw new Error('Cloudevent \"datacontenttype\" must be a string')\r\n\r\n\t\tthis.dataschema = dataschema\r\n\t\tif (this.dataschema && typeof this.dataschema !== 'string') throw new Error('Cloudevent \"dataschema\" must be a string')\r\n\r\n\t\tthis.subject = subject\r\n\t\tif (this.subject && typeof this.subject !== 'string') throw new Error('Cloudevent \"subject\" must be a string')\r\n\r\n\t\tthis.time = new Date().toISOString()\r\n\r\n\t\t// * In-house extentions\r\n\t\tthis.originid = originid || this.id\r\n\t\tif (!this.originid) throw new Error('Cloudevent \"originid\" is required')\r\n\t\tif (typeof this.originid !== 'string') throw new Error('Cloudevent \"originid\" must be a string')\r\n\r\n\t\tthis.originsource = originsource || this.source\r\n\t\tif (!this.originsource) throw new Error('Cloudevent \"originsource\" is required')\r\n\t\tif (typeof this.originsource !== 'string') throw new Error('Cloudevent \"originsource\" must be a string')\r\n\r\n\t\tthis.origintime = origintime || this.time\r\n\t\tif (!this.origintime) throw new Error('Cloudevent \"origintime\" is required')\r\n\t\tif (typeof this.origintime !== 'string') throw new Error('Cloudevent \"origintime\" must be a string')\r\n\r\n\t\tthis.origintype = origintype || this.type\r\n\t\tif (!this.origintype) throw new Error('Cloudevent \"origintype\" is required')\r\n\t\tif (typeof this.origintype !== 'string') throw new Error('Cloudevent \"origintype\" must be a string')\r\n\t}\r\n}\r\n\r\nmodule.exports = Object.freeze({ Cloudevent })\r\n"],"names":["nanoid","require","fetchNodeEnv","name","process","env","module","exports","Object","freeze","Cloudevent","data","datacontenttype","dataschema","originid","originsource","origintime","origintype","source","specversion","subject","type","this","id","Error","time","Date","toISOString"],"mappings":"AAAA,IAAQA,EAAWC,QAAQ,UAAnBD,OAEFE,EAAe,SAAAC,UAAQC,SAAWA,QAAQC,KAAOD,QAAQC,IAAIF,IAoEnEG,OAAOC,QAAUC,OAAOC,OAAO,CAAEC,WAjEhC,gBACCC,IAAAA,KACAC,IAAAA,gBACAC,IAAAA,WACAC,IAAAA,SACAC,IAAAA,aACAC,IAAAA,WACAC,IAAAA,WACAC,IAAAA,OACAC,IAAAA,YACAC,IAAAA,QACAC,IAAAA,KAIA,GADAC,KAAKC,GAAKvB,EAAOE,EAAa,oCAAsC,KAC/DoB,KAAKC,GAAI,UAAUC,MAAM,+BAC9B,GAAuB,sBAAPD,GAAiB,UAAUC,MAAM,oCAGjD,GADAF,KAAKJ,OAASA,GAAUhB,EAAa,6BAChCoB,KAAKJ,OAAQ,UAAUM,MAAM,mCAClC,GAA2B,sBAAXN,OAAqB,UAAUM,MAAM,wCAGrD,GADAF,KAAKD,KAAOA,GACPC,KAAKD,KAAM,UAAUG,MAAM,iCAChC,GAAyB,sBAATH,KAAmB,UAAUG,MAAM,sCAGnD,GADAF,KAAKH,YAAcA,GAAe,OAC7BG,KAAKH,YAAa,UAAUK,MAAM,wCACvC,GAAgC,sBAAhBL,YAA0B,UAAUK,MAAM,6CAQ1D,GALAF,KAAKX,KAAOA,EAEZW,KAAKV,qBAAuC,SAATD,KAChCC,GAAmB,mBACnBA,EACCU,KAAKV,iBAAmD,sBAApBA,gBAA8B,UAAUY,MAAM,iDAGtF,GADAF,KAAKT,WAAaA,EACdS,KAAKT,YAAyC,sBAAfA,WAAyB,UAAUW,MAAM,4CAG5E,GADAF,KAAKF,QAAUA,EACXE,KAAKF,SAAmC,sBAAZA,QAAsB,UAAUI,MAAM,yCAMtE,GAJAF,KAAKG,MAAO,IAAIC,MAAOC,cAGvBL,KAAKR,SAAWA,GAAYQ,KAAKC,IAC5BD,KAAKR,SAAU,UAAUU,MAAM,qCACpC,GAA6B,sBAAbV,SAAuB,UAAUU,MAAM,0CAGvD,GADAF,KAAKP,aAAeA,GAAgBO,KAAKJ,QACpCI,KAAKP,aAAc,UAAUS,MAAM,yCACxC,GAAiC,sBAAjBT,aAA2B,UAAUS,MAAM,8CAG3D,GADAF,KAAKN,WAAaA,GAAcM,KAAKG,MAChCH,KAAKN,WAAY,UAAUQ,MAAM,uCACtC,GAA+B,sBAAfR,WAAyB,UAAUQ,MAAM,4CAGzD,GADAF,KAAKL,WAAaA,GAAcK,KAAKD,MAChCC,KAAKL,WAAY,UAAUO,MAAM,uCACtC,GAA+B,sBAAfP,WAAyB,UAAUO,MAAM"}
@@ -0,0 +1,2 @@
1
+ const{nanoid:t}=require("nanoid"),e=t=>process&&process.env&&process.env[t];module.exports=Object.freeze({Cloudevent:class{constructor({data:i,datacontenttype:r,dataschema:o,originid:s,originsource:n,origintime:h,origintype:u,source:d,specversion:a,subject:g,type:c}){if(this.id=t(e("1MILL_CLOUDEVENTS_NANOID_LENGTH")||21),!this.id)throw new Error('Cloudevent "id" is required');if("string"!=typeof this.id)throw new Error('Cloudevent "id" must be a string');if(this.source=d||e("1MILL_CLOUDEVENTS_SOURCE"),!this.source)throw new Error('Cloudevent "source" is required');if("string"!=typeof this.source)throw new Error('Cloudevent "source" must be a string');if(this.type=c,!this.type)throw new Error('Cloudevent "type" is required');if("string"!=typeof this.type)throw new Error('Cloudevent "type" must be a string');if(this.specversion=a||"1.0",!this.specversion)throw new Error('Cloudevent "specversion" is required');if("string"!=typeof this.specversion)throw new Error('Cloudevent "specversion" must be a string');if(this.data=i,this.datacontenttype=void 0!==this.data?r||"application/json":r,this.datacontenttype&&"string"!=typeof this.datacontenttype)throw new Error('Cloudevent "datacontenttype" must be a string');if(this.dataschema=o,this.dataschema&&"string"!=typeof this.dataschema)throw new Error('Cloudevent "dataschema" must be a string');if(this.subject=g,this.subject&&"string"!=typeof this.subject)throw new Error('Cloudevent "subject" must be a string');if(this.time=(new Date).toISOString(),this.originid=s||this.id,!this.originid)throw new Error('Cloudevent "originid" is required');if("string"!=typeof this.originid)throw new Error('Cloudevent "originid" must be a string');if(this.originsource=n||this.source,!this.originsource)throw new Error('Cloudevent "originsource" is required');if("string"!=typeof this.originsource)throw new Error('Cloudevent "originsource" must be a string');if(this.origintime=h||this.time,!this.origintime)throw new Error('Cloudevent "origintime" is required');if("string"!=typeof this.origintime)throw new Error('Cloudevent "origintime" must be a string');if(this.origintype=u||this.type,!this.origintype)throw new Error('Cloudevent "origintype" is required');if("string"!=typeof this.origintype)throw new Error('Cloudevent "origintype" must be a string')}}});
2
+ //# sourceMappingURL=index.modern.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.modern.js","sources":["../src/index.js"],"sourcesContent":["const { nanoid } = require('nanoid')\r\n\r\nconst fetchNodeEnv = name => process && process.env && process.env[name]\r\n\r\nclass Cloudevent {\r\n\tconstructor({\r\n\t\tdata,\r\n\t\tdatacontenttype,\r\n\t\tdataschema,\r\n\t\toriginid,\r\n\t\toriginsource,\r\n\t\torigintime,\r\n\t\torigintype,\r\n\t\tsource,\r\n\t\tspecversion,\r\n\t\tsubject,\r\n\t\ttype,\r\n\t}) {\r\n\t\t// * Required fields by CloudEvent specification\r\n\t\tthis.id = nanoid(fetchNodeEnv('1MILL_CLOUDEVENTS_NANOID_LENGTH') || 21)\r\n\t\tif (!this.id) throw new Error('Cloudevent \"id\" is required')\r\n\t\tif (typeof this.id !== 'string') throw new Error('Cloudevent \"id\" must be a string')\r\n\r\n\t\tthis.source = source || fetchNodeEnv('1MILL_CLOUDEVENTS_SOURCE')\r\n\t\tif (!this.source) throw new Error('Cloudevent \"source\" is required')\r\n\t\tif (typeof this.source !== 'string') throw new Error('Cloudevent \"source\" must be a string')\r\n\r\n\t\tthis.type = type\r\n\t\tif (!this.type) throw new Error('Cloudevent \"type\" is required')\r\n\t\tif (typeof this.type !== 'string') throw new Error('Cloudevent \"type\" must be a string')\r\n\r\n\t\tthis.specversion = specversion || '1.0'\r\n\t\tif (!this.specversion) throw new Error('Cloudevent \"specversion\" is required')\r\n\t\tif (typeof this.specversion !== 'string') throw new Error('Cloudevent \"specversion\" must be a string')\r\n\r\n\t\t// * Optional fields by CloudEvent specification\r\n\t\tthis.data = data\r\n\r\n\t\tthis.datacontenttype = typeof this.data !== 'undefined'\r\n\t\t\t? datacontenttype || 'application/json'\r\n\t\t\t: datacontenttype\r\n\t\tif (this.datacontenttype && typeof this.datacontenttype !== 'string') throw new Error('Cloudevent \"datacontenttype\" must be a string')\r\n\r\n\t\tthis.dataschema = dataschema\r\n\t\tif (this.dataschema && typeof this.dataschema !== 'string') throw new Error('Cloudevent \"dataschema\" must be a string')\r\n\r\n\t\tthis.subject = subject\r\n\t\tif (this.subject && typeof this.subject !== 'string') throw new Error('Cloudevent \"subject\" must be a string')\r\n\r\n\t\tthis.time = new Date().toISOString()\r\n\r\n\t\t// * In-house extentions\r\n\t\tthis.originid = originid || this.id\r\n\t\tif (!this.originid) throw new Error('Cloudevent \"originid\" is required')\r\n\t\tif (typeof this.originid !== 'string') throw new Error('Cloudevent \"originid\" must be a string')\r\n\r\n\t\tthis.originsource = originsource || this.source\r\n\t\tif (!this.originsource) throw new Error('Cloudevent \"originsource\" is required')\r\n\t\tif (typeof this.originsource !== 'string') throw new Error('Cloudevent \"originsource\" must be a string')\r\n\r\n\t\tthis.origintime = origintime || this.time\r\n\t\tif (!this.origintime) throw new Error('Cloudevent \"origintime\" is required')\r\n\t\tif (typeof this.origintime !== 'string') throw new Error('Cloudevent \"origintime\" must be a string')\r\n\r\n\t\tthis.origintype = origintype || this.type\r\n\t\tif (!this.origintype) throw new Error('Cloudevent \"origintype\" is required')\r\n\t\tif (typeof this.origintype !== 'string') throw new Error('Cloudevent \"origintype\" must be a string')\r\n\t}\r\n}\r\n\r\nmodule.exports = Object.freeze({ Cloudevent })\r\n"],"names":["nanoid","require","fetchNodeEnv","name","process","env","module","exports","Object","freeze","Cloudevent","constructor","data","datacontenttype","dataschema","originid","originsource","origintime","origintype","source","specversion","subject","type","this","id","Error","time","Date","toISOString"],"mappings":"AAAA,MAAMA,OAAEA,GAAWC,QAAQ,UAErBC,EAAeC,GAAQC,SAAWA,QAAQC,KAAOD,QAAQC,IAAIF,GAoEnEG,OAAOC,QAAUC,OAAOC,OAAO,CAAEC,WAlEjC,MACCC,aAAYC,KACXA,EADWC,gBAEXA,EAFWC,WAGXA,EAHWC,SAIXA,EAJWC,aAKXA,EALWC,WAMXA,EANWC,WAOXA,EAPWC,OAQXA,EARWC,YASXA,EATWC,QAUXA,EAVWC,KAWXA,IAIA,GADAC,KAAKC,GAAKxB,EAAOE,EAAa,oCAAsC,KAC/DqB,KAAKC,GAAI,UAAUC,MAAM,+BAC9B,GAAuB,sBAAPD,GAAiB,UAAUC,MAAM,oCAGjD,GADAF,KAAKJ,OAASA,GAAUjB,EAAa,6BAChCqB,KAAKJ,OAAQ,UAAUM,MAAM,mCAClC,GAA2B,sBAAXN,OAAqB,UAAUM,MAAM,wCAGrD,GADAF,KAAKD,KAAOA,GACPC,KAAKD,KAAM,UAAUG,MAAM,iCAChC,GAAyB,sBAATH,KAAmB,UAAUG,MAAM,sCAGnD,GADAF,KAAKH,YAAcA,GAAe,OAC7BG,KAAKH,YAAa,UAAUK,MAAM,wCACvC,GAAgC,sBAAhBL,YAA0B,UAAUK,MAAM,6CAQ1D,GALAF,KAAKX,KAAOA,EAEZW,KAAKV,qBAAuC,SAATD,KAChCC,GAAmB,mBACnBA,EACCU,KAAKV,iBAAmD,sBAApBA,gBAA8B,UAAUY,MAAM,iDAGtF,GADAF,KAAKT,WAAaA,EACdS,KAAKT,YAAyC,sBAAfA,WAAyB,UAAUW,MAAM,4CAG5E,GADAF,KAAKF,QAAUA,EACXE,KAAKF,SAAmC,sBAAZA,QAAsB,UAAUI,MAAM,yCAMtE,GAJAF,KAAKG,MAAO,IAAIC,MAAOC,cAGvBL,KAAKR,SAAWA,GAAYQ,KAAKC,IAC5BD,KAAKR,SAAU,UAAUU,MAAM,qCACpC,GAA6B,sBAAbV,SAAuB,UAAUU,MAAM,0CAGvD,GADAF,KAAKP,aAAeA,GAAgBO,KAAKJ,QACpCI,KAAKP,aAAc,UAAUS,MAAM,yCACxC,GAAiC,sBAAjBT,aAA2B,UAAUS,MAAM,8CAG3D,GADAF,KAAKN,WAAaA,GAAcM,KAAKG,MAChCH,KAAKN,WAAY,UAAUQ,MAAM,uCACtC,GAA+B,sBAAfR,WAAyB,UAAUQ,MAAM,4CAGzD,GADAF,KAAKL,WAAaA,GAAcK,KAAKD,MAChCC,KAAKL,WAAY,UAAUO,MAAM,uCACtC,GAA+B,sBAAfP,WAAyB,UAAUO,MAAM"}
@@ -0,0 +1,2 @@
1
+ var t=require("nanoid").nanoid,i=function(t){return process&&process.env&&process.env[t]};module.exports=Object.freeze({Cloudevent:function(e){var r=e.data,o=e.datacontenttype,s=e.dataschema,n=e.originid,h=e.originsource,u=e.origintime,d=e.origintype,a=e.source,g=e.specversion,c=e.subject,p=e.type;if(this.id=t(i("1MILL_CLOUDEVENTS_NANOID_LENGTH")||21),!this.id)throw new Error('Cloudevent "id" is required');if("string"!=typeof this.id)throw new Error('Cloudevent "id" must be a string');if(this.source=a||i("1MILL_CLOUDEVENTS_SOURCE"),!this.source)throw new Error('Cloudevent "source" is required');if("string"!=typeof this.source)throw new Error('Cloudevent "source" must be a string');if(this.type=p,!this.type)throw new Error('Cloudevent "type" is required');if("string"!=typeof this.type)throw new Error('Cloudevent "type" must be a string');if(this.specversion=g||"1.0",!this.specversion)throw new Error('Cloudevent "specversion" is required');if("string"!=typeof this.specversion)throw new Error('Cloudevent "specversion" must be a string');if(this.data=r,this.datacontenttype=void 0!==this.data?o||"application/json":o,this.datacontenttype&&"string"!=typeof this.datacontenttype)throw new Error('Cloudevent "datacontenttype" must be a string');if(this.dataschema=s,this.dataschema&&"string"!=typeof this.dataschema)throw new Error('Cloudevent "dataschema" must be a string');if(this.subject=c,this.subject&&"string"!=typeof this.subject)throw new Error('Cloudevent "subject" must be a string');if(this.time=(new Date).toISOString(),this.originid=n||this.id,!this.originid)throw new Error('Cloudevent "originid" is required');if("string"!=typeof this.originid)throw new Error('Cloudevent "originid" must be a string');if(this.originsource=h||this.source,!this.originsource)throw new Error('Cloudevent "originsource" is required');if("string"!=typeof this.originsource)throw new Error('Cloudevent "originsource" must be a string');if(this.origintime=u||this.time,!this.origintime)throw new Error('Cloudevent "origintime" is required');if("string"!=typeof this.origintime)throw new Error('Cloudevent "origintime" must be a string');if(this.origintype=d||this.type,!this.origintype)throw new Error('Cloudevent "origintype" is required');if("string"!=typeof this.origintype)throw new Error('Cloudevent "origintype" must be a string')}});
2
+ //# sourceMappingURL=index.module.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.module.js","sources":["../src/index.js"],"sourcesContent":["const { nanoid } = require('nanoid')\r\n\r\nconst fetchNodeEnv = name => process && process.env && process.env[name]\r\n\r\nclass Cloudevent {\r\n\tconstructor({\r\n\t\tdata,\r\n\t\tdatacontenttype,\r\n\t\tdataschema,\r\n\t\toriginid,\r\n\t\toriginsource,\r\n\t\torigintime,\r\n\t\torigintype,\r\n\t\tsource,\r\n\t\tspecversion,\r\n\t\tsubject,\r\n\t\ttype,\r\n\t}) {\r\n\t\t// * Required fields by CloudEvent specification\r\n\t\tthis.id = nanoid(fetchNodeEnv('1MILL_CLOUDEVENTS_NANOID_LENGTH') || 21)\r\n\t\tif (!this.id) throw new Error('Cloudevent \"id\" is required')\r\n\t\tif (typeof this.id !== 'string') throw new Error('Cloudevent \"id\" must be a string')\r\n\r\n\t\tthis.source = source || fetchNodeEnv('1MILL_CLOUDEVENTS_SOURCE')\r\n\t\tif (!this.source) throw new Error('Cloudevent \"source\" is required')\r\n\t\tif (typeof this.source !== 'string') throw new Error('Cloudevent \"source\" must be a string')\r\n\r\n\t\tthis.type = type\r\n\t\tif (!this.type) throw new Error('Cloudevent \"type\" is required')\r\n\t\tif (typeof this.type !== 'string') throw new Error('Cloudevent \"type\" must be a string')\r\n\r\n\t\tthis.specversion = specversion || '1.0'\r\n\t\tif (!this.specversion) throw new Error('Cloudevent \"specversion\" is required')\r\n\t\tif (typeof this.specversion !== 'string') throw new Error('Cloudevent \"specversion\" must be a string')\r\n\r\n\t\t// * Optional fields by CloudEvent specification\r\n\t\tthis.data = data\r\n\r\n\t\tthis.datacontenttype = typeof this.data !== 'undefined'\r\n\t\t\t? datacontenttype || 'application/json'\r\n\t\t\t: datacontenttype\r\n\t\tif (this.datacontenttype && typeof this.datacontenttype !== 'string') throw new Error('Cloudevent \"datacontenttype\" must be a string')\r\n\r\n\t\tthis.dataschema = dataschema\r\n\t\tif (this.dataschema && typeof this.dataschema !== 'string') throw new Error('Cloudevent \"dataschema\" must be a string')\r\n\r\n\t\tthis.subject = subject\r\n\t\tif (this.subject && typeof this.subject !== 'string') throw new Error('Cloudevent \"subject\" must be a string')\r\n\r\n\t\tthis.time = new Date().toISOString()\r\n\r\n\t\t// * In-house extentions\r\n\t\tthis.originid = originid || this.id\r\n\t\tif (!this.originid) throw new Error('Cloudevent \"originid\" is required')\r\n\t\tif (typeof this.originid !== 'string') throw new Error('Cloudevent \"originid\" must be a string')\r\n\r\n\t\tthis.originsource = originsource || this.source\r\n\t\tif (!this.originsource) throw new Error('Cloudevent \"originsource\" is required')\r\n\t\tif (typeof this.originsource !== 'string') throw new Error('Cloudevent \"originsource\" must be a string')\r\n\r\n\t\tthis.origintime = origintime || this.time\r\n\t\tif (!this.origintime) throw new Error('Cloudevent \"origintime\" is required')\r\n\t\tif (typeof this.origintime !== 'string') throw new Error('Cloudevent \"origintime\" must be a string')\r\n\r\n\t\tthis.origintype = origintype || this.type\r\n\t\tif (!this.origintype) throw new Error('Cloudevent \"origintype\" is required')\r\n\t\tif (typeof this.origintype !== 'string') throw new Error('Cloudevent \"origintype\" must be a string')\r\n\t}\r\n}\r\n\r\nmodule.exports = Object.freeze({ Cloudevent })\r\n"],"names":["nanoid","require","fetchNodeEnv","name","process","env","module","exports","Object","freeze","Cloudevent","data","datacontenttype","dataschema","originid","originsource","origintime","origintype","source","specversion","subject","type","this","id","Error","time","Date","toISOString"],"mappings":"AAAA,IAAQA,EAAWC,QAAQ,UAAnBD,OAEFE,EAAe,SAAAC,UAAQC,SAAWA,QAAQC,KAAOD,QAAQC,IAAIF,IAoEnEG,OAAOC,QAAUC,OAAOC,OAAO,CAAEC,WAjEhC,gBACCC,IAAAA,KACAC,IAAAA,gBACAC,IAAAA,WACAC,IAAAA,SACAC,IAAAA,aACAC,IAAAA,WACAC,IAAAA,WACAC,IAAAA,OACAC,IAAAA,YACAC,IAAAA,QACAC,IAAAA,KAIA,GADAC,KAAKC,GAAKvB,EAAOE,EAAa,oCAAsC,KAC/DoB,KAAKC,GAAI,UAAUC,MAAM,+BAC9B,GAAuB,sBAAPD,GAAiB,UAAUC,MAAM,oCAGjD,GADAF,KAAKJ,OAASA,GAAUhB,EAAa,6BAChCoB,KAAKJ,OAAQ,UAAUM,MAAM,mCAClC,GAA2B,sBAAXN,OAAqB,UAAUM,MAAM,wCAGrD,GADAF,KAAKD,KAAOA,GACPC,KAAKD,KAAM,UAAUG,MAAM,iCAChC,GAAyB,sBAATH,KAAmB,UAAUG,MAAM,sCAGnD,GADAF,KAAKH,YAAcA,GAAe,OAC7BG,KAAKH,YAAa,UAAUK,MAAM,wCACvC,GAAgC,sBAAhBL,YAA0B,UAAUK,MAAM,6CAQ1D,GALAF,KAAKX,KAAOA,EAEZW,KAAKV,qBAAuC,SAATD,KAChCC,GAAmB,mBACnBA,EACCU,KAAKV,iBAAmD,sBAApBA,gBAA8B,UAAUY,MAAM,iDAGtF,GADAF,KAAKT,WAAaA,EACdS,KAAKT,YAAyC,sBAAfA,WAAyB,UAAUW,MAAM,4CAG5E,GADAF,KAAKF,QAAUA,EACXE,KAAKF,SAAmC,sBAAZA,QAAsB,UAAUI,MAAM,yCAMtE,GAJAF,KAAKG,MAAO,IAAIC,MAAOC,cAGvBL,KAAKR,SAAWA,GAAYQ,KAAKC,IAC5BD,KAAKR,SAAU,UAAUU,MAAM,qCACpC,GAA6B,sBAAbV,SAAuB,UAAUU,MAAM,0CAGvD,GADAF,KAAKP,aAAeA,GAAgBO,KAAKJ,QACpCI,KAAKP,aAAc,UAAUS,MAAM,yCACxC,GAAiC,sBAAjBT,aAA2B,UAAUS,MAAM,8CAG3D,GADAF,KAAKN,WAAaA,GAAcM,KAAKG,MAChCH,KAAKN,WAAY,UAAUQ,MAAM,uCACtC,GAA+B,sBAAfR,WAAyB,UAAUQ,MAAM,4CAGzD,GADAF,KAAKL,WAAaA,GAAcK,KAAKD,MAChCC,KAAKL,WAAY,UAAUO,MAAM,uCACtC,GAA+B,sBAAfP,WAAyB,UAAUO,MAAM"}
@@ -0,0 +1,2 @@
1
+ !function(t){"function"==typeof define&&define.amd?define(t):t()}(function(){var t=require("nanoid").nanoid,e=function(t){return process&&process.env&&process.env[t]};module.exports=Object.freeze({Cloudevent:function(i){var r=i.data,o=i.datacontenttype,n=i.dataschema,s=i.originid,u=i.originsource,h=i.origintime,d=i.origintype,a=i.source,g=i.specversion,c=i.subject,p=i.type;if(this.id=t(e("1MILL_CLOUDEVENTS_NANOID_LENGTH")||21),!this.id)throw new Error('Cloudevent "id" is required');if("string"!=typeof this.id)throw new Error('Cloudevent "id" must be a string');if(this.source=a||e("1MILL_CLOUDEVENTS_SOURCE"),!this.source)throw new Error('Cloudevent "source" is required');if("string"!=typeof this.source)throw new Error('Cloudevent "source" must be a string');if(this.type=p,!this.type)throw new Error('Cloudevent "type" is required');if("string"!=typeof this.type)throw new Error('Cloudevent "type" must be a string');if(this.specversion=g||"1.0",!this.specversion)throw new Error('Cloudevent "specversion" is required');if("string"!=typeof this.specversion)throw new Error('Cloudevent "specversion" must be a string');if(this.data=r,this.datacontenttype=void 0!==this.data?o||"application/json":o,this.datacontenttype&&"string"!=typeof this.datacontenttype)throw new Error('Cloudevent "datacontenttype" must be a string');if(this.dataschema=n,this.dataschema&&"string"!=typeof this.dataschema)throw new Error('Cloudevent "dataschema" must be a string');if(this.subject=c,this.subject&&"string"!=typeof this.subject)throw new Error('Cloudevent "subject" must be a string');if(this.time=(new Date).toISOString(),this.originid=s||this.id,!this.originid)throw new Error('Cloudevent "originid" is required');if("string"!=typeof this.originid)throw new Error('Cloudevent "originid" must be a string');if(this.originsource=u||this.source,!this.originsource)throw new Error('Cloudevent "originsource" is required');if("string"!=typeof this.originsource)throw new Error('Cloudevent "originsource" must be a string');if(this.origintime=h||this.time,!this.origintime)throw new Error('Cloudevent "origintime" is required');if("string"!=typeof this.origintime)throw new Error('Cloudevent "origintime" must be a string');if(this.origintype=d||this.type,!this.origintype)throw new Error('Cloudevent "origintype" is required');if("string"!=typeof this.origintype)throw new Error('Cloudevent "origintype" must be a string')}})});
2
+ //# sourceMappingURL=index.umd.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.umd.js","sources":["../src/index.js"],"sourcesContent":["const { nanoid } = require('nanoid')\r\n\r\nconst fetchNodeEnv = name => process && process.env && process.env[name]\r\n\r\nclass Cloudevent {\r\n\tconstructor({\r\n\t\tdata,\r\n\t\tdatacontenttype,\r\n\t\tdataschema,\r\n\t\toriginid,\r\n\t\toriginsource,\r\n\t\torigintime,\r\n\t\torigintype,\r\n\t\tsource,\r\n\t\tspecversion,\r\n\t\tsubject,\r\n\t\ttype,\r\n\t}) {\r\n\t\t// * Required fields by CloudEvent specification\r\n\t\tthis.id = nanoid(fetchNodeEnv('1MILL_CLOUDEVENTS_NANOID_LENGTH') || 21)\r\n\t\tif (!this.id) throw new Error('Cloudevent \"id\" is required')\r\n\t\tif (typeof this.id !== 'string') throw new Error('Cloudevent \"id\" must be a string')\r\n\r\n\t\tthis.source = source || fetchNodeEnv('1MILL_CLOUDEVENTS_SOURCE')\r\n\t\tif (!this.source) throw new Error('Cloudevent \"source\" is required')\r\n\t\tif (typeof this.source !== 'string') throw new Error('Cloudevent \"source\" must be a string')\r\n\r\n\t\tthis.type = type\r\n\t\tif (!this.type) throw new Error('Cloudevent \"type\" is required')\r\n\t\tif (typeof this.type !== 'string') throw new Error('Cloudevent \"type\" must be a string')\r\n\r\n\t\tthis.specversion = specversion || '1.0'\r\n\t\tif (!this.specversion) throw new Error('Cloudevent \"specversion\" is required')\r\n\t\tif (typeof this.specversion !== 'string') throw new Error('Cloudevent \"specversion\" must be a string')\r\n\r\n\t\t// * Optional fields by CloudEvent specification\r\n\t\tthis.data = data\r\n\r\n\t\tthis.datacontenttype = typeof this.data !== 'undefined'\r\n\t\t\t? datacontenttype || 'application/json'\r\n\t\t\t: datacontenttype\r\n\t\tif (this.datacontenttype && typeof this.datacontenttype !== 'string') throw new Error('Cloudevent \"datacontenttype\" must be a string')\r\n\r\n\t\tthis.dataschema = dataschema\r\n\t\tif (this.dataschema && typeof this.dataschema !== 'string') throw new Error('Cloudevent \"dataschema\" must be a string')\r\n\r\n\t\tthis.subject = subject\r\n\t\tif (this.subject && typeof this.subject !== 'string') throw new Error('Cloudevent \"subject\" must be a string')\r\n\r\n\t\tthis.time = new Date().toISOString()\r\n\r\n\t\t// * In-house extentions\r\n\t\tthis.originid = originid || this.id\r\n\t\tif (!this.originid) throw new Error('Cloudevent \"originid\" is required')\r\n\t\tif (typeof this.originid !== 'string') throw new Error('Cloudevent \"originid\" must be a string')\r\n\r\n\t\tthis.originsource = originsource || this.source\r\n\t\tif (!this.originsource) throw new Error('Cloudevent \"originsource\" is required')\r\n\t\tif (typeof this.originsource !== 'string') throw new Error('Cloudevent \"originsource\" must be a string')\r\n\r\n\t\tthis.origintime = origintime || this.time\r\n\t\tif (!this.origintime) throw new Error('Cloudevent \"origintime\" is required')\r\n\t\tif (typeof this.origintime !== 'string') throw new Error('Cloudevent \"origintime\" must be a string')\r\n\r\n\t\tthis.origintype = origintype || this.type\r\n\t\tif (!this.origintype) throw new Error('Cloudevent \"origintype\" is required')\r\n\t\tif (typeof this.origintype !== 'string') throw new Error('Cloudevent \"origintype\" must be a string')\r\n\t}\r\n}\r\n\r\nmodule.exports = Object.freeze({ Cloudevent })\r\n"],"names":["nanoid","require","fetchNodeEnv","name","process","env","module","exports","Object","freeze","Cloudevent","data","datacontenttype","dataschema","originid","originsource","origintime","origintype","source","specversion","subject","type","this","id","Error","time","Date","toISOString"],"mappings":"6EAAA,IAAQA,EAAWC,QAAQ,UAAnBD,OAEFE,EAAe,SAAAC,UAAQC,SAAWA,QAAQC,KAAOD,QAAQC,IAAIF,IAoEnEG,OAAOC,QAAUC,OAAOC,OAAO,CAAEC,WAjEhC,gBACCC,IAAAA,KACAC,IAAAA,gBACAC,IAAAA,WACAC,IAAAA,SACAC,IAAAA,aACAC,IAAAA,WACAC,IAAAA,WACAC,IAAAA,OACAC,IAAAA,YACAC,IAAAA,QACAC,IAAAA,KAIA,GADAC,KAAKC,GAAKvB,EAAOE,EAAa,oCAAsC,KAC/DoB,KAAKC,GAAI,UAAUC,MAAM,+BAC9B,GAAuB,sBAAPD,GAAiB,UAAUC,MAAM,oCAGjD,GADAF,KAAKJ,OAASA,GAAUhB,EAAa,6BAChCoB,KAAKJ,OAAQ,UAAUM,MAAM,mCAClC,GAA2B,sBAAXN,OAAqB,UAAUM,MAAM,wCAGrD,GADAF,KAAKD,KAAOA,GACPC,KAAKD,KAAM,UAAUG,MAAM,iCAChC,GAAyB,sBAATH,KAAmB,UAAUG,MAAM,sCAGnD,GADAF,KAAKH,YAAcA,GAAe,OAC7BG,KAAKH,YAAa,UAAUK,MAAM,wCACvC,GAAgC,sBAAhBL,YAA0B,UAAUK,MAAM,6CAQ1D,GALAF,KAAKX,KAAOA,EAEZW,KAAKV,qBAAuC,SAATD,KAChCC,GAAmB,mBACnBA,EACCU,KAAKV,iBAAmD,sBAApBA,gBAA8B,UAAUY,MAAM,iDAGtF,GADAF,KAAKT,WAAaA,EACdS,KAAKT,YAAyC,sBAAfA,WAAyB,UAAUW,MAAM,4CAG5E,GADAF,KAAKF,QAAUA,EACXE,KAAKF,SAAmC,sBAAZA,QAAsB,UAAUI,MAAM,yCAMtE,GAJAF,KAAKG,MAAO,IAAIC,MAAOC,cAGvBL,KAAKR,SAAWA,GAAYQ,KAAKC,IAC5BD,KAAKR,SAAU,UAAUU,MAAM,qCACpC,GAA6B,sBAAbV,SAAuB,UAAUU,MAAM,0CAGvD,GADAF,KAAKP,aAAeA,GAAgBO,KAAKJ,QACpCI,KAAKP,aAAc,UAAUS,MAAM,yCACxC,GAAiC,sBAAjBT,aAA2B,UAAUS,MAAM,8CAG3D,GADAF,KAAKN,WAAaA,GAAcM,KAAKG,MAChCH,KAAKN,WAAY,UAAUQ,MAAM,uCACtC,GAA+B,sBAAfR,WAAyB,UAAUQ,MAAM,4CAGzD,GADAF,KAAKL,WAAaA,GAAcK,KAAKD,MAChCC,KAAKL,WAAY,UAAUO,MAAM,uCACtC,GAA+B,sBAAfP,WAAyB,UAAUO,MAAM"}
package/package.json CHANGED
@@ -1,21 +1,34 @@
1
1
  {
2
2
  "name": "@1mill/cloudevents",
3
- "version": "0.11.0",
4
- "description": "Node cloudevents specification and helper functions",
5
- "main": "index.js",
3
+ "version": "2.0.0",
4
+ "description": "Node cloudevents specification and helper",
5
+ "jsnext:main": "dist/index.module.js",
6
+ "main": "dist/index.js",
7
+ "module": "dist/index.module.js",
8
+ "source": "src/index.js",
9
+ "umd:main": "dist/index.umd.js",
10
+ "files": [
11
+ "dist",
12
+ "src"
13
+ ],
6
14
  "scripts": {
7
- "test": "mocha **/*.test.js --recursive"
15
+ "build": "rm -rf ./dist && microbundle",
16
+ "deploy": "npm run build && npm publish",
17
+ "dev": "microbundle watch"
8
18
  },
9
- "author": "@1mill",
10
- "license": "ISC",
19
+ "homepage": "https://github.com/1mill/cloudevents",
20
+ "authors": [
21
+ "Erik Ekberg <nightw0lf@hotmail.com>"
22
+ ],
23
+ "license": "MIT",
11
24
  "repository": {
12
25
  "type": "git",
13
26
  "url": "https://github.com/1mill/cloudevents.git"
14
27
  },
15
28
  "dependencies": {
16
- "kafkajs": "1.12.0"
29
+ "nanoid": "^3.1.25"
17
30
  },
18
31
  "devDependencies": {
19
- "mocha": "^8.1.3"
32
+ "microbundle": "^0.13.3"
20
33
  }
21
34
  }
package/src/index.js ADDED
@@ -0,0 +1,71 @@
1
+ const { nanoid } = require('nanoid')
2
+
3
+ const fetchNodeEnv = name => process && process.env && process.env[name]
4
+
5
+ class Cloudevent {
6
+ constructor({
7
+ data,
8
+ datacontenttype,
9
+ dataschema,
10
+ originid,
11
+ originsource,
12
+ origintime,
13
+ origintype,
14
+ source,
15
+ specversion,
16
+ subject,
17
+ type,
18
+ }) {
19
+ // * Required fields by CloudEvent specification
20
+ this.id = nanoid(fetchNodeEnv('1MILL_CLOUDEVENTS_NANOID_LENGTH') || 21)
21
+ if (!this.id) throw new Error('Cloudevent "id" is required')
22
+ if (typeof this.id !== 'string') throw new Error('Cloudevent "id" must be a string')
23
+
24
+ this.source = source || fetchNodeEnv('1MILL_CLOUDEVENTS_SOURCE')
25
+ if (!this.source) throw new Error('Cloudevent "source" is required')
26
+ if (typeof this.source !== 'string') throw new Error('Cloudevent "source" must be a string')
27
+
28
+ this.type = type
29
+ if (!this.type) throw new Error('Cloudevent "type" is required')
30
+ if (typeof this.type !== 'string') throw new Error('Cloudevent "type" must be a string')
31
+
32
+ this.specversion = specversion || '1.0'
33
+ if (!this.specversion) throw new Error('Cloudevent "specversion" is required')
34
+ if (typeof this.specversion !== 'string') throw new Error('Cloudevent "specversion" must be a string')
35
+
36
+ // * Optional fields by CloudEvent specification
37
+ this.data = data
38
+
39
+ this.datacontenttype = typeof this.data !== 'undefined'
40
+ ? datacontenttype || 'application/json'
41
+ : datacontenttype
42
+ if (this.datacontenttype && typeof this.datacontenttype !== 'string') throw new Error('Cloudevent "datacontenttype" must be a string')
43
+
44
+ this.dataschema = dataschema
45
+ if (this.dataschema && typeof this.dataschema !== 'string') throw new Error('Cloudevent "dataschema" must be a string')
46
+
47
+ this.subject = subject
48
+ if (this.subject && typeof this.subject !== 'string') throw new Error('Cloudevent "subject" must be a string')
49
+
50
+ this.time = new Date().toISOString()
51
+
52
+ // * In-house extentions
53
+ this.originid = originid || this.id
54
+ if (!this.originid) throw new Error('Cloudevent "originid" is required')
55
+ if (typeof this.originid !== 'string') throw new Error('Cloudevent "originid" must be a string')
56
+
57
+ this.originsource = originsource || this.source
58
+ if (!this.originsource) throw new Error('Cloudevent "originsource" is required')
59
+ if (typeof this.originsource !== 'string') throw new Error('Cloudevent "originsource" must be a string')
60
+
61
+ this.origintime = origintime || this.time
62
+ if (!this.origintime) throw new Error('Cloudevent "origintime" is required')
63
+ if (typeof this.origintime !== 'string') throw new Error('Cloudevent "origintime" must be a string')
64
+
65
+ this.origintype = origintype || this.type
66
+ if (!this.origintype) throw new Error('Cloudevent "origintype" is required')
67
+ if (typeof this.origintype !== 'string') throw new Error('Cloudevent "origintype" must be a string')
68
+ }
69
+ }
70
+
71
+ module.exports = Object.freeze({ Cloudevent })
package/.editorconfig DELETED
@@ -1,21 +0,0 @@
1
- # editorconfig.org
2
- root = true
3
-
4
- [*]
5
- charset = utf-8
6
- end_of_line = crlf
7
- indent_size = 8
8
- indent_style = tab
9
- insert_final_newline = true
10
- trim_trailing_whitespace = true
11
-
12
- [*.{yaml,yml,json}]
13
- indent_size = 2
14
- indent_style = space
15
-
16
- [*.md]
17
- indent_size = 2
18
- indent_style = space
19
-
20
- [*.{bash,bashrc,pem,sh}]
21
- end_of_line = lf
package/index.js DELETED
@@ -1,6 +0,0 @@
1
- const { v3 } = require('./v3')
2
- const { v4 } = require('./v4')
3
- const { v5 } = require('./v5')
4
- const { v6 } = require('./v6')
5
-
6
- module.exports = Object.freeze({ v3, v4, v5, v6 })
@@ -1,51 +0,0 @@
1
- const createCloudevent = ({
2
- data,
3
- datacontenttype,
4
- dlx = 'dlx',
5
- enrichmentdata,
6
- enrichmentdatacontenttype,
7
- id,
8
- source,
9
- specversion = '1.0',
10
- time,
11
- type,
12
- }) => {
13
- if (!id) { throw new Error('Cloudevent "id" is as required') }
14
- if (!source) { throw new Error('Cloudevent "source" is as required') }
15
- if (!type) { throw new Error('Cloudevent "type" is as required') }
16
-
17
- const isDataEncoded = data !== undefined && datacontenttype !== undefined
18
- const isEnrichmentEncoded = enrichmentdata !== undefined && enrichmentdatacontenttype !== undefined
19
-
20
- const cloudevent = {
21
- // Required defaults
22
- dlx,
23
- id,
24
- source,
25
- specversion,
26
- time: time || new Date().toISOString(),
27
- type,
28
-
29
- // Optional original data
30
- data: isDataEncoded
31
- ? data
32
- : JSON.stringify(data),
33
- datacontenttype: isDataEncoded
34
- ? datacontenttype
35
- : 'application/json',
36
-
37
- // Optional enrichment data
38
- enrichmentdata: isEnrichmentEncoded
39
- ? enrichmentdata
40
- : JSON.stringify(enrichmentdata),
41
- enrichmentdatacontenttype: isEnrichmentEncoded
42
- ? enrichmentcontenttype
43
- : 'application/json',
44
- enrichmenttime: enrichmentdata === undefined
45
- ? undefined
46
- : new Date().toISOString(),
47
- }
48
- return cloudevent
49
- }
50
-
51
- 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 }
@@ -1,15 +0,0 @@
1
- const { createCloudevent } = require('./createCloudevent')
2
-
3
- const enrichCloudevent = ({
4
- cloudevent,
5
- enrichmentdata,
6
- enrichmentdatacontenttype,
7
- }) => {
8
- return createCloudevent({
9
- ...cloudevent,
10
- enrichmentdata,
11
- enrichmentdatacontenttype,
12
- })
13
- }
14
-
15
- module.exports = { enrichCloudevent }
package/v3/index.js DELETED
@@ -1,16 +0,0 @@
1
- const { PROTOCOL_KAFKA, PROTOCOL_LAMBDA } = require("./lib/constants");
2
- const { createCloudevent } = require('./createCloudevent')
3
- const { createEventStream } = require('./createEventStream')
4
- const { enrichCloudevent } = require('./enrichCloudevent')
5
- const { isEnriched } = require('./isEnriched')
6
-
7
- const v3 = {
8
- PROTOCOL_KAFKA,
9
- PROTOCOL_LAMBDA,
10
- createCloudevent,
11
- createEventStream,
12
- enrichCloudevent,
13
- isEnriched,
14
- }
15
-
16
- module.exports = { v3 }
package/v3/isEnriched.js DELETED
@@ -1,5 +0,0 @@
1
- const isEnriched = ({ cloudevent }) => {
2
- return cloudevent.enrichmentdata !== undefined;
3
- }
4
-
5
- module.exports = { isEnriched }
@@ -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
- const { connect, disconnect, send } = kafka.producer()
23
-
24
- const emit = async ({ cloudevent }) => {
25
- await connect()
26
- const { event } = convertTo({ cloudevent })
27
- await send(event)
28
- await disconnect()
29
- }
30
- return { emit }
31
- }
32
-
33
- module.exports = { createEmit }