@freedome/client 0.0.0-stage → 1.1.0

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.
package/dist/node.js ADDED
@@ -0,0 +1,136 @@
1
+ import {
2
+ webSocketConnector
3
+ } from "./chunk-YB5V2MMF.js";
4
+ import {
5
+ FetchTransport,
6
+ FreedomeEndpoint
7
+ } from "./chunk-K3G5AG65.js";
8
+ import "./chunk-CAEBM4AN.js";
9
+
10
+ // src/node.ts
11
+ import { lstat, open, readFile, readdir, stat } from "fs/promises";
12
+ import { basename, dirname, join, relative, sep } from "path";
13
+ async function inputFromPath(path) {
14
+ const st = await lstat(path);
15
+ if (st.isSymbolicLink())
16
+ return {
17
+ kind: "dir",
18
+ name: basename(path),
19
+ files: /* @__PURE__ */ new Map(),
20
+ refused: [{ path: "", reason: "symlink" }]
21
+ };
22
+ if (!st.isDirectory()) {
23
+ const dir = dirname(path);
24
+ return {
25
+ kind: "file",
26
+ name: basename(path),
27
+ bytes: new Uint8Array(await readFile(path)),
28
+ sibling: async (rel) => {
29
+ try {
30
+ return new Uint8Array(await readFile(join(dir, rel)));
31
+ } catch {
32
+ return void 0;
33
+ }
34
+ }
35
+ };
36
+ }
37
+ const files = /* @__PURE__ */ new Map();
38
+ const refused = [];
39
+ const walk = async (d) => {
40
+ for (const e of (await readdir(d, { withFileTypes: true })).sort(
41
+ (a, b) => a.name < b.name ? -1 : 1
42
+ )) {
43
+ const p = join(d, e.name);
44
+ const rel = relative(path, p).split(sep).join("/");
45
+ if (e.isSymbolicLink()) refused.push({ path: rel, reason: "symlink" });
46
+ else if (e.isDirectory()) await walk(p);
47
+ else if (!e.isFile()) refused.push({ path: rel, reason: "not-regular" });
48
+ else files.set(rel, new Uint8Array(await readFile(p)));
49
+ }
50
+ };
51
+ await walk(path);
52
+ return { kind: "dir", name: basename(path), files, ...refused.length ? { refused } : {} };
53
+ }
54
+ var FileContentSource = class _FileContentSource {
55
+ constructor(path, name, length, contentType) {
56
+ this.path = path;
57
+ this.name = name;
58
+ this.length = length;
59
+ this.contentType = contentType;
60
+ }
61
+ path;
62
+ name;
63
+ length;
64
+ contentType;
65
+ static async open(path, o = {}) {
66
+ return new _FileContentSource(
67
+ path,
68
+ o.name ?? basename(path),
69
+ (await stat(path)).size,
70
+ o.contentType
71
+ );
72
+ }
73
+ async read(start, end) {
74
+ const fh = await open(this.path, "r");
75
+ try {
76
+ const b = new Uint8Array(end - start);
77
+ let o = 0;
78
+ while (o < b.length) {
79
+ const { bytesRead } = await fh.read(b, o, b.length - o, start + o);
80
+ if (bytesRead === 0) break;
81
+ o += bytesRead;
82
+ }
83
+ return b.subarray(0, o);
84
+ } finally {
85
+ await fh.close();
86
+ }
87
+ }
88
+ };
89
+ async function createNodeTransports() {
90
+ let ctor = globalThis.WebSocket;
91
+ if (!ctor) {
92
+ const ws = await import("ws");
93
+ ctor = ws.WebSocket ?? ws.default;
94
+ }
95
+ return { rest: (e) => new FetchTransport(e), sockets: webSocketConnector(ctor) };
96
+ }
97
+ var MdnsDiscovery = class {
98
+ constructor(preferIpv4 = true) {
99
+ this.preferIpv4 = preferIpv4;
100
+ }
101
+ preferIpv4;
102
+ async scan(timeoutMs = 3e3) {
103
+ const mod = await import("bonjour-service");
104
+ const Ctor = mod.Bonjour ?? mod.default?.Bonjour;
105
+ if (!Ctor) throw new Error("bonjour-service not available");
106
+ const b = new Ctor();
107
+ const found = /* @__PURE__ */ new Map();
108
+ const browser = b.find({ type: "freedome" }, (s) => {
109
+ const txt = s.txt ?? {};
110
+ const addr = (s.addresses ?? []).find((a) => this.preferIpv4 ? !a.includes(":") : true) ?? s.host;
111
+ const host = addr.includes(":") ? `[${addr}]` : addr;
112
+ const tls = txt.tls === "1" || !!txt.fp;
113
+ const endpoint = new FreedomeEndpoint(`${tls ? "https" : "http"}://${host}:${s.port}`, {
114
+ ...txt.id ? { id: txt.id } : {},
115
+ ...txt.name || s.name ? { name: txt.name ?? s.name } : {},
116
+ ...txt.fp ? { tlsFingerprint: txt.fp } : {}
117
+ });
118
+ found.set(txt.id ?? `${host}:${s.port}`, {
119
+ endpoint,
120
+ name: txt.name ?? s.name,
121
+ source: "mdns"
122
+ });
123
+ });
124
+ await new Promise((r) => setTimeout(r, timeoutMs));
125
+ browser.stop();
126
+ b.destroy();
127
+ return [...found.values()];
128
+ }
129
+ };
130
+ export {
131
+ FileContentSource,
132
+ MdnsDiscovery,
133
+ createNodeTransports,
134
+ inputFromPath as formatInputFromPath,
135
+ inputFromPath
136
+ };