-
Notifications
You must be signed in to change notification settings - Fork 3
/
middleware.js
180 lines (159 loc) · 6.39 KB
/
middleware.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
176
177
178
179
180
const fs = require('fs')
const fp = require('fs').promises
const path = require('path')
const url = require('url')
const EventEmitter = require('events').EventEmitter
const { E, indexPack, plugins, readObject, resolveRef, serveInfoRefs, serveReceivePack, parseReceivePackRequest } = require('isomorphic-git')
const { pgp } = require('@isomorphic-git/pgp-plugin')
let ee = new EventEmitter()
plugins.set('emitter', ee)
plugins.set('fs', fs)
plugins.set('pgp', pgp)
const chalk = require('chalk')
const is = require('./identify-request.js')
const parse = require('./parse-request.js')
const { lookup, demote } = require('./lookup.js')
const { sandbox } = require('./sandbox.js')
function pad (str) {
return (str + ' ').slice(0, 7)
}
function abbr (oid) {
return oid.slice(0, 7)
}
const sleep = ms => new Promise(cb => setTimeout(cb, ms))
const tick = () => new Promise(cb => process.nextTick(cb))
function logIncoming(req, res) {
const color = res.statusCode > 399 ? chalk.red : chalk.green
console.log(` ${pad(req.method)} ${req.url}`)
return false
}
function logOutgoing(req, res) {
const color = res.statusCode > 399 ? chalk.red : chalk.green
console.log(color(`${res.statusCode} ${pad(req.method)} ${req.url}`))
return false
}
function factory (config) {
fp.mkdir(path.join(__dirname, 'quarantine'), { recursive: true })
return async function middleware (req, res, next) {
const u = url.parse(req.url, true)
if (!next) next = () => void(0)
logIncoming(req, res)
if (is.preflightInfoRefs(req, u)) {
res.statusCode = 204
res.end('')
} else if (is.preflightPull(req, u)) {
res.statusCode = 204
res.end('')
} else if (is.preflightPush(req, u)) {
res.statusCode = 204
res.end('')
} else if (is.infoRefs(req, u)) {
let { gitdir, service } = parse.infoRefs(req, u)
gitdir = path.join(__dirname, gitdir)
const { headers, response } = await serveInfoRefs({ fs, gitdir, service })
for (const header in headers) {
res.setHeader(header, headers[header])
}
res.statusCode = 200
for (const buffer of response) {
res.write(buffer)
}
res.end()
} else if (is.pull(req, u)) {
const { gitdir } = parse.pull(req, u)
res.statusCode = 500
res.end('Unsupported operation\n')
} else if (is.push(req, u)) {
let { gitdir, service } = parse.push(req, u)
try {
let { capabilities, updates, packfile } = await parseReceivePackRequest(req)
// Save packfile to quarantine folder
const dir = await fp.mkdtemp(path.join(__dirname, 'quarantine', gitdir + '-'))
let filepath = 'pack-.pack'
const stream = fs.createWriteStream(path.join(dir, filepath))
let last20
for await (const buffer of packfile) {
if (buffer) {
last20 = buffer.slice(-20)
stream.write(buffer)
}
}
stream.end()
if (last20 && last20.length === 20) {
last20 = last20.toString('hex')
const oldfilepath = filepath
filepath = `pack-${last20}.pack`
await fp.rename(path.join(dir, oldfilepath), path.join(dir, filepath))
}
const core = gitdir + '-' + String(Math.random()).slice(2, 8)
gitdir = path.join(__dirname, gitdir)
// send HTTP response headers
const { headers } = await serveReceivePack({ type: 'service', service })
res.writeHead(200, headers)
// index packfile
res.write(await serveReceivePack({ type: 'print', message: 'Indexing packfile...' }))
await tick()
let currentPhase = null
const listener = async ({ phase, loaded, total, lengthComputable }) => {
let np = phase !== currentPhase ? '\n' : '\r'
currentPhase = phase
res.write(await serveReceivePack({ type: 'print', message: `${np}${phase} ${loaded}/${total}` }))
}
let oids
try {
ee.on(`${last20}:progress`, listener)
oids = await indexPack({ fs, gitdir, dir, filepath, emitterPrefix: `${last20}:` })
await tick()
res.write(await serveReceivePack({ type: 'print', message: '\nIndexing completed' }))
res.write(await serveReceivePack({ type: 'unpack', unpack: 'ok' }))
} catch (e) {
res.write(await serveReceivePack({ type: 'print', message: '\nOh dear!' }))
res.write(await serveReceivePack({ type: 'unpack', unpack: e.message }))
for (const update of updates) {
res.write(await serveReceivePack({ type: 'ng', ref: update.fullRef, message: 'Could not index pack' }))
}
throw e
} finally {
ee.removeListener(`${last20}:progress`, listener)
}
await tick()
// Move packfile and index into repo
await fp.mkdir(path.join(gitdir, 'objects', 'pack'), { recursive: true })
await fp.rename(path.join(dir, filepath), path.join(gitdir, 'objects', 'pack', filepath))
await fp.rename(path.join(dir, filepath.replace(/\.pack$/, '.idx')), path.join(gitdir, 'objects', 'pack', filepath.replace(/\.pack$/, '.idx')))
await fp.rmdir(path.join(dir))
// Run pre-receive-hook
res.write(await serveReceivePack({ type: 'print', message: '\nRunning pre-receive hook\n' }))
await tick()
let script
try {
const oid = await resolveRef({ gitdir, ref: 'HEAD' })
const { object } = await readObject({ gitdir, oid, filepath: '.hooks/pre-receive.js', encoding: 'utf8' })
script = object
} catch (e) {
console.log(e)
script = fs.readFileSync('./pre-receive-hook.js', 'utf8')
}
await sandbox({ name: 'pre-receive.js', core, dir, gitdir, res, oids, updates, script })
// refs
for (const update of updates) {
res.write(await serveReceivePack({ type: 'ok', ref: update.fullRef }))
}
// gratuitous banner
res.write(await serveReceivePack({ type: 'print', message: '\n' + require('./logo.js') }))
} catch (e) {
if (e.message === 'Client is done') {
res.statusCode = 200
} else {
res.write(await serveReceivePack({ type: 'error', message: `${e.message}\n${e.stack}` }))
}
} finally {
// fin
res.write(await serveReceivePack({ type: 'fin' }))
res.end('')
}
}
logOutgoing(req, res)
}
}
module.exports = factory