sprucehttp_sjs 1.0.4 → 1.0.5
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 +17 -0
- package/index.js +20 -4
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -8,6 +8,7 @@ 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)
|
|
@@ -77,6 +78,22 @@ const fs = require('fs');
|
|
|
77
78
|
sjs.writeData(fs.readFileSync("image.png", 'binary'));
|
|
78
79
|
```
|
|
79
80
|
|
|
81
|
+
### ``writeDataAsync(data)``:
|
|
82
|
+
- ``data:`` string | Buffer: The data to send.
|
|
83
|
+
|
|
84
|
+
Writes data (the body) to the response asynchronously. When not in stream mode, the Content-Length header is automatically updated.
|
|
85
|
+
|
|
86
|
+
Writes text data to the body.
|
|
87
|
+
```js
|
|
88
|
+
await sjs.writeDataAsync("Hello, world!");
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
Writes binary data (a picture, in this case) to the body.
|
|
92
|
+
```js
|
|
93
|
+
const fs = require('fs');
|
|
94
|
+
await sjs.writeDataAsync(fs.readFileSync("image.png", 'binary'));
|
|
95
|
+
```
|
|
96
|
+
|
|
80
97
|
### ``siteConfig()``:
|
|
81
98
|
Returns the site-specific configuration set in your config file.
|
|
82
99
|
|
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
|
-
|
|
49
|
-
|
|
50
|
-
|
|
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
|
|
|
@@ -140,6 +152,10 @@ module.exports = {
|
|
|
140
152
|
* @returns {Buffer} The body of the request.
|
|
141
153
|
*/
|
|
142
154
|
body: function() {
|
|
143
|
-
|
|
155
|
+
let retVal = undefined;
|
|
156
|
+
if (process.env.reqBody) {
|
|
157
|
+
retVal = fs.readFileSync(process.env.reqBody);
|
|
158
|
+
}
|
|
159
|
+
return retVal;
|
|
144
160
|
}
|
|
145
161
|
}
|