Replace request library with node-fetch

Signed-off-by: Erik Michelson <github@erik.michelson.eu>
This commit is contained in:
Erik Michelson 2021-03-11 16:40:24 +01:00
parent 91846ac9a6
commit 731fb24500
No known key found for this signature in database
GPG key ID: DB99ADDDC5C0AF82
3 changed files with 64 additions and 55 deletions

View file

@ -3,7 +3,7 @@
// external modules // external modules
const fs = require('fs') const fs = require('fs')
const path = require('path') const path = require('path')
const request = require('request') const fetch = require('node-fetch')
// core // core
const config = require('./config') const config = require('./config')
const logger = require('./logger') const logger = require('./logger')
@ -76,46 +76,57 @@ function githubActionGist (req, res, note) {
state: state state: state
} }
const authUrl = 'https://github.com/login/oauth/access_token' const authUrl = 'https://github.com/login/oauth/access_token'
request({ fetch(authUrl, {
url: authUrl,
method: 'POST', method: 'POST',
json: data body: JSON.stringify(data),
}, function (error, httpResponse, body) { headers: {
if (!error && httpResponse.statusCode === 200) { 'Content-Type': 'application/json',
const accessToken = body.access_token Accept: 'application/json'
if (accessToken) { }
const content = note.content }).then(resp => {
const title = models.Note.decodeTitle(note.title) if (!resp.ok) {
const filename = title.replace('/', ' ') + '.md' throw new Error('forbidden')
const gist = { }
files: {} return resp.json()
} }).then(body => {
gist.files[filename] = { const accessToken = body.access_token
content: content if (!accessToken) {
} throw new Error('forbidden')
const gistUrl = 'https://api.github.com/gists' }
request({ const content = note.content
url: gistUrl, const title = models.Note.decodeTitle(note.title)
headers: { const filename = title.replace('/', ' ') + '.md'
'User-Agent': 'HedgeDoc', const gist = {
Authorization: 'token ' + accessToken files: {}
}, }
method: 'POST', gist.files[filename] = {
json: gist content: content
}, function (error, httpResponse, body) { }
if (!error && httpResponse.statusCode === 201) { const gistUrl = 'https://api.github.com/gists'
res.setHeader('referer', '') return fetch(gistUrl, {
res.redirect(body.html_url) method: 'POST',
} else { body: JSON.stringify(gist),
return errors.errorForbidden(res) headers: {
} 'User-Agent': 'HedgeDoc',
}) Authorization: 'token ' + accessToken,
} else { 'Content-Type': 'application/json',
return errors.errorForbidden(res) Accept: 'application/json'
} }
} else { })
}).then(resp => {
if (resp.status !== 201) {
throw new Error('forbidden')
}
return resp.json()
}).then(body => {
res.setHeader('referer', '')
res.redirect(body.html_url)
}).catch(error => {
if (error.message === 'forbidden') {
return errors.errorForbidden(res) return errors.errorForbidden(res)
} }
logger.error('GitHub Gist auth failed: ' + error)
return errors.errorInternalError(res)
}) })
} }
} }
@ -146,17 +157,17 @@ function gitlabActionProjects (req, res, note) {
const ret = { baseURL: config.gitlab.baseURL, version: config.gitlab.version } const ret = { baseURL: config.gitlab.baseURL, version: config.gitlab.version }
ret.accesstoken = user.accessToken ret.accesstoken = user.accessToken
ret.profileid = user.profileid ret.profileid = user.profileid
request( const apiUrl = `${config.gitlab.baseURL}/api/${config.gitlab.version}/projects?membership=yes&per_page=100&access_token=${user.accessToken}`
config.gitlab.baseURL + '/api/' + config.gitlab.version + '/projects?membership=yes&per_page=100&access_token=' + user.accessToken, fetch(apiUrl).then(resp => {
function (error, httpResponse, body) { if (!resp.ok) {
if (!error && httpResponse.statusCode === 200) { res.send(ret)
ret.projects = JSON.parse(body) throw new Error('HTTP request returned not okay-ish status')
return res.send(ret)
} else {
return res.send(ret)
}
} }
) return resp.json()
}).then(body => {
ret.projects = body
return res.send(ret)
})
}).catch(function (err) { }).catch(function (err) {
logger.error('gitlab action projects failed: ' + err) logger.error('gitlab action projects failed: ' + err)
return errors.errorInternalError(res) return errors.errorInternalError(res)

View file

@ -91,6 +91,7 @@
"moment": "^2.17.1", "moment": "^2.17.1",
"morgan": "^1.7.0", "morgan": "^1.7.0",
"mysql2": "^2.0.0", "mysql2": "^2.0.0",
"node-fetch": "^2.6.1",
"passport": "^0.4.0", "passport": "^0.4.0",
"passport-dropbox-oauth2": "^1.1.0", "passport-dropbox-oauth2": "^1.1.0",
"passport-facebook": "^3.0.0", "passport-facebook": "^3.0.0",
@ -110,7 +111,6 @@
"randomcolor": "^0.6.0", "randomcolor": "^0.6.0",
"raphael": "^2.3.0", "raphael": "^2.3.0",
"readline-sync": "^1.4.7", "readline-sync": "^1.4.7",
"request": "^2.88.0",
"reveal.js": "^3.9.2", "reveal.js": "^3.9.2",
"rimraf": "^3.0.2", "rimraf": "^3.0.2",
"scrypt-async": "^2.0.1", "scrypt-async": "^2.0.1",

View file

@ -448,7 +448,6 @@
"Idle.Js@git+https://github.com/shawnmclean/Idle.js": "Idle.Js@git+https://github.com/shawnmclean/Idle.js":
version "0.0.1" version "0.0.1"
uid db9beb3483a460ad638ec947867720f0ed066a62
resolved "git+https://github.com/shawnmclean/Idle.js#db9beb3483a460ad638ec947867720f0ed066a62" resolved "git+https://github.com/shawnmclean/Idle.js#db9beb3483a460ad638ec947867720f0ed066a62"
JSV@^4.0.x: JSV@^4.0.x:
@ -2193,7 +2192,6 @@ code-point-at@^1.0.0:
"codemirror@git+https://github.com/hedgedoc/CodeMirror.git": "codemirror@git+https://github.com/hedgedoc/CodeMirror.git":
version "5.58.2" version "5.58.2"
uid f780b569b3717cdff4c8507538cc63101bfa02e1
resolved "git+https://github.com/hedgedoc/CodeMirror.git#f780b569b3717cdff4c8507538cc63101bfa02e1" resolved "git+https://github.com/hedgedoc/CodeMirror.git#f780b569b3717cdff4c8507538cc63101bfa02e1"
collection-visit@^1.0.0: collection-visit@^1.0.0:
@ -3225,7 +3223,6 @@ detect-libc@^1.0.2:
"diff-match-patch@git+https://github.com/hackmdio/diff-match-patch.git": "diff-match-patch@git+https://github.com/hackmdio/diff-match-patch.git":
version "1.1.1" version "1.1.1"
uid c2f8fb9d69aa9490b764850aa86ba442c93ccf78
resolved "git+https://github.com/hackmdio/diff-match-patch.git#c2f8fb9d69aa9490b764850aa86ba442c93ccf78" resolved "git+https://github.com/hackmdio/diff-match-patch.git#c2f8fb9d69aa9490b764850aa86ba442c93ccf78"
diff@5.0.0: diff@5.0.0:
@ -4923,7 +4920,6 @@ image-size@~0.5.0:
"imgur@git+https://github.com/hackmdio/node-imgur.git": "imgur@git+https://github.com/hackmdio/node-imgur.git":
version "0.5.0" version "0.5.0"
uid de0a7a1f1eb2cb6628385fedb990ad396a190573
resolved "git+https://github.com/hackmdio/node-imgur.git#de0a7a1f1eb2cb6628385fedb990ad396a190573" resolved "git+https://github.com/hackmdio/node-imgur.git#de0a7a1f1eb2cb6628385fedb990ad396a190573"
dependencies: dependencies:
commander "^2.13.0" commander "^2.13.0"
@ -5526,7 +5522,6 @@ js-cookie@^2.1.3:
"js-sequence-diagrams@git+https://github.com/hedgedoc/js-sequence-diagrams.git": "js-sequence-diagrams@git+https://github.com/hedgedoc/js-sequence-diagrams.git":
version "2.0.1" version "2.0.1"
uid bda0e49b6c2754f3c7158b1dfb9ccf26efc24b39
resolved "git+https://github.com/hedgedoc/js-sequence-diagrams.git#bda0e49b6c2754f3c7158b1dfb9ccf26efc24b39" resolved "git+https://github.com/hedgedoc/js-sequence-diagrams.git#bda0e49b6c2754f3c7158b1dfb9ccf26efc24b39"
dependencies: dependencies:
lodash "4.17.x" lodash "4.17.x"
@ -6090,7 +6085,6 @@ lutim@^1.0.2:
"lz-string@git+https://github.com/hackmdio/lz-string.git": "lz-string@git+https://github.com/hackmdio/lz-string.git":
version "1.4.4" version "1.4.4"
uid efd1f64676264d6d8871b01f4f375fc6ef4f9022
resolved "git+https://github.com/hackmdio/lz-string.git#efd1f64676264d6d8871b01f4f375fc6ef4f9022" resolved "git+https://github.com/hackmdio/lz-string.git#efd1f64676264d6d8871b01f4f375fc6ef4f9022"
make-dir@^1.0.0: make-dir@^1.0.0:
@ -6404,7 +6398,6 @@ messageformat@^2.3.0:
"meta-marked@git+https://github.com/hedgedoc/meta-marked": "meta-marked@git+https://github.com/hedgedoc/meta-marked":
version "0.4.5" version "0.4.5"
uid "4fb5cb5a204969cc91e66eee92c0211188e69a2b"
resolved "git+https://github.com/hedgedoc/meta-marked#4fb5cb5a204969cc91e66eee92c0211188e69a2b" resolved "git+https://github.com/hedgedoc/meta-marked#4fb5cb5a204969cc91e66eee92c0211188e69a2b"
dependencies: dependencies:
js-yaml "~3.14.0" js-yaml "~3.14.0"
@ -6869,6 +6862,11 @@ node-addon-api@^3.0.0:
resolved "https://registry.yarnpkg.com/node-addon-api/-/node-addon-api-3.1.0.tgz#98b21931557466c6729e51cb77cd39c965f42239" resolved "https://registry.yarnpkg.com/node-addon-api/-/node-addon-api-3.1.0.tgz#98b21931557466c6729e51cb77cd39c965f42239"
integrity sha512-flmrDNB06LIl5lywUz7YlNGZH/5p0M7W28k8hzd9Lshtdh1wshD2Y+U4h9LD6KObOy1f+fEVdgprPrEymjM5uw== integrity sha512-flmrDNB06LIl5lywUz7YlNGZH/5p0M7W28k8hzd9Lshtdh1wshD2Y+U4h9LD6KObOy1f+fEVdgprPrEymjM5uw==
node-fetch@^2.6.1:
version "2.6.1"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.1.tgz#045bd323631f76ed2e2b55573394416b639a0052"
integrity sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw==
node-forge@^0.10.0: node-forge@^0.10.0:
version "0.10.0" version "0.10.0"
resolved "https://registry.yarnpkg.com/node-forge/-/node-forge-0.10.0.tgz#32dea2afb3e9926f02ee5ce8794902691a676bf3" resolved "https://registry.yarnpkg.com/node-forge/-/node-forge-0.10.0.tgz#32dea2afb3e9926f02ee5ce8794902691a676bf3"