Hiroppy
Node.jsのビルトインモジュールに名前空間が使われるかもしれない

Node.jsのビルトインモジュールに名前空間が使われるかもしれない

2018 / 05 / 25
Hiroppy

Hiroppy

JavaScript Engineer


この記事は Hatena Blog からの移行記事です

注意: これはまだ未定です。 他にいい案があれば、提案やコメントをしてほしいです。


"use strict";

const fs = require("@nodejs/fs");
// import fs from '@nodejs/fs';

console.log(fs.readFileSync(__filename, "utf8"));

このように、@nodejsという名前空間でビルトインモジュールを保護する案が出ています。 もちろん、過去のコードに影響はありません。

PR は一旦、閉じられました。

WIP: tools: add core files to @nodejs namespace by cjihrig · Pull Request #20922 · nodejs/node

This is probably not the ideal solution, but I wanted to at least get the ball moving after the little bit of 'worker' module naming drama. This commit allows all of Node core's public ...

github.com
WIP: tools: add core files to @nodejs namespace by cjihrig · Pull Request #20922 · nodejs/node

発端

今現在、Ayo.js(Node.js の downstream)に試験的に導入されていたworkerが Node.js 本体に入ろうとしてしています。

worker: initial implementation by addaleax · Pull Request #20876 · nodejs/node

Hi everyone! 👋 This PR adds threading support for to Node.js. I realize that this is not exactly a small PR and is going to take a while to review, so: I appreciate comments, questions (any kind, a...

github.com
worker: initial implementation by addaleax · Pull Request #20876 · nodejs/node

Worker とはなにか?

Page Not Found

blog.hiroppy.me

それにあたり、名前の衝突問題があります。

npm module name / Node.js built-in `worker` module · Issue #1 · blake-regalia/worker.js

Hi there! I’m currently working on built-in Worker support (as in, threaded workers rather than cluster/child process) in Node.js, and there is only little left to do for that. (It’s basically just...

github.com
npm module name / Node.js built-in `worker` module · Issue #1 · blake-regalia/worker.js

すでに、worker.jsというライブラリが、npm の方にworkerというのが登録しており、その名前を変更してもらい衝突を避ける提案です。 しかし、作者により拒否をされてしまったため、このような名前空間を付ける案がでる流れになりました。

将来

この変更が行われると過去からあるモジュールは後方互換のために、グローバルと@nodejsという名前空間の 2 つが存在し、今後入るモジュールには、名前空間の中にしか存在しないこととなります。

なので、これから書く新しいコードにはビルトインモジュールのインポートには名前空間を付ける習慣に変わるということが予想されます。

個人的には避けたい問題だと思いますが、拒否されてしまった以上、難しそうです。


関連記事