Comparing version 0.7.0 to 0.7.1
@@ -43,3 +43,4 @@ 'use strict'; | ||
const log = this.log = this.log || this.config.log || bunyan.createLogger({ name: 'flora', component: 'master' }); | ||
this.log = this.log || this.config.log || bunyan.createLogger({ name: 'flora', component: 'master' }); | ||
const log = this.log; | ||
callback = callback || function nop() {}; | ||
@@ -46,0 +47,0 @@ |
@@ -67,2 +67,11 @@ 'use strict'; | ||
/** | ||
* Payload data | ||
* | ||
* @type {Status} | ||
* @name Request#_status | ||
* @readonly | ||
*/ | ||
readable('data', options.data); | ||
/** | ||
* Status helper object | ||
@@ -69,0 +78,0 @@ * |
@@ -144,38 +144,34 @@ 'use strict'; | ||
_handleRequest(httpRequest, httpResponse) { | ||
let floraRequest; | ||
try { | ||
floraRequest = httpToFloraRequest(httpRequest); | ||
} catch (err) { | ||
return this._sendError(err, httpRequest, httpResponse); | ||
} | ||
if (!floraRequest) { | ||
if (this._staticHandler) { | ||
return this._staticHandler(httpRequest, httpResponse, () => { | ||
this._sendError( | ||
httpToFloraRequest(httpRequest) | ||
.then((floraRequest) => { | ||
if (!floraRequest) { | ||
if (this._staticHandler) { | ||
return this._staticHandler(httpRequest, httpResponse, () => { | ||
this._sendError( | ||
new NotFoundError(`URL "${httpRequest.url}" not found (not a valid resource url)`), | ||
httpRequest, httpResponse); | ||
}); | ||
} | ||
return this._sendError( | ||
new NotFoundError(`URL "${httpRequest.url}" not found (not a valid resource url)`), | ||
httpRequest, httpResponse); | ||
}); | ||
} | ||
return this._sendError( | ||
new NotFoundError(`URL "${httpRequest.url}" not found (not a valid resource url)`), | ||
httpRequest, httpResponse); | ||
} | ||
} | ||
if (!this.api.getResource(floraRequest.resource) && this._staticHandler) { | ||
return this._staticHandler(httpRequest, httpResponse, () => { | ||
this._sendError( | ||
new NotFoundError(`Resource "${floraRequest.resource}" not found`), | ||
httpRequest, httpResponse); | ||
}); | ||
} | ||
if (!this.api.getResource(floraRequest.resource) && this._staticHandler) { | ||
return this._staticHandler(httpRequest, httpResponse, () => { | ||
this._sendError( | ||
new NotFoundError(`Resource "${floraRequest.resource}" not found`), | ||
httpRequest, httpResponse); | ||
}); | ||
} | ||
return this.api.execute(floraRequest, (err, response) => { | ||
if (err) { | ||
err.response = response; | ||
return this._sendError(err, httpRequest, httpResponse); | ||
} | ||
return this._sendResponse(response, httpRequest, httpResponse); | ||
}); | ||
return this.api.execute(floraRequest, (err, response) => { | ||
if (err) { | ||
err.response = response; | ||
return this._sendError(err, httpRequest, httpResponse); | ||
} | ||
return this._sendResponse(response, httpRequest, httpResponse); | ||
}); | ||
}) | ||
.catch(err => this._sendError(err, httpRequest, httpResponse)); | ||
} | ||
@@ -182,0 +178,0 @@ |
'use strict'; | ||
const url = require('url'); | ||
const querystring = require('querystring'); | ||
@@ -8,2 +9,3 @@ const { RequestError } = require('flora-errors'); | ||
const Request = require('./request'); | ||
const contentType = require('content-type'); | ||
@@ -14,31 +16,84 @@ /** | ||
* @param httpRequest | ||
* @return {Object|null} | ||
* @return {Promise} | ||
*/ | ||
function httpToFloraRequest(httpRequest) { | ||
const parsedUrl = url.parse(httpRequest.url, true); | ||
const matches = parsedUrl.pathname.match(/^\/(.+)\/([^/.]*)(?:\.([a-z]+))?$/); | ||
if (!matches) return null; | ||
return new Promise((resolve, reject) => { | ||
const parsedUrl = url.parse(httpRequest.url, true); | ||
const matches = parsedUrl.pathname.match(/^\/(.+)\/([^/.]*)(?:\.([a-z]+))?$/); | ||
if (!matches) { | ||
resolve(null); | ||
return; | ||
} | ||
const opts = { | ||
resource: matches[1], | ||
_status: httpRequest.flora.status, | ||
_httpRequest: httpRequest | ||
}; | ||
/* | ||
* Gather GET parameters. | ||
*/ | ||
if (matches[2]) opts.id = matches[2]; | ||
if (matches[3]) opts.format = matches[3]; | ||
const opts = { | ||
resource: matches[1], | ||
_status: httpRequest.flora.status, | ||
_httpRequest: httpRequest | ||
}; | ||
Object.keys(parsedUrl.query).forEach((key) => { | ||
if (!Object.prototype.hasOwnProperty.call(opts, key)) { | ||
if (Array.isArray(parsedUrl.query[key])) { | ||
throw new RequestError(`Duplicate parameter "${key}" in URL`); | ||
if (matches[2]) opts.id = matches[2]; | ||
if (matches[3]) opts.format = matches[3]; | ||
Object.keys(parsedUrl.query).forEach((key) => { | ||
if (!Object.prototype.hasOwnProperty.call(opts, key)) { | ||
if (Array.isArray(parsedUrl.query[key])) { | ||
reject(new RequestError(`Duplicate parameter "${key}" in URL`)); | ||
return; | ||
} | ||
opts[key] = parsedUrl.query[key]; | ||
} | ||
}); | ||
opts[key] = parsedUrl.query[key]; | ||
} | ||
/* | ||
* Handle POST payload. | ||
*/ | ||
let payload = ''; | ||
const contentTypes = contentType.parse(httpRequest.headers['content-type']); | ||
httpRequest.setEncoding(contentTypes.parameters.charset || 'utf-8'); | ||
httpRequest.on('data', (chunk) => { | ||
payload += chunk; | ||
}); | ||
httpRequest.on('end', () => { | ||
if (httpRequest.method === 'POST' && payload.length) { | ||
if (contentTypes.type === 'application/x-www-form-urlencoded') { | ||
payload = querystring.parse(payload); | ||
Object.keys(payload).forEach((key) => { | ||
if (!Object.prototype.hasOwnProperty.call(opts, key)) { | ||
if (Array.isArray(payload[key])) { | ||
reject(new RequestError(`Duplicate parameter "${key}" in Payload`)); | ||
return; | ||
} | ||
opts[key] = payload[key]; | ||
} | ||
}); | ||
} else if (contentTypes.type === 'application/json') { | ||
try { | ||
opts.data = JSON.parse(payload); | ||
} catch (err) { | ||
reject(new RequestError('Invalid payload, must be valid JSON')); | ||
return; | ||
} | ||
} else { | ||
reject(new RequestError('Content-Type not supported')); | ||
return; | ||
} | ||
} | ||
resolve(new Request(opts)); | ||
}); | ||
httpRequest.on('error', err => reject(new RequestError('Error reading HTTP-Request: ' + err.message))); | ||
}); | ||
return new Request(opts); | ||
} | ||
module.exports = httpToFloraRequest; |
{ | ||
"name": "flora", | ||
"version": "0.7.0", | ||
"version": "0.7.1", | ||
"description": "FLexible Open Rest API", | ||
@@ -44,2 +44,3 @@ "main": "index.js", | ||
"chokidar": "^1.0.4", | ||
"content-type": "^1.0.2", | ||
"flora-cluster": "^0.7.1", | ||
@@ -46,0 +47,0 @@ "flora-errors": "^0.7.0", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
158532
13
31
3665
+ Addedcontent-type@^1.0.2
+ Addedcontent-type@1.0.5(transitive)