Skip to content
This repository was archived by the owner on Dec 8, 2025. It is now read-only.

Commit ce7a126

Browse files
committed
patch: make tcp connection optional
1 parent c420c70 commit ce7a126

File tree

2 files changed

+30
-28
lines changed

2 files changed

+30
-28
lines changed

cli/src/cli.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export async function mainCli() {
2626
"--localhost",
2727
"use localhost:8000 instead of the internet dashboard"
2828
)
29+
.option("-t, --tcp", "open native TCP socket at 8082")
2930
.arguments("[file.ts]")
3031
.action(build)
3132

@@ -44,6 +45,7 @@ export async function mainCli() {
4445
"--localhost",
4546
"use localhost:8000 instead of the internet dashboard"
4647
)
48+
.option("-t, --tcp", "open native TCP socket at 8082")
4749
.action(devtools)
4850

4951
program.parse(process.argv)

cli/src/devtools.ts

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,19 @@ function fetchProxy(localhost: boolean): Promise<string> {
4545
export interface DevToolsOptions {
4646
internet?: boolean
4747
localhost?: boolean
48+
tcp?: boolean
4849

4950
bytecodeFile?: string
5051
debugFile?: string
5152
}
5253

5354
export async function devtools(options: DevToolsOptions & CmdOptions) {
54-
const { internet, localhost, bytecodeFile, debugFile } = options
55+
const { internet, localhost, bytecodeFile, debugFile, tcp } = options
5556
const port = 8081
5657
const tcpPort = 8082
5758
const listenHost = internet ? undefined : "127.0.0.1"
5859

5960
log(`starting dev tools at http://localhost:${port}`)
60-
log(` websocket: ws://localhost:${port}`)
61-
log(` tcpsocket: tcp://localhost:${tcpPort}`)
6261

6362
// download proxy sources
6463
const proxyHtml = await fetchProxy(localhost)
@@ -71,9 +70,7 @@ export async function devtools(options: DevToolsOptions & CmdOptions) {
7170
? () => {
7271
const bytecode = readFileSync(bytecodeFile)
7372
const dbg = debugFile ? readJSONSync(debugFile) : undefined
74-
debug(
75-
`refresh bytecode ${prettySize(bytecode.length)}...`
76-
)
73+
debug(`refresh bytecode ${prettySize(bytecode.length)}...`)
7774
const msg = JSON.stringify({
7875
type: "bytecode",
7976
channel: "devicescript",
@@ -84,6 +81,7 @@ export async function devtools(options: DevToolsOptions & CmdOptions) {
8481
}
8582
: undefined
8683

84+
log(` websocket: ws://localhost:${port}`)
8785
const server = http.createServer(function (req, res) {
8886
const parsedUrl = url.parse(req.url)
8987
const pathname = parsedUrl.pathname
@@ -121,31 +119,33 @@ export async function devtools(options: DevToolsOptions & CmdOptions) {
121119
client.on("error", (ev: Error) => error(ev))
122120
}
123121
})
122+
server.listen(port, listenHost)
124123

125-
const tcpServer = net.createServer((client: any) => {
126-
const sender = "tcp" + Math.random()
127-
client[SENDER_FIELD] = sender
128-
client.send = (pkt0: Buffer) => {
129-
const pkt = new Uint8Array(pkt0)
130-
const b = new Uint8Array(1 + pkt.length)
131-
b[0] = pkt.length
132-
b.set(pkt, 1)
133-
try {
134-
client.write(b)
135-
} catch {
124+
if (tcp) {
125+
log(` tcpsocket: tcp://localhost:${tcpPort}`)
126+
const tcpServer = net.createServer((client: any) => {
127+
const sender = "tcp" + Math.random()
128+
client[SENDER_FIELD] = sender
129+
client.send = (pkt0: Buffer) => {
130+
const pkt = new Uint8Array(pkt0)
131+
const b = new Uint8Array(1 + pkt.length)
132+
b[0] = pkt.length
133+
b.set(pkt, 1)
136134
try {
137-
client.end()
138-
} catch {} // eslint-disable-line no-empty
135+
client.write(b)
136+
} catch {
137+
try {
138+
client.end()
139+
} catch {} // eslint-disable-line no-empty
140+
}
139141
}
140-
}
141-
clients.push(client)
142-
log(`tcpclient: connected (${sender} ${clients.length} clients)`)
143-
client.on("end", () => removeClient(client))
144-
client.on("error", (ev: Error) => error(ev))
145-
})
146-
147-
server.listen(port, listenHost)
148-
tcpServer.listen(tcpPort, listenHost)
142+
clients.push(client)
143+
log(`tcpclient: connected (${sender} ${clients.length} clients)`)
144+
client.on("end", () => removeClient(client))
145+
client.on("error", (ev: Error) => error(ev))
146+
})
147+
tcpServer.listen(tcpPort, listenHost)
148+
}
149149

150150
if (bytecodeFile) {
151151
debug(`watching ${bytecodeFile}...`)

0 commit comments

Comments
 (0)