|  | 
|  | 1 | +const net = require('node:net'); | 
|  | 2 | + | 
|  | 3 | +const proxyPortIn = 5004;		// proxy listening port | 
|  | 4 | + | 
|  | 5 | +/* | 
|  | 6 | +	Moddable SDK prrojects connect to xsbug at loaclhost:5002. | 
|  | 7 | +	This proxy listens on loaclhost:5004. To get the Noddable SDK project to connect to the proxy | 
|  | 8 | +	requires a manual source code change. On macOS, in $MODDABLE/xs/platforms/mac_xs.c, change  | 
|  | 9 | +		address.sin_port = htons(5002); | 
|  | 10 | +	to | 
|  | 11 | +		address.sin_port = htons(5004); | 
|  | 12 | +	Linux and Windows have similar changes. Eventuall We can add an option to mcconfig to set the xsbug | 
|  | 13 | +	port.  | 
|  | 14 | +*/ | 
|  | 15 | + | 
|  | 16 | +const proxyPortOut = 5002;		// xsbug listening port | 
|  | 17 | +const trace = false;				// trace progress to console for debugging  | 
|  | 18 | +const relay = true; | 
|  | 19 | +/* | 
|  | 20 | +	When relay is true, proxy relays messages between Moddable SDK project and xsbug. | 
|  | 21 | +		This allows using xsbug as usual while proxy has access to all communication. | 
|  | 22 | +	When relay is false, no cconnection is made to xsbug. The Moddabe SDK project sends | 
|  | 23 | +		messages as-if xsbug is present. | 
|  | 24 | +*/  | 
|  | 25 | + | 
|  | 26 | +const server = net.createServer(target => {  | 
|  | 27 | +	if (trace) | 
|  | 28 | +		console.log('target connected'); | 
|  | 29 | + | 
|  | 30 | +	let xsbug; | 
|  | 31 | +	if (relay) { | 
|  | 32 | +		// connect to xsbug to be able to relay messages | 
|  | 33 | +		xsbug = net.connect({ | 
|  | 34 | +			port: proxyPortOut, | 
|  | 35 | +			host: "localhost" | 
|  | 36 | +		}); | 
|  | 37 | +		xsbug.setEncoding("utf8"); | 
|  | 38 | +		xsbug.on('ready', data => { | 
|  | 39 | +			while (xsbug.deferred.length) | 
|  | 40 | +				xsbug.write(xsbug.deferred.shift()); | 
|  | 41 | +			delete xsbug.deferred; | 
|  | 42 | +		}); | 
|  | 43 | +		xsbug.on('data', data => { | 
|  | 44 | +			if (trace) | 
|  | 45 | +				console.log("from xsbug: " + data); | 
|  | 46 | +			target.write(data); | 
|  | 47 | +		}); | 
|  | 48 | +		xsbug.on('end', () => { | 
|  | 49 | +			if (trace) | 
|  | 50 | +				console.log('xsbug disconnected'); | 
|  | 51 | +			target.destroy(); | 
|  | 52 | +		}); | 
|  | 53 | +		xsbug.deferred = []; | 
|  | 54 | +		xsbug.deferred.push("2"); | 
|  | 55 | +	} | 
|  | 56 | +	else { | 
|  | 57 | +	} | 
|  | 58 | + | 
|  | 59 | +	target.setEncoding("utf8"); | 
|  | 60 | +	let first = true; | 
|  | 61 | +	target.on('data', data => { | 
|  | 62 | +		if (trace) | 
|  | 63 | +			console.log("to xsbug: " + data); | 
|  | 64 | +		 | 
|  | 65 | +		// parse messages here | 
|  | 66 | +		// each message is an XML document | 
|  | 67 | +		// status messages are sent in a bubble right message of the form: | 
|  | 68 | +		// <xsbug><bubble name="" value="2" path="/Users/hoddie/Projects/moddable/examples/helloworld/main.js" line="18">JSON STATUS MESSAGE HERE</bubble></xsbug> | 
|  | 69 | + | 
|  | 70 | +		if (relay) {  | 
|  | 71 | +			if (xsbug.deferred) | 
|  | 72 | +				xsbug.deferred.push(data); | 
|  | 73 | +			else | 
|  | 74 | +				xsbug.write(data); | 
|  | 75 | +		} | 
|  | 76 | +		else { | 
|  | 77 | +			if (first) { | 
|  | 78 | +				// first time need to send set-all-breakpoints as xsbug does | 
|  | 79 | +				first = false;; | 
|  | 80 | +				target.write('<set-all-breakpoints><breakpoint path="exceptions" line="0"/></set-all-breakpoints>\r\n'); | 
|  | 81 | +			} | 
|  | 82 | +			else { | 
|  | 83 | +				// assume any other messages are a break, so send go. This isn't always corrrect but may always work. | 
|  | 84 | +				target.write('<go/>\r\n'); | 
|  | 85 | +			} | 
|  | 86 | +		} | 
|  | 87 | +	}); | 
|  | 88 | +	target.on('end', () => { | 
|  | 89 | +		if (trace) | 
|  | 90 | +			console.log('target disconnected'); | 
|  | 91 | +		if (xsbug) | 
|  | 92 | +			xsbug.destroy(); | 
|  | 93 | +	}); | 
|  | 94 | +}); | 
|  | 95 | + | 
|  | 96 | +server.listen(proxyPortIn, () => {  | 
|  | 97 | +   console.log('proxy listening'); | 
|  | 98 | +}); | 
0 commit comments