Comparing version 1.8.2 to 1.8.3
{ | ||
"name": "esmock", | ||
"version": "1.8.2", | ||
"license": "MIT", | ||
"version": "1.8.3", | ||
"license": "ISC", | ||
"readmeFilename": "README.md", | ||
@@ -55,28 +55,4 @@ "description": "provides native ESM import mocking for unit tests", | ||
"dependencies": { | ||
"resolvewithplus": "^0.8.0" | ||
}, | ||
"devDependencies": { | ||
"uvu": "0.5.3", | ||
"ava": "^4.0.1", | ||
"eslint": "^8.12.0", | ||
"ts-node": "^10.9.1", | ||
"esmock": "file:.", | ||
"form-urlencoded": "4.2.1", | ||
"run-script-os": "^1.1.6", | ||
"sinon": "^12.0.1" | ||
}, | ||
"scripts": { | ||
"test-node-esbuildts": "node --no-warnings --loader=@esbuild-kit/esm-loader --loader=esmock --test ./spec/node-ts/*ts", | ||
"test-node-ts": "node --no-warnings --loader=ts-node/esm --loader=esmock --test ./spec/node-ts/*ts", | ||
"test-node": "node --no-warnings --loader=esmock --test ./spec/node/", | ||
"test-node18": "npm run test-node && npm run test-node-ts", | ||
"test-nodeis18": "if (node -v | grep v18); then npm run test-node18; fi;", | ||
"test-ava": "npx ava --node-arguments=\"--loader=esmock\" ./spec/ava/*.spec.js", | ||
"test-uvu": "node --loader=esmock ./node_modules/uvu/bin.js ./spec/uvu/", | ||
"test:default": "npm run test-nodeis18 && npm run test-ava && npm run test-uvu", | ||
"test:windows": "npm run test-ava && npm run test-uvu", | ||
"test": "run-script-os", | ||
"test-no-warn": "npx ava --node-arguments=\"--loader=esmock --no-warnings\"", | ||
"lint": "npx eslint src/*js spec" | ||
"resolvewithplus": "^0.8.4" | ||
} | ||
} |
@@ -1,46 +0,2 @@ | ||
import { | ||
esmockModuleMock, | ||
esmockModuleImportedPurge, | ||
esmockModuleImportedSanitize | ||
} from './esmockModule.js'; | ||
import { | ||
esmockCache | ||
} from './esmockCache.js'; | ||
const esmock = async (modulePath, mockDefs, globalDefs, opt = {}, err) => { | ||
const calleePath = (err || new Error()).stack.split('\n')[2] | ||
.replace(/^.*file:\/\//, '') // rm every before filepath | ||
.replace(/:[\d]*:[\d]*.*$/, '') // rm line and row number | ||
.replace(/^.*:/, '') // rm windows-style drive location | ||
.replace(/.*at [^(]*\(/, ''); // rm ' at TestContext.<anonymous> (' | ||
if (!global.esmockloader) | ||
throw new Error('process must be started with --loader=esmock'); | ||
const modulePathKey = await esmockModuleMock( | ||
calleePath, modulePath, mockDefs || {}, globalDefs || {}, opt); | ||
const importedModule = await import(modulePathKey); | ||
if (opt.purge !== false) | ||
esmockModuleImportedPurge(modulePathKey); | ||
return esmockModuleImportedSanitize(importedModule, modulePathKey); | ||
}; | ||
esmock.px = async (modulePath, mockDefs, globalDefs) => ( | ||
esmock(modulePath, mockDefs, globalDefs, { partial : true }, new Error())); | ||
esmock.p = async (modulePath, mockDefs, globalDefs) => ( | ||
esmock(modulePath, mockDefs, globalDefs, { purge : false }, new Error())); | ||
esmock.purge = mockModule => { | ||
if (mockModule && /object|function/.test(typeof mockModule) | ||
&& 'esmockKey' in mockModule) | ||
esmockModuleImportedPurge(mockModule.esmockKey); | ||
}; | ||
esmock.esmockCache = esmockCache; | ||
export default esmock; | ||
import{esmockModuleMock as m,esmockModuleImportedPurge as s,esmockModuleImportedSanitize as n}from"./esmockModule.js";import{esmockCache as d}from"./esmockCache.js";const r=async(e,o,t,c={},p)=>{const l=(p||new Error).stack.split(` | ||
`)[2].replace(/^.*file:\/\//,"").replace(/:[\d]*:[\d]*.*$/,"").replace(/^.*:/,"").replace(/.*at [^(]*\(/,"");if(!global.esmockloader)throw new Error("process must be started with --loader=esmock");const a=await m(l,e,o||{},t||{},c),i=await import(a);return c.purge!==!1&&s(a),n(i,a)};r.px=async(e,o,t)=>r(e,o,t,{partial:!0},new Error),r.p=async(e,o,t)=>r(e,o,t,{purge:!1},new Error),r.purge=e=>{e&&/object|function/.test(typeof e)&&"esmockKey"in e&&s(e.esmockKey)},r.esmockCache=d;export default r; |
@@ -1,41 +0,1 @@ | ||
const esmockCache = { | ||
isESM : {}, | ||
// record of fullpaths with associated mocks | ||
// eg, { '/path/to/mock.js': true } | ||
mockDefs : {} | ||
}; | ||
const esmockKeySet = (key, keylong) => ( | ||
global.mockKeys[String(key)] = keylong); | ||
const esmockKeyGet = key => ( | ||
global.mockKeys[String(key)]); | ||
const esmockCacheSet = (key, mockDef) => ( | ||
esmockCache.mockDefs[key] = mockDef); | ||
const esmockCacheGet = key => ( | ||
esmockCache.mockDefs[key]); | ||
const esmockCacheResolvedPathIsESMGet = mockPathFull => ( | ||
esmockCache.isESM[mockPathFull]); | ||
const esmockCacheResolvedPathIsESMSet = (mockPathFull, isesm) => ( | ||
esmockCache.isESM[mockPathFull] = isesm); | ||
Object.assign(global, { | ||
esmockCacheGet, | ||
esmockKeyGet, | ||
mockKeys : {} | ||
}); | ||
export { | ||
esmockCache, | ||
esmockCacheSet, | ||
esmockCacheGet, | ||
esmockKeySet, | ||
esmockKeyGet, | ||
esmockCacheResolvedPathIsESMGet, | ||
esmockCacheResolvedPathIsESMSet | ||
}; | ||
const c={isESM:{},mockDefs:{}},k=(e,s)=>global.mockKeys[String(e)]=s,o=e=>global.mockKeys[String(e)],m=(e,s)=>c.mockDefs[e]=s,t=e=>c.mockDefs[e],a=e=>c.isESM[e],n=(e,s)=>c.isESM[e]=s;Object.assign(global,{esmockCacheGet:t,esmockKeyGet:o,mockKeys:{}});export{c as esmockCache,m as esmockCacheSet,t as esmockCacheGet,k as esmockKeySet,o as esmockKeyGet,a as esmockCacheResolvedPathIsESMGet,n as esmockCacheResolvedPathIsESMSet}; |
@@ -1,4 +0,1 @@ | ||
export default process.execArgv | ||
.some(args => args.startsWith('--loader=') && args.includes('esmock')) | ||
|| /(?:^|\s)?--(?:experimental-)?loader=(["']*)esmock\1(?:\s|$)/ | ||
.test(process.env.NODE_OPTIONS); | ||
export default process.execArgv.some(e=>e.startsWith("--loader=")&&e.includes("esmock"))||/(?:^|\s)?--(?:experimental-)?loader=(["']*)esmock\1(?:\s|$)/.test(process.env.NODE_OPTIONS); |
@@ -1,191 +0,1 @@ | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
import resolvewith from 'resolvewithplus'; | ||
import { | ||
esmockKeySet, | ||
esmockKeyGet, | ||
esmockCacheSet, | ||
esmockCacheResolvedPathIsESMGet, | ||
esmockCacheResolvedPathIsESMSet | ||
} from './esmockCache.js'; | ||
const isObj = o => typeof o === 'object' && o; | ||
const isObjOrFnRe = /^(object|function)$/; | ||
const isObjOrFn = o => isObjOrFnRe.test(typeof o) && o; | ||
const isDefaultDefined = o => isObj(o) && 'default' in o; | ||
const FILE_PROTOCOL = 'file:///'; | ||
// https://url.spec.whatwg.org/, eg, file:///C:/demo file:///root/linux/path | ||
const pathAddProtocol = (pathFull, protocol) => { | ||
if (!protocol) | ||
protocol = !resolvewith.iscoremodule(pathFull) ? FILE_PROTOCOL : 'node:'; | ||
if (protocol.includes(FILE_PROTOCOL)) | ||
pathFull = fs.realpathSync.native(pathFull); | ||
if (process.platform === 'win32') | ||
pathFull = pathFull.split(path.sep).join(path.posix.sep); | ||
return `${protocol}${pathFull.replace(/^\//, '')}`; | ||
} | ||
const esmockModuleMergeDefault = (defaultLive, defaultMock, merged) => { | ||
const defaultLiveIsObj = isObj(defaultLive); | ||
const defaultMockIsObj = isObj(defaultMock); | ||
if (defaultLiveIsObj && defaultMockIsObj) { | ||
merged = Object.assign({}, defaultLive, defaultMock); | ||
} else if (defaultMock) { | ||
merged = defaultMock; | ||
} else if (defaultLiveIsObj) { | ||
merged = Object.assign({}, defaultLive); | ||
} else if (defaultLive) { | ||
merged = defaultLive; | ||
} | ||
return merged; | ||
}; | ||
const esmockModuleApply = (definitionLive, definitionMock, definitionPath) => { | ||
const isCorePath = resolvewith.iscoremodule(definitionPath); | ||
const definitionDefault = esmockModuleMergeDefault( | ||
isDefaultDefined(definitionLive) && definitionLive.default, | ||
isDefaultDefined(definitionMock) ? definitionMock.default : definitionMock); | ||
const definition = Object.assign({}, definitionLive || {}, { | ||
default : definitionDefault | ||
}, definitionMock); | ||
// if safe, an extra default definition is added for compatibility, | ||
// because babel-generated dist cjs files often import in this way, | ||
// note: core modules do not define "default.default" | ||
// import package from 'package'; | ||
// package.default(); <- extra default definition | ||
if (!isCorePath && isObjOrFn(definition.default)) | ||
definition.default.default = definition.default; | ||
return definition; | ||
}; | ||
// eslint-disable-next-line max-len | ||
const esmockModuleESMRe = /(^\s*|[}\);\n]\s*)(import\s+(['"]|(\*\s+as\s+)?[^"'\(\)\n;]+\s+from\s+['"]|\{)|export\s+\*\s+from\s+["']|export\s+(\{|default|function|class|var|const|let|async\s+function))/; | ||
// tries to return resolved value from a cache first | ||
// else, builds value, stores in cache and returns | ||
const esmockModuleIsESM = (mockPathFull, isesm) => { | ||
isesm = esmockCacheResolvedPathIsESMGet(mockPathFull); | ||
if (typeof isesm === 'boolean') | ||
return isesm; | ||
isesm = !resolvewith.iscoremodule(mockPathFull) | ||
&& esmockModuleESMRe.test(fs.readFileSync(mockPathFull, 'utf-8')); | ||
esmockCacheResolvedPathIsESMSet(mockPathFull, isesm); | ||
return isesm; | ||
}; | ||
// return the default value directly, so that the esmock caller | ||
// does not need to lookup default as in "esmockedValue.default" | ||
const esmockModuleImportedSanitize = (importedModule, esmockKey) => { | ||
const importedDefault = 'default' in importedModule && importedModule.default; | ||
if (!/boolean|string|number/.test(typeof importedDefault)) { | ||
// an example of [object Module]: import * as mod from 'fs'; export mod; | ||
return Object.prototype.toString.call(importedDefault) === '[object Module]' | ||
? Object.assign({}, importedDefault, importedModule, { esmockKey }) | ||
: Object.assign(importedDefault, importedModule, { esmockKey }); | ||
} | ||
return importedModule; | ||
}; | ||
const esmockModuleImportedPurge = modulePathKey => { | ||
const purgeKey = key => key === 'null' || esmockCacheSet(key, null); | ||
const longKey = esmockKeyGet(modulePathKey.split('esmk=')[1]); | ||
const [ url, keys ] = longKey.split('#esmockModuleKeys='); | ||
String(keys).split('#').forEach(purgeKey); | ||
String(url.split('esmockGlobals=')[1]).split('#').forEach(purgeKey); | ||
}; | ||
const esmockNextKey = ((key = 0) => () => ++key)(); | ||
// eslint-disable-next-line max-len | ||
const esmockModuleCreate = async (esmockKey, key, mockPathFull, mockDef, opt) => { | ||
const isesm = esmockModuleIsESM(mockPathFull); | ||
const originalDefinition = opt.partial | ||
? await import(pathAddProtocol(mockPathFull)) : null; | ||
const mockDefinitionFinal = esmockModuleApply( | ||
originalDefinition, mockDef, mockPathFull); | ||
const mockExportNames = Object.keys(mockDefinitionFinal).sort().join(); | ||
const mockModuleKey = `${pathAddProtocol(mockPathFull)}?` + [ | ||
'esmockKey=' + esmockKey, | ||
'esmockModuleKey=' + key, | ||
'isesm=' + isesm, | ||
mockExportNames ? 'exportNames=' + mockExportNames : 'exportNone' | ||
].join('&'); | ||
esmockCacheSet(mockModuleKey, mockDefinitionFinal); | ||
return mockModuleKey; | ||
}; | ||
// eslint-disable-next-line max-len | ||
const esmockModulesCreate = async (pathCallee, pathModule, esmockKey, defs, keys, mocks, opt) => { | ||
keys = keys || Object.keys(defs); | ||
mocks = mocks || []; | ||
if (!keys.length) | ||
return mocks; | ||
let mockedPathFull = resolvewith(keys[0], pathCallee); | ||
if (!mockedPathFull) { | ||
pathCallee = pathCallee | ||
.replace(/^\/\//, '') | ||
.replace(process.cwd(), '.') | ||
.replace(process.env.HOME, '~'); | ||
throw new Error(`not a valid path: "${keys[0]}" (used by ${pathCallee})`); | ||
} | ||
if (process.platform === 'win32') | ||
mockedPathFull = mockedPathFull.split(path.sep).join(path.posix.sep); | ||
mocks.push(await esmockModuleCreate( | ||
esmockKey, | ||
keys[0], | ||
mockedPathFull, | ||
defs[keys[0]], | ||
opt | ||
)); | ||
return esmockModulesCreate( | ||
pathCallee, pathModule, esmockKey, defs, keys.slice(1), mocks, opt); | ||
}; | ||
const esmockModuleMock = async (calleePath, modulePath, defs, gdefs, opt) => { | ||
const pathModuleFull = resolvewith(modulePath, calleePath); | ||
const esmockKey = typeof opt.key === 'number' ? opt.key : esmockNextKey(); | ||
const esmockModuleKeys = await esmockModulesCreate( | ||
calleePath, pathModuleFull, esmockKey, defs, Object.keys(defs), 0, opt); | ||
const esmockGlobalKeys = await esmockModulesCreate( | ||
calleePath, pathModuleFull, esmockKey, gdefs, Object.keys(gdefs), 0, opt); | ||
if (pathModuleFull === null) | ||
throw new Error(`modulePath not found: "${modulePath}"`); | ||
const esmockKeyLong = pathAddProtocol(pathModuleFull, FILE_PROTOCOL) + '?' | ||
+ 'key=:esmockKey?esmockGlobals=:esmockGlobals#esmockModuleKeys=:moduleKeys' | ||
.replace(/:esmockKey/, esmockKey) | ||
.replace(/:esmockGlobals/, esmockGlobalKeys.join('#') || 'null') | ||
.replace(/:moduleKeys/, esmockModuleKeys.join('#')); | ||
esmockKeySet(String(esmockKey), esmockKeyLong); | ||
return pathAddProtocol(pathModuleFull, FILE_PROTOCOL) + `?esmk=${esmockKey}`; | ||
}; | ||
export { | ||
esmockModuleMock, | ||
esmockModuleImportedPurge, | ||
esmockModuleImportedSanitize | ||
}; | ||
import d from"fs";import f from"path";import l from"resolvewithplus";import{esmockKeySet as k,esmockKeyGet as K,esmockCacheSet as j,esmockCacheResolvedPathIsESMGet as M,esmockCacheResolvedPathIsESMSet as S}from"./esmockCache.js";const y=e=>typeof e=="object"&&e,g=/^(object|function)$/,w=e=>g.test(typeof e)&&e,O=e=>y(e)&&"default"in e,m="file:///",p=(e,s)=>(s||(s=l.iscoremodule(e)?"node:":m),s.includes(m)&&(e=d.realpathSync.native(e)),process.platform==="win32"&&(e=e.split(f.sep).join(f.posix.sep)),`${s}${e.replace(/^\//,"")}`),E=(e,s,o)=>{const c=y(e),t=y(s);return c&&t?o=Object.assign({},e,s):s?o=s:c?o=Object.assign({},e):e&&(o=e),o},x=(e,s,o)=>{const c=l.iscoremodule(o),t=E(O(e)&&e.default,O(s)?s.default:s),n=Object.assign({},e||{},{default:t},s);return!c&&w(n.default)&&(n.default.default=n.default),n},I=/(^\s*|[}\);\n]\s*)(import\s+(['"]|(\*\s+as\s+)?[^"'\(\)\n;]+\s+from\s+['"]|\{)|export\s+\*\s+from\s+["']|export\s+(\{|default|function|class|var|const|let|async\s+function))/,$=(e,s)=>(s=M(e),typeof s=="boolean"||(s=!l.iscoremodule(e)&&I.test(d.readFileSync(e,"utf-8")),S(e,s)),s),D=(e,s)=>{const o="default"in e&&e.default;return/boolean|string|number/.test(typeof o)?e:Object.prototype.toString.call(o)==="[object Module]"?Object.assign({},o,e,{esmockKey:s}):Object.assign(o,e,{esmockKey:s})},G=e=>{const s=n=>n==="null"||j(n,null),o=K(e.split("esmk=")[1]),[c,t]=o.split("#esmockModuleKeys=");String(t).split("#").forEach(s),String(c.split("esmockGlobals=")[1]).split("#").forEach(s)},R=((e=0)=>()=>++e)(),N=async(e,s,o,c,t)=>{const n=$(o),r=t.partial?await import(p(o)):null,i=x(r,c,o),a=Object.keys(i).sort().join(),u=`${p(o)}?`+["esmockKey="+e,"esmockModuleKey="+s,"isesm="+n,a?"exportNames="+a:"exportNone"].join("&");return j(u,i),u},b=async(e,s,o,c,t,n,r)=>{if(t=t||Object.keys(c),n=n||[],!t.length)return n;let i=l(t[0],e);if(!i)throw e=e.replace(/^\/\//,"").replace(process.cwd(),".").replace(process.env.HOME,"~"),new Error(`not a valid path: "${t[0]}" (used by ${e})`);return process.platform==="win32"&&(i=i.split(f.sep).join(f.posix.sep)),n.push(await N(o,t[0],i,c[t[0]],r)),b(e,s,o,c,t.slice(1),n,r)},C=async(e,s,o,c,t)=>{const n=l(s,e),r=typeof t.key=="number"?t.key:R(),i=await b(e,n,r,o,Object.keys(o),0,t),a=await b(e,n,r,c,Object.keys(c),0,t);if(n===null)throw new Error(`modulePath not found: "${s}"`);const u=p(n,m)+"?"+"key=:esmockKey?esmockGlobals=:esmockGlobals#esmockModuleKeys=:moduleKeys".replace(/:esmockKey/,r).replace(/:esmockGlobals/,a.join("#")||"null").replace(/:moduleKeys/,i.join("#"));return k(String(r),u),p(n,m)+`?esmk=${r}`};export{C as esmockModuleMock,G as esmockModuleImportedPurge,D as esmockModuleImportedSanitize}; |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
0
13994
115
1
Updatedresolvewithplus@^0.8.4