summaryrefslogtreecommitdiff
path: root/js/fetch.ts
blob: 1c733e0669ff51e29b2ecb285e67fc5d189e88e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
import {
  assert,
  log,
  createResolvable,
  Resolvable,
  typedArrayToArrayBuffer,
  notImplemented
} from "./util";
import { flatbuffers } from "flatbuffers";
import { libdeno } from "./libdeno";
import { deno as fbs } from "gen/msg_generated";
import {
  Headers,
  Request,
  Response,
  Blob,
  RequestInit,
  FormData
} from "./fetch_types";
import { TextDecoder } from "./text_encoding";

/** @internal */
export function onFetchRes(base: fbs.Base, msg: fbs.FetchRes) {
  const id = msg.id();
  const req = fetchRequests.get(id);
  assert(req != null, `Couldn't find FetchRequest id ${id}`);
  req!.onMsg(base, msg);
}

const fetchRequests = new Map<number, FetchRequest>();

class DenoHeaders implements Headers {
  append(name: string, value: string): void {
    assert(false, "Implement me");
  }
  delete(name: string): void {
    assert(false, "Implement me");
  }
  get(name: string): string | null {
    assert(false, "Implement me");
    return null;
  }
  has(name: string): boolean {
    assert(false, "Implement me");
    return false;
  }
  set(name: string, value: string): void {
    assert(false, "Implement me");
  }
  forEach(
    callbackfn: (value: string, key: string, parent: Headers) => void,
    // tslint:disable-next-line:no-any
    thisArg?: any
  ): void {
    assert(false, "Implement me");
  }
}

class FetchResponse implements Response {
  readonly url: string;
  body: null;
  bodyUsed = false; // TODO
  status = 0;
  statusText = "FIXME"; // TODO
  readonly type = "basic"; // TODO
  redirected = false; // TODO
  headers = new DenoHeaders();
  readonly trailer: Promise<Headers>;
  //private bodyChunks: Uint8Array[] = [];
  private first = true;
  private bodyWaiter: Resolvable<ArrayBuffer>;

  constructor(readonly req: FetchRequest) {
    this.url = req.url;
    this.bodyWaiter = createResolvable();
    this.trailer = createResolvable();
  }

  arrayBuffer(): Promise<ArrayBuffer> {
    return this.bodyWaiter;
  }

  async blob(): Promise<Blob> {
    notImplemented();
    return {} as Blob;
  }

  async formData(): Promise<FormData> {
    notImplemented();
    return {} as FormData;
  }

  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 {
    notImplemented();
    return {} as Response;
  }

  onHeader?: (res: FetchResponse) => void;
  onError?: (error: Error) => void;

  onMsg(base: fbs.Base, msg: fbs.FetchRes) {
    const error = base.error();
    if (error != null) {
      assert(this.onError != null);
      this.onError!(new Error(error));
      return;
    }

    if (this.first) {
      this.first = false;
      this.status = msg.status();
      assert(this.onHeader != null);
      this.onHeader!(this);
    } else {
      // Body message. Assuming it all comes in one message now.
      const bodyArray = msg.bodyArray();
      assert(bodyArray != null);
      const ab = typedArrayToArrayBuffer(bodyArray!);
      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(base: fbs.Base, msg: fbs.FetchRes) {
    this.response.onMsg(base, msg);
  }

  destroy() {
    fetchRequests.delete(this.id);
  }

  start() {
    log("dispatch FETCH_REQ", this.id, this.url);

    // Send FetchReq message
    const builder = new flatbuffers.Builder();
    const url = builder.createString(this.url);
    fbs.FetchReq.startFetchReq(builder);
    fbs.FetchReq.addId(builder, this.id);
    fbs.FetchReq.addUrl(builder, url);
    const msg = fbs.FetchReq.endFetchReq(builder);
    fbs.Base.startBase(builder);
    fbs.Base.addMsg(builder, msg);
    fbs.Base.addMsgType(builder, fbs.Any.FetchReq);
    builder.finish(fbs.Base.endBase(builder));
    const resBuf = libdeno.send(builder.asUint8Array());
    assert(resBuf == null);

    //console.log("FetchReq sent", builder);
  }
}

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) => {
    response.onHeader = (response: FetchResponse) => {
      log("onHeader");
      resolve(response);
    };
    response.onError = (error: Error) => {
      log("onError", error);
      reject(error);
    };
    fetchReq.start();
  });
}