sprucehttp_sjs 1.0.2 → 1.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +140 -0
  2. package/index.js +114 -4
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -8,6 +8,17 @@ A module for responding to requests within SpruceHTTP in an SJS file.
8
8
  - [writeStatusLine(statusCode, reasonPhrase)](#writestatuslinestatuscode-reasonphrase)
9
9
  - [writeHeader(name, value)](#writeheadername-value)
10
10
  - [writeData(data)](#writedatadata)
11
+ - [writeDataAsync(data)](#writedataasyncdata)
12
+ - [siteConfig()](#siteconfig)
13
+ - [requestIP()](#requestip)
14
+ - [method()](#method)
15
+ - [path()](#path)
16
+ - [query()](#query)
17
+ - [queryValue(key)](#queryvaluekey)
18
+ - [httpVersion()](#httpversion)
19
+ - [headers()](#headers)
20
+ - [headerValue(name)](#headervaluename)
21
+ - [body()](#body)
11
22
 
12
23
  ## Documentation:
13
24
  Importing the module:
@@ -65,4 +76,133 @@ Writes binary data (a picture, in this case) to the body.
65
76
  ```js
66
77
  const fs = require('fs');
67
78
  sjs.writeData(fs.readFileSync("image.png", 'binary'));
79
+ ```
80
+
81
+ ### ``writeDataAsync(data)``:
82
+ - ``data:`` string | Buffer: The data to send.
83
+
84
+ Writes data (the body) to the response asynchronously. When not in stream mode, the Content-Length header is automatically updated.
85
+
86
+ Writes text data to the body.
87
+ ```js
88
+ await sjs.writeDataAsync("Hello, world!");
89
+ ```
90
+
91
+ Writes binary data (a picture, in this case) to the body.
92
+ ```js
93
+ const fs = require('fs');
94
+ await sjs.writeDataAsync(fs.readFileSync("image.png", 'binary'));
95
+ ```
96
+
97
+ ### ``siteConfig()``:
98
+ Returns the site-specific configuration set in your config file.
99
+
100
+ ```js
101
+ console.log(sjs.siteConfig());
102
+ /*
103
+ {
104
+ "type": "local",
105
+ "location": "./",
106
+ "upgradeInsecure": true,
107
+ "directoryListing": true,
108
+ "sjs": true,
109
+ "headers": {
110
+ "Access-Control-Allow-Origin": "*"
111
+ },
112
+ "ssl": {
113
+ "key": "./ssl/key.pem",
114
+ "cert": "./ssl/cert.pem",
115
+ "minVersion": "TLSv1.2",
116
+ "maxVersion": "TLSv1.3"
117
+ }
118
+ }
119
+ */
120
+ ```
121
+
122
+ ### ``requestIP()``:
123
+ Returns the requestor's IP address.
124
+
125
+ ```js
126
+ console.log(sjs.requestIP());
127
+ // ::ffff:127.0.0.1
128
+ ```
129
+
130
+ ### ``method()``:
131
+ Returns the request method.
132
+
133
+ ```js
134
+ console.log(sjs.method());
135
+ // get
136
+ ```
137
+
138
+ ### ``path()``:
139
+ Returns the request path.
140
+
141
+ ```js
142
+ console.log(sjs.path());
143
+ // /index.sjs
144
+ ```
145
+
146
+ ### ``query()``:
147
+ Returns the request query.
148
+
149
+ ```js
150
+ console.log(sjs.query());
151
+ /*
152
+ {
153
+ "name": "John",
154
+ "age": "42"
155
+ }
156
+ */
157
+ ```
158
+
159
+ ### ``queryValue(key)``:
160
+ - ``key:`` string: The key of the query to get.
161
+
162
+ Returns the value of a query parameter.
163
+
164
+ ```js
165
+ console.log(sjs.queryValue("name"));
166
+ // John
167
+ ```
168
+
169
+ ### ``httpVersion()``:
170
+ Returns the HTTP version of the request.
171
+
172
+ ```js
173
+ console.log(sjs.httpVersion());
174
+ // http/1.1
175
+ ```
176
+
177
+ ### ``headers()``:
178
+ Returns the request headers.
179
+
180
+ ```js
181
+ console.log(sjs.headers());
182
+ /*
183
+ {
184
+ "connection": "keep-alive",
185
+ "host": "localhost:8080",
186
+ "upgrade-insecure-requests": "1",
187
+ "user-agent": "Bruh/1.0 (Macintosh; PPC Mac OS X 7_0_1) AppleBruhKit 1.3 (XHTML, like IE) Netscape/69.420 Moment/360 NoScope/1.0"
188
+ }
189
+ */
190
+ ```
191
+
192
+ ### ``headerValue(name)``:
193
+ - ``name:`` string: The name of the header to get.
194
+
195
+ Returns the value of a header.
196
+
197
+ ```js
198
+ console.log(sjs.headerValue("user-agent"));
199
+ // Bruh/1.0 (Macintosh; PPC Mac OS X 7_0_1) AppleBruhKit 1.3 (XHTML, like IE) Netscape/69.420 Moment/360 NoScope/1.0
200
+ ```
201
+
202
+ ### ``body()``:
203
+ Returns the request body as a buffer.
204
+
205
+ ```js
206
+ console.log(sjs.body().toString("utf8"));
207
+ // Hello, world!
68
208
  ```
package/index.js CHANGED
@@ -1,3 +1,5 @@
1
+ const fs = require("fs");
2
+
1
3
  module.exports = {
2
4
  /**
3
5
  * Sets the response into stream mode.
@@ -30,7 +32,7 @@ module.exports = {
30
32
  */
31
33
  writeHeader: function(name, value) {
32
34
  process.send({
33
- type: 'status',
35
+ type: 'header',
34
36
  name,
35
37
  value
36
38
  });
@@ -43,9 +45,117 @@ module.exports = {
43
45
  * @param {string | Buffer} data The data to send.
44
46
  */
45
47
  writeData: function(data) {
46
- process.send({
47
- type: 'data',
48
- data: Buffer.from(data).toString('base64')
48
+ (async () => {await this.writeDataAsync(data);})();
49
+ },
50
+
51
+ /**
52
+ * Writes data (the body) to the response asynchronously.
53
+ *
54
+ * When not in stream mode, the Content-Length header is automatically updated.
55
+ * @param {string | Buffer} data The data to send.
56
+ */
57
+ writeDataAsync: function(data) {
58
+ return new Promise(function(resolve) {
59
+ process.send({
60
+ type: 'data',
61
+ data: Buffer.from(data)
62
+ }, resolve);
49
63
  });
64
+ },
65
+
66
+ /**
67
+ * Returns the site-specific configuration.
68
+ *
69
+ * @returns {{[key: string]: string}} The site-specific configuration.
70
+ */
71
+ siteConfig: function() {
72
+ return JSON.parse(process.env.siteConfig);
73
+ },
74
+
75
+ /**
76
+ * Returns the requestor's IP address.
77
+ *
78
+ * @returns {string} The requestor's IP address.
79
+ */
80
+ requestIP: function() {
81
+ return process.env.reqIP;
82
+ },
83
+
84
+ /**
85
+ * Returns the request method.
86
+ *
87
+ * @returns {string} The request method.
88
+ */
89
+ method: function() {
90
+ return process.env.reqMethod;
91
+ },
92
+
93
+ /**
94
+ * Returns the full path of the request.
95
+ *
96
+ * @returns {string} The full request path.
97
+ */
98
+ path: function() {
99
+ return process.env.reqPath;
100
+ },
101
+
102
+ /**
103
+ * Returns the parsed query string of the request.
104
+ *
105
+ * @returns {{[key: string]: string}} The parsed query string of the request.
106
+ */
107
+ query: function() {
108
+ return JSON.parse(process.env.reqQuery || "{}");
109
+ },
110
+
111
+ /**
112
+ * Returns the value of a query parameter.
113
+ *
114
+ * @param {string} key The key of the query to get.
115
+ * @returns {string} The value of the query parameter.
116
+ */
117
+ queryValue: function(key) {
118
+ return this.query()[key];
119
+ },
120
+
121
+ /**
122
+ * Returns the HTTP version of the request.
123
+ *
124
+ * @returns {string} The HTTP version of the request.
125
+ */
126
+ httpVersion: function() {
127
+ return process.env.reqHttpVersion;
128
+ },
129
+
130
+ /**
131
+ * Returns the headers of the request.
132
+ *
133
+ * @returns {{[key: string]: string}} The headers of the request.
134
+ */
135
+ headers: function() {
136
+ return JSON.parse(process.env.reqHeaders || "{}");
137
+ },
138
+
139
+ /**
140
+ * Returns a specific header of the request.
141
+ *
142
+ * @param {string} name The name of the header to get.
143
+ * @returns {string} The value of the header.
144
+ */
145
+ headerValue: function(name) {
146
+ return this.headers()[name];
147
+ },
148
+
149
+ /**
150
+ * Returns the body of the request.
151
+ *
152
+ * @returns {Buffer} The body of the request.
153
+ */
154
+ body: function() {
155
+ let retVal = undefined;
156
+ if (process.env.reqBody) {
157
+ retVal = fs.readFileSync(process.env.reqBody);
158
+ }
159
+ return retVal;
50
160
  }
51
161
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sprucehttp_sjs",
3
- "version": "1.0.2",
3
+ "version": "1.0.5",
4
4
  "description": "A module for responding to requests within SpruceHTTP in an SJS file",
5
5
  "main": "index.js",
6
6
  "scripts": {