From e1ada75101952f5d94800334e64f3b452f18a89f Mon Sep 17 00:00:00 2001
From: Sascha Schulz <sschulz@dh-software.de>
Date: Mo, 22 Jul 2024 16:12:20 +0200
Subject: [PATCH] add streams chapter
---
index.html | 76 ++++++++++++++++++++++++++++++++++++++
1 files changed, 76 insertions(+), 0 deletions(-)
diff --git a/index.html b/index.html
index 31beec7..91a7015 100644
--- a/index.html
+++ b/index.html
@@ -4163,6 +4163,82 @@
<p>Aufgabe</p>
<p>Implementiere zur vorherigen Verschlüsselung eine Entschlüsselung.</p>
</section>
+ <section>
+ <p>Lösung</p>
+ <pre>
+ <code class="js" data-trim data-line-numbers>
+ const word = Buffer.from("abcdef");
+ const key = Buffer.from("xyz");
+ const encrypted = Buffer.alloc(word.length, 0);
+
+ // encrypt
+ for (let i = 0; i < word.length; i++) {
+ encrypted[i] = word[i] ^ key[i % key.length];
+ }
+
+ // decrypt
+ for (let i = 0; i < word.length; i++) {
+ word[i] = encrypted[i] ^ key[i % key.length];
+ }
+
+ console.log(word.toString()); // => "abcdef"
+ </code>
+ </pre>
+ </section>
+ <section>
+ <h3>Streams</h3>
+ <p>Konstanter Fluss von (meist) großen Datenmengen in kleinen Paketen</p>
+ </section>
+ <section>
+ <p>Gängige Typen von Streams</p>
+ <ul>
+ <li>Standard In / Out</li>
+ <li>HTTP-Requests / -Responses</li>
+ <li>TCP-Sockets</li>
+ <li>Lese- und Schreib-Streams auf Dateien</li>
+ <li>Große Datenbank-Ergebnisse mittels Cursor</li>
+ </ul>
+ </section>
+ <section>
+ <pre>
+ <code class="js" data-trim data-line-numbers>
+ const readableStream = fs.createReadStream("./source.txt");
+ const writableStream = fs.createWriteStream("./target.txt");
+
+ readableStream.on("data", (data /* Buffer */) => {
+ // process data here
+ writableStream.write(data)
+ });
+
+ readableStream.on("end", (data) => {
+ // stream has finished, e.g. EOF (end of file)
+ });
+
+ // alternatively pipe content
+ readableStream.pipe(writableStream);
+ </code>
+ </pre>
+ </section>
+ <section>
+ <p>Aufgabe</p>
+ <p>Lese eine beliebige Datei als Readable Stream, verschlüssele den Inhalt mit der XOR-Methode und schreibe die verschlüsselten Bytes in eine Zieldatei</p>
+ </section>
+ <section>
+ <p>Aufgabe</p>
+ <p>Ändere das Programm, sodass auf Konsolenebene ein Pipe (|) ermöglicht wird:</p>
+ <pre>
+ <code class="bash" data-trim data-line-numbers>
+ echo "Hello World!" | node index.js
+ </code>
+ </pre>
+ <p>Nutze dafür die implizit vorhandenen Streams von Standard In / Out:</p>
+ <pre>
+ <code class="js" data-trim data-line-numbers>
+ process.stdin; // Readable Stream
+ process.stdout; // Writable Stream
+ </code>
+ </pre>
+ </section>
</section>
</div>
</div>
--
Gitblit v1.9.3