Skip to content

Commit

Permalink
Merge pull request #49 from paullewis/extension-backport
Browse files Browse the repository at this point in the history
Extension backport and refactor.
  • Loading branch information
paullewis committed Mar 22, 2016
2 parents cb07d7f + 8b05475 commit 5a8849d
Show file tree
Hide file tree
Showing 38 changed files with 1,420 additions and 903 deletions.
18 changes: 12 additions & 6 deletions auditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,18 @@
*/
'use strict';

module.exports = function(audits) {
return function audit(results) {
var flattenedAudits = results.reduce(function(prev, curr) {
class Auditor {

_flattenArtifacts(artifacts) {
return artifacts.reduce(function(prev, curr) {
return Object.assign(prev, curr);
}, {});
}

audit(artifacts, audits) {
const flattenedArtifacts = this._flattenArtifacts(artifacts);
return Promise.all(audits.map(audit => audit.audit(flattenedArtifacts)));
}
}

return Promise.all(audits.map(v => v(flattenedAudits)));
};
};
module.exports = Auditor;
47 changes: 47 additions & 0 deletions audits/manifest/background-color.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Copyright 2016 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

const manifestParser = require('../../helpers/manifest-parser');

class ManifestBackgroundColor {

static get tags() {
return ['Manifest'];
}

static get description() {
return 'Contains background_color';
}

static audit(inputs) {
let hasBackgroundColor = false;
const manifest = manifestParser(inputs.manifest).value;

if (manifest) {
hasBackgroundColor = (!!manifest.background_color);
}

return {
value: hasBackgroundColor,
tags: ManifestBackgroundColor.tags,
description: ManifestBackgroundColor.description
};
}
}

module.exports = ManifestBackgroundColor;
29 changes: 20 additions & 9 deletions audits/service-worker/audit.js → audits/manifest/exists.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,26 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

module.exports = function(data) {
// Test the Service Worker registrations for validity.
let registrations = data.serviceWorkerRegistrations;
let activatedRegistrations = registrations.versions.filter(reg =>
reg.status === 'activated');
class ManifestExists {

static get tags() {
return ['Manifest'];
}

static get description() {
return 'Exists';
}

static audit(inputs) {
return {
value: inputs.manifest.length > 0,
tags: ManifestExists.tags,
description: ManifestExists.description
};
}
}

return {
'service-worker': activatedRegistrations.length > 0
};
};
module.exports = ManifestExists;
48 changes: 48 additions & 0 deletions audits/manifest/icons-192.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* Copyright 2016 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

const manifestParser = require('../../helpers/manifest-parser');

class ManifestIcons192 {

static get tags() {
return ['Manifest'];
}

static get description() {
return 'Contains 192px icons';
}

static audit(inputs) {
let hasIcons = false;
const manifest = manifestParser(inputs.manifest).value;

if (manifest && manifest.icons) {
const icons192 = manifest.icons.raw.find(i => i.sizes === '192x192');
hasIcons = (!!icons192);
}

return {
value: hasIcons,
tags: ManifestIcons192.tags,
description: ManifestIcons192.description
};
}
}

module.exports = ManifestIcons192;
47 changes: 47 additions & 0 deletions audits/manifest/icons.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Copyright 2016 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

const manifestParser = require('../../helpers/manifest-parser');

class ManifestIcons {

static get tags() {
return ['Manifest'];
}

static get description() {
return 'Contains icons';
}

static audit(inputs) {
let hasIcons = false;
const manifest = manifestParser(inputs.manifest).value;

if (manifest) {
hasIcons = (!!manifest.icons);
}

return {
value: hasIcons,
tags: ManifestIcons.tags,
description: ManifestIcons.description
};
}
}

module.exports = ManifestIcons;
46 changes: 30 additions & 16 deletions audits/minify-html/audit.js → audits/manifest/name.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,35 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

module.exports = function(data) {
// See how compressed the HTML _could_ be if whitespace was removed.
// This could be a lot more aggressive.
let htmlNoWhiteSpaces = data.html
.replace(/\n/igm, '')
.replace(/\t/igm, '')
.replace(/\s+/igm, ' ');

let htmlLen = Math.max(1, data.html.length);
let htmlNoWhiteSpacesLen = htmlNoWhiteSpaces.length;
let ratio = Math.min(1, (htmlNoWhiteSpacesLen / htmlLen));

return {
'minify-html': ratio
};
};
const manifestParser = require('../../helpers/manifest-parser');

class ManifestName {

static get tags() {
return ['Manifest'];
}

static get description() {
return 'Contains name';
}

static audit(inputs) {
let hasName = false;
const manifest = manifestParser(inputs.manifest).value;

if (manifest) {
hasName = (!!manifest.name);
}

return {
value: hasName,
tags: ManifestName.tags,
description: ManifestName.description
};
}
}

module.exports = ManifestName;
47 changes: 47 additions & 0 deletions audits/manifest/short-name.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Copyright 2016 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

const manifestParser = require('../../helpers/manifest-parser');

class ManifestShortName {

static get tags() {
return ['Manifest'];
}

static get description() {
return 'Contains short_name';
}

static audit(inputs) {
let hasShortName = false;
const manifest = manifestParser(inputs.manifest).value;

if (manifest) {
hasShortName = (!!manifest.short_name);
}

return {
value: hasShortName,
tags: ManifestShortName.tags,
description: ManifestShortName.description
};
}
}

module.exports = ManifestShortName;
47 changes: 47 additions & 0 deletions audits/manifest/start-url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Copyright 2016 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

const manifestParser = require('../../helpers/manifest-parser');

class ManifestStartUrl {

static get tags() {
return ['Manifest'];
}

static get description() {
return 'Contains start_url';
}

static audit(inputs) {
let hasStartUrl = false;
const manifest = manifestParser(inputs.manifest).value;

if (manifest) {
hasStartUrl = (!!manifest.start_url);
}

return {
value: hasStartUrl,
tags: ManifestStartUrl.tags,
description: ManifestStartUrl.description
};
}
}

module.exports = ManifestStartUrl;

0 comments on commit 5a8849d

Please sign in to comment.