diff --git a/examples/emissions/co2-sensor/Dockerfile b/examples/emissions/co2-sensor/Dockerfile index 365e233..a0a86d9 100644 --- a/examples/emissions/co2-sensor/Dockerfile +++ b/examples/emissions/co2-sensor/Dockerfile @@ -1,15 +1,17 @@ +# We use ubuntu as it's glibc version is compatible with the prebuilt binaries FROM ubuntu RUN apt-get update && apt-get install -y wget +# Install node v20.12.2 ENV NVM_DIR /usr/local/nvm ENV NODE_VERSION v20.12.2 -RUN mkdir -p /usr/local/nvm && apt-get update && echo "y" | apt-get install curl -RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash +RUN mkdir -p $NVM_DIR && wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash RUN /bin/bash -c "source $NVM_DIR/nvm.sh && nvm install $NODE_VERSION && nvm use --delete-prefix $NODE_VERSION" ENV NODE_PATH $NVM_DIR/versions/node/$NODE_VERSION/bin ENV PATH $NODE_PATH:$PATH +# Install prebuilt holochain binaries RUN wget -nv https://github.com/holochain/holochain/releases/download/holochain-0.4.0-dev.20/hc-x86_64-linux \ -O /usr/local/bin/hc RUN wget -nv https://github.com/holochain/holochain/releases/download/holochain-0.4.0-dev.20/holochain-x86_64-linux \ @@ -18,9 +20,17 @@ RUN wget -nv https://github.com/holochain/holochain/releases/download/holochain- -O /usr/local/bin/lair-keystore RUN chmod 755 /usr/local/bin/hc /usr/local/bin/holochain /usr/local/bin/lair-keystore +# Install tsx WORKDIR /home/node -COPY ./package.json ./package.json +RUN /bin/bash -c "source $NVM_DIR/nvm.sh && npm i tsx" + +# Copy the actual server script COPY ./co2-sensor.ts ./co2-sensor.ts +# So container runs with nvm loaded SHELL ["/bin/bash", "--login", "-c"] -CMD npm install ./packages/types ./packages/sandbox; npm start + +# The ./packages directory is mounted from the locally built npm workspace. We +# npm install at launch to avoid the need for the package.json to know the +# latest version or features of the workspace. +CMD npm install ./packages/types ./packages/sandbox; npx tsx ./co2-sensor.ts diff --git a/examples/emissions/co2-sensor/package.json b/examples/emissions/co2-sensor/package.json deleted file mode 100644 index d948d50..0000000 --- a/examples/emissions/co2-sensor/package.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "name": "co2-sensor", - "scripts": { - "start": "tsx co2-sensor.ts" - }, - "dependencies": { - "tsx": "^4.19.0", - "@holochain/client": "^0.18.0-dev", - "@holoom/sandbox": "^0.1.0-dev", - "@holoom/types": "^0.1.0-dev", - "yaml": "^2.5.1" - } -} diff --git a/packages/authority/package.json b/packages/authority/package.json index 90a0c99..ea04f51 100644 --- a/packages/authority/package.json +++ b/packages/authority/package.json @@ -4,6 +4,7 @@ "description": "Helpers for interacting with a holoom network authority agent", "type": "module", "license": "MIT", + "main": "dist/index.browser.js", "types": "dist/types/index.d.ts", "exports": { ".": { @@ -11,7 +12,9 @@ "node": { "require": "./dist/index.node.cjs", "default": "./dist/index.node.js" - } + }, + "browser": "./dist/index.browser.js", + "default": "./dist/index.browser.js" } }, "files": [ @@ -26,7 +29,8 @@ "url": "https://github.com/holochain-open-dev/holoom.git/issues" }, "scripts": { - "build": "rimraf dist && npm run build:node", + "build": "rimraf dist && npm run build:browser && npm run build:node", + "build:browser": "rollup -c rollup.browser.config.ts --configPlugin typescript", "build:node": "rollup -c rollup.node.config.ts --configPlugin typescript" }, "publishConfig": { @@ -51,7 +55,6 @@ "bs58": "^4.0.1", "dotenv": "^16.4.5", "express": "^4.19.2", - "viem": "^2.8.13", - "yaml": "^2.5.1" + "viem": "^2.8.13" } } diff --git a/packages/authority/rollup.browser.config.ts b/packages/authority/rollup.browser.config.ts new file mode 100644 index 0000000..18d2158 --- /dev/null +++ b/packages/authority/rollup.browser.config.ts @@ -0,0 +1,38 @@ +import json from "@rollup/plugin-json"; +import typescript from "@rollup/plugin-typescript"; +import * as fs from "fs"; +import cleanup from "rollup-plugin-cleanup"; +import { RollupOptions } from "rollup"; + +const pkg = JSON.parse(fs.readFileSync("./package.json", "utf-8")); +const banner = `/** + * @module ${pkg.name} + * @version ${pkg.version} + * @file ${pkg.description} + * @license ${pkg.license} + * @see [Github]{@link ${pkg.homepage}} +*/`; + +const config: RollupOptions = { + input: "src/index.ts", + output: [ + { + file: pkg.exports["."].browser, + format: "es", + banner, + }, + ], + external: [...Object.keys(pkg.dependencies), "viem/accounts"], + plugins: [ + typescript({ + tsconfig: "./build.tsconfig.json", + }), + cleanup({ comments: "jsdoc" }), + json(), + ], + onwarn: (warning) => { + throw new Error(warning.message); + }, +}; + +export default config; diff --git a/packages/authority/tsconfig.json b/packages/authority/tsconfig.json index e87861b..aaa9cce 100644 --- a/packages/authority/tsconfig.json +++ b/packages/authority/tsconfig.json @@ -1,4 +1,4 @@ { "extends": "../../tsconfig.json", - "include": ["src", "rollup.node.config.ts"] + "include": ["src", "rollup.node.config.ts", "rollup.browser.config.ts"] } diff --git a/packages/sandbox/src/sandbox.ts b/packages/sandbox/src/sandbox.ts index d3a955c..d4e5125 100644 --- a/packages/sandbox/src/sandbox.ts +++ b/packages/sandbox/src/sandbox.ts @@ -8,6 +8,13 @@ export interface SandboxOptions { password: string; } +/** + * Creates a sandbox by invoking `hc sandbox create` at the specified directory + * if the directory doesn't already exist. + * + * @param path The directory of the conductor sandbox + * @param options Configuration for the sandbox + */ export async function ensureSandbox(path: string, options: SandboxOptions) { const exists = await fs .stat(path) @@ -18,6 +25,13 @@ export async function ensureSandbox(path: string, options: SandboxOptions) { } } +/** + * Creates a sandbox by invoking `hc sandbox create` at the specified + * directory. + * + * @param path The directory of the conductor sandbox + * @param options Configuration for the sandbox + */ export async function createSandbox(path: string, options: SandboxOptions) { const pathComps = path.split("/"); const sandboxDirName = pathComps.pop(); @@ -81,6 +95,14 @@ export async function createSandbox(path: string, options: SandboxOptions) { await fs.writeFile(conductorConfigPath, yaml.stringify(conductorConfig)); } +/** + * Starts the sandbox's conductor + * + * @param path The path to the sandbox + * @param password The password on the sandbox + * @returns A handle on the conductor process and the conductor's admin API + * websocket url + */ export async function startSandbox( path: string, password: string @@ -124,11 +146,13 @@ export async function startSandbox( return { process, adminApiUrl: new URL(`http://localhost:${adminPort}`) }; } +/** + * Shuts down the given conductor process. + * @param process The conductor process + */ export async function endSandboxProcess( process: ChildProcessWithoutNullStreams ) { - console.debug("closing admin and app web sockets\n"); - console.debug("shutting down conductor\n"); const conductorShutDown = new Promise((resolve) => { process.on("exit", (code) => {