pinokiod 7.2.9 → 7.2.10

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.
@@ -3,7 +3,139 @@ const portfinder = require('portfinder-cp')
3
3
  const os = require('os')
4
4
  const fs = require('fs')
5
5
  const Util = require('./util')
6
- const platform = os.platform()
6
+ const TEMP_ENV_KEYS = ["TMP", "TEMP", "TMPDIR", "PIP_TMPDIR"]
7
+ const CACHE_ENV_KEYS = ["UV_CACHE_DIR", "PIP_CACHE_DIR"]
8
+
9
+ const envKey = (env, key) => {
10
+ return Object.keys(env).find((candidate) => candidate.toLowerCase() === key.toLowerCase())
11
+ }
12
+
13
+ const setEnv = (env, key, value) => {
14
+ for (const candidate of Object.keys(env)) {
15
+ if (candidate.toLowerCase() === key.toLowerCase() && candidate !== key) {
16
+ delete env[candidate]
17
+ }
18
+ }
19
+ env[key] = value
20
+ }
21
+
22
+ const normalizeEnvPath = (value, root) => {
23
+ if (typeof value !== "string") {
24
+ return null
25
+ }
26
+ const trimmed = value.trim()
27
+ if (!trimmed) {
28
+ return null
29
+ }
30
+ if (path.isAbsolute(trimmed)) {
31
+ return path.resolve(trimmed)
32
+ }
33
+ if (trimmed.startsWith("./") || trimmed.startsWith(".\\")) {
34
+ return path.resolve(root, trimmed)
35
+ }
36
+ return path.resolve(root, trimmed)
37
+ }
38
+
39
+ const isInsidePath = (candidate, root) => {
40
+ const rel = path.relative(path.resolve(root), path.resolve(candidate))
41
+ return rel === "" || (!!rel && !rel.startsWith("..") && !path.isAbsolute(rel))
42
+ }
43
+
44
+ const canWriteDirectory = async (dirPath) => {
45
+ if (!dirPath) {
46
+ return false
47
+ }
48
+ const testDir = path.resolve(
49
+ dirPath,
50
+ `.pinokio-write-test-${process.pid}-${Date.now()}-${Math.random().toString(16).slice(2)}`
51
+ )
52
+ const testFile = path.resolve(testDir, "probe.tmp")
53
+ const renamedFile = path.resolve(testDir, "probe-renamed.tmp")
54
+ try {
55
+ await fs.promises.mkdir(testDir, { recursive: true })
56
+ await fs.promises.writeFile(testFile, "pinokio")
57
+ await fs.promises.appendFile(testFile, "-probe")
58
+ await fs.promises.rename(testFile, renamedFile)
59
+ await fs.promises.unlink(renamedFile)
60
+ await fs.promises.rmdir(testDir)
61
+ return true
62
+ } catch (error) {
63
+ await fs.promises.rm(testDir, { recursive: true, force: true }).catch(() => {})
64
+ return false
65
+ }
66
+ }
67
+
68
+ const ensureWritableDirectory = async (dirPath, options = {}) => {
69
+ if (!dirPath) {
70
+ return false
71
+ }
72
+ const repair = !!options.repair
73
+ try {
74
+ await fs.promises.mkdir(dirPath, { recursive: true })
75
+ } catch (error) {
76
+ if (!repair) {
77
+ return false
78
+ }
79
+ await fs.promises.rm(dirPath, { recursive: true, force: true }).catch(() => {})
80
+ try {
81
+ await fs.promises.mkdir(dirPath, { recursive: true })
82
+ } catch (mkdirError) {
83
+ return false
84
+ }
85
+ }
86
+ if (await canWriteDirectory(dirPath)) {
87
+ return true
88
+ }
89
+ if (!repair) {
90
+ return false
91
+ }
92
+ await fs.promises.rm(dirPath, { recursive: true, force: true }).catch(() => {})
93
+ try {
94
+ await fs.promises.mkdir(dirPath, { recursive: true })
95
+ } catch (error) {
96
+ return false
97
+ }
98
+ return canWriteDirectory(dirPath)
99
+ }
100
+
101
+ const canRepairManagedCachePath = (candidate, cacheRoot) => {
102
+ const rel = path.relative(path.resolve(cacheRoot), path.resolve(candidate))
103
+ return !!rel && !rel.startsWith("..") && !path.isAbsolute(rel)
104
+ }
105
+
106
+ const managedCacheEnvDefaults = () => {
107
+ const defaults = {}
108
+ for (const key of TEMP_ENV_KEYS.concat(CACHE_ENV_KEYS)) {
109
+ defaults[key] = `./cache/${key}`
110
+ }
111
+ return defaults
112
+ }
113
+
114
+ const ensurePinokioCacheDirs = async (kernel) => {
115
+ if (!kernel || !kernel.homedir) {
116
+ return {}
117
+ }
118
+ const root = path.resolve(kernel.homedir)
119
+ const cacheRoot = path.resolve(root, "cache")
120
+ const envPath = path.resolve(root, "ENVIRONMENT")
121
+ const defaults = managedCacheEnvDefaults()
122
+ await Util.update_env(envPath, defaults)
123
+ const env = await get(root, kernel)
124
+
125
+ for (const key of TEMP_ENV_KEYS.concat(CACHE_ENV_KEYS)) {
126
+ const existingKey = envKey(env, key)
127
+ const dirPath = normalizeEnvPath(existingKey ? env[existingKey] : defaults[key], root)
128
+ const managedPath = path.resolve(cacheRoot, key)
129
+ const targetPath = dirPath && isInsidePath(dirPath, root) ? dirPath : managedPath
130
+ const repair = canRepairManagedCachePath(targetPath, cacheRoot)
131
+ if (!repair || !(await ensureWritableDirectory(targetPath, { repair: true }))) {
132
+ throw new Error(`Pinokio could not create a writable ${key} directory: ${targetPath}`)
133
+ }
134
+ setEnv(env, key, targetPath)
135
+ }
136
+
137
+ return env
138
+ }
7
139
  const ENVS = async () => {
8
140
  // const primary_port = 80
9
141
  // const secondary_port = 42000
@@ -828,4 +960,4 @@ const init = async (options, kernel) => {
828
960
  env_path: current
829
961
  }
830
962
  }
831
- module.exports = { ENV, get, get2, init_folders, requirements, init, get_root }
963
+ module.exports = { ENV, get, get2, init_folders, ensurePinokioCacheDirs, requirements, init, get_root }
package/kernel/index.js CHANGED
@@ -1053,6 +1053,7 @@ class Kernel {
1053
1053
 
1054
1054
  // 2. mkdir all the folders if not already created
1055
1055
  await Environment.init_folders(this.homedir, this)
1056
+ await Environment.ensurePinokioCacheDirs(this)
1056
1057
 
1057
1058
  // if key.json doesn't exist, create an empty json file
1058
1059
  let ee = await this.exists(this.homedir, "key.json")
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pinokiod",
3
- "version": "7.2.9",
3
+ "version": "7.2.10",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {