@wooksjs/http-static 0.4.11 → 0.4.13

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/dist/index.cjs CHANGED
@@ -1224,8 +1224,6 @@ async function serveFile(filePath, options = {}) {
1224
1224
  throw new eventHttp.HttpError(404);
1225
1225
  }
1226
1226
  status(200);
1227
- // if-none-match & if-modified-since processing start
1228
- // rfc7232
1229
1227
  const etag = `"${[
1230
1228
  fileStats.ino,
1231
1229
  fileStats.size,
@@ -1236,7 +1234,6 @@ async function serveFile(filePath, options = {}) {
1236
1234
  status(304);
1237
1235
  return '';
1238
1236
  }
1239
- // if-none-match & if-modified-since processing end
1240
1237
  setHeader('etag', etag);
1241
1238
  setHeader('last-modified', lastModified.toUTCString());
1242
1239
  if (options.cacheControl !== undefined) {
@@ -1271,7 +1268,6 @@ async function serveFile(filePath, options = {}) {
1271
1268
  removeHeader('last-modified');
1272
1269
  throw new eventHttp.HttpError(404);
1273
1270
  }
1274
- // range header processing start
1275
1271
  let range = headers.range;
1276
1272
  let start, end;
1277
1273
  let size = fileStats.size;
@@ -1288,30 +1284,20 @@ async function serveFile(filePath, options = {}) {
1288
1284
  throw new eventHttp.HttpError(416);
1289
1285
  }
1290
1286
  size = end - start + 1;
1291
- // if-range processing start
1292
- // rfc7233#section-3.2\
1293
1287
  const ifRange = headers['if-range'] || '';
1294
1288
  const ifRangeTag = ifRange[0] === '"' ? ifRange : '';
1295
1289
  const ifRangeDate = ifRangeTag ? '' : ifRange;
1296
1290
  if (ifRange &&
1297
1291
  !isNotModified(etag, lastModified, ifRangeTag, ifRangeDate)) {
1298
- // If the validator does not match, the server MUST ignore
1299
- // the Range header field.
1300
1292
  status(200);
1301
1293
  size = fileStats.size;
1302
1294
  range = '';
1303
1295
  }
1304
1296
  else {
1305
- // If the validator given in the If-Range header field matches the
1306
- // current validator for the selected representation of the target
1307
- // resource, then the server SHOULD process the Range header field as
1308
- // requested.
1309
1297
  setHeader('content-range', `bytes ${start}-${end}/${fileStats.size}`);
1310
1298
  status(206);
1311
1299
  }
1312
- // if-range processing end
1313
1300
  }
1314
- // range header processing end
1315
1301
  setHeader('accept-ranges', 'bytes');
1316
1302
  setHeader('content-type', getMimeType(normalizedPath) || 'application/octet-stream');
1317
1303
  setHeader('content-length', size);
@@ -1324,9 +1310,6 @@ async function serveFile(filePath, options = {}) {
1324
1310
  ? ''
1325
1311
  : fs.createReadStream(normalizedPath, !!range ? { start, end } : undefined);
1326
1312
  }
1327
- // function toWeak(etag: string): string {
1328
- // return `W/${etag}`
1329
- // }
1330
1313
  function isNotModified(etag, lastModified, clientEtag, clientLM) {
1331
1314
  if (clientEtag) {
1332
1315
  const parts = clientEtag.split(',').map((v) => v.trim());
@@ -1335,18 +1318,9 @@ function isNotModified(etag, lastModified, clientEtag, clientLM) {
1335
1318
  return true;
1336
1319
  }
1337
1320
  }
1338
- // A recipient MUST ignore If-Modified-Since if the request contains an
1339
- // If-None-Match header field; the condition in If-None-Match is
1340
- // considered to be a more accurate replacement for the condition in
1341
- // If-Modified-Since, and the two are only combined for the sake of
1342
- // interoperating with older intermediaries that might not implement
1343
- // If-None-Match.
1344
1321
  return false;
1345
1322
  }
1346
1323
  const date = new Date(clientLM);
1347
- // A recipient MUST ignore the If-Modified-Since header field if the
1348
- // received field-value is not a valid HTTP-date, or if the request
1349
- // method is neither GET nor HEAD.
1350
1324
  if (date.toString() !== 'Invalid Date' &&
1351
1325
  date.getTime() > lastModified.getTime()) {
1352
1326
  return true;
package/dist/index.d.ts CHANGED
@@ -1,20 +1,17 @@
1
- /// <reference types="node" />
2
-
3
- import { Readable } from 'stream';
4
- import { TCacheControl } from '@wooksjs/event-http';
5
-
6
- export declare function serveFile(filePath: string, options?: TServeFileOptions): Promise<Readable | string | string[] | unknown>;
7
-
8
- declare interface TServeFileOptions {
9
- headers?: Record<string, string>;
10
- cacheControl?: TCacheControl;
11
- expires?: Date | string | number;
12
- pragmaNoCache?: boolean;
13
- baseDir?: string;
14
- defaultExt?: string;
15
- listDirectory?: boolean;
16
- index?: string;
17
- allowDotDot?: boolean;
18
- }
19
-
20
- export { }
1
+ import { TCacheControl } from '@wooksjs/event-http';
2
+ import { Readable } from 'stream';
3
+
4
+ interface TServeFileOptions {
5
+ headers?: Record<string, string>;
6
+ cacheControl?: TCacheControl;
7
+ expires?: Date | string | number;
8
+ pragmaNoCache?: boolean;
9
+ baseDir?: string;
10
+ defaultExt?: string;
11
+ listDirectory?: boolean;
12
+ index?: string;
13
+ allowDotDot?: boolean;
14
+ }
15
+ declare function serveFile(filePath: string, options?: TServeFileOptions): Promise<Readable | string | string[] | unknown>;
16
+
17
+ export { serveFile };
package/dist/index.mjs CHANGED
@@ -1222,8 +1222,6 @@ async function serveFile(filePath, options = {}) {
1222
1222
  throw new HttpError(404);
1223
1223
  }
1224
1224
  status(200);
1225
- // if-none-match & if-modified-since processing start
1226
- // rfc7232
1227
1225
  const etag = `"${[
1228
1226
  fileStats.ino,
1229
1227
  fileStats.size,
@@ -1234,7 +1232,6 @@ async function serveFile(filePath, options = {}) {
1234
1232
  status(304);
1235
1233
  return '';
1236
1234
  }
1237
- // if-none-match & if-modified-since processing end
1238
1235
  setHeader('etag', etag);
1239
1236
  setHeader('last-modified', lastModified.toUTCString());
1240
1237
  if (options.cacheControl !== undefined) {
@@ -1269,7 +1266,6 @@ async function serveFile(filePath, options = {}) {
1269
1266
  removeHeader('last-modified');
1270
1267
  throw new HttpError(404);
1271
1268
  }
1272
- // range header processing start
1273
1269
  let range = headers.range;
1274
1270
  let start, end;
1275
1271
  let size = fileStats.size;
@@ -1286,30 +1282,20 @@ async function serveFile(filePath, options = {}) {
1286
1282
  throw new HttpError(416);
1287
1283
  }
1288
1284
  size = end - start + 1;
1289
- // if-range processing start
1290
- // rfc7233#section-3.2\
1291
1285
  const ifRange = headers['if-range'] || '';
1292
1286
  const ifRangeTag = ifRange[0] === '"' ? ifRange : '';
1293
1287
  const ifRangeDate = ifRangeTag ? '' : ifRange;
1294
1288
  if (ifRange &&
1295
1289
  !isNotModified(etag, lastModified, ifRangeTag, ifRangeDate)) {
1296
- // If the validator does not match, the server MUST ignore
1297
- // the Range header field.
1298
1290
  status(200);
1299
1291
  size = fileStats.size;
1300
1292
  range = '';
1301
1293
  }
1302
1294
  else {
1303
- // If the validator given in the If-Range header field matches the
1304
- // current validator for the selected representation of the target
1305
- // resource, then the server SHOULD process the Range header field as
1306
- // requested.
1307
1295
  setHeader('content-range', `bytes ${start}-${end}/${fileStats.size}`);
1308
1296
  status(206);
1309
1297
  }
1310
- // if-range processing end
1311
1298
  }
1312
- // range header processing end
1313
1299
  setHeader('accept-ranges', 'bytes');
1314
1300
  setHeader('content-type', getMimeType(normalizedPath) || 'application/octet-stream');
1315
1301
  setHeader('content-length', size);
@@ -1322,9 +1308,6 @@ async function serveFile(filePath, options = {}) {
1322
1308
  ? ''
1323
1309
  : createReadStream(normalizedPath, !!range ? { start, end } : undefined);
1324
1310
  }
1325
- // function toWeak(etag: string): string {
1326
- // return `W/${etag}`
1327
- // }
1328
1311
  function isNotModified(etag, lastModified, clientEtag, clientLM) {
1329
1312
  if (clientEtag) {
1330
1313
  const parts = clientEtag.split(',').map((v) => v.trim());
@@ -1333,18 +1316,9 @@ function isNotModified(etag, lastModified, clientEtag, clientLM) {
1333
1316
  return true;
1334
1317
  }
1335
1318
  }
1336
- // A recipient MUST ignore If-Modified-Since if the request contains an
1337
- // If-None-Match header field; the condition in If-None-Match is
1338
- // considered to be a more accurate replacement for the condition in
1339
- // If-Modified-Since, and the two are only combined for the sake of
1340
- // interoperating with older intermediaries that might not implement
1341
- // If-None-Match.
1342
1319
  return false;
1343
1320
  }
1344
1321
  const date = new Date(clientLM);
1345
- // A recipient MUST ignore the If-Modified-Since header field if the
1346
- // received field-value is not a valid HTTP-date, or if the request
1347
- // method is neither GET nor HEAD.
1348
1322
  if (date.toString() !== 'Invalid Date' &&
1349
1323
  date.getTime() > lastModified.getTime()) {
1350
1324
  return true;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wooksjs/http-static",
3
- "version": "0.4.11",
3
+ "version": "0.4.13",
4
4
  "description": "@wooksjs/http-static",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.mjs",
@@ -8,6 +8,14 @@
8
8
  "files": [
9
9
  "dist"
10
10
  ],
11
+ "exports": {
12
+ "./package.json": "./package.json",
13
+ ".": {
14
+ "require": "./dist/index.cjs",
15
+ "import": "./dist/index.mjs",
16
+ "types": "./dist/index.d.ts"
17
+ }
18
+ },
11
19
  "repository": {
12
20
  "type": "git",
13
21
  "url": "git+https://github.com/wooksjs/wooksjs.git",
@@ -31,7 +39,7 @@
31
39
  "url": "https://github.com/wooksjs/wooksjs/issues"
32
40
  },
33
41
  "peerDependencies": {
34
- "@wooksjs/event-http": "0.4.11"
42
+ "@wooksjs/event-http": "0.4.13"
35
43
  },
36
44
  "homepage": "https://github.com/wooksjs/wooksjs/tree/main/packages/http-static#readme"
37
45
  }