Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chapter 2 Coordinating IO #2

Merged
merged 8 commits into from
May 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added 2-Coordinating-IO.pdf
Binary file not shown.
1,452 changes: 1,452 additions & 0 deletions 2-Coordinating-IO/content.md

Large diffs are not rendered by default.

Binary file added 2-Coordinating-IO/images/fig1.1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 2-Coordinating-IO/images/fig1.2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 2-Coordinating-IO/images/fig1.3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 2-Coordinating-IO/images/fig1.4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 2-Coordinating-IO/images/fig1.5.png
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 2-Coordinating-IO/source/communicating-over-sockets/client.js
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')
})
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')
})
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)
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)
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 2-Coordinating-IO/source/communicating-over-sockets/server.js
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')
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())
})
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)
})
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')
})
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')
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)
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] || '.')
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
my edit
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')
}

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')
}
51 changes: 51 additions & 0 deletions 2-Coordinating-IO/source/fetching-meta-data/meta.js
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] || '.')
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
my edit
Empty file.
14 changes: 14 additions & 0 deletions 2-Coordinating-IO/source/fetching-meta-data/package.json
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"
}
}
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')
})
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)
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": ""
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
back again
Loading