File size: 1,058 Bytes
87fc763
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { Urbit } from "@urbit/http-api";

let patched = false;

export function ensureUrbitConnectPatched() {
  if (patched) {
    return;
  }
  patched = true;
  Urbit.prototype.connect = async function patchedConnect() {
    const resp = await fetch(`${this.url}/~/login`, {
      method: "POST",
      body: `password=${this.code}`,
      credentials: "include",
    });

    if (resp.status >= 400) {
      throw new Error(`Login failed with status ${resp.status}`);
    }

    const cookie = resp.headers.get("set-cookie");
    if (cookie) {
      const match = /urbauth-~([\w-]+)/.exec(cookie);
      if (match) {
        if (!(this as unknown as { ship?: string | null }).ship) {
          (this as unknown as { ship?: string | null }).ship = match[1];
        }
        (this as unknown as { nodeId?: string }).nodeId = match[1];
      }
      (this as unknown as { cookie?: string }).cookie = cookie;
    }

    await (this as typeof Urbit.prototype).getShipName();
    await (this as typeof Urbit.prototype).getOurName();
  };
}

export { Urbit };