From 38ddab2438b18a73546cd6f6d499ee436002bb19 Mon Sep 17 00:00:00 2001
From: Sascha Schulz <sschulz@dh-software.de>
Date: Mo, 09 Dez 2024 14:54:21 +0100
Subject: [PATCH] Merge branch 'draft'

---
 index.html |  245 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 245 insertions(+), 0 deletions(-)

diff --git a/index.html b/index.html
index 91a7015..710cff3 100644
--- a/index.html
+++ b/index.html
@@ -4239,6 +4239,251 @@
 							</code>
 						</pre>
 					</section>
+					<section>
+						<h3>Kryptographie</h3>
+					</section>
+					<section>
+						Mithilfe des nativen <code>crypto</code>-Moduls ist es möglich, moderne sichere kryptografische Verfahren zu verwenden:
+						<pre>
+							<code class="js" data-trim data-line-numbers>
+								const crypto = require("crypto");
+							</code>
+						</pre>
+						Hierzu nutzt NodeJS selber die weit verbreitete OpenSSL-Bibliothek
+					</section>
+					<section>
+						Einen MD5-Hash erzeugen:
+						<pre>
+							<code class="js" data-trim data-line-numbers>
+								const crypto = require("crypto");
+
+								const hash = crypto.createHash("md5");
+								const md5 = hash.update("abcdefg")
+									.digest(); // Buffer (default)
+									// .digest("hex") // Hexadezimal
+									// .digest("utf8") // Bytes forciert als Chars
+								
+								console.log(md5);
+							</code>
+						</pre>
+					</section>
+					<section>
+						<p>Aufgabe</p>
+						<p>Erzeuge jeweils den md5- und den sha256-Hash als Hex-Wert der beiden Texte in der Datei <code>text-examples.txt</code> und vergleiche jeweils ihren md5- und sha256-Hash miteinander</p>
+					</section>
+					<section>
+						<h3>Zippen</h3>
+					</section>
+					<section>
+						Mit dem nativen Modul <code>zlib</code> ist es möglich, Kompression in seinen Anwendungen zu nutzen.
+						<pre>
+							<code class="js" data-trim data-line-numbers>
+								const zlib = require("zlib");
+
+								// Komprimieren
+								zlib.deflate("abc" /* buffer | string */, (err, compressed) => {});
+								
+								// Dekomprimieren
+								zlib.inflate(compressed /* buffer */, (err, decompressed) => {});
+								
+								// Als Stream
+								const zipper = zlib.createDeflate();
+								const unzipper = zlib.createInflate();
+								
+								source.pipe(zipper).pipe(target);
+								source.pipe(unzipper).pipe(target);
+							</code>
+						</pre>
+					</section>
+					<section>
+						<p>Aufgabe</p>
+						<p>Nutze die einfachen Varianten <code>zlib.deflate</code> und <code>zlib.inflate</code>, um einen beliebigen Text zu komprimieren und anschließend wieder zu dekomprimieren</p>
+					</section>
+					<section>
+						<p>Aufgabe</p>
+						<p>Nutze die Stream-Varianten <code>zlib.createDeflate</code> und <code>zlib.createInflate</code>, um jeweils ein getrenntes Programm für die Kompression und Dekompression mit <code>process.stdin</code> und <code>process.stdout</code> zu machen, sodass in der Konsole folgende Nutzung möglich ist:</p>
+						<pre>
+							<code class="bash" data-trim data-line-numbers>
+								echo "abc" | node compress.js | node decompress.js
+								cat file.txt | node compress.js > file.zipped
+								cat file.zipped | node decompress.js > file.txt
+							</code>
+						</pre>
+					</section>
+					<section>
+						<h3>TCP-Verbindungen</h3>
+					</section>
+					<section>
+						<ul>
+							<li>Bidirektionale Verbindung</li>
+							<li>Duplexfähig</li>
+							<li>Besteht immer aus Server- und Client-Socket</li>
+						</ul>
+					</section>
+					<section>
+						Mit dem nativen Modul <code>net</code> ist es möglich, eine TCP-Verbindung aufzubauen
+						<pre>
+							<code class="js" data-trim data-line-numbers>
+								/* Server */
+								const net = require("net");
+
+								const server = net.createServer((socket) => {
+								
+									// new incoming connection
+									socket.on("data", (data) => {
+										// data is a buffer
+									});
+								
+									socket.write("Hello World!");
+								});
+								
+								server.listen(3000);
+							</code>
+						</pre>
+					</section>
+					<section>
+						<pre>
+							<code class="js" data-trim data-line-numbers>
+								/* Client */
+								const net = require("net");
+
+								const socket = net.connect(3000, () => {
+									// 'connect' listener
+								});
+								
+								socket.on("data", (data) => {
+									// data is a buffer
+								});
+							</code>
+						</pre>
+					</section>
+					<section>
+						<p>Aufgabe</p>
+						<p>Implementiere die Clientlogik in der <code>tcp-client.js</code>, um dem Server ein <code>"Hello Server!"</code> zurück zu senden</p>
+					</section>
+					<section>
+						<p>Aufgabe</p>
+						<p>Sende eine festgelegte Datei vom Server zum Client. Der Server liest die Datei ein und sendet die Inhalte zum Client, welcher diese wiederum in eine Zieldatei schreibt. Eventuell darf auch noch eine Kompression mittels Zip / Deflate implementiert werden.</p>
+					</section>
+					<section>
+						<p>Kurze Wiederholung des HTTP-Protokolls</p>
+						<p>Request</p>
+						<pre>
+							<code class="http" data-trim data-line-numbers>
+								GET / HTTP/1.1
+								Host: localhost:3456
+							</code>
+						</pre>
+						<p>Response</p>
+						<pre>
+							<code class="http" data-trim data-line-numbers>
+								HTTP/1.1 200
+								Content-Type: text/plain
+								Content-Length: 12
+								
+								Hello World!
+							</code>
+						</pre>
+					</section>
+					<section>
+						<p>Aufgabe</p>
+						<p>Implementiere mithilfe des TCP-Sockets einen minimalen HTTP-Server, welcher <code>Hello &lt;name></code> als Text zurück gibt, wobei <code>&lt;name></code> aus dem Query-Parameter gelesen werden soll</p>
+					</section>
+					<section>
+						<h3>HTTP-Server</h3>
+					</section>
+					<section>
+						<pre>
+							<code class="js" data-trim data-line-numbers>
+								const http = require("http");
+						
+								const server = http.createServer((req, res) => {
+									res.writeHeader(200, {
+										"Content-Type": "text/plain", /* Put Content-Type here */
+									});
+									
+									console.log(req.rawHeaders);
+									console.log(req.method);
+									console.log(req.url);
+									
+									res.write(/* response content */);
+									res.end(); /* important if no Content-Length is specified */
+								});
+								
+								server.listen(3456);
+							</code>
+						</pre>
+					</section>
+					<section>
+						<p>Aufgabe</p>
+						<p>Experimentiert mit verschiedenen Möglichkeiten von <code>Content-Length</code> und dem Vorhandensein von <code>res.end()</code>, mit längerer oder kürzerer <code>Content-Length</code> als der tatsächliche Body oder teilt den Body in zwei <code>res.write()</code> und verzögert künstlich mit einem setTimeout() den zweiten Teil und schaut euch im Browser bei den Entwickler-Tools die Timings der Response an.</p>
+					</section>
+					<section>
+						<h3>Worker Threads</h3>
+					</section>
+					<section>
+						<p>Was sind Worker Threads?</p>
+						<p>Eine bidirektionale nachrichten-basierte Implementierung von Multithreading in NodeJS</p>
+					</section>
+					<section>
+						<p>Beispiel:</p>
+						<pre>
+							<code class="js" data-trim data-line-numbers>
+								// Main Thread
+								const { Worker } = require('worker_threads');
+
+								const data = 'some data';
+								
+								const worker = new Worker("path/to/worker.js", { workerData: data });
+								
+								worker.on('message', message => console.log('Reply from Thread:', message));
+								
+								// Worker Thread
+								const { workerData, parentPort } = require('worker_threads');
+
+								const result = workerData.toUpperCase();
+								
+								parentPort.postMessage(result);
+							</code>
+						</pre>
+					</section>
+					<section>
+						<p>Weitere nützliche Funktionen:</p>
+						<pre>
+							<code class="js" data-trim data-line-numbers>
+								// Main Thread
+								worker.on('message', message => processMessage(message));
+								worker.on('error', error => console.error(error));
+								
+								worker.postMessage({a: 1}); // send any data to the worker
+								
+								worker.terminate(); // manually terminate worker thread
+								
+								// Worker Thread
+								parentPort.on('message', message => doSomething(message)); // receive messages from the main thread
+								
+								process.exit(0); // terminate self, optionally with exit code
+							</code>
+						</pre>
+					</section>
+					<section>
+						<p>Aufgabe</p>
+						<p>
+							Nutze die in den <code>examples</code> vorhandene Funktion <code>isPrime</code> (010-nodejs/mt-main.js und 010-nodejs/mt-worker.js) und implementiere mit Hilfe der <code>Worker Threads</code> die Suche nach Primzahlen.
+							Die gewünschte maximale Zahl, bis zu der die Primzahlen gesucht werden sollen als auch die Anzahl der Threads, mit der gleichzeitig gesucht werden soll, sollen möglichst einfach parametrisierbar sein (einfache Variable im Code genügt).
+							
+							Probiert verschiedene Thread-Anzahlen aus und vergleicht einmal die benötigten Zeiten.
+						</p>
+					</section>
+					<section>
+						<p>Hinweise:</p>
+						<ul>
+							<li>Bei der Suche bis zu 1.000.000 und 8 Threads soll der erste Thread von 0 bis 125.000 suchen, der zweite von 125.000 bis 250.000 usw.</li>
+							<li>Nutzt Promises, um auf alle Ergebnisse warten zu können. (s. <code>Promise.all</code>)</li>
+							<li>per <code>workerData</code> können auch Objekte übergeben werden</li>
+							<li>Die gefundenen Primzahlen können ausgegeben werden, müssen aber nicht. In erster Linie geht es darum, die Arbeit auf mehrere Threads aufzuteilen.</li>
+						</ul>
+					</section>
 				</section>
 			</div>
 		</div>

--
Gitblit v1.9.3