@sockethub/server 5.0.0-alpha.3

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 (118) hide show
  1. package/LICENSE +165 -0
  2. package/README.md +136 -0
  3. package/bin/sockethub +3 -0
  4. package/coverage/tmp/coverage-93126-1649152190997-0.json +1 -0
  5. package/dist/bootstrap/init.d.ts +18 -0
  6. package/dist/bootstrap/init.js +63 -0
  7. package/dist/bootstrap/init.js.map +1 -0
  8. package/dist/bootstrap/platforms.js +75 -0
  9. package/dist/common.d.ts +3 -0
  10. package/dist/common.js +20 -0
  11. package/dist/common.js.map +1 -0
  12. package/dist/config.d.ts +6 -0
  13. package/dist/config.js +102 -0
  14. package/dist/config.js.map +1 -0
  15. package/dist/crypto.d.ts +10 -0
  16. package/dist/crypto.js +38 -0
  17. package/dist/crypto.js.map +1 -0
  18. package/dist/defaults.json +28 -0
  19. package/dist/index.d.ts +2 -0
  20. package/dist/index.js +25 -0
  21. package/dist/index.js.map +1 -0
  22. package/dist/janitor.d.ts +15 -0
  23. package/dist/janitor.js +89 -0
  24. package/dist/janitor.js.map +1 -0
  25. package/dist/listener.d.ts +28 -0
  26. package/dist/listener.js +91 -0
  27. package/dist/listener.js.map +1 -0
  28. package/dist/middleware/create-activity-object.d.ts +6 -0
  29. package/dist/middleware/create-activity-object.js +19 -0
  30. package/dist/middleware/create-activity-object.js.map +1 -0
  31. package/dist/middleware/expand-activity-stream.d.ts +2 -0
  32. package/dist/middleware/expand-activity-stream.js +33 -0
  33. package/dist/middleware/expand-activity-stream.js.map +1 -0
  34. package/dist/middleware/expand-activity-stream.test.data.d.ts +480 -0
  35. package/dist/middleware/expand-activity-stream.test.data.js +360 -0
  36. package/dist/middleware/expand-activity-stream.test.data.js.map +1 -0
  37. package/dist/middleware/store-credentials.d.ts +3 -0
  38. package/dist/middleware/store-credentials.js +19 -0
  39. package/dist/middleware/store-credentials.js.map +1 -0
  40. package/dist/middleware/validate.d.ts +2 -0
  41. package/dist/middleware/validate.js +58 -0
  42. package/dist/middleware/validate.js.map +1 -0
  43. package/dist/middleware/validate.test.data.d.ts +532 -0
  44. package/dist/middleware/validate.test.data.js +263 -0
  45. package/dist/middleware/validate.test.data.js.map +1 -0
  46. package/dist/middleware.d.ts +10 -0
  47. package/dist/middleware.js +54 -0
  48. package/dist/middleware.js.map +1 -0
  49. package/dist/platform-instance.d.ts +77 -0
  50. package/dist/platform-instance.js +211 -0
  51. package/dist/platform-instance.js.map +1 -0
  52. package/dist/platform.d.ts +6 -0
  53. package/dist/platform.js +187 -0
  54. package/dist/platform.js.map +1 -0
  55. package/dist/process-manager.d.ts +11 -0
  56. package/dist/process-manager.js +78 -0
  57. package/dist/process-manager.js.map +1 -0
  58. package/dist/routes.d.ts +13 -0
  59. package/dist/routes.js +83 -0
  60. package/dist/routes.js.map +1 -0
  61. package/dist/sockethub.d.ts +39 -0
  62. package/dist/sockethub.js +119 -0
  63. package/dist/sockethub.js.map +1 -0
  64. package/dist/store.d.ts +5 -0
  65. package/dist/store.js +17 -0
  66. package/dist/store.js.map +1 -0
  67. package/package.json +103 -0
  68. package/sockethub.config.example.json +33 -0
  69. package/src/bootstrap/init.d.ts +8 -0
  70. package/src/bootstrap/init.ts +83 -0
  71. package/src/bootstrap/platforms.js +75 -0
  72. package/src/common.test.ts +54 -0
  73. package/src/common.ts +14 -0
  74. package/src/config.d.ts +2 -0
  75. package/src/config.test.ts +28 -0
  76. package/src/config.ts +94 -0
  77. package/src/crypto.d.ts +5 -0
  78. package/src/crypto.test.ts +41 -0
  79. package/src/crypto.ts +41 -0
  80. package/src/defaults.json +28 -0
  81. package/src/index.ts +28 -0
  82. package/src/janitor.d.ts +8 -0
  83. package/src/janitor.ts +89 -0
  84. package/src/listener.ts +79 -0
  85. package/src/middleware/create-activity-object.test.ts +10 -0
  86. package/src/middleware/create-activity-object.ts +13 -0
  87. package/src/middleware/expand-activity-stream.test.data.ts +365 -0
  88. package/src/middleware/expand-activity-stream.test.ts +78 -0
  89. package/src/middleware/expand-activity-stream.ts +27 -0
  90. package/src/middleware/store-credentials.test.ts +73 -0
  91. package/src/middleware/store-credentials.ts +16 -0
  92. package/src/middleware/validate.d.ts +1 -0
  93. package/src/middleware/validate.test.data.ts +261 -0
  94. package/src/middleware/validate.test.ts +83 -0
  95. package/src/middleware/validate.ts +49 -0
  96. package/src/middleware.d.ts +21 -0
  97. package/src/middleware.test.ts +154 -0
  98. package/src/middleware.ts +52 -0
  99. package/src/platform-instance.test.ts +237 -0
  100. package/src/platform-instance.ts +242 -0
  101. package/src/platform.ts +191 -0
  102. package/src/process-manager.ts +64 -0
  103. package/src/routes.test.ts +100 -0
  104. package/src/routes.ts +98 -0
  105. package/src/sockethub.d.ts +1 -0
  106. package/src/sockethub.ts +165 -0
  107. package/src/store.test.ts +28 -0
  108. package/src/store.ts +17 -0
  109. package/test/init-suite.js +41 -0
  110. package/test/queue.functional.test.js +0 -0
  111. package/test/sockethub-suite.js +25 -0
  112. package/tsconfig.json +18 -0
  113. package/views/examples/dummy.ejs +93 -0
  114. package/views/examples/feeds.ejs +90 -0
  115. package/views/examples/irc.ejs +239 -0
  116. package/views/examples/shared.js +72 -0
  117. package/views/examples/xmpp.ejs +191 -0
  118. package/views/index.ejs +17 -0
@@ -0,0 +1,73 @@
1
+ import { expect } from 'chai';
2
+ import * as sinon from 'sinon';
3
+
4
+ import storeCredentials from "./store-credentials";
5
+
6
+ const creds = {
7
+ "id":"blah",
8
+ "type":"credentials",
9
+ "context":"dummy",
10
+ "actor":{
11
+ "id":"dood@irc.freenode.net",
12
+ "type":"person",
13
+ "name":"dood"
14
+ },
15
+ "target":{
16
+ "id":"irc.freenode.net/service",
17
+ "type":"person",
18
+ "name":"service"
19
+ },
20
+ "object":{
21
+ "type":"credentials"
22
+ }
23
+ };
24
+
25
+ describe('Middleware: storeCredentials', () => {
26
+ let storeSuccess: any, storeError: any, saveErrorFake: any,
27
+ saveSuccessFake: any, sessionLogStub: any;
28
+
29
+ beforeEach(() => {
30
+ storeSuccess = {
31
+ save: (id: any, creds: any, cb: Function) => {
32
+ cb();
33
+ }
34
+ };
35
+ storeError = {
36
+ save: (id: any, creds: any, cb: Function) => {
37
+ cb('some error');
38
+ }
39
+ };
40
+ saveSuccessFake = sinon.replace(storeSuccess, 'save', sinon.fake(storeSuccess.save));
41
+ saveErrorFake = sinon.replace(storeError, 'save', sinon.fake(storeError.save));
42
+ sessionLogStub = sinon.stub();
43
+ });
44
+
45
+ afterEach(() => {
46
+ sinon.reset();
47
+ });
48
+
49
+ it('returns a middleware handler', () => {
50
+ const sc = storeCredentials(storeSuccess, sessionLogStub);
51
+ expect(typeof sc).to.equal('function');
52
+ expect(saveSuccessFake.callCount).to.equal(0);
53
+ expect(sessionLogStub.callCount).to.equal(0);
54
+ });
55
+
56
+ it('successfully store credentials', () => {
57
+ const sc = storeCredentials(storeSuccess, sessionLogStub);
58
+ sc(creds, (err: any) => {
59
+ expect(saveSuccessFake.callCount).to.equal(1);
60
+ expect(saveSuccessFake.firstArg).to.equal(creds.actor.id);
61
+ expect(err).to.be.undefined;
62
+ });
63
+ });
64
+
65
+ it('handle error while storing credentials', () => {
66
+ const sc = storeCredentials(storeError, sessionLogStub);
67
+ sc(creds, (err: any) => {
68
+ expect(saveErrorFake.callCount).to.equal(1);
69
+ expect(saveErrorFake.firstArg).to.equal(creds.actor.id);
70
+ expect(err).to.eql('some error');
71
+ });
72
+ });
73
+ });
@@ -0,0 +1,16 @@
1
+ import { IActivityStream } from "@sockethub/schemas";
2
+ import { ISecureStoreInstance } from "../store";
3
+
4
+ export default function storeCredentials(store: ISecureStoreInstance, sessionLog: Function) {
5
+ return (creds: IActivityStream, done: Function) => {
6
+ store.save(creds.actor.id, creds, (err) => {
7
+ if (err) {
8
+ sessionLog('error saving credentials to store ' + err);
9
+ done(err);
10
+ } else {
11
+ sessionLog('credentials encrypted and saved');
12
+ done();
13
+ }
14
+ });
15
+ };
16
+ };
@@ -0,0 +1 @@
1
+ export default function validate(type: any): (next: any, msg: any) => void;
@@ -0,0 +1,261 @@
1
+ export default [
2
+ {
3
+ "name": "mismatched types",
4
+ "valid": false,
5
+ "type": "credentials",
6
+ "input":{
7
+ "id":"blah",
8
+ "type":"send",
9
+ "context":"fake",
10
+ "actor":{
11
+ "id":"dood@irc.freenode.net",
12
+ "type":"person",
13
+ "name":"dood"
14
+ },
15
+ "target":{
16
+ "id":"irc.freenode.net/service",
17
+ "type":"person",
18
+ "name":"service"
19
+ },
20
+ "object":{
21
+ "type":"credentials",
22
+ "user": 'foo',
23
+ "pass": 'bar'
24
+ }
25
+ },
26
+ 'error': "Error: credential activity streams must have credentials set as type"
27
+ },
28
+ {
29
+ "name": "basic",
30
+ "valid":true,
31
+ "type":"credentials",
32
+ "input":{
33
+ "id":"blah",
34
+ "type":"credentials",
35
+ "context":"fake",
36
+ "actor":{
37
+ "id":"dood@irc.freenode.net",
38
+ "type":"person",
39
+ "name":"dood"
40
+ },
41
+ "object":{
42
+ 'type': 'credentials',
43
+ "user": 'foo',
44
+ "pass": 'bar'
45
+ }
46
+ },
47
+ "output":"same"
48
+ },
49
+ {
50
+ "name":"no type specified",
51
+ "valid":false,
52
+ "type":"credentials",
53
+ "input":{
54
+ "actor":"hyper_rau@localhost",
55
+ "context":"fake",
56
+ "object":{
57
+ "username":"hyper_rau",
58
+ "password":"123",
59
+ "server":"localhost",
60
+ "port":5222,
61
+ "resource":"laptop"
62
+ }
63
+ },
64
+ "error": "Error: credential activity streams must have credentials set as type"
65
+ },
66
+ {
67
+ "name":"basic person",
68
+ "type":"activity-object",
69
+ "valid":true,
70
+ "input":{
71
+ "id":"blah",
72
+ "type":"person",
73
+ "name":"dood"
74
+ },
75
+ "output":"same"
76
+ },
77
+ {
78
+ "name":"person with extras",
79
+ "valid":true,
80
+ "type":"activity-object",
81
+ "input":{
82
+ "id":"blah",
83
+ "type":"person",
84
+ "name":"bob",
85
+ "hello":"there",
86
+ "i":[
87
+ "am",
88
+ "extras"
89
+ ]
90
+ },
91
+ "output":"same"
92
+ },
93
+ {
94
+ "name":"alone credentials (as activity-object)",
95
+ "valid":false,
96
+ "type":"activity-object",
97
+ "input":{
98
+ "type":"credentials",
99
+ "nick":"sh-9K3Vk",
100
+ "port":6667,
101
+ "secure":false,
102
+ "server":"irc.freenode.net"
103
+ },
104
+ "error":
105
+ "Error: /object: must match exactly one schema in oneOf: " +
106
+ "credentials, feed, message, me, person, room, service, website, " +
107
+ "attendance, presence, relationship, topic, address"
108
+ },
109
+ {
110
+ "name":"alone credentials (as credentials)",
111
+ "valid":false,
112
+ "type":"credentials",
113
+ "input":{
114
+ "type":"credentials",
115
+ "nick":"sh-9K3Vk",
116
+ "port":6667,
117
+ "secure":false,
118
+ "server":"irc.freenode.net"
119
+ },
120
+ "error": "Error: platform context undefined not registered with this Sockethub instance."
121
+ },
122
+ {
123
+ "name":"new person",
124
+ "valid":true,
125
+ "type":"activity-object",
126
+ "input":{
127
+ "id":"sh-9K3Vk@irc.freenode.net",
128
+ "type":"person",
129
+ "name":"sh-9K3Vk",
130
+ "image":{
131
+ "height":250,
132
+ "mediaType":"image/jpeg",
133
+ "url":"http://example.org/image.jpg",
134
+ "width":250
135
+ },
136
+ "url":"http://sockethub.org"
137
+ },
138
+ "output":"same"
139
+ },
140
+ {
141
+ "name":"new person",
142
+ "valid":true,
143
+ "type":"activity-object",
144
+ "input":{
145
+ "id":"irc://sh-9K3Vk@irc.freenode.net",
146
+ "type":"person",
147
+ "name":"sh-9K3Vk",
148
+ "url":"http://sockethub.org"
149
+ },
150
+ "output":"same"
151
+ },
152
+ {
153
+ "name":"bad parent object",
154
+ "valid":false,
155
+ "type":"activity-stream",
156
+ "input":{
157
+ "string":"this is a string",
158
+ "array":[
159
+ "this",
160
+ "is",
161
+ {
162
+ "an":"array"
163
+ }
164
+ ],
165
+ "as":{
166
+ "id":"blah",
167
+ "type":"send",
168
+ "context":"hello",
169
+ "actor":{
170
+ "name":"dood"
171
+ },
172
+ "target":{
173
+ "type":"person",
174
+ "name":"bob"
175
+ },
176
+ "object":{
177
+ "type":"credentials"
178
+ }
179
+ },
180
+ "noId":{
181
+ "name":"dood"
182
+ },
183
+ "noId2":{
184
+ "type":"person",
185
+ "name":"bob"
186
+ },
187
+ "noDisplayName":{
188
+ "id":"larg"
189
+ }
190
+ },
191
+ "error": "Error: platform context undefined not registered with this Sockethub instance."
192
+ },
193
+ {
194
+ "name":"unexpected AS",
195
+ "valid":false,
196
+ "type":"message",
197
+ "input":{
198
+ "actor":"irc://uuu@localhost",
199
+ "type":"join",
200
+ "context":"fake",
201
+ "target":"irc://irc.dooder.net/a-room"
202
+ },
203
+ "error": "Error: /actor: must be object"
204
+ },
205
+ {
206
+ "name":"missing type property",
207
+ "valid":false,
208
+ "type":"message",
209
+ "input":{
210
+ "actor": { "id": "irc://uuu@localhost", "type": "person" },
211
+ "context":"fake",
212
+ "target": { "id": "irc://irc.dooder.net/a-room", "type": "room" }
213
+ },
214
+ "error": "Error: activity stream: must have required property \'type\'"
215
+ },
216
+ {
217
+ "name":"invalid context property",
218
+ "valid":false,
219
+ "type":"message",
220
+ "input":{
221
+ "actor": { "id": "irc://uuu@localhost", "type": "person" },
222
+ "type":"foo",
223
+ "context": "foobar",
224
+ "target": { "id": "irc://irc.dooder.net/a-room", "type": "room" }
225
+ },
226
+ "error": "Error: platform context foobar not registered with this Sockethub instance."
227
+ },
228
+ {
229
+ "name":"missing actor property",
230
+ "valid":false,
231
+ "type":"message",
232
+ "input":{
233
+ "type": "foo",
234
+ "context":"fake",
235
+ "target": { "id": "irc://irc.dooder.net/a-room", "type": "room" }
236
+ },
237
+ "error": "Error: activity stream: must have required property \'actor\'"
238
+ },
239
+ {
240
+ "name":"traditional message",
241
+ "valid":true,
242
+ "type":"message",
243
+ "input":{
244
+ "type": "echo",
245
+ "context": "fake",
246
+ "actor": { "id": "irc://uuu@localhost", "type": "person" }
247
+ }
248
+ },
249
+ {
250
+ "name":"message with wrong type",
251
+ "valid":false,
252
+ "type":"message",
253
+ "input":{
254
+ "type": "foorg",
255
+ "context": "fake",
256
+ "actor": { "id": "irc://uuu@localhost", "type": "person" }
257
+ },
258
+ "error": "Error: platform type foorg not supported by fake platform. " +
259
+ "(types: credentials, echo, fail)"
260
+ }
261
+ ];
@@ -0,0 +1,83 @@
1
+ import { expect } from 'chai';
2
+ import proxyquire from 'proxyquire';
3
+
4
+ proxyquire.noPreserveCache();
5
+ proxyquire.noCallThru();
6
+
7
+ class FakeSockethubPlatform {
8
+ constructor() {}
9
+ get config() {
10
+ return {};
11
+ }
12
+ get schema() {
13
+ return {
14
+ name: 'fake',
15
+ version: 'infinity',
16
+ credentials: {},
17
+ messages: {
18
+ "required": ["type"],
19
+ "properties": {
20
+ "type": {
21
+ "enum": ["echo", "fail"]
22
+ }
23
+ }
24
+ }
25
+ }
26
+ }
27
+ }
28
+ const modules = {
29
+ 'fake-sockethub-platform': FakeSockethubPlatform
30
+ }
31
+
32
+ // @ts-ignore
33
+ import platformLoad from './../bootstrap/platforms';
34
+ const platforms = platformLoad(['fake-sockethub-platform'], (module) => {
35
+ return modules[module];
36
+ });
37
+ const validateMod = proxyquire('./validate', {
38
+ '../bootstrap/init': {
39
+ platforms: platforms
40
+ }
41
+ });
42
+ const validate = validateMod.default;
43
+
44
+ import asObjects from "./validate.test.data";
45
+
46
+ describe('platformLoad', () => {
47
+ it('loads all platforms', () => {
48
+ const expectedPlatforms = ['fake'];
49
+ expect(platforms.size).to.equal(expectedPlatforms.length);
50
+ for (let platform of expectedPlatforms) {
51
+ expect(platforms.has(platform)).to.be.true;
52
+ }
53
+ });
54
+ });
55
+
56
+ describe('Middleware: Validate', () => {
57
+
58
+ describe('AS object validations', () => {
59
+ asObjects.forEach((obj) => {
60
+ it(`${obj.type}: ${obj.name}, should ${obj.valid ? 'pass' : 'fail'}`, (done) => {
61
+ // @ts-ignore
62
+ validate(obj.type, 'tests')(obj.input, (msg) => {
63
+ if (obj.output) {
64
+ if (obj.output === 'same') {
65
+ expect(msg).to.eql(obj.input);
66
+ } else {
67
+ expect(msg).to.eql(obj.output);
68
+ }
69
+ }
70
+ if (obj.valid) {
71
+ expect(msg).to.not.be.instanceof(Error);
72
+ } else {
73
+ expect(msg).to.be.instanceof(Error);
74
+ if (obj.error) {
75
+ expect(msg.toString()).to.equal(obj.error);
76
+ }
77
+ }
78
+ done();
79
+ });
80
+ });
81
+ });
82
+ });
83
+ });
@@ -0,0 +1,49 @@
1
+ /**
2
+ * responsible for handling the validation and expansion (when applicable) of all incoming objects
3
+ */
4
+ import debug from 'debug';
5
+ import schemas from '@sockethub/schemas';
6
+ import { IActivityStream } from "@sockethub/schemas";
7
+
8
+ // @ts-ignore
9
+ import init from "../bootstrap/init";
10
+
11
+ init.platforms.forEach((platform) => {
12
+ Object.keys(platform.schemas).forEach((key) => {
13
+ if (! platform.schemas[key]) { return; }
14
+ schemas.addPlatformSchema(platform.schemas[key], `${platform.id}/${key}`);
15
+ });
16
+ });
17
+
18
+ // called when registered with the middleware function, define the type of validation
19
+ // that will be called when the middleware eventually does.
20
+ export default function validate(type: string, sockethubId: string) {
21
+ const sessionLog = debug(`sockethub:validate:${sockethubId}`);
22
+ return (msg: IActivityStream, done: Function) => {
23
+ sessionLog('applying schema validation for ' + type);
24
+ if (type === 'activity-object') {
25
+ const err = schemas.validateActivityObject(msg);
26
+ err ? done(new Error(err)) : done(msg);
27
+ } else if (! init.platforms.has(msg.context)) {
28
+ return done(
29
+ new Error(`platform context ${msg.context} not registered with this Sockethub instance.`)
30
+ );
31
+ } else if (type === 'credentials') {
32
+ const err = schemas.validateCredentials(msg);
33
+ err ? done(new Error(err)) : done(msg);
34
+ } else {
35
+ const err = schemas.validateActivityStream(msg);
36
+ if (err) {
37
+ done(new Error(err));
38
+ } else {
39
+ const platformMeta = init.platforms.get(msg.context);
40
+ if (!platformMeta.types.includes(msg.type)) {
41
+ return done(new Error(`platform type ${msg.type} not supported by ${msg.context} ` +
42
+ `platform. (types: ${platformMeta.types.join(', ')})`));
43
+ } else {
44
+ done(msg);
45
+ }
46
+ }
47
+ }
48
+ };
49
+ };
@@ -0,0 +1,21 @@
1
+ /**
2
+ * a very simple middleware handler
3
+ *
4
+ * When initialized, provide a function which will be called if there were any failures during
5
+ * the execution of functions along the chain.
6
+ *
7
+ * Use middleware.chain, passing in a list of functions to call in order. It then returns
8
+ * a function which accepts the message from the input. That function then will
9
+ * call each of the originally passed in functions, in order, with a `next` callback as
10
+ * the first parameter, and any number of originating parameters.
11
+ *
12
+ * As one middleware function is done, they call `next` with the first argument `true`
13
+ * (succeeded, continue) any parameters to pass along.
14
+ *
15
+ * If any of the middleware function calls the `next` handler with `false` as the first param, the
16
+ * execution of the function chain is halted, and the failure callback is called. Again, any
17
+ * number of params passed after the `false` will be passed to the failure callback.
18
+ *
19
+ */
20
+ declare function Middleware(errorHandler: any): void;
21
+ export default Middleware;
@@ -0,0 +1,154 @@
1
+ import { expect } from 'chai';
2
+ import * as sinon from 'sinon';
3
+
4
+ import middleware, { MiddlewareChain } from "./middleware";
5
+
6
+ describe("Middleware", () => {
7
+ it("middleware() is a function", () => {
8
+ expect(typeof middleware).to.be.equal('function');
9
+ });
10
+
11
+ it('returns a MiddlewareChain instance', () => {
12
+ const mw = middleware('testa');
13
+ expect(mw).to.be.instanceof(MiddlewareChain);
14
+ const mwc = new MiddlewareChain('testa');
15
+ expect(mw.name).to.be.eql(mwc.name);
16
+ });
17
+
18
+ it("only accepts functions", () => {
19
+ const mw = middleware('test');
20
+ // @ts-ignore
21
+ expect(()=>{mw.use('foobar');}).to.throw(
22
+ 'middleware use() can only take a function as an argument');
23
+ });
24
+
25
+ it("only accepts functions that expect 2 or 3 params", () => {
26
+ const mw = middleware('test');
27
+ // @ts-ignore
28
+ expect(()=>{mw.use((one, two, three, four) => {});}).to.throw(
29
+ 'middleware function provided with incorrect number of params: 4');
30
+ });
31
+
32
+ it("calls each member of call list", (done) => {
33
+ const callback = (data: any, cb: Function) => { cb(data); };
34
+ const funcs = [ sinon.spy(callback), sinon.spy(callback), sinon.spy(callback) ];
35
+ const mw = middleware('test');
36
+ for (let func of funcs) {
37
+ // @ts-ignore
38
+ const entry = mw.use(func);
39
+ }
40
+ mw.done()('some data', (data: any) => {
41
+ expect(data).to.eql('some data');
42
+ // @ts-ignore
43
+ funcs.unshift(callback);
44
+ for (let i = 1; i < funcs.length; i++) {
45
+ expect(funcs[i].calledOnce).to.be.true;
46
+ expect(funcs[i].calledWith('foobar', funcs[i - 1]));
47
+ }
48
+ done();
49
+ });
50
+ });
51
+
52
+ it("does not throw exception on error with no callback provided", (done) => {
53
+ let errorHandlerCalled = false;
54
+ const callbackError = (data: any, cb: (arg0: Error) => void) => {
55
+ cb(new Error('some error')); };
56
+ const funcs = [ sinon.spy(callbackError) ];
57
+ const mw = middleware('test');
58
+ for (let func of funcs) {
59
+ mw.use(func);
60
+ }
61
+ mw.use((err: any, data: any, next: Function) => {
62
+ expect(err.toString()).to.equal('Error: some error');
63
+ errorHandlerCalled = true;
64
+ next(err);
65
+ });
66
+ mw.done()('foobar', () => {
67
+ expect(errorHandlerCalled).to.be.true;
68
+ done();
69
+ });
70
+ });
71
+
72
+ it("aborts call stack on error - calls error handler, and callback", (done) => {
73
+ let errorHandlerCalled = false;
74
+ const callback = (data: any, cb: Function) => { cb(data); };
75
+ const callbackError = (data: any, cb: (arg0: Error) => void) => {
76
+ cb(new Error('some error'));
77
+ };
78
+ const funcs = [ sinon.spy(callback), sinon.spy(callbackError), sinon.spy(callback) ];
79
+ const mw = middleware('test');
80
+ for (let func of funcs) {
81
+ mw.use(func);
82
+ }
83
+ mw.use((err: any, data: any, next: Function) => {
84
+ expect(err.toString()).to.equal('Error: some error');
85
+ errorHandlerCalled = true;
86
+ next(err);
87
+ });
88
+ mw.done()('foobar', (data: any) => {
89
+ // FIXME -- We need to also handle socket.io callbacks!
90
+ expect(data instanceof Error).to.be.true;
91
+ expect(funcs[0].calledOnce).to.be.true;
92
+ expect(funcs[0].calledWith('foobar', callback));
93
+ expect(funcs[1].calledOnce).to.be.true;
94
+ expect(funcs[1].calledWith('foobar', funcs[0]));
95
+ expect(funcs[2].calledOnce).to.be.false;
96
+ expect(errorHandlerCalled).to.be.true;
97
+ done();
98
+ });
99
+ });
100
+
101
+ it("error handler receives error and no callback provided", (done) => {
102
+ let errorHandlerCalled = false;
103
+ const callback = (data: any, cb: Function) => { cb(data); };
104
+ const callbackError = (data: any, cb: (arg0: Error) => void) => {
105
+ cb(new Error('some error'));
106
+ };
107
+ const funcs = [ sinon.spy(callback), sinon.spy(callback), sinon.spy(callbackError) ];
108
+ const mw = middleware('test');
109
+ for (let func of funcs) {
110
+ mw.use(func);
111
+ }
112
+ mw.use((err: any, data: any, next: Function) => {
113
+ expect(err instanceof Error).to.be.true;
114
+ expect(err.toString()).to.equal('Error: some error');
115
+ errorHandlerCalled = true;
116
+ expect(funcs[0].calledOnce).to.be.true;
117
+ expect(funcs[0].calledWith('foobar', callback));
118
+ expect(funcs[1].calledOnce).to.be.true;
119
+ expect(funcs[1].calledWith('foobar', funcs[0]));
120
+ expect(funcs[2].calledOnce).to.be.true;
121
+ expect(funcs[2].calledWith('foobar', funcs[1]));
122
+ expect(errorHandlerCalled).to.be.true;
123
+ setTimeout(done, 0);
124
+ });
125
+ mw.done()('foobar', () => {});
126
+ });
127
+
128
+ it("calls each member of chain (50)", (done) => {
129
+ let errorHandlerCalled = false;
130
+ const callback = (data: any, cb: Function) => { cb(data); };
131
+ let funcs: Array<any> = [];
132
+ for (let i = 0; i <= 50; i++) {
133
+ funcs.push(sinon.spy(callback));
134
+ }
135
+ const mw = middleware('test');
136
+ for (let func of funcs) {
137
+ mw.use(func);
138
+ }
139
+ mw.use((err: any, data: any, next: Function) => {
140
+ expect(err.toString()).to.equal('Error: some error');
141
+ errorHandlerCalled = true;
142
+ });
143
+ mw.done()('some data', (data: any) => {
144
+ expect(data).to.equal('some data');
145
+ funcs.unshift(callback);
146
+ for (let i = 1; i < funcs.length; i++) {
147
+ expect(funcs[i].calledOnce).to.be.true;
148
+ expect(funcs[i].calledWith('foo', funcs[i - 1]));
149
+ }
150
+ expect(errorHandlerCalled).to.be.false;
151
+ done();
152
+ });
153
+ });
154
+ });