sprucehttp_sjs 1.0.9 → 1.0.11

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 (2) hide show
  1. package/index.js +48 -32
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -1,3 +1,5 @@
1
+ // This is still CommonJS to support both the old and new versions of the module system.
2
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
1
3
  const fs = require("fs");
2
4
 
3
5
  module.exports = {
@@ -6,20 +8,20 @@ module.exports = {
6
8
  *
7
9
  * Stream mode means that responses are not buffered by the webserver.
8
10
  */
9
- streamMode: function() {
11
+ streamMode: function () {
10
12
  process.send({
11
- type: 'streamMode'
13
+ type: "streamMode"
12
14
  });
13
15
  },
14
-
16
+
15
17
  /**
16
18
  * Writes the status line to the response.
17
19
  * @param {number} statusCode The HTTP status code to send.
18
20
  * @param {string} [reasonPhrase] The reason phrase to send.
19
21
  */
20
- writeStatusLine: function(statusCode, reasonPhrase) {
22
+ writeStatusLine: function (statusCode, reasonPhrase) {
21
23
  process.send({
22
- type: 'status',
24
+ type: "status",
23
25
  statusCode,
24
26
  reasonPhrase
25
27
  });
@@ -30,9 +32,9 @@ module.exports = {
30
32
  * @param {string} name The name of the header to send.
31
33
  * @param {string} value The value of the header to send.
32
34
  */
33
- writeHeader: function(name, value) {
35
+ writeHeader: function (name, value) {
34
36
  process.send({
35
- type: 'header',
37
+ type: "header",
36
38
  name,
37
39
  value
38
40
  });
@@ -44,8 +46,8 @@ module.exports = {
44
46
  * When not in stream mode, the Content-Length header is automatically updated.
45
47
  * @param {string | Buffer} data The data to send.
46
48
  */
47
- writeData: function(data) {
48
- (async () => {await this.writeDataAsync(data);})();
49
+ writeData: function (data) {
50
+ (async () => { await this.writeDataAsync(data); })();
49
51
  },
50
52
 
51
53
  /**
@@ -54,12 +56,26 @@ module.exports = {
54
56
  * When not in stream mode, the Content-Length header is automatically updated.
55
57
  * @param {string | Buffer} data The data to send.
56
58
  */
57
- writeDataAsync: function(data) {
58
- return new Promise(function(resolve) {
59
- process.send({
60
- type: 'data',
61
- data: Buffer.from(data)
62
- }, resolve);
59
+ writeDataAsync: function (data) {
60
+ return new Promise(async function (resolve) {
61
+ // Send the data in 1 MiB chunks
62
+ const chunkSize = 1024 * 1024;
63
+ // Split the data into chunks
64
+ let chunks = [];
65
+ const data2 = Buffer.from(data);
66
+ for (let i = 0; i < data2.length; i += chunkSize) {
67
+ chunks.push(data2.subarray(i, i + chunkSize));
68
+ }
69
+ // Send all chunks
70
+ for (const chunk of chunks) {
71
+ await new Promise(function (resolve2) {
72
+ process.send({
73
+ type: "data",
74
+ data: chunk
75
+ }, resolve2);
76
+ });
77
+ }
78
+ resolve();
63
79
  });
64
80
  },
65
81
 
@@ -69,9 +85,9 @@ module.exports = {
69
85
  *
70
86
  * This function does not work in stream mode.
71
87
  */
72
- clearResponse: function() {
88
+ clearResponse: function () {
73
89
  process.send({
74
- type: 'clear'
90
+ type: "clear"
75
91
  });
76
92
  },
77
93
 
@@ -80,7 +96,7 @@ module.exports = {
80
96
  *
81
97
  * @returns {{[key: string]: string}} The site-specific configuration.
82
98
  */
83
- siteConfig: function() {
99
+ siteConfig: function () {
84
100
  return JSON.parse(process.env.siteConfig);
85
101
  },
86
102
 
@@ -89,7 +105,7 @@ module.exports = {
89
105
  *
90
106
  * @returns {string} The requestor's IP address.
91
107
  */
92
- requestIP: function() {
108
+ requestIP: function () {
93
109
  return process.env.reqIP;
94
110
  },
95
111
 
@@ -98,8 +114,8 @@ module.exports = {
98
114
  *
99
115
  * @returns {string} The request method.
100
116
  */
101
- method: function() {
102
- return process.env.reqMethod;
117
+ method: function () {
118
+ return process.env.reqMethod.toLowerCase();
103
119
  },
104
120
 
105
121
  /**
@@ -107,7 +123,7 @@ module.exports = {
107
123
  *
108
124
  * @returns {string} The full request path.
109
125
  */
110
- path: function() {
126
+ path: function () {
111
127
  return process.env.reqPath;
112
128
  },
113
129
 
@@ -116,7 +132,7 @@ module.exports = {
116
132
  *
117
133
  * @returns {string} The request's info path.
118
134
  */
119
- pathInfo: function() {
135
+ pathInfo: function () {
120
136
  return process.env.reqPathInfo;
121
137
  },
122
138
 
@@ -125,7 +141,7 @@ module.exports = {
125
141
  *
126
142
  * @returns {{[key: string]: string}} The parsed query string of the request.
127
143
  */
128
- query: function() {
144
+ query: function () {
129
145
  return JSON.parse(process.env.reqQuery || "{}");
130
146
  },
131
147
 
@@ -135,7 +151,7 @@ module.exports = {
135
151
  * @param {string} key The key of the query to get.
136
152
  * @returns {string} The value of the query parameter.
137
153
  */
138
- queryValue: function(key) {
154
+ queryValue: function (key) {
139
155
  return this.query()[key];
140
156
  },
141
157
 
@@ -144,7 +160,7 @@ module.exports = {
144
160
  *
145
161
  * @returns {string} The HTTP version of the request.
146
162
  */
147
- httpVersion: function() {
163
+ httpVersion: function () {
148
164
  return process.env.reqHttpVersion;
149
165
  },
150
166
 
@@ -153,7 +169,7 @@ module.exports = {
153
169
  *
154
170
  * @returns {{[key: string]: string}} The headers of the request.
155
171
  */
156
- headers: function() {
172
+ headers: function () {
157
173
  return JSON.parse(process.env.reqHeaders || "{}");
158
174
  },
159
175
 
@@ -163,7 +179,7 @@ module.exports = {
163
179
  * @param {string} name The name of the header to get.
164
180
  * @returns {string} The value of the header.
165
181
  */
166
- headerValue: function(name) {
182
+ headerValue: function (name) {
167
183
  return this.headers()[name];
168
184
  },
169
185
 
@@ -172,7 +188,7 @@ module.exports = {
172
188
  *
173
189
  * @returns {Buffer} The body of the request.
174
190
  */
175
- body: function() {
191
+ body: function () {
176
192
  let retVal = undefined;
177
193
  if (process.env.reqBody) {
178
194
  retVal = fs.readFileSync(process.env.reqBody);
@@ -183,14 +199,14 @@ module.exports = {
183
199
  /**
184
200
  * Returns the body of the request as an fs.ReadStream.
185
201
  *
186
- * @param {string | Object} [options] The options to pass to fs.createReadStream.
202
+ * @param {BufferEncoding | ReadStreamOptions} [options] The options to pass to fs.createReadStream.
187
203
  * @returns {fs.ReadStream} A ReadStream of the body of the request.
188
204
  */
189
- bodyStream: function(options) {
205
+ bodyStream: function (options) {
190
206
  let retVal = undefined;
191
207
  if (process.env.reqBody) {
192
208
  retVal = fs.createReadStream(process.env.reqBody, options);
193
209
  }
194
210
  return retVal;
195
211
  }
196
- }
212
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sprucehttp_sjs",
3
- "version": "1.0.9",
3
+ "version": "1.0.11",
4
4
  "description": "A module for responding to requests within SpruceHTTP in an SJS file",
5
5
  "main": "index.js",
6
6
  "scripts": {