This commit is contained in:
MICHAEL JACKSON
2017-11-25 13:25:01 -08:00
parent f3974b5e2d
commit 3a309241da
64 changed files with 635 additions and 801 deletions

View File

@ -1,35 +1,35 @@
const getFileContentType = require('../getFileContentType')
const getFileContentType = require("../getFileContentType")
it('gets a content type of text/plain for LICENSE|README|CHANGES|AUTHORS|Makefile', () => {
expect(getFileContentType('AUTHORS')).toBe('text/plain')
expect(getFileContentType('CHANGES')).toBe('text/plain')
expect(getFileContentType('LICENSE')).toBe('text/plain')
expect(getFileContentType('Makefile')).toBe('text/plain')
expect(getFileContentType('PATENTS')).toBe('text/plain')
expect(getFileContentType('README')).toBe('text/plain')
it("gets a content type of text/plain for LICENSE|README|CHANGES|AUTHORS|Makefile", () => {
expect(getFileContentType("AUTHORS")).toBe("text/plain")
expect(getFileContentType("CHANGES")).toBe("text/plain")
expect(getFileContentType("LICENSE")).toBe("text/plain")
expect(getFileContentType("Makefile")).toBe("text/plain")
expect(getFileContentType("PATENTS")).toBe("text/plain")
expect(getFileContentType("README")).toBe("text/plain")
})
it('gets a content type of text/plain for .*rc files', () => {
expect(getFileContentType('.eslintrc')).toBe('text/plain')
expect(getFileContentType('.babelrc')).toBe('text/plain')
expect(getFileContentType('.anythingrc')).toBe('text/plain')
it("gets a content type of text/plain for .*rc files", () => {
expect(getFileContentType(".eslintrc")).toBe("text/plain")
expect(getFileContentType(".babelrc")).toBe("text/plain")
expect(getFileContentType(".anythingrc")).toBe("text/plain")
})
it('gets a content type of text/plain for .git* files', () => {
expect(getFileContentType('.gitignore')).toBe('text/plain')
expect(getFileContentType('.gitanything')).toBe('text/plain')
it("gets a content type of text/plain for .git* files", () => {
expect(getFileContentType(".gitignore")).toBe("text/plain")
expect(getFileContentType(".gitanything")).toBe("text/plain")
})
it('gets a content type of text/plain for .*ignore files', () => {
expect(getFileContentType('.eslintignore')).toBe('text/plain')
expect(getFileContentType('.anythingignore')).toBe('text/plain')
it("gets a content type of text/plain for .*ignore files", () => {
expect(getFileContentType(".eslintignore")).toBe("text/plain")
expect(getFileContentType(".anythingignore")).toBe("text/plain")
})
it('gets a content type of text/plain for .ts files', () => {
expect(getFileContentType('app.ts')).toBe('text/plain')
expect(getFileContentType('app.d.ts')).toBe('text/plain')
it("gets a content type of text/plain for .ts files", () => {
expect(getFileContentType("app.ts")).toBe("text/plain")
expect(getFileContentType("app.d.ts")).toBe("text/plain")
})
it('gets a content type of text/plain for .flow files', () => {
expect(getFileContentType('app.js.flow')).toBe('text/plain')
it("gets a content type of text/plain for .flow files", () => {
expect(getFileContentType("app.js.flow")).toBe("text/plain")
})

View File

@ -1,8 +1,8 @@
const db = require('../../RedisClient')
const db = require("../../RedisClient")
function createCache(keyPrefix) {
function createKey(key) {
return keyPrefix + '-' + key
return keyPrefix + "-" + key
}
function set(key, value, expiry, callback) {

View File

@ -2,16 +2,16 @@ function createSearch(query) {
const params = []
Object.keys(query).forEach(param => {
if (query[param] === '') {
if (query[param] === "") {
params.push(param) // Omit the trailing "=" from param=
} else {
params.push(`${param}=${encodeURIComponent(query[param])}`)
}
})
const search = params.join('&')
const search = params.join("&")
return search ? `?${search}` : ''
return search ? `?${search}` : ""
}
module.exports = createSearch

View File

@ -1,22 +1,13 @@
const mime = require('mime')
const mime = require("mime")
mime.define({
'text/plain': [
'authors',
'changes',
'license',
'makefile',
'patents',
'readme',
'ts',
'flow'
]
"text/plain": ["authors", "changes", "license", "makefile", "patents", "readme", "ts", "flow"]
})
const TextFiles = /\/?(\.[a-z]*rc|\.git[a-z]*|\.[a-z]*ignore)$/i
function getFileContentType(file) {
return TextFiles.test(file) ? 'text/plain' : mime.lookup(file)
return TextFiles.test(file) ? "text/plain" : mime.lookup(file)
}
module.exports = getFileContentType

View File

@ -1,4 +1,4 @@
const fs = require('fs')
const fs = require("fs")
function getFileStats(file) {
return new Promise((resolve, reject) => {

View File

@ -1,12 +1,12 @@
function getFileType(stats) {
if (stats.isFile()) return 'file'
if (stats.isDirectory()) return 'directory'
if (stats.isBlockDevice()) return 'blockDevice'
if (stats.isCharacterDevice()) return 'characterDevice'
if (stats.isSymbolicLink()) return 'symlink'
if (stats.isSocket()) return 'socket'
if (stats.isFIFO()) return 'fifo'
return 'unknown'
if (stats.isFile()) return "file"
if (stats.isDirectory()) return "directory"
if (stats.isBlockDevice()) return "blockDevice"
if (stats.isCharacterDevice()) return "characterDevice"
if (stats.isSymbolicLink()) return "symlink"
if (stats.isSocket()) return "socket"
if (stats.isFIFO()) return "fifo"
return "unknown"
}
module.exports = getFileType

View File

@ -1,9 +1,9 @@
const fs = require('fs')
const path = require('path')
const React = require('react')
const ReactDOMServer = require('react-dom/server')
const getFileStats = require('./getFileStats')
const IndexPage = require('../components/IndexPage')
const fs = require("fs")
const path = require("path")
const React = require("react")
const ReactDOMServer = require("react-dom/server")
const getFileStats = require("./getFileStats")
const IndexPage = require("../components/IndexPage")
const e = React.createElement
@ -14,9 +14,7 @@ function getEntries(dir) {
reject(error)
} else {
resolve(
Promise.all(
files.map(file => getFileStats(path.join(dir, file)))
).then(statsArray => {
Promise.all(files.map(file => getFileStats(path.join(dir, file)))).then(statsArray => {
return statsArray.map((stats, index) => {
return { file: files[index], stats }
})
@ -27,7 +25,7 @@ function getEntries(dir) {
})
}
const DOCTYPE = '<!DOCTYPE html>'
const DOCTYPE = "<!DOCTYPE html>"
function createHTML(props) {
return DOCTYPE + ReactDOMServer.renderToStaticMarkup(e(IndexPage, props))

View File

@ -1,9 +1,9 @@
const fs = require('fs')
const path = require('path')
const SRIToolbox = require('sri-toolbox')
const getFileContentType = require('./getFileContentType')
const getFileStats = require('./getFileStats')
const getFileType = require('./getFileType')
const fs = require("fs")
const path = require("path")
const SRIToolbox = require("sri-toolbox")
const getFileContentType = require("./getFileContentType")
const getFileStats = require("./getFileStats")
const getFileType = require("./getFileType")
function getEntries(dir, file, maximumDepth) {
return new Promise((resolve, reject) => {
@ -12,17 +12,10 @@ function getEntries(dir, file, maximumDepth) {
reject(error)
} else {
resolve(
Promise.all(
files.map(f => getFileStats(path.join(dir, file, f)))
).then(statsArray => {
Promise.all(files.map(f => getFileStats(path.join(dir, file, f)))).then(statsArray => {
return Promise.all(
statsArray.map((stats, index) =>
getMetadataRecursive(
dir,
path.join(file, files[index]),
stats,
maximumDepth - 1
)
getMetadataRecursive(dir, path.join(file, files[index]), stats, maximumDepth - 1)
)
)
})
@ -42,7 +35,7 @@ function getIntegrity(file) {
if (error) {
reject(error)
} else {
resolve(SRIToolbox.generate({ algorithms: ['sha384'] }, data))
resolve(SRIToolbox.generate({ algorithms: ["sha384"] }, data))
}
})
})
@ -64,8 +57,7 @@ function getMetadataRecursive(dir, file, stats, maximumDepth) {
})
}
if (!stats.isDirectory() || maximumDepth === 0)
return Promise.resolve(metadata)
if (!stats.isDirectory() || maximumDepth === 0) return Promise.resolve(metadata)
return getEntries(dir, file, maximumDepth).then(files => {
metadata.files = files
@ -74,12 +66,9 @@ function getMetadataRecursive(dir, file, stats, maximumDepth) {
}
function getMetadata(baseDir, path, stats, maximumDepth, callback) {
getMetadataRecursive(baseDir, path, stats, maximumDepth).then(function(
metadata
) {
getMetadataRecursive(baseDir, path, stats, maximumDepth).then(function(metadata) {
callback(null, metadata)
},
callback)
}, callback)
}
module.exports = getMetadata

View File

@ -1,14 +1,14 @@
require('isomorphic-fetch')
const fs = require('fs')
const path = require('path')
const tmpdir = require('os-tmpdir')
const gunzip = require('gunzip-maybe')
const mkdirp = require('mkdirp')
const tar = require('tar-fs')
const createMutex = require('./createMutex')
require("isomorphic-fetch")
const fs = require("fs")
const path = require("path")
const tmpdir = require("os-tmpdir")
const gunzip = require("gunzip-maybe")
const mkdirp = require("mkdirp")
const tar = require("tar-fs")
const createMutex = require("./createMutex")
function createTempPath(name, version) {
const normalName = name.replace(/\//g, '-')
const normalName = name.replace(/\//g, "-")
return path.join(tmpdir(), `unpkg-${normalName}-${version}`)
}
@ -17,12 +17,12 @@ function stripNamePrefix(headers) {
// so we shorten that to just "index.js" here. A few packages use a
// prefix other than "package/". e.g. the firebase package uses the
// "firebase_npm/" prefix. So we just strip the first dir name.
headers.name = headers.name.replace(/^[^\/]+\//, '')
headers.name = headers.name.replace(/^[^\/]+\//, "")
return headers
}
function ignoreSymlinks(file, headers) {
return headers.type === 'link'
return headers.type === "link"
}
function extractResponse(response, outputDir) {
@ -36,8 +36,8 @@ function extractResponse(response, outputDir) {
response.body
.pipe(gunzip())
.pipe(extract)
.on('finish', resolve)
.on('error', reject)
.on("finish", resolve)
.on("error", reject)
})
}
@ -54,7 +54,7 @@ const fetchMutex = createMutex((payload, callback) => {
fs.access(outputDir, function(error) {
if (error) {
if (error.code === 'ENOENT' || error.code === 'ENOTDIR') {
if (error.code === "ENOENT" || error.code === "ENOTDIR") {
// ENOENT or ENOTDIR are to be expected when we haven't yet
// fetched a package for the first time. Carry on!
mkdirp(outputDir, function(error) {

View File

@ -1,16 +1,16 @@
require('isomorphic-fetch')
const createCache = require('./createCache')
const createMutex = require('./createMutex')
require("isomorphic-fetch")
const createCache = require("./createCache")
const createMutex = require("./createMutex")
const RegistryURL = process.env.NPM_REGISTRY_URL || 'https://registry.npmjs.org'
const RegistryURL = process.env.NPM_REGISTRY_URL || "https://registry.npmjs.org"
const PackageInfoCache = createCache('packageInfo')
const PackageInfoCache = createCache("packageInfo")
function fetchPackageInfo(packageName) {
console.log(`info: Fetching package info for ${packageName}`)
let encodedPackageName
if (packageName.charAt(0) === '@') {
if (packageName.charAt(0) === "@") {
encodedPackageName = `@${encodeURIComponent(packageName.substring(1))}`
} else {
encodedPackageName = encodeURIComponent(packageName)
@ -20,14 +20,14 @@ function fetchPackageInfo(packageName) {
return fetch(url, {
headers: {
Accept: 'application/json'
Accept: "application/json"
}
}).then(res => {
return res.status === 404 ? null : res.json()
})
}
const PackageNotFound = 'PackageNotFound'
const PackageNotFound = "PackageNotFound"
// This mutex prevents multiple concurrent requests to
// the registry for the same package info.

View File

@ -1,4 +1,4 @@
const db = require('../../RedisClient')
const db = require("../../RedisClient")
function incrementCounter(counter, key, by) {
return new Promise((resolve, reject) => {

View File

@ -1,9 +1,9 @@
const fs = require('fs')
const path = require('path')
const csso = require('csso')
const fs = require("fs")
const path = require("path")
const csso = require("csso")
function readCSS(...args) {
return csso.minify(fs.readFileSync(path.resolve(...args), 'utf8')).css
return csso.minify(fs.readFileSync(path.resolve(...args), "utf8")).css
}
module.exports = readCSS