-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgopher.ts
91 lines (75 loc) · 2.37 KB
/
gopher.ts
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
import {GopherClient, GopherProtocol, TYPE_MENU} from "https://deno.land/x/gopher/mod.ts";
/** A response from the GopherServer. **/
export class GopherResponse {
constructor(
readonly bytes:Uint8Array,
private readonly startTimeMillis:number,
private readonly endTimeMillis:number) {
}
public get duration() : number {
return this.endTimeMillis - this.startTimeMillis;
}
}
/**
* This class exists to handle interactions with the Gopher client library,
* and provide responses to the gui in an appropriate format. It is largely
* glue code.
*/
export class Gopher {
private gopherClient:GopherClient;
constructor() {
this.gopherClient = new GopherClient({
protocolVersion: GopherProtocol.RFC1436,
tls: false,
});
}
/** Load a Gopher menu and return results. */
async loadMenu(hostname:string, port = 70, selector = ''): Promise<GopherResponse> {
console.log(`Downloading menu from gopher: ${hostname} ${port} ${selector}`);
let menu;
const start = Date.now();
try {
menu = await this.gopherClient.downloadMenu({
hostname,
selector,
port,
});
} catch (error) {
console.warn(error);
throw error;
}
return new GopherResponse(new TextEncoder().encode(JSON.stringify(menu)), start, Date.now());
}
/**
* Load a Gopher item (e.g. image etc) and return results. If it is an image
* type then return the result as as a base64 encoded string for use in data
* URL.
*/
async loadItem(hostname:string, port = 70, selector = ''): Promise<GopherResponse> {
console.log(`Downloading item from gopher: ${hostname} ${port} ${selector }`);
const start = Date.now();
const item = await this.gopherClient.downloadItem({
hostname,
selector,
port,
});
return new GopherResponse(item.body, start, Date.now());
}
/** Query a gopher search server. */
async search(hostname:string, port = 70, selector = '', query = ''): Promise<GopherResponse> {
console.log(`Querying gopher: ${hostname} ${port} ${selector} ${query}`);
let menu;
const start = Date.now();
try {
menu = await this.gopherClient.search({
hostname,
selector,
port,
query,
});
} catch (error) {
throw error;
}
return new GopherResponse(new TextEncoder().encode(JSON.stringify(menu)), start, Date.now());
}
}