@lincyaw/studio 0.2.0-rc.24 → 0.2.0-rc.25

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
@@ -13,7 +13,7 @@ presmium studio [dir] [--port <n>] [--host <addr>]
13
13
 
14
14
  Studio is included with Presmium. In a project with `pnpm add -D @lincyaw/presmium`, use `pnpm exec presmium studio`. The separate `presmium-studio` command remains available when this package is installed directly; it runs the `presmium` command on PATH (install it with `pnpm add -g @lincyaw/presmium`).
15
15
 
16
- `dir` is a workspace root (`presmium.workspace.yaml`) or a deck's directory, as for `presmium serve`. The workbench listens on 127.0.0.1, port 4320 by default (a free port when that one is in use); `--host <addr>` makes it listen on one address of the machine on the local network instead, with no login.
16
+ `dir` is a workspace root (`presmium.workspace.yaml`) or a deck's directory, as for `presmium serve`. The workbench listens on 127.0.0.1, port 4320 by default (a free port when that one is in use); `--host <addr>` makes it listen on one local IP address, and `--host 0.0.0.0` makes it listen on all local IPv4 network interfaces, with no login. Network access prints one address for each local network interface.
17
17
 
18
18
  Sessions use the workspace root as their working directory and can read and write files throughout it. Opening another lecture changes document context, not the session or its directory.
19
19
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lincyaw/studio",
3
- "version": "0.2.0-rc.24",
3
+ "version": "0.2.0-rc.25",
4
4
  "description": "presmium studio: Presmium's workbench on DeepSeek Harness in one command, with the workspace service of a directory (experimental)",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -37,10 +37,11 @@
37
37
  ],
38
38
  "dependencies": {
39
39
  "@deepseek-ai/dsh": "0.2.1-alpha.4",
40
+ "http-proxy-3": "2.0.1",
40
41
  "jiti": "^2.7.0",
41
- "@lincyaw/dsh": "0.2.0-rc.24"
42
+ "@lincyaw/dsh": "0.2.0-rc.25"
42
43
  },
43
44
  "devDependencies": {
44
- "@lincyaw/cli": "1.0.0-rc.25"
45
+ "@lincyaw/cli": "1.0.0-rc.26"
45
46
  }
46
47
  }
package/src/cli.ts CHANGED
@@ -61,9 +61,12 @@ export async function main(argv: string[], options: MainOptions = {}): Promise<n
61
61
  return 2
62
62
  }
63
63
  process.stdout.write(`studio ${studio.url}\n`)
64
+ for (const url of studio.networkUrls) process.stdout.write(`network ${url}\n`)
64
65
  process.stderr.write(`${t.log(studio.log)}\n`)
65
66
  if (values.host && values.host !== '127.0.0.1' && values.host !== '::1' && !values['public-url']) {
66
- process.stderr.write(`${t.openOnNetwork(values.host, new URL(studio.url).port, new URL(studio.workspaceServerUrl).port)}\n`)
67
+ process.stderr.write(`${studio.networkUrls.length
68
+ ? t.openOnAllNetworks(new URL(studio.url).port, new URL(studio.workspaceServerUrl).port)
69
+ : t.openOnNetwork(values.host, new URL(studio.url).port, new URL(studio.workspaceServerUrl).port)}\n`)
67
70
  }
68
71
 
69
72
  const signals = ['SIGINT', 'SIGTERM', 'SIGHUP'] as const
package/src/messages.ts CHANGED
@@ -10,6 +10,7 @@ export interface StudioMessages {
10
10
  notDirectory(dir: string): string
11
11
  log(file: string): string
12
12
  openOnNetwork(address: string, port: string, previewPort: string): string
13
+ openOnAllNetworks(port: string, previewPort: string): string
13
14
  ended(how: string, log: string): string
14
15
  portInUse(port: number, address: string): string
15
16
  noFreePort(address: string): string
@@ -33,7 +34,7 @@ presmium-studio-<散列>.log(每个安装一个)保存在 $XDG_STATE_HOME/pr
33
34
 
34
35
  选项:
35
36
  --port <端口> Presmium Studio 的端口(默认为 4320,被占用时取空闲端口)
36
- --host <地址> 改为监听本机在局域网中的这个地址,而不是 127.0.0.1,且没有登录:
37
+ --host <地址> 改为监听本机在局域网中的一个 IP 地址或 0.0.0.0,且没有登录:
37
38
  任何能访问这个端口的人都以你的身份操作
38
39
  --public-url <网址> 有反向代理在前面时浏览器打开的根地址(例如 Presmium 云端);
39
40
  它的主机受信任,打印的网址用它。代理需保留 Host 并转发 WebSocket
@@ -48,10 +49,11 @@ presmium-studio-<散列>.log(每个安装一个)保存在 $XDG_STATE_HOME/pr
48
49
  notDirectory: dir => `${dir} 不是目录。请给出工作区的根目录或一份文稿所在的目录。`,
49
50
  log: file => `presmium studio: 日志在 ${file}`,
50
51
  openOnNetwork: (address, port, previewPort) => `presmium studio: 正在 ${address} 上监听,局域网中的设备可以访问,且没有登录:任何能用上面的网址访问端口 ${port}(以及预览的端口 ${previewPort})的人都以你的身份使用这个工作区(编辑、运行代码、使用你的模型)。请只在可信的网络中使用;不加 --host 时只在本机监听。`,
52
+ openOnAllNetworks: (port, previewPort) => `presmium studio: 正在全部局域网网络接口上监听,且没有登录:任何能用上面的网址访问端口 ${port}(以及预览的端口 ${previewPort})的人都以你的身份使用这个工作区(编辑、运行代码、使用你的模型)。请只在可信的网络中使用;不加 --host 时只在本机监听。`,
51
53
  ended: (how, log) => `presmium studio: ${how}。日志在 ${log}`,
52
54
  portInUse: (port, address) => `${address} 的端口 ${port} 已被占用。请用 --port 指定另一个端口。`,
53
55
  noFreePort: address => `${address} 上没有空闲的端口。请停止占用端口的服务后重试。`,
54
- badHost: address => `--host 需要本机的一个地址,例如 192.168.0.21,而不是“${address}”(Presmium Studio 只在一个确定的地址上监听,不能同时监听全部地址)。`,
56
+ badHost: address => `--host 需要本机的一个 IP 地址,例如 192.168.0.21,也可以使用 0.0.0.0,而不是“${address}”。`,
55
57
  wholeLog: (message, file) => `${message}${message.endsWith('\n') ? '' : '。'}完整的日志在 ${file}`,
56
58
  serviceEnded: '工作区服务(presmium serve)已停止',
57
59
  appEnded: 'Presmium Studio 的应用进程已停止',
@@ -69,8 +71,8 @@ presmium-studio-<hash>.log (one per installation) are kept in $XDG_STATE_HOME/pr
69
71
 
70
72
  Options:
71
73
  --port <n> Presmium Studio’s port (default: 4320, or a free one when that is in use)
72
- --host <addr> listen on this address of the machine on the local network instead of 127.0.0.1, with
73
- no login: whoever reaches the port works as you
74
+ --host <addr> listen on one local IP address, or on all IPv4 network interfaces with 0.0.0.0,
75
+ with no login: whoever reaches the port works as you
74
76
  --public-url <url> the root browsers open when a reverse proxy fronts Studio (as in Presmium
75
77
  Cloud): its host is trusted, and the printed address uses it. The proxy keeps Host and
76
78
  forwards WebSockets
@@ -86,14 +88,14 @@ Options:
86
88
  notDirectory: dir => `${dir} is not a directory. Give the workspace root, or the directory of a document.`,
87
89
  log: file => `presmium studio: log ${file}`,
88
90
  openOnNetwork: (address, port, previewPort) => `presmium studio: listening on ${address}, on the local network, with no login: anyone who reaches port ${port} (and the previews’ port ${previewPort}) with the address above works in this workspace as you (edits, runs code, uses your model). Use it on a network you trust; without --host it listens on this machine only.`,
91
+ openOnAllNetworks: (port, previewPort) => `presmium studio: listening on all local network interfaces, with no login: anyone who reaches port ${port} (and the previews’ port ${previewPort}) with the addresses above works in this workspace as you (edits, runs code, uses your model). Use it on a network you trust; without --host it listens on this machine only.`,
89
92
  ended: (how, log) => `presmium studio: ${how}; the log is ${log}`,
90
93
  portInUse: (port, address) => `Port ${port} of ${address} is in use. Give another with --port.`,
91
94
  noFreePort: address => `There is no free port on ${address}. Stop a service that uses one, then retry.`,
92
- badHost: address => `--host takes one address of this machine, such as 192.168.0.21, not “${address}” (Presmium Studio listens on one concrete address, never on all of them).`,
95
+ badHost: address => `--host takes one local IP address, such as 192.168.0.21, or 0.0.0.0 to listen on all IPv4 network interfaces, not “${address}”.`,
93
96
  wholeLog: (message, file) => `${message.endsWith('\n') ? message : `${message}; `}the whole log is ${file}`,
94
97
  serviceEnded: 'the workspace service (presmium serve) ended',
95
98
  appEnded: 'the Presmium Studio app ended',
96
99
  modelVariables: (variables, unset) => `${variables.join(', ')} go together; ${unset.join(' and ')} ${unset.length > 1 ? 'are' : 'is'} not set.`,
97
100
  },
98
101
  }
99
-
package/src/proxy.ts ADDED
@@ -0,0 +1,62 @@
1
+ import { createServer } from 'node:http'
2
+ import type { Server } from 'node:http'
3
+ import type { Duplex } from 'node:stream'
4
+ import { createProxyServer } from 'http-proxy-3'
5
+
6
+ export interface ProxyOptions {
7
+ host: string
8
+ port: number
9
+ target: string
10
+ }
11
+
12
+ export interface Proxy {
13
+ close(): Promise<void>
14
+ }
15
+
16
+ function listen(server: Server, port: number, host: string): Promise<void> {
17
+ return new Promise((resolve, reject) => {
18
+ server.once('error', reject)
19
+ server.listen(port, host, () => {
20
+ server.off('error', reject)
21
+ resolve()
22
+ })
23
+ })
24
+ }
25
+
26
+ export async function startProxy(options: ProxyOptions): Promise<Proxy> {
27
+ const proxy = createProxyServer({ ws: true, xfwd: false })
28
+ proxy.on('error', (error: Error, _request: unknown, response: unknown) => {
29
+ const answer = response as { writeHead?: (status: number, headers: Record<string, string>) => void, end?: (body: string) => void, destroy?: () => void, headersSent?: boolean } | undefined
30
+ if (answer?.writeHead && answer.end && !answer.headersSent) {
31
+ answer.writeHead(502, { 'Content-Type': 'text/plain; charset=utf-8' })
32
+ answer.end(`proxy error: ${error.message}`)
33
+ } else {
34
+ answer?.destroy?.()
35
+ }
36
+ })
37
+ const upgrades = new Set<Duplex>()
38
+ const server: Server = createServer((request, response) => {
39
+ request.url = request.url ?? '/'
40
+ proxy.web(request, response, { target: options.target })
41
+ })
42
+ server.on('upgrade', (request, socket, head) => {
43
+ upgrades.add(socket)
44
+ socket.once('close', () => upgrades.delete(socket))
45
+ request.url = request.url ?? '/'
46
+ proxy.ws(request, socket, head, { target: options.target })
47
+ })
48
+ try {
49
+ await listen(server, options.port, options.host)
50
+ } catch (error) {
51
+ proxy.close()
52
+ throw error
53
+ }
54
+ return {
55
+ close: () => new Promise((resolve) => {
56
+ server.close(() => resolve())
57
+ server.closeAllConnections()
58
+ for (const socket of upgrades) socket.destroy()
59
+ proxy.close()
60
+ }),
61
+ }
62
+ }
package/src/studio.ts CHANGED
@@ -3,13 +3,15 @@ import { closeSync, copyFileSync, existsSync, mkdirSync, openSync, readdirSync,
3
3
  import { createRequire } from 'node:module'
4
4
  import { createServer, isIP } from 'node:net'
5
5
  import type { AddressInfo } from 'node:net'
6
- import { homedir } from 'node:os'
6
+ import { homedir, hostname, networkInterfaces } from 'node:os'
7
7
  import { delimiter, dirname, join, resolve } from 'node:path'
8
8
  import { fileURLToPath } from 'node:url'
9
9
  import { bracketIPv6, dshBin, inheritedEnv, isLauncherVariable, launchArgs, launchEnv, launchNode, launchWorkspaceServer, WEB_READY, writeProfile } from '@lincyaw/dsh/launch'
10
10
  import type { LaunchedProcess } from '@lincyaw/dsh/launch'
11
11
  import { STUDIO_MESSAGES, systemLocale } from './messages.ts'
12
12
  import { modelEnv } from './model-env.ts'
13
+ import { startProxy } from './proxy.ts'
14
+ import type { Proxy } from './proxy.ts'
13
15
 
14
16
  const PROFILE = 'presmium-studio'
15
17
  const DEFAULT_PORT = 4320
@@ -38,6 +40,7 @@ export interface StudioOptions {
38
40
 
39
41
  export interface Studio {
40
42
  url: string
43
+ networkUrls: string[]
41
44
  workspaceServerUrl: string
42
45
  root: string
43
46
  home: string
@@ -113,7 +116,27 @@ function nodeScript(command: string): string | undefined {
113
116
  }
114
117
  }
115
118
 
116
- const WILDCARD = /^(?:0\.0\.0\.0|::|(?:0+:){7}0+|::ffff:0\.0\.0\.0)$/i
119
+ const WILDCARD = /^0\.0\.0\.0$/
120
+
121
+ function networkHosts(): string[] {
122
+ return [...new Set(Object.values(networkInterfaces()).flatMap(entries => entries ?? []).filter(entry => entry.family === 'IPv4' && !entry.internal).map(entry => entry.address))]
123
+ }
124
+
125
+ function authority(host: string, port: number): string {
126
+ return `${bracketIPv6(host)}:${port}`
127
+ }
128
+
129
+ function httpOrigin(host: string, port: number): string {
130
+ return `http://${authority(host, port)}`
131
+ }
132
+
133
+ function studioUrl(backend: string, publicUrl: string | undefined, host: string, port: number): string {
134
+ const token = new URL(backend).searchParams.get('token')
135
+ if (!token) throw new Error('DeepSeek Harness printed an address without its launch token')
136
+ const url = new URL(publicUrl ?? `http://${authority(host, port)}/`)
137
+ url.searchParams.set('token', token)
138
+ return url.href
139
+ }
117
140
 
118
141
  function freePort(listenAddress: string, port: number): Promise<number | undefined> {
119
142
  return new Promise((done) => {
@@ -174,11 +197,21 @@ export async function startStudio(options: StudioOptions): Promise<Studio> {
174
197
  const env = options.env ?? process.env
175
198
  const t = STUDIO_MESSAGES[systemLocale(env)]
176
199
  const { listenAddress } = options
177
- if (listenAddress !== undefined && (isIP(listenAddress) === 0 || WILDCARD.test(listenAddress))) {
200
+ if (listenAddress !== undefined && isIP(listenAddress) === 0) {
178
201
  throw new Error(t.badHost(listenAddress))
179
202
  }
180
- const port = await choosePort(listenAddress ?? '127.0.0.1', options.port)
181
- const origins = options.publicUrl ? [new URL(options.publicUrl).origin] : [`http://${bracketIPv6(listenAddress ?? '127.0.0.1')}:${port}`, ...!listenAddress || listenAddress === '127.0.0.1' ? [`http://localhost:${port}`] : []]
203
+ const boundAddress = listenAddress ?? '127.0.0.1'
204
+ const wildcard = WILDCARD.test(boundAddress)
205
+ const port = await choosePort(boundAddress, options.port)
206
+ const dshAddress = wildcard ? '127.0.0.1' : boundAddress
207
+ const dshPort = wildcard ? await choosePort(dshAddress, undefined) : port
208
+ const hosts = wildcard ? networkHosts() : []
209
+ const primaryHost = hosts[0] ?? dshAddress
210
+ const origins = options.publicUrl
211
+ ? [new URL(options.publicUrl).origin, ...hosts.map(host => httpOrigin(host, port)), httpOrigin('127.0.0.1', port)]
212
+ : wildcard
213
+ ? [...hosts.map(host => httpOrigin(host, port)), httpOrigin('127.0.0.1', port), `http://localhost:${port}`]
214
+ : [httpOrigin(boundAddress, port), ...boundAddress === '127.0.0.1' ? [`http://localhost:${port}`] : []]
182
215
 
183
216
  const home = studioHome(env)
184
217
  mkdirSync(home, { recursive: true, mode: 0o700 })
@@ -188,7 +221,10 @@ export async function startStudio(options: StudioOptions): Promise<Studio> {
188
221
  const log = new Log(join(home, `${profile}.log`))
189
222
  const serviceToken = randomBytes(24).toString('base64url')
190
223
  const started: LaunchedProcess[] = []
224
+ let proxy: Proxy | undefined
191
225
  const stopAll = async () => {
226
+ await proxy?.close()
227
+ proxy = undefined
192
228
  for (const launched of started.reverse()) await launched.stop()
193
229
  started.length = 0
194
230
  }
@@ -210,9 +246,13 @@ export async function startStudio(options: StudioOptions): Promise<Studio> {
210
246
  started.push(server.process)
211
247
  const { url, root } = await server.ready
212
248
 
249
+ const trustedHosts = wildcard
250
+ ? [...hosts, hostname(), `${hostname()}.local`]
251
+ : listenAddress ? [listenAddress] : []
213
252
  const dsh = launchNode('DeepSeek Harness', [
214
- dshBin(PACKAGE_DIR), ...launchArgs(profile, 'web'), '--port', String(port), '--no-open',
215
- ...listenAddress ? ['--host', listenAddress, '--trusted-host', `${bracketIPv6(listenAddress)}:${port}`] : [],
253
+ dshBin(PACKAGE_DIR), ...launchArgs(profile, 'web'), '--port', String(dshPort), '--no-open',
254
+ ...wildcard || listenAddress ? ['--host', dshAddress] : [],
255
+ ...trustedHosts.flatMap(host => ['--trusted-host', authority(host, port)]),
216
256
  ...options.publicUrl ? ['--public-url', options.publicUrl, '--trusted-host', new URL(options.publicUrl).host] : [],
217
257
  ], {
218
258
  cwd: root,
@@ -227,8 +267,11 @@ export async function startStudio(options: StudioOptions): Promise<Studio> {
227
267
  onOutput: text => log.write('dsh', text),
228
268
  })
229
269
  started.push(dsh)
230
- const address = (await dsh.ready(WEB_READY))[1]!
270
+ const backend = (await dsh.ready(WEB_READY))[1]!
271
+ if (wildcard) proxy = await startProxy({ host: boundAddress, port, target: `http://${authority(dshAddress, dshPort)}` })
231
272
  dsh.stopBuffering()
273
+ const address = studioUrl(backend, options.publicUrl, primaryHost, port)
274
+ const networkUrls = hosts.slice(1).map(host => studioUrl(backend, `http://${authority(host, port)}/`, host, port))
232
275
  // The address with its launch token, for a host that opens Studio for the user (0600, removed on close).
233
276
  const state = join(home, `${profile}.json`)
234
277
  writeFileSync(state, `${JSON.stringify({ url: address, workspace: root, workspaceServerUrl: url, pid: process.pid }, null, 2)}\n`, { mode: 0o600 })
@@ -240,6 +283,7 @@ export async function startStudio(options: StudioOptions): Promise<Studio> {
240
283
  ]).then(how => (closing ? new Promise<string>(() => {}) : how))
241
284
  return {
242
285
  url: address,
286
+ networkUrls,
243
287
  workspaceServerUrl: url,
244
288
  root,
245
289
  home,