@@ -2,7 +2,6 @@ import { NodeDriverServiceProvider } from '@mongosh/service-provider-node-driver
2
2
import { ElectronRuntime } from '@mongosh/browser-runtime-electron' ;
3
3
import { parentPort } from 'worker_threads' ;
4
4
import { ServerCommands } from './serverCommands' ;
5
- import type { Document } from 'bson' ;
6
5
7
6
import type {
8
7
ShellEvaluateResult ,
@@ -17,7 +16,7 @@ interface EvaluationResult {
17
16
type : string | null ;
18
17
}
19
18
20
- const getContent = ( { type, printable } : EvaluationResult ) : Document => {
19
+ const getContent = ( { type, printable } : EvaluationResult ) : unknown => {
21
20
if ( type === 'Cursor' || type === 'AggregationCursor' ) {
22
21
return getEJSON ( printable . documents ) ;
23
22
}
@@ -27,11 +26,7 @@ const getContent = ({ type, printable }: EvaluationResult): Document => {
27
26
: getEJSON ( printable ) ;
28
27
} ;
29
28
30
- export const getLanguage = (
31
- evaluationResult : EvaluationResult
32
- ) : 'json' | 'plaintext' => {
33
- const content = getContent ( evaluationResult ) ;
34
-
29
+ export const getLanguage = ( content : unknown ) : 'json' | 'plaintext' => {
35
30
if ( typeof content === 'object' && content !== null ) {
36
31
return 'json' ;
37
32
}
@@ -105,12 +100,19 @@ export const execute = async ({
105
100
? `${ source . namespace . db } .${ source . namespace . collection } `
106
101
: undefined ;
107
102
103
+ // The RPC protocol can't handle functions and it wouldn't make sense to return them anyway. Since just
104
+ // declaring a function doesn't execute it, the best thing we can do is return undefined, similarly to
105
+ // what we do when there's no return value from the script.
106
+ const rpcSafePrintable =
107
+ typeof printable !== 'function' ? printable : undefined ;
108
+
108
109
// Prepare a playground result.
110
+ const content = getContent ( { type, printable : rpcSafePrintable } ) ;
109
111
const result = {
110
112
namespace,
111
- type : type ? type : typeof printable ,
112
- content : getContent ( { type , printable } ) ,
113
- language : getLanguage ( { type , printable } ) ,
113
+ type : type ? type : typeof rpcSafePrintable ,
114
+ content,
115
+ language : getLanguage ( content ) ,
114
116
} ;
115
117
116
118
return { data : { result } } ;
0 commit comments