bff-store 0.1.0 → 0.1.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/.claude/settings.local.json +10 -1
- package/README.md +37 -8
- package/dist/index.d.mts +42 -1
- package/dist/index.d.ts +42 -1
- package/dist/index.mjs +43 -0
- package/dist/package.json +2 -5
- package/dist/server/entry.js +31970 -3
- package/dist/server/entry.mjs +32005 -4
- package/dist/storage/mongodb-entry.js +31978 -1
- package/dist/storage/mongodb-entry.mjs +32003 -2
- package/docs/BUG_FIX_MONGODB_CHILD_PROCESS.md +129 -0
- package/docs/FRONTEND_BACKEND_ISOLATION.md +150 -0
- package/docs/LOCAL_LINK_STRATEGIES.md +117 -0
- package/package.json +6 -6
- package/src/environment.ts +11 -0
- package/src/index.ts +7 -0
- package/src/nodeStore.ts +79 -0
- package/tsconfig.json +1 -0
- package/docs/BUG_DIAGNOSIS_REMOTE_STORAGE_OPTIONS.md +0 -104
- package/docs/BUG_FIX_REMOTE_STORAGE_OPTIONS.md +0 -63
- package/docs/BUG_FIX_SESSION_2026-06-03.md +0 -171
- /package/docs/{SIDECAR_SERVER.md → BFF_SIDECAR_SERVER.md} +0 -0
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
# Bug Fix: `'backend' does not exist in type 'RemoteStorageOptions'`
|
|
2
|
-
|
|
3
|
-
## 现象
|
|
4
|
-
|
|
5
|
-
`tests/next_test` 中使用 `"bff-store": "file:../../dist"` 时,TypeScript 报错:
|
|
6
|
-
|
|
7
|
-
```
|
|
8
|
-
Object literal may only specify known properties, and 'backend' does not exist in type 'RemoteStorageOptions'.ts(2353)
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
## 根因
|
|
12
|
-
|
|
13
|
-
### 因素一(主因):dist/package.json 路径错误
|
|
14
|
-
|
|
15
|
-
根目录 `package.json` 的 `main`/`module`/`types`/`exports` 字段路径都带有 `./dist/` 前缀:
|
|
16
|
-
|
|
17
|
-
```json
|
|
18
|
-
"main": "dist/index.js",
|
|
19
|
-
"exports": { ".": { "types": "./dist/index.d.ts", ... } }
|
|
20
|
-
```
|
|
21
|
-
|
|
22
|
-
当通过 `"file:../../dist"` 本地安装时,包根目录就是 `dist/` 本身,`./dist/index.d.ts` 解析为 `dist/dist/index.d.ts` —— 该文件不存在。
|
|
23
|
-
|
|
24
|
-
| 场景 | 包根目录 | `./dist/index.d.ts` 解析结果 | 存在? |
|
|
25
|
-
|------|----------|---------------------------|------|
|
|
26
|
-
| npm 发布 | 项目根 | `项目根/dist/index.d.ts` | 是 |
|
|
27
|
-
| `file:../../dist` | `dist/` | `dist/dist/index.d.ts` | 否 |
|
|
28
|
-
|
|
29
|
-
### 因素二(助推):全局安装了旧版本
|
|
30
|
-
|
|
31
|
-
本地 symlink 解析失败后,TypeScript 沿目录树向上查找,在 `/Users/Admin/node_modules/bff-store/` 找到全局安装的旧版本(v0.1.1),该版本 `RemoteStorageOptions` 没有 `backend` 等新字段。
|
|
32
|
-
|
|
33
|
-
## 修复方案
|
|
34
|
-
|
|
35
|
-
1. **新增** `scripts/adapt-dist-package.js` — 构建后将 `dist/package.json` 中的路径从"相对于项目根"改写为"相对于 dist/":
|
|
36
|
-
- `dist/index.js` → `index.js`
|
|
37
|
-
- `./dist/index.d.ts` → `./index.d.ts`
|
|
38
|
-
- `./dist/storage/...` → `./storage/...`
|
|
39
|
-
- `./dist/server/...` → `./server/...`
|
|
40
|
-
|
|
41
|
-
2. **修改** `package.json` 的 `build` 脚本:
|
|
42
|
-
```
|
|
43
|
-
"build": "tsup && cp package.json dist/package.json && node scripts/adapt-dist-package.js && for f in dist/*.d.mts; do cp \"$f\" \"${f%.d.mts}.d.ts\"; done"
|
|
44
|
-
```
|
|
45
|
-
完整流程:
|
|
46
|
-
- `tsup` — 构建到 dist/
|
|
47
|
-
- `cp package.json dist/package.json` — 复制配置到 dist
|
|
48
|
-
- `node scripts/adapt-dist-package.js` — 改写路径
|
|
49
|
-
- shell 循环 — 复制 `.d.mts` → `.d.ts`(主入口 tsup 只生成 `.d.mts`)
|
|
50
|
-
|
|
51
|
-
## 修复文件
|
|
52
|
-
|
|
53
|
-
| 文件 | 操作 |
|
|
54
|
-
|------|------|
|
|
55
|
-
| `scripts/adapt-dist-package.js` | 新增 — 路径改写脚本 |
|
|
56
|
-
| `package.json` | 修改 — build 脚本 |
|
|
57
|
-
| `tests/next_test/test-regression-remote-storage.ts` | 新增 — jsonl/mongodb/默认三种场景类型回归测试 |
|
|
58
|
-
| `tests/next_test/src/app/test-regression-store.tsx` | 新增 — createStore + storage 的 .tsx 回归测试 |
|
|
59
|
-
|
|
60
|
-
## 验证
|
|
61
|
-
|
|
62
|
-
- `npm run build`:构建成功,dist/package.json 路径正确
|
|
63
|
-
- `npx tsc --noEmit` in `tests/next_test`:0 errors
|
|
@@ -1,171 +0,0 @@
|
|
|
1
|
-
# Bug Fix Session — 2026-06-03
|
|
2
|
-
|
|
3
|
-
本次会话诊断并修复了 4 个 bug,覆盖类型解析、协议层、存储适配器三个层面。
|
|
4
|
-
|
|
5
|
-
---
|
|
6
|
-
|
|
7
|
-
## Bug 1 (P0): `'backend' does not exist in type 'RemoteStorageOptions'`
|
|
8
|
-
|
|
9
|
-
### 现象
|
|
10
|
-
|
|
11
|
-
`tests/next_test` 中 `"bff-store": "file:../../dist"` 安装时,TypeScript 报错:
|
|
12
|
-
|
|
13
|
-
```
|
|
14
|
-
Object literal may only specify known properties, and 'backend' does not exist in type 'RemoteStorageOptions'.ts(2353)
|
|
15
|
-
```
|
|
16
|
-
|
|
17
|
-
### 根因
|
|
18
|
-
|
|
19
|
-
**因素一(主因):dist/package.json 路径错误。** 根目录 `package.json` 的路径字段带有 `./dist/` 前缀:
|
|
20
|
-
|
|
21
|
-
```json
|
|
22
|
-
"main": "dist/index.js",
|
|
23
|
-
"exports": { ".": { "types": "./dist/index.d.ts", ... } }
|
|
24
|
-
```
|
|
25
|
-
|
|
26
|
-
当通过 `"file:../../dist"` 本地安装时,包根目录就是 `dist/` 本身,`./dist/index.d.ts` 解析为 `dist/dist/index.d.ts` —— 文件不存在。
|
|
27
|
-
|
|
28
|
-
| 场景 | 包根目录 | `./dist/index.d.ts` 解析 | 存在? |
|
|
29
|
-
|------|----------|-------------------------|------|
|
|
30
|
-
| npm 发布 | 项目根 | `项目根/dist/index.d.ts` | 是 |
|
|
31
|
-
| `file:../../dist` | `dist/` | `dist/dist/index.d.ts` | 否 |
|
|
32
|
-
|
|
33
|
-
**因素二(助推):全局安装了旧版本。** 本地 symlink 解析失败后,TypeScript 沿目录树向上查找,在 `/Users/Admin/node_modules/bff-store/` 找到旧版 v0.1.1,该版本 `RemoteStorageOptions` 无 `backend` 字段。
|
|
34
|
-
|
|
35
|
-
### 修复
|
|
36
|
-
|
|
37
|
-
1. **新增** `scripts/adapt-dist-package.js` — 构建后将 `dist/package.json` 路径改写为相对 dist/:
|
|
38
|
-
- `dist/index.js` → `index.js`
|
|
39
|
-
- `./dist/index.d.ts` → `./index.d.ts`
|
|
40
|
-
- `./dist/storage/...` → `./storage/...`
|
|
41
|
-
|
|
42
|
-
2. **修改** `package.json` build 脚本:
|
|
43
|
-
```
|
|
44
|
-
"build": "tsup && cp package.json dist/package.json && node scripts/adapt-dist-package.js && for f in dist/*.d.mts; do cp \"$f\" \"${f%.d.mts}.d.ts\"; done"
|
|
45
|
-
```
|
|
46
|
-
|
|
47
|
-
### 涉及文件
|
|
48
|
-
|
|
49
|
-
| 文件 | 操作 |
|
|
50
|
-
|------|------|
|
|
51
|
-
| `scripts/adapt-dist-package.js` | 新增 |
|
|
52
|
-
| `package.json` | 修改 build 脚本 |
|
|
53
|
-
|
|
54
|
-
---
|
|
55
|
-
|
|
56
|
-
## Bug 2 (P0): GET/DELETE 请求拿不到 `mongoUrl`
|
|
57
|
-
|
|
58
|
-
### 现象
|
|
59
|
-
|
|
60
|
-
BFF 模式下,POST (`/storage/set`) 正常,但 GET (`/storage/get`) 返回:
|
|
61
|
-
|
|
62
|
-
```
|
|
63
|
-
{"error":"Error: mongoUrl is required for mongodb backend"}
|
|
64
|
-
```
|
|
65
|
-
|
|
66
|
-
### 根因
|
|
67
|
-
|
|
68
|
-
`src/storage/protocol.ts` 的 `buildUrl` / `buildBatchGetUrl` / `buildBatchSetUrl` 方法只将 `backend` 和 `entityId` 拼入 URL query params,`mongoUrl`、`mongoDb`、`jsonlDir` 仅通过 POST body 传递(`createStorageWithProtocol` 中将 `backendConfig` spread 到 body)。
|
|
69
|
-
|
|
70
|
-
GET/DELETE 无 body → server 端 `resolveStorage` 从 URL query params 解析不到 `mongoUrl` → 尝试 `mongodbStorage({ url: undefined })` → 抛错。
|
|
71
|
-
|
|
72
|
-
### 修复
|
|
73
|
-
|
|
74
|
-
抽取 `appendBackendParams()` 方法,将全部 backend 参数拼入 URL query params:
|
|
75
|
-
|
|
76
|
-
```typescript
|
|
77
|
-
private appendBackendParams(params: URLSearchParams): void {
|
|
78
|
-
if (this.backendConfig.backend) params.set('backend', this.backendConfig.backend);
|
|
79
|
-
if (this.backendConfig.mongoUrl) params.set('mongoUrl', this.backendConfig.mongoUrl);
|
|
80
|
-
if (this.backendConfig.mongoDb) params.set('mongoDb', this.backendConfig.mongoDb);
|
|
81
|
-
if (this.backendConfig.jsonlDir) params.set('jsonlDir', this.backendConfig.jsonlDir);
|
|
82
|
-
}
|
|
83
|
-
```
|
|
84
|
-
|
|
85
|
-
### 涉及文件
|
|
86
|
-
|
|
87
|
-
| 文件 | 操作 |
|
|
88
|
-
|------|------|
|
|
89
|
-
| `src/storage/protocol.ts` | 新增 `appendBackendParams()`,`buildUrl` / `buildBatchGetUrl` / `buildBatchSetUrl` 调用之 |
|
|
90
|
-
|
|
91
|
-
---
|
|
92
|
-
|
|
93
|
-
## Bug 3 (P2): JSONL adapter 静默失败(entityId 默认 null)
|
|
94
|
-
|
|
95
|
-
### 现象
|
|
96
|
-
|
|
97
|
-
BFF server 使用 JSONL backend 时,SET 返回 `{"success":true}` 但数据未写入磁盘,GET 返回 `{"value":null}`。
|
|
98
|
-
|
|
99
|
-
### 根因
|
|
100
|
-
|
|
101
|
-
`src/storage/jsonl.ts` 中 `entityId` 初始值为 `null`,GET 和 SET 操作均有 `if (!entityId) return` 守卫,导致静默跳过。对比 MongoDB adapter 默认 `currentEntityId = 'default'`,行为不一致。
|
|
102
|
-
|
|
103
|
-
### 修复
|
|
104
|
-
|
|
105
|
-
```typescript
|
|
106
|
-
// Before
|
|
107
|
-
let entityId: string | null = null;
|
|
108
|
-
|
|
109
|
-
// After
|
|
110
|
-
let entityId: string = 'default';
|
|
111
|
-
```
|
|
112
|
-
|
|
113
|
-
### 涉及文件
|
|
114
|
-
|
|
115
|
-
| 文件 | 操作 |
|
|
116
|
-
|------|------|
|
|
117
|
-
| `src/storage/jsonl.ts` | `entityId` 默认值 `null` → `'default'` |
|
|
118
|
-
|
|
119
|
-
---
|
|
120
|
-
|
|
121
|
-
## Bug 4 (P2): MongoDB 副本集连接失败
|
|
122
|
-
|
|
123
|
-
### 现象
|
|
124
|
-
|
|
125
|
-
BFF server 连接 MongoDB 时报:
|
|
126
|
-
|
|
127
|
-
```
|
|
128
|
-
MongoServerSelectionError: connection <monitor> to 172.16.42.101:27017 closed
|
|
129
|
-
TopologyDescription { type: 'ReplicaSetNoPrimary', setName: 'rs0' }
|
|
130
|
-
```
|
|
131
|
-
|
|
132
|
-
### 根因
|
|
133
|
-
|
|
134
|
-
`localhost:27017` 属于副本集 `rs0`,成员地址 `172.16.42.101:27017`。MongoDB driver 自动发现副本集后切换到该地址,但该地址网络不通。
|
|
135
|
-
|
|
136
|
-
### 修复
|
|
137
|
-
|
|
138
|
-
在 MongoDB URL 中拼接 `?directConnection=true&authSource=admin` 绕过副本集发现。
|
|
139
|
-
|
|
140
|
-
```typescript
|
|
141
|
-
const mongoUrl = `mongodb://${user}:${pwd}@${host}/${db}?directConnection=true&authSource=admin`;
|
|
142
|
-
```
|
|
143
|
-
|
|
144
|
-
### 涉及文件
|
|
145
|
-
|
|
146
|
-
| 文件 | 操作 |
|
|
147
|
-
|------|------|
|
|
148
|
-
| `tests/next_test/src/app/page.tsx` | 添加 URL 参数 |
|
|
149
|
-
|
|
150
|
-
---
|
|
151
|
-
|
|
152
|
-
## 测试覆盖
|
|
153
|
-
|
|
154
|
-
| 文件 | 内容 |
|
|
155
|
-
|------|------|
|
|
156
|
-
| `tests/next_test/test-types.ts` | 原始 bug 用例 — `backend: 'jsonl'` |
|
|
157
|
-
| `tests/next_test/test-similar-root.tsx` | 同上 .tsx 版本 |
|
|
158
|
-
| `tests/next_test/test-regression-remote-storage.ts` | 全参数组合:jsonl / mongodb / 可选字段 / 默认构造 |
|
|
159
|
-
| `tests/next_test/src/app/test-similar.tsx` | .tsx 中 remoteStorage 类型测试 |
|
|
160
|
-
| `tests/next_test/src/app/test-regression-store.tsx` | createStore + remoteStorage 组合测试 |
|
|
161
|
-
| `tests/next_test/src/app/test-mongodb.tsx` | MongoDB 前端组件测试 |
|
|
162
|
-
| `tests/next_test/test-mongodb-direct.ts` | mongodbStorage 直接调用测试脚本 |
|
|
163
|
-
| `tests/next_test/test-mongodb-remote.ts` | remoteStorage → MongoDB 路由测试脚本 |
|
|
164
|
-
| `tests/next_test/src/app/page.tsx` | 浏览器端到端测试(可切换 jsonl/mongodb) |
|
|
165
|
-
|
|
166
|
-
## 验证结果
|
|
167
|
-
|
|
168
|
-
- `npm run build`(项目根):构建成功
|
|
169
|
-
- `npx tsc --noEmit`(tests/next_test):0 errors
|
|
170
|
-
- `curl` 测试 JSONL SET/GET/DELETE 全链路:通过
|
|
171
|
-
- `curl` 测试 MongoDB SET/GET/DELETE 全链路:通过
|
|
File without changes
|