Merge pull request #3665 from code-yeongyu/fix/posthog-arch-cpus-crash
fix(posthog): guard os.cpus() against Bun /sys/firmware permissions error (fixes #3496)
This commit is contained in:
@@ -54,4 +54,49 @@ describe("posthog client creation", () => {
|
||||
expect(() => pluginPostHog.trackActive("plugin", "plugin_loaded")).not.toThrow()
|
||||
await expect(pluginPostHog.shutdown()).resolves.toBeUndefined()
|
||||
})
|
||||
|
||||
it("creates a plugin client when os.cpus throws", async () => {
|
||||
// given
|
||||
process.env.OMO_DISABLE_POSTHOG = "0"
|
||||
process.env.OMO_SEND_ANONYMOUS_TELEMETRY = "1"
|
||||
process.env.POSTHOG_API_KEY = "test-api-key"
|
||||
|
||||
mock.module("os", () => ({
|
||||
default: {
|
||||
arch: () => "x64",
|
||||
cpus: () => {
|
||||
throw new Error("Failed to get CPU information")
|
||||
},
|
||||
hostname: () => "test-host",
|
||||
platform: () => "linux",
|
||||
release: () => "6.8.0-arch1-1",
|
||||
totalmem: () => 8 * 1024 * 1024 * 1024,
|
||||
type: () => "Linux",
|
||||
},
|
||||
}))
|
||||
|
||||
mock.module("posthog-node", () => ({
|
||||
PostHog: class {
|
||||
capture() {}
|
||||
captureException() {}
|
||||
async shutdown() {}
|
||||
},
|
||||
}))
|
||||
|
||||
const { createPluginPostHog } = await importPostHogModule()
|
||||
|
||||
// when
|
||||
const pluginPostHog = createPluginPostHog()
|
||||
|
||||
// then
|
||||
expect(() =>
|
||||
pluginPostHog.capture({
|
||||
distinctId: "plugin",
|
||||
event: "plugin_loaded",
|
||||
}),
|
||||
).not.toThrow()
|
||||
expect(() => pluginPostHog.captureException(new Error("plugin failure"), "plugin")).not.toThrow()
|
||||
expect(() => pluginPostHog.trackActive("plugin", "plugin_loaded")).not.toThrow()
|
||||
await expect(pluginPostHog.shutdown()).resolves.toBeUndefined()
|
||||
})
|
||||
})
|
||||
|
||||
+13
-2
@@ -55,7 +55,18 @@ function getPostHogHost(): string {
|
||||
return process.env.POSTHOG_HOST?.trim() || DEFAULT_POSTHOG_HOST
|
||||
}
|
||||
|
||||
function safeCpus(): { length: number; model: string | undefined } {
|
||||
try {
|
||||
const cpus = os.cpus()
|
||||
return { length: cpus.length, model: cpus[0]?.model }
|
||||
} catch {
|
||||
return { length: 0, model: undefined }
|
||||
}
|
||||
}
|
||||
|
||||
function getSharedProperties(source: PostHogSource): NonNullable<PostHogCaptureEvent["properties"]> {
|
||||
const cpus = safeCpus()
|
||||
|
||||
return {
|
||||
platform: "oh-my-opencode",
|
||||
package_name: PUBLISHED_PACKAGE_NAME,
|
||||
@@ -68,8 +79,8 @@ function getSharedProperties(source: PostHogSource): NonNullable<PostHogCaptureE
|
||||
$os_version: os.release(),
|
||||
os_arch: os.arch(),
|
||||
os_type: os.type(),
|
||||
cpu_count: os.cpus().length,
|
||||
cpu_model: os.cpus()[0]?.model,
|
||||
cpu_count: cpus.length,
|
||||
cpu_model: cpus.model,
|
||||
total_memory_gb: Math.round(os.totalmem() / 1024 / 1024 / 1024),
|
||||
locale: Intl.DateTimeFormat().resolvedOptions().locale,
|
||||
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
|
||||
|
||||
Reference in New Issue
Block a user