|
| 1 | +const { existsSync, readFileSync } = require('fs') |
| 2 | +const { join } = require('path') |
| 3 | + |
| 4 | +const { platform, arch } = process |
| 5 | + |
| 6 | +let nativeBinding = null |
| 7 | +let localFileExisted = false |
| 8 | +let isMusl = false |
| 9 | +let loadError = null |
| 10 | + |
| 11 | +switch (platform) { |
| 12 | + case 'android': |
| 13 | + if (arch !== 'arm64') { |
| 14 | + throw new Error(`Unsupported architecture on Android ${arch}`) |
| 15 | + } |
| 16 | + localFileExisted = existsSync(join(__dirname, 'skia.android-arm64.node')) |
| 17 | + try { |
| 18 | + if (localFileExisted) { |
| 19 | + nativeBinding = require('./skia.android-arm64.node') |
| 20 | + } else { |
| 21 | + nativeBinding = require('@napi-rs/canvas-android-arm64') |
| 22 | + } |
| 23 | + } catch (e) { |
| 24 | + loadError = e |
| 25 | + } |
| 26 | + break |
| 27 | + case 'win32': |
| 28 | + // eslint-disable-next-line sonarjs/no-nested-switch |
| 29 | + switch (arch) { |
| 30 | + case 'x64': |
| 31 | + localFileExisted = existsSync(join(__dirname, 'skia.win32-x64-msvc.node')) |
| 32 | + try { |
| 33 | + if (localFileExisted) { |
| 34 | + nativeBinding = require('./skia.win32-x64-msvc.node') |
| 35 | + } else { |
| 36 | + nativeBinding = require('@napi-rs/canvas-win32-x64-msvc') |
| 37 | + } |
| 38 | + } catch (e) { |
| 39 | + loadError = e |
| 40 | + } |
| 41 | + break |
| 42 | + case 'ia32': |
| 43 | + localFileExisted = existsSync(join(__dirname, 'skia.win32-ia32-msvc.node')) |
| 44 | + try { |
| 45 | + if (localFileExisted) { |
| 46 | + nativeBinding = require('./skia.win32-ia32-msvc.node') |
| 47 | + } else { |
| 48 | + nativeBinding = require('@napi-rs/canvas-win32-ia32-msvc') |
| 49 | + } |
| 50 | + } catch (e) { |
| 51 | + loadError = e |
| 52 | + } |
| 53 | + break |
| 54 | + case 'arm64': |
| 55 | + localFileExisted = existsSync(join(__dirname, 'skia.win32-arm64-msvc.node')) |
| 56 | + try { |
| 57 | + if (localFileExisted) { |
| 58 | + nativeBinding = require('./skia.win32-arm64-msvc.node') |
| 59 | + } else { |
| 60 | + nativeBinding = require('@napi-rs/canvas-win32-arm64-msvc') |
| 61 | + } |
| 62 | + } catch (e) { |
| 63 | + loadError = e |
| 64 | + } |
| 65 | + break |
| 66 | + default: |
| 67 | + throw new Error(`Unsupported architecture on Windows: ${arch}`) |
| 68 | + } |
| 69 | + break |
| 70 | + case 'darwin': |
| 71 | + // eslint-disable-next-line sonarjs/no-nested-switch |
| 72 | + switch (arch) { |
| 73 | + case 'x64': |
| 74 | + localFileExisted = existsSync(join(__dirname, 'skia.darwin-x64.node')) |
| 75 | + try { |
| 76 | + if (localFileExisted) { |
| 77 | + nativeBinding = require('./skia.darwin-x64.node') |
| 78 | + } else { |
| 79 | + nativeBinding = require('@napi-rs/canvas-darwin-x64') |
| 80 | + } |
| 81 | + } catch (e) { |
| 82 | + loadError = e |
| 83 | + } |
| 84 | + break |
| 85 | + case 'arm64': |
| 86 | + localFileExisted = existsSync(join(__dirname, 'skia.darwin-arm64.node')) |
| 87 | + try { |
| 88 | + if (localFileExisted) { |
| 89 | + nativeBinding = require('./skia.darwin-arm64.node') |
| 90 | + } else { |
| 91 | + nativeBinding = require('@napi-rs/canvas-darwin-arm64') |
| 92 | + } |
| 93 | + } catch (e) { |
| 94 | + loadError = e |
| 95 | + } |
| 96 | + break |
| 97 | + default: |
| 98 | + throw new Error(`Unsupported architecture on macOS: ${arch}`) |
| 99 | + } |
| 100 | + break |
| 101 | + case 'freebsd': |
| 102 | + if (arch !== 'x64') { |
| 103 | + throw new Error(`Unsupported architecture on FreeBSD: ${arch}`) |
| 104 | + } |
| 105 | + localFileExisted = existsSync(join(__dirname, 'skia.freebsd-x64.node')) |
| 106 | + try { |
| 107 | + if (localFileExisted) { |
| 108 | + nativeBinding = require('./skia.freebsd-x64.node') |
| 109 | + } else { |
| 110 | + nativeBinding = require('@napi-rs/canvas-freebsd-x64') |
| 111 | + } |
| 112 | + } catch (e) { |
| 113 | + loadError = e |
| 114 | + } |
| 115 | + break |
| 116 | + case 'linux': |
| 117 | + // eslint-disable-next-line sonarjs/no-nested-switch |
| 118 | + switch (arch) { |
| 119 | + case 'x64': |
| 120 | + isMusl = readFileSync('/usr/bin/ldd', 'utf8').includes('musl') |
| 121 | + if (isMusl) { |
| 122 | + localFileExisted = existsSync(join(__dirname, 'skia.linux-x64-musl.node')) |
| 123 | + try { |
| 124 | + if (localFileExisted) { |
| 125 | + nativeBinding = require('./skia.linux-x64-musl.node') |
| 126 | + } else { |
| 127 | + nativeBinding = require('@napi-rs/canvas-linux-x64-musl') |
| 128 | + } |
| 129 | + } catch (e) { |
| 130 | + loadError = e |
| 131 | + } |
| 132 | + } else { |
| 133 | + localFileExisted = existsSync(join(__dirname, 'skia.linux-x64-gnu.node')) |
| 134 | + try { |
| 135 | + if (localFileExisted) { |
| 136 | + nativeBinding = require('./skia.linux-x64-gnu.node') |
| 137 | + } else { |
| 138 | + nativeBinding = require('@napi-rs/canvas-linux-x64-gnu') |
| 139 | + } |
| 140 | + } catch (e) { |
| 141 | + loadError = e |
| 142 | + } |
| 143 | + } |
| 144 | + break |
| 145 | + case 'arm64': |
| 146 | + isMusl = readFileSync('/usr/bin/ldd', 'utf8').includes('musl') |
| 147 | + if (isMusl) { |
| 148 | + localFileExisted = existsSync(join(__dirname, 'skia.linux-arm64-musl.node')) |
| 149 | + try { |
| 150 | + if (localFileExisted) { |
| 151 | + nativeBinding = require('./skia.linux-arm64-musl.node') |
| 152 | + } else { |
| 153 | + nativeBinding = require('@napi-rs/canvas-linux-arm64-musl') |
| 154 | + } |
| 155 | + } catch (e) { |
| 156 | + loadError = e |
| 157 | + } |
| 158 | + } else { |
| 159 | + localFileExisted = existsSync(join(__dirname, 'skia.linux-arm64-gnu.node')) |
| 160 | + try { |
| 161 | + if (localFileExisted) { |
| 162 | + nativeBinding = require('./skia.linux-arm64-gnu.node') |
| 163 | + } else { |
| 164 | + nativeBinding = require('@napi-rs/canvas-linux-arm64-gnu') |
| 165 | + } |
| 166 | + } catch (e) { |
| 167 | + loadError = e |
| 168 | + } |
| 169 | + } |
| 170 | + break |
| 171 | + case 'arm': |
| 172 | + localFileExisted = existsSync(join(__dirname, 'skia.linux-arm-gnueabihf.node')) |
| 173 | + try { |
| 174 | + if (localFileExisted) { |
| 175 | + nativeBinding = require('./skia.linux-arm-gnueabihf.node') |
| 176 | + } else { |
| 177 | + nativeBinding = require('@napi-rs/canvas-linux-arm-gnueabihf') |
| 178 | + } |
| 179 | + } catch (e) { |
| 180 | + loadError = e |
| 181 | + } |
| 182 | + break |
| 183 | + default: |
| 184 | + throw new Error(`Unsupported architecture on Linux: ${arch}`) |
| 185 | + } |
| 186 | + break |
| 187 | + default: |
| 188 | + throw new Error(`Unsupported OS: ${platform}, architecture: ${arch}`) |
| 189 | +} |
| 190 | + |
| 191 | +if (!nativeBinding) { |
| 192 | + if (loadError) { |
| 193 | + throw loadError |
| 194 | + } |
| 195 | + throw new Error(`Failed to load native binding`) |
| 196 | +} |
| 197 | + |
| 198 | +const { |
| 199 | + CanvasRenderingContext2D, |
| 200 | + CanvasElement, |
| 201 | + createContext, |
| 202 | + SVGCanvas, |
| 203 | + Path2D, |
| 204 | + ImageData, |
| 205 | + Image, |
| 206 | + CanvasPattern, |
| 207 | + GlobalFonts, |
| 208 | + convertSVGTextToPath, |
| 209 | +} = nativeBinding |
| 210 | + |
| 211 | +module.exports.CanvasRenderingContext2D = CanvasRenderingContext2D |
| 212 | +module.exports.CanvasElement = CanvasElement |
| 213 | +module.exports.createContext = createContext |
| 214 | +module.exports.SVGCanvas = SVGCanvas |
| 215 | +module.exports.Path2D = Path2D |
| 216 | +module.exports.ImageData = ImageData |
| 217 | +module.exports.Image = Image |
| 218 | +module.exports.CanvasPattern = CanvasPattern |
| 219 | +module.exports.GlobalFonts = GlobalFonts |
| 220 | +module.exports.convertSVGTextToPath = convertSVGTextToPath |
0 commit comments