dsh-plugin-core-demo 0.20.3 → 0.20.5

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.
Files changed (3) hide show
  1. package/dist/client.js +4526 -4555
  2. package/dist/index.js +198 -0
  3. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -892,6 +892,202 @@ var init_lib2 = __esm({
892
892
  }
893
893
  });
894
894
 
895
+ // dist/host/fs-route.js
896
+ import { access, lstat, open, readdir, stat } from "node:fs/promises";
897
+ import { isAbsolute, join, normalize, sep } from "node:path";
898
+ var FS_PAGE_LIMIT = 500;
899
+ var READ_LINES = 5e3;
900
+ var BYTES_MAX = 32 * 1024 * 1024;
901
+ var LIST_TIMEOUT = 8e3;
902
+ function fail(code, message) {
903
+ return { ok: false, error: { code, message } };
904
+ }
905
+ function errCode(error) {
906
+ const code = error?.code;
907
+ if (code === "ENOENT")
908
+ return "not-found";
909
+ if (code === "ENOTDIR")
910
+ return "not-directory";
911
+ if (code === "EACCES" || code === "EPERM")
912
+ return "service-unavailable";
913
+ if (code === "EISDIR")
914
+ return "not-regular-file";
915
+ return "service-unavailable";
916
+ }
917
+ function normalizeAbsPath(raw) {
918
+ if (raw === "")
919
+ return null;
920
+ const slashed = raw.replace(/\\/g, "/");
921
+ if (!isAbsolute(slashed))
922
+ return null;
923
+ const normalized = normalize(slashed.replace(/\//g, sep)).replace(/\\/g, "/");
924
+ const drive = /^([a-zA-Z]):/.exec(normalized);
925
+ if (drive !== null)
926
+ return `${drive[1].toUpperCase()}${normalized.slice(1)}`;
927
+ return normalized.length > 1 ? normalized.replace(/\/+$/, "") : normalized;
928
+ }
929
+ async function listDir(target, offset, limit) {
930
+ let info;
931
+ try {
932
+ info = await lstat(target);
933
+ } catch (error) {
934
+ return fail(errCode(error), `no entry at ${target}`);
935
+ }
936
+ if (!info.isDirectory())
937
+ return fail("not-directory", `${target} is not a directory`);
938
+ let names;
939
+ try {
940
+ names = await Promise.race([
941
+ readdir(target),
942
+ new Promise((_resolve, reject) => {
943
+ setTimeout(() => reject(Object.assign(new Error("list timeout"), { code: "ETIMEDOUT" })), LIST_TIMEOUT);
944
+ })
945
+ ]);
946
+ } catch (error) {
947
+ const code = error?.code;
948
+ return fail(code === "ETIMEDOUT" ? "timeout" : errCode(error), `cannot list ${target}`);
949
+ }
950
+ names.sort((left, right) => left.toLowerCase().localeCompare(right.toLowerCase()));
951
+ const page = names.slice(offset, offset + limit);
952
+ const entries = [];
953
+ for (const name2 of page) {
954
+ const child = join(target, name2);
955
+ try {
956
+ const childInfo = await lstat(child);
957
+ entries.push({
958
+ name: name2,
959
+ type: childInfo.isDirectory() ? "directory" : childInfo.isFile() ? "file" : "other",
960
+ ...childInfo.isFile() ? { size: childInfo.size } : {}
961
+ });
962
+ } catch {
963
+ entries.push({ name: name2, type: "other" });
964
+ }
965
+ }
966
+ const listing = {
967
+ path: target,
968
+ entries,
969
+ truncated: names.length > offset + limit
970
+ };
971
+ return { ok: true, value: listing };
972
+ }
973
+ async function readText(target, offset, limit) {
974
+ let info;
975
+ try {
976
+ info = await stat(target);
977
+ } catch (error) {
978
+ return fail(errCode(error), `no file at ${target}`);
979
+ }
980
+ if (!info.isFile())
981
+ return fail("not-regular-file", `${target} is not a regular file`);
982
+ if (info.size > BYTES_MAX)
983
+ return fail("too-large", `${target} exceeds the ${BYTES_MAX} byte cap`);
984
+ const buffer = Buffer.alloc(info.size);
985
+ const handle = await open(target, "r");
986
+ try {
987
+ if (info.size > 0)
988
+ await handle.read(buffer, 0, info.size, 0);
989
+ } finally {
990
+ await handle.close();
991
+ }
992
+ if (buffer.includes(0))
993
+ return fail("not-text", `${target} contains NUL bytes`);
994
+ const all = buffer.toString("utf8").split("\n");
995
+ const start = Math.max(0, offset - 1);
996
+ const page = all.slice(start, start + limit);
997
+ const text = page.join("\n");
998
+ return {
999
+ ok: true,
1000
+ value: {
1001
+ text,
1002
+ offset,
1003
+ lines: page.length,
1004
+ eof: start + page.length >= all.length,
1005
+ absolutePath: target,
1006
+ bytes: info.size
1007
+ }
1008
+ };
1009
+ }
1010
+ async function readBinary(target, offset, length) {
1011
+ let info;
1012
+ try {
1013
+ info = await stat(target);
1014
+ } catch (error) {
1015
+ return fail(errCode(error), `no file at ${target}`);
1016
+ }
1017
+ if (!info.isFile())
1018
+ return fail("not-regular-file", `${target} is not a regular file`);
1019
+ const want = length > 0 ? length : info.size - offset;
1020
+ if (want > BYTES_MAX)
1021
+ return fail("too-large", `${target} exceeds the ${BYTES_MAX} byte cap`);
1022
+ const size = Math.max(0, Math.min(want, info.size - offset));
1023
+ const buffer = Buffer.alloc(size);
1024
+ if (size > 0) {
1025
+ const handle = await open(target, "r");
1026
+ try {
1027
+ await handle.read(buffer, 0, size, offset);
1028
+ } finally {
1029
+ await handle.close();
1030
+ }
1031
+ }
1032
+ return {
1033
+ ok: true,
1034
+ value: {
1035
+ offset,
1036
+ data: buffer.toString("base64"),
1037
+ eof: offset + size >= info.size,
1038
+ bytes: info.size
1039
+ }
1040
+ };
1041
+ }
1042
+ async function roots() {
1043
+ if (process.platform !== "win32")
1044
+ return ["/"];
1045
+ const found = [];
1046
+ await Promise.all(Array.from({ length: 26 }, (_, index) => String.fromCharCode(65 + index)).map(async (letter) => {
1047
+ const drive = `${letter}:\\`;
1048
+ try {
1049
+ await access(drive);
1050
+ found.push(`${letter}:/`);
1051
+ } catch {
1052
+ }
1053
+ }));
1054
+ return found.sort();
1055
+ }
1056
+ function serveFsRoute(ctx, options) {
1057
+ ctx.inject(["webServer"], (services) => {
1058
+ const webServer = services.webServer;
1059
+ if (webServer === void 0)
1060
+ return;
1061
+ webServer.register({
1062
+ kind: "prefix",
1063
+ path: options.path,
1064
+ handler: async (req, res) => {
1065
+ const url = new URL(req.url ?? "", "http://host");
1066
+ const target = normalizeAbsPath(url.searchParams.get("path") ?? "");
1067
+ const op = url.searchParams.get("op") ?? "list";
1068
+ const offset = Number(url.searchParams.get("offset") ?? "0") || 0;
1069
+ const limitParam = Number(url.searchParams.get("limit") ?? "0") || 0;
1070
+ let reply;
1071
+ if (op === "roots") {
1072
+ reply = { ok: true, value: { roots: await roots() } };
1073
+ } else if (target === null) {
1074
+ reply = fail("bad-request", "path must be absolute");
1075
+ } else if (op === "list") {
1076
+ reply = await listDir(target, offset, limitParam > 0 ? Math.min(limitParam, FS_PAGE_LIMIT) : FS_PAGE_LIMIT);
1077
+ } else if (op === "read") {
1078
+ reply = await readText(target, offset > 0 ? offset : 1, limitParam > 0 ? limitParam : READ_LINES);
1079
+ } else if (op === "readBytes") {
1080
+ reply = await readBinary(target, offset, limitParam);
1081
+ } else {
1082
+ reply = fail("bad-request", `unknown op ${op}`);
1083
+ }
1084
+ res.writeHead(200, { "content-type": "application/json; charset=utf-8" });
1085
+ res.end(JSON.stringify(reply));
1086
+ }
1087
+ });
1088
+ });
1089
+ }
1090
+
895
1091
  // dist/host/index.js
896
1092
  async function buildModelRoutes(llm) {
897
1093
  const routes = [];
@@ -931,6 +1127,7 @@ function serveModelCatalog(ctx, options) {
931
1127
 
932
1128
  // packages/demo/src/paths.ts
933
1129
  var MODELS_PATH = "/api/dsh-plugin-core-demo/models";
1130
+ var FS_PATH = "/api/dsh-plugin-core-demo/fs";
934
1131
 
935
1132
  // packages/demo/src/index.ts
936
1133
  var name = "dsh-plugin-core-demo";
@@ -949,6 +1146,7 @@ var Config = z === void 0 ? void 0 : z.object({
949
1146
  });
950
1147
  function apply(ctx) {
951
1148
  serveModelCatalog(ctx, { path: MODELS_PATH });
1149
+ serveFsRoute(ctx, { path: FS_PATH });
952
1150
  }
953
1151
  export {
954
1152
  Config,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-plugin-core-demo",
3
- "version": "0.20.3",
3
+ "version": "0.20.5",
4
4
  "description": "The shared UI foundation for DSH plugins, with a live component preview: design tokens, common controls, workspace browsing and notifications maintained in one place — install it to preview every component in a real host.",
5
5
  "icon": "./assets/icon-components.svg",
6
6
  "keywords": [