sprucehttp_sjs 1.0.0 → 1.0.1
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 +68 -0
- package/index.js +24 -0
- package/package.json +11 -11
package/README.md
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# SpruceHTTP SJS:
|
|
2
|
+
A module for responding to requests within SpruceHTTP in an SJS file.
|
|
3
|
+
|
|
4
|
+
## Table of Contents:
|
|
5
|
+
- [Table of Contents](#table-of-contents)
|
|
6
|
+
- [Documentation](#documentation)
|
|
7
|
+
- [streamMode()](#streammode)
|
|
8
|
+
- [writeStatusLine(statusCode, reasonPhrase)](#writestatuslinestatuscode-reasonphrase)
|
|
9
|
+
- [writeHeader(name, value)](#writestatuslinestatuscode-reasonphrase)
|
|
10
|
+
- [writeData(data)](#writedatadata)
|
|
11
|
+
|
|
12
|
+
## Documentation:
|
|
13
|
+
Importing the module:
|
|
14
|
+
```js
|
|
15
|
+
const sjs = require("sprucehttp_sjs");
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
### ``streamMode()``:
|
|
19
|
+
Sets the response into stream mode. This cannot be undone for the duration of the script, and should be the first action performed if this is the intended mode.
|
|
20
|
+
|
|
21
|
+
Stream mode means that responses are not buffered by the webserver, and thus are sent immediately as they're received from the SJS script. This means that the status line and headers _**must**_ be sent first before the data, and the ``\r\n`` after the headers must be sent by the script as well.
|
|
22
|
+
|
|
23
|
+
```js
|
|
24
|
+
sjs.streamMode(); // Set the response to stream mode.
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
### ``writeStatusLine(statusCode[, reasonPhrase])``
|
|
28
|
+
- ``statusLine:`` number: The HTTP status code to send.
|
|
29
|
+
- ``reasonPhrase:`` string: The reason phrase to send.
|
|
30
|
+
|
|
31
|
+
Writes the status line to the response.
|
|
32
|
+
|
|
33
|
+
To send a 200 "OK" response, without needing to specify the reason phrase.
|
|
34
|
+
```js
|
|
35
|
+
sjs.writeStatusLine(200);
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
To send a 418 "I'm a teapot" response, with a specified reason phrase.
|
|
39
|
+
```js
|
|
40
|
+
sjs.writeStatusLine(418, "I'm a teapot");
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
### ``writeHeader(name, value)``
|
|
44
|
+
- ``name:`` string: The name of the header to send.
|
|
45
|
+
- ``value:`` string: The value of the header to send.
|
|
46
|
+
|
|
47
|
+
Writes a header to the response.
|
|
48
|
+
|
|
49
|
+
To write the "Content-Type: text/html" header.
|
|
50
|
+
```js
|
|
51
|
+
sjs.writeHeader("Content-Type", "text/html");
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### ``writeData(data)``
|
|
55
|
+
- ``data:`` string | Buffer: The data to send.
|
|
56
|
+
|
|
57
|
+
Writes data (the body) to the response. When not in stream mode, the Content-Length header is automatically updated.
|
|
58
|
+
|
|
59
|
+
Writes text data to the body.
|
|
60
|
+
```js
|
|
61
|
+
sjs.writeData("Hello, world!");
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
Writes binary data (a picture, in this case) to the body.
|
|
65
|
+
```js
|
|
66
|
+
const fs = require('fs');
|
|
67
|
+
sjs.writeData(fs.readFileSync("image.png", 'binary'));
|
|
68
|
+
```
|
package/index.js
CHANGED
|
@@ -1,9 +1,20 @@
|
|
|
1
1
|
module.exports = {
|
|
2
|
+
/**
|
|
3
|
+
* Sets the response into stream mode.
|
|
4
|
+
*
|
|
5
|
+
* Stream mode means that responses are not buffered by the webserver.
|
|
6
|
+
*/
|
|
2
7
|
streamMode: function() {
|
|
3
8
|
process.send({
|
|
4
9
|
type: 'streamMode'
|
|
5
10
|
});
|
|
6
11
|
},
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Writes the status line to the response.
|
|
15
|
+
* @param {number} statusCode The HTTP status code to send.
|
|
16
|
+
* @param {string} [reasonPhrase] The reason phrase to send.
|
|
17
|
+
*/
|
|
7
18
|
writeStatusLine: function(statusCode, reasonPhrase) {
|
|
8
19
|
process.send({
|
|
9
20
|
type: 'status',
|
|
@@ -11,6 +22,12 @@ module.exports = {
|
|
|
11
22
|
reasonPhrase
|
|
12
23
|
});
|
|
13
24
|
},
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Writes a header to the response.
|
|
28
|
+
* @param {string} name The name of the header to send.
|
|
29
|
+
* @param {string} value The value of the header to send.
|
|
30
|
+
*/
|
|
14
31
|
writeHeader: function(name, value) {
|
|
15
32
|
process.send({
|
|
16
33
|
type: 'status',
|
|
@@ -18,6 +35,13 @@ module.exports = {
|
|
|
18
35
|
value
|
|
19
36
|
});
|
|
20
37
|
},
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Writes data (the body) to the response.
|
|
41
|
+
*
|
|
42
|
+
* When not in stream mode, the Content-Length header is automatically updated.
|
|
43
|
+
* @param {string | Buffer} data The data to send.
|
|
44
|
+
*/
|
|
21
45
|
writeData: function(data) {
|
|
22
46
|
process.send({
|
|
23
47
|
type: 'data',
|
package/package.json
CHANGED
|
@@ -1,27 +1,27 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
2
|
+
"name": "sprucehttp_sjs",
|
|
3
|
+
"version": "1.0.1",
|
|
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
10
|
"http",
|
|
11
11
|
"server",
|
|
12
12
|
"https",
|
|
13
13
|
"spruce",
|
|
14
14
|
"sprucehttp"
|
|
15
15
|
],
|
|
16
|
-
|
|
16
|
+
"homepage": "https://stibarc.dev/spruce/",
|
|
17
17
|
"bugs": {
|
|
18
18
|
"url": "https://stibarc.dev/spruce/reportbug/",
|
|
19
19
|
"email": "herronjo@stibarc.dev"
|
|
20
20
|
},
|
|
21
|
-
|
|
21
|
+
"author": {
|
|
22
22
|
"name": "Joshua Herron",
|
|
23
23
|
"email": "herronjo@stibarc.dev",
|
|
24
24
|
"url": "https://stibarc.dev"
|
|
25
25
|
},
|
|
26
|
-
|
|
26
|
+
"license": "Copyright (c) STiBaRC LLC. All rights reserved."
|
|
27
27
|
}
|