summaryrefslogtreecommitdiffstats
path: root/layout/generic/test/file_SlowImage.sjs
blob: 6fc86056ac05267cebde307c51458989d5ea463e (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
"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(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();
    });
  }
}