@oh-my-pi/pi-natives 12.17.2 → 12.18.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.
Binary file
Binary file
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "@oh-my-pi/pi-natives",
4
- "version": "12.17.2",
4
+ "version": "12.18.0",
5
5
  "description": "Native Rust bindings for grep, clipboard, image processing, syntax highlighting, PTY, and shell operations via N-API",
6
6
  "homepage": "https://github.com/can1357/oh-my-pi",
7
7
  "author": "Can Bölük",
@@ -38,7 +38,7 @@
38
38
  "bench": "bun bench/grep.ts"
39
39
  },
40
40
  "dependencies": {
41
- "@oh-my-pi/pi-utils": "12.17.2"
41
+ "@oh-my-pi/pi-utils": "12.18.0"
42
42
  },
43
43
  "devDependencies": {
44
44
  "@types/bun": "^1.3.9"
package/src/native.ts CHANGED
@@ -7,7 +7,7 @@ import * as fs from "node:fs";
7
7
  import { createRequire } from "node:module";
8
8
  import * as os from "node:os";
9
9
  import * as path from "node:path";
10
- import { $env } from "@oh-my-pi/pi-utils";
10
+ import { $env, logger } from "@oh-my-pi/pi-utils";
11
11
  import { getNativesDir } from "@oh-my-pi/pi-utils/dirs";
12
12
  import packageJson from "../package.json";
13
13
  import type { NativeBindings } from "./bindings";
@@ -27,11 +27,8 @@ import "./work/types";
27
27
 
28
28
  export type { NativeBindings, TsFunc } from "./bindings";
29
29
 
30
- const debugStartup = $env.PI_DEBUG_STARTUP ? (stage: string) => process.stderr.write(`[startup] ${stage}\n`) : () => {};
31
-
32
30
  type CpuVariant = "modern" | "baseline";
33
31
  const require = createRequire(import.meta.url);
34
- const textDecoder = new TextDecoder();
35
32
  const platformTag = `${process.platform}-${process.arch}`;
36
33
  const packageVersion = (packageJson as { version: string }).version;
37
34
  const nativeDir = path.join(import.meta.dir, "..", "native");
@@ -66,22 +63,17 @@ const releaseCandidates = isCompiledBinary ? [...compiledCandidates, ...baseRele
66
63
  const candidates = $env.PI_DEV ? [...debugCandidates, ...releaseCandidates] : releaseCandidates;
67
64
  const dedupedCandidates = [...new Set(candidates)];
68
65
 
69
- function decodeOutput(output: string | ArrayBufferView | ArrayBuffer | null | undefined): string {
70
- if (!output) return "";
71
- if (typeof output === "string") return output;
72
- if (ArrayBuffer.isView(output))
73
- return textDecoder.decode(new Uint8Array(output.buffer, output.byteOffset, output.byteLength));
74
- return textDecoder.decode(new Uint8Array(output));
75
- }
76
-
77
66
  function runCommand(command: string, args: string[]): string | null {
78
- try {
79
- const result = Bun.spawnSync([command, ...args], { stdout: "pipe", stderr: "pipe" });
80
- if (result.exitCode !== 0) return null;
81
- return decodeOutput(result.stdout).trim();
82
- } catch {
83
- return null;
84
- }
67
+ const cmdLine = `${command} '${args.join(" ")}'`;
68
+ return logger.time(`runCommand:${cmdLine}`, () => {
69
+ try {
70
+ const result = Bun.spawnSync([command, ...args], { stdout: "pipe", stderr: "pipe" });
71
+ if (result.exitCode !== 0) return null;
72
+ return result.stdout.toString("utf-8").trim();
73
+ } catch {
74
+ return null;
75
+ }
76
+ });
85
77
  }
86
78
 
87
79
  function getVariantOverride(): CpuVariant | null {
@@ -92,56 +84,45 @@ function getVariantOverride(): CpuVariant | null {
92
84
  }
93
85
 
94
86
  function detectAvx2Support(): boolean {
95
- debugStartup("native:detectAvx2Support:start");
96
87
  if (process.arch !== "x64") {
97
- debugStartup("native:detectAvx2Support:done");
98
88
  return false;
99
89
  }
100
90
 
101
91
  if (process.platform === "linux") {
102
92
  try {
103
- debugStartup("native:detectAvx2Support:readCpuinfo");
104
93
  const cpuInfo = fs.readFileSync("/proc/cpuinfo", "utf8");
105
- debugStartup("native:detectAvx2Support:done");
106
94
  return /\bavx2\b/i.test(cpuInfo);
107
95
  } catch {
108
- debugStartup("native:detectAvx2Support:done");
109
96
  return false;
110
97
  }
111
98
  }
112
99
 
113
100
  if (process.platform === "darwin") {
114
- debugStartup("native:detectAvx2Support:sysctl");
115
101
  const leaf7 = runCommand("sysctl", ["-n", "machdep.cpu.leaf7_features"]);
116
102
  if (leaf7 && /\bAVX2\b/i.test(leaf7)) {
117
- debugStartup("native:detectAvx2Support:done");
118
103
  return true;
119
104
  }
120
105
  const features = runCommand("sysctl", ["-n", "machdep.cpu.features"]);
121
- debugStartup("native:detectAvx2Support:done");
122
106
  return Boolean(features && /\bAVX2\b/i.test(features));
123
107
  }
124
108
 
125
109
  if (process.platform === "win32") {
126
- debugStartup("native:detectAvx2Support:powershell");
127
110
  const output = runCommand("powershell.exe", [
128
111
  "-NoProfile",
129
112
  "-NonInteractive",
130
113
  "-Command",
131
114
  "[System.Runtime.Intrinsics.X86.Avx2]::IsSupported",
132
115
  ]);
133
- debugStartup("native:detectAvx2Support:done");
134
116
  return output?.toLowerCase() === "true";
135
117
  }
136
118
 
137
- debugStartup("native:detectAvx2Support:done");
138
119
  return false;
139
120
  }
140
121
 
141
122
  function resolveCpuVariant(override: CpuVariant | null): CpuVariant | null {
142
123
  if (process.arch !== "x64") return null;
143
124
  if (override) return override;
144
- return detectAvx2Support() ? "modern" : "baseline";
125
+ return logger.time("native:detectAvx2Support", () => detectAvx2Support()) ? "modern" : "baseline";
145
126
  }
146
127
 
147
128
  function getAddonFilenames(tag: string, variant: CpuVariant | null): string[] {
@@ -169,7 +150,6 @@ function selectEmbeddedAddonFile(): { filename: string; filePath: string } | nul
169
150
  return embeddedAddon.files.find(file => file.variant === "baseline") ?? null;
170
151
  }
171
152
  function maybeExtractEmbeddedAddon(errors: string[]): string | null {
172
- debugStartup("native:maybeExtractEmbeddedAddon:start");
173
153
  if (!isCompiledBinary || !embeddedAddon) return null;
174
154
  if (embeddedAddon.platformTag !== platformTag || embeddedAddon.version !== packageVersion) return null;
175
155
 
@@ -178,7 +158,6 @@ function maybeExtractEmbeddedAddon(errors: string[]): string | null {
178
158
  const targetPath = path.join(versionedDir, selectedEmbeddedFile.filename);
179
159
 
180
160
  try {
181
- debugStartup("native:maybeExtractEmbeddedAddon:mkdir");
182
161
  fs.mkdirSync(versionedDir, { recursive: true });
183
162
  } catch (err) {
184
163
  const message = err instanceof Error ? err.message : String(err);
@@ -186,18 +165,13 @@ function maybeExtractEmbeddedAddon(errors: string[]): string | null {
186
165
  return null;
187
166
  }
188
167
 
189
- debugStartup("native:maybeExtractEmbeddedAddon:exists");
190
168
  if (fs.existsSync(targetPath)) {
191
- debugStartup("native:maybeExtractEmbeddedAddon:done");
192
169
  return targetPath;
193
170
  }
194
171
 
195
172
  try {
196
- debugStartup("native:maybeExtractEmbeddedAddon:read");
197
173
  const buffer = fs.readFileSync(selectedEmbeddedFile.filePath);
198
- debugStartup("native:maybeExtractEmbeddedAddon:write");
199
174
  fs.writeFileSync(targetPath, buffer);
200
- debugStartup("native:maybeExtractEmbeddedAddon:done");
201
175
  return targetPath;
202
176
  } catch (err) {
203
177
  const message = err instanceof Error ? err.message : String(err);
@@ -206,19 +180,18 @@ function maybeExtractEmbeddedAddon(errors: string[]): string | null {
206
180
  }
207
181
  }
208
182
  function loadNative(): NativeBindings {
209
- debugStartup("native:loadNative:start");
210
183
  const errors: string[] = [];
211
- const embeddedCandidate = maybeExtractEmbeddedAddon(errors);
184
+ const embeddedCandidate = logger.time("native:maybeExtractEmbeddedAddon", () => maybeExtractEmbeddedAddon(errors));
212
185
  const runtimeCandidates = embeddedCandidate ? [embeddedCandidate, ...dedupedCandidates] : dedupedCandidates;
213
186
  for (const candidate of runtimeCandidates) {
214
187
  try {
215
- debugStartup(`native:loadNative:require:${path.basename(candidate)}`);
216
- const bindings = require(candidate) as NativeBindings;
188
+ const bindings = logger.time(`native:loadNative:require:${path.basename(candidate)}`, () =>
189
+ require(candidate),
190
+ ) as NativeBindings;
217
191
  validateNative(bindings, candidate);
218
192
  if ($env.PI_DEV) {
219
193
  console.log(`Loaded native addon from ${candidate}`);
220
194
  }
221
- debugStartup("native:loadNative:done");
222
195
  return bindings;
223
196
  } catch (err) {
224
197
  if ($env.PI_DEV) {
@@ -302,4 +275,4 @@ function validateNative(bindings: NativeBindings, source: string): void {
302
275
  );
303
276
  }
304
277
  }
305
- export const native = loadNative();
278
+ export const native = logger.time("native:loadNative", () => loadNative());