@aomex/etag 3.30.7 → 3.30.8-alpha.3d94097.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/dist/index.d.ts +9 -12
- package/dist/index.js +24 -30
- package/dist/index.js.map +1 -1
- package/package.json +6 -6
package/dist/index.d.ts
CHANGED
|
@@ -1,13 +1,10 @@
|
|
|
1
|
-
import { WebMiddleware } from
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
weak?: boolean | undefined;
|
|
1
|
+
import { WebMiddleware } from "@aomex/web";
|
|
2
|
+
export interface EtagOptions {
|
|
3
|
+
/**
|
|
4
|
+
* 是否增加 `W/` 前缀,即弱校验。默认:`false`
|
|
5
|
+
*
|
|
6
|
+
* 如果是ReadStream,则默认开启。
|
|
7
|
+
*/
|
|
8
|
+
weak?: boolean | undefined;
|
|
10
9
|
}
|
|
11
|
-
declare const etag: (options?: EtagOptions) => WebMiddleware;
|
|
12
|
-
|
|
13
|
-
export { type EtagOptions, etag };
|
|
10
|
+
export declare const etag: (options?: EtagOptions) => WebMiddleware;
|
package/dist/index.js
CHANGED
|
@@ -1,34 +1,28 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
import { stat } from "fs/promises";
|
|
1
|
+
import { ReadStream } from "node:fs";
|
|
2
|
+
import { stat } from "node:fs/promises";
|
|
4
3
|
import calculate from "etag";
|
|
5
4
|
import { middleware } from "@aomex/common";
|
|
6
|
-
import { Stream } from "stream";
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
if (!entity) return;
|
|
27
|
-
response.setHeader("Etag", calculate(entity, options));
|
|
28
|
-
request.fresh && ctx.send(304, null);
|
|
29
|
-
});
|
|
30
|
-
};
|
|
31
|
-
export {
|
|
32
|
-
etag
|
|
5
|
+
import { Stream } from "node:stream";
|
|
6
|
+
// src/index.ts
|
|
7
|
+
const etag = (options = {}) => {
|
|
8
|
+
return middleware.web(async (ctx, next) => {
|
|
9
|
+
await next();
|
|
10
|
+
const { response, request } = ctx;
|
|
11
|
+
const { method } = request;
|
|
12
|
+
const { body, statusCode } = response;
|
|
13
|
+
if (statusCode < 200 || statusCode > 299) return;
|
|
14
|
+
if (method !== "GET" && method !== "HEAD") return;
|
|
15
|
+
if (body === null || response.hasHeader("Etag")) return;
|
|
16
|
+
let entity;
|
|
17
|
+
if (body instanceof Stream) {
|
|
18
|
+
if (body instanceof ReadStream) entity = await stat(body.path);
|
|
19
|
+
} else if (typeof body === "string" || Buffer.isBuffer(body)) entity = body;
|
|
20
|
+
else entity = JSON.stringify(body);
|
|
21
|
+
if (!entity) return;
|
|
22
|
+
response.setHeader("Etag", calculate(entity, options));
|
|
23
|
+
request.fresh && ctx.send(304, null);
|
|
24
|
+
});
|
|
33
25
|
};
|
|
26
|
+
export { etag };
|
|
27
|
+
|
|
34
28
|
//# sourceMappingURL=index.js.map
|
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/common';\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":"
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/index.ts"],"sourcesContent":["import { ReadStream } from 'node:fs';\nimport { stat } from 'node:fs/promises';\nimport calculate from 'etag';\nimport { middleware } from '@aomex/common';\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":";;;;;;AAgBA,MAAa,QAAQ,UAAuB,CAAC,MAAqB;CAChE,OAAO,WAAW,IAAI,OAAO,KAAK,SAAS;EACzC,MAAM,KAAK;EAEX,MAAM,EAAE,UAAU,YAAY;EAC9B,MAAM,EAAE,WAAW;EACnB,MAAM,EAAE,MAAM,eAAe;EAE7B,IAAI,aAAa,OAAO,aAAa,KAAK;EAC1C,IAAI,WAAW,SAAS,WAAW,QAAQ;EAC3C,IAAI,SAAS,QAAQ,SAAS,UAAU,MAAM,GAAG;EAEjD,IAAI;EACJ,IAAI,gBAAgB,QACd;OAAA,gBAAgB,YAClB,SAAS,MAAM,KAAK,KAAK,IAAI;EAAA,OAE1B,IAAI,OAAO,SAAS,YAAY,OAAO,SAAS,IAAI,GACzD,SAAS;OAET,SAAS,KAAK,UAAU,IAAI;EAG9B,IAAI,CAAC,QAAQ;EAEb,SAAS,UAAU,QAAQ,UAAU,QAAQ,OAAO,CAAC;EACrD,QAAQ,SAAS,IAAI,KAAK,KAAK,IAAI;CACrC,CAAC;AACH"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aomex/etag",
|
|
3
|
-
"version": "3.30.
|
|
3
|
+
"version": "3.30.8-alpha.3d94097.0",
|
|
4
4
|
"description": "响应实体标签",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -29,16 +29,16 @@
|
|
|
29
29
|
"access": "public"
|
|
30
30
|
},
|
|
31
31
|
"peerDependencies": {
|
|
32
|
-
"@aomex/common": "^
|
|
33
|
-
"@aomex/web": "^
|
|
32
|
+
"@aomex/common": "^4.0.0",
|
|
33
|
+
"@aomex/web": "^4.0.0"
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {
|
|
36
36
|
"etag": "^1.8.1"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
|
-
"@aomex/common": "3.30.
|
|
40
|
-
"@aomex/web": "3.30.
|
|
41
|
-
"@types/etag": "^1.8.
|
|
39
|
+
"@aomex/common": "3.30.8-alpha.3d94097.0",
|
|
40
|
+
"@aomex/web": "3.30.8-alpha.3d94097.0",
|
|
41
|
+
"@types/etag": "^1.8.4"
|
|
42
42
|
},
|
|
43
43
|
"scripts": {}
|
|
44
44
|
}
|