File tree 6 files changed +73
-0
lines changed
6 files changed +73
-0
lines changed Original file line number Diff line number Diff line change
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 -> €
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
1
+ const string = 'touché' ;
2
+ const buffer = Buffer . from ( 'touché' ) ;
3
+
4
+ console . log ( string , string . length ) ;
5
+ console . log ( buffer , buffer . length ) ;
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 ( ) ;
Original file line number Diff line number Diff line change
1
+ export const config = {
2
+ port : process . env . PORT || 8080
3
+ } ;
You can’t perform that action at this time.
0 commit comments