@archildata/just-bash 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/index.mjs ADDED
@@ -0,0 +1,522 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __esm = (fn, res) => function __init() {
6
+ return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
7
+ };
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
21
+
22
+ // src/ArchilFs.ts
23
+ var ArchilFs_exports = {};
24
+ __export(ArchilFs_exports, {
25
+ ArchilFs: () => ArchilFs
26
+ });
27
+ var ArchilFs;
28
+ var init_ArchilFs = __esm({
29
+ "src/ArchilFs.ts"() {
30
+ "use strict";
31
+ ArchilFs = class {
32
+ client;
33
+ inodeCache;
34
+ user;
35
+ rootInodeId = 1;
36
+ /**
37
+ * Create a new ArchilFs adapter
38
+ *
39
+ * @param client - Connected ArchilClient instance
40
+ * @param options - Optional configuration
41
+ * @param options.user - Unix user context for permission checks
42
+ */
43
+ constructor(client, options) {
44
+ this.client = client;
45
+ this.user = options?.user;
46
+ this.inodeCache = /* @__PURE__ */ new Map();
47
+ this.inodeCache.set("/", this.rootInodeId);
48
+ }
49
+ // ========================================================================
50
+ // Path Resolution
51
+ // ========================================================================
52
+ /**
53
+ * Normalize a path (remove . and .., ensure leading /)
54
+ */
55
+ normalizePath(path) {
56
+ if (!path || path === "") {
57
+ return "/";
58
+ }
59
+ if (!path.startsWith("/")) {
60
+ path = "/" + path;
61
+ }
62
+ const parts = path.split("/").filter((p) => p !== "" && p !== ".");
63
+ const result = [];
64
+ for (const part of parts) {
65
+ if (part === "..") {
66
+ result.pop();
67
+ } else {
68
+ result.push(part);
69
+ }
70
+ }
71
+ return "/" + result.join("/");
72
+ }
73
+ /**
74
+ * Resolve a path to its inode ID, walking the directory tree
75
+ */
76
+ async resolve(path) {
77
+ const normalizedPath = this.normalizePath(path);
78
+ const cachedInode = this.inodeCache.get(normalizedPath);
79
+ if (cachedInode !== void 0) {
80
+ const attributes2 = await this.client.getAttributes(cachedInode, this.user);
81
+ return { inodeId: cachedInode, attributes: attributes2 };
82
+ }
83
+ const parts = normalizedPath.split("/").filter((p) => p !== "");
84
+ let currentInodeId = this.rootInodeId;
85
+ let currentPath = "";
86
+ for (const part of parts) {
87
+ currentPath += "/" + part;
88
+ const cached = this.inodeCache.get(currentPath);
89
+ if (cached !== void 0) {
90
+ currentInodeId = cached;
91
+ continue;
92
+ }
93
+ const response = await this.client.lookupInode(currentInodeId, part, this.user);
94
+ if (response.inodeId === -1) {
95
+ throw new Error(`ENOENT: no such file or directory, '${path}'`);
96
+ }
97
+ currentInodeId = response.inodeId;
98
+ this.inodeCache.set(currentPath, currentInodeId);
99
+ }
100
+ const attributes = await this.client.getAttributes(currentInodeId, this.user);
101
+ return { inodeId: currentInodeId, attributes };
102
+ }
103
+ /**
104
+ * Resolve parent directory and get child name
105
+ */
106
+ async resolveParent(path) {
107
+ const normalizedPath = this.normalizePath(path);
108
+ const lastSlash = normalizedPath.lastIndexOf("/");
109
+ const parentPath = lastSlash === 0 ? "/" : normalizedPath.substring(0, lastSlash);
110
+ const name = normalizedPath.substring(lastSlash + 1);
111
+ const { inodeId: parentInodeId } = await this.resolve(parentPath);
112
+ return { parentInodeId, name };
113
+ }
114
+ /**
115
+ * Invalidate cache entries for a path and its descendants
116
+ */
117
+ invalidateCache(path) {
118
+ const normalizedPath = this.normalizePath(path);
119
+ for (const cachedPath of this.inodeCache.keys()) {
120
+ if (cachedPath === normalizedPath || cachedPath.startsWith(normalizedPath + "/")) {
121
+ this.inodeCache.delete(cachedPath);
122
+ }
123
+ }
124
+ }
125
+ /**
126
+ * Convert InodeAttributes to FsStat
127
+ */
128
+ toStat(attrs) {
129
+ const isFile = attrs.inodeType === "File";
130
+ const isDirectory = attrs.inodeType === "Directory";
131
+ const isSymlink = attrs.inodeType === "Symlink";
132
+ return {
133
+ isFile: () => isFile,
134
+ isDirectory: () => isDirectory,
135
+ isSymlink: () => isSymlink,
136
+ mode: attrs.mode,
137
+ size: Number(attrs.size),
138
+ mtimeMs: attrs.mtimeMs,
139
+ atimeMs: attrs.atimeMs,
140
+ ctimeMs: attrs.ctimeMs,
141
+ birthtimeMs: attrs.btimeMs
142
+ };
143
+ }
144
+ // ========================================================================
145
+ // IFileSystem Implementation - Read Operations
146
+ // ========================================================================
147
+ resolvePath(base, ...paths) {
148
+ let result = base;
149
+ for (const p of paths) {
150
+ if (p.startsWith("/")) {
151
+ result = p;
152
+ } else {
153
+ result = result.endsWith("/") ? result + p : result + "/" + p;
154
+ }
155
+ }
156
+ return this.normalizePath(result);
157
+ }
158
+ async readFile(path, encoding) {
159
+ const buffer = await this.readFileBuffer(path);
160
+ const decoder = new TextDecoder(encoding || "utf-8");
161
+ return decoder.decode(buffer);
162
+ }
163
+ async readFileBuffer(path) {
164
+ const { inodeId, attributes } = await this.resolve(path);
165
+ if (attributes.inodeType !== "File") {
166
+ throw new Error(`EISDIR: illegal operation on a directory, read '${path}'`);
167
+ }
168
+ const size = Number(attributes.size);
169
+ if (size === 0) {
170
+ return new Uint8Array(0);
171
+ }
172
+ const MAX_CHUNK = 4 * 1024 * 1024;
173
+ if (size <= MAX_CHUNK) {
174
+ const buffer = await this.client.readInode(inodeId, 0, size, this.user);
175
+ return new Uint8Array(buffer);
176
+ }
177
+ const result = new Uint8Array(size);
178
+ let offset = 0;
179
+ while (offset < size) {
180
+ const chunkSize = Math.min(MAX_CHUNK, size - offset);
181
+ const chunk = await this.client.readInode(inodeId, offset, chunkSize, this.user);
182
+ result.set(new Uint8Array(chunk), offset);
183
+ offset += chunkSize;
184
+ }
185
+ return result;
186
+ }
187
+ async readdir(path) {
188
+ const { inodeId, attributes } = await this.resolve(path);
189
+ if (attributes.inodeType !== "Directory") {
190
+ throw new Error(`ENOTDIR: not a directory, scandir '${path}'`);
191
+ }
192
+ const entries = await this.client.readDirectory(inodeId, void 0, void 0, this.user);
193
+ return entries.map((e) => e.name).filter((name) => name !== "." && name !== "..");
194
+ }
195
+ async readdirWithFileTypes(path) {
196
+ const { inodeId, attributes } = await this.resolve(path);
197
+ if (attributes.inodeType !== "Directory") {
198
+ throw new Error(`ENOTDIR: not a directory, scandir '${path}'`);
199
+ }
200
+ const entries = await this.client.readDirectory(inodeId, void 0, void 0, this.user);
201
+ return entries.filter((e) => e.name !== "." && e.name !== "..").map((e) => ({
202
+ name: e.name,
203
+ isFile: () => e.inodeType === "File",
204
+ isDirectory: () => e.inodeType === "Directory",
205
+ isSymlink: () => e.inodeType === "Symlink"
206
+ }));
207
+ }
208
+ async stat(path) {
209
+ const { attributes } = await this.resolve(path);
210
+ if (attributes.inodeType === "Symlink" && attributes.symlinkTarget) {
211
+ const targetPath = attributes.symlinkTarget.startsWith("/") ? attributes.symlinkTarget : this.resolvePath(path, "..", attributes.symlinkTarget);
212
+ return this.stat(targetPath);
213
+ }
214
+ return this.toStat(attributes);
215
+ }
216
+ async lstat(path) {
217
+ const { attributes } = await this.resolve(path);
218
+ return this.toStat(attributes);
219
+ }
220
+ async exists(path) {
221
+ try {
222
+ await this.resolve(path);
223
+ return true;
224
+ } catch {
225
+ return false;
226
+ }
227
+ }
228
+ async readlink(path) {
229
+ const { attributes } = await this.resolve(path);
230
+ if (attributes.inodeType !== "Symlink") {
231
+ throw new Error(`EINVAL: invalid argument, readlink '${path}'`);
232
+ }
233
+ return attributes.symlinkTarget || "";
234
+ }
235
+ async realpath(path) {
236
+ const normalizedPath = this.normalizePath(path);
237
+ const parts = normalizedPath.split("/").filter((p) => p !== "");
238
+ let resolvedPath = "/";
239
+ let currentInodeId = this.rootInodeId;
240
+ for (const part of parts) {
241
+ const response = await this.client.lookupInode(currentInodeId, part, this.user);
242
+ if (response.inodeId === -1) {
243
+ throw new Error(`ENOENT: no such file or directory, realpath '${path}'`);
244
+ }
245
+ const attrs = response.attributes;
246
+ if (attrs.inodeType === "Symlink" && attrs.symlinkTarget) {
247
+ const targetPath = attrs.symlinkTarget.startsWith("/") ? attrs.symlinkTarget : this.resolvePath(resolvedPath, attrs.symlinkTarget);
248
+ const resolved = await this.realpath(targetPath);
249
+ resolvedPath = resolved;
250
+ const { inodeId } = await this.resolve(resolved);
251
+ currentInodeId = inodeId;
252
+ } else {
253
+ resolvedPath = resolvedPath === "/" ? "/" + part : resolvedPath + "/" + part;
254
+ currentInodeId = response.inodeId;
255
+ }
256
+ }
257
+ return resolvedPath;
258
+ }
259
+ // ========================================================================
260
+ // IFileSystem Implementation - Write Operations
261
+ // ========================================================================
262
+ async writeFile(path, content) {
263
+ const data = typeof content === "string" ? new TextEncoder().encode(content) : content;
264
+ let inodeId;
265
+ let isNewFile = false;
266
+ try {
267
+ const resolved = await this.resolve(path);
268
+ inodeId = resolved.inodeId;
269
+ if (resolved.attributes.inodeType !== "File") {
270
+ throw new Error(`EISDIR: illegal operation on a directory, write '${path}'`);
271
+ }
272
+ await this.client.checkout(inodeId, false, this.user);
273
+ } catch (err) {
274
+ if (err instanceof Error && err.message.includes("EISDIR")) {
275
+ throw err;
276
+ }
277
+ const { parentInodeId, name } = await this.resolveParent(path);
278
+ const range = await this.client.reserveInodes(1);
279
+ inodeId = range.start;
280
+ const now = Date.now();
281
+ await this.client.conditionalCreate(
282
+ parentInodeId,
283
+ name,
284
+ inodeId,
285
+ {
286
+ inodeId: Number(inodeId),
287
+ inodeType: "File",
288
+ size: 0,
289
+ uid: this.user?.uid ?? 0,
290
+ gid: this.user?.gid ?? 0,
291
+ mode: 420,
292
+ nlink: 1,
293
+ ctimeMs: now,
294
+ atimeMs: now,
295
+ mtimeMs: now,
296
+ btimeMs: now,
297
+ rdev: void 0,
298
+ symlinkTarget: void 0
299
+ },
300
+ this.user
301
+ );
302
+ this.inodeCache.set(this.normalizePath(path), inodeId);
303
+ isNewFile = true;
304
+ }
305
+ try {
306
+ await this.client.writeData(inodeId, 0, Buffer.from(data), this.user);
307
+ } finally {
308
+ if (!isNewFile) {
309
+ try {
310
+ await this.client.checkin(inodeId, this.user);
311
+ } catch {
312
+ }
313
+ }
314
+ }
315
+ }
316
+ async appendFile(path, content) {
317
+ let existing;
318
+ try {
319
+ existing = await this.readFileBuffer(path);
320
+ } catch {
321
+ existing = new Uint8Array(0);
322
+ }
323
+ const data = typeof content === "string" ? new TextEncoder().encode(content) : content;
324
+ const combined = new Uint8Array(existing.length + data.length);
325
+ combined.set(existing, 0);
326
+ combined.set(data, existing.length);
327
+ await this.writeFile(path, combined);
328
+ }
329
+ async mkdir(path, options) {
330
+ const normalizedPath = this.normalizePath(path);
331
+ if (options?.recursive) {
332
+ const parts = normalizedPath.split("/").filter((p) => p !== "");
333
+ let currentPath = "";
334
+ for (const part of parts) {
335
+ currentPath += "/" + part;
336
+ const exists = await this.exists(currentPath);
337
+ if (exists) {
338
+ continue;
339
+ }
340
+ await this.mkdirSingle(currentPath);
341
+ }
342
+ } else {
343
+ await this.mkdirSingle(normalizedPath);
344
+ }
345
+ }
346
+ async mkdirSingle(path) {
347
+ const { parentInodeId, name } = await this.resolveParent(path);
348
+ const range = await this.client.reserveInodes(1);
349
+ const inodeId = range.start;
350
+ const now = Date.now();
351
+ await this.client.conditionalCreate(
352
+ parentInodeId,
353
+ name,
354
+ inodeId,
355
+ {
356
+ inodeId: Number(inodeId),
357
+ inodeType: "Directory",
358
+ size: 4096,
359
+ uid: this.user?.uid ?? 0,
360
+ gid: this.user?.gid ?? 0,
361
+ mode: 493,
362
+ nlink: 2,
363
+ ctimeMs: now,
364
+ atimeMs: now,
365
+ mtimeMs: now,
366
+ btimeMs: now,
367
+ rdev: void 0,
368
+ symlinkTarget: void 0
369
+ },
370
+ this.user
371
+ );
372
+ this.inodeCache.set(this.normalizePath(path), inodeId);
373
+ }
374
+ async rm(path, options) {
375
+ let resolved;
376
+ try {
377
+ resolved = await this.resolve(path);
378
+ } catch {
379
+ if (options?.force) {
380
+ return;
381
+ }
382
+ throw new Error(`ENOENT: no such file or directory, rm '${path}'`);
383
+ }
384
+ if (resolved.attributes.inodeType === "Directory") {
385
+ if (!options?.recursive) {
386
+ throw new Error(`EISDIR: illegal operation on a directory, rm '${path}'`);
387
+ }
388
+ const entries = await this.readdir(path);
389
+ for (const entry of entries) {
390
+ await this.rm(this.resolvePath(path, entry), options);
391
+ }
392
+ }
393
+ const { parentInodeId, name } = await this.resolveParent(path);
394
+ await this.client.unlink(parentInodeId, name, this.user);
395
+ this.invalidateCache(path);
396
+ }
397
+ async cp(src, dest, options) {
398
+ const srcResolved = await this.resolve(src);
399
+ if (srcResolved.attributes.inodeType === "Directory") {
400
+ if (!options?.recursive) {
401
+ throw new Error(`EISDIR: illegal operation on a directory, cp '${src}'`);
402
+ }
403
+ await this.mkdir(dest, { recursive: true });
404
+ const entries = await this.readdir(src);
405
+ for (const entry of entries) {
406
+ await this.cp(
407
+ this.resolvePath(src, entry),
408
+ this.resolvePath(dest, entry),
409
+ options
410
+ );
411
+ }
412
+ } else {
413
+ const content = await this.readFileBuffer(src);
414
+ await this.writeFile(dest, content);
415
+ }
416
+ }
417
+ async mv(src, dest) {
418
+ const srcResolved = await this.resolve(src);
419
+ if (srcResolved.attributes.inodeType === "Directory") {
420
+ await this.cp(src, dest, { recursive: true });
421
+ } else {
422
+ const content = await this.readFileBuffer(src);
423
+ await this.writeFile(dest, content);
424
+ }
425
+ await this.rm(src, { recursive: true, force: true });
426
+ }
427
+ async symlink(target, path) {
428
+ const { parentInodeId, name } = await this.resolveParent(path);
429
+ const range = await this.client.reserveInodes(1);
430
+ const inodeId = range.start;
431
+ const now = Date.now();
432
+ await this.client.conditionalCreate(
433
+ parentInodeId,
434
+ name,
435
+ inodeId,
436
+ {
437
+ inodeId: Number(inodeId),
438
+ inodeType: "Symlink",
439
+ size: target.length,
440
+ uid: this.user?.uid ?? 0,
441
+ gid: this.user?.gid ?? 0,
442
+ mode: 511,
443
+ nlink: 1,
444
+ ctimeMs: now,
445
+ atimeMs: now,
446
+ mtimeMs: now,
447
+ btimeMs: now,
448
+ rdev: void 0,
449
+ symlinkTarget: target
450
+ },
451
+ this.user
452
+ );
453
+ this.inodeCache.set(this.normalizePath(path), inodeId);
454
+ }
455
+ async link(existingPath, newPath) {
456
+ throw new Error(
457
+ "Hard link operations not yet implemented. The archil-node bindings need to expose link for hard links."
458
+ );
459
+ }
460
+ async chmod(path, mode) {
461
+ throw new Error(
462
+ "chmod not yet implemented. The archil-node bindings need to expose setattr for permission changes."
463
+ );
464
+ }
465
+ async utimes(path, atime, mtime) {
466
+ throw new Error(
467
+ "utimes not yet implemented. The archil-node bindings need to expose setattr for timestamp changes."
468
+ );
469
+ }
470
+ // ========================================================================
471
+ // Utility Methods
472
+ // ========================================================================
473
+ async getAllPaths() {
474
+ const paths = [];
475
+ const walk = async (dirPath) => {
476
+ paths.push(dirPath);
477
+ try {
478
+ const entries = await this.readdirWithFileTypes(dirPath);
479
+ for (const entry of entries) {
480
+ const fullPath = this.resolvePath(dirPath, entry.name);
481
+ if (entry.isDirectory()) {
482
+ await walk(fullPath);
483
+ } else {
484
+ paths.push(fullPath);
485
+ }
486
+ }
487
+ } catch {
488
+ }
489
+ };
490
+ await walk("/");
491
+ return paths;
492
+ }
493
+ /**
494
+ * Clear the inode cache
495
+ */
496
+ clearCache() {
497
+ this.inodeCache.clear();
498
+ this.inodeCache.set("/", this.rootInodeId);
499
+ }
500
+ /**
501
+ * Get cache statistics
502
+ */
503
+ getCacheStats() {
504
+ return {
505
+ size: this.inodeCache.size,
506
+ paths: Array.from(this.inodeCache.keys())
507
+ };
508
+ }
509
+ };
510
+ }
511
+ });
512
+
513
+ // src/index.ts
514
+ init_ArchilFs();
515
+ function createArchilFs(client, options) {
516
+ const { ArchilFs: ArchilFs2 } = (init_ArchilFs(), __toCommonJS(ArchilFs_exports));
517
+ return new ArchilFs2(client, options);
518
+ }
519
+ export {
520
+ ArchilFs,
521
+ createArchilFs
522
+ };
package/package.json ADDED
@@ -0,0 +1,55 @@
1
+ {
2
+ "name": "@archildata/just-bash",
3
+ "version": "0.1.0",
4
+ "description": "Archil filesystem adapter for just-bash",
5
+ "type": "module",
6
+ "main": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.mjs",
12
+ "require": "./dist/index.js"
13
+ }
14
+ },
15
+ "bin": {
16
+ "archil-shell": "./bin/shell.ts"
17
+ },
18
+ "files": [
19
+ "dist",
20
+ "bin"
21
+ ],
22
+ "scripts": {
23
+ "build": "tsup src/index.ts --format cjs,esm --dts",
24
+ "dev": "tsup src/index.ts --format cjs,esm --dts --watch",
25
+ "lint": "eslint src/",
26
+ "typecheck": "tsc --noEmit",
27
+ "shell": "tsx bin/shell.ts"
28
+ },
29
+ "dependencies": {
30
+ "@archildata/client": "^0.1.0",
31
+ "debug": "^4.3.4",
32
+ "just-bash": "^2.7.0"
33
+ },
34
+ "devDependencies": {
35
+ "@types/debug": "^4.1.12",
36
+ "@types/node": "^20.0.0",
37
+ "ts-node": "^10.9.2",
38
+ "tsup": "^8.0.0",
39
+ "tsx": "^4.21.0",
40
+ "typescript": "^5.9.3"
41
+ },
42
+ "keywords": [
43
+ "archil",
44
+ "just-bash",
45
+ "filesystem",
46
+ "bash",
47
+ "ai-agents"
48
+ ],
49
+ "license": "MIT",
50
+ "repository": {
51
+ "type": "git",
52
+ "url": "https://github.com/archil/archil-dataplane.git",
53
+ "directory": "packages/archil-just-bash"
54
+ }
55
+ }