javascripttypescriptnpmk6

k6.io read binary file with TextDecoder file due to issue with util package


I'm using k6.io tests and would like to import TextDecoder (from util package)

In my script, I would like to read the binary file:

import { sleep, check } from 'k6';
import { Options } from 'k6/options';
import http from 'k6/http';
/* @ts-ignore */
import { TextDecoder } from 'util';


export const options:Options = {
  vus: 1,
  iterations: 1,
};

const file = open('uuids.txt', 'b');


export function setup() {

  console.log("File is " + file);

  let enc = new TextDecoder("utf-8")

  console.log("Decode: " + enc.decode(file));


}

export default () => {

  .....post HTTP Request
  
}

My package.json with 'util' (npm install util --save-dev):

{
  "name": "typescript",
  "version": "1.0.0",
  "main": "index.js",
  "repository": "ssh://git@github.com/k6io/example-typescript.git",
  "author": "Simon Aronsson <simme@k6.io>",
  "license": "MIT",
  "devDependencies": {
    "@babel/core": "7.13.16",
    "@babel/plugin-proposal-class-properties": "7.13.0",
    "@babel/plugin-proposal-object-rest-spread": "7.13.8",
    "@babel/preset-env": "7.13.15",
    "@babel/preset-typescript": "7.13.0",
    "@types/k6": "~0.41.0",
    "@types/node": "^20.4.5",
    "@types/webpack": "5.28.0",
    "babel-loader": "8.2.2",
    "clean-webpack-plugin": "4.0.0-alpha.0",
    "copy-webpack-plugin": "^9.0.1",
    "typescript": "4.2.4",
    "util": "^0.12.5",
    "webpack": "5.61.0",
    "webpack-cli": "4.6.0",
    "webpack-glob-entries": "^1.0.1"
  },
  "scripts": {
    "start": "webpack"
  },
  "browser": {
    "fs": false,
    "path": false,
    "os": false
  }
}

However, when I run my script: k6 run I got, seems util package is not resolved. Could you please help me understand what is the reason?

k6 run dist\post-201-create-node-test.js

          /\      |‾‾| /‾‾/   /‾‾/
     /\  /  \     |  |/  /   /  /
    /  \/    \    |     (   /   ‾‾\
   /          \   |  |\  \ |  (‾)  |
  / __________ \  |__| \__\ \_____/ .io

ERRO[0000] ReferenceError: process is not defined
        at 539 (webpack://typescript/./node_modules/util/util.js:109:0(78))
        at __webpack_require__ (webpack://typescript/webpack/bootstrap:19:0(26))
        at webpack://typescript/external%20commonjs%20%22https://jslib.k6.io/k6-utils/1.4.0/index.js%22:1:34(47)
        at file:///C:/Dev//dist/post-201-create-node-test.js:2323:3(71)
        at file:///C:/Dev/dist/post-201-create-node-test.js:2328:12(3)  hint="script exception"

----------------- RESOLVED ----------------

I resolved my problem by using my test data via SharedArray:

const data = new SharedArray('some data name', function () {
  return JSON.parse(open('./data.json')).uuids;
});

Solution

  • What are you trying to do exactly? You don't need a TextDecoder object to get a file's content. open can already read a file's content as string (assuming the file contains ASCII or UTF-8 encoded text). uuids.txt sounds like a file that would only contain UUIDs, which are usually encoded as hex digits separated by hyphens, so they can be purely represented with the ASCII charset.

    const uuids = open('uuids.txt'); // uuids contains the content of the text file
    
    export default function() {
      console.log(`First line of file: ${uuids.split('\n')[0]}`);  
    }