summaryrefslogtreecommitdiffstats
path: root/layout/generic/test/file_SlowImage.sjs
blob: cf95eeeeaa61e5e0659f4c38da461e97c584db92 (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 Cc = Components.classes;
const Ci = Components.interfaces;

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(Components.interfaces.nsISupports))
            return this;
          throw Components.results.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();
    });
  }
}