From 5ed3017faa498507cdc8cdfddcf2381e447728a0 Mon Sep 17 00:00:00 2001 From: ZHAO Jin-Xiang Date: Wed, 25 Dec 2024 23:09:48 +0800 Subject: [PATCH] fix: esm suport --- .../platform/node/machine-id/getMachineId.ts | 38 +++++++++++-------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/packages/opentelemetry-resources/src/detectors/platform/node/machine-id/getMachineId.ts b/packages/opentelemetry-resources/src/detectors/platform/node/machine-id/getMachineId.ts index 47de5913b48..96976412485 100644 --- a/packages/opentelemetry-resources/src/detectors/platform/node/machine-id/getMachineId.ts +++ b/packages/opentelemetry-resources/src/detectors/platform/node/machine-id/getMachineId.ts @@ -15,23 +15,29 @@ */ import * as process from 'process'; -let getMachineId: () => Promise; +let getMachineIdImpl: () => Promise; +async function getMachineId(): Promise { + if(!getMachineIdImpl) { + switch (process.platform) { + case 'darwin': + getMachineIdImpl = await import('./getMachineId-darwin').then((m) => m.getMachineId); + break; + case 'linux': + getMachineIdImpl = await import('./getMachineId-linux').then((m) => m.getMachineId); + break; + case 'freebsd': + getMachineIdImpl = await import('./getMachineId-bsd').then((m) => m.getMachineId); + break; + case 'win32': + getMachineIdImpl = await import('./getMachineId-win').then((m) => m.getMachineId); + break; + default: + getMachineIdImpl = await import('./getMachineId-unsupported').then((m) => m.getMachineId); + break; + } + } -switch (process.platform) { - case 'darwin': - ({ getMachineId } = require('./getMachineId-darwin')); - break; - case 'linux': - ({ getMachineId } = require('./getMachineId-linux')); - break; - case 'freebsd': - ({ getMachineId } = require('./getMachineId-bsd')); - break; - case 'win32': - ({ getMachineId } = require('./getMachineId-win')); - break; - default: - ({ getMachineId } = require('./getMachineId-unsupported')); + return getMachineIdImpl(); } export { getMachineId };