Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 26 additions & 18 deletions lib/webid/lib/get.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,35 @@
module.exports = get

const request = require('request')
const fetch = require('node-fetch')
const url = require('url')

function get (webid, callback) {
const uri = url.URL(webid)
const options = {
url: uri,
method: 'GET',
headers: {
Accept: 'text/turtle, application/ld+json'
}
let uri
try {
uri = new url.URL(webid)
} catch (err) {
return callback(new Error('Invalid WebID URI: ' + webid + ': ' + err.message))
}

request(options, function (err, res, body) {
if (err) {
return callback(new Error('Failed to fetch profile from ' + uri.href + ': ' + err))
}

if (res.statusCode !== 200) {
return callback(new Error('Failed to retrieve WebID from ' + uri.href + ': HTTP ' + res.statusCode))
}
const headers = {
Accept: 'text/turtle, application/ld+json'
}

callback(null, body, res.headers['content-type'])
})
fetch(uri.href, { method: 'GET', headers })
.then(async res => {
if (!res.ok) {
return callback(new Error('Failed to retrieve WebID from ' + uri.href + ': HTTP ' + res.status))
}
const contentType = res.headers.get('content-type')
let body
if (contentType && contentType.includes('json')) {
body = JSON.stringify(await res.json(), null, 2)
} else {
body = await res.text()
}
callback(null, body, contentType)
})
.catch(err => {
return callback(new Error('Failed to fetch profile from ' + uri.href + ': ' + err))
})
}
Loading
Loading