@adep/web-container 0.1.1 → 0.2.0

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.
@@ -14,7 +14,7 @@
14
14
  */
15
15
  import type { WebContainerFsEntry } from '@adep/types';
16
16
  /** shell 内建命令(`shell.ts` 的 switch 分支,`help` 文案由此派生,避免两处清单漂移)。 */
17
- export declare const SHELL_COMMANDS: readonly ["pwd", "ls", "cat", "echo", "mkdir", "touch", "rm", "rmdir", "cp", "mv", "cd", "head", "tail", "test", "node", "js", "help"];
17
+ export declare const SHELL_COMMANDS: readonly ["pwd", "ls", "cat", "echo", "mkdir", "touch", "rm", "rmdir", "cp", "mv", "cd", "head", "tail", "curl", "wget", "test", "node", "js", "vite", "help"];
18
18
  /** 只在补全里出现的平台命令(由上层拦截实现,非 shell 内建)。 */
19
19
  export declare const EXTRA_COMMANDS: readonly ["npm"];
20
20
  /** 命令位可补的全部命令。 */
@@ -13,6 +13,7 @@ import type { CloudFetch } from '@adep/types';
13
13
  import { VirtualFileSystem } from './vfs';
14
14
  import { type JsRuntime } from './shell';
15
15
  import type { TestReport } from './test-framework';
16
+ import type { SfcCompiler } from './vite-dev';
16
17
  export interface BootstrapOptions {
17
18
  /**
18
19
  * 平台 npm 中继基础路径(浏览器端相对当前源,如 `/api/v1/npm-relay`)。
@@ -24,6 +25,17 @@ export interface BootstrapOptions {
24
25
  runtime?: JsRuntime;
25
26
  /** npm 中继网络实现(缺省 globalThis.fetch;测试注入假中继)。 */
26
27
  fetchImpl?: CloudFetch;
28
+ /**
29
+ * 平台 HTTP 中继基础路径(浏览器端相对当前源,如 `/api/v1/http-relay`)。
30
+ * shell 的 `curl` / `wget` 经此中继访问任意主机(规避浏览器跨域限制;服务端带 SSRF 防护)。
31
+ * 缺省 `/api/v1/http-relay`;显式传 `net` 可整体替换网络实现。
32
+ */
33
+ httpRelayBaseUrl?: string;
34
+ /**
35
+ * `curl` / `wget` 的网络端口(受控 fetch 契约同 `cloud.fetch`)。
36
+ * 缺省按 `httpRelayBaseUrl` 装配成平台 HTTP 中继客户端;测试 / 自定义环境可直接注入。
37
+ */
38
+ net?: CloudFetch;
27
39
  /**
28
40
  * 注入既有 VFS 实例(FE-022):容器默认自建内存 VFS,接持久化(`createFsPersistence`)
29
41
  * 需要上层先拿到同一个实例去 `hydrate()`,故开此注入点。省略即原行为。
@@ -41,5 +53,10 @@ export interface BootstrapOptions {
41
53
  * (`@adep/types`)不含这两者,故经装配选项回灌,不为此改动对外 SDK 契约。
42
54
  */
43
55
  onTestResult?: (report: TestReport) => void;
56
+ /**
57
+ * `.vue` SFC 编译器注入点(`vite` 命令):IDE 侧动态 import `vue/compiler-sfc` 后装配
58
+ * (本包零第三方依赖,不直接依赖 vue);未注入时 vite 遇 `.vue` 模块如实报「未装配」。
59
+ */
60
+ viteCompiler?: SfcCompiler;
44
61
  }
45
62
  export declare function bootstrap(options: BootstrapOptions): WebContainer;
@@ -0,0 +1,54 @@
1
+ /**
2
+ * Web IDE 预览 ↔ 云函数草稿的 postMessage 桥(CLI-014 的 Web IDE 对应物)。
3
+ *
4
+ * 浏览器内 vite-dev 预览(blob URL)与 Nodebox 真 vite 预览(异源 iframe)都不直接处理
5
+ * HTTP 请求——它们把 `/api/*` 当成「要发到后端的请求」。本模块在预览 iframe 内注入一段
6
+ * fetch 拦截器,把 `/api/*` 请求经 `window.parent.postMessage` 转发到 IDE 主线程;主线程
7
+ * 用 `OfflineRunner` 执行**当前项目的云函数草稿**(改函数即生效,因为每次都实时取草稿),
8
+ * 再把执行结果 postMessage 回 iframe。与 origin 无关:blob URL 与异源 Nodebox 隧道都能用。
9
+ *
10
+ * 消息协议(两侧逐字对齐;改任一侧必须同步另一侧——见 web-project-template.ts 的
11
+ * web/vite.config.ts 模板里那份逐字复制的 FN_PROXY_SCRIPT):
12
+ * - 请求(iframe → parent):`{ __adepFnRequest: true, id, method, url, headers, body }`
13
+ * - `url` 为 pathname + search(不含 origin),如 `/api/hello?a=1`;
14
+ * - `body` 为已读成文本的请求体(GET 为 null)。
15
+ * - 响应(parent → iframe,经 event.source.postMessage 单播,不广播):
16
+ * `{ __adepFnResponse: true, id, status, headers, body }`
17
+ * - `body` 为文本(对象侧已 JSON.stringify)。
18
+ *
19
+ * 函数名解析与 CLI-014 `resolveRouteFnName` 同口径:`/api/{fnName}/...` 剥 `/api/` 后第一段
20
+ * 为函数名,剩余路径作为 `ctx.path`(无剩余则 `/`)。
21
+ */
22
+ import type { CapabilityBundle } from '@adep/types';
23
+ import type { OfflineRunInput, OfflineRunResult } from './sim/runner';
24
+ /** 离线函数运行器的结构面(`createOfflineFunctionRunner()` 的返回值;这里只依赖 `run`)。 */
25
+ export interface OfflineRunner {
26
+ run(input: OfflineRunInput): Promise<OfflineRunResult>;
27
+ }
28
+ /**
29
+ * 浏览器端 fetch 拦截器脚本(IIFE,纯 JS——必须能直接内联进 `<script>`,不能有 TS / ESM)。
30
+ *
31
+ * **同步纪律**:本字符串与 `shared/sdk/web-project-template.ts` 里 web/vite.config.ts 模板的
32
+ * `FN_PROXY_SCRIPT` 必须**逐字一致**(两处复制粘贴,无共享 import——预览 iframe 与浏览器内
33
+ * vite-dev 是两个独立加载上下文,共享一份源码反而会引入打包耦合)。改这里务必同步另一侧。
34
+ */
35
+ export declare const FN_FETCH_INTERCEPTOR_SCRIPT: string;
36
+ /** IDE 主线程侧的消息处理器类型:直接挂到 `window.addEventListener('message', handler)`。 */
37
+ export type FunctionFetchHandler = (event: MessageEvent) => void;
38
+ export interface FunctionFetchHandlerOptions {
39
+ /**
40
+ * 按函数名取草稿文件表(函数名 → 相对路径 → 文件内容)。同步返回当前草稿(含未保存改动);
41
+ * 也允许异步(如对未打开的函数回源拉取)。返回 null / 空表 → 404 FN_NOT_FOUND。
42
+ */
43
+ getFunctionFiles: (name: string) => Record<string, string> | null | Promise<Record<string, string> | null>;
44
+ /** 离线函数运行器(`createBrowserSimRuntime().runner`)。 */
45
+ runner: OfflineRunner;
46
+ /** 能力 bundle(`createBrowserSimRuntime().bundle`)——函数内 cloud.* 的落点。 */
47
+ bundle: CapabilityBundle;
48
+ }
49
+ /**
50
+ * 创建 IDE 主线程侧的 message 处理器:收 iframe 的 `__adepFnRequest` → 解析函数名 → 取草稿
51
+ * → 离线执行 → 经 `event.source.postMessage` 单播回响应。非本协议的消息原样放过(不影响
52
+ * IDE 页面其它 postMessage 用途)。
53
+ */
54
+ export declare function createFunctionFetchHandler(options: FunctionFetchHandlerOptions): FunctionFetchHandler;
package/dist/index.d.ts CHANGED
@@ -20,12 +20,16 @@ export { createWorkerRuntime, evaluateSource, evaluateModuleSource } from './wor
20
20
  export type { WorkerRuntimeOptions, WorkerLike, ModuleEvalOptions } from './worker-runtime';
21
21
  export { createNpmClient, parseSpec, DEFAULT_MAX_INSTALL_DEPTH } from './npm-client';
22
22
  export type { NpmClientOptions, NpmInstallResult, NpmInstalledPackage, NpmInstallStorage, NpmPersistence, } from './npm-client';
23
+ export { createRelayFetch } from './relay-fetch';
24
+ export type { RelayFetchOptions, RelayEnvelope } from './relay-fetch';
23
25
  export { gunzip, unpackTar, unpackNpmTarball } from './tar';
24
26
  export type { TarEntry } from './tar';
25
27
  export { ResolveError, parsePackageMeta, resolveImport, resolvePackageEntry, splitPackageSpecifier, RESOLVE_EXTENSIONS, } from './node-resolve';
26
28
  export type { ResolveFs, PackageMeta, ExportCondition } from './node-resolve';
27
29
  export { createRequire, createSandboxModuleGlobals, interopDefault, transformStaticImports, usesModuleSyntax, } from './module-loader';
28
30
  export type { ModuleCache, RequireOptions } from './module-loader';
31
+ export { createViteServer, scanEsmSpecifiers, ViteDevError } from './vite-dev';
32
+ export type { SfcCompiler, ViteServer, ViteServerOptions, ViteCommandPort, ViteCommandOutcome, EsmSpecifierHit, } from './vite-dev';
29
33
  export * as posix from './path';
30
34
  export { snapshotVfs, restoreVfs, entriesToTree, createMemoryFsBackend, createIndexedDbFsBackend, pickDefaultFsBackend, createFsPersistence, createMemoryKvStorage, createCommandHistory, } from './persistence';
31
35
  export type { FsPersistEntry, FsPersistenceBackend, FsPersistence, FsPersistenceOptions, CommandHistory, KvStorage, } from './persistence';
@@ -38,4 +42,6 @@ export { createTestScope, runCollectedTests, expect as expectAssertion, deepEqua
38
42
  export type { TestReport, TestFileResult, TestCaseResult, TestScope, TestRegistration, Expectation, } from './test-framework';
39
43
  export * from './offline';
40
44
  export * from './sim';
45
+ export { FN_FETCH_INTERCEPTOR_SCRIPT, createFunctionFetchHandler } from './function-fetch-proxy';
46
+ export type { FunctionFetchHandler, FunctionFetchHandlerOptions, OfflineRunner, } from './function-fetch-proxy';
41
47
  export type { WebContainer, WebContainerDirectoryTree, WebContainerEventMap, WebContainerFsEntry, WebContainerProcess, } from '@adep/types';