@adep/vite-plugin 0.1.9 → 0.1.11

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 ADDED
@@ -0,0 +1,66 @@
1
+ # @adep/vite-plugin — AgentDeploy 云函数 Vite 开发插件
2
+
3
+ > **云函数本地开发与预览注入**(IDE-030 自 `@adep/cli/vite` 抽出):`vite dev` 进程内拉起本地
4
+ > `adep dev` 模拟运行时、把 `/{prefix}/*` 代理到云函数,并注入预览脚本三件套(fetch 拦截器 /
5
+ > 控制台中继 / 自动化中继)。
6
+
7
+ ```bash
8
+ npm install -D @adep/vite-plugin
9
+ ```
10
+
11
+ ## 用法
12
+
13
+ ```ts
14
+ // vite.config.ts
15
+ import { defineConfig } from 'vite'
16
+ import vue from '@vitejs/plugin-vue'
17
+ import adep from '@adep/vite-plugin'
18
+
19
+ export default defineConfig({
20
+ plugins: [
21
+ vue(),
22
+ adep({
23
+ cwd: process.cwd(), // 应用根目录,读取 adep.config.ts 与 functions/
24
+ // functionsPrefix: '/api', // 函数路由前缀,默认从 adep.config.ts 读取
25
+ }),
26
+ ],
27
+ })
28
+ ```
29
+
30
+ ## 能力总览
31
+
32
+ | 能力 | 说明 |
33
+ | --- | --- |
34
+ | 进程内 dev server | `vite dev` 启动时自动拉起 `adep dev` 模拟运行时,函数改动即热重载 |
35
+ | `/api/*` 代理 | 前端 `fetch('/api/hello')` 被代理到本地模拟运行时的云函数 |
36
+ | Web IDE fetch 拦截器 | serve `/@adep/fn-proxy.js`(`FN_FETCH_INTERCEPTOR_SCRIPT`):预览 iframe 内**同源** `/api/*` 请求经 postMessage 转发到 IDE 主线程执行函数草稿 |
37
+ | 预览控制台中继 | 注入 `CONSOLE_RELAY_SCRIPT`:应用 `console.*` 与未捕获错误回传父窗口,并侦测 vite 构建错误浮层(接通 IDE「报错收件箱」) |
38
+ | 预览自动化中继 | 注入 `PREVIEW_AUTOMATION_SCRIPT`:IDE 自动化命令经 message 驱动预览页 |
39
+
40
+ ## fetch 拦截器的劫持面(同源收窄)
41
+
42
+ 拦截器只劫持 **`u.origin === window.location.origin`** 的 `/api/*` 请求(IDE-054,2026-11-03):
43
+
44
+ - 应用自身的函数面(相对路径 `/api/count` 或预览子域绝对 URL)→ 经 postMessage 走 IDE 主线程;
45
+ - **跨域** `/api/*`(平台 OIDC 登录面 `https://<平台域>/api/oauth/*`、三方 API)→ 放行原生 fetch
46
+ 直达目标。此前只看 pathname,预览里接平台 OIDC 登录会被劫进模拟运行时按「函数不存在」报错;
47
+ - blob 预览(浏览器内 vite-dev)继承创建者 origin,相对 `/api/*` 仍判同源,行为不变;
48
+ 该形态下以绝对 URL 调平台 API 与自身函数面同 origin,无法区分,维持劫持(已知限制)。
49
+
50
+ 注入脚本仅在 `serve`(`vite` / `vite dev`)时写入 index.html;`vite build` 产物不注入
51
+ (dev-only 脚本进了静态托管会把 HTML 当 JS 执行报 `SyntaxError`)。
52
+
53
+ ## 开发态 vs 生产态
54
+
55
+ | 环境 | 请求路径 | 路由去向 |
56
+ | --- | --- | --- |
57
+ | **开发态**(vite dev) | `fetch('/api/*')`(同源) | 插件代理到本地 `adep dev` 模拟运行时 |
58
+ | **生产态**(已部署站点) | `fetch('/api/*')` | 由平台网关路由到已发布的云函数 |
59
+
60
+ 两种态走同一份业务代码,无需分支判断。
61
+
62
+ ## 相关资源
63
+
64
+ - [Vite 插件文档(docs 站)](https://adep.top/sdk/vite-plugin)
65
+ - [浏览器端 SDK(@adep/client)](https://adep.top/sdk/client)
66
+ - [CLI 工具](https://adep.top/guide/cli)
@@ -26,10 +26,20 @@
26
26
  * blob URL SPA 路由:patch `history.pushState/replaceState`,对相对路径直接吞掉(blob 不可
27
27
  * 分层,原生 pushState 解析相对路径抛 TypeError),`location.pathname` 覆盖为当前路由路径,
28
28
  * sessionStorage 持久化,刷新后恢复。
29
+ *
30
+ * 同源收窄(2026-11-03 生产实录):拦截判定原先只看 pathname,把**跨域绝对 URL** 中路径
31
+ * 恰为 /api/* 的请求也劫走——用户应用在预览里接平台 OIDC 登录(issuer 推导为
32
+ * `https://<平台域>/api`,register 路径 `/api/oauth/register`),请求被送进模拟函数运行时
33
+ * 按「函数 oauth 不存在」回 4xx/5xx,登录按钮报「注册登录客户端失败(HTTP 500)」,且
34
+ * 请求从未离开浏览器(服务端 access.log 无痕)。故拦截限**同源**(`u.origin ===
35
+ * location.origin`)的 /api/*:应用自身的函数面(相对路径或预览子域绝对 URL)不变;
36
+ * 跨域 /api/*(平台 OIDC 端点、三方 API)一律放行原生 fetch。blob 预览继承创建者 origin,
37
+ * 相对 /api/* 经 location.origin 兜底解析后仍判同源,行为不变;该形态下应用若以绝对 URL
38
+ * 调平台 API 则与自身函数面同 origin、无法区分,仍会被劫持——已知限制,非本收窄目标。
29
39
  */
30
40
  import type { IncomingMessage, ServerResponse } from 'node:http';
31
41
  /** 浏览器端 fetch 拦截器脚本(IIFE,纯 JS——必须能直接内联进 script 标签,无 TS / ESM)。 */
32
- export declare const FN_FETCH_INTERCEPTOR_SCRIPT = "(function () {\n if (window.__adepFnProxyInstalled) return;\n window.__adepFnProxyInstalled = true;\n var ORIGINAL_FETCH = window.fetch.bind(window);\n var pending = {};\n var nextId = 1;\n var TIMEOUT_MS = 15000;\n\n window.addEventListener(\"message\", function (event) {\n var data = event.data;\n if (!data || data.__adepFnResponse !== true) return;\n var entry = pending[data.id];\n if (!entry) return;\n delete pending[data.id];\n clearTimeout(entry.timer);\n var headers;\n try { headers = new Headers(data.headers || {}); }\n catch (e) { headers = new Headers({ \"content-type\": \"text/plain\" }); }\n var body = data.body == null ? null : data.body;\n try {\n entry.resolve(new Response(body, { status: data.status || 200, statusText: \"\", headers: headers }));\n } catch (e2) {\n entry.resolve(new Response(String(body), { status: data.status || 200, headers: { \"content-type\": \"text/plain\" } }));\n }\n });\n\n function readBody(input, init) {\n if (typeof Request !== \"undefined\" && input instanceof Request) {\n return input.text().catch(function () { return null; });\n }\n var body = init && init.body !== undefined ? init.body : input;\n if (body == null) return Promise.resolve(null);\n if (typeof body === \"string\") return Promise.resolve(body);\n if (typeof ArrayBuffer !== \"undefined\" && body instanceof ArrayBuffer) {\n return Promise.resolve(new TextDecoder().decode(body));\n }\n if (typeof Blob !== \"undefined\" && body instanceof Blob) return body.text();\n if (typeof URLSearchParams !== \"undefined\" && body instanceof URLSearchParams) {\n return Promise.resolve(body.toString());\n }\n try { return Promise.resolve(String(body)); } catch (e) { return Promise.resolve(null); }\n }\n\n function collectHeaders(input, init) {\n var out = {};\n try { input.headers.forEach(function (v, k) { out[k] = v; }); } catch (e) {}\n if (init && init.headers) {\n try { new Headers(init.headers).forEach(function (v, k) { out[k] = v; }); } catch (e2) {}\n }\n return out;\n }\n\n window.fetch = function (input, init) {\n var urlStr;\n var method;\n var headers = {};\n var isRequest = typeof Request !== \"undefined\" && input instanceof Request;\n if (isRequest) {\n urlStr = String(input.url);\n method = (init && init.method) || input.method || \"GET\";\n headers = collectHeaders(input, init);\n } else if (typeof input === \"string\" || (typeof URL !== \"undefined\" && input instanceof URL)) {\n urlStr = String(input);\n method = (init && init.method) || \"GET\";\n try { new Headers(init && init.headers || {}).forEach(function (v, k) { headers[k] = v; }); } catch (e) {}\n } else {\n return ORIGINAL_FETCH(input, init);\n }\n var u;\n try { u = new URL(urlStr, document.baseURI); }\n catch (e0) {\n try { u = new URL(urlStr, window.location.origin); }\n catch (e1) { return ORIGINAL_FETCH(input, init); }\n }\n if (u.pathname.indexOf(\"/api/\") !== 0 && u.pathname !== \"/api\") {\n return ORIGINAL_FETCH(input, init);\n }\n var bridgeWindow = window.parent !== window ? window.parent : window.opener;\n if (!bridgeWindow) return ORIGINAL_FETCH(input, init);\n method = (method || \"GET\").toUpperCase();\n var target = u.pathname + u.search;\n return readBody(input, init).then(function (textBody) {\n return new Promise(function (resolve) {\n var id = nextId++;\n var timer = setTimeout(function () {\n delete pending[id];\n resolve(new Response('{\"error\":{\"code\":\"FN_PROXY_TIMEOUT\",\"message\":\"\\u4e91\\u51fd\\u6570\\u6267\\u884c\\u8d85\\u65f6\"}}', { status: 504, headers: { \"content-type\": \"application/json\" } }));\n }, TIMEOUT_MS);\n pending[id] = { resolve: resolve, timer: timer };\n bridgeWindow.postMessage({ __adepFnRequest: true, id: id, method: method, url: target, headers: headers, body: textBody }, \"*\");\n });\n });\n };\n\n /* \u2014\u2014 blob URL SPA \u8DEF\u7531\u652F\u6301 \u2014\u2014\n blob: \u662F\u4E0D\u53EF\u5206\u5C42 scheme\uFF0Chistory.pushState('/about') \u5185\u90E8\u89E3\u6790\u76F8\u5BF9\u8DEF\u5F84\u4F1A\u629B\n TypeError \u2192 SPA history \u6A21\u5F0F\u8DEF\u7531\u5D29\u6E83\u3002patch pushState/replaceState\uFF0C\u5BF9\u76F8\u5BF9\n \u8DEF\u5F84\u76F4\u63A5\u541E\u6389\uFF08\u4E0D\u5BFC\u822A\u3001\u4E0D\u629B\u9519\uFF09\uFF0C\u8DEF\u7531\u5E93\u5185\u90E8\u72B6\u6001\u81EA\u884C\u66F4\u65B0\uFF1Blocation.pathname\n \u8986\u76D6\u4E3A\u5F53\u524D\u8DEF\u7531\u8DEF\u5F84\uFF0C\u4F9B\u8DEF\u7531\u5E93\u521D\u59CB\u5316\u65F6\u8BFB\u53D6\u3002sessionStorage \u6301\u4E45\u5316\uFF0C\u5237\u65B0\u540E\u6062\u590D\u3002 */\n if (window.location.protocol === \"blob:\") {\n var SPA_KEY = \"__adep_spa_path\";\n var spaPath = \"/\";\n try {\n var _saved = window.sessionStorage.getItem(SPA_KEY);\n if (_saved) spaPath = _saved;\n } catch (e) {}\n try {\n Object.defineProperty(window.location, \"pathname\", {\n get: function () { return spaPath; },\n configurable: true\n });\n } catch (e) {}\n var _origPush = window.history.pushState.bind(window.history);\n var _origReplace = window.history.replaceState.bind(window.history);\n function _isRelative(url) {\n if (!url || typeof url !== \"string\") return false;\n if (url.charAt(0) === \"#\") return false;\n if (/^[a-zA-Z][a-zA-Z0-9+.-]*:/.test(url)) return false;\n return true;\n }\n function _resolvePath(url) {\n var qi = url.indexOf(\"?\");\n var hi = url.indexOf(\"#\");\n var end = url.length;\n if (qi !== -1) end = qi;\n if (hi !== -1 && hi < end) end = hi;\n var p = url.slice(0, end);\n if (p.charAt(0) !== \"/\") {\n var base = spaPath.slice(0, spaPath.lastIndexOf(\"/\") + 1);\n p = base + p;\n }\n var parts = p.split(\"/\");\n var out = [];\n for (var i = 0; i < parts.length; i++) {\n var seg = parts[i];\n if (seg === \"\" || seg === \".\") continue;\n if (seg === \"..\") { out.pop(); continue; }\n out.push(seg);\n }\n return \"/\" + out.join(\"/\");\n }\n window.history.pushState = function (state, title, url) {\n if (_isRelative(url)) {\n spaPath = _resolvePath(url);\n try { window.sessionStorage.setItem(SPA_KEY, spaPath); } catch (e) {}\n return;\n }\n return _origPush(state, title, url);\n };\n window.history.replaceState = function (state, title, url) {\n if (_isRelative(url)) {\n spaPath = _resolvePath(url);\n try { window.sessionStorage.setItem(SPA_KEY, spaPath); } catch (e) {}\n return;\n }\n return _origReplace(state, title, url);\n };\n }\n})();";
42
+ export declare const FN_FETCH_INTERCEPTOR_SCRIPT = "(function () {\n if (window.__adepFnProxyInstalled) return;\n window.__adepFnProxyInstalled = true;\n var ORIGINAL_FETCH = window.fetch.bind(window);\n var pending = {};\n var nextId = 1;\n var TIMEOUT_MS = 15000;\n\n window.addEventListener(\"message\", function (event) {\n var data = event.data;\n if (!data || data.__adepFnResponse !== true) return;\n var entry = pending[data.id];\n if (!entry) return;\n delete pending[data.id];\n clearTimeout(entry.timer);\n var headers;\n try { headers = new Headers(data.headers || {}); }\n catch (e) { headers = new Headers({ \"content-type\": \"text/plain\" }); }\n var body = data.body == null ? null : data.body;\n try {\n entry.resolve(new Response(body, { status: data.status || 200, statusText: \"\", headers: headers }));\n } catch (e2) {\n entry.resolve(new Response(String(body), { status: data.status || 200, headers: { \"content-type\": \"text/plain\" } }));\n }\n });\n\n function readBody(input, init) {\n if (typeof Request !== \"undefined\" && input instanceof Request) {\n return input.text().catch(function () { return null; });\n }\n var body = init && init.body !== undefined ? init.body : input;\n if (body == null) return Promise.resolve(null);\n if (typeof body === \"string\") return Promise.resolve(body);\n if (typeof ArrayBuffer !== \"undefined\" && body instanceof ArrayBuffer) {\n return Promise.resolve(new TextDecoder().decode(body));\n }\n if (typeof Blob !== \"undefined\" && body instanceof Blob) return body.text();\n if (typeof URLSearchParams !== \"undefined\" && body instanceof URLSearchParams) {\n return Promise.resolve(body.toString());\n }\n try { return Promise.resolve(String(body)); } catch (e) { return Promise.resolve(null); }\n }\n\n function collectHeaders(input, init) {\n var out = {};\n try { input.headers.forEach(function (v, k) { out[k] = v; }); } catch (e) {}\n if (init && init.headers) {\n try { new Headers(init.headers).forEach(function (v, k) { out[k] = v; }); } catch (e2) {}\n }\n return out;\n }\n\n window.fetch = function (input, init) {\n var urlStr;\n var method;\n var headers = {};\n var isRequest = typeof Request !== \"undefined\" && input instanceof Request;\n if (isRequest) {\n urlStr = String(input.url);\n method = (init && init.method) || input.method || \"GET\";\n headers = collectHeaders(input, init);\n } else if (typeof input === \"string\" || (typeof URL !== \"undefined\" && input instanceof URL)) {\n urlStr = String(input);\n method = (init && init.method) || \"GET\";\n try { new Headers(init && init.headers || {}).forEach(function (v, k) { headers[k] = v; }); } catch (e) {}\n } else {\n return ORIGINAL_FETCH(input, init);\n }\n var u;\n try { u = new URL(urlStr, document.baseURI); }\n catch (e0) {\n try { u = new URL(urlStr, window.location.origin); }\n catch (e1) { return ORIGINAL_FETCH(input, init); }\n }\n if (\n u.origin !== window.location.origin ||\n (u.pathname.indexOf(\"/api/\") !== 0 && u.pathname !== \"/api\")\n ) {\n return ORIGINAL_FETCH(input, init);\n }\n var bridgeWindow = window.parent !== window ? window.parent : window.opener;\n if (!bridgeWindow) return ORIGINAL_FETCH(input, init);\n method = (method || \"GET\").toUpperCase();\n var target = u.pathname + u.search;\n return readBody(input, init).then(function (textBody) {\n return new Promise(function (resolve) {\n var id = nextId++;\n var timer = setTimeout(function () {\n delete pending[id];\n resolve(new Response('{\"error\":{\"code\":\"FN_PROXY_TIMEOUT\",\"message\":\"\\u4e91\\u51fd\\u6570\\u6267\\u884c\\u8d85\\u65f6\"}}', { status: 504, headers: { \"content-type\": \"application/json\" } }));\n }, TIMEOUT_MS);\n pending[id] = { resolve: resolve, timer: timer };\n bridgeWindow.postMessage({ __adepFnRequest: true, id: id, method: method, url: target, headers: headers, body: textBody }, \"*\");\n });\n });\n };\n\n /* \u2014\u2014 blob URL SPA \u8DEF\u7531\u652F\u6301 \u2014\u2014\n blob: \u662F\u4E0D\u53EF\u5206\u5C42 scheme\uFF0Chistory.pushState('/about') \u5185\u90E8\u89E3\u6790\u76F8\u5BF9\u8DEF\u5F84\u4F1A\u629B\n TypeError \u2192 SPA history \u6A21\u5F0F\u8DEF\u7531\u5D29\u6E83\u3002patch pushState/replaceState\uFF0C\u5BF9\u76F8\u5BF9\n \u8DEF\u5F84\u76F4\u63A5\u541E\u6389\uFF08\u4E0D\u5BFC\u822A\u3001\u4E0D\u629B\u9519\uFF09\uFF0C\u8DEF\u7531\u5E93\u5185\u90E8\u72B6\u6001\u81EA\u884C\u66F4\u65B0\uFF1Blocation.pathname\n \u8986\u76D6\u4E3A\u5F53\u524D\u8DEF\u7531\u8DEF\u5F84\uFF0C\u4F9B\u8DEF\u7531\u5E93\u521D\u59CB\u5316\u65F6\u8BFB\u53D6\u3002sessionStorage \u6301\u4E45\u5316\uFF0C\u5237\u65B0\u540E\u6062\u590D\u3002 */\n if (window.location.protocol === \"blob:\") {\n var SPA_KEY = \"__adep_spa_path\";\n var spaPath = \"/\";\n try {\n var _saved = window.sessionStorage.getItem(SPA_KEY);\n if (_saved) spaPath = _saved;\n } catch (e) {}\n try {\n Object.defineProperty(window.location, \"pathname\", {\n get: function () { return spaPath; },\n configurable: true\n });\n } catch (e) {}\n var _origPush = window.history.pushState.bind(window.history);\n var _origReplace = window.history.replaceState.bind(window.history);\n function _isRelative(url) {\n if (!url || typeof url !== \"string\") return false;\n if (url.charAt(0) === \"#\") return false;\n if (/^[a-zA-Z][a-zA-Z0-9+.-]*:/.test(url)) return false;\n return true;\n }\n function _resolvePath(url) {\n var qi = url.indexOf(\"?\");\n var hi = url.indexOf(\"#\");\n var end = url.length;\n if (qi !== -1) end = qi;\n if (hi !== -1 && hi < end) end = hi;\n var p = url.slice(0, end);\n if (p.charAt(0) !== \"/\") {\n var base = spaPath.slice(0, spaPath.lastIndexOf(\"/\") + 1);\n p = base + p;\n }\n var parts = p.split(\"/\");\n var out = [];\n for (var i = 0; i < parts.length; i++) {\n var seg = parts[i];\n if (seg === \"\" || seg === \".\") continue;\n if (seg === \"..\") { out.pop(); continue; }\n out.push(seg);\n }\n return \"/\" + out.join(\"/\");\n }\n window.history.pushState = function (state, title, url) {\n if (_isRelative(url)) {\n spaPath = _resolvePath(url);\n try { window.sessionStorage.setItem(SPA_KEY, spaPath); } catch (e) {}\n return;\n }\n return _origPush(state, title, url);\n };\n window.history.replaceState = function (state, title, url) {\n if (_isRelative(url)) {\n spaPath = _resolvePath(url);\n try { window.sessionStorage.setItem(SPA_KEY, spaPath); } catch (e) {}\n return;\n }\n return _origReplace(state, title, url);\n };\n }\n})();";
33
43
  /**
34
44
  * 浏览器端控制台中继脚本(IIFE,纯 JS——必须能直接内联进 script 标签,无 TS / ESM)。
35
45
  *
package/dist/index.js CHANGED
@@ -594,7 +594,10 @@ var FN_FETCH_INTERCEPTOR_SCRIPT = `(function () {
594
594
  try { u = new URL(urlStr, window.location.origin); }
595
595
  catch (e1) { return ORIGINAL_FETCH(input, init); }
596
596
  }
597
- if (u.pathname.indexOf("/api/") !== 0 && u.pathname !== "/api") {
597
+ if (
598
+ u.origin !== window.location.origin ||
599
+ (u.pathname.indexOf("/api/") !== 0 && u.pathname !== "/api")
600
+ ) {
598
601
  return ORIGINAL_FETCH(input, init);
599
602
  }
600
603
  var bridgeWindow = window.parent !== window ? window.parent : window.opener;
package/package.json CHANGED
@@ -7,7 +7,7 @@
7
7
  },
8
8
  "homepage": "https://adep.top",
9
9
  "bugs": "https://gitcode.com/yunqiang_wu/ai-yunke/issues",
10
- "version": "0.1.9",
10
+ "version": "0.1.11",
11
11
  "private": false,
12
12
  "type": "module",
13
13
  "description": "云函数 Vite 插件(IDE-030,自 CLI-014 @adep/cli/vite 抽出):vite dev 进程内启动本地 adep dev server 并代理 /{prefix}/* 到云函数;同时 serve /@adep/fn-proxy.js(Web IDE 预览 fetch 拦截器)。本地运行时与配置加载经 createDevServer / loadConfig 注入端口接入,供 CLI / Web IDE / 非 CLI 用户独立使用。",