@kawaz/sockat 0.0.0-stage → 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/LICENSE +21 -0
- package/README-ja.md +202 -0
- package/README.md +201 -2
- package/dist/bind.d.ts +54 -0
- package/dist/bind.js +255 -0
- package/dist/bun.d.ts +25 -0
- package/dist/bun.js +46 -0
- package/dist/bunapi.d.ts +12 -0
- package/dist/bunapi.js +76 -0
- package/dist/config.d.ts +70 -0
- package/dist/config.js +93 -0
- package/dist/connect.d.ts +33 -0
- package/dist/connect.js +260 -0
- package/dist/drive.d.ts +49 -0
- package/dist/drive.js +122 -0
- package/dist/errors.d.ts +62 -0
- package/dist/errors.js +76 -0
- package/dist/fsx.d.ts +87 -0
- package/dist/fsx.js +149 -0
- package/dist/index.d.ts +64 -0
- package/dist/index.js +69 -0
- package/dist/lock.d.ts +28 -0
- package/dist/lock.js +61 -0
- package/dist/names.d.ts +19 -0
- package/dist/names.js +55 -0
- package/dist/netapi.d.ts +12 -0
- package/dist/netapi.js +80 -0
- package/dist/paths.d.ts +46 -0
- package/dist/paths.js +111 -0
- package/dist/resolve.d.ts +7 -0
- package/dist/resolve.js +98 -0
- package/dist/route.d.ts +79 -0
- package/dist/route.js +108 -0
- package/dist/socketfile.d.ts +38 -0
- package/dist/socketfile.js +91 -0
- package/dist/sweep.d.ts +14 -0
- package/dist/sweep.js +172 -0
- package/dist/types.d.ts +100 -0
- package/dist/types.js +1 -0
- package/dist/world.d.ts +198 -0
- package/dist/world.js +666 -0
- package/package.json +41 -4
package/dist/paths.js
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
// Pure path arithmetic: splitting a socket path into its directory and file
|
|
2
|
+
// name, comparing paths component by component, and spelling an absolute
|
|
3
|
+
// path relative to a working directory. Nothing here touches the file system.
|
|
4
|
+
/** The byte length of a path as the kernel sees it (UTF-8). */
|
|
5
|
+
export function byteLength(path) {
|
|
6
|
+
return Buffer.byteLength(path, "utf8");
|
|
7
|
+
}
|
|
8
|
+
export function isAbs(path) {
|
|
9
|
+
return path.startsWith("/");
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Splits a socket path into the directory that holds the socket and the
|
|
13
|
+
* socket's file name, by what follows the last '/'. A path that does not end
|
|
14
|
+
* with a name ("dir/", "dir/.", "dir/..", "..") gives null: the kernel would
|
|
15
|
+
* resolve it as a directory, not as a socket file.
|
|
16
|
+
*
|
|
17
|
+
* | path | directory | file name |
|
|
18
|
+
* |-------------|-----------|-----------|
|
|
19
|
+
* | /d/e/s.sock | /d/e | s.sock |
|
|
20
|
+
* | /s.sock | / | s.sock |
|
|
21
|
+
* | s.sock | . | s.sock |
|
|
22
|
+
* | d//s.sock | d/ | s.sock |
|
|
23
|
+
*/
|
|
24
|
+
export function splitPath(path) {
|
|
25
|
+
const i = path.lastIndexOf("/");
|
|
26
|
+
let dir;
|
|
27
|
+
let name;
|
|
28
|
+
if (i === 0) {
|
|
29
|
+
dir = "/";
|
|
30
|
+
name = path.slice(1);
|
|
31
|
+
}
|
|
32
|
+
else if (i > 0) {
|
|
33
|
+
dir = path.slice(0, i);
|
|
34
|
+
name = path.slice(i + 1);
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
dir = ".";
|
|
38
|
+
name = path;
|
|
39
|
+
}
|
|
40
|
+
if (name === "" || name === "." || name === "..") {
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
return { dir, name };
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Splits a path into its components the way the kernel walks it: empty
|
|
47
|
+
* segments and "." are dropped, ".." is kept as it is (it is not interpreted
|
|
48
|
+
* lexically, which would be wrong across symlinks). An absolute path starts
|
|
49
|
+
* with the component "/".
|
|
50
|
+
*/
|
|
51
|
+
export function components(path) {
|
|
52
|
+
const out = [];
|
|
53
|
+
if (path.startsWith("/")) {
|
|
54
|
+
out.push("/");
|
|
55
|
+
}
|
|
56
|
+
for (const c of path.split("/")) {
|
|
57
|
+
if (c !== "" && c !== ".") {
|
|
58
|
+
out.push(c);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return out;
|
|
62
|
+
}
|
|
63
|
+
/** Whether a and b are the same spelling component by component. */
|
|
64
|
+
export function sameComponents(a, b) {
|
|
65
|
+
const ca = components(a);
|
|
66
|
+
const cb = components(b);
|
|
67
|
+
return ca.length === cb.length && ca.every((c, i) => c === cb[i]);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* The part of target below the directory prefix, when target starts with
|
|
71
|
+
* prefix component by component ("/a/b" is a prefix of "/a/b/c" but not of
|
|
72
|
+
* "/a/bc"). null when either is not absolute or prefix is not a proper prefix.
|
|
73
|
+
*/
|
|
74
|
+
export function below(target, prefix) {
|
|
75
|
+
if (!isAbs(target) || !isAbs(prefix)) {
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
const t = components(target);
|
|
79
|
+
const p = components(prefix);
|
|
80
|
+
if (t.length <= p.length || !p.every((c, i) => c === t[i])) {
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
83
|
+
return t.slice(p.length).join("/");
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* The absolute path target spelt relative to the absolute directory cwd:
|
|
87
|
+
* climb with ".." out of the part of cwd that target does not share, then
|
|
88
|
+
* descend along the rest of target. Only meaningful when both are real paths
|
|
89
|
+
* (each ".." then climbs out of a real directory, as the kernel does); the
|
|
90
|
+
* caller confirms that the result names the same directory. null when either
|
|
91
|
+
* is not absolute, or target is cwd itself or one of its ancestors.
|
|
92
|
+
*/
|
|
93
|
+
export function relativeTo(target, cwd) {
|
|
94
|
+
if (!isAbs(target) || !isAbs(cwd)) {
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
const t = components(target);
|
|
98
|
+
const c = components(cwd);
|
|
99
|
+
let common = 0;
|
|
100
|
+
while (common < t.length && common < c.length && t[common] === c[common]) {
|
|
101
|
+
common++;
|
|
102
|
+
}
|
|
103
|
+
if (common >= t.length) {
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
return [...c.slice(common).map(() => ".."), ...t.slice(common)].join("/");
|
|
107
|
+
}
|
|
108
|
+
/** The entry name of the directory dir. */
|
|
109
|
+
export function joinUnder(dir, name) {
|
|
110
|
+
return `${dir.endsWith("/") ? dir.slice(0, -1) : dir}/${name}`;
|
|
111
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type StandardSpelling } from "./route.ts";
|
|
2
|
+
import type { ForBind, ForConnect, Options } from "./types.ts";
|
|
3
|
+
import type { World } from "./world.ts";
|
|
4
|
+
/** resolveForBind on w, for the standard spell spells for (node:net's, which also suits Bun's). */
|
|
5
|
+
export declare function resolveForBindIn(w: World, path: string, opts: Options, spell?: StandardSpelling): ForBind;
|
|
6
|
+
/** resolveForConnect on w: link-file only, through the process's D (usableCrossUid is not looked at). */
|
|
7
|
+
export declare function resolveForConnectIn(w: World, path: string, opts: Options, spell?: StandardSpelling): ForConnect;
|
package/dist/resolve.js
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { linkDirSpelling, linkFileSpelling, newBindCall } from "./bind.js";
|
|
2
|
+
import { asError, systemError, Trace, Unusable, withDiagnostics } from "./errors.js";
|
|
3
|
+
import { realFile, sameId, socketAt } from "./fsx.js";
|
|
4
|
+
import { absTarget, classify, cwdId, forNodeNet } from "./route.js";
|
|
5
|
+
import { Cleaner, RecordedSocketFile } from "./socketfile.js";
|
|
6
|
+
// The resolves (SPEC "resolve"): a spelling to hand to the standard yourself.
|
|
7
|
+
/**
|
|
8
|
+
* ForBind's socketFile: follows the spelling now to the socket file bound
|
|
9
|
+
* (its last component followed while a symlink, as bind(2) follows it on
|
|
10
|
+
* some systems) and records it. An abstract name has no file.
|
|
11
|
+
*/
|
|
12
|
+
function socketFileOf(path, spelt, spelling, cwd, cleaner, abstract) {
|
|
13
|
+
if (abstract) {
|
|
14
|
+
return new RecordedSocketFile(null, cleaner);
|
|
15
|
+
}
|
|
16
|
+
if (spelling.kind === "CwdRelative") {
|
|
17
|
+
const now = cwdId();
|
|
18
|
+
if (now === null || cwd === null || !sameId(now, cwd)) {
|
|
19
|
+
throw new Error(`socketfile ${path}: the current directory changed since the path was resolved; the socket file it names may be another`);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
const file = realFile(spelt);
|
|
23
|
+
if (!file.ok) {
|
|
24
|
+
throw systemError(file.code, "socketfile", path);
|
|
25
|
+
}
|
|
26
|
+
const sock = socketAt(file.value);
|
|
27
|
+
if (!sock.ok) {
|
|
28
|
+
throw systemError(sock.code, "socketfile", path);
|
|
29
|
+
}
|
|
30
|
+
return new RecordedSocketFile(sock.value, cleaner);
|
|
31
|
+
}
|
|
32
|
+
/** resolveForBind on w, for the standard spell spells for (node:net's, which also suits Bun's). */
|
|
33
|
+
export function resolveForBindIn(w, path, opts, spell = forNodeNet) {
|
|
34
|
+
const trace = new Trace();
|
|
35
|
+
try {
|
|
36
|
+
const kind = classify(w, path, opts, spell);
|
|
37
|
+
if (kind.kind !== "long") {
|
|
38
|
+
const relative = kind.kind === "short" && !path.startsWith("/");
|
|
39
|
+
const spelling = relative ? { kind: "CwdRelative" } : { kind: "Original" };
|
|
40
|
+
return forBind(path, kind.spelt, spelling, "standard", null, trace, new Cleaner(null, path), relative ? cwdId() : null, kind.kind === "abstract");
|
|
41
|
+
}
|
|
42
|
+
const c = newBindCall(w, path, opts, { syscall: "resolve", path });
|
|
43
|
+
trace.skip("standard", "path too long");
|
|
44
|
+
let route = "link-dir";
|
|
45
|
+
let ln = linkDirSpelling(c);
|
|
46
|
+
if (ln instanceof Unusable && w.cfg.bindFollows === "yes") {
|
|
47
|
+
trace.skip(route, ln.reason);
|
|
48
|
+
route = "link-file";
|
|
49
|
+
const target = absTarget(path);
|
|
50
|
+
ln = target instanceof Unusable ? target : linkFileSpelling(c, target, false);
|
|
51
|
+
}
|
|
52
|
+
if (ln instanceof Unusable) {
|
|
53
|
+
trace.skip(route, ln.reason);
|
|
54
|
+
throw systemError("ENAMETOOLONG", "resolve", path);
|
|
55
|
+
}
|
|
56
|
+
return forBind(path, ln.spelling, ln.sp, route, ln.base, trace, new Cleaner(ln.link, path), null, false);
|
|
57
|
+
}
|
|
58
|
+
catch (e) {
|
|
59
|
+
throw withDiagnostics(asError(e), trace);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
function forBind(path, spelt, spelling, route, base, trace, cleaner, cwd, abstract) {
|
|
63
|
+
return {
|
|
64
|
+
path: spelt,
|
|
65
|
+
spelling,
|
|
66
|
+
route,
|
|
67
|
+
base,
|
|
68
|
+
skipped: [...trace.skipped],
|
|
69
|
+
cleanup: () => cleaner.cleanup(),
|
|
70
|
+
socketFile: () => socketFileOf(path, spelt, spelling, cwd, cleaner, abstract),
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
/** resolveForConnect on w: link-file only, through the process's D (usableCrossUid is not looked at). */
|
|
74
|
+
export function resolveForConnectIn(w, path, opts, spell = forNodeNet) {
|
|
75
|
+
const trace = new Trace();
|
|
76
|
+
const given = opts.base === undefined ? {} : { base: opts.base };
|
|
77
|
+
try {
|
|
78
|
+
const kind = classify(w, path, given, spell);
|
|
79
|
+
if (kind.kind !== "long") {
|
|
80
|
+
return forConnect(kind.spelt, "standard", trace, new Cleaner(null, path));
|
|
81
|
+
}
|
|
82
|
+
const c = newBindCall(w, path, given, { syscall: "resolve", path });
|
|
83
|
+
trace.skip("standard", "path too long");
|
|
84
|
+
const target = absTarget(path);
|
|
85
|
+
const ln = target instanceof Unusable ? target : linkFileSpelling(c, target, w.cfg.os === "linux");
|
|
86
|
+
if (ln instanceof Unusable) {
|
|
87
|
+
trace.skip("link-file", ln.reason);
|
|
88
|
+
throw systemError("ENAMETOOLONG", "resolve", path);
|
|
89
|
+
}
|
|
90
|
+
return forConnect(ln.spelling, "link-file", trace, new Cleaner(ln.link, path));
|
|
91
|
+
}
|
|
92
|
+
catch (e) {
|
|
93
|
+
throw withDiagnostics(asError(e), trace);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
function forConnect(spelt, route, trace, cleaner) {
|
|
97
|
+
return { path: spelt, route, skipped: [...trace.skipped], cleanup: () => cleaner.cleanup() };
|
|
98
|
+
}
|
package/dist/route.d.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { type CallSite, Unusable } from "./errors.ts";
|
|
2
|
+
import { type FileId } from "./fsx.ts";
|
|
3
|
+
import type { Options } from "./types.ts";
|
|
4
|
+
import { type World } from "./world.ts";
|
|
5
|
+
/** The routes' names (SPEC "経路の名前"). */
|
|
6
|
+
export type RouteName = "standard" | "link-dir" | "link-file" | "thread-cwd" | "at" | "proc-direct" | "relative" | "widened" | "at-direct" | "thread-cwd-direct";
|
|
7
|
+
/** The reason of a route sockat-ts never makes (DR-0002). */
|
|
8
|
+
export declare const noNative = "no native code in sockat-ts";
|
|
9
|
+
/**
|
|
10
|
+
* What the standard did with a spelling a route handed to it: bound /
|
|
11
|
+
* connected (close discards the listener or socket), or failed.
|
|
12
|
+
*/
|
|
13
|
+
export type Outcome = {
|
|
14
|
+
readonly ok: true;
|
|
15
|
+
readonly close: () => void;
|
|
16
|
+
} | {
|
|
17
|
+
readonly ok: false;
|
|
18
|
+
readonly error: Error;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* How a standard is handed a path it takes: the path itself, or the same
|
|
22
|
+
* file spelt so that the standard reads it as a path (SPEC "短いパス").
|
|
23
|
+
*/
|
|
24
|
+
export type StandardSpelling = (path: string) => string;
|
|
25
|
+
/** For a standard that reads every string as a path (Bun.listen / Bun.connect's `unix`). */
|
|
26
|
+
export declare const asItIs: StandardSpelling;
|
|
27
|
+
/**
|
|
28
|
+
* For node:net, which reads a string that Number() makes 0 or more ("123",
|
|
29
|
+
* "1e3", " ") as a TCP port: such a spelling without a '/' gets "./" in
|
|
30
|
+
* front, which names the same file relative to the cwd.
|
|
31
|
+
*/
|
|
32
|
+
export declare const forNodeNet: StandardSpelling;
|
|
33
|
+
/** The kind of path, and the spelling to hand the standard when it takes it. */
|
|
34
|
+
export type Kind = {
|
|
35
|
+
readonly kind: "abstract" | "short";
|
|
36
|
+
readonly spelt: string;
|
|
37
|
+
} | {
|
|
38
|
+
readonly kind: "long";
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* The kind of path, after the checks every call makes first (SPEC "短いパス",
|
|
42
|
+
* "エラー"): an Options.base of the wrong form, an empty path, and a NUL
|
|
43
|
+
* other than the leading one of a Linux abstract name, are argument errors
|
|
44
|
+
* whatever the length; an abstract name, and a path whose spelling for the
|
|
45
|
+
* standard (spell) is at most the standard length, go to the standard; a
|
|
46
|
+
* longer one takes the routes.
|
|
47
|
+
*/
|
|
48
|
+
export declare function classify(w: World, path: string, opts: Options, spell: StandardSpelling): Kind;
|
|
49
|
+
/** The socket's directory of a long path, looked up before anything else. */
|
|
50
|
+
export interface SocketDir {
|
|
51
|
+
/** The directory as the caller spelt it. */
|
|
52
|
+
readonly dir: string;
|
|
53
|
+
readonly name: string;
|
|
54
|
+
/** Its real path. */
|
|
55
|
+
readonly real: string;
|
|
56
|
+
readonly id: FileId;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Looks up the socket's directory of a long path first (SPEC "長いパスの経路と
|
|
60
|
+
* 試す順番"): a path not ending with a name is an argument error; a missing
|
|
61
|
+
* directory ENOENT, one that is not a directory ENOTDIR, and a failure to
|
|
62
|
+
* resolve its real path its own errno, before anything is decided or made.
|
|
63
|
+
* Then Options.base is checked (making nothing) with cross as the call has it.
|
|
64
|
+
*/
|
|
65
|
+
export declare function lookUpDir(w: World, path: string, opts: Options, cross: boolean, site: CallSite): SocketDir;
|
|
66
|
+
/**
|
|
67
|
+
* The error of a long path whose route bound / connected and failed: the
|
|
68
|
+
* standard's errno, with the caller's path in place of the spelling (which
|
|
69
|
+
* never shows in an error).
|
|
70
|
+
*/
|
|
71
|
+
export declare function routeFailure(error: Error, site: CallSite, spelling: string): Error;
|
|
72
|
+
/**
|
|
73
|
+
* The caller's spelling made absolute against the cwd, for a symlink to
|
|
74
|
+
* hold: the kernel then resolves the link as it resolves the caller's path.
|
|
75
|
+
* Unusable when the cwd cannot be read.
|
|
76
|
+
*/
|
|
77
|
+
export declare function absTarget(path: string): string | Unusable;
|
|
78
|
+
/** The (st_dev, st_ino) of the cwd, null when it cannot be looked up. */
|
|
79
|
+
export declare function cwdId(): FileId | null;
|
package/dist/route.js
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { invalidArgument, systemError, errnoCode, Unusable } from "./errors.js";
|
|
2
|
+
import { attempt, realDir, sameId, statId } from "./fsx.js";
|
|
3
|
+
import { byteLength, isAbs, splitPath } from "./paths.js";
|
|
4
|
+
import { givenShapeOk } from "./world.js";
|
|
5
|
+
import fs from "node:fs";
|
|
6
|
+
/** The reason of a route sockat-ts never makes (DR-0002). */
|
|
7
|
+
export const noNative = "no native code in sockat-ts";
|
|
8
|
+
/** For a standard that reads every string as a path (Bun.listen / Bun.connect's `unix`). */
|
|
9
|
+
export const asItIs = (path) => path;
|
|
10
|
+
/**
|
|
11
|
+
* For node:net, which reads a string that Number() makes 0 or more ("123",
|
|
12
|
+
* "1e3", " ") as a TCP port: such a spelling without a '/' gets "./" in
|
|
13
|
+
* front, which names the same file relative to the cwd.
|
|
14
|
+
*/
|
|
15
|
+
export const forNodeNet = (path) => (!path.includes("/") && Number(path) >= 0 ? `./${path}` : path);
|
|
16
|
+
/**
|
|
17
|
+
* The kind of path, after the checks every call makes first (SPEC "短いパス",
|
|
18
|
+
* "エラー"): an Options.base of the wrong form, an empty path, and a NUL
|
|
19
|
+
* other than the leading one of a Linux abstract name, are argument errors
|
|
20
|
+
* whatever the length; an abstract name, and a path whose spelling for the
|
|
21
|
+
* standard (spell) is at most the standard length, go to the standard; a
|
|
22
|
+
* longer one takes the routes.
|
|
23
|
+
*/
|
|
24
|
+
export function classify(w, path, opts, spell) {
|
|
25
|
+
if (opts.base !== undefined && !givenShapeOk(opts.base)) {
|
|
26
|
+
throw invalidArgument("options.base", opts.base, "must be an absolute path ending with a name");
|
|
27
|
+
}
|
|
28
|
+
if (path === "") {
|
|
29
|
+
throw invalidArgument("path", path, "must not be empty");
|
|
30
|
+
}
|
|
31
|
+
if (w.cfg.os === "linux" && path.startsWith("\0")) {
|
|
32
|
+
return { kind: "abstract", spelt: path };
|
|
33
|
+
}
|
|
34
|
+
if (path.includes("\0")) {
|
|
35
|
+
throw invalidArgument("path", path, w.cfg.os === "linux"
|
|
36
|
+
? "must not contain a NUL byte other than the leading one of an abstract socket name"
|
|
37
|
+
: "must not contain a NUL byte");
|
|
38
|
+
}
|
|
39
|
+
const spelt = spell(path);
|
|
40
|
+
return byteLength(spelt) <= w.cfg.standardLength ? { kind: "short", spelt } : { kind: "long" };
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Looks up the socket's directory of a long path first (SPEC "長いパスの経路と
|
|
44
|
+
* 試す順番"): a path not ending with a name is an argument error; a missing
|
|
45
|
+
* directory ENOENT, one that is not a directory ENOTDIR, and a failure to
|
|
46
|
+
* resolve its real path its own errno, before anything is decided or made.
|
|
47
|
+
* Then Options.base is checked (making nothing) with cross as the call has it.
|
|
48
|
+
*/
|
|
49
|
+
export function lookUpDir(w, path, opts, cross, site) {
|
|
50
|
+
const parts = splitPath(path);
|
|
51
|
+
if (parts === null) {
|
|
52
|
+
throw invalidArgument("path", path, "must end with a file name");
|
|
53
|
+
}
|
|
54
|
+
const st = attempt(() => fs.statSync(parts.dir, { bigint: true }));
|
|
55
|
+
if (!st.ok) {
|
|
56
|
+
throw systemError(st.code, site.syscall, site.path);
|
|
57
|
+
}
|
|
58
|
+
if (!st.value.isDirectory()) {
|
|
59
|
+
throw systemError("ENOTDIR", site.syscall, site.path);
|
|
60
|
+
}
|
|
61
|
+
const real = realDir(parts.dir);
|
|
62
|
+
if (!real.ok) {
|
|
63
|
+
throw systemError(real.code, site.syscall, site.path);
|
|
64
|
+
}
|
|
65
|
+
// The real path must name the directory stat looked up: another one there
|
|
66
|
+
// now (renamed or replaced in between) would put E elsewhere.
|
|
67
|
+
const seen = statId(real.value);
|
|
68
|
+
if (seen === null || !sameId(seen, { dev: st.value.dev, ino: st.value.ino })) {
|
|
69
|
+
throw new Error(`${site.syscall} ${site.path}: the socket's directory changed while it was looked up`);
|
|
70
|
+
}
|
|
71
|
+
if (opts.base !== undefined) {
|
|
72
|
+
w.checkGiven(opts.base, cross, site);
|
|
73
|
+
}
|
|
74
|
+
return { dir: parts.dir, name: parts.name, real: real.value, id: { dev: st.value.dev, ino: st.value.ino } };
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* The error of a long path whose route bound / connected and failed: the
|
|
78
|
+
* standard's errno, with the caller's path in place of the spelling (which
|
|
79
|
+
* never shows in an error).
|
|
80
|
+
*/
|
|
81
|
+
export function routeFailure(error, site, spelling) {
|
|
82
|
+
const code = errnoCode(error);
|
|
83
|
+
if (code === undefined) {
|
|
84
|
+
// Not the system's answer (a misuse the standard refused, such as Bun's
|
|
85
|
+
// TypeError for missing socket handlers): passed on as it is, unless it
|
|
86
|
+
// would show the spelling.
|
|
87
|
+
return error.message.includes(spelling)
|
|
88
|
+
? new Error(`${site.syscall} failed for ${site.path}: the standard gave an error without an errno`)
|
|
89
|
+
: error;
|
|
90
|
+
}
|
|
91
|
+
return systemError(code, site.syscall, site.path);
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* The caller's spelling made absolute against the cwd, for a symlink to
|
|
95
|
+
* hold: the kernel then resolves the link as it resolves the caller's path.
|
|
96
|
+
* Unusable when the cwd cannot be read.
|
|
97
|
+
*/
|
|
98
|
+
export function absTarget(path) {
|
|
99
|
+
if (isAbs(path)) {
|
|
100
|
+
return path;
|
|
101
|
+
}
|
|
102
|
+
const cwd = attempt(() => process.cwd());
|
|
103
|
+
return cwd.ok ? `${cwd.value}/${path}` : new Unusable("cwd unreadable");
|
|
104
|
+
}
|
|
105
|
+
/** The (st_dev, st_ino) of the cwd, null when it cannot be looked up. */
|
|
106
|
+
export function cwdId() {
|
|
107
|
+
return statId(".");
|
|
108
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { type FileId } from "./fsx.ts";
|
|
2
|
+
import type { SocketFile } from "./types.ts";
|
|
3
|
+
import { type Link } from "./world.ts";
|
|
4
|
+
/**
|
|
5
|
+
* Cleans up the E of a spelling (null for none) for every holder of it: the
|
|
6
|
+
* SocketFile of bindEx, and the ForBind (and the SocketFiles it gives) or
|
|
7
|
+
* ForConnect of a resolve. After a success it does nothing; after a failure
|
|
8
|
+
* the next call tries again. JavaScript runs one call at a time, so two
|
|
9
|
+
* calls never remove at once.
|
|
10
|
+
*/
|
|
11
|
+
export declare class Cleaner {
|
|
12
|
+
readonly link: Link | null;
|
|
13
|
+
/** The caller's path, for the errors. */
|
|
14
|
+
readonly path: string;
|
|
15
|
+
private done;
|
|
16
|
+
constructor(link: Link | null, path: string);
|
|
17
|
+
cleanup(): void;
|
|
18
|
+
}
|
|
19
|
+
/** A bound socket file, recorded right after the bind (its real path and (st_dev, st_ino)), and its spelling's cleanup. */
|
|
20
|
+
export declare class RecordedSocketFile implements SocketFile {
|
|
21
|
+
/** The socket file's real path; null for none (an abstract name). */
|
|
22
|
+
private readonly file;
|
|
23
|
+
private readonly id;
|
|
24
|
+
private readonly cleaner;
|
|
25
|
+
private removed;
|
|
26
|
+
constructor(file: {
|
|
27
|
+
file: string;
|
|
28
|
+
id: FileId;
|
|
29
|
+
} | null, cleaner: Cleaner);
|
|
30
|
+
/**
|
|
31
|
+
* Removes the socket file while its path still names the file bound, then
|
|
32
|
+
* cleans up (both are done; the file's error is thrown first, then the
|
|
33
|
+
* cleanup's). A file gone, or replaced, is left alone without an error.
|
|
34
|
+
*/
|
|
35
|
+
remove(): void;
|
|
36
|
+
cleanup(): void;
|
|
37
|
+
private removeFile;
|
|
38
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import { systemError } from "./errors.js";
|
|
3
|
+
import { attempt, lstat, sameId } from "./fsx.js";
|
|
4
|
+
import { removeLink } from "./world.js";
|
|
5
|
+
// The cleanup of a spelling's E and the removal of a bound socket file (SPEC
|
|
6
|
+
// "remove と cleanup").
|
|
7
|
+
/**
|
|
8
|
+
* Cleans up the E of a spelling (null for none) for every holder of it: the
|
|
9
|
+
* SocketFile of bindEx, and the ForBind (and the SocketFiles it gives) or
|
|
10
|
+
* ForConnect of a resolve. After a success it does nothing; after a failure
|
|
11
|
+
* the next call tries again. JavaScript runs one call at a time, so two
|
|
12
|
+
* calls never remove at once.
|
|
13
|
+
*/
|
|
14
|
+
export class Cleaner {
|
|
15
|
+
link;
|
|
16
|
+
/** The caller's path, for the errors. */
|
|
17
|
+
path;
|
|
18
|
+
done = false;
|
|
19
|
+
constructor(link, path) {
|
|
20
|
+
this.link = link;
|
|
21
|
+
this.path = path;
|
|
22
|
+
}
|
|
23
|
+
cleanup() {
|
|
24
|
+
if (this.done || this.link === null) {
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
const r = removeLink(this.link);
|
|
28
|
+
if (!r.ok) {
|
|
29
|
+
throw systemError(r.code, "unlink", this.path, "the symlink the spelling goes through");
|
|
30
|
+
}
|
|
31
|
+
this.done = true;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
/** A bound socket file, recorded right after the bind (its real path and (st_dev, st_ino)), and its spelling's cleanup. */
|
|
35
|
+
export class RecordedSocketFile {
|
|
36
|
+
/** The socket file's real path; null for none (an abstract name). */
|
|
37
|
+
file;
|
|
38
|
+
id;
|
|
39
|
+
cleaner;
|
|
40
|
+
removed = false;
|
|
41
|
+
constructor(file, cleaner) {
|
|
42
|
+
this.file = file === null ? null : file.file;
|
|
43
|
+
this.id = file === null ? null : file.id;
|
|
44
|
+
this.cleaner = cleaner;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Removes the socket file while its path still names the file bound, then
|
|
48
|
+
* cleans up (both are done; the file's error is thrown first, then the
|
|
49
|
+
* cleanup's). A file gone, or replaced, is left alone without an error.
|
|
50
|
+
*/
|
|
51
|
+
remove() {
|
|
52
|
+
let fileError = null;
|
|
53
|
+
if (!this.removed) {
|
|
54
|
+
fileError = this.removeFile();
|
|
55
|
+
if (fileError === null) {
|
|
56
|
+
this.removed = true;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
try {
|
|
60
|
+
this.cleaner.cleanup();
|
|
61
|
+
}
|
|
62
|
+
catch (e) {
|
|
63
|
+
if (fileError === null) {
|
|
64
|
+
throw e;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
if (fileError !== null) {
|
|
68
|
+
throw fileError;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
cleanup() {
|
|
72
|
+
this.cleaner.cleanup();
|
|
73
|
+
}
|
|
74
|
+
removeFile() {
|
|
75
|
+
if (this.file === null || this.id === null) {
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
const file = this.file;
|
|
79
|
+
const st = lstat(file);
|
|
80
|
+
if (!st.ok) {
|
|
81
|
+
// Gone (or a component no longer a directory): nothing to remove. Any
|
|
82
|
+
// other failure leaves it unknown whether it is the file bound.
|
|
83
|
+
return st.code === "ENOENT" || st.code === "ENOTDIR" ? null : systemError(st.code, "lstat", this.cleaner.path);
|
|
84
|
+
}
|
|
85
|
+
if (!sameId({ dev: st.value.dev, ino: st.value.ino }, this.id)) {
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
const r = attempt(() => fs.unlinkSync(file));
|
|
89
|
+
return r.ok || r.code === "ENOENT" ? null : systemError(r.code, "unlink", this.cleaner.path);
|
|
90
|
+
}
|
|
91
|
+
}
|
package/dist/sweep.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type World } from "./world.ts";
|
|
2
|
+
/**
|
|
3
|
+
* sweep on w: the B of a default name in each of w's places 1 to 3 whose
|
|
4
|
+
* ancestors pass their check. A place or B that fails a check is passed
|
|
5
|
+
* over; nothing is made.
|
|
6
|
+
*/
|
|
7
|
+
export declare function sweepIn(w: World): void;
|
|
8
|
+
/**
|
|
9
|
+
* sweepBase on w: sweep for the B at base (as given to Options.base).
|
|
10
|
+
* Nothing when base, or its parent, does not exist; an argument error for a
|
|
11
|
+
* base of the wrong form; EPERM / ENOTDIR for a B or an ancestor that fails
|
|
12
|
+
* the check.
|
|
13
|
+
*/
|
|
14
|
+
export declare function sweepBaseIn(w: World, base: string): void;
|