-
항해 99 5기 TIL_93항해 99 2022. 4. 14. 00:36
▶ Today I Learned
<CS 공부>
[package.json이 꼭 필요할까요?]
꼭 필요하다. 왜냐하면 npm이 이를 통해 프로젝트를 식별하고 어떤 패키지를 의존하고 있는지 의존(dependency & devdependency)파일을 참고하고 종속처리를 해주기 때문이다.
package.json은 npm이라는 오픈소스 패키지 생태계를 사용하기 위한 명세이자, 프로젝트의 의존성 관리를 위한 명세, 또 이 생태계로의 배포를 위한 명세라고 볼 수 있다.
특정 패키지를 npm install을 통해 설치할 때 npm registry로 부터 그 패키지를 가져오는 것이며 이때 패키지에 대한 정보가 package.json에 들어있어야만 한다. 그렇지 않으면 npm에 있는 패키지를 설치하거나 npm에 패키지를 배포할 수 없다.
특히 package.json에서 name필드와 version필드는 꼭 있어야 한다.
그 외 여러 정보를 기술하며 devdependency는 개발 시 필요한 패키지들의 버전 범위를 나타내고 dependency는 실제 배포시 필요한 버전 범위들을 나타낸다. npm install을 통해 특정 패키지를 설치하게 되면 해당 정보는 dependency에 기록되고 devdependency에 기록하려면 "npm install -D 패키지명"이라는 형태로 설치하면 된다. (-D는 --save-dev 의 축약형)
참고자료:
https://www.daleseo.com/js-npm-publish/
https://docs.npmjs.com/about-the-public-npm-registry
[Node.js 동작원리]
Node.js는 이벤트 리스너에 등록해둔 콜백함수를 실행하는 방식으로 동작하는데 이를 이벤트 루프가 가능하게 해준다.
cf) 이벤트 루프: 이벤트에 따라 호출되는 콜백함수를 관리하는 것
이벤트 루프는 아래와 같은 단계를 거친다.
1) timer
setTimeOut(), setIntever() 같은 timer 함수들이 처리된다.
2) pending callbacks
이 단계에서는 다음 루프 반복으로 연기된 I/O 완료 결과가 큐에 담긴다. I/O 작업이 완료되면 다음번 루프에 이 단계에 들어와있게 되고, I/O 작업 블록내의 콜백함수들을 poll 단계의 큐로 넘겨준다. 또한 TCP 오류 같은 시스템 작업의 콜백을 실행한다.
3) idle, prepare
nodejs 내부 관리를 위해 사용된다.
4) poll
poll 단계에서는 I/O와 연관된 콜백(클로즈 콜백, 타이머로 스케줄링된 콜백,setImmediate()를 제외한 거의 모든 콜백)을 실행한다. 또한 timer 단계에서의 실행 시간 제어를 담당한다. 이 단계에서는 poll 큐에 쌓인 콜백함수들을 한도가 넘지 않을때까지 모두 동기적으로 실행한다. 만약 한도가 넘거나, 더이상 실행할 콜백함수가 없을때는 별도의 규칙을 따라, 다음 단계로 넘어가거나 대기한다.
- check 단계를 검사하여 setImmediate() 가 있는지 확인한다.
- check 단계에 setImmediate() 있는 경우에는 check 단계로 넘어간다.
- 만약 없다면, timer 단계에서 실행할 timer 함수가 있는지 확인한다.
- timer 함수를 실행할 수 있는 시간까지 대기한 후에, timer 단계로 넘어간다. 대기하는 동안에 poll 큐에 콜백함수가 쌓인다면 즉시 실행한다.
5) check
setImmediate() 의 콜백함수가 실행된다. 위에서 언급한대로, 이벤트루프가 poll 단계에서 작업을 수행한 뒤, poll 단계가 유휴상태가 되었다면 poll 이벤트를 기다리지 않고 check 단계로 넘어가게 된다.
6) close callbacks
close 이벤트에 따른 콜백함수를 실행한다. socket.on('close', ...) 이벤트에 따른 콜백함수를 예로 들 수 있다.해당 원리는 대부분 아래 블로그들에서 발췌한 것이고 아직 관련 개념들을 완전히 이해한 것은 아니다.
하지만 아래 블로그들을 모두 한 번씩 읽어보면 Node.js는 나름의 순서를 갖고 있는 이벤트 루프에 따라 동작하는 것 같다는 생각이 들 것이다. (물론 이것이 맞지 않을 수도 있다.)
참고자료:
https://www.uniever.space/263ffd78-794c-45a7-8279-ba2120b69044
▶ 느낀 점
부트캠프를 하는 동안엔 우선은 코드를 이용해서 구현하는 것들이 더 중점이었기에
아직 사용하는 툴의 작동 원리 등 익숙하지 않은 CS 지식들이 꽤 있다.
모든 것을 한 번에 알 수 없으니 틈틈이 사용해보고 조사해보며 점점 지식의 너비와 깊이를 늘려나가야겠다.
▶ 공부 시 참고 링크들
https://hahahoho5915.tistory.com/32
https://hijjang2.tistory.com/323
'항해 99' 카테고리의 다른 글
항해 99 5기 후기 (0) 2022.04.18 항해 99 5기 TIL_94 (0) 2022.04.18 항해 99 5기 TIL_92 (0) 2022.04.13 항해 99 5기 WIL_13 (0) 2022.04.11 항해 99 5기 TIL_91 (0) 2022.04.11