summaryrefslogtreecommitdiffstats
path: root/layout/generic/test/file_SlowImage.sjs
blob: a9eaeef8381ea7a10f2002f53b55f853771a2ccb (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
36
37
38
39
40
41
42
43
44
45
"use strict";

const IMG_BYTES = atob(
  "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAA" +
    "DUlEQVQImWNgY2P7DwABOgESJhRQtgAAAABJRU5ErkJggg=="
);

function handleRequest(request, response) {
  response.processAsync();
  getObjectState("context", function (obj) {
    let ctx;
    if (obj == null) {
      ctx = {
        QueryInterface: function (iid) {
          if (iid.equals(Ci.nsISupports)) {
            return this;
          }
          throw Components.Exception("", Cr.NS_ERROR_NO_INTERFACE);
        },
      };
      ctx.wrappedJSObject = ctx;

      ctx.promise = new Promise(resolve => {
        ctx.resolve = resolve;
      });

      setObjectState("context", ctx);
    } else {
      ctx = obj.wrappedJSObject;
    }
    Promise.resolve(ctx).then(next);
  });

  function next(ctx) {
    if (request.queryString.indexOf("continue") >= 0) {
      ctx.resolve();
    }

    ctx.promise.then(() => {
      response.setHeader("Content-Type", "image/png");
      response.write(IMG_BYTES);
      response.finish();
    });
  }
}