2018-07-23 14:46:30 -04:00
|
|
|
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
|
2018-11-14 21:19:38 -05:00
|
|
|
import { assert, createResolvable, notImplemented, isTypedArray } from "./util";
|
2018-10-17 13:04:28 -04:00
|
|
|
import * as 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-10-07 19:33:30 -04:00
|
|
|
import * as domTypes from "./dom_types";
|
2018-12-06 13:01:15 -05:00
|
|
|
import { TextDecoder, TextEncoder } from "./text_encoding";
|
2018-09-14 13:56:37 -04:00
|
|
|
import { DenoBlob } from "./blob";
|
2018-11-02 21:43:37 -04:00
|
|
|
import { Headers } from "./headers";
|
2018-10-26 12:14:06 -04:00
|
|
|
import * as io from "./io";
|
|
|
|
import { read, close } from "./files";
|
|
|
|
import { Buffer } from "./buffer";
|
|
|
|
|
|
|
|
class Body implements domTypes.Body, domTypes.ReadableStream, io.ReadCloser {
|
|
|
|
bodyUsed = false;
|
|
|
|
private _bodyPromise: null | Promise<ArrayBuffer> = null;
|
|
|
|
private _data: ArrayBuffer | null = null;
|
|
|
|
readonly locked: boolean = false; // TODO
|
|
|
|
readonly body: null | Body = this;
|
|
|
|
|
|
|
|
constructor(private rid: number, readonly contentType: string) {}
|
|
|
|
|
|
|
|
private async _bodyBuffer(): Promise<ArrayBuffer> {
|
|
|
|
assert(this._bodyPromise == null);
|
|
|
|
const buf = new Buffer();
|
|
|
|
try {
|
|
|
|
const nread = await buf.readFrom(this);
|
|
|
|
const ui8 = buf.bytes();
|
|
|
|
assert(ui8.byteLength === nread);
|
|
|
|
this._data = ui8.buffer.slice(
|
|
|
|
ui8.byteOffset,
|
|
|
|
ui8.byteOffset + nread
|
|
|
|
) as ArrayBuffer;
|
|
|
|
assert(this._data.byteLength === nread);
|
|
|
|
} finally {
|
|
|
|
this.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
return this._data;
|
|
|
|
}
|
|
|
|
|
|
|
|
async arrayBuffer(): Promise<ArrayBuffer> {
|
|
|
|
// If we've already bufferred the response, just return it.
|
|
|
|
if (this._data != null) {
|
|
|
|
return this._data;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there is no _bodyPromise yet, start it.
|
|
|
|
if (this._bodyPromise == null) {
|
|
|
|
this._bodyPromise = this._bodyBuffer();
|
|
|
|
}
|
2018-10-23 07:43:43 -04:00
|
|
|
|
2018-10-26 12:14:06 -04:00
|
|
|
return this._bodyPromise;
|
|
|
|
}
|
|
|
|
|
|
|
|
async blob(): Promise<domTypes.Blob> {
|
|
|
|
const arrayBuffer = await this.arrayBuffer();
|
|
|
|
return new DenoBlob([arrayBuffer], {
|
|
|
|
type: this.contentType
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async formData(): Promise<domTypes.FormData> {
|
|
|
|
return notImplemented();
|
|
|
|
}
|
|
|
|
|
|
|
|
// tslint:disable-next-line:no-any
|
|
|
|
async json(): Promise<any> {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
read(p: Uint8Array): Promise<io.ReadResult> {
|
|
|
|
return read(this.rid, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
close(): void {
|
|
|
|
close(this.rid);
|
|
|
|
}
|
|
|
|
|
|
|
|
async cancel(): Promise<void> {
|
|
|
|
return notImplemented();
|
|
|
|
}
|
|
|
|
|
|
|
|
getReader(): domTypes.ReadableStreamReader {
|
|
|
|
return notImplemented();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Response implements domTypes.Response {
|
2018-09-05 22:13:36 -04:00
|
|
|
readonly url: string = "";
|
2018-07-06 11:20:35 -04:00
|
|
|
statusText = "FIXME"; // TODO
|
|
|
|
readonly type = "basic"; // TODO
|
|
|
|
redirected = false; // TODO
|
2018-10-23 07:43:43 -04:00
|
|
|
headers: domTypes.Headers;
|
2018-10-07 19:33:30 -04:00
|
|
|
readonly trailer: Promise<domTypes.Headers>;
|
2018-10-26 12:14:06 -04:00
|
|
|
bodyUsed = false;
|
|
|
|
readonly body: Body;
|
2018-07-06 11:20:35 -04:00
|
|
|
|
2018-09-12 15:16:42 -04:00
|
|
|
constructor(
|
|
|
|
readonly status: number,
|
2018-10-26 12:14:06 -04:00
|
|
|
headersList: Array<[string, string]>,
|
|
|
|
rid: number,
|
|
|
|
body_: null | Body = null
|
2018-09-12 15:16:42 -04:00
|
|
|
) {
|
2018-08-15 20:57:36 -04:00
|
|
|
this.trailer = createResolvable();
|
2018-10-23 07:43:43 -04:00
|
|
|
this.headers = new Headers(headersList);
|
2018-10-26 12:14:06 -04:00
|
|
|
const contentType = this.headers.get("content-type") || "";
|
|
|
|
|
|
|
|
if (body_ == null) {
|
|
|
|
this.body = new Body(rid, contentType);
|
|
|
|
} else {
|
|
|
|
this.body = body_;
|
|
|
|
}
|
2018-07-06 11:20:35 -04:00
|
|
|
}
|
|
|
|
|
2018-10-26 12:14:06 -04:00
|
|
|
async arrayBuffer(): Promise<ArrayBuffer> {
|
|
|
|
return this.body.arrayBuffer();
|
2018-07-06 11:20:35 -04:00
|
|
|
}
|
|
|
|
|
2018-10-07 19:33:30 -04:00
|
|
|
async blob(): Promise<domTypes.Blob> {
|
2018-10-26 12:14:06 -04:00
|
|
|
return this.body.blob();
|
2018-07-06 11:20:35 -04:00
|
|
|
}
|
|
|
|
|
2018-10-07 19:33:30 -04:00
|
|
|
async formData(): Promise<domTypes.FormData> {
|
2018-10-26 12:14:06 -04:00
|
|
|
return this.body.formData();
|
2018-07-06 11:20:35 -04:00
|
|
|
}
|
|
|
|
|
2018-10-26 12:14:06 -04:00
|
|
|
// tslint:disable-next-line:no-any
|
|
|
|
async json(): Promise<any> {
|
|
|
|
return this.body.json();
|
2018-07-06 11:20:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async text(): Promise<string> {
|
2018-10-26 12:14:06 -04:00
|
|
|
return this.body.text();
|
2018-07-06 11:20:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
get ok(): boolean {
|
|
|
|
return 200 <= this.status && this.status < 300;
|
|
|
|
}
|
|
|
|
|
2018-10-07 19:33:30 -04:00
|
|
|
clone(): domTypes.Response {
|
2018-10-21 17:42:18 -04:00
|
|
|
if (this.bodyUsed) {
|
|
|
|
throw new TypeError(
|
|
|
|
"Failed to execute 'clone' on 'Response': Response body is already used"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const iterators = this.headers.entries();
|
|
|
|
const headersList: Array<[string, string]> = [];
|
|
|
|
for (const header of iterators) {
|
|
|
|
headersList.push(header);
|
|
|
|
}
|
|
|
|
|
2018-10-26 12:14:06 -04:00
|
|
|
return new Response(this.status, headersList, -1, this.body);
|
2018-07-06 11:20:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-14 20:36:34 -05:00
|
|
|
function msgHttpRequest(
|
|
|
|
builder: flatbuffers.Builder,
|
|
|
|
url: string,
|
|
|
|
method: null | string,
|
|
|
|
headers: null | domTypes.Headers
|
|
|
|
): flatbuffers.Offset {
|
|
|
|
const methodOffset = !method ? -1 : builder.createString(method);
|
|
|
|
let fieldsOffset: flatbuffers.Offset = -1;
|
|
|
|
const urlOffset = builder.createString(url);
|
|
|
|
if (headers) {
|
|
|
|
const kvOffsets: flatbuffers.Offset[] = [];
|
|
|
|
for (const [key, val] of headers.entries()) {
|
|
|
|
const keyOffset = builder.createString(key);
|
|
|
|
const valOffset = builder.createString(val);
|
|
|
|
msg.KeyValue.startKeyValue(builder);
|
|
|
|
msg.KeyValue.addKey(builder, keyOffset);
|
|
|
|
msg.KeyValue.addValue(builder, valOffset);
|
|
|
|
kvOffsets.push(msg.KeyValue.endKeyValue(builder));
|
|
|
|
}
|
|
|
|
fieldsOffset = msg.HttpHeader.createFieldsVector(builder, kvOffsets);
|
|
|
|
} else {
|
|
|
|
}
|
|
|
|
msg.HttpHeader.startHttpHeader(builder);
|
|
|
|
msg.HttpHeader.addIsRequest(builder, true);
|
|
|
|
msg.HttpHeader.addUrl(builder, urlOffset);
|
|
|
|
if (methodOffset >= 0) {
|
|
|
|
msg.HttpHeader.addMethod(builder, methodOffset);
|
|
|
|
}
|
|
|
|
if (fieldsOffset >= 0) {
|
|
|
|
msg.HttpHeader.addFields(builder, fieldsOffset);
|
|
|
|
}
|
|
|
|
return msg.HttpHeader.endHttpHeader(builder);
|
|
|
|
}
|
|
|
|
|
2018-10-14 16:29:50 -04:00
|
|
|
/** Fetch a resource from the network. */
|
2018-09-05 22:13:36 -04:00
|
|
|
export async function fetch(
|
2018-11-14 20:36:34 -05:00
|
|
|
input: domTypes.Request | string,
|
2018-10-07 19:33:30 -04:00
|
|
|
init?: domTypes.RequestInit
|
2018-10-26 12:14:06 -04:00
|
|
|
): Promise<Response> {
|
2018-11-14 20:36:34 -05:00
|
|
|
let url: string;
|
|
|
|
let method: string | null = null;
|
|
|
|
let headers: domTypes.Headers | null = null;
|
2018-11-14 21:19:38 -05:00
|
|
|
let body: ArrayBufferView | undefined;
|
2018-11-14 20:36:34 -05:00
|
|
|
|
|
|
|
if (typeof input === "string") {
|
|
|
|
url = input;
|
|
|
|
if (init != null) {
|
|
|
|
method = init.method || null;
|
|
|
|
if (init.headers) {
|
|
|
|
headers =
|
|
|
|
init.headers instanceof Headers
|
|
|
|
? init.headers
|
|
|
|
: new Headers(init.headers);
|
|
|
|
} else {
|
|
|
|
headers = null;
|
|
|
|
}
|
2018-11-14 21:19:38 -05:00
|
|
|
|
|
|
|
if (init.body) {
|
|
|
|
if (typeof init.body === "string") {
|
|
|
|
body = new TextEncoder().encode(init.body);
|
|
|
|
} else if (isTypedArray(init.body)) {
|
|
|
|
body = init.body;
|
|
|
|
} else {
|
|
|
|
notImplemented();
|
|
|
|
}
|
|
|
|
}
|
2018-11-14 20:36:34 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
url = input.url;
|
|
|
|
method = input.method;
|
|
|
|
headers = input.headers;
|
|
|
|
}
|
2018-09-05 22:13:36 -04:00
|
|
|
|
2018-10-30 15:02:18 -04:00
|
|
|
// Send Fetch message
|
2018-10-17 13:04:28 -04:00
|
|
|
const builder = flatbuffers.createBuilder();
|
2018-11-14 20:36:34 -05:00
|
|
|
const headerOff = msgHttpRequest(builder, url, method, headers);
|
2018-10-30 15:02:18 -04:00
|
|
|
msg.Fetch.startFetch(builder);
|
2018-11-14 20:36:34 -05:00
|
|
|
msg.Fetch.addHeader(builder, headerOff);
|
2018-09-05 22:13:36 -04:00
|
|
|
const resBase = await sendAsync(
|
|
|
|
builder,
|
2018-10-30 15:02:18 -04:00
|
|
|
msg.Any.Fetch,
|
2018-11-14 21:19:38 -05:00
|
|
|
msg.Fetch.endFetch(builder),
|
|
|
|
body
|
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-11-14 20:36:34 -05:00
|
|
|
const header = inner.header()!;
|
2018-10-26 12:14:06 -04:00
|
|
|
const bodyRid = inner.bodyRid();
|
2018-11-14 20:36:34 -05:00
|
|
|
assert(!header.isRequest());
|
|
|
|
const status = header.status();
|
2018-09-05 22:13:36 -04:00
|
|
|
|
2018-11-14 20:36:34 -05:00
|
|
|
const headersList = deserializeHeaderFields(header);
|
2018-09-12 15:16:42 -04:00
|
|
|
|
2018-10-26 12:14:06 -04:00
|
|
|
const response = new Response(status, headersList, bodyRid);
|
2018-09-05 22:13:36 -04:00
|
|
|
return response;
|
2018-07-06 11:20:35 -04:00
|
|
|
}
|
2018-11-14 20:36:34 -05:00
|
|
|
|
|
|
|
function deserializeHeaderFields(m: msg.HttpHeader): Array<[string, string]> {
|
|
|
|
const out: Array<[string, string]> = [];
|
|
|
|
for (let i = 0; i < m.fieldsLength(); i++) {
|
|
|
|
const item = m.fields(i)!;
|
|
|
|
out.push([item.key()!, item.value()!]);
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|