﻿
{"id":8638,"date":"2018-07-23T08:36:40","date_gmt":"2018-07-23T08:36:40","guid":{"rendered":"http:\/\/www.gencayyildiz.com\/blog\/?p=8638"},"modified":"2018-07-23T08:36:40","modified_gmt":"2018-07-23T08:36:40","slug":"node-js-stream-nedir-nasil-olusturulur","status":"publish","type":"post","link":"https:\/\/www.gencayyildiz.com\/blog\/node-js-stream-nedir-nasil-olusturulur\/","title":{"rendered":"Node.js &#8211; Stream Nedir? Nas\u0131l Olu\u015fturulur?"},"content":{"rendered":"<div id=\"fb-root\"><\/div>\n<p>Merhaba,<\/p>\n<p>Bu i\u00e7eri\u011fimizde, verilerin b\u00fct\u00fcn olarak de\u011fil par\u00e7a par\u00e7a transfer edilmesini sa\u011flayan Stream yap\u0131lar\u0131 \u00fczerine konu\u015fuyor olaca\u011f\u0131z.<\/p>\n<h3>Stream Nedir?<\/h3>\n<p>Stream, yukar\u0131da da de\u011finildi\u011fi gibi b\u00fcy\u00fck boyutlu bir veriyi bir yerden ba\u015fka bir yere transfer ederken b\u00fct\u00fcn olarak de\u011fil de par\u00e7a par\u00e7a transfer edilmesini sa\u011flayan yay\u0131n ya da ak\u0131\u015f \u015feklinde tarif edilebilen bir kavramd\u0131r. Bu yap\u0131n\u0131n i\u015flevine en g\u00fczel \u00f6rne\u011fi youtube olarak verebiliriz. \u0130lgili platformda izlenen herhangi bir videonun tam olarak y\u00fcklenmesine gerek duyulmaks\u0131z\u0131n par\u00e7a par\u00e7a transfer sa\u011flanmakta ve onlarca boyuttaki video bu \u015fekilde bekleme olmaks\u0131z\u0131n rahat\u00e7a izlenmektedir. Bizler izledik\u00e7e videonun ak\u0131\u015f\u0131 devam etmekte ve geri kalan verileride zamanla bilgisayar\u0131m\u0131za indirilmektedir.<\/p>\n<p>Streamleri yap\u0131sal olarak bu \u015fekilde tarif edebiliriz lakin Node.js&#8217;e \u00f6zel baz\u0131 fakt\u00f6rleride yap\u0131lar\u0131nda bar\u0131nd\u0131rmaktad\u0131rlar. Node.js&#8217;de Streamlar, aslen birer <a href=\"http:\/\/www.gencayyildiz.com\/blog\/node-js-eventemitter-sinifi\/\" rel=\"noopener\" target=\"_blank\">event emitter<\/a>&#8216;d\u0131r. Dolay\u0131s\u0131yla, ak\u0131\u015f an\u0131nda her bir par\u00e7a sunuldu\u011fu anda bundan bizim haberimiz olmaktad\u0131r.<\/p>\n<p>Teknik olarak ak\u0131\u015ftaki her bir par\u00e7a bir Buffer nesnesidir ve &#8220;Chunk&#8221; s\u0131fat\u0131yla temsil edilmektedir. Her chunk ak\u0131\u015f\u0131 sa\u011fland\u0131\u011f\u0131nda &#8220;data&#8221; isimli event tetiklenmektedir. T\u00fcm veri transferi sa\u011fland\u0131\u011f\u0131nda ise &#8220;end&#8221; event\u0131 tetiklenecektir.<\/p>\n<p>Bu tan\u0131mlamalardan sonra s\u0131ra Streamlerin nas\u0131l olu\u015fturuldu\u011funu incelemeye geldi.<\/p>\n<p>Streamlar y\u00f6nleri bak\u0131m\u0131ndan &#8220;Readable(Okunabilir)&#8221;, &#8220;Writeable(Yaz\u0131labilir)&#8221; ve &#8220;Readable\/Writeable(Hem Okunabilir, Hem Yaz\u0131labilir) olmak \u00fczere \u00fc\u00e7 farkl\u0131 kategoride de\u011ferlendirilmektedir.<\/p>\n<h3>Okunabilir Stream Olu\u015fturmak<\/h3>\n<p>&#8220;Readable(Okunabilir)&#8221; stream yap\u0131s\u0131 bir veriyi par\u00e7a par\u00e7a okumada kulland\u0131\u011f\u0131m\u0131z bir yakla\u015f\u0131md\u0131r.<br \/>\n\u00d6rnek olarak a\u015fa\u011f\u0131daki kod blo\u011funu inceleyebilirsiniz.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n\/\/Dosyaya ula\u015fmak i\u00e7in fs k\u00fct\u00fcphanesini getiriyoruz.\r\nconst fs = require(&quot;fs&quot;);\r\n\/\/Geriye EventEmitter d\u00f6nd\u00fcrecek createReadStream fonksiyonu ile dosyay\u0131\r\n\/\/Stream olarak okuyoruz.\r\nconst readStream = fs.createReadStream(&quot;video.avi&quot;);\r\n\r\n\/\/\u015eimdi her data geldi\u011finde data gelip gelmedi\u011fini bilmemiz gerekiyor.\r\n\/\/Data geliyorsa e\u011fer EventEmitter &quot;data&quot; isimli olay\u0131 tetikleyecektir.\r\n\/\/Dolay\u0131s\u0131yla ilgili fonksiyonu olu\u015fturuyorum.\r\nreadStream.on(&quot;data&quot;, (chunk) =&gt; {\r\n    console.log(`${chunk.length} : Veri geldi.`);\r\n});\r\n\r\n\/\/E\u011fer ak\u0131\u015f sona erdiyse EventEmitter nesnemiz &quot;end&quot; isimli olay\u0131 tetikleyecektir.\r\n\/\/Yine fonksiyonumu tan\u0131ml\u0131yorum.\r\nreadStream.on(&quot;end&quot;, () =&gt; {\r\n    console.log(&quot;Veri ak\u0131\u015f\u0131 son buldu.&quot;);\r\n});\r\n<\/pre>\n<p>G\u00f6r\u00fcld\u00fc\u011f\u00fc \u00fczere \u00f6rne\u011fimizde kodlar aras\u0131nda gerekli a\u00e7\u0131klamalar\u0131 yapt\u0131\u011f\u0131m i\u00e7in ek bir a\u00e7\u0131klamada bulunmayaca\u011f\u0131m lakin veriyi stream ile nas\u0131l okundu\u011funu incelemi\u015f bulunmaktay\u0131z. Bu \u00e7al\u0131\u015fma neticesinde a\u015fa\u011f\u0131daki sonucu almaktay\u0131z;<br \/>\n<a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/07\/Node.js-Stream-Nedir-Nas\u0131l-Olu\u015fturulur..png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/07\/Node.js-Stream-Nedir-Nas\u0131l-Olu\u015fturulur..png\" alt=\"Node.js - Stream Nedir? Nas\u0131l Olu\u015fturulur?\" width=\"191\" height=\"254\" class=\"aligncenter size-full wp-image-8651\" \/><\/a><\/p>\n<p>Okunabilir streamlerde okunan verinin y\u00fczdelik durumunu g\u00f6rebilmek i\u00e7in a\u015fa\u011f\u0131daki gibi bir \u00e7al\u0131\u015fma yapabiliriz. Tabi burada yukar\u0131daki \u00f6rnekten farkl\u0131 bir \u00e7al\u0131\u015fma ve dosya \u00fczerinden \u00f6rneklendirme yap\u0131yorum.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nconst file = require(&quot;fs&quot;);\r\n\r\nconst stream = file.createReadStream(&quot;dosya.rar&quot;);\r\nfile.stat(&quot;dosya.rar&quot;, (error, data) =&gt; {\r\n    if (error)\r\n        throw error;\r\n\r\n    const fileSize = data.size;\r\n    let chunkSize = 0, oldSize = 0, lastSize = 0;\r\n    stream.on(&quot;data&quot;, chunk =&gt; {\r\n        chunkSize += chunk.length;\r\n        lastSize = Math.round(chunkSize * 100 \/ fileSize);\r\n        if (oldSize != lastSize)\r\n            console.log(`${lastSize}%`);\r\n        oldSize = lastSize;\r\n    });\r\n});\r\n<\/pre>\n<p><a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/07\/Node.js-Stream-Nedir-Nas\u0131l-Olu\u015fturulur.gif\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/07\/Node.js-Stream-Nedir-Nas\u0131l-Olu\u015fturulur.gif\" alt=\"Node.js \u2013 Stream Nedir? Nas\u0131l Olu\u015fturulur?\" width=\"600\" height=\"791\" class=\"aligncenter size-full wp-image-8654\" \/><\/a><\/p>\n<h3>Yaz\u0131labilir Stream Olu\u015fturmak<\/h3>\n<p>Yaz\u0131labilir streamlerede \u00f6rnek olarak; okunabilir stremden elde etti\u011fimiz verileri al\u0131p yaz\u0131labilir stream arac\u0131l\u0131\u011f\u0131yla ba\u015fka bir noktaya transfer etmek i\u00e7in kullanaca\u011f\u0131m.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nconst fs = require(&quot;fs&quot;);\r\nconst readStream = fs.createReadStream(&quot;video.avi&quot;);\r\nconst writeStream = fs.createWriteStream(&quot;video2.avi&quot;);\r\nreadStream.on(&quot;data&quot;, (chunk) =&gt; {\r\n    console.log(`${chunk.length} : Veri geldi.`);\r\n});\r\n\/\/readStream'a writeStream'\u0131 pipe olarak vermekteyim.\r\nreadStream.pipe(writeStream);\r\n\/\/Yaz\u0131m i\u015flemi bitti\u011fi vakit &quot;finish&quot; olay\u0131 tetiklenecektir.\r\nwriteStream.on(&quot;finish&quot;, () =&gt; {\r\n    console.log(&quot;Yaz\u0131m i\u015flemi son buldu.&quot;);\r\n})\r\nreadStream.on(&quot;end&quot;, () =&gt; {\r\n    console.log(&quot;Veri ak\u0131\u015f\u0131 son buldu.&quot;);\r\n});\r\n<\/pre>\n<p>Yukar\u0131daki kod blo\u011funu incelerseniz e\u011fer okunabilir stream&#8217;dan(readStream) gelen verileri &#8220;pipe&#8221; fonksiyonu arac\u0131l\u0131\u011f\u0131yla yaz\u0131labilir stream&#8217;e(writeStream) atam\u0131\u015f bulunmaktay\u0131m. Veri her okundu\u011funda ilgili pipe tetiklenecek ve yaz\u0131labilir stream arac\u0131l\u0131\u011f\u0131yla belirtilen dosyaya transfer edilecektir. Ayr\u0131ca yaz\u0131labilir stream&#8217;ler de komple i\u015flemin bitti\u011fini &#8220;finish&#8221; event\u0131 ile \u00f6\u011frenebilmekteyiz.<\/p>\n<p><a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/07\/Node.js-Stream-Nedir-Nas\u0131l-Olu\u015fturulur-1.gif\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/07\/Node.js-Stream-Nedir-Nas\u0131l-Olu\u015fturulur-1.gif\" alt=\"Node.js - Stream Nedir? Nas\u0131l Olu\u015fturulur?\" width=\"600\" height=\"309\" class=\"aligncenter size-full wp-image-8661\" \/><\/a><\/p>\n<p>\u0130\u00e7eri\u011fimizi zenginle\u015ftirmek ad\u0131na yaz\u0131labilir streamlerde de y\u00fczdelik durumu g\u00f6sterecek algoritmay\u0131 olu\u015fturmak istersek e\u011fer;<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nconst file = require(&quot;fs&quot;);\r\n\r\nconst stream = file.createReadStream(&quot;dosya.rar&quot;);\r\nconst writeStream = file.createWriteStream(&quot;dosya2.rar&quot;);\r\nfile.stat(&quot;dosya.rar&quot;, (error, data) =&gt; {\r\n    if (error)\r\n        throw error;\r\n\r\n    const fileSize = data.size;\r\n    let chunkSize = 0, oldSize = 0, lastSize = 0;\r\n    stream.on(&quot;data&quot;, chunk =&gt; {\r\n        chunkSize += chunk.length;\r\n        lastSize = Math.round(chunkSize * 100 \/ fileSize);\r\n        if (oldSize != lastSize)\r\n            console.log(`${lastSize}%`);\r\n        oldSize = lastSize;\r\n    });\r\n\r\n    stream.pipe(writeStream);\r\n    writeStream.on(&quot;finish&quot;, () =&gt; {\r\n        console.log(&quot;Yazma tamamland\u0131...&quot;);\r\n    });\r\n});\r\n<\/pre>\n<p>yukar\u0131daki gibi yine ba\u015fka bir \u00f6rnek \u00fczerinden g\u00f6sterdi\u011fim \u015fekilde olay\u0131 ele alabiliriz.<\/p>\n<p><a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/07\/Node.js-Stream-Nedir-Nas\u0131l-Olu\u015fturulur-2.gif\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/07\/Node.js-Stream-Nedir-Nas\u0131l-Olu\u015fturulur-2.gif\" alt=\"Node.js - Stream Nedir? Nas\u0131l Olu\u015fturulur?\" width=\"600\" height=\"380\" class=\"aligncenter size-full wp-image-8665\" \/><\/a><\/p>\n<h3>Bellek Performans Optimizasyonu<\/h3>\n<p>\u015eimdi ise streamlerin de\u011ferini daha iyi anlayabilmek i\u00e7in bellek performans optimizasyonunu yapacak ve elde etti\u011fimiz sonu\u00e7larla durumu de\u011ferlendirece\u011fiz.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nconst http = require(&quot;http&quot;);\r\nconst fs = require(&quot;fs&quot;);\r\nconst server = http.createServer((request, response) =&gt; {\r\n    fs.readFile(&quot;text.txt&quot;, (error, data) =&gt; {\r\n        if (error)\r\n            throw error;\r\n\r\n        response.end(data);\r\n    });\r\n});\r\nserver.listen(1453);\r\n<\/pre>\n<p>Yukar\u0131daki kod blo\u011funu incelerseniz e\u011fer 1453 portunda bir server yay\u0131nlanmakta ve bu server i\u00e7erisinde &#8220;text.txt&#8221;(1.975.000 KB) isimli dosya okunmaktad\u0131r. \u00d6ncelik olarak normal bir \u00e7al\u0131\u015fmada performans durumunu g\u00f6zlemleyebilmek i\u00e7in dikkat ederseniz e\u011fer burada herhangi bir stream \u00e7al\u0131\u015fmas\u0131 s\u00f6z konusu de\u011fildir.<\/p>\n<p>\u0130lgili portu taray\u0131c\u0131dan tetiklemeden \u00f6nce &#8220;Task Manager&#8221; penceresinden &#8220;Performance&#8221; sekmesinde &#8220;Memory&#8221; istatistiklerini a\u00e7man\u0131z\u0131 \u00f6neririm.<br \/>\n<a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/07\/Node.js-Stream-Nedir-Nas\u0131l-Olu\u015fturulur-3.gif\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/07\/Node.js-Stream-Nedir-Nas\u0131l-Olu\u015fturulur-3.gif\" alt=\"Node.js - Stream Nedir? Nas\u0131l Olu\u015fturulur?\" width=\"600\" height=\"591\" class=\"aligncenter size-full wp-image-8674\" \/><\/a><\/p>\n<p>Yukar\u0131daki ekran g\u00f6r\u00fcnt\u00fcs\u00fcn\u00fc incelerseniz e\u011fer %42&#8217;lik memory yo\u011funlu\u011fu portumuz tetiklendi\u011fi andan itibaren yap\u0131lan i\u015flerin y\u00fck\u00fcnden dolay\u0131 %100 gibi muazzam bir seviyeye ula\u015fmaktad\u0131r.<\/p>\n<p>\u015eimdi ayn\u0131 i\u015fi stream ile yaparken duruma g\u00f6z atal\u0131m.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nconst http = require(&quot;http&quot;);\r\nconst fs = require(&quot;fs&quot;);\r\nconst stream = fs.createReadStream(&quot;text.txt&quot;);\r\nconst server = http.createServer((request, response) =&gt; {\r\n    stream.pipe(response);\r\n});\r\nserver.listen(1453);\r\n<\/pre>\n<p><a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/07\/Node.js-Stream-Nedir-Nas\u0131l-Olu\u015fturulur-4.gif\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/07\/Node.js-Stream-Nedir-Nas\u0131l-Olu\u015fturulur-4.gif\" alt=\"Node.js - Stream Nedir? Nas\u0131l Olu\u015fturulur?\" width=\"600\" height=\"591\" class=\"aligncenter size-full wp-image-8677\" \/><\/a><br \/>\nG\u00f6rm\u00fc\u015f oldu\u011funuz gibi memory %100 gibi abart\u0131l\u0131 neticeler veren durumlara hi\u00e7 meyletmemekte, stream ile okunan her bir chunk&#8217;\u0131 i\u015flemekte lakin memoryde tutarak biriktirmedi\u011fi i\u00e7in yo\u011funluk bir \u015fekilde kendini dengelemektedir.<\/p>\n<p>Bu yapm\u0131\u015f oldu\u011fumuz optimizasyon sayesinde de Stream&#8217;lerin performans a\u00e7\u0131s\u0131ndan ne kadar hayati m\u00fcdahalede bulundu\u011funu g\u00f6rm\u00fc\u015f olduk.<\/p>\n<p>Bol bol faydalanman\u0131z dile\u011fiyle&#8230;<\/p>\n<p>Sonraki yaz\u0131lar\u0131mda g\u00f6r\u00fc\u015fmek \u00fczere&#8230;<br \/>\n\u0130yi \u00e7al\u0131\u015fmalar&#8230;<\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>Merhaba, Bu i\u00e7eri\u011fimizde, verilerin b\u00fct\u00fcn olarak de\u011fil par\u00e7a par\u00e7a transfer edilmesini sa\u011flayan Stream yap\u0131lar\u0131 \u00fczerine konu\u015fuyor olaca\u011f\u0131z. Stream Nedir? Stream, yukar\u0131da da de\u011finildi\u011fi gibi b\u00fcy\u00fck boyutlu bir veriyi bir yerden ba\u015fka bir yere transfer&#46;&#46;&#46;<!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":1,"featured_media":8677,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2145],"tags":[2489,2493,2488,2492,878,2490,2487,2491,2494],"class_list":["post-8638","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-node-js","tag-node-js-stream","tag-okunabilir-stream","tag-readable","tag-readable-stream","tag-stream","tag-stream-nedir","tag-writeable","tag-writeable-stream","tag-yazilabilir-stream"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/8638","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/comments?post=8638"}],"version-history":[{"count":36,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/8638\/revisions"}],"predecessor-version":[{"id":8680,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/8638\/revisions\/8680"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media\/8677"}],"wp:attachment":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media?parent=8638"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/categories?post=8638"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/tags?post=8638"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}