gologinapp / gologin

REST API provides programmatic access to GoLogin App. Create a new browser profile, get a list of all browser profiles, add a browser profile and runnig
110 stars 84 forks source link

Can't initiate a GoLogin instance on win due to wrong path ' C:\C:\Users\' #136

Open hydevip opened 2 months ago

hydevip commented 2 months ago

OS: Win Server 2019 standard Node js v: 20.13.1 Gologin v: 2.0.30

Can't initiate a GoLogin instance on win due to wrong path ' C:\C:\Users\' in gologin.js file line 248 emptyProfileFolder method

(async () => {
    const {GoLogin} = await import('gologin');
  const GL = new GoLogin({
    profile_id: 'XXXX',
    token: 'XXXXx',
    args: ['--disable-background-timer-throttling', '--disable-backgrounding-occluded-windows', '--disable-renderer-backgrounding'],
  });

  const { status, wsUrl } = await GL.start().catch((e) => {
    console.trace(e);

    return { status: 'failure' };
  });
.....
})();

The new GoLogin(...) will throw this error:

C:\Users\Administrator\Desktop\chrome-server>node test-orbita.js
currentDir ::  /C:/Users/Administrator/Desktop/chrome-server/node_modules/gologin/src
zeroProfilePath ::  \C:\Users\Administrator\Desktop\chrome-server\node_modules\gologin\zero_profile.zip
_resolve(zeroProfilePath) ::  C:\C:\Users\Administrator\Desktop\chrome-server\node_modules\gologin\zero_profile.zip
Trace: Error: ENOENT: no such file or directory, open 'C:\C:\Users\Administrator\Desktop\chrome-server\node_modules\gologin\zero_profile.zip'
    at async open (node:internal/fs/promises:636:25)
    at async readFile (node:internal/fs/promises:1246:14)
    at async GoLogin.emptyProfileFolder (file:///C:/Users/Administrator/Desktop/chrome-server/node_modules/gologin/src/gologin.js:256:21)
    at async GoLogin.createStartup (file:///C:/Users/Administrator/Desktop/chrome-server/node_modules/gologin/src/gologin.js:401:26)
    at async GoLogin.start (file:///C:/Users/Administrator/Desktop/chrome-server/node_modules/gologin/src/gologin.js:1379:5)
    at async C:\Users\Administrator\Desktop\chrome-server\test-orbita.js:11:29 {
  errno: -4058,
  code: 'ENOENT',
  syscall: 'open',
  path: 'C:\\C:\\Users\\Administrator\\Desktop\\chrome-server\\node_modules\\gologin\\zero_profile.zip'
}
    at C:\Users\Administrator\Desktop\chrome-server\test-orbita.js:12:13
    at async C:\Users\Administrator\Desktop\chrome-server\test-orbita.js:11:29
Invalid status

C:\Users\Administrator\Desktop\chrome-server>node -v
v20.13.1

It looks like the issue is in the gologin.js file line 248 emptyProfileFolder method

image
hydevip commented 2 months ago

GPT answer : The issue you're encountering with the double C: in the path is likely due to the handling of URL paths and file paths in a cross-platform Node.js environment. Specifically, new URL(import.meta.url).pathname can produce paths prefixed with a forward slash, which can lead to incorrect path concatenations when used with functions like join from the path module.

Here's a breakdown of why this happens and how to fix it:

Why This Happens URL Pathname: new URL(import.meta.url).pathname returns a pathname that starts with a / on Windows. For example, /C:/Users/... which is valid in URL contexts but not in file system paths. Path Join: The path.join method expects clean file paths. When you join a path that starts with /C:/, it can result in \C:... which then resolves incorrectly to C:\C:....

Arsh1219 commented 2 months ago

replacing above function (the one mentioned in screenshot by OP) with below code fixed for me. Be cautious - This code was given by ChatGPT, I am not liable for anything.

async function emptyProfileFolder() {
  debug('get emptyProfileFolder');

  // Get the current directory path
  let currentDir = dirname(new URL(import.meta.url).pathname);

  // Handle Windows paths
  if (process.platform === 'win32') {
    currentDir = currentDir.substring(1).replace(/\//g, '\\');
  }

  // Join the path to zero_profile.zip
  const zeroProfilePath = join(currentDir, '..', 'zero_profile.zip');

  // Read the profile file
  const profile = await readFile(zeroProfilePath);
  debug('emptyProfileFolder LENGTH ::', profile.length);

  return profile;
}
SattyaP commented 2 months ago

Has your problem been resolved?