assert: create internal/assert micro-module

For use in built-in modules that could benefit from `assert()` without
having to load the entire module (unless an AssertionError actually
occurs): lib/internal/assert.js.

PR-URL: https://github.com/nodejs/node/pull/25956
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Minwoo Jung <minwoo@nodesource.com>
This commit is contained in:
Rich Trott 2019-02-05 21:05:24 -08:00
parent 64745c3ade
commit 5d609bb11c
3 changed files with 25 additions and 0 deletions

9
lib/internal/assert.js Normal file
View File

@ -0,0 +1,9 @@
'use strict';
function assert(value, message) {
if (!value) {
require('assert')(value, message);
}
}
module.exports = assert;

View File

@ -85,6 +85,7 @@
'lib/vm.js',
'lib/worker_threads.js',
'lib/zlib.js',
'lib/internal/assert.js',
'lib/internal/assert/assertion_error.js',
'lib/internal/async_hooks.js',
'lib/internal/buffer.js',

View File

@ -0,0 +1,15 @@
// Flags: --expose-internals
'use strict';
require('../common');
const assert = require('assert');
const internalAssert = require('internal/assert');
// Should not throw.
internalAssert(true);
internalAssert(true, 'fhqwhgads');
assert.throws(() => { internalAssert(false); }, assert.AssertionError);
assert.throws(() => { internalAssert(false, 'fhqwhgads'); },
{ code: 'ERR_ASSERTION', message: 'fhqwhgads' });