@3leaps/fsprims 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-APACHE ADDED
@@ -0,0 +1,190 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ Copyright 2026 3 Leaps, LLC
179
+
180
+ Licensed under the Apache License, Version 2.0 (the "License");
181
+ you may not use this file except in compliance with the License.
182
+ You may obtain a copy of the License at
183
+
184
+ http://www.apache.org/licenses/LICENSE-2.0
185
+
186
+ Unless required by applicable law or agreed to in writing, software
187
+ distributed under the License is distributed on an "AS IS" BASIS,
188
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
189
+ See the License for the specific language governing permissions and
190
+ limitations under the License.
package/LICENSE-MIT ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 3 Leaps, LLC
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/NOTICE ADDED
@@ -0,0 +1,23 @@
1
+ fsprims
2
+ Copyright 2026 3 Leaps, LLC
3
+
4
+ This project is dual-licensed under the MIT License (see LICENSE-MIT) or the
5
+ Apache License, Version 2.0 (see LICENSE-APACHE), at your option.
6
+
7
+ Subject to 3 Leaps OSS policies: https://github.com/3leaps/oss-policies
8
+
9
+ ## Project Name Notice
10
+
11
+ "fsprims" is a project name of 3 Leaps, LLC, used to identify this software.
12
+ "3 Leaps®" is a registered trademark of 3 Leaps, LLC, a North Carolina LLC.
13
+
14
+ While code and documentation are open under the MIT or Apache-2.0 License,
15
+ usage of the "fsprims" project name is reserved for official implementations
16
+ to prevent confusion and benefit the ecosystem. Use of this name in derivative
17
+ works does not imply endorsement by 3 Leaps, LLC.
18
+
19
+ For the benefit of the community, please rename your project folder and
20
+ configurations to avoid using "3leaps" or "fsprims" in derivative works that
21
+ are not official 3 Leaps projects.
22
+
23
+ For questions regarding project name usage, contact hello@3leaps.net.
package/README.md CHANGED
@@ -1,3 +1,50 @@
1
- # Temporary Holding Version
1
+ # @3leaps/fsprims
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ TypeScript bindings for [fsprims](https://github.com/3leaps/fsprims): typed
4
+ local filesystem operations relative to a held root, with explicit link and
5
+ containment policy enforced at the operation.
6
+
7
+ **Status:** experimental, 0.x. The package is a Node-API addon (napi-rs) built
8
+ from the Rust tree and implements the experimental `filesystem-access/v0`
9
+ contract; it may change when the contract does. `containment/v0` is supported;
10
+ `no-links/v0` is refused with `unsupported_capability`.
11
+
12
+ ## Profiles
13
+
14
+ The `filesystem-access/v0` contract defines `containment/v0` and
15
+ `no-links/v0` (`profileIds()`). This release enforces `containment/v0` only:
16
+ a request under `no-links/v0` fails with `unsupported_capability` after name
17
+ validation and before any resolution or mutation. `supportedProfileIds()`
18
+ returns the enforced profiles.
19
+
20
+ ## Install
21
+
22
+ ```sh
23
+ npm install @3leaps/fsprims
24
+ ```
25
+
26
+ Node.js 22 or later. The prebuilt addon comes from a platform package that npm
27
+ selects for the host:
28
+
29
+ | Platform | Package |
30
+ | --- | --- |
31
+ | Linux x64 (glibc 2.17 or later) | `@3leaps/fsprims-linux-x64-gnu` |
32
+ | Linux arm64 (glibc 2.17 or later) | `@3leaps/fsprims-linux-arm64-gnu` |
33
+ | macOS arm64 | `@3leaps/fsprims-darwin-arm64` |
34
+ | Windows x64 | `@3leaps/fsprims-win32-x64-msvc` |
35
+
36
+ Other platforms (musl, Windows arm64, macOS x64) have no prebuilt package;
37
+ build from a checkout.
38
+
39
+ ## Build from a checkout
40
+
41
+ ```sh
42
+ npm install
43
+ npm test # tsc, then napi build, then the conformance runner
44
+ ```
45
+
46
+ ## License
47
+
48
+ Licensed under either of the [MIT License](LICENSE-MIT) or the
49
+ [Apache License, Version 2.0](LICENSE-APACHE), at your option. See
50
+ [NOTICE](NOTICE).
@@ -0,0 +1,126 @@
1
+ /**
2
+ * A `filesystem-access/v0` policy profile identifier.
3
+ *
4
+ * The contract defines both profiles. This release enforces `containment/v0`
5
+ * only: an operation requested under `no-links/v0` fails with
6
+ * `unsupported_capability` after name validation and before any resolution or
7
+ * mutation. `supportedProfileIds()` lists the enforced profiles.
8
+ */
9
+ export type ProfileId = "containment/v0" | "no-links/v0";
10
+ export type AdversaryLevel = "stable_tree" | "concurrent_rename_resistant";
11
+ export type ErrorCategory = "invalid_name" | "policy_denied" | "unsupported_capability" | "not_found" | "already_exists" | "wrong_type" | "bound_exceeded" | "io_error" | "unknown_mutation_outcome";
12
+ export type PolicyReason = "escape" | "link_refused" | "reparse_refused" | "special_file" | "cross_device" | "hop_limit";
13
+ export type FileKind = "directory" | "regular_file" | "symlink" | "reparse_point" | "other";
14
+ export type ObjectId = {
15
+ kind: "unix";
16
+ device: string;
17
+ inode: string;
18
+ } | {
19
+ kind: "windows";
20
+ volume_serial: string;
21
+ file_id_128: string;
22
+ };
23
+ export interface RequestCapabilities {
24
+ noXdev?: boolean;
25
+ adversary?: AdversaryLevel;
26
+ }
27
+ export interface RootOptions {
28
+ capabilities?: RequestCapabilities;
29
+ expectedIdentity?: ObjectId;
30
+ }
31
+ export interface ReadOptions {
32
+ maxBytes?: number;
33
+ maxSymlinkHops?: number;
34
+ capabilities?: RequestCapabilities;
35
+ }
36
+ export interface CreateOptions {
37
+ maxSymlinkHops?: number;
38
+ capabilities?: RequestCapabilities;
39
+ }
40
+ export interface ReadDirOptions {
41
+ maxEntries?: number;
42
+ maxSymlinkHops?: number;
43
+ capabilities?: RequestCapabilities;
44
+ }
45
+ export interface StatOptions {
46
+ lifetimeGeneration?: number;
47
+ capabilities?: RequestCapabilities;
48
+ }
49
+ export interface SameObjectOptions {
50
+ leftGeneration?: number;
51
+ rightGeneration?: number;
52
+ capabilities?: RequestCapabilities;
53
+ }
54
+ export interface DirEntry {
55
+ name: string;
56
+ kind: FileKind;
57
+ }
58
+ export interface Stat {
59
+ kind: FileKind;
60
+ identity: ObjectId;
61
+ byteLength?: number;
62
+ }
63
+ export interface ReadResult {
64
+ bytes: Buffer;
65
+ byteLength: number;
66
+ handle: HeldFile;
67
+ }
68
+ /** Path-free structured error raised by `@3leaps/fsprims` operations. */
69
+ export declare class FsprimsError extends Error {
70
+ readonly category: ErrorCategory;
71
+ readonly reason?: PolicyReason;
72
+ readonly detail: string;
73
+ constructor(category: ErrorCategory, reason: PolicyReason | undefined, detail: string);
74
+ }
75
+ /** The stable identifiers of every v0 policy profile the contract defines. */
76
+ export declare function profileIds(): string[];
77
+ /**
78
+ * The identifiers of the profiles this release enforces (`containment/v0`).
79
+ * Operations under `no-links/v0` fail with `unsupported_capability`; a string
80
+ * that is not a profile identifier fails with `invalid_name`.
81
+ */
82
+ export declare function supportedProfileIds(): string[];
83
+ /** Validate and split a relative path or symlink target into physical components (`F1`/`F3`). */
84
+ export declare function admittedComponents(value: string, profile: ProfileId, linkTarget?: boolean): string[];
85
+ /** Validate an observed directory entry name against `observed-name.schema.json`. */
86
+ export declare function validateObservedEntryName(name: string): void;
87
+ /** An open regular-file handle returned by `Root.openReadWith` or `Root.createExclusive`. */
88
+ export declare class HeldFile {
89
+ private readonly handleId;
90
+ private readonly generation;
91
+ private readonly objId;
92
+ private closed;
93
+ /** @internal */
94
+ constructor(handleId: number, generation: number, identity: ObjectId);
95
+ /** @internal */
96
+ get _handleId(): number;
97
+ identity(): ObjectId;
98
+ lifetimeGeneration(): number;
99
+ stat(options?: StatOptions): Stat;
100
+ close(): void;
101
+ }
102
+ /** Compare raw object identities of two held file handles (`same_object`). */
103
+ export declare function sameObject(left: HeldFile, right: HeldFile, options?: SameObjectOptions): boolean;
104
+ /** An open directory handle anchoring rooted filesystem operations. */
105
+ export declare class Root {
106
+ private readonly handleId;
107
+ private readonly generation;
108
+ private readonly objId;
109
+ private closed;
110
+ private constructor();
111
+ /** Open `dirPath` as a held root directory (`open_root`). */
112
+ static open(dirPath: string, options?: RootOptions): Root;
113
+ identity(): ObjectId;
114
+ lifetimeGeneration(): number;
115
+ stat(options?: StatOptions): Stat;
116
+ /** Resolve `name` under `profile`, read at most `maxBytes` bytes, and close the leaf handle. */
117
+ openRead(profile: ProfileId, name: string, options?: ReadOptions): Buffer;
118
+ /** Resolve `name` under `profile` and return both the read bytes and open `HeldFile`. */
119
+ openReadWith(profile: ProfileId, name: string, options?: ReadOptions): ReadResult;
120
+ /** Exclusively create a new regular file relative to this root (`create_exclusive`). */
121
+ createExclusive(profile: ProfileId, name: string, options?: CreateOptions): HeldFile;
122
+ /** Enumerate immediate directory entries relative to this root (`read_dir`). */
123
+ readDir(profile: ProfileId, name: string, options?: ReadDirOptions): DirEntry[];
124
+ close(): void;
125
+ }
126
+ //# sourceMappingURL=index.d.ts.map
package/dist/index.js ADDED
@@ -0,0 +1,263 @@
1
+ "use strict";
2
+ // SPDX-License-Identifier: MIT OR Apache-2.0
3
+ //
4
+ // @3leaps/fsprims: typed filesystem operations relative to a held root
5
+ // implementing the filesystem-access/v0 contract. This release enforces
6
+ // containment/v0 only; requests under no-links/v0 are refused with
7
+ // unsupported_capability (see `supportedProfileIds()`).
8
+ var __importDefault = (this && this.__importDefault) || function (mod) {
9
+ return (mod && mod.__esModule) ? mod : { "default": mod };
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.Root = exports.HeldFile = exports.FsprimsError = void 0;
13
+ exports.profileIds = profileIds;
14
+ exports.supportedProfileIds = supportedProfileIds;
15
+ exports.admittedComponents = admittedComponents;
16
+ exports.validateObservedEntryName = validateObservedEntryName;
17
+ exports.sameObject = sameObject;
18
+ const node_path_1 = __importDefault(require("node:path"));
19
+ const native_1 = require("./native");
20
+ let cached;
21
+ function native() {
22
+ cached ??= (0, native_1.loadNativeBinding)(node_path_1.default.resolve(__dirname, ".."));
23
+ return cached;
24
+ }
25
+ /** Path-free structured error raised by `@3leaps/fsprims` operations. */
26
+ class FsprimsError extends Error {
27
+ category;
28
+ reason;
29
+ detail;
30
+ constructor(category, reason, detail) {
31
+ const label = reason ? `${category} (${reason})` : category;
32
+ super(detail ? `${label}: ${detail}` : label);
33
+ this.name = "FsprimsError";
34
+ this.category = category;
35
+ this.reason = reason;
36
+ this.detail = detail;
37
+ }
38
+ }
39
+ exports.FsprimsError = FsprimsError;
40
+ function throwFromNative(err) {
41
+ if (!err) {
42
+ throw new FsprimsError("io_error", undefined, "unexpected missing native error");
43
+ }
44
+ throw new FsprimsError(err.category, err.reason, err.detail);
45
+ }
46
+ function toObjectId(dto) {
47
+ if (dto.kind === "unix") {
48
+ return {
49
+ kind: "unix",
50
+ device: dto.device ?? "0",
51
+ inode: dto.inode ?? "0",
52
+ };
53
+ }
54
+ return {
55
+ kind: "windows",
56
+ volume_serial: dto.volumeSerial ?? "00000000",
57
+ file_id_128: dto.fileId128 ?? "00000000000000000000000000000000",
58
+ };
59
+ }
60
+ function fromObjectId(id) {
61
+ if (id.kind === "unix") {
62
+ return { kind: "unix", device: id.device, inode: id.inode };
63
+ }
64
+ return {
65
+ kind: "windows",
66
+ volumeSerial: id.volume_serial,
67
+ fileId128: id.file_id_128,
68
+ };
69
+ }
70
+ function toNativeCaps(caps) {
71
+ if (!caps)
72
+ return undefined;
73
+ return {
74
+ noXdev: caps.noXdev,
75
+ adversary: caps.adversary,
76
+ };
77
+ }
78
+ /** The stable identifiers of every v0 policy profile the contract defines. */
79
+ function profileIds() {
80
+ return native().profileIds();
81
+ }
82
+ /**
83
+ * The identifiers of the profiles this release enforces (`containment/v0`).
84
+ * Operations under `no-links/v0` fail with `unsupported_capability`; a string
85
+ * that is not a profile identifier fails with `invalid_name`.
86
+ */
87
+ function supportedProfileIds() {
88
+ return native().supportedProfileIds();
89
+ }
90
+ /** Validate and split a relative path or symlink target into physical components (`F1`/`F3`). */
91
+ function admittedComponents(value, profile, linkTarget = false) {
92
+ const res = native().admittedComponentsNative(value, profile, linkTarget);
93
+ if (!res.ok || !res.components) {
94
+ throwFromNative(res.error);
95
+ }
96
+ return res.components;
97
+ }
98
+ /** Validate an observed directory entry name against `observed-name.schema.json`. */
99
+ function validateObservedEntryName(name) {
100
+ const res = native().validateObservedEntryNameNative(name);
101
+ if (!res.ok) {
102
+ throwFromNative(res.error);
103
+ }
104
+ }
105
+ /** An open regular-file handle returned by `Root.openReadWith` or `Root.createExclusive`. */
106
+ class HeldFile {
107
+ handleId;
108
+ generation;
109
+ objId;
110
+ closed = false;
111
+ /** @internal */
112
+ constructor(handleId, generation, identity) {
113
+ this.handleId = handleId;
114
+ this.generation = generation;
115
+ this.objId = identity;
116
+ }
117
+ /** @internal */
118
+ get _handleId() {
119
+ return this.handleId;
120
+ }
121
+ identity() {
122
+ return this.objId;
123
+ }
124
+ lifetimeGeneration() {
125
+ return this.generation;
126
+ }
127
+ stat(options) {
128
+ const gen = options?.lifetimeGeneration ?? this.generation;
129
+ const res = native().statFileNative(this.handleId, gen, toNativeCaps(options?.capabilities));
130
+ if (!res.ok || !res.kind || !res.identity) {
131
+ throwFromNative(res.error);
132
+ }
133
+ return {
134
+ kind: res.kind,
135
+ identity: toObjectId(res.identity),
136
+ byteLength: res.size,
137
+ };
138
+ }
139
+ close() {
140
+ if (!this.closed) {
141
+ this.closed = true;
142
+ native().closeFileNative(this.handleId);
143
+ }
144
+ }
145
+ }
146
+ exports.HeldFile = HeldFile;
147
+ /** Compare raw object identities of two held file handles (`same_object`). */
148
+ function sameObject(left, right, options) {
149
+ const leftGen = options?.leftGeneration ?? left.lifetimeGeneration();
150
+ const rightGen = options?.rightGeneration ?? right.lifetimeGeneration();
151
+ const res = native().sameObjectNative(left._handleId, leftGen, right._handleId, rightGen, toNativeCaps(options?.capabilities));
152
+ if (!res.ok || res.sameObject === undefined) {
153
+ throwFromNative(res.error);
154
+ }
155
+ return res.sameObject;
156
+ }
157
+ /** An open directory handle anchoring rooted filesystem operations. */
158
+ class Root {
159
+ handleId;
160
+ generation;
161
+ objId;
162
+ closed = false;
163
+ constructor(handleId, generation, identity) {
164
+ this.handleId = handleId;
165
+ this.generation = generation;
166
+ this.objId = identity;
167
+ }
168
+ /** Open `dirPath` as a held root directory (`open_root`). */
169
+ static open(dirPath, options) {
170
+ const res = native().openRootNative(dirPath, {
171
+ capabilities: toNativeCaps(options?.capabilities),
172
+ expectedIdentity: options?.expectedIdentity
173
+ ? fromObjectId(options.expectedIdentity)
174
+ : undefined,
175
+ });
176
+ if (!res.ok || res.handleId === undefined || res.lifetimeGeneration === undefined || !res.identity) {
177
+ throwFromNative(res.error);
178
+ }
179
+ return new Root(res.handleId, res.lifetimeGeneration, toObjectId(res.identity));
180
+ }
181
+ identity() {
182
+ return this.objId;
183
+ }
184
+ lifetimeGeneration() {
185
+ return this.generation;
186
+ }
187
+ stat(options) {
188
+ const gen = options?.lifetimeGeneration ?? this.generation;
189
+ const res = native().statRootNative(this.handleId, gen, toNativeCaps(options?.capabilities));
190
+ if (!res.ok || !res.kind || !res.identity) {
191
+ throwFromNative(res.error);
192
+ }
193
+ return {
194
+ kind: res.kind,
195
+ identity: toObjectId(res.identity),
196
+ byteLength: res.size,
197
+ };
198
+ }
199
+ /** Resolve `name` under `profile`, read at most `maxBytes` bytes, and close the leaf handle. */
200
+ openRead(profile, name, options) {
201
+ const res = this.openReadWith(profile, name, options);
202
+ res.handle.close();
203
+ return res.bytes;
204
+ }
205
+ /** Resolve `name` under `profile` and return both the read bytes and open `HeldFile`. */
206
+ openReadWith(profile, name, options) {
207
+ const res = native().openReadNative(this.handleId, profile, name, {
208
+ maxBytes: options?.maxBytes,
209
+ maxSymlinkHops: options?.maxSymlinkHops,
210
+ capabilities: toNativeCaps(options?.capabilities),
211
+ });
212
+ if (!res.ok ||
213
+ !res.data ||
214
+ res.byteLength === undefined ||
215
+ res.handleId === undefined ||
216
+ res.lifetimeGeneration === undefined ||
217
+ !res.identity) {
218
+ throwFromNative(res.error);
219
+ }
220
+ return {
221
+ bytes: res.data,
222
+ byteLength: res.byteLength,
223
+ handle: new HeldFile(res.handleId, res.lifetimeGeneration, toObjectId(res.identity)),
224
+ };
225
+ }
226
+ /** Exclusively create a new regular file relative to this root (`create_exclusive`). */
227
+ createExclusive(profile, name, options) {
228
+ const res = native().createExclusiveNative(this.handleId, profile, name, {
229
+ maxSymlinkHops: options?.maxSymlinkHops,
230
+ capabilities: toNativeCaps(options?.capabilities),
231
+ });
232
+ if (!res.ok ||
233
+ res.handleId === undefined ||
234
+ res.lifetimeGeneration === undefined ||
235
+ !res.identity) {
236
+ throwFromNative(res.error);
237
+ }
238
+ return new HeldFile(res.handleId, res.lifetimeGeneration, toObjectId(res.identity));
239
+ }
240
+ /** Enumerate immediate directory entries relative to this root (`read_dir`). */
241
+ readDir(profile, name, options) {
242
+ const res = native().readDirNative(this.handleId, profile, name, {
243
+ maxEntries: options?.maxEntries,
244
+ maxSymlinkHops: options?.maxSymlinkHops,
245
+ capabilities: toNativeCaps(options?.capabilities),
246
+ });
247
+ if (!res.ok || !res.entries) {
248
+ throwFromNative(res.error);
249
+ }
250
+ return res.entries.map((e) => ({
251
+ name: e.name,
252
+ kind: e.kind,
253
+ }));
254
+ }
255
+ close() {
256
+ if (!this.closed) {
257
+ this.closed = true;
258
+ native().closeRootNative(this.handleId);
259
+ }
260
+ }
261
+ }
262
+ exports.Root = Root;
263
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,105 @@
1
+ export interface NativeErrorDto {
2
+ category: string;
3
+ reason?: string;
4
+ detail: string;
5
+ }
6
+ export interface NativeObjectIdDto {
7
+ kind: string;
8
+ device?: string;
9
+ inode?: string;
10
+ volumeSerial?: string;
11
+ fileId128?: string;
12
+ }
13
+ export interface NativeCapabilitiesDto {
14
+ noXdev?: boolean;
15
+ adversary?: string;
16
+ }
17
+ export interface NativeRootOptionsDto {
18
+ capabilities?: NativeCapabilitiesDto;
19
+ expectedIdentity?: NativeObjectIdDto;
20
+ }
21
+ export interface NativeReadOptionsDto {
22
+ maxBytes?: number;
23
+ maxSymlinkHops?: number;
24
+ capabilities?: NativeCapabilitiesDto;
25
+ }
26
+ export interface NativeCreateOptionsDto {
27
+ maxSymlinkHops?: number;
28
+ capabilities?: NativeCapabilitiesDto;
29
+ }
30
+ export interface NativeReadDirOptionsDto {
31
+ maxEntries?: number;
32
+ maxSymlinkHops?: number;
33
+ capabilities?: NativeCapabilitiesDto;
34
+ }
35
+ export interface NativeDirEntryDto {
36
+ name: string;
37
+ kind: string;
38
+ }
39
+ export interface AdmittedComponentsOutcomeDto {
40
+ ok: boolean;
41
+ components?: string[];
42
+ error?: NativeErrorDto;
43
+ }
44
+ export interface SimpleOutcomeDto {
45
+ ok: boolean;
46
+ error?: NativeErrorDto;
47
+ }
48
+ export interface OpenRootOutcomeDto {
49
+ ok: boolean;
50
+ handleId?: number;
51
+ lifetimeGeneration?: number;
52
+ identity?: NativeObjectIdDto;
53
+ error?: NativeErrorDto;
54
+ }
55
+ export interface OpenReadOutcomeDto {
56
+ ok: boolean;
57
+ data?: Buffer;
58
+ byteLength?: number;
59
+ handleId?: number;
60
+ lifetimeGeneration?: number;
61
+ identity?: NativeObjectIdDto;
62
+ error?: NativeErrorDto;
63
+ }
64
+ export interface CreateExclusiveOutcomeDto {
65
+ ok: boolean;
66
+ handleId?: number;
67
+ lifetimeGeneration?: number;
68
+ identity?: NativeObjectIdDto;
69
+ error?: NativeErrorDto;
70
+ }
71
+ export interface ReadDirOutcomeDto {
72
+ ok: boolean;
73
+ entries?: NativeDirEntryDto[];
74
+ error?: NativeErrorDto;
75
+ }
76
+ export interface StatOutcomeDto {
77
+ ok: boolean;
78
+ kind?: string;
79
+ identity?: NativeObjectIdDto;
80
+ size?: number;
81
+ error?: NativeErrorDto;
82
+ }
83
+ export interface SameObjectOutcomeDto {
84
+ ok: boolean;
85
+ sameObject?: boolean;
86
+ error?: NativeErrorDto;
87
+ }
88
+ /** The functions the Node-API addon exports. */
89
+ export interface NativeBinding {
90
+ profileIds(): string[];
91
+ supportedProfileIds(): string[];
92
+ admittedComponentsNative(value: string, profile: string, linkTarget: boolean): AdmittedComponentsOutcomeDto;
93
+ validateObservedEntryNameNative(name: string): SimpleOutcomeDto;
94
+ openRootNative(path: string, opts?: NativeRootOptionsDto): OpenRootOutcomeDto;
95
+ closeRootNative(handleId: number): boolean;
96
+ statRootNative(handleId: number, generation: number, caps?: NativeCapabilitiesDto): StatOutcomeDto;
97
+ openReadNative(rootId: number, profile: string, name: string, opts?: NativeReadOptionsDto): OpenReadOutcomeDto;
98
+ createExclusiveNative(rootId: number, profile: string, name: string, opts?: NativeCreateOptionsDto): CreateExclusiveOutcomeDto;
99
+ readDirNative(rootId: number, profile: string, name: string, opts?: NativeReadDirOptionsDto): ReadDirOutcomeDto;
100
+ statFileNative(fileId: number, generation: number, caps?: NativeCapabilitiesDto): StatOutcomeDto;
101
+ sameObjectNative(leftId: number, leftGen: number, rightId: number, rightGen: number, caps?: NativeCapabilitiesDto): SameObjectOutcomeDto;
102
+ closeFileNative(fileId: number): boolean;
103
+ }
104
+ export declare function loadNativeBinding(packageRoot: string): NativeBinding;
105
+ //# sourceMappingURL=native.d.ts.map
package/dist/native.js ADDED
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+ // SPDX-License-Identifier: MIT OR Apache-2.0
3
+ var __importDefault = (this && this.__importDefault) || function (mod) {
4
+ return (mod && mod.__esModule) ? mod : { "default": mod };
5
+ };
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.loadNativeBinding = loadNativeBinding;
8
+ const node_fs_1 = __importDefault(require("node:fs"));
9
+ const node_path_1 = __importDefault(require("node:path"));
10
+ function isMusl() {
11
+ // Best-effort detection for Node on Linux. Without `process.report`,
12
+ // default to musl (safer for container environments).
13
+ const report = process.report;
14
+ if (!report || typeof report.getReport !== "function")
15
+ return true;
16
+ try {
17
+ const r = report.getReport();
18
+ return !r?.header?.glibcVersionRuntime;
19
+ }
20
+ catch {
21
+ return true;
22
+ }
23
+ }
24
+ function bindingIdForRuntime() {
25
+ const { platform, arch } = process;
26
+ if (platform === "darwin" && arch === "arm64")
27
+ return "darwin-arm64";
28
+ if (platform === "win32" && arch === "x64")
29
+ return "win32-x64-msvc";
30
+ if (platform === "win32" && arch === "arm64")
31
+ return "win32-arm64-msvc";
32
+ if (platform === "linux" && (arch === "x64" || arch === "arm64")) {
33
+ return `linux-${arch}-${isMusl() ? "musl" : "gnu"}`;
34
+ }
35
+ throw new Error(`Unsupported platform for fsprims: ${platform}/${arch}`);
36
+ }
37
+ function loadNativeBinding(packageRoot) {
38
+ const bindingId = bindingIdForRuntime();
39
+ const localNode = node_path_1.default.join(packageRoot, "dist", "native", `fsprims.${bindingId}.node`);
40
+ const pkg = `@3leaps/fsprims-${bindingId}`;
41
+ // Prefer a local build (git checkout or local path install).
42
+ if (node_fs_1.default.existsSync(localNode)) {
43
+ return require(localNode);
44
+ }
45
+ // Otherwise the platform package (npm optional dependency).
46
+ try {
47
+ return require(pkg);
48
+ }
49
+ catch (e) {
50
+ const msg = e instanceof Error ? e.message : String(e);
51
+ throw new Error("Failed to load the fsprims native addon for this platform.\n" +
52
+ `Expected: ${pkg}\n` +
53
+ "From a git checkout, build the addon first: npm run build:native\n" +
54
+ `Original error: ${msg}`);
55
+ }
56
+ }
57
+ //# sourceMappingURL=native.js.map
package/package.json CHANGED
@@ -1,6 +1,55 @@
1
1
  {
2
2
  "name": "@3leaps/fsprims",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "Typed filesystem operations relative to a held root with explicit link and containment policy (TypeScript bindings via Node-API native addon)",
5
+ "license": "MIT OR Apache-2.0",
6
+ "type": "commonjs",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/3leaps/fsprims.git",
10
+ "directory": "bindings/typescript/fsprims"
11
+ },
12
+ "main": "dist/index.js",
13
+ "types": "dist/index.d.ts",
14
+ "files": [
15
+ "dist/*.js",
16
+ "dist/*.d.ts",
17
+ "README.md",
18
+ "LICENSE-MIT",
19
+ "LICENSE-APACHE",
20
+ "NOTICE"
21
+ ],
22
+ "publishConfig": {
23
+ "access": "public",
24
+ "registry": "https://registry.npmjs.org/"
25
+ },
26
+ "engines": {
27
+ "node": ">=22"
28
+ },
29
+ "napi": {
30
+ "binaryName": "fsprims",
31
+ "targets": [
32
+ "x86_64-unknown-linux-gnu",
33
+ "aarch64-unknown-linux-gnu",
34
+ "aarch64-apple-darwin",
35
+ "x86_64-pc-windows-msvc"
36
+ ]
37
+ },
38
+ "scripts": {
39
+ "build:native": "napi build --manifest-path native/Cargo.toml --release --platform --strip --output-dir dist/native",
40
+ "build": "tsc -p tsconfig.json",
41
+ "test": "npm run build && npm run build:native && node test/conformance.test.cjs"
42
+ },
43
+ "dependencies": {},
44
+ "devDependencies": {
45
+ "@napi-rs/cli": "3.11.1",
46
+ "@types/node": "^22.20.7",
47
+ "typescript": "^7.0.2"
48
+ },
49
+ "optionalDependencies": {
50
+ "@3leaps/fsprims-darwin-arm64": "0.1.0",
51
+ "@3leaps/fsprims-linux-arm64-gnu": "0.1.0",
52
+ "@3leaps/fsprims-linux-x64-gnu": "0.1.0",
53
+ "@3leaps/fsprims-win32-x64-msvc": "0.1.0"
54
+ }
55
+ }