@cortexkit/pi-magic-context 0.45.0 → 0.46.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.
@@ -0,0 +1,150 @@
1
+ // ../plugin/src/shared/storage-permissions.ts
2
+ import { randomBytes } from "node:crypto";
3
+ import {
4
+ chmodSync,
5
+ closeSync,
6
+ createWriteStream,
7
+ lstatSync,
8
+ mkdirSync,
9
+ openSync,
10
+ readdirSync,
11
+ readlinkSync,
12
+ readSync,
13
+ renameSync,
14
+ rmSync,
15
+ symlinkSync,
16
+ writeFileSync,
17
+ writeSync
18
+ } from "node:fs";
19
+ import { open as openAsync, writeFile as writeFileAsync } from "node:fs/promises";
20
+ import { dirname, join } from "node:path";
21
+ var enforcePrivateStoragePermissions = true;
22
+ function setStoragePrivatePermissionEnforcement(enforce) {
23
+ enforcePrivateStoragePermissions = enforce;
24
+ }
25
+ function shouldEnforcePrivateStoragePermissions() {
26
+ return enforcePrivateStoragePermissions;
27
+ }
28
+ function ensureStorageDirectorySync(directory, forcePrivate = false, recursive = true) {
29
+ const privateMode = forcePrivate || shouldEnforcePrivateStoragePermissions();
30
+ mkdirSync(directory, {
31
+ recursive,
32
+ ...privateMode ? { mode: 448 } : {}
33
+ });
34
+ }
35
+ function writeStorageFileSync(filePath, content, options = {}) {
36
+ const { forcePrivate = false, ...writeOptions } = options;
37
+ const privateMode = forcePrivate || shouldEnforcePrivateStoragePermissions();
38
+ writeFileSync(filePath, content, {
39
+ ...writeOptions,
40
+ ...privateMode ? { mode: 384 } : {}
41
+ });
42
+ }
43
+ function writeStorageFileAtomicSync(filePath, content, forcePrivate = false) {
44
+ ensureStorageDirectorySync(dirname(filePath), forcePrivate);
45
+ const tempPath = `${filePath}.${process.pid}.${randomBytes(6).toString("hex")}.tmp`;
46
+ try {
47
+ writeStorageFileSync(tempPath, content, { flag: "wx", forcePrivate });
48
+ renameSync(tempPath, filePath);
49
+ } catch (error) {
50
+ rmSync(tempPath, { force: true });
51
+ throw error;
52
+ }
53
+ }
54
+ function copyStorageFileSync(sourcePath, destinationPath, forcePrivate = false) {
55
+ ensureStorageDirectorySync(dirname(destinationPath), forcePrivate);
56
+ const privateMode = forcePrivate || shouldEnforcePrivateStoragePermissions();
57
+ const sourceFd = openSync(sourcePath, "r");
58
+ let destinationFd;
59
+ try {
60
+ destinationFd = openSync(destinationPath, "wx", privateMode ? 384 : 438);
61
+ const buffer = Buffer.allocUnsafe(1024 * 1024);
62
+ for (;; ) {
63
+ const bytesRead = readSync(sourceFd, buffer, 0, buffer.length, null);
64
+ if (bytesRead === 0)
65
+ break;
66
+ let offset = 0;
67
+ while (offset < bytesRead) {
68
+ offset += writeSync(destinationFd, buffer, offset, bytesRead - offset);
69
+ }
70
+ }
71
+ } catch (error) {
72
+ if (destinationFd !== undefined)
73
+ rmSync(destinationPath, { force: true });
74
+ throw error;
75
+ } finally {
76
+ closeSync(sourceFd);
77
+ if (destinationFd !== undefined)
78
+ closeSync(destinationFd);
79
+ }
80
+ }
81
+ function copyStorageTreeSync(sourcePath, destinationPath, forcePrivate = false) {
82
+ const stat = lstatSync(sourcePath);
83
+ if (stat.isSymbolicLink()) {
84
+ ensureStorageDirectorySync(dirname(destinationPath), forcePrivate);
85
+ symlinkSync(readlinkSync(sourcePath), destinationPath);
86
+ return;
87
+ }
88
+ if (!stat.isDirectory()) {
89
+ copyStorageFileSync(sourcePath, destinationPath, forcePrivate);
90
+ return;
91
+ }
92
+ ensureStorageDirectorySync(destinationPath, forcePrivate);
93
+ for (const entry of readdirSync(sourcePath)) {
94
+ copyStorageTreeSync(join(sourcePath, entry), join(destinationPath, entry), forcePrivate);
95
+ }
96
+ }
97
+ async function writeStorageFileAsync(filePath, content, forcePrivate = false) {
98
+ const privateMode = forcePrivate || shouldEnforcePrivateStoragePermissions();
99
+ ensureStorageDirectorySync(dirname(filePath), privateMode);
100
+ await writeFileAsync(filePath, content, privateMode ? { mode: 384 } : undefined);
101
+ }
102
+ async function writeStorageFileHandleAsync(handle, content) {
103
+ await handle.writeFile(content);
104
+ }
105
+ async function createStorageFileHandleAsync(filePath, flags, forcePrivate = false) {
106
+ const privateMode = forcePrivate || shouldEnforcePrivateStoragePermissions();
107
+ return openAsync(filePath, flags, privateMode ? 384 : undefined);
108
+ }
109
+ function tightenStorageTreeSync(root) {
110
+ if (!shouldEnforcePrivateStoragePermissions() || process.platform === "win32") {
111
+ return { tightened: 0, failures: 0 };
112
+ }
113
+ let tightened = 0;
114
+ let failures = 0;
115
+ const visit = (entryPath) => {
116
+ let stat;
117
+ try {
118
+ stat = lstatSync(entryPath);
119
+ } catch {
120
+ failures++;
121
+ return;
122
+ }
123
+ if (stat.isSymbolicLink())
124
+ return;
125
+ const mode = stat.isDirectory() ? 448 : 384;
126
+ if ((stat.mode & 63) !== 0 || (stat.mode & 511) !== mode) {
127
+ try {
128
+ chmodSync(entryPath, mode);
129
+ tightened++;
130
+ } catch {
131
+ failures++;
132
+ }
133
+ }
134
+ if (!stat.isDirectory())
135
+ return;
136
+ let entries;
137
+ try {
138
+ entries = readdirSync(entryPath);
139
+ } catch {
140
+ failures++;
141
+ return;
142
+ }
143
+ for (const entry of entries)
144
+ visit(join(entryPath, entry));
145
+ };
146
+ visit(root);
147
+ return { tightened, failures };
148
+ }
149
+
150
+ export { setStoragePrivatePermissionEnforcement, shouldEnforcePrivateStoragePermissions, ensureStorageDirectorySync, writeStorageFileSync, writeStorageFileAtomicSync, copyStorageFileSync, copyStorageTreeSync, writeStorageFileAsync, writeStorageFileHandleAsync, createStorageFileHandleAsync, tightenStorageTreeSync };