Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rust integration #612

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,20 @@ jobs:
with:
node-version-file: '.nvmrc'
cache: 'yarn'

- name: Setup Rust
uses: dtolnay/rust-toolchain@stable

- uses: Swatinem/rust-cache@v2
with:
workspaces: './src/wasm-lib'

- run: yarn install

- run: yarn install:wasm

- run: yarn build:wasm

- run: yarn build

- run: yarn test
Expand Down
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,11 @@ yarn-error.log*
/tests/extension.spec.ts-snapshots/*darwin*

.env*

# rust
src/wasm-lib/target
src/wasm-lib/bindings
src/wasm-lib/kcl/bindings
public/wasm_lib_bg.wasm
src/wasm-lib/lcov.info
src/wasm-lib/grackle/test_json_output
7 changes: 6 additions & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"https://github.com/",
"https://api.github.com/",
"https://media.githubusercontent.com/",
"https://api.kittycad.io/"
"https://api.kittycad.io/",
"https://api.zoo.dev/"
],
"content_scripts": [
{
Expand All @@ -36,5 +37,9 @@
"background": {
"service_worker": "src/chrome/background.ts",
"type": "module"
},
"content_security_policy": {
"extension_pages": "script-src 'self' 'wasm-unsafe-eval'; object-src 'self';",
"sandbox": "sandbox allow-scripts allow-forms allow-popups allow-modals; script-src 'self' 'unsafe-inline' 'unsafe-eval'; child-src 'self';"
}
}
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
},
"scripts": {
"start": "vite",
"install:rust": "curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh",
"install:wasm": "curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh",
"build:wasm:dev": "(cd src/wasm-lib && wasm-pack build --dev --target web --out-dir pkg) && cp src/wasm-lib/pkg/wasm_lib_bg.wasm public",
"build:wasm": "(cd src/wasm-lib && wasm-pack build --target web --out-dir pkg) && cp src/wasm-lib/pkg/wasm_lib_bg.wasm public",
"build": "vite build",
"test": "vitest",
"e2e": "yarn build && yarn playwright test",
Expand Down
22 changes: 20 additions & 2 deletions src/chrome/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
setStorageKittycadToken,
} from './storage'
import { getFileBlob, getFileDiff } from './diff'
import init, { greet } from '../wasm-lib/pkg/wasm_lib'

let github: Octokit | undefined
let kittycad: Client | undefined
Expand All @@ -35,7 +36,9 @@ async function initGithubApi() {

async function initKittycadApi() {
try {
kittycad = new Client(await getStorageKittycadToken())
const token = await getStorageKittycadToken()
console.log('greet(token)', await greet(token))
kittycad = new Client(token)
const response = await users.get_user_self({ client: kittycad })
if ('error_code' in response) throw response
const { email } = response
Expand All @@ -59,9 +62,24 @@ async function saveKittycadTokenAndReload(token: string): Promise<void> {
await initKittycadApi()
}

;(async () => {
async function initialiseWasm() {
try {
const fullUrl = '/wasm_lib_bg.wasm'
const input = await fetch(fullUrl)
const buffer = await input.arrayBuffer()
const output = await init(buffer)
console.log('Wasm loaded: ', output)
return output
} catch (e) {
console.log('Error initialising WASM', e)
throw e
}
}

; (async () => {
// Delay to allow for external storage sets before auth, like in e2e
await new Promise(resolve => setTimeout(resolve, 1000))
await initialiseWasm()
await initKittycadApi()
await initGithubApi()
})()
Expand Down