-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.js
175 lines (141 loc) · 4.93 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
'use strict'
const {BigQuery} = require('@google-cloud/bigquery')
const Datastore = require('@google-cloud/datastore')
const {KVStore} = require('google-cloud-kvstore')
const express = require('express')
const githubUrl = require('github-url-from-git')
const packageJson = require('package-json')
const through = require('through2')
const validateNpmPackageName = require('validate-npm-package-name')
const config = {projectId: 'git-npm', keyFilename: './key.json'}
const datastore = new Datastore(config)
const bigQuery = new BigQuery(config)
const logDataset = new KVStore(datastore)
const logTable = bigQuery.dataset('gitnpm').table('npm_packages')
function parseUrl(pkg) {
const repository = pkg.repository
if (!repository) return 'https://npmjs.org/package/' + pkg.name
if (repository.url) return githubUrl(repository.url)
const hosts = {
gist: {
pattern: /gist:(\w+)/,
getUrl: function (match) {
return 'https://gist.github.com/' + match[1]
}
},
bitbucket: {
pattern: /bitbucket:([^/]+)\/(.+)/,
getUrl: function (match) {
return 'https://bitbucket.org/' + match[1] + '/' + match[2]
}
},
gitlab: {
pattern: /gitlab:([^/]+)\/(.+)/,
getUrl: function (match) {
return 'https://gitlab.com/' + match[1] + '/' + match[2]
}
},
github: {
pattern: /([^/]+)\/(.+)/,
getUrl: function (match) {
return 'https://github.com/' + match[1] + '/' + match[2]
}
}
}
for (const host in hosts) {
const pattern = hosts[host].pattern
const getUrl = hosts[host].getUrl
if (pattern.test(repository)) return getUrl(pattern.exec(repository))
}
}
function validatePkgName(req, res, next) {
const isNameValid = validateNpmPackageName(req.params.pkgName)
if (!isNameValid.validForNewPackages && !isNameValid.validForOldPackages) {
return res.end('this looks funky. try something else')
}
next()
}
function getPkgInfo(req, res, next) {
const pkgName = req.params.pkgName
packageJson(pkgName, function (err, json) {
if (err) return res.end(pkgName + ' isn\'t a thing... go make it?')
const latestVersion = json['dist-tags'] && json['dist-tags'].latest
res._pkgInfo = {
all: json,
latest: latestVersion ? json.versions[latestVersion] : {}
}
next()
})
}
const app = express()
app
.set('json spaces', 2)
// display a form to accept a package name
.get('/', function (req, res) {
if (req.query.pkgName) return res.redirect('/' + req.query.pkgName)
res.write('<div style="margin-top:40vh;text-align:center">')
res.write(' <form method=get action=/ style="font:3em monospace">')
res.write(' <span style="color:#F08">$</span> npm repo')
res.write(' <input name=pkgName placeholder=pkgname size=10 style="font:1em monospace;color:#777;border:0">')
res.end()
})
// redirect to a package's github
.get('/:pkgName', validatePkgName, getPkgInfo, function (req, res) {
const pkgName = req.params.pkgName
const pkg = res._pkgInfo
const url = parseUrl(pkg.latest)
res.redirect(url)
logDataset.set(pkgName, url, console.log)
logTable.insert({ name: pkgName, url: url, created: (new Date()).toJSON() }, console.log)
})
.get('/:pkgName/json', validatePkgName, getPkgInfo, function (req, res) {
const pkg = res._pkgInfo
res.json(pkg.latest || pkg.all)
res.end()
})
.get('/:pkgName/:version/json', validatePkgName, getPkgInfo, function (req, res) {
const pkg = res._pkgInfo
const version = req.params.version.replace(/^v/, '')
const json = pkg.all.versions[version]
if (!json) {
res.json(new Error('Could not load requested version'))
} else {
res.json(json)
}
res.end()
})
.get('/:pkgName/json/:prop', validatePkgName, getPkgInfo, function (req, res) {
const pkg = res._pkgInfo
const prop = req.params.prop
if (!pkg.latest) {
res.json(new Error('Could not parse property'))
} else {
res.json(pkg.latest[prop])
}
res.end()
})
.get('/:pkgName/:version/json/:prop', validatePkgName, getPkgInfo, function (req, res) {
const pkg = res._pkgInfo
const version = req.params.version.replace(/^v/, '')
const prop = req.params.prop
const json = pkg.all.versions[version]
if (!json) {
res.json(new Error('Could not load requested version'))
} else {
res.json(json[prop])
}
res.end()
})
.get('/:pkgName/hits', validatePkgName, function (req, res) {
const pkgName = req.params.pkgName
res.write('<h1>redirects from gitnpm.com/' + pkgName + '</h1>')
res.write('<em>running query...</em> ')
logTable
.query('SELECT * FROM npm_packages WHERE name="' + pkgName + '" ORDER BY created DESC')
.pipe(through.obj(function (row, enc, next) {
next(null, '<p>' + new Date(row.created * 1000) + '</p>')
}))
.on('prefinish', res.write.bind(res, 'done.'))
.pipe(res)
})
app.listen(process.env.PORT || 8080)