René's URL Explorer Experiment


Title: Validation fails when running via JsDom in NodeJs · Issue #5151 · plotly/plotly.js · GitHub

Open Graph Title: Validation fails when running via JsDom in NodeJs · Issue #5151 · plotly/plotly.js

X Title: Validation fails when running via JsDom in NodeJs · Issue #5151 · plotly/plotly.js

Description: Dear, we are integrating the powerful Plotly library into the open-source Node-RED iot framework, which is running on NodeJs. Running Plotly on the NodeJs server (via JsDom) works fine, but calling the validate function fails. A small pr...

Open Graph Description: Dear, we are integrating the powerful Plotly library into the open-source Node-RED iot framework, which is running on NodeJs. Running Plotly on the NodeJs server (via JsDom) works fine, but calling...

X Description: Dear, we are integrating the powerful Plotly library into the open-source Node-RED iot framework, which is running on NodeJs. Running Plotly on the NodeJs server (via JsDom) works fine, but calling...

Opengraph URL: https://github.com/plotly/plotly.js/issues/5151

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Validation fails when running via JsDom in NodeJs","articleBody":"Dear,\r\n\r\nwe are integrating the powerful Plotly library into the open-source Node-RED iot framework, which is running on NodeJs.\r\n\r\nRunning Plotly on the NodeJs server (via JsDom) works fine, but calling the `validate` function fails.\r\nA small program to reproduce the problem (remark: the jsdom and plotly.js-dist NPM packages need to be installed):\r\n```\r\nconst jsdom = require('jsdom');\r\nconst vm = require('vm');  \r\nconst fs = require('fs');\r\n\r\nvar plotlyServerDom = new jsdom.JSDOM('', { runScripts: 'dangerously'});\r\n \r\n// Mock a few things that JSDOM doesn't support out-of-the-box\r\nplotlyServerDom.window.HTMLCanvasElement.prototype.getContext = function() { return null; };\r\nplotlyServerDom.window.URL.createObjectURL = function() { return null; };\r\n\r\n// Run Plotly inside Jsdom\r\nvar plotlyJsPath = require.resolve(\"plotly.js-dist\");\r\nvar plotlyJsSource = fs.readFileSync(plotlyJsPath, 'utf-8');\r\nplotlyServerDom.window.eval(plotlyJsSource);\r\n\r\nvar data = [];\r\nvar trace ={};\r\ntrace.name = \"mytrace\"; \r\ntrace.type = \"scatter\";\r\ntrace.x = [\"2020-09-06 09:10:49\"];\r\ntrace.y = [5];\r\ntrace.opacity = 1;\r\ndata.push(trace);\r\n\r\nvar layout = {};\r\nlayout.title = {};\r\nlayout.title.text = \"mytitle\";\r\n\r\nvar result = plotlyServerDom.window.Plotly.validate(data, layout);\r\n\r\nif (result) {\r\n    console.log(result);\r\n}\r\nelse {\r\n    console.log(\"Validation ok\");\r\n}\r\n```\r\nThe result is:\r\n\u003e[\r\n  {\r\n    code: 'object',\r\n    container: 'layout',\r\n    trace: null,\r\n    path: '',\r\n    astr: '',\r\n    msg: 'The layout argument must be linked to an object container'\r\n  },\r\n  {\r\n    code: 'object',\r\n    container: 'data',\r\n    trace: 0,\r\n    path: '',\r\n    astr: '',\r\n    msg: 'Trace 0 in the data argument must be linked to an object container'\r\n  }\r\n]\r\n\r\nWhich means that Plotly doesn't recognize my input parameters as Javascript objects, which is checked in the [isPlainObject ](https://github.com/plotly/plotly.js/blob/master/src/lib/is_plain_object.js#L23) function:\r\n\r\n![image](https://user-images.githubusercontent.com/14224149/93267317-cbddcb00-f7ab-11ea-87d8-40a4cb3e0479.png)\r\n\r\nThe second check fails, so the input is not considered as a Javascript object...\r\nSeems that the problem is caused by this:\r\n1. JsDom runs its own vm (virtual machine) in NodeJs.\r\n2. That vm gets its very own instance of Object. \r\n3. The prototype of an object created inside the vm will not be the exact equal to the prototype of the Object outside the vm (i.e. in my program).\r\n4. So the equality in the IF condition will fail ...\r\n\r\nYou can find a prove of this theory in the next program.  Here a global function `createObject` is declared inside the vm, and called from outside the vm.  This allows us to create an empty Javascript object inside the vm, return it back to us so we can add data to the object, and then we pass the object to Plotly (which is running in the Jsdom vm):\r\n```\r\nconst jsdom      = require('jsdom');\r\nconst vm         = require('vm');  \r\nconst fs         = require('fs');\r\n\r\nvar plotlyServerDom = new jsdom.JSDOM('', { runScripts: 'dangerously'});\r\n \r\n// Mock a few things that JSDOM doesn't support out-of-the-box\r\nplotlyServerDom.window.HTMLCanvasElement.prototype.getContext = function() { return null; };\r\nplotlyServerDom.window.URL.createObjectURL = function() { return null; };\r\n    \r\n// Script to add global functions (to create a Javascript object) in the server DOM context\r\nconst script = new vm.Script(`\r\n    function createObject() {\r\n        return {};\r\n    }\r\n`);\r\n\r\n// Execute the script in the VM (of jsDom), so that the global function is added to the VM context\r\nconst serverDomVmContext = plotlyServerDom.getInternalVMContext();\r\nscript.runInContext(serverDomVmContext);\r\n    \r\nvar plotlyJsPath = require.resolve(\"plotly.js-dist\");\r\nvar plotlyJsSource = fs.readFileSync(plotlyJsPath, 'utf-8');\r\nplotlyServerDom.window.eval(plotlyJsSource);\r\n\r\nvar data = [];\r\nvar trace = plotlyServerDom.window.createObject();\r\ntrace.name = \"mytrace\"; \r\ntrace.type = \"scatter\";\r\ntrace.x = [\"2020-09-06 09:10:49\"];\r\ntrace.y = [5];\r\ntrace.opacity = 1;\r\ndata.push(trace);\r\n\r\nvar layout = plotlyServerDom.window.createObject();\r\nlayout.title = plotlyServerDom.window.createObject();\r\nlayout.title.text = \"mytitle\";\r\n\r\nvar result = plotlyServerDom.window.Plotly.validate(data, layout);\r\n\r\nif (result) {\r\n    console.log(result);\r\n}\r\nelse {\r\n    console.log(\"Validation ok\");\r\n}\r\n```\r\nAnd that works fine, and the validation is succesful.\r\n\r\nWe could use this latter program as a workaround, but it becomes harder when the number of nested levels increases.  Because all objects that we receive from anywhere in the NodeJs application, we need to create an new object for and copy all the properties.  Which is of course not a very neat solution ...\r\n\r\nSo we would like to ask if it is possible to remove the second condition from the IF statement, since we think that the first condition is enough:\r\n\r\n![image](https://user-images.githubusercontent.com/14224149/93268682-e3b64e80-f7ad-11ea-829c-0969c95d8044.png)\r\n\r\nThanks for your time!\r\nBart Butenaers\r\n\r\n","author":{"url":"https://github.com/bartbutenaers","@type":"Person","name":"bartbutenaers"},"datePublished":"2020-09-15T21:54:09.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":5},"url":"https://github.com/5151/plotly.js/issues/5151"}

route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:3ae35ef1-5bae-e772-cddd-b2bae1543137
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idC116:208386:34310C:45855B:6A5B2324
html-safe-noncebc8e7d84e2267037528e2fce8f155f3b3c4676976b2ac86794b8ba368d8afe8a
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDMTE2OjIwODM4NjozNDMxMEM6NDU4NTVCOjZBNUIyMzI0IiwidmlzaXRvcl9pZCI6IjYzOTU2OTY4NzY2OTI5MDY3ODgiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmacbfbf01d4ddfb2f234a6ded6b105fc3813bfa4a02e45fc354b5aa0041da04804e
hovercard-subject-tagissue:702299358
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/plotly/plotly.js/5151/issue_layout
twitter:imagehttps://opengraph.githubassets.com/86294ab953021da2209a7430bbd9e82fb0ac333b899ea84cd4f2bdbe1143cfbf/plotly/plotly.js/issues/5151
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/86294ab953021da2209a7430bbd9e82fb0ac333b899ea84cd4f2bdbe1143cfbf/plotly/plotly.js/issues/5151
og:image:altDear, we are integrating the powerful Plotly library into the open-source Node-RED iot framework, which is running on NodeJs. Running Plotly on the NodeJs server (via JsDom) works fine, but calling...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamebartbutenaers
hostnamegithub.com
expected-hostnamegithub.com
None5290d7e14309ad1e76106a9c4237bd1041517e83ea182c8ab756752cb0c6940b
turbo-cache-controlno-preview
go-importgithub.com/plotly/plotly.js git https://github.com/plotly/plotly.js.git
octolytics-dimension-user_id5997976
octolytics-dimension-user_loginplotly
octolytics-dimension-repository_id45646037
octolytics-dimension-repository_nwoplotly/plotly.js
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id45646037
octolytics-dimension-repository_network_root_nwoplotly/plotly.js
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release9c975978430e9ad293956f2bbdaf153b1bd84a99
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/plotly/plotly.js/issues/5151#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fplotly%2Fplotly.js%2Fissues%2F5151
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub Copilot appDirect agents from issue to mergehttps://github.com/features/ai/github-app
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
View all resourceshttps://github.com/resources
GitHub SponsorsFund open source developershttps://github.com/open-source/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/open-source/accelerator
GitHub Starshttps://stars.github.com
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://github.com/security/advanced-security
Copilot for BusinessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fplotly%2Fplotly.js%2Fissues%2F5151
Sign up https://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=plotly%2Fplotly.js
Reloadhttps://github.com/plotly/plotly.js/issues/5151
Reloadhttps://github.com/plotly/plotly.js/issues/5151
Reloadhttps://github.com/plotly/plotly.js/issues/5151
Please reload this pagehttps://github.com/plotly/plotly.js/issues/5151
plotly https://github.com/plotly
plotly.jshttps://github.com/plotly/plotly.js
Please reload this pagehttps://github.com/plotly/plotly.js/issues/5151
Notifications https://github.com/login?return_to=%2Fplotly%2Fplotly.js
Fork 2k https://github.com/login?return_to=%2Fplotly%2Fplotly.js
Star 18.3k https://github.com/login?return_to=%2Fplotly%2Fplotly.js
Code https://github.com/plotly/plotly.js
Issues 773 https://github.com/plotly/plotly.js/issues
Pull requests 58 https://github.com/plotly/plotly.js/pulls
Actions https://github.com/plotly/plotly.js/actions
Security and quality 0 https://github.com/plotly/plotly.js/security
Insights https://github.com/plotly/plotly.js/pulse
Code https://github.com/plotly/plotly.js
Issues https://github.com/plotly/plotly.js/issues
Pull requests https://github.com/plotly/plotly.js/pulls
Actions https://github.com/plotly/plotly.js/actions
Security and quality https://github.com/plotly/plotly.js/security
Insights https://github.com/plotly/plotly.js/pulse
#5411https://github.com/plotly/plotly.js/pull/5411
Validation fails when running via JsDom in NodeJshttps://github.com/plotly/plotly.js/issues/5151#top
#5411https://github.com/plotly/plotly.js/pull/5411
https://github.com/bartbutenaers
bartbutenaershttps://github.com/bartbutenaers
on Sep 15, 2020https://github.com/plotly/plotly.js/issues/5151#issue-702299358
isPlainObject https://github.com/plotly/plotly.js/blob/master/src/lib/is_plain_object.js#L23
https://user-images.githubusercontent.com/14224149/93267317-cbddcb00-f7ab-11ea-87d8-40a4cb3e0479.png
https://user-images.githubusercontent.com/14224149/93268682-e3b64e80-f7ad-11ea-829c-0969c95d8044.png
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.