Skip to content

Commit 8340e4d

Browse files
committed
update
1 parent 1920fd0 commit 8340e4d

File tree

2 files changed

+86
-24
lines changed

2 files changed

+86
-24
lines changed

examples/fetch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const foo = async () => {
33
const res = await fetch("https://developer.mozilla.org");
44
console.log(res);
55
} catch (e) {
6-
console.error(e);
6+
console.error("error", e);
77
}
88
};
99

runtime/src/ext/fetch/fetch/mod.ts

Lines changed: 85 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@
1010

1111
type RequestInfo = Request | URL;
1212

13+
class Fetch {
14+
// TODO: Event
15+
constructor() {
16+
(this as any).dispatcher = {};
17+
(this as any).connection = null;
18+
(this as any).dump = false;
19+
(this as any).state = "ongoing";
20+
}
21+
}
22+
1323
/** The fetch(input, init) method steps are: */
1424
const fetch = (input: RequestInfo, init = undefined) => {
1525
// 1. Let p be a new promise.
@@ -80,9 +90,12 @@ const fetch = (input: RequestInfo, init = undefined) => {
8090
// 3. If response is a network error, then reject p with a TypeError and abort these steps.
8191
// 4. Set responseObject to the result of creating a Response object, given response, "immutable", and relevantRealm.
8292
// 5. Resolve p with responseObject.
93+
controller = fetching({
94+
request,
95+
});
8396

8497
// 13. Return p.
85-
return p;
98+
return p.promise;
8699
};
87100

88101
(globalThis as unknown as { fetch: typeof fetch }).fetch = fetch;
@@ -123,21 +136,19 @@ const fetching = (
123136
processResponseEndOfBody,
124137
processResponseConsumeBody,
125138
processEarlyHintsResponse,
126-
useParallelQueue = false,
127139
}: {
128140
request: any;
129-
processRequestBodyChunkLength: any;
130-
processRequestEndOfBody: any;
131-
processResponse: any;
132-
processResponseEndOfBody: any;
133-
processResponseConsumeBody: any;
134-
processEarlyHintsResponse: any;
135-
useParallelQueue?: boolean;
141+
processRequestBodyChunkLength?: any;
142+
processRequestEndOfBody?: any;
143+
processResponse?: any;
144+
processResponseEndOfBody?: any;
145+
processResponseConsumeBody?: any;
146+
processEarlyHintsResponse?: any;
136147
},
137148
) => {
138149
// 1. Assert: request’s mode is "navigate" or processEarlyHintsResponse is null.
139150
// NOTE: Processing of early hints (responses whose status is 103) is only vetted for navigations.
140-
if (request.mode === "navigate" || processEarlyHintsResponse == null) {
151+
if (request.mode === "navigate") {
141152
throw new Error("error");
142153
}
143154

@@ -148,20 +159,49 @@ const fetching = (
148159
let crossOriginIsolatedCapability = false;
149160

150161
// 4. Populate request from client given request.
151-
populateRequest();
162+
// populateRequest();
163+
152164
// 5. If request’s client is non-null, then:
153-
// 1. Set taskDestination to request’s client’s global object.
154-
// 2. Set crossOriginIsolatedCapability to request’s client’s cross-origin isolated capability.
165+
if (request.client != null) {
166+
// 1. Set taskDestination to request’s client’s global object.
167+
taskDestination = request.client.globalObject;
168+
// 2. Set crossOriginIsolatedCapability to request’s client’s cross-origin isolated capability.
169+
crossOriginIsolatedCapability =
170+
request.client.crossOriginIsolatedCapability;
171+
}
172+
173+
// TODO
155174
// 6. If useParallelQueue is true, then set taskDestination to the result of starting a new parallel queue.
175+
176+
// TODO
156177
// 7. Let timingInfo be a new fetch timing info whose start time and post-redirect start time are
157178
// the coarsened shared current time given crossOriginIsolatedCapability,
158179
// and render-blocking is set to request’s render-blocking.
159-
// 8. Let fetchParams be a new fetch params whose request is request, timing info is timingInfo,
160-
// process request body chunk length is processRequestBodyChunkLength, process request end-of-body
161-
// is processRequestEndOfBody, process early hints response is processEarlyHintsResponse,
162-
// process response is processResponse, process response consume body is processResponseConsumeBody,
163-
// process response end-of-body is processResponseEndOfBody, task destination is taskDestination,
180+
let timingInfo = 0;
181+
182+
// 8. Let fetchParams be a new fetch params whose
183+
// request is request, timing info is timingInfo,
184+
// process request body chunk length is processRequestBodyChunkLength,
185+
// process request end-of-body is processRequestEndOfBody,
186+
// process early hints response is processEarlyHintsResponse,
187+
// process response is processResponse,
188+
// process response consume body is processResponseConsumeBody,
189+
// process response end-of-body is processResponseEndOfBody,
190+
// task destination is taskDestination,
164191
// and cross-origin isolated capability is crossOriginIsolatedCapability.
192+
const fetchParams = {
193+
controller: new Fetch(),
194+
timingInfo, // TODO
195+
processRequestBodyChunkLength,
196+
processRequestEndOfBody,
197+
processResponse,
198+
processResponseConsumeBody,
199+
processResponseEndOfBody,
200+
taskDestination,
201+
crossOriginIsolatedCapability,
202+
};
203+
204+
// TODO: step9, 10
165205
// 9. If request’s body is a byte sequence, then set request’s body to request’s body as a body.
166206
// 10. If all of the following conditions are true:
167207
// - request’s URL’s scheme is an HTTP(S) scheme
@@ -177,8 +217,13 @@ const fetching = (
177217
// given request’s URL, request’s destination, request’s mode, request’s credentials mode, request’s integrity metadata,
178218
// and onPreloadedResponseAvailable.
179219
// 4. If foundPreloadedResource is true and fetchParams’s preloaded response candidate is null, then set fetchParams’s preloaded response candidate to "pending".
220+
180221
// 11. If request’s header list does not contain `Accept`, then:
222+
// if (!request.headersList.contains("accept", true)) {
181223
// 1. Let value be `*/*`.
224+
// const value = "*/*";
225+
226+
// TODO
182227
// 2. If request’s initiator is "prefetch", then set value to the document `Accept` header value.
183228
// 3. Otherwise, the user agent should set value to the first matching statement, if any, switching on request’s destination:
184229
// ↪︎ "document"
@@ -191,19 +236,36 @@ const fetching = (
191236
// `application/json,*/*;q=0.5`
192237
// ↪︎ "style"
193238
// `text/css,*/*;q=0.1`
239+
194240
// 4. Append (`Accept`, value) to request’s header list.
195-
// 12. If request’s header list does not contain `Accept-Language`, then user agents should append (`Accept-Language, an appropriate header value) to request’s header list.
196-
// 13. If request’s internal priority is null, then use request’s priority, initiator, destination, and render-blocking
197-
// in an implementation-defined manner to set request’s internal priority to an implementation-defined object.
241+
// request.headersList.append("accept", value, true);
242+
// }
243+
244+
// 12. If request’s header list does not contain `Accept-Language`,
245+
// then user agents should append
246+
// (`Accept-Language, an appropriate header value)
247+
// to request’s header list.
248+
// if (!request.headersList.contains("accept-language", true)) {
249+
// request.headersList.append("accept-language", "*", true);
250+
// }
251+
252+
// TODO
253+
// 13. If request’s internal priority is null, then use request’s priority,
254+
// initiator, destination, and render-blocking in an implementation-defined
255+
// manner to set request’s internal priority to an implementation-defined object.
256+
198257
// NOTE: The implementation-defined object could encompass stream weight and dependency for HTTP/2, priorities used
199258
// in Extensible Prioritization Scheme for HTTP for transports where it applies (including HTTP/3),
200259
// and equivalent information used to prioritize dispatch and processing of HTTP/1 fetches. [RFC9218]
201260
// 14. If request is a subresource request, then:
202261
// 1. Let record be a new fetch record whose request is request and controller is fetchParams’s controller.
203262
// 2. Append record to request’s client’s fetch group list of fetch records.
263+
204264
// 15. Run main fetch given fetchParams.
205-
// 16. Return fetchParams’s controller.
206265
mainFetch(fetchParams);
266+
267+
// 16. Return fetchParams’s controller.
268+
return fetchParams.controller;
207269
};
208270

209271
/**
@@ -229,5 +291,5 @@ const populateRequest = () => {
229291
* @see https://fetch.spec.whatwg.org/#main-fetch
230292
*/
231293
const mainFetch = (fetchParams: any) => {
232-
throw new Error("TODO");
294+
console.log(fetchParams);
233295
};

0 commit comments

Comments
 (0)