-
Notifications
You must be signed in to change notification settings - Fork 84
chore(shell-api): add test and note about runtime independence MONGOSH-1975 #2312
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,5 @@ | ||
| # @mongosh/shell-api | ||
|
|
||
| Provides the runtime-independent classes that make up the MongoDB Shell API, | ||
| such as `Database`, `Collection`, etc., and the global objects and APIs | ||
| available to shell users, such as `db`, `rs`, `sh`, `console`, `print()`, etc. |
This file contains hidden or 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 hidden or 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,89 @@ | ||
| // This test verifies that shell-api only uses standard JS features. | ||
| import fsSync from 'fs'; | ||
| import vm from 'vm'; | ||
| import { createRequire } from 'module'; | ||
| import { expect } from 'chai'; | ||
| import sinon from 'ts-sinon'; | ||
|
|
||
| describe('Runtime independence', function () { | ||
| it('Can run using exclusively JS standard features', async function () { | ||
| const entryPoint = require.resolve('../'); | ||
|
|
||
| const context = vm.createContext(Object.create(null), { | ||
| codeGeneration: { | ||
| strings: false, | ||
| wasm: false, | ||
| }, | ||
| }); | ||
|
|
||
| // These are all used Node.js modules that are somewhat easily polyfill-able | ||
| // for other environments, but which we should still ideally remove in the | ||
| // long run (and definitely not add anything here). | ||
| // Guaranteed bonusly for anyone who removes a package from this list! | ||
| const allowedNodeBuiltins = ['crypto', 'util', 'events', 'path']; | ||
| // Our TextDecoder/TextEncoder polyfills require this, unfortunately. | ||
| context.Buffer = Buffer; | ||
| // lodash used by mongodb-redact used by @mongosh/history requires this Node.js-ism. | ||
| // Let's get rid of it: https://github.com/mongodb-js/devtools-shared/pull/497 | ||
| vm.runInContext('globalThis.global = globalThis;', context); | ||
|
|
||
| // Small CJS implementation, without __dirname or __filename | ||
| const cache = Object.create(null); | ||
| const absolutePathRequire = (absolutePath: string) => { | ||
| absolutePath = fsSync.realpathSync(absolutePath); | ||
| if (cache[absolutePath]) return cache[absolutePath]; | ||
| const module = (cache[absolutePath] = { exports: {} }); | ||
| const localRequire = (specifier: string) => { | ||
| if (allowedNodeBuiltins.includes(specifier)) return require(specifier); | ||
| return absolutePathRequire( | ||
| createRequire(absolutePath).resolve(specifier) | ||
| ).exports; | ||
| }; | ||
| const source = fsSync.readFileSync(absolutePath, 'utf8'); | ||
| const fn = vm.runInContext( | ||
| `(function(module, exports, require) {\n${source}\n})`, | ||
| context, | ||
| { | ||
| filename: `IN_CONTEXT:${absolutePath}`, | ||
| } | ||
| ); | ||
| fn(module, module.exports, localRequire); | ||
| return module; | ||
| }; | ||
|
|
||
| // service-provider-core injects a dummy polyfill for TextDecoder/TextEncoder | ||
| absolutePathRequire(require.resolve('@mongosh/service-provider-core')); | ||
| const shellApi = | ||
| // eslint-disable-next-line @typescript-eslint/consistent-type-imports | ||
| absolutePathRequire(entryPoint).exports as typeof import('./'); | ||
|
|
||
| // Verify that `shellApi` is generally usable. | ||
| const sp = { platform: 'CLI', close: sinon.spy() }; | ||
| const evaluationListener = { onExit: sinon.spy() }; | ||
| const instanceState = new shellApi.ShellInstanceState(sp as any); | ||
| instanceState.setEvaluationListener(evaluationListener); | ||
| expect(instanceState.initialServiceProvider).to.equal(sp); | ||
| const bsonObj = instanceState.shellBson.ISODate( | ||
| '2025-01-09T20:43:51+01:00' | ||
| ); | ||
| expect(bsonObj.toISOString()).to.equal('2025-01-09T19:43:51.000Z'); | ||
| expect(bsonObj instanceof Date).to.equal(false); | ||
| expect(Object.prototype.toString.call(bsonObj)).to.equal( | ||
| Object.prototype.toString.call(new Date()) | ||
| ); | ||
|
|
||
| try { | ||
| await instanceState.shellApi.exit(); | ||
| expect.fail('missed exception'); | ||
| } catch (err: any) { | ||
| expect(err.message).to.include('.onExit listener returned'); | ||
| expect(err.stack).to.include('IN_CONTEXT'); | ||
| expect(err instanceof Error).to.equal(false); | ||
| expect(Object.prototype.toString.call(err)).to.equal( | ||
| Object.prototype.toString.call(new Error()) | ||
| ); | ||
| } | ||
| expect(sp.close).to.have.been.calledOnce; | ||
| expect(evaluationListener.onExit).to.have.been.calledOnce; | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.