From 726281bd2fb79f2c64cd19f7f57b81160f721e86 Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Sun, 30 Jan 2022 18:52:48 +0100 Subject: [PATCH 01/14] Add `Response.json` static method This commit adds a `Response.json` static method that can be used to create well formed JSON responses will very little effort. --- fetch.bs | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/fetch.bs b/fetch.bs index afa26221d..68baca49e 100644 --- a/fetch.bs +++ b/fetch.bs @@ -7298,6 +7298,7 @@ interface Response { [NewObject] static Response error(); [NewObject] static Response redirect(USVString url, optional unsigned short status = 302); + [NewObject] static Response json(any data, optional ResponseInit init = {}); readonly attribute ResponseType type; @@ -7349,6 +7350,10 @@ enum ResponseType { "basic", "cors", "default", "error", "opaque", "opaqueredire

Creates a redirect {{Response}} that redirects to url with status status. +

response = Response . json(data [, init]) +

Creates a {{Response}} whose body is the JSON-encoded data, and status, status + message, and headers are provided by init. +

response . type

Returns response's type, e.g., "cors". @@ -7480,6 +7485,48 @@ are:

  • Return responseObject. + +

    The static +json(data, init) method steps +are: + +

      +
    1. If init["{{ResponseInit/status}}"] is not in the range 200 to 599, inclusive, + then throw a {{RangeError}}. + +

    2. If init["{{ResponseInit/status}}"] is a null body status, then + throw a {{TypeError}}. + +

    3. If init["{{ResponseInit/statusText}}"] does not match the + reason-phrase token production, then throw a {{TypeError}}. + +

    4. Let responseObject be the result of creating a {{Response}} + object, given a new response, "response", and this's + relevant Realm. + +

    5. Set responseObject's response's status to + init["{{ResponseInit/status}}"]. + +

    6. Set responseObject's response's + status message to init["{{ResponseInit/statusText}}"]. + +

    7. If init["{{ResponseInit/headers}}"] exists, then + fill responseObject's headers with + init["{{ResponseInit/headers}}"]. + +

    8. Set responseObject's response's body to + the UTF-8 encoding of the result of running + serialize a JavaScript value to a JSON string on data. + +

    9. If responseObject's response's header list + does not contain `Content-Type`, then + append (`Content-Type`, + `application/json;charset=utf-8`) to responseObject's + response's header list. + +

    10. Return responseObject. +

    +

    The type getter steps are to return this's response's type. From 77ac97fbaa8739ba1cbd95340d65508da9327238 Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Sun, 30 Jan 2022 20:47:07 +0100 Subject: [PATCH 02/14] use -> JSON bytes algorithm --- fetch.bs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/fetch.bs b/fetch.bs index 68baca49e..8e2074bc6 100644 --- a/fetch.bs +++ b/fetch.bs @@ -7515,8 +7515,7 @@ are: init["{{ResponseInit/headers}}"].

  • Set responseObject's response's body to - the UTF-8 encoding of the result of running - serialize a JavaScript value to a JSON string on data. + the result of running serialize a JavaScript value to a JSON bytes on data.

  • If responseObject's response's header list does not contain `Content-Type`, then From b026a334a9e4a10be25f373f5f33d41dbfbc090e Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Fri, 11 Feb 2022 23:51:33 +0100 Subject: [PATCH 03/14] Split out common steps into a separate algorithm --- fetch.bs | 61 +++++++++++++++++++++++++------------------------------- 1 file changed, 27 insertions(+), 34 deletions(-) diff --git a/fetch.bs b/fetch.bs index 8e2074bc6..6be59d313 100644 --- a/fetch.bs +++ b/fetch.bs @@ -7399,11 +7399,8 @@ enum ResponseType { "basic", "cors", "default", "error", "opaque", "opaqueredire

  • Return responseObject. -


    - -

    The -new Response(body, init) -constructor steps are: +

    To initialize a response, given a {{Response}} response, and +{{ResponseInit}} init, run these steps:

    1. If init["{{ResponseInit/status}}"] is not in the range 200 to 599, inclusive, @@ -7412,6 +7409,24 @@ constructor steps are:

    2. If init["{{ResponseInit/statusText}}"] does not match the reason-phrase token production, then throw a {{TypeError}}. +

    3. Set response's response's status to + init["{{ResponseInit/status}}"]. + +

    4. Set response's response's status message to + init["{{ResponseInit/statusText}}"]. + +

    5. If init["{{ResponseInit/headers}}"] exists, then + fill response's headers with + init["{{ResponseInit/headers}}"]. +

    + +
    + +

    The +new Response(body, init) +constructor steps are: + +

    1. Set this's response to a new response.

    2. Set this's headers to a new {{Headers}} object with @@ -7419,23 +7434,15 @@ constructor steps are: response's header list and guard is "response". -

    3. Set this's response's status to - init["{{ResponseInit/status}}"]. - -

    4. Set this's response's status message to - init["{{ResponseInit/statusText}}"]. - -

    5. If init["{{ResponseInit/headers}}"] exists, then - fill this's headers with - init["{{ResponseInit/headers}}"]. +

    6. Perform initialize a response given this, and init.

    7. If body is non-null, then:

      1. -

        If init["{{ResponseInit/status}}"] is a null body status, then throw - a {{TypeError}}. +

        If this's response's status is a + null body status, then throw a {{TypeError}}.

        101 is included in null body status due to its use elsewhere. It does not affect this step. @@ -7491,31 +7498,17 @@ are: are:

          -
        1. If init["{{ResponseInit/status}}"] is not in the range 200 to 599, inclusive, - then throw a {{RangeError}}. - -

        2. If init["{{ResponseInit/status}}"] is a null body status, then - throw a {{TypeError}}. - -

        3. If init["{{ResponseInit/statusText}}"] does not match the - reason-phrase token production, then throw a {{TypeError}}. -

        4. Let responseObject be the result of creating a {{Response}} object, given a new response, "response", and this's relevant Realm. -

        5. Set responseObject's response's status to - init["{{ResponseInit/status}}"]. - -

        6. Set responseObject's response's - status message to init["{{ResponseInit/statusText}}"]. +

        7. Perform initialize a response given responseObject, and init. -

        8. If init["{{ResponseInit/headers}}"] exists, then - fill responseObject's headers with - init["{{ResponseInit/headers}}"]. +

        9. If responseObject's response's status is + a null body status, then throw a {{TypeError}}.

        10. Set responseObject's response's body to - the result of running serialize a JavaScript value to a JSON bytes on data. + the result of running serialize a JavaScript value to JSON bytes on data.

        11. If responseObject's response's header list does not contain `Content-Type`, then From 1c2fa9196c91e3009312a676c64a2b220fdbe563 Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Wed, 9 Mar 2022 00:10:07 +0100 Subject: [PATCH 04/14] move init steps into "initialize a response" --- fetch.bs | 80 +++++++++++++++++++++++++++++--------------------------- 1 file changed, 41 insertions(+), 39 deletions(-) diff --git a/fetch.bs b/fetch.bs index 6be59d313..1fe787112 100644 --- a/fetch.bs +++ b/fetch.bs @@ -1256,6 +1256,13 @@ null), run these steps. processBody must be an algorithm accepting a


          +

          A body with type is a +tuple that consists of a +body (a body) and +type (a header value). + +


          +

          To handle content codings given codings and bytes, run these steps: @@ -7399,8 +7406,8 @@ enum ResponseType { "basic", "cors", "default", "error", "opaque", "opaqueredire

        12. Return responseObject.

        -

        To initialize a response, given a {{Response}} response, and -{{ResponseInit}} init, run these steps: +

        To initialize a response, given a {{Response}} response, {{ResponseInit}} +init, and an optional body with type body, run these steps:

        1. If init["{{ResponseInit/status}}"] is not in the range 200 to 599, inclusive, @@ -7418,6 +7425,26 @@ enum ResponseType { "basic", "cors", "default", "error", "opaque", "opaqueredire

        2. If init["{{ResponseInit/headers}}"] exists, then fill response's headers with init["{{ResponseInit/headers}}"]. + +

        3. +

          If body was given, then: + +

            +
          1. +

            If response's status is a null body status, then + throw a {{TypeError}}. + +

            101 is included in null body status due to its use elsewhere. + It does not affect this step. + +

          2. Set response's body to body's + body. + +

          3. If body's type is non-null and + response's header list does not contain + `Content-Type`, then append (`Content-Type`, + body's type) to response's + header list.


          @@ -7434,30 +7461,13 @@ constructor steps are: response's header list and guard is "response". -
        4. Perform initialize a response given this, and init. +

        5. Let bodyWithType be null. -

        6. -

          If body is non-null, then: +

        7. If body is non-null, then let bodyWithType be the result of + extracting body. -

            -
          1. -

            If this's response's status is a - null body status, then throw a {{TypeError}}. - -

            101 is included in null body status due to its use elsewhere. - It does not affect this step. - -

          2. Let Content-Type be null. - -

          3. Set this's response's body and - Content-Type to the result of extracting body. - -

          4. If Content-Type is non-null and this's response's - header list does not contain - `Content-Type`, then append (`Content-Type`, - Content-Type) to this's response's - header list. -

          +
        8. Perform initialize a response given this, init, and + bodyWithType.

        The static error() method steps are to return the @@ -7498,25 +7508,17 @@ are: are:

          +
        1. Let bytes the result of running serialize a JavaScript value to JSON bytes + on data. + +

        2. Let body be the result of extracting bytes. +

        3. Let responseObject be the result of creating a {{Response}} object, given a new response, "response", and this's relevant Realm. -

        4. Perform initialize a response given responseObject, and init. - -

        5. If responseObject's response's status is - a null body status, then throw a {{TypeError}}. - -

        6. Set responseObject's response's body to - the result of running serialize a JavaScript value to JSON bytes on data. - -

        7. If responseObject's response's header list - does not contain `Content-Type`, then - append (`Content-Type`, - `application/json;charset=utf-8`) to responseObject's - response's header list. - -

        8. Return responseObject. +

        9. Perform initialize a response given responseObject, init, and + (body, "application/json;charset=utf-8").

        The type getter steps are to return this's From a0050fa33343e3523ac754ea9d72eded0fe628bc Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Wed, 9 Mar 2022 00:11:26 +0100 Subject: [PATCH 05/14] fix --- fetch.bs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fetch.bs b/fetch.bs index 1fe787112..3aa635093 100644 --- a/fetch.bs +++ b/fetch.bs @@ -1259,7 +1259,7 @@ null), run these steps. processBody must be an algorithm accepting a

        A body with type is a tuple that consists of a body (a body) and -type (a header value). +type (a header value).


        From 9830a2aae85eade09ee20834164cd768084573ad Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Wed, 9 Mar 2022 15:24:53 +0100 Subject: [PATCH 06/14] redeploy From 80a7a7c58ca74659d4143b8c5d24ee212dacb911 Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Wed, 9 Mar 2022 15:54:30 +0100 Subject: [PATCH 07/14] redeploy From 9d577390d2c10841b6787ddad28d19444a5f42dc Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Wed, 9 Mar 2022 19:06:45 +0100 Subject: [PATCH 08/14] redeploy From 391b8173d0ac347ace0a31fb94c5a2760b6e3348 Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Thu, 10 Mar 2022 13:35:45 +0100 Subject: [PATCH 09/14] address review comments --- fetch.bs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/fetch.bs b/fetch.bs index 460ff3ba6..aeff44644 100644 --- a/fetch.bs +++ b/fetch.bs @@ -1256,10 +1256,9 @@ null), run these steps. processBody must be an algorithm accepting a
        -

        A body with type is a -tuple that consists of a -body (a body) and -type (a header value). +

        A body with type is a tuple that consists of a +body (a body) and +type (a header value).


        @@ -7447,7 +7446,7 @@ enum ResponseType { "basic", "cors", "default", "error", "opaque", "opaqueredire

      To initialize a response, given a {{Response}} response, {{ResponseInit}} -init, and an optional body with type body, run these steps: +init, and an optional body with type body, run these steps:

      1. If init["{{ResponseInit/status}}"] is not in the range 200 to 599, inclusive, @@ -7485,6 +7484,7 @@ enum ResponseType { "basic", "cors", "default", "error", "opaque", "opaqueredire `Content-Type`, then append (`Content-Type`, body's type) to response's header list. +


    @@ -7503,7 +7503,7 @@ constructor steps are:
  • Let bodyWithType be null. -

  • If body is non-null, then let bodyWithType be the result of +

  • If body is non-null, then set bodyWithType to the result of extracting body.

  • Perform initialize a response given this, init, and @@ -7559,6 +7559,8 @@ are:

  • Perform initialize a response given responseObject, init, and (body, "application/json;charset=utf-8"). + +

  • Return responseObject.

    The type getter steps are to return this's From 8627b78fc7ac30585f0c18b980a09f71d5ed2bd5 Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Tue, 10 May 2022 18:08:13 +0200 Subject: [PATCH 10/14] capitalize UTF-8 It is capitalized in text/plain, so we should stay consistent. --- fetch.bs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fetch.bs b/fetch.bs index aeff44644..5ae75a828 100644 --- a/fetch.bs +++ b/fetch.bs @@ -7558,7 +7558,7 @@ are: relevant Realm.

  • Perform initialize a response given responseObject, init, and - (body, "application/json;charset=utf-8"). + (body, "application/json;charset=UTF-8").

  • Return responseObject. From 02367963c1272543544d84d0173add2321977117 Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Thu, 12 May 2022 14:03:56 +0200 Subject: [PATCH 11/14] remove charset --- fetch.bs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fetch.bs b/fetch.bs index 547cd8721..fd4a09385 100644 --- a/fetch.bs +++ b/fetch.bs @@ -7657,7 +7657,7 @@ are: relevant Realm.

  • Perform initialize a response given responseObject, init, and - (body, "application/json;charset=UTF-8"). + (body, "application/json").

  • Return responseObject. From 5bfb2870c2fb45d14a7ab2f8fff795befc70396c Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Thu, 12 May 2022 14:05:15 +0200 Subject: [PATCH 12/14] review comment --- fetch.bs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fetch.bs b/fetch.bs index fd4a09385..279498d19 100644 --- a/fetch.bs +++ b/fetch.bs @@ -7657,7 +7657,7 @@ are: relevant Realm.

  • Perform initialize a response given responseObject, init, and - (body, "application/json"). + (body, "application/json").

  • Return responseObject. From bc38ea737a7babdce478f2afc6fce81ed7d6f401 Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Fri, 13 May 2022 10:47:10 +0200 Subject: [PATCH 13/14] review comments --- fetch.bs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/fetch.bs b/fetch.bs index 279498d19..3507cf3bf 100644 --- a/fetch.bs +++ b/fetch.bs @@ -7544,8 +7544,9 @@ enum ResponseType { "basic", "cors", "default", "error", "opaque", "opaqueredire

  • Return responseObject. -

    To initialize a response, given a {{Response}} response, {{ResponseInit}} -init, and an optional body with type body, run these steps: +

    To initialize a response, given a {{Response}} object response, +{{ResponseInit}} init, and an optional body with type body, run +these steps:

    1. If init["{{ResponseInit/status}}"] is not in the range 200 to 599, inclusive, @@ -7557,8 +7558,8 @@ enum ResponseType { "basic", "cors", "default", "error", "opaque", "opaqueredire

    2. Set response's response's status to init["{{ResponseInit/status}}"]. -

    3. Set response's response's status message to - init["{{ResponseInit/statusText}}"]. +

    4. Set response's response's status message + to init["{{ResponseInit/statusText}}"].

    5. If init["{{ResponseInit/headers}}"] exists, then fill response's headers with @@ -7657,7 +7658,7 @@ are: relevant Realm.

    6. Perform initialize a response given responseObject, init, and - (body, "application/json"). + (body, "application/json").

    7. Return responseObject.

    From 7ef2f434d10e916c8ca04b006a43a936b3101f52 Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Fri, 13 May 2022 10:57:14 +0200 Subject: [PATCH 14/14] lint --- fetch.bs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fetch.bs b/fetch.bs index 3507cf3bf..02b64f038 100644 --- a/fetch.bs +++ b/fetch.bs @@ -7544,8 +7544,8 @@ enum ResponseType { "basic", "cors", "default", "error", "opaque", "opaqueredire
  • Return responseObject. -

    To initialize a response, given a {{Response}} object response, -{{ResponseInit}} init, and an optional body with type body, run +

    To initialize a response, given a {{Response}} object response, +{{ResponseInit}} init, and an optional body with type body, run these steps: