Skip to content

Commit

Permalink
Merge pull request #136 from grafana/chore/npm-audit-on-20231009
Browse files Browse the repository at this point in the history
[chore] Update node to fix docker image vulnerabilities
  • Loading branch information
w1kman authored Oct 13, 2023
2 parents 7700747 + de0264c commit 48e3db3
Show file tree
Hide file tree
Showing 10 changed files with 1,846 additions and 7,765 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: '16'
node-version: '18'
cache: 'npm'

- name: Install dependencies
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- name: Setup environment
uses: actions/setup-node@v3
with:
node-version: '16'
node-version: '18'
registry-url: 'https://registry.npmjs.org'
cache: 'npm'

Expand Down
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v16
v18
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:16-slim
FROM node:18-slim

WORKDIR /converter

Expand All @@ -7,4 +7,4 @@ COPY . .
RUN npm install
RUN npm run bundle

ENTRYPOINT ["node", "bin/har-to-k6.js"]
ENTRYPOINT ["node", "bin/har-to-k6.js", "--stdout"]
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ $ ./node_modules/.bin/har-to-k6 archive.har -o my-k6-script.js
$ har-to-k6 archive.har -o my-k6-script.js
```

#### Docker

```shell
$ docker run grafana/har-to-k6:latest archive.har > my-k6-script.js
```

### Programmatic Usage

#### Converting
Expand Down
63 changes: 53 additions & 10 deletions bin/har-to-k6.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const { DEFAULT_CLI_OPTIONS } = require('../src/constants')
const { VError } = require('verror')

class CommandLineError extends VError {}

const BOM_REGEX = /^\uFEFF/

pkginfo(module, 'version')
Expand All @@ -21,13 +22,19 @@ io.version(version)
'-o, --output <output>',
'Output file',
output,
DEFAULT_CLI_OPTIONS.output
/** @see {normalizeOptions} */
''
)
.option(
'--add-sleep',
'Add automatic sleep() based on startDateTime',
DEFAULT_CLI_OPTIONS.addSleep
)
.option(
'-s, --stdout',
'Write to stdout (ignored when running with -o, --output)',
DEFAULT_CLI_OPTIONS.stdout
)
.argument('<archive>', 'LI-HAR archive to convert')
.action(run)

Expand All @@ -40,21 +47,51 @@ function output(value) {
return value
}

/**
* Returns logger that either logs to stdout or stderr
* When writing to stdout, we want to log to stderr
*
* @param {import('caporal').LoggerInstance} log
* @param {{stdout: boolean}} opt
*/
function getLogger(log, opt) {
return opt.stdout ? log.warn : log.info
}

async function run(arg, opt, log) {
normalizeOptions(opt)
const logger = getLogger(log, opt)

try {
start(arg.archive, log)
start(arg.archive, logger)
const json = read(arg.archive)
const archive = parse(json)
const { main } = await transform(archive, opt)
write(main, opt.output)
success(opt.output, log)
write(main, opt)
success(opt, logger)
} catch (error) {
inform(error, log)
}
}

function start(file, log) {
log.info(chalk.green(`Converting '${file}'`))
/**
* Normalize options so that they are sane
* @param {{output: string, addSleep: boolean, stdout: boolean}} opt
*/
function normalizeOptions(opt) {
// If output is empty, and stdout is not set, set output to default value
if (opt.output === '' && !opt.stdout) {
opt.output = DEFAULT_CLI_OPTIONS.output
}

// If output is not empty, and stdout is set, set stdout to FALSE
if (opt.output !== '' && opt.stdout) {
opt.stdout = false
}
}

function start(file, logger) {
logger(chalk.green(`Converting '${file}'`))
}

function read(file) {
Expand All @@ -81,16 +118,22 @@ async function transform(archive, options) {
}
}

function write(main, output) {
function write(main, opt) {
try {
fs.writeFileSync(output, main)
// Write to stdout if requested, AND if no output file was specified
if (opt.stdout) {
process.stdout.write(main)
} else {
fs.writeFileSync(opt.output, main)
}
} catch (error) {
throw new CommandLineError({ name: 'WriteError', cause: error })
}
}

function success(output, log) {
log.info(chalk.green(`Wrote k6 script to '${output}'`))
function success(opt, logger) {
const target = opt.stdout ? 'STDOUT' : `'${opt.output}'`
logger(chalk.green(`Wrote k6 script to ${target}`))
}

function inform(error, log) {
Expand Down
Loading

0 comments on commit 48e3db3

Please sign in to comment.