Skip to content

Commit

Permalink
Start moving some of the Response types around.
Browse files Browse the repository at this point in the history
Moves to use FetchEvent.purpose (etc.)

Refs #140
Refs #46
  • Loading branch information
slightlyoff committed Jan 24, 2014
1 parent ab602ba commit d8dbe8f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 41 deletions.
57 changes: 32 additions & 25 deletions service_worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,10 @@ var Request = (function () {
})();

// http://fetch.spec.whatwg.org/#responses
var Response = (function () {
function Response() {
var AbstractResponse = (function () {
function AbstractResponse() {
}
return Response;
return AbstractResponse;
})();

var OpaqueResponse = (function (_super) {
Expand All @@ -211,19 +211,11 @@ var OpaqueResponse = (function (_super) {
configurable: true
});
return OpaqueResponse;
})(Response);

var CORSResponse = (function (_super) {
__extends(CORSResponse, _super);
function CORSResponse() {
_super.apply(this, arguments);
}
return CORSResponse;
})(Response);
})(AbstractResponse);

var BasicResponse = (function (_super) {
__extends(BasicResponse, _super);
function BasicResponse(params) {
var Response = (function (_super) {
__extends(Response, _super);
function Response(params) {
if (params) {
if (typeof params.statusCode != "undefined") {
this.statusCode = params.statusCode;
Expand All @@ -249,8 +241,9 @@ var BasicResponse = (function (_super) {
}
_super.call(this);
}
Object.defineProperty(BasicResponse.prototype, "headers", {
Object.defineProperty(Response.prototype, "headers", {
get: function () {
// TODO: outline the whitelist of readable headers
return this._headers;
},
set: function (items) {
Expand All @@ -273,10 +266,18 @@ var BasicResponse = (function (_super) {
configurable: true
});

BasicResponse.prototype.toBlob = function () {
Response.prototype.toBlob = function () {
return accepted(new Blob());
};
return BasicResponse;
return Response;
})(AbstractResponse);

var CORSResponse = (function (_super) {
__extends(CORSResponse, _super);
function CORSResponse() {
_super.apply(this, arguments);
}
return CORSResponse;
})(Response);

var ResponsePromise = (function (_super) {
Expand All @@ -302,12 +303,18 @@ var FetchEvent = (function (_super) {
function FetchEvent() {
_super.call(this, "fetch", { cancelable: true, bubbles: false });
// Can be one of:
// "navigate"
// "fetch"
this.type = "navigate";
// Does the request correspond to navigation of the top-level window, e.g.
// reloading a tab or typing a URL into the URL bar?
this.isTopLevel = false;
// "connect",
// "font",
// "img",
// "img",
// "object",
// "script",
// "style",
// "worker",
// "popup",
// "child",
// "navigate"
this.purpose = "connect";
// Does the navigation or fetch come from a document that has been "soft
// reloaded"? That is to say, the reload button in the URL bar or the
// back/forward buttons in browser chrome? If true, some apps may choose not
Expand Down Expand Up @@ -362,7 +369,7 @@ var FetchEvent = (function (_super) {
this.stopImmediatePropagation();

return new Promise(function (resolver) {
resolver.resolve(new BasicResponse({
resolver.resolve(new Response({
statusCode: 302,
headers: { "Location": url.toString() }
}));
Expand Down
40 changes: 24 additions & 16 deletions service_worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,23 +322,18 @@ class Request {
}

// http://fetch.spec.whatwg.org/#responses
class Response {
class AbstractResponse {
constructor() {}
}

class OpaqueResponse extends Response {
class OpaqueResponse extends AbstractResponse {
// This class represents the result of cross-origin fetched resources that are
// tainted, e.g. <img src="http://cross-origin.example/test.png">

get url(): string { return ""; } // Read-only for x-origin
}

class CORSResponse extends Response {
// TODO: slightlyoff: make CORS headers readable but not setable?
// TODO: this should probably share a lot with BasicResponse
}

class BasicResponse extends Response {
class Response extends AbstractResponse {
constructor(params?) {
if (params) {
if (typeof params.statusCode != "undefined") {
Expand Down Expand Up @@ -377,6 +372,7 @@ class BasicResponse extends Response {
// pseudo-impl purposes.
_headers: Map<string, string>; // FIXME: Needs filtering!
get headers() {
// TODO: outline the whitelist of readable headers
return this._headers;
}
set headers(items) {
Expand All @@ -395,6 +391,12 @@ class BasicResponse extends Response {
toBlob(): Promise { return accepted(new Blob()); }
}

class CORSResponse extends Response {
// TODO: slightlyoff: make CORS headers readable but not setable?
// TODO: outline the whitelist of readable headers
}


class ResponsePromise extends Promise {
toBlob(): Promise { return accepted(new Blob()); }
}
Expand All @@ -403,17 +405,23 @@ class RequestPromise extends Promise {}
class FetchEvent extends _Event {
// The body of the request.
request: Request;
// Can be one of:
// "navigate"
// "fetch"
type: string = "navigate";

// The window issuing the request.
client: Client;

// Does the request correspond to navigation of the top-level window, e.g.
// reloading a tab or typing a URL into the URL bar?
isTopLevel: boolean = false;
// Can be one of:
// "connect",
// "font",
// "img",
// "img",
// "object",
// "script",
// "style",
// "worker",
// "popup",
// "child",
// "navigate"
purpose: string = "connect";

// Does the navigation or fetch come from a document that has been "soft
// reloaded"? That is to say, the reload button in the URL bar or the
Expand Down Expand Up @@ -457,7 +465,7 @@ class FetchEvent extends _Event {
this.stopImmediatePropagation();

return new Promise(function(resolver){
resolver.resolve(new BasicResponse({
resolver.resolve(new Response({
statusCode: 302,
headers: { "Location": url.toString() }
}));
Expand Down

0 comments on commit d8dbe8f

Please sign in to comment.