Skip to content
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
35 changes: 35 additions & 0 deletions bin/cre-ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env node

import { execFileSync } from 'node:child_process'
import { join } from 'node:path'
import { resolve } from 'node:path'

function getBinaryPath() {
// Lookup table for all platforms and binary distribution packages
const BINARY_DISTRIBUTION_PACKAGES = {
'linux-x64': '@chainlink/cre-ts-linux-x64',
'linux-arm64': '@chainlink/cre-ts-linux-arm',
'darwin-x64': '@chainlink/cre-ts-darwin-x64',
'darwin-arm64': '@chainlink/cre-ts-darwin-arm64',
'win32-x64': '@chainlink/cre-ts-windows-x64',
}

// Windows binaries end with .exe so we need to special case them.
const binaryName = process.platform === 'win32' ? 'cre-build.exe' : 'cre-build'

// Determine package name for this platform
const platformSpecificPackageName =
BINARY_DISTRIBUTION_PACKAGES[`${process.platform}-${process.arch}`]

try {
// Resolving will fail if the optionalDependency was not installed
return resolve(`node_modules/${platformSpecificPackageName}/bin/${binaryName}`)
} catch (e) {
return join(__dirname, binaryName)
}
}


execFileSync(getBinaryPath(), process.argv.slice(2), {
stdio: 'inherit',
})
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: add EOF new line

2 changes: 1 addition & 1 deletion biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
},
"files": {
"ignoreUnknown": false,
"includes": ["src/**/*", "scripts/**/*"]
"includes": ["src/**/*", "scripts/**/*", "cli/**/*", "**/*.json"]
},
"formatter": {
"enabled": true,
Expand Down
38 changes: 38 additions & 0 deletions build-bins.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

#!/bin/bash

# Platform configurations: "bun-target:platform-arch:os:cpu"
platforms=(
"bun-darwin-arm64:darwin-arm64:darwin:arm64"
"bun-darwin-x64:darwin-x64:darwin:x64"
"bun-linux-x64:linux-x64:linux:x64"
"bun-linux-arm64:linux-arm64:linux:arm64"
"bun-windows-x64:windows-x64:win32:x64"
)

# Loop through each platform configuration
for platform_config in "${platforms[@]}"; do
# Split the configuration string
IFS=':' read -r bun_target platform_arch os cpu <<< "$platform_config"

echo "Building for $platform_arch..."

# Build the binary
bun build ./cli/run.ts --target="$bun_target" --compile --outfile "dist/bin/$platform_arch/bin/cre-build"

chmod +x "dist/bin/$platform_arch/bin/cre-build"

# Create package.json for this platform
cat > "dist/bin/$platform_arch/package.json" << EOF
{
"name": "@chainlink/cre-build-$platform_arch",
"version": "0.0.1",
"os": ["$os"],
"cpu": ["$cpu"]
}
EOF

echo "✓ Built $platform_arch binary"
done

echo "All platform binaries built successfully!"
12 changes: 12 additions & 0 deletions build-npm-tar.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
sh build-pkg.sh

bun pm pack --destination ./dist
# tar -cvzf backup.tgz ./dist

# Pack each platform-specific folder in dist/bin
for folder in dist/bin/*/; do
if [ -d "$folder" ] && [ -f "$folder/package.json" ]; then
echo "Packing $folder..."
(cd "$folder" && bun pm pack --destination ../..)
fi
done
18 changes: 18 additions & 0 deletions build-pkg.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
bun run build:javy:sdk:wasm

bun tsc --project tsconfig.build.json

sh build-bins.sh

cp bin/* dist/bin/
cp install.js dist/install.js

chmod +x dist/bin/cre-ts

bun rimraf tsconfig.types.tsbuildinfo

cp package.json dist/package.json

cd dist

bun pm pkg set scripts.postinstall="node ./install.js"
100 changes: 92 additions & 8 deletions bun.lock

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions cli/compile-cmd.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { main as compileToJs } from './compile-to-js'
import { main as compileToWasm } from './compile-to-wasm-cmd'

export const main = async () => {
await compileToJs()
await compileToWasm()
}
28 changes: 28 additions & 0 deletions cli/compile-to-js.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import fg from 'fast-glob'
import { $ } from 'bun'

export const main = async () => {
console.info('\n\n---> Compile TS source files to JS\n\n')

const workflowsSourcePaths = await fg('src/workflows/**/*.ts')
const workflows = Array.from(workflowsSourcePaths).map((path) => `./${path}`)

await Bun.build({
entrypoints: [...workflows],
outdir: './dist/workflows',
target: 'node',
format: 'esm',
root: './src/workflows',
})

console.info('\n---> Bundling individual workflow files\n\n')

// Get all the generated JS files in dist/workflows
const workflowJSFiles = await fg('dist/workflows/**/*.js')

for (const jsFile of workflowJSFiles) {
await $`bun build ${jsFile} --bundle --outfile=${jsFile}`
}

console.info('Bundling: Done!')
}
51 changes: 51 additions & 0 deletions cli/compile-to-wasm-cmd.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { execFileSync } from 'node:child_process'
import path from 'node:path'
import fg from 'fast-glob'

export const main = async () => {
console.info('\n\n---> Compile JS workflows to WASM \n\n')

const workflowJSFiles = fg.sync('dist/workflows/**/*.js')

for (const jsFile of workflowJSFiles) {
if (jsFile.includes('abi')) continue

const wasmFile = jsFile.replace(/\.js$/, '.wasm')

console.log(`\n\n🔨 Building WASM for: ${jsFile}`)

const javyBinary =
process.platform === 'darwin'
? './.bin/javy-arm-macos-v5.0.4'
: './.bin/javy-arm-linux-v5.0.4'

const javyPath = path.join(process.cwd(), 'node_modules', javyBinary)

/**
* -C wit=src/workflows/workflow.wit — points to the WIT file (definition of what will be available for the Host).
* -C wit-world=workflow — specifies the WIT world name (world "workflow" which is defined in the .wit file).
* -C plugin=... — uses your custom runtime (bundled javy chainlink sdk plugin)
*/

execFileSync(
javyPath,
[
'build',
'-C',
'wit=src/workflows/workflow.wit',
'-C',
'wit-world=workflow',
'-C',
'plugin=node_modules/@chainlink/cre-sdk/javy-chainlink-sdk.plugin.wasm',
jsFile,
'-o',
wasmFile,
],
{
stdio: 'inherit',
},
)
}

console.info('Done!')
}
39 changes: 39 additions & 0 deletions cli/run.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env bun
import { main as compileCmd } from './compile-cmd'
import { main as compileToJs } from './compile-to-js'
import { main as compileToWasm } from './compile-to-wasm-cmd'

const availableScripts = {
'compile-to-js': compileToJs,
'compile-to-wasm': compileToWasm,
compile: compileCmd,
}

const main = async () => {
const scriptName = process.argv[2]

if (!scriptName) {
console.error('Usage: bun run.ts <script-name>')
console.error('Available scripts:')
Object.keys(availableScripts).forEach((script) => {
console.error(` ${script}`)
})
process.exit(1)
}

try {
const script = availableScripts[scriptName]

if (!script) {
console.error(`Script ${scriptName} not found`)
process.exit(1)
}

await script()
} catch (error) {
console.error(`Failed to load script ${scriptName}:`, error)
process.exit(1)
}
}

main()
122 changes: 122 additions & 0 deletions install.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import fs from 'fs'
import path from 'path'
import zlib from 'zlib'
import https from 'https'

const __dirname = import.meta.dirname;

// Lookup table for all platforms and binary distribution packages
const BINARY_DISTRIBUTION_PACKAGES = {
'linux-x64': '@chainlink/cre-ts-linux-x64',
'linux-arm64': '@chainlink/cre-ts-linux-arm',
'darwin-x64': '@chainlink/cre-ts-darwin-x64',
'darwin-arm64': '@chainlink/cre-ts-darwin-arm64',
'win32-x64': '@chainlink/cre-ts-windows-x64',
}

// Adjust the version you want to install. You can also make this dynamic.
const BINARY_DISTRIBUTION_VERSION = '1.0.0'

// Windows binaries end with .exe so we need to special case them.
const binaryName = process.platform === 'win32' ? 'cre-build.exe' : 'cre-build'

// Determine package name for this platform
const platformSpecificPackageName =
BINARY_DISTRIBUTION_PACKAGES[`${process.platform}-${process.arch}`]

// Compute the path we want to emit the fallback binary to
const fallbackBinaryPath = path.join(__dirname, binaryName)

function makeRequest(url) {
return new Promise((resolve, reject) => {
https
.get(url, (response) => {
if (response.statusCode >= 200 && response.statusCode < 300) {
const chunks = []
response.on('data', (chunk) => chunks.push(chunk))
response.on('end', () => {
resolve(Buffer.concat(chunks))
})
} else if (
response.statusCode >= 300 &&
response.statusCode < 400 &&
response.headers.location
) {
// Follow redirects
makeRequest(response.headers.location).then(resolve, reject)
} else {
reject(
new Error(
`npm responded with status code ${response.statusCode} when downloading the package!`
)
)
}
})
.on('error', (error) => {
reject(error)
})
})
}

function extractFileFromTarball(tarballBuffer, filepath) {
// Tar archives are organized in 512 byte blocks.
// Blocks can either be header blocks or data blocks.
// Header blocks contain file names of the archive in the first 100 bytes, terminated by a null byte.
// The size of a file is contained in bytes 124-135 of a header block and in octal format.
// The following blocks will be data blocks containing the file.
let offset = 0
while (offset < tarballBuffer.length) {
const header = tarballBuffer.subarray(offset, offset + 512)
offset += 512

const fileName = header.toString('utf-8', 0, 100).replace(/\0.*/g, '')
const fileSize = parseInt(header.toString('utf-8', 124, 136).replace(/\0.*/g, ''), 8)

if (fileName === filepath) {
return tarballBuffer.subarray(offset, offset + fileSize)
}

// Clamp offset to the uppoer multiple of 512
offset = (offset + fileSize + 511) & ~511
}
}

async function downloadBinaryFromNpm() {
// Download the tarball of the right binary distribution package
const tarballDownloadBuffer = await makeRequest(
`https://registry.npmjs.org/${platformSpecificPackageName}/-/${platformSpecificPackageName}-${BINARY_DISTRIBUTION_VERSION}.tgz`
)

const tarballBuffer = zlib.unzipSync(tarballDownloadBuffer)

// Extract binary from package and write to disk
fs.writeFileSync(
fallbackBinaryPath,
extractFileFromTarball(tarballBuffer, `package/bin/${binaryName}`),
{ mode: 0o755 } // Make binary file executable
)
}

function isPlatformSpecificPackageInstalled() {
try {
// Resolving will fail if the optionalDependency was not installed
require.resolve(`${platformSpecificPackageName}/bin/${binaryName}`)
return true
} catch (e) {
return false
}
}

if (!platformSpecificPackageName) {
throw new Error('Platform not supported!')
}

// Skip downloading the binary if it was already installed via optionalDependencies
if (!isPlatformSpecificPackageInstalled()) {
console.log('Platform specific package not found. Will manually download binary.')
downloadBinaryFromNpm()
} else {
console.log(
'Platform specific package already installed. Will fall back to manually downloading binary.'
)
Comment on lines +119 to +121

Choose a reason for hiding this comment

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

Thought: I'm confused with this log

If the package is already installed, the install script isn't expected to do anything, right?
The code says so.
OTOH, IIUC the log is saying it will manually download a binary.

}
Loading
Loading