Skip to content

Commit

Permalink
Improvement: Setting Up The Project (#9543)
Browse files Browse the repository at this point in the history
* Adds certificate related commands to project setup steps

* Adds nss installation guide to project setup steps

* Improve console messages on holodeck ensure-cert script

* Improve certificate related error messages on holodeck index

* Cleanup unsused imports and functions on holodeck

* Adds default cert and key paths to holodeck server index file
  • Loading branch information
leoeuclids authored Oct 15, 2024
1 parent f3d4919 commit 5bfbd71
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 43 deletions.
43 changes: 34 additions & 9 deletions contributing/setting-up-the-project.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Setting Up The Project

1. Setup Volta
## Setup Volta

If you are not already using [⚡️ volta](https://volta.sh/) or have a version older than `1.1.1` you will want to begin by [installing it](https://docs.volta.sh/guide/getting-started).

Expand All @@ -16,37 +16,62 @@ export VOLTA_FEATURE_PNPM=1;

> **Note** if you have previously installed pnpm globally via other means you should uninstall it from all other locations first. You may also need to uninstall nvm or other node version managers if they turn out to conflict.
2. Install bun.sh
## Install bun.sh

If you don't already have [bun.sh](https://bun.sh/)
For MacOS and Linux
If you don't already have [bun.sh](https://bun.sh/)
For MacOS or Linux
```sh
curl -fsSL https://bun.sh/install | bash
```
can be done using homebrew, npm or Docker (User choice) checkout installation [doc](https://bun.sh/docs/installation#macos-and-linux)

For Windows
For Windows
```sh
# WARNING: No stability is guaranteed on the experimental Windows builds
powershell -c "irm bun.sh/install.ps1|iex"
```
Installation [doc](https://bun.sh/docs/installation#windows)
Installation [doc](https://bun.sh/docs/installation#windows)

3. Clone the repository
## Install certificate packages

Install mkcert using homebrew on MacOS or Linux
```sh
brew install mkcert
```
can be done using Chocolatey, Scoop or MacPorts (User choice) checkout installation [doc][https://github.com/FiloSottile/mkcert?tab=readme-ov-file#installation]

For Firefox users, Mozilla NSS is also needed
Using homebrew on MacOS
```sh
brew install nss
```
Or apt on Linux
```sh
sudo apt install libnss3-tools
```
but can also be done using other methods.

## Clone the repository

```sh
git clone [email protected]:emberjs/data.git
```

4. Install the project dependencies
## Install the project dependencies

```sh
cd data && pnpm install
```

Currently the install command is also what builds all of the individual packages in the monorepo and hardlinks them together, so if making changes to one package that need to be used by another you will need to rerun `pnpm install` for the changes to be picked up.

5. Run some commands
## Create certificates

```sh
pnpx @warp-drive/holodeck ensure-cert
```

## Run some commands

Generally test and lint commands can be found in the `"scripts"` section of the root `package.json` manifest. Individual packages or test packages have additional commands in the `"scripts"` section of their own `package.json` manifest as well.

Expand Down
32 changes: 20 additions & 12 deletions packages/holodeck/server/ensure-cert.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,26 @@ function main() {
let KEY_PATH = process.env.HOLODECK_SSL_KEY_PATH;
const configFilePath = getShellConfigFilePath();

if (!CERT_PATH) {
CERT_PATH = path.join(homedir(), 'holodeck-localhost.pem');
process.env.HOLODECK_SSL_CERT_PATH = CERT_PATH;
execSync(`echo '\nexport HOLODECK_SSL_CERT_PATH="${CERT_PATH}"' >> ${configFilePath}`);
console.log(`Added HOLODECK_SSL_CERT_PATH to ${configFilePath}`);
}

if (!KEY_PATH) {
KEY_PATH = path.join(homedir(), 'holodeck-localhost-key.pem');
process.env.HOLODECK_SSL_KEY_PATH = KEY_PATH;
execSync(`echo '\nexport HOLODECK_SSL_KEY_PATH="${KEY_PATH}"' >> ${configFilePath}`);
console.log(`Added HOLODECK_SSL_KEY_PATH to ${configFilePath}`);
if (!CERT_PATH || !KEY_PATH) {
console.log(`Environment variables not found, updating the environment config file...\n`);

if (!CERT_PATH) {
CERT_PATH = path.join(homedir(), 'holodeck-localhost.pem');
process.env.HOLODECK_SSL_CERT_PATH = CERT_PATH;
execSync(`echo '\nexport HOLODECK_SSL_CERT_PATH="${CERT_PATH}"' >> ${configFilePath}`);
console.log(`Added HOLODECK_SSL_CERT_PATH to ${configFilePath}`);
}

if (!KEY_PATH) {
KEY_PATH = path.join(homedir(), 'holodeck-localhost-key.pem');
process.env.HOLODECK_SSL_KEY_PATH = KEY_PATH;
execSync(`echo '\nexport HOLODECK_SSL_KEY_PATH="${KEY_PATH}"' >> ${configFilePath}`);
console.log(`Added HOLODECK_SSL_KEY_PATH to ${configFilePath}`);
}

console.log(
`\n*** Please restart your terminal session to apply the changes or run \`source ${configFilePath}\`. ***\n`
);
}

if (!fs.existsSync(CERT_PATH) || !fs.existsSync(KEY_PATH)) {
Expand Down
32 changes: 10 additions & 22 deletions packages/holodeck/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,54 +5,42 @@ import { Hono } from 'hono';
import { cors } from 'hono/cors';
import { HTTPException } from 'hono/http-exception';
import { logger } from 'hono/logger';
import { execSync } from 'node:child_process';
import crypto from 'node:crypto';
import fs from 'node:fs';
import http2 from 'node:http2';
import zlib from 'node:zlib';
import { homedir, userInfo } from 'os';
import { homedir } from 'os';
import path from 'path';

/** @type {import('bun-types')} */
const isBun = typeof Bun !== 'undefined';
const DEBUG = process.env.DEBUG?.includes('holodeck') || process.env.DEBUG === '*';
const CURRENT_FILE = new URL(import.meta.url).pathname;

function getShellConfigFilePath() {
const shell = userInfo().shell;
switch (shell) {
case '/bin/zsh':
return path.join(homedir(), '.zshrc');
case '/bin/bash':
return path.join(homedir(), '.bashrc');
default:
throw Error(
`Unable to determine configuration file for shell: ${shell}. Manual SSL Cert Setup Required for Holodeck.`
);
}
}

function getCertInfo() {
let CERT_PATH = process.env.HOLODECK_SSL_CERT_PATH;
let KEY_PATH = process.env.HOLODECK_SSL_KEY_PATH;
const configFilePath = getShellConfigFilePath();

if (!CERT_PATH) {
CERT_PATH = path.join(homedir(), 'holodeck-localhost.pem');
process.env.HOLODECK_SSL_CERT_PATH = CERT_PATH;
execSync(`echo '\nexport HOLODECK_SSL_CERT_PATH="${CERT_PATH}"' >> ${configFilePath}`);
console.log(`Added HOLODECK_SSL_CERT_PATH to ${configFilePath}`);

console.log(
`HOLODECK_SSL_CERT_PATH was not found in the current environment. Setting it to default value of ${CERT_PATH}`
);
}

if (!KEY_PATH) {
KEY_PATH = path.join(homedir(), 'holodeck-localhost-key.pem');
process.env.HOLODECK_SSL_KEY_PATH = KEY_PATH;
execSync(`echo '\nexport HOLODECK_SSL_KEY_PATH="${KEY_PATH}"' >> ${configFilePath}`);
console.log(`Added HOLODECK_SSL_KEY_PATH to ${configFilePath}`);

console.log(
`HOLODECK_SSL_KEY_PATH was not found in the current environment. Setting it to default value of ${KEY_PATH}`
);
}

if (!fs.existsSync(CERT_PATH) || !fs.existsSync(KEY_PATH)) {
throw new Error('SSL certificate or key not found, you may need to run `npx -p @warp-drive/holodeck ensure-cert`');
throw new Error('SSL certificate or key not found, you may need to run `pnpx @warp-drive/holodeck ensure-cert`');
}

return {
Expand Down

0 comments on commit 5bfbd71

Please sign in to comment.