The implemented TCP protocol is
The most glaring problem there is that $PWD can have spaces and that is not taken into account.
Further, I saw that the protocol accepts JSON although it's not documented.
|
if (data.substring(0, 1) === '{') { |
|
return JSON.parse(data); |
Yet another problem there: text is supposed to be included in the JSON payload, but it shouldn't; encoding thousands of lines to JSON is costly and there's no reason for it.
My suggestion is the following:
- First line should have all the parameters
- Second line and below is the text
i.e. parseData should be like
function parseData(data) {
const newlineIndex = data.indexOf("\n")
const payload = JSON.parse(data.slice(0, newlineIndex))
const text = data.slice(newlineIndex + 1)
return {
payload,
text,
}
}
for the format
as an example:
{ "cwd": "foo", "args": ["bar"] }
function sayHello() { console.log("hello") }
sayHello()
Summary of the proposal
- Deprecate space-delimited protocol
- Implement new protocol
- All options should be as JSON object in the first line
- Second line and below is reserved for text
Even in Bash it's pretty easy to escape the cwd for JSON, which would get rid of the spacing problem. Here's how I'm doing it in a script:
# escape a character of choice with '\'; outputs the result to $out
escape_string() {
v="$1"
c="$2"
len=${#v}
for ((i=0; i<$len; i++)); do
if [ "${v:$i:1}" = "$c" ]; then
v="${v:0:$i}\\${c}${v:$(( $i + 1 ))}"
len=$(( $len + 1 ))
i=$(( $i + 1 ))
fi
done
out="$v"
}
cwd="$(dirname "$1")"
# escape all double quotes since they're used as delimiters in JSON
escape_string "$cwd" '"'
encoded_cwd="$out"
msg="{ \"cwd\": \"$encoded_cwd\", \"args\": [\"--stdin\"] }"
Upsides for JSON:
- You can support any other options in the future without messing with the encoding
- It's ubiquituous
- Reasonably human-readable for a single line
The implemented TCP protocol is
The most glaring problem there is that
$PWDcan have spaces and that is not taken into account.Further, I saw that the protocol accepts JSON although it's not documented.
core_d.js/lib/server.js
Lines 25 to 26 in bfe1f7c
Yet another problem there:
textis supposed to be included in the JSON payload, but it shouldn't; encoding thousands of lines to JSON is costly and there's no reason for it.My suggestion is the following:
i.e. parseData should be like
for the format
as an example:
Summary of the proposal
Even in Bash it's pretty easy to escape the
cwdfor JSON, which would get rid of the spacing problem. Here's how I'm doing it in a script:Upsides for JSON: