Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deps(snyk): prettify snyk snapshot #5080

Merged
merged 3 commits into from
May 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions lighthouse-core/scripts/cleanup-vuln-snapshot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env node

/**
* @license Copyright 2018 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';

/** @fileoverview Read in the snyk snapshot, remove whatever we don't need, write it back */

const {readFileSync, writeFileSync} = require('fs');
const prettyJSONStringify = require('pretty-json-stringify');

const filename = process.argv[2];
if (!filename) throw new Error('No filename provided.');

const data = readFileSync(filename, 'utf8');
const output = cleanAndFormat(data);
JSON.parse(output); // make sure it's parseable
writeFileSync(filename, output, 'utf8');

/** @typedef {import('../audits/dobetterweb/no-vulnerable-libraries.js').SnykDB} SnykDB */

/**
* @param {string} vulnString
* @return {string}
*/
function cleanAndFormat(vulnString) {
const snapshot = /** @type {!SnykDB} */ (JSON.parse(vulnString));
for (const libEntries of Object.values(snapshot.npm)) {
libEntries.forEach((entry, i) => {
const pruned = {
id: entry.id,
severity: entry.severity,
semver: {vulnerable: entry.semver.vulnerable},
};

libEntries[i] = pruned;
});
}

// Normal pretty JSON-stringify has too many newlines. This strikes the right signal:noise ratio
return prettyJSONStringify(snapshot, {
tab: ' ',
spaceBeforeColon: '',
spaceAfterColon: '',
spaceAfterComma: '',
spaceInsideObject: '',
shouldExpand: (_, level) => level < 3,
});
}
1 change: 1 addition & 0 deletions lighthouse-core/scripts/update-vuln-snapshot.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@
url="https://snyk.io/partners/api/v2/vulndb/clientside.json"

wget "$url" -O snyk-snapshot.json && mv snyk-snapshot.json ./third-party/snyk/snapshot.json
node ./lighthouse-core/scripts/cleanup-vuln-snapshot.js ./third-party/snyk/snapshot.json
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
"npm-run-posix-or-windows": "^2.0.2",
"nyc": "^11.6.0",
"postinstall-prepare": "^1.0.1",
"pretty-json-stringify": "^0.0.2",
"puppeteer": "1.4.0",
"sinon": "^2.3.5",
"typescript": "2.9.1-insiders.20180516",
Expand Down
107 changes: 106 additions & 1 deletion third-party/snyk/snapshot.json

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions typings/pretty-json-stringify/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* @license Copyright 2018 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.
*/

declare module 'pretty-json-stringify' {
interface Params {
tab?: string;
spaceBeforeColon?: string;
spaceAfterColon?: string;
spaceAfterComma?: string;
spaceInsideObject?: string;
spaceInsideArray?: string;
shouldExpand?(obj: Object, level: number, index: string): Boolean;
}

function stringify(object: Object, params: Params): string;
export = stringify;
}
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3989,6 +3989,10 @@ pretty-hrtime@^1.0.0:
version "1.0.2"
resolved "https://registry.yarnpkg.com/pretty-hrtime/-/pretty-hrtime-1.0.2.tgz#70ca96f4d0628a443b918758f79416a9a7bc9fa8"

pretty-json-stringify@^0.0.2:
version "0.0.2"
resolved "https://registry.yarnpkg.com/pretty-json-stringify/-/pretty-json-stringify-0.0.2.tgz#dc0f1dbeab6bc1ab15f40d4cce4f38b75b17a334"

prettycli@^1.4.3:
version "1.4.3"
resolved "https://registry.yarnpkg.com/prettycli/-/prettycli-1.4.3.tgz#b28ec2aad9de07ae1fd75ef294fb54cbdee07ed5"
Expand Down