GithubHelp home page GithubHelp logo

huenchao / node-study Goto Github PK

View Code? Open in Web Editor NEW
8.0 8.0 0.0 139 KB

nodejs源码阅读,我看的源码是v12.0.0的,但是我在issue里收录了许多其他版本别人写的源码解析,大部分理念还是适用的。感谢这些无私的大佬为我们贡献高质量的文章!

JavaScript 100.00%

node-study's People

Contributors

huenchao avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

node-study's Issues

重启读书

现代操作系统 Modern Operating Systems

https://book.douban.com/subject/27096665/

操作系统概念 Operating System Concepts

https://book.douban.com/subject/30297919/

上学时读的第2本和操作系统有关的书,都是走马观花似的看了过去。

操作系统导论 Operating Systems-Three Easy Pieces

https://book.douban.com/subject/33463930/

非常好的书,不是简单的讲概念,而是有代码,可运行,可操作。如何比较pthread_create与fork的开销差异,如何测量进程context switch的开销,都是读了这本书才明白的,应该说读了这本书,才开始有点理解操作系统系统了。

深入理解计算机系统 Computer Systems: A Programmer's Perspective

https://book.douban.com/subject/26912767/

http://csapp.cs.cmu.edu/3e/labs.html

常读常新。这本书配套的lab,是最不能放过的。读着ffmpeg的代码,看着里面处理flv、h264 NALU时候的各种位运算,总会想起Data Lab;每当程序core了,一边查阅gdb官方文档,debuging with gdb,一边用gdb 调试coredump file的时候,总会想起当年被Bomb Lab支配的恐惧;还有Shell Lab,其实就是自己写一个简单的Shell,被各种fork exec signal waitpid折磨;Malloc Lab就是自己写一个简单的malloc,使用mmap获取一大块匿名内存,然后构建某种free list来管理内存,这里面有好多种算法,best fit、first fit、buddy system。能学习这些底层的知识,其实是很好的,因为后来会发现,计算机世界里的很多理论和技术,其实都是相通的,比如缓存更新里面有cache aside pattern,讲的是cache和memory,其实换成redis和MySQL也是可以的。

程序员的自我修养

https://book.douban.com/subject/3652388/

这本书给我的感觉,大概就是余读孔氏书,想见其为人。ELF文件里面有什么,.data .text .bss都是什么东西,静态库共享库有什么不一样,-fPIC这个编译选项又是什么意思,gcc抱怨 undefined reference of xxx, multiple definition of xxxx该怎么解决,如何在ELF文件里面加东西,如何写一个不依赖crt的程序。这本书大概就是内功心法,也许在工作中不会直接碰到,但也许某天进程莫名其妙core了,gdb thread apply all bt的时候,看到某些蛛丝马迹,这本书里面的知识突然涌上心头,疑难杂症也就解决了。不过这本书有一个缺点,就是出版时间比较早,例子都是32位系统下的,不知道还有没有机会出第二版,介绍64位系统下的相关知识呢。

Unix/Linux编程实践教程 Understanding UNIX/LINUX Programming

https://book.douban.com/subject/1219329/

Unix环境高级编程 Advanced Programming in the UNIX Environment

https://book.douban.com/subject/25900403/

Linux/Unix 系统编程手册The Linux Programming Interface: A Linux and UNIX System Programming Handbook

https://book.douban.com/subject/25809330/

学习C语言,一个很重要部分是学习系统编程。说起系统编程,大家耳熟能详的书籍就是传世经典《APUE》了,但是我个人认为这本书不适合入门,对新人不友好,最后只能拿出去垫鼠标了(逃)。我更喜欢《Unix/Linux编程实践教程》,这本书从常见的Linux命令入手,cp命令总用过吧,那我们就来写一个cp吧(笑),那么open read write 等system call自然也就会用了,cp读写之间总得有个buffer吧,buffer大小对cp的性能有什么影响呢,内核态 用户态的相关知识也就引出来了。这之后本书会介绍如何自己实现pwd、ls,如何自己写shell,如何自己写socket client、socket server。有了这本书打下的基础再去读《APUE》、《Linux/Unix 系统编程手册》,甚至自己man 2都很轻松了吧。

os

依赖c++和一些系统命令来做事情。。。

构建本地debug node的环境,我用cmake编译

  1. 下载源码
$ git clone https://github.com/nodejs/node.git
  1. 配置
$ ./configure --debug

如果出现:

Node configure: Found Python 3.7.2...
INFO: Using floating patch "tools/icu/patches/64/source/common/putil.cpp" from "tools/icu"
INFO: Using floating patch "tools/icu/patches/64/source/i18n/dtptngen.cpp" from "tools/icu"
No receipt for 'com.apple.pkg.DeveloperToolsCLILeo' found at '/'.
No receipt for 'com.apple.pkg.DeveloperToolsCLI' found at '/'.
gyp: No Xcode or CLT version detected!
Error running GYP

首先,你的py版本不对,nodejs需要用py2,你需要把py3.x都删了,然后执行下面的

$ sudo rm -rf /Library/Developer/CommandLineTools
$  xcode-select --install

如果上面还不行,就去看一下这个issue

$ python2 configure.py --debug
INFO: Using floating patch "tools/icu/patches/64/source/common/putil.cpp" from "tools/icu"
INFO: Using floating patch "tools/icu/patches/64/source/i18n/dtptngen.cpp" from "tools/icu"
INFO: configure completed successfully
  1. 只编译debug的版本(如果第二步你出错的话,你记得加上在下面命令前PYTHON=python2)
$ make -C out BUILDTYPE=Debug -j 4

测试markdown注释

The conditions for reading more data are (one of):

  • Not enough data buffered (state.length < state.highWaterMark). The loop is responsible for filling the buffer with enough data if such data is available. If highWaterMark is 0 and we are not in the flowing mode we should not attempt to buffer any extra data. We'll get more data when the stream consumer calls read() instead.
  • No data in the buffer, and the stream is in flowing mode. In this mode the loop below is responsible for ensuring read() is called. Failing to call read here would abort the flow and there's no other mechanism for continuing the flow if the stream consumer has just subscribed to the
    'data' event.

c++ addon相关

// hello.cc
#include <node.h>

namespace demo {

using v8::FunctionCallbackInfo;
using v8::Isolate;
using v8::Local;
using v8::NewStringType;
using v8::Object;
using v8::String;
using v8::Value;

void Method(const FunctionCallbackInfo<Value>& args) {
  Isolate* isolate = args.GetIsolate();
  args.GetReturnValue().Set(String::NewFromUtf8(
      isolate, "world", NewStringType::kNormal).ToLocalChecked());
}

void Initialize(Local<Object> exports) {
  NODE_SET_METHOD(exports, "hello", Method);
}

NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)

}  
// Used to be a macro, hence the uppercase name.
inline void NODE_SET_METHOD(v8::Local<v8::Object> recv,
                            const char* name,
                            v8::FunctionCallback callback) {
  v8::Isolate* isolate = v8::Isolate::GetCurrent();
  v8::HandleScope handle_scope(isolate);
  v8::Local<v8::Context> context = isolate->GetCurrentContext();
  v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(isolate,
                                                                callback);
  v8::Local<v8::Function> fn = t->GetFunction(context).ToLocalChecked();
  v8::Local<v8::String> fn_name = v8::String::NewFromUtf8(isolate, name,
      v8::NewStringType::kInternalized).ToLocalChecked();
  fn->SetName(fn_name);
  recv->Set(context, fn_name, fn).Check();
}

event

event的实现比较简单,就是在每个event对象上根据eventName和listener组成绑定关系。另外once的是通过一个中间的state对象,记录是否使用过,如果调用了,就删除它。

参考文章:LouisWT/Blog#2

pub priv

作者:永安在线反欺诈
链接:https://www.zhihu.com/question/33645891/answer/192604856
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

看一个小时候经常在《趣味数学》这类书里的一个数学小魔术:让对方任意想一个3位数,并把这个数和91相乘,然后告诉我积的最后三位数,我就可以猜出对方想的是什么数字啦!比如对方想的是123,那么对方就计算出123 * 91等于11193,并把结果的末三位193告诉我。看起来,这么做似乎损失了不少信息,让我没法反推出原来的数。不过,我仍然有办法:只需要把对方告诉我的结果再乘以11,乘积的末三位就是对方刚开始想的数了。可以验证一下,193 * 11 = 2123,末三位正是对方所想的秘密数字!其实道理很简单,91乘以11等于1001,而任何一个三位数乘以1001后,末三位显然都不变(例如123乘以1001就等于123123)。知道原理后,我们可以构造一个定义域和值域更大的加密解密系统。比方说,任意一个数乘以400000001后,末8位都不变,而400000001 = 19801 * 20201,于是你来乘以19801,我来乘以20201,又一个加密解密不对称的系统就构造好了。甚至可以构造得更大一些:4000000000000000000000000000001 = 1199481995446957 * 3334772856269093,这样我们就成功构造了一个30位的加密系统。这是一件非常coooooooool的事情,任何人都可以按照我公布的方法加密一个数,但是只有我才知道怎么把所得的密文变回去。其安全性就建立在算乘积非常容易,但是要把4000000000000000000000000000001分解成后面两个数相乘,在没有计算机的时代几乎不可能成功!但如果仅仅按照上面的思路,如果对方知道原理,知道我要构造出带很多0的数,根据19801和8位算法这2个条件非常容易穷举出400000001这个目标值。要解决这个问题,真实世界就不是使用乘法了,比如RSA算法使用的是指数和取模运算,但本质上就是上面这套**。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.