@ -1,26 +1,18 @@
/ * *
* Various utilities and constants for communicating with the python sandbox .
* /
var MemBuffer = require ( 'app/common/MemBuffer' ) ;
var log = require ( './log' ) ;
import * as MemBuffer from 'app/common/MemBuffer' ;
import * as log from 'app/server/lib/log' ;
/ * *
* SandboxError is an error type for reporting errors forwarded from the sandbox .
* /
function SandboxError ( message ) {
// Poorly documented node feature, required to make the derived error keep a proper stack trace.
Error . captureStackTrace ( this , this . constructor ) ;
this . name = 'SandboxError' ;
this . message = "[Sandbox] " + ( message || 'Python reported an error' ) ;
export class SandboxError extends Error {
constructor ( message : string ) {
super ( "[Sandbox] " + ( message || 'Python reported an error' ) ) ;
}
}
SandboxError . prototype = new Error ( ) ;
// We need to set the .constructor property for Error.captureStackTrace to work correctly.
SandboxError . prototype . constructor = SandboxError ;
exports . SandboxError = SandboxError ;
/ * *
@ -30,9 +22,9 @@ exports.SandboxError = SandboxError;
* DATA = data must be a value to return to a call from the other side
* EXC = data must be an exception to return to a call from the other side
* /
exports . CALL = null ;
exports . DATA = true ;
exports . EXC = false ;
export const CALL = null ;
export const DATA = true ;
export const EXC = false ;
/ * *
@ -40,19 +32,18 @@ exports.EXC = false;
* The logged output is line - oriented , so that the prefix is only inserted at the start of a line .
* Binary data is encoded as with JSON . stringify .
* /
function makeLinePrefixer ( prefix , logMeta ) {
var partial = '' ;
return data = > {
export function makeLinePrefixer ( prefix : string , logMeta : object ) {
let partial = '' ;
return ( data : Uint8Array ) = > {
partial += MemBuffer . arrayToString ( data ) ;
var newline ;
let newline ;
while ( ( newline = partial . indexOf ( "\n" ) ) !== - 1 ) {
var line = partial . slice ( 0 , newline ) ;
const line = partial . slice ( 0 , newline ) ;
partial = partial . slice ( newline + 1 ) ;
// Escape some parts of the string by serializing it to JSON (without the quotes).
log . rawInfo( prefix + JSON . stringify ( line ) . slice ( 1 , - 1 ) . replace ( /\\"/g , '"' )
. replace ( /\\\\/g , '\\' ) ,
log . origLog( 'info' , "%s%s" , prefix ,
JSON . stringify ( line ) . slice ( 1 , - 1 ) . replace ( /\\"/g , '"' ) . replace ( /\\\\/g , '\\' ) ,
logMeta ) ;
}
} ;
}
exports . makeLinePrefixer = makeLinePrefixer ;