• Node.js 21.7.0 发布:内置彩色文本输出、环境变量功能增强、crypto 增加新 hash 方法...


    1. 内置方法输出彩色文本

    意味着通过 console.log 可以间接的输出彩色文本了,不用再引入三方库了。

    1. const { styleText } = require('node:util');
    2. const errorMessage = styleText('red''Error! Error!');
    3. console.log(errorMessage);

    2. 加载和解析环境变量

    通过两个 API 支持加载和解析环境变量:process.loadEnvFile(path)util.parseEnv(content)

    3. .env 文件支持多行值

    1. MULTI_LINE="HELLO
    2. WORLD"

    4. sea:支持嵌入资产 assets

    这个 sea 以前没注意过,原来是 Node.js 又增加了一个新模块,sea 的全称为 Single executable applications,即单一执行模块。后面单独写个文章来介绍下~

    以下是本次 Node.js 的更新:

    “用户现在可以通过将键路径字典添加到配置作为资产字段来包含资产。在构建时,Node.js 将从指定路径读取资源并将它们捆绑到准备的 blob 中。在生成的可执行文件中,用户可以使用 sea.getAsset()sea.getAssetAsBlob() API 检索资产。”

    1. {
    2.   "main""/path/to/bundled/script.js",
    3.   "output""/path/to/write/the/generated/blob.blob",
    4.   "assets": {
    5.     "a.jpg""/path/to/a.jpg",
    6.     "b.txt""/path/to/b.txt"
    7.   }
    8. }
    1. const { getAsset } = require('node:sea');
    2. // Returns a copy of the data in an ArrayBuffer
    3. const image = getAsset('a.jpg');
    4. // Returns a string decoded from the asset as UTF8.
    5. const text = getAsset('b.txt''utf8');
    6. // Returns a Blob containing the asset without copying.
    7. const blob = getAssetAsBlob('a.jpg');

    4. vm:支持使用默认加载器来处理动态 import()

    第一步,将 test.jstest.txt 写入当前运行脚本所在目录。

    第二步,编译一个加载 test.mjstest.json 的脚本,就好像该脚本放置在同一目录中一样。

    1. const { Script, constants } = require('node:vm');
    2. const { resolve } = require('node:path');
    3. const { writeFileSync } = require('node:fs');
    4. // Write test.js and test.txt to the directory where the current script
    5. // being run is located.
    6. writeFileSync(
    7.   resolve(__dirname, 'test.mjs'),
    8.   'export const filename = "./test.json";'
    9. );
    10. writeFileSync(resolve(__dirname, 'test.json'), '{"hello": "world"}');
    11. // Compile a script that loads test.mjs and then test.json
    12. // as if the script is placed in the same directory.
    13. const script = new Script(
    14.   `(async function() {
    15.     const { filename } = await import('./test.mjs');
    16.     return import(filename, { with: { type: 'json' } })
    17.   })();`,
    18.   {
    19.     filename: resolve(__dirname, 'test-with-default.js'),
    20.     importModuleDynamically: constants.USE_MAIN_CONTEXT_DEFAULT_LOADER,
    21.   }
    22. );
    23. // { default: { hello: 'world' } }
    24. script.runInThisContext().then(console.log);

    5. 新增 crypto.hash() 方法

    crypto.hash()crypto.createHash() 快 1.2 - 2 倍,并且由于不会创建中间对象,因此会产生更少的内存开销

    1. const crypto = require('node:crypto');
    2. // Hashing a string and return the result as a hex-encoded string.
    3. const string = 'Node.js';
    4. // 10b3493287f831e81a438811a1ffba01f8cec4b7
    5. console.log(crypto.hash('sha1'string));

    参考 https://nodejs.org/en/blog/release/v21.7.0

  • 相关阅读:
    ruby对比python,30分钟教程
    【LeetCode刷题】-- 50.Pow(x,n)
    Java学习笔记(三十)
    Matlab信号处理:FFT频谱分辨率
    【Rust 笔记】13-迭代器(中)
    【Pytorch】广播机制
    请根据该图片写出代码
    二十二、环境变量和模式
    HDU - 1114 Piggy-Bank(完全背包)
    感谢有你 | FISCO BCOS 2022年第二季度贡献榜单
  • 原文地址:https://blog.csdn.net/weixin_44829437/article/details/136576523