-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from PacktPublishing/Chapter-2
Chapter 2 Coordinating IO
- Loading branch information
Showing
60 changed files
with
2,474 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
Large diffs are not rendered by default.
Oops, something went wrong.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions
16
2-Coordinating-IO/source/communicating-over-sockets/client.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
'use strict' | ||
|
||
const net = require('net') | ||
|
||
const socket = net.connect(1337, 'localhost') | ||
const name = process.argv[2] || 'Dave' | ||
|
||
socket.write(name) | ||
|
||
socket.on('data', (data) => { | ||
console.log(data.toString()) | ||
}) | ||
|
||
socket.on('close', () => { | ||
console.log('-> disconnected by server') | ||
}) |
14 changes: 14 additions & 0 deletions
14
2-Coordinating-IO/source/communicating-over-sockets/net-sockets-are-streams/client.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
'use strict' | ||
|
||
const net = require('net') | ||
|
||
const socket = net.connect(1337) | ||
const name = process.argv[2] || 'Dave' | ||
|
||
socket.write(name) | ||
|
||
socket.pipe(process.stdout) | ||
|
||
socket.on('close', () => { | ||
console.log('-> disconnected by server') | ||
}) |
3 changes: 3 additions & 0 deletions
3
2-Coordinating-IO/source/communicating-over-sockets/net-sockets-are-streams/echo-client.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
'use strict' | ||
|
||
process.stdin.pipe(require('net').connect(1338)).pipe(process.stdout) |
3 changes: 3 additions & 0 deletions
3
2-Coordinating-IO/source/communicating-over-sockets/net-sockets-are-streams/echo-server.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
'use strict' | ||
|
||
require('net').createServer((socket) => socket.pipe(socket)).listen(1338) |
13 changes: 13 additions & 0 deletions
13
2-Coordinating-IO/source/communicating-over-sockets/net-sockets-are-streams/server.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
'use strict' | ||
|
||
const net = require('net') | ||
|
||
net.createServer((socket) => { | ||
console.log('-> client connected') | ||
socket.once('data', name => { | ||
socket.write(`Hi ${name}!`) | ||
}) | ||
socket.on('close', () => { | ||
console.log('-> client disconnected') | ||
}) | ||
}).listen(1337) |
13 changes: 13 additions & 0 deletions
13
2-Coordinating-IO/source/communicating-over-sockets/server.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
'use strict' | ||
|
||
const net = require('net') | ||
|
||
net.createServer((socket) => { | ||
console.log('-> client connected') | ||
socket.on('data', name => { | ||
socket.write(`Hi ${name}!`) | ||
}) | ||
socket.on('close', () => { | ||
console.log('-> client disconnected') | ||
}) | ||
}).listen(1337, 'localhost') |
13 changes: 13 additions & 0 deletions
13
2-Coordinating-IO/source/communicating-over-sockets/udp-sockets/client.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
'use strict' | ||
|
||
const dgram = require('dgram') | ||
|
||
const socket = dgram.createSocket('udp4') | ||
const name = process.argv[2] || 'Dave' | ||
|
||
socket.bind(1400) | ||
socket.send(name, 1339) | ||
|
||
socket.on('message', (data) => { | ||
console.log(data.toString()) | ||
}) |
10 changes: 10 additions & 0 deletions
10
2-Coordinating-IO/source/communicating-over-sockets/udp-sockets/server.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
'use strict' | ||
|
||
const dgram = require('dgram') | ||
|
||
const socket = dgram.createSocket('udp4') | ||
socket.bind(1339) | ||
|
||
socket.on('message', (name) => { | ||
socket.send(`Hi ${name}!`, 1400) | ||
}) |
16 changes: 16 additions & 0 deletions
16
2-Coordinating-IO/source/communicating-over-sockets/unix-sockets/client.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
'use strict' | ||
|
||
const net = require('net') | ||
|
||
const socket = net.connect('/tmp/my.socket') | ||
const name = process.argv[2] || 'Dave' | ||
|
||
socket.write(name) | ||
|
||
socket.on('data', (data) => { | ||
console.log(data.toString()) | ||
}) | ||
|
||
socket.on('close', () => { | ||
console.log('-> disconnected by server') | ||
}) |
13 changes: 13 additions & 0 deletions
13
2-Coordinating-IO/source/communicating-over-sockets/unix-sockets/server.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
'use strict' | ||
|
||
const net = require('net') | ||
|
||
net.createServer((socket) => { | ||
console.log('-> client connected') | ||
socket.on('data', name => { | ||
socket.write(`Hi ${name}!`) | ||
}) | ||
socket.on('close', () => { | ||
console.log('-> client disconnected') | ||
}) | ||
}).listen('/tmp/my.socket') |
17 changes: 17 additions & 0 deletions
17
2-Coordinating-IO/source/fetching-meta-data/checking-file-existence/check.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
'use strict' | ||
|
||
const fs = require('fs') | ||
|
||
const exists = (file) => new Promise((resolve, reject) => { | ||
fs.access(file, (err) => { | ||
if (err) { | ||
if (err.code !== 'ENOENT') { return reject(err) } | ||
return resolve({file, exists: false}) | ||
} | ||
resolve({file, exists: true}) | ||
}) | ||
}) | ||
|
||
exists(process.argv[2]) | ||
.then(({file, exists}) => console.log(`"${file}" does${exists ? '' : ' not'} exist`)) | ||
.catch(console.error) |
66 changes: 66 additions & 0 deletions
66
2-Coordinating-IO/source/fetching-meta-data/getting-symlink-information/meta.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
'use strict' | ||
|
||
const fs = require('fs') | ||
const path = require('path') | ||
const tableaux = require('tableaux') | ||
|
||
const write = tableaux( | ||
{name: 'Name', size: 20}, | ||
{name: 'Created', size: 30}, | ||
{name: 'Inode', size: 10}, | ||
{name: 'Mode', size: 8}, | ||
{name: 'Lnks', size: 4}, | ||
{name: 'Size', size: 6} | ||
) | ||
|
||
function print(dir) { | ||
fs.readdirSync(dir) | ||
.map((file) => ({file, dir})) | ||
.map(toMeta) | ||
.forEach(output) | ||
write.newline() | ||
} | ||
|
||
function toMeta({file, dir}) { | ||
const stats = fs.lstatSync(path.join(dir, file)) | ||
let {birthtime, ino, mode, nlink, size} = stats | ||
birthtime = birthtime.toUTCString() | ||
mode = mode.toString(8) | ||
size += 'B' | ||
return { | ||
file, | ||
dir, | ||
info: [birthtime, ino, mode, nlink, size], | ||
isDir: stats.isDirectory(), | ||
isSymLink: stats.isSymbolicLink() | ||
} | ||
} | ||
|
||
function output({file, dir, info, isDir, isSymLink}) { | ||
if (isSymLink) { | ||
outputSymlink(file, dir, info) | ||
return | ||
} | ||
write(file, ...info) | ||
if (!isDir) { return } | ||
const p = path.join(dir, file) | ||
write.arrow() | ||
fs.readdirSync(p).forEach((f) => { | ||
const stats = fs.lstatSync(path.join(p, f)) | ||
const style = stats.isDirectory() ? 'bold' : 'dim' | ||
if (stats.isSymbolicLink()) { f = '\u001b[33m' + f + '\u001b[0m'} | ||
write[style](f) | ||
}) | ||
write.newline() | ||
} | ||
|
||
function outputSymlink(file, dir, info) { | ||
write('\u001b[33m' + file + '\u001b[0m', ...info) | ||
process.stdout.write('\u001b[33m') | ||
write.arrow(4) | ||
write.bold(fs.readlinkSync(path.join(dir, file))) | ||
process.stdout.write('\u001b[0m') | ||
write.newline() | ||
} | ||
|
||
print(process.argv[2] || '.') |
1 change: 1 addition & 0 deletions
1
...ating-IO/source/fetching-meta-data/getting-symlink-information/my-folder/absolute-symlink
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/tmp |
1 change: 1 addition & 0 deletions
1
...nating-IO/source/fetching-meta-data/getting-symlink-information/my-folder/link-to-symlink
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
my-symlink |
1 change: 1 addition & 0 deletions
1
2-Coordinating-IO/source/fetching-meta-data/getting-symlink-information/my-folder/my-file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
my edit |
Empty file.
Empty file.
Empty file.
1 change: 1 addition & 0 deletions
1
.../source/fetching-meta-data/getting-symlink-information/my-folder/my-subdir/subdir-symlink
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
another-file |
1 change: 1 addition & 0 deletions
1
2-Coordinating-IO/source/fetching-meta-data/getting-symlink-information/my-folder/my-symlink
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
my-file |
1 change: 1 addition & 0 deletions
1
...ating-IO/source/fetching-meta-data/getting-symlink-information/my-folder/relative-symlink
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../meta.js |
1 change: 1 addition & 0 deletions
1
2-Coordinating-IO/source/fetching-meta-data/getting-symlink-information/my-folder/too-deep
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
my-subdir/my-subsubdir/too-deep |
26 changes: 26 additions & 0 deletions
26
2-Coordinating-IO/source/fetching-meta-data/manipulating-meta-data/make-file-efficiently.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
'use strict' | ||
|
||
const fs = require('fs') | ||
const {execSync} = require('child_process') | ||
|
||
const file = process.argv[2] | ||
if (!file) { | ||
console.error('specify a file') | ||
process.exit(1) | ||
} | ||
try { | ||
fs.accessSync(file) | ||
console.error('file already exists') | ||
process.exit(1) | ||
} catch (e) { | ||
makeIt() | ||
} | ||
|
||
function makeIt() { | ||
const nobody = Number(execSync('id -u nobody').toString().trim()) | ||
const fd = fs.openSync(file, 'w') | ||
fs.fchmodSync(fd, 0) | ||
fs.fchownSync(fd, nobody, nobody) | ||
console.log(file + ' created') | ||
} | ||
|
25 changes: 25 additions & 0 deletions
25
2-Coordinating-IO/source/fetching-meta-data/manipulating-meta-data/make-file.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
'use strict' | ||
|
||
const fs = require('fs') | ||
const {execSync} = require('child_process') | ||
|
||
const file = process.argv[2] | ||
if (!file) { | ||
console.error('specify a file') | ||
process.exit(1) | ||
} | ||
try { | ||
fs.accessSync(file) | ||
console.error('file already exists') | ||
process.exit(1) | ||
} catch (e) { | ||
makeIt() | ||
} | ||
|
||
function makeIt() { | ||
const nobody = Number(execSync('id -u nobody').toString().trim()) | ||
fs.writeFileSync(file, '') | ||
fs.chownSync(file, nobody, nobody) | ||
fs.chmodSync(file, 0) | ||
console.log(file + ' created') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
'use strict' | ||
|
||
const fs = require('fs') | ||
const path = require('path') | ||
const tableaux = require('tableaux') | ||
|
||
const write = tableaux( | ||
{name: 'Name', size: 20}, | ||
{name: 'Created', size: 30}, | ||
{name: 'Inode', size: 10}, | ||
{name: 'Mode', size: 8}, | ||
{name: 'Lnks', size: 4}, | ||
{name: 'Size', size: 6} | ||
) | ||
|
||
function print(dir) { | ||
fs.readdirSync(dir) | ||
.map((file) => ({file, dir})) | ||
.map(toMeta) | ||
.forEach(output) | ||
write.newline() | ||
} | ||
|
||
function toMeta({file, dir}) { | ||
const stats = fs.statSync(path.join(dir, file)) | ||
let {birthtime, ino, mode, nlink, size} = stats | ||
birthtime = birthtime.toUTCString() | ||
mode = mode.toString(8) | ||
size += 'B' | ||
return { | ||
file, | ||
dir, | ||
info: [birthtime, ino, mode, nlink, size], | ||
isDir: stats.isDirectory() | ||
} | ||
} | ||
|
||
function output({file, dir, info, isDir}) { | ||
write(file, ...info) | ||
if (!isDir) { return } | ||
const p = path.join(dir, file) | ||
write.arrow() | ||
fs.readdirSync(p).forEach((f) => { | ||
const stats = fs.statSync(path.join(p, f)) | ||
const style = stats.isDirectory() ? 'bold' : 'dim' | ||
write[style](f) | ||
}) | ||
write.newline() | ||
} | ||
|
||
print(process.argv[2] || '.') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
my edit |
Empty file.
Empty file.
Empty file.
1 change: 1 addition & 0 deletions
1
2-Coordinating-IO/source/fetching-meta-data/my-folder/my-symlink
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
my-file |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"name": "fetching-meta-data", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "meta.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"author": "David Mark Clements", | ||
"license": "MIT", | ||
"dependencies": { | ||
"tableaux": "^1.0.1" | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
2-Coordinating-IO/source/interfacing-with-standard-io/base64.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
'use strict' | ||
|
||
process.stdin.on('data', data => { | ||
process.stderr.write(`Converting: "${data}" to base64\n`) | ||
process.stdout.write(data.toString('base64') + '\n') | ||
}) |
4 changes: 4 additions & 0 deletions
4
2-Coordinating-IO/source/interfacing-with-standard-io/piping/base64.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
'use strict' | ||
|
||
const encode = require('base64-encode-stream') | ||
process.stdin.pipe(encode()).pipe(process.stdout) |
16 changes: 16 additions & 0 deletions
16
2-Coordinating-IO/source/interfacing-with-standard-io/piping/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"name": "piping", | ||
"version": "1.0.0", | ||
"main": "base64.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"author": "David Mark Clements", | ||
"license": "MIT", | ||
"dependencies": { | ||
"base64-encode-stream": "^1.0.0" | ||
}, | ||
"devDependencies": {}, | ||
"keywords": [], | ||
"description": "" | ||
} |
1 change: 1 addition & 0 deletions
1
2-Coordinating-IO/source/watching-files-and-directories/my-file.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
back again |
Oops, something went wrong.