@segment/analytics.js-core
Advanced tools
Comparing version 3.13.2 to 3.13.3
@@ -0,1 +1,5 @@ | ||
# 3.13.3 / 2020-05-26 | ||
- feat: add destination middleware | ||
# 3.13.2 / 2020-05-21 | ||
@@ -2,0 +6,0 @@ |
@@ -17,2 +17,4 @@ 'use strict'; | ||
.IntegrationMiddlewareChain; | ||
var DestinationMiddlewareChain = require('./middleware') | ||
.DestinationMiddlewareChain; | ||
var Page = require('segmentio-facade').Page; | ||
@@ -54,2 +56,3 @@ var Track = require('segmentio-facade').Track; | ||
this._integrationMiddlewares = new IntegrationMiddlewareChain(); | ||
this._destinationMiddlewares = {}; | ||
this._integrations = {}; | ||
@@ -116,2 +119,3 @@ this._readied = false; | ||
* Define a new `IntegrationMiddleware` | ||
* DEPRECATED | ||
* | ||
@@ -128,2 +132,28 @@ * @param {Function} Middleware | ||
/** | ||
* Define a new `DestinationMiddleware` | ||
* Destination Middleware is chained after integration middleware | ||
* | ||
* @param {String} integrationName | ||
* @param {Array} Middlewares | ||
* @return {Analytics} | ||
*/ | ||
Analytics.prototype.addDestinationMiddleware = function( | ||
integrationName, | ||
middlewares | ||
) { | ||
var self = this; | ||
middlewares.forEach(function(middleware) { | ||
if (!self._destinationMiddlewares[integrationName]) { | ||
self._destinationMiddlewares[ | ||
integrationName | ||
] = new DestinationMiddlewareChain(); | ||
} | ||
self._destinationMiddlewares[integrationName].add(middleware); | ||
}); | ||
return self; | ||
}; | ||
/** | ||
* Initialize with the given integration `settings` and `options`. | ||
@@ -798,8 +828,39 @@ * | ||
metrics.increment('analytics_js.integration.invoke', { | ||
method: method, | ||
integration_name: integration.name | ||
}); | ||
// apply destination middlewares | ||
// Apply any integration middlewares that exist, then invoke the integration with the result. | ||
if (self._destinationMiddlewares[integration.name]) { | ||
self._destinationMiddlewares[integration.name].applyMiddlewares( | ||
facadeCopy, | ||
integration.name, | ||
function(result) { | ||
// A nullified payload should not be sent to an integration. | ||
if (result === null) { | ||
self.log( | ||
'Payload to destination "%s" was null and dropped by a middleware.', | ||
name | ||
); | ||
return; | ||
} | ||
integration.invoke.call(integration, method, result); | ||
// Check if the payload is still a Facade. If not, convert it to one. | ||
if (!(result instanceof Facade)) { | ||
result = new Facade(result); | ||
} | ||
metrics.increment('analytics_js.integration.invoke', { | ||
method: method, | ||
integration_name: integration.name | ||
}); | ||
integration.invoke.call(integration, method, result); | ||
} | ||
); | ||
} else { | ||
metrics.increment('analytics_js.integration.invoke', { | ||
method: method, | ||
integration_name: integration.name | ||
}); | ||
integration.invoke.call(integration, method, result); | ||
} | ||
} | ||
@@ -806,0 +867,0 @@ ); |
@@ -37,2 +37,16 @@ 'use strict'; | ||
module.exports.DestinationMiddlewareChain = function DestinationMiddlewareChain() { | ||
var apply = middlewareChain(this); | ||
this.applyMiddlewares = function(facade, integration, callback) { | ||
return apply( | ||
function(mw, payload, next) { | ||
mw({ payload: payload, integration: integration, next: next }); | ||
}, | ||
facade, | ||
callback | ||
); | ||
}; | ||
}; | ||
// Chain is essentially a linked list of middlewares to run in order. | ||
@@ -39,0 +53,0 @@ function middlewareChain(dest) { |
{ | ||
"name": "@segment/analytics.js-core", | ||
"author": "Segment <friends@segment.com>", | ||
"version": "3.13.2", | ||
"version": "3.13.3", | ||
"description": "The hassle-free way to integrate analytics into any web application.", | ||
@@ -6,0 +6,0 @@ "keywords": [ |
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
266519
6355
1