Skip to content

TCP arguments protocol is lacking #11

Description

@resolritter

The implemented TCP protocol is

$TOKEN $PWD [...args]

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.

core_d.js/lib/server.js

Lines 25 to 26 in bfe1f7c

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

<payload>
<text>

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions