Files
logos-messaging-nim/scripts/gen_edge_builders.py
Ivan FBandClaude Opus 5 53911000b7 build(wasm): regenerate edge_builders from libp2p 2.0.0
edge_builders.nim was a stripped copy of libp2p 1.15.2's builders. Against
2.0.0 it does not compile: `{.public.}` was removed from the package
altogether, so the very first proc carrying it fails with "invalid pragma".
It had drifted ~850 lines from the version it is supposed to shadow.

Regenerated from 2.0.0's builders.nim with the same three edits as before --
imports rewritten to absolute `libp2p/...`, the QUIC and WS transports and
their with*Transport builders removed, autotls reduced to opaque stubs -- all
because those reach lsquic and boringssl x86 asm, which cannot build for
wasm32. 2.0.0 already guards the autotls service behind
`libp2p_autotls_support`, so only the two type names needed stubbing.

The edits are now a script rather than a hand-patch, so the next libp2p bump
is a re-run instead of an archaeology exercise.

Also tracks the wasm build script. build/ is gitignored, so it existed only in
one working tree -- the same "one rm -rf from unrecoverable" problem that
b3531ea0 fixed for wasm-deps itself.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012qDYE5r2t2dMry5XpWWySu
2026-08-08 01:45:23 +02:00

102 lines
4.1 KiB
Python

import re, sys
SRC = "nimbledeps/pkgs2/libp2p-2.0.0-327dc7a0cb7e9d0be3d6083841bd496c4cbc48dc/libp2p/builders.nim"
DST = "wasm-deps/edge_builders.nim"
s = open(SRC).read()
# 1. Rewrite the import block: qualify every module to absolute `libp2p/...`
# (Nim keys modules by resolved path, so type identity with the real package
# is preserved), and drop the transports that cannot build for wasm32.
old_imports = s[s.index("import\n switch,"):s.index("import services/wildcardresolverservice")]
new_imports = """import
libp2p/switch,
libp2p/peerid,
libp2p/peerinfo,
libp2p/peeraddrpolicy,
libp2p/stream/connection,
libp2p/multiaddress,
libp2p/crypto/crypto,
# wstransport and quictransport dropped: ws pulls autotls/service ->
# certificate_ffi -> lsquic, quic pulls lsquic + boringssl x86 asm. Neither
# builds for wasm32, and a browser edge node uses WsBrowserTransport anyway.
libp2p/transports/[transport, tcptransport, memorytransport],
libp2p/muxers/[muxer, mplex/mplex, yamux/yamux],
libp2p/protocols/[identify, secure/secure, secure/noise, rendezvous, kademlia],
libp2p/protocols/connectivity/[
autonat/server,
autonat/client,
autonat/service,
autonatv2/server,
autonatv2/service,
autonatv2/client,
relay/relay,
relay/client,
relay/rtransport,
],
libp2p/services/[autorelayservice, hpservice, identify_pusher, natservice],
libp2p/connmanager,
libp2p/upgrademngrs/muxedupgrade,
libp2p/observedaddrmanager,
libp2p/nameresolving/nameresolver,
libp2p/errors,
libp2p/utility
"""
s = s.replace(old_imports, new_imports)
s = s.replace("import services/wildcardresolverservice",
"import libp2p/services/wildcardresolverservice")
# 2. TLSPrivateKey/TLSCertificate/TLSFlags came from the dropped wstransport.
s = s.replace(" TLSPrivateKey, TLSCertificate, TLSFlags, ServerFlags, connmanager.ConnectionLimits,\n",
" ServerFlags, connmanager.ConnectionLimits,\n")
# 3. autotls/service is dropped with wstransport, but SwitchBuilder still names
# its types in field declarations that are NOT behind
# `when defined(libp2p_autotls_support)`. Stub them: without that define
# nothing ever constructs one, so the fields stay permanently none.
anchor = "const MemoryAutoAddress* = memorytransport.MemoryAutoAddress"
stub = """# autotls/service is not imported here (it reaches lsquic through
# certificate_ffi). These two types are still named by SwitchBuilder fields that
# sit outside `when defined(libp2p_autotls_support)`, so declare them as opaque
# stubs. libp2p_autotls_support is never defined for the edge build, so nothing
# constructs either one and both fields stay Opt.none forever.
type
AutotlsService* = ref object
AutotlsConfig* = ref object
"""
s = s.replace(anchor, stub + anchor)
# 4. Drop the two transport builders whose transports we removed.
for proc_name in ("withWsTransport", "withQuicTransport"):
m = re.search(r"^proc " + proc_name + r"\*\(.*?^\n", s, re.S | re.M)
assert m, proc_name
s = s[:m.start()] + s[m.end():]
note = """# withWsTransport and withQuicTransport are removed with their transports. The
# edge node registers WsBrowserTransport itself and never speaks QUIC.
"""
s = s.replace("proc withMemoryTransport*", note + "proc withMemoryTransport*", 1)
header = """# SPDX-License-Identifier: Apache-2.0 OR MIT
# Copyright (c) Status Research & Development GmbH
## Generated override of libp2p/builders for the wasm/edge build.
##
## Identical to libp2p 2.0.0's builders.nim EXCEPT that the QUIC and WS
## transports (and autotls, which WS drags in) are removed -- they reach lsquic
## and boringssl x86 asm, neither of which compiles for wasm32.
##
## It is a separate module rather than a --path override because Nim resolves a
## bare `import libp2p/builders` to the nimble package no matter what --path
## says. Regenerate with scripts/gen_edge_builders.py after a libp2p bump.
"""
s = s.replace("""# SPDX-License-Identifier: Apache-2.0 OR MIT
# Copyright (c) Status Research & Development GmbH
## This module contains a Switch Building helper.
""", header)
open(DST, "w").write(s)
print("wrote", DST, len(s.splitlines()), "lines")