Skip to content

Commit

Permalink
プロパティ更新処理を整理
Browse files Browse the repository at this point in the history
  • Loading branch information
ktty1220 committed Feb 7, 2017
1 parent 09e0a02 commit d2a9085
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 15 deletions.
6 changes: 3 additions & 3 deletions lib/cheerio/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ module.exports.colorMessage = function (type, msg) {
var typeConf = {
default: {
color: 'white',
shift: 2
shift: 1
},
DEPRECATED: {
color: 'yellow',
shift: 2
shift: 3
},
WARNING: {
color: 'magenta',
shift: 3
shift: 2
}
};

Expand Down
47 changes: 35 additions & 12 deletions lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,30 @@ var client = require('./client');
var pkg = require('../package.json');
var browsers = require('./browsers.json');

var propSetKey = Math.random().toString(36).substr(2);
/**
* cheerio-httpcli内部からプロパティを直接更新する際の黒魔術
*/
var propertyUpdater = {
// 内部的にプロパティを直接更新する際の照合キー
Key: Math.random().toString(36).substr(2),

// プロパティ更新時の値を黒魔術で包み込む
wrap: function (value) {
return [ this.key, value ];
},

// プロパティ更新時の値を黒魔術から取り出す
unwrap: function (value) {
if ((value instanceof Array) &&
value.length === 2 && value[0] === this.key) {
return value[1];
}

util.colorMessage('DEPRECATED', 'direct property update will be refused in the future. use set(key, value)');
//throw new Error(direct property update is not permitted. use set(key, value)');
return value;
}
};

// リクエストヘッダを作り直す
var rebuildHeaders = function (value) {
Expand Down Expand Up @@ -71,14 +94,7 @@ var defineNormalProperties = function (cli) {
return property[prop].value;
},
set: function (value) {
// cheerio-httpcli内部から更新する場合の黒魔術
if ((value instanceof Array) &&
value.length === 2 && value[0] === propSetKey) {
value = value[1];
} else {
util.colorMessage('DEPRECATED', 'direct property update will be refused in the future. use set(key, value)');
//throw new Error('direct property update is not permitted. use set(key, value)');
}
value = propertyUpdater.unwrap(value);

// 型チェック
var types = property[prop].types;
Expand Down Expand Up @@ -106,6 +122,7 @@ var defineNormalProperties = function (cli) {
var defineSpecialProperties = function (cli) {
// browserプロパティ
Object.defineProperty(cli, 'browser', {
enumerable: true,
get: function () {
// User-Agentとブラウザテンプレが一致するものを探す
var ua = this.headers['user-agent'];
Expand All @@ -121,6 +138,8 @@ var defineSpecialProperties = function (cli) {
return 'custom';
},
set: function (type) {
type = propertyUpdater.unwrap(type);

if (type in browsers) {
this.set('headers', {
'User-Agent': browsers[type]
Expand All @@ -133,10 +152,13 @@ var defineSpecialProperties = function (cli) {

// iconvプロパティ
Object.defineProperty(cli, 'iconv', {
enumerable: true,
get: function () {
return encoding.getIconvType();
},
set: function (icmod) {
icmod = propertyUpdater.unwrap(icmod);

if (! encoding.iconvLoad(icmod)) {
throw new Error('Cannot find module "' + icmod + '"');
}
Expand All @@ -145,6 +167,7 @@ var defineSpecialProperties = function (cli) {

// バージョン情報プロパティ
Object.defineProperty(cli, 'version', {
enumerable: true,
get: function () {
return pkg.version;
}
Expand Down Expand Up @@ -196,7 +219,7 @@ var CheerioHttpCli = (function () {
CheerioHttpCli.prototype.set = function (name, value, nomerge) {
// 特殊プロパティ
if ([ 'browser', 'iconv' ].indexOf(name) !== -1) {
this[name] = value;
this[name] = propertyUpdater.wrap(value);
return this;
}

Expand All @@ -209,9 +232,9 @@ var CheerioHttpCli = (function () {

// オブジェクトへの代入ならマージする(黒魔術使用)
if (! nomerge && typeOf(this[name]) === 'object' && typeOf(value) === 'object') {
this[name] = [ propSetKey, assign(this[name], value) ];
this[name] = propertyUpdater.wrap(assign(this[name], value));
} else {
this[name] = [ propSetKey, value ];
this[name] = propertyUpdater.wrap(value);
}
return this;
};
Expand Down

0 comments on commit d2a9085

Please sign in to comment.