sprucehttp_sjs 1.0.8 → 1.0.10

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 +19 -0
  2. package/index.js +40 -26
  3. package/package.json +5 -2
package/README.md CHANGED
@@ -9,6 +9,7 @@ A module for responding to requests within SpruceHTTP in an SJS file.
9
9
  - [writeHeader(name, value)](#writeheadername-value)
10
10
  - [writeData(data)](#writedatadata)
11
11
  - [writeDataAsync(data)](#writedataasyncdata)
12
+ - [clearResponse()](#clearresponse)
12
13
  - [siteConfig()](#siteconfig)
13
14
  - [requestIP()](#requestip)
14
15
  - [method()](#method)
@@ -96,6 +97,24 @@ const fs = require('fs');
96
97
  await sjs.writeDataAsync(fs.readFileSync("image.png", 'binary'));
97
98
  ```
98
99
 
100
+ ### ``clearResponse()``:
101
+ Clears the currently written response and resets the status line, headers, and body.
102
+ This is useful when you want to send a different response than the one you have already written.
103
+
104
+ This function does not work in stream mode.
105
+
106
+ ```js
107
+ sjs.writeStatusLine(418, "I'm a teapot");
108
+ sjs.writeHeader("Content-Type", "text/html");
109
+ sjs.writeData("<h1>I'm a teapot</h1>");
110
+ // Be serious
111
+ sjs.clearResponse();
112
+
113
+ sjs.writeStatusLine(200);
114
+ sjs.writeHeader("Content-Type", "text/html");
115
+ sjs.writeData("<h1>I'm <i>not</i> a teapot</h1>");
116
+ ```
117
+
99
118
  ### ``siteConfig()``:
100
119
  Returns the site-specific configuration set in your config file.
101
120
 
package/index.js CHANGED
@@ -1,3 +1,5 @@
1
+ // This is still CommonJS to support both the old and new versions of the module system.
2
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
1
3
  const fs = require("fs");
2
4
 
3
5
  module.exports = {
@@ -6,20 +8,20 @@ module.exports = {
6
8
  *
7
9
  * Stream mode means that responses are not buffered by the webserver.
8
10
  */
9
- streamMode: function() {
11
+ streamMode: function () {
10
12
  process.send({
11
- type: 'streamMode'
13
+ type: "streamMode"
12
14
  });
13
15
  },
14
-
16
+
15
17
  /**
16
18
  * Writes the status line to the response.
17
19
  * @param {number} statusCode The HTTP status code to send.
18
20
  * @param {string} [reasonPhrase] The reason phrase to send.
19
21
  */
20
- writeStatusLine: function(statusCode, reasonPhrase) {
22
+ writeStatusLine: function (statusCode, reasonPhrase) {
21
23
  process.send({
22
- type: 'status',
24
+ type: "status",
23
25
  statusCode,
24
26
  reasonPhrase
25
27
  });
@@ -30,9 +32,9 @@ module.exports = {
30
32
  * @param {string} name The name of the header to send.
31
33
  * @param {string} value The value of the header to send.
32
34
  */
33
- writeHeader: function(name, value) {
35
+ writeHeader: function (name, value) {
34
36
  process.send({
35
- type: 'header',
37
+ type: "header",
36
38
  name,
37
39
  value
38
40
  });
@@ -44,8 +46,8 @@ module.exports = {
44
46
  * When not in stream mode, the Content-Length header is automatically updated.
45
47
  * @param {string | Buffer} data The data to send.
46
48
  */
47
- writeData: function(data) {
48
- (async () => {await this.writeDataAsync(data);})();
49
+ writeData: function (data) {
50
+ (async () => { await this.writeDataAsync(data); })();
49
51
  },
50
52
 
51
53
  /**
@@ -54,21 +56,33 @@ module.exports = {
54
56
  * When not in stream mode, the Content-Length header is automatically updated.
55
57
  * @param {string | Buffer} data The data to send.
56
58
  */
57
- writeDataAsync: function(data) {
58
- return new Promise(function(resolve) {
59
+ writeDataAsync: function (data) {
60
+ return new Promise(function (resolve) {
59
61
  process.send({
60
- type: 'data',
62
+ type: "data",
61
63
  data: Buffer.from(data)
62
64
  }, resolve);
63
65
  });
64
66
  },
65
67
 
68
+ /**
69
+ * Clears the currently written response and resets the status line, headers, and body.
70
+ * This is useful when you want to send a different response than the one you have already written.
71
+ *
72
+ * This function does not work in stream mode.
73
+ */
74
+ clearResponse: function () {
75
+ process.send({
76
+ type: "clear"
77
+ });
78
+ },
79
+
66
80
  /**
67
81
  * Returns the site-specific configuration.
68
82
  *
69
83
  * @returns {{[key: string]: string}} The site-specific configuration.
70
84
  */
71
- siteConfig: function() {
85
+ siteConfig: function () {
72
86
  return JSON.parse(process.env.siteConfig);
73
87
  },
74
88
 
@@ -77,7 +91,7 @@ module.exports = {
77
91
  *
78
92
  * @returns {string} The requestor's IP address.
79
93
  */
80
- requestIP: function() {
94
+ requestIP: function () {
81
95
  return process.env.reqIP;
82
96
  },
83
97
 
@@ -86,8 +100,8 @@ module.exports = {
86
100
  *
87
101
  * @returns {string} The request method.
88
102
  */
89
- method: function() {
90
- return process.env.reqMethod;
103
+ method: function () {
104
+ return process.env.reqMethod.toLowerCase();
91
105
  },
92
106
 
93
107
  /**
@@ -95,7 +109,7 @@ module.exports = {
95
109
  *
96
110
  * @returns {string} The full request path.
97
111
  */
98
- path: function() {
112
+ path: function () {
99
113
  return process.env.reqPath;
100
114
  },
101
115
 
@@ -104,7 +118,7 @@ module.exports = {
104
118
  *
105
119
  * @returns {string} The request's info path.
106
120
  */
107
- pathInfo: function() {
121
+ pathInfo: function () {
108
122
  return process.env.reqPathInfo;
109
123
  },
110
124
 
@@ -113,7 +127,7 @@ module.exports = {
113
127
  *
114
128
  * @returns {{[key: string]: string}} The parsed query string of the request.
115
129
  */
116
- query: function() {
130
+ query: function () {
117
131
  return JSON.parse(process.env.reqQuery || "{}");
118
132
  },
119
133
 
@@ -123,7 +137,7 @@ module.exports = {
123
137
  * @param {string} key The key of the query to get.
124
138
  * @returns {string} The value of the query parameter.
125
139
  */
126
- queryValue: function(key) {
140
+ queryValue: function (key) {
127
141
  return this.query()[key];
128
142
  },
129
143
 
@@ -132,7 +146,7 @@ module.exports = {
132
146
  *
133
147
  * @returns {string} The HTTP version of the request.
134
148
  */
135
- httpVersion: function() {
149
+ httpVersion: function () {
136
150
  return process.env.reqHttpVersion;
137
151
  },
138
152
 
@@ -141,7 +155,7 @@ module.exports = {
141
155
  *
142
156
  * @returns {{[key: string]: string}} The headers of the request.
143
157
  */
144
- headers: function() {
158
+ headers: function () {
145
159
  return JSON.parse(process.env.reqHeaders || "{}");
146
160
  },
147
161
 
@@ -151,7 +165,7 @@ module.exports = {
151
165
  * @param {string} name The name of the header to get.
152
166
  * @returns {string} The value of the header.
153
167
  */
154
- headerValue: function(name) {
168
+ headerValue: function (name) {
155
169
  return this.headers()[name];
156
170
  },
157
171
 
@@ -160,7 +174,7 @@ module.exports = {
160
174
  *
161
175
  * @returns {Buffer} The body of the request.
162
176
  */
163
- body: function() {
177
+ body: function () {
164
178
  let retVal = undefined;
165
179
  if (process.env.reqBody) {
166
180
  retVal = fs.readFileSync(process.env.reqBody);
@@ -174,11 +188,11 @@ module.exports = {
174
188
  * @param {string | Object} [options] The options to pass to fs.createReadStream.
175
189
  * @returns {fs.ReadStream} A ReadStream of the body of the request.
176
190
  */
177
- bodyStream: function(options) {
191
+ bodyStream: function (options) {
178
192
  let retVal = undefined;
179
193
  if (process.env.reqBody) {
180
194
  retVal = fs.createReadStream(process.env.reqBody, options);
181
195
  }
182
196
  return retVal;
183
197
  }
184
- }
198
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sprucehttp_sjs",
3
- "version": "1.0.8",
3
+ "version": "1.0.10",
4
4
  "description": "A module for responding to requests within SpruceHTTP in an SJS file",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -23,5 +23,8 @@
23
23
  "email": "herronjo@stibarc.dev",
24
24
  "url": "https://stibarc.dev"
25
25
  },
26
- "license": "Copyright (c) STiBaRC LLC. All rights reserved."
26
+ "license": "Copyright (c) STiBaRC LLC. All rights reserved.",
27
+ "dependencies": {
28
+ "sprucehttp_sjs": "file:"
29
+ }
27
30
  }