@@ -430,7 +467,7 @@ - 67 + 68 日志 @@ -447,7 +484,7 @@ - 167 + 168 标签 diff --git a/archives/index.html b/archives/index.html index 19c0d57b..cee4e458 100644 --- a/archives/index.html +++ b/archives/index.html @@ -230,7 +230,7 @@ - 好! 目前共计 67 篇日志。 继续努力。 + 好! 目前共计 68 篇日志。 继续努力。 @@ -249,6 +249,43 @@ 2018 + + + + + + + + Buffer 简介 + + + + + + + + 10-07 + + + + + + + + + + + + + + + + + + + @@ -948,48 +985,6 @@ - - - - - - - - 2016 - - - - - - - - - - - - - - 2016年总结 - - - - - - - - 12-31 - - - - - - - - - - @@ -1036,7 +1031,7 @@ - 67 + 68 日志 @@ -1053,7 +1048,7 @@ - 167 + 168 标签 diff --git a/archives/page/2/index.html b/archives/page/2/index.html index aeda331e..c7f34fff 100644 --- a/archives/page/2/index.html +++ b/archives/page/2/index.html @@ -230,7 +230,7 @@ - 好! 目前共计 67 篇日志。 继续努力。 + 好! 目前共计 68 篇日志。 继续努力。 @@ -249,6 +249,43 @@ 2016 + + + + + + + + 2016年总结 + + + + + + + + 12-31 + + + + + + + + + + + + + + + + + + + @@ -943,43 +980,6 @@ - - - - - - - - - - - - - - - - - 解决IIS中MIME类型方面的404错误 - - - - - - - - 09-19 - - - - - - - - - - @@ -1026,7 +1026,7 @@ - 67 + 68 日志 @@ -1043,7 +1043,7 @@ - 167 + 168 标签 diff --git a/archives/page/3/index.html b/archives/page/3/index.html index 45fcadb1..ad67e6bd 100644 --- a/archives/page/3/index.html +++ b/archives/page/3/index.html @@ -230,7 +230,7 @@ - 好! 目前共计 67 篇日志。 继续努力。 + 好! 目前共计 68 篇日志。 继续努力。 @@ -249,6 +249,43 @@ 2016 + + + + + + + + 解决IIS中MIME类型方面的404错误 + + + + + + + + 09-19 + + + + + + + + + + + + + + + + + + + @@ -943,43 +980,6 @@ - - - - - - - - - - - - - - - - - markdown语法说明 - - - - - - - - 06-17 - - - - - - - - - - @@ -1026,7 +1026,7 @@ - 67 + 68 日志 @@ -1043,7 +1043,7 @@ - 167 + 168 标签 diff --git a/archives/page/4/index.html b/archives/page/4/index.html index 9999b59d..cd11b332 100644 --- a/archives/page/4/index.html +++ b/archives/page/4/index.html @@ -230,7 +230,7 @@ - 好! 目前共计 67 篇日志。 继续努力。 + 好! 目前共计 68 篇日志。 继续努力。 @@ -249,6 +249,43 @@ 2016 + + + + + + + + markdown语法说明 + + + + + + + + 06-17 + + + + + + + + + + + + + + + + + + + @@ -545,7 +582,7 @@ - 67 + 68 日志 @@ -562,7 +599,7 @@ - 167 + 168 标签 diff --git a/article/10-educationists.html b/article/10-educationists.html index ca44fbe1..a804be44 100644 --- a/article/10-educationists.html +++ b/article/10-educationists.html @@ -473,7 +473,7 @@ - 67 + 68 日志 @@ -490,7 +490,7 @@ - 167 + 168 标签 diff --git a/article/a-business-trip-to-shenzhen.html b/article/a-business-trip-to-shenzhen.html index c2466445..3f0e0a38 100644 --- a/article/a-business-trip-to-shenzhen.html +++ b/article/a-business-trip-to-shenzhen.html @@ -447,7 +447,7 @@ - 67 + 68 日志 @@ -464,7 +464,7 @@ - 167 + 168 标签 diff --git a/article/a-fragment-code-of-multi-line-string-trim.html b/article/a-fragment-code-of-multi-line-string-trim.html index 8db3c37d..43d2fcae 100644 --- a/article/a-fragment-code-of-multi-line-string-trim.html +++ b/article/a-fragment-code-of-multi-line-string-trim.html @@ -430,7 +430,7 @@ - 67 + 68 日志 @@ -447,7 +447,7 @@ - 167 + 168 标签 diff --git a/article/a-logo-note-for-zte-fpga-design-department.html b/article/a-logo-note-for-zte-fpga-design-department.html index 55ebe7e1..1c5edf83 100644 --- a/article/a-logo-note-for-zte-fpga-design-department.html +++ b/article/a-logo-note-for-zte-fpga-design-department.html @@ -442,7 +442,7 @@ - 67 + 68 日志 @@ -459,7 +459,7 @@ - 167 + 168 标签 diff --git a/article/a-responsive-menu.html b/article/a-responsive-menu.html index 51fd973d..7720eba6 100644 --- a/article/a-responsive-menu.html +++ b/article/a-responsive-menu.html @@ -457,7 +457,7 @@ - 67 + 68 日志 @@ -474,7 +474,7 @@ - 167 + 168 标签 diff --git a/article/a-trap-about-object.html b/article/a-trap-about-object.html index 1c34df46..9b97690f 100644 --- a/article/a-trap-about-object.html +++ b/article/a-trap-about-object.html @@ -431,7 +431,7 @@ - 67 + 68 日志 @@ -448,7 +448,7 @@ - 167 + 168 标签 diff --git a/article/abbreviation.html b/article/abbreviation.html index c5f15a84..2c03b673 100644 --- a/article/abbreviation.html +++ b/article/abbreviation.html @@ -467,7 +467,7 @@ - 67 + 68 日志 @@ -484,7 +484,7 @@ - 167 + 168 标签 diff --git a/article/angular-function.html b/article/angular-function.html index bd572275..4ccc7964 100644 --- a/article/angular-function.html +++ b/article/angular-function.html @@ -476,7 +476,7 @@ - 67 + 68 日志 @@ -493,7 +493,7 @@ - 167 + 168 标签 diff --git a/article/audit-time-of-beian.html b/article/audit-time-of-beian.html index a516e61e..a1057143 100644 --- a/article/audit-time-of-beian.html +++ b/article/audit-time-of-beian.html @@ -558,7 +558,7 @@ - 67 + 68 日志 @@ -575,7 +575,7 @@ - 167 + 168 标签 diff --git a/article/book-review-about-the-bio-of-du-yue-sheng.html b/article/book-review-about-the-bio-of-du-yue-sheng.html index 7113f1c6..31256b2d 100644 --- a/article/book-review-about-the-bio-of-du-yue-sheng.html +++ b/article/book-review-about-the-bio-of-du-yue-sheng.html @@ -449,7 +449,7 @@ - 67 + 68 日志 @@ -466,7 +466,7 @@ - 167 + 168 标签 diff --git a/article/brackets-emmet.html b/article/brackets-emmet.html index d63d4a93..dbc596ff 100644 --- a/article/brackets-emmet.html +++ b/article/brackets-emmet.html @@ -458,7 +458,7 @@ - 67 + 68 日志 @@ -475,7 +475,7 @@ - 167 + 168 标签 diff --git a/article/code-golf-output-fit-numbers.html b/article/code-golf-output-fit-numbers.html index 7d682443..e383b146 100644 --- a/article/code-golf-output-fit-numbers.html +++ b/article/code-golf-output-fit-numbers.html @@ -485,7 +485,7 @@ 后记 - 67 + 68 日志 @@ -502,7 +502,7 @@ 后记 - 167 + 168 标签 diff --git a/article/css-animation.html b/article/css-animation.html index 35b11ee0..ba3a0c72 100644 --- a/article/css-animation.html +++ b/article/css-animation.html @@ -480,7 +480,7 @@ - 67 + 68 日志 @@ -497,7 +497,7 @@ - 167 + 168 标签 diff --git a/article/css-flex.html b/article/css-flex.html index bc6dad82..633866fc 100644 --- a/article/css-flex.html +++ b/article/css-flex.html @@ -513,7 +513,7 @@ - 67 + 68 日志 @@ -530,7 +530,7 @@ - 167 + 168 标签 diff --git a/article/css-specificity.html b/article/css-specificity.html index 7cd9651d..07f5d493 100644 --- a/article/css-specificity.html +++ b/article/css-specificity.html @@ -42,7 +42,7 @@ - + @@ -61,7 +61,7 @@ - + @@ -467,10 +467,10 @@ - #前端 - #css + #前端 + #specificity @@ -558,7 +558,7 @@ - 67 + 68 日志 @@ -575,7 +575,7 @@ - 167 + 168 标签 diff --git a/article/dom-nodetype.html b/article/dom-nodetype.html index f0e17e78..e910a0b4 100644 --- a/article/dom-nodetype.html +++ b/article/dom-nodetype.html @@ -447,7 +447,7 @@ 示例 - 67 + 68 日志 @@ -464,7 +464,7 @@ 示例 - 167 + 168 标签 diff --git a/article/double-eleven-an-ironic-day.html b/article/double-eleven-an-ironic-day.html index c07241d8..a118da09 100644 --- a/article/double-eleven-an-ironic-day.html +++ b/article/double-eleven-an-ironic-day.html @@ -441,7 +441,7 @@ - 67 + 68 日志 @@ -458,7 +458,7 @@ - 167 + 168 标签 diff --git a/article/equal-loan-payment.html b/article/equal-loan-payment.html index 8121135f..542667bf 100644 --- a/article/equal-loan-payment.html +++ b/article/equal-loan-payment.html @@ -446,7 +446,7 @@ - 67 + 68 日志 @@ -463,7 +463,7 @@ - 167 + 168 标签 diff --git a/article/es6-modules.html b/article/es6-modules.html index fc7b7d51..618bab5f 100644 --- a/article/es6-modules.html +++ b/article/es6-modules.html @@ -68,7 +68,7 @@ - + @@ -396,6 +396,10 @@ + + Buffer 简介 + + @@ -462,7 +466,7 @@ - 67 + 68 日志 @@ -479,7 +483,7 @@ - 167 + 168 标签 diff --git a/article/es8.html b/article/es8.html index 8ddeca51..800d97e1 100644 --- a/article/es8.html +++ b/article/es8.html @@ -469,7 +469,7 @@ - 67 + 68 日志 @@ -486,7 +486,7 @@ - 167 + 168 标签 diff --git a/article/excel-even-odd-color.html b/article/excel-even-odd-color.html index fc054f40..1bed15c1 100644 --- a/article/excel-even-odd-color.html +++ b/article/excel-even-odd-color.html @@ -422,7 +422,7 @@ - 67 + 68 日志 @@ -439,7 +439,7 @@ - 167 + 168 标签 diff --git a/article/fix-foxmail-receive-password-error.html b/article/fix-foxmail-receive-password-error.html index f0052291..bbc918f2 100644 --- a/article/fix-foxmail-receive-password-error.html +++ b/article/fix-foxmail-receive-password-error.html @@ -463,7 +463,7 @@ - 67 + 68 日志 @@ -480,7 +480,7 @@ - 167 + 168 标签 diff --git a/article/flex-compability.html b/article/flex-compability.html index 5a66afe8..d1c906b1 100644 --- a/article/flex-compability.html +++ b/article/flex-compability.html @@ -440,7 +440,7 @@ - 67 + 68 日志 @@ -457,7 +457,7 @@ - 167 + 168 标签 diff --git a/article/gulp-4.html b/article/gulp-4.html index 3446433b..992ad623 100644 --- a/article/gulp-4.html +++ b/article/gulp-4.html @@ -435,7 +435,7 @@ - 67 + 68 日志 @@ -452,7 +452,7 @@ - 167 + 168 标签 diff --git a/article/how-technology-hijacks-poeples-minds.html b/article/how-technology-hijacks-poeples-minds.html index baaeb30d..62d86476 100644 --- a/article/how-technology-hijacks-poeples-minds.html +++ b/article/how-technology-hijacks-poeples-minds.html @@ -539,7 +539,7 @@ - 67 + 68 日志 @@ -556,7 +556,7 @@ - 167 + 168 标签 diff --git a/article/htree.html b/article/htree.html index 05e13d32..7af8468e 100644 --- a/article/htree.html +++ b/article/htree.html @@ -42,7 +42,7 @@ - + @@ -61,7 +61,7 @@ - + @@ -383,12 +383,12 @@ #npm - #node - #tree #htree + #node + #CLI @@ -476,7 +476,7 @@ - 67 + 68 日志 @@ -493,7 +493,7 @@ - 167 + 168 标签 diff --git a/article/http-status-code.html b/article/http-status-code.html index 6145765a..0ac7d3bf 100644 --- a/article/http-status-code.html +++ b/article/http-status-code.html @@ -1339,7 +1339,7 @@ - 67 + 68 日志 @@ -1356,7 +1356,7 @@ - 167 + 168 标签 diff --git a/article/i-am-lost-in-the-colorful-world-2.html b/article/i-am-lost-in-the-colorful-world-2.html index 65a5f5aa..8ead7f76 100644 --- a/article/i-am-lost-in-the-colorful-world-2.html +++ b/article/i-am-lost-in-the-colorful-world-2.html @@ -428,7 +428,7 @@ - 67 + 68 日志 @@ -445,7 +445,7 @@ - 167 + 168 标签 diff --git a/article/i-am-lost-in-the-colorful-world-3.html b/article/i-am-lost-in-the-colorful-world-3.html index bdcf24f2..8de63480 100644 --- a/article/i-am-lost-in-the-colorful-world-3.html +++ b/article/i-am-lost-in-the-colorful-world-3.html @@ -421,7 +421,7 @@ - 67 + 68 日志 @@ -438,7 +438,7 @@ - 167 + 168 标签 diff --git a/article/i-am-lost-in-the-colorful-world.html b/article/i-am-lost-in-the-colorful-world.html index 049c7052..59b4b5fa 100644 --- a/article/i-am-lost-in-the-colorful-world.html +++ b/article/i-am-lost-in-the-colorful-world.html @@ -428,7 +428,7 @@ - 67 + 68 日志 @@ -445,7 +445,7 @@ - 167 + 168 标签 diff --git a/article/iis-404-mime-type.html b/article/iis-404-mime-type.html index 00848e6d..749c091c 100644 --- a/article/iis-404-mime-type.html +++ b/article/iis-404-mime-type.html @@ -428,7 +428,7 @@ - 67 + 68 日志 @@ -445,7 +445,7 @@ - 167 + 168 标签 diff --git a/article/introduction-to-jade.html b/article/introduction-to-jade.html index 57a6a335..0065d2db 100644 --- a/article/introduction-to-jade.html +++ b/article/introduction-to-jade.html @@ -458,7 +458,7 @@ - 67 + 68 日志 @@ -475,7 +475,7 @@ - 167 + 168 标签 diff --git a/article/javascript-semicolons.html b/article/javascript-semicolons.html index 9ff96135..6e0aa6c9 100644 --- a/article/javascript-semicolons.html +++ b/article/javascript-semicolons.html @@ -618,7 +618,7 @@ - 67 + 68 日志 @@ -635,7 +635,7 @@ - 167 + 168 标签 diff --git a/article/js-operator-precedence.html b/article/js-operator-precedence.html index 53fe885f..13073dca 100644 --- a/article/js-operator-precedence.html +++ b/article/js-operator-precedence.html @@ -826,7 +826,7 @@ - 67 + 68 日志 @@ -843,7 +843,7 @@ - 167 + 168 标签 diff --git a/article/language-tags-in-html.html b/article/language-tags-in-html.html index 5e747fc8..3dd8d288 100644 --- a/article/language-tags-in-html.html +++ b/article/language-tags-in-html.html @@ -549,7 +549,7 @@ - 67 + 68 日志 @@ -566,7 +566,7 @@ - 167 + 168 标签 diff --git a/article/limitation-of-wechat-weapp.html b/article/limitation-of-wechat-weapp.html index 8f63c04a..6be76992 100644 --- a/article/limitation-of-wechat-weapp.html +++ b/article/limitation-of-wechat-weapp.html @@ -436,7 +436,7 @@ - 67 + 68 日志 @@ -453,7 +453,7 @@ - 167 + 168 标签 diff --git a/article/markdown-syntax.html b/article/markdown-syntax.html index 6a81110c..09a3c6b9 100644 --- a/article/markdown-syntax.html +++ b/article/markdown-syntax.html @@ -693,7 +693,7 @@ - 67 + 68 日志 @@ -710,7 +710,7 @@ - 167 + 168 标签 diff --git a/article/mathjax-advanced.html b/article/mathjax-advanced.html index d0c38eb9..d7475d24 100644 --- a/article/mathjax-advanced.html +++ b/article/mathjax-advanced.html @@ -591,7 +591,7 @@ 高亮 - 67 + 68 日志 @@ -608,7 +608,7 @@ 高亮 - 167 + 168 标签 diff --git a/article/mathjax-basic-tutorial-and-quick-reference.html b/article/mathjax-basic-tutorial-and-quick-reference.html index 1cede7fb..7fd56ba4 100644 --- a/article/mathjax-basic-tutorial-and-quick-reference.html +++ b/article/mathjax-basic-tutorial-and-quick-reference.html @@ -672,7 +672,7 @@ - 67 + 68 日志 @@ -689,7 +689,7 @@ - 167 + 168 标签 diff --git a/article/minify-static-html-files.html b/article/minify-static-html-files.html index baed98b5..455a0761 100644 --- a/article/minify-static-html-files.html +++ b/article/minify-static-html-files.html @@ -442,7 +442,7 @@ - 67 + 68 日志 @@ -459,7 +459,7 @@ - 167 + 168 标签 diff --git a/article/misunderstood-songs-for-mother.html b/article/misunderstood-songs-for-mother.html index c950b218..b8c0b83e 100644 --- a/article/misunderstood-songs-for-mother.html +++ b/article/misunderstood-songs-for-mother.html @@ -432,7 +432,7 @@ - 67 + 68 日志 @@ -449,7 +449,7 @@ - 167 + 168 标签 diff --git a/article/naming-rule.html b/article/naming-rule.html index dbf60b37..08372ebc 100644 --- a/article/naming-rule.html +++ b/article/naming-rule.html @@ -450,7 +450,7 @@ 总结 - 67 + 68 日志 @@ -467,7 +467,7 @@ 总结 - 167 + 168 标签 diff --git a/article/node-buffer.html b/article/node-buffer.html new file mode 100644 index 00000000..5dcff822 --- /dev/null +++ b/article/node-buffer.html @@ -0,0 +1,663 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Buffer 简介 | 清风轩 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 清风轩 + + + + 清风轩居 - 引仙阁 + + + + + + + + + + + + + + + + + + + + + + + 首页 + + + + + + + + + + 分类 + + + + + + + + + + 标签 + + + + + + + + + + 归档 + + + + + + + + + + 关于 + + + + + + + + + + 花园 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Buffer 简介 + + + + + + + + + + + 发表于 + + 2018-10-07 + + + + + + | + + + + 分类于 + + + + Node + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Buffer 是 node 中的一个全局对象,用来读取或者操作二进制数据流。 + + +本文直接使用 Buffer 讲述这个对象,该对象翻译成中文,可以叫做“缓冲区对象”。 + +官方文档的介绍如下: + +Prior to the introduction of TypedArray, the JavaScript language had no mechanism for reading or manipulating streams of binary data. The Buffer class was introduced as part of the Node.js API to enable interaction with octet streams in TCP streams, file system operations, and other contexts. + +在 ECMAScript 2015 引入 TypedArray 之后,Buffer 类使用了一种更优化、更适合 Node.js 用例的方式实现了 Uint8Array API。 +Buffer 类可以视作一个跟 Array 类一样的对象,区别在于 Buffer 的大小是固定的,且在 v8 堆外分配物理内存。Buffer 的大小在创建时就确定了,无法调整。 +先来看一段官方文档提供的示例代码: +1234567891011121314151617181920// Creates a zero-filled Buffer of length 10.const buf1 = Buffer.alloc(10);// Creates a Buffer of length 10, filled with 0x1.const buf2 = Buffer.alloc(10, 1);// Creates an uninitialized buffer of length 10.// This is faster than calling Buffer.alloc() but the returned// Buffer instance might contain old data that needs to be// overwritten using either fill() or write().const buf3 = Buffer.allocUnsafe(10);// Creates a Buffer containing [0x1, 0x2, 0x3].const buf4 = Buffer.from([1, 2, 3]);// Creates a Buffer containing UTF-8 bytes [0x74, 0xc3, 0xa9, 0x73, 0x74].const buf5 = Buffer.from('tést');// Creates a Buffer containing Latin-1 bytes [0x74, 0xe9, 0x73, 0x74].const buf6 = Buffer.from('tést', 'latin1'); + +Buffer 实例一般用于表示编码字符的序列,用过指定字符编码,可以在 Buffer 实例与常规 JavaScript 字符串之间进行相互转换。 +这样的描述,可能太过于抽象,直接上代码解释吧: +123456789function btoa(str) { const buf = Buffer.from(str); return buf.toString('base64');}function atob(str) { const buf = Buffer.from(str, 'base64'); return buf.toString();} +上面的代码,是 node 环境下关于浏览器 atob 和 btoa 方法的实现。 +Node.js 目前支持的字符编码如下: + +ascii,仅支持 7 位 ASCII 数据。utf8,多字节编码的 Unicode 字符。许多网页和其他文档格式都使用 UTF-8。utf16le,2 或 4 字节,little-endian(小字节序)编码的 Unicode 字符。支持 U+10000 到 U+10FFFF 的代理对。usc2,即 utf16le。base64,即 Base64 编码。latin1,即 Latin-1 编码,具体由 IANA 在 RFC1345 中定义。binary,即 latin1。hex,将每个字节编码为两个十六进制字符。 + +注意,现代浏览器遵循 WHATWG 编码标准 将 latin1 和 ISO-8859-1 别名为 win-1252。这意味着在进行例如 http.get() 获取到的字符编码在规范列表中,服务器可能返回 win-1252 编码的数据,此时如果使用 latin1 字符编码,可能会得到错误的解码数据。 + +Buffer 对象可以使用 for...of 进行内部迭代。同样的,实例对象的 .values、.keys 和 .entries 方法都可以创建迭代。 +使用 new 操作符实例化一个 Buffer 的方法已经被弃用。注意,已经弃用的方法本文不做介绍。 +创建 Buffer 实例的方法有下面几个: + +Buffer.from(array),从数组进行创建。Buffer.from(arrayBuffer[, byteOffset[, length]]),从 arrayBuffer 的实例进行创建。Buffer.from(buffer),直接根据 buffer 创建。Buffer.from(string[, encoding]),根据字符串创建,encoding 即使用的编码方式。Buffer.from(object[, offsetOrEncoding[, length]]),根据对象创建,该对象需要支持 Symbol.toPrimitive 或者有 valueOf() 方法,比如 Buffer.from(new String('this is a test'));。Buffer.alloc(size[, fill[, encoding]]),创建一个指定长度的对象,fill 表示用来填充新建的 Buffer 的值,默认为 0。Buffer.allocUnsafe(size),分配一个大小为 size 的对象。该方式创建的 Buffer 对象是未经过初始化的,内容未知,可能包含敏感数据。 + +更多的创建方式不在本文的记述范围之内。 + +创建之后的实例,我们使用 buf 进行表示,buf 的特性跟数组类似。 +buf.buffer 指向了 Buffer 底层的 ArrayBuffer 对象。 +可以使用 fill 方法进行填充,语法为 buf.fill(value[, offset[, end]][, encoding])。 +toString 可以将 buf 转为一个常规字符串,语法为 buf.toString([encoding[, start[, end]]])。 +之前做了一个简单的获取网页源代码的方法,代码大概是这样的: +12345678910111213141516171819202122const http = require('http');const https = require('https');module.exports = function getHTML(url) { return new Promise(function (resolve, reject) { (/^https/i.test(url) ? https : http).get(url, function (res) { var html = ''; res.on('data', function (chunk) { // chunk is a Buffer instance, use the method toString to get the string html += chunk; }); res.on('end', function () { resolve(html); }); }).on('error', function () { reject(); }); });} +是的,这里面的 chunk 对象就是一个 Buffer 实例。 + +真·简介。本文到此就结束了。 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/article/node-wxpay.html b/article/node-wxpay.html index 2b2f2c31..4b4fe087 100644 --- a/article/node-wxpay.html +++ b/article/node-wxpay.html @@ -477,7 +477,7 @@ - 67 + 68 日志 @@ -494,7 +494,7 @@ - 167 + 168 标签 diff --git a/article/normalize-cssreset.html b/article/normalize-cssreset.html index 642b1af6..600d2225 100644 --- a/article/normalize-cssreset.html +++ b/article/normalize-cssreset.html @@ -42,7 +42,7 @@ - + @@ -61,7 +61,7 @@ - + @@ -361,10 +361,10 @@ - #css - #normalize + #css + @@ -446,7 +446,7 @@ - 67 + 68 日志 @@ -463,7 +463,7 @@ - 167 + 168 标签 diff --git a/article/notes-for-git.html b/article/notes-for-git.html index 08c7eb6d..c79774f8 100644 --- a/article/notes-for-git.html +++ b/article/notes-for-git.html @@ -436,7 +436,7 @@ - 67 + 68 日志 @@ -453,7 +453,7 @@ - 167 + 168 标签 diff --git a/article/npm-alias.html b/article/npm-alias.html index 8205762d..d8bc7401 100644 --- a/article/npm-alias.html +++ b/article/npm-alias.html @@ -442,7 +442,7 @@ -B - 67 + 68 日志 @@ -459,7 +459,7 @@ -B - 167 + 168 标签 diff --git a/article/object-property-accessor.html b/article/object-property-accessor.html index a551d9ac..c133a148 100644 --- a/article/object-property-accessor.html +++ b/article/object-property-accessor.html @@ -432,7 +432,7 @@ - 67 + 68 日志 @@ -449,7 +449,7 @@ - 167 + 168 标签 diff --git a/article/parcel-bundler.html b/article/parcel-bundler.html index a2934ce8..2b84a87b 100644 --- a/article/parcel-bundler.html +++ b/article/parcel-bundler.html @@ -492,7 +492,7 @@ Licen - 67 + 68 日志 @@ -509,7 +509,7 @@ Licen - 167 + 168 标签 diff --git a/article/react-or-vue-which-javascript-ui-library-should-you-be-using.html b/article/react-or-vue-which-javascript-ui-library-should-you-be-using.html index eccde3bf..dfc01389 100644 --- a/article/react-or-vue-which-javascript-ui-library-should-you-be-using.html +++ b/article/react-or-vue-which-javascript-ui-library-should-you-be-using.html @@ -537,7 +537,7 @@ - 67 + 68 日志 @@ -554,7 +554,7 @@ - 167 + 168 标签 diff --git a/article/record-to-two-special-weddings.html b/article/record-to-two-special-weddings.html index 3b052ffc..c50df8e1 100644 --- a/article/record-to-two-special-weddings.html +++ b/article/record-to-two-special-weddings.html @@ -453,7 +453,7 @@ - 67 + 68 日志 @@ -470,7 +470,7 @@ - 167 + 168 标签 diff --git a/article/regexp-assertion.html b/article/regexp-assertion.html index dc3946cc..75ee03cd 100644 --- a/article/regexp-assertion.html +++ b/article/regexp-assertion.html @@ -457,7 +457,7 @@ - 67 + 68 日志 @@ -474,7 +474,7 @@ - 167 + 168 标签 diff --git a/article/rfc2616-is-obsoleted.html b/article/rfc2616-is-obsoleted.html index 33979d02..b0e46b26 100644 --- a/article/rfc2616-is-obsoleted.html +++ b/article/rfc2616-is-obsoleted.html @@ -459,7 +459,7 @@
@@ -948,48 +985,6 @@ - - - - - - - - 2016 - - - - - - - - - - - - - - 2016年总结 - - - - - - - - 12-31 - - - - - - - - - - @@ -1036,7 +1031,7 @@ - 67 + 68 日志 @@ -1053,7 +1048,7 @@ - 167 + 168 标签 diff --git a/archives/page/2/index.html b/archives/page/2/index.html index aeda331e..c7f34fff 100644 --- a/archives/page/2/index.html +++ b/archives/page/2/index.html @@ -230,7 +230,7 @@ - 好! 目前共计 67 篇日志。 继续努力。 + 好! 目前共计 68 篇日志。 继续努力。 @@ -249,6 +249,43 @@ 2016 + + + + + + + + 2016年总结 + + + + + + + + 12-31 + + + + + + + + + + + + + + + + + + + @@ -943,43 +980,6 @@ - - - - - - - - - - - - - - - - - 解决IIS中MIME类型方面的404错误 - - - - - - - - 09-19 - - - - - - - - - - @@ -1026,7 +1026,7 @@ - 67 + 68 日志 @@ -1043,7 +1043,7 @@ - 167 + 168 标签 diff --git a/archives/page/3/index.html b/archives/page/3/index.html index 45fcadb1..ad67e6bd 100644 --- a/archives/page/3/index.html +++ b/archives/page/3/index.html @@ -230,7 +230,7 @@ - 好! 目前共计 67 篇日志。 继续努力。 + 好! 目前共计 68 篇日志。 继续努力。 @@ -249,6 +249,43 @@ 2016 + + + + + + + + 解决IIS中MIME类型方面的404错误 + + + + + + + + 09-19 + + + + + + + + + + + + + + + + + + + @@ -943,43 +980,6 @@ - - - - - - - - - - - - - - - - - markdown语法说明 - - - - - - - - 06-17 - - - - - - - - - - @@ -1026,7 +1026,7 @@ - 67 + 68 日志 @@ -1043,7 +1043,7 @@ - 167 + 168 标签 diff --git a/archives/page/4/index.html b/archives/page/4/index.html index 9999b59d..cd11b332 100644 --- a/archives/page/4/index.html +++ b/archives/page/4/index.html @@ -230,7 +230,7 @@ - 好! 目前共计 67 篇日志。 继续努力。 + 好! 目前共计 68 篇日志。 继续努力。 @@ -249,6 +249,43 @@ 2016 + + + + + + + + markdown语法说明 + + + + + + + + 06-17 + + + + + + + + + + + + + + + + + + + @@ -545,7 +582,7 @@ - 67 + 68 日志 @@ -562,7 +599,7 @@ - 167 + 168 标签 diff --git a/article/10-educationists.html b/article/10-educationists.html index ca44fbe1..a804be44 100644 --- a/article/10-educationists.html +++ b/article/10-educationists.html @@ -473,7 +473,7 @@ - 67 + 68 日志 @@ -490,7 +490,7 @@ - 167 + 168 标签 diff --git a/article/a-business-trip-to-shenzhen.html b/article/a-business-trip-to-shenzhen.html index c2466445..3f0e0a38 100644 --- a/article/a-business-trip-to-shenzhen.html +++ b/article/a-business-trip-to-shenzhen.html @@ -447,7 +447,7 @@ - 67 + 68 日志 @@ -464,7 +464,7 @@ - 167 + 168 标签 diff --git a/article/a-fragment-code-of-multi-line-string-trim.html b/article/a-fragment-code-of-multi-line-string-trim.html index 8db3c37d..43d2fcae 100644 --- a/article/a-fragment-code-of-multi-line-string-trim.html +++ b/article/a-fragment-code-of-multi-line-string-trim.html @@ -430,7 +430,7 @@ - 67 + 68 日志 @@ -447,7 +447,7 @@ - 167 + 168 标签 diff --git a/article/a-logo-note-for-zte-fpga-design-department.html b/article/a-logo-note-for-zte-fpga-design-department.html index 55ebe7e1..1c5edf83 100644 --- a/article/a-logo-note-for-zte-fpga-design-department.html +++ b/article/a-logo-note-for-zte-fpga-design-department.html @@ -442,7 +442,7 @@ - 67 + 68 日志 @@ -459,7 +459,7 @@ - 167 + 168 标签 diff --git a/article/a-responsive-menu.html b/article/a-responsive-menu.html index 51fd973d..7720eba6 100644 --- a/article/a-responsive-menu.html +++ b/article/a-responsive-menu.html @@ -457,7 +457,7 @@ - 67 + 68 日志 @@ -474,7 +474,7 @@ - 167 + 168 标签 diff --git a/article/a-trap-about-object.html b/article/a-trap-about-object.html index 1c34df46..9b97690f 100644 --- a/article/a-trap-about-object.html +++ b/article/a-trap-about-object.html @@ -431,7 +431,7 @@ - 67 + 68 日志 @@ -448,7 +448,7 @@ - 167 + 168 标签 diff --git a/article/abbreviation.html b/article/abbreviation.html index c5f15a84..2c03b673 100644 --- a/article/abbreviation.html +++ b/article/abbreviation.html @@ -467,7 +467,7 @@ - 67 + 68 日志 @@ -484,7 +484,7 @@ - 167 + 168 标签 diff --git a/article/angular-function.html b/article/angular-function.html index bd572275..4ccc7964 100644 --- a/article/angular-function.html +++ b/article/angular-function.html @@ -476,7 +476,7 @@ - 67 + 68 日志 @@ -493,7 +493,7 @@ - 167 + 168 标签 diff --git a/article/audit-time-of-beian.html b/article/audit-time-of-beian.html index a516e61e..a1057143 100644 --- a/article/audit-time-of-beian.html +++ b/article/audit-time-of-beian.html @@ -558,7 +558,7 @@ - 67 + 68 日志 @@ -575,7 +575,7 @@ - 167 + 168 标签 diff --git a/article/book-review-about-the-bio-of-du-yue-sheng.html b/article/book-review-about-the-bio-of-du-yue-sheng.html index 7113f1c6..31256b2d 100644 --- a/article/book-review-about-the-bio-of-du-yue-sheng.html +++ b/article/book-review-about-the-bio-of-du-yue-sheng.html @@ -449,7 +449,7 @@ - 67 + 68 日志 @@ -466,7 +466,7 @@ - 167 + 168 标签 diff --git a/article/brackets-emmet.html b/article/brackets-emmet.html index d63d4a93..dbc596ff 100644 --- a/article/brackets-emmet.html +++ b/article/brackets-emmet.html @@ -458,7 +458,7 @@ - 67 + 68 日志 @@ -475,7 +475,7 @@ - 167 + 168 标签 diff --git a/article/code-golf-output-fit-numbers.html b/article/code-golf-output-fit-numbers.html index 7d682443..e383b146 100644 --- a/article/code-golf-output-fit-numbers.html +++ b/article/code-golf-output-fit-numbers.html @@ -485,7 +485,7 @@ 后记 - 67 + 68 日志 @@ -502,7 +502,7 @@ 后记 - 167 + 168 标签 diff --git a/article/css-animation.html b/article/css-animation.html index 35b11ee0..ba3a0c72 100644 --- a/article/css-animation.html +++ b/article/css-animation.html @@ -480,7 +480,7 @@ - 67 + 68 日志 @@ -497,7 +497,7 @@ - 167 + 168 标签 diff --git a/article/css-flex.html b/article/css-flex.html index bc6dad82..633866fc 100644 --- a/article/css-flex.html +++ b/article/css-flex.html @@ -513,7 +513,7 @@ - 67 + 68 日志 @@ -530,7 +530,7 @@ - 167 + 168 标签 diff --git a/article/css-specificity.html b/article/css-specificity.html index 7cd9651d..07f5d493 100644 --- a/article/css-specificity.html +++ b/article/css-specificity.html @@ -42,7 +42,7 @@ - + @@ -61,7 +61,7 @@ - + @@ -467,10 +467,10 @@ - #前端 - #css + #前端 + #specificity @@ -558,7 +558,7 @@ - 67 + 68 日志 @@ -575,7 +575,7 @@ - 167 + 168 标签 diff --git a/article/dom-nodetype.html b/article/dom-nodetype.html index f0e17e78..e910a0b4 100644 --- a/article/dom-nodetype.html +++ b/article/dom-nodetype.html @@ -447,7 +447,7 @@ 示例 - 67 + 68 日志 @@ -464,7 +464,7 @@ 示例 - 167 + 168 标签 diff --git a/article/double-eleven-an-ironic-day.html b/article/double-eleven-an-ironic-day.html index c07241d8..a118da09 100644 --- a/article/double-eleven-an-ironic-day.html +++ b/article/double-eleven-an-ironic-day.html @@ -441,7 +441,7 @@ - 67 + 68 日志 @@ -458,7 +458,7 @@ - 167 + 168 标签 diff --git a/article/equal-loan-payment.html b/article/equal-loan-payment.html index 8121135f..542667bf 100644 --- a/article/equal-loan-payment.html +++ b/article/equal-loan-payment.html @@ -446,7 +446,7 @@ - 67 + 68 日志 @@ -463,7 +463,7 @@ - 167 + 168 标签 diff --git a/article/es6-modules.html b/article/es6-modules.html index fc7b7d51..618bab5f 100644 --- a/article/es6-modules.html +++ b/article/es6-modules.html @@ -68,7 +68,7 @@ - + @@ -396,6 +396,10 @@ + + Buffer 简介 + + @@ -462,7 +466,7 @@ - 67 + 68 日志 @@ -479,7 +483,7 @@ - 167 + 168 标签 diff --git a/article/es8.html b/article/es8.html index 8ddeca51..800d97e1 100644 --- a/article/es8.html +++ b/article/es8.html @@ -469,7 +469,7 @@ - 67 + 68 日志 @@ -486,7 +486,7 @@ - 167 + 168 标签 diff --git a/article/excel-even-odd-color.html b/article/excel-even-odd-color.html index fc054f40..1bed15c1 100644 --- a/article/excel-even-odd-color.html +++ b/article/excel-even-odd-color.html @@ -422,7 +422,7 @@ - 67 + 68 日志 @@ -439,7 +439,7 @@ - 167 + 168 标签 diff --git a/article/fix-foxmail-receive-password-error.html b/article/fix-foxmail-receive-password-error.html index f0052291..bbc918f2 100644 --- a/article/fix-foxmail-receive-password-error.html +++ b/article/fix-foxmail-receive-password-error.html @@ -463,7 +463,7 @@ - 67 + 68 日志 @@ -480,7 +480,7 @@ - 167 + 168 标签 diff --git a/article/flex-compability.html b/article/flex-compability.html index 5a66afe8..d1c906b1 100644 --- a/article/flex-compability.html +++ b/article/flex-compability.html @@ -440,7 +440,7 @@ - 67 + 68 日志 @@ -457,7 +457,7 @@ - 167 + 168 标签 diff --git a/article/gulp-4.html b/article/gulp-4.html index 3446433b..992ad623 100644 --- a/article/gulp-4.html +++ b/article/gulp-4.html @@ -435,7 +435,7 @@ - 67 + 68 日志 @@ -452,7 +452,7 @@ - 167 + 168 标签 diff --git a/article/how-technology-hijacks-poeples-minds.html b/article/how-technology-hijacks-poeples-minds.html index baaeb30d..62d86476 100644 --- a/article/how-technology-hijacks-poeples-minds.html +++ b/article/how-technology-hijacks-poeples-minds.html @@ -539,7 +539,7 @@ - 67 + 68 日志 @@ -556,7 +556,7 @@ - 167 + 168 标签 diff --git a/article/htree.html b/article/htree.html index 05e13d32..7af8468e 100644 --- a/article/htree.html +++ b/article/htree.html @@ -42,7 +42,7 @@ - + @@ -61,7 +61,7 @@ - + @@ -383,12 +383,12 @@ #npm - #node - #tree #htree + #node + #CLI @@ -476,7 +476,7 @@ - 67 + 68 日志 @@ -493,7 +493,7 @@ - 167 + 168 标签 diff --git a/article/http-status-code.html b/article/http-status-code.html index 6145765a..0ac7d3bf 100644 --- a/article/http-status-code.html +++ b/article/http-status-code.html @@ -1339,7 +1339,7 @@ - 67 + 68 日志 @@ -1356,7 +1356,7 @@ - 167 + 168 标签 diff --git a/article/i-am-lost-in-the-colorful-world-2.html b/article/i-am-lost-in-the-colorful-world-2.html index 65a5f5aa..8ead7f76 100644 --- a/article/i-am-lost-in-the-colorful-world-2.html +++ b/article/i-am-lost-in-the-colorful-world-2.html @@ -428,7 +428,7 @@ - 67 + 68 日志 @@ -445,7 +445,7 @@ - 167 + 168 标签 diff --git a/article/i-am-lost-in-the-colorful-world-3.html b/article/i-am-lost-in-the-colorful-world-3.html index bdcf24f2..8de63480 100644 --- a/article/i-am-lost-in-the-colorful-world-3.html +++ b/article/i-am-lost-in-the-colorful-world-3.html @@ -421,7 +421,7 @@ - 67 + 68 日志 @@ -438,7 +438,7 @@ - 167 + 168 标签 diff --git a/article/i-am-lost-in-the-colorful-world.html b/article/i-am-lost-in-the-colorful-world.html index 049c7052..59b4b5fa 100644 --- a/article/i-am-lost-in-the-colorful-world.html +++ b/article/i-am-lost-in-the-colorful-world.html @@ -428,7 +428,7 @@ - 67 + 68 日志 @@ -445,7 +445,7 @@ - 167 + 168 标签 diff --git a/article/iis-404-mime-type.html b/article/iis-404-mime-type.html index 00848e6d..749c091c 100644 --- a/article/iis-404-mime-type.html +++ b/article/iis-404-mime-type.html @@ -428,7 +428,7 @@ - 67 + 68 日志 @@ -445,7 +445,7 @@ - 167 + 168 标签 diff --git a/article/introduction-to-jade.html b/article/introduction-to-jade.html index 57a6a335..0065d2db 100644 --- a/article/introduction-to-jade.html +++ b/article/introduction-to-jade.html @@ -458,7 +458,7 @@ - 67 + 68 日志 @@ -475,7 +475,7 @@ - 167 + 168 标签 diff --git a/article/javascript-semicolons.html b/article/javascript-semicolons.html index 9ff96135..6e0aa6c9 100644 --- a/article/javascript-semicolons.html +++ b/article/javascript-semicolons.html @@ -618,7 +618,7 @@ - 67 + 68 日志 @@ -635,7 +635,7 @@ - 167 + 168 标签 diff --git a/article/js-operator-precedence.html b/article/js-operator-precedence.html index 53fe885f..13073dca 100644 --- a/article/js-operator-precedence.html +++ b/article/js-operator-precedence.html @@ -826,7 +826,7 @@ - 67 + 68 日志 @@ -843,7 +843,7 @@ - 167 + 168 标签 diff --git a/article/language-tags-in-html.html b/article/language-tags-in-html.html index 5e747fc8..3dd8d288 100644 --- a/article/language-tags-in-html.html +++ b/article/language-tags-in-html.html @@ -549,7 +549,7 @@ - 67 + 68 日志 @@ -566,7 +566,7 @@ - 167 + 168 标签 diff --git a/article/limitation-of-wechat-weapp.html b/article/limitation-of-wechat-weapp.html index 8f63c04a..6be76992 100644 --- a/article/limitation-of-wechat-weapp.html +++ b/article/limitation-of-wechat-weapp.html @@ -436,7 +436,7 @@ - 67 + 68 日志 @@ -453,7 +453,7 @@ - 167 + 168 标签 diff --git a/article/markdown-syntax.html b/article/markdown-syntax.html index 6a81110c..09a3c6b9 100644 --- a/article/markdown-syntax.html +++ b/article/markdown-syntax.html @@ -693,7 +693,7 @@ - 67 + 68 日志 @@ -710,7 +710,7 @@ - 167 + 168 标签 diff --git a/article/mathjax-advanced.html b/article/mathjax-advanced.html index d0c38eb9..d7475d24 100644 --- a/article/mathjax-advanced.html +++ b/article/mathjax-advanced.html @@ -591,7 +591,7 @@ 高亮 - 67 + 68 日志 @@ -608,7 +608,7 @@ 高亮 - 167 + 168 标签 diff --git a/article/mathjax-basic-tutorial-and-quick-reference.html b/article/mathjax-basic-tutorial-and-quick-reference.html index 1cede7fb..7fd56ba4 100644 --- a/article/mathjax-basic-tutorial-and-quick-reference.html +++ b/article/mathjax-basic-tutorial-and-quick-reference.html @@ -672,7 +672,7 @@ - 67 + 68 日志 @@ -689,7 +689,7 @@ - 167 + 168 标签 diff --git a/article/minify-static-html-files.html b/article/minify-static-html-files.html index baed98b5..455a0761 100644 --- a/article/minify-static-html-files.html +++ b/article/minify-static-html-files.html @@ -442,7 +442,7 @@ - 67 + 68 日志 @@ -459,7 +459,7 @@ - 167 + 168 标签 diff --git a/article/misunderstood-songs-for-mother.html b/article/misunderstood-songs-for-mother.html index c950b218..b8c0b83e 100644 --- a/article/misunderstood-songs-for-mother.html +++ b/article/misunderstood-songs-for-mother.html @@ -432,7 +432,7 @@ - 67 + 68 日志 @@ -449,7 +449,7 @@ - 167 + 168 标签 diff --git a/article/naming-rule.html b/article/naming-rule.html index dbf60b37..08372ebc 100644 --- a/article/naming-rule.html +++ b/article/naming-rule.html @@ -450,7 +450,7 @@ 总结 - 67 + 68 日志 @@ -467,7 +467,7 @@ 总结 - 167 + 168 标签 diff --git a/article/node-buffer.html b/article/node-buffer.html new file mode 100644 index 00000000..5dcff822 --- /dev/null +++ b/article/node-buffer.html @@ -0,0 +1,663 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Buffer 简介 | 清风轩 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 清风轩 + + + + 清风轩居 - 引仙阁 + + + + + + + + + + + + + + + + + + + + + + + 首页 + + + + + + + + + + 分类 + + + + + + + + + + 标签 + + + + + + + + + + 归档 + + + + + + + + + + 关于 + + + + + + + + + + 花园 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Buffer 简介 + + + + + + + + + + + 发表于 + + 2018-10-07 + + + + + + | + + + + 分类于 + + + + Node + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Buffer 是 node 中的一个全局对象,用来读取或者操作二进制数据流。 + + +本文直接使用 Buffer 讲述这个对象,该对象翻译成中文,可以叫做“缓冲区对象”。 + +官方文档的介绍如下: + +Prior to the introduction of TypedArray, the JavaScript language had no mechanism for reading or manipulating streams of binary data. The Buffer class was introduced as part of the Node.js API to enable interaction with octet streams in TCP streams, file system operations, and other contexts. + +在 ECMAScript 2015 引入 TypedArray 之后,Buffer 类使用了一种更优化、更适合 Node.js 用例的方式实现了 Uint8Array API。 +Buffer 类可以视作一个跟 Array 类一样的对象,区别在于 Buffer 的大小是固定的,且在 v8 堆外分配物理内存。Buffer 的大小在创建时就确定了,无法调整。 +先来看一段官方文档提供的示例代码: +1234567891011121314151617181920// Creates a zero-filled Buffer of length 10.const buf1 = Buffer.alloc(10);// Creates a Buffer of length 10, filled with 0x1.const buf2 = Buffer.alloc(10, 1);// Creates an uninitialized buffer of length 10.// This is faster than calling Buffer.alloc() but the returned// Buffer instance might contain old data that needs to be// overwritten using either fill() or write().const buf3 = Buffer.allocUnsafe(10);// Creates a Buffer containing [0x1, 0x2, 0x3].const buf4 = Buffer.from([1, 2, 3]);// Creates a Buffer containing UTF-8 bytes [0x74, 0xc3, 0xa9, 0x73, 0x74].const buf5 = Buffer.from('tést');// Creates a Buffer containing Latin-1 bytes [0x74, 0xe9, 0x73, 0x74].const buf6 = Buffer.from('tést', 'latin1'); + +Buffer 实例一般用于表示编码字符的序列,用过指定字符编码,可以在 Buffer 实例与常规 JavaScript 字符串之间进行相互转换。 +这样的描述,可能太过于抽象,直接上代码解释吧: +123456789function btoa(str) { const buf = Buffer.from(str); return buf.toString('base64');}function atob(str) { const buf = Buffer.from(str, 'base64'); return buf.toString();} +上面的代码,是 node 环境下关于浏览器 atob 和 btoa 方法的实现。 +Node.js 目前支持的字符编码如下: + +ascii,仅支持 7 位 ASCII 数据。utf8,多字节编码的 Unicode 字符。许多网页和其他文档格式都使用 UTF-8。utf16le,2 或 4 字节,little-endian(小字节序)编码的 Unicode 字符。支持 U+10000 到 U+10FFFF 的代理对。usc2,即 utf16le。base64,即 Base64 编码。latin1,即 Latin-1 编码,具体由 IANA 在 RFC1345 中定义。binary,即 latin1。hex,将每个字节编码为两个十六进制字符。 + +注意,现代浏览器遵循 WHATWG 编码标准 将 latin1 和 ISO-8859-1 别名为 win-1252。这意味着在进行例如 http.get() 获取到的字符编码在规范列表中,服务器可能返回 win-1252 编码的数据,此时如果使用 latin1 字符编码,可能会得到错误的解码数据。 + +Buffer 对象可以使用 for...of 进行内部迭代。同样的,实例对象的 .values、.keys 和 .entries 方法都可以创建迭代。 +使用 new 操作符实例化一个 Buffer 的方法已经被弃用。注意,已经弃用的方法本文不做介绍。 +创建 Buffer 实例的方法有下面几个: + +Buffer.from(array),从数组进行创建。Buffer.from(arrayBuffer[, byteOffset[, length]]),从 arrayBuffer 的实例进行创建。Buffer.from(buffer),直接根据 buffer 创建。Buffer.from(string[, encoding]),根据字符串创建,encoding 即使用的编码方式。Buffer.from(object[, offsetOrEncoding[, length]]),根据对象创建,该对象需要支持 Symbol.toPrimitive 或者有 valueOf() 方法,比如 Buffer.from(new String('this is a test'));。Buffer.alloc(size[, fill[, encoding]]),创建一个指定长度的对象,fill 表示用来填充新建的 Buffer 的值,默认为 0。Buffer.allocUnsafe(size),分配一个大小为 size 的对象。该方式创建的 Buffer 对象是未经过初始化的,内容未知,可能包含敏感数据。 + +更多的创建方式不在本文的记述范围之内。 + +创建之后的实例,我们使用 buf 进行表示,buf 的特性跟数组类似。 +buf.buffer 指向了 Buffer 底层的 ArrayBuffer 对象。 +可以使用 fill 方法进行填充,语法为 buf.fill(value[, offset[, end]][, encoding])。 +toString 可以将 buf 转为一个常规字符串,语法为 buf.toString([encoding[, start[, end]]])。 +之前做了一个简单的获取网页源代码的方法,代码大概是这样的: +12345678910111213141516171819202122const http = require('http');const https = require('https');module.exports = function getHTML(url) { return new Promise(function (resolve, reject) { (/^https/i.test(url) ? https : http).get(url, function (res) { var html = ''; res.on('data', function (chunk) { // chunk is a Buffer instance, use the method toString to get the string html += chunk; }); res.on('end', function () { resolve(html); }); }).on('error', function () { reject(); }); });} +是的,这里面的 chunk 对象就是一个 Buffer 实例。 + +真·简介。本文到此就结束了。 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/article/node-wxpay.html b/article/node-wxpay.html index 2b2f2c31..4b4fe087 100644 --- a/article/node-wxpay.html +++ b/article/node-wxpay.html @@ -477,7 +477,7 @@ - 67 + 68 日志 @@ -494,7 +494,7 @@ - 167 + 168 标签 diff --git a/article/normalize-cssreset.html b/article/normalize-cssreset.html index 642b1af6..600d2225 100644 --- a/article/normalize-cssreset.html +++ b/article/normalize-cssreset.html @@ -42,7 +42,7 @@ - + @@ -61,7 +61,7 @@ - + @@ -361,10 +361,10 @@ - #css - #normalize + #css + @@ -446,7 +446,7 @@ - 67 + 68 日志 @@ -463,7 +463,7 @@ - 167 + 168 标签 diff --git a/article/notes-for-git.html b/article/notes-for-git.html index 08c7eb6d..c79774f8 100644 --- a/article/notes-for-git.html +++ b/article/notes-for-git.html @@ -436,7 +436,7 @@ - 67 + 68 日志 @@ -453,7 +453,7 @@ - 167 + 168 标签 diff --git a/article/npm-alias.html b/article/npm-alias.html index 8205762d..d8bc7401 100644 --- a/article/npm-alias.html +++ b/article/npm-alias.html @@ -442,7 +442,7 @@ -B - 67 + 68 日志 @@ -459,7 +459,7 @@ -B - 167 + 168 标签 diff --git a/article/object-property-accessor.html b/article/object-property-accessor.html index a551d9ac..c133a148 100644 --- a/article/object-property-accessor.html +++ b/article/object-property-accessor.html @@ -432,7 +432,7 @@ - 67 + 68 日志 @@ -449,7 +449,7 @@ - 167 + 168 标签 diff --git a/article/parcel-bundler.html b/article/parcel-bundler.html index a2934ce8..2b84a87b 100644 --- a/article/parcel-bundler.html +++ b/article/parcel-bundler.html @@ -492,7 +492,7 @@ Licen - 67 + 68 日志 @@ -509,7 +509,7 @@ Licen - 167 + 168 标签 diff --git a/article/react-or-vue-which-javascript-ui-library-should-you-be-using.html b/article/react-or-vue-which-javascript-ui-library-should-you-be-using.html index eccde3bf..dfc01389 100644 --- a/article/react-or-vue-which-javascript-ui-library-should-you-be-using.html +++ b/article/react-or-vue-which-javascript-ui-library-should-you-be-using.html @@ -537,7 +537,7 @@ - 67 + 68 日志 @@ -554,7 +554,7 @@ - 167 + 168 标签 diff --git a/article/record-to-two-special-weddings.html b/article/record-to-two-special-weddings.html index 3b052ffc..c50df8e1 100644 --- a/article/record-to-two-special-weddings.html +++ b/article/record-to-two-special-weddings.html @@ -453,7 +453,7 @@ - 67 + 68 日志 @@ -470,7 +470,7 @@ - 167 + 168 标签 diff --git a/article/regexp-assertion.html b/article/regexp-assertion.html index dc3946cc..75ee03cd 100644 --- a/article/regexp-assertion.html +++ b/article/regexp-assertion.html @@ -457,7 +457,7 @@ - 67 + 68 日志 @@ -474,7 +474,7 @@ - 167 + 168 标签 diff --git a/article/rfc2616-is-obsoleted.html b/article/rfc2616-is-obsoleted.html index 33979d02..b0e46b26 100644 --- a/article/rfc2616-is-obsoleted.html +++ b/article/rfc2616-is-obsoleted.html @@ -459,7 +459,7 @@
@@ -943,43 +980,6 @@ - - - - - - - - - - - - - - - - - 解决IIS中MIME类型方面的404错误 - - - - - - - - 09-19 - - - - - - - - - - @@ -1026,7 +1026,7 @@ - 67 + 68 日志 @@ -1043,7 +1043,7 @@ - 167 + 168 标签 diff --git a/archives/page/3/index.html b/archives/page/3/index.html index 45fcadb1..ad67e6bd 100644 --- a/archives/page/3/index.html +++ b/archives/page/3/index.html @@ -230,7 +230,7 @@ - 好! 目前共计 67 篇日志。 继续努力。 + 好! 目前共计 68 篇日志。 继续努力。 @@ -249,6 +249,43 @@ 2016 + + + + + + + + 解决IIS中MIME类型方面的404错误 + + + + + + + + 09-19 + + + + + + + + + + + + + + + + + + + @@ -943,43 +980,6 @@ - - - - - - - - - - - - - - - - - markdown语法说明 - - - - - - - - 06-17 - - - - - - - - - - @@ -1026,7 +1026,7 @@ - 67 + 68 日志 @@ -1043,7 +1043,7 @@ - 167 + 168 标签 diff --git a/archives/page/4/index.html b/archives/page/4/index.html index 9999b59d..cd11b332 100644 --- a/archives/page/4/index.html +++ b/archives/page/4/index.html @@ -230,7 +230,7 @@ - 好! 目前共计 67 篇日志。 继续努力。 + 好! 目前共计 68 篇日志。 继续努力。 @@ -249,6 +249,43 @@ 2016 + + + + + + + + markdown语法说明 + + + + + + + + 06-17 + + + + + + + + + + + + + + + + + + + @@ -545,7 +582,7 @@ - 67 + 68 日志 @@ -562,7 +599,7 @@ - 167 + 168 标签 diff --git a/article/10-educationists.html b/article/10-educationists.html index ca44fbe1..a804be44 100644 --- a/article/10-educationists.html +++ b/article/10-educationists.html @@ -473,7 +473,7 @@ - 67 + 68 日志 @@ -490,7 +490,7 @@ - 167 + 168 标签 diff --git a/article/a-business-trip-to-shenzhen.html b/article/a-business-trip-to-shenzhen.html index c2466445..3f0e0a38 100644 --- a/article/a-business-trip-to-shenzhen.html +++ b/article/a-business-trip-to-shenzhen.html @@ -447,7 +447,7 @@ - 67 + 68 日志 @@ -464,7 +464,7 @@ - 167 + 168 标签 diff --git a/article/a-fragment-code-of-multi-line-string-trim.html b/article/a-fragment-code-of-multi-line-string-trim.html index 8db3c37d..43d2fcae 100644 --- a/article/a-fragment-code-of-multi-line-string-trim.html +++ b/article/a-fragment-code-of-multi-line-string-trim.html @@ -430,7 +430,7 @@ - 67 + 68 日志 @@ -447,7 +447,7 @@ - 167 + 168 标签 diff --git a/article/a-logo-note-for-zte-fpga-design-department.html b/article/a-logo-note-for-zte-fpga-design-department.html index 55ebe7e1..1c5edf83 100644 --- a/article/a-logo-note-for-zte-fpga-design-department.html +++ b/article/a-logo-note-for-zte-fpga-design-department.html @@ -442,7 +442,7 @@ - 67 + 68 日志 @@ -459,7 +459,7 @@ - 167 + 168 标签 diff --git a/article/a-responsive-menu.html b/article/a-responsive-menu.html index 51fd973d..7720eba6 100644 --- a/article/a-responsive-menu.html +++ b/article/a-responsive-menu.html @@ -457,7 +457,7 @@ - 67 + 68 日志 @@ -474,7 +474,7 @@ - 167 + 168 标签 diff --git a/article/a-trap-about-object.html b/article/a-trap-about-object.html index 1c34df46..9b97690f 100644 --- a/article/a-trap-about-object.html +++ b/article/a-trap-about-object.html @@ -431,7 +431,7 @@ - 67 + 68 日志 @@ -448,7 +448,7 @@ - 167 + 168 标签 diff --git a/article/abbreviation.html b/article/abbreviation.html index c5f15a84..2c03b673 100644 --- a/article/abbreviation.html +++ b/article/abbreviation.html @@ -467,7 +467,7 @@ - 67 + 68 日志 @@ -484,7 +484,7 @@ - 167 + 168 标签 diff --git a/article/angular-function.html b/article/angular-function.html index bd572275..4ccc7964 100644 --- a/article/angular-function.html +++ b/article/angular-function.html @@ -476,7 +476,7 @@ - 67 + 68 日志 @@ -493,7 +493,7 @@ - 167 + 168 标签 diff --git a/article/audit-time-of-beian.html b/article/audit-time-of-beian.html index a516e61e..a1057143 100644 --- a/article/audit-time-of-beian.html +++ b/article/audit-time-of-beian.html @@ -558,7 +558,7 @@ - 67 + 68 日志 @@ -575,7 +575,7 @@ - 167 + 168 标签 diff --git a/article/book-review-about-the-bio-of-du-yue-sheng.html b/article/book-review-about-the-bio-of-du-yue-sheng.html index 7113f1c6..31256b2d 100644 --- a/article/book-review-about-the-bio-of-du-yue-sheng.html +++ b/article/book-review-about-the-bio-of-du-yue-sheng.html @@ -449,7 +449,7 @@ - 67 + 68 日志 @@ -466,7 +466,7 @@ - 167 + 168 标签 diff --git a/article/brackets-emmet.html b/article/brackets-emmet.html index d63d4a93..dbc596ff 100644 --- a/article/brackets-emmet.html +++ b/article/brackets-emmet.html @@ -458,7 +458,7 @@ - 67 + 68 日志 @@ -475,7 +475,7 @@ - 167 + 168 标签 diff --git a/article/code-golf-output-fit-numbers.html b/article/code-golf-output-fit-numbers.html index 7d682443..e383b146 100644 --- a/article/code-golf-output-fit-numbers.html +++ b/article/code-golf-output-fit-numbers.html @@ -485,7 +485,7 @@ 后记 - 67 + 68 日志 @@ -502,7 +502,7 @@ 后记 - 167 + 168 标签 diff --git a/article/css-animation.html b/article/css-animation.html index 35b11ee0..ba3a0c72 100644 --- a/article/css-animation.html +++ b/article/css-animation.html @@ -480,7 +480,7 @@ - 67 + 68 日志 @@ -497,7 +497,7 @@ - 167 + 168 标签 diff --git a/article/css-flex.html b/article/css-flex.html index bc6dad82..633866fc 100644 --- a/article/css-flex.html +++ b/article/css-flex.html @@ -513,7 +513,7 @@ - 67 + 68 日志 @@ -530,7 +530,7 @@ - 167 + 168 标签 diff --git a/article/css-specificity.html b/article/css-specificity.html index 7cd9651d..07f5d493 100644 --- a/article/css-specificity.html +++ b/article/css-specificity.html @@ -42,7 +42,7 @@ - + @@ -61,7 +61,7 @@ - + @@ -467,10 +467,10 @@ - #前端 - #css + #前端 + #specificity @@ -558,7 +558,7 @@ - 67 + 68 日志 @@ -575,7 +575,7 @@ - 167 + 168 标签 diff --git a/article/dom-nodetype.html b/article/dom-nodetype.html index f0e17e78..e910a0b4 100644 --- a/article/dom-nodetype.html +++ b/article/dom-nodetype.html @@ -447,7 +447,7 @@ 示例 - 67 + 68 日志 @@ -464,7 +464,7 @@ 示例 - 167 + 168 标签 diff --git a/article/double-eleven-an-ironic-day.html b/article/double-eleven-an-ironic-day.html index c07241d8..a118da09 100644 --- a/article/double-eleven-an-ironic-day.html +++ b/article/double-eleven-an-ironic-day.html @@ -441,7 +441,7 @@ - 67 + 68 日志 @@ -458,7 +458,7 @@ - 167 + 168 标签 diff --git a/article/equal-loan-payment.html b/article/equal-loan-payment.html index 8121135f..542667bf 100644 --- a/article/equal-loan-payment.html +++ b/article/equal-loan-payment.html @@ -446,7 +446,7 @@ - 67 + 68 日志 @@ -463,7 +463,7 @@ - 167 + 168 标签 diff --git a/article/es6-modules.html b/article/es6-modules.html index fc7b7d51..618bab5f 100644 --- a/article/es6-modules.html +++ b/article/es6-modules.html @@ -68,7 +68,7 @@ - + @@ -396,6 +396,10 @@ + + Buffer 简介 + + @@ -462,7 +466,7 @@ - 67 + 68 日志 @@ -479,7 +483,7 @@ - 167 + 168 标签 diff --git a/article/es8.html b/article/es8.html index 8ddeca51..800d97e1 100644 --- a/article/es8.html +++ b/article/es8.html @@ -469,7 +469,7 @@ - 67 + 68 日志 @@ -486,7 +486,7 @@ - 167 + 168 标签 diff --git a/article/excel-even-odd-color.html b/article/excel-even-odd-color.html index fc054f40..1bed15c1 100644 --- a/article/excel-even-odd-color.html +++ b/article/excel-even-odd-color.html @@ -422,7 +422,7 @@ - 67 + 68 日志 @@ -439,7 +439,7 @@ - 167 + 168 标签 diff --git a/article/fix-foxmail-receive-password-error.html b/article/fix-foxmail-receive-password-error.html index f0052291..bbc918f2 100644 --- a/article/fix-foxmail-receive-password-error.html +++ b/article/fix-foxmail-receive-password-error.html @@ -463,7 +463,7 @@ - 67 + 68 日志 @@ -480,7 +480,7 @@ - 167 + 168 标签 diff --git a/article/flex-compability.html b/article/flex-compability.html index 5a66afe8..d1c906b1 100644 --- a/article/flex-compability.html +++ b/article/flex-compability.html @@ -440,7 +440,7 @@ - 67 + 68 日志 @@ -457,7 +457,7 @@ - 167 + 168 标签 diff --git a/article/gulp-4.html b/article/gulp-4.html index 3446433b..992ad623 100644 --- a/article/gulp-4.html +++ b/article/gulp-4.html @@ -435,7 +435,7 @@ - 67 + 68 日志 @@ -452,7 +452,7 @@ - 167 + 168 标签 diff --git a/article/how-technology-hijacks-poeples-minds.html b/article/how-technology-hijacks-poeples-minds.html index baaeb30d..62d86476 100644 --- a/article/how-technology-hijacks-poeples-minds.html +++ b/article/how-technology-hijacks-poeples-minds.html @@ -539,7 +539,7 @@ - 67 + 68 日志 @@ -556,7 +556,7 @@ - 167 + 168 标签 diff --git a/article/htree.html b/article/htree.html index 05e13d32..7af8468e 100644 --- a/article/htree.html +++ b/article/htree.html @@ -42,7 +42,7 @@ - + @@ -61,7 +61,7 @@ - + @@ -383,12 +383,12 @@ #npm - #node - #tree #htree + #node + #CLI @@ -476,7 +476,7 @@ - 67 + 68 日志 @@ -493,7 +493,7 @@ - 167 + 168 标签 diff --git a/article/http-status-code.html b/article/http-status-code.html index 6145765a..0ac7d3bf 100644 --- a/article/http-status-code.html +++ b/article/http-status-code.html @@ -1339,7 +1339,7 @@ - 67 + 68 日志 @@ -1356,7 +1356,7 @@ - 167 + 168 标签 diff --git a/article/i-am-lost-in-the-colorful-world-2.html b/article/i-am-lost-in-the-colorful-world-2.html index 65a5f5aa..8ead7f76 100644 --- a/article/i-am-lost-in-the-colorful-world-2.html +++ b/article/i-am-lost-in-the-colorful-world-2.html @@ -428,7 +428,7 @@ - 67 + 68 日志 @@ -445,7 +445,7 @@ - 167 + 168 标签 diff --git a/article/i-am-lost-in-the-colorful-world-3.html b/article/i-am-lost-in-the-colorful-world-3.html index bdcf24f2..8de63480 100644 --- a/article/i-am-lost-in-the-colorful-world-3.html +++ b/article/i-am-lost-in-the-colorful-world-3.html @@ -421,7 +421,7 @@ - 67 + 68 日志 @@ -438,7 +438,7 @@ - 167 + 168 标签 diff --git a/article/i-am-lost-in-the-colorful-world.html b/article/i-am-lost-in-the-colorful-world.html index 049c7052..59b4b5fa 100644 --- a/article/i-am-lost-in-the-colorful-world.html +++ b/article/i-am-lost-in-the-colorful-world.html @@ -428,7 +428,7 @@ - 67 + 68 日志 @@ -445,7 +445,7 @@ - 167 + 168 标签 diff --git a/article/iis-404-mime-type.html b/article/iis-404-mime-type.html index 00848e6d..749c091c 100644 --- a/article/iis-404-mime-type.html +++ b/article/iis-404-mime-type.html @@ -428,7 +428,7 @@ - 67 + 68 日志 @@ -445,7 +445,7 @@ - 167 + 168 标签 diff --git a/article/introduction-to-jade.html b/article/introduction-to-jade.html index 57a6a335..0065d2db 100644 --- a/article/introduction-to-jade.html +++ b/article/introduction-to-jade.html @@ -458,7 +458,7 @@ - 67 + 68 日志 @@ -475,7 +475,7 @@ - 167 + 168 标签 diff --git a/article/javascript-semicolons.html b/article/javascript-semicolons.html index 9ff96135..6e0aa6c9 100644 --- a/article/javascript-semicolons.html +++ b/article/javascript-semicolons.html @@ -618,7 +618,7 @@ - 67 + 68 日志 @@ -635,7 +635,7 @@ - 167 + 168 标签 diff --git a/article/js-operator-precedence.html b/article/js-operator-precedence.html index 53fe885f..13073dca 100644 --- a/article/js-operator-precedence.html +++ b/article/js-operator-precedence.html @@ -826,7 +826,7 @@ - 67 + 68 日志 @@ -843,7 +843,7 @@ - 167 + 168 标签 diff --git a/article/language-tags-in-html.html b/article/language-tags-in-html.html index 5e747fc8..3dd8d288 100644 --- a/article/language-tags-in-html.html +++ b/article/language-tags-in-html.html @@ -549,7 +549,7 @@ - 67 + 68 日志 @@ -566,7 +566,7 @@ - 167 + 168 标签 diff --git a/article/limitation-of-wechat-weapp.html b/article/limitation-of-wechat-weapp.html index 8f63c04a..6be76992 100644 --- a/article/limitation-of-wechat-weapp.html +++ b/article/limitation-of-wechat-weapp.html @@ -436,7 +436,7 @@ - 67 + 68 日志 @@ -453,7 +453,7 @@ - 167 + 168 标签 diff --git a/article/markdown-syntax.html b/article/markdown-syntax.html index 6a81110c..09a3c6b9 100644 --- a/article/markdown-syntax.html +++ b/article/markdown-syntax.html @@ -693,7 +693,7 @@ - 67 + 68 日志 @@ -710,7 +710,7 @@ - 167 + 168 标签 diff --git a/article/mathjax-advanced.html b/article/mathjax-advanced.html index d0c38eb9..d7475d24 100644 --- a/article/mathjax-advanced.html +++ b/article/mathjax-advanced.html @@ -591,7 +591,7 @@ 高亮 - 67 + 68 日志 @@ -608,7 +608,7 @@ 高亮 - 167 + 168 标签 diff --git a/article/mathjax-basic-tutorial-and-quick-reference.html b/article/mathjax-basic-tutorial-and-quick-reference.html index 1cede7fb..7fd56ba4 100644 --- a/article/mathjax-basic-tutorial-and-quick-reference.html +++ b/article/mathjax-basic-tutorial-and-quick-reference.html @@ -672,7 +672,7 @@ - 67 + 68 日志 @@ -689,7 +689,7 @@ - 167 + 168 标签 diff --git a/article/minify-static-html-files.html b/article/minify-static-html-files.html index baed98b5..455a0761 100644 --- a/article/minify-static-html-files.html +++ b/article/minify-static-html-files.html @@ -442,7 +442,7 @@ - 67 + 68 日志 @@ -459,7 +459,7 @@ - 167 + 168 标签 diff --git a/article/misunderstood-songs-for-mother.html b/article/misunderstood-songs-for-mother.html index c950b218..b8c0b83e 100644 --- a/article/misunderstood-songs-for-mother.html +++ b/article/misunderstood-songs-for-mother.html @@ -432,7 +432,7 @@ - 67 + 68 日志 @@ -449,7 +449,7 @@ - 167 + 168 标签 diff --git a/article/naming-rule.html b/article/naming-rule.html index dbf60b37..08372ebc 100644 --- a/article/naming-rule.html +++ b/article/naming-rule.html @@ -450,7 +450,7 @@ 总结 - 67 + 68 日志 @@ -467,7 +467,7 @@ 总结 - 167 + 168 标签 diff --git a/article/node-buffer.html b/article/node-buffer.html new file mode 100644 index 00000000..5dcff822 --- /dev/null +++ b/article/node-buffer.html @@ -0,0 +1,663 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Buffer 简介 | 清风轩 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 清风轩 + + + + 清风轩居 - 引仙阁 + + + + + + + + + + + + + + + + + + + + + + + 首页 + + + + + + + + + + 分类 + + + + + + + + + + 标签 + + + + + + + + + + 归档 + + + + + + + + + + 关于 + + + + + + + + + + 花园 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Buffer 简介 + + + + + + + + + + + 发表于 + + 2018-10-07 + + + + + + | + + + + 分类于 + + + + Node + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Buffer 是 node 中的一个全局对象,用来读取或者操作二进制数据流。 + + +本文直接使用 Buffer 讲述这个对象,该对象翻译成中文,可以叫做“缓冲区对象”。 + +官方文档的介绍如下: + +Prior to the introduction of TypedArray, the JavaScript language had no mechanism for reading or manipulating streams of binary data. The Buffer class was introduced as part of the Node.js API to enable interaction with octet streams in TCP streams, file system operations, and other contexts. + +在 ECMAScript 2015 引入 TypedArray 之后,Buffer 类使用了一种更优化、更适合 Node.js 用例的方式实现了 Uint8Array API。 +Buffer 类可以视作一个跟 Array 类一样的对象,区别在于 Buffer 的大小是固定的,且在 v8 堆外分配物理内存。Buffer 的大小在创建时就确定了,无法调整。 +先来看一段官方文档提供的示例代码: +1234567891011121314151617181920// Creates a zero-filled Buffer of length 10.const buf1 = Buffer.alloc(10);// Creates a Buffer of length 10, filled with 0x1.const buf2 = Buffer.alloc(10, 1);// Creates an uninitialized buffer of length 10.// This is faster than calling Buffer.alloc() but the returned// Buffer instance might contain old data that needs to be// overwritten using either fill() or write().const buf3 = Buffer.allocUnsafe(10);// Creates a Buffer containing [0x1, 0x2, 0x3].const buf4 = Buffer.from([1, 2, 3]);// Creates a Buffer containing UTF-8 bytes [0x74, 0xc3, 0xa9, 0x73, 0x74].const buf5 = Buffer.from('tést');// Creates a Buffer containing Latin-1 bytes [0x74, 0xe9, 0x73, 0x74].const buf6 = Buffer.from('tést', 'latin1'); + +Buffer 实例一般用于表示编码字符的序列,用过指定字符编码,可以在 Buffer 实例与常规 JavaScript 字符串之间进行相互转换。 +这样的描述,可能太过于抽象,直接上代码解释吧: +123456789function btoa(str) { const buf = Buffer.from(str); return buf.toString('base64');}function atob(str) { const buf = Buffer.from(str, 'base64'); return buf.toString();} +上面的代码,是 node 环境下关于浏览器 atob 和 btoa 方法的实现。 +Node.js 目前支持的字符编码如下: + +ascii,仅支持 7 位 ASCII 数据。utf8,多字节编码的 Unicode 字符。许多网页和其他文档格式都使用 UTF-8。utf16le,2 或 4 字节,little-endian(小字节序)编码的 Unicode 字符。支持 U+10000 到 U+10FFFF 的代理对。usc2,即 utf16le。base64,即 Base64 编码。latin1,即 Latin-1 编码,具体由 IANA 在 RFC1345 中定义。binary,即 latin1。hex,将每个字节编码为两个十六进制字符。 + +注意,现代浏览器遵循 WHATWG 编码标准 将 latin1 和 ISO-8859-1 别名为 win-1252。这意味着在进行例如 http.get() 获取到的字符编码在规范列表中,服务器可能返回 win-1252 编码的数据,此时如果使用 latin1 字符编码,可能会得到错误的解码数据。 + +Buffer 对象可以使用 for...of 进行内部迭代。同样的,实例对象的 .values、.keys 和 .entries 方法都可以创建迭代。 +使用 new 操作符实例化一个 Buffer 的方法已经被弃用。注意,已经弃用的方法本文不做介绍。 +创建 Buffer 实例的方法有下面几个: + +Buffer.from(array),从数组进行创建。Buffer.from(arrayBuffer[, byteOffset[, length]]),从 arrayBuffer 的实例进行创建。Buffer.from(buffer),直接根据 buffer 创建。Buffer.from(string[, encoding]),根据字符串创建,encoding 即使用的编码方式。Buffer.from(object[, offsetOrEncoding[, length]]),根据对象创建,该对象需要支持 Symbol.toPrimitive 或者有 valueOf() 方法,比如 Buffer.from(new String('this is a test'));。Buffer.alloc(size[, fill[, encoding]]),创建一个指定长度的对象,fill 表示用来填充新建的 Buffer 的值,默认为 0。Buffer.allocUnsafe(size),分配一个大小为 size 的对象。该方式创建的 Buffer 对象是未经过初始化的,内容未知,可能包含敏感数据。 + +更多的创建方式不在本文的记述范围之内。 + +创建之后的实例,我们使用 buf 进行表示,buf 的特性跟数组类似。 +buf.buffer 指向了 Buffer 底层的 ArrayBuffer 对象。 +可以使用 fill 方法进行填充,语法为 buf.fill(value[, offset[, end]][, encoding])。 +toString 可以将 buf 转为一个常规字符串,语法为 buf.toString([encoding[, start[, end]]])。 +之前做了一个简单的获取网页源代码的方法,代码大概是这样的: +12345678910111213141516171819202122const http = require('http');const https = require('https');module.exports = function getHTML(url) { return new Promise(function (resolve, reject) { (/^https/i.test(url) ? https : http).get(url, function (res) { var html = ''; res.on('data', function (chunk) { // chunk is a Buffer instance, use the method toString to get the string html += chunk; }); res.on('end', function () { resolve(html); }); }).on('error', function () { reject(); }); });} +是的,这里面的 chunk 对象就是一个 Buffer 实例。 + +真·简介。本文到此就结束了。 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/article/node-wxpay.html b/article/node-wxpay.html index 2b2f2c31..4b4fe087 100644 --- a/article/node-wxpay.html +++ b/article/node-wxpay.html @@ -477,7 +477,7 @@ - 67 + 68 日志 @@ -494,7 +494,7 @@ - 167 + 168 标签 diff --git a/article/normalize-cssreset.html b/article/normalize-cssreset.html index 642b1af6..600d2225 100644 --- a/article/normalize-cssreset.html +++ b/article/normalize-cssreset.html @@ -42,7 +42,7 @@ - + @@ -61,7 +61,7 @@ - + @@ -361,10 +361,10 @@ - #css - #normalize + #css + @@ -446,7 +446,7 @@ - 67 + 68 日志 @@ -463,7 +463,7 @@ - 167 + 168 标签 diff --git a/article/notes-for-git.html b/article/notes-for-git.html index 08c7eb6d..c79774f8 100644 --- a/article/notes-for-git.html +++ b/article/notes-for-git.html @@ -436,7 +436,7 @@ - 67 + 68 日志 @@ -453,7 +453,7 @@ - 167 + 168 标签 diff --git a/article/npm-alias.html b/article/npm-alias.html index 8205762d..d8bc7401 100644 --- a/article/npm-alias.html +++ b/article/npm-alias.html @@ -442,7 +442,7 @@ -B - 67 + 68 日志 @@ -459,7 +459,7 @@ -B - 167 + 168 标签 diff --git a/article/object-property-accessor.html b/article/object-property-accessor.html index a551d9ac..c133a148 100644 --- a/article/object-property-accessor.html +++ b/article/object-property-accessor.html @@ -432,7 +432,7 @@ - 67 + 68 日志 @@ -449,7 +449,7 @@ - 167 + 168 标签 diff --git a/article/parcel-bundler.html b/article/parcel-bundler.html index a2934ce8..2b84a87b 100644 --- a/article/parcel-bundler.html +++ b/article/parcel-bundler.html @@ -492,7 +492,7 @@ Licen - 67 + 68 日志 @@ -509,7 +509,7 @@ Licen - 167 + 168 标签 diff --git a/article/react-or-vue-which-javascript-ui-library-should-you-be-using.html b/article/react-or-vue-which-javascript-ui-library-should-you-be-using.html index eccde3bf..dfc01389 100644 --- a/article/react-or-vue-which-javascript-ui-library-should-you-be-using.html +++ b/article/react-or-vue-which-javascript-ui-library-should-you-be-using.html @@ -537,7 +537,7 @@ - 67 + 68 日志 @@ -554,7 +554,7 @@ - 167 + 168 标签 diff --git a/article/record-to-two-special-weddings.html b/article/record-to-two-special-weddings.html index 3b052ffc..c50df8e1 100644 --- a/article/record-to-two-special-weddings.html +++ b/article/record-to-two-special-weddings.html @@ -453,7 +453,7 @@ - 67 + 68 日志 @@ -470,7 +470,7 @@ - 167 + 168 标签 diff --git a/article/regexp-assertion.html b/article/regexp-assertion.html index dc3946cc..75ee03cd 100644 --- a/article/regexp-assertion.html +++ b/article/regexp-assertion.html @@ -457,7 +457,7 @@ - 67 + 68 日志 @@ -474,7 +474,7 @@ - 167 + 168 标签 diff --git a/article/rfc2616-is-obsoleted.html b/article/rfc2616-is-obsoleted.html index 33979d02..b0e46b26 100644 --- a/article/rfc2616-is-obsoleted.html +++ b/article/rfc2616-is-obsoleted.html @@ -459,7 +459,7 @@
@@ -943,43 +980,6 @@ - - - - - - - - - - - - - - - - - markdown语法说明 - - - - - - - - 06-17 - - - - - - - - - - @@ -1026,7 +1026,7 @@ - 67 + 68 日志 @@ -1043,7 +1043,7 @@ - 167 + 168 标签 diff --git a/archives/page/4/index.html b/archives/page/4/index.html index 9999b59d..cd11b332 100644 --- a/archives/page/4/index.html +++ b/archives/page/4/index.html @@ -230,7 +230,7 @@ - 好! 目前共计 67 篇日志。 继续努力。 + 好! 目前共计 68 篇日志。 继续努力。 @@ -249,6 +249,43 @@ 2016 + + + + + + + + markdown语法说明 + + + + + + + + 06-17 + + + + + + + + + + + + + + + + + + + @@ -545,7 +582,7 @@ - 67 + 68 日志 @@ -562,7 +599,7 @@ - 167 + 168 标签 diff --git a/article/10-educationists.html b/article/10-educationists.html index ca44fbe1..a804be44 100644 --- a/article/10-educationists.html +++ b/article/10-educationists.html @@ -473,7 +473,7 @@ - 67 + 68 日志 @@ -490,7 +490,7 @@ - 167 + 168 标签 diff --git a/article/a-business-trip-to-shenzhen.html b/article/a-business-trip-to-shenzhen.html index c2466445..3f0e0a38 100644 --- a/article/a-business-trip-to-shenzhen.html +++ b/article/a-business-trip-to-shenzhen.html @@ -447,7 +447,7 @@ - 67 + 68 日志 @@ -464,7 +464,7 @@ - 167 + 168 标签 diff --git a/article/a-fragment-code-of-multi-line-string-trim.html b/article/a-fragment-code-of-multi-line-string-trim.html index 8db3c37d..43d2fcae 100644 --- a/article/a-fragment-code-of-multi-line-string-trim.html +++ b/article/a-fragment-code-of-multi-line-string-trim.html @@ -430,7 +430,7 @@ - 67 + 68 日志 @@ -447,7 +447,7 @@ - 167 + 168 标签 diff --git a/article/a-logo-note-for-zte-fpga-design-department.html b/article/a-logo-note-for-zte-fpga-design-department.html index 55ebe7e1..1c5edf83 100644 --- a/article/a-logo-note-for-zte-fpga-design-department.html +++ b/article/a-logo-note-for-zte-fpga-design-department.html @@ -442,7 +442,7 @@ - 67 + 68 日志 @@ -459,7 +459,7 @@ - 167 + 168 标签 diff --git a/article/a-responsive-menu.html b/article/a-responsive-menu.html index 51fd973d..7720eba6 100644 --- a/article/a-responsive-menu.html +++ b/article/a-responsive-menu.html @@ -457,7 +457,7 @@ - 67 + 68 日志 @@ -474,7 +474,7 @@ - 167 + 168 标签 diff --git a/article/a-trap-about-object.html b/article/a-trap-about-object.html index 1c34df46..9b97690f 100644 --- a/article/a-trap-about-object.html +++ b/article/a-trap-about-object.html @@ -431,7 +431,7 @@ - 67 + 68 日志 @@ -448,7 +448,7 @@ - 167 + 168 标签 diff --git a/article/abbreviation.html b/article/abbreviation.html index c5f15a84..2c03b673 100644 --- a/article/abbreviation.html +++ b/article/abbreviation.html @@ -467,7 +467,7 @@ - 67 + 68 日志 @@ -484,7 +484,7 @@ - 167 + 168 标签 diff --git a/article/angular-function.html b/article/angular-function.html index bd572275..4ccc7964 100644 --- a/article/angular-function.html +++ b/article/angular-function.html @@ -476,7 +476,7 @@ - 67 + 68 日志 @@ -493,7 +493,7 @@ - 167 + 168 标签 diff --git a/article/audit-time-of-beian.html b/article/audit-time-of-beian.html index a516e61e..a1057143 100644 --- a/article/audit-time-of-beian.html +++ b/article/audit-time-of-beian.html @@ -558,7 +558,7 @@ - 67 + 68 日志 @@ -575,7 +575,7 @@ - 167 + 168 标签 diff --git a/article/book-review-about-the-bio-of-du-yue-sheng.html b/article/book-review-about-the-bio-of-du-yue-sheng.html index 7113f1c6..31256b2d 100644 --- a/article/book-review-about-the-bio-of-du-yue-sheng.html +++ b/article/book-review-about-the-bio-of-du-yue-sheng.html @@ -449,7 +449,7 @@ - 67 + 68 日志 @@ -466,7 +466,7 @@ - 167 + 168 标签 diff --git a/article/brackets-emmet.html b/article/brackets-emmet.html index d63d4a93..dbc596ff 100644 --- a/article/brackets-emmet.html +++ b/article/brackets-emmet.html @@ -458,7 +458,7 @@ - 67 + 68 日志 @@ -475,7 +475,7 @@ - 167 + 168 标签 diff --git a/article/code-golf-output-fit-numbers.html b/article/code-golf-output-fit-numbers.html index 7d682443..e383b146 100644 --- a/article/code-golf-output-fit-numbers.html +++ b/article/code-golf-output-fit-numbers.html @@ -485,7 +485,7 @@ 后记 - 67 + 68 日志 @@ -502,7 +502,7 @@ 后记 - 167 + 168 标签 diff --git a/article/css-animation.html b/article/css-animation.html index 35b11ee0..ba3a0c72 100644 --- a/article/css-animation.html +++ b/article/css-animation.html @@ -480,7 +480,7 @@ - 67 + 68 日志 @@ -497,7 +497,7 @@ - 167 + 168 标签 diff --git a/article/css-flex.html b/article/css-flex.html index bc6dad82..633866fc 100644 --- a/article/css-flex.html +++ b/article/css-flex.html @@ -513,7 +513,7 @@ - 67 + 68 日志 @@ -530,7 +530,7 @@ - 167 + 168 标签 diff --git a/article/css-specificity.html b/article/css-specificity.html index 7cd9651d..07f5d493 100644 --- a/article/css-specificity.html +++ b/article/css-specificity.html @@ -42,7 +42,7 @@ - + @@ -61,7 +61,7 @@ - + @@ -467,10 +467,10 @@ - #前端 - #css + #前端 + #specificity @@ -558,7 +558,7 @@ - 67 + 68 日志 @@ -575,7 +575,7 @@ - 167 + 168 标签 diff --git a/article/dom-nodetype.html b/article/dom-nodetype.html index f0e17e78..e910a0b4 100644 --- a/article/dom-nodetype.html +++ b/article/dom-nodetype.html @@ -447,7 +447,7 @@ 示例 - 67 + 68 日志 @@ -464,7 +464,7 @@ 示例 - 167 + 168 标签 diff --git a/article/double-eleven-an-ironic-day.html b/article/double-eleven-an-ironic-day.html index c07241d8..a118da09 100644 --- a/article/double-eleven-an-ironic-day.html +++ b/article/double-eleven-an-ironic-day.html @@ -441,7 +441,7 @@ - 67 + 68 日志 @@ -458,7 +458,7 @@ - 167 + 168 标签 diff --git a/article/equal-loan-payment.html b/article/equal-loan-payment.html index 8121135f..542667bf 100644 --- a/article/equal-loan-payment.html +++ b/article/equal-loan-payment.html @@ -446,7 +446,7 @@ - 67 + 68 日志 @@ -463,7 +463,7 @@ - 167 + 168 标签 diff --git a/article/es6-modules.html b/article/es6-modules.html index fc7b7d51..618bab5f 100644 --- a/article/es6-modules.html +++ b/article/es6-modules.html @@ -68,7 +68,7 @@ - + @@ -396,6 +396,10 @@ + + Buffer 简介 + + @@ -462,7 +466,7 @@ - 67 + 68 日志 @@ -479,7 +483,7 @@ - 167 + 168 标签 diff --git a/article/es8.html b/article/es8.html index 8ddeca51..800d97e1 100644 --- a/article/es8.html +++ b/article/es8.html @@ -469,7 +469,7 @@ - 67 + 68 日志 @@ -486,7 +486,7 @@ - 167 + 168 标签 diff --git a/article/excel-even-odd-color.html b/article/excel-even-odd-color.html index fc054f40..1bed15c1 100644 --- a/article/excel-even-odd-color.html +++ b/article/excel-even-odd-color.html @@ -422,7 +422,7 @@ - 67 + 68 日志 @@ -439,7 +439,7 @@ - 167 + 168 标签 diff --git a/article/fix-foxmail-receive-password-error.html b/article/fix-foxmail-receive-password-error.html index f0052291..bbc918f2 100644 --- a/article/fix-foxmail-receive-password-error.html +++ b/article/fix-foxmail-receive-password-error.html @@ -463,7 +463,7 @@ - 67 + 68 日志 @@ -480,7 +480,7 @@ - 167 + 168 标签 diff --git a/article/flex-compability.html b/article/flex-compability.html index 5a66afe8..d1c906b1 100644 --- a/article/flex-compability.html +++ b/article/flex-compability.html @@ -440,7 +440,7 @@ - 67 + 68 日志 @@ -457,7 +457,7 @@ - 167 + 168 标签 diff --git a/article/gulp-4.html b/article/gulp-4.html index 3446433b..992ad623 100644 --- a/article/gulp-4.html +++ b/article/gulp-4.html @@ -435,7 +435,7 @@ - 67 + 68 日志 @@ -452,7 +452,7 @@ - 167 + 168 标签 diff --git a/article/how-technology-hijacks-poeples-minds.html b/article/how-technology-hijacks-poeples-minds.html index baaeb30d..62d86476 100644 --- a/article/how-technology-hijacks-poeples-minds.html +++ b/article/how-technology-hijacks-poeples-minds.html @@ -539,7 +539,7 @@ - 67 + 68 日志 @@ -556,7 +556,7 @@ - 167 + 168 标签 diff --git a/article/htree.html b/article/htree.html index 05e13d32..7af8468e 100644 --- a/article/htree.html +++ b/article/htree.html @@ -42,7 +42,7 @@ - + @@ -61,7 +61,7 @@ - + @@ -383,12 +383,12 @@ #npm - #node - #tree #htree + #node + #CLI @@ -476,7 +476,7 @@ - 67 + 68 日志 @@ -493,7 +493,7 @@ - 167 + 168 标签 diff --git a/article/http-status-code.html b/article/http-status-code.html index 6145765a..0ac7d3bf 100644 --- a/article/http-status-code.html +++ b/article/http-status-code.html @@ -1339,7 +1339,7 @@ - 67 + 68 日志 @@ -1356,7 +1356,7 @@ - 167 + 168 标签 diff --git a/article/i-am-lost-in-the-colorful-world-2.html b/article/i-am-lost-in-the-colorful-world-2.html index 65a5f5aa..8ead7f76 100644 --- a/article/i-am-lost-in-the-colorful-world-2.html +++ b/article/i-am-lost-in-the-colorful-world-2.html @@ -428,7 +428,7 @@ - 67 + 68 日志 @@ -445,7 +445,7 @@ - 167 + 168 标签 diff --git a/article/i-am-lost-in-the-colorful-world-3.html b/article/i-am-lost-in-the-colorful-world-3.html index bdcf24f2..8de63480 100644 --- a/article/i-am-lost-in-the-colorful-world-3.html +++ b/article/i-am-lost-in-the-colorful-world-3.html @@ -421,7 +421,7 @@ - 67 + 68 日志 @@ -438,7 +438,7 @@ - 167 + 168 标签 diff --git a/article/i-am-lost-in-the-colorful-world.html b/article/i-am-lost-in-the-colorful-world.html index 049c7052..59b4b5fa 100644 --- a/article/i-am-lost-in-the-colorful-world.html +++ b/article/i-am-lost-in-the-colorful-world.html @@ -428,7 +428,7 @@ - 67 + 68 日志 @@ -445,7 +445,7 @@ - 167 + 168 标签 diff --git a/article/iis-404-mime-type.html b/article/iis-404-mime-type.html index 00848e6d..749c091c 100644 --- a/article/iis-404-mime-type.html +++ b/article/iis-404-mime-type.html @@ -428,7 +428,7 @@ - 67 + 68 日志 @@ -445,7 +445,7 @@ - 167 + 168 标签 diff --git a/article/introduction-to-jade.html b/article/introduction-to-jade.html index 57a6a335..0065d2db 100644 --- a/article/introduction-to-jade.html +++ b/article/introduction-to-jade.html @@ -458,7 +458,7 @@ - 67 + 68 日志 @@ -475,7 +475,7 @@ - 167 + 168 标签 diff --git a/article/javascript-semicolons.html b/article/javascript-semicolons.html index 9ff96135..6e0aa6c9 100644 --- a/article/javascript-semicolons.html +++ b/article/javascript-semicolons.html @@ -618,7 +618,7 @@ - 67 + 68 日志 @@ -635,7 +635,7 @@ - 167 + 168 标签 diff --git a/article/js-operator-precedence.html b/article/js-operator-precedence.html index 53fe885f..13073dca 100644 --- a/article/js-operator-precedence.html +++ b/article/js-operator-precedence.html @@ -826,7 +826,7 @@ - 67 + 68 日志 @@ -843,7 +843,7 @@ - 167 + 168 标签 diff --git a/article/language-tags-in-html.html b/article/language-tags-in-html.html index 5e747fc8..3dd8d288 100644 --- a/article/language-tags-in-html.html +++ b/article/language-tags-in-html.html @@ -549,7 +549,7 @@ - 67 + 68 日志 @@ -566,7 +566,7 @@ - 167 + 168 标签 diff --git a/article/limitation-of-wechat-weapp.html b/article/limitation-of-wechat-weapp.html index 8f63c04a..6be76992 100644 --- a/article/limitation-of-wechat-weapp.html +++ b/article/limitation-of-wechat-weapp.html @@ -436,7 +436,7 @@ - 67 + 68 日志 @@ -453,7 +453,7 @@ - 167 + 168 标签 diff --git a/article/markdown-syntax.html b/article/markdown-syntax.html index 6a81110c..09a3c6b9 100644 --- a/article/markdown-syntax.html +++ b/article/markdown-syntax.html @@ -693,7 +693,7 @@ - 67 + 68 日志 @@ -710,7 +710,7 @@ - 167 + 168 标签 diff --git a/article/mathjax-advanced.html b/article/mathjax-advanced.html index d0c38eb9..d7475d24 100644 --- a/article/mathjax-advanced.html +++ b/article/mathjax-advanced.html @@ -591,7 +591,7 @@ 高亮 - 67 + 68 日志 @@ -608,7 +608,7 @@ 高亮 - 167 + 168 标签 diff --git a/article/mathjax-basic-tutorial-and-quick-reference.html b/article/mathjax-basic-tutorial-and-quick-reference.html index 1cede7fb..7fd56ba4 100644 --- a/article/mathjax-basic-tutorial-and-quick-reference.html +++ b/article/mathjax-basic-tutorial-and-quick-reference.html @@ -672,7 +672,7 @@ - 67 + 68 日志 @@ -689,7 +689,7 @@ - 167 + 168 标签 diff --git a/article/minify-static-html-files.html b/article/minify-static-html-files.html index baed98b5..455a0761 100644 --- a/article/minify-static-html-files.html +++ b/article/minify-static-html-files.html @@ -442,7 +442,7 @@ - 67 + 68 日志 @@ -459,7 +459,7 @@ - 167 + 168 标签 diff --git a/article/misunderstood-songs-for-mother.html b/article/misunderstood-songs-for-mother.html index c950b218..b8c0b83e 100644 --- a/article/misunderstood-songs-for-mother.html +++ b/article/misunderstood-songs-for-mother.html @@ -432,7 +432,7 @@ - 67 + 68 日志 @@ -449,7 +449,7 @@ - 167 + 168 标签 diff --git a/article/naming-rule.html b/article/naming-rule.html index dbf60b37..08372ebc 100644 --- a/article/naming-rule.html +++ b/article/naming-rule.html @@ -450,7 +450,7 @@ 总结 - 67 + 68 日志 @@ -467,7 +467,7 @@ 总结 - 167 + 168 标签 diff --git a/article/node-buffer.html b/article/node-buffer.html new file mode 100644 index 00000000..5dcff822 --- /dev/null +++ b/article/node-buffer.html @@ -0,0 +1,663 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Buffer 简介 | 清风轩 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 清风轩 + + + + 清风轩居 - 引仙阁 + + + + + + + + + + + + + + + + + + + + + + + 首页 + + + + + + + + + + 分类 + + + + + + + + + + 标签 + + + + + + + + + + 归档 + + + + + + + + + + 关于 + + + + + + + + + + 花园 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Buffer 简介 + + + + + + + + + + + 发表于 + + 2018-10-07 + + + + + + | + + + + 分类于 + + + + Node + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Buffer 是 node 中的一个全局对象,用来读取或者操作二进制数据流。 + + +本文直接使用 Buffer 讲述这个对象,该对象翻译成中文,可以叫做“缓冲区对象”。 + +官方文档的介绍如下: + +Prior to the introduction of TypedArray, the JavaScript language had no mechanism for reading or manipulating streams of binary data. The Buffer class was introduced as part of the Node.js API to enable interaction with octet streams in TCP streams, file system operations, and other contexts. + +在 ECMAScript 2015 引入 TypedArray 之后,Buffer 类使用了一种更优化、更适合 Node.js 用例的方式实现了 Uint8Array API。 +Buffer 类可以视作一个跟 Array 类一样的对象,区别在于 Buffer 的大小是固定的,且在 v8 堆外分配物理内存。Buffer 的大小在创建时就确定了,无法调整。 +先来看一段官方文档提供的示例代码: +1234567891011121314151617181920// Creates a zero-filled Buffer of length 10.const buf1 = Buffer.alloc(10);// Creates a Buffer of length 10, filled with 0x1.const buf2 = Buffer.alloc(10, 1);// Creates an uninitialized buffer of length 10.// This is faster than calling Buffer.alloc() but the returned// Buffer instance might contain old data that needs to be// overwritten using either fill() or write().const buf3 = Buffer.allocUnsafe(10);// Creates a Buffer containing [0x1, 0x2, 0x3].const buf4 = Buffer.from([1, 2, 3]);// Creates a Buffer containing UTF-8 bytes [0x74, 0xc3, 0xa9, 0x73, 0x74].const buf5 = Buffer.from('tést');// Creates a Buffer containing Latin-1 bytes [0x74, 0xe9, 0x73, 0x74].const buf6 = Buffer.from('tést', 'latin1'); + +Buffer 实例一般用于表示编码字符的序列,用过指定字符编码,可以在 Buffer 实例与常规 JavaScript 字符串之间进行相互转换。 +这样的描述,可能太过于抽象,直接上代码解释吧: +123456789function btoa(str) { const buf = Buffer.from(str); return buf.toString('base64');}function atob(str) { const buf = Buffer.from(str, 'base64'); return buf.toString();} +上面的代码,是 node 环境下关于浏览器 atob 和 btoa 方法的实现。 +Node.js 目前支持的字符编码如下: + +ascii,仅支持 7 位 ASCII 数据。utf8,多字节编码的 Unicode 字符。许多网页和其他文档格式都使用 UTF-8。utf16le,2 或 4 字节,little-endian(小字节序)编码的 Unicode 字符。支持 U+10000 到 U+10FFFF 的代理对。usc2,即 utf16le。base64,即 Base64 编码。latin1,即 Latin-1 编码,具体由 IANA 在 RFC1345 中定义。binary,即 latin1。hex,将每个字节编码为两个十六进制字符。 + +注意,现代浏览器遵循 WHATWG 编码标准 将 latin1 和 ISO-8859-1 别名为 win-1252。这意味着在进行例如 http.get() 获取到的字符编码在规范列表中,服务器可能返回 win-1252 编码的数据,此时如果使用 latin1 字符编码,可能会得到错误的解码数据。 + +Buffer 对象可以使用 for...of 进行内部迭代。同样的,实例对象的 .values、.keys 和 .entries 方法都可以创建迭代。 +使用 new 操作符实例化一个 Buffer 的方法已经被弃用。注意,已经弃用的方法本文不做介绍。 +创建 Buffer 实例的方法有下面几个: + +Buffer.from(array),从数组进行创建。Buffer.from(arrayBuffer[, byteOffset[, length]]),从 arrayBuffer 的实例进行创建。Buffer.from(buffer),直接根据 buffer 创建。Buffer.from(string[, encoding]),根据字符串创建,encoding 即使用的编码方式。Buffer.from(object[, offsetOrEncoding[, length]]),根据对象创建,该对象需要支持 Symbol.toPrimitive 或者有 valueOf() 方法,比如 Buffer.from(new String('this is a test'));。Buffer.alloc(size[, fill[, encoding]]),创建一个指定长度的对象,fill 表示用来填充新建的 Buffer 的值,默认为 0。Buffer.allocUnsafe(size),分配一个大小为 size 的对象。该方式创建的 Buffer 对象是未经过初始化的,内容未知,可能包含敏感数据。 + +更多的创建方式不在本文的记述范围之内。 + +创建之后的实例,我们使用 buf 进行表示,buf 的特性跟数组类似。 +buf.buffer 指向了 Buffer 底层的 ArrayBuffer 对象。 +可以使用 fill 方法进行填充,语法为 buf.fill(value[, offset[, end]][, encoding])。 +toString 可以将 buf 转为一个常规字符串,语法为 buf.toString([encoding[, start[, end]]])。 +之前做了一个简单的获取网页源代码的方法,代码大概是这样的: +12345678910111213141516171819202122const http = require('http');const https = require('https');module.exports = function getHTML(url) { return new Promise(function (resolve, reject) { (/^https/i.test(url) ? https : http).get(url, function (res) { var html = ''; res.on('data', function (chunk) { // chunk is a Buffer instance, use the method toString to get the string html += chunk; }); res.on('end', function () { resolve(html); }); }).on('error', function () { reject(); }); });} +是的,这里面的 chunk 对象就是一个 Buffer 实例。 + +真·简介。本文到此就结束了。 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/article/node-wxpay.html b/article/node-wxpay.html index 2b2f2c31..4b4fe087 100644 --- a/article/node-wxpay.html +++ b/article/node-wxpay.html @@ -477,7 +477,7 @@ - 67 + 68 日志 @@ -494,7 +494,7 @@ - 167 + 168 标签 diff --git a/article/normalize-cssreset.html b/article/normalize-cssreset.html index 642b1af6..600d2225 100644 --- a/article/normalize-cssreset.html +++ b/article/normalize-cssreset.html @@ -42,7 +42,7 @@ - + @@ -61,7 +61,7 @@ - + @@ -361,10 +361,10 @@ - #css - #normalize + #css + @@ -446,7 +446,7 @@ - 67 + 68 日志 @@ -463,7 +463,7 @@ - 167 + 168 标签 diff --git a/article/notes-for-git.html b/article/notes-for-git.html index 08c7eb6d..c79774f8 100644 --- a/article/notes-for-git.html +++ b/article/notes-for-git.html @@ -436,7 +436,7 @@ - 67 + 68 日志 @@ -453,7 +453,7 @@ - 167 + 168 标签 diff --git a/article/npm-alias.html b/article/npm-alias.html index 8205762d..d8bc7401 100644 --- a/article/npm-alias.html +++ b/article/npm-alias.html @@ -442,7 +442,7 @@ -B - 67 + 68 日志 @@ -459,7 +459,7 @@ -B - 167 + 168 标签 diff --git a/article/object-property-accessor.html b/article/object-property-accessor.html index a551d9ac..c133a148 100644 --- a/article/object-property-accessor.html +++ b/article/object-property-accessor.html @@ -432,7 +432,7 @@ - 67 + 68 日志 @@ -449,7 +449,7 @@ - 167 + 168 标签 diff --git a/article/parcel-bundler.html b/article/parcel-bundler.html index a2934ce8..2b84a87b 100644 --- a/article/parcel-bundler.html +++ b/article/parcel-bundler.html @@ -492,7 +492,7 @@ Licen - 67 + 68 日志 @@ -509,7 +509,7 @@ Licen - 167 + 168 标签 diff --git a/article/react-or-vue-which-javascript-ui-library-should-you-be-using.html b/article/react-or-vue-which-javascript-ui-library-should-you-be-using.html index eccde3bf..dfc01389 100644 --- a/article/react-or-vue-which-javascript-ui-library-should-you-be-using.html +++ b/article/react-or-vue-which-javascript-ui-library-should-you-be-using.html @@ -537,7 +537,7 @@ - 67 + 68 日志 @@ -554,7 +554,7 @@ - 167 + 168 标签 diff --git a/article/record-to-two-special-weddings.html b/article/record-to-two-special-weddings.html index 3b052ffc..c50df8e1 100644 --- a/article/record-to-two-special-weddings.html +++ b/article/record-to-two-special-weddings.html @@ -453,7 +453,7 @@ - 67 + 68 日志 @@ -470,7 +470,7 @@ - 167 + 168 标签 diff --git a/article/regexp-assertion.html b/article/regexp-assertion.html index dc3946cc..75ee03cd 100644 --- a/article/regexp-assertion.html +++ b/article/regexp-assertion.html @@ -457,7 +457,7 @@ - 67 + 68 日志 @@ -474,7 +474,7 @@ - 167 + 168 标签 diff --git a/article/rfc2616-is-obsoleted.html b/article/rfc2616-is-obsoleted.html index 33979d02..b0e46b26 100644 --- a/article/rfc2616-is-obsoleted.html +++ b/article/rfc2616-is-obsoleted.html @@ -459,7 +459,7 @@
@@ -545,7 +582,7 @@ - 67 + 68 日志 @@ -562,7 +599,7 @@ - 167 + 168 标签 diff --git a/article/10-educationists.html b/article/10-educationists.html index ca44fbe1..a804be44 100644 --- a/article/10-educationists.html +++ b/article/10-educationists.html @@ -473,7 +473,7 @@ - 67 + 68 日志 @@ -490,7 +490,7 @@ - 167 + 168 标签 diff --git a/article/a-business-trip-to-shenzhen.html b/article/a-business-trip-to-shenzhen.html index c2466445..3f0e0a38 100644 --- a/article/a-business-trip-to-shenzhen.html +++ b/article/a-business-trip-to-shenzhen.html @@ -447,7 +447,7 @@ - 67 + 68 日志 @@ -464,7 +464,7 @@ - 167 + 168 标签 diff --git a/article/a-fragment-code-of-multi-line-string-trim.html b/article/a-fragment-code-of-multi-line-string-trim.html index 8db3c37d..43d2fcae 100644 --- a/article/a-fragment-code-of-multi-line-string-trim.html +++ b/article/a-fragment-code-of-multi-line-string-trim.html @@ -430,7 +430,7 @@ - 67 + 68 日志 @@ -447,7 +447,7 @@ - 167 + 168 标签 diff --git a/article/a-logo-note-for-zte-fpga-design-department.html b/article/a-logo-note-for-zte-fpga-design-department.html index 55ebe7e1..1c5edf83 100644 --- a/article/a-logo-note-for-zte-fpga-design-department.html +++ b/article/a-logo-note-for-zte-fpga-design-department.html @@ -442,7 +442,7 @@ - 67 + 68 日志 @@ -459,7 +459,7 @@ - 167 + 168 标签 diff --git a/article/a-responsive-menu.html b/article/a-responsive-menu.html index 51fd973d..7720eba6 100644 --- a/article/a-responsive-menu.html +++ b/article/a-responsive-menu.html @@ -457,7 +457,7 @@ - 67 + 68 日志 @@ -474,7 +474,7 @@ - 167 + 168 标签 diff --git a/article/a-trap-about-object.html b/article/a-trap-about-object.html index 1c34df46..9b97690f 100644 --- a/article/a-trap-about-object.html +++ b/article/a-trap-about-object.html @@ -431,7 +431,7 @@ - 67 + 68 日志 @@ -448,7 +448,7 @@ - 167 + 168 标签 diff --git a/article/abbreviation.html b/article/abbreviation.html index c5f15a84..2c03b673 100644 --- a/article/abbreviation.html +++ b/article/abbreviation.html @@ -467,7 +467,7 @@ - 67 + 68 日志 @@ -484,7 +484,7 @@ - 167 + 168 标签 diff --git a/article/angular-function.html b/article/angular-function.html index bd572275..4ccc7964 100644 --- a/article/angular-function.html +++ b/article/angular-function.html @@ -476,7 +476,7 @@ - 67 + 68 日志 @@ -493,7 +493,7 @@ - 167 + 168 标签 diff --git a/article/audit-time-of-beian.html b/article/audit-time-of-beian.html index a516e61e..a1057143 100644 --- a/article/audit-time-of-beian.html +++ b/article/audit-time-of-beian.html @@ -558,7 +558,7 @@ - 67 + 68 日志 @@ -575,7 +575,7 @@ - 167 + 168 标签 diff --git a/article/book-review-about-the-bio-of-du-yue-sheng.html b/article/book-review-about-the-bio-of-du-yue-sheng.html index 7113f1c6..31256b2d 100644 --- a/article/book-review-about-the-bio-of-du-yue-sheng.html +++ b/article/book-review-about-the-bio-of-du-yue-sheng.html @@ -449,7 +449,7 @@ - 67 + 68 日志 @@ -466,7 +466,7 @@ - 167 + 168 标签 diff --git a/article/brackets-emmet.html b/article/brackets-emmet.html index d63d4a93..dbc596ff 100644 --- a/article/brackets-emmet.html +++ b/article/brackets-emmet.html @@ -458,7 +458,7 @@ - 67 + 68 日志 @@ -475,7 +475,7 @@ - 167 + 168 标签 diff --git a/article/code-golf-output-fit-numbers.html b/article/code-golf-output-fit-numbers.html index 7d682443..e383b146 100644 --- a/article/code-golf-output-fit-numbers.html +++ b/article/code-golf-output-fit-numbers.html @@ -485,7 +485,7 @@ 后记 - 67 + 68 日志 @@ -502,7 +502,7 @@ 后记 - 167 + 168 标签 diff --git a/article/css-animation.html b/article/css-animation.html index 35b11ee0..ba3a0c72 100644 --- a/article/css-animation.html +++ b/article/css-animation.html @@ -480,7 +480,7 @@ - 67 + 68 日志 @@ -497,7 +497,7 @@ - 167 + 168 标签 diff --git a/article/css-flex.html b/article/css-flex.html index bc6dad82..633866fc 100644 --- a/article/css-flex.html +++ b/article/css-flex.html @@ -513,7 +513,7 @@ - 67 + 68 日志 @@ -530,7 +530,7 @@ - 167 + 168 标签 diff --git a/article/css-specificity.html b/article/css-specificity.html index 7cd9651d..07f5d493 100644 --- a/article/css-specificity.html +++ b/article/css-specificity.html @@ -42,7 +42,7 @@ - + @@ -61,7 +61,7 @@ - + @@ -467,10 +467,10 @@ - #前端 - #css + #前端 + #specificity @@ -558,7 +558,7 @@ - 67 + 68 日志 @@ -575,7 +575,7 @@ - 167 + 168 标签 diff --git a/article/dom-nodetype.html b/article/dom-nodetype.html index f0e17e78..e910a0b4 100644 --- a/article/dom-nodetype.html +++ b/article/dom-nodetype.html @@ -447,7 +447,7 @@ 示例 - 67 + 68 日志 @@ -464,7 +464,7 @@ 示例 - 167 + 168 标签 diff --git a/article/double-eleven-an-ironic-day.html b/article/double-eleven-an-ironic-day.html index c07241d8..a118da09 100644 --- a/article/double-eleven-an-ironic-day.html +++ b/article/double-eleven-an-ironic-day.html @@ -441,7 +441,7 @@ - 67 + 68 日志 @@ -458,7 +458,7 @@ - 167 + 168 标签 diff --git a/article/equal-loan-payment.html b/article/equal-loan-payment.html index 8121135f..542667bf 100644 --- a/article/equal-loan-payment.html +++ b/article/equal-loan-payment.html @@ -446,7 +446,7 @@ - 67 + 68 日志 @@ -463,7 +463,7 @@ - 167 + 168 标签 diff --git a/article/es6-modules.html b/article/es6-modules.html index fc7b7d51..618bab5f 100644 --- a/article/es6-modules.html +++ b/article/es6-modules.html @@ -68,7 +68,7 @@ - + @@ -396,6 +396,10 @@ + + Buffer 简介 + + @@ -462,7 +466,7 @@ - 67 + 68 日志 @@ -479,7 +483,7 @@ - 167 + 168 标签 diff --git a/article/es8.html b/article/es8.html index 8ddeca51..800d97e1 100644 --- a/article/es8.html +++ b/article/es8.html @@ -469,7 +469,7 @@ - 67 + 68 日志 @@ -486,7 +486,7 @@ - 167 + 168 标签 diff --git a/article/excel-even-odd-color.html b/article/excel-even-odd-color.html index fc054f40..1bed15c1 100644 --- a/article/excel-even-odd-color.html +++ b/article/excel-even-odd-color.html @@ -422,7 +422,7 @@ - 67 + 68 日志 @@ -439,7 +439,7 @@ - 167 + 168 标签 diff --git a/article/fix-foxmail-receive-password-error.html b/article/fix-foxmail-receive-password-error.html index f0052291..bbc918f2 100644 --- a/article/fix-foxmail-receive-password-error.html +++ b/article/fix-foxmail-receive-password-error.html @@ -463,7 +463,7 @@ - 67 + 68 日志 @@ -480,7 +480,7 @@ - 167 + 168 标签 diff --git a/article/flex-compability.html b/article/flex-compability.html index 5a66afe8..d1c906b1 100644 --- a/article/flex-compability.html +++ b/article/flex-compability.html @@ -440,7 +440,7 @@ - 67 + 68 日志 @@ -457,7 +457,7 @@ - 167 + 168 标签 diff --git a/article/gulp-4.html b/article/gulp-4.html index 3446433b..992ad623 100644 --- a/article/gulp-4.html +++ b/article/gulp-4.html @@ -435,7 +435,7 @@ - 67 + 68 日志 @@ -452,7 +452,7 @@ - 167 + 168 标签 diff --git a/article/how-technology-hijacks-poeples-minds.html b/article/how-technology-hijacks-poeples-minds.html index baaeb30d..62d86476 100644 --- a/article/how-technology-hijacks-poeples-minds.html +++ b/article/how-technology-hijacks-poeples-minds.html @@ -539,7 +539,7 @@ - 67 + 68 日志 @@ -556,7 +556,7 @@ - 167 + 168 标签 diff --git a/article/htree.html b/article/htree.html index 05e13d32..7af8468e 100644 --- a/article/htree.html +++ b/article/htree.html @@ -42,7 +42,7 @@ - + @@ -61,7 +61,7 @@ - + @@ -383,12 +383,12 @@ #npm - #node - #tree #htree + #node + #CLI @@ -476,7 +476,7 @@ - 67 + 68 日志 @@ -493,7 +493,7 @@ - 167 + 168 标签 diff --git a/article/http-status-code.html b/article/http-status-code.html index 6145765a..0ac7d3bf 100644 --- a/article/http-status-code.html +++ b/article/http-status-code.html @@ -1339,7 +1339,7 @@ - 67 + 68 日志 @@ -1356,7 +1356,7 @@ - 167 + 168 标签 diff --git a/article/i-am-lost-in-the-colorful-world-2.html b/article/i-am-lost-in-the-colorful-world-2.html index 65a5f5aa..8ead7f76 100644 --- a/article/i-am-lost-in-the-colorful-world-2.html +++ b/article/i-am-lost-in-the-colorful-world-2.html @@ -428,7 +428,7 @@ - 67 + 68 日志 @@ -445,7 +445,7 @@ - 167 + 168 标签 diff --git a/article/i-am-lost-in-the-colorful-world-3.html b/article/i-am-lost-in-the-colorful-world-3.html index bdcf24f2..8de63480 100644 --- a/article/i-am-lost-in-the-colorful-world-3.html +++ b/article/i-am-lost-in-the-colorful-world-3.html @@ -421,7 +421,7 @@ - 67 + 68 日志 @@ -438,7 +438,7 @@ - 167 + 168 标签 diff --git a/article/i-am-lost-in-the-colorful-world.html b/article/i-am-lost-in-the-colorful-world.html index 049c7052..59b4b5fa 100644 --- a/article/i-am-lost-in-the-colorful-world.html +++ b/article/i-am-lost-in-the-colorful-world.html @@ -428,7 +428,7 @@ - 67 + 68 日志 @@ -445,7 +445,7 @@ - 167 + 168 标签 diff --git a/article/iis-404-mime-type.html b/article/iis-404-mime-type.html index 00848e6d..749c091c 100644 --- a/article/iis-404-mime-type.html +++ b/article/iis-404-mime-type.html @@ -428,7 +428,7 @@ - 67 + 68 日志 @@ -445,7 +445,7 @@ - 167 + 168 标签 diff --git a/article/introduction-to-jade.html b/article/introduction-to-jade.html index 57a6a335..0065d2db 100644 --- a/article/introduction-to-jade.html +++ b/article/introduction-to-jade.html @@ -458,7 +458,7 @@ - 67 + 68 日志 @@ -475,7 +475,7 @@ - 167 + 168 标签 diff --git a/article/javascript-semicolons.html b/article/javascript-semicolons.html index 9ff96135..6e0aa6c9 100644 --- a/article/javascript-semicolons.html +++ b/article/javascript-semicolons.html @@ -618,7 +618,7 @@ - 67 + 68 日志 @@ -635,7 +635,7 @@ - 167 + 168 标签 diff --git a/article/js-operator-precedence.html b/article/js-operator-precedence.html index 53fe885f..13073dca 100644 --- a/article/js-operator-precedence.html +++ b/article/js-operator-precedence.html @@ -826,7 +826,7 @@ - 67 + 68 日志 @@ -843,7 +843,7 @@ - 167 + 168 标签 diff --git a/article/language-tags-in-html.html b/article/language-tags-in-html.html index 5e747fc8..3dd8d288 100644 --- a/article/language-tags-in-html.html +++ b/article/language-tags-in-html.html @@ -549,7 +549,7 @@ - 67 + 68 日志 @@ -566,7 +566,7 @@ - 167 + 168 标签 diff --git a/article/limitation-of-wechat-weapp.html b/article/limitation-of-wechat-weapp.html index 8f63c04a..6be76992 100644 --- a/article/limitation-of-wechat-weapp.html +++ b/article/limitation-of-wechat-weapp.html @@ -436,7 +436,7 @@ - 67 + 68 日志 @@ -453,7 +453,7 @@ - 167 + 168 标签 diff --git a/article/markdown-syntax.html b/article/markdown-syntax.html index 6a81110c..09a3c6b9 100644 --- a/article/markdown-syntax.html +++ b/article/markdown-syntax.html @@ -693,7 +693,7 @@ - 67 + 68 日志 @@ -710,7 +710,7 @@ - 167 + 168 标签 diff --git a/article/mathjax-advanced.html b/article/mathjax-advanced.html index d0c38eb9..d7475d24 100644 --- a/article/mathjax-advanced.html +++ b/article/mathjax-advanced.html @@ -591,7 +591,7 @@ 高亮 - 67 + 68 日志 @@ -608,7 +608,7 @@ 高亮 - 167 + 168 标签 diff --git a/article/mathjax-basic-tutorial-and-quick-reference.html b/article/mathjax-basic-tutorial-and-quick-reference.html index 1cede7fb..7fd56ba4 100644 --- a/article/mathjax-basic-tutorial-and-quick-reference.html +++ b/article/mathjax-basic-tutorial-and-quick-reference.html @@ -672,7 +672,7 @@ - 67 + 68 日志 @@ -689,7 +689,7 @@ - 167 + 168 标签 diff --git a/article/minify-static-html-files.html b/article/minify-static-html-files.html index baed98b5..455a0761 100644 --- a/article/minify-static-html-files.html +++ b/article/minify-static-html-files.html @@ -442,7 +442,7 @@ - 67 + 68 日志 @@ -459,7 +459,7 @@ - 167 + 168 标签 diff --git a/article/misunderstood-songs-for-mother.html b/article/misunderstood-songs-for-mother.html index c950b218..b8c0b83e 100644 --- a/article/misunderstood-songs-for-mother.html +++ b/article/misunderstood-songs-for-mother.html @@ -432,7 +432,7 @@ - 67 + 68 日志 @@ -449,7 +449,7 @@ - 167 + 168 标签 diff --git a/article/naming-rule.html b/article/naming-rule.html index dbf60b37..08372ebc 100644 --- a/article/naming-rule.html +++ b/article/naming-rule.html @@ -450,7 +450,7 @@ 总结 - 67 + 68 日志 @@ -467,7 +467,7 @@ 总结 - 167 + 168 标签 diff --git a/article/node-buffer.html b/article/node-buffer.html new file mode 100644 index 00000000..5dcff822 --- /dev/null +++ b/article/node-buffer.html @@ -0,0 +1,663 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Buffer 简介 | 清风轩 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 清风轩 + + + + 清风轩居 - 引仙阁 + + + + + + + + + + + + + + + + + + + + + + + 首页 + + + + + + + + + + 分类 + + + + + + + + + + 标签 + + + + + + + + + + 归档 + + + + + + + + + + 关于 + + + + + + + + + + 花园 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Buffer 简介 + + + + + + + + + + + 发表于 + + 2018-10-07 + + + + + + | + + + + 分类于 + + + + Node + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Buffer 是 node 中的一个全局对象,用来读取或者操作二进制数据流。 + + +本文直接使用 Buffer 讲述这个对象,该对象翻译成中文,可以叫做“缓冲区对象”。 + +官方文档的介绍如下: + +Prior to the introduction of TypedArray, the JavaScript language had no mechanism for reading or manipulating streams of binary data. The Buffer class was introduced as part of the Node.js API to enable interaction with octet streams in TCP streams, file system operations, and other contexts. + +在 ECMAScript 2015 引入 TypedArray 之后,Buffer 类使用了一种更优化、更适合 Node.js 用例的方式实现了 Uint8Array API。 +Buffer 类可以视作一个跟 Array 类一样的对象,区别在于 Buffer 的大小是固定的,且在 v8 堆外分配物理内存。Buffer 的大小在创建时就确定了,无法调整。 +先来看一段官方文档提供的示例代码: +1234567891011121314151617181920// Creates a zero-filled Buffer of length 10.const buf1 = Buffer.alloc(10);// Creates a Buffer of length 10, filled with 0x1.const buf2 = Buffer.alloc(10, 1);// Creates an uninitialized buffer of length 10.// This is faster than calling Buffer.alloc() but the returned// Buffer instance might contain old data that needs to be// overwritten using either fill() or write().const buf3 = Buffer.allocUnsafe(10);// Creates a Buffer containing [0x1, 0x2, 0x3].const buf4 = Buffer.from([1, 2, 3]);// Creates a Buffer containing UTF-8 bytes [0x74, 0xc3, 0xa9, 0x73, 0x74].const buf5 = Buffer.from('tést');// Creates a Buffer containing Latin-1 bytes [0x74, 0xe9, 0x73, 0x74].const buf6 = Buffer.from('tést', 'latin1'); + +Buffer 实例一般用于表示编码字符的序列,用过指定字符编码,可以在 Buffer 实例与常规 JavaScript 字符串之间进行相互转换。 +这样的描述,可能太过于抽象,直接上代码解释吧: +123456789function btoa(str) { const buf = Buffer.from(str); return buf.toString('base64');}function atob(str) { const buf = Buffer.from(str, 'base64'); return buf.toString();} +上面的代码,是 node 环境下关于浏览器 atob 和 btoa 方法的实现。 +Node.js 目前支持的字符编码如下: + +ascii,仅支持 7 位 ASCII 数据。utf8,多字节编码的 Unicode 字符。许多网页和其他文档格式都使用 UTF-8。utf16le,2 或 4 字节,little-endian(小字节序)编码的 Unicode 字符。支持 U+10000 到 U+10FFFF 的代理对。usc2,即 utf16le。base64,即 Base64 编码。latin1,即 Latin-1 编码,具体由 IANA 在 RFC1345 中定义。binary,即 latin1。hex,将每个字节编码为两个十六进制字符。 + +注意,现代浏览器遵循 WHATWG 编码标准 将 latin1 和 ISO-8859-1 别名为 win-1252。这意味着在进行例如 http.get() 获取到的字符编码在规范列表中,服务器可能返回 win-1252 编码的数据,此时如果使用 latin1 字符编码,可能会得到错误的解码数据。 + +Buffer 对象可以使用 for...of 进行内部迭代。同样的,实例对象的 .values、.keys 和 .entries 方法都可以创建迭代。 +使用 new 操作符实例化一个 Buffer 的方法已经被弃用。注意,已经弃用的方法本文不做介绍。 +创建 Buffer 实例的方法有下面几个: + +Buffer.from(array),从数组进行创建。Buffer.from(arrayBuffer[, byteOffset[, length]]),从 arrayBuffer 的实例进行创建。Buffer.from(buffer),直接根据 buffer 创建。Buffer.from(string[, encoding]),根据字符串创建,encoding 即使用的编码方式。Buffer.from(object[, offsetOrEncoding[, length]]),根据对象创建,该对象需要支持 Symbol.toPrimitive 或者有 valueOf() 方法,比如 Buffer.from(new String('this is a test'));。Buffer.alloc(size[, fill[, encoding]]),创建一个指定长度的对象,fill 表示用来填充新建的 Buffer 的值,默认为 0。Buffer.allocUnsafe(size),分配一个大小为 size 的对象。该方式创建的 Buffer 对象是未经过初始化的,内容未知,可能包含敏感数据。 + +更多的创建方式不在本文的记述范围之内。 + +创建之后的实例,我们使用 buf 进行表示,buf 的特性跟数组类似。 +buf.buffer 指向了 Buffer 底层的 ArrayBuffer 对象。 +可以使用 fill 方法进行填充,语法为 buf.fill(value[, offset[, end]][, encoding])。 +toString 可以将 buf 转为一个常规字符串,语法为 buf.toString([encoding[, start[, end]]])。 +之前做了一个简单的获取网页源代码的方法,代码大概是这样的: +12345678910111213141516171819202122const http = require('http');const https = require('https');module.exports = function getHTML(url) { return new Promise(function (resolve, reject) { (/^https/i.test(url) ? https : http).get(url, function (res) { var html = ''; res.on('data', function (chunk) { // chunk is a Buffer instance, use the method toString to get the string html += chunk; }); res.on('end', function () { resolve(html); }); }).on('error', function () { reject(); }); });} +是的,这里面的 chunk 对象就是一个 Buffer 实例。 + +真·简介。本文到此就结束了。 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/article/node-wxpay.html b/article/node-wxpay.html index 2b2f2c31..4b4fe087 100644 --- a/article/node-wxpay.html +++ b/article/node-wxpay.html @@ -477,7 +477,7 @@ - 67 + 68 日志 @@ -494,7 +494,7 @@ - 167 + 168 标签 diff --git a/article/normalize-cssreset.html b/article/normalize-cssreset.html index 642b1af6..600d2225 100644 --- a/article/normalize-cssreset.html +++ b/article/normalize-cssreset.html @@ -42,7 +42,7 @@ - + @@ -61,7 +61,7 @@ - + @@ -361,10 +361,10 @@ - #css - #normalize + #css + @@ -446,7 +446,7 @@ - 67 + 68 日志 @@ -463,7 +463,7 @@ - 167 + 168 标签 diff --git a/article/notes-for-git.html b/article/notes-for-git.html index 08c7eb6d..c79774f8 100644 --- a/article/notes-for-git.html +++ b/article/notes-for-git.html @@ -436,7 +436,7 @@ - 67 + 68 日志 @@ -453,7 +453,7 @@ - 167 + 168 标签 diff --git a/article/npm-alias.html b/article/npm-alias.html index 8205762d..d8bc7401 100644 --- a/article/npm-alias.html +++ b/article/npm-alias.html @@ -442,7 +442,7 @@ -B - 67 + 68 日志 @@ -459,7 +459,7 @@ -B - 167 + 168 标签 diff --git a/article/object-property-accessor.html b/article/object-property-accessor.html index a551d9ac..c133a148 100644 --- a/article/object-property-accessor.html +++ b/article/object-property-accessor.html @@ -432,7 +432,7 @@ - 67 + 68 日志 @@ -449,7 +449,7 @@ - 167 + 168 标签 diff --git a/article/parcel-bundler.html b/article/parcel-bundler.html index a2934ce8..2b84a87b 100644 --- a/article/parcel-bundler.html +++ b/article/parcel-bundler.html @@ -492,7 +492,7 @@ Licen - 67 + 68 日志 @@ -509,7 +509,7 @@ Licen - 167 + 168 标签 diff --git a/article/react-or-vue-which-javascript-ui-library-should-you-be-using.html b/article/react-or-vue-which-javascript-ui-library-should-you-be-using.html index eccde3bf..dfc01389 100644 --- a/article/react-or-vue-which-javascript-ui-library-should-you-be-using.html +++ b/article/react-or-vue-which-javascript-ui-library-should-you-be-using.html @@ -537,7 +537,7 @@ - 67 + 68 日志 @@ -554,7 +554,7 @@ - 167 + 168 标签 diff --git a/article/record-to-two-special-weddings.html b/article/record-to-two-special-weddings.html index 3b052ffc..c50df8e1 100644 --- a/article/record-to-two-special-weddings.html +++ b/article/record-to-two-special-weddings.html @@ -453,7 +453,7 @@ - 67 + 68 日志 @@ -470,7 +470,7 @@ - 167 + 168 标签 diff --git a/article/regexp-assertion.html b/article/regexp-assertion.html index dc3946cc..75ee03cd 100644 --- a/article/regexp-assertion.html +++ b/article/regexp-assertion.html @@ -457,7 +457,7 @@ - 67 + 68 日志 @@ -474,7 +474,7 @@ - 167 + 168 标签 diff --git a/article/rfc2616-is-obsoleted.html b/article/rfc2616-is-obsoleted.html index 33979d02..b0e46b26 100644 --- a/article/rfc2616-is-obsoleted.html +++ b/article/rfc2616-is-obsoleted.html @@ -459,7 +459,7 @@
+ + + + + + + + + + + Buffer 简介 + + + + + + + + + + + 发表于 + + 2018-10-07 + + + + + + | + + + + 分类于 + + + + Node + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Buffer 是 node 中的一个全局对象,用来读取或者操作二进制数据流。 + + +本文直接使用 Buffer 讲述这个对象,该对象翻译成中文,可以叫做“缓冲区对象”。 + +官方文档的介绍如下: + +Prior to the introduction of TypedArray, the JavaScript language had no mechanism for reading or manipulating streams of binary data. The Buffer class was introduced as part of the Node.js API to enable interaction with octet streams in TCP streams, file system operations, and other contexts. + +在 ECMAScript 2015 引入 TypedArray 之后,Buffer 类使用了一种更优化、更适合 Node.js 用例的方式实现了 Uint8Array API。 +Buffer 类可以视作一个跟 Array 类一样的对象,区别在于 Buffer 的大小是固定的,且在 v8 堆外分配物理内存。Buffer 的大小在创建时就确定了,无法调整。 +先来看一段官方文档提供的示例代码: +1234567891011121314151617181920// Creates a zero-filled Buffer of length 10.const buf1 = Buffer.alloc(10);// Creates a Buffer of length 10, filled with 0x1.const buf2 = Buffer.alloc(10, 1);// Creates an uninitialized buffer of length 10.// This is faster than calling Buffer.alloc() but the returned// Buffer instance might contain old data that needs to be// overwritten using either fill() or write().const buf3 = Buffer.allocUnsafe(10);// Creates a Buffer containing [0x1, 0x2, 0x3].const buf4 = Buffer.from([1, 2, 3]);// Creates a Buffer containing UTF-8 bytes [0x74, 0xc3, 0xa9, 0x73, 0x74].const buf5 = Buffer.from('tést');// Creates a Buffer containing Latin-1 bytes [0x74, 0xe9, 0x73, 0x74].const buf6 = Buffer.from('tést', 'latin1'); + +Buffer 实例一般用于表示编码字符的序列,用过指定字符编码,可以在 Buffer 实例与常规 JavaScript 字符串之间进行相互转换。 +这样的描述,可能太过于抽象,直接上代码解释吧: +123456789function btoa(str) { const buf = Buffer.from(str); return buf.toString('base64');}function atob(str) { const buf = Buffer.from(str, 'base64'); return buf.toString();} +上面的代码,是 node 环境下关于浏览器 atob 和 btoa 方法的实现。 +Node.js 目前支持的字符编码如下: + +ascii,仅支持 7 位 ASCII 数据。utf8,多字节编码的 Unicode 字符。许多网页和其他文档格式都使用 UTF-8。utf16le,2 或 4 字节,little-endian(小字节序)编码的 Unicode 字符。支持 U+10000 到 U+10FFFF 的代理对。usc2,即 utf16le。base64,即 Base64 编码。latin1,即 Latin-1 编码,具体由 IANA 在 RFC1345 中定义。binary,即 latin1。hex,将每个字节编码为两个十六进制字符。 + +注意,现代浏览器遵循 WHATWG 编码标准 将 latin1 和 ISO-8859-1 别名为 win-1252。这意味着在进行例如 http.get() 获取到的字符编码在规范列表中,服务器可能返回 win-1252 编码的数据,此时如果使用 latin1 字符编码,可能会得到错误的解码数据。 + +Buffer 对象可以使用 for...of 进行内部迭代。同样的,实例对象的 .values、.keys 和 .entries 方法都可以创建迭代。 +使用 new 操作符实例化一个 Buffer 的方法已经被弃用。注意,已经弃用的方法本文不做介绍。 +创建 Buffer 实例的方法有下面几个: + +Buffer.from(array),从数组进行创建。Buffer.from(arrayBuffer[, byteOffset[, length]]),从 arrayBuffer 的实例进行创建。Buffer.from(buffer),直接根据 buffer 创建。Buffer.from(string[, encoding]),根据字符串创建,encoding 即使用的编码方式。Buffer.from(object[, offsetOrEncoding[, length]]),根据对象创建,该对象需要支持 Symbol.toPrimitive 或者有 valueOf() 方法,比如 Buffer.from(new String('this is a test'));。Buffer.alloc(size[, fill[, encoding]]),创建一个指定长度的对象,fill 表示用来填充新建的 Buffer 的值,默认为 0。Buffer.allocUnsafe(size),分配一个大小为 size 的对象。该方式创建的 Buffer 对象是未经过初始化的,内容未知,可能包含敏感数据。 + +更多的创建方式不在本文的记述范围之内。 + +创建之后的实例,我们使用 buf 进行表示,buf 的特性跟数组类似。 +buf.buffer 指向了 Buffer 底层的 ArrayBuffer 对象。 +可以使用 fill 方法进行填充,语法为 buf.fill(value[, offset[, end]][, encoding])。 +toString 可以将 buf 转为一个常规字符串,语法为 buf.toString([encoding[, start[, end]]])。 +之前做了一个简单的获取网页源代码的方法,代码大概是这样的: +12345678910111213141516171819202122const http = require('http');const https = require('https');module.exports = function getHTML(url) { return new Promise(function (resolve, reject) { (/^https/i.test(url) ? https : http).get(url, function (res) { var html = ''; res.on('data', function (chunk) { // chunk is a Buffer instance, use the method toString to get the string html += chunk; }); res.on('end', function () { resolve(html); }); }).on('error', function () { reject(); }); });} +是的,这里面的 chunk 对象就是一个 Buffer 实例。 + +真·简介。本文到此就结束了。 + + + + + + + + + + + + + + + + + + +