Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prep #1781

Closed
wants to merge 4 commits into from
Closed

Prep #1781

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
12 changes: 12 additions & 0 deletions lib/create-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ const ResourceMapper = require('./resource-mapper')
const aclCheck = require('@solid/acl-check')
const { version } = require('../package.json')

const { awaitSync } = require('@kaciras/deasync')

const [acceptEvents, events, eventID, prep] = (awaitSync(Promise.all([
import('express-accept-events'),
import('express-events-negotiate'),
import('express-prep/event-id'),
import('express-prep')
]))).map(lib => lib.default)

const corsSettings = cors({
methods: [
'OPTIONS', 'HEAD', 'GET', 'PATCH', 'POST', 'PUT', 'DELETE'
Expand Down Expand Up @@ -61,6 +70,9 @@ function createApp (argv = {}) {

const app = express()

// Add PREP support
app.use(acceptEvents, events, eventID, prep)

initAppLocals(app, argv, ldp)
initHeaders(app)
initViews(app, configPath)
Expand Down
47 changes: 40 additions & 7 deletions lib/handlers/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const translate = require('../utils.js').translate
const error = require('../http-error')

const RDFs = require('../ldp').mimeTypesAsArray()
const isRdf = require('../ldp').mimeTypeIsRdf

async function handler (req, res, next) {
const ldp = req.app.locals.ldp
Expand Down Expand Up @@ -110,15 +111,39 @@ async function handler (req, res, next) {
}

// If request accepts the content-type we found
// if (stream && negotiator.mediaType([contentType])) {
// res.setHeader('Content-Type', contentType)
// if (contentRange) {
// const headers = { 'Content-Range': contentRange, 'Accept-Ranges': 'bytes', 'Content-Length': chunksize }
// res.writeHead(206, headers)
// return stream.pipe(res)
// } else {
// return stream.pipe(res)
// }
// }

if (stream && negotiator.mediaType([contentType])) {
res.setHeader('Content-Type', contentType)
let headers = {
'Content-Type': contentType
}
if (contentRange) {
const headers = { 'Content-Range': contentRange, 'Accept-Ranges': 'bytes', 'Content-Length': chunksize }
res.writeHead(206, headers)
return stream.pipe(res)
} else {
return stream.pipe(res)
headers = {
...headers,
'Content-Range': contentRange,
'Accept-Ranges': 'bytes',
'Content-Length': chunksize
}
res.statusCode = 206
}

// PREP response
/* if (isRdf(contentType) && !res.sendEvents({
config: { prep: '' },
body: stream,
headers
})) return */
res.set(headers)
return stream.pipe(res)
}

// If it is not in our RDFs we can't even translate,
Expand All @@ -130,8 +155,16 @@ async function handler (req, res, next) {
// Translate from the contentType found to the possibleRDFType desired
const data = await translate(stream, baseUri, contentType, possibleRDFType)
debug(req.originalUrl + ' translating ' + contentType + ' -> ' + possibleRDFType)
const headers = {
'Content-Type': possibleRDFType
}
if (isRdf(contentType) && !res.sendEvents({
config: { prep: '' },
body: data,
headers
})) return
res.setHeader('Content-Type', possibleRDFType)
res.send(data)
res.write(data) // res.send(data)
return next()
} catch (err) {
debug('error translating: ' + req.originalUrl + ' ' + contentType + ' -> ' + possibleRDFType + ' -- ' + 406 + ' ' + err.message)
Expand Down
7 changes: 7 additions & 0 deletions lib/handlers/patch.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ const crypto = require('crypto')
const { overQuota, getContentType } = require('../utils')
const withLock = require('../lock')

const { awaitSync } = require('@kaciras/deasync')
const rfc822Template = awaitSync(import('express-prep/templates')).rfc822

// Patch parsers by request body content type
const PATCH_PARSERS = {
'application/sparql-update': require('./patch/sparql-update-parser.js'),
Expand Down Expand Up @@ -93,6 +96,10 @@ async function patchHandler (req, res, next) {

// Send the result to the client
res.send(result)
// PREP response
res.events.prep.trigger({
generateNotifications: () => `\r\n${rfc822Template({ res })}`
})
} catch (err) {
return next(err)
}
Expand Down
186 changes: 182 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading