@yydb/yydb-client 0.0.15 → 0.0.16

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/errors.d.ts CHANGED
@@ -1,12 +1,87 @@
1
1
  /**
2
- * @yydb/yydb-client consumes the same error surface as @yydb/yydb.
3
- *
4
- * YydbError / YydbErrorCodes / YydbClientErrorCodes / isYydbError are re-exported
5
- * from {@linkcode @yydb/yydb/shared/errors} so engine code, retryability and
6
- * params handling live in exactly one place. Client authors minting
7
- * client-local errors (connect failure, request-id mismatch, …) still use
8
- * {@link YydbClientErrorCodes} from this package via YydbErrorCodes or the
9
- * named export.
2
+ * @yydb/yydb-client 公共错误面。
3
+ *
4
+ * 与 Node 嵌入式库 `@yydb/yydb` 共享同一错误合同:稳定 `code` 字符串、
5
+ * `operation`、`retryable`、`params`。**分支依据是 `code` 不是 `message`。**
6
+ *
7
+ * 引擎侧错误 code 字符串来自 Rust 内核(经 wire 协议传入),这里不维护
8
+ * 引擎 code 常量表——Rust 新增变体时 host 直接拿到字符串即可。
9
+ *
10
+ * Client 本地错误(`yydb.client.*`)只在本包 mint,不流入 Rust。
11
+ *
12
+ * 本包与 `@yydb/yydb` **互不依赖**——错误合同在各自边界独立持有。
13
+ */
14
+ /**
15
+ * Client 侧专属错误 code(永远不来自 Rust)。
16
+ */
17
+ export declare const YydbClientErrorCodes: {
18
+ /** serve() endpoint 为空或无效。 */
19
+ readonly clientInvalidEndpoint: "yydb.client.invalid_endpoint";
20
+ /** connect() socket / transport 层失败。 */
21
+ readonly clientTransportFailure: "yydb.client.transport_failure";
22
+ /** Server 返回的 request id 与请求不匹配。 */
23
+ readonly clientRequestIdMismatch: "yydb.client.request_id_mismatch";
24
+ /** Server 返回了未请求的 msg_type。 */
25
+ readonly clientUnexpectedResponse: "yydb.client.unexpected_response";
26
+ /** 无法解析 wire Error frame body。 */
27
+ readonly clientDecodeFailure: "yydb.client.decode_failure";
28
+ /** YYDB session 已关闭。 */
29
+ readonly sessionClosed: "yydb.session.closed";
30
+ };
31
+ /**
32
+ * 所有已知 client error code 的联合(引擎 code 作为字符串直接使用)。
10
33
  */
11
- export { YydbError, YydbErrorCodes, YydbClientErrorCodes, isYydbError, } from '@yydb/yydb';
12
- export type { YydbErrorCode, YydbErrorOptions } from '@yydb/yydb';
34
+ export declare const YydbErrorCodes: {
35
+ /** serve() endpoint 为空或无效。 */
36
+ readonly clientInvalidEndpoint: "yydb.client.invalid_endpoint";
37
+ /** connect() socket / transport 层失败。 */
38
+ readonly clientTransportFailure: "yydb.client.transport_failure";
39
+ /** Server 返回的 request id 与请求不匹配。 */
40
+ readonly clientRequestIdMismatch: "yydb.client.request_id_mismatch";
41
+ /** Server 返回了未请求的 msg_type。 */
42
+ readonly clientUnexpectedResponse: "yydb.client.unexpected_response";
43
+ /** 无法解析 wire Error frame body。 */
44
+ readonly clientDecodeFailure: "yydb.client.decode_failure";
45
+ /** YYDB session 已关闭。 */
46
+ readonly sessionClosed: "yydb.session.closed";
47
+ };
48
+ /** 稳定错误 code 字符串。引擎 code 来自 Rust,client code 来自本包常量。 */
49
+ export type YydbErrorCode = string;
50
+ /** {@link YydbError} 构造参数。 */
51
+ export interface YydbErrorOptions {
52
+ /** 失败操作(`schemaGet`、`kvPut`、`query` …)。 */
53
+ operation?: string;
54
+ /**
55
+ * 相同参数是否可重试。
56
+ *
57
+ * 缺省为 `false`(保守)。client 侧 mint 的可重试错误(如
58
+ * `yydb.client.transport_failure`)**必须**显式设置。引擎侧经 wire 传入
59
+ * 的错误携带 Rust 权威 retryable 值,解析时覆盖默认。
60
+ */
61
+ retryable?: boolean;
62
+ /** 结构化参数,供 catalog 模板化渲染使用。 */
63
+ params?: Readonly<Record<string, string>>;
64
+ /** 被本错误包装的底层失败。 */
65
+ cause?: unknown;
66
+ }
67
+ /** 带稳定机器可读 `code` 的 YYDB 错误。 */
68
+ export declare class YydbError extends Error {
69
+ /** 稳定 code —— 引擎侧来自 Rust,或 `yydb.client.*`。 */
70
+ readonly code: string;
71
+ /** 失败操作。 */
72
+ readonly operation: string;
73
+ /** 是否可重试。 */
74
+ readonly retryable: boolean;
75
+ /** 结构化参数(存在时)。 */
76
+ readonly params?: Readonly<Record<string, string>>;
77
+ constructor(code: YydbErrorCode, message: string, options?: YydbErrorOptions);
78
+ /**
79
+ * 从 wire error body(JSON)解析出 {@link YydbError}。格式:
80
+ * `{ code, operation?, retryable?, message, params? }`。
81
+ *
82
+ * 返回解析后的 {@link YydbError};纯文本输入返回 `null`。
83
+ */
84
+ static fromDiagnosticJson(value: unknown): YydbError | null;
85
+ }
86
+ /** 让调用方无需 `instanceof` import 即可收窄。 */
87
+ export declare function isYydbError(value: unknown): value is YydbError;
package/dist/errors.js CHANGED
@@ -1,11 +1,109 @@
1
1
  /**
2
- * @yydb/yydb-client consumes the same error surface as @yydb/yydb.
3
- *
4
- * YydbError / YydbErrorCodes / YydbClientErrorCodes / isYydbError are re-exported
5
- * from {@linkcode @yydb/yydb/shared/errors} so engine code, retryability and
6
- * params handling live in exactly one place. Client authors minting
7
- * client-local errors (connect failure, request-id mismatch, …) still use
8
- * {@link YydbClientErrorCodes} from this package via YydbErrorCodes or the
9
- * named export.
2
+ * @yydb/yydb-client 公共错误面。
3
+ *
4
+ * 与 Node 嵌入式库 `@yydb/yydb` 共享同一错误合同:稳定 `code` 字符串、
5
+ * `operation`、`retryable`、`params`。**分支依据是 `code` 不是 `message`。**
6
+ *
7
+ * 引擎侧错误 code 字符串来自 Rust 内核(经 wire 协议传入),这里不维护
8
+ * 引擎 code 常量表——Rust 新增变体时 host 直接拿到字符串即可。
9
+ *
10
+ * Client 本地错误(`yydb.client.*`)只在本包 mint,不流入 Rust。
11
+ *
12
+ * 本包与 `@yydb/yydb` **互不依赖**——错误合同在各自边界独立持有。
13
+ */
14
+ /**
15
+ * Client 侧专属错误 code(永远不来自 Rust)。
16
+ */
17
+ export const YydbClientErrorCodes = {
18
+ /** serve() endpoint 为空或无效。 */
19
+ clientInvalidEndpoint: 'yydb.client.invalid_endpoint',
20
+ /** connect() socket / transport 层失败。 */
21
+ clientTransportFailure: 'yydb.client.transport_failure',
22
+ /** Server 返回的 request id 与请求不匹配。 */
23
+ clientRequestIdMismatch: 'yydb.client.request_id_mismatch',
24
+ /** Server 返回了未请求的 msg_type。 */
25
+ clientUnexpectedResponse: 'yydb.client.unexpected_response',
26
+ /** 无法解析 wire Error frame body。 */
27
+ clientDecodeFailure: 'yydb.client.decode_failure',
28
+ /** YYDB session 已关闭。 */
29
+ sessionClosed: 'yydb.session.closed',
30
+ };
31
+ /**
32
+ * 所有已知 client error code 的联合(引擎 code 作为字符串直接使用)。
10
33
  */
11
- export { YydbError, YydbErrorCodes, YydbClientErrorCodes, isYydbError, } from '@yydb/yydb';
34
+ export const YydbErrorCodes = {
35
+ ...YydbClientErrorCodes,
36
+ };
37
+ /** 带稳定机器可读 `code` 的 YYDB 错误。 */
38
+ export class YydbError extends Error {
39
+ /** 稳定 code —— 引擎侧来自 Rust,或 `yydb.client.*`。 */
40
+ code;
41
+ /** 失败操作。 */
42
+ operation;
43
+ /** 是否可重试。 */
44
+ retryable;
45
+ /** 结构化参数(存在时)。 */
46
+ params;
47
+ constructor(code, message, options = {}) {
48
+ super(message, { cause: options.cause });
49
+ this.name = 'YydbError';
50
+ this.code = code;
51
+ this.operation = options.operation ?? 'unknown';
52
+ this.retryable = options.retryable ?? false;
53
+ if (options.params !== undefined) {
54
+ this.params = options.params;
55
+ }
56
+ }
57
+ /**
58
+ * 从 wire error body(JSON)解析出 {@link YydbError}。格式:
59
+ * `{ code, operation?, retryable?, message, params? }`。
60
+ *
61
+ * 返回解析后的 {@link YydbError};纯文本输入返回 `null`。
62
+ */
63
+ static fromDiagnosticJson(value) {
64
+ let obj;
65
+ if (typeof value === 'string') {
66
+ try {
67
+ const parsed = JSON.parse(value);
68
+ if (!parsed || typeof parsed !== 'object')
69
+ return null;
70
+ obj = parsed;
71
+ }
72
+ catch {
73
+ return null;
74
+ }
75
+ }
76
+ else if (value && typeof value === 'object') {
77
+ obj = value;
78
+ }
79
+ else {
80
+ return null;
81
+ }
82
+ // WASM envelopes nest the diagnostic under `{ ok, error: {...} }`.
83
+ let diag = obj;
84
+ if (typeof obj['ok'] === 'boolean' && !obj['ok'] && obj['error'] && typeof obj['error'] === 'object') {
85
+ diag = obj['error'];
86
+ }
87
+ const code = typeof diag['code'] === 'string' ? diag['code'] : null;
88
+ const message = typeof diag['message'] === 'string' ? diag['message'] : '';
89
+ if (!code)
90
+ return null;
91
+ const operation = typeof diag['operation'] === 'string' ? diag['operation'] : undefined;
92
+ const retryable = typeof diag['retryable'] === 'boolean' ? diag['retryable'] : undefined;
93
+ const params = diag['params'] && typeof diag['params'] === 'object'
94
+ ? diag['params']
95
+ : undefined;
96
+ const opts = {};
97
+ if (operation !== undefined)
98
+ opts.operation = operation;
99
+ if (retryable !== undefined)
100
+ opts.retryable = retryable;
101
+ if (params !== undefined)
102
+ opts.params = params;
103
+ return new YydbError(code, message, opts);
104
+ }
105
+ }
106
+ /** 让调用方无需 `instanceof` import 即可收窄。 */
107
+ export function isYydbError(value) {
108
+ return value instanceof YydbError;
109
+ }
package/package.json CHANGED
@@ -1,33 +1,38 @@
1
1
  {
2
- "bugs": {
3
- "url": "https://github.com/yy-database/yydb.rs/issues"
4
- },
5
- "dependencies": {
6
- "@yydb/yydb": "0.0.15"
7
- },
2
+ "name": "@yydb/yydb-client",
3
+ "version": "0.0.16",
8
4
  "description": "TypeScript client for the YYDB WebSocket and TCP wire protocol",
9
- "devDependencies": {
10
- "@types/node": "^20.10.0",
11
- "typescript": "~5.3.0"
12
- },
13
- "engines": {
14
- "node": ">=18"
15
- },
5
+ "license": "MPL-2.0",
6
+ "type": "module",
7
+ "main": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
16
9
  "exports": {
17
10
  ".": {
18
- "import": "./dist/index.js",
19
- "types": "./dist/index.d.ts"
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js"
20
13
  },
21
14
  "./node": {
22
- "import": "./dist/node.js",
23
- "types": "./dist/node.d.ts"
15
+ "types": "./dist/node.d.ts",
16
+ "import": "./dist/node.js"
24
17
  }
25
18
  },
26
19
  "files": [
27
20
  "dist",
28
21
  "README.md"
29
22
  ],
30
- "homepage": "https://github.com/yy-database/yydb.rs/tree/dev/projects/packages/yydb-client#readme",
23
+ "scripts": {
24
+ "build": "tsc -p tsconfig.json",
25
+ "test": "pnpm run build && node --experimental-strip-types --test src/**/*.test.ts"
26
+ },
27
+ "repository": {
28
+ "type": "git",
29
+ "url": "git+https://github.com/yy-database/yydb.git",
30
+ "directory": "projects/packages/yydb-client"
31
+ },
32
+ "homepage": "https://github.com/yy-database/yydb/tree/dev/projects/packages/yydb-client#readme",
33
+ "bugs": {
34
+ "url": "https://github.com/yy-database/yydb/issues"
35
+ },
31
36
  "keywords": [
32
37
  "yydb",
33
38
  "yyds",
@@ -35,22 +40,14 @@
35
40
  "wire-protocol",
36
41
  "typescript"
37
42
  ],
38
- "license": "MPL-2.0",
39
- "main": "./dist/index.js",
40
- "name": "@yydb/yydb-client",
41
43
  "publishConfig": {
42
44
  "access": "public"
43
45
  },
44
- "repository": {
45
- "directory": "projects/packages/yydb-client",
46
- "type": "git",
47
- "url": "git+https://github.com/yy-database/yydb.rs.git"
48
- },
49
- "scripts": {
50
- "build": "tsc -p tsconfig.json",
51
- "test": "pnpm run build && node --experimental-strip-types --test src/**/*.test.ts"
46
+ "devDependencies": {
47
+ "@types/node": "^20.10.0",
48
+ "typescript": "~5.3.0"
52
49
  },
53
- "type": "module",
54
- "types": "./dist/index.d.ts",
55
- "version": "0.0.15"
50
+ "engines": {
51
+ "node": ">=18"
52
+ }
56
53
  }