@peccul is peccu

(love peccu '(emacs lisp cat outdoor bicycle mac linux coffee))

JavaScriptでオブジェクトを対象にmapやreduceする

JavaScriptでオブジェクトを対象にmapやreduceしたかった。

Object.keys()でオブジェクトのキーが取り出せるので、それを元にmapやreduceする。

var o = {
  key1: 'value1',
  key2: 'value2',
  key3: 'value3'
};
// retuce
Object.keys(o).reduce(function (previous, key) {
  return previous + key + '=' + o[key] + '&';
}, '');
// => "key1=value1&key2=value2&key3=value3&"

// map
// element means `o`s key  (`keys(o)`s element)
Object.keys(o).map(function (element, index, array) {
  return element + '=' + o[element];
}).join('&');
// => "key1=value1&key2=value2&key3=value3"

参考

stackoverflow.com

nginxでレスポンスヘッダを追加する

リバースプロキシとしてnginxを動かしていて、どのnginxやlocationの設定が有効なのか区別したくてヘッダ情報を追加したかった。

この設定で X-Whom: www-node01とヘッダに追加される。

add_header X-Whom www-node01

www.cyberciti.biz

この記事ではcurl -Iを使っているけれど、個人的には curl -vの方が好き。

git://で始まるリポジトリURLをプロキシ環境下で使う

npmパッケージの依存するパッケージの中にはgit://で始まるURLが指定されていることがあってプロキシ環境下だとクローンできない。 git://github.com/hogehoge/fugafuga.git のような。

git://のみcloneに失敗とか、timeoutのエラー表示ならほぼプロキシを経由できていないからかと思う。

gitがプロキシを超えられるようにする

準備(git://でなくても必要)

環境変数の設定 HTTP_PROXY, http_proxy, HTTPS_PROXY, https_proxyそれぞれに http://proxy.example.com:8080のように設定する

続く手段1と2はどちらでも良い。1が簡単。

手段1

参考:A simple wrapper around socat to use as a git proxy command

gitプロトコルのアクセスをhttpsでのアクセスに読み替える

  • gitの設定ファイルを編集する ~/.gitconfigの内容(抜粋)
[url "https://"]
        insteadOf = git://

手段2

参考:How to use the git protocol through a HTTP CONNECT proxy - Thoughts on Systems socatを利用する

  • パッケージマネージャでsocatをインストールする
  • パスの通っている場所にスクリプトを置く 内容はこれ。ここでは ~/bin/gitproxy-socat という名前で保存したとする (プロキシのドメインとポートは適宜修正する)
#!/bin/sh
# Use socat to proxy git through an HTTP CONNECT firewall.
# Useful if you are trying to clone git:// from inside a company.
# Requires that the proxy allows CONNECT to port 9418.
#
# Save this file as gitproxy somewhere in your path (e.g., ~/bin) and then run
#   chmod +x gitproxy
#   git config --global core.gitproxy gitproxy
#
# More details at http://tinyurl.com/8xvpny

# Configuration. Common proxy ports are 3128, 8123, 8000.
_proxy=proxy.example.com
_proxyport=8080

exec socat STDIO PROXY:$_proxy:$1:$2,proxyport=$_proxyport
  • 実行権限をつける chmod +x ~/bin/gitproxy-socat
  • gitの設定ファイルを編集する ~/.gitconfigの内容(抜粋)
[core]
    gitproxy=gitproxy-socat