URL API feature request
roobscoob opened this issue ยท 9 comments
Hey! I'm currently working on a TypeScript -> ComputerCraft Lua transpiler on top of the brilliant TSTL project done by a few folx. One major issue I'm running into right now (building a ts-specific API) is that the way URLs are handled in computercraft is less than ideal.
So the real-world issue I'm trying to fix here is #629 . I want to represent WebSocket instances along with Request instances as classes and promises respectively, and thus fix the issues associated with identifying responses. My planned idea for doing this was to have my API generate identifiers and attach them as query parameters ?__hidden_id={ some identifier }
, then read that in the callback.
Unfortunately, cc exposes no APIs for parsing/serializing URLs and given the fact that lua doesn't support regex, doing such parsing would be wildly unpreformant.
I'm proposing a URL api. Nothing special:
local base = "https://user:[email protected]:8080/abc?a=1&b=2#hash";
local parsed = url.parse(base)
-- parsed = {
-- protocol: "https:",
-- origin: "https//",
-- username: "user",
-- password: "pass",
-- auth: "user:pass",
-- hostname: "google.com",
-- port: 8080,
-- host: "google.com:8080",
-- path: "/abc?a=1&b=2",
-- pathname: "/abc",
-- search: "?a=1&b=2",
-- query: { a: 1, b: 2 },
-- hash: "hash",
-- }
local serialized = url.serialize(parsed)
-- serialized == base
I think that unless you're trying to parse thousands of URLs/second, you're unlikely to have any performance issues. We've a few encryption implementations which hit a good enough speed, so parsing shouldn't be an issue.
That said, there's definitely an argument we could ship a URL parser with CC, though would obviously need to be aware of the copyright issues there.
to append to this, it would also be nice for http.checkURL
(and async) to accept a table and not just a string with this.
(and maybe the other HTTP methods?)
Is there a reason that Lua's pattern system can't do this? Patterns aren't as powerful as regex but they can do a lot of the same things.
it's probably theoretically possible, but I don't think that it would be
- good
- performant
- fun to write
and i don't see a significant downside to an API call here
If you don't want to write it then use off the shelf one? https://github.com/golgote/neturl
I'm worried it would be wildly unperformant. With the strict limitations cc is under it's probably best to call back to java whenever possible, i would think
it's probably best to call back to java whenever possible,
CC's philosophy is that if it can be done on the Lua side then it's done on the Lua side, so if this was added to CC it would probably be done on the Lua side and thus not any more perfomant that what you could implement currently.