sprucehttp_sjs 1.0.4 → 1.0.7

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 +38 -0
  2. package/index.js +43 -4
  3. package/package.json +27 -27
package/README.md CHANGED
@@ -8,16 +8,19 @@ 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)
11
12
  - [siteConfig()](#siteconfig)
12
13
  - [requestIP()](#requestip)
13
14
  - [method()](#method)
14
15
  - [path()](#path)
16
+ - [pathInfo()](#pathinfo)
15
17
  - [query()](#query)
16
18
  - [queryValue(key)](#queryvaluekey)
17
19
  - [httpVersion()](#httpversion)
18
20
  - [headers()](#headers)
19
21
  - [headerValue(name)](#headervaluename)
20
22
  - [body()](#body)
23
+ - [bodyStream()](#bodystream)
21
24
 
22
25
  ## Documentation:
23
26
  Importing the module:
@@ -77,6 +80,22 @@ const fs = require('fs');
77
80
  sjs.writeData(fs.readFileSync("image.png", 'binary'));
78
81
  ```
79
82
 
83
+ ### ``writeDataAsync(data)``:
84
+ - ``data:`` string | Buffer: The data to send.
85
+
86
+ Writes data (the body) to the response asynchronously. When not in stream mode, the Content-Length header is automatically updated.
87
+
88
+ Writes text data to the body.
89
+ ```js
90
+ await sjs.writeDataAsync("Hello, world!");
91
+ ```
92
+
93
+ Writes binary data (a picture, in this case) to the body.
94
+ ```js
95
+ const fs = require('fs');
96
+ await sjs.writeDataAsync(fs.readFileSync("image.png", 'binary'));
97
+ ```
98
+
80
99
  ### ``siteConfig()``:
81
100
  Returns the site-specific configuration set in your config file.
82
101
 
@@ -126,6 +145,17 @@ console.log(sjs.path());
126
145
  // /index.sjs
127
146
  ```
128
147
 
148
+ ### ``pathInfo()``:
149
+ Returns the request's info path.
150
+
151
+ ```js
152
+ // Using path /index.sjs/info
153
+ console.log(sjs.path());
154
+ console.log(sjs.pathInfo());
155
+ // /index.sjs
156
+ // /info
157
+ ```
158
+
129
159
  ### ``query()``:
130
160
  Returns the request query.
131
161
 
@@ -188,4 +218,12 @@ Returns the request body as a buffer.
188
218
  ```js
189
219
  console.log(sjs.body().toString("utf8"));
190
220
  // Hello, world!
221
+ ```
222
+
223
+ ### ``bodyStream()``:
224
+ Returns the request body as a buffer.
225
+
226
+ ```js
227
+ console.log(sjs.bodyStream().read(13).toString("utf8"));
228
+ // Hello, world!
191
229
  ```
package/index.js CHANGED
@@ -45,9 +45,21 @@ module.exports = {
45
45
  * @param {string | Buffer} data The data to send.
46
46
  */
47
47
  writeData: function(data) {
48
- process.send({
49
- type: 'data',
50
- 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);
51
63
  });
52
64
  },
53
65
 
@@ -87,6 +99,15 @@ module.exports = {
87
99
  return process.env.reqPath;
88
100
  },
89
101
 
102
+ /**
103
+ * Returns the request's info path.
104
+ *
105
+ * @returns {string} The request's info path.
106
+ */
107
+ pathInfo: function() {
108
+ return process.env.reqPathInfo;
109
+ },
110
+
90
111
  /**
91
112
  * Returns the parsed query string of the request.
92
113
  *
@@ -140,6 +161,24 @@ module.exports = {
140
161
  * @returns {Buffer} The body of the request.
141
162
  */
142
163
  body: function() {
143
- return fs.readFileSync(process.env.reqBody, 'binary');
164
+ let retVal = undefined;
165
+ if (process.env.reqBody) {
166
+ retVal = fs.readFileSync(process.env.reqBody);
167
+ }
168
+ return retVal;
169
+ },
170
+
171
+ /**
172
+ * Returns the body of the request as an fs.ReadStream.
173
+ *
174
+ * @param {string | Object} [options] The options to pass to fs.createReadStream.
175
+ * @returns {fs.ReadStream} A ReadStream of the body of the request.
176
+ */
177
+ bodyStream: function(options) {
178
+ let retVal = undefined;
179
+ if (process.env.reqBody) {
180
+ retVal = fs.createReadStream(process.env.reqBody, options);
181
+ }
182
+ return retVal;
144
183
  }
145
184
  }
package/package.json CHANGED
@@ -1,27 +1,27 @@
1
- {
2
- "name": "sprucehttp_sjs",
3
- "version": "1.0.4",
4
- "description": "A module for responding to requests within SpruceHTTP in an SJS file",
5
- "main": "index.js",
6
- "scripts": {
7
- "test": "echo \"Error: no test specified\" && exit 1"
8
- },
9
- "keywords": [
10
- "http",
11
- "server",
12
- "https",
13
- "spruce",
14
- "sprucehttp"
15
- ],
16
- "homepage": "https://stibarc.dev/spruce/",
17
- "bugs": {
18
- "url": "https://stibarc.dev/spruce/reportbug/",
19
- "email": "herronjo@stibarc.dev"
20
- },
21
- "author": {
22
- "name": "Joshua Herron",
23
- "email": "herronjo@stibarc.dev",
24
- "url": "https://stibarc.dev"
25
- },
26
- "license": "Copyright (c) STiBaRC LLC. All rights reserved."
27
- }
1
+ {
2
+ "name": "sprucehttp_sjs",
3
+ "version": "1.0.7",
4
+ "description": "A module for responding to requests within SpruceHTTP in an SJS file",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "keywords": [
10
+ "http",
11
+ "server",
12
+ "https",
13
+ "spruce",
14
+ "sprucehttp"
15
+ ],
16
+ "homepage": "https://stibarc.dev/spruce/",
17
+ "bugs": {
18
+ "url": "https://stibarc.dev/spruce/reportbug/",
19
+ "email": "herronjo@stibarc.dev"
20
+ },
21
+ "author": {
22
+ "name": "Joshua Herron",
23
+ "email": "herronjo@stibarc.dev",
24
+ "url": "https://stibarc.dev"
25
+ },
26
+ "license": "Copyright (c) STiBaRC LLC. All rights reserved."
27
+ }