Skip to content
/ node Public
forked from nodejs/node

Node.js JavaScript runtime ✨🐢🚀✨ with native support for preprocessor directives (D Script), and CoffeeScript and TypeScript

License

Notifications You must be signed in to change notification settings

sdneon/node

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Node.js

About this mod

This is a fun mod of Node.JS that initially embedded a modified version of dcodeIO’s defunct pre-processor.js, to allow use of simple C++ like preprocessor directives in CJS modules. Subsequently, it also embedded CoffeeScript and TypeScript.

Thanks to the inspiration from dcodeIO et al =D

Version 23.8.0

  • Update to Node.JS 23.8.0 baseline, and TypeScript v5.7.3.
  • --experimental-strip-types is disabled (in favour of full TS transpiler), although Node.JS 23.6.0 enables it by default.

It embeds:

  • DS v1.2.2.
  • CS v2.7.0.
  • TS v5.7.3.
  • Refer to prior readmes for old changes.

Built with VS 2022 v17.9.3 (rollback'd), as v17.10.0 fails!

Usage

The mod thus allows Node.JS+ to run .ds, .cs and .ts scripts directly for D Script, CoffeeScript and TypeScript directly. D being just a simple moniker for preprocessor sprinkled scripts.

Main Directives

  • Preprocessor directives are only available for D at the moment, but not for CS & TS.
    • v1.2.0: #import/#cs/#ts/#include code blocks can now use preprocessor directives within.
      • Each code block is transpiled (recursively) at the point of inclusion, and uses the #define'd variables at that point in time.
      • The effects of #define and #undef within the code blocks carry back to the main/parent code.
    • D transpiler does Not parse JS et al syntax, so it does Not recognise comments, and Will evaluate preprocessor directives in comment blocks as well.
  • #ds: D scripts can be identified by file extension of .ds, or CJS files with initial 3 characters of #ds.
    • From v1.2.0, ESM modules can also specify #ds to use preprocessor directives.
    • #ds colors: declares a D script file and imports colors with basic color mapping:
    require('colors').setTheme({
        silly: 'rainbow',
        input: 'grey',
        verbose: 'cyan',
        prompt: 'grey',
        info: 'green',
        data: 'grey',
        help: 'cyan',
        warn: 'yellow',
        debug: 'blue',
        error: 'red'
    });
    • #ds seapp: [from v19.7.0] for embedding as single executable app, seapp 'restores' the normal 'require()'. Code inject:
    require = require('module').createRequire(__filename);
    which is the shortened form of the doc suggested:
    const { createRequire } = require('module');
    require = createRequire(__filename);
    • Note: original doc suggests require('node:module') but that throw an error as 'node:' is unrecognized! Simply use require('module') instead.
  • #cs, #ts, #end: CS & TS code fragments/blocks can be embedded in D using #cs & #ts respectively, and terminated by #end.
    #ds colors This is a D example 
    #cs
    # this is a sample CoffeeScript code block in D
    square = (x) -> x * x
    #end
    console.log('2 squared='.bold.info, square(2));
    
    • To allow for #private_member syntax in TypeScript, all preprocessor directives must start the line. I.e. #'s that are not the 1st character of a line are ignored and not evaluated as preprocessor directives.
  • #define: declares preprocessor variables for use with #ifdefs et al.
    • Does not support elaborate C++ like marco function definitions.
    • Can declare variables via DS_DEFINES environment variable.
      • Windows OS example: set DS_DEFINES=VERBOSE=false;DETAILS=true
    • Evaluation is simplistic: all #define and #undef for the same variable are tallied till the end, where the final value is used to evaluate #if's et al.
  • #undef: (Since v1.2.0) undefines/removes preprocessor variables.
  • #ifdef, #ifndef, #if, #elif, #else, #endif: conditional code blocks
  • #put: pre-processor.js' version of inline expressions.
  • #include, #include_once: inline one or more files, using simple path or glob pattern.
    • Root path defaults to '.' but can be specified via 'DS_ROOT' environment variable.

Other wacky experimental Directives

  • #import: asychonously import a ESM module.
    • Example 1:
    #import exported_name '<ESM module path>'
    <callback content>
    #end
    
    expands to:
    (async () => {
        const exported_name = await import('<ESM module path>');
        <callback content>
    })();
    • Example 2:
    #import {exported_names} '<ESM module path>'
    <callback content>
    #end
    
    expands to:
    (async () => {
        const {exported_names} = await import('<ESM module path>');
        <callback content>
    })();
  • #inflate: inline base64 encoded and zipped code block.
  • others in the form #name where name is not a reserved directive keyword: exapnds to module.exports.name; ## expands to module.exports.

Exports

The transpilers are made available in global.transpiler. I.e. the transpiler functions are: transpiler.dscript.transpile, transpiler.coffeescript.compile, and transpiler.typescript.transpile. Example:

const dCode = '...'; //<- your code goes here
const outputCode = global.transpiler.dscript.transpile(dCode);

Example:

const coffeeCode = '...'; //<- your code goes here
const outputCode = global.transpiler.coffeescript.compile(
    coffeeCode,
    { bare: true }); //<- this option is to exclude function wrapper

Example:

const typescriptCode = '...'; //<- your code goes here
const outputCode = global.transpiler.typescript.transpile(typescriptCode);

Replacing Transpilers At Runtime

You can replace the cs & ts transpilers at runtime, say if you wish to try different versions other than the latest embedded in Node.JS+. A few versions are provided in src_packs folder.

  • *.packed.js are the non-minified ones good for digging into the innards of the transpiler.
  • *.packed.min.js are the compact, minified ones.

To make the swap, for example in REPL, simple do this:

//backup if need to restore latest later:
global.transpiler.typescript_latest = global.transpiler.typescript;

//override with alternate version:
require('path_to_alternate_src/src_packs/ts/4.2.3/TypeScript.packed.min.js');

src_packs folder is not used in Node.JS build. It's purely provided for playing around with.

Command-line Option

Use --experimental-external-dscript command-line option to override the internal D Script transpiler with that in external dscript.js file in current working directory. This is loaded before running your main code. An example is provided in src-packs/ds

REPL mode for D Script, CoffeeScript and TypeScript

REPL mode for D Script, CoffeeScript and TypeScript is activated by:

  • Setting environment variable NODE_REPL_SCRIPT to 'ds', 'cs' or 'ts' respectively.
    • You can even change mode during REPL.
      • E.g. enter this command in REPL to switch to CoffeeScript: process.env.NODE_REPL_SCRIPT = 'cs'
    • You should also use editor mode for entering multi-lines code blocks.
      • Simply enter this command in REPL: .editor
      • Paste or type in your code.
      • To end and execute the code block, use shortcut: CTRL-D
      • To cancel and exit editor, use shortcut: CTRL-C
  • Or running with commandline option '--repl=<script_type>'.
    • E.g.: node.exe --repl=ds

This mode is not available/valid for nodejs4Cpp DLL (node.DLL).

Register Bundled Module

If you use bundled modules (packed for example by browserify or Webpack), the modules can now be registered using global.registerPackedModule(name, module) and subsequently obtained via require(name). require() will work as usual in typical Node.JS module-scoped style. If it (actually is require.resolve) fails, then it will try to find in bundled modules, and return it if found. If not the require.resolve's error is rethrown.

Example Register the modules to be exposed, in your WebPacked codes by calling registerPackedModule:

//webpacked_bundle.min.js
...
var __webpack_exports__ = {};
// This entry need to be wrapped in an IIFE because it need to be isolated against other modules in the chunk.
(() => {
function pack()
{
    registerPackedModule('@turf/turf', __webpack_require__(83855));
    registerPackedModule('moment', moment);
    registerPackedModule('express', __webpack_require__(26083));
    registerPackedModule('http-proxy', __webpack_require__(99528));
    registerPackedModule('abort-controller', __webpack_require__(44203));
    registerPackedModule('geo-coordinates-parser', __webpack_require__(33185));
}
pack();
...

Then in your app codes, use require as usual:

//your app
require('./webpacked_bundle.min.js'); //load the minified bundle
const turf = require('@turf/turf'),
    moment = require('moment'),
    ...
    { convert } = require('geo-coordinates-parser');

Build Info

From Node.JS 22.x, VS2022 is needed; as VS2019 fails to compile some template syntax in V8 codes. CoffeeScript, TypeScript and Glob are now packed using Webpack.

Backward Compatibility for Win 7

If building for Windows 7, you'll need the deps\uv\src\win\util.c patch for uv_clock_gettime() to replace use of GetSystemTimePreciseAsFileTime() which is only available from Win8 onwards. If building for newer versions (>= 8.1), you may omit this patch.

Debugging Node.JS Internals

It appears that chrome://inspect & --inspect-brk no longer respects breakpoints in Node.JS internals upon startup; i.e. will skip right to beginning of user code. Thus, you'll need to add debugger; statement (and build the EXE) to force a break to debug the internals!

Original README

Node.js is an open-source, cross-platform JavaScript runtime environment.

For information on using Node.js, see the Node.js website.

The Node.js project uses an open governance model. The OpenJS Foundation provides support for the project.

Contributors are expected to act in a collaborative manner to move the project forward. We encourage the constructive exchange of contrary opinions and compromise. The TSC reserves the right to limit or block contributors who repeatedly act in ways that discourage, exhaust, or otherwise negatively affect other participants.

This project has a Code of Conduct.

Table of contents

Support

Looking for help? Check out the instructions for getting support.

Release types

  • Current: Under active development. Code for the Current release is in the branch for its major version number (for example, v22.x). Node.js releases a new major version every 6 months, allowing for breaking changes. This happens in April and October every year. Releases appearing each October have a support life of 8 months. Releases appearing each April convert to LTS (see below) each October.
  • LTS: Releases that receive Long Term Support, with a focus on stability and security. Every even-numbered major version will become an LTS release. LTS releases receive 12 months of Active LTS support and a further 18 months of Maintenance. LTS release lines have alphabetically-ordered code names, beginning with v4 Argon. There are no breaking changes or feature additions, except in some special circumstances.
  • Nightly: Code from the Current branch built every 24-hours when there are changes. Use with caution.

Current and LTS releases follow semantic versioning. A member of the Release Team signs each Current and LTS release. For more information, see the Release README.

Download

Binaries, installers, and source tarballs are available at https://nodejs.org/en/download/.

Current and LTS releases

https://nodejs.org/download/release/

The latest directory is an alias for the latest Current release. The latest-codename directory is an alias for the latest release from an LTS line. For example, the latest-hydrogen directory contains the latest Hydrogen (Node.js 18) release.

Nightly releases

https://nodejs.org/download/nightly/

Each directory and filename includes the version (e.g., v22.0.0), followed by the UTC date (e.g., 20240424 for April 24, 2024), and the short commit SHA of the HEAD of the release (e.g., ddd0a9e494). For instance, a full directory name might look like v22.0.0-nightly20240424ddd0a9e494.

API documentation

Documentation for the latest Current release is at https://nodejs.org/api/. Version-specific documentation is available in each release directory in the docs subdirectory. Version-specific documentation is also at https://nodejs.org/download/docs/.

Verifying binaries

Download directories contain a SHASUMS256.txt file with SHA checksums for the files.

To download SHASUMS256.txt using curl:

curl -O https://nodejs.org/dist/vx.y.z/SHASUMS256.txt

To check that downloaded files match the checksum, use sha256sum:

sha256sum -c SHASUMS256.txt --ignore-missing

For Current and LTS, the GPG detached signature of SHASUMS256.txt is in SHASUMS256.txt.sig. You can use it with gpg to verify the integrity of SHASUMS256.txt. You will first need to import the GPG keys of individuals authorized to create releases.

See Release keys for commands to import active release keys.

Next, download the SHASUMS256.txt.sig for the release:

curl -O https://nodejs.org/dist/vx.y.z/SHASUMS256.txt.sig

Then use gpg --verify SHASUMS256.txt.sig SHASUMS256.txt to verify the file's signature.

Building Node.js

See BUILDING.md for instructions on how to build Node.js from source and a list of supported platforms.

Security

For information on reporting security vulnerabilities in Node.js, see SECURITY.md.

Contributing to Node.js

Current project team members

For information about the governance of the Node.js project, see GOVERNANCE.md.

TSC (Technical Steering Committee)

TSC voting members

TSC regular members

TSC emeriti members

TSC emeriti members

Collaborators

Emeriti

Collaborator emeriti

Collaborators follow the Collaborator Guide in maintaining the Node.js project.

Triagers

Triagers follow the Triage Guide when responding to new issues.

Release keys

Primary GPG keys for Node.js Releasers (some Releasers sign with subkeys):

To import the full set of trusted release keys (including subkeys possibly used to sign releases):

gpg --keyserver hkps://keys.openpgp.org --recv-keys C0D6248439F1D5604AAFFB4021D900FFDB233756 # Antoine du Hamel
gpg --keyserver hkps://keys.openpgp.org --recv-keys DD792F5973C6DE52C432CBDAC77ABFA00DDBF2B7 # Juan José Arboleda
gpg --keyserver hkps://keys.openpgp.org --recv-keys CC68F5A3106FF448322E48ED27F5E38D5B0A215F # Marco Ippolito
gpg --keyserver hkps://keys.openpgp.org --recv-keys 8FCCA13FEF1D0C2E91008E09770F7A9A5AE15600 # Michaël Zasso
gpg --keyserver hkps://keys.openpgp.org --recv-keys 890C08DB8579162FEE0DF9DB8BEAB4DFCF555EF4 # Rafael Gonzaga
gpg --keyserver hkps://keys.openpgp.org --recv-keys C82FA3AE1CBEDC6BE46B9360C43CEC45C17AB93C # Richard Lau
gpg --keyserver hkps://keys.openpgp.org --recv-keys 108F52B48DB57BB0CC439B2997B01419BD92F80A # Ruy Adorno
gpg --keyserver hkps://keys.openpgp.org --recv-keys A363A499291CBBC940DD62E41F10027AF002F8B0 # Ulises Gascón

See Verifying binaries for how to use these keys to verify a downloaded file.

Other keys used to sign some previous releases

Security release stewards

When possible, the commitment to take slots in the security release steward rotation is made by companies in order to ensure individuals who act as security stewards have the support and recognition from their employer to be able to prioritize security releases. Security release stewards manage security releases on a rotation basis as outlined in the security release process.

License

Node.js is available under the MIT License. Node.js also includes external libraries that are available under a variety of licenses. See LICENSE for the full license text.

About

Node.js JavaScript runtime ✨🐢🚀✨ with native support for preprocessor directives (D Script), and CoffeeScript and TypeScript

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 97.6%
  • C++ 1.5%
  • Python 0.7%
  • C 0.2%
  • HTML 0.0%
  • Shell 0.0%