summaryrefslogtreecommitdiffstats
path: root/devtools/client/netmonitor/test/sjs_long-polling-server.sjs
blob: 2c801492234fee45ca48339fa2c535e9bc11992b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
"use strict";

const key = "blocked-response";
function setResponse(response) {
  setObjectState(key, response);
}

function getResponse() {
  let response;
  getObjectState(key, v => {
    response = v;
  });
  return response;
}

function handleRequest(request, response) {
  const { queryString } = request;
  if (!queryString) {
    // The default end point will return a blocked response.
    // The response object will be stored and will be released
    // when "?unblock" is called.
    response.processAsync();
    response.setHeader("Content-Type", "text/plain", false);
    response.write("Begin...\n");
    setResponse(response);
  } else if (queryString == "unblock") {
    // unblock the pending response
    getResponse().finish();
    setResponse(null);

    // and return synchronously.
    response.setHeader("Content-Type", "text/plain");
    response.write("ok");
  }
}