Skip to content

[Do not merge] Prototype: Replace async rewriter with sync worker thread driver facade #1936

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
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
2 changes: 1 addition & 1 deletion packages/browser-runtime-core/src/open-context-runtime.ts
Original file line number Diff line number Diff line change
@@ -42,7 +42,7 @@ export class OpenContextRuntime implements Runtime {
) {
this.interpreterEnvironment = interpreterEnvironment;
this.instanceState = new ShellInstanceState(
serviceProvider,
serviceProvider as any,
messageBus || new EventEmitter()
);
this.instanceState.isInteractive = true;
6 changes: 3 additions & 3 deletions packages/cli-repl/src/cli-repl.ts
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@ import { redactURICredentials } from '@mongosh/history';
import i18n from '@mongosh/i18n';
import type { AutoEncryptionOptions } from '@mongosh/service-provider-core';
import { bson } from '@mongosh/service-provider-core';
import { CliServiceProvider } from '@mongosh/service-provider-server';
import { SynchronousCliServiceProvider } from '@mongosh/service-provider-server';
import type { CliOptions, DevtoolsConnectOptions } from '@mongosh/arg-parser';
import { SnippetManager } from '@mongosh/snippet-manager';
import { Editor } from '@mongosh/editor';
@@ -792,7 +792,7 @@ export class CliRepl implements MongoshIOProvider {
async connect(
driverUri: string,
driverOptions: DevtoolsConnectOptions
): Promise<CliServiceProvider> {
): Promise<SynchronousCliServiceProvider> {
const { quiet } = CliRepl.getFileAndEvalInfo(this.cliOptions);
if (!this.cliOptions.nodb && !quiet) {
this.output.write(
@@ -802,7 +802,7 @@ export class CliRepl implements MongoshIOProvider {
'\n'
);
}
return await CliServiceProvider.connect(
return await SynchronousCliServiceProvider.connect(
driverUri,
driverOptions,
this.cliOptions,
7 changes: 4 additions & 3 deletions packages/cli-repl/src/mongosh-repl.ts
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ import { MongoshInternalError, MongoshWarning } from '@mongosh/errors';
import { changeHistory } from '@mongosh/history';
import type {
AutoEncryptionOptions,
ServiceProvider,
//ServiceProvider,
} from '@mongosh/service-provider-core';
import type {
EvaluationListener,
@@ -42,6 +42,7 @@ import type { FormatOptions } from './format-output';
import { markTime } from './startup-timing';
import type { Context } from 'vm';
import { Script, createContext, runInContext } from 'vm';
import type { SynchronousServiceProvider } from '@mongosh/service-provider-core';

declare const __non_webpack_require__: any;

@@ -176,7 +177,7 @@ class MongoshNodeRepl implements EvaluationListener {
* or print any user prompt.
*/
async initialize(
serviceProvider: ServiceProvider,
serviceProvider: SynchronousServiceProvider,
moreRecentMongoshVersion?: string | null
): Promise<InitializationToken> {
const instanceState = new ShellInstanceState(
@@ -198,7 +199,7 @@ class MongoshNodeRepl implements EvaluationListener {
let mongodVersion = extraInfo?.is_stream
? 'Atlas Stream Processing'
: buildInfo?.version;
const apiVersion = serviceProvider.getRawClient()?.serverApi?.version;
const apiVersion = undefined; //serviceProvider.getRawClient()?.serverApi?.version;
if (apiVersion) {
mongodVersion =
(mongodVersion ? mongodVersion + ' ' : '') +
80 changes: 1 addition & 79 deletions packages/cli-repl/src/smoke-tests-fle.ts
Original file line number Diff line number Diff line change
@@ -4,82 +4,4 @@
* to create an auto-encryption-aware connection.
*/

export default String.raw`
const assert = function(value, message) {
if (!value) {
console.error('assertion failed:', message);
unencryptedDb.dropDatabase();
process.exit(1);
}
};
if (db.version().startsWith('4.0.') ||
!db.runCommand({buildInfo:1}).modules.includes('enterprise')) {
// No FLE on mongod < 4.2 or community
print('Test skipped')
process.exit(0)
}

const dbname = 'testdb_fle' + new Date().getTime();
use(dbname);
unencryptedDb = db;
assert(db.getName() === dbname, 'db name must match');

const local = { key: Buffer.from('kh4Gv2N8qopZQMQYMEtww/AkPsIrXNmEMxTrs3tUoTQZbZu4msdRUaR8U5fXD7A7QXYHcEvuu4WctJLoT+NvvV3eeIg3MD+K8H9SR794m/safgRHdIfy6PD+rFpvmFbY', 'base64') };

const keyMongo = Mongo(db.getMongo(), {
keyVaultNamespace: dbname + '.__keyVault',
kmsProviders: { local }
});

const keyVault = keyMongo.getKeyVault();
const keyId = keyVault.createKey('local');
sleep(100);

const schemaMap = {};
schemaMap[dbname + '.employees'] = {
bsonType: 'object',
properties: {
taxid: {
encrypt: {
keyId: [keyId],
bsonType: 'string',
algorithm: 'AEAD_AES_256_CBC_HMAC_SHA_512-Random'
}
}
}
};

console.log('Using schema map', schemaMap);

const autoMongo = Mongo(db.getMongo(), {
keyVaultNamespace: dbname + '.__keyVault',
kmsProviders: { local },
schemaMap
});

db = autoMongo.getDB(dbname);
db.employees.insertOne({ taxid: 'abc' });

// If there is some failure that is not related to the assert() calls, we still
// want to make sure that we only print the success message if everything
// has worked so far, because the shell keeps evaluating statements after errors.
let verifiedEncrypted = false
let verifiedUnencrypted = false
{
const document = db.employees.find().toArray()[0];
console.log('auto-decrypted document', document);
verifiedEncrypted = document.taxid === 'abc';
assert(verifiedEncrypted, 'Must do automatic decryption');
}
db = unencryptedDb;
{
const document = db.employees.find().toArray()[0];
console.log('non-decrypted document', document);
verifiedUnencrypted = document.taxid instanceof Binary && document.taxid.sub_type === 6;
assert(verifiedUnencrypted, 'Must not do decryption without keys');
}
if (verifiedEncrypted && verifiedUnencrypted) {
print('Test succeeded')
}
db.dropDatabase();
`;
export default String.raw`print('Test skipped')`;
4 changes: 2 additions & 2 deletions packages/cli-repl/src/smoke-tests.ts
Original file line number Diff line number Diff line change
@@ -160,7 +160,7 @@ export async function runSmokeTests({
input: 'crypto.createHash("md5").update("hello").digest("hex")',
output: expectFipsSupport
? /disabled for FIPS|digital envelope routines::unsupported/i
: /disabled for FIPS|digital envelope routines::unsupported|Could not enable FIPS mode/i,
: /disabled for FIPS|digital envelope routines::unsupported|Could not enable FIPS mode|Assertion failed: crypto::CSPRNG/i,
includeStderr: true,
testArgs: ['--tlsFIPSMode', '--nodb'],
perfTestIterations: 0,
@@ -170,7 +170,7 @@ export async function runSmokeTests({
input: 'crypto.createHash("sha256").update("hello").digest("hex")',
output: expectFipsSupport
? /2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824/i
: /2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824|digital envelope routines::unsupported|Could not enable FIPS mode/i,
: /2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824|digital envelope routines::unsupported|Could not enable FIPS mode|Assertion failed: crypto::CSPRNG/i,
includeStderr: true,
testArgs: ['--tlsFIPSMode', '--nodb'],
perfTestIterations: 0,
6 changes: 5 additions & 1 deletion packages/service-provider-core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import './textencoder-polyfill'; // for mongodb-connection-string-url in the java-shell
import ServiceProvider, { ServiceProviderCore } from './service-provider';
import ServiceProvider, {
ServiceProviderCore,
SynchronousServiceProvider,
} from './service-provider';
import getConnectInfo, { ConnectInfo } from './connect-info';
import type { ReplPlatform } from './platform';
const DEFAULT_DB = 'test';
@@ -18,6 +21,7 @@ export {
export { bson } from './bson-export';

export {
SynchronousServiceProvider,
ServiceProvider,
ShellAuthOptions,
getConnectInfo,
2 changes: 1 addition & 1 deletion packages/service-provider-core/src/readable.ts
Original file line number Diff line number Diff line change
@@ -157,7 +157,7 @@ export default interface Readable {
/**
* Get currently known topology information.
*/
getTopology(): any;
getTopology?(): any;

/**
* Returns an array that holds a list of documents that identify and
8 changes: 8 additions & 0 deletions packages/service-provider-core/src/service-provider.ts
Original file line number Diff line number Diff line change
@@ -15,6 +15,14 @@ export default interface ServiceProvider
Closable,
Admin {}

export type SynchronousServiceProvider = {
[k in keyof ServiceProvider]: ServiceProvider[k] extends (
...args: infer A
) => Promise<infer R>
? (...args: A) => R
: ServiceProvider[k];
};

export class ServiceProviderCore {
public bsonLibrary: typeof BSON;
constructor(bsonLibrary?: typeof BSON) {
3 changes: 2 additions & 1 deletion packages/service-provider-server/package.json
Original file line number Diff line number Diff line change
@@ -12,7 +12,8 @@
"url": "git://github.com/mongodb-js/mongosh.git"
},
"scripts": {
"compile": "tsc -p tsconfig.json",
"precompile": "rm -rf lib/",
"compile": "tsc -p tsconfig.json && webpack --mode production && node scripts/wrap-pack.js",
"test": "cross-env TS_NODE_PROJECT=../../configs/tsconfig-mongosh/tsconfig.test.json mocha -r \"../../scripts/import-expansions.js\" --timeout 60000 -r ts-node/register \"./src/**/*.spec.ts\"",
"test-ci": "node ../../scripts/run-if-package-requested.js npm test",
"test-coverage": "nyc --no-clean --cwd ../.. --reporter=none npm run test",
5 changes: 5 additions & 0 deletions packages/service-provider-server/scripts/wrap-pack.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';
const fs = require('fs');
const packed = fs.readFileSync('./lib/packed.js', 'utf8');
const wrapped = `module.exports = ${JSON.stringify(packed)}`;
fs.writeFileSync('./lib/wrapped.js', wrapped);
Loading