Skip to content

Commit

Permalink
Use single quotes :P
Browse files Browse the repository at this point in the history
  • Loading branch information
mjackson committed Dec 17, 2018
1 parent ada3703 commit 19b2e55
Show file tree
Hide file tree
Showing 93 changed files with 792 additions and 791 deletions.
3 changes: 0 additions & 3 deletions .prettierrc

This file was deleted.

18 changes: 9 additions & 9 deletions modules/AuthAPI.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
const crypto = require("crypto");
const jwt = require("jsonwebtoken");
const crypto = require('crypto');
const jwt = require('jsonwebtoken');

const db = require("./utils/data");
const secretKey = require("./secretKey");
const db = require('./utils/data');
const secretKey = require('./secretKey');

function getCurrentSeconds() {
return Math.floor(Date.now() / 1000);
}

function createTokenId() {
return crypto.randomBytes(16).toString("hex");
return crypto.randomBytes(16).toString('hex');
}

function createToken(scopes = {}) {
return new Promise((resolve, reject) => {
const payload = {
jti: createTokenId(),
iss: "https://unpkg.com",
iss: 'https://unpkg.com',
iat: getCurrentSeconds(),
scopes
};

jwt.sign(
payload,
secretKey.private,
{ algorithm: "RS256" },
{ algorithm: 'RS256' },
(error, token) => {
if (error) {
reject(error);
Expand All @@ -36,11 +36,11 @@ function createToken(scopes = {}) {
});
}

const revokedTokensSet = "revoked-tokens";
const revokedTokensSet = 'revoked-tokens';

function verifyToken(token) {
return new Promise((resolve, reject) => {
const options = { algorithms: ["RS256"] };
const options = { algorithms: ['RS256'] };

jwt.verify(token, secretKey.public, options, (error, payload) => {
if (error) {
Expand Down
4 changes: 2 additions & 2 deletions modules/BlacklistAPI.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const db = require("./utils/data");
const db = require('./utils/data');

const blacklistSet = "blacklisted-packages";
const blacklistSet = 'blacklisted-packages';

function addPackage(packageName) {
return new Promise((resolve, reject) => {
Expand Down
28 changes: 14 additions & 14 deletions modules/CloudflareAPI.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
require("isomorphic-fetch");
const invariant = require("invariant");
const gunzip = require("gunzip-maybe");
const ndjson = require("ndjson");
require('isomorphic-fetch');
const invariant = require('invariant');
const gunzip = require('gunzip-maybe');
const ndjson = require('ndjson');

const cloudflareURL = "https://api.cloudflare.com/client/v4";
const cloudflareURL = 'https://api.cloudflare.com/client/v4';
const cloudflareEmail = process.env.CLOUDFLARE_EMAIL;
const cloudflareKey = process.env.CLOUDFLARE_KEY;

invariant(
cloudflareEmail,
"Missing the $CLOUDFLARE_EMAIL environment variable"
'Missing the $CLOUDFLARE_EMAIL environment variable'
);

invariant(cloudflareKey, "Missing the $CLOUDFLARE_KEY environment variable");
invariant(cloudflareKey, 'Missing the $CLOUDFLARE_KEY environment variable');

function get(path, headers) {
return fetch(`${cloudflareURL}${path}`, {
headers: Object.assign({}, headers, {
"X-Auth-Email": cloudflareEmail,
"X-Auth-Key": cloudflareKey
'X-Auth-Email': cloudflareEmail,
'X-Auth-Key': cloudflareKey
})
});
}
Expand All @@ -32,7 +32,7 @@ function getJSON(path, headers) {
if (!data.success) {
console.error(`CloudflareAPI.getJSON failed at ${path}`);
console.error(data);
throw new Error("Failed to getJSON from Cloudflare");
throw new Error('Failed to getJSON from Cloudflare');
}

return data.result;
Expand All @@ -51,9 +51,9 @@ function reduceResults(target, values) {
Object.keys(values).forEach(key => {
const value = values[key];

if (typeof value === "object" && value) {
if (typeof value === 'object' && value) {
target[key] = reduceResults(target[key] || {}, value);
} else if (typeof value === "number") {
} else if (typeof value === 'number') {
target[key] = (target[key] || 0) + values[key];
}
});
Expand All @@ -75,7 +75,7 @@ function getZoneAnalyticsDashboard(zones, since, until) {

function getJSONStream(path, headers) {
const gzipHeaders = Object.assign({}, headers, {
"Accept-Encoding": "gzip"
'Accept-Encoding': 'gzip'
});

return get(path, gzipHeaders)
Expand All @@ -84,7 +84,7 @@ function getJSONStream(path, headers) {
}

function getLogs(zoneId, startTime, endTime, fieldsArray) {
const fields = fieldsArray.join(",");
const fields = fieldsArray.join(',');

// console.log(
// `https://api.cloudflare.com/client/v4/zones/${zoneId}/logs/received?start=${startTime}&end=${endTime}&fields=${fields}`
Expand Down
10 changes: 5 additions & 5 deletions modules/StatsAPI.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const db = require("./utils/data");
const db = require('./utils/data');

const CloudflareAPI = require("./CloudflareAPI");
const BlacklistAPI = require("./BlacklistAPI");
const CloudflareAPI = require('./CloudflareAPI');
const BlacklistAPI = require('./BlacklistAPI');

function prunePackages(packagesMap) {
return Promise.all(
Expand Down Expand Up @@ -39,7 +39,7 @@ function createScoresMap(array) {

function getScoresMap(key, n = 100) {
return new Promise((resolve, reject) => {
db.zrevrange(key, 0, n, "withscores", (error, value) => {
db.zrevrange(key, 0, n, 'withscores', (error, value) => {
if (error) {
reject(error);
} else {
Expand Down Expand Up @@ -141,7 +141,7 @@ function extractPublicInfo(data) {
};
}

const DomainNames = ["unpkg.com", "npmcdn.com"];
const DomainNames = ['unpkg.com', 'npmcdn.com'];

function fetchStats(since, until) {
return CloudflareAPI.getZones(DomainNames).then(zones => {
Expand Down
8 changes: 4 additions & 4 deletions modules/__tests__/AuthAPI-test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const AuthAPI = require("../AuthAPI");
const AuthAPI = require('../AuthAPI');

describe("Auth API", () => {
describe('Auth API', () => {
beforeEach(done => {
AuthAPI.removeAllRevokedTokens().then(() => done(), done);
});

it("creates tokens with the right scopes", done => {
it('creates tokens with the right scopes', done => {
const scopes = {
blacklist: {
add: true,
Expand All @@ -24,7 +24,7 @@ describe("Auth API", () => {
});
});

it("refuses to verify revoked tokens", done => {
it('refuses to verify revoked tokens', done => {
const scopes = {};

AuthAPI.createToken(scopes).then(token => {
Expand Down
8 changes: 4 additions & 4 deletions modules/__tests__/BlacklistAPI-test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const BlacklistAPI = require("../BlacklistAPI");
const BlacklistAPI = require('../BlacklistAPI');

describe("Blacklist API", () => {
describe('Blacklist API', () => {
beforeEach(done => {
BlacklistAPI.removeAllPackages().then(() => done(), done);
});

it("adds and removes packages to/from the blacklist", done => {
const packageName = "bad-package";
it('adds and removes packages to/from the blacklist', done => {
const packageName = 'bad-package';

BlacklistAPI.addPackage(packageName).then(() => {
BlacklistAPI.getPackages().then(packageNames => {
Expand Down
46 changes: 23 additions & 23 deletions modules/__tests__/_auth-test.js
Original file line number Diff line number Diff line change
@@ -1,61 +1,61 @@
const request = require("supertest");
const request = require('supertest');

const createServer = require("../createServer");
const withRevokedToken = require("./utils/withRevokedToken");
const withToken = require("./utils/withToken");
const createServer = require('../createServer');
const withRevokedToken = require('./utils/withRevokedToken');
const withToken = require('./utils/withToken');

describe("The /_auth endpoint", () => {
describe('The /_auth endpoint', () => {
let server;
beforeEach(() => {
server = createServer();
});

describe("POST /_auth", () => {
it("creates a new auth token", done => {
describe('POST /_auth', () => {
it('creates a new auth token', done => {
request(server)
.post("/_auth")
.post('/_auth')
.end((err, res) => {
expect(res.body).toHaveProperty("token");
expect(res.body).toHaveProperty('token');
done();
});
});
});

describe("GET /_auth", () => {
describe("with no auth", () => {
it("echoes back null", done => {
describe('GET /_auth', () => {
describe('with no auth', () => {
it('echoes back null', done => {
request(server)
.get("/_auth")
.get('/_auth')
.end((err, res) => {
expect(res.body).toHaveProperty("auth");
expect(res.body).toHaveProperty('auth');
expect(res.body.auth).toBe(null);
done();
});
});
});

describe("with a revoked auth token", () => {
it("echoes back null", done => {
describe('with a revoked auth token', () => {
it('echoes back null', done => {
withRevokedToken({ some: { scope: true } }, token => {
request(server)
.get("/_auth?token=" + token)
.get('/_auth?token=' + token)
.end((err, res) => {
expect(res.body).toHaveProperty("auth");
expect(res.body).toHaveProperty('auth');
expect(res.body.auth).toBe(null);
done();
});
});
});
});

describe("with a valid auth token", () => {
it("echoes back the auth payload", done => {
describe('with a valid auth token', () => {
it('echoes back the auth payload', done => {
withToken({ some: { scope: true } }, token => {
request(server)
.get("/_auth?token=" + token)
.get('/_auth?token=' + token)
.end((err, res) => {
expect(res.body).toHaveProperty("auth");
expect(typeof res.body.auth).toBe("object");
expect(res.body).toHaveProperty('auth');
expect(typeof res.body.auth).toBe('object');
done();
});
});
Expand Down
Loading

0 comments on commit 19b2e55

Please sign in to comment.