Skip to content

Commit

Permalink
Update: 개정판 초고
Browse files Browse the repository at this point in the history
  • Loading branch information
ZeroCho committed Dec 15, 2019
1 parent fe7b29e commit 51fc74d
Show file tree
Hide file tree
Showing 120 changed files with 11,445 additions and 3,985 deletions.
3 changes: 3 additions & 0 deletions ch2/2.1.6-1.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@ promise
})
.catch((error) => {
console.error(error); // 실패(reject)한 경우 실행
})
.finally(() => { // 끝나고 무조건 실행
console.log('무조건');
});
4 changes: 2 additions & 2 deletions ch3/3.3/var.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const odd = '짝수입니다';
const even = '홀수입니다';
const odd = '홀수입니다';
const even = '짝수입니다';

module.exports = {
odd,
Expand Down
6 changes: 6 additions & 0 deletions ch3/3.4/dep-run-1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const { dep1 } = require('./dep1-1');
const { dep2 } = require('./dep2-1');

dep1();
dep2();

5 changes: 5 additions & 0 deletions ch3/3.4/dep-run.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const dep1 = require('./dep1');
const dep2 = require('./dep2');

dep1();
dep2();
5 changes: 5 additions & 0 deletions ch3/3.4/dep1-1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const dep2 = require('./dep2-1');
console.log('require dep2', dep2);
exports.dep1 = () => {
console.log('dep2', dep2);
};
5 changes: 5 additions & 0 deletions ch3/3.4/dep1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const dep2 = require('./dep2');
console.log('require dep2', dep2);
module.exports = () => {
console.log('dep2', dep2);
};
5 changes: 5 additions & 0 deletions ch3/3.4/dep2-1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const dep1 = require('./dep1-1');
console.log('require dep1', dep1);
exports.dep2 = () => {
console.log('dep1', dep1);
};
5 changes: 5 additions & 0 deletions ch3/3.4/dep2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const dep1 = require('./dep1');
console.log('require dep1', dep1);
module.exports = () => {
console.log('dep1', dep1);
};
8 changes: 6 additions & 2 deletions ch3/3.5/cipher.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
const crypto = require('crypto');

const cipher = crypto.createCipher('aes-256-cbc', '열쇠');
const algorithm = 'aes-256-cbc';
const key = 'abcdefghijklmnopqrstuvwxyz123456';
const iv = '1234567890123456';

const cipher = crypto.createCipheriv(algorithm, key, iv);
let result = cipher.update('암호화 할 문장', 'utf8', 'base64');
result += cipher.final('base64');
console.log('암호화:', result);

const decipher = crypto.createDecipher('aes-256-cbc', '열쇠');
const decipher = crypto.createDecipheriv(algorithm, key, iv);
let result2 = decipher.update(result, 'base64', 'utf8');
result2 += decipher.final('utf8');
console.log('복호화:', result2);
11 changes: 11 additions & 0 deletions ch3/3.5/exec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const exec = require('child_process').exec;

var process = exec('dir');

process.stdout.on('data', function(data) {
console.log(data.toString());
}); // 실행 결과

process.stderr.on('data', function(data) {
console.error(data.toString());
}); // 실행 에러
6 changes: 3 additions & 3 deletions ch3/3.5/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ console.log('------------------------------');
console.log('path.dirname():', path.dirname(string));
console.log('path.extname():', path.extname(string));
console.log('path.basename():', path.basename(string));
console.log('path.basename():', path.basename(string, path.extname(string)));
console.log('path.basename - extname:', path.basename(string, path.extname(string)));
console.log('------------------------------');
console.log('path.parse()', path.parse(string));
console.log('path.format():', path.format({
Expand All @@ -18,8 +18,8 @@ console.log('path.format():', path.format({
}));
console.log('path.normalize():', path.normalize('C://users\\\\zerocho\\\path.js'));
console.log('------------------------------');
console.log('path.isAbsolute():', path.isAbsolute('C:\\'));
console.log('path.isAbsolute():', path.isAbsolute('./home'));
console.log('path.isAbsolute(C:\\):', path.isAbsolute('C:\\'));
console.log('path.isAbsolute(./home):', path.isAbsolute('./home'));
console.log('------------------------------');
console.log('path.relative():', path.relative('C:\\users\\zerocho\\path.js', 'C:\\'));
console.log('path.join():', path.join(__dirname, '..', '..', '/users', '.', '/zerocho'));
Expand Down
54 changes: 54 additions & 0 deletions ch3/3.5/prime-worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const { Worker, isMainThread, parentPort, workerData } = require('worker_threads');

const min = 2;
let primes = [];

function findPrimes(start, range) {
let isPrime = true;
const end = start + range;
for (let i = start; i < end; i++) {
for (let j = min; j < Math.sqrt(end); j++) {
if (i !== j && i % j === 0) {
isPrime = false;
break;
}
}
if (isPrime) {
primes.push(i);
}
isPrime = true;
}
}

if (isMainThread) {
const max = 10000000;
const threadCount = 8;
const threads = new Set();
const range = Math.ceil((max - min) / threadCount);
let start = min;
console.time('prime');
for (let i = 0; i < threadCount - 1; i++) {
const wStart = start;
threads.add(new Worker(__filename, { workerData: { start: wStart, range } }));
start += range;
}
threads.add(new Worker(__filename, { workerData: { start, range: range + ((max - min + 1) % threadCount) } }));
for (let worker of threads) {
worker.on('error', (err) => {
throw err;
});
worker.on('exit', () => {
threads.delete(worker);
if (threads.size === 0) {
console.timeEnd('prime');
console.log(primes.length);
}
});
worker.on('message', (msg) => {
primes = primes.concat(msg);
});
}
} else {
findPrimes(workerData.start, workerData.range);
parentPort.postMessage(primes);
}
25 changes: 25 additions & 0 deletions ch3/3.5/prime.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const min = 2;
const max = 10000000;
const primes = [];

function findPrimes(start, range) {
let isPrime = true;
const end = start + range;
for (let i = start; i < end; i++) {
for (let j = min; j < Math.sqrt(end); j++) {
if (i !== j && i % j === 0) {
isPrime = false;
break;
}
}
if (isPrime) {
primes.push(i);
}
isPrime = true;
}
}

console.time('prime');
findPrimes(min, max);
console.timeEnd('prime');
console.log(primes.length);
11 changes: 11 additions & 0 deletions ch3/3.5/spawn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const spawn = require('child_process').spawn;

var process = spawn('python', ['test.py']);

process.stdout.on('data', function(data) {
console.log(data.toString());
}); // 실행 결과

process.stderr.on('data', function(data) {
console.error(data.toString());
}); // 실행 에러
1 change: 1 addition & 0 deletions ch3/3.5/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print('hello python')
25 changes: 25 additions & 0 deletions ch3/3.5/worker_data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const {
Worker, isMainThread, parentPort, workerData,
} = require('worker_threads');

if (isMainThread) { // 부모일 때
const threads = new Set();
threads.add(new Worker(__filename, {
workerData: { start: 1 },
}));
threads.add(new Worker(__filename, {
workerData: { start: 2 },
}));
for (let worker of threads) {
worker.on('message', message => console.log('from worker', message));
worker.on('exit', () => {
threads.delete(worker);
if (threads.size === 0) {
console.log('job done');
}
});
}
} else { // 워커일 때
const data = workerData;
parentPort.postMessage(data.start + 100);
}
16 changes: 16 additions & 0 deletions ch3/3.5/worker_threads.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const {
Worker, isMainThread, parentPort,
} = require('worker_threads');

if (isMainThread) { // 부모일 때
const worker = new Worker(__filename);
worker.on('message', message => console.log('from worker', message));
worker.on('exit', () => console.log('worker exit'));
worker.postMessage('ping');
} else { // 워커일 때
parentPort.on('message', (value) => {
console.log('from parent', value);
parentPort.postMessage('pong');
parentPort.close();
});
}
2 changes: 1 addition & 1 deletion ch3/3.6/asyncOrder.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fs.readFile('./readme2.txt', (err, data) => {
throw err;
}
console.log('3번', data.toString());
console.log('끝');
});
});
});
console.log('끝');
19 changes: 19 additions & 0 deletions ch3/3.6/asyncOrderPromise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const fs = require('fs').promises;

console.log('시작');
fs.readFile('./readme2.txt')
.then((data) => {
return fs.readFile('./readme2.txt');
})
.then((data) => {
console.log('2번', data.toString());
return fs.readFile('./readme2.txt');
})
.then((data) => {
console.log('3번', data.toString());
console.log('끝');
})
.catch((err) => {
console.error(err);
});

7 changes: 7 additions & 0 deletions ch3/3.6/buffer-memory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const fs = require('fs');

console.log('before: ', process.memoryUsage().rss);

const data1 = fs.readFileSync('./big.txt');
fs.writeFileSync('./big2.txt', data1);
console.log('buffer: ', process.memoryUsage().rss);
9 changes: 9 additions & 0 deletions ch3/3.6/copyFilePromise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const fs = require('fs').promises;

fs.copyFile('readme4.txt', 'writeme4.txt')
.then(() => {
console.log('복사 완료');
})
.catch((error) => {
console.error(error);
});
7 changes: 7 additions & 0 deletions ch3/3.6/createBigFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const fs = require('fs');
const file = fs.createWriteStream('./big.txt');

for (let i = 0; i <= 10000000; i++) {
file.write('안녕하세요. 엄청나게 큰 파일을 만들어 볼 것입니다. 각오 단단히 하세요!\n');
}
file.end();
29 changes: 29 additions & 0 deletions ch3/3.6/fsCreatePromise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const fs = require('fs').promises;
const constants = require('fs').constants;

console.log(constants.F_OK, constants.R_OK, constants.W_OK);
fs.access('./folder', constants.F_OK | constants.W_OK | constants.R_OK)
.then(() => {
return Promise.reject('이미 폴더 있음');
})
.catch((err) => {
if (err.code === 'ENOENT') {
console.log('폴더 없음');
return fs.mkdir('./folder');
}
return Promise.reject(err);
})
.then(() => {
console.log('폴더 만들기 성공');
return fs.open('./folder/file.js', 'w');
})
.then((fd) => {
console.log('빈 파일 만들기 성공', fd);
fs.rename('./folder/file.js', './folder/newfile.js');
})
.then(() => {
console.log('이름 바꾸기 성공');
})
.catch((err) => {
console.error(err);
});
17 changes: 17 additions & 0 deletions ch3/3.6/fsDeletePromise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const fs = require('fs').promises;

fs.readdir('./folder')
.then((dir) => {
console.log('폴더 내용 확인', dir);
return fs.unlink('./folder/newFile.js');
})
.then(() => {
console.log('파일 삭제 성공');
return fs.rmdir('./folder');
})
.then(() => {
console.log('폴더 삭제 성공');
})
.catch((err) => {
console.error(err);
});
10 changes: 10 additions & 0 deletions ch3/3.6/readFilePromise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const fs = require('fs').promises;

fs.readFile('./readme.txt')
.then((data) => {
console.log(data);
console.log(data.toString());
})
.catch((err) => {
console.error(err);
});
10 changes: 10 additions & 0 deletions ch3/3.6/stream-memory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const fs = require('fs');

console.log('before: ', process.memoryUsage().rss);

const readStream = fs.createReadStream('./big.txt');
const writeStream = fs.createWriteStream('./big3.txt');
readStream.pipe(writeStream);
readStream.on('end', () => {
console.log('stream: ', process.memoryUsage().rss);
});
5 changes: 5 additions & 0 deletions ch3/3.6/target.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
asdfa
as
asas
asasdasdas
asdasd
Loading

0 comments on commit 51fc74d

Please sign in to comment.