forked from jgkaplan/gemini-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResponse.js
72 lines (60 loc) · 1.82 KB
/
Response.js
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
const truncate = require("truncate-utf8-bytes")
const mime = require('mime');
const fs = require('fs');
const { STATUS } = require('./utils.js');
mime.define({'text/gemini': ['gemini', 'gmi']})
class Response {
#status = null;
#meta = "";
#body = null;
#setMeta(m){
this.#meta = truncate(m, 1024);
}
constructor(status = null, meta = null){
this.#status = status;
this.#setMeta(meta);
}
status(s){
this.#status = s;
return this;
}
getStatus(){
return this.#status;
}
data(d, mimeType='text/plain'){
this.status(STATUS._20);
this.#body = d;
this.#setMeta(mimeType);
return this;
}
//for success, The <META> line is a MIME media type which applies to the response body.
//for redirect, <META> is a new URL for the requested resource. The URL may be absolute or relative.
//for 4* and 5*, The contents of <META> may provide additional information on the failure, and should be displayed to human users.
file(filename){ // might throw error if file doesn't exist
this.#body = fs.readFileSync(filename);
this.status(STATUS._20);
this.#setMeta(mime.getType(filename));
return this;
}
input(prompt, sensitive=false){ //client should re-request same url with input as a query param
this.status(sensitive?STATUS._11:STATUS._10);
this.#setMeta(prompt);
return this;
}
certify(info="Please include a certificate."){ //request certificate from client
this.#setMeta(info);
this.status(STATUS._60);
return this;
}
redirect(url){
this.status(STATUS._30);
this.#setMeta(url);
}
format_header(){
return `${this.#status} ${this.#meta}\r\n`;
}
format_body(){
return `${this.#body}\r\n`;
}
}
module.exports = Response;