<commit_before>import os from 'os'
import fs from 'fs'
import path from 'path'
import { spawn, exec, execSync } from 'child_process'
import WebSocket from 'ws'
import got from 'got'
import AWS from 'aws-sdk'
import { sleep } from './utils'

export const tracelogBucket = new AWS.S3({
  params: { Bucket: process.env.S3_PDF_BUCKET },
})

const CHROME_PATH = process.env.CHROME_PATH && path.resolve(process.env.CHROME_PATH)

// const mountface = execSync('mount --bind /tmp/ /dev/shm/')
// console.log('mounted', mountface.toString())

let chromeProcess

if (CHROME_PATH) {
  console.log('os.tmpdir()', os.tmpdir())

  const copyface = execSync('cp -R /var/task/headless-chrome /tmp')
  console.log('copied', copyface.toString())

  console.log('LOL chrome headless bin path', CHROME_PATH)

  execSync('ls && df -h && cat /etc/fstab', (err, out, end) => {
    console.log('exec', err, out, end)
  })

  const blah1 = spawn('df', ['-h'])
  blah1.stdout.on('data', (data) => {
    console.log(`blah1 stdout: ${data}`)
  })

  chromeProcess = new Promise((resolve, reject) => {
    const child = spawn(
      // CHROME_PATH,
      '/tmp/headless-chrome/headless_shell',
      [
        // '--headless',
        '--disable-gpu',

        '--no-sandbox',
        '--remote-debugging-port=9222',
        '--user-data-dir=/tmp',
        '--hide-scrollbars',
        // '--dump-dom', // Dump DOM is disabled when remote debugging is enabled.
        // '--use-gl=""',
        // '--screenshot="/tmp/test.png"', // Capture screenshot is disabled when remote debugging is enabled.
        // '--trace-startup=*,disabled-by-default-memory-infra',
        '--trace-startup=*',
        '--trace=*',
        'https://google.com/',
      ],
      { cwd: os.tmpdir(), shell: true, detached: true },
    ) // '--window-size=1280x1696'

    child.on('error', (error) => {
      console.log('Failed to start child process.', error)
      reject(error)
    })

    child.stdout.on('data', (data) => {
      console.log(`child stdout: ${data}`)
      resolve()
    })

    child.stderr.on('data', (data) => {
      console.log(`child stderr: ${data}`)

      try {
        console.log('trace?', fs.readFileSync('/tmp/chrometrace.log').toString())
      } catch (error) {}

      const a = execSync('ls -lhtra /tmp')
      console.log('a', a.toString())

      resolve()
    })

    child.on('close', (code) => {
      if (code !== 0) {
        console.log(`child process exited with code ${code}`)
      }
    })
  })
}
<commit_msg>try some suggestions from <NAME> https://groups.google.com/a/chromium.org/d/msg/headless-dev/qqbZVZ2IwEw/XMKlEMP3EQAJ<commit_after>import os from 'os'
import fs from 'fs'
import path from 'path'
import { spawn, exec, execSync } from 'child_process'
import WebSocket from 'ws'
import got from 'got'
import AWS from 'aws-sdk'
import { sleep } from './utils'

export const tracelogBucket = new AWS.S3({
  params: { Bucket: process.env.S3_PDF_BUCKET },
})

const CHROME_PATH = process.env.CHROME_PATH && path.resolve(process.env.CHROME_PATH)

// const mountface = execSync('mount --bind /tmp/ /dev/shm/')
// console.log('mounted', mountface.toString())

let chromeProcess

if (CHROME_PATH) {
  console.log('os.tmpdir()', os.tmpdir())

  const copyface = execSync('cp -R /var/task/headless-chrome /tmp')
  console.log('copied', copyface.toString())

  console.log('LOL chrome headless bin path', CHROME_PATH)

  const blah1 = spawn('df', ['-h'])
  blah1.stdout.on('data', (data) => {
    console.log(`blah1 stdout: ${data}`)
  })

  chromeProcess = new Promise((resolve, reject) => {
    const child = spawn(
      // CHROME_PATH,
      '/tmp/headless-chrome/headless_shell',
      [
        // '--headless',
        '--disable-gpu',

        '--no-sandbox',
        '--remote-debugging-port=9222',
        '--user-data-dir=/tmp',
        '--hide-scrollbars',
        '--enable-logging',
        '--v=99',
        '--single-process',
        // '--dump-dom', // Dump DOM is disabled when remote debugging is enabled.
        // '--use-gl=""',
        // '--screenshot="/tmp/test.png"', // Capture screenshot is disabled when remote debugging is enabled.
        // '--trace-startup=*,disabled-by-default-memory-infra',
        '--trace-startup=*',
        '--trace=*',
        //'https://google.com/',
      ],
      { cwd: os.tmpdir(), shell: true, detached: true },
    ) // '--window-size=1280x1696'

    child.on('error', (error) => {
      console.log('Failed to start chrome process.', error)
      reject(error)
    })

    child.stdout.on('data', (data) => {
      console.log(`chrome stdout: ${data}`)
      resolve()
    })

    child.stderr.on('data', (data) => {
      console.log(`chrome stderr: ${data}`)
      resolve()
    })

    child.on('close', (code) => {
      if (code !== 0) {
        console.log(`chrome process exited with code ${code}`)
      }
    })
  })
}

{'fraction_non_alphanumeric': 0.18895705521472392, 'fraction_numerical': 0.0081799591002045, 'mean_word_length': 2.577907827359181, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 17, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3202504', 'n_tokens_mistral': 1726, 'n_tokens_neox': 1656, 'n_words': 356}