@aomex/etag 0.0.27 → 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/CHANGELOG.md CHANGED
@@ -1,57 +1,15 @@
1
1
  # @aomex/etag
2
2
 
3
- ## 0.0.27
3
+ ## 1.0.1
4
4
 
5
- ### Patch Changes
6
-
7
- - Updated dependencies [[`59cd3f9`](https://github.com/aomex/aomex/commit/59cd3f98a0b5648a972d6118ba21501130a9ee7e), [`90ca7d5`](https://github.com/aomex/aomex/commit/90ca7d5fc736b523c4fbf7949f64653428dc413c)]:
8
- - @aomex/web@0.0.29
9
- - @aomex/core@0.0.28
10
-
11
- ## 0.0.26
12
-
13
- ### Patch Changes
14
-
15
- - Updated dependencies [[`00bafbb`](https://github.com/aomex/aomex/commit/00bafbbac2d32205b63a6bf561fb0a69c38a54bb), [`8becf8e`](https://github.com/aomex/aomex/commit/8becf8ee5ef86a5909783d0654e536db7be9bf5b), [`0776719`](https://github.com/aomex/aomex/commit/077671963401f1dafb5b03722899452d45df13fc), [`f996bf7`](https://github.com/aomex/aomex/commit/f996bf7e529e7751a5e858c579feed33f5f02d65)]:
16
- - @aomex/core@0.0.27
17
- - @aomex/web@0.0.28
18
-
19
- ## 0.0.25
20
-
21
- ### Patch Changes
22
-
23
- - Updated dependencies [[`33c3c61`](https://github.com/aomex/aomex/commit/33c3c614be9f66077f7487350b88d2db854f5fc8), [`fbcea3d`](https://github.com/aomex/aomex/commit/fbcea3d68ff033e6861130c645c8e5ad7336193f)]:
24
- - @aomex/web@0.0.27
25
- - @aomex/core@0.0.26
26
-
27
- ## 0.0.24
28
-
29
- ### Patch Changes
30
-
31
- - Updated dependencies [[`5ef2022`](https://github.com/aomex/aomex/commit/5ef202248b4320ff9076fbecb54379055cffb7db), [`8d9e9d8`](https://github.com/aomex/aomex/commit/8d9e9d8ece0bbcc7e1ac685702eacbfdfa145aa9), [`0e6ed2c`](https://github.com/aomex/aomex/commit/0e6ed2c611100dcfaafb6fb41357624ad9f5c67a)]:
32
- - @aomex/web@0.0.26
33
- - @aomex/core@0.0.25
34
-
35
- ## 0.0.23
36
-
37
- ### Patch Changes
38
-
39
- - Updated dependencies [[`2ac62fd`](https://github.com/aomex/aomex/commit/2ac62fd28166a1d9dd60b3c6d5a6508a6f9ee82b), [`4258410`](https://github.com/aomex/aomex/commit/42584107ad9f7e34492ae1053fef83aa2d9d747a), [`4177cba`](https://github.com/aomex/aomex/commit/4177cba7877e38120842bd8d287eaed54e4926ca)]:
40
- - @aomex/core@0.0.24
41
- - @aomex/web@0.0.25
42
-
43
- ## 0.0.22
44
-
45
- ### Patch Changes
5
+ ## 1.0.0
46
6
 
47
- - Updated dependencies [[`6621e4f`](https://github.com/aomex/aomex/commit/6621e4ff0f3509beeb332a0571a7db9c7d6ca99a), [`7b09277`](https://github.com/aomex/aomex/commit/7b09277136910966f500c8132303c7ddee84340c), [`46c5b72`](https://github.com/aomex/aomex/commit/46c5b72785011fa181767f4c8ea0d0f5008b21ae), [`9c78999`](https://github.com/aomex/aomex/commit/9c78999ebcb2962f30344acfbf6de0733d6fdd41), [`f4b012d`](https://github.com/aomex/aomex/commit/f4b012d98cddb2918479ea05df6c266dd914e53a)]:
48
- - @aomex/web@0.0.24
49
- - @aomex/core@0.0.23
7
+ ### Minor Changes
50
8
 
51
- ## 0.0.21
9
+ - [`11103a2`](https://github.com/aomex/aomex/commit/11103a2aff4e081754c56b0ff18fa5130ca252e8) Thanks [@geekact](https://github.com/geekact)! - 重新制作
52
10
 
53
11
  ### Patch Changes
54
12
 
55
- - Updated dependencies [[`6f7d706`](https://github.com/aomex/aomex/commit/6f7d7066c23711abdd149eb1c9a293ab8c4284a4), [`e21007a`](https://github.com/aomex/aomex/commit/e21007a82cb8eac73e1f696340bbe986d57dc159), [`e7bf93c`](https://github.com/aomex/aomex/commit/e7bf93cee6896c61d0bf3eb0921151dc6c1bc107), [`e21007a`](https://github.com/aomex/aomex/commit/e21007a82cb8eac73e1f696340bbe986d57dc159), [`818e840`](https://github.com/aomex/aomex/commit/818e840d36c7456a863fc071968b246c123c17f5), [`fb6c72d`](https://github.com/aomex/aomex/commit/fb6c72dbb266be4db92a542afe93dfa5d8c7cd41)]:
56
- - @aomex/core@0.0.22
57
- - @aomex/web@0.0.23
13
+ - Updated dependencies [[`11103a2`](https://github.com/aomex/aomex/commit/11103a2aff4e081754c56b0ff18fa5130ca252e8)]:
14
+ - @aomex/core@1.0.0
15
+ - @aomex/web@1.0.0
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2023 geekact
3
+ Copyright (c) 2023-2024 geekact
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/dist/index.d.ts CHANGED
@@ -1,6 +1,13 @@
1
- import calculate from 'etag';
2
1
  import { WebMiddleware } from '@aomex/web';
3
2
 
4
- declare const etag: (options?: calculate.Options) => WebMiddleware;
3
+ interface EtagOptions {
4
+ /**
5
+ * 是否增加 `W/` 前缀。默认:`false`
6
+ *
7
+ * 如果是ReadStream,则默认开启。
8
+ */
9
+ weak?: boolean | undefined;
10
+ }
11
+ declare const etag: (options?: EtagOptions) => WebMiddleware;
5
12
 
6
- export { etag };
13
+ export { type EtagOptions, etag };
package/dist/index.js CHANGED
@@ -3,29 +3,33 @@ import { ReadStream } from "node:fs";
3
3
  import { stat } from "node:fs/promises";
4
4
  import calculate from "etag";
5
5
  import { middleware } from "@aomex/core";
6
+ import { Stream } from "node:stream";
6
7
  var etag = (options = {}) => {
7
8
  return middleware.web(async (ctx, next) => {
8
9
  await next();
9
10
  const { response, request } = ctx;
10
- const { body } = response;
11
- if (body === null || response.hasHeader("Etag"))
11
+ const { method } = request;
12
+ const { body, statusCode } = response;
13
+ if (statusCode < 200 || statusCode > 299)
14
+ return;
15
+ if (method !== "GET" && method !== "HEAD")
12
16
  return;
13
- if (response.statusCode < 200 || response.statusCode > 299)
17
+ if (body === null || response.hasHeader("Etag"))
14
18
  return;
15
19
  let entity;
16
- if (body instanceof ReadStream) {
17
- entity = await stat(body.path);
20
+ if (body instanceof Stream) {
21
+ if (body instanceof ReadStream) {
22
+ entity = await stat(body.path);
23
+ }
18
24
  } else if (typeof body === "string" || Buffer.isBuffer(body)) {
19
25
  entity = body;
20
26
  } else {
21
27
  entity = JSON.stringify(body);
22
28
  }
23
- if (entity) {
24
- response.setHeader("Etag", calculate(entity, options));
25
- }
26
- if (request.fresh) {
27
- ctx.send(304, null);
28
- }
29
+ if (!entity)
30
+ return;
31
+ response.setHeader("Etag", calculate(entity, options));
32
+ request.fresh && ctx.send(304, null);
29
33
  });
30
34
  };
31
35
  export {
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { ReadStream } from 'node:fs';\nimport { stat } from 'node:fs/promises';\nimport calculate from 'etag';\nimport { middleware } from '@aomex/core';\nimport type { WebMiddleware } from '@aomex/web';\n\nexport const etag = (options: calculate.Options = {}): WebMiddleware => {\n return middleware.web(async (ctx, next) => {\n await next();\n\n const { response, request } = ctx;\n const { body } = response;\n\n if (body === null || response.hasHeader('Etag')) return;\n if (response.statusCode < 200 || response.statusCode > 299) return;\n\n let entity: Parameters<typeof calculate>[0];\n if (body instanceof ReadStream) {\n entity = await stat(body.path);\n } else if (typeof body === 'string' || Buffer.isBuffer(body)) {\n entity = body;\n } else {\n entity = JSON.stringify(body);\n }\n\n if (entity) {\n response.setHeader('Etag', calculate(entity, options));\n }\n\n if (request.fresh) {\n ctx.send(304, null);\n }\n });\n};\n"],"mappings":";AAAA,SAAS,kBAAkB;AAC3B,SAAS,YAAY;AACrB,OAAO,eAAe;AACtB,SAAS,kBAAkB;AAGpB,IAAM,OAAO,CAAC,UAA6B,CAAC,MAAqB;AACtE,SAAO,WAAW,IAAI,OAAO,KAAK,SAAS;AACzC,UAAM,KAAK;AAEX,UAAM,EAAE,UAAU,QAAQ,IAAI;AAC9B,UAAM,EAAE,KAAK,IAAI;AAEjB,QAAI,SAAS,QAAQ,SAAS,UAAU,MAAM;AAAG;AACjD,QAAI,SAAS,aAAa,OAAO,SAAS,aAAa;AAAK;AAE5D,QAAI;AACJ,QAAI,gBAAgB,YAAY;AAC9B,eAAS,MAAM,KAAK,KAAK,IAAI;AAAA,IAC/B,WAAW,OAAO,SAAS,YAAY,OAAO,SAAS,IAAI,GAAG;AAC5D,eAAS;AAAA,IACX,OAAO;AACL,eAAS,KAAK,UAAU,IAAI;AAAA,IAC9B;AAEA,QAAI,QAAQ;AACV,eAAS,UAAU,QAAQ,UAAU,QAAQ,OAAO,CAAC;AAAA,IACvD;AAEA,QAAI,QAAQ,OAAO;AACjB,UAAI,KAAK,KAAK,IAAI;AAAA,IACpB;AAAA,EACF,CAAC;AACH;","names":[]}
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { ReadStream } from 'node:fs';\nimport { stat } from 'node:fs/promises';\nimport calculate from 'etag';\nimport { middleware } from '@aomex/core';\nimport type { WebMiddleware } from '@aomex/web';\nimport { Stream } from 'node:stream';\n\nexport interface EtagOptions {\n /**\n * 是否增加 `W/` 前缀。默认:`false`\n *\n * 如果是ReadStream,则默认开启。\n */\n weak?: boolean | undefined;\n}\n\nexport const etag = (options: EtagOptions = {}): WebMiddleware => {\n return middleware.web(async (ctx, next) => {\n await next();\n\n const { response, request } = ctx;\n const { method } = request;\n const { body, statusCode } = response;\n\n if (statusCode < 200 || statusCode > 299) return;\n if (method !== 'GET' && method !== 'HEAD') return;\n if (body === null || response.hasHeader('Etag')) return;\n\n let entity: Parameters<typeof calculate>[0] | undefined;\n if (body instanceof Stream) {\n if (body instanceof ReadStream) {\n entity = await stat(body.path);\n }\n } else if (typeof body === 'string' || Buffer.isBuffer(body)) {\n entity = body;\n } else {\n entity = JSON.stringify(body);\n }\n\n if (!entity) return;\n\n response.setHeader('Etag', calculate(entity, options));\n request.fresh && ctx.send(304, null);\n });\n};\n"],"mappings":";AAAA,SAAS,kBAAkB;AAC3B,SAAS,YAAY;AACrB,OAAO,eAAe;AACtB,SAAS,kBAAkB;AAE3B,SAAS,cAAc;AAWhB,IAAM,OAAO,CAAC,UAAuB,CAAC,MAAqB;AAChE,SAAO,WAAW,IAAI,OAAO,KAAK,SAAS;AACzC,UAAM,KAAK;AAEX,UAAM,EAAE,UAAU,QAAQ,IAAI;AAC9B,UAAM,EAAE,OAAO,IAAI;AACnB,UAAM,EAAE,MAAM,WAAW,IAAI;AAE7B,QAAI,aAAa,OAAO,aAAa;AAAK;AAC1C,QAAI,WAAW,SAAS,WAAW;AAAQ;AAC3C,QAAI,SAAS,QAAQ,SAAS,UAAU,MAAM;AAAG;AAEjD,QAAI;AACJ,QAAI,gBAAgB,QAAQ;AAC1B,UAAI,gBAAgB,YAAY;AAC9B,iBAAS,MAAM,KAAK,KAAK,IAAI;AAAA,MAC/B;AAAA,IACF,WAAW,OAAO,SAAS,YAAY,OAAO,SAAS,IAAI,GAAG;AAC5D,eAAS;AAAA,IACX,OAAO;AACL,eAAS,KAAK,UAAU,IAAI;AAAA,IAC9B;AAEA,QAAI,CAAC;AAAQ;AAEb,aAAS,UAAU,QAAQ,UAAU,QAAQ,OAAO,CAAC;AACrD,YAAQ,SAAS,IAAI,KAAK,KAAK,IAAI;AAAA,EACrC,CAAC;AACH;","names":[]}
package/package.json CHANGED
@@ -1,10 +1,7 @@
1
1
  {
2
2
  "name": "@aomex/etag",
3
- "version": "0.0.27",
4
- "description": "response token",
5
- "keywords": [
6
- "etag"
7
- ],
3
+ "version": "1.0.1",
4
+ "description": "响应实体标签",
8
5
  "repository": "git@github.com:aomex/aomex.git",
9
6
  "homepage": "https://aomex.js.org",
10
7
  "license": "MIT",
@@ -29,18 +26,16 @@
29
26
  "access": "public"
30
27
  },
31
28
  "peerDependencies": {
32
- "@aomex/core": "^0.0.28",
33
- "@aomex/web": "^0.0.29"
29
+ "@aomex/core": "^1.0.0",
30
+ "@aomex/web": "^1.0.0"
34
31
  },
35
32
  "dependencies": {
36
33
  "etag": "^1.8.1"
37
34
  },
38
35
  "devDependencies": {
39
- "@aomex/core": "^0.0.28",
40
- "@aomex/web": "^0.0.29",
41
- "@types/etag": "^1.8.1"
36
+ "@types/etag": "^1.8.1",
37
+ "@aomex/core": "^1.0.1",
38
+ "@aomex/web": "^1.0.1"
42
39
  },
43
- "scripts": {
44
- "test": "vitest"
45
- }
40
+ "scripts": {}
46
41
  }