Skip to content
This repository was archived by the owner on Sep 3, 2022. It is now read-only.

Commit 8b36b99

Browse files
authored
Merge pull request #139 from segmentio/public-middleware
rm checks to disallow adding middleware post-init
2 parents 4c08f5b + edb3e06 commit 8b36b99

File tree

5 files changed

+57
-28
lines changed

5 files changed

+57
-28
lines changed

HISTORY.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1+
# 3.12.4 / 2020-04-23
2+
3+
- test: add add/apply middleware stress test
4+
- update tests to not fail when adding middleware post-init
5+
- rm checks to disallow adding middleware post-init
16

27
# v3.11.3 / 2020-04-13
38

49
- Transform package.json to strip excess in browserify
510

611
# v3.11.0
12+
713
- feat: use SameSite=Lax by default (#128)
814

915
# v3.10.1 / 2019-11-20

lib/analytics.js

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,6 @@ Analytics.prototype.addIntegration = function(Integration) {
105105
*/
106106

107107
Analytics.prototype.addSourceMiddleware = function(middleware) {
108-
if (this.initialized)
109-
throw new Error(
110-
'attempted to add a source middleware after initialization'
111-
);
112-
113108
this._sourceMiddlewares.add(middleware);
114109
return this;
115110
};
@@ -122,11 +117,6 @@ Analytics.prototype.addSourceMiddleware = function(middleware) {
122117
*/
123118

124119
Analytics.prototype.addIntegrationMiddleware = function(middleware) {
125-
if (this.initialized)
126-
throw new Error(
127-
'attempted to add an integration middleware after initialization'
128-
);
129-
130120
this._integrationMiddlewares.add(middleware);
131121
return this;
132122
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@segment/analytics.js-core",
33
"author": "Segment <friends@segment.com>",
4-
"version": "3.11.4",
4+
"version": "3.12.4",
55
"description": "The hassle-free way to integrate analytics into any web application.",
66
"keywords": [
77
"analytics",

test/analytics.test.js

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2043,19 +2043,13 @@ describe('Analytics', function() {
20432043
}
20442044
});
20452045

2046-
it('should throw an error if AJS has already initialized', function() {
2046+
it('should not throw an error if AJS has already initialized', function() {
20472047
analytics.init();
20482048
try {
20492049
analytics.addIntegrationMiddleware(function() {});
2050-
2051-
// This assert should not run.
2052-
assert(false, 'error was not thrown!');
20532050
} catch (e) {
2054-
assert(
2055-
e.message ===
2056-
'attempted to add an integration middleware after initialization',
2057-
'wrong error return'
2058-
);
2051+
// This assert should not run.
2052+
assert(false, 'error was thrown!');
20592053
}
20602054
});
20612055

@@ -2092,19 +2086,13 @@ describe('Analytics', function() {
20922086
}
20932087
});
20942088

2095-
it('should throw an error if AJS has already initialized', function() {
2089+
it('should not throw an error if AJS has already initialized', function() {
20962090
analytics.init();
20972091
try {
20982092
analytics.addSourceMiddleware(function() {});
2099-
2093+
} catch (e) {
21002094
// This assert should not run.
21012095
assert(false, 'error was not thrown!');
2102-
} catch (e) {
2103-
assert(
2104-
e.message ===
2105-
'attempted to add a source middleware after initialization',
2106-
'wrong error return'
2107-
);
21082096
}
21092097
});
21102098

test/middleware.test.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,4 +345,49 @@ describe('SourceMiddlewareChain', function() {
345345
assert(payload instanceof Facade, 'Payload should still be a facade.');
346346
});
347347
});
348+
349+
it('should be able to add and apply middleware interchangably', function() {
350+
chain.add(function(chain) {
351+
chain.payload.obj.test.push(1);
352+
chain.next(chain.payload);
353+
});
354+
chain.applyMiddlewares({ test: [] }, 'Test', function(payload) {
355+
assert.deepEqual(payload.obj.test, [1]);
356+
});
357+
chain.add(function(chain) {
358+
chain.payload.obj.test.push(2);
359+
chain.next(chain.payload);
360+
});
361+
chain.applyMiddlewares({ test: [] }, 'Test', function(payload) {
362+
assert.deepEqual(payload.obj.test, [1, 2]);
363+
});
364+
chain.add(function(chain) {
365+
chain.payload.obj.test.push(3);
366+
chain.next(chain.payload);
367+
});
368+
chain.applyMiddlewares({ test: [] }, 'Test', function(payload) {
369+
assert.deepEqual(payload.obj.test, [1, 2, 3]);
370+
});
371+
chain.add(function(chain) {
372+
chain.payload.obj.test.push(4);
373+
chain.next(chain.payload);
374+
});
375+
chain.applyMiddlewares({ test: [] }, 'Test', function(payload) {
376+
assert.deepEqual(payload.obj.test, [1, 2, 3, 4]);
377+
});
378+
chain.add(function(chain) {
379+
chain.payload.obj.test.push(5);
380+
chain.next(chain.payload);
381+
});
382+
chain.applyMiddlewares({ test: [] }, 'Test', function(payload) {
383+
assert.deepEqual(payload.obj.test, [1, 2, 3, 4, 5]);
384+
});
385+
chain.add(function(chain) {
386+
chain.payload.obj.test.push(6);
387+
chain.next(chain.payload);
388+
});
389+
chain.applyMiddlewares({ test: [] }, 'Test', function(payload) {
390+
assert.deepEqual(payload.obj.test, [1, 2, 3, 4, 5, 6]);
391+
});
392+
});
348393
});

0 commit comments

Comments
 (0)