@flash-tunnel/sdk 0.0.0 → 0.3.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.
- package/LICENSE +21 -0
- package/README.md +17 -0
- package/dist/index.d.mts +54 -0
- package/dist/index.d.ts +54 -0
- package/dist/index.mjs +74 -0
- package/package.json +37 -8
- package/index.js +0 -1
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 flash-tunnel
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# @flash-tunnel/sdk
|
|
2
|
+
|
|
3
|
+
面向程序化集成的 Tunnel SDK。它接收一个已经运行的本地 origin,负责启动 Cloudflare Tunnel、等待公网地址,并在停止时清理本次会话创建的资源。
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import { startTunnel } from '@flash-tunnel/sdk'
|
|
7
|
+
|
|
8
|
+
const session = await startTunnel({
|
|
9
|
+
localUrl: 'http://127.0.0.1:4317',
|
|
10
|
+
mode: 'quick'
|
|
11
|
+
})
|
|
12
|
+
|
|
13
|
+
console.log(session.publicUrl)
|
|
14
|
+
await session.stop()
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
宿主程序负责本地服务、进程信号和业务协议;SDK 不安装全局信号处理器,也不依赖 CLI UI。运行仍然需要开发机安装 `cloudflared`。
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { Emitter } from '@flash-tunnel/core';
|
|
2
|
+
export { Emitter, PreviewSession } from '@flash-tunnel/core';
|
|
3
|
+
import { TunnelMode, FlashTunnelEvents } from '@flash-tunnel/types';
|
|
4
|
+
export { FlashTunnelEvents, TunnelMode } from '@flash-tunnel/types';
|
|
5
|
+
|
|
6
|
+
interface EmbeddedTunnelOptions {
|
|
7
|
+
/** Origin URL already serving the local application. */
|
|
8
|
+
localUrl: string;
|
|
9
|
+
/** Working directory used for session state and diagnostics. */
|
|
10
|
+
cwd?: string;
|
|
11
|
+
mode?: Extract<TunnelMode, 'quick' | 'named'>;
|
|
12
|
+
/** Required for named tunnels. */
|
|
13
|
+
hostname?: string;
|
|
14
|
+
/** Required for named tunnels. */
|
|
15
|
+
zone?: string;
|
|
16
|
+
name?: string;
|
|
17
|
+
accountId?: string;
|
|
18
|
+
zoneId?: string;
|
|
19
|
+
apiToken?: string;
|
|
20
|
+
apiBase?: string;
|
|
21
|
+
cloudflaredPath?: string;
|
|
22
|
+
originRequest?: {
|
|
23
|
+
httpHostHeader?: string;
|
|
24
|
+
noTLSVerify?: boolean;
|
|
25
|
+
};
|
|
26
|
+
persist?: boolean;
|
|
27
|
+
keep?: boolean;
|
|
28
|
+
overwrite?: boolean;
|
|
29
|
+
autoStopAfterMs?: number;
|
|
30
|
+
verifyTimeoutMs?: number;
|
|
31
|
+
signal?: AbortSignal;
|
|
32
|
+
onLog?: (line: string, stream: 'stdout' | 'stderr') => void;
|
|
33
|
+
}
|
|
34
|
+
interface EmbeddedTunnelSession {
|
|
35
|
+
sessionId: string;
|
|
36
|
+
localUrl: string;
|
|
37
|
+
publicUrl: string;
|
|
38
|
+
hostname?: string;
|
|
39
|
+
expiresAt?: string;
|
|
40
|
+
events: Emitter<FlashTunnelEvents>;
|
|
41
|
+
exited: Promise<void>;
|
|
42
|
+
/** Stop cloudflared and remove resources owned by this session. */
|
|
43
|
+
stop: () => Promise<void>;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Start a tunnel for an already-running local origin.
|
|
47
|
+
*
|
|
48
|
+
* The caller owns the local server lifecycle. This API only owns cloudflared
|
|
49
|
+
* and any named-tunnel/DNS resources created for the session.
|
|
50
|
+
*/
|
|
51
|
+
declare function startTunnel(options: EmbeddedTunnelOptions): Promise<EmbeddedTunnelSession>;
|
|
52
|
+
|
|
53
|
+
export { startTunnel };
|
|
54
|
+
export type { EmbeddedTunnelOptions, EmbeddedTunnelSession };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { Emitter } from '@flash-tunnel/core';
|
|
2
|
+
export { Emitter, PreviewSession } from '@flash-tunnel/core';
|
|
3
|
+
import { TunnelMode, FlashTunnelEvents } from '@flash-tunnel/types';
|
|
4
|
+
export { FlashTunnelEvents, TunnelMode } from '@flash-tunnel/types';
|
|
5
|
+
|
|
6
|
+
interface EmbeddedTunnelOptions {
|
|
7
|
+
/** Origin URL already serving the local application. */
|
|
8
|
+
localUrl: string;
|
|
9
|
+
/** Working directory used for session state and diagnostics. */
|
|
10
|
+
cwd?: string;
|
|
11
|
+
mode?: Extract<TunnelMode, 'quick' | 'named'>;
|
|
12
|
+
/** Required for named tunnels. */
|
|
13
|
+
hostname?: string;
|
|
14
|
+
/** Required for named tunnels. */
|
|
15
|
+
zone?: string;
|
|
16
|
+
name?: string;
|
|
17
|
+
accountId?: string;
|
|
18
|
+
zoneId?: string;
|
|
19
|
+
apiToken?: string;
|
|
20
|
+
apiBase?: string;
|
|
21
|
+
cloudflaredPath?: string;
|
|
22
|
+
originRequest?: {
|
|
23
|
+
httpHostHeader?: string;
|
|
24
|
+
noTLSVerify?: boolean;
|
|
25
|
+
};
|
|
26
|
+
persist?: boolean;
|
|
27
|
+
keep?: boolean;
|
|
28
|
+
overwrite?: boolean;
|
|
29
|
+
autoStopAfterMs?: number;
|
|
30
|
+
verifyTimeoutMs?: number;
|
|
31
|
+
signal?: AbortSignal;
|
|
32
|
+
onLog?: (line: string, stream: 'stdout' | 'stderr') => void;
|
|
33
|
+
}
|
|
34
|
+
interface EmbeddedTunnelSession {
|
|
35
|
+
sessionId: string;
|
|
36
|
+
localUrl: string;
|
|
37
|
+
publicUrl: string;
|
|
38
|
+
hostname?: string;
|
|
39
|
+
expiresAt?: string;
|
|
40
|
+
events: Emitter<FlashTunnelEvents>;
|
|
41
|
+
exited: Promise<void>;
|
|
42
|
+
/** Stop cloudflared and remove resources owned by this session. */
|
|
43
|
+
stop: () => Promise<void>;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Start a tunnel for an already-running local origin.
|
|
47
|
+
*
|
|
48
|
+
* The caller owns the local server lifecycle. This API only owns cloudflared
|
|
49
|
+
* and any named-tunnel/DNS resources created for the session.
|
|
50
|
+
*/
|
|
51
|
+
declare function startTunnel(options: EmbeddedTunnelOptions): Promise<EmbeddedTunnelSession>;
|
|
52
|
+
|
|
53
|
+
export { startTunnel };
|
|
54
|
+
export type { EmbeddedTunnelOptions, EmbeddedTunnelSession };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import process from 'node:process';
|
|
2
|
+
import { createCloudflareTunnel, createCloudflareDns } from '@flash-tunnel/adapter-cloudflare';
|
|
3
|
+
import { startPreviewSession } from '@flash-tunnel/core';
|
|
4
|
+
|
|
5
|
+
function toConfig(options, mode) {
|
|
6
|
+
return {
|
|
7
|
+
server: { url: options.localUrl },
|
|
8
|
+
domain: {
|
|
9
|
+
zone: options.zone
|
|
10
|
+
},
|
|
11
|
+
tunnel: {
|
|
12
|
+
mode,
|
|
13
|
+
name: options.name
|
|
14
|
+
},
|
|
15
|
+
providers: {
|
|
16
|
+
cloudflare: {
|
|
17
|
+
accountId: options.accountId,
|
|
18
|
+
zoneId: options.zoneId,
|
|
19
|
+
cloudflaredPath: options.cloudflaredPath,
|
|
20
|
+
originRequest: options.originRequest
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
autoStopAfter: options.autoStopAfterMs
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
function publicSession(session) {
|
|
27
|
+
return {
|
|
28
|
+
sessionId: session.sessionId,
|
|
29
|
+
localUrl: session.localUrl,
|
|
30
|
+
publicUrl: session.publicUrl,
|
|
31
|
+
hostname: session.hostname,
|
|
32
|
+
expiresAt: session.expiresAt,
|
|
33
|
+
events: session.events,
|
|
34
|
+
exited: session.exited,
|
|
35
|
+
stop: session.stop
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
async function startTunnel(options) {
|
|
39
|
+
const mode = options.mode ?? "quick";
|
|
40
|
+
const apiToken = options.apiToken ?? process.env.CLOUDFLARE_API_TOKEN;
|
|
41
|
+
const tunnel = createCloudflareTunnel({
|
|
42
|
+
cloudflaredPath: options.cloudflaredPath,
|
|
43
|
+
accountId: options.accountId,
|
|
44
|
+
apiToken,
|
|
45
|
+
apiBase: options.apiBase,
|
|
46
|
+
originRequest: options.originRequest
|
|
47
|
+
});
|
|
48
|
+
const dns = mode === "named" ? createCloudflareDns({
|
|
49
|
+
apiToken: apiToken ?? "",
|
|
50
|
+
zoneId: options.zoneId,
|
|
51
|
+
apiBase: options.apiBase
|
|
52
|
+
}) : void 0;
|
|
53
|
+
if (mode === "named" && (!options.hostname || !options.zone)) {
|
|
54
|
+
throw new Error("Named tunnels require both hostname and zone");
|
|
55
|
+
}
|
|
56
|
+
const session = await startPreviewSession({
|
|
57
|
+
cwd: options.cwd ?? process.cwd(),
|
|
58
|
+
config: toConfig(options, mode),
|
|
59
|
+
adapters: { tunnel, dns },
|
|
60
|
+
mode,
|
|
61
|
+
hostname: options.hostname,
|
|
62
|
+
zone: options.zone,
|
|
63
|
+
signal: options.signal,
|
|
64
|
+
onLog: options.onLog,
|
|
65
|
+
persist: options.persist,
|
|
66
|
+
keep: options.keep,
|
|
67
|
+
overwrite: options.overwrite,
|
|
68
|
+
autoStopAfterMs: options.autoStopAfterMs,
|
|
69
|
+
verifyTimeoutMs: options.verifyTimeoutMs
|
|
70
|
+
});
|
|
71
|
+
return publicSession(session);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export { startTunnel };
|
package/package.json
CHANGED
|
@@ -1,14 +1,43 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@flash-tunnel/sdk",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
"
|
|
7
|
-
"
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.3.0",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"homepage": "https://github.com/flash-tunnel/flash-tunnel#readme",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/flash-tunnel/flash-tunnel.git"
|
|
10
|
+
},
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/flash-tunnel/flash-tunnel/issues"
|
|
13
|
+
},
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"types": "./dist/index.d.mts",
|
|
17
|
+
"import": "./dist/index.mjs",
|
|
18
|
+
"default": "./dist/index.mjs"
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"main": "./dist/index.mjs",
|
|
22
|
+
"types": "./dist/index.d.mts",
|
|
23
|
+
"files": [
|
|
24
|
+
"dist"
|
|
25
|
+
],
|
|
26
|
+
"engines": {
|
|
27
|
+
"node": ">=22.14.0"
|
|
28
|
+
},
|
|
11
29
|
"publishConfig": {
|
|
12
30
|
"access": "public"
|
|
31
|
+
},
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"@flash-tunnel/adapter-cloudflare": "0.3.0",
|
|
34
|
+
"@flash-tunnel/config": "0.3.0",
|
|
35
|
+
"@flash-tunnel/core": "0.3.0",
|
|
36
|
+
"@flash-tunnel/types": "0.3.0"
|
|
37
|
+
},
|
|
38
|
+
"scripts": {
|
|
39
|
+
"stub": "unbuild --stub",
|
|
40
|
+
"build": "unbuild",
|
|
41
|
+
"typecheck": "tsc --noEmit"
|
|
13
42
|
}
|
|
14
43
|
}
|
package/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
// Placeholder
|