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