forked from ZeroCho/nodejs-book
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
120 changed files
with
11,445 additions
and
3,985 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
const odd = '짝수입니다'; | ||
const even = '홀수입니다'; | ||
const odd = '홀수입니다'; | ||
const even = '짝수입니다'; | ||
|
||
module.exports = { | ||
odd, | ||
|
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 @@ | ||
const { dep1 } = require('./dep1-1'); | ||
const { dep2 } = require('./dep2-1'); | ||
|
||
dep1(); | ||
dep2(); | ||
|
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,5 @@ | ||
const dep1 = require('./dep1'); | ||
const dep2 = require('./dep2'); | ||
|
||
dep1(); | ||
dep2(); |
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,5 @@ | ||
const dep2 = require('./dep2-1'); | ||
console.log('require dep2', dep2); | ||
exports.dep1 = () => { | ||
console.log('dep2', dep2); | ||
}; |
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,5 @@ | ||
const dep2 = require('./dep2'); | ||
console.log('require dep2', dep2); | ||
module.exports = () => { | ||
console.log('dep2', dep2); | ||
}; |
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,5 @@ | ||
const dep1 = require('./dep1-1'); | ||
console.log('require dep1', dep1); | ||
exports.dep2 = () => { | ||
console.log('dep1', dep1); | ||
}; |
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,5 @@ | ||
const dep1 = require('./dep1'); | ||
console.log('require dep1', dep1); | ||
module.exports = () => { | ||
console.log('dep1', dep1); | ||
}; |
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 |
---|---|---|
@@ -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); |
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,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()); | ||
}); // 실행 에러 |
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
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,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); | ||
} |
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 @@ | ||
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); |
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,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()); | ||
}); // 실행 에러 |
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 @@ | ||
print('hello python') |
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 @@ | ||
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); | ||
} |
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 @@ | ||
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(); | ||
}); | ||
} |
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
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,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); | ||
}); | ||
|
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,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); |
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,9 @@ | ||
const fs = require('fs').promises; | ||
|
||
fs.copyFile('readme4.txt', 'writeme4.txt') | ||
.then(() => { | ||
console.log('복사 완료'); | ||
}) | ||
.catch((error) => { | ||
console.error(error); | ||
}); |
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,7 @@ | ||
const fs = require('fs'); | ||
const file = fs.createWriteStream('./big.txt'); | ||
|
||
for (let i = 0; i <= 10000000; i++) { | ||
file.write('안녕하세요. 엄청나게 큰 파일을 만들어 볼 것입니다. 각오 단단히 하세요!\n'); | ||
} | ||
file.end(); |
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,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); | ||
}); |
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 @@ | ||
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); | ||
}); |
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 @@ | ||
const fs = require('fs').promises; | ||
|
||
fs.readFile('./readme.txt') | ||
.then((data) => { | ||
console.log(data); | ||
console.log(data.toString()); | ||
}) | ||
.catch((err) => { | ||
console.error(err); | ||
}); |
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 @@ | ||
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); | ||
}); |
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,5 @@ | ||
asdfa | ||
as | ||
asas | ||
asasdasdas | ||
asdasd |
Oops, something went wrong.