From 426e140048383fda85b16cf07fdba81d68a016b2 Mon Sep 17 00:00:00 2001 From: noClaps <04plugs-bios@icloud.com> Date: Fri, 3 Jan 2025 18:57:18 +0000 Subject: [PATCH] Don't strip from release --- browser.js | 1 + highlight.test.ts | 2 +- index.d.ts | 3 + index.js | 367 ++++++++++++++++++++++++++++++++++++++++++++++ main.ts | 2 +- package.json | 26 ++-- 6 files changed, 386 insertions(+), 15 deletions(-) create mode 100644 browser.js create mode 100644 index.d.ts create mode 100644 index.js diff --git a/browser.js b/browser.js new file mode 100644 index 0000000..d263193 --- /dev/null +++ b/browser.js @@ -0,0 +1 @@ +export * from '@noclaps/highlight-wasm32-wasi' diff --git a/highlight.test.ts b/highlight.test.ts index aa7b021..399ac9b 100644 --- a/highlight.test.ts +++ b/highlight.test.ts @@ -1,4 +1,4 @@ -import { highlight, type BundledLanguage, type Theme } from "./index.ts"; +import { highlight, type BundledLanguage, type Theme } from "./main.ts"; const theme: Theme = { fg: "#fff", diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..a8e9791 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,3 @@ +/* auto-generated by NAPI-RS */ +/* eslint-disable */ +export declare function highlight(highlightNames: Array, language: string, code: string): string diff --git a/index.js b/index.js new file mode 100644 index 0000000..e44df76 --- /dev/null +++ b/index.js @@ -0,0 +1,367 @@ +// prettier-ignore +/* eslint-disable */ +// @ts-nocheck +/* auto-generated by NAPI-RS */ + +const { createRequire } = require('node:module') +require = createRequire(__filename) + +const { readFileSync } = require('node:fs') +let nativeBinding = null +const loadErrors = [] + +const isMusl = () => { + let musl = false + if (process.platform === 'linux') { + musl = isMuslFromFilesystem() + if (musl === null) { + musl = isMuslFromReport() + } + if (musl === null) { + musl = isMuslFromChildProcess() + } + } + return musl +} + +const isFileMusl = (f) => f.includes('libc.musl-') || f.includes('ld-musl-') + +const isMuslFromFilesystem = () => { + try { + return readFileSync('/usr/bin/ldd', 'utf-8').includes('musl') + } catch { + return null + } +} + +const isMuslFromReport = () => { + const report = typeof process.report.getReport === 'function' ? process.report.getReport() : null + if (!report) { + return null + } + if (report.header && report.header.glibcVersionRuntime) { + return false + } + if (Array.isArray(report.sharedObjects)) { + if (report.sharedObjects.some(isFileMusl)) { + return true + } + } + return false +} + +const isMuslFromChildProcess = () => { + try { + return require('child_process').execSync('ldd --version', { encoding: 'utf8' }).includes('musl') + } catch (e) { + // If we reach this case, we don't know if the system is musl or not, so is better to just fallback to false + return false + } +} + +function requireNative() { + if (process.platform === 'android') { + if (process.arch === 'arm64') { + try { + return require('./highlight.android-arm64.node') + } catch (e) { + loadErrors.push(e) + } + try { + return require('@noclaps/highlight-android-arm64') + } catch (e) { + loadErrors.push(e) + } + + } else if (process.arch === 'arm') { + try { + return require('./highlight.android-arm-eabi.node') + } catch (e) { + loadErrors.push(e) + } + try { + return require('@noclaps/highlight-android-arm-eabi') + } catch (e) { + loadErrors.push(e) + } + + } else { + loadErrors.push(new Error(`Unsupported architecture on Android ${process.arch}`)) + } + } else if (process.platform === 'win32') { + if (process.arch === 'x64') { + try { + return require('./highlight.win32-x64-msvc.node') + } catch (e) { + loadErrors.push(e) + } + try { + return require('@noclaps/highlight-win32-x64-msvc') + } catch (e) { + loadErrors.push(e) + } + + } else if (process.arch === 'ia32') { + try { + return require('./highlight.win32-ia32-msvc.node') + } catch (e) { + loadErrors.push(e) + } + try { + return require('@noclaps/highlight-win32-ia32-msvc') + } catch (e) { + loadErrors.push(e) + } + + } else if (process.arch === 'arm64') { + try { + return require('./highlight.win32-arm64-msvc.node') + } catch (e) { + loadErrors.push(e) + } + try { + return require('@noclaps/highlight-win32-arm64-msvc') + } catch (e) { + loadErrors.push(e) + } + + } else { + loadErrors.push(new Error(`Unsupported architecture on Windows: ${process.arch}`)) + } + } else if (process.platform === 'darwin') { + try { + return require('./highlight.darwin-universal.node') + } catch (e) { + loadErrors.push(e) + } + try { + return require('@noclaps/highlight-darwin-universal') + } catch (e) { + loadErrors.push(e) + } + + if (process.arch === 'x64') { + try { + return require('./highlight.darwin-x64.node') + } catch (e) { + loadErrors.push(e) + } + try { + return require('@noclaps/highlight-darwin-x64') + } catch (e) { + loadErrors.push(e) + } + + } else if (process.arch === 'arm64') { + try { + return require('./highlight.darwin-arm64.node') + } catch (e) { + loadErrors.push(e) + } + try { + return require('@noclaps/highlight-darwin-arm64') + } catch (e) { + loadErrors.push(e) + } + + } else { + loadErrors.push(new Error(`Unsupported architecture on macOS: ${process.arch}`)) + } + } else if (process.platform === 'freebsd') { + if (process.arch === 'x64') { + try { + return require('./highlight.freebsd-x64.node') + } catch (e) { + loadErrors.push(e) + } + try { + return require('@noclaps/highlight-freebsd-x64') + } catch (e) { + loadErrors.push(e) + } + + } else if (process.arch === 'arm64') { + try { + return require('./highlight.freebsd-arm64.node') + } catch (e) { + loadErrors.push(e) + } + try { + return require('@noclaps/highlight-freebsd-arm64') + } catch (e) { + loadErrors.push(e) + } + + } else { + loadErrors.push(new Error(`Unsupported architecture on FreeBSD: ${process.arch}`)) + } + } else if (process.platform === 'linux') { + if (process.arch === 'x64') { + if (isMusl()) { + try { + return require('./highlight.linux-x64-musl.node') + } catch (e) { + loadErrors.push(e) + } + try { + return require('@noclaps/highlight-linux-x64-musl') + } catch (e) { + loadErrors.push(e) + } + + } else { + try { + return require('./highlight.linux-x64-gnu.node') + } catch (e) { + loadErrors.push(e) + } + try { + return require('@noclaps/highlight-linux-x64-gnu') + } catch (e) { + loadErrors.push(e) + } + + } + } else if (process.arch === 'arm64') { + if (isMusl()) { + try { + return require('./highlight.linux-arm64-musl.node') + } catch (e) { + loadErrors.push(e) + } + try { + return require('@noclaps/highlight-linux-arm64-musl') + } catch (e) { + loadErrors.push(e) + } + + } else { + try { + return require('./highlight.linux-arm64-gnu.node') + } catch (e) { + loadErrors.push(e) + } + try { + return require('@noclaps/highlight-linux-arm64-gnu') + } catch (e) { + loadErrors.push(e) + } + + } + } else if (process.arch === 'arm') { + if (isMusl()) { + try { + return require('./highlight.linux-arm-musleabihf.node') + } catch (e) { + loadErrors.push(e) + } + try { + return require('@noclaps/highlight-linux-arm-musleabihf') + } catch (e) { + loadErrors.push(e) + } + + } else { + try { + return require('./highlight.linux-arm-gnueabihf.node') + } catch (e) { + loadErrors.push(e) + } + try { + return require('@noclaps/highlight-linux-arm-gnueabihf') + } catch (e) { + loadErrors.push(e) + } + + } + } else if (process.arch === 'riscv64') { + if (isMusl()) { + try { + return require('./highlight.linux-riscv64-musl.node') + } catch (e) { + loadErrors.push(e) + } + try { + return require('@noclaps/highlight-linux-riscv64-musl') + } catch (e) { + loadErrors.push(e) + } + + } else { + try { + return require('./highlight.linux-riscv64-gnu.node') + } catch (e) { + loadErrors.push(e) + } + try { + return require('@noclaps/highlight-linux-riscv64-gnu') + } catch (e) { + loadErrors.push(e) + } + + } + } else if (process.arch === 'ppc64') { + try { + return require('./highlight.linux-ppc64-gnu.node') + } catch (e) { + loadErrors.push(e) + } + try { + return require('@noclaps/highlight-linux-ppc64-gnu') + } catch (e) { + loadErrors.push(e) + } + + } else if (process.arch === 's390x') { + try { + return require('./highlight.linux-s390x-gnu.node') + } catch (e) { + loadErrors.push(e) + } + try { + return require('@noclaps/highlight-linux-s390x-gnu') + } catch (e) { + loadErrors.push(e) + } + + } else { + loadErrors.push(new Error(`Unsupported architecture on Linux: ${process.arch}`)) + } + } else { + loadErrors.push(new Error(`Unsupported OS: ${process.platform}, architecture: ${process.arch}`)) + } +} + +nativeBinding = requireNative() + +if (!nativeBinding || process.env.NAPI_RS_FORCE_WASI) { + try { + nativeBinding = require('./highlight.wasi.cjs') + } catch (err) { + if (process.env.NAPI_RS_FORCE_WASI) { + loadErrors.push(err) + } + } + if (!nativeBinding) { + try { + nativeBinding = require('@noclaps/highlight-wasm32-wasi') + } catch (err) { + if (process.env.NAPI_RS_FORCE_WASI) { + loadErrors.push(err) + } + } + } +} + +if (!nativeBinding) { + if (loadErrors.length > 0) { + // TODO Link to documentation with potential fixes + // - The package owner could build/publish bindings for this arch + // - The user may need to bundle the correct files + // - The user may need to re-install node_modules to get new packages + throw new Error('Failed to load native binding', { cause: loadErrors }) + } + throw new Error(`Failed to load native binding`) +} + +module.exports.highlight = nativeBinding.highlight diff --git a/main.ts b/main.ts index 2ac5340..53ac6ff 100644 --- a/main.ts +++ b/main.ts @@ -1,4 +1,4 @@ -import { highlight as tsHighlight } from "./dist/index.js"; +import { highlight as tsHighlight } from "./index.js"; function escapeHTML(input: string): string { return input diff --git a/package.json b/package.json index 18c104b..52616f0 100644 --- a/package.json +++ b/package.json @@ -8,23 +8,23 @@ "optionalDependencies": { "@noclaps/highlight-wasm32-wasi": "^0.3.2" }, - "scripts": { - "artifacts": "napi artifacts -o .", - "build": "napi build --platform --release --strip", - "bump": "bunx changelogen --bump && bun bump-version.ts" - }, - "napi": { - "binaryName": "highlight", - "targets": [ - "wasm32-wasip1" - ] - }, - "type": "module", "exports": "./main.ts", "files": [ "main.ts", "LICENSE", "README.md", "package.json" - ] + ], + "napi": { + "binaryName": "highlight", + "targets": [ + "wasm32-wasip1" + ] + }, + "scripts": { + "artifacts": "napi artifacts -o .", + "build": "napi build --platform --release", + "bump": "bunx changelogen --bump && bun bump-version.ts" + }, + "type": "module" }