diff --git a/lib/plugins/assets/index.js b/lib/plugins/assets/index.js index 685436b89..e2ce4d3d6 100644 --- a/lib/plugins/assets/index.js +++ b/lib/plugins/assets/index.js @@ -1,16 +1,13 @@ 'use strict'; -const messageMaker = require('../../support/messageMaker'); const isEmpty = require('lodash.isempty'); const filterRegistry = require('../../support/filterRegistry'); const aggregator = require('./aggregator'); - -const make = messageMaker('assets').make; - const DEFAULT_METRICS_LARGEST_ASSETS = ['image.0.transferSize']; module.exports = { open(context, options) { + this.make = context.messageMaker('assets').make; this.options = options; this.resultUrls = context.resultUrls; filterRegistry.registerFilterForType( @@ -23,6 +20,7 @@ module.exports = { filterRegistry.registerFilterForType([], 'largestthirdpartyassets.summary'); }, processMessage(message, queue) { + const make = this.make; switch (message.type) { case 'pagexray.run': { aggregator.addToAggregate( @@ -41,7 +39,9 @@ module.exports = { if (!isEmpty(summary)) { for (let group of Object.keys(summary.groups)) { queue.postMessage( - make('aggregateassets.summary', summary.groups[group], { group }) + make('aggregateassets.summary', summary.groups[group], { + group + }) ); queue.postMessage( @@ -49,7 +49,9 @@ module.exports = { ); queue.postMessage( - make('slowestassets.summary', summary.timing[group], { group }) + make('slowestassets.summary', summary.timing[group], { + group + }) ); } diff --git a/lib/plugins/browsertime/index.js b/lib/plugins/browsertime/index.js index cbf906dd3..e2205d8a9 100644 --- a/lib/plugins/browsertime/index.js +++ b/lib/plugins/browsertime/index.js @@ -2,7 +2,6 @@ const browsertime = require('browsertime'); const Promise = require('bluebird'); -const messageMaker = require('../../support/messageMaker'); const filterRegistry = require('../../support/filterRegistry'); const aggregator = require('./aggregator'); const api = require('webcoach'); @@ -14,8 +13,6 @@ const analyzer = require('./analyzer'); const moment = require('moment'); const isEmpty = require('lodash.isempty'); -const make = messageMaker('browsertime').make; - const visitedUrls = new Set(); const TIME_FORMAT = 'YYYY-MM-DD HH:mm:ss'; @@ -80,6 +77,7 @@ const DEFAULT_METRICS_SUMMARY = [ module.exports = { concurrency: 1, open(context, options) { + this.make = context.messageMaker('browsertime').make; this.options = merge({}, defaultConfig, options.browsertime); this.firstParty = options.firstParty; this.options.mobile = options.mobile; @@ -107,6 +105,7 @@ module.exports = { ); }, processMessage(message, queue) { + const make = this.make; function processCoachOutput(url, group, results) { return Promise.resolve(results.browserScripts).each((run, runIndex) => { const coachAdvice = run.coach.coachAdvice; diff --git a/lib/plugins/coach/index.js b/lib/plugins/coach/index.js index 554dd3433..04775027d 100644 --- a/lib/plugins/coach/index.js +++ b/lib/plugins/coach/index.js @@ -1,13 +1,9 @@ 'use strict'; - -const messageMaker = require('../../support/messageMaker'); const aggregator = require('./aggregator'); const pagexrayAggregator = require('./pagexrayAggregator'); const pagexray = require('pagexray'); const filterRegistry = require('../../support/filterRegistry'); -const make = messageMaker('coach').make; - const DEFAULT_METRICS_SUMMARY = [ 'score.*', 'performance.score.*', @@ -56,6 +52,8 @@ const DEFAULT_PAGEXRAY_SUMMARY_METRICS = [ module.exports = { open(context, options) { this.options = options; + this.make = context.messageMaker('coach').make; + filterRegistry.registerFilterForType( DEFAULT_METRICS_SUMMARY, 'coach.summary' @@ -74,6 +72,7 @@ module.exports = { ); }, processMessage(message, queue) { + const make = this.make; switch (message.type) { case 'coach.run': { if (message.runIndex === 0) { diff --git a/lib/plugins/crawler/index.js b/lib/plugins/crawler/index.js index 05bcde3db..05b6db5fc 100644 --- a/lib/plugins/crawler/index.js +++ b/lib/plugins/crawler/index.js @@ -4,11 +4,8 @@ const Promise = require('bluebird'); const path = require('path'); const merge = require('lodash.merge'); const log = require('intel').getLogger('sitespeedio.plugin.crawler'); -const messageMaker = require('../../support/messageMaker'); const Crawler = require('simplecrawler'); -const make = messageMaker('crawler').make; - const defaultOptions = { depth: 3 }; @@ -16,8 +13,10 @@ const defaultOptions = { module.exports = { open(context, options) { this.options = merge({}, defaultOptions, options.crawler); + this.make = context.messageMaker('crawler').make; }, processMessage(message, queue) { + const make = this.make; if (message.type === 'url' && message.source !== 'crawler') { const maxPages = this.options.maxPages || Number.MAX_SAFE_INTEGER; diff --git a/lib/plugins/domains/index.js b/lib/plugins/domains/index.js index 18c3bdd66..86eef7b6a 100644 --- a/lib/plugins/domains/index.js +++ b/lib/plugins/domains/index.js @@ -1,18 +1,16 @@ 'use strict'; - -const messageMaker = require('../../support/messageMaker'); const filterRegistry = require('../../support/filterRegistry'); const isEmpty = require('lodash.isempty'); const aggregator = require('./aggregator'); -const make = messageMaker('domains').make; - module.exports = { - open() { + open(context) { + this.make = context.messageMaker('domains').make; // '*.requestCounts, 'domains.summary' filterRegistry.registerFilterForType([], 'domains.summary'); }, processMessage(message, queue) { + const make = this.make; switch (message.type) { case 'browsertime.har': { aggregator.addToAggregate(message.data, message.url); diff --git a/lib/plugins/gpsi/index.js b/lib/plugins/gpsi/index.js index dc9507cd5..100d212e4 100644 --- a/lib/plugins/gpsi/index.js +++ b/lib/plugins/gpsi/index.js @@ -1,16 +1,15 @@ 'use strict'; -const messageMaker = require('../../support/messageMaker'); const aggregator = require('./aggregator'); const log = require('intel').getLogger('sitespeedio.plugin.gpsi'); const filterRegistry = require('../../support/filterRegistry'); const analyzer = require('./analyzer'); -const make = messageMaker('gpsi').make; const DEFAULT_METRICS_PAGESUMMARY = ['ruleGroups.SPEED.score']; module.exports = { open(context, options) { + this.make = context.messageMaker('gpsi').make; this.options = { gpsi: options.gpsi, mobile: options.mobile @@ -21,6 +20,7 @@ module.exports = { ); }, processMessage(message, queue) { + const make = this.make; switch (message.type) { case 'url': { const url = message.url; diff --git a/lib/plugins/html/index.js b/lib/plugins/html/index.js index 0c460f79d..6ae7e8195 100644 --- a/lib/plugins/html/index.js +++ b/lib/plugins/html/index.js @@ -5,11 +5,8 @@ const HTMLBuilder = require('./htmlBuilder'); const get = require('lodash.get'); const set = require('lodash.set'); const reduce = require('lodash.reduce'); -const messageMaker = require('../../support/messageMaker'); const DataCollector = require('../../support/dataCollector'); -const make = messageMaker('html').make; - // lets keep this in the HTML context, since we need things from the // regular options object in the output const defaultConfig = { @@ -20,6 +17,7 @@ const defaultConfig = { module.exports = { open(context, options) { + this.make = context.messageMaker('html').make; this.options = merge({}, defaultConfig, options); this.HTMLBuilder = new HTMLBuilder(context, this.options); this.dataCollector = new DataCollector(context); @@ -27,6 +25,7 @@ module.exports = { }, processMessage(message, queue) { const dataCollector = this.dataCollector; + const make = this.make; if ( message.type.endsWith('.run') || diff --git a/lib/plugins/s3/index.js b/lib/plugins/s3/index.js index 2065b8ef7..a6bde9acc 100644 --- a/lib/plugins/s3/index.js +++ b/lib/plugins/s3/index.js @@ -5,8 +5,6 @@ const s3 = require('s3'); const throwIfMissing = require('../../support/util').throwIfMissing; const fs = require('fs-extra'); const pick = require('lodash.pick'); -const messageMaker = require('../../support/messageMaker'); -const make = messageMaker('s3').make; function createS3Client(s3Options) { const clientOptions = pick(s3Options, [ @@ -29,6 +27,7 @@ function createS3Client(s3Options) { module.exports = { open(context, options) { this.s3Options = options.s3; + this.make = context.messageMaker('s3').make; throwIfMissing(this.s3Options, ['bucketname'], 's3'); if (this.s3Options.key || this.s3Options.secret) { @@ -38,6 +37,7 @@ module.exports = { }, processMessage(message, queue) { + const make = this.make; if (message.type === 'html.finished') { const s3Options = this.s3Options; const client = createS3Client(s3Options); diff --git a/lib/plugins/webpagetest/index.js b/lib/plugins/webpagetest/index.js index 2fee9047b..dafa96e67 100644 --- a/lib/plugins/webpagetest/index.js +++ b/lib/plugins/webpagetest/index.js @@ -1,7 +1,6 @@ 'use strict'; const urlParser = require('url'); -const messageMaker = require('../../support/messageMaker'); const filterRegistry = require('../../support/filterRegistry'); const analyzer = require('./analyzer'); const aggregator = require('./aggregator'); @@ -11,8 +10,6 @@ const get = require('lodash.get'); const log = require('intel').getLogger('sitespeedio.plugin.webpagetest'); const WebPageTest = require('webpagetest'); -const make = messageMaker('webpagetest').make; - const DEFAULT_PAGE_SUMMARY_METRICS = [ 'data.median.*.SpeedIndex', 'data.median.*.render', @@ -69,6 +66,7 @@ function isPublicWptHost(address) { module.exports = { open(context, options) { + this.make = context.messageMaker('webpagetest').make; this.options = merge({}, defaultConfig, options.webpagetest); this.storageManager = context.storageManager; if (!this.options.key && isPublicWptHost(this.options.host)) { @@ -87,6 +85,7 @@ module.exports = { ); }, processMessage(message, queue) { + const make = this.make; const wptOptions = this.options; switch (message.type) { case 'url': { diff --git a/lib/sitespeed.js b/lib/sitespeed.js index a44a305f8..1dec01b83 100644 --- a/lib/sitespeed.js +++ b/lib/sitespeed.js @@ -13,6 +13,7 @@ const Promise = require('bluebird'), union = require('lodash.union'), browsertimeConfig = require('./plugins/browsertime').config, webpagetestConfig = require('./plugins/webpagetest').config, + messageMaker = require('./support/messageMaker'), packageInfo = require('../package'); const QueueHandler = require('./support/queueHandler'), @@ -145,7 +146,8 @@ module.exports = { timestamp, budget: budgetResult, name: url, - log + log, + messageMaker }; return runOptionalFunction(allPlugins, 'open', context, options) .then(() => queueHandler.run(urlSources)) diff --git a/test/webpagetestTests.js b/test/webpagetestTests.js index f3e526817..882d5b47c 100644 --- a/test/webpagetestTests.js +++ b/test/webpagetestTests.js @@ -5,6 +5,7 @@ const aggregator = require('../lib/plugins/webpagetest/aggregator'); const fs = require('fs'); const path = require('path'); const expect = require('chai').expect; +const messageMaker = require('../lib/support/messageMaker'); const wptResultPath = path.resolve( __dirname, @@ -16,16 +17,22 @@ const wptResult = JSON.parse(fs.readFileSync(wptResultPath, 'utf8')); describe('webpagetest', () => { describe('plugin', () => { it('should require key for default server', () => { - expect(() => plugin.open({}, {})).to.throw(); + expect(() => plugin.open({ messageMaker }, {})).to.throw(); }); it('should require key for public server', () => { expect(() => - plugin.open({}, { webpagetest: { host: 'www.webpagetest.org' } }) + plugin.open( + { messageMaker }, + { webpagetest: { host: 'www.webpagetest.org' } } + ) ).to.throw(); }); it('should not require key for private server', () => { expect(() => - plugin.open({}, { webpagetest: { host: 'http://myserver.foo' } }) + plugin.open( + { messageMaker }, + { webpagetest: { host: 'http://myserver.foo' } } + ) ).to.not.throw(); }); });