Comparing version 3.0.0-beta3 to 3.0.0-beta4
@@ -191,4 +191,3 @@ const path = require('path'); | ||
if (argv.path) { | ||
think.isCli = true; | ||
instance.loadAll('worker'); | ||
instance.loadAll('worker', true); | ||
return this.runInCli(argv.path); | ||
@@ -195,0 +194,0 @@ } else if (cluster.isMaster) { |
@@ -10,2 +10,6 @@ const helper = require('think-helper'); | ||
const COOKIE_STORE = Symbol('cookie-store'); | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
const onFinished = require('on-finished'); | ||
const destroy = require('destroy'); | ||
@@ -23,2 +27,20 @@ /** | ||
/** | ||
* is get request | ||
*/ | ||
get isGet() { | ||
return this.method === 'GET'; | ||
}, | ||
/** | ||
* is post request | ||
*/ | ||
get isPost() { | ||
return this.method === 'POST'; | ||
}, | ||
/** | ||
* is command line invoke | ||
*/ | ||
get isCli() { | ||
return this.method === 'CLI'; | ||
}, | ||
/** | ||
* get referer header | ||
@@ -38,14 +60,2 @@ */ | ||
/** | ||
* is get request | ||
*/ | ||
get isGet() { | ||
return this.method === 'GET'; | ||
}, | ||
/** | ||
* is post request | ||
*/ | ||
get isPost() { | ||
return this.method === 'POST'; | ||
}, | ||
/** | ||
* is method | ||
@@ -57,8 +67,2 @@ */ | ||
/** | ||
* is command line invoke | ||
*/ | ||
get isCli() { | ||
return think.isCli; | ||
}, | ||
/** | ||
* is ajax request | ||
@@ -261,4 +265,5 @@ */ | ||
delete this[COOKIE_STORE][name]; | ||
options.maxAge = -1; | ||
return instance.set(name, '', options); | ||
// If the value is omitted, an outbound header with an expired date is used to delete the cookie. | ||
// https://github.com/pillarjs/cookies#cookiesset-name--value---options-- | ||
return instance.set(name, undefined, options); | ||
} | ||
@@ -275,17 +280,30 @@ assert(helper.isString(value), 'cookie value must be a string'); | ||
/** | ||
* get controller instance | ||
* get service | ||
* @param {String} name | ||
* @param {String} m | ||
*/ | ||
controller(name, m) { | ||
return think.controller(name, this, m); | ||
service(...args) { | ||
return think.service(...args); | ||
}, | ||
/** | ||
* get service | ||
* @param {String} name | ||
* @param {String} m | ||
* download | ||
* @param {String} filepath | ||
* @param {String} filename | ||
*/ | ||
service(name, m) { | ||
return think.service(name, m); | ||
download(filepath, filename = path.basename(filepath)) { | ||
assert(filepath, 'filepath can not be empty'); | ||
const contentType = this.response.get('Content-Type'); | ||
if (!contentType) { | ||
this.type = path.extname(filename); | ||
} | ||
const contentDisposition = this.response.get('Content-Disposition'); | ||
if (!contentDisposition) { | ||
this.attachment(filename); | ||
} | ||
const stream = fs.createReadStream(filepath); | ||
this.body = stream; | ||
onFinished(this.res, () => { | ||
destroy(stream); | ||
}); | ||
} | ||
}; |
const helper = require('think-helper'); | ||
const debug = require('debug')('thinkjs'); | ||
const assert = require('assert'); | ||
/** | ||
@@ -56,9 +57,6 @@ * extend controller | ||
/** | ||
* get or set config | ||
* @param {String} name | ||
* @param {Mix} value | ||
* @param {String} m | ||
* get userAgent header | ||
*/ | ||
config(name, value, m = this.ctx.module) { | ||
return think.config(name, value, m); | ||
get userAgent() { | ||
return this.ctx.userAgent; | ||
}, | ||
@@ -72,9 +70,2 @@ /** | ||
/** | ||
* is method | ||
* @param {String} method | ||
*/ | ||
isMethod(method) { | ||
return this.ctx.isMethod(method); | ||
}, | ||
/** | ||
* is get method | ||
@@ -95,5 +86,21 @@ */ | ||
get isCli() { | ||
return think.isCli; | ||
return this.ctx.isCli; | ||
}, | ||
/** | ||
* get or set config | ||
* @param {String} name | ||
* @param {Mix} value | ||
* @param {String} m | ||
*/ | ||
config(name, value, m = this.ctx.module) { | ||
return think.config(name, value, m); | ||
}, | ||
/** | ||
* is method | ||
* @param {String} method | ||
*/ | ||
isMethod(method) { | ||
return this.ctx.isMethod(method); | ||
}, | ||
/** | ||
* check if is ajax request | ||
@@ -205,8 +212,2 @@ * @param {String} method | ||
/** | ||
* get userAgent header | ||
*/ | ||
get userAgent() { | ||
return this.ctx.userAgent; | ||
}, | ||
/** | ||
* get referer header | ||
@@ -233,7 +234,13 @@ */ | ||
* get controller instance | ||
* @param {String} name | ||
* @param {String} m | ||
* @param {String} name | ||
* @param {String} m | ||
*/ | ||
controller(name, m) { | ||
return think.controller(name, this.ctx, m); | ||
controller(name, m = this.ctx.module) { | ||
let mcls = think.app.controllers; | ||
if (this.ctx.app.modules.length) { | ||
mcls = think.app.controllers[m || 'common'] || {}; | ||
} | ||
const Cls = mcls[name]; | ||
assert(Cls, `can not find controller: ${name}`); | ||
return new Cls(this.ctx); | ||
}, | ||
@@ -245,4 +252,4 @@ /** | ||
*/ | ||
service(name, m) { | ||
return think.service(name, m); | ||
service(...args) { | ||
return think.service(...args); | ||
}, | ||
@@ -256,3 +263,7 @@ /** | ||
action(controller, actionName, m) { | ||
const instance = this.controller(controller, m); | ||
let instance = controller; | ||
// if controller is an controller instance, ignore invoke controller method | ||
if (helper.isString(controller)) { | ||
instance = this.controller(controller, m); | ||
} | ||
let promise = Promise.resolve(); | ||
@@ -268,13 +279,17 @@ if (instance.__before) { | ||
} | ||
if (instance[method]) { | ||
return instance[method](); | ||
} | ||
if (instance[method]) return instance[method](); | ||
}).then(data => { | ||
if (data === false) return false; | ||
if (instance.__after) { | ||
return instance.__after(); | ||
} | ||
if (instance.__after) return instance.__after(); | ||
return data; | ||
}); | ||
}, | ||
/** | ||
* download | ||
* @param {String} filepath | ||
* @param {String} filename | ||
*/ | ||
download(filepath, filename) { | ||
return this.ctx.download(filepath, filename); | ||
} | ||
}; |
@@ -7,2 +7,3 @@ const getConfigFn = require('think-config').getConfigFn; | ||
const Crontab = require('think-crontab'); | ||
const fs = require('fs'); | ||
@@ -70,3 +71,4 @@ require('./think.js'); | ||
['controller', think.Controller.prototype], | ||
['logic', think.Logic.prototype] | ||
['logic', think.Logic.prototype], | ||
['service', think.Service.prototype] | ||
]; | ||
@@ -89,6 +91,13 @@ list.forEach(item => { | ||
*/ | ||
loadAll(type) { | ||
loadAll(type, isCli) { | ||
this.initPath(); | ||
think.loader = new Loader(think.APP_PATH, thinkPath); | ||
think.config = getConfigFn(think.loader.loadConfig(think.app.env), think.loader.modules.length > 0); | ||
// write config to APP_PATH/runtime/config/[env].json file | ||
const config = think.loader.loadConfig(think.app.env); | ||
const configFilepath = path.join(think.ROOT_PATH, `runtime/config`); | ||
helper.mkdir(configFilepath); | ||
fs.writeFileSync(`${configFilepath}/${think.app.env}.json`, JSON.stringify(config, undefined, 2)); | ||
think.config = getConfigFn(config, think.loader.modules.length > 0); | ||
think.logger = new Logger(helper.parseAdapterConfig(think.config('logger')), true); | ||
@@ -100,3 +109,3 @@ | ||
this.loadMiddleware(); | ||
if (!think.isCli) { | ||
if (!isCli) { | ||
this.loadCrontab(); | ||
@@ -103,0 +112,0 @@ } |
@@ -26,11 +26,11 @@ const Koa = require('koa'); | ||
/** | ||
* think env | ||
* think.env | ||
*/ | ||
think.__defineGetter__('env', () => think.app.env); | ||
Object.defineProperty(think, 'env', { | ||
get value() { | ||
return think.app.env; | ||
} | ||
}); | ||
/** | ||
* is cli mode | ||
*/ | ||
think.isCli = false; | ||
/** | ||
* add think to think.app | ||
@@ -65,28 +65,6 @@ */ | ||
const getClass = function(type, name, m) { | ||
const mcls = think.app[type]; | ||
let cls = null; | ||
if (think.app.modules.length) { | ||
if (mcls[m]) { | ||
cls = mcls[m][name]; | ||
} | ||
if (!cls && m !== 'common' && mcls.common) { | ||
cls = mcls.common[name]; | ||
} | ||
} else { | ||
cls = mcls[name]; | ||
} | ||
return cls; | ||
}; | ||
/** | ||
* get controller instance | ||
* @param {String} name | ||
* @param {Object} ctx | ||
* @param {String} m | ||
* service base class | ||
*/ | ||
think.controller = (name, ctx, m = 'common') => { | ||
const Cls = getClass('controllers', name, m); | ||
assert(Cls, `can not find controller:${name}`); | ||
return new Cls(ctx); | ||
}; | ||
think.Service = class Service {}; | ||
@@ -96,4 +74,13 @@ /** | ||
*/ | ||
think.service = (name, m) => { | ||
return getClass('services', name, m); | ||
think.service = (name, m, ...args) => { | ||
let mcls = think.app.services; | ||
if (think.app.modules.length) { | ||
mcls = think.app.services[m || 'common'] || {}; | ||
} else { | ||
args.unshift(m); | ||
} | ||
const Cls = mcls[name]; | ||
assert(Cls, `can not find service: ${name}`); | ||
if (helper.isFunction(Cls)) return new Cls(...args); | ||
return Cls; | ||
}; | ||
@@ -100,0 +87,0 @@ |
{ | ||
"name": "thinkjs", | ||
"description": "ThinkJS - Use full ES6/7 features to develop web applications, Support TypeScript", | ||
"version": "3.0.0-beta3", | ||
"version": "3.0.0-beta4", | ||
"author": { | ||
@@ -48,3 +48,5 @@ "name": "welefen", | ||
"debug": "^2.6.1", | ||
"destroy": "^1.0.4", | ||
"koa": "^2.2.0", | ||
"on-finished": "^2.3.0", | ||
"think-cache": "^1.0.0", | ||
@@ -51,0 +53,0 @@ "think-cluster": "^1.0.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
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
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
212109
30
1107
24
2
+ Addeddestroy@^1.0.4
+ Addedon-finished@^2.3.0
+ Addedgopd@1.0.1(transitive)
+ Addedhas-proto@1.0.3(transitive)
- Removedgopd@1.1.0(transitive)
- Removedhas-proto@1.1.0(transitive)