pinokiod 7.2.9 → 7.2.11

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,168 @@ 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
+ const CACHE_PREFLIGHT_KEYS = TEMP_ENV_KEYS.concat(CACHE_ENV_KEYS)
9
+
10
+ const formatCachePreflightError = (error) => {
11
+ if (!error) {
12
+ return ""
13
+ }
14
+ const parts = []
15
+ if (error.code) {
16
+ parts.push(`code=${error.code}`)
17
+ }
18
+ if (typeof error.errno !== "undefined") {
19
+ parts.push(`errno=${error.errno}`)
20
+ }
21
+ if (error.syscall) {
22
+ parts.push(`syscall=${error.syscall}`)
23
+ }
24
+ if (error.path) {
25
+ parts.push(`path=${error.path}`)
26
+ }
27
+ if (error.dest) {
28
+ parts.push(`dest=${error.dest}`)
29
+ }
30
+ if (error.message) {
31
+ parts.push(`message=${error.message}`)
32
+ }
33
+ return parts.join(" ")
34
+ }
35
+
36
+ const logCachePreflight = (message) => {
37
+ console.log(`[Pinokio cache preflight] ${message}`)
38
+ }
39
+
40
+ const probeCacheDir = async (dirPath) => {
41
+ const probeDir = path.resolve(
42
+ dirPath,
43
+ `.pinokio-cache-probe-${process.pid}-${Date.now()}-${Math.random().toString(16).slice(2)}`
44
+ )
45
+ const probeFile = path.resolve(probeDir, "probe.tmp")
46
+ const renamedFile = path.resolve(probeDir, "probe-renamed.tmp")
47
+ const steps = [
48
+ ["create probe directory", () => fs.promises.mkdir(probeDir, { recursive: false })],
49
+ ["write probe file", () => fs.promises.writeFile(probeFile, "pinokio")],
50
+ ["append probe file", () => fs.promises.appendFile(probeFile, "-cache-probe")],
51
+ ["rename probe file", () => fs.promises.rename(probeFile, renamedFile)],
52
+ ["delete probe file", () => fs.promises.unlink(renamedFile)],
53
+ ["remove probe directory", () => fs.promises.rmdir(probeDir)]
54
+ ]
55
+
56
+ for (const [step, run] of steps) {
57
+ try {
58
+ await run()
59
+ } catch (error) {
60
+ await fs.promises.rm(probeDir, { recursive: true, force: true }).catch(() => {})
61
+ return { ok: false, step, error }
62
+ }
63
+ }
64
+ return { ok: true }
65
+ }
66
+
67
+ const managedCacheEnvDefaults = () => {
68
+ const defaults = {}
69
+ for (const key of CACHE_PREFLIGHT_KEYS) {
70
+ defaults[key] = `./cache/${key}`
71
+ }
72
+ return defaults
73
+ }
74
+
75
+ const ensureCachePreflightDir = async (key, targetPath) => {
76
+ logCachePreflight(`${key}: target=${targetPath}`)
77
+ try {
78
+ await fs.promises.mkdir(targetPath, { recursive: true })
79
+ logCachePreflight(`${key}: mkdir ok`)
80
+ } catch (error) {
81
+ logCachePreflight(`${key}: mkdir failed ${formatCachePreflightError(error)}`)
82
+ }
83
+
84
+ const firstProbe = await probeCacheDir(targetPath)
85
+ if (firstProbe.ok) {
86
+ logCachePreflight(`${key}: probe ok`)
87
+ return { key, path: targetPath, repaired: false, ok: true }
88
+ }
89
+
90
+ logCachePreflight(`${key}: probe failed step="${firstProbe.step}" ${formatCachePreflightError(firstProbe.error)}`)
91
+ logCachePreflight(`${key}: repair delete start path=${targetPath}`)
92
+
93
+ try {
94
+ await fs.promises.rm(targetPath, { recursive: true, force: true })
95
+ logCachePreflight(`${key}: repair delete ok`)
96
+ } catch (error) {
97
+ logCachePreflight(`${key}: repair delete failed ${formatCachePreflightError(error)}`)
98
+ return { key, path: targetPath, repaired: false, ok: false, step: "repair delete", error }
99
+ }
100
+
101
+ try {
102
+ await fs.promises.mkdir(targetPath, { recursive: true })
103
+ logCachePreflight(`${key}: repair mkdir ok`)
104
+ } catch (error) {
105
+ logCachePreflight(`${key}: repair mkdir failed ${formatCachePreflightError(error)}`)
106
+ return { key, path: targetPath, repaired: true, ok: false, step: "repair mkdir", error }
107
+ }
108
+
109
+ const secondProbe = await probeCacheDir(targetPath)
110
+ if (secondProbe.ok) {
111
+ logCachePreflight(`${key}: repair probe ok`)
112
+ return { key, path: targetPath, repaired: true, ok: true }
113
+ }
114
+
115
+ logCachePreflight(`${key}: repair probe failed step="${secondProbe.step}" ${formatCachePreflightError(secondProbe.error)}`)
116
+ return { key, path: targetPath, repaired: true, ok: false, step: secondProbe.step, error: secondProbe.error }
117
+ }
118
+
119
+ const ensurePinokioCacheDirs = async (kernel, options = {}) => {
120
+ if (!kernel || !kernel.homedir) {
121
+ return {}
122
+ }
123
+ const throwOnFailure = !!options.throwOnFailure
124
+ const root = path.resolve(kernel.homedir)
125
+ const cacheRoot = path.resolve(root, "cache")
126
+ const envPath = path.resolve(root, "ENVIRONMENT")
127
+ const defaults = managedCacheEnvDefaults()
128
+ logCachePreflight(`start root=${root}`)
129
+ await Util.update_env(envPath, defaults)
130
+ logCachePreflight(`ENVIRONMENT updated keys=${CACHE_PREFLIGHT_KEYS.join(",")}`)
131
+ try {
132
+ await fs.promises.mkdir(cacheRoot, { recursive: true })
133
+ logCachePreflight(`cache root mkdir ok path=${cacheRoot}`)
134
+ } catch (error) {
135
+ logCachePreflight(`cache root mkdir failed path=${cacheRoot} ${formatCachePreflightError(error)}`)
136
+ }
137
+
138
+ const errors = []
139
+ const results = []
140
+
141
+ for (const key of CACHE_PREFLIGHT_KEYS) {
142
+ const targetPath = path.resolve(cacheRoot, key)
143
+ const result = await ensureCachePreflightDir(key, targetPath)
144
+ results.push(result)
145
+ if (!result.ok) {
146
+ errors.push(result)
147
+ }
148
+ }
149
+
150
+ if (errors.length > 0) {
151
+ kernel.cacheDirErrors = errors
152
+ const message = errors
153
+ .map((error) => `${error.key}: ${error.path} (${error.step || "unknown"} ${formatCachePreflightError(error.error)})`)
154
+ .join(", ")
155
+ logCachePreflight(`failed ${message}`)
156
+ if (throwOnFailure) {
157
+ throw new Error(`Pinokio could not create writable cache directories: ${message}`)
158
+ }
159
+ } else {
160
+ kernel.cacheDirErrors = []
161
+ logCachePreflight(`complete ok checked=${results.length} repaired=${results.filter((result) => result.repaired).length}`)
162
+ }
163
+
164
+ kernel.cacheDirPreflight = results
165
+ const env = await get(root, kernel)
166
+ return { env, errors, results }
167
+ }
7
168
  const ENVS = async () => {
8
169
  // const primary_port = 80
9
170
  // const secondary_port = 42000
@@ -828,4 +989,4 @@ const init = async (options, kernel) => {
828
989
  env_path: current
829
990
  }
830
991
  }
831
- module.exports = { ENV, get, get2, init_folders, requirements, init, get_root }
992
+ 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.11",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {