minimalistic-server 0.0.1 → 0.0.2

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.mjs +47 -5
  2. package/package.json +32 -32
package/index.mjs CHANGED
@@ -1159,6 +1159,8 @@ export class FileResponse extends Response {
1159
1159
 
1160
1160
  #dataPromise = null;
1161
1161
 
1162
+ #maxChunkSize = 16 * 1024 * 1024;
1163
+
1162
1164
  constructor(filePath, code = 200, contentType = null, cookies = null) {
1163
1165
  super();
1164
1166
  this.#filePath = filePath;
@@ -1190,6 +1192,30 @@ export class FileResponse extends Response {
1190
1192
  this.#filePath = filePath;
1191
1193
  }
1192
1194
 
1195
+ async * #getBodyStream() {
1196
+ const data = await this.#retreiveData();
1197
+
1198
+ if (typeof data.body !== 'function') {
1199
+ yield data.body;
1200
+ return;
1201
+ }
1202
+
1203
+ let filehandle;
1204
+
1205
+ try {
1206
+ filehandle = await fs.open(this.#filePath, 'r');
1207
+
1208
+ for (let offset = 0; offset < data.size; offset += this.#maxChunkSize) {
1209
+ const size = Math.min(this.#maxChunkSize, data.size - offset);
1210
+ const chunk = await filehandle.read(Buffer.alloc(size), 0, size);
1211
+ yield chunk;
1212
+ await new Promise(resolve => setTimeout(resolve, 100));
1213
+ }
1214
+ } finally {
1215
+ filehandle?.close();
1216
+ }
1217
+ }
1218
+
1193
1219
  #retreiveData() {
1194
1220
  if (!this.#dataPromise) {
1195
1221
  this.#dataPromise = new Promise(async (resolve) => {
@@ -1197,15 +1223,18 @@ export class FileResponse extends Response {
1197
1223
 
1198
1224
  try {
1199
1225
  filehandle = await fs.open(this.#filePath, 'r');
1200
- const body = await filehandle.readFile();
1226
+ const size = (await filehandle.stat()).size;
1227
+ const body = size > this.#maxChunkSize ? (() => this.#getBodyStream()) : await filehandle.readFile();
1201
1228
 
1202
1229
  resolve({
1203
1230
  code: this.#code,
1204
1231
  headers: this.getMergedWithOtherHeaders({
1205
- 'Content-Type': this.#contentType ?? mimeTypes[this.#filePath.split('.').at(-1).toLowerCase()] ?? 'application/octet-stream'
1232
+ 'Content-Type': this.#contentType ?? mimeTypes[this.#filePath.split('.').at(-1).toLowerCase()] ?? 'application/octet-stream',
1233
+ 'Content-Length': size,
1206
1234
  }
1207
1235
  ),
1208
1236
  body,
1237
+ size,
1209
1238
  });
1210
1239
  } catch (e) {
1211
1240
  console.error(e);
@@ -1335,9 +1364,22 @@ export function serve(routes, port = 80, staticFileDirectory = null) {
1335
1364
  unserve(port);
1336
1365
 
1337
1366
  const server = http.createServer(async (req, res) => {
1338
- const [code, headers, body] = await handleRequest(req, routes, staticFileDirectory);
1339
- res.writeHead(code, headers);
1340
- res.end(body);
1367
+ try {
1368
+ const [code, headers, body] = await handleRequest(req, routes, staticFileDirectory);
1369
+ res.writeHead(code, headers);
1370
+
1371
+ if (typeof body === 'function') {
1372
+ for await (const chunk of body()) {
1373
+ res.write(chunk);
1374
+ }
1375
+ } else {
1376
+ res.write(body);
1377
+ }
1378
+ } catch (error) {
1379
+ console.error(error);
1380
+ } finally {
1381
+ res.end();
1382
+ }
1341
1383
  });
1342
1384
 
1343
1385
  servers.set(port, server);
package/package.json CHANGED
@@ -1,32 +1,32 @@
1
- {
2
- "name": "minimalistic-server",
3
- "version": "0.0.1",
4
- "engines" : {
5
- "npm" : ">=8.6.0",
6
- "node" : ">=22.0.0"
7
- },
8
- "type": "module",
9
- "description": "Minimalistic server library hor Node.js. It supports routing, middlewares, parsing common request bodies (JSON, form data, etc).",
10
- "main": "index.mjs",
11
- "scripts": {
12
- "test": "echo \"Error: no test specified\" && exit 1"
13
- },
14
- "repository": {
15
- "type": "git",
16
- "url": "git+https://github.com/surenenfiajyan/node-minimalistic-server.git"
17
- },
18
- "keywords": [
19
- "minimalistic",
20
- "server",
21
- "lite",
22
- "server",
23
- "small",
24
- "server"
25
- ],
26
- "author": "Suren Enfiajyan <surenenfiajyan@gmail.com> (https://github.com/surenenfiajyan)",
27
- "license": "MIT",
28
- "bugs": {
29
- "url": "https://github.com/surenenfiajyan/node-minimalistic-server/issues"
30
- },
31
- "homepage": "https://github.com/surenenfiajyan/node-minimalistic-server#readme"
32
- }
1
+ {
2
+ "name": "minimalistic-server",
3
+ "version": "0.0.2",
4
+ "engines" : {
5
+ "npm" : ">=8.6.0",
6
+ "node" : ">=22.0.0"
7
+ },
8
+ "type": "module",
9
+ "description": "Minimalistic server library hor Node.js. It supports routing, middlewares, parsing common request bodies (JSON, form data, etc).",
10
+ "main": "index.mjs",
11
+ "scripts": {
12
+ "test": "echo \"Error: no test specified\" && exit 1"
13
+ },
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "git+https://github.com/surenenfiajyan/node-minimalistic-server.git"
17
+ },
18
+ "keywords": [
19
+ "minimalistic",
20
+ "server",
21
+ "lite",
22
+ "server",
23
+ "small",
24
+ "server"
25
+ ],
26
+ "author": "Suren Enfiajyan <surenenfiajyan@gmail.com> (https://github.com/surenenfiajyan)",
27
+ "license": "MIT",
28
+ "bugs": {
29
+ "url": "https://github.com/surenenfiajyan/node-minimalistic-server/issues"
30
+ },
31
+ "homepage": "https://github.com/surenenfiajyan/node-minimalistic-server#readme"
32
+ }