weifuwu 0.55.0 → 0.55.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/README.md CHANGED
@@ -150,7 +150,7 @@ createApp()
150
150
  |------|---------|------|
151
151
  | `weifuwu/client` | `https://unpkg.com/weifuwu@latest/dist/client/index.js` | 客户端核心(createApp, h, 路由, 状态管理等) |
152
152
  | `weifuwu/components` | `https://unpkg.com/weifuwu@latest/dist/components/index.js` | 41 个 UI 组件(Button, Card, Table, Modal 等) |
153
- | 组件样式 | `https://unpkg.com/weifuwu@latest/dist/components/style.css` | 组件 CSS + 72 个主题 Token + 35 个布局原语 |
153
+ | 组件样式 | `https://unpkg.com/weifuwu@latest/dist/components/style.css` | 组件 CSS + 82 个主题 Token + 35 个布局原语 |
154
154
  | 独立布局系统 | `https://unpkg.com/weifuwu@latest/dist/layout/weifuwu-layout.css` | 仅 CSS 布局,不依赖 JS |
155
155
 
156
156
 
@@ -179,7 +179,7 @@ createApp()
179
179
  | `weifuwu/client` | **lockScroll/trapFocus** | 滚动锁定 / 焦点陷阱工具 | — |
180
180
  | `weifuwu/client` | **popup** | 弹层 fixed 定位工具(`computeFixedPos` / `computeFixedPosRect`) | — |
181
181
  | `weifuwu/components` | **42 个组件** | Button/Table/Modal/Confirm/Toast/... + `confirm()` / `toast()` 命令式中间件 | weifuwu/client |
182
- | `weifuwu/layout` | **CSS 布局** | 35 个布局原语 + 72 个主题 Token(也支持 `weifuwu/layout/style.css`) | — |
182
+ | `weifuwu/layout` | **CSS 布局** | 35 个布局原语 + 82 个主题 Token(也支持 `weifuwu/layout/style.css`) | — |
183
183
 
184
184
  ---
185
185
 
@@ -465,7 +465,7 @@ app.get('/assets/*', serveStatic('./assets', {
465
465
 
466
466
  ## postgres — PostgreSQL 客户端(自研)
467
467
 
468
- > **自研 PG v3 协议**(零第三方依赖)——支持 SCRAM-SHA-256 认证、扩展查询(参数化)、类型映射、事务、连接池、schema 写前校验。
468
+ > **自研 PG v3 协议**(零第三方依赖)——支持 SCRAM-SHA-256 认证、扩展查询(参数化)、类型映射(int8 超范围自动 string 防丢精度)、事务、连接池(acquire 超时防饿死)、schema 写前校验、statement_timeout 慢查询保护。
469
469
 
470
470
  ```ts
471
471
  import { postgres } from 'weifuwu'
@@ -534,15 +534,47 @@ await ctx.sql.insert('decks', { title: 'x', status: 'INVALID' }) // → Validati
534
534
  | `ctx.sql.transaction(fn)` | 事务(回调收到 `{ query }`) |
535
535
  | `ctx.sql.register(table, schema)` | 注册表结构(写前校验) |
536
536
  | `ctx.sql.insert(table, row)` | schema 校验 + 参数化插入 |
537
+ | `ctx.sql\`...\` 内嵌片段` | 条件 SQL 片段(嵌套过滤,参数自动重编号) |
537
538
  | `ctx.sql.close()` | 关闭连接池 |
538
539
 
540
+ ### 条件片段(嵌套过滤)
541
+
542
+ ```ts
543
+ const status = req.query.status // 可能为空
544
+ const rows = await ctx.sql`
545
+ SELECT * FROM orders WHERE amount > ${100}
546
+ ${status ? ctx.sql`AND status = ${status}` : ctx.sql``}
547
+ `
548
+ // 空片段内联为空,参数自动重编号——同一 SQL 无论条件多少都安全参数化
549
+ ```
550
+
539
551
  ### 选项
540
552
 
541
553
  | 选项 | 类型 | 默认值 | 说明 |
542
554
  |------|------|--------|------|
543
- | `poolSize` | `number` | `10` | 连接池大小 |
555
+ | `connection` | `string` | `DATABASE_URL` | 连接字符串 |
556
+ | `max`(或 `poolSize`) | `number` | `10` | 连接池大小 |
544
557
  | `acquireTimeoutMs` | `number` | `30000` | 池全忙时 acquire 超时(防饿死,0=无限) |
545
- | `statementTimeoutMs` | `number` | `0` | 语句超时(慢查询保护,0=禁用) |
558
+ | `statementTimeoutMs`(或 `statementTimeout`) | `number` | `0` | 语句超时(慢查询保护,0=禁用) |
559
+ | `onQuery` | `(sql, durationMs, rowCount) => void` | — | 查询观测钩子(慢查询日志/审计) |
560
+
561
+ ### 幂等迁移(内置)
562
+
563
+ `postgres()` 返回的中间件自带迁移跟踪(`_weifuwu_migrations` 表),模块启动时检查-执行-记录三步幂等:
564
+
565
+ ```ts
566
+ const db = postgres()
567
+ await db.migrate() // ① 建迁移跟踪表(幂等)
568
+
569
+ if (!(await db.isMigrated('users'))) { // ② 检查是否已迁移
570
+ await db.sql.unsafe(`CREATE TABLE users (...)`)
571
+ await db.markMigrated('users') // ③ 记录(幂等,重复调用无害)
572
+ }
573
+
574
+ app.use(db)
575
+ ```
576
+
577
+ > 多副本部署时天然安全:`markMigrated` 用 `ON CONFLICT DO NOTHING`,两个实例同时迁移也不会重复执行。
546
578
 
547
579
  ### 错误映射(自动)
548
580
 
@@ -562,7 +594,7 @@ await ctx.sql.insert('decks', { title: 'x', status: 'INVALID' }) // → Validati
562
594
 
563
595
  ## redis — Redis 客户端(自研)
564
596
 
565
- > **自研 RESP2 协议**(零第三方依赖)——连接/重连/离线队列/管道/Pub-Sub + 消除 ioredis 高频痛点(TTL 参数顺序、JSON 手动序列化、缓存样板)。
597
+ > **自研 RESP2 协议**(零第三方依赖)——连接/重连(断线 pending 拒绝、指数退避)/离线队列/管道/Pub-Sub(订阅断线自动重放)+ 消除 ioredis 高频痛点(TTL 参数顺序、JSON 手动序列化、缓存样板)。
566
598
 
567
599
  ```ts
568
600
  import { redis } from 'weifuwu'
@@ -588,6 +620,26 @@ app.get('/llm/:id', async (req, ctx) => {
588
620
  }, 3600)
589
621
  return Response.json(result)
590
622
  })
623
+
624
+ // ④ Pub/Sub —— 发布用 ctx.redis,订阅用独立连接(回调式,断线自动重连恢复订阅)
625
+ app.post('/events', async (req, ctx) => {
626
+ await ctx.redis.publish('events', JSON.stringify({ type: 'deck.created' }))
627
+ })
628
+
629
+ const sub = ctx.redis.createSubscriber()
630
+ await sub.connect()
631
+ await sub.subscribe('events', (channel, message) => {
632
+ // 收到实时消息
633
+ })
634
+ await sub.psubscribe('jobs:*', (channel, message) => {
635
+ // 模式匹配订阅
636
+ })
637
+
638
+ // ⑤ 任意命令透传 + keyPrefix 隔离
639
+ await ctx.redis.command('LRANGE', 'list', '0', '-1')
640
+
641
+ app.use(redis({ keyPrefix: 'api:' })) // 之后所有 key 自动加前缀
642
+ await ctx.redis.set('user', 1) // 实际写入 'api:user'
591
643
  ```
592
644
 
593
645
  ### 方法面
@@ -967,6 +1019,20 @@ h('div', { class: 'x' }, child1, child2)
967
1019
  | `h(type, props, ...children)` | hyperscript |
968
1020
  | `jsx` / `jsxs` / `jsxDEV` | JSX 编译目标 |
969
1021
  | `Fragment` | 片段 |
1022
+ | `Portal` / `createPortal(children, portalKey?)` | 渲染到 `document.body#__wf_portal` 独立容器(弹层/对话框,脱离父级 overflow 裁剪) |
1023
+
1024
+ ```tsx
1025
+ import { createPortal } from 'weifuwu/client'
1026
+
1027
+ // 内容渲染到 body 下的独立容器(不在父组件的 DOM 树内)
1028
+ const Tooltip = (_init, ctx) =>
1029
+ (props) => createPortal(
1030
+ <div class="tooltip">{props.text}</div>
1031
+ )
1032
+
1033
+ // 配合 ctx.ui.selfId('name') 可从任何地方精准刷新 portal 内容
1034
+ ctx.ui.render(['name'])
1035
+ ```
970
1036
 
971
1037
  ---
972
1038
 
@@ -2061,7 +2127,7 @@ props 变化 ──────────────────────
2061
2127
 
2062
2128
  # 布局系统 (`weifuwu/layout`)
2063
2129
 
2064
- 纯 CSS 布局原语 + 72 个主题 Token。不绑定任何 JS 框架。
2130
+ 纯 CSS 布局原语 + 82 个主题 Token。不绑定任何 JS 框架。
2065
2131
 
2066
2132
  > **全栈 weifuwu 项目**:`weifuwu/components/style.css` 已包含布局系统,一条 import 就够了,无需单独引用本页。
2067
2133
  > 本页仅适用于**非 weifuwu 项目**或**只需 CSS 布局**的场景。
@@ -2121,7 +2187,7 @@ app.get('/layout.css', (req, ctx) => ctx.ui.css('weifuwu/layout'))
2121
2187
  | | `wf-inline-block` | display: inline-block |
2122
2188
  | | `wf-contents` | display: contents |
2123
2189
 
2124
- ## 72 个主题 Token
2190
+ ## 82 个主题 Token
2125
2191
 
2126
2192
  ```css
2127
2193
  /* 品牌色 */
package/dist/index.js CHANGED
@@ -2175,7 +2175,10 @@ function postgres(options) {
2175
2175
  user: decodeURIComponent(u.username),
2176
2176
  password: decodeURIComponent(u.password),
2177
2177
  database: u.pathname.replace(/^\//, ""),
2178
- poolSize: opts.max ?? 10
2178
+ poolSize: opts.max ?? opts.poolSize ?? 10,
2179
+ acquireTimeoutMs: opts.acquireTimeoutMs,
2180
+ statementTimeoutMs: opts.statementTimeoutMs ?? opts.statementTimeout,
2181
+ onQuery: opts.onQuery
2179
2182
  });
2180
2183
  const sql = makeSql(pool);
2181
2184
  const mw = ((req, ctx, next) => {
@@ -24,18 +24,26 @@ export interface PostgresInjected {
24
24
  sql: SqlClient;
25
25
  }
26
26
  export interface PostgresOptions {
27
+ /** 连接字符串(默认 DATABASE_URL) */
27
28
  connection?: string;
28
- signal?: AbortSignal;
29
- closeTimeout?: number;
30
29
  /** 池大小(连接数)。默认 10。 */
31
30
  max?: number;
31
+ /** 池全忙时 acquire 超时 ms(防饿死)。默认 30_000。0 = 无限。 */
32
+ acquireTimeoutMs?: number;
33
+ /** 语句超时 ms(慢查询保护,会话级 SET statement_timeout)。默认 0 = 禁用。 */
34
+ statementTimeoutMs?: number;
35
+ /** 查询观测钩子(慢查询日志/审计) */
36
+ onQuery?: (query: string, durationMs: number, rowCount: number) => void;
37
+ /** postgres.js 兼容名(= max) */
38
+ poolSize?: number;
39
+ /** postgres.js 兼容名(= statementTimeoutMs) */
40
+ statementTimeout?: number;
41
+ /** 连接超时 ms。默认 10_000。 */
42
+ connect_timeout?: number;
43
+ signal?: AbortSignal;
44
+ closeTimeout?: number;
32
45
  ssl?: boolean | Record<string, unknown>;
33
46
  idle_timeout?: number;
34
- connect_timeout?: number;
35
- /** 兼容保留(自研客户端暂以连接池替代 statement_timeout 注入) */
36
- statementTimeout?: number;
37
- /** Called after every query completes. */
38
- onQuery?: (query: string, durationMs: number, rowCount: number) => void;
39
47
  }
40
48
  export interface PostgresClient extends Middleware<Context, Context & PostgresInjected>, Closeable {
41
49
  sql: SqlClient;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "weifuwu",
3
3
  "type": "module",
4
- "version": "0.55.0",
4
+ "version": "0.55.1",
5
5
  "description": "AI SaaS framework — (req, ctx) => Response",
6
6
  "exports": {
7
7
  ".": {