The Future of Programming in Node.js

The Future of Programming in Node.js

Isaac Schlueter :

There’s been a lot of debates, theories, and requests about Node’s core API patterns on this list lately. I’d like to clarify the actual plans of the project on these points.

Callbacks will remain the de facto way to implement asynchrony. Generators and Promises are interesting and will remain a userland option.

Streams are more consistent, faster, and 100% backwards compatible as of 0.10. The “compatibility mode” aka “old mode” is folded into the API more cleanly. You can pause() a flowing stream, and then start calling read() again safely. We’ll keep exposing streams from core APIs, and advocating extending the stream base classes in userland programs.

Domains will be refactored to support more generic continuation-tracking systems, to enable alternative error-handling mechanisms in userland. Eventually the Domain module will be a thing that could be done in userland, but it will continue to be bundled with the Node binary.

There will be no changes to the module system. None. It’s finished. It’s been finished for over a year. Any proposed changes must come with a clear reproducible bug that cannot be worked around or addressed with documentation.

TypeScript and CoffeeScript will not be added to core. However, as the module system will remain locked, anything that works today will keep working.

A stable C API shim will be added such that binary addons can be code-stable (and perhaps, binary-stable) across stable branches of Node for the most common use-cases. Note that the V8 API changed significantly in 0.12, so basically every binary addon is broken today. Also, it’s very difficult to bind to the appropriate openssl/zlib/c-ares/etc. We’re addressing that.

As new language features are added to V8, they’ll make their way into Node. We have no plans to “auto-enable” any specific flags. Sniff for what you need, and throw with a helpful error message if it’s not available.

The VM module is being refactored to bring the features of the "contextify" module into core, so that contexts work like everyone expects that they should. Multi-context support is being added to the VM module API as well.

Synchronous child process execution is being added, at long last.

v0.12 is nearing feature completion. Once we get there, we’ll be trying to get everyone to use it. After v0.12, there will probably not be any API changes before v1.0. Between v0.12 and v1.0, we’ll be focusing on performance, bug fixing, and stability.

We are done making breaking changes at the Node.js layer. If your program runs today, we’re doing everything we can to make sure that it will run next year, albeit faster and more reliably.

This is not a democracy. However, there’s plenty of room for everyone’s opinion. If you want to make exciting dramatic breaking changes to node-core, and you can’t find satisfaction by writing userland npm modules, then please fork joyent/node, give it a new name and logo, and go as completely crazy as you want.

OSS FTW.

原文: https://groups.google.com/forum/#!topic/nodejs/9afurRCTlOc


3 回复

The Future of Programming in Node.js

Node.js has been a cornerstone of modern web development, enabling developers to write scalable and high-performance applications using JavaScript. The future of programming in Node.js looks promising, with several key areas of focus that aim to enhance its capabilities while maintaining backward compatibility.

Asynchronous Programming with Callbacks

One of the core principles of Node.js is its non-blocking I/O model, which is primarily implemented through callbacks. Despite the emergence of newer paradigms like generators and promises, Isaac Schlueter clarifies that callbacks will remain the de facto standard for asynchronous operations in Node.js.

fs.readFile('example.txt', function(err, data) {
    if (err) throw err;
    console.log(data.toString());
});

This code snippet reads a file asynchronously and logs its content. While promises and async/await provide cleaner syntax, callbacks will continue to be the primary method for handling asynchronous operations in Node.js core APIs.

Stream Improvements

Streams are a fundamental part of Node.js, used for handling large data sets efficiently. In version 0.10, streams were made more consistent, faster, and fully backward compatible. You can now pause a stream and resume reading safely:

const fs = require('fs');
const readableStream = fs.createReadStream('example.txt');

readableStream.pause();
// Perform some other operations...
readableStream.resume();

This example demonstrates pausing and resuming a readable stream, showcasing the improved consistency and safety of streams in Node.js.

Error Handling with Domains

Domains were introduced to handle errors in a more structured manner. In the future, domains will be refactored to support more generic continuation-tracking systems, allowing alternative error-handling mechanisms in userland programs.

const domain = require('domain');

const d = domain.create();

d.on('error', (err) => {
    console.error(`Caught an error: ${err.message}`);
});

d.run(() => {
    // Code that might throw an error
    fs.readFile('nonexistent.txt', (err, data) => {
        if (err) throw err;
        console.log(data.toString());
    });
});

This code creates a domain and handles errors within it, demonstrating how domains can improve error management in Node.js applications.

Stability of the Module System

The Node.js module system is considered complete and will not undergo significant changes. This ensures that existing modules will continue to work without modification.

const myModule = require('./myModule');
myModule.someFunction();

This simple import statement shows how the module system remains unchanged, providing stability for developers.

Binary Addons and C API

To address the issue of binary addons breaking with each major version update, a stable C API shim will be added. This will allow binary addons to be code-stable and potentially binary-stable across stable branches of Node.js.

#include <node.h>

void Initialize(v8::Local<v8::Object> exports) {
  NODE_SET_METHOD(exports, "add", Add);
}

NODE_MODULE(addon, Initialize)

This C++ code demonstrates how to create a binary addon using the stable C API, ensuring compatibility across different versions of Node.js.

Performance and Stability

After the release of v0.12, the focus will shift to performance improvements, bug fixes, and stability enhancements. This means that once v0.12 is released, developers can expect a more robust and reliable environment for their applications.

In summary, the future of programming in Node.js will prioritize stability, performance, and backward compatibility while incorporating new language features and enhancing existing functionalities. This approach ensures that Node.js remains a powerful and flexible platform for building scalable web applications.


0.12啥时候到来, 好期盼呀.

回答:“The Future of Programming in Node.js”

根据Isaac Schlueter的描述,Node.js的核心API在未来几年内将保持相对稳定。以下是针对一些关键点的具体说明:

1. 回调(Callback)

  • 现状:回调是实现异步编程的主要方式。
  • 未来:回调将继续作为主要的异步处理方法。
  • 示例代码
    const fs = require('fs');
    fs.readFile('/path/to/file', (err, data) => {
      if (err) throw err;
      console.log(data);
    });
    

2. 流(Streams)

  • 现状:流已经变得更一致、更快,并且与旧版本完全兼容。
  • 未来:流将继续被核心API使用,并鼓励用户在用户空间中扩展流基类。
  • 示例代码
    const { Readable } = require('stream');
    class MyReadableStream extends Readable {
      _read() {}
    }
    

3. 模块系统(Module System)

  • 现状:模块系统已基本定型,不会有大的变动。
  • 未来:模块系统不会有任何变化,现有的模块将继续工作。
  • 示例代码
    // 使用CommonJS模块系统
    const myModule = require('./myModule');
    

4. C API Shim

  • 现状:将添加一个稳定的C API,以确保二进制插件在不同稳定分支中的兼容性。
  • 未来:这将使大多数二进制插件在不同版本之间更稳定。
  • 示例代码
    // C API 示例
    #include <node_api.h>
    
    napi_value MyAddon(napi_env env, napi_callback_info info) {
      return napi_create_string_utf8(env, "Hello World!", NAPI_AUTO_LENGTH);
    }
    
    NAPI_MODULE(NODE_GYP_MODULE_NAME, MyAddon)
    

5. 同步子进程执行(Synchronous Child Process Execution)

  • 现状:将添加同步子进程执行功能。
  • 未来:这将提供更可靠的方式来执行子进程操作。
  • 示例代码
    const { spawnSync } = require('child_process');
    const result = spawnSync('ls', ['-lh', '/usr']);
    console.log(result.stdout.toString());
    

总结来说,Node.js将在未来保持其核心API的稳定性,同时通过添加新功能来提高性能和可靠性。

回到顶部