Skip to content

Commit

Permalink
Merge pull request #5 from FreeAllMedia/feature/weakmaps
Browse files Browse the repository at this point in the history
weakmaps implementation
  • Loading branch information
nicosommi committed Aug 12, 2015
2 parents 5137e50 + 4d923ea commit 72f05c8
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 110 deletions.
36 changes: 18 additions & 18 deletions es5/lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,23 @@ Object.defineProperty(exports, "__esModule", {

var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var _incognito = require("incognito");

var _incognito2 = _interopRequireDefault(_incognito);

var loadDynamicMethods = Symbol();

var Response = (function () {
function Response(expressResponse, middlewares) {
_classCallCheck(this, Response);

Object.defineProperties(this, {
"_response": {
enumerable: false,
value: expressResponse
},
"_middlewares": {
enumerable: false,
value: middlewares
}
});
var _ = (0, _incognito2["default"])(this);
_._response = expressResponse;
_._middlewares = middlewares;

this[loadDynamicMethods]();
}
Expand All @@ -33,12 +32,13 @@ var Response = (function () {
value: function value() {
var _this = this;

var _ = (0, _incognito2["default"])(this);
var statuses = require("../../http.statuses.json");
if (Array.isArray(statuses)) {
statuses.forEach(function (status) {
_this[status.name] = function (data) {
//call hook for data format middleware in a pipeline
_this._middlewares.forEach(function (middleware) {
_._middlewares.forEach(function (middleware) {
if (middleware.formatResponse && typeof middleware.formatResponse === "function") {
middleware.formatResponse(_this);
}
Expand All @@ -56,38 +56,38 @@ var Response = (function () {
}, {
key: "end",
value: function end(message) {
this._response.end(message);
(0, _incognito2["default"])(this)._response.end(message);
}
}, {
key: "status",
value: function status(code) {
this._response.status(code);
(0, _incognito2["default"])(this)._response.status(code);
return this;
}
}, {
key: "json",
value: function json(data) {
this._response.json(data);
(0, _incognito2["default"])(this)._response.json(data);
}
}, {
key: "send",
value: function send(data) {
this._response.send(data);
(0, _incognito2["default"])(this)._response.send(data);
}
}, {
key: "download",
value: function download(data) {
this._response.download(data);
(0, _incognito2["default"])(this)._response.download(data);
}
}, {
key: "set",
value: function set(key, value) {
this._response.set(key, value);
(0, _incognito2["default"])(this)._response.set(key, value);
}
}, {
key: "get",
value: function get(key) {
return this._response.get(key);
return (0, _incognito2["default"])(this)._response.get(key);
}
}]);

Expand Down
12 changes: 9 additions & 3 deletions es5/lib/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ var _upcast = require("upcast");

var _upcast2 = _interopRequireDefault(_upcast);

var _incognito = require("incognito");

var _incognito2 = _interopRequireDefault(_incognito);

var setupFilters = Symbol("setupFilters"),
setupDynamicProperties = Symbol("setupDynamicProperties"),
actionNames = Symbol("actionNames"),
Expand All @@ -41,11 +45,13 @@ var Route = (function (_EventEmitter) {

_get(Object.getPrototypeOf(Route.prototype), "constructor", this).call(this);
this.setMaxListeners(0);
var _ = (0, _incognito2["default"])(this);
_._casts = [];

Object.defineProperties(this, {
"type": { value: type },
"path": { value: path },
"router": { value: router },
"_casts": { value: [] },
"callback": { value: null, writable: true }
});

Expand All @@ -64,13 +70,13 @@ var Route = (function (_EventEmitter) {
if (this.path.indexOf(":" + parameterName) < 0) {
throw new Error("Parameter " + parameterName + " not found in the route path.");
}
this._casts.push({ name: parameterName, type: parameterType });
(0, _incognito2["default"])(this)._casts.push({ name: parameterName, type: parameterType });
return this;
}
}, {
key: castCallback,
value: function value(request, response, next) {
this._casts.forEach(function (cast) {
(0, _incognito2["default"])(this)._casts.forEach(function (cast) {
if (request && request.params[cast.name]) {
var type = "string";
switch (cast.type) {
Expand Down
52 changes: 21 additions & 31 deletions es5/lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ var _routeJs = require("./route.js");

var _routeJs2 = _interopRequireDefault(_routeJs);

var _incognito = require("incognito");

var _incognito2 = _interopRequireDefault(_incognito);

var _responseJs = require("./response.js");

var _responseJs2 = _interopRequireDefault(_responseJs);
Expand All @@ -34,34 +38,21 @@ var Router = (function () {
function Router() {
_classCallCheck(this, Router);

var _ = (0, _incognito2["default"])(this);
_._express = (0, _express2["default"])();

for (var _len = arguments.length, routerOptions = Array(_len), _key = 0; _key < _len; _key++) {
routerOptions[_key] = arguments[_key];
}

Object.defineProperties(this, {
"_express": {
enumerable: false,
value: (0, _express2["default"])()
},
"_options": {
enumerable: false,
value: routerOptions
},
"_server": {
writable: true,
enumerable: false,
value: undefined
},
"_middlewares": {
enumerable: false,
value: []
}
});
_._options = routerOptions;
_._server = undefined;
_._middlewares = [];

this._express.disable("x-powered-by");
_._express.disable("x-powered-by");
//TYPE is not working by somehow, despites the website says it does
//https://github.com/expressjs/body-parser
this._express.use(_bodyParser2["default"].json({ type: "application/vnd.api+json" }));
_._express.use(_bodyParser2["default"].json({ type: "application/vnd.api+json" }));

this.initialize.apply(this, routerOptions);
}
Expand All @@ -74,12 +65,13 @@ var Router = (function () {
// Stubbed

value: function listen(portNumber, callback) {
this._server = this._express.listen(portNumber, callback);
var _ = (0, _incognito2["default"])(this);
_._server = _._express.listen(portNumber, callback);
}
}, {
key: "close",
value: function close(callback) {
this._server.close(callback);
(0, _incognito2["default"])(this)._server.close(callback);
}
}, {
key: _createRequest,
Expand All @@ -90,7 +82,7 @@ var Router = (function () {
key: _createResponse,
value: function value(expressResponse) {
//propagates the middleware to response formatters
return new _responseJs2["default"](expressResponse, this._middlewares);
return new _responseJs2["default"](expressResponse, (0, _incognito2["default"])(this)._middlewares);
}
}, {
key: _defineExpressRoute,
Expand All @@ -99,7 +91,7 @@ var Router = (function () {

var route = new _routeJs2["default"](method, path, this);
route.on("callback", function () {
_this._express[method](path, function (expressRequest, expressResponse) {
(0, _incognito2["default"])(_this)._express[method](path, function (expressRequest, expressResponse) {
return route.handle(_this[_createRequest](expressRequest), _this[_createResponse](expressResponse));
});
});
Expand Down Expand Up @@ -149,7 +141,7 @@ var Router = (function () {
}, {
key: "use",
value: function use(middlewareClass) {
this._middlewares.push(Object.create(middlewareClass.prototype));
(0, _incognito2["default"])(this)._middlewares.push(Object.create(middlewareClass.prototype));
}
}]);

Expand All @@ -162,11 +154,9 @@ var Request = (function () {
function Request(expressRequest) {
_classCallCheck(this, Request);

var _ = (0, _incognito2["default"])(this);
_._request = expressRequest;
Object.defineProperties(this, {
"_request": {
enumerable: false,
value: expressRequest
},
"body": {
enumerable: true,
value: expressRequest.body
Expand All @@ -185,7 +175,7 @@ var Request = (function () {
_createClass(Request, [{
key: "header",
value: function header(headerName) {
return this._request.get(headerName);
return (0, _incognito2["default"])(this)._request.get(headerName);
}
}]);

Expand Down
32 changes: 14 additions & 18 deletions es6/lib/response.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
import privateData from "incognito";

const loadDynamicMethods = Symbol();

export default class Response {
constructor(expressResponse, middlewares) {
Object.defineProperties(this, {
"_response": {
enumerable: false,
value: expressResponse
},
"_middlewares": {
enumerable: false,
value: middlewares
}
});
const _ = privateData(this);
_._response = expressResponse;
_._middlewares = middlewares;

this[loadDynamicMethods]();
}

[loadDynamicMethods]() {
const _ = privateData(this);
const statuses = require("../../http.statuses.json");
if(Array.isArray(statuses)) {
statuses.forEach((status) => {
this[status.name] = (data) => {
//call hook for data format middleware in a pipeline
this._middlewares.forEach((middleware) => {
_._middlewares.forEach((middleware) => {
if(middleware.formatResponse && typeof middleware.formatResponse === "function" ) {
middleware.formatResponse(this);
}
Expand All @@ -39,31 +35,31 @@ export default class Response {
}

end(message) {
this._response.end(message);
privateData(this)._response.end(message);
}

status(code) {
this._response.status(code);
privateData(this)._response.status(code);
return this;
}

json(data) {
this._response.json(data);
privateData(this)._response.json(data);
}

send(data) {
this._response.send(data);
privateData(this)._response.send(data);
}

download(data) {
this._response.download(data);
privateData(this)._response.download(data);
}

set(key, value) {
this._response.set(key, value);
privateData(this)._response.set(key, value);
}

get(key) {
return this._response.get(key);
return privateData(this)._response.get(key);
}
}
22 changes: 13 additions & 9 deletions es6/lib/route.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import EventEmitter from "events";
import flowsync from "flowsync";
import upcast from "upcast";
import privateData from "incognito";

const setupFilters = Symbol("setupFilters"),
setupDynamicProperties = Symbol("setupDynamicProperties"),
Expand All @@ -15,32 +16,35 @@ export default class Route extends EventEmitter {
constructor(type, path, router) {
super();
this.setMaxListeners(0);
const _ = privateData(this);
_._casts = [];

Object.defineProperties(this,
{
"type": {value: type},
"path": {value: path},
"router": {value: router},
"_casts": {value: []},
"callback": {value: null, writable: true}
});
}
);

this[setupDynamicProperties]();
this[setupFilters]();
this[setupDynamicProperties]();
this[setupFilters]();

this.filters();
this.filters();
}

cast(parameterName, parameterType) {
//TODO parameterName validation with path
if(this.path.indexOf(`:${parameterName}`) < 0) {
throw new Error(`Parameter ${parameterName} not found in the route path.`);
}
this._casts.push({name: parameterName, type: parameterType});
privateData(this)._casts.push({name: parameterName, type: parameterType});
return this;
}

[castCallback](request, response, next) {
this._casts.forEach((cast) => {
privateData(this)._casts.forEach((cast) => {
if(request && request.params[cast.name]) {
let type = "string";
switch(cast.type) {
Expand All @@ -59,12 +63,12 @@ export default class Route extends EventEmitter {
}

then(callback) {
this.callback = callback;
this.callback = callback;
this.emit("callback", this.handle);
}

handle(...options) {
return this.callback(...options);
return this.callback(...options);
}

/**
Expand Down
Loading

0 comments on commit 72f05c8

Please sign in to comment.