2018-07-23 14:46:30 -04:00
|
|
|
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
|
2018-07-06 11:20:35 -04:00
|
|
|
import {
|
|
|
|
assert,
|
|
|
|
log,
|
|
|
|
createResolvable,
|
|
|
|
Resolvable,
|
2018-08-19 15:04:27 -04:00
|
|
|
typedArrayToArrayBuffer,
|
2018-08-21 12:10:09 -04:00
|
|
|
notImplemented
|
2018-07-06 11:20:35 -04:00
|
|
|
} from "./util";
|
2018-08-15 20:57:36 -04:00
|
|
|
import { flatbuffers } from "flatbuffers";
|
2018-09-09 18:54:42 -04:00
|
|
|
import { sendAsync } from "./dispatch";
|
2018-10-03 21:18:23 -04:00
|
|
|
import * as msg from "gen/msg_generated";
|
2018-08-15 20:57:36 -04:00
|
|
|
import {
|
|
|
|
Headers,
|
|
|
|
Request,
|
|
|
|
Response,
|
|
|
|
Blob,
|
|
|
|
RequestInit,
|
2018-09-12 15:16:42 -04:00
|
|
|
HeadersInit,
|
2018-08-15 20:57:36 -04:00
|
|
|
FormData
|
2018-09-14 14:13:38 -04:00
|
|
|
} from "./dom_types";
|
2018-08-15 20:57:36 -04:00
|
|
|
import { TextDecoder } from "./text_encoding";
|
2018-09-14 13:56:37 -04:00
|
|
|
import { DenoBlob } from "./blob";
|
2018-08-15 20:57:36 -04:00
|
|
|
|
2018-09-30 10:31:50 -04:00
|
|
|
// ref: https://fetch.spec.whatwg.org/#dom-headers
|
2018-09-12 15:16:42 -04:00
|
|
|
export class DenoHeaders implements Headers {
|
2018-09-30 10:31:50 -04:00
|
|
|
private headerMap: Map<string, string> = new Map();
|
2018-09-12 15:16:42 -04:00
|
|
|
|
|
|
|
constructor(init?: HeadersInit) {
|
2018-09-30 10:31:50 -04:00
|
|
|
if (arguments.length === 0 || init === undefined) {
|
|
|
|
return;
|
2018-09-12 15:16:42 -04:00
|
|
|
}
|
|
|
|
|
2018-09-30 10:31:50 -04:00
|
|
|
if (init instanceof DenoHeaders) {
|
|
|
|
// init is the instance of Header
|
|
|
|
init.forEach((value: string, name: string) => {
|
|
|
|
this.headerMap.set(name, value);
|
|
|
|
});
|
|
|
|
} else if (Array.isArray(init)) {
|
|
|
|
// init is a sequence
|
|
|
|
init.forEach(item => {
|
|
|
|
if (item.length !== 2) {
|
2018-09-12 15:16:42 -04:00
|
|
|
throw new TypeError("Failed to construct 'Headers': Invalid value");
|
|
|
|
}
|
2018-09-30 10:31:50 -04:00
|
|
|
const [name, value] = this.normalizeParams(item[0], item[1]);
|
|
|
|
const v = this.headerMap.get(name);
|
|
|
|
const str = v ? `${v}, ${value}` : value;
|
|
|
|
this.headerMap.set(name, str);
|
|
|
|
});
|
|
|
|
} else if (Object.prototype.toString.call(init) === "[object Object]") {
|
|
|
|
// init is a object
|
|
|
|
const names = Object.keys(init);
|
|
|
|
names.forEach(name => {
|
|
|
|
const value = (init as Record<string, string>)[name];
|
|
|
|
const [newname, newvalue] = this.normalizeParams(name, value);
|
|
|
|
this.headerMap.set(newname, newvalue);
|
|
|
|
});
|
2018-09-12 15:16:42 -04:00
|
|
|
} else {
|
2018-09-30 10:31:50 -04:00
|
|
|
throw new TypeError("Failed to construct 'Headers': Invalid value");
|
2018-09-12 15:16:42 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-30 10:31:50 -04:00
|
|
|
private normalizeParams(name: string, value?: string): string[] {
|
|
|
|
name = String(name).toLowerCase();
|
|
|
|
value = String(value).trim();
|
|
|
|
return [name, value];
|
|
|
|
}
|
|
|
|
|
2018-08-15 20:57:36 -04:00
|
|
|
append(name: string, value: string): void {
|
2018-09-30 10:31:50 -04:00
|
|
|
const [newname, newvalue] = this.normalizeParams(name, value);
|
|
|
|
const v = this.headerMap.get(newname);
|
|
|
|
const str = v ? `${v}, ${newvalue}` : newvalue;
|
|
|
|
this.headerMap.set(newname, str);
|
2018-08-15 20:57:36 -04:00
|
|
|
}
|
2018-09-12 15:16:42 -04:00
|
|
|
|
2018-08-15 20:57:36 -04:00
|
|
|
delete(name: string): void {
|
2018-09-30 10:31:50 -04:00
|
|
|
const [newname] = this.normalizeParams(name);
|
|
|
|
this.headerMap.delete(newname);
|
2018-08-15 20:57:36 -04:00
|
|
|
}
|
2018-09-30 10:31:50 -04:00
|
|
|
|
2018-08-15 20:57:36 -04:00
|
|
|
get(name: string): string | null {
|
2018-09-30 10:31:50 -04:00
|
|
|
const [newname] = this.normalizeParams(name);
|
|
|
|
const value = this.headerMap.get(newname);
|
|
|
|
return value || null;
|
2018-08-15 20:57:36 -04:00
|
|
|
}
|
2018-09-30 10:31:50 -04:00
|
|
|
|
2018-08-15 20:57:36 -04:00
|
|
|
has(name: string): boolean {
|
2018-09-30 10:31:50 -04:00
|
|
|
const [newname] = this.normalizeParams(name);
|
|
|
|
return this.headerMap.has(newname);
|
2018-08-15 20:57:36 -04:00
|
|
|
}
|
2018-09-30 10:31:50 -04:00
|
|
|
|
2018-08-15 20:57:36 -04:00
|
|
|
set(name: string, value: string): void {
|
2018-09-30 10:31:50 -04:00
|
|
|
const [newname, newvalue] = this.normalizeParams(name, value);
|
|
|
|
this.headerMap.set(newname, newvalue);
|
2018-08-15 20:57:36 -04:00
|
|
|
}
|
2018-09-30 10:31:50 -04:00
|
|
|
|
2018-08-15 20:57:36 -04:00
|
|
|
forEach(
|
|
|
|
callbackfn: (value: string, key: string, parent: Headers) => void,
|
|
|
|
// tslint:disable-next-line:no-any
|
|
|
|
thisArg?: any
|
|
|
|
): void {
|
2018-09-30 10:31:50 -04:00
|
|
|
this.headerMap.forEach((value, name) => {
|
|
|
|
callbackfn(value, name, this);
|
|
|
|
});
|
2018-08-15 20:57:36 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-06 11:20:35 -04:00
|
|
|
class FetchResponse implements Response {
|
2018-09-05 22:13:36 -04:00
|
|
|
readonly url: string = "";
|
2018-07-06 11:20:35 -04:00
|
|
|
body: null;
|
|
|
|
bodyUsed = false; // TODO
|
|
|
|
statusText = "FIXME"; // TODO
|
|
|
|
readonly type = "basic"; // TODO
|
|
|
|
redirected = false; // TODO
|
2018-09-12 15:16:42 -04:00
|
|
|
headers: DenoHeaders;
|
2018-08-15 20:57:36 -04:00
|
|
|
readonly trailer: Promise<Headers>;
|
2018-07-06 11:20:35 -04:00
|
|
|
//private bodyChunks: Uint8Array[] = [];
|
|
|
|
private first = true;
|
2018-08-15 20:57:36 -04:00
|
|
|
private bodyWaiter: Resolvable<ArrayBuffer>;
|
2018-07-06 11:20:35 -04:00
|
|
|
|
2018-09-12 15:16:42 -04:00
|
|
|
constructor(
|
|
|
|
readonly status: number,
|
|
|
|
readonly body_: ArrayBuffer,
|
|
|
|
headersList: Array<[string, string]>
|
|
|
|
) {
|
2018-08-15 20:57:36 -04:00
|
|
|
this.bodyWaiter = createResolvable();
|
|
|
|
this.trailer = createResolvable();
|
2018-09-12 15:16:42 -04:00
|
|
|
this.headers = new DenoHeaders(headersList);
|
2018-09-05 22:13:36 -04:00
|
|
|
setTimeout(() => {
|
|
|
|
this.bodyWaiter.resolve(body_);
|
|
|
|
}, 0);
|
2018-07-06 11:20:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
arrayBuffer(): Promise<ArrayBuffer> {
|
|
|
|
return this.bodyWaiter;
|
|
|
|
}
|
|
|
|
|
2018-08-20 21:03:11 -04:00
|
|
|
async blob(): Promise<Blob> {
|
2018-09-14 13:56:37 -04:00
|
|
|
const arrayBuffer = await this.arrayBuffer();
|
|
|
|
return new DenoBlob([arrayBuffer], {
|
|
|
|
type: this.headers.get("content-type") || ""
|
|
|
|
});
|
2018-07-06 11:20:35 -04:00
|
|
|
}
|
|
|
|
|
2018-08-20 21:03:11 -04:00
|
|
|
async formData(): Promise<FormData> {
|
2018-08-19 15:04:27 -04:00
|
|
|
notImplemented();
|
2018-08-20 21:03:11 -04:00
|
|
|
return {} as FormData;
|
2018-07-06 11:20:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async json(): Promise<object> {
|
|
|
|
const text = await this.text();
|
|
|
|
return JSON.parse(text);
|
|
|
|
}
|
|
|
|
|
|
|
|
async text(): Promise<string> {
|
|
|
|
const ab = await this.arrayBuffer();
|
|
|
|
const decoder = new TextDecoder("utf-8");
|
|
|
|
return decoder.decode(ab);
|
|
|
|
}
|
|
|
|
|
|
|
|
get ok(): boolean {
|
|
|
|
return 200 <= this.status && this.status < 300;
|
|
|
|
}
|
|
|
|
|
|
|
|
clone(): Response {
|
2018-08-19 15:04:27 -04:00
|
|
|
notImplemented();
|
2018-08-20 21:03:11 -04:00
|
|
|
return {} as Response;
|
2018-07-06 11:20:35 -04:00
|
|
|
}
|
|
|
|
|
2018-08-15 20:57:36 -04:00
|
|
|
onHeader?: (res: FetchResponse) => void;
|
|
|
|
onError?: (error: Error) => void;
|
2018-07-06 11:20:35 -04:00
|
|
|
|
2018-10-03 21:18:23 -04:00
|
|
|
onMsg(base: msg.Base) {
|
2018-09-05 22:13:36 -04:00
|
|
|
/*
|
2018-08-15 20:57:36 -04:00
|
|
|
const error = base.error();
|
|
|
|
if (error != null) {
|
|
|
|
assert(this.onError != null);
|
|
|
|
this.onError!(new Error(error));
|
2018-07-06 11:20:35 -04:00
|
|
|
return;
|
|
|
|
}
|
2018-09-05 22:13:36 -04:00
|
|
|
*/
|
2018-07-06 11:20:35 -04:00
|
|
|
|
|
|
|
if (this.first) {
|
|
|
|
this.first = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-05 22:13:36 -04:00
|
|
|
export async function fetch(
|
2018-07-06 11:20:35 -04:00
|
|
|
input?: Request | string,
|
|
|
|
init?: RequestInit
|
|
|
|
): Promise<Response> {
|
2018-09-05 22:13:36 -04:00
|
|
|
const url = input as string;
|
|
|
|
log("dispatch FETCH_REQ", url);
|
|
|
|
|
|
|
|
// Send FetchReq message
|
|
|
|
const builder = new flatbuffers.Builder();
|
|
|
|
const url_ = builder.createString(url);
|
2018-10-03 21:18:23 -04:00
|
|
|
msg.FetchReq.startFetchReq(builder);
|
|
|
|
msg.FetchReq.addUrl(builder, url_);
|
2018-09-05 22:13:36 -04:00
|
|
|
const resBase = await sendAsync(
|
|
|
|
builder,
|
2018-10-03 21:18:23 -04:00
|
|
|
msg.Any.FetchReq,
|
|
|
|
msg.FetchReq.endFetchReq(builder)
|
2018-09-05 22:13:36 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
// Decode FetchRes
|
2018-10-03 21:18:23 -04:00
|
|
|
assert(msg.Any.FetchRes === resBase.innerType());
|
|
|
|
const inner = new msg.FetchRes();
|
2018-10-03 21:12:23 -04:00
|
|
|
assert(resBase.inner(inner) != null);
|
2018-09-05 22:13:36 -04:00
|
|
|
|
2018-10-03 21:12:23 -04:00
|
|
|
const status = inner.status();
|
|
|
|
const bodyArray = inner.bodyArray();
|
2018-09-05 22:13:36 -04:00
|
|
|
assert(bodyArray != null);
|
|
|
|
const body = typedArrayToArrayBuffer(bodyArray!);
|
|
|
|
|
2018-09-12 15:16:42 -04:00
|
|
|
const headersList: Array<[string, string]> = [];
|
2018-10-03 21:12:23 -04:00
|
|
|
const len = inner.headerKeyLength();
|
2018-09-12 15:16:42 -04:00
|
|
|
for (let i = 0; i < len; ++i) {
|
2018-10-03 21:12:23 -04:00
|
|
|
const key = inner.headerKey(i);
|
|
|
|
const value = inner.headerValue(i);
|
2018-09-12 15:16:42 -04:00
|
|
|
headersList.push([key, value]);
|
|
|
|
}
|
|
|
|
|
|
|
|
const response = new FetchResponse(status, body, headersList);
|
2018-09-05 22:13:36 -04:00
|
|
|
return response;
|
2018-07-06 11:20:35 -04:00
|
|
|
}
|