The k6.io library is unable to read binary files using the TextDecoder file because of a problem with the util

For my k6.io tests, I am trying to import TextDecoder from the util package.

Within my script, I aim to read a 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
  
}

In my package.json, I have included 'util' (npm install util --save-dev):

{
  "name": "typescript",
  "version": "1.0.0",
  "main": "index.js",
  "repository": "ssh://<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="60070914200709140815024e030f0d">[email protected]</a>/k6io/example-typescript.git",
  "author": "Simon Aronsson <<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="13607a7e7e765378253d7a7c">[email protected]</a>>",
  "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 attempting to run my script with 'k6 run', it appears that the util package was not resolved. Can you provide insight into why this may be happening?

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 ----------------

To resolve this issue, I utilized my test data using SharedArray:

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

Answer №1

How exactly do you plan to accomplish your goal? Utilizing a TextDecoder object is unnecessary to access the contents of a file. You can already use open to read a file's content as a string (assuming it contains ASCII or UTF-8 encoded text). The file named uuids.txt likely only includes UUIDs, which are typically in hex digits separated by hyphens and can be represented using the ASCII charset.

const uuids = open('uuids.txt'); // variable uuids now holds the text file's content

export default function() {
  console.log(`First line from the file: ${uuids.split('\n')[0]}`);  
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Change the Bootstrap components according to the size of the screen

Is there a built-in Bootstrap feature to change an element's class based on screen size? I'm working on a webpage with scrollable card elements. On desktop, the cards are arranged horizontally, but on mobile they are stacked vertically, requirin ...

What is the best way to distinguish between various objects that are all in a single line?

After creating an array of objects with data such as name and id, I used the res.json() method to convert it into json format for browser use. However, when I input the array of object data, it looked like this: [ { id: 1, name: 'albany sof ...

Challenges arise when working with texture size in Three.js

Less talk, more code =) var objects = []; var camera, scene, renderer; document.addEventListener('mousedown', onDocumentMouseDown, false); init(); render(); function onDocumentMouseDown(event) { event.preventDefault(); var vector = new THRE ...

Accessing class properties from JavaScript code

Can you retrieve the class values of a style element using Vue's script section? For example: .node-output02 { bottom: #{-2+$portSize/-2}px; left: #{$nodeWidth/3}px; } In the script, I'd like to achieve: const left = .node-output02. ...

Is there a way to verify whether a key within an Interface corresponds to a class that is a subclass of a specific Parent

Is there a method in typescript to ensure that a property in an interface must be of type "child subclass C, which extends class P"? example.ts import { P } from '/path/to/types' class C extends P { ... } types.ts // `C` cannot be accessed ...

Tips for managing the ever-evolving language state in expressJS

I am currently working on a project utilizing nodeJs, handlebars, and the expressJs framework. I have implemented a language change functionality using the i18n-express module. This module adds a query string at the end of the URL when changing languages. ...

There was an issue attempting to differentiate '[object Object]'. The Angular API Get Request from .Net only allows for arrays and iterables to be used

I am currently in the learning stage and consider myself a novice, so please forgive me if this question seems silly. I am working on a Movie Database project that involves integrating movies from a live API, creating a favorite list, implementing JWT auth ...

Closing a Bootstrap 4 accordion that is dynamically generated within a table when another accordion is opened

Currently, I am utilizing bootstrap-4 to create accordions within a table. Whenever clicking on the image, another tr element is appended which triggers the opening of the accordion. Furthermore, within each accordion, I am adding another tr element, essen ...

Reloading data in Angular using jQuery DataTables

After successfully implementing the jQuery datatables library, I encountered an issue where new data retrieved from my API was not displaying inside the datatable as expected. Instead, it was being shown below the table using ng-repeat. It seems that the d ...

Having trouble with installing forked node_module react. Let's troubleshoot and get

After discovering that the original author had abandoned a react native module that met my needs, I decided to fork it for corrections. Following some online articles on how to Fork repositories, I successfully made the necessary changes and cloned the rep ...

Elegant Decline of Javascript Functionality - Imported Web Assets

Looking for assistance from experienced JS coders. I'm currently working on a Wordpress website that utilizes jQuery AJAX methods to reload either the entire content area or just the main content section based on different navigation clicks. My aim i ...

Designing a carousel-style menu list with navigation buttons for moving forward and backward

I'm running into some trouble while attempting to create a carousel. Firstly, the issue I am facing is that when you continuously click on the Next button, the function keeps working even after reaching the last item. I'm not sure how to make th ...

Can curly braces be utilized in the style section of Vue.js?

Can I utilize curly braces in the style section of vue.js to set a value? For instance .container { background: url({{ image-url }}; color: {{ color-1 }} } ...

JavaScript declares that the intersection property is missing

Initially, I have a variable of type IPerson[], but after undergoing a couple of mapping iterations, it should have an added _id property, transforming it into Array<IPerson & IWithId>. However, upon trying to access the _id property in the fourt ...

What is the best way to transform this string into a Luxon Datetime object using Typescript?

Here is a snippet of Javascript/Typescript code that I have for converting a string into a Luxon DateTime: import { DateTime } from 'luxon'; const x = '2023-10-27T01:00:57.830+00:00' const y = DateTime.fromFormat(x, 'yyyy-MM-dd ...

npm attempts to install a different version of Node than the one selected by nvm

Having a problem with npm - it keeps trying to install node version 14.21.3 even though I specifically chose version 20.17 using the command nvm use 20.17. To troubleshoot, I uninstalled both npm and node, clearing out all modules in the process. I made s ...

Encountering an error during the installation of a package in a Meteor app on Windows 7 due to a 'node-gyp

When attempting to integrate the mizzao:turkserver package into my Meteor application, I encountered a persistent error message: mizzao:turkserver: updating npm dependencies -- request, libxmljs, validator, querystring, async, deepmerge... gypnpm ERR! W ...

Testing Vue Components - Simulating the return value of a plugin

I have a unique scenario where I need to mock the return value of a custom plugin without importing it directly into my test. By creating a mock function for the plugin, I can easily achieve this goal. However, I am unsure how to change the return value of ...

Exploring the ideal scenarios for utilizing propTypes in React

When creating in-house components that require specific props to function properly, I believe it is best to conduct prop requirement checks during testing rather than including propTypes in the production code. This is especially important for components t ...

Using jQuery's .append() method within a for loop

As a newbie in JavaScript, I've been trying to find answers to my questions, but the solutions I've come across are quite complex for my level of understanding. One specific issue I'm tackling involves creating a grid of divs using user inpu ...