forked from denoland/deno
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
330 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/golang/protobuf/proto" | ||
"io/ioutil" | ||
"net/http" | ||
) | ||
|
||
func InitFetch() { | ||
Sub("fetch", func(buf []byte) []byte { | ||
msg := &Msg{} | ||
check(proto.Unmarshal(buf, msg)) | ||
switch msg.Command { | ||
case Msg_FETCH_REQ: | ||
return Fetch( | ||
msg.FetchReqId, | ||
msg.FetchReqUrl) | ||
default: | ||
panic("[fetch] Unexpected message " + string(buf)) | ||
} | ||
}) | ||
} | ||
|
||
func Fetch(id int32, targetUrl string) []byte { | ||
logDebug("Fetch %d %s", id, targetUrl) | ||
async(func() { | ||
resMsg := &Msg{ | ||
Command: Msg_FETCH_RES, | ||
FetchResId: id, | ||
} | ||
|
||
resp, err := http.Get(targetUrl) | ||
if err != nil { | ||
resMsg.Error = err.Error() | ||
PubMsg("fetch", resMsg) | ||
return | ||
} | ||
if resp == nil { | ||
resMsg.Error = "resp is nil " | ||
PubMsg("fetch", resMsg) | ||
return | ||
} | ||
|
||
resMsg.FetchResStatus = int32(resp.StatusCode) | ||
logDebug("fetch success %d %s", resMsg.FetchResStatus, targetUrl) | ||
PubMsg("fetch", resMsg) | ||
|
||
// Now we read the body and send another message0 | ||
|
||
defer resp.Body.Close() | ||
body, err := ioutil.ReadAll(resp.Body) | ||
if resp == nil { | ||
resMsg.Error = "resp is nil " | ||
PubMsg("fetch", resMsg) | ||
return | ||
} | ||
|
||
resMsg.FetchResBody = body | ||
PubMsg("fetch", resMsg) | ||
|
||
// TODO streaming. | ||
}) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
import { assert, log, createResolvable, Resolvable } from "./util"; | ||
import * as util from "./util"; | ||
import * as dispatch from "./dispatch"; | ||
import { main as pb } from "./msg.pb"; | ||
import { TextDecoder } from "text-encoding"; | ||
|
||
export function initFetch() { | ||
dispatch.sub("fetch", (payload: Uint8Array) => { | ||
const msg = pb.Msg.decode(payload); | ||
assert(msg.command === pb.Msg.Command.FETCH_RES); | ||
const id = msg.fetchResId; | ||
const f = fetchRequests.get(id); | ||
assert(f != null, `Couldn't find FetchRequest id ${id}`); | ||
|
||
f.onMsg(msg); | ||
}); | ||
} | ||
|
||
const fetchRequests = new Map<number, FetchRequest>(); | ||
|
||
class FetchResponse implements Response { | ||
readonly url: string; | ||
body: null; | ||
bodyUsed = false; // TODO | ||
status: number; | ||
statusText = "FIXME"; // TODO | ||
readonly type = "basic"; // TODO | ||
redirected = false; // TODO | ||
headers: null; // TODO | ||
//private bodyChunks: Uint8Array[] = []; | ||
private first = true; | ||
|
||
constructor(readonly req: FetchRequest) { | ||
this.url = req.url; | ||
} | ||
|
||
bodyWaiter: Resolvable<ArrayBuffer>; | ||
arrayBuffer(): Promise<ArrayBuffer> { | ||
this.bodyWaiter = createResolvable(); | ||
return this.bodyWaiter; | ||
} | ||
|
||
blob(): Promise<Blob> { | ||
throw Error("not implemented"); | ||
} | ||
|
||
formData(): Promise<FormData> { | ||
throw Error("not implemented"); | ||
} | ||
|
||
async json(): Promise<object> { | ||
const text = await this.text(); | ||
return JSON.parse(text); | ||
} | ||
|
||
async text(): Promise<string> { | ||
const ab = await this.arrayBuffer(); | ||
const enc = new TextDecoder("utf-8"); | ||
// Maybe new Uint8Array(ab) | ||
return enc.decode(ab); | ||
} | ||
|
||
get ok(): boolean { | ||
return 200 <= this.status && this.status < 300; | ||
} | ||
|
||
clone(): Response { | ||
throw Error("not implemented"); | ||
} | ||
|
||
onHeader: (res: Response) => void; | ||
onError: (error: Error) => void; | ||
|
||
onMsg(msg: pb.Msg) { | ||
if (msg.error !== null && msg.error !== "") { | ||
//throw new Error(msg.error) | ||
this.onError(new Error(msg.error)); | ||
return; | ||
} | ||
|
||
if (this.first) { | ||
this.first = false; | ||
this.status = msg.fetchResStatus; | ||
this.onHeader(this); | ||
} else { | ||
// Body message. Assuming it all comes in one message now. | ||
const ab = util.typedArrayToArrayBuffer(msg.fetchResBody); | ||
this.bodyWaiter.resolve(ab); | ||
} | ||
} | ||
} | ||
|
||
let nextFetchId = 0; | ||
//TODO implements Request | ||
class FetchRequest { | ||
private readonly id: number; | ||
response: FetchResponse; | ||
constructor(readonly url: string) { | ||
this.id = nextFetchId++; | ||
fetchRequests.set(this.id, this); | ||
this.response = new FetchResponse(this); | ||
} | ||
|
||
onMsg(msg: pb.Msg) { | ||
this.response.onMsg(msg); | ||
} | ||
|
||
destroy() { | ||
fetchRequests.delete(this.id); | ||
} | ||
|
||
start() { | ||
log("dispatch FETCH_REQ", this.id, this.url); | ||
const res = dispatch.sendMsg("fetch", { | ||
command: pb.Msg.Command.FETCH_REQ, | ||
fetchReqId: this.id, | ||
fetchReqUrl: this.url | ||
}); | ||
assert(res == null); | ||
} | ||
} | ||
|
||
export function fetch( | ||
input?: Request | string, | ||
init?: RequestInit | ||
): Promise<Response> { | ||
const fetchReq = new FetchRequest(input as string); | ||
const response = fetchReq.response; | ||
return new Promise((resolve, reject) => { | ||
// tslint:disable-next-line:no-any | ||
response.onHeader = (response: any) => { | ||
log("onHeader"); | ||
resolve(response); | ||
}; | ||
response.onError = (error: Error) => { | ||
log("onError", error); | ||
reject(error); | ||
}; | ||
fetchReq.start(); | ||
}); | ||
} | ||
|
||
/* | ||
fetch('http:https://example.com/movies.json') | ||
.then(function(response) { | ||
return response.json(); | ||
}) | ||
.then(function(myJson) { | ||
console.log(myJson); | ||
}); | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.