Skip to content

Commit

Permalink
fix rust generation
Browse files Browse the repository at this point in the history
  • Loading branch information
Zwiterrion committed Feb 12, 2024
1 parent 1b55c37 commit 9c318e0
Show file tree
Hide file tree
Showing 24 changed files with 51 additions and 47 deletions.
3 changes: 2 additions & 1 deletion server/routers/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,8 @@ function addPluginToBuildQueue(folder, plugin, req, res, zipHash, release, saveI
pluginType: plugin.type,
metadata: opaMetadata ? opaMetadata : (metadata ? metadata : {}),
release,
saveInLocal
saveInLocal,
pluginName
});

res.json({
Expand Down
2 changes: 0 additions & 2 deletions server/routers/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ router.get('/', (req, res) => {
const { type } = req.query;
const template = !req.query.template || req.query.template === 'undefined' ? 'empty' : req.query.template;

console.log(req.query, template)

if (['rust', 'js', 'go', 'ts', 'opa'].includes(type)) {
getTemplates(type, template, res);
} else {
Expand Down
4 changes: 3 additions & 1 deletion server/services/compiler/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ class BuildOptions {
metadata,
isReleaseBuild,
wasi,
saveInLocal
saveInLocal,
pluginName
}) {
this.folderPath = folderPath;
this.userEmail = userEmail;
Expand All @@ -50,6 +51,7 @@ class BuildOptions {
this.isReleaseBuild = isReleaseBuild;
this.wasi = wasi;
this.saveInLocal = saveInLocal;
this.pluginName = pluginName;
}
}

Expand Down
2 changes: 1 addition & 1 deletion server/services/compiler/rust.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module.exports = options => new Compiler({
name: 'RUST',
options,
commands: [
`cargo build --manifest-path ./Cargo.toml ${options.isReleaseBuild ? '--release ' : ''}--target ${options.wasi ? "wasm32-wasi" : "wasm32-unknown-unknown"}`
`cargo build --manifest-path ./Cargo.toml ${options.isReleaseBuild ? '--release ' : ''}--target ${options.wasi ? "wasm32-wasi" : "wasm32-unknown-unknown"}`,
],
outputWasmFolder
});
3 changes: 2 additions & 1 deletion server/services/queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ const loop = () => {
metadata: nextBuild.metadata,
isReleaseBuild: nextBuild.release,
wasi: nextBuild.metadata?.wasi,
saveInLocal: nextBuild.saveInLocal
saveInLocal: nextBuild.saveInLocal,
pluginName: nextBuild.pluginName
}))
.then(() => {
WebSocket.emit(nextBuild.plugin, "JOB", "You can now use the generated wasm")
Expand Down
63 changes: 38 additions & 25 deletions server/services/wasmgc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,54 @@ const fs = require('fs-extra');
const p = require('path');

function wasmgc(options, buildOptions, path, log) {
const outputFilepath = p.join(path.split("/").slice(0, -1).join("/"), options.wasmName);
let outputFilepath = p.join(path.split("/").slice(0, -1).join("/"), options.wasmName);

if (!outputFilepath.endsWith('.wasm'))
outputFilepath = `${outputFilepath}.wasm`

const command = {
executable: 'wasm-opt',
args: ['-O', outputFilepath, '-o', outputFilepath]
}

return new Promise((resolve) => {
exec(`mv ${path} ${outputFilepath}`, () => {
getFileSize(outputFilepath)
.then(originalSize => {
log(`File size before optimizing: ${originalSize} bytes`)
log(`${command.executable} ${command.args.join(" ")}`);
exec(`${command.executable} ${command.args.join(" ")}`, (error, stdout, stderr) => {
if (error) {
log(error.message, true);
return resolve(outputFilepath);
}
if (stderr) {
log(stderr, true);
return resolve(outputFilepath);
}

getFileSize(outputFilepath)
.then(newSize => {
log(`File size after optimizing: ${newSize} bytes - ${(1 - (newSize / originalSize)) * 100}%`)

resolve(outputFilepath);
});
});
});
})
exec(`mv ${path} ${outputFilepath}`, (error) => {
if (error) {
exec(`mv ${p.join(path.split("/").slice(0, -1).join("/"), buildOptions.pluginName.replace('-', '_'))}.wasm ${outputFilepath}`, () => {
optimizeFile(outputFilepath, command, log, resolve)
})
} else {
optimizeFile(outputFilepath, command, log, resolve)
}
});
});
}

function optimizeFile(outputFilepath, command, log, resolve) {
return getFileSize(outputFilepath)
.then(originalSize => {
log(`File size before optimizing: ${originalSize} bytes`)
log(`${command.executable} ${command.args.join(" ")}`);
exec(`${command.executable} ${command.args.join(" ")}`, (error, stdout, stderr) => {
if (error) {
log(error.message, true);
return resolve(outputFilepath);
}
if (stderr) {
log(stderr, true);
return resolve(outputFilepath);
}

getFileSize(outputFilepath)
.then(newSize => {
log(`File size after optimizing: ${newSize} bytes - ${(1 - (newSize / originalSize)) * 100}%`)

resolve(outputFilepath);
});
});
});
}

function getFileSize(path) {
return new Promise(resolve => {
fs.stat(path, (_, stats) => {
Expand Down
Binary file modified server/templates/go.zip
Binary file not shown.
Binary file modified server/templates/izanami/go.zip
Binary file not shown.
Binary file modified server/templates/izanami/js.zip
Binary file not shown.
Binary file modified server/templates/izanami/opa.zip
Binary file not shown.
6 changes: 5 additions & 1 deletion server/templates/izanami/opa/policies.rego
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
package example

default can_access = true
default can_access = false

can_access {
true
}
Binary file modified server/templates/izanami/rust.zip
Binary file not shown.
Binary file modified server/templates/izanami/ts.zip
Binary file not shown.
Binary file modified server/templates/js.zip
Binary file not shown.
Binary file modified server/templates/opa.zip
Binary file not shown.
Binary file modified server/templates/otoroshi/go.zip
Binary file not shown.
Binary file modified server/templates/otoroshi/js.zip
Binary file not shown.
Binary file modified server/templates/otoroshi/opa.zip
Binary file not shown.
Binary file modified server/templates/otoroshi/rust.zip
Binary file not shown.
Binary file modified server/templates/otoroshi/ts.zip
Binary file not shown.
Binary file modified server/templates/rust.zip
Binary file not shown.
Binary file modified server/templates/ts.zip
Binary file not shown.
1 change: 0 additions & 1 deletion server/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ const unzipTo = (zipString, outputPaths) => {
content
)
} catch (err) {
console.log(err)
return Promise.reject(err)
}
}))
Expand Down
14 changes: 0 additions & 14 deletions ui/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,7 @@ class App extends React.Component {
})
}

// componentDidUpdate() {
// const { selectedPlugin } = this.state;

// const params = new URLSearchParams(window.location.search);
// const pluginId = params.get('plugin');

// if (selectedPlugin && selectedPlugin.pluginId !== pluginId) {
// window.history.replaceState(null, "", '?plugin=' + selectedPlugin.pluginId);
// } else if (!selectedPlugin) {
// window.history.replaceState(null, null, '/');
// }
// }

updateSelectedPlugin = (newValue, callback) => {
console.log(newValue)
if (newValue.selectedPlugin)
this.props.navigate(`?pluginId=${newValue.selectedPlugin.pluginId}`)

Expand Down

0 comments on commit 9c318e0

Please sign in to comment.