Skip to content

Commit ee100b8

Browse files
committed
Examples for 1.5
1 parent d00a50a commit ee100b8

File tree

6 files changed

+73
-0
lines changed

6 files changed

+73
-0
lines changed

1.5/StringDecoder.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const { StringDecoder } = require('string_decoder');
2+
const decoder = new StringDecoder('utf8');
3+
4+
process.stdin.on('readable', () => {
5+
const chunk = process.stdin.read();
6+
if (chunk != null) {
7+
const buffer = Buffer.from([chunk]);
8+
console.log('With .toString():', buffer.toString());
9+
console.log('With StringDecoder:', decoder.write(buffer));
10+
}
11+
});
12+
13+
// 0xE2, 0x82, 0xAC -> €

1.5/buff.slice.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const fs = require('fs');
2+
3+
const conversionMap = {
4+
'88': '65',
5+
'89': '66',
6+
'90': '67',
7+
};
8+
9+
fs.readFile(__filename, (err, buffer) => {
10+
let tag = buffer.slice(-4, -1);
11+
12+
for(let i=0;i < tag.length; i++) {
13+
tag[i] = conversionMap[tag[i]];
14+
}
15+
16+
console.log(buffer.toString());
17+
});
18+
19+
// TAG: XYZ

1.5/buffer.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const string = 'touché';
2+
const buffer = Buffer.from('touché');
3+
4+
console.log(string, string.length);
5+
console.log(buffer, buffer.length);

1.5/index.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// instead of
2+
3+
process.env.PORT
4+
5+
// read from a config utility
6+
7+
const { config } = require('./util');
8+
9+
config.port

1.5/process.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// process is an event emitter
2+
3+
process.on('exit', (code) => {
4+
// do one final synchronous operation
5+
// before the node process terminates
6+
7+
console.log(`About to exit with code: ${code}`);
8+
});
9+
10+
process.on('uncaughtException', (err) => {
11+
// something went unhandled.
12+
// Do any cleanup and exit anyway!
13+
14+
console.error(err); // don't do just that.
15+
16+
// FORCE exit the process too.
17+
process.exit(1);
18+
});
19+
20+
// keep the event loop busy
21+
process.stdin.resume();
22+
23+
// trigger a TypeError exception
24+
console.dog();

1.5/util.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const config = {
2+
port: process.env.PORT || 8080
3+
};

0 commit comments

Comments
 (0)