2020-05-06 06:12:36 -04:00
|
|
|
/* eslint-disable
|
|
|
|
no-return-assign,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-05-06 06:12:17 -04:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-05-06 06:12:47 -04:00
|
|
|
let MockWebApi
|
|
|
|
const express = require('express')
|
|
|
|
const bodyParser = require('body-parser')
|
|
|
|
const app = express()
|
|
|
|
const MAX_REQUEST_SIZE = 2 * (2 * 1024 * 1024 + 64 * 1024)
|
2020-05-06 06:12:17 -04:00
|
|
|
|
2020-05-06 06:12:47 -04:00
|
|
|
module.exports = MockWebApi = {
|
|
|
|
docs: {},
|
2020-05-06 06:12:17 -04:00
|
|
|
|
2020-05-06 06:12:47 -04:00
|
|
|
clearDocs() {
|
|
|
|
return (this.docs = {})
|
|
|
|
},
|
2020-05-06 06:12:17 -04:00
|
|
|
|
2023-03-21 08:06:13 -04:00
|
|
|
insertDoc(projectId, docId, doc) {
|
2020-05-06 06:12:47 -04:00
|
|
|
if (doc.version == null) {
|
|
|
|
doc.version = 0
|
|
|
|
}
|
|
|
|
if (doc.lines == null) {
|
|
|
|
doc.lines = []
|
|
|
|
}
|
|
|
|
doc.pathname = '/a/b/c.tex'
|
2023-03-21 08:06:13 -04:00
|
|
|
return (this.docs[`${projectId}:${docId}`] = doc)
|
2020-05-06 06:12:47 -04:00
|
|
|
},
|
2020-05-06 06:12:17 -04:00
|
|
|
|
2020-05-06 06:12:47 -04:00
|
|
|
setDocument(
|
2023-03-21 08:06:13 -04:00
|
|
|
projectId,
|
|
|
|
docId,
|
2020-05-06 06:12:47 -04:00
|
|
|
lines,
|
|
|
|
version,
|
|
|
|
ranges,
|
|
|
|
lastUpdatedAt,
|
|
|
|
lastUpdatedBy,
|
|
|
|
callback
|
|
|
|
) {
|
|
|
|
if (callback == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2020-05-06 06:12:47 -04:00
|
|
|
}
|
|
|
|
const doc =
|
2023-03-21 08:06:13 -04:00
|
|
|
this.docs[`${projectId}:${docId}`] ||
|
|
|
|
(this.docs[`${projectId}:${docId}`] = {})
|
2020-05-06 06:12:47 -04:00
|
|
|
doc.lines = lines
|
|
|
|
doc.version = version
|
|
|
|
doc.ranges = ranges
|
|
|
|
doc.pathname = '/a/b/c.tex'
|
|
|
|
doc.lastUpdatedAt = lastUpdatedAt
|
|
|
|
doc.lastUpdatedBy = lastUpdatedBy
|
|
|
|
return callback(null)
|
|
|
|
},
|
2020-05-06 06:12:17 -04:00
|
|
|
|
2023-03-21 08:06:13 -04:00
|
|
|
getDocument(projectId, docId, callback) {
|
2020-05-06 06:12:47 -04:00
|
|
|
if (callback == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2020-05-06 06:12:47 -04:00
|
|
|
}
|
2023-03-21 08:06:13 -04:00
|
|
|
return callback(null, this.docs[`${projectId}:${docId}`])
|
2020-05-06 06:12:47 -04:00
|
|
|
},
|
2020-05-06 06:12:17 -04:00
|
|
|
|
2020-05-06 06:12:47 -04:00
|
|
|
run() {
|
|
|
|
app.get('/project/:project_id/doc/:doc_id', (req, res, next) => {
|
|
|
|
return this.getDocument(
|
|
|
|
req.params.project_id,
|
|
|
|
req.params.doc_id,
|
|
|
|
(error, doc) => {
|
|
|
|
if (error != null) {
|
|
|
|
return res.sendStatus(500)
|
|
|
|
} else if (doc != null) {
|
|
|
|
return res.send(JSON.stringify(doc))
|
|
|
|
} else {
|
|
|
|
return res.sendStatus(404)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
2020-05-06 06:12:17 -04:00
|
|
|
|
2020-05-06 06:12:47 -04:00
|
|
|
app.post(
|
|
|
|
'/project/:project_id/doc/:doc_id',
|
|
|
|
bodyParser.json({ limit: MAX_REQUEST_SIZE }),
|
|
|
|
(req, res, next) => {
|
|
|
|
return MockWebApi.setDocument(
|
|
|
|
req.params.project_id,
|
|
|
|
req.params.doc_id,
|
|
|
|
req.body.lines,
|
|
|
|
req.body.version,
|
|
|
|
req.body.ranges,
|
|
|
|
req.body.lastUpdatedAt,
|
|
|
|
req.body.lastUpdatedBy,
|
2021-07-13 07:04:42 -04:00
|
|
|
error => {
|
2020-05-06 06:12:47 -04:00
|
|
|
if (error != null) {
|
|
|
|
return res.sendStatus(500)
|
|
|
|
} else {
|
2022-09-21 08:01:32 -04:00
|
|
|
return res.json({ rev: '123' })
|
2020-05-06 06:12:47 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
2020-05-06 06:12:17 -04:00
|
|
|
|
2020-05-06 06:12:47 -04:00
|
|
|
return app
|
2021-07-13 07:04:42 -04:00
|
|
|
.listen(3000, error => {
|
2020-05-06 06:12:47 -04:00
|
|
|
if (error != null) {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
})
|
2021-07-13 07:04:42 -04:00
|
|
|
.on('error', error => {
|
2020-05-06 06:12:47 -04:00
|
|
|
console.error('error starting MockWebApi:', error.message)
|
|
|
|
return process.exit(1)
|
|
|
|
})
|
2021-07-13 07:04:42 -04:00
|
|
|
},
|
2020-05-06 06:12:47 -04:00
|
|
|
}
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2020-05-06 06:12:47 -04:00
|
|
|
MockWebApi.run()
|