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

Remove node-canvas from @jbrowse/core dependencies #3044

Merged
merged 2 commits into from
Jun 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
"@types/unzipper": "^0.10.3",
"@typescript-eslint/parser": "^5.8.0",
"babel-loader": "^8.2.5",
"canvas": "^2.9.1",
"chai": "^4.3.4",
"concurrently": "^6.4.0",
"core-js": "^3.19.3",
Expand Down
1 change: 0 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
"@babel/runtime": "^7.17.9",
"@material-ui/icons": "^4.0.1",
"abortable-promise-cache": "^1.5.0",
"canvas": "^2.8.0",
"canvas-sequencer": "^3.1.0",
"canvas2svg": "^1.0.16",
"clone": "^2.1.2",
Expand Down
16 changes: 9 additions & 7 deletions packages/core/util/offscreenCanvasPonyfill.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@ export function drawImageOntoCanvasContext(
if (imageData.serializedCommands) {
const seq = new CanvasSequence(imageData.serializedCommands)
seq.execute(context)
} else if (imageData instanceof ImageBitmapType) {
} else {
context.drawImage(imageData as CanvasImageSource, 0, 0)
} else if (imageData.dataURL) {
throw new Error('dataURL deserialization no longer supported')
}
}

Expand All @@ -45,23 +43,27 @@ if (weHave.realOffscreenCanvas) {
ImageBitmapType = window.ImageBitmap || self.ImageBitmap
} else if (weHave.node) {
// use node-canvas if we are running in node (i.e. automated tests)
const { createCanvas: nodeCreateCanvas, Image } = require('canvas')
createCanvas = nodeCreateCanvas
createCanvas = (...args) => {
// @ts-ignore
// eslint-disable-next-line no-undef
return nodeCreateCanvas(...args)
}
createImageBitmap = async (canvas, ...otherargs) => {
if (otherargs.length) {
throw new Error(
'only one-argument uses of createImageBitmap are supported by the node offscreencanvas ponyfill',
)
}
const dataUri = canvas.toDataURL()
const img = new Image()
// @ts-ignore
// eslint-disable-next-line no-undef
const img = new nodeImage()
return new Promise((resolve, reject) => {
img.onload = () => resolve(img)
img.onerror = reject
img.src = dataUri
})
}
ImageBitmapType = Image
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ImageBitmapType now not available statically (due to trying not to depend on initialization order). There could be alternative ways e.g. we make the ponyfill return a callback that gives us the createCanvas callback perhaps, but this is a bit intrusive.

} else {
createCanvas = (width: number, height: number) => {
const context = new CanvasSequence()
Expand Down
5 changes: 5 additions & 0 deletions plugins/wiggle/src/DensityRenderer/DensityRenderer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import SimpleFeature from '@jbrowse/core/util/simpleFeature'
import { renderToAbstractCanvas } from '@jbrowse/core/util/offscreenCanvasUtils'
import DensityRenderer, { configSchema, ReactComponent } from '.'

import { Image, createCanvas } from 'canvas'

global.nodeImage = Image
global.nodeCreateCanvas = createCanvas

const pluginManager = {}
const renderer = new DensityRenderer({
name: 'DensityRenderer',
Expand Down
5 changes: 5 additions & 0 deletions plugins/wiggle/src/XYPlotRenderer/XYPlotRenderer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import SimpleFeature from '@jbrowse/core/util/simpleFeature'
import { renderToAbstractCanvas } from '@jbrowse/core/util/offscreenCanvasUtils'
import XYPlotRenderer, { configSchema, ReactComponent } from '.'

import { Image, createCanvas } from 'canvas'

global.nodeImage = Image
global.nodeCreateCanvas = createCanvas

test('several features', async () => {
const pluginManager = {}
const renderer = new XYPlotRenderer({
Expand Down
1 change: 1 addition & 0 deletions products/jbrowse-img/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@jbrowse/react-linear-genome-view": "^1.7.10",
"abortcontroller-polyfill": "^1.7.3",
"jsdom": "^19.0.0",
"canvas": "^2.9.1",
"mobx": "^5.10.1",
"node-fetch": "^2.6.7",
"react": "^17.0.1",
Expand Down
4 changes: 4 additions & 0 deletions products/jbrowse-img/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import 'abortcontroller-polyfill/dist/abortcontroller-polyfill-only'
import { spawnSync } from 'child_process'
import fetch, { Headers, Response, Request } from 'node-fetch'
import { JSDOM } from 'jsdom'
import { Image, createCanvas } from 'canvas'

global.nodeImage = Image
global.nodeCreateCanvas = createCanvas

const { document } = new JSDOM(`...`).window
global.document = document
Expand Down
4 changes: 4 additions & 0 deletions products/jbrowse-img/src/index.testmod.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ import { renderRegion } from './renderRegion'
import fs from 'fs'
import { JSDOM } from 'jsdom'
import 'abortcontroller-polyfill/dist/abortcontroller-polyfill-only'
import { Image, createCanvas } from 'canvas'

const { document } = new JSDOM(`...`).window
global.document = document

global.nodeImage = Image
global.nodeCreateCanvas = createCanvas

function hashCode(str) {
let hash = 0
let chr
Expand Down
6 changes: 6 additions & 0 deletions products/jbrowse-web/src/tests/Loader.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@ import { render, waitFor } from '@testing-library/react'
import { TextEncoder, TextDecoder } from 'web-encoding'
import { LocalFile } from 'generic-filehandle'
import rangeParser from 'range-parser'
import { Image, createCanvas } from 'canvas'

import { QueryParamProvider } from 'use-query-params'

import { Loader } from '../Loader'

// @ts-ignore
global.nodeImage = Image
// @ts-ignore
global.nodeCreateCanvas = createCanvas

jest.mock('../makeWorkerInstance', () => () => {})

const delay = { timeout: 20000 }
Expand Down
6 changes: 5 additions & 1 deletion products/jbrowse-web/src/tests/util.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React from 'react'

import rangeParser from 'range-parser'
import PluginManager from '@jbrowse/core/PluginManager'
import { QueryParamProvider } from 'use-query-params'
Expand All @@ -8,8 +7,13 @@ import JBrowseWithoutQueryParamProvider from '../JBrowse'
import JBrowseRootModelFactory from '../rootModel'
import configSnapshot from '../../test_data/volvox/config.json'
import corePlugins from '../corePlugins'
import { Image, createCanvas } from 'canvas'

jest.mock('../makeWorkerInstance', () => () => {})

global.nodeImage = Image
global.nodeCreateCanvas = createCanvas

configSnapshot.configuration = {
rpc: {
defaultDriver: 'MainThreadRpcDriver',
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6662,7 +6662,7 @@ canvas2svg@^1.0.16:
resolved "https://registry.yarnpkg.com/canvas2svg/-/canvas2svg-1.0.16.tgz#0814c53bbab7c3406e7387279cdf257fe4f6f2bd"
integrity sha512-r3ryHprzDOtAsFuczw+/DKkLR3XexwIlJWnJ+71I9QF7V9scYaV5JZgYDoCUlYtT3ARnOpDcm/hDNZYbWMRHqA==

canvas@^2.8.0:
canvas@^2.9.1:
version "2.9.1"
resolved "https://registry.yarnpkg.com/canvas/-/canvas-2.9.1.tgz#58ec841cba36cef0675bc7a74ebd1561f0b476b0"
integrity sha512-vSQti1uG/2gjv3x6QLOZw7TctfufaerTWbVe+NSduHxxLGB+qf3kFgQ6n66DSnuoINtVUjrLLIK2R+lxrBG07A==
Expand Down