sprucehttp_sjs 1.0.11 → 2.0.0
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.
- package/README.md +43 -31
- package/index.js +37 -20
- package/package.json +6 -6
package/README.md
CHANGED
|
@@ -10,18 +10,19 @@ A module for responding to requests within SpruceHTTP in an SJS file.
|
|
|
10
10
|
- [writeData(data)](#writedatadata)
|
|
11
11
|
- [writeDataAsync(data)](#writedataasyncdata)
|
|
12
12
|
- [clearResponse()](#clearresponse)
|
|
13
|
-
- [siteConfig
|
|
14
|
-
- [requestIP
|
|
15
|
-
- [method
|
|
16
|
-
- [path
|
|
17
|
-
- [pathInfo
|
|
18
|
-
- [query
|
|
13
|
+
- [siteConfig](#siteconfig)
|
|
14
|
+
- [requestIP](#requestip)
|
|
15
|
+
- [method](#method)
|
|
16
|
+
- [path](#path)
|
|
17
|
+
- [pathInfo](#pathinfo)
|
|
18
|
+
- [query](#query)
|
|
19
19
|
- [queryValue(key)](#queryvaluekey)
|
|
20
|
-
- [httpVersion
|
|
21
|
-
- [headers
|
|
20
|
+
- [httpVersion](#httpversion)
|
|
21
|
+
- [headers](#headers)
|
|
22
22
|
- [headerValue(name)](#headervaluename)
|
|
23
|
-
- [body
|
|
24
|
-
- [bodyStream()](#
|
|
23
|
+
- [body](#body)
|
|
24
|
+
- [bodyStream([options])](#bodystreamoptions)
|
|
25
|
+
- [mTlsPeerCertificate](#mtlspeercertificate)
|
|
25
26
|
|
|
26
27
|
## Documentation:
|
|
27
28
|
Importing the module:
|
|
@@ -40,7 +41,7 @@ sjs.streamMode(); // Set the response to stream mode.
|
|
|
40
41
|
|
|
41
42
|
### ``writeStatusLine(statusCode[, reasonPhrase])``:
|
|
42
43
|
- ``statusLine:`` number: The HTTP status code to send.
|
|
43
|
-
- ``reasonPhrase:`` string: The reason phrase to send.
|
|
44
|
+
- ``reasonPhrase:`` string: Optional. The reason phrase to send.
|
|
44
45
|
|
|
45
46
|
Writes the status line to the response.
|
|
46
47
|
|
|
@@ -70,6 +71,8 @@ sjs.writeHeader("Content-Type", "text/html");
|
|
|
70
71
|
|
|
71
72
|
Writes data (the body) to the response. When not in stream mode, the Content-Length header is automatically updated.
|
|
72
73
|
|
|
74
|
+
This function is deprecated. Use ``writeDataAsync`` instead, as data cannot be reliably sent synchronously.
|
|
75
|
+
|
|
73
76
|
Writes text data to the body.
|
|
74
77
|
```js
|
|
75
78
|
sjs.writeData("Hello, world!");
|
|
@@ -115,11 +118,11 @@ sjs.writeHeader("Content-Type", "text/html");
|
|
|
115
118
|
sjs.writeData("<h1>I'm <i>not</i> a teapot</h1>");
|
|
116
119
|
```
|
|
117
120
|
|
|
118
|
-
### ``siteConfig
|
|
121
|
+
### ``siteConfig``:
|
|
119
122
|
Returns the site-specific configuration set in your config file.
|
|
120
123
|
|
|
121
124
|
```js
|
|
122
|
-
console.log(sjs.siteConfig
|
|
125
|
+
console.log(sjs.siteConfig);
|
|
123
126
|
/*
|
|
124
127
|
{
|
|
125
128
|
"type": "local",
|
|
@@ -140,46 +143,46 @@ console.log(sjs.siteConfig());
|
|
|
140
143
|
*/
|
|
141
144
|
```
|
|
142
145
|
|
|
143
|
-
### ``requestIP
|
|
146
|
+
### ``requestIP``:
|
|
144
147
|
Returns the requestor's IP address.
|
|
145
148
|
|
|
146
149
|
```js
|
|
147
|
-
console.log(sjs.requestIP
|
|
150
|
+
console.log(sjs.requestIP);
|
|
148
151
|
// ::ffff:127.0.0.1
|
|
149
152
|
```
|
|
150
153
|
|
|
151
|
-
### ``method
|
|
154
|
+
### ``method``:
|
|
152
155
|
Returns the request method.
|
|
153
156
|
|
|
154
157
|
```js
|
|
155
|
-
console.log(sjs.method
|
|
158
|
+
console.log(sjs.method);
|
|
156
159
|
// get
|
|
157
160
|
```
|
|
158
161
|
|
|
159
|
-
### ``path
|
|
162
|
+
### ``path``:
|
|
160
163
|
Returns the request path.
|
|
161
164
|
|
|
162
165
|
```js
|
|
163
|
-
console.log(sjs.path
|
|
166
|
+
console.log(sjs.path);
|
|
164
167
|
// /index.sjs
|
|
165
168
|
```
|
|
166
169
|
|
|
167
|
-
### ``pathInfo
|
|
170
|
+
### ``pathInfo``:
|
|
168
171
|
Returns the request's info path.
|
|
169
172
|
|
|
170
173
|
```js
|
|
171
174
|
// Using path /index.sjs/info
|
|
172
|
-
console.log(sjs.path
|
|
173
|
-
console.log(sjs.pathInfo
|
|
175
|
+
console.log(sjs.path);
|
|
176
|
+
console.log(sjs.pathInfo);
|
|
174
177
|
// /index.sjs
|
|
175
178
|
// /info
|
|
176
179
|
```
|
|
177
180
|
|
|
178
|
-
### ``query
|
|
181
|
+
### ``query``:
|
|
179
182
|
Returns the request query.
|
|
180
183
|
|
|
181
184
|
```js
|
|
182
|
-
console.log(sjs.query
|
|
185
|
+
console.log(sjs.query);
|
|
183
186
|
/*
|
|
184
187
|
{
|
|
185
188
|
"name": "John",
|
|
@@ -198,19 +201,19 @@ console.log(sjs.queryValue("name"));
|
|
|
198
201
|
// John
|
|
199
202
|
```
|
|
200
203
|
|
|
201
|
-
### ``httpVersion
|
|
204
|
+
### ``httpVersion``:
|
|
202
205
|
Returns the HTTP version of the request.
|
|
203
206
|
|
|
204
207
|
```js
|
|
205
|
-
console.log(sjs.httpVersion
|
|
208
|
+
console.log(sjs.httpVersion);
|
|
206
209
|
// http/1.1
|
|
207
210
|
```
|
|
208
211
|
|
|
209
|
-
### ``headers
|
|
212
|
+
### ``headers``:
|
|
210
213
|
Returns the request headers.
|
|
211
214
|
|
|
212
215
|
```js
|
|
213
|
-
console.log(sjs.headers
|
|
216
|
+
console.log(sjs.headers);
|
|
214
217
|
/*
|
|
215
218
|
{
|
|
216
219
|
"connection": "keep-alive",
|
|
@@ -231,18 +234,27 @@ console.log(sjs.headerValue("user-agent"));
|
|
|
231
234
|
// 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
|
|
232
235
|
```
|
|
233
236
|
|
|
234
|
-
### ``body
|
|
237
|
+
### ``body``:
|
|
235
238
|
Returns the request body as a buffer.
|
|
236
239
|
|
|
237
240
|
```js
|
|
238
|
-
console.log(sjs.body
|
|
241
|
+
console.log(sjs.body.toString("utf8"));
|
|
239
242
|
// Hello, world!
|
|
240
243
|
```
|
|
241
244
|
|
|
242
|
-
### ``bodyStream()``:
|
|
245
|
+
### ``bodyStream([options])``:
|
|
246
|
+
- ``options:`` BufferEncoding | ReadStreamOptions: Optional. The options to pass to fs.createReadStream.
|
|
243
247
|
Returns the request body as a ReadStream.
|
|
244
248
|
|
|
245
249
|
```js
|
|
246
250
|
console.log(sjs.bodyStream().read(13).toString("utf8"));
|
|
247
251
|
// Hello, world!
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
### ``mTlsPeerCertificate``:
|
|
255
|
+
Returns details about the mTLS peer's certificate, if present.
|
|
256
|
+
|
|
257
|
+
```js
|
|
258
|
+
console.log(sjs.mTlsPeerCertificate.subject.CN);
|
|
259
|
+
// sprucehttp.com
|
|
248
260
|
```
|
package/index.js
CHANGED
|
@@ -8,7 +8,7 @@ module.exports = {
|
|
|
8
8
|
*
|
|
9
9
|
* Stream mode means that responses are not buffered by the webserver.
|
|
10
10
|
*/
|
|
11
|
-
streamMode
|
|
11
|
+
streamMode() {
|
|
12
12
|
process.send({
|
|
13
13
|
type: "streamMode"
|
|
14
14
|
});
|
|
@@ -19,7 +19,7 @@ module.exports = {
|
|
|
19
19
|
* @param {number} statusCode The HTTP status code to send.
|
|
20
20
|
* @param {string} [reasonPhrase] The reason phrase to send.
|
|
21
21
|
*/
|
|
22
|
-
writeStatusLine
|
|
22
|
+
writeStatusLine(statusCode, reasonPhrase) {
|
|
23
23
|
process.send({
|
|
24
24
|
type: "status",
|
|
25
25
|
statusCode,
|
|
@@ -32,7 +32,7 @@ module.exports = {
|
|
|
32
32
|
* @param {string} name The name of the header to send.
|
|
33
33
|
* @param {string} value The value of the header to send.
|
|
34
34
|
*/
|
|
35
|
-
writeHeader
|
|
35
|
+
writeHeader(name, value) {
|
|
36
36
|
process.send({
|
|
37
37
|
type: "header",
|
|
38
38
|
name,
|
|
@@ -45,8 +45,9 @@ module.exports = {
|
|
|
45
45
|
*
|
|
46
46
|
* When not in stream mode, the Content-Length header is automatically updated.
|
|
47
47
|
* @param {string | Buffer} data The data to send.
|
|
48
|
+
* @deprecated Use writeDataAsync instead. Data cannot be reliably sent synchronously.
|
|
48
49
|
*/
|
|
49
|
-
writeData
|
|
50
|
+
writeData(data) {
|
|
50
51
|
(async () => { await this.writeDataAsync(data); })();
|
|
51
52
|
},
|
|
52
53
|
|
|
@@ -56,7 +57,8 @@ module.exports = {
|
|
|
56
57
|
* When not in stream mode, the Content-Length header is automatically updated.
|
|
57
58
|
* @param {string | Buffer} data The data to send.
|
|
58
59
|
*/
|
|
59
|
-
writeDataAsync
|
|
60
|
+
writeDataAsync(data) {
|
|
61
|
+
// eslint-disable-next-line no-async-promise-executor
|
|
60
62
|
return new Promise(async function (resolve) {
|
|
61
63
|
// Send the data in 1 MiB chunks
|
|
62
64
|
const chunkSize = 1024 * 1024;
|
|
@@ -85,7 +87,7 @@ module.exports = {
|
|
|
85
87
|
*
|
|
86
88
|
* This function does not work in stream mode.
|
|
87
89
|
*/
|
|
88
|
-
clearResponse
|
|
90
|
+
clearResponse() {
|
|
89
91
|
process.send({
|
|
90
92
|
type: "clear"
|
|
91
93
|
});
|
|
@@ -96,7 +98,7 @@ module.exports = {
|
|
|
96
98
|
*
|
|
97
99
|
* @returns {{[key: string]: string}} The site-specific configuration.
|
|
98
100
|
*/
|
|
99
|
-
siteConfig
|
|
101
|
+
get siteConfig() {
|
|
100
102
|
return JSON.parse(process.env.siteConfig);
|
|
101
103
|
},
|
|
102
104
|
|
|
@@ -105,7 +107,7 @@ module.exports = {
|
|
|
105
107
|
*
|
|
106
108
|
* @returns {string} The requestor's IP address.
|
|
107
109
|
*/
|
|
108
|
-
requestIP
|
|
110
|
+
get requestIP() {
|
|
109
111
|
return process.env.reqIP;
|
|
110
112
|
},
|
|
111
113
|
|
|
@@ -114,7 +116,7 @@ module.exports = {
|
|
|
114
116
|
*
|
|
115
117
|
* @returns {string} The request method.
|
|
116
118
|
*/
|
|
117
|
-
method
|
|
119
|
+
get method() {
|
|
118
120
|
return process.env.reqMethod.toLowerCase();
|
|
119
121
|
},
|
|
120
122
|
|
|
@@ -123,7 +125,7 @@ module.exports = {
|
|
|
123
125
|
*
|
|
124
126
|
* @returns {string} The full request path.
|
|
125
127
|
*/
|
|
126
|
-
path
|
|
128
|
+
get path() {
|
|
127
129
|
return process.env.reqPath;
|
|
128
130
|
},
|
|
129
131
|
|
|
@@ -132,7 +134,7 @@ module.exports = {
|
|
|
132
134
|
*
|
|
133
135
|
* @returns {string} The request's info path.
|
|
134
136
|
*/
|
|
135
|
-
pathInfo
|
|
137
|
+
get pathInfo() {
|
|
136
138
|
return process.env.reqPathInfo;
|
|
137
139
|
},
|
|
138
140
|
|
|
@@ -141,7 +143,7 @@ module.exports = {
|
|
|
141
143
|
*
|
|
142
144
|
* @returns {{[key: string]: string}} The parsed query string of the request.
|
|
143
145
|
*/
|
|
144
|
-
query
|
|
146
|
+
get query() {
|
|
145
147
|
return JSON.parse(process.env.reqQuery || "{}");
|
|
146
148
|
},
|
|
147
149
|
|
|
@@ -151,8 +153,8 @@ module.exports = {
|
|
|
151
153
|
* @param {string} key The key of the query to get.
|
|
152
154
|
* @returns {string} The value of the query parameter.
|
|
153
155
|
*/
|
|
154
|
-
queryValue
|
|
155
|
-
return this.query
|
|
156
|
+
queryValue(key) {
|
|
157
|
+
return this.query[key];
|
|
156
158
|
},
|
|
157
159
|
|
|
158
160
|
/**
|
|
@@ -160,7 +162,7 @@ module.exports = {
|
|
|
160
162
|
*
|
|
161
163
|
* @returns {string} The HTTP version of the request.
|
|
162
164
|
*/
|
|
163
|
-
httpVersion
|
|
165
|
+
get httpVersion() {
|
|
164
166
|
return process.env.reqHttpVersion;
|
|
165
167
|
},
|
|
166
168
|
|
|
@@ -169,7 +171,7 @@ module.exports = {
|
|
|
169
171
|
*
|
|
170
172
|
* @returns {{[key: string]: string}} The headers of the request.
|
|
171
173
|
*/
|
|
172
|
-
headers
|
|
174
|
+
get headers() {
|
|
173
175
|
return JSON.parse(process.env.reqHeaders || "{}");
|
|
174
176
|
},
|
|
175
177
|
|
|
@@ -179,8 +181,8 @@ module.exports = {
|
|
|
179
181
|
* @param {string} name The name of the header to get.
|
|
180
182
|
* @returns {string} The value of the header.
|
|
181
183
|
*/
|
|
182
|
-
headerValue
|
|
183
|
-
return this.headers
|
|
184
|
+
headerValue(name) {
|
|
185
|
+
return this.headers[name];
|
|
184
186
|
},
|
|
185
187
|
|
|
186
188
|
/**
|
|
@@ -188,7 +190,7 @@ module.exports = {
|
|
|
188
190
|
*
|
|
189
191
|
* @returns {Buffer} The body of the request.
|
|
190
192
|
*/
|
|
191
|
-
body
|
|
193
|
+
get body() {
|
|
192
194
|
let retVal = undefined;
|
|
193
195
|
if (process.env.reqBody) {
|
|
194
196
|
retVal = fs.readFileSync(process.env.reqBody);
|
|
@@ -202,11 +204,26 @@ module.exports = {
|
|
|
202
204
|
* @param {BufferEncoding | ReadStreamOptions} [options] The options to pass to fs.createReadStream.
|
|
203
205
|
* @returns {fs.ReadStream} A ReadStream of the body of the request.
|
|
204
206
|
*/
|
|
205
|
-
bodyStream
|
|
207
|
+
bodyStream(options) {
|
|
206
208
|
let retVal = undefined;
|
|
207
209
|
if (process.env.reqBody) {
|
|
208
210
|
retVal = fs.createReadStream(process.env.reqBody, options);
|
|
209
211
|
}
|
|
210
212
|
return retVal;
|
|
213
|
+
},
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Returns details about the mTLS peer's certificate, if present.
|
|
217
|
+
*
|
|
218
|
+
* @returns {PeerCertificate}
|
|
219
|
+
*/
|
|
220
|
+
get mTlsPeerCertificate() {
|
|
221
|
+
let retVal = undefined;
|
|
222
|
+
if (process.env.mTlsPeerCertificate) {
|
|
223
|
+
retVal = JSON.parse(process.env.mTlsPeerCertificate);
|
|
224
|
+
if (retVal.raw) retVal.raw = Buffer.from(retVal.raw, "base64");
|
|
225
|
+
if (retVal.pubkey) retVal.pubkey = Buffer.from(retVal.pubkey, "base64");
|
|
226
|
+
}
|
|
227
|
+
return retVal;
|
|
211
228
|
}
|
|
212
229
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "sprucehttp_sjs",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "A module for responding to requests within SpruceHTTP in an SJS file",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -13,15 +13,15 @@
|
|
|
13
13
|
"spruce",
|
|
14
14
|
"sprucehttp"
|
|
15
15
|
],
|
|
16
|
-
"homepage": "https://
|
|
16
|
+
"homepage": "https://sprucehttp.com/",
|
|
17
17
|
"bugs": {
|
|
18
|
-
"url": "https://
|
|
19
|
-
"email": "herronjo@
|
|
18
|
+
"url": "https://sprucehttp.com/reportbug/",
|
|
19
|
+
"email": "herronjo@sprucehttp.com"
|
|
20
20
|
},
|
|
21
21
|
"author": {
|
|
22
22
|
"name": "Joshua Herron",
|
|
23
|
-
"email": "herronjo@
|
|
24
|
-
"url": "https://
|
|
23
|
+
"email": "herronjo@sprucehttp.com",
|
|
24
|
+
"url": "https://sprucehttp.com"
|
|
25
25
|
},
|
|
26
26
|
"license": "Copyright (c) STiBaRC LLC. All rights reserved."
|
|
27
27
|
}
|