Category Archives: nodejs

WebSocket node.js ws 註解整理 – 1

這裡 註解 整理在Node.js中,WebSocket client/server常用的實現 ws 套件的程式碼 https://github.com/websockets/ws 關於WebSocket protocol,可參考另一篇文章的整理 index.js 所有的implementation在lib資料夾,大約3600行,算是一個輕量的實現,可先從index.js export的WebSocket開始追起,這也是一般js WebSocket client使用的class,WebSocket client API在實現上也是參考 browser使用的API,所以https://developer.mozilla.org/en-US/docs/Web/API/WebSocket 也要一併參考 以下逐行整理註解lib/websocket.js lib/websocket.js 上面可以看到支援的兩個version 8、13 8主要是對應https://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-08 開始的版本到 -12 ,13主要是對應 https://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-13 開始的版本,不過實際上支援的程度或相容性仍要看實作細節 繼承 EventEmitter,可參考 Nodejs Events整理的說明 constructor的部分: 接下來從constructor內的initAsClient往下追 以下設置http req,透過http.get function,可參考: https://nodejs.org/docs/latest-v12.x/api/http.html#http_http_request_options_callback 上面的error event … Continue reading

Posted in nodejs | Leave a comment

Nodejs Events 整理

Nodejs Events module是一個很重要的元件,主要原因是在asynchronous programming,Node.js在Javascript的語言層面提供了如callback、Promise、async/await等機制,這些機制都是類似於request-response的概念,也就是先發起一個function call,等待function call回應,可以對於notification類型,特別是event driver programming,就必須使用callback方式。在browser的DOM規範中,也有定義類似的介面EventTarget https://dom.spec.whatwg.org/#interface-eventtarget 主要就是定義註冊管理使用callback function的介面 event driven programming的特色就是將event透過handler/listener function來處理,並且event的時間點不確定,所以handler/listener callback function的角色是被動的,有什麼event過來就處理什麼event,event可以是新的資料,譬如說網路socket接收到的內容,也可能是狀態的改變,譬如說XMLHttpRequest的progress event。 在瀏覽器的javascript,因為UI有各種IO原始事件(滑鼠、鍵盤),進而在瀏覽器產生對應出衍生的UI事件。在Nodejs更多的IO操作也是透過event傳達狀態改變,例如’open’, ‘ready’等。或是如Readable stream的’data’ 在Node.js中,按照習慣,會發出event的object繼承於EventEmitter,透過.on(eventName, eventHandler/Listener) 來接收event,並且eventName建議是以camel case的方式命名,只是Nodejs大部分的event不需要用到第二個字,所以常見的都只是小寫event name 這裡節錄一段官方文件的範例,透過extends EventEmitter方式,使物件具有收發event的能力,emit透過 function argument,傳遞eventName之外的其他資訊。 .on呼叫的callback不應該是async function,並且callback被呼叫的順序是按照註冊的順序被 synchronously呼叫,另外也有一個.once 的API,主要用於handler/listener只一次性處理event,可參考另一篇文的說明 在Node.js中,錯誤處理有兩類,一種是js exception機制,synchronous function call內部透過throw,可以將錯誤傳遞出來,另外一種是callback中帶err argument,但這些都是function call流程很明確的時候。event … Continue reading

Posted in nodejs | 1 Comment

Node.js event .once 使用時機

Node.js EventEmitter裡註冊接收事件一般使用 .on(‘event name’, handler) 的方式,但有一個api是 .once,也就是註冊後觸發listener會自動remove,可參考 https://nodejs.org/docs/latest-v12.x/api/events.html#events_emitter_once_eventname_listener .once 在一般情境較少用到,原因是處理接收事件常常是跟著發送事件的物件生命週期(例如stream的’data’事件,.once只做一次性的handle,能使用到的情境比較像是需要接收到狀態變更,系統落入A狀態要等待切換到B狀態,因此在A狀態時註冊一個接收切換到B狀態的listener,一旦切換到B狀態就remove listener,等待下一次落入A狀態時再註冊 在Node.js doc介紹stream的地方有個不錯的範例 https://nodejs.org/docs/latest-v12.x/api/stream.html#stream_stream 節錄於下 可以注意 當writer.write 失敗時,就休息等待’drain’ event,再繼續write,也就是如上描述的 – 等待新的狀態變更。

Posted in nodejs | 1 Comment

js function test if it is called by new operator

在javascript function中,如果要強制使用new,可以參考以下nodejs cluster/worker.js的範例 他這邊利用檢查this是否是instanceof Worker,在一般情況下 https://www.w3schools.com/js/js_this.asp In a function, this refers to the global object. In a function, in strict mode, this is undefined. 但是在new operator下 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new The new keyword does the following things:1. Creates a blank, plain JavaScript object;2. Links (sets the constructor of) this object to … Continue reading

Posted in nodejs | Leave a comment

nodejs quick install

nodejs 在linux安裝如果不透過安裝包或是yum的包管理來安裝,最快的方式是直接從官方網站抓binary dist,以下是在CentOS 6.9測試 https://nodejs.org/dist/latest-v10.x/Linux: https://nodejs.org/dist/latest-v10.x/node-v10.17.0-linux-x64.tar.gz tar xf node-v10.17.0-linux-x64.tar.gz 將 bin/node copy到 /usr/local/bin cp node /usr/local/bin/ install npm 解壓縮的binary包有帶npm,用這個npm來抓取最新的npm安裝到系統 ./npm -g install npm 如果只是拿nodejs來執行的話,npm並不需要裝,只需要node執行檔。在壓縮包裏有 include,那是需要build binary addon時會用到,可直接複製到 /usr/local/include cp -r include/node/ /usr/local/include/

Posted in nodejs | Leave a comment

v8 build from source

注意python要2.7 否則在gm.py那一步會失敗 Linux 安裝build環境https://commondatastorage.googleapis.com/chrome-infra-docs/flat/depot_tools/docs/html/depot_tools_tutorial.html git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git export PATH=$PATH:~/depot_tools mkdir v8cd v8fetch v8 注意 在~/v8 下會有.gclient,所以工作目錄不要在home solutions = [ { “url”: “https://chromium.googlesource.com/v8/v8.git”, “managed”: False, “name”: “v8”, “deps_file”: “DEPS”, “custom_deps”: {}, }, ] gclient sync cd v8./build/install-build-deps.sh 下面這步是helper script,產生build files, compile, … Continue reading

Posted in nodejs | Leave a comment

detect elevated privilege execution in windows

要偵測是否是 UAC Privilege escalation,直接檢查是否Administrator並不可行,而是透過執行特定command來確認目前的執行是否是elevated privilege 這種作法在nodejs中不必在整合C API 參考: https://stackoverflow.com/questions/4051883/batch-script-how-to-check-for-admin-rights

Posted in nodejs | Leave a comment

js multiline string的幾種做法

Posted in nodejs | Leave a comment

javascript code hot swapping

最近看到erlang的動態更新patch https://stackoverflow.com/questions/1840717/achieving-code-swapping-in-erlangs-gen-server 事實上在nodejs也可以做到類似的作法 基本的思路是透過eval動態將輸入的程式碼字串置換 注意在eval裡面要返回expression value,在裡面的程式碼加上'()’ 上面的作法只是一個簡單的範例, 完整的設計可以考慮針對執行介面設計一個code置換處理的handler, 包含如何定位到要改動的變數(狀態)或是函式,透過外部統一的訊息介面將要更新的程式碼送到對應的handler來處理。 這邊主要說明的是當function被置換時,並不會影響到原來正在執行的function, 在javascript裡,function是first-class citizenship,而其生命週期就如同物件的生命週期,但有可能因為javascript engine的optimization而延長。

Posted in nodejs | Leave a comment

util.promise

在nodejs裡,大部分的io相關function是async,async透過callback返回處理結果, 因此在流程處理上容易因為連續多個async造成callback hell, 有些方式可以減緩這樣的問題:例如 async套件(https://www.npmjs.com/package/async), ES2015的promise ES2017的async await 都是處理這類的問題(async await在nodejs 7.6+支援)。 而promise和async await又能夠很好的配合使用。透過async await能夠大幅地消除callback hell, 並且很接近synchronous call的編寫流程。 但大多數的nodejs io function都是callback style的,無法直接被async await利用。 在nodejs8中有提供一個util function: util.promisify 能夠將callback style function轉成promise util.promisify使用時對於大部分callback style function FUNC(arg1, arg2, arg3…, callback) 不需特別注意,但少數callback參數不是在最後一個,則可以透過指定util.promisify.custom 告訴util.promisify要使用特定的promise 事實上可參考 setTimeout, setImmediate … Continue reading

Posted in nodejs | Leave a comment