@freedome/client 0.0.0-stage → 1.0.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/CHANGELOG.md +5 -0
- package/LICENSE +289 -0
- package/README.md +20 -2
- package/dist/chunk-CAEBM4AN.js +180 -0
- package/dist/chunk-HYGH4JIY.js +30 -0
- package/dist/chunk-J7A5LF32.js +180 -0
- package/dist/chunk-K3G5AG65.js +211 -0
- package/dist/chunk-YB5V2MMF.js +81 -0
- package/dist/codecs.cjs +1608 -0
- package/dist/codecs.d.cts +204 -0
- package/dist/codecs.d.ts +204 -0
- package/dist/codecs.js +1364 -0
- package/dist/discovery-BsEQfHam.d.cts +419 -0
- package/dist/discovery-DTV9yp9S.d.ts +419 -0
- package/dist/errors-L_bIF-OR.d.cts +108 -0
- package/dist/errors-L_bIF-OR.d.ts +108 -0
- package/dist/index.cjs +4753 -0
- package/dist/index.d.cts +1124 -0
- package/dist/index.d.ts +1124 -0
- package/dist/index.js +4084 -0
- package/dist/input-CxW5zgc-.d.cts +27 -0
- package/dist/input-CxW5zgc-.d.ts +27 -0
- package/dist/node.cjs +465 -0
- package/dist/node.d.cts +31 -0
- package/dist/node.d.ts +31 -0
- package/dist/node.js +136 -0
- package/dist/testing.cjs +1622 -0
- package/dist/testing.d.cts +184 -0
- package/dist/testing.d.ts +184 -0
- package/dist/testing.js +1251 -0
- package/dist/transport-dvmDQyvB.d.cts +130 -0
- package/dist/transport-dvmDQyvB.d.ts +130 -0
- package/package.json +78 -3
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/** A file. */
|
|
2
|
+
interface FileInput {
|
|
3
|
+
kind: 'file';
|
|
4
|
+
name?: string;
|
|
5
|
+
bytes: Uint8Array;
|
|
6
|
+
/** Reads a file relative to the file's directory (`.zelim` quantum.scene); undefined when absent. */
|
|
7
|
+
sibling?: (relPath: string) => Promise<Uint8Array | undefined>;
|
|
8
|
+
}
|
|
9
|
+
/** An unpacked directory (paths `/`-separated, relative to the root). */
|
|
10
|
+
interface DirectoryInput {
|
|
11
|
+
kind: 'dir';
|
|
12
|
+
name?: string;
|
|
13
|
+
files: Map<string, Uint8Array>;
|
|
14
|
+
/** Paths that are symbolic links or not regular files (refused by the readers). */
|
|
15
|
+
refused?: {
|
|
16
|
+
path: string;
|
|
17
|
+
reason: 'symlink' | 'not-regular';
|
|
18
|
+
}[];
|
|
19
|
+
}
|
|
20
|
+
/** Input of the codecs. */
|
|
21
|
+
type FormatInput = FileInput | DirectoryInput;
|
|
22
|
+
/** File input from bytes. */
|
|
23
|
+
declare const bytesInput: (bytes: Uint8Array, name?: string) => FileInput;
|
|
24
|
+
/** Directory input from a path → bytes map. */
|
|
25
|
+
declare const directoryInput: (files: Map<string, Uint8Array> | Record<string, Uint8Array>, name?: string) => DirectoryInput;
|
|
26
|
+
|
|
27
|
+
export { type DirectoryInput as D, type FormatInput as F, type FileInput as a, bytesInput as b, directoryInput as d };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/** A file. */
|
|
2
|
+
interface FileInput {
|
|
3
|
+
kind: 'file';
|
|
4
|
+
name?: string;
|
|
5
|
+
bytes: Uint8Array;
|
|
6
|
+
/** Reads a file relative to the file's directory (`.zelim` quantum.scene); undefined when absent. */
|
|
7
|
+
sibling?: (relPath: string) => Promise<Uint8Array | undefined>;
|
|
8
|
+
}
|
|
9
|
+
/** An unpacked directory (paths `/`-separated, relative to the root). */
|
|
10
|
+
interface DirectoryInput {
|
|
11
|
+
kind: 'dir';
|
|
12
|
+
name?: string;
|
|
13
|
+
files: Map<string, Uint8Array>;
|
|
14
|
+
/** Paths that are symbolic links or not regular files (refused by the readers). */
|
|
15
|
+
refused?: {
|
|
16
|
+
path: string;
|
|
17
|
+
reason: 'symlink' | 'not-regular';
|
|
18
|
+
}[];
|
|
19
|
+
}
|
|
20
|
+
/** Input of the codecs. */
|
|
21
|
+
type FormatInput = FileInput | DirectoryInput;
|
|
22
|
+
/** File input from bytes. */
|
|
23
|
+
declare const bytesInput: (bytes: Uint8Array, name?: string) => FileInput;
|
|
24
|
+
/** Directory input from a path → bytes map. */
|
|
25
|
+
declare const directoryInput: (files: Map<string, Uint8Array> | Record<string, Uint8Array>, name?: string) => DirectoryInput;
|
|
26
|
+
|
|
27
|
+
export { type DirectoryInput as D, type FormatInput as F, type FileInput as a, bytesInput as b, directoryInput as d };
|
package/dist/node.cjs
ADDED
|
@@ -0,0 +1,465 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/node.ts
|
|
31
|
+
var node_exports = {};
|
|
32
|
+
__export(node_exports, {
|
|
33
|
+
FileContentSource: () => FileContentSource,
|
|
34
|
+
MdnsDiscovery: () => MdnsDiscovery,
|
|
35
|
+
createNodeTransports: () => createNodeTransports,
|
|
36
|
+
formatInputFromPath: () => inputFromPath,
|
|
37
|
+
inputFromPath: () => inputFromPath
|
|
38
|
+
});
|
|
39
|
+
module.exports = __toCommonJS(node_exports);
|
|
40
|
+
var import_promises = require("fs/promises");
|
|
41
|
+
var import_node_path = require("path");
|
|
42
|
+
|
|
43
|
+
// src/endpoint.ts
|
|
44
|
+
var FreedomeEndpoint = class _FreedomeEndpoint {
|
|
45
|
+
constructor(baseUrl, opts = {}) {
|
|
46
|
+
this.opts = opts;
|
|
47
|
+
const u = new URL(String(baseUrl));
|
|
48
|
+
this.baseUrl = `${u.protocol}//${u.host}`;
|
|
49
|
+
}
|
|
50
|
+
opts;
|
|
51
|
+
baseUrl;
|
|
52
|
+
/** `host[:port]`, `http(s)://…` or a `freedome://pair?…` link (default port 8080 / 443). */
|
|
53
|
+
static parse(text) {
|
|
54
|
+
const t = text.trim();
|
|
55
|
+
if (t.startsWith("freedome://")) return PairingLink.parse(t).endpoint;
|
|
56
|
+
const u = new URL(t.includes("://") ? t : `http://${t}`);
|
|
57
|
+
if (u.protocol !== "http:" && u.protocol !== "https:")
|
|
58
|
+
throw new TypeError(`unsupported scheme "${u.protocol}"`);
|
|
59
|
+
if (!u.hostname) throw new TypeError("no host");
|
|
60
|
+
if (!u.port) u.port = u.protocol === "https:" ? "443" : "8080";
|
|
61
|
+
return new _FreedomeEndpoint(u);
|
|
62
|
+
}
|
|
63
|
+
get id() {
|
|
64
|
+
return this.opts.id;
|
|
65
|
+
}
|
|
66
|
+
get tlsFingerprint() {
|
|
67
|
+
return this.opts.tlsFingerprint;
|
|
68
|
+
}
|
|
69
|
+
get pairingCode() {
|
|
70
|
+
return this.opts.pairingCode;
|
|
71
|
+
}
|
|
72
|
+
/** `<base>/api/v1`. */
|
|
73
|
+
get apiBase() {
|
|
74
|
+
return `${this.baseUrl}/api/v1`;
|
|
75
|
+
}
|
|
76
|
+
/** WebSocket base (`ws://` / `wss://`). */
|
|
77
|
+
get wsBase() {
|
|
78
|
+
return this.baseUrl.replace(/^http/, "ws");
|
|
79
|
+
}
|
|
80
|
+
/** Key of the stored tokens / caches (server instance id once known). */
|
|
81
|
+
get storageKey() {
|
|
82
|
+
return this.opts.id ?? this.baseUrl;
|
|
83
|
+
}
|
|
84
|
+
withId(id) {
|
|
85
|
+
return new _FreedomeEndpoint(this.baseUrl, { ...this.opts, id });
|
|
86
|
+
}
|
|
87
|
+
toString() {
|
|
88
|
+
return `FreedomeEndpoint(${this.baseUrl}${this.opts.id ? `, id: ${this.opts.id}` : ""})`;
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
var HOST = /^(?:[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?(?:\.[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?)*|\[[0-9A-Fa-f:.]+\]|[0-9A-Fa-f:]+:[0-9A-Fa-f:.]*)$/;
|
|
92
|
+
var PairingLink = class _PairingLink {
|
|
93
|
+
constructor(host, port = 8080, opts = {}) {
|
|
94
|
+
this.host = host;
|
|
95
|
+
this.port = port;
|
|
96
|
+
this.opts = opts;
|
|
97
|
+
}
|
|
98
|
+
host;
|
|
99
|
+
port;
|
|
100
|
+
opts;
|
|
101
|
+
static parse(text) {
|
|
102
|
+
let u;
|
|
103
|
+
try {
|
|
104
|
+
u = new URL(text.trim());
|
|
105
|
+
} catch {
|
|
106
|
+
throw new TypeError("not a pairing link");
|
|
107
|
+
}
|
|
108
|
+
if (u.protocol !== "freedome:" || u.hostname !== "pair" && u.host !== "pair")
|
|
109
|
+
throw new TypeError("not a freedome://pair link");
|
|
110
|
+
const q = u.searchParams;
|
|
111
|
+
const host = q.get("host");
|
|
112
|
+
if (!host || !HOST.test(host)) throw new TypeError("pairing link: bad or missing host");
|
|
113
|
+
const portText = q.get("port");
|
|
114
|
+
const port = portText === null ? 8080 : Number(portText);
|
|
115
|
+
if (!Number.isInteger(port) || port < 1 || port > 65535)
|
|
116
|
+
throw new TypeError("pairing link: bad port");
|
|
117
|
+
const id = q.get("id");
|
|
118
|
+
if (id !== null && !/^[A-Za-z0-9._-]{1,64}$/.test(id))
|
|
119
|
+
throw new TypeError("pairing link: bad id");
|
|
120
|
+
const code = q.get("code");
|
|
121
|
+
if (code !== null && !/^[0-9]{6}$/.test(code))
|
|
122
|
+
throw new TypeError("pairing link: code must be 6 digits");
|
|
123
|
+
const fp = q.get("fp");
|
|
124
|
+
if (fp !== null && !/^(?:[0-9A-Fa-f]{2}:?){8,32}$/.test(fp))
|
|
125
|
+
throw new TypeError("pairing link: fp must be \u2265 16 hex digits");
|
|
126
|
+
const name = q.get("name");
|
|
127
|
+
if (name !== null && name.length > 128) throw new TypeError("pairing link: name too long");
|
|
128
|
+
return new _PairingLink(host, port, {
|
|
129
|
+
...id !== null ? { id } : {},
|
|
130
|
+
...code !== null ? { code } : {},
|
|
131
|
+
...fp !== null ? { fingerprint: fp } : {},
|
|
132
|
+
tls: q.get("tls") === "1" || fp !== null,
|
|
133
|
+
...name !== null ? { name } : {}
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
static tryParse(text) {
|
|
137
|
+
try {
|
|
138
|
+
return _PairingLink.parse(text);
|
|
139
|
+
} catch {
|
|
140
|
+
return null;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
get endpoint() {
|
|
144
|
+
const h = this.host.includes(":") && !this.host.startsWith("[") ? `[${this.host}]` : this.host;
|
|
145
|
+
return new FreedomeEndpoint(`${this.opts.tls ? "https" : "http"}://${h}:${this.port}`, {
|
|
146
|
+
...this.opts.id ? { id: this.opts.id } : {},
|
|
147
|
+
...this.opts.name ? { name: this.opts.name } : {},
|
|
148
|
+
...this.opts.fingerprint ? { tlsFingerprint: this.opts.fingerprint } : {},
|
|
149
|
+
...this.opts.code ? { pairingCode: this.opts.code } : {}
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
toString() {
|
|
153
|
+
const q = new URLSearchParams({ host: this.host, port: String(this.port) });
|
|
154
|
+
if (this.opts.id) q.set("id", this.opts.id);
|
|
155
|
+
if (this.opts.code) q.set("code", this.opts.code);
|
|
156
|
+
if (this.opts.fingerprint) q.set("fp", this.opts.fingerprint);
|
|
157
|
+
else if (this.opts.tls) q.set("tls", "1");
|
|
158
|
+
if (this.opts.name) q.set("name", this.opts.name);
|
|
159
|
+
return `freedome://pair?${q.toString()}`;
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
// src/errors.ts
|
|
164
|
+
var FreedomeError = class extends Error {
|
|
165
|
+
code;
|
|
166
|
+
problem;
|
|
167
|
+
constructor(message, opts = {}) {
|
|
168
|
+
super(message, opts.cause === void 0 ? void 0 : { cause: opts.cause });
|
|
169
|
+
this.name = new.target.name;
|
|
170
|
+
this.code = opts.code;
|
|
171
|
+
this.problem = opts.problem;
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
var NetworkError = class extends FreedomeError {
|
|
175
|
+
};
|
|
176
|
+
var RequestTimeoutError = class extends FreedomeError {
|
|
177
|
+
constructor(message, timeoutMs) {
|
|
178
|
+
super(message, { code: "timeout" });
|
|
179
|
+
this.timeoutMs = timeoutMs;
|
|
180
|
+
}
|
|
181
|
+
timeoutMs;
|
|
182
|
+
};
|
|
183
|
+
var UnauthorizedError = class extends FreedomeError {
|
|
184
|
+
constructor(message, problem) {
|
|
185
|
+
super(message, { code: "unauthenticated", ...problem ? { problem } : {} });
|
|
186
|
+
}
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
// src/transport.ts
|
|
190
|
+
var FreedomeResponse = class _FreedomeResponse {
|
|
191
|
+
constructor(status, headers = {}, body = new Uint8Array(0)) {
|
|
192
|
+
this.status = status;
|
|
193
|
+
this.headers = headers;
|
|
194
|
+
this.body = body;
|
|
195
|
+
}
|
|
196
|
+
status;
|
|
197
|
+
headers;
|
|
198
|
+
body;
|
|
199
|
+
get ok() {
|
|
200
|
+
return this.status >= 200 && this.status < 300;
|
|
201
|
+
}
|
|
202
|
+
get text() {
|
|
203
|
+
return new TextDecoder().decode(this.body);
|
|
204
|
+
}
|
|
205
|
+
/** Parsed JSON body (`undefined` when empty). */
|
|
206
|
+
get json() {
|
|
207
|
+
if (this.body.length === 0) return void 0;
|
|
208
|
+
return JSON.parse(this.text);
|
|
209
|
+
}
|
|
210
|
+
static json(status, value, headers = {}) {
|
|
211
|
+
return new _FreedomeResponse(
|
|
212
|
+
status,
|
|
213
|
+
{ "content-type": "application/json", ...headers },
|
|
214
|
+
new TextEncoder().encode(JSON.stringify(value))
|
|
215
|
+
);
|
|
216
|
+
}
|
|
217
|
+
};
|
|
218
|
+
function requestUrl(endpoint, r) {
|
|
219
|
+
const base = r.rootRelative ? endpoint.baseUrl : endpoint.apiBase;
|
|
220
|
+
const q = r.query && Object.keys(r.query).length ? `?${new URLSearchParams(r.query).toString()}` : "";
|
|
221
|
+
return `${base}${r.path}${q}`;
|
|
222
|
+
}
|
|
223
|
+
var FetchTransport = class {
|
|
224
|
+
constructor(endpoint, fetchImpl = globalThis.fetch.bind(globalThis)) {
|
|
225
|
+
this.endpoint = endpoint;
|
|
226
|
+
this.fetchImpl = fetchImpl;
|
|
227
|
+
}
|
|
228
|
+
endpoint;
|
|
229
|
+
fetchImpl;
|
|
230
|
+
controllers = /* @__PURE__ */ new Set();
|
|
231
|
+
async send(r, timeoutMs) {
|
|
232
|
+
const ac = new AbortController();
|
|
233
|
+
this.controllers.add(ac);
|
|
234
|
+
let timedOut = false;
|
|
235
|
+
const timer = setTimeout(() => {
|
|
236
|
+
timedOut = true;
|
|
237
|
+
ac.abort();
|
|
238
|
+
}, timeoutMs);
|
|
239
|
+
try {
|
|
240
|
+
const res = await this.fetchImpl(requestUrl(this.endpoint, r), {
|
|
241
|
+
method: r.method,
|
|
242
|
+
headers: r.headers,
|
|
243
|
+
...r.body ? { body: r.body } : {},
|
|
244
|
+
signal: ac.signal
|
|
245
|
+
});
|
|
246
|
+
const body = new Uint8Array(await res.arrayBuffer());
|
|
247
|
+
const headers = {};
|
|
248
|
+
res.headers.forEach((v, k) => headers[k.toLowerCase()] = v);
|
|
249
|
+
return new FreedomeResponse(res.status, headers, body);
|
|
250
|
+
} catch (e) {
|
|
251
|
+
if (timedOut)
|
|
252
|
+
throw new RequestTimeoutError(
|
|
253
|
+
`${r.method} ${r.path}: no answer within ${timeoutMs} ms`,
|
|
254
|
+
timeoutMs
|
|
255
|
+
);
|
|
256
|
+
throw new NetworkError(`${r.method} ${r.path}: ${e?.message ?? e}`, { cause: e });
|
|
257
|
+
} finally {
|
|
258
|
+
clearTimeout(timer);
|
|
259
|
+
this.controllers.delete(ac);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
async close() {
|
|
263
|
+
for (const c of this.controllers) c.abort();
|
|
264
|
+
this.controllers.clear();
|
|
265
|
+
}
|
|
266
|
+
};
|
|
267
|
+
|
|
268
|
+
// src/realtime/socket.ts
|
|
269
|
+
function webSocketConnector(impl) {
|
|
270
|
+
return (endpoint, path, protocols, timeoutMs) => new Promise((resolve, reject) => {
|
|
271
|
+
const Ctor = impl ?? globalThis.WebSocket;
|
|
272
|
+
if (!Ctor) {
|
|
273
|
+
reject(
|
|
274
|
+
new NetworkError(
|
|
275
|
+
"no WebSocket implementation (Node < 22: use createNodeTransports from @freedome/client/node)"
|
|
276
|
+
)
|
|
277
|
+
);
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
let ws;
|
|
281
|
+
try {
|
|
282
|
+
ws = new Ctor(`${endpoint.wsBase}${path}`, protocols);
|
|
283
|
+
} catch (e) {
|
|
284
|
+
reject(new NetworkError(`websocket: ${e.message}`, { cause: e }));
|
|
285
|
+
return;
|
|
286
|
+
}
|
|
287
|
+
let settled = false;
|
|
288
|
+
const timer = setTimeout(() => {
|
|
289
|
+
if (settled) return;
|
|
290
|
+
settled = true;
|
|
291
|
+
try {
|
|
292
|
+
ws.close();
|
|
293
|
+
} catch {
|
|
294
|
+
}
|
|
295
|
+
reject(
|
|
296
|
+
new RequestTimeoutError(`websocket ${path}: not open within ${timeoutMs} ms`, timeoutMs)
|
|
297
|
+
);
|
|
298
|
+
}, timeoutMs);
|
|
299
|
+
let onMsg;
|
|
300
|
+
let onClose;
|
|
301
|
+
const buffered = [];
|
|
302
|
+
ws.onmessage = (ev) => {
|
|
303
|
+
const t = typeof ev.data === "string" ? ev.data : new TextDecoder().decode(ev.data);
|
|
304
|
+
if (onMsg) onMsg(t);
|
|
305
|
+
else buffered.push(t);
|
|
306
|
+
};
|
|
307
|
+
ws.onclose = (ev) => {
|
|
308
|
+
clearTimeout(timer);
|
|
309
|
+
if (!settled) {
|
|
310
|
+
settled = true;
|
|
311
|
+
reject(
|
|
312
|
+
ev.code === 4401 || ev.code === 4403 ? new UnauthorizedError(`websocket ${path}: ${ev.reason || "unauthorized"}`) : new NetworkError(
|
|
313
|
+
`websocket ${path} closed during handshake (${ev.code ?? "network"})`
|
|
314
|
+
)
|
|
315
|
+
);
|
|
316
|
+
return;
|
|
317
|
+
}
|
|
318
|
+
onClose?.(ev.code, ev.reason || void 0);
|
|
319
|
+
};
|
|
320
|
+
ws.onerror = () => {
|
|
321
|
+
};
|
|
322
|
+
ws.onopen = () => {
|
|
323
|
+
clearTimeout(timer);
|
|
324
|
+
if (settled) return;
|
|
325
|
+
settled = true;
|
|
326
|
+
resolve({
|
|
327
|
+
protocol: ws.protocol,
|
|
328
|
+
send: (t) => ws.send(t),
|
|
329
|
+
close: (code, reason) => ws.close(code ?? 1e3, reason),
|
|
330
|
+
listen(m, c) {
|
|
331
|
+
onMsg = m;
|
|
332
|
+
onClose = c;
|
|
333
|
+
for (const t of buffered.splice(0)) m(t);
|
|
334
|
+
}
|
|
335
|
+
});
|
|
336
|
+
};
|
|
337
|
+
});
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
// src/node.ts
|
|
341
|
+
async function inputFromPath(path) {
|
|
342
|
+
const st = await (0, import_promises.lstat)(path);
|
|
343
|
+
if (st.isSymbolicLink())
|
|
344
|
+
return {
|
|
345
|
+
kind: "dir",
|
|
346
|
+
name: (0, import_node_path.basename)(path),
|
|
347
|
+
files: /* @__PURE__ */ new Map(),
|
|
348
|
+
refused: [{ path: "", reason: "symlink" }]
|
|
349
|
+
};
|
|
350
|
+
if (!st.isDirectory()) {
|
|
351
|
+
const dir = (0, import_node_path.dirname)(path);
|
|
352
|
+
return {
|
|
353
|
+
kind: "file",
|
|
354
|
+
name: (0, import_node_path.basename)(path),
|
|
355
|
+
bytes: new Uint8Array(await (0, import_promises.readFile)(path)),
|
|
356
|
+
sibling: async (rel) => {
|
|
357
|
+
try {
|
|
358
|
+
return new Uint8Array(await (0, import_promises.readFile)((0, import_node_path.join)(dir, rel)));
|
|
359
|
+
} catch {
|
|
360
|
+
return void 0;
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
};
|
|
364
|
+
}
|
|
365
|
+
const files = /* @__PURE__ */ new Map();
|
|
366
|
+
const refused = [];
|
|
367
|
+
const walk = async (d) => {
|
|
368
|
+
for (const e of (await (0, import_promises.readdir)(d, { withFileTypes: true })).sort(
|
|
369
|
+
(a, b) => a.name < b.name ? -1 : 1
|
|
370
|
+
)) {
|
|
371
|
+
const p = (0, import_node_path.join)(d, e.name);
|
|
372
|
+
const rel = (0, import_node_path.relative)(path, p).split(import_node_path.sep).join("/");
|
|
373
|
+
if (e.isSymbolicLink()) refused.push({ path: rel, reason: "symlink" });
|
|
374
|
+
else if (e.isDirectory()) await walk(p);
|
|
375
|
+
else if (!e.isFile()) refused.push({ path: rel, reason: "not-regular" });
|
|
376
|
+
else files.set(rel, new Uint8Array(await (0, import_promises.readFile)(p)));
|
|
377
|
+
}
|
|
378
|
+
};
|
|
379
|
+
await walk(path);
|
|
380
|
+
return { kind: "dir", name: (0, import_node_path.basename)(path), files, ...refused.length ? { refused } : {} };
|
|
381
|
+
}
|
|
382
|
+
var FileContentSource = class _FileContentSource {
|
|
383
|
+
constructor(path, name, length, contentType) {
|
|
384
|
+
this.path = path;
|
|
385
|
+
this.name = name;
|
|
386
|
+
this.length = length;
|
|
387
|
+
this.contentType = contentType;
|
|
388
|
+
}
|
|
389
|
+
path;
|
|
390
|
+
name;
|
|
391
|
+
length;
|
|
392
|
+
contentType;
|
|
393
|
+
static async open(path, o = {}) {
|
|
394
|
+
return new _FileContentSource(
|
|
395
|
+
path,
|
|
396
|
+
o.name ?? (0, import_node_path.basename)(path),
|
|
397
|
+
(await (0, import_promises.stat)(path)).size,
|
|
398
|
+
o.contentType
|
|
399
|
+
);
|
|
400
|
+
}
|
|
401
|
+
async read(start, end) {
|
|
402
|
+
const fh = await (0, import_promises.open)(this.path, "r");
|
|
403
|
+
try {
|
|
404
|
+
const b = new Uint8Array(end - start);
|
|
405
|
+
let o = 0;
|
|
406
|
+
while (o < b.length) {
|
|
407
|
+
const { bytesRead } = await fh.read(b, o, b.length - o, start + o);
|
|
408
|
+
if (bytesRead === 0) break;
|
|
409
|
+
o += bytesRead;
|
|
410
|
+
}
|
|
411
|
+
return b.subarray(0, o);
|
|
412
|
+
} finally {
|
|
413
|
+
await fh.close();
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
};
|
|
417
|
+
async function createNodeTransports() {
|
|
418
|
+
let ctor = globalThis.WebSocket;
|
|
419
|
+
if (!ctor) {
|
|
420
|
+
const ws = await import("ws");
|
|
421
|
+
ctor = ws.WebSocket ?? ws.default;
|
|
422
|
+
}
|
|
423
|
+
return { rest: (e) => new FetchTransport(e), sockets: webSocketConnector(ctor) };
|
|
424
|
+
}
|
|
425
|
+
var MdnsDiscovery = class {
|
|
426
|
+
constructor(preferIpv4 = true) {
|
|
427
|
+
this.preferIpv4 = preferIpv4;
|
|
428
|
+
}
|
|
429
|
+
preferIpv4;
|
|
430
|
+
async scan(timeoutMs = 3e3) {
|
|
431
|
+
const mod = await import("bonjour-service");
|
|
432
|
+
const Ctor = mod.Bonjour ?? mod.default?.Bonjour;
|
|
433
|
+
if (!Ctor) throw new Error("bonjour-service not available");
|
|
434
|
+
const b = new Ctor();
|
|
435
|
+
const found = /* @__PURE__ */ new Map();
|
|
436
|
+
const browser = b.find({ type: "freedome" }, (s) => {
|
|
437
|
+
const txt = s.txt ?? {};
|
|
438
|
+
const addr = (s.addresses ?? []).find((a) => this.preferIpv4 ? !a.includes(":") : true) ?? s.host;
|
|
439
|
+
const host = addr.includes(":") ? `[${addr}]` : addr;
|
|
440
|
+
const tls = txt.tls === "1" || !!txt.fp;
|
|
441
|
+
const endpoint = new FreedomeEndpoint(`${tls ? "https" : "http"}://${host}:${s.port}`, {
|
|
442
|
+
...txt.id ? { id: txt.id } : {},
|
|
443
|
+
...txt.name || s.name ? { name: txt.name ?? s.name } : {},
|
|
444
|
+
...txt.fp ? { tlsFingerprint: txt.fp } : {}
|
|
445
|
+
});
|
|
446
|
+
found.set(txt.id ?? `${host}:${s.port}`, {
|
|
447
|
+
endpoint,
|
|
448
|
+
name: txt.name ?? s.name,
|
|
449
|
+
source: "mdns"
|
|
450
|
+
});
|
|
451
|
+
});
|
|
452
|
+
await new Promise((r) => setTimeout(r, timeoutMs));
|
|
453
|
+
browser.stop();
|
|
454
|
+
b.destroy();
|
|
455
|
+
return [...found.values()];
|
|
456
|
+
}
|
|
457
|
+
};
|
|
458
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
459
|
+
0 && (module.exports = {
|
|
460
|
+
FileContentSource,
|
|
461
|
+
MdnsDiscovery,
|
|
462
|
+
createNodeTransports,
|
|
463
|
+
formatInputFromPath,
|
|
464
|
+
inputFromPath
|
|
465
|
+
});
|
package/dist/node.d.cts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { F as FormatInput } from './input-CxW5zgc-.cjs';
|
|
2
|
+
import { C as ContentSource, t as Discovery, s as DiscoveredDome } from './discovery-BsEQfHam.cjs';
|
|
3
|
+
import { a as FreedomeTransportFactory } from './transport-dvmDQyvB.cjs';
|
|
4
|
+
import '@freedome/protocol';
|
|
5
|
+
import './errors-L_bIF-OR.cjs';
|
|
6
|
+
|
|
7
|
+
/** Format input of a file or an unpacked directory (symbolic links recorded as refused). */
|
|
8
|
+
declare function inputFromPath(path: string): Promise<FormatInput>;
|
|
9
|
+
/** Upload source reading byte ranges of a file (never the whole file). */
|
|
10
|
+
declare class FileContentSource implements ContentSource {
|
|
11
|
+
readonly path: string;
|
|
12
|
+
readonly name: string;
|
|
13
|
+
readonly length: number;
|
|
14
|
+
readonly contentType: string | undefined;
|
|
15
|
+
private constructor();
|
|
16
|
+
static open(path: string, o?: {
|
|
17
|
+
name?: string;
|
|
18
|
+
contentType?: string;
|
|
19
|
+
}): Promise<FileContentSource>;
|
|
20
|
+
read(start: number, end: number): Promise<Uint8Array>;
|
|
21
|
+
}
|
|
22
|
+
/** fetch + WebSocket (global on Node ≥ 22, else the `ws` package). */
|
|
23
|
+
declare function createNodeTransports(): Promise<FreedomeTransportFactory>;
|
|
24
|
+
/** mDNS `_freedome._tcp` browser (TXT: id, name, tls, fp, api). */
|
|
25
|
+
declare class MdnsDiscovery implements Discovery {
|
|
26
|
+
private readonly preferIpv4;
|
|
27
|
+
constructor(preferIpv4?: boolean);
|
|
28
|
+
scan(timeoutMs?: number): Promise<DiscoveredDome[]>;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export { FileContentSource, MdnsDiscovery, createNodeTransports, inputFromPath as formatInputFromPath, inputFromPath };
|
package/dist/node.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { F as FormatInput } from './input-CxW5zgc-.js';
|
|
2
|
+
import { C as ContentSource, t as Discovery, s as DiscoveredDome } from './discovery-DTV9yp9S.js';
|
|
3
|
+
import { a as FreedomeTransportFactory } from './transport-dvmDQyvB.js';
|
|
4
|
+
import '@freedome/protocol';
|
|
5
|
+
import './errors-L_bIF-OR.js';
|
|
6
|
+
|
|
7
|
+
/** Format input of a file or an unpacked directory (symbolic links recorded as refused). */
|
|
8
|
+
declare function inputFromPath(path: string): Promise<FormatInput>;
|
|
9
|
+
/** Upload source reading byte ranges of a file (never the whole file). */
|
|
10
|
+
declare class FileContentSource implements ContentSource {
|
|
11
|
+
readonly path: string;
|
|
12
|
+
readonly name: string;
|
|
13
|
+
readonly length: number;
|
|
14
|
+
readonly contentType: string | undefined;
|
|
15
|
+
private constructor();
|
|
16
|
+
static open(path: string, o?: {
|
|
17
|
+
name?: string;
|
|
18
|
+
contentType?: string;
|
|
19
|
+
}): Promise<FileContentSource>;
|
|
20
|
+
read(start: number, end: number): Promise<Uint8Array>;
|
|
21
|
+
}
|
|
22
|
+
/** fetch + WebSocket (global on Node ≥ 22, else the `ws` package). */
|
|
23
|
+
declare function createNodeTransports(): Promise<FreedomeTransportFactory>;
|
|
24
|
+
/** mDNS `_freedome._tcp` browser (TXT: id, name, tls, fp, api). */
|
|
25
|
+
declare class MdnsDiscovery implements Discovery {
|
|
26
|
+
private readonly preferIpv4;
|
|
27
|
+
constructor(preferIpv4?: boolean);
|
|
28
|
+
scan(timeoutMs?: number): Promise<DiscoveredDome[]>;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export { FileContentSource, MdnsDiscovery, createNodeTransports, inputFromPath as formatInputFromPath, inputFromPath };
|