sprucehttp_sjs 1.0.3 → 1.0.6

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 +29 -0
  2. package/index.js +30 -5
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -8,10 +8,12 @@ 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)
@@ -77,6 +79,22 @@ const fs = require('fs');
77
79
  sjs.writeData(fs.readFileSync("image.png", 'binary'));
78
80
  ```
79
81
 
82
+ ### ``writeDataAsync(data)``:
83
+ - ``data:`` string | Buffer: The data to send.
84
+
85
+ Writes data (the body) to the response asynchronously. When not in stream mode, the Content-Length header is automatically updated.
86
+
87
+ Writes text data to the body.
88
+ ```js
89
+ await sjs.writeDataAsync("Hello, world!");
90
+ ```
91
+
92
+ Writes binary data (a picture, in this case) to the body.
93
+ ```js
94
+ const fs = require('fs');
95
+ await sjs.writeDataAsync(fs.readFileSync("image.png", 'binary'));
96
+ ```
97
+
80
98
  ### ``siteConfig()``:
81
99
  Returns the site-specific configuration set in your config file.
82
100
 
@@ -126,6 +144,17 @@ console.log(sjs.path());
126
144
  // /index.sjs
127
145
  ```
128
146
 
147
+ ### ``pathInfo()``:
148
+ Returns the request's info path.
149
+
150
+ ```js
151
+ // Using path /index.sjs/info
152
+ console.log(sjs.path());
153
+ console.log(sjs.pathInfo());
154
+ // /index.sjs
155
+ // /info
156
+ ```
157
+
129
158
  ### ``query()``:
130
159
  Returns the request query.
131
160
 
package/index.js CHANGED
@@ -32,7 +32,7 @@ module.exports = {
32
32
  */
33
33
  writeHeader: function(name, value) {
34
34
  process.send({
35
- type: 'status',
35
+ type: 'header',
36
36
  name,
37
37
  value
38
38
  });
@@ -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,10 @@ 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;
144
169
  }
145
170
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sprucehttp_sjs",
3
- "version": "1.0.3",
3
+ "version": "1.0.6",
4
4
  "description": "A module for responding to requests within SpruceHTTP in an SJS file",
5
5
  "main": "index.js",
6
6
  "scripts": {