Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/internal/modules/cjs/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,13 @@ function makeRequireFunction(mod, redirects) {

resolve.paths = paths;

require.main = process.mainModule;
ObjectDefineProperty(require, 'main', {
__proto__: null,
configurable: true,
enumerable: true,
value: process.mainModule,
writable: true,
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll have a few properties that need to be defined like this and they all have the same descriptors aside from value. Might it be worthwhile to move this into a const and spread it in order to DRY things up a bit?

  const pjsonFieldConfig = ObjectAssign(ObjectCreate(null), {
    __proto__: null,
    configurable: true,
    enumerable: true,
    writable: true,
  });


// Enable support to add extra extension types.
require.extensions = Module._extensions;
Expand Down
33 changes: 22 additions & 11 deletions lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ const {
maybeCacheSourceMap,
} = require('internal/source_map/source_map_cache');
const { pathToFileURL, fileURLToPath, isURLInstance } = require('internal/url');
const { deprecate, kEmptyObject } = require('internal/util');
const { deprecate, kEmptyObject, filterOwnProperties } = require('internal/util');
const vm = require('vm');
const assert = require('internal/assert');
const fs = require('fs');
Expand Down Expand Up @@ -172,7 +172,13 @@ const moduleParentCache = new SafeWeakMap();
function Module(id = '', parent) {
this.id = id;
this.path = path.dirname(id);
this.exports = {};
ObjectDefineProperty(this, 'exports', {
__proto__: null,
configurable: true,
enumerable: true,
value: {},
writable: true,
});
moduleParentCache.set(this, parent);
updateChildren(parent, this, false);
this.filename = null;
Expand Down Expand Up @@ -312,14 +318,13 @@ function readPackage(requestPath) {
}

try {
const parsed = JSONParse(json);
const filtered = {
name: parsed.name,
main: parsed.main,
exports: parsed.exports,
imports: parsed.imports,
type: parsed.type
};
const filtered = filterOwnProperties(JSONParse(json), [
'name',
'main',
'exports',
'imports',
'type',
]);
packageJsonCache.set(jsonPath, filtered);
return filtered;
} catch (e) {
Expand Down Expand Up @@ -1185,7 +1190,13 @@ Module._extensions['.json'] = function(module, filename) {
}

try {
module.exports = JSONParse(stripBOM(content));
ObjectDefineProperty(module, 'exports', {
__proto__: null,
configurable: true,
enumerable: true,
value: JSONParse(stripBOM(content)),
writable: true,
});
} catch (err) {
err.message = filename + ': ' + err.message;
throw err;
Expand Down
6 changes: 4 additions & 2 deletions lib/internal/modules/esm/package_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const {
JSONParse,
ObjectPrototypeHasOwnProperty,
SafeMap,
StringPrototypeEndsWith,
} = primordials;
Expand All @@ -11,6 +12,7 @@ const {
} = require('internal/errors').codes;

const packageJsonReader = require('internal/modules/package_json_reader');
const { filterOwnProperties } = require('internal/util');


/**
Expand Down Expand Up @@ -66,8 +68,8 @@ function getPackageConfig(path, specifier, base) {
);
}

let { imports, main, name, type } = packageJSON;
const { exports } = packageJSON;
let { imports, main, name, type } = filterOwnProperties(packageJSON, ['imports', 'main', 'name', 'type']);
const exports = ObjectPrototypeHasOwnProperty(packageJSON, 'exports') ? packageJSON.exports : undefined;
if (typeof imports !== 'object' || imports === null) {
imports = undefined;
}
Expand Down
14 changes: 14 additions & 0 deletions lib/internal/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const {
ObjectGetOwnPropertyDescriptors,
ObjectGetPrototypeOf,
ObjectFreeze,
ObjectPrototypeHasOwnProperty,
ObjectSetPrototypeOf,
Promise,
ReflectApply,
Expand Down Expand Up @@ -507,6 +508,18 @@ ObjectFreeze(kEnumerableProperty);

const kEmptyObject = ObjectFreeze(ObjectCreate(null));

function filterOwnProperties(source, keys) {
const filtered = ObjectCreate(null);
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
if (ObjectPrototypeHasOwnProperty(source, key)) {
filtered[key] = source[key];
}
}

return filtered;
}

module.exports = {
assertCrypto,
cachedResult,
Expand All @@ -519,6 +532,7 @@ module.exports = {
emitExperimentalWarning,
exposeInterface,
filterDuplicateStrings,
filterOwnProperties,
getConstructorOf,
getSystemErrorMap,
getSystemErrorName,
Expand Down
3 changes: 2 additions & 1 deletion test/fixtures/es-module-specifiers/index.mjs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import explicit from 'explicit-main';
import implicit from 'implicit-main';
import implicitModule from 'implicit-main-type-module';
import noMain from 'no-main-field';

function getImplicitCommonjs () {
return import('implicit-main-type-commonjs');
}

export {explicit, implicit, implicitModule, getImplicitCommonjs};
export {explicit, implicit, implicitModule, getImplicitCommonjs, noMain};
export default 'success';

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 43 additions & 0 deletions test/parallel/test-module-prototype-mutation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'use strict';
const common = require('../common');
const fixtures = require('../common/fixtures');
const assert = require('assert');

Object.defineProperty(Object.prototype, 'name', {
__proto__: null,
get: common.mustNotCall('get %Object.prototype%.name'),
set: common.mustNotCall('set %Object.prototype%.name'),
enumerable: false,
});
Object.defineProperty(Object.prototype, 'main', {
__proto__: null,
get: common.mustNotCall('get %Object.prototype%.main'),
set: common.mustNotCall('set %Object.prototype%.main'),
enumerable: false,
});
Object.defineProperty(Object.prototype, 'type', {
__proto__: null,
get: common.mustNotCall('get %Object.prototype%.type'),
set: common.mustNotCall('set %Object.prototype%.type'),
enumerable: false,
});
Object.defineProperty(Object.prototype, 'exports', {
__proto__: null,
get: common.mustNotCall('get %Object.prototype%.exports'),
set: common.mustNotCall('set %Object.prototype%.exports'),
enumerable: false,
});
Object.defineProperty(Object.prototype, 'imports', {
__proto__: null,
get: common.mustNotCall('get %Object.prototype%.imports'),
set: common.mustNotCall('set %Object.prototype%.imports'),
enumerable: false,
});

assert.strictEqual(
require(fixtures.path('es-module-specifiers', 'node_modules', 'no-main-field')),
'no main field'
);

import(fixtures.fileURL('es-module-specifiers', 'index.mjs'))
.then(common.mustCall((module) => assert.strictEqual(module.noMain, 'no main field')));