@kawaz/sockat 0.0.0 → 0.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/bind.js ADDED
@@ -0,0 +1,255 @@
1
+ import { systemError, Unusable } from "./errors.js";
2
+ import { lstat, sameId, statId } from "./fsx.js";
3
+ import { randomLength } from "./names.js";
4
+ import { byteLength } from "./paths.js";
5
+ import { absTarget, classify, lookUpDir, noNative, routeFailure } from "./route.js";
6
+ import { crossOf, removeLink } from "./world.js";
7
+ /** Looks up the socket's directory (lookUpDir) and starts a bind call. */
8
+ export function newBindCall(w, path, opts, site) {
9
+ const dir = lookUpDir(w, path, opts, opts.usableCrossUid === true, site);
10
+ return { w, path, dir, opts, site, noD: new Set() };
11
+ }
12
+ /** b's D for a route of this call; unusable when it could not be made, for this route and every later one. */
13
+ function ownD(c, b) {
14
+ if (c.noD.has(b)) {
15
+ return new Unusable("D not made");
16
+ }
17
+ const d = c.w.ownD(b);
18
+ if (d === null) {
19
+ c.noD.add(b);
20
+ return new Unusable("D not made");
21
+ }
22
+ return d;
23
+ }
24
+ /**
25
+ * The B for a route of this call: Options.base's when given (its errors
26
+ * thrown, except a spelling estimated not to fit); otherwise the world's B of
27
+ * the places 1 to 3 (cross: one others can follow), and, when none of them
28
+ * has or can make one (not for cross), the socket's directory's.
29
+ */
30
+ function baseFor(c, cross, fits) {
31
+ if (c.opts.base !== undefined) {
32
+ return c.w.givenBase(c.opts.base, fits, c.site);
33
+ }
34
+ const b = c.w.autoBase(cross, fits);
35
+ if (!(b instanceof Unusable) || b.reason !== "no base") {
36
+ return b;
37
+ }
38
+ if (cross) {
39
+ return new Unusable("no base others can follow");
40
+ }
41
+ if (!fits("sockdir", "")) {
42
+ return new Unusable("spelling too long");
43
+ }
44
+ return c.w.sockdirBase(c.dir.real, c.dir.id) ?? b;
45
+ }
46
+ /** /proc/<pid>/fd. */
47
+ function procFD(w) {
48
+ return `${w.cfg.proc}/${process.pid}/fd`;
49
+ }
50
+ /**
51
+ * procB, /proc/<pid>/fd/<B's fd>, when /proc can be used for it: a stat of
52
+ * it has B's (st_dev, st_ino) (SPEC-routes "綴りの形").
53
+ */
54
+ function procB(w, b) {
55
+ if (w.cfg.os !== "linux" || w.cfg.proc === "") {
56
+ return null;
57
+ }
58
+ const p = `${procFD(w)}/${b.fd}`;
59
+ const id = statId(p);
60
+ return id !== null && sameId(id, b.id) ? p : null;
61
+ }
62
+ /** The shortest procB can be (a one-digit fd), for the estimate before B is opened; null without /proc. */
63
+ function procBShortest(w) {
64
+ if (w.cfg.os !== "linux" || w.cfg.proc === "" || statId(procFD(w)) === null) {
65
+ return null;
66
+ }
67
+ return `${procFD(w)}/0`;
68
+ }
69
+ /** The length of link-dir's spelling <bspell>/<D>/<E>/<name>. */
70
+ function linkDirLen(bspell, e, name) {
71
+ return byteLength(bspell) + 1 + randomLength + 1 + byteLength(e) + 1 + byteLength(name);
72
+ }
73
+ /** The length of link-file's spelling <bspell>/<D>/<E>. */
74
+ function linkFileLen(bspell, e) {
75
+ return byteLength(bspell) + 1 + randomLength + 1 + byteLength(e);
76
+ }
77
+ /**
78
+ * What a spelling through b is good for: through procB it lasts as long as
79
+ * the process, is followed by the same uid only, and goes through no /tmp;
80
+ * through B's real path it lasts until the cleanup, and b's mode and place
81
+ * decide the rest.
82
+ */
83
+ function linkedSpelling(b, viaProc, removable) {
84
+ if (viaProc) {
85
+ return { kind: "Linked", removable, until: "Process", usableCrossUid: false, usableWithPrivateTmp: true };
86
+ }
87
+ return { kind: "Linked", removable, until: "Cleanup", usableCrossUid: crossOf(b), usableWithPrivateTmp: !b.tmpPath };
88
+ }
89
+ /**
90
+ * The diagnostics' base of a spelling through b: the form of the spelling
91
+ * for a B in a temporary place (procB is "proc", a real path the place's
92
+ * kind), and the B itself otherwise ("sockdir", "given").
93
+ */
94
+ function baseValue(b, viaProc) {
95
+ if (viaProc && (b.kind === "tmp" || b.kind === "runtime" || b.kind === "tmpdir")) {
96
+ return "proc";
97
+ }
98
+ return b.kind;
99
+ }
100
+ /**
101
+ * Makes the spelling <B>/<D>/<E><rest> of length size(<B>, <E>), E a resolve
102
+ * E in the process's D of the call's B pointing to target. B is spelt by
103
+ * procB when useProc and it fits, otherwise by its real path when B is not in
104
+ * the socket's directory and it fits; unusable when neither does (estimated
105
+ * before B is made, with the shortest procB and E).
106
+ */
107
+ function throughB(c, useProc, target, rest, removable, size) {
108
+ const w = c.w;
109
+ const max = w.cfg.spellingLength;
110
+ const fits = (kind, breal) => {
111
+ const p = procBShortest(w);
112
+ if (useProc && p !== null && size(p, "0") <= max) {
113
+ return true;
114
+ }
115
+ return kind !== "sockdir" && size(breal, "0") <= max;
116
+ };
117
+ const b = baseFor(c, c.opts.usableCrossUid === true, fits);
118
+ if (b instanceof Unusable) {
119
+ return b;
120
+ }
121
+ const e = b.nextE.toString(36);
122
+ let spell = null;
123
+ let viaProc = false;
124
+ if (useProc) {
125
+ const p = procB(w, b);
126
+ if (p !== null && size(p, e) <= max) {
127
+ spell = p;
128
+ viaProc = true;
129
+ }
130
+ }
131
+ if (spell === null && b.kind !== "sockdir" && size(b.real, e) <= max) {
132
+ spell = b.real;
133
+ }
134
+ if (spell === null) {
135
+ return new Unusable("spelling too long");
136
+ }
137
+ const d = ownD(c, b);
138
+ if (d instanceof Unusable) {
139
+ return d;
140
+ }
141
+ const link = w.resolveE(b, d, target);
142
+ if (link === null) {
143
+ return new Unusable("E not made");
144
+ }
145
+ const spelling = `${spell}/${d.name}/${link.name}${rest}`;
146
+ if (byteLength(spelling) > max) {
147
+ // A name taken made the E's name longer than estimated.
148
+ removeLink(link);
149
+ return new Unusable("spelling too long");
150
+ }
151
+ return { spelling, link, sp: linkedSpelling(b, viaProc, removable), base: baseValue(b, viaProc) };
152
+ }
153
+ /**
154
+ * link-dir's spelling <B>/<D>/<E>/<name>, E a symlink to the socket's
155
+ * directory (its real path), B spelt by procB (Linux with /proc, not for
156
+ * usableCrossUid) or its real path, the first that fits.
157
+ */
158
+ export function linkDirSpelling(c) {
159
+ const useProc = c.w.cfg.os === "linux" && c.opts.usableCrossUid !== true;
160
+ return throughB(c, useProc, c.dir.real, `/${c.dir.name}`, true, (bspell, e) => linkDirLen(bspell, e, c.dir.name));
161
+ }
162
+ /**
163
+ * link-file's spelling <B>/<D>/<E>, E a symlink to target (the socket file),
164
+ * B spelt by procB when useProc (a connect's resolve) or its real path.
165
+ */
166
+ export function linkFileSpelling(c, target, useProc) {
167
+ return throughB(c, useProc, target, "", false, linkFileLen);
168
+ }
169
+ /** The routes of the bind order after standard on this system, with those not made. */
170
+ function bindOrder(w) {
171
+ const order = ["link-dir"];
172
+ if (w.cfg.bindFollows !== "no") {
173
+ order.push("link-file");
174
+ }
175
+ switch (w.cfg.os) {
176
+ case "linux":
177
+ case "darwin":
178
+ order.push("thread-cwd");
179
+ break;
180
+ case "freebsd":
181
+ order.push("at");
182
+ break;
183
+ default:
184
+ break;
185
+ }
186
+ return order;
187
+ }
188
+ /**
189
+ * The bind of path, as the steps the standard takes: yields each spelling to
190
+ * bind (and listen) and gets what the standard did with it. A path the
191
+ * standard takes, and an abstract name, are yielded as they are; their
192
+ * failure is the standard's own error. A long path takes the bind order; a
193
+ * route that bound and failed removes its E and throws the standard's errno
194
+ * with the caller's path. ENAMETOOLONG when no route can be used.
195
+ */
196
+ export function* bindSteps(w, path, opts, trace, syscall, spell) {
197
+ const site = { syscall, path };
198
+ const kind = classify(w, path, opts, spell);
199
+ if (kind.kind !== "long") {
200
+ const o = yield kind.spelt;
201
+ trace.route = "standard";
202
+ if (!o.ok) {
203
+ throw o.error;
204
+ }
205
+ const spelling = kind.kind === "short" && !path.startsWith("/") ? { kind: "CwdRelative" } : { kind: "Original" };
206
+ return { route: "standard", spelling, base: null, link: null, spelt: kind.spelt };
207
+ }
208
+ const c = newBindCall(w, path, opts, site);
209
+ trace.skip("standard", "path too long");
210
+ for (const route of bindOrder(w)) {
211
+ let ln;
212
+ let target = "";
213
+ switch (route) {
214
+ case "link-dir":
215
+ ln = linkDirSpelling(c);
216
+ break;
217
+ case "link-file": {
218
+ const t = absTarget(path);
219
+ if (t instanceof Unusable) {
220
+ ln = t;
221
+ break;
222
+ }
223
+ target = t;
224
+ ln = linkFileSpelling(c, t, false);
225
+ break;
226
+ }
227
+ default:
228
+ trace.skip(route, noNative);
229
+ continue;
230
+ }
231
+ if (ln instanceof Unusable) {
232
+ trace.skip(route, ln.reason);
233
+ continue;
234
+ }
235
+ const o = yield ln.spelling;
236
+ if (o.ok) {
237
+ trace.route = route;
238
+ return { route, spelling: ln.sp, base: ln.base, link: ln.link, spelt: ln.spelling };
239
+ }
240
+ // A failure to remove E is not reported: the bind's error is the call's.
241
+ removeLink(ln.link);
242
+ // Where bind(2) is not known to follow a symlink, a failure with nothing
243
+ // at the path is taken for one that did not follow it (SPEC-routes
244
+ // "link-file"). Where it is known to follow, the failure is real
245
+ // (EACCES, ...): no route after link-file here (thread-cwd is not made)
246
+ // would meet it again and return it.
247
+ if (route === "link-file" && w.cfg.bindFollows === "unknown" && !lstat(target).ok) {
248
+ trace.skip(route, "bind did not follow the symlink");
249
+ continue;
250
+ }
251
+ trace.route = route;
252
+ throw routeFailure(o.error, site, ln.spelling);
253
+ }
254
+ throw systemError("ENAMETOOLONG", syscall, path);
255
+ }
package/dist/bun.d.ts ADDED
@@ -0,0 +1,25 @@
1
+ /// <reference types="bun" preserve="true" />
2
+ import type { Bound, Connected, Options } from "./types.ts";
3
+ export type { Bound, Connected, Options, Skip, SocketFile, Spelling } from "./types.ts";
4
+ export type { RouteDiagnostics, SystemError } from "./errors.ts";
5
+ /**
6
+ * `Bun.listen(options)` for a `unix` path of any length: synchronous, as
7
+ * Bun.listen is. The listener's `unix` is the path given. Stopping it
8
+ * unlinks the spelling the kernel keeps: for a long path through a symlink
9
+ * to the socket file (Spelling `Linked` with `removable: false`), that
10
+ * removes the symlink and leaves the socket file.
11
+ */
12
+ export declare function listen<Data = undefined>(options: Bun.UnixSocketOptions<Data>): Bun.UnixSocketListener<Data>;
13
+ /** `Bun.connect(options)` for a `unix` path of any length. */
14
+ export declare function connect<Data = undefined>(options: Bun.UnixSocketOptions<Data>): Promise<Bun.Socket<Data>>;
15
+ /**
16
+ * listen, with what the spelling is good for, how to remove the socket file
17
+ * (`file.remove()`), and diagnostics. The error carries `route` and
18
+ * `skipped`.
19
+ *
20
+ * Unlike the specification, stopping the listener unlinks the spelling (Bun
21
+ * gives no way to stop it): call `file.remove()` before stopping it.
22
+ */
23
+ export declare function listenEx<Data = undefined>(options: Bun.UnixSocketOptions<Data>, sockatOptions?: Options): Bound<Bun.UnixSocketListener<Data>>;
24
+ /** connect, with diagnostics. The error carries `route` and `skipped`. */
25
+ export declare function connectEx<Data = undefined>(options: Bun.UnixSocketOptions<Data>, sockatOptions?: Options): Promise<Connected<Bun.Socket<Data>>>;
package/dist/bun.js ADDED
@@ -0,0 +1,46 @@
1
+ /// <reference types="bun" preserve="true" />
2
+ /**
3
+ * sockat for Bun's own sockets: `Bun.listen` / `Bun.connect` with a `unix`
4
+ * path longer than sun_path. Takes the options `Bun.listen` / `Bun.connect`
5
+ * take; `unix` may be long.
6
+ *
7
+ * The behaviour is the language-independent specification's:
8
+ * https://github.com/kawaz/sockat/blob/main/docs/SPEC.md
9
+ *
10
+ * @module
11
+ */
12
+ import { connectExIn, connectIn, listenExIn, listenIn, requireBun } from "./bunapi.js";
13
+ import { defaultWorld } from "./world.js";
14
+ /**
15
+ * `Bun.listen(options)` for a `unix` path of any length: synchronous, as
16
+ * Bun.listen is. The listener's `unix` is the path given. Stopping it
17
+ * unlinks the spelling the kernel keeps: for a long path through a symlink
18
+ * to the socket file (Spelling `Linked` with `removable: false`), that
19
+ * removes the symlink and leaves the socket file.
20
+ */
21
+ export function listen(options) {
22
+ requireBun();
23
+ return listenIn(defaultWorld(), options);
24
+ }
25
+ /** `Bun.connect(options)` for a `unix` path of any length. */
26
+ export async function connect(options) {
27
+ requireBun();
28
+ return connectIn(defaultWorld(), options);
29
+ }
30
+ /**
31
+ * listen, with what the spelling is good for, how to remove the socket file
32
+ * (`file.remove()`), and diagnostics. The error carries `route` and
33
+ * `skipped`.
34
+ *
35
+ * Unlike the specification, stopping the listener unlinks the spelling (Bun
36
+ * gives no way to stop it): call `file.remove()` before stopping it.
37
+ */
38
+ export function listenEx(options, sockatOptions = {}) {
39
+ requireBun();
40
+ return listenExIn(defaultWorld(), options, sockatOptions);
41
+ }
42
+ /** connect, with diagnostics. The error carries `route` and `skipped`. */
43
+ export async function connectEx(options, sockatOptions = {}) {
44
+ requireBun();
45
+ return connectExIn(defaultWorld(), options, sockatOptions);
46
+ }
@@ -0,0 +1,12 @@
1
+ /// <reference types="bun" preserve="true" />
2
+ import type { Bound, Connected, Options } from "./types.ts";
3
+ import type { World } from "./world.ts";
4
+ /**
5
+ * Throws at once where `@kawaz/sockat/bun` is used without Bun, before
6
+ * anything is made in the places.
7
+ */
8
+ export declare function requireBun(): void;
9
+ export declare function listenIn<Data>(w: World, options: Bun.UnixSocketOptions<Data>): Bun.UnixSocketListener<Data>;
10
+ export declare function listenExIn<Data>(w: World, options: Bun.UnixSocketOptions<Data>, sockatOptions: Options): Bound<Bun.UnixSocketListener<Data>>;
11
+ export declare function connectIn<Data>(w: World, options: Bun.UnixSocketOptions<Data>): Promise<Bun.Socket<Data>>;
12
+ export declare function connectExIn<Data>(w: World, options: Bun.UnixSocketOptions<Data>, sockatOptions: Options): Promise<Connected<Bun.Socket<Data>>>;
package/dist/bunapi.js ADDED
@@ -0,0 +1,76 @@
1
+ /// <reference types="bun" preserve="true" />
2
+ import { bindSteps } from "./bind.js";
3
+ import { connectSteps } from "./connect.js";
4
+ import { beforeBind, boundOf, diagnosed, driveAsync, driveSync } from "./drive.js";
5
+ import { asError, Trace } from "./errors.js";
6
+ import { asItIs } from "./route.js";
7
+ // The Bun.listen / Bun.connect side of the public API, on a given world (the
8
+ // tests hand their own to force each route).
9
+ /** Bun.listen with options at spelling. */
10
+ function listenAt(options) {
11
+ return (spelling) => {
12
+ try {
13
+ const listener = Bun.listen({ ...options, unix: spelling });
14
+ return { ok: true, socket: listener, close: () => listener.stop(true) };
15
+ }
16
+ catch (e) {
17
+ return { ok: false, error: asError(e) };
18
+ }
19
+ };
20
+ }
21
+ /** Bun.connect with options to spelling. */
22
+ function connectAt(options) {
23
+ return (spelling) => Bun.connect({ ...options, unix: spelling }).then((socket) => ({ ok: true, socket, close: () => socket.terminate() }), (e) => ({ ok: false, error: asError(e) }));
24
+ }
25
+ /**
26
+ * The listener's `unix` is the caller's path, not the spelling the kernel
27
+ * keeps (DR-0002), when they differ. Bun's own listener keeps returning the
28
+ * path it was given after stop(), and so does this one. Bun's stop() still
29
+ * unlinks the spelling.
30
+ */
31
+ function showPath(listener, path, spelt) {
32
+ if (spelt !== path) {
33
+ Object.defineProperty(listener, "unix", { value: path, configurable: true, enumerable: true });
34
+ }
35
+ }
36
+ /**
37
+ * Throws at once where `@kawaz/sockat/bun` is used without Bun, before
38
+ * anything is made in the places.
39
+ */
40
+ export function requireBun() {
41
+ if (typeof Bun === "undefined") {
42
+ throw new Error("@kawaz/sockat/bun needs Bun (Bun.listen / Bun.connect); with Node, use @kawaz/sockat");
43
+ }
44
+ }
45
+ export function listenIn(w, options) {
46
+ const { result, socket } = driveSync(bindSteps(w, options.unix, {}, new Trace(), "listen", asItIs), listenAt(options));
47
+ showPath(socket, options.unix, result.spelt);
48
+ return socket;
49
+ }
50
+ export function listenExIn(w, options, sockatOptions) {
51
+ const trace = new Trace();
52
+ const path = options.unix;
53
+ try {
54
+ const before = beforeBind(path);
55
+ const { result, socket } = driveSync(bindSteps(w, path, sockatOptions, trace, "listen", asItIs), listenAt(options));
56
+ showPath(socket, path, result.spelt);
57
+ return boundOf(path, before, result, socket, () => socket.stop(true), trace);
58
+ }
59
+ catch (e) {
60
+ throw diagnosed(e, trace);
61
+ }
62
+ }
63
+ export async function connectIn(w, options) {
64
+ const { socket } = await driveAsync(connectSteps(w, options.unix, {}, new Trace(), "connect", asItIs), connectAt(options));
65
+ return socket;
66
+ }
67
+ export async function connectExIn(w, options, sockatOptions) {
68
+ const trace = new Trace();
69
+ try {
70
+ const { result, socket } = await driveAsync(connectSteps(w, options.unix, sockatOptions, trace, "connect", asItIs), connectAt(options));
71
+ return { socket, route: result.route, skipped: [...trace.skipped] };
72
+ }
73
+ catch (e) {
74
+ throw diagnosed(e, trace);
75
+ }
76
+ }
@@ -0,0 +1,70 @@
1
+ /** The systems as SPEC groups them ("用語": Linux / Android, Apple's, ...). */
2
+ export type Os = "linux" | "darwin" | "freebsd" | "netbsd" | "openbsd" | "illumos" | "other";
3
+ /** The runtime whose standard the spellings go to. */
4
+ export type Runtime = "node" | "bun";
5
+ /** Whether bind(2) follows a symlink in the last component (SPEC "bind の順"). */
6
+ export type Follows = "yes" | "no" | "unknown";
7
+ /**
8
+ * How D is locked (DR-0002 "D のロック"): opening it O_SHLOCK / O_EXLOCK
9
+ * (the BSDs and Apple's), flock(1) on an fd handed to it (Linux), or no way
10
+ * (then D cannot be made).
11
+ */
12
+ export type LockMethod = {
13
+ readonly kind: "bsd";
14
+ } | {
15
+ readonly kind: "flock-command";
16
+ readonly command: string;
17
+ } | {
18
+ readonly kind: "none";
19
+ };
20
+ export interface Config {
21
+ readonly os: Os;
22
+ /**
23
+ * The longest path handed to the standard as it is (DR-0003), and so the
24
+ * longest spelling a route may hand to it.
25
+ */
26
+ readonly standardLength: number;
27
+ /**
28
+ * The longest spelling a route hands to the kernel: sun_path less the NUL
29
+ * (SPEC "sockaddr に入る長さ": 107 on Linux and illumos, 103 elsewhere),
30
+ * whatever the standard would take.
31
+ */
32
+ readonly spellingLength: number;
33
+ /** The first place for B (SPEC-routes "実体の置き場"): /tmp. */
34
+ readonly tmp: string;
35
+ /** Reads $XDG_RUNTIME_DIR and $TMPDIR at each call. */
36
+ readonly getenv: (name: string) => string | undefined;
37
+ /** Where the kernel's /proc is ("" for none: the routes through it are not tried). */
38
+ readonly proc: string;
39
+ /** The directories a systemd PrivateTmp replaces (Spelling's usableWithPrivateTmp). */
40
+ readonly privateTmps: readonly string[];
41
+ readonly bindFollows: Follows;
42
+ /** Random names of D, of the default connect's E and of a B fallen back to. */
43
+ readonly random: () => string;
44
+ readonly lock: LockMethod;
45
+ }
46
+ /** The system this process runs on, as SPEC groups them. */
47
+ export declare function currentOs(): Os;
48
+ export declare function currentRuntime(): Runtime;
49
+ /**
50
+ * The standard length (DR-0003): on Linux what the runtime hands to the
51
+ * kernel unchanged in a sockaddr it does not widen (Node 108, Bun 107),
52
+ * elsewhere sun_path less the NUL (103 on Apple's and the BSDs, 107 on
53
+ * illumos).
54
+ *
55
+ * Design rationale: a system SPEC does not name gets 103, the shortest
56
+ * sun_path less the NUL among the systems measured: a path the standard
57
+ * might cut short or widen is better taken by the routes.
58
+ */
59
+ export declare function standardLength(os: Os, runtime: Runtime): number;
60
+ /**
61
+ * sun_path less the NUL (SPEC "sockaddr に入る長さ"). A system SPEC does not
62
+ * name gets 103, as for the standard length.
63
+ */
64
+ export declare function sockaddrLength(os: Os): number;
65
+ /** The systems' table of whether bind(2) follows a symlink (SPEC "bind の順"). */
66
+ export declare function bindFollowsOn(os: Os): Follows;
67
+ /** The systems' way of locking D (DR-0002). */
68
+ export declare function lockMethodOn(os: Os): LockMethod;
69
+ /** The configuration of this process's environment. */
70
+ export declare function envConfig(): Config;
package/dist/config.js ADDED
@@ -0,0 +1,93 @@
1
+ import { randomName } from "./names.js";
2
+ /** The system this process runs on, as SPEC groups them. */
3
+ export function currentOs() {
4
+ switch (process.platform) {
5
+ case "linux":
6
+ case "android":
7
+ return "linux";
8
+ case "darwin":
9
+ case "freebsd":
10
+ case "netbsd":
11
+ case "openbsd":
12
+ return process.platform;
13
+ case "sunos":
14
+ return "illumos";
15
+ default:
16
+ return "other";
17
+ }
18
+ }
19
+ export function currentRuntime() {
20
+ return typeof process.versions["bun"] === "string" ? "bun" : "node";
21
+ }
22
+ /**
23
+ * The standard length (DR-0003): on Linux what the runtime hands to the
24
+ * kernel unchanged in a sockaddr it does not widen (Node 108, Bun 107),
25
+ * elsewhere sun_path less the NUL (103 on Apple's and the BSDs, 107 on
26
+ * illumos).
27
+ *
28
+ * Design rationale: a system SPEC does not name gets 103, the shortest
29
+ * sun_path less the NUL among the systems measured: a path the standard
30
+ * might cut short or widen is better taken by the routes.
31
+ */
32
+ export function standardLength(os, runtime) {
33
+ switch (os) {
34
+ case "linux":
35
+ return runtime === "bun" ? 107 : 108;
36
+ case "illumos":
37
+ return 107;
38
+ default:
39
+ return 103;
40
+ }
41
+ }
42
+ /**
43
+ * sun_path less the NUL (SPEC "sockaddr に入る長さ"). A system SPEC does not
44
+ * name gets 103, as for the standard length.
45
+ */
46
+ export function sockaddrLength(os) {
47
+ return os === "linux" || os === "illumos" ? 107 : 103;
48
+ }
49
+ /** The systems' table of whether bind(2) follows a symlink (SPEC "bind の順"). */
50
+ export function bindFollowsOn(os) {
51
+ switch (os) {
52
+ case "darwin":
53
+ case "netbsd":
54
+ return "yes";
55
+ case "linux":
56
+ case "freebsd":
57
+ case "openbsd":
58
+ case "illumos":
59
+ return "no";
60
+ default:
61
+ return "unknown";
62
+ }
63
+ }
64
+ /** The systems' way of locking D (DR-0002). */
65
+ export function lockMethodOn(os) {
66
+ switch (os) {
67
+ case "darwin":
68
+ case "freebsd":
69
+ case "netbsd":
70
+ case "openbsd":
71
+ return { kind: "bsd" };
72
+ case "linux":
73
+ return { kind: "flock-command", command: "flock" };
74
+ default:
75
+ return { kind: "none" };
76
+ }
77
+ }
78
+ /** The configuration of this process's environment. */
79
+ export function envConfig() {
80
+ const os = currentOs();
81
+ return {
82
+ os,
83
+ standardLength: standardLength(os, currentRuntime()),
84
+ spellingLength: sockaddrLength(os),
85
+ tmp: "/tmp",
86
+ getenv: (name) => process.env[name],
87
+ proc: os === "linux" ? "/proc" : "",
88
+ privateTmps: ["/tmp", "/var/tmp"],
89
+ bindFollows: bindFollowsOn(os),
90
+ random: randomName,
91
+ lock: lockMethodOn(os),
92
+ };
93
+ }
@@ -0,0 +1,33 @@
1
+ import { type Trace } from "./errors.ts";
2
+ import { type FileId } from "./fsx.ts";
3
+ import { type Outcome, type RouteName, type StandardSpelling } from "./route.ts";
4
+ import type { Options } from "./types.ts";
5
+ import { type World } from "./world.ts";
6
+ /** What a connect produced besides the socket. */
7
+ export interface ConnectResult {
8
+ readonly route: RouteName;
9
+ }
10
+ /** A relative spelling of an absolute socket path, checked to name the same directory. */
11
+ export interface Relative {
12
+ readonly spelling: string;
13
+ /** The cwd when the spelling was made. */
14
+ readonly cwd: FileId;
15
+ }
16
+ /**
17
+ * path (whose directory part is dir) spelt relative to the cwd (SPEC-routes
18
+ * "相対パスの作り方"): below a spelling of the cwd when path starts with one,
19
+ * otherwise (only when that gives nothing short enough) through real paths,
20
+ * climbing out of the cwd with "..". The shorter is kept, and used only when
21
+ * its directory is the absolute one and the cwd did not change meanwhile.
22
+ * Lengths are those of the spelling as the standard is handed it (spell).
23
+ * null when there is no such spelling of at most max bytes.
24
+ */
25
+ export declare function newRelative(path: string, dir: string, max: number, spell: StandardSpelling): Relative | null;
26
+ /**
27
+ * The connect of path, as the steps the standard takes (see bindSteps): a
28
+ * path the standard takes and an abstract name go to it as they are; a
29
+ * longer one takes the connect order. relative discards a socket connected
30
+ * while the cwd changed (a general error); link-file unlinks its E right
31
+ * after, whatever the outcome.
32
+ */
33
+ export declare function connectSteps(w: World, path: string, opts: Options, trace: Trace, syscall: string, spell: StandardSpelling): Generator<string, ConnectResult, Outcome>;