Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error loading certain minified JS libraries #1126

Closed
imiric opened this issue Aug 27, 2019 · 5 comments · Fixed by #1707
Closed

Error loading certain minified JS libraries #1126

imiric opened this issue Aug 27, 2019 · 5 comments · Fixed by #1707

Comments

@imiric
Copy link
Contributor

imiric commented Aug 27, 2019

I ran into this issue when trying to import the latest minified release of faker.js.

Example:

// test.js
import faker from "https://raw.githubusercontent.com/Marak/faker.js/master/build/build/faker.min.js";

export default function() {
    console.log(faker.name.findName())
}

Running k6 run -v test.js outputs:

DEBU[0000] Loading...                                    moduleSpecifier="file:///home/ivan/.local/go/src/github.com/loadimpact/k6/test.js" original moduleSpecifier=test.js
DEBU[0000] Babel: Transformed                            t=37.284193ms
DEBU[0000] Loading...                                    moduleSpecifier="https://raw.githubusercontent.com/Marak/faker.js/master/build/build/faker.min.js" original moduleSpecifier="https://raw.githubusercontent.com/Marak/faker.js/master/build/build/faker.min.js"
DEBU[0000] Fetching source...                            url="https://raw.githubusercontent.com/Marak/faker.js/master/build/build/faker.min.js?_k6=1"
DEBU[0000] Fetched!                                      len=1015294 t=214.483039ms url="https://raw.githubusercontent.com/Marak/faker.js/master/build/build/faker.min.js?_k6=1"
DEBU[0001] Loading...                                    moduleSpecifier=//1000 original moduleSpecifier=1000
WARN[0001] A url was resolved but it didn't have scheme. This will be deprecated in the future and all remote modules will need to explicitly use `https` as scheme  url=//1000
DEBU[0001] Fetching source...                            url="https://1000?_k6=1"
DEBU[0001] Fetching source...                            url="https://1000"
ERRO[0001] GoError: The moduleSpecifier "1000" couldn't be retrieved from the resolved url "https://1000". Error : "Get https://1000: dial tcp: lookup 1000: no such host"

Note that this doesn't happen with non-minified releases, or the older minified version from CDNJS (https://cdnjs.cloudflare.com/ajax/libs/Faker/3.1.0/faker.min.js), which work fine.

It seems that the 1e3 (scientific notation for 1000) dependency is incorrectly interpreted as a URL at some point, which k6 attempts to load and fails.

Strangely enough, manually changing the 1e3 reference in the minified JS to 1000 fixes the issue, so this seems like either Goja or k6 misinterpreting scientific notation, for some reason. A quick test of vm.RunString("1 + 1e3") correctly returns 1001, however, so this must be something deeper with how it resolves imports.

@jedkcanderson
Copy link

jedkcanderson commented Nov 28, 2019

I'm using the k6 docker image with Typescript-compiled scriptsw and I'm seeing a similar issue trying to import local packages such as chai.

import chai from 'chai';
...
chai.assert(2 > 1);

Produces this at runtime:

WARN[0000] A url was resolved but it didn't have scheme. This will be deprecated in the future and all remote modules will need to explicitly use `https` as scheme  url=//chai
ERRO[0000] GoError: The moduleSpecifier "chai" couldn't be retrieved from the resolved url "https://chai". Error : "Get https://chai: dial tcp: lookup chai on 127.0.0.11:53: no such host"

Similarly, if I enable Typescript's "importHelpers": true flag in my tsconfig.json, I get a very simillar warning:

WARN[0000] A url was resolved but it didn't have scheme. This will be deprecated in the future and all remote modules will need to explicitly use `https` as scheme  url=//tslib
ERRO[0000] GoError: The moduleSpecifier "tslib" couldn't be retrieved from the resolved url "https://tslib". Error : "Get https://tslib: dial tcp: lookup tslib on 127.0.0.11:53: no such host"

@na--
Copy link
Member

na-- commented Nov 28, 2019

@jedkcanderson, I'm not very familiar with TypeScript, but it's normal for import chai from 'chai'; to not work in k6. k6 is not Node.js, nor is it based on Node.js, so it doesn't support the Node.js module resolution algorithm or anything like it.

You have to use something like import chai from './chai.js';.

@mstoykov
Copy link
Collaborator

Also, k6 does not support TypeScript (#422), but you can somewhat mitigate both problems by using my k6-es6 project which uses nodejs, webpack and babel to combine your script in one single file and do the nodejs module resolution, as well as polyfill what is needed for anything that comes from npm for you ... which hopefully will make it work.
You can even (if using the current master) use --compatibility-mode=base with the final script for some perfomance gains ;)
You will need to add typescript support and your dependency in package.json as usual.

@na--
Copy link
Member

na-- commented Nov 3, 2020

This was fixed by dop251/goja#221, now we should only need to update goja (and test to double-check) to close this...

mstoykov added a commit that referenced this issue Nov 4, 2020
@mstoykov mstoykov mentioned this issue Nov 4, 2020
@mstoykov
Copy link
Collaborator

mstoykov commented Nov 4, 2020

I tested with

// test.js
import faker from "https://raw.githubusercontent.com/Marak/faker.js/9c65e5dd4902dbdf12088c36c098a9d7958afe09/dist/faker.min.js";

export default function() {
    console.log(faker.name.findName())
}

as the path has changed.. I decided to pin the current latest version with its commit. This still breaks with the same error on 0.28.0 but works with the changes from #1707

mstoykov added a commit that referenced this issue Nov 4, 2020
salem84 pushed a commit to salem84/k6 that referenced this issue Nov 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants