appolo-agent
Advanced tools
Comparing version
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const tslib_1 = require("tslib"); | ||
const appolo_route_1 = require("appolo-route"); | ||
@@ -121,21 +120,17 @@ const request_1 = require("./request"); | ||
} | ||
close() { | ||
return tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
try { | ||
yield Q.fromCallback(c => this._server.close(c)); | ||
async close() { | ||
try { | ||
await Q.fromCallback(c => this._server.close(c)); | ||
} | ||
catch (e) { | ||
if (e.message !== "Not running" && e.code !== "ERR_SERVER_NOT_RUNNING") { | ||
throw e; | ||
} | ||
catch (e) { | ||
if (e.message !== "Not running" && e.code !== "ERR_SERVER_NOT_RUNNING") { | ||
throw e; | ||
} | ||
} | ||
}); | ||
} | ||
} | ||
listen(port, cb) { | ||
return tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
this._initialize(); | ||
yield Q.fromCallback(c => this._server.listen(port, c)); | ||
(cb) && cb(this); | ||
return this; | ||
}); | ||
async listen(port, cb) { | ||
this._initialize(); | ||
await Q.fromCallback(c => this._server.listen(port, c)); | ||
(cb) && cb(this); | ||
return this; | ||
} | ||
@@ -142,0 +137,0 @@ } |
117
lib/view.js
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const tslib_1 = require("tslib"); | ||
const appolo_cache_1 = require("appolo-cache"); | ||
@@ -14,73 +13,65 @@ const httpError_1 = require("./errors/httpError"); | ||
} | ||
render(paths, params, res) { | ||
return tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
try { | ||
params = params || {}; | ||
let item = null; | ||
if (res) { | ||
item = this._cache.peek(`${res.req.url}${res.req.method}`); | ||
} | ||
if (!item) { | ||
item = yield this._findPath(paths); | ||
} | ||
if (res) { | ||
this._cache.set(`${res.req.url}${res.req.method}`, item); | ||
} | ||
let result = yield this._options.viewEngine(item.path, Object.assign({ cache: true }, params)); | ||
return result; | ||
async render(paths, params, res) { | ||
try { | ||
params = params || {}; | ||
let item = null; | ||
if (res) { | ||
item = this._cache.peek(`${res.req.url}${res.req.method}`); | ||
} | ||
catch (e) { | ||
throw new httpError_1.HttpError(500, `failed to render view ${e.toString()}`); | ||
if (!item) { | ||
item = await this._findPath(paths); | ||
} | ||
}); | ||
} | ||
_findPath(paths) { | ||
return tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
let lookPaths = []; | ||
for (let i = 0, len = paths.length; i < len; i++) { | ||
let p = paths[i]; | ||
let ext = path.extname(p); | ||
if (!ext) { | ||
p += `.${this._options.viewExt || "html"}`; | ||
} | ||
lookPaths.push(path.resolve(process.cwd(), p)); | ||
lookPaths.push(path.resolve(process.cwd(), this._options.viewFolder, p)); | ||
if (res) { | ||
this._cache.set(`${res.req.url}${res.req.method}`, item); | ||
} | ||
let foundPath = yield this._lookup(lookPaths.slice()); | ||
if (!foundPath) { | ||
throw new httpError_1.HttpError(500, `failed to find view path searched paths ${JSON.stringify(lookPaths)}`); | ||
} | ||
if (!this._options.viewEngine) { | ||
throw new httpError_1.HttpError(500, `tried to call render but view engine is no defined`); | ||
} | ||
let item = { path: foundPath }; | ||
return item; | ||
}); | ||
let result = await this._options.viewEngine(item.path, Object.assign({ cache: true }, params)); | ||
return result; | ||
} | ||
catch (e) { | ||
throw new httpError_1.HttpError(500, `failed to render view ${e.toString()}`); | ||
} | ||
} | ||
_lookup(paths) { | ||
return tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
let path = paths.shift(); | ||
if (!path) { | ||
return null; | ||
async _findPath(paths) { | ||
let lookPaths = []; | ||
for (let i = 0, len = paths.length; i < len; i++) { | ||
let p = paths[i]; | ||
let ext = path.extname(p); | ||
if (!ext) { | ||
p += `.${this._options.viewExt || "html"}`; | ||
} | ||
let isExist = yield this._isFileExist(path); | ||
if (isExist) { | ||
return path; | ||
} | ||
return this._lookup(paths); | ||
}); | ||
lookPaths.push(path.resolve(process.cwd(), p)); | ||
lookPaths.push(path.resolve(process.cwd(), this._options.viewFolder, p)); | ||
} | ||
let foundPath = await this._lookup(lookPaths.slice()); | ||
if (!foundPath) { | ||
throw new httpError_1.HttpError(500, `failed to find view path searched paths ${JSON.stringify(lookPaths)}`); | ||
} | ||
if (!this._options.viewEngine) { | ||
throw new httpError_1.HttpError(500, `tried to call render but view engine is no defined`); | ||
} | ||
let item = { path: foundPath }; | ||
return item; | ||
} | ||
_isFileExist(path) { | ||
return tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
try { | ||
let result = yield Q.fromCallback(c => fs.stat(path, c)); | ||
return result.isFile(); | ||
} | ||
catch (e) { | ||
return false; | ||
} | ||
}); | ||
async _lookup(paths) { | ||
let path = paths.shift(); | ||
if (!path) { | ||
return null; | ||
} | ||
let isExist = await this._isFileExist(path); | ||
if (isExist) { | ||
return path; | ||
} | ||
return this._lookup(paths); | ||
} | ||
async _isFileExist(path) { | ||
try { | ||
let result = await Q.fromCallback(c => fs.stat(path, c)); | ||
return result.isFile(); | ||
} | ||
catch (e) { | ||
return false; | ||
} | ||
} | ||
} | ||
exports.View = View; | ||
//# sourceMappingURL=view.js.map |
@@ -100,3 +100,3 @@ import {Cache} from "appolo-cache"; | ||
try { | ||
let result: fs.Stats = await Q.fromCallback(c => fs.stat(path, c)); | ||
let result: fs.Stats = await Q.fromCallback< fs.Stats>(c => fs.stat(path, c)); | ||
return result.isFile(); | ||
@@ -103,0 +103,0 @@ } catch (e) { |
@@ -27,3 +27,3 @@ { | ||
"main": "./index.js", | ||
"version": "6.0.4", | ||
"version": "6.0.5", | ||
"license": "MIT", | ||
@@ -48,9 +48,9 @@ "repository": { | ||
"@types/benchmark": "^1.0.31", | ||
"@types/bluebird": "^3.5.20", | ||
"@types/bluebird": "^3.5.23", | ||
"@types/chai": "^4.1.3", | ||
"@types/chai-http": "^3.0.4", | ||
"@types/cookie": "^0.3.1", | ||
"@types/lodash": "^4.14.107", | ||
"@types/mocha": "^5.2.0", | ||
"@types/node": "^9.6.6", | ||
"@types/lodash": "^4.14.113", | ||
"@types/mocha": "^5.2.5", | ||
"@types/node": "^10.5.3", | ||
"@types/qs": "^6.5.1", | ||
@@ -72,4 +72,4 @@ "@types/supertest": "^2.0.4", | ||
"supertest": "^3.0.0", | ||
"typescript": "^2.8.3" | ||
"typescript": "^2.9.2" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
68031
-1.13%1268
-1.09%