neo773 / chrome-fetch

Type safe curl-impersonate fetch API wrapper
1 stars 0 forks source link

Does this work in Bun? #1

Open austinm911 opened 2 months ago

austinm911 commented 2 months ago

Error some errors below

Using the package

43 |     headers: {
44 |       ...typeof requestInfo === "object" ? requestInfo.headers : {},
45 |       ...requestInit?.headers || {}
46 |     }
47 |   };
48 |   const curlString = (0, import_fetch_to_curl.default)(requestInfo, mergedInit);
                                                   ^
TypeError: import_fetch_to_curl.default is not a function. (In 'import_fetch_to_curl.default(requestInfo, mergedInit)', 'import_fetch_to_curl.default' is an instance of Object)
      at /Users/am/Coding/playground/node_modules/chrome-fetch/dist/index.cjs:48:47

Just copying the code into my code base

import { exec } from 'node:child_process'
import { promisify } from 'node:util'
import fetchToCurl, { type FetchOptions } from 'fetch-to-curl'

const execAsync = promisify(exec)

export async function chromeFetch(requestInfo: string | FetchOptions, requestInit?: FetchOptions): Promise<string> {
    const mergedInit: FetchOptions = {
        ...requestInit,
        headers: {
            ...(typeof requestInfo === 'object' ? requestInfo.headers : {}),
            ...(requestInit?.headers || {}),
        },
    }

    const curlString = fetchToCurl(requestInfo, mergedInit)
    const curlImpersonateString = curlString.replace('curl', 'curl-impersonate-chrome')

    try {
        const { stdout, stderr } = await execAsync(curlImpersonateString)
        if (stderr && !stdout) {
            throw new Error(stderr)
        }
        return stdout
    } catch (error) {
        throw error as Error
    }
}
const response = await chromeFetch(url)
console.log(response)

error

555 | }
556 | function toPathIfFileURL(fileURLOrPath) {
557 |   if (!isURLInstance(fileURLOrPath))
558 |     return fileURLOrPath;
559 |   return Bun.fileURLToPath(fileURLOrPath);
560 | function genericNodeError(message, options) {
                               ^
error: Command failed: curl-impersonate-chrome 'URL-TO-FETCH'
/bin/sh: curl-impersonate-chrome: command not found

 code: "127"

      at genericNodeError (node:child_process:560:26)
      at exitHandler (node:child_process:70:23)
      at emit (node:events:201:48)
      at #maybeClose (node:child_process:857:14)
      at #handleOnExit (node:child_process:627:16)

Bun v1.1.27-canary.108+267afa293 (macOS arm64)
neo773 commented 2 months ago

I haven't really tested this after publishing it.

However, in your 2nd log it shows you the cause, you haven't installed the lib

/bin/sh: curl-impersonate-chrome: command not found

You can install it by running.

brew tap shakacode/brew
brew install curl-impersonate

Also are you sure about the intended use case of this library? it's mainly built for bypassing WAFs