-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: drop streams support COMPASS-7124 (#208)
We don't really need streams usage anymore and can just drop it. That's an easy way to ensure that we don't have any dependencies that require Node.js-specific builtins or polyfills of those.
- Loading branch information
Showing
6 changed files
with
98 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import assert from 'assert'; | ||
import vm from 'vm'; | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
|
||
function createMockModuleSystem() { | ||
const context = vm.createContext(Object.create(null)); | ||
class Module { | ||
exports = {}; | ||
} | ||
const modules = new Map<string, Module>(); | ||
// Tiny (incomplete) CommonJS module system mock | ||
function makeRequire(basename: string) { | ||
return function require(identifier: string): any { | ||
if (!identifier.startsWith('./') && !identifier.startsWith('../') && !path.isAbsolute(identifier)) { | ||
let current = path.dirname(basename); | ||
let previous: string; | ||
do { | ||
const nodeModulesEntry = path.resolve(current, 'node_modules', identifier); | ||
previous = current; | ||
current = path.dirname(current); | ||
if (fs.existsSync(nodeModulesEntry)) { | ||
return require(nodeModulesEntry); | ||
} | ||
} while (previous !== current); | ||
throw new Error(`mock require() does not support Node.js built-ins (${identifier})`); | ||
} | ||
let file = path.resolve(path.dirname(basename), identifier); | ||
if (!fs.existsSync(file) && fs.existsSync(`${file}.js`)) { | ||
file = `${file}.js`; | ||
} else if (fs.statSync(file).isDirectory()) { | ||
if (fs.existsSync(`${file}/package.json`)) { | ||
const pjson = JSON.parse(fs.readFileSync(`${file}/package.json`, 'utf8')); | ||
file = path.resolve(file, pjson.main || 'index.js'); | ||
} else if (fs.existsSync(`${file}/index.js`)) { | ||
file = path.resolve(file, 'index.js'); | ||
} | ||
} | ||
const existing = modules.get(file); | ||
if (existing) { | ||
return existing.exports; | ||
} | ||
const module = new Module(); | ||
const source = fs.readFileSync(file); | ||
vm.runInContext(`(function(require, module, exports, __filename, __dirname) {\n${source}\n})`, context)( | ||
makeRequire(file), module, module.exports, file, path.dirname(file) | ||
); | ||
modules.set(file, module); | ||
return module.exports; | ||
}; | ||
} | ||
return makeRequire; | ||
} | ||
|
||
describe('getSchema should work in plain JS environment without Node.js or browser dependencies', function() { | ||
const docs = [ | ||
{ foo: 'bar' }, | ||
{ country: 'Croatia' }, | ||
{ country: 'Croatia' }, | ||
{ country: 'England' } | ||
]; | ||
|
||
it('Check if return value is a promise', async function() { | ||
const makeRequire = createMockModuleSystem(); | ||
|
||
const { parseSchema } = makeRequire(__filename)('../lib/index.js') as typeof import('..'); | ||
|
||
const result = await parseSchema(docs); | ||
assert.strictEqual(result.count, 4); | ||
assert.strictEqual(result.fields.map(f => f.name).join(','), 'country,foo'); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.