Cédric runs the backend for a video streaming service. Since video streaming, even in modern HTML5, is still a bit of a mess, they have to be able to provide many different stream formats. So, for example, the JSON data might look like this:

{"DASH":"https:\/\/anonymised-wtfdash.akamaihd.invalid\/path\/to\/film.mpd",
"HLS":"https:\/\/anonymised-wtfhls.akamaihd.invalid\/path\/to\/film.mp4\/master.m3u8",
"PD":"https:\/\/anonymised-wtfpd.akamaihd.invalid\/path\/to\/film.mp4"}

They had a problem, however. Many “clever” front-end programmers, especially the ones building Flash front-ends, viewed this data not as JSON, but as a string. Thus they’d try using substring calls to pick out the specific slice of the data they needed, and then their code would break when the length of one of the URLs changed, or a new entry was added. So Cédric “fixed” this problem by randomizing the order of the keys, thus forcing them to parse the data as JSON.

Having idiot proofed his service, an advanced version of Idiot was released a few weeks later, with new features.

  frame 9 {
    function print_a(obj, indent) {
      if (indent == null) {
        indent = '';
      }
      var v2 = '';
      for (item in obj) {
        if (typeof obj[item] == 'object') {
          v2 += indent + '[' + item + '] => Objectn';
        } else {
          v2 += indent + '[' + item + '] => ' + obj[item] + 'n';
        }
        v2 += print_a(obj[item], indent + '   ');
      }
      return v2;
    }

    var jsonFetcher = new LoadVars();
    jsonFetcher.onLoad = function (success) {
      if (!success) {
        trace('Error connecting to server.');
      }
    };

    jsonFetcher.onData = function (thedata) {
      try {
        var v1 = JSON.parse(thedata);
        testvar = print_a(v1);
      }
      catch (ex) {
        trace(ex.name + ':' + ex.message + ':' + ex.at + ':' + ex.text);
      }
    };

    jsonFetcher.load(loadVideo);
  }

  frame 17 {
    index = testvar.indexOf('[PD] => ');
  }

  frame 18 {
    index += 8;
  }

  frame 19 {
    index2 = testvar.indexOf('.mp4n');
  }

  frame 20 {
    index2 -= index;
  }

  frame 21 {
    var mySubstring = new String();
    mySubstring = testvar.substr(index, index2);
  }

  frame 22 {
    mySubstring2 = str_replace('\n', '', mySubstring);
  }

  frame 23 {
    mySubstring2 = mySubstring + '.mp4';
    trace('mySubstring2: ' + mySubstring2);
  }

  frame 25 {
    Videoplayer.contentPath = mySubstring2;
    Videoplayer.volume = _root.VideoVolume;
  }

The function print_a is… special. Given an object, its job is to format it as a pretty string, complete with line-breaks. The goal, obviously, is to allow them to use string slicing to pick out the specific fields they care about (instead of just accessing them directly), but along the way, the line breaks in their print_a function went from being “\n” to just “n”. Notice how they attempt to strip the “\n”s in frame 22. Similarly, through frame 19 to 21, they attempt to trim the “.mp4n” off the end (which they just put there), so that in frame 23, they can just pop back on a “.mp4”.

All of this, amazingly, worked. Unfortunately, you can see that the request was sent on frame 9. The results of the request were checked on frame 17. Since this is video, we can assume a frame-rate of 29.97 frames per second, which means that if the request isn’t serviced in about a quarter of a second, testvar isn’t going to have any data, and everything after it is going to fail, so by frame 25, there’s no video URL to load.

Of course, to start this process, they use JSON.parse, making 90% of this code utterly redundant.

[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!