|
| 1 | +interface ResponseInit { |
| 2 | + headers?: HeadersInit; |
| 3 | + status?: number; |
| 4 | + statusText?: string; |
| 5 | +} |
| 6 | + |
| 7 | +class Response { |
| 8 | + #response; |
| 9 | + #headers; |
| 10 | + /** |
| 11 | + * The new Response(body, init) constructor steps are: |
| 12 | + * @see https://fetch.spec.whatwg.org/#dom-response |
| 13 | + */ |
| 14 | + constructor(body, init: ResponseInit) { |
| 15 | + // 1. Set this’s response to a new response. |
| 16 | + this.#response = makeResponse(init); |
| 17 | + |
| 18 | + // TODO: implement module |
| 19 | + // 2. Set this’s headers to a new Headers object with this’s relevant realm, whose header list is this’s response’s header list and guard is "response". |
| 20 | + |
| 21 | + // 3. Let bodyWithType be null. |
| 22 | + let bodyWithType = null; |
| 23 | + |
| 24 | + // 4. If body is non-null, then set bodyWithType to the result of extracting body. |
| 25 | + if (body != null){ |
| 26 | + bodyWithType = |
| 27 | + } |
| 28 | + // 5. Perform initialize a response given this, init, and bodyWithType. |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +// TODO: headers |
| 33 | +function makeResponse(init: ResponseInit) { |
| 34 | + return { |
| 35 | + aborted: false, |
| 36 | + rangeRequested: false, |
| 37 | + timingAllowPassed: false, |
| 38 | + requestIncludesCredentials: false, |
| 39 | + type: "default", |
| 40 | + status: 200, |
| 41 | + timingInfo: null, |
| 42 | + cacheState: "", |
| 43 | + statusText: "", |
| 44 | + url: "", |
| 45 | + ...init, |
| 46 | + }; |
| 47 | +} |
| 48 | + |
| 49 | +/** |
| 50 | + * To extract a body with type from a byte sequence or BodyInit object object, with an optional boolean keepalive (default false) |
| 51 | + * @see https://fetch.spec.whatwg.org/#concept-bodyinit-extract |
| 52 | + */ |
| 53 | + function extractBody (object, keepalive = false) { |
| 54 | + // 1. Let stream be null. |
| 55 | + let stream = null; |
| 56 | + // 2. If object is a ReadableStream object, then set stream to object. |
| 57 | + // TODO: implement ReadableStream |
| 58 | + // 3. Otherwise, if object is a Blob object, set stream to the result of running object’s get stream. |
| 59 | + // 4. Otherwise, set stream to a new ReadableStream object, and set up stream with byte reading support. |
| 60 | + // 5. Assert: stream is a ReadableStream object. |
| 61 | + |
| 62 | + // 6, Let action be null. |
| 63 | + let action = null; |
| 64 | + |
| 65 | + // 7. Let source be null. |
| 66 | + let source = null; |
| 67 | + |
| 68 | + // 8. Let length be null. |
| 69 | + let length = null; |
| 70 | + |
| 71 | + // Let type be null. |
| 72 | + let type = null |
| 73 | + |
| 74 | + // Switch on object: |
| 75 | + if (Blob.prototype.isPrototypeOf(object)){ |
| 76 | + // Blob: |
| 77 | + // 1. Set source to object. |
| 78 | + // 2. Set length to object’s size. |
| 79 | + // 3. If object’s type attribute is not the empty byte sequence, set type to its value. |
| 80 | + stream = object.stream(); |
| 81 | + stream = object |
| 82 | + length = object.size |
| 83 | + } else if (object ==="string"){ |
| 84 | + // byte sequence: |
| 85 | + // 1. Set source to object. |
| 86 | + source = object |
| 87 | + } |
| 88 | + |
| 89 | + |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | + // BufferSource |
| 94 | + // Set source to a copy of the bytes held by object. |
| 95 | + |
| 96 | + // FormData |
| 97 | + // Set action to this step: run the multipart/form-data encoding algorithm, with object’s entry list and UTF-8. |
| 98 | + |
| 99 | + // Set source to object. |
| 100 | + |
| 101 | + // Set length to unclear, see html/6424 for improving this. |
| 102 | + |
| 103 | + // Set type to `multipart/form-data; boundary=`, followed by the multipart/form-data boundary string generated by the multipart/form-data encoding algorithm. |
| 104 | + |
| 105 | + // URLSearchParams |
| 106 | + // Set source to the result of running the application/x-www-form-urlencoded serializer with object’s list. |
| 107 | + |
| 108 | + // Set type to `application/x-www-form-urlencoded;charset=UTF-8`. |
| 109 | + |
| 110 | + // scalar value string |
| 111 | + // Set source to the UTF-8 encoding of object. |
| 112 | + |
| 113 | + // Set type to `text/plain;charset=UTF-8`. |
| 114 | + |
| 115 | + // ReadableStream |
| 116 | + // If keepalive is true, then throw a TypeError. |
| 117 | + |
| 118 | + // If object is disturbed or locked, then throw a TypeError. |
| 119 | + |
| 120 | + // If source is a byte sequence, then set action to a step that returns source and length to source’s length. |
| 121 | + |
| 122 | + // If action is non-null, then run these steps in parallel: |
| 123 | + |
| 124 | + // Run action. |
| 125 | + |
| 126 | + // Whenever one or more bytes are available and stream is not errored, enqueue the result of creating a Uint8Array from the available bytes into stream. |
| 127 | + |
| 128 | + // When running action is done, close stream. |
| 129 | + |
| 130 | + // Let body be a body whose stream is stream, source is source, and length is length. |
| 131 | + |
| 132 | + // Return (body, type). |
| 133 | + } |
0 commit comments