From 7cec9b61219a0bbf784a0b8bc48aa28504fdb019 Mon Sep 17 00:00:00 2001 From: Zef Hemel Date: Fri, 7 Nov 2025 10:51:17 +0100 Subject: [PATCH] Lua: introduce net.* APIs and deprecate http.request in favor of new net.proxyFetch API Introduces foundation support for URIs (net.readURI, net.writeURI). --- client/plugos/syscalls/fetch.ts | 1 + client/space_lua/lua.test.ts | 4 ++ client/space_lua/stdlib/net.ts | 105 ++++++++++++++++++++++++++++ libraries/Library/Std/Editor/Lua.md | 1 - website/API/http.md | 21 ++---- website/API/net.md | 27 +++++++ website/URIs.md | 5 ++ 7 files changed, 147 insertions(+), 17 deletions(-) create mode 100644 client/space_lua/stdlib/net.ts create mode 100644 website/API/net.md create mode 100644 website/URIs.md diff --git a/client/plugos/syscalls/fetch.ts b/client/plugos/syscalls/fetch.ts index d3d1785d..3707c878 100644 --- a/client/plugos/syscalls/fetch.ts +++ b/client/plugos/syscalls/fetch.ts @@ -22,6 +22,7 @@ export function sandboxFetchSyscalls( url: string, options: ProxyFetchRequest = {}, ): Promise => { + console.warn("Deprecated: use net.proxyFetch() instead"); // JSONify any non-serializable body if ( options?.body && typeof options.body !== "string" && diff --git a/client/space_lua/lua.test.ts b/client/space_lua/lua.test.ts index 9f45fdab..b599c35a 100644 --- a/client/space_lua/lua.test.ts +++ b/client/space_lua/lua.test.ts @@ -62,6 +62,10 @@ Deno.test("[Lua] Encoding functions tests", async () => { await runLuaTest("./stdlib/encoding_test.lua"); }); +Deno.test("[Lua] Crypto functions tests", async () => { + await runLuaTest("./stdlib/crypto_test.lua"); +}); + Deno.test("[Lua] Lume functions tests", async () => { await runLuaTest("./lume_test.lua"); }); diff --git a/client/space_lua/stdlib/net.ts b/client/space_lua/stdlib/net.ts new file mode 100644 index 00000000..ccb96765 --- /dev/null +++ b/client/space_lua/stdlib/net.ts @@ -0,0 +1,105 @@ +import type { Client } from "../../client.ts"; +import type { + ProxyFetchRequest, + ProxyFetchResponse, +} from "../../plugos/proxy_fetch.ts"; +import { fsEndpoint } from "../../spaces/constants.ts"; +import { LuaNativeJSFunction, LuaTable } from "../runtime.ts"; + +export const netApi = new LuaTable({ + proxyFetch: new LuaNativeJSFunction( + async ( + url: string, + options: ProxyFetchRequest = {}, + ): Promise => { + // JSONify any non-serializable body + if ( + options?.body && typeof options.body !== "string" && + !(options.body instanceof Uint8Array) + ) { + options.body = JSON.stringify(options.body); + } + const fetchOptions = options + ? { + method: options.method, + headers: {} as Record, + body: options.body, + } + : {}; + fetchOptions.headers = buildProxyHeaders(options.headers); + const resp = await client.httpSpacePrimitives.authenticatedFetch( + buildProxyUrl(client, url), + fetchOptions, + ); + // Do sensible things with the body based on the content type + let body: any; + const contentTypeHeader = options.responseEncoding || + resp.headers.get("x-proxy-header-content-type"); + const statusCode = +(resp.headers.get("x-proxy-status-code") || "200"); + if (contentTypeHeader?.startsWith("application/json")) { + body = await resp.json(); + } else if ( + contentTypeHeader?.startsWith("application/xml") || + contentTypeHeader?.startsWith("text/") + ) { + body = await resp.text(); + } else { + body = new Uint8Array(await resp.arrayBuffer()); + } + return { + ok: resp.ok, + status: statusCode, + headers: extractProxyHeaders(resp.headers), + body: body, + }; + }, + ), + readURI: new LuaNativeJSFunction( + (uri: string, options: { uri?: string; encoding?: string }) => { + options.uri = uri; + return client.clientSystem.serviceRegistry.invokeBestMatch( + "net.readURI:" + uri, + options, + ); + }, + ), + writeURI: new LuaNativeJSFunction( + (uri: string, content: string | Uint8Array) => { + return client.clientSystem.serviceRegistry.invokeBestMatch( + "net.writeURI:" + uri, + { uri, content }, + ); + }, + ), +}); + +// Utility functions +function buildProxyUrl(client: Client, url: string) { + url = url.replace(/^https?:\/\//, ""); + // Strip off the /.fs and replace with /.proxy + return client.httpSpacePrimitives.url.slice(0, -fsEndpoint.length) + + "/.proxy/" + url; +} + +function buildProxyHeaders(headers?: Record): Record { + const newHeaders: Record = { "X-Proxy-Request": "true" }; + if (!headers) { + return newHeaders; + } + for (const [key, value] of Object.entries(headers)) { + newHeaders[`X-Proxy-Header-${key}`] = value; + } + return newHeaders; +} + +function extractProxyHeaders( + headers: Headers, +): Record { + const newHeaders: Record = {}; + for (const [key, value] of headers.entries()) { + if (key.toLowerCase().startsWith("x-proxy-header-")) { + newHeaders[key.slice("x-proxy-header-".length)] = value; + } + } + return newHeaders; +} diff --git a/libraries/Library/Std/Editor/Lua.md b/libraries/Library/Std/Editor/Lua.md index 347c5573..af410839 100644 --- a/libraries/Library/Std/Editor/Lua.md +++ b/libraries/Library/Std/Editor/Lua.md @@ -166,7 +166,6 @@ event.listen { end end local callText = text:sub(startPos+1, endPos-1) - print("Potential call text", callText, #callText) local propParts = callText:split(".") local currentValue = _CTX._GLOBAL for i = 1, #propParts do diff --git a/website/API/http.md b/website/API/http.md index ab81ee89..dda8f7de 100644 --- a/website/API/http.md +++ b/website/API/http.md @@ -1,18 +1,7 @@ -HTTP APIs. +HTTP APIs + +> **warning** Warning +> Deprecated: use [[API/net]] instead. ### http.request(url, options?) -Performs a HTTP call, proxied via the server (to avoid CORS issues). - -Options: -* `method`: GET, POST, PUT, DELETE (GET is default) -* `headers`: table with header -> value mappings -* `body`: either a string or table (which will be JSON stringified) - -Returns: -* `ok`: boolean if the request went ok -* `status`: HTTP status code -* `headers`: HTTP headers -* `body`: for content types: - * `text/*`: string - * `application/json`: parsed JSON object - * anything else: UInt8Array \ No newline at end of file +Deprecated, use [[API/net#net.proxyRequest(url, options?)]] instead. diff --git a/website/API/net.md b/website/API/net.md new file mode 100644 index 00000000..44b00d9b --- /dev/null +++ b/website/API/net.md @@ -0,0 +1,27 @@ +Network-related APIs. + +### net.proxyRequest(url, options?) +Performs a HTTP call, proxied via the server (to avoid CORS issues, see [[HTTP API]]). + +Options: +* `method`: GET, POST, PUT, DELETE (GET is default) +* `headers`: table with header -> value mappings +* `body`: either a string or table (which will be JSON stringified) + +Returns: +* `ok`: boolean if the request went ok +* `status`: HTTP status code +* `headers`: HTTP headers +* `body`: for content types: + * `text/*`: string + * `application/json`: parsed JSON object + * anything else: UInt8Array + +## net.readURI(uri, options?) +Fetches the content of a [[URIs|URI]]. + +Options: +* `encoding` force an encoding for the result, e.g. `{encoding = "text/markdown"}` + +## net.writeURI(uri, content) +Writes content to a specific [[URIs|URI]]. \ No newline at end of file diff --git a/website/URIs.md b/website/URIs.md new file mode 100644 index 00000000..a2d3b7bb --- /dev/null +++ b/website/URIs.md @@ -0,0 +1,5 @@ +URIs in SilverBullet are a means to point to locations outside of your [[Spaces|Space]], APIs to read and write to URIs are available via [[API/net]]. + +Use cases: +* [[Repositories]] +* [[Share]] \ No newline at end of file