Skip to content

Commit

Permalink
Merge 8711997 into f77aa7a
Browse files Browse the repository at this point in the history
  • Loading branch information
Munter committed Jul 2, 2020
2 parents f77aa7a + 8711997 commit db0d546
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 7 deletions.
40 changes: 37 additions & 3 deletions docs/_data/supporters.js
Expand Up @@ -7,6 +7,7 @@ const {resolve} = require('path');
const debug = require('debug')('mocha:docs:data:supporters');
const needle = require('needle');
const imageSize = require('image-size');
const fileType = require('file-type');
const blocklist = new Set(require('./blocklist.json'));

const API_ENDPOINT = 'https://api.opencollective.com/graphql/v2';
Expand Down Expand Up @@ -84,6 +85,21 @@ const getAllOrders = async (slug = 'mochajs') => {
}
};

/**
* Blank images from https://png-pixel.com/ for when OpenCollective
* doesn't respond with a PNG image while downloading supporter avatars
*/
// 64x64 #f9f9f9 png
const blank64 = Buffer.from(
'iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAQAAAAAYLlVAAAAPElEQVR42u3OMQEAAAgDINc/sZfG2AMJyN5URUBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQKAdeHK9fkGpx7l4AAAAAElFTkSuQmCC',
'base64'
);
// 32x32 #f9f9f9 png
const blank32 = Buffer.from(
'iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAQAAADZc7J/AAAAIUlEQVR42mP8+Z+BIsA4asCoAaMGjBowasCoAaMGDDcDAC5IPyHFDzg6AAAAAElFTkSuQmCC',
'base64'
);

module.exports = async () => {
const orders = await getAllOrders();
// Deduplicating supporters with multiple orders
Expand Down Expand Up @@ -129,8 +145,18 @@ module.exports = async () => {
supporters.sponsors.map(async sponsor => {
const filePath = resolve(supporterImagePath, sponsor.id + '.png');
const {body} = await needle('get', encodeURI(sponsor.avatar));
sponsor.dimensions = imageSize(body);
await writeFile(filePath, body);

let imageBuffer = body;

// Some times OpenCollective responds with a non-image
if ((await fileType.fromBuffer(body)).mime === 'image/png') {
sponsor.dimensions = imageSize(body);
} else {
sponsor.dimensions = {width: 64, height: 64};
imageBuffer = blank64;
}

await writeFile(filePath, imageBuffer);
})
);

Expand All @@ -139,7 +165,15 @@ module.exports = async () => {
supporters.backers.map(async backer => {
const filePath = resolve(supporterImagePath, backer.id + '.png');
const {body} = await needle('get', encodeURI(backer.avatar));
await writeFile(filePath, body);

let imageBuffer = body;

// Some times OpenCollective responds with a non-image
if ((await fileType.fromBuffer(body)).mime !== 'image/png') {
imageBuffer = blank32;
}

await writeFile(filePath, imageBuffer);
})
);

Expand Down
93 changes: 89 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -100,6 +100,7 @@
"eslint-plugin-prettier": "^3.1.2",
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-standard": "^4.0.1",
"file-type": "^14.6.2",
"fs-extra": "^9.0.0",
"husky": "^4.2.3",
"hyperlink": "^4.4.3",
Expand Down

0 comments on commit db0d546

Please sign in to comment.