chore: add npm packages (#92)

This commit is contained in:
Fathy Boundjadj 2023-02-09 10:43:26 +01:00 committed by GitHub
parent 65bb7d4e08
commit 7887952e19
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 234 additions and 40 deletions

5
package.json Normal file
View File

@ -0,0 +1,5 @@
{
"name": "carbonyl",
"version": "0.0.1",
"license": "BSD-3-Clause"
}

View File

@ -25,19 +25,27 @@ Carbonyl originally started as [`html2svg`](https://github.com/fathyb/html2svg)
## Usage
> Carbonyl on Linux without Docker requires the same dependencies as Chromium.
### Docker
```shell
# Watch YouTube inside a Docker container
$ docker run -ti fathyb/carbonyl https://youtube.com
```
### Download
### npm
- [macOS amd64](https://refloat.ci/artifact/fathyb/carbonyl/59/carbonyl.macos-amd64.zip)
- [macOS arm64](https://refloat.ci/artifact/fathyb/carbonyl/59/carbonyl.macos-arm64.zip)
- [Linux amd64](https://refloat.ci/artifact/fathyb/carbonyl/59/carbonyl.linux-amd64.zip)
- [Linux arm64](https://refloat.ci/artifact/fathyb/carbonyl/59/carbonyl.linux-arm64.zip)
```console
$ npm install --global carbonyl
$ carbonyl https://github.com
```
Carbonyl on Linux requires the same dependencies as Chromium.
### Binaries
- [macOS amd64](https://refloat.ci/artifact/fathyb/carbonyl/59/carbonyl.macos-amd64.zip)
- [macOS arm64](https://refloat.ci/artifact/fathyb/carbonyl/59/carbonyl.macos-arm64.zip)
- [Linux amd64](https://refloat.ci/artifact/fathyb/carbonyl/59/carbonyl.linux-amd64.zip)
- [Linux arm64](https://refloat.ci/artifact/fathyb/carbonyl/59/carbonyl.linux-arm64.zip)
## Demo
@ -61,7 +69,7 @@ Carbonyl on Linux requires the same dependencies as Chromium.
## Known issues
- Fullscreen mode not supported yet
- Fullscreen mode not supported yet
## Comparisons
@ -71,15 +79,15 @@ Lynx is the original terminal web browser, and the oldest one still maintained.
#### Pros
- When it understands a page, Lynx has the best layout, fully optimized for the terminal
- When it understands a page, Lynx has the best layout, fully optimized for the terminal
#### Cons
> Some might sound like pluses, but Browsh and Carbonyl let you disable most of those if you'd like
- Does not support a lot of modern web standards
- Cannot run JavaScript/WebAssembly
- Cannot view or play media (audio, video, DOOM)
- Does not support a lot of modern web standards
- Cannot run JavaScript/WebAssembly
- Cannot view or play media (audio, video, DOOM)
### Browsh
@ -87,22 +95,22 @@ Browsh is the original "normal browser into a terminal" project. It starts Firef
#### Pro
- It's easier to update the underlying browser: just update Firefox
- This makes development easier: just install Firefox and compile the Go code in a few seconds
- As of today, Browsh supports extensions while Carbonyl doesn't, although it's on our roadmap
- It's easier to update the underlying browser: just update Firefox
- This makes development easier: just install Firefox and compile the Go code in a few seconds
- As of today, Browsh supports extensions while Carbonyl doesn't, although it's on our roadmap
#### Cons
- It runs slower and requires more resources than Carbonyl. 50x more CPU power is needed for the same content in average, that's because Carbonyl does not downscale or copy the window framebuffer, it natively renders to the terminal resolution.
- It uses custom stylesheets to fix the layout, which is less reliable than Carbonyl's changes to its HTML engine (Blink).
- It runs slower and requires more resources than Carbonyl. 50x more CPU power is needed for the same content in average, that's because Carbonyl does not downscale or copy the window framebuffer, it natively renders to the terminal resolution.
- It uses custom stylesheets to fix the layout, which is less reliable than Carbonyl's changes to its HTML engine (Blink).
## Operating System Support
As far as tested, the operating systems under are supported:
- Linux (Debian, Ubuntu and Arch tested)
- MacOS
- Windows 11 and WSL
- Linux (Debian, Ubuntu and Arch tested)
- MacOS
- Windows 11 and WSL
## Contributing
@ -122,10 +130,10 @@ $ cargo build
Few notes:
- Building the runtime is almost the same as building Chromium with extra steps to patch and bundle the Rust library. Scripts in the `scripts/` directory are simple wrappers around `gn`, `ninja`, etc..
- Building Chromium for arm64 on Linux requires an amd64 processor
- Carbonyl is only tested on Linux and macOS, other platforms likely require code changes to Chromium
- Chromium is huge and takes a long time to build, making your computer mostly unresponsive. An 8-core CPU such as an M1 Max or an i9 9900k with 10 Gbps fiber takes around ~1 hour to fetch and build. It requires around 100 GB of disk space.
- Building the runtime is almost the same as building Chromium with extra steps to patch and bundle the Rust library. Scripts in the `scripts/` directory are simple wrappers around `gn`, `ninja`, etc..
- Building Chromium for arm64 on Linux requires an amd64 processor
- Carbonyl is only tested on Linux and macOS, other platforms likely require code changes to Chromium
- Chromium is huge and takes a long time to build, making your computer mostly unresponsive. An 8-core CPU such as an M1 Max or an i9 9900k with 10 Gbps fiber takes around ~1 hour to fetch and build. It requires around 100 GB of disk space.
#### Fetch
@ -186,11 +194,11 @@ $ ./scripts/build.sh Default
This should produce the following outputs:
- `out/Default/headless_shell`: browser binary
- `out/Default/icudtl.dat`
- `out/Default/libEGL.so`
- `out/Default/libGLESv2.so`
- `out/Default/v8_context_snapshot.bin`
- `out/Default/headless_shell`: browser binary
- `out/Default/icudtl.dat`
- `out/Default/libEGL.so`
- `out/Default/libGLESv2.so`
- `out/Default/v8_context_snapshot.bin`
#### Build Docker image

146
scripts/npm-publish.mjs Normal file
View File

@ -0,0 +1,146 @@
import fs from 'fs/promises'
import path from 'path'
import { fileURLToPath } from 'url'
const dirname = path.dirname(fileURLToPath(import.meta.url))
const pkg = JSON.parse(await fs.readFile(path.resolve(dirname, '../package.json'), 'utf-8'))
const version = `${pkg.version}-next.${process.env.VERSION_ID}`
const manifest = {
version,
license: 'BSD-3-Clause',
description: 'Chromium running in your terminal',
homepage: 'https://github.com/fathyb/carbonyl',
repository: 'fathyb/carbonyl',
bugs: 'https://github.com/fathyb/carbonyl/issues',
author: {
name: 'Fathy Boundjadj',
email: 'hey@fathy.fr',
url: 'https://fathy.fr'
}
}
async function buildMain() {
const root = path.resolve(dirname, '../build/packages/carbonyl')
await fs.rm(root, { recursive: true, force: true })
await fs.mkdir(root, { recursive: true })
await Promise.all([
Promise.all(
['readme.md', 'license.md'].map(file =>
fs.cp(path.join(dirname, '..', file), path.join(root, file)),
)
),
fs.writeFile(
path.join(root, 'package.json'),
JSON.stringify(
{
name: 'carbonyl',
...manifest,
files: ['index.js', 'index.sh'],
bin: { carbonyl: 'index.sh' },
optionalDependencies: {
'@fathyb/carbonyl-linux-amd64': version,
'@fathyb/carbonyl-linux-arm64': version,
'@fathyb/carbonyl-macos-amd64': version,
'@fathyb/carbonyl-macos-arm64': version
}
},
null,
4
)
),
fs.writeFile(
path.join(root, 'index.js'),
`
function tryModule(name) {
try {
return require(name)
} catch {
return null
}
}
const path = (
tryModule('@fathyb/carbonyl-linux-amd64') ||
tryModule('@fathyb/carbonyl-linux-arm64') ||
tryModule('@fathyb/carbonyl-macos-amd64') ||
tryModule('@fathyb/carbonyl-macos-arm64')
)
if (path) {
module.exports = path
} else {
throw new Error('Could not find a Carbonyl runtime installed')
}
`
),
fs.writeFile(
path.join(root, 'index.sh'),
[
'#!/usr/bin/env bash',
`"$(node -p "require('@fathyb/carbonyl')")" "$@"`
].join('\n'),
{ mode: '755' }
)
])
return root
}
async function buildPlatform([os, npmOs, llvmOs], [cpu, npmCpu, llvmCpu]) {
const pkg = `carbonyl-${os}-${cpu}`
const root = path.resolve(dirname, `../build/packages/${pkg}`)
await fs.rm(root, { recursive: true, force: true })
await fs.mkdir(root, { recursive: true })
await Promise.all([
Promise.all(
['readme.md', 'license.md'].map(file =>
fs.cp(path.join(dirname, '..', file), path.join(root, file)),
)
),
fs.cp(
path.join(dirname, `../build/pre-built/${llvmCpu}-${llvmOs}`),
path.join(root, 'build'),
{ recursive: true }
),
fs.writeFile(
path.join(root, 'package.json'),
JSON.stringify(
{
name: `@fathyb/${pkg}`,
...manifest,
files: ['build', 'index.js'],
os: [npmOs],
cpu: [npmCpu],
},
null,
4
)
),
fs.writeFile(
path.join(root, 'index.js'),
`module.exports = __dirname + '/build/carbonyl'`
)
])
return root
}
const [root, platforms] = await Promise.all([
buildMain(),
Promise.all([
['macos', 'darwin', 'apple-darwin'],
['linux', 'linux', 'unknown-linux-gnu']
].map(async (os) =>
await Promise.all(
[
['arm64', 'arm64', 'aarch64'],
['amd64', 'x64', 'x86_64']
].map(async (cpu) => await buildPlatform(os, cpu))
)
)),
])

32
scripts/npm-publish.sh Executable file
View File

@ -0,0 +1,32 @@
#!/usr/bin/env bash
export CARBONYL_ROOT=$(cd $(dirname -- "$0") && dirname -- $(pwd))
export SKIP_DEPOT_TOOLS="true"
cd "$CARBONYL_ROOT"
source "scripts/env.sh"
"$CARBONYL_ROOT/scripts/runtime-pull.sh" aarch64-unknown-linux-gnu
"$CARBONYL_ROOT/scripts/runtime-pull.sh" x86_64-unknown-linux-gnu
"$CARBONYL_ROOT/scripts/runtime-pull.sh" aarch64-apple-darwin
"$CARBONYL_ROOT/scripts/runtime-pull.sh" x86_64-apple-darwin
VERSION_ID="$(git rev-parse --short HEAD)" \
node "$CARBONYL_ROOT/scripts/npm-publish.mjs"
cd "$CARBONYL_ROOT/build/packages"
cd carbonyl-linux-amd64
yarn publish --non-interactive --access public "$@"
cd ../carbonyl-linux-arm64
yarn publish --non-interactive --access public "$@"
cd ../carbonyl-macos-amd64
yarn publish --non-interactive --access public "$@"
cd ../carbonyl-macos-arm64
yarn publish --non-interactive --access public "$@"
cd ../carbonyl
yarn publish --non-interactive --access public "$@"

View File

@ -11,20 +11,23 @@ echo "Computing Chromium patches sha.."
sha=$(scripts/runtime-hash.sh)
triple="$1"
url="https://carbonyl.fathy.fr/runtime/$sha/$triple.tgz"
echo "Downloading pre-built binaries from $url"
if [ ! -f "build/pre-built/$triple.tgz" ]; then
url="https://carbonyl.fathy.fr/runtime/$sha/$triple.tgz"
mkdir -p build/pre-built
echo "Downloading pre-built binaries from $url"
if ! curl --silent --fail --output "build/pre-built/$triple.tgz" "$url"; then
echo "Pre-built binaries not available"
mkdir -p build/pre-built
exit 1
else
echo "Pre-build binaries available, extracting.."
if ! curl --silent --fail --output "build/pre-built/$triple.tgz" "$url"; then
echo "Pre-built binaries not available"
cd build/pre-built
rm -rf "$triple"
tar -xvzf "$triple.tgz"
exit 1
fi
fi
echo "Pre-build binaries available, extracting.."
cd build/pre-built
rm -rf "$triple"
tar -xvzf "$triple.tgz"