@mmon-co/ncept-local 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/dist/tar.js ADDED
@@ -0,0 +1,99 @@
1
+ /** A deliberately small, portable ustar codec: regular files only, no links or filesystem access. */
2
+ const encoder = new TextEncoder();
3
+ const decoder = new TextDecoder("utf-8", { fatal: true });
4
+ const block = 512;
5
+ function field(header, start, length) {
6
+ const bytes = header.subarray(start, start + length);
7
+ const end = bytes.indexOf(0);
8
+ return decoder.decode(end === -1 ? bytes : bytes.subarray(0, end));
9
+ }
10
+ function number(header, start, length) {
11
+ const value = field(header, start, length).trim();
12
+ if (!/^[0-7]+$/.test(value))
13
+ throw new Error("Invalid tar number");
14
+ const result = Number.parseInt(value, 8);
15
+ if (!Number.isSafeInteger(result))
16
+ throw new Error("Tar number exceeds safe size");
17
+ return result;
18
+ }
19
+ function checksum(header) {
20
+ return header.reduce((sum, byte, index) => sum + (index >= 148 && index < 156 ? 32 : byte), 0);
21
+ }
22
+ function safePath(path) {
23
+ if (!path ||
24
+ path.startsWith("/") ||
25
+ path.includes("\\") ||
26
+ path.includes("\0") ||
27
+ path.split("/").some((part) => !part || part === "." || part === ".."))
28
+ throw new Error("Unsafe tar path: " + path);
29
+ }
30
+ function put(header, start, length, value) {
31
+ const bytes = encoder.encode(value);
32
+ if (bytes.length > length)
33
+ throw new Error("Tar field too long: " + value);
34
+ header.set(bytes, start);
35
+ }
36
+ function octal(value, width) {
37
+ return value.toString(8).padStart(width - 1, "0") + "\0";
38
+ }
39
+ export function encodeTar(files) {
40
+ let size = block * 2;
41
+ for (const bytes of files.values())
42
+ size += block + Math.ceil(bytes.length / block) * block;
43
+ const output = new Uint8Array(size);
44
+ let offset = 0;
45
+ for (const [path, bytes] of [...files].sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))) {
46
+ safePath(path);
47
+ const header = output.subarray(offset, offset + block);
48
+ put(header, 0, 100, path);
49
+ put(header, 100, 8, octal(0o644, 8));
50
+ put(header, 108, 8, octal(0, 8));
51
+ put(header, 116, 8, octal(0, 8));
52
+ put(header, 124, 12, octal(bytes.length, 12));
53
+ put(header, 136, 12, octal(0, 12));
54
+ header[156] = 48;
55
+ put(header, 257, 6, "ustar\0");
56
+ put(header, 263, 2, "00");
57
+ put(header, 148, 8, checksum(header).toString(8).padStart(6, "0") + "\0 ");
58
+ output.set(bytes, offset + block);
59
+ offset += block + Math.ceil(bytes.length / block) * block;
60
+ }
61
+ return output;
62
+ }
63
+ export function decodeTar(bytes) {
64
+ if (bytes.length % block !== 0)
65
+ throw new Error("Truncated tar archive");
66
+ const files = new Map();
67
+ let offset = 0;
68
+ while (offset + block <= bytes.length) {
69
+ const header = bytes.subarray(offset, offset + block);
70
+ if (header.every((byte) => byte === 0)) {
71
+ if (offset + block * 2 > bytes.length || bytes.subarray(offset).some((byte) => byte !== 0))
72
+ throw new Error("Invalid tar end marker");
73
+ return files;
74
+ }
75
+ if (number(header, 148, 8) !== checksum(header))
76
+ throw new Error("Tar checksum mismatch");
77
+ const prefix = field(header, 345, 155);
78
+ const name = field(header, 0, 100);
79
+ const path = prefix ? prefix + "/" + name : name;
80
+ const size = number(header, 124, 12);
81
+ const type = header[156];
82
+ const end = offset + block + Math.ceil(size / block) * block;
83
+ if (end > bytes.length)
84
+ throw new Error("Truncated tar file: " + path);
85
+ // Directory records from standard tar writers are harmless, but never extract links or extensions.
86
+ if (type === 53 && size === 0)
87
+ safePath(path.replace(/\/$/, ""));
88
+ else {
89
+ if (type !== 0 && type !== 48)
90
+ throw new Error("Unsupported tar entry type: " + path);
91
+ safePath(path);
92
+ if (files.has(path))
93
+ throw new Error("Duplicate tar path: " + path);
94
+ files.set(path, bytes.slice(offset + block, offset + block + size));
95
+ }
96
+ offset = end;
97
+ }
98
+ throw new Error("Missing tar end marker");
99
+ }
package/package.json CHANGED
@@ -1,6 +1,46 @@
1
1
  {
2
2
  "name": "@mmon-co/ncept-local",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "license": "MIT",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/mmon-co/ncept.git",
8
+ "directory": "packages/local"
9
+ },
10
+ "files": [
11
+ "dist",
12
+ "README.md",
13
+ "LICENSE"
14
+ ],
15
+ "type": "module",
16
+ "exports": {
17
+ ".": {
18
+ "types": "./dist/index.d.ts",
19
+ "default": "./dist/index.js"
20
+ },
21
+ "./exchange": {
22
+ "types": "./dist/exchange.d.ts",
23
+ "default": "./dist/exchange.js"
24
+ },
25
+ "./sessions": {
26
+ "types": "./dist/sessions.d.ts",
27
+ "default": "./dist/sessions.js"
28
+ }
29
+ },
30
+ "scripts": {
31
+ "build": "bun run --cwd ../core build && tsc -p tsconfig.build.json",
32
+ "prepack": "bun run build"
33
+ },
34
+ "dependencies": {
35
+ "@mmon-co/ncept-core": "^0.1.0",
36
+ "@modelcontextprotocol/sdk": "1.32.1"
37
+ },
38
+ "devDependencies": {
39
+ "@mmon-co/ncept-default-theory": "^1.0.0",
40
+ "bun-types": "^1.3.14",
41
+ "typescript": "5.9.3"
42
+ },
43
+ "engines": {
44
+ "node": ">=20.19.0"
45
+ }
46
+ }