sprucehttp_sjs 2.0.0 → 2.1.0
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 +12 -0
- package/index.js +31 -15
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -35,6 +35,8 @@ Sets the response into stream mode. This cannot be undone for the duration of th
|
|
|
35
35
|
|
|
36
36
|
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.
|
|
37
37
|
|
|
38
|
+
Returns a Promise that resolves when the server acknowledges the switch to stream mode.
|
|
39
|
+
|
|
38
40
|
```js
|
|
39
41
|
sjs.streamMode(); // Set the response to stream mode.
|
|
40
42
|
```
|
|
@@ -45,6 +47,8 @@ sjs.streamMode(); // Set the response to stream mode.
|
|
|
45
47
|
|
|
46
48
|
Writes the status line to the response.
|
|
47
49
|
|
|
50
|
+
Returns a Promise that resolves when the server acknowledges the status line.
|
|
51
|
+
|
|
48
52
|
To send a 200 "OK" response, without needing to specify the reason phrase.
|
|
49
53
|
```js
|
|
50
54
|
sjs.writeStatusLine(200);
|
|
@@ -61,6 +65,8 @@ sjs.writeStatusLine(418, "I'm a teapot");
|
|
|
61
65
|
|
|
62
66
|
Writes a header to the response.
|
|
63
67
|
|
|
68
|
+
Returns a Promise that resolves when the server acknowledges the header.
|
|
69
|
+
|
|
64
70
|
To write the "Content-Type: text/html" header.
|
|
65
71
|
```js
|
|
66
72
|
sjs.writeHeader("Content-Type", "text/html");
|
|
@@ -89,6 +95,8 @@ sjs.writeData(fs.readFileSync("image.png", 'binary'));
|
|
|
89
95
|
|
|
90
96
|
Writes data (the body) to the response asynchronously. When not in stream mode, the Content-Length header is automatically updated.
|
|
91
97
|
|
|
98
|
+
Returns a Promise that resolves when the data has been written.
|
|
99
|
+
|
|
92
100
|
Writes text data to the body.
|
|
93
101
|
```js
|
|
94
102
|
await sjs.writeDataAsync("Hello, world!");
|
|
@@ -104,6 +112,8 @@ await sjs.writeDataAsync(fs.readFileSync("image.png", 'binary'));
|
|
|
104
112
|
Clears the currently written response and resets the status line, headers, and body.
|
|
105
113
|
This is useful when you want to send a different response than the one you have already written.
|
|
106
114
|
|
|
115
|
+
Returns a Promise that resolves when the response has been cleared.
|
|
116
|
+
|
|
107
117
|
This function does not work in stream mode.
|
|
108
118
|
|
|
109
119
|
```js
|
|
@@ -121,6 +131,8 @@ sjs.writeData("<h1>I'm <i>not</i> a teapot</h1>");
|
|
|
121
131
|
### ``siteConfig``:
|
|
122
132
|
Returns the site-specific configuration set in your config file.
|
|
123
133
|
|
|
134
|
+
Returns an object representing the site configuration.
|
|
135
|
+
|
|
124
136
|
```js
|
|
125
137
|
console.log(sjs.siteConfig);
|
|
126
138
|
/*
|
package/index.js
CHANGED
|
@@ -1,16 +1,19 @@
|
|
|
1
1
|
// This is still CommonJS to support both the old and new versions of the module system.
|
|
2
2
|
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
3
|
-
const fs = require("fs");
|
|
3
|
+
const fs = require("node:fs");
|
|
4
4
|
|
|
5
5
|
module.exports = {
|
|
6
6
|
/**
|
|
7
7
|
* Sets the response into stream mode.
|
|
8
8
|
*
|
|
9
9
|
* Stream mode means that responses are not buffered by the webserver.
|
|
10
|
+
* @returns {Promise<void>} A Promise that resolves when the server acknowledges the switch to stream mode.
|
|
10
11
|
*/
|
|
11
12
|
streamMode() {
|
|
12
|
-
|
|
13
|
-
|
|
13
|
+
return new Promise(function (resolve) {
|
|
14
|
+
process.send({
|
|
15
|
+
type: "streamMode"
|
|
16
|
+
}, resolve);
|
|
14
17
|
});
|
|
15
18
|
},
|
|
16
19
|
|
|
@@ -18,12 +21,15 @@ module.exports = {
|
|
|
18
21
|
* Writes the status line to the response.
|
|
19
22
|
* @param {number} statusCode The HTTP status code to send.
|
|
20
23
|
* @param {string} [reasonPhrase] The reason phrase to send.
|
|
24
|
+
* @return {Promise<void>} A Promise that resolves when the server acknowledges the status line.
|
|
21
25
|
*/
|
|
22
26
|
writeStatusLine(statusCode, reasonPhrase) {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
+
return new Promise(function (resolve) {
|
|
28
|
+
process.send({
|
|
29
|
+
type: "status",
|
|
30
|
+
statusCode,
|
|
31
|
+
reasonPhrase
|
|
32
|
+
}, resolve);
|
|
27
33
|
});
|
|
28
34
|
},
|
|
29
35
|
|
|
@@ -31,12 +37,15 @@ module.exports = {
|
|
|
31
37
|
* Writes a header to the response.
|
|
32
38
|
* @param {string} name The name of the header to send.
|
|
33
39
|
* @param {string} value The value of the header to send.
|
|
40
|
+
* @return {Promise<void>} A Promise that resolves when the server acknowledges the header.
|
|
34
41
|
*/
|
|
35
42
|
writeHeader(name, value) {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
43
|
+
return new Promise(function (resolve) {
|
|
44
|
+
process.send({
|
|
45
|
+
type: "header",
|
|
46
|
+
name,
|
|
47
|
+
value
|
|
48
|
+
}, resolve);
|
|
40
49
|
});
|
|
41
50
|
},
|
|
42
51
|
|
|
@@ -45,7 +54,8 @@ module.exports = {
|
|
|
45
54
|
*
|
|
46
55
|
* When not in stream mode, the Content-Length header is automatically updated.
|
|
47
56
|
* @param {string | Buffer} data The data to send.
|
|
48
|
-
* @
|
|
57
|
+
* @returns {void}
|
|
58
|
+
* @deprecated Use writeDataAsync instead. Data cannot be reliably sent synchronously. Will be removed in a future major version.
|
|
49
59
|
*/
|
|
50
60
|
writeData(data) {
|
|
51
61
|
(async () => { await this.writeDataAsync(data); })();
|
|
@@ -54,8 +64,11 @@ module.exports = {
|
|
|
54
64
|
/**
|
|
55
65
|
* Writes data (the body) to the response asynchronously.
|
|
56
66
|
*
|
|
67
|
+
* Will be renamed to writeData in a future major version.
|
|
68
|
+
*
|
|
57
69
|
* When not in stream mode, the Content-Length header is automatically updated.
|
|
58
70
|
* @param {string | Buffer} data The data to send.
|
|
71
|
+
* @returns {Promise<void>} A Promise that resolves when the data has been written.
|
|
59
72
|
*/
|
|
60
73
|
writeDataAsync(data) {
|
|
61
74
|
// eslint-disable-next-line no-async-promise-executor
|
|
@@ -86,10 +99,13 @@ module.exports = {
|
|
|
86
99
|
* This is useful when you want to send a different response than the one you have already written.
|
|
87
100
|
*
|
|
88
101
|
* This function does not work in stream mode.
|
|
102
|
+
* @returns {Promise<void>} A Promise that resolves when the response has been cleared.
|
|
89
103
|
*/
|
|
90
104
|
clearResponse() {
|
|
91
|
-
|
|
92
|
-
|
|
105
|
+
return new Promise(function (resolve) {
|
|
106
|
+
process.send({
|
|
107
|
+
type: "clear"
|
|
108
|
+
}, resolve);
|
|
93
109
|
});
|
|
94
110
|
},
|
|
95
111
|
|
|
@@ -215,7 +231,7 @@ module.exports = {
|
|
|
215
231
|
/**
|
|
216
232
|
* Returns details about the mTLS peer's certificate, if present.
|
|
217
233
|
*
|
|
218
|
-
* @returns {PeerCertificate}
|
|
234
|
+
* @returns {import("node:tls").PeerCertificate}
|
|
219
235
|
*/
|
|
220
236
|
get mTlsPeerCertificate() {
|
|
221
237
|
let retVal = undefined;
|