Skip to content

Commit 5cb84b3

Browse files
committed
stream
1 parent 1945a1a commit 5cb84b3

File tree

7 files changed

+222
-54
lines changed

7 files changed

+222
-54
lines changed

pages/ai/index.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@ <h2>Leaderboard</h2>
2323
</script>
2424
<h2>Text -> Images</h2>
2525
<script type="editor" data-lang="sh">
26-
26+
2727
// https://sora.chatgpt.com/explore
28-
2928
</script>
3029
<h2>Interesting links</h2>
3130
<a href="https://arena.lmsys.org/">https://arena.lmsys.org/</a>

pages/bible/index.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ <h2>Notes</h2>
3939
sOslOGlslYGGRF67jir4uu1OasYXTmArkOOKna0iGToTRjh7qZ4xRopBFn0O7XRVDP5UhIDOj/E6rLNrmr7N90L31zP1WmJtmgX5XqZaMEJ7eo
4040
L00LxwDx4IrXvxzmIyi9T2r4cV83YjkYLuzQKlSRhwnZ4cMJ5fsTGFbVN+CLGsLilddosa6MHQU/gsVTSr1FXv3To1m+0RF/XmuplYB0EHTUb8
4141
CUC9fpzu8tCQHB7XQjzjFImawdEAnxFUIbjM:]:
42-
4342
</script>
4443
<h2>Versions</h2>
4544

pages/c/index.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@ <h2>Examples</h2>
1919
<script type="editor" data-lang="sh">
2020

2121
// https://youtu.be/tEmBqQIqR-U GNU/Linux & Ethernet: Writing a simple TCP socket Client in Linux with C
22-
2322
</script>
24-
23+
2524
<h2>stdin from file</h2>
2625
<script type="editor" data-lang="sh">
2726

pages/clock/index.template.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ <h2>US timezones</h2>
7474
Chicago, Illinois
7575
Eastern Time Zone: ET (also known as EST during the winter months and EDT during the summer months)
7676
New York City, New York
77-
77+
7878
After discussion with one guy from US: it seems using PST, MST, CST and EST is the most commonly used
7979
Sometimes people say PDT ( I know one anomaly ;) ) but apparently pretty much no one
8080
is using MDT, CDT, EDT at all

pages/encryptor/index.template.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ <h2>encrypt</h2>
3939
<span> key: </span>
4040
<input type="text" class="key" />
4141
<button class="generate">generate</button>
42-
<a href="https://github.com/stopsopa/gitstorage/blob/master/git%40github.com%3Astopsopa__stopsopa.github.io.git/.env#L56C13-L56C57">*</a>
42+
<a
43+
href="https://github.com/stopsopa/gitstorage/blob/master/git%40github.com%3Astopsopa__stopsopa.github.io.git/.env#L56C13-L56C57"
44+
>*</a
45+
>
4346
</label>
4447
<br />
4548
<label>

pages/iphone/index.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ <h2>Identify iPhone/Ipad model</h2>
2222

2323
iPad:
2424
https://support.apple.com/en-gb/108043
25-
2625
</script>
2726
<h2>re-downloading apps without password</h2>
2827
<script type="editor" data-lang="python">

pages/node/index.template.html

Lines changed: 215 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -197,28 +197,27 @@ <h2>getLine</h2>
197197

198198
function getLine(opt) {
199199
let { error, stackLine = 2 } = opt || {};
200-
200+
201201
if (!Number.isInteger(stackLine)) {
202202
throw new Error(`getLine: stackLine must be an integer`);
203203
}
204-
204+
205205
// Create an Error object to capture the stack trace
206206
if (!error) {
207207
error = new Error();
208208
}
209-
209+
210210
// Parse the stack trace to extract the line number
211211
const stackLines = error.stack.split("\n");
212-
212+
213213
// Get the caller's stack frame (index 2 skips this function and its immediate caller)
214214
const callerFrame = stackLines[stackLine];
215-
215+
216216
// Extract the line number using regex
217217
const lineMatch = callerFrame.match(/:(\d+):\d+\)?$/);
218-
218+
219219
return lineMatch ? parseInt(lineMatch[1], 10) : -1;
220220
}
221-
222221
</script>
223222
<h2>useful</h2>
224223
<script type="editor" data-lang="sh">
@@ -1401,64 +1400,234 @@ <h2>nodemon & chokidar</h2>
14011400
<br />
14021401
<a href="https://stackoverflow.com/a/13807906">about fswatch</a>
14031402
<h2>streams</h2>
1404-
<script type="editor" data-lang="js">
14051403

1406-
// https://github.com/substack/stream-handbook
1407-
// from: https://github.com/nkzawa/socket.io-stream
1404+
<div data-vanilla-tabs>
1405+
<div data-buttons>
1406+
<span class="active">stream</span>
1407+
<span>pipeline</span>
1408+
<span>Transform</span>
1409+
<span>response to file</span>
1410+
<span>Transform Streams</span>
1411+
<span>stream to response</span>
1412+
</div>
1413+
<div data-tabs>
1414+
<div>
1415+
<script type="editor" data-lang="js">
1416+
1417+
// https://github.com/substack/stream-handbook
1418+
// from: https://github.com/nkzawa/socket.io-stream
14081419

1409-
// https://nodesource.com/blog/understanding-streams-in-nodejs/
1420+
// https://nodesource.com/blog/understanding-streams-in-nodejs/
14101421

14111422

1412-
// https://www.freecodecamp.org/news/node-js-streams-everything-you-need-to-know-c9141306be93/
1413-
// Create big file using stream
1414-
const fs = require('fs');
1415-
const file = fs.createWriteStream('./big.file');
1423+
// https://www.freecodecamp.org/news/node-js-streams-everything-you-need-to-know-c9141306be93/
1424+
// Create big file using stream
1425+
const fs = require('fs');
1426+
const file = fs.createWriteStream('./big.file');
14161427

1417-
for(let i=0; i<= 1e6; i++) {
1418-
file.write('Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\n');
1419-
}
1428+
for(let i=0; i<= 1e6; i++) {
1429+
file.write('Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\n');
1430+
}
14201431

1421-
file.end();
1432+
file.end();
14221433

14231434

14241435

1425-
# https://www.freecodecamp.org/news/node-js-streams-everything-you-need-to-know-c9141306be93/
1426-
# more: https://jscomplete.com/learn/node-beyond-basics/node-streams g(node beyond basics)
1436+
# https://www.freecodecamp.org/news/node-js-streams-everything-you-need-to-know-c9141306be93/
1437+
# more: https://jscomplete.com/learn/node-beyond-basics/node-streams g(node beyond basics)
14271438

1428-
# https://www.sitepoint.com/basics-node-js-streams/
1429-
# http://2ality.com/2018/04/async-iter-nodejs.html
1430-
# http://2ality.com/2018/05/child-process-streams.html
1439+
# https://www.sitepoint.com/basics-node-js-streams/
1440+
# http://2ality.com/2018/04/async-iter-nodejs.html
1441+
# http://2ality.com/2018/05/child-process-streams.html
14311442

1432-
# read file to http response:
1433-
const stream = fs.createReadStream(file);
1434-
stream.pipe(res);
1443+
# read file to http response:
1444+
const stream = fs.createReadStream(file);
1445+
stream.pipe(res);
14351446

1436-
# write http response to file:
1437-
const stream = fs.createWriteStream(file);
1438-
res.pipe(stream);
1447+
# write http response to file:
1448+
const stream = fs.createWriteStream(file);
1449+
res.pipe(stream);
14391450

1440-
readableStream.pipe(writableStream);
1451+
readableStream.pipe(writableStream);
14411452

1442-
# pipe stream error handling
1443-
await new Promise((resolve, reject ) => {
1453+
# pipe stream error handling
1454+
await new Promise((resolve, reject ) => {
14441455

1445-
var stream = res.pipe(fs.createWriteStream(file));
1456+
var stream = res.pipe(fs.createWriteStream(file));
14461457

1447-
stream.on('finish', () => resolve());
1458+
stream.on('finish', () => resolve());
14481459

1449-
stream.on('error', e => reject({
1450-
writable_stream_error: e
1451-
}));
1452-
});
1460+
stream.on('error', e => reject({
1461+
writable_stream_error: e
1462+
}));
1463+
});
14531464

1454-
# unzip
1455-
var fs = require('fs');
1456-
var zlib = require('zlib');
1465+
# unzip
1466+
var fs = require('fs');
1467+
var zlib = require('zlib');
1468+
1469+
fs.createReadStream('input.txt.gz')
1470+
.pipe(zlib.createGunzip())
1471+
.pipe(fs.createWriteStream('output.txt'));
1472+
</script>
1473+
</div>
1474+
<div>
1475+
<script type="editor" data-lang="ts">
1476+
1477+
// from: https://www.sitepoint.com/node-js-streams-with-typescript/#h-example-4-compressing-files-with-a-duplex-stream
1478+
1479+
import { createReadStream, createWriteStream } from 'fs';
1480+
import { createGzip } from 'zlib';
1481+
import { pipeline } from 'stream';
1482+
1483+
const source = createReadStream('data.txt');
1484+
const destination = createWriteStream('data.txt.gz');
1485+
const gzip = createGzip();
1486+
1487+
pipeline(source, gzip, destination, (err: Error | null) => {
1488+
if (err) {
1489+
console.error('Compression failed:', err.message);
1490+
return;
1491+
}
1492+
console.log('File compressed successfully! Check data.txt.gz');
1493+
});
1494+
</script>
1495+
</div>
1496+
<div>
1497+
<script type="editor" data-lang="ts">
1498+
1499+
// from: https://www.sitepoint.com/node-js-streams-with-typescript/#h-example-3-piping-with-a-transform-stream
1500+
1501+
import { createReadStream, createWriteStream } from 'fs';
1502+
import { Transform, TransformCallback } from 'stream';
1503+
1504+
// Custom Transform stream to uppercase text
1505+
class UppercaseTransform extends Transform {
1506+
_transform(chunk: Buffer, encoding: string, callback: TransformCallback): void {
1507+
const upperChunk = chunk.toString().toUpperCase();
1508+
this.push(upperChunk);
1509+
callback();
1510+
}
1511+
}
1512+
1513+
const readStream = createReadStream('data.txt', { encoding: 'utf8' });
1514+
const writeStream = createWriteStream('output_upper.txt');
1515+
const transformStream = new UppercaseTransform();
1516+
1517+
readStream
1518+
.pipe(transformStream)
1519+
.pipe(writeStream)
1520+
.on('finish', () => {
1521+
console.log('Transform complete! Check output_upper.txt');
1522+
})
1523+
.on('error', (err: Error) => {
1524+
console.error('Error:', err.message);
1525+
});
1526+
</script>
1527+
</div>
1528+
1529+
<div>
1530+
<script type="editor" data-lang="ts">
1531+
1532+
// from: https://www.sitepoint.com/node-js-streams-with-typescript/#h-example-5-streaming-http-responses
1533+
1534+
import axios from 'axios';
1535+
import { createWriteStream } from 'fs';
1536+
import { Writable } from 'stream';
1537+
1538+
async function streamHttpResponse(url: string, outputFile: string): Promise<void> {
1539+
const response = await axios({
1540+
method: 'get',
1541+
url,
1542+
responseType: 'stream',
1543+
});
1544+
1545+
const writeStream: Writable = createWriteStream(outputFile);
1546+
response.data.pipe(writeStream);
1547+
1548+
return new Promise((resolve, reject) => {
1549+
writeStream.on('finish', () => {
1550+
console.log(`Downloaded to ${outputFile}`);
1551+
resolve();
1552+
});
1553+
writeStream.on('error', (err: Error) => {
1554+
console.error('Download failed:', err.message);
1555+
reject(err);
1556+
});
1557+
});
1558+
}
1559+
1560+
streamHttpResponse('https://example.com', 'example.html').catch(console.error);
1561+
</script>
1562+
</div>
1563+
1564+
<div>
1565+
<script type="editor" data-lang="ts">
1566+
1567+
// from: https://www.sitepoint.com/node-js-streams-with-typescript/#h-example-6-real-time-data-processing-with-a-custom-readable-stream
1568+
1569+
import { createReadStream, createWriteStream } from 'fs';
1570+
import { Transform, TransformCallback } from 'stream';
1571+
1572+
class UppercaseTransform extends Transform {
1573+
_transform(chunk: Buffer, encoding: string, callback: TransformCallback): void {
1574+
this.push(chunk.toString().toUpperCase());
1575+
callback();
1576+
}
1577+
}
1578+
1579+
class TimestampTransform extends Transform {
1580+
_transform(chunk: Buffer, encoding: string, callback: TransformCallback): void {
1581+
const timestamp = new Date().toISOString();
1582+
this.push(`[${timestamp}] ${chunk.toString()}`);
1583+
callback();
1584+
}
1585+
}
1586+
1587+
const readStream = createReadStream('data.txt', { encoding: 'utf8' });
1588+
const writeStream = createWriteStream('output_chain.txt');
1589+
const upper = new UppercaseTransform();
1590+
const timestamp = new TimestampTransform();
1591+
1592+
readStream
1593+
.pipe(upper)
1594+
.pipe(timestamp)
1595+
.pipe(writeStream)
1596+
.on('finish', () => {
1597+
console.log('Chained transform complete! Check output_chain.txt');
1598+
})
1599+
.on('error', (err: Error) => {
1600+
console.error('Error:', err.message);
1601+
});
1602+
</script>
1603+
</div>
1604+
1605+
<div>
1606+
<script type="editor" data-lang="ts">
1607+
1608+
// from: https://www.sitepoint.com/node-js-streams-with-typescript/#h-real-world-use-case-streaming-api-responses
1609+
1610+
import express from 'express';
1611+
import { Readable } from 'stream';
1612+
1613+
const app = express();
1614+
1615+
app.get('/stream-data', (req, res) => {
1616+
const data = ['Item 1\n', 'Item 2\n', 'Item 3\n'];
1617+
const stream = Readable.from(data);
1618+
1619+
res.setHeader('Content-Type', 'text/plain');
1620+
stream.pipe(res);
1621+
});
1622+
1623+
app.listen(3000, () => {
1624+
console.log('Server running on port 3000');
1625+
});
1626+
</script>
1627+
</div>
1628+
</div>
1629+
</div>
14571630

1458-
fs.createReadStream('input.txt.gz')
1459-
.pipe(zlib.createGunzip())
1460-
.pipe(fs.createWriteStream('output.txt'));
1461-
</script>
14621631
<h2>Signleton (kinda) module template</h2>
14631632
<script type="editor" data-lang="js">
14641633

0 commit comments

Comments
 (0)