| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent ebff06b commit 3f32526
8 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -24,6 +24,7 @@ const path = require('path'); | |||
| 24 | 24 | const { pathToFileURL, fileURLToPath, URL } = require('internal/url'); | |
| 25 | 25 | ||
| 26 | 26 | const { getOptionValue } = require('internal/options'); | |
| 27 | + const { setOwnProperty } = require('internal/util'); | ||
| 27 | 28 | const userConditions = getOptionValue('--conditions'); | |
| 28 | 29 | ||
| 29 | 30 | let debug = require('internal/util/debuglog').debuglog('module', (fn) => { | |
@@ -117,7 +118,7 @@ function makeRequireFunction(mod, redirects) { | |||
| 117 | 118 | ||
| 118 | 119 | resolve.paths = paths; | |
| 119 | 120 | ||
| 120 | - require.main = process.mainModule; | ||
| 121 | + setOwnProperty(require, 'main', process.mainModule); | ||
| 121 | 122 | ||
| 122 | 123 | // Enable support to add extra extension types. | |
| 123 | 124 | require.extensions = Module._extensions; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -79,7 +79,7 @@ const { | |||
| 79 | 79 | maybeCacheSourceMap, | |
| 80 | 80 | } = require('internal/source_map/source_map_cache'); | |
| 81 | 81 | const { pathToFileURL, fileURLToPath, isURLInstance } = require('internal/url'); | |
| 82 | - const { deprecate, kEmptyObject } = require('internal/util'); | ||
| 82 | + const { deprecate, kEmptyObject, filterOwnProperties, setOwnProperty } = require('internal/util'); | ||
| 83 | 83 | const vm = require('vm'); | |
| 84 | 84 | const assert = require('internal/assert'); | |
| 85 | 85 | const fs = require('fs'); | |
@@ -172,7 +172,7 @@ const moduleParentCache = new SafeWeakMap(); | |||
| 172 | 172 | function Module(id = '', parent) { | |
| 173 | 173 | this.id = id; | |
| 174 | 174 | this.path = path.dirname(id); | |
| 175 | - this.exports = {}; | ||
| 175 | + setOwnProperty(this, 'exports', {}); | ||
| 176 | 176 | moduleParentCache.set(this, parent); | |
| 177 | 177 | updateChildren(parent, this, false); | |
| 178 | 178 | this.filename = null; | |
@@ -312,14 +312,13 @@ function readPackage(requestPath) { | |||
| 312 | 312 | } | |
| 313 | 313 | ||
| 314 | 314 | try { | |
| 315 | - const parsed = JSONParse(json); | ||
| 316 | - const filtered = { | ||
| 317 | - name: parsed.name, | ||
| 318 | - main: parsed.main, | ||
| 319 | - exports: parsed.exports, | ||
| 320 | - imports: parsed.imports, | ||
| 321 | - type: parsed.type | ||
| 322 | - }; | ||
| 315 | + const filtered = filterOwnProperties(JSONParse(json), [ | ||
| 316 | + 'name', | ||
| 317 | + 'main', | ||
| 318 | + 'exports', | ||
| 319 | + 'imports', | ||
| 320 | + 'type', | ||
| 321 | + ]); | ||
| 323 | 322 | packageJsonCache.set(jsonPath, filtered); | |
| 324 | 323 | return filtered; | |
| 325 | 324 | } catch (e) { | |
@@ -1185,7 +1184,7 @@ Module._extensions['.json'] = function(module, filename) { | |||
| 1185 | 1184 | } | |
| 1186 | 1185 | ||
| 1187 | 1186 | try { | |
| 1188 | - module.exports = JSONParse(stripBOM(content)); | ||
| 1187 | + setOwnProperty(module, 'exports', JSONParse(stripBOM(content))); | ||
| 1189 | 1188 | } catch (err) { | |
| 1190 | 1189 | err.message = filename + ': ' + err.message; | |
| 1191 | 1190 | throw err; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,6 +2,7 @@ | |||
| 2 | 2 | ||
| 3 | 3 | const { | |
| 4 | 4 | JSONParse, | |
| 5 | + ObjectPrototypeHasOwnProperty, | ||
| 5 | 6 | SafeMap, | |
| 6 | 7 | StringPrototypeEndsWith, | |
| 7 | 8 | } = primordials; | |
@@ -11,6 +12,7 @@ const { | |||
| 11 | 12 | } = require('internal/errors').codes; | |
| 12 | 13 | ||
| 13 | 14 | const packageJsonReader = require('internal/modules/package_json_reader'); | |
| 15 | + const { filterOwnProperties } = require('internal/util'); | ||
| 14 | 16 | ||
| 15 | 17 | ||
| 16 | 18 | /** | |
@@ -66,8 +68,8 @@ function getPackageConfig(path, specifier, base) { | |||
| 66 | 68 | ); | |
| 67 | 69 | } | |
| 68 | 70 | ||
| 69 | - let { imports, main, name, type } = packageJSON; | ||
| 70 | - const { exports } = packageJSON; | ||
| 71 | + let { imports, main, name, type } = filterOwnProperties(packageJSON, ['imports', 'main', 'name', 'type']); | ||
| 72 | + const exports = ObjectPrototypeHasOwnProperty(packageJSON, 'exports') ? packageJSON.exports : undefined; | ||
| 71 | 73 | if (typeof imports !== 'object' || imports === null) { | |
| 72 | 74 | imports = undefined; | |
| 73 | 75 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -14,6 +14,7 @@ const { | |||
| 14 | 14 | ObjectGetOwnPropertyDescriptors, | |
| 15 | 15 | ObjectGetPrototypeOf, | |
| 16 | 16 | ObjectFreeze, | |
| 17 | + ObjectPrototypeHasOwnProperty, | ||
| 17 | 18 | ObjectSetPrototypeOf, | |
| 18 | 19 | Promise, | |
| 19 | 20 | ReflectApply, | |
@@ -507,6 +508,35 @@ ObjectFreeze(kEnumerableProperty); | |||
| 507 | 508 | ||
| 508 | 509 | const kEmptyObject = ObjectFreeze(ObjectCreate(null)); | |
| 509 | 510 | ||
| 511 | + function filterOwnProperties(source, keys) { | ||
| 512 | + const filtered = ObjectCreate(null); | ||
| 513 | + for (let i = 0; i < keys.length; i++) { | ||
| 514 | + const key = keys[i]; | ||
| 515 | + if (ObjectPrototypeHasOwnProperty(source, key)) { | ||
| 516 | + filtered[key] = source[key]; | ||
| 517 | + } | ||
| 518 | + } | ||
| 519 | + | ||
| 520 | + return filtered; | ||
| 521 | + } | ||
| 522 | + | ||
| 523 | + /** | ||
| 524 | + * Mimics `obj[key] = value` but ignoring potential prototype inheritance. | ||
| 525 | + * @param {any} obj | ||
| 526 | + * @param {string} key | ||
| 527 | + * @param {any} value | ||
| 528 | + * @returns {any} | ||
| 529 | + */ | ||
| 530 | + function setOwnProperty(obj, key, value) { | ||
| 531 | + return ObjectDefineProperty(obj, key, { | ||
| 532 | + __proto__: null, | ||
| 533 | + configurable: true, | ||
| 534 | + enumerable: true, | ||
| 535 | + value, | ||
| 536 | + writable: true, | ||
| 537 | + }); | ||
| 538 | + } | ||
| 539 | + | ||
| 510 | 540 | module.exports = { | |
| 511 | 541 | assertCrypto, | |
| 512 | 542 | cachedResult, | |
@@ -519,6 +549,7 @@ module.exports = { | |||
| 519 | 549 | emitExperimentalWarning, | |
| 520 | 550 | exposeInterface, | |
| 521 | 551 | filterDuplicateStrings, | |
| 552 | + filterOwnProperties, | ||
| 522 | 553 | getConstructorOf, | |
| 523 | 554 | getSystemErrorMap, | |
| 524 | 555 | getSystemErrorName, | |
@@ -549,4 +580,5 @@ module.exports = { | |||
| 549 | 580 | ||
| 550 | 581 | kEmptyObject, | |
| 551 | 582 | kEnumerableProperty, | |
| 583 | + setOwnProperty, | ||
| 552 | 584 | }; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,10 +1,11 @@ | |||
| 1 | 1 | import explicit from 'explicit-main'; | |
| 2 | 2 | import implicit from 'implicit-main'; | |
| 3 | 3 | import implicitModule from 'implicit-main-type-module'; | |
| 4 | + import noMain from 'no-main-field'; | ||
| 4 | 5 | ||
| 5 | 6 | function getImplicitCommonjs () { | |
| 6 | 7 | return import('implicit-main-type-commonjs'); | |
| 7 | 8 | } | |
| 8 | 9 | ||
| 9 | - export {explicit, implicit, implicitModule, getImplicitCommonjs}; | ||
| 10 | + export {explicit, implicit, implicitModule, getImplicitCommonjs, noMain}; | ||
| 10 | 11 | export default 'success'; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,43 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + const common = require('../common'); | ||
| 3 | + const fixtures = require('../common/fixtures'); | ||
| 4 | + const assert = require('assert'); | ||
| 5 | + | ||
| 6 | + Object.defineProperty(Object.prototype, 'name', { | ||
| 7 | + __proto__: null, | ||
| 8 | + get: common.mustNotCall('get %Object.prototype%.name'), | ||
| 9 | + set: common.mustNotCall('set %Object.prototype%.name'), | ||
| 10 | + enumerable: false, | ||
| 11 | + }); | ||
| 12 | + Object.defineProperty(Object.prototype, 'main', { | ||
| 13 | + __proto__: null, | ||
| 14 | + get: common.mustNotCall('get %Object.prototype%.main'), | ||
| 15 | + set: common.mustNotCall('set %Object.prototype%.main'), | ||
| 16 | + enumerable: false, | ||
| 17 | + }); | ||
| 18 | + Object.defineProperty(Object.prototype, 'type', { | ||
| 19 | + __proto__: null, | ||
| 20 | + get: common.mustNotCall('get %Object.prototype%.type'), | ||
| 21 | + set: common.mustNotCall('set %Object.prototype%.type'), | ||
| 22 | + enumerable: false, | ||
| 23 | + }); | ||
| 24 | + Object.defineProperty(Object.prototype, 'exports', { | ||
| 25 | + __proto__: null, | ||
| 26 | + get: common.mustNotCall('get %Object.prototype%.exports'), | ||
| 27 | + set: common.mustNotCall('set %Object.prototype%.exports'), | ||
| 28 | + enumerable: false, | ||
| 29 | + }); | ||
| 30 | + Object.defineProperty(Object.prototype, 'imports', { | ||
| 31 | + __proto__: null, | ||
| 32 | + get: common.mustNotCall('get %Object.prototype%.imports'), | ||
| 33 | + set: common.mustNotCall('set %Object.prototype%.imports'), | ||
| 34 | + enumerable: false, | ||
| 35 | + }); | ||
| 36 | + | ||
| 37 | + assert.strictEqual( | ||
| 38 | + require(fixtures.path('es-module-specifiers', 'node_modules', 'no-main-field')), | ||
| 39 | + 'no main field' | ||
| 40 | + ); | ||
| 41 | + | ||
| 42 | + import(fixtures.fileURL('es-module-specifiers', 'index.mjs')) | ||
| 43 | + .then(common.mustCall((module) => assert.strictEqual(module.noMain, 'no main field'))); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments