Skip to content

Commit

Permalink
chore: add npm packages (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
fathyb authored Feb 9, 2023
1 parent 65bb7d4 commit 7887952
Show file tree
Hide file tree
Showing 5 changed files with 234 additions and 40 deletions.
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "carbonyl",
"version": "0.0.1",
"license": "BSD-3-Clause"
}
66 changes: 37 additions & 29 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

```console
$ npm install --global carbonyl
$ carbonyl https://github.com
```

- [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)
### Binaries

Carbonyl on Linux requires the same dependencies as Chromium.
- [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

Expand All @@ -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

Expand All @@ -71,38 +79,38 @@ 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

Browsh is the original "normal browser into a terminal" project. It starts Firefox in headless mode and connects to it through an automation protocol.

#### 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

Expand All @@ -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

Expand Down Expand Up @@ -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

Expand Down
146 changes: 146 additions & 0 deletions scripts/npm-publish.mjs
Original file line number Diff line number Diff line change
@@ -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: '[email protected]',
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 changes: 32 additions & 0 deletions scripts/npm-publish.sh
Original file line number Diff line number Diff line change
@@ -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 "$@"
25 changes: 14 additions & 11 deletions scripts/runtime-pull.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit 7887952

Please sign in to comment.