@ohoooho/taoxian-runtime-sdk 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/CHANGELOG.md +7 -0
- package/README.md +26 -6
- package/examples/fenshen-http.mjs +34 -0
- package/fenshen.mjs +24 -4
- package/package.json +1 -1
- package/types/fenshen.d.ts +3 -1
- package/examples/fenshen-express.mjs +0 -27
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.1.1
|
|
4
|
+
|
|
5
|
+
- 修复登录回跳参数:移除随意定义的 `next`,改为 `return_to`,仅接受 Runtime 同源相对路径。
|
|
6
|
+
- `runtimeLoginUrl()` 不再默认把任意 `req.originalUrl` 放进回跳参数。
|
|
7
|
+
- `createFenshenAuth()` 新增 `loginReturnTo`,默认回到 Runtime `/`;跨端口回跳留给正式 handoff。
|
|
8
|
+
- 示例改为原生 `node:http`,不引入 Express。
|
|
9
|
+
|
|
3
10
|
## 0.1.0
|
|
4
11
|
|
|
5
12
|
- 首个公开 SDK 包结构:`runtime`、`fenshen`、`api-key`、`browser` 四个入口。
|
package/README.md
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
npm install @ohoooho/taoxian-runtime-sdk
|
|
7
7
|
```
|
|
8
8
|
|
|
9
|
-
当前版本:`0.1.
|
|
9
|
+
当前版本:`0.1.1`,license:MIT。Node.js >= 20。浏览器请使用 `@ohoooho/taoxian-runtime-sdk/browser`。
|
|
10
10
|
包是 **ESM-only**;CommonJS 项目请用动态 `import()` 或先转译。自带 TypeScript 声明。
|
|
11
11
|
|
|
12
12
|
## 入口
|
|
@@ -36,21 +36,41 @@ console.log(status.version, status.db.ready);
|
|
|
36
36
|
|
|
37
37
|
分身后端拿到浏览器的 Cookie header,服务端转发给 Runtime:
|
|
38
38
|
|
|
39
|
+
不依赖 Express,原生 `node:http` 就够了:
|
|
40
|
+
|
|
39
41
|
```js
|
|
40
|
-
import {
|
|
42
|
+
import { createServer } from 'node:http';
|
|
43
|
+
import { createFenshenAuth, runtimeLoginUrl } from '@ohoooho/taoxian-runtime-sdk/fenshen';
|
|
41
44
|
|
|
42
45
|
const auth = createFenshenAuth({
|
|
43
46
|
runtimeUrl: process.env.TAOXIAN_RUNTIME_URL || 'http://127.0.0.1:4319',
|
|
44
47
|
});
|
|
45
48
|
|
|
46
|
-
|
|
47
|
-
|
|
49
|
+
const server = createServer(async (req, res) => {
|
|
50
|
+
if (req.url === '/health') {
|
|
51
|
+
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
52
|
+
res.end(JSON.stringify({ ok: true }));
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const user = await auth.authenticate(req);
|
|
57
|
+
if (!user) {
|
|
58
|
+
res.writeHead(302, {
|
|
59
|
+
Location: runtimeLoginUrl(req, { returnTo: '/' }),
|
|
60
|
+
});
|
|
61
|
+
res.end();
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
res.end(`你好,${user.display_name || user.username}`);
|
|
66
|
+
});
|
|
48
67
|
```
|
|
49
68
|
|
|
50
69
|
- 未登录时 302 到同一个 host 的 Runtime 登录页
|
|
51
70
|
- Runtime 不可用时返回 503,不会重定向循环
|
|
52
71
|
- 不要自己再实现一套登录页
|
|
53
72
|
- 不要把 Cookie 暴露给浏览器 JS 或日志
|
|
73
|
+
- `returnTo` 只接受 Runtime 同源的相对路径;跨端口回跳需要正式的 handoff,不用裸 URL 参数
|
|
54
74
|
|
|
55
75
|
完整契约见 [`../docs/FENSHEN-INTEGRATION.md`](../docs/FENSHEN-INTEGRATION.md)。
|
|
56
76
|
|
|
@@ -115,7 +135,7 @@ const url = resolveFenshenUiUrl(fenshens[0]);
|
|
|
115
135
|
|
|
116
136
|
```html
|
|
117
137
|
<script type="module">
|
|
118
|
-
import { createRuntimeBrowserClient } from 'https://cdn.jsdelivr.net/npm/@ohoooho/taoxian-runtime-sdk@0.1.
|
|
138
|
+
import { createRuntimeBrowserClient } from 'https://cdn.jsdelivr.net/npm/@ohoooho/taoxian-runtime-sdk@0.1.1/browser.mjs';
|
|
119
139
|
const client = createRuntimeBrowserClient();
|
|
120
140
|
console.log(await client.getStatus());
|
|
121
141
|
</script>
|
|
@@ -123,7 +143,7 @@ const url = resolveFenshenUiUrl(fenshens[0]);
|
|
|
123
143
|
|
|
124
144
|
## 直接从仓库使用
|
|
125
145
|
|
|
126
|
-
|
|
146
|
+
已发布到 npm;本仓开发时也可以按相对路径引用源码:
|
|
127
147
|
|
|
128
148
|
```js
|
|
129
149
|
import { createFenshenAuth } from './sdk/fenshen.mjs';
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { createServer } from 'node:http';
|
|
2
|
+
import { createFenshenAuth, runtimeLoginUrl } from '@ohoooho/taoxian-runtime-sdk/fenshen';
|
|
3
|
+
|
|
4
|
+
const port = Number(process.env.PORT || process.env.TAOXIAN_FENSHEN_PORT || 4173);
|
|
5
|
+
const runtimeUrl = process.env.TAOXIAN_RUNTIME_URL || 'http://127.0.0.1:4319';
|
|
6
|
+
const auth = createFenshenAuth({ runtimeUrl });
|
|
7
|
+
|
|
8
|
+
const server = createServer(async (req, res) => {
|
|
9
|
+
if (req.url === '/health') {
|
|
10
|
+
res.writeHead(200, { 'Content-Type': 'application/json; charset=utf-8' });
|
|
11
|
+
res.end(JSON.stringify({ ok: true, schema_version: 1 }));
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const user = await auth.authenticate(req);
|
|
16
|
+
if (!user) {
|
|
17
|
+
res.writeHead(302, {
|
|
18
|
+
Location: runtimeLoginUrl(req, { runtimeUrl, returnTo: '/' }),
|
|
19
|
+
'Cache-Control': 'no-store',
|
|
20
|
+
});
|
|
21
|
+
res.end();
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
|
|
26
|
+
res.end(`<!doctype html>
|
|
27
|
+
<meta name="viewport" content="width=device-width,initial-scale=1">
|
|
28
|
+
<h1>你好,${user.display_name || user.username}</h1>
|
|
29
|
+
<p>这是原生 node:http 分身示例。</p>`);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
server.listen(port, '0.0.0.0', () => {
|
|
33
|
+
console.log(`fenshen listening on ${port}`);
|
|
34
|
+
});
|
package/fenshen.mjs
CHANGED
|
@@ -54,15 +54,28 @@ export function runtimePublicUrl(req, {
|
|
|
54
54
|
return url.toString();
|
|
55
55
|
}
|
|
56
56
|
|
|
57
|
+
export function normalizeReturnTo(value, { fallback = '/' } = {}) {
|
|
58
|
+
if (!value) return fallback;
|
|
59
|
+
const text = String(value);
|
|
60
|
+
if (!text.startsWith('/') || text.startsWith('//') || text.includes('\\')) return fallback;
|
|
61
|
+
if (/[\u0000-\u001f\u007f]/.test(text)) return fallback;
|
|
62
|
+
try {
|
|
63
|
+
const url = new URL(text, 'http://taoxian.invalid');
|
|
64
|
+
return `${url.pathname}${url.search}${url.hash}`;
|
|
65
|
+
} catch {
|
|
66
|
+
return fallback;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
57
70
|
export function runtimeLoginUrl(req, {
|
|
58
71
|
runtimeUrl = process.env.TAOXIAN_RUNTIME_URL || DEFAULT_RUNTIME_URL,
|
|
59
72
|
runtimePort,
|
|
60
73
|
trustProxy = false,
|
|
61
|
-
|
|
74
|
+
returnTo,
|
|
62
75
|
} = {}) {
|
|
63
76
|
const url = runtimeBaseUrl(req, { runtimeUrl, runtimePort, trustProxy });
|
|
64
|
-
const path =
|
|
65
|
-
if (path
|
|
77
|
+
const path = normalizeReturnTo(returnTo);
|
|
78
|
+
if (path !== '/') url.searchParams.set('return_to', path);
|
|
66
79
|
return url.toString();
|
|
67
80
|
}
|
|
68
81
|
|
|
@@ -89,6 +102,9 @@ export function createFenshenAuth({
|
|
|
89
102
|
timeoutMs = 2_000,
|
|
90
103
|
fetchImpl = globalThis.fetch,
|
|
91
104
|
trustProxy = false,
|
|
105
|
+
// For now this is a Runtime-relative path. Cross-service return (fenshen
|
|
106
|
+
// origin + port) needs a proper handoff/state flow, not a raw URL param.
|
|
107
|
+
loginReturnTo = '/',
|
|
92
108
|
onUnauthorized,
|
|
93
109
|
onUnavailable,
|
|
94
110
|
} = {}) {
|
|
@@ -102,7 +118,11 @@ export function createFenshenAuth({
|
|
|
102
118
|
if (!user) {
|
|
103
119
|
if (onUnauthorized) return onUnauthorized(req, res);
|
|
104
120
|
res.statusCode = 302;
|
|
105
|
-
res.setHeader('Location', runtimeLoginUrl(req, {
|
|
121
|
+
res.setHeader('Location', runtimeLoginUrl(req, {
|
|
122
|
+
runtimeUrl,
|
|
123
|
+
trustProxy,
|
|
124
|
+
returnTo: loginReturnTo,
|
|
125
|
+
}));
|
|
106
126
|
return res.end();
|
|
107
127
|
}
|
|
108
128
|
req.user = user;
|
package/package.json
CHANGED
package/types/fenshen.d.ts
CHANGED
|
@@ -5,6 +5,7 @@ export interface FenshenAuthOptions {
|
|
|
5
5
|
timeoutMs?: number;
|
|
6
6
|
fetchImpl?: FetchLike;
|
|
7
7
|
trustProxy?: boolean;
|
|
8
|
+
loginReturnTo?: string;
|
|
8
9
|
onUnauthorized?: (req: any, res: any) => any;
|
|
9
10
|
onUnavailable?: (req: any, res: any, error: unknown) => any;
|
|
10
11
|
}
|
|
@@ -23,7 +24,7 @@ export interface RuntimePublicUrlOptions {
|
|
|
23
24
|
}
|
|
24
25
|
|
|
25
26
|
export interface RuntimeLoginUrlOptions extends Omit<RuntimePublicUrlOptions, 'path'> {
|
|
26
|
-
|
|
27
|
+
returnTo?: string;
|
|
27
28
|
}
|
|
28
29
|
|
|
29
30
|
export class FenshenAuthError extends Error {
|
|
@@ -31,6 +32,7 @@ export class FenshenAuthError extends Error {
|
|
|
31
32
|
cause?: unknown;
|
|
32
33
|
}
|
|
33
34
|
|
|
35
|
+
export function normalizeReturnTo(value: unknown, options?: { fallback?: string }): string;
|
|
34
36
|
export function runtimePublicUrl(req: any, options?: RuntimePublicUrlOptions): string;
|
|
35
37
|
export function runtimeLoginUrl(req: any, options?: RuntimeLoginUrlOptions): string;
|
|
36
38
|
export function getFenshenUser(req: any, options?: Omit<FenshenAuthOptions, 'trustProxy' | 'onUnauthorized' | 'onUnavailable'>): Promise<LocalUser | null>;
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import express from 'express';
|
|
2
|
-
import { createFenshenAuth } from '@ohoooho/taoxian-runtime-sdk/fenshen';
|
|
3
|
-
|
|
4
|
-
const app = express();
|
|
5
|
-
const port = Number(process.env.PORT || 4173);
|
|
6
|
-
const auth = createFenshenAuth({
|
|
7
|
-
runtimeUrl: process.env.TAOXIAN_RUNTIME_URL || 'http://127.0.0.1:4319',
|
|
8
|
-
});
|
|
9
|
-
|
|
10
|
-
app.get('/health', (req, res) => {
|
|
11
|
-
res.json({ ok: true, schema_version: 1 });
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
// Browser cookie is validated by the fenshen backend, not by browser JS.
|
|
15
|
-
app.use(auth.middleware);
|
|
16
|
-
|
|
17
|
-
app.get('/', (req, res) => {
|
|
18
|
-
const name = req.user.display_name || req.user.username;
|
|
19
|
-
res.type('html').send(`<!doctype html>
|
|
20
|
-
<meta name="viewport" content="width=device-width,initial-scale=1">
|
|
21
|
-
<h1>你好,${name}</h1>
|
|
22
|
-
<p>这是分身自己的业务页面。</p>`);
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
app.listen(port, '0.0.0.0', () => {
|
|
26
|
-
console.log(`fenshen listening on ${port}`);
|
|
27
|
-
});
|