dsh-config-manager 0.3.0 → 0.3.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/lib/client.js +528 -528
- package/lib/index.d.ts +1 -1
- package/lib/index.js +1 -1
- package/lib/sync/s3/oss4-verify.js +21 -6
- package/lib/sync/s3/s3-providers.d.ts +13 -0
- package/lib/sync/s3/s3-providers.js +6 -0
- package/lib/sync/s3/s3-transport.d.ts +22 -0
- package/lib/sync/s3/s3-transport.js +89 -18
- package/package.json +1 -1
- package/src/index.ts +1 -1
- package/src/sync/s3/oss4-verify.ts +22 -6
- package/src/sync/s3/s3-conditional-write.test.ts +153 -3
- package/src/sync/s3/s3-providers.ts +20 -0
- package/src/sync/s3/s3-transport.oss4-server.test.ts +48 -0
- package/src/sync/s3/s3-transport.ts +98 -17
package/lib/index.d.ts
CHANGED
|
@@ -77,7 +77,7 @@ export declare const name = "config-manager";
|
|
|
77
77
|
/** Services required before the engine can mount (present in every profile). */
|
|
78
78
|
export declare const inject: string[];
|
|
79
79
|
/** Plugin version, kept in sync with package.json ("version"). */
|
|
80
|
-
export declare const PLUGIN_VERSION = "0.3.
|
|
80
|
+
export declare const PLUGIN_VERSION = "0.3.1";
|
|
81
81
|
/**
|
|
82
82
|
* Star 引导弹窗指向的 GitHub 仓库(用户引导点 Star 的目标)。
|
|
83
83
|
* 与 package.json 的 repository 字段保持一致;界面不可改(硬编码,参照
|
package/lib/index.js
CHANGED
|
@@ -137,7 +137,7 @@ export const name = 'config-manager';
|
|
|
137
137
|
/** Services required before the engine can mount (present in every profile). */
|
|
138
138
|
export const inject = ['settings', 'credentials'];
|
|
139
139
|
/** Plugin version, kept in sync with package.json ("version"). */
|
|
140
|
-
export const PLUGIN_VERSION = '0.3.
|
|
140
|
+
export const PLUGIN_VERSION = '0.3.1';
|
|
141
141
|
/** Plugin own package name — excluded from its own exported plugins list. */
|
|
142
142
|
const PLUGIN_NAME = 'dsh-config-manager';
|
|
143
143
|
/**
|
|
@@ -7,6 +7,10 @@
|
|
|
7
7
|
* `getResourceV4`、`oss/auth.go` 的 `getSubResource` / `getSignedStrV4`)**独立重写**一遍,
|
|
8
8
|
* 只吃「收到的 HTTP 请求」(method / url / 头 / body),绝不复用客户端的签名中间量。
|
|
9
9
|
*
|
|
10
|
+
* 也照真服务端实现**条件写能力**:PutObject 收到 If-Match / If-None-Match 一律拒(0017-00000245),
|
|
11
|
+
* 只接受 `x-oss-forbid-overwrite: true`(已存在 → 409 FileAlreadyExists)—— 这一层不照真服务端写,
|
|
12
|
+
* 「本地全绿、真机 400」的盲点就会回来(issue #109 第二层)。
|
|
13
|
+
*
|
|
10
14
|
* 用法:`s3-transport.oss4-server.test.ts`(内存对象存储,逐请求验签)与
|
|
11
15
|
* `scripts/oss-live-check.mjs`(真桶联调)。它证明的是「与本仓库对官方算法的独立复现一致」,
|
|
12
16
|
* 不等于官方服务端盖章 —— 真桶连通性要靠带真实密钥跑一次脚本。
|
|
@@ -96,6 +100,10 @@ export function verifyOssV4(init, cfg) {
|
|
|
96
100
|
}
|
|
97
101
|
return { ok: true, reason: '', canonicalRequest, stringToSign, signature, objectKey, bucketLevel };
|
|
98
102
|
}
|
|
103
|
+
/** 真 OSS 会拒绝的条件写头(PutObject 不支持;错误码 0017-00000245)。 */
|
|
104
|
+
const IF_CONDITIONAL_HEADERS = ['if-match', 'if-none-match'];
|
|
105
|
+
/** OSS 回显头名时用的原始大小写(真机报文里看到的是 `If-None-Match`)。 */
|
|
106
|
+
const IF_HEADER_ECHO = { 'if-match': 'If-Match', 'if-none-match': 'If-None-Match' };
|
|
99
107
|
/**
|
|
100
108
|
* 内存 OSS(验签通过才落盘/回读):返回可直接注入 S3Transport 的 `request`,
|
|
101
109
|
* 以及验签成功/失败流水,供测试与联调脚本断言。
|
|
@@ -127,13 +135,20 @@ export function makeOssVerifyServer(cfg) {
|
|
|
127
135
|
const h = lower(headers);
|
|
128
136
|
const existing = objects.get(key);
|
|
129
137
|
if (method === 'PUT') {
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
138
|
+
// 真 OSS 的 PutObject **不支持 If-Match / If-None-Match**:收到就回
|
|
139
|
+
// 400 InvalidArgument「A header you provided implies functionality that is not implemented.」
|
|
140
|
+
// (错误码 0017-00000245,issue #109 第二层)。这里必须照真服务端拒掉 —— 之前这个验签器
|
|
141
|
+
// 自己实现了 if-none-match,等于用自家规则验自家人,于是「14 个请求零失败」漏掉了这一层。
|
|
142
|
+
const unsupported = IF_CONDITIONAL_HEADERS.find((n) => h[n] !== undefined);
|
|
143
|
+
if (unsupported !== undefined) {
|
|
144
|
+
return respond(400, '<Error><Code>InvalidArgument</Code><Message>A header you provided implies '
|
|
145
|
+
+ 'functionality that is not implemented.</Message><Header>' + IF_HEADER_ECHO[unsupported]
|
|
146
|
+
+ '</Header><EC>0017-00000245</EC></Error>');
|
|
134
147
|
}
|
|
135
|
-
|
|
136
|
-
|
|
148
|
+
// OSS 唯一支持的「禁止覆盖」:对象已存在即 409 FileAlreadyExists。
|
|
149
|
+
if ((h['x-oss-forbid-overwrite'] ?? '').trim().toLowerCase() === 'true' && existing !== undefined) {
|
|
150
|
+
return respond(409, '<Error><Code>FileAlreadyExists</Code><Message>The object you specified already '
|
|
151
|
+
+ 'exists and cannot be overwritten again.</Message></Error>');
|
|
137
152
|
}
|
|
138
153
|
const body = opts.body ?? '';
|
|
139
154
|
const etag = sha256(body).slice(0, 32);
|
|
@@ -6,6 +6,15 @@ export declare const S3_DEFAULT_PREFIX = "dsh-config-manager";
|
|
|
6
6
|
export declare const S3_INDEX_FILE = "index.json";
|
|
7
7
|
export declare const S3_BLOBS_SEG = "blobs";
|
|
8
8
|
export declare const S3_BLOBS_INDEX_FILE = "blobs-index.json";
|
|
9
|
+
/**
|
|
10
|
+
* 条件写能力(决定「防两台设备同时写」怎么落地):
|
|
11
|
+
* - `etag`:AWS 风格 If-Match / If-None-Match(s3 / minio / cos / kodo);
|
|
12
|
+
* - `forbid-overwrite`:阿里云 OSS —— PutObject **不支持 If-\***(真机会回 400
|
|
13
|
+
* InvalidArgument「A header you provided implies functionality that is not implemented.」,
|
|
14
|
+
* 错误码 0017-00000245;issue #109 第二层),只有 `x-oss-forbid-overwrite: true`
|
|
15
|
+
* 「对象已存在即拒」这一种禁止覆盖语义。
|
|
16
|
+
*/
|
|
17
|
+
export type S3ConditionalWrite = 'etag' | 'forbid-overwrite';
|
|
9
18
|
export interface S3ProviderVariant {
|
|
10
19
|
id: S3CompatProvider;
|
|
11
20
|
/** 默认签名方言(可被 \`dialectId\` 覆盖) */
|
|
@@ -18,6 +27,8 @@ export interface S3ProviderVariant {
|
|
|
18
27
|
endpointTemplate?: string;
|
|
19
28
|
/** 默认寻址风格:true = path-style(<endpoint>/<bucket>/<key>),false = virtual-host(<bucket>.<endpoint>/<key>) */
|
|
20
29
|
defaultPathStyle: boolean;
|
|
30
|
+
/** 条件写能力(见 S3ConditionalWrite;与签名方言解耦,跟着服务商走而不是跟着 dialectId 走) */
|
|
31
|
+
conditionalWrite: S3ConditionalWrite;
|
|
21
32
|
/**
|
|
22
33
|
* 变体说明的**目录键**(自 t6 起字段是键而不是文案 —— 中文只存在于 `sync/s3/messages.ts` 的本地目录,
|
|
23
34
|
* 经 `s3Msg` / `composeMsg(host, s3Msg)` 解析成当前语言;本字段本身不渲染)。
|
|
@@ -46,6 +57,8 @@ export interface S3Target {
|
|
|
46
57
|
prefix: string;
|
|
47
58
|
pathStyle: boolean;
|
|
48
59
|
dialect: SigV4Dialect;
|
|
60
|
+
/** 条件写能力(来自变体表;dialectId 覆盖不影响它) */
|
|
61
|
+
conditionalWrite: S3ConditionalWrite;
|
|
49
62
|
}
|
|
50
63
|
export interface S3TargetInput {
|
|
51
64
|
provider: S3CompatProvider;
|
|
@@ -31,25 +31,30 @@ export const S3_PROVIDER_VARIANTS = {
|
|
|
31
31
|
s3: {
|
|
32
32
|
id: 's3', dialectId: 'aws4', service: 's3', defaultRegion: 'us-east-1',
|
|
33
33
|
endpointTemplate: 'https://s3.{region}.amazonaws.com', defaultPathStyle: false,
|
|
34
|
+
conditionalWrite: 'etag',
|
|
34
35
|
note: 'sync.s3.providerNoteS3',
|
|
35
36
|
},
|
|
36
37
|
oss: {
|
|
37
38
|
id: 'oss', dialectId: 'oss4', service: 'oss',
|
|
38
39
|
endpointTemplate: 'https://oss-{region}.aliyuncs.com', defaultPathStyle: false,
|
|
40
|
+
conditionalWrite: 'forbid-overwrite',
|
|
39
41
|
note: 'sync.s3.providerNoteOss',
|
|
40
42
|
},
|
|
41
43
|
cos: {
|
|
42
44
|
id: 'cos', dialectId: 'aws4', service: 's3',
|
|
43
45
|
endpointTemplate: 'https://cos.{region}.myqcloud.com', defaultPathStyle: false,
|
|
46
|
+
conditionalWrite: 'etag',
|
|
44
47
|
note: 'sync.s3.providerNoteCos',
|
|
45
48
|
},
|
|
46
49
|
minio: {
|
|
47
50
|
id: 'minio', dialectId: 'aws4', service: 's3', defaultRegion: 'us-east-1', defaultPathStyle: true,
|
|
51
|
+
conditionalWrite: 'etag',
|
|
48
52
|
note: 'sync.s3.providerNoteMinio',
|
|
49
53
|
},
|
|
50
54
|
kodo: {
|
|
51
55
|
id: 'kodo', dialectId: 'aws4', service: 's3',
|
|
52
56
|
endpointTemplate: 'https://s3-{region}.qiniucs.com', defaultPathStyle: false,
|
|
57
|
+
conditionalWrite: 'etag',
|
|
53
58
|
note: 'sync.s3.providerNoteKodo',
|
|
54
59
|
},
|
|
55
60
|
};
|
|
@@ -119,6 +124,7 @@ export function resolveS3Target(input) {
|
|
|
119
124
|
prefix,
|
|
120
125
|
pathStyle: input.pathStyle ?? variant.defaultPathStyle,
|
|
121
126
|
dialect: sigV4Dialect(input.dialectId ?? variant.dialectId),
|
|
127
|
+
conditionalWrite: variant.conditionalWrite,
|
|
122
128
|
},
|
|
123
129
|
};
|
|
124
130
|
}
|
|
@@ -84,6 +84,25 @@ export declare class S3Transport implements SyncTransport {
|
|
|
84
84
|
delete(id: string): Promise<void>;
|
|
85
85
|
/** 条件写失败:远端索引在本次读取之后被别人改过 —— 中止写入让用户重跑(cloud-sync 借鉴项 ①)。 */
|
|
86
86
|
private concurrentWrite;
|
|
87
|
+
/**
|
|
88
|
+
* OSS 方言的「ETag 条件写」替身(issue #109 第二层):PutObject 既无 If-Match 也无
|
|
89
|
+
* If-None-Match,`x-oss-forbid-overwrite` 只能表达「别覆盖已存在的对象」,表达不了
|
|
90
|
+
* 「远端必须还是我读到的那一版」。所以索引进已存在时改成**写前 HEAD 复核 ETag**:
|
|
91
|
+
* 变了 / 对象没了 → 按并发写中止(零写入)。
|
|
92
|
+
*
|
|
93
|
+
* **不是原子的**:复核与 PUT 之间仍有窗口(服务端不支持条件写就没有真正的解),
|
|
94
|
+
* 比无保护强、比 If-Match 弱 —— 如实登记在 docs/decisions/0003。
|
|
95
|
+
* 其它方言(etag)与「服务端不给 ETag」两种情况一律直接放行,不改既有行为。
|
|
96
|
+
*/
|
|
97
|
+
private reassertIndexEtag;
|
|
98
|
+
/**
|
|
99
|
+
* 条件写失败分类(issue #109 第二层复核):`isConcurrentWrite` 只看「状态码像不像」,
|
|
100
|
+
* 这里再问报文一句确认「对象确实已存在」。OSS 的 **409 不止 FileAlreadyExists** ——
|
|
101
|
+
* FileImmutable(Bucket 数据处于保护状态,删改都会被拒)同为 409,一律按并发写报
|
|
102
|
+
* 「别的设备改过索引」就是假话。报文读不出时仍按并发写处理(宁可让用户重跑一次,
|
|
103
|
+
* 不吞掉真冲突)。412 是 If-Match 语义,恒为并发写。
|
|
104
|
+
*/
|
|
105
|
+
private conditionalFailure;
|
|
87
106
|
/**
|
|
88
107
|
* 连通性探针(cloud-sync 借鉴项 ②):HEAD 桶证明端点/凭据/桶可达,再读一次索引回报快照数。
|
|
89
108
|
* 重定向(301/307/308)时读 x-amz-bucket-region 直接把「region 该填什么」告诉用户 ——
|
|
@@ -95,7 +114,10 @@ export declare class S3Transport implements SyncTransport {
|
|
|
95
114
|
private secretOnce;
|
|
96
115
|
/** 发送一次已签名请求;网络错误/超时归一为带分类的通道错误 */
|
|
97
116
|
private send;
|
|
117
|
+
/** 读错误响应体(未脱敏原文,截断到 ERR_BODY_MAX;读不出按空串) */
|
|
118
|
+
private errorBody;
|
|
98
119
|
/** 构造 HTTP 非 2xx 失败消息:附上脱敏后的响应体片段(截断) */
|
|
120
|
+
private failMessage;
|
|
99
121
|
private failText;
|
|
100
122
|
/** 识别超时错误(AbortError / TimeoutError) */
|
|
101
123
|
private isTimeout;
|
|
@@ -43,17 +43,32 @@ const REDACTED = '[REDACTED]';
|
|
|
43
43
|
/**
|
|
44
44
|
* 条件写头(cloud-sync 借鉴项 ①):有 ETag → If-Match(远端必须仍是我读到的那一版);
|
|
45
45
|
* 索引本来就不存在 → If-None-Match: *(防两设备同时首推);服务端不给 ETag → 空(无条件写,不改既有行为)。
|
|
46
|
+
*
|
|
47
|
+
* **阿里云 OSS 没有 If-\***(issue #109 第二层):PutObject 收到 If-Match / If-None-Match 一律回
|
|
48
|
+
* 400 InvalidArgument「A header you provided implies functionality that is not implemented.」
|
|
49
|
+
* (错误码 0017-00000245,真机报文里的 `Header: If-None-Match` 即此)。它只提供
|
|
50
|
+
* `x-oss-forbid-overwrite: true`(对象已存在即 409 FileAlreadyExists)。于是按变体的
|
|
51
|
+
* `conditionalWrite` 能力位映射:
|
|
52
|
+
* - 首推(索引本来就不存在)→ 语义完全等价,改用 x-oss-forbid-overwrite;
|
|
53
|
+
* - 索引已存在(拿到了 ETag)→ 没有对应头,退回「写前 HEAD 复核 ETag」
|
|
54
|
+
* (`assertIndexEtagUnchanged`;非原子,尽力而为)。
|
|
46
55
|
*/
|
|
47
|
-
function conditionalHeaders(etag, indexMissing) {
|
|
56
|
+
function conditionalHeaders(etag, indexMissing, mode) {
|
|
57
|
+
const guarded = mode === 'forbid-overwrite';
|
|
48
58
|
if (etag !== undefined)
|
|
49
|
-
return { 'if-match': etag };
|
|
50
|
-
|
|
59
|
+
return guarded ? {} : { 'if-match': etag };
|
|
60
|
+
if (!indexMissing)
|
|
61
|
+
return {};
|
|
62
|
+
return guarded ? { 'x-oss-forbid-overwrite': 'true' } : { 'if-none-match': '*' };
|
|
51
63
|
}
|
|
52
|
-
/**
|
|
53
|
-
|
|
64
|
+
/**
|
|
65
|
+
* 条件写失败判定:412 恒是;409 仅在**确实带了条件写头**时才算
|
|
66
|
+
* (无条件的 409 另有含义,不吞;条件写头的存在由调用方显式告知)。
|
|
67
|
+
*/
|
|
68
|
+
function isConcurrentWrite(status, guarded) {
|
|
54
69
|
if (status === 412)
|
|
55
70
|
return true;
|
|
56
|
-
return status === 409 &&
|
|
71
|
+
return status === 409 && guarded;
|
|
57
72
|
}
|
|
58
73
|
/** S3 通道错误:继承统一错误基类(kind / retryable / status 对上层可见)。 */
|
|
59
74
|
export class S3TransportError extends SyncTransportError {
|
|
@@ -210,14 +225,16 @@ export class S3Transport {
|
|
|
210
225
|
const merged = idxBefore.filter((m) => m.id !== snapshot.id);
|
|
211
226
|
merged.push(meta);
|
|
212
227
|
merged.sort((a, b) => (a.createdAt < b.createdAt ? -1 : a.createdAt > b.createdAt ? 1 : 0));
|
|
213
|
-
const condHeaders = conditionalHeaders(idxEtag, indexMissing);
|
|
228
|
+
const condHeaders = conditionalHeaders(idxEtag, indexMissing, this.target.conditionalWrite);
|
|
229
|
+
await this.reassertIndexEtag(idxEtag, secret);
|
|
214
230
|
const putIdx = await this.send('PUT', indexKey(this.target.prefix), {
|
|
215
231
|
body: JSON.stringify(merged),
|
|
216
232
|
secret,
|
|
217
233
|
headers: condHeaders,
|
|
218
234
|
});
|
|
219
|
-
|
|
220
|
-
|
|
235
|
+
const idxFailure = await this.conditionalFailure('PUT', putIdx.url, putIdx.res, Object.keys(condHeaders).length > 0, secret);
|
|
236
|
+
if (idxFailure !== undefined)
|
|
237
|
+
throw idxFailure;
|
|
221
238
|
if (!putIdx.res.ok) {
|
|
222
239
|
throw new S3TransportError(await this.failText('PUT', putIdx.url, putIdx.res, secret), classifyHttpStatus(putIdx.res.status));
|
|
223
240
|
}
|
|
@@ -262,14 +279,16 @@ export class S3Transport {
|
|
|
262
279
|
const remaining = idx.filter((m) => m.id !== id);
|
|
263
280
|
if (remaining.length === idx.length && del.res.status === 404)
|
|
264
281
|
return; // 对象与索引都没有 → 静默成功
|
|
265
|
-
const condHeaders = conditionalHeaders(idxEtag, false);
|
|
282
|
+
const condHeaders = conditionalHeaders(idxEtag, false, this.target.conditionalWrite);
|
|
283
|
+
await this.reassertIndexEtag(idxEtag, secret);
|
|
266
284
|
const putIdx = await this.send('PUT', indexKey(this.target.prefix), {
|
|
267
285
|
body: JSON.stringify(remaining),
|
|
268
286
|
secret,
|
|
269
287
|
headers: condHeaders,
|
|
270
288
|
});
|
|
271
|
-
|
|
272
|
-
|
|
289
|
+
const idxFailure = await this.conditionalFailure('PUT', putIdx.url, putIdx.res, Object.keys(condHeaders).length > 0, secret);
|
|
290
|
+
if (idxFailure !== undefined)
|
|
291
|
+
throw idxFailure;
|
|
273
292
|
if (!putIdx.res.ok) {
|
|
274
293
|
throw new S3TransportError(await this.failText('PUT', putIdx.url, putIdx.res, secret), classifyHttpStatus(putIdx.res.status));
|
|
275
294
|
}
|
|
@@ -280,6 +299,53 @@ export class S3Transport {
|
|
|
280
299
|
concurrentWrite(url, status) {
|
|
281
300
|
return new S3TransportError(this.o.msg('sync.s3.concurrentWrite', { status: String(status), url }), { kind: 'conflict', retryable: false, status });
|
|
282
301
|
}
|
|
302
|
+
/**
|
|
303
|
+
* OSS 方言的「ETag 条件写」替身(issue #109 第二层):PutObject 既无 If-Match 也无
|
|
304
|
+
* If-None-Match,`x-oss-forbid-overwrite` 只能表达「别覆盖已存在的对象」,表达不了
|
|
305
|
+
* 「远端必须还是我读到的那一版」。所以索引进已存在时改成**写前 HEAD 复核 ETag**:
|
|
306
|
+
* 变了 / 对象没了 → 按并发写中止(零写入)。
|
|
307
|
+
*
|
|
308
|
+
* **不是原子的**:复核与 PUT 之间仍有窗口(服务端不支持条件写就没有真正的解),
|
|
309
|
+
* 比无保护强、比 If-Match 弱 —— 如实登记在 docs/decisions/0003。
|
|
310
|
+
* 其它方言(etag)与「服务端不给 ETag」两种情况一律直接放行,不改既有行为。
|
|
311
|
+
*/
|
|
312
|
+
async reassertIndexEtag(etag, secret) {
|
|
313
|
+
if (this.target.conditionalWrite !== 'forbid-overwrite' || etag === undefined)
|
|
314
|
+
return;
|
|
315
|
+
const key = indexKey(this.target.prefix);
|
|
316
|
+
const url = objectUrl(this.target, key);
|
|
317
|
+
const head = await this.send('HEAD', key, { secret });
|
|
318
|
+
if (head.res.status === 404)
|
|
319
|
+
throw this.concurrentWrite(url, 409);
|
|
320
|
+
if (!head.res.ok) {
|
|
321
|
+
throw new S3TransportError(await this.failText('HEAD', head.url, head.res, secret), classifyHttpStatus(head.res.status));
|
|
322
|
+
}
|
|
323
|
+
const current = head.res.headers?.['etag'];
|
|
324
|
+
if (typeof current === 'string' && current !== '' && current !== etag) {
|
|
325
|
+
throw this.concurrentWrite(url, 409);
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
/**
|
|
329
|
+
* 条件写失败分类(issue #109 第二层复核):`isConcurrentWrite` 只看「状态码像不像」,
|
|
330
|
+
* 这里再问报文一句确认「对象确实已存在」。OSS 的 **409 不止 FileAlreadyExists** ——
|
|
331
|
+
* FileImmutable(Bucket 数据处于保护状态,删改都会被拒)同为 409,一律按并发写报
|
|
332
|
+
* 「别的设备改过索引」就是假话。报文读不出时仍按并发写处理(宁可让用户重跑一次,
|
|
333
|
+
* 不吞掉真冲突)。412 是 If-Match 语义,恒为并发写。
|
|
334
|
+
*/
|
|
335
|
+
async conditionalFailure(method, url, res, guarded, secret) {
|
|
336
|
+
if (!isConcurrentWrite(res.status, guarded))
|
|
337
|
+
return undefined;
|
|
338
|
+
if (res.status !== 409)
|
|
339
|
+
return this.concurrentWrite(url, res.status);
|
|
340
|
+
// 只有 forbid-overwrite 方言的 409 有两种含义(FileAlreadyExists / FileImmutable);
|
|
341
|
+
// etag 方言的 409 由各家自定语义,报文不可当作判据 —— 沿用「带条件头的 409 = 并发写」。
|
|
342
|
+
if (this.target.conditionalWrite !== 'forbid-overwrite')
|
|
343
|
+
return this.concurrentWrite(url, res.status);
|
|
344
|
+
const body = await this.errorBody(res);
|
|
345
|
+
if (body === '' || body.includes('FileAlreadyExists'))
|
|
346
|
+
return this.concurrentWrite(url, res.status);
|
|
347
|
+
return new S3TransportError(this.failMessage(method, url, res.status, body, secret), classifyHttpStatus(res.status));
|
|
348
|
+
}
|
|
283
349
|
/**
|
|
284
350
|
* 连通性探针(cloud-sync 借鉴项 ②):HEAD 桶证明端点/凭据/桶可达,再读一次索引回报快照数。
|
|
285
351
|
* 重定向(301/307/308)时读 x-amz-bucket-region 直接把「region 该填什么」告诉用户 ——
|
|
@@ -368,22 +434,27 @@ export class S3Transport {
|
|
|
368
434
|
throw new S3TransportError(this.o.msg('sync.s3.requestError', { method, url, err: this.mask(rawMsg, opts.secret) }), { ...classifyNetworkErrorText(rawMsg), cause: err });
|
|
369
435
|
}
|
|
370
436
|
}
|
|
371
|
-
/**
|
|
372
|
-
async
|
|
373
|
-
let body = '';
|
|
437
|
+
/** 读错误响应体(未脱敏原文,截断到 ERR_BODY_MAX;读不出按空串) */
|
|
438
|
+
async errorBody(res) {
|
|
374
439
|
try {
|
|
375
|
-
|
|
440
|
+
return (await res.text()).slice(0, ERR_BODY_MAX);
|
|
376
441
|
}
|
|
377
442
|
catch {
|
|
378
|
-
|
|
443
|
+
return '';
|
|
379
444
|
}
|
|
445
|
+
}
|
|
446
|
+
/** 构造 HTTP 非 2xx 失败消息:附上脱敏后的响应体片段(截断) */
|
|
447
|
+
failMessage(method, url, status, body, secret) {
|
|
380
448
|
return this.o.msg('sync.s3.requestFailed', {
|
|
381
449
|
method,
|
|
382
450
|
url,
|
|
383
|
-
status: String(
|
|
451
|
+
status: String(status),
|
|
384
452
|
err: this.mask(body, secret),
|
|
385
453
|
});
|
|
386
454
|
}
|
|
455
|
+
async failText(method, url, res, secret) {
|
|
456
|
+
return this.failMessage(method, url, res.status, await this.errorBody(res), secret);
|
|
457
|
+
}
|
|
387
458
|
/** 识别超时错误(AbortError / TimeoutError) */
|
|
388
459
|
isTimeout(err) {
|
|
389
460
|
const name = err?.name ?? '';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-config-manager",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "Backup, restore, export, import, migrate and sync your DeepSeek Harness (DSH) configuration: settings, plugins, MCP servers, skills, agent presets and workspaces. One-click migration to another machine. DSH 配置一键备份、恢复、导出、导入、迁移与同步插件。",
|
|
6
6
|
"icon": "./icon.svg",
|
package/src/index.ts
CHANGED
|
@@ -181,7 +181,7 @@ export const name = 'config-manager'
|
|
|
181
181
|
export const inject = ['settings', 'credentials']
|
|
182
182
|
|
|
183
183
|
/** Plugin version, kept in sync with package.json ("version"). */
|
|
184
|
-
export const PLUGIN_VERSION = '0.3.
|
|
184
|
+
export const PLUGIN_VERSION = '0.3.1'
|
|
185
185
|
|
|
186
186
|
/** Plugin own package name — excluded from its own exported plugins list. */
|
|
187
187
|
const PLUGIN_NAME = 'dsh-config-manager'
|
|
@@ -7,6 +7,10 @@
|
|
|
7
7
|
* `getResourceV4`、`oss/auth.go` 的 `getSubResource` / `getSignedStrV4`)**独立重写**一遍,
|
|
8
8
|
* 只吃「收到的 HTTP 请求」(method / url / 头 / body),绝不复用客户端的签名中间量。
|
|
9
9
|
*
|
|
10
|
+
* 也照真服务端实现**条件写能力**:PutObject 收到 If-Match / If-None-Match 一律拒(0017-00000245),
|
|
11
|
+
* 只接受 `x-oss-forbid-overwrite: true`(已存在 → 409 FileAlreadyExists)—— 这一层不照真服务端写,
|
|
12
|
+
* 「本地全绿、真机 400」的盲点就会回来(issue #109 第二层)。
|
|
13
|
+
*
|
|
10
14
|
* 用法:`s3-transport.oss4-server.test.ts`(内存对象存储,逐请求验签)与
|
|
11
15
|
* `scripts/oss-live-check.mjs`(真桶联调)。它证明的是「与本仓库对官方算法的独立复现一致」,
|
|
12
16
|
* 不等于官方服务端盖章 —— 真桶连通性要靠带真实密钥跑一次脚本。
|
|
@@ -134,6 +138,11 @@ export function verifyOssV4(
|
|
|
134
138
|
return { ok: true, reason: '', canonicalRequest, stringToSign, signature, objectKey, bucketLevel };
|
|
135
139
|
}
|
|
136
140
|
|
|
141
|
+
/** 真 OSS 会拒绝的条件写头(PutObject 不支持;错误码 0017-00000245)。 */
|
|
142
|
+
const IF_CONDITIONAL_HEADERS = ['if-match', 'if-none-match'] as const;
|
|
143
|
+
/** OSS 回显头名时用的原始大小写(真机报文里看到的是 `If-None-Match`)。 */
|
|
144
|
+
const IF_HEADER_ECHO: Record<string, string> = { 'if-match': 'If-Match', 'if-none-match': 'If-None-Match' };
|
|
145
|
+
|
|
137
146
|
interface StoredObject {
|
|
138
147
|
body: string;
|
|
139
148
|
etag: string;
|
|
@@ -174,13 +183,20 @@ export function makeOssVerifyServer(cfg: Oss4VerifyConfig): {
|
|
|
174
183
|
const h = lower(headers);
|
|
175
184
|
const existing = objects.get(key);
|
|
176
185
|
if (method === 'PUT') {
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
186
|
+
// 真 OSS 的 PutObject **不支持 If-Match / If-None-Match**:收到就回
|
|
187
|
+
// 400 InvalidArgument「A header you provided implies functionality that is not implemented.」
|
|
188
|
+
// (错误码 0017-00000245,issue #109 第二层)。这里必须照真服务端拒掉 —— 之前这个验签器
|
|
189
|
+
// 自己实现了 if-none-match,等于用自家规则验自家人,于是「14 个请求零失败」漏掉了这一层。
|
|
190
|
+
const unsupported = IF_CONDITIONAL_HEADERS.find((n) => h[n] !== undefined);
|
|
191
|
+
if (unsupported !== undefined) {
|
|
192
|
+
return respond(400, '<Error><Code>InvalidArgument</Code><Message>A header you provided implies '
|
|
193
|
+
+ 'functionality that is not implemented.</Message><Header>' + IF_HEADER_ECHO[unsupported]
|
|
194
|
+
+ '</Header><EC>0017-00000245</EC></Error>');
|
|
181
195
|
}
|
|
182
|
-
|
|
183
|
-
|
|
196
|
+
// OSS 唯一支持的「禁止覆盖」:对象已存在即 409 FileAlreadyExists。
|
|
197
|
+
if ((h['x-oss-forbid-overwrite'] ?? '').trim().toLowerCase() === 'true' && existing !== undefined) {
|
|
198
|
+
return respond(409, '<Error><Code>FileAlreadyExists</Code><Message>The object you specified already '
|
|
199
|
+
+ 'exists and cannot be overwritten again.</Message></Error>');
|
|
184
200
|
}
|
|
185
201
|
const body = opts.body ?? '';
|
|
186
202
|
const etag = sha256(body).slice(0, 32);
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* S3 兼容系通道:本轮借鉴项的**针对性**单测(不重复 s3-transport.test.ts 的既有契约)。
|
|
3
|
-
* - ①
|
|
4
|
-
* 索引 PUT 412 → conflict;409
|
|
5
|
-
* 409 **不带条件头** →
|
|
3
|
+
* - ① 条件写(etag 方言):索引 ETag → if-match;索引缺失且远端无内容 → if-none-match: *;
|
|
4
|
+
* 索引 PUT 412 → conflict;409 **带条件头且报文是 FileAlreadyExists** → conflict;
|
|
5
|
+
* 409 **不带条件头** → 绝不走并发写冲突那条路径;
|
|
6
|
+
* - ①-b 阿里云 OSS(forbid-overwrite 方言):不发任何 If-*;首推 → x-oss-forbid-overwrite: true;
|
|
7
|
+
* 索引已有 ETag → 写前 HEAD 复核;409 报文是 FileImmutable → 如实报错,不冒充并发写;
|
|
6
8
|
* - ② 探针:HEAD 回 301/307/308 + x-amz-bucket-region → 抛错且文案里带可执行的 region;
|
|
7
9
|
* 404 / 403 各自文案;HEAD 200 时读索引回报快照数。
|
|
8
10
|
*/
|
|
@@ -234,6 +236,154 @@ test('条件写①(删除路径):delete 读 index 拿 ETag → 写回剩
|
|
|
234
236
|
);
|
|
235
237
|
});
|
|
236
238
|
|
|
239
|
+
/* ---------------- ①-b 阿里云 OSS:没有 If-*,只有 x-oss-forbid-overwrite(issue #109 第二层) ---------------- */
|
|
240
|
+
|
|
241
|
+
const OSS_ENDPOINT = 'https://oss-cn-hangzhou.aliyuncs.com';
|
|
242
|
+
const OSS_INDEX_URL = 'https://' + BUCKET + '.oss-cn-hangzhou.aliyuncs.com/' + PREFIX + '/index.json';
|
|
243
|
+
const OSS_SNAPSHOT_URL = 'https://' + BUCKET + '.oss-cn-hangzhou.aliyuncs.com/' + PREFIX + '/snap-001.json';
|
|
244
|
+
const OSS_BLOBS_INDEX_URL = 'https://' + BUCKET + '.oss-cn-hangzhou.aliyuncs.com/' + PREFIX + '/blobs-index.json';
|
|
245
|
+
|
|
246
|
+
/** OSS 变体选项:虚拟主机风格(现场配置),region 必填,provider=oss。 */
|
|
247
|
+
function ossOptions(request: S3RequestFn): S3TransportOptions {
|
|
248
|
+
return makeOptions(request, { provider: 'oss', endpoint: OSS_ENDPOINT, region: 'cn-hangzhou', pathStyle: false });
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
test('条件写①-b(OSS):首推发 x-oss-forbid-overwrite,绝不发 if-none-match(真机回 0017-00000245)', async () => {
|
|
252
|
+
const calls: S3Call[] = [];
|
|
253
|
+
const t = new S3Transport(ossOptions(record(calls, (m) => {
|
|
254
|
+
if (m.method === 'GET' && m.url === OSS_INDEX_URL) return res(404, '');
|
|
255
|
+
if (m.method === 'GET' && m.url === OSS_BLOBS_INDEX_URL) return res(404, '');
|
|
256
|
+
if (m.method === 'PUT' && m.url === OSS_SNAPSHOT_URL) return res(200, '');
|
|
257
|
+
if (m.method === 'PUT' && m.url === OSS_INDEX_URL) return res(200, '');
|
|
258
|
+
return res(500, 'unexpected ' + m.method + ' ' + m.url);
|
|
259
|
+
})));
|
|
260
|
+
|
|
261
|
+
await t.upload(sampleSnapshot());
|
|
262
|
+
|
|
263
|
+
const put = calls.filter((c) => c.method === 'PUT' && c.url === OSS_INDEX_URL)[0]!;
|
|
264
|
+
assert.equal(put.headers['x-oss-forbid-overwrite'], 'true', 'OSS 的「禁止覆盖」只能这么表达');
|
|
265
|
+
assert.equal(put.headers['if-none-match'], undefined, 'If-None-Match 会被 OSS 判成未实现的功能');
|
|
266
|
+
assert.equal(put.headers['if-match'], undefined);
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
test('条件写①-b(OSS):索引已有 ETag → 不发 if-match,改在写前 HEAD 复核 ETag', async () => {
|
|
270
|
+
const calls: S3Call[] = [];
|
|
271
|
+
const t = new S3Transport(ossOptions(record(calls, (m) => {
|
|
272
|
+
if (m.method === 'GET' && m.url === OSS_INDEX_URL) return res(200, '[]', { etag: '"v1-oss"' });
|
|
273
|
+
if (m.method === 'PUT' && m.url === OSS_SNAPSHOT_URL) return res(200, '');
|
|
274
|
+
if (m.method === 'HEAD' && m.url === OSS_INDEX_URL) return res(200, '', { etag: '"v1-oss"' });
|
|
275
|
+
if (m.method === 'PUT' && m.url === OSS_INDEX_URL) return res(200, '');
|
|
276
|
+
return res(500, 'unexpected ' + m.method + ' ' + m.url);
|
|
277
|
+
})));
|
|
278
|
+
|
|
279
|
+
await t.upload(sampleSnapshot());
|
|
280
|
+
|
|
281
|
+
const headAt = calls.findIndex((c) => c.method === 'HEAD' && c.url === OSS_INDEX_URL);
|
|
282
|
+
const putAt = calls.findIndex((c) => c.method === 'PUT' && c.url === OSS_INDEX_URL);
|
|
283
|
+
assert.ok(headAt >= 0, '写前必须复核一次 ETag');
|
|
284
|
+
assert.ok(headAt < putAt, '复核必须在索引 PUT 之前');
|
|
285
|
+
const put = calls[putAt]!;
|
|
286
|
+
assert.equal(put.headers['if-match'], undefined, 'OSS PutObject 不支持 If-Match');
|
|
287
|
+
assert.equal(put.headers['x-oss-forbid-overwrite'], undefined, '索引已存在时不能用「禁止覆盖」(会被自己挡住)');
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
test('条件写①-b(OSS):写前复核发现 ETag 变了 → 判并发写并中止(索引零写入)', async () => {
|
|
291
|
+
const calls: S3Call[] = [];
|
|
292
|
+
const t = new S3Transport(ossOptions(record(calls, (m) => {
|
|
293
|
+
if (m.method === 'GET' && m.url === OSS_INDEX_URL) return res(200, '[]', { etag: '"v1-oss"' });
|
|
294
|
+
if (m.method === 'PUT' && m.url === OSS_SNAPSHOT_URL) return res(200, '');
|
|
295
|
+
if (m.method === 'HEAD' && m.url === OSS_INDEX_URL) return res(200, '', { etag: '"v2-oss"' });
|
|
296
|
+
return res(500, 'unexpected ' + m.method + ' ' + m.url);
|
|
297
|
+
})));
|
|
298
|
+
|
|
299
|
+
await assert.rejects(
|
|
300
|
+
() => t.upload(sampleSnapshot()),
|
|
301
|
+
(err: unknown) => {
|
|
302
|
+
assert.ok(err instanceof S3TransportError);
|
|
303
|
+
assert.equal(err.kind, 'conflict');
|
|
304
|
+
assert.equal(err.retryable, false);
|
|
305
|
+
assert.equal(err.message, s3Msg('sync.s3.concurrentWrite', { status: '409', url: OSS_INDEX_URL }));
|
|
306
|
+
return true;
|
|
307
|
+
},
|
|
308
|
+
);
|
|
309
|
+
assert.equal(calls.filter((c) => c.method === 'PUT' && c.url === OSS_INDEX_URL).length, 0, '复核不过 ⇒ 索引零写入');
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
test('条件写①-b(OSS):索引 PUT 回 409(FileAlreadyExists)→ 仍判并发写冲突', async () => {
|
|
313
|
+
const calls: S3Call[] = [];
|
|
314
|
+
const t = new S3Transport(ossOptions(record(calls, (m) => {
|
|
315
|
+
if (m.method === 'GET' && m.url === OSS_INDEX_URL) return res(404, '');
|
|
316
|
+
if (m.method === 'GET' && m.url === OSS_BLOBS_INDEX_URL) return res(404, '');
|
|
317
|
+
if (m.method === 'PUT' && m.url === OSS_SNAPSHOT_URL) return res(200, '');
|
|
318
|
+
if (m.method === 'PUT' && m.url === OSS_INDEX_URL) return res(409, '<Error><Code>FileAlreadyExists</Code></Error>');
|
|
319
|
+
return res(500, 'unexpected ' + m.method + ' ' + m.url);
|
|
320
|
+
})));
|
|
321
|
+
|
|
322
|
+
await assert.rejects(
|
|
323
|
+
() => t.upload(sampleSnapshot()),
|
|
324
|
+
(err: unknown) => {
|
|
325
|
+
assert.ok(err instanceof S3TransportError);
|
|
326
|
+
assert.equal(err.kind, 'conflict');
|
|
327
|
+
assert.equal(err.status, 409);
|
|
328
|
+
return true;
|
|
329
|
+
},
|
|
330
|
+
);
|
|
331
|
+
});
|
|
332
|
+
|
|
333
|
+
test('条件写①-b(OSS):索引 PUT 回 409 FileImmutable(桶数据被保护)→ 不冒充并发写,如实带报文', async () => {
|
|
334
|
+
const immutable = '<Error><Code>FileImmutable</Code><Message>The bucket is protected.</Message></Error>';
|
|
335
|
+
const t = new S3Transport(ossOptions(record([], (m) => {
|
|
336
|
+
if (m.method === 'GET' && m.url === OSS_INDEX_URL) return res(404, '');
|
|
337
|
+
if (m.method === 'GET' && m.url === OSS_BLOBS_INDEX_URL) return res(404, '');
|
|
338
|
+
if (m.method === 'PUT' && m.url === OSS_SNAPSHOT_URL) return res(200, '');
|
|
339
|
+
if (m.method === 'PUT' && m.url === OSS_INDEX_URL) return res(409, immutable);
|
|
340
|
+
return res(500, 'unexpected ' + m.method + ' ' + m.url);
|
|
341
|
+
})));
|
|
342
|
+
|
|
343
|
+
await assert.rejects(
|
|
344
|
+
() => t.upload(sampleSnapshot()),
|
|
345
|
+
(err: unknown) => {
|
|
346
|
+
assert.ok(err instanceof S3TransportError);
|
|
347
|
+
assert.equal(err.status, 409);
|
|
348
|
+
assert.notEqual(
|
|
349
|
+
err.message,
|
|
350
|
+
s3Msg('sync.s3.concurrentWrite', { status: '409', url: OSS_INDEX_URL }),
|
|
351
|
+
'FileImmutable 与 FileAlreadyExists 同为 409,不能都说成「别的设备改过索引」',
|
|
352
|
+
);
|
|
353
|
+
assert.match(err.message, /FileImmutable/, '真实错误码必须原样带出,用户才看得出是桶的保护策略');
|
|
354
|
+
return true;
|
|
355
|
+
},
|
|
356
|
+
);
|
|
357
|
+
});
|
|
358
|
+
|
|
359
|
+
test('条件写①-b(OSS,删除路径):索引已有 ETag → 写回前 HEAD 复核,且不发 if-match / forbid-overwrite', async () => {
|
|
360
|
+
const meta = {
|
|
361
|
+
id: 'snap-001',
|
|
362
|
+
createdAt: '2026-10-18T09:00:00.000Z',
|
|
363
|
+
sections: {},
|
|
364
|
+
manifest: { schemaVersion: 1, dshVersion: '1', platform: 'win32', sectionIds: [], containsSecrets: false },
|
|
365
|
+
};
|
|
366
|
+
const calls: S3Call[] = [];
|
|
367
|
+
const t = new S3Transport(ossOptions(record(calls, (m) => {
|
|
368
|
+
if (m.method === 'GET' && m.url === OSS_INDEX_URL) return res(200, JSON.stringify([meta]), { etag: '"v1-oss-del"' });
|
|
369
|
+
if (m.method === 'DELETE' && m.url === OSS_SNAPSHOT_URL) return res(204, '');
|
|
370
|
+
if (m.method === 'HEAD' && m.url === OSS_INDEX_URL) return res(200, '', { etag: '"v1-oss-del"' });
|
|
371
|
+
if (m.method === 'PUT' && m.url === OSS_INDEX_URL) return res(200, '');
|
|
372
|
+
if (m.method === 'GET' && m.url === OSS_BLOBS_INDEX_URL) return res(404, '');
|
|
373
|
+
return res(500, 'unexpected ' + m.method + ' ' + m.url);
|
|
374
|
+
})));
|
|
375
|
+
|
|
376
|
+
await t.delete('snap-001');
|
|
377
|
+
|
|
378
|
+
const headAt = calls.findIndex((c) => c.method === 'HEAD' && c.url === OSS_INDEX_URL);
|
|
379
|
+
const putAt = calls.findIndex((c) => c.method === 'PUT' && c.url === OSS_INDEX_URL);
|
|
380
|
+
assert.ok(headAt >= 0 && headAt < putAt, 'OSS 删除路径同样要在写回前复核 ETag');
|
|
381
|
+
const put = calls[putAt]!;
|
|
382
|
+
assert.equal(put.headers['if-match'], undefined, 'OSS PutObject 不支持 If-Match');
|
|
383
|
+
assert.equal(put.headers['x-oss-forbid-overwrite'], undefined, '索引已存在,「禁止覆盖」会把自己挡在门外');
|
|
384
|
+
assert.equal(put.body, '[]');
|
|
385
|
+
});
|
|
386
|
+
|
|
237
387
|
/* ---------------- ② 探针 ---------------- */
|
|
238
388
|
|
|
239
389
|
test('② 探针:HEAD 回 301 + x-amz-bucket-region: cn-hangzhou → 抛错且文案含该 region', async () => {
|