@ureq/lib-hash 0.0.3 → 0.0.4
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/README.md +37 -43
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -15,37 +15,41 @@ pnpm add @ureq/lib-hash
|
|
|
15
15
|
## 使用
|
|
16
16
|
|
|
17
17
|
```typescript
|
|
18
|
-
import {
|
|
18
|
+
import { generateHash, generateRequestHash } from '@ureq/lib-hash';
|
|
19
19
|
|
|
20
|
-
|
|
20
|
+
// 生成字符串哈希
|
|
21
|
+
const hash = generateHash('some-string');
|
|
21
22
|
|
|
22
23
|
// 生成请求哈希
|
|
23
|
-
const requestHash =
|
|
24
|
+
const requestHash = generateRequestHash(
|
|
24
25
|
'GET',
|
|
25
26
|
'/api/users',
|
|
26
27
|
null,
|
|
27
28
|
{ params: { page: 1 } }
|
|
28
29
|
);
|
|
29
|
-
|
|
30
|
-
// 生成字符串哈希
|
|
31
|
-
const strHash = hashService.hashString('some-string');
|
|
32
30
|
```
|
|
33
31
|
|
|
34
32
|
## API
|
|
35
33
|
|
|
36
|
-
###
|
|
34
|
+
### generateHash
|
|
35
|
+
|
|
36
|
+
生成字符串的哈希值。
|
|
37
|
+
|
|
38
|
+
```typescript
|
|
39
|
+
function generateHash(input: string): string
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### generateRequestHash
|
|
43
|
+
|
|
44
|
+
生成请求的唯一哈希值。
|
|
37
45
|
|
|
38
46
|
```typescript
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
): string;
|
|
46
|
-
|
|
47
|
-
hashString(str: string): string;
|
|
48
|
-
}
|
|
47
|
+
function generateRequestHash(
|
|
48
|
+
method: string,
|
|
49
|
+
url: string,
|
|
50
|
+
data?: any,
|
|
51
|
+
options?: Record<string, any>
|
|
52
|
+
): string
|
|
49
53
|
```
|
|
50
54
|
|
|
51
55
|
## 功能
|
|
@@ -59,40 +63,28 @@ class DefaultHashService implements HashService {
|
|
|
59
63
|
- 请求标识
|
|
60
64
|
|
|
61
65
|
```typescript
|
|
62
|
-
|
|
63
|
-
|
|
66
|
+
import { generateRequestHash } from '@ureq/lib-hash';
|
|
67
|
+
|
|
68
|
+
const hash1 = generateRequestHash('GET', '/users', null, { params: { page: 1 } });
|
|
69
|
+
const hash2 = generateRequestHash('GET', '/users', null, { params: { page: 1 } });
|
|
64
70
|
|
|
65
71
|
console.log(hash1 === hash2); // true - 相同的请求生成相同的哈希
|
|
66
72
|
```
|
|
67
73
|
|
|
68
|
-
###
|
|
74
|
+
### generateHash
|
|
69
75
|
|
|
70
76
|
生成字符串的哈希值。
|
|
71
77
|
|
|
72
78
|
```typescript
|
|
73
|
-
|
|
79
|
+
import { generateHash } from '@ureq/lib-hash';
|
|
80
|
+
|
|
81
|
+
const hash = generateHash('hello world');
|
|
74
82
|
console.log(hash); // 生成的哈希值
|
|
75
83
|
```
|
|
76
84
|
|
|
77
85
|
## 哈希算法
|
|
78
86
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
```typescript
|
|
82
|
-
import { HashService } from '@ureq/business';
|
|
83
|
-
import crypto from 'crypto';
|
|
84
|
-
|
|
85
|
-
class CryptoHashService implements HashService {
|
|
86
|
-
generateRequestHash(method: string, url: string, data?: any, options?: any): string {
|
|
87
|
-
const str = JSON.stringify({ method, url, data, options });
|
|
88
|
-
return this.hashString(str);
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
hashString(str: string): string {
|
|
92
|
-
return crypto.createHash('sha256').update(str).digest('hex');
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
```
|
|
87
|
+
使用简单高效的字符串哈希算法,将输入转换为 32 位整数,然后转换为 36 进制字符串。适用于大多数场景。
|
|
96
88
|
|
|
97
89
|
## 使用场景
|
|
98
90
|
|
|
@@ -101,15 +93,15 @@ class CryptoHashService implements HashService {
|
|
|
101
93
|
```typescript
|
|
102
94
|
import { Request } from '@ureq/core';
|
|
103
95
|
import { FetchRequestor } from '@ureq/impl-fetch';
|
|
104
|
-
import {
|
|
105
|
-
|
|
106
|
-
const hashService = new DefaultHashService();
|
|
96
|
+
import { generateRequestHash } from '@ureq/lib-hash';
|
|
107
97
|
|
|
108
98
|
const request = new Request(
|
|
109
99
|
new FetchRequestor(),
|
|
110
100
|
{
|
|
111
101
|
idempotent: {
|
|
112
|
-
|
|
102
|
+
getRequestId: (method, url, data, options) => {
|
|
103
|
+
return generateRequestHash(method, url, data, options);
|
|
104
|
+
},
|
|
113
105
|
dedupeTime: 1000
|
|
114
106
|
}
|
|
115
107
|
}
|
|
@@ -119,12 +111,14 @@ const request = new Request(
|
|
|
119
111
|
### 缓存键生成
|
|
120
112
|
|
|
121
113
|
```typescript
|
|
114
|
+
import { generateRequestHash } from '@ureq/lib-hash';
|
|
115
|
+
|
|
122
116
|
const request = new Request(
|
|
123
117
|
new FetchRequestor(),
|
|
124
118
|
{
|
|
125
119
|
cache: {
|
|
126
120
|
getCacheKey: (url, options) => {
|
|
127
|
-
return
|
|
121
|
+
return generateRequestHash('GET', url, null, options);
|
|
128
122
|
}
|
|
129
123
|
}
|
|
130
124
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ureq/lib-hash",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4",
|
|
4
4
|
"description": "Hash utilities for universal request library",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
@@ -18,6 +18,6 @@
|
|
|
18
18
|
"scripts": {
|
|
19
19
|
"build": "tsup --config tsup.config.ts",
|
|
20
20
|
"dev": "tsup --config tsup.config.ts --watch",
|
|
21
|
-
"test": "vitest"
|
|
21
|
+
"test": "vitest --run"
|
|
22
22
|
}
|
|
23
23
|
}
|