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
21 changes: 21 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ rustls-pki-types = { version = "1", features = ["alloc"] }
tokio-tungstenite = { version = "0.26", default-features = false, features = ["handshake", "connect", "rustls-tls-webpki-roots"] }
socket2 = "0.6.5"
toml = "0.8"
glob = "0.3"
serde_yaml = "0.9"
rusqlite = { version = "0.33", features = ["bundled"] }
ring = "0.17"
aws-lc-rs = "1"
Expand Down Expand Up @@ -90,6 +92,9 @@ aws-lc-rs = "1"
scrypt = { version = "0.11", default-features = false }
notify = "8"
base64 = "0.22"
toml = "0.8"
glob = "0.3"
serde_yaml = "0.9"

url = "2"

Expand Down
85 changes: 85 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,91 @@ mod config {
pub static CONFIG_ENV: LazyLock<RwLock<HashMap<String, String>>> = LazyLock::new(|| RwLock::new(HashMap::new()));
}

#[allow(dead_code)]
mod redis {
use deno_core::OpState;
use deno_core::op2;
use deno_error::JsErrorBox;

#[op2(fast)]
pub fn op_redis_connect(_state: &mut OpState, #[string] _url: String) -> Result<u32, JsErrorBox> {
Ok(0)
}

#[op2]
#[serde]
pub fn op_redis_command(
_state: &mut OpState,
_id: u32,
#[serde] _args: Vec<String>,
) -> Result<serde_json::Value, JsErrorBox> {
Ok(serde_json::Value::Null)
}

#[op2]
#[serde]
pub fn op_redis_pipeline(
_state: &mut OpState,
_id: u32,
#[serde] _commands: Vec<Vec<String>>,
) -> Result<Vec<serde_json::Value>, JsErrorBox> {
Ok(Vec::new())
}
}

#[allow(dead_code)]
mod s3 {
use deno_core::OpState;
use deno_core::op2;
use deno_error::JsErrorBox;

#[op2(fast)]
pub fn op_s3_connect(
_state: &mut OpState,
#[string] _endpoint: String,
#[string] _bucket: String,
#[string] _region: String,
#[string] _access_key: String,
#[string] _secret_key: String,
) -> Result<u32, JsErrorBox> {
Ok(0)
}

#[op2]
#[serde]
pub fn op_s3_get(_state: &mut OpState, _id: u32, #[string] _key: String) -> Result<serde_json::Value, JsErrorBox> {
Ok(serde_json::Value::Null)
}

#[op2]
#[serde]
pub fn op_s3_put(
_state: &mut OpState,
_id: u32,
#[string] _key: String,
#[buffer] _data: &[u8],
) -> Result<serde_json::Value, JsErrorBox> {
Ok(serde_json::Value::Null)
}

#[op2]
#[serde]
pub fn op_s3_delete(_state: &mut OpState, _id: u32, #[string] _key: String) -> Result<serde_json::Value, JsErrorBox> {
Ok(serde_json::Value::Null)
}

#[op2]
#[string]
pub fn op_s3_presign(
_state: &mut OpState,
_id: u32,
#[string] _key: String,
_expires_secs: u32,
) -> Result<String, JsErrorBox> {
Ok(String::new())
}
}

#[allow(dead_code)]
mod ops {
include!("src/ops.rs");
Expand Down
25 changes: 25 additions & 0 deletions src/js/30_formats.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// malia.yaml and malia.toml -- built-in config format parsing.
// Eliminates js-yaml (70M/week) and @iarna/toml.
(function (globalThis) {
const ops = Deno.core.ops;

function makeFormat(parseFn, stringifyFn) {
return {
parse(input) {
if (typeof input !== "string") throw new TypeError("input must be a string");
return parseFn(input);
},
stringify(value) {
return stringifyFn(value);
},
};
}

const yaml = makeFormat(ops.op_yaml_parse, ops.op_yaml_stringify);
const toml = makeFormat(ops.op_toml_parse, ops.op_toml_stringify);

globalThis.jse = globalThis.jse || {};
globalThis.jse.yaml = yaml;
globalThis.jse.toml = toml;
globalThis.malia = globalThis.malia || globalThis.jse;
})(globalThis);
98 changes: 98 additions & 0 deletions src/js/31_shell.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// malia.$ --shell scripting API (like Bun.$, zx).
// Tagged template literal that runs shell commands with interpolation,
// piping, and streaming output. Escapes interpolated values to prevent injection.
(function (globalThis) {
const ops = Deno.core.ops;

function escapeShellArg(arg) {
if (typeof arg !== "string") arg = String(arg);
if (arg.length === 0) return "''";
if (/^[a-zA-Z0-9._\-/:=@]+$/.test(arg)) return arg;
return "'" + arg.replace(/'/g, "'\\''") + "'";
}

function buildCommand(strings, values) {
let cmd = strings[0];
for (let i = 0; i < values.length; i++) {
const val = values[i];
if (Array.isArray(val)) {
cmd += val.map(escapeShellArg).join(" ");
} else {
cmd += escapeShellArg(val);
}
cmd += strings[i + 1];
}
return cmd.trim();
}

class ShellOutput {
constructor(code, stdout, stderr) {
this.exitCode = code ?? 0;
this._stdout = stdout;
this._stderr = stderr;
}
get stdout() {
return typeof this._stdout === "string" ? this._stdout : new TextDecoder().decode(this._stdout);
}
get stderr() {
return typeof this._stderr === "string" ? this._stderr : new TextDecoder().decode(this._stderr);
}
get text() { return this.stdout.trimEnd(); }
get lines() { return this.text.split("\n"); }
get json() { return JSON.parse(this.stdout); }
toString() { return this.stdout; }
[Symbol.toPrimitive]() { return this.stdout; }
}

// malia.$`command ${arg}` --returns a promise that resolves to ShellOutput.
// Throws on non-zero exit code (like Bun.$).
function $(strings, ...values) {
const cmd = buildCommand(strings, values);
const shell = globalThis.process?.platform === "win32" ? "cmd.exe" : "/bin/sh";
const args = globalThis.process?.platform === "win32" ? ["/c", cmd] : ["-c", cmd];

const result = ops.op_child_spawn_sync({
cmd: shell,
args,
cwd: null,
env: null,
stdin: "inherit",
stdout: "pipe",
stderr: "pipe",
});

const output = new ShellOutput(result.code, result.stdout, result.stderr);
if (output.exitCode !== 0) {
const err = new Error(`Shell command failed with exit code ${output.exitCode}: ${cmd}`);
err.exitCode = output.exitCode;
err.stdout = output.stdout;
err.stderr = output.stderr;
throw err;
}
return output;
}

// $.nothrow`cmd` --same but doesn't throw on non-zero exit
$.nothrow = function (strings, ...values) {
const cmd = buildCommand(strings, values);
const shell = globalThis.process?.platform === "win32" ? "cmd.exe" : "/bin/sh";
const args = globalThis.process?.platform === "win32" ? ["/c", cmd] : ["-c", cmd];
const result = ops.op_child_spawn_sync({
cmd: shell,
args,
cwd: null,
env: null,
stdin: "inherit",
stdout: "pipe",
stderr: "pipe",
});
return new ShellOutput(result.code, result.stdout, result.stderr);
};

// $.escape(str) --shell-escape a string
$.escape = escapeShellArg;

globalThis.jse = globalThis.jse || {};
globalThis.jse.$ = $;
globalThis.malia = globalThis.malia || globalThis.jse;
})(globalThis);
67 changes: 67 additions & 0 deletions src/js/32_redis.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// malia.redis -- built-in Redis client. Eliminates ioredis (13M/week).
(function (globalThis) {
const ops = Deno.core.ops;

function connect(url) {
url = url || process.env.REDIS_URL || "redis://localhost:6379";
const id = ops.op_redis_connect(String(url));

function cmd(...args) {
return ops.op_redis_command(id, args.map(String));
}

return {
// Core commands
get: (key) => cmd("GET", key),
set: (key, value, ...opts) => cmd("SET", key, value, ...opts),
del: (...keys) => cmd("DEL", ...keys),
exists: (...keys) => cmd("EXISTS", ...keys),
mget: (...keys) => cmd("MGET", ...keys),
mset: (...pairs) => cmd("MSET", ...pairs),
incr: (key) => cmd("INCR", key),
incrby: (key, n) => cmd("INCRBY", key, n),
decr: (key) => cmd("DECR", key),
expire: (key, secs) => cmd("EXPIRE", key, secs),
ttl: (key) => cmd("TTL", key),
keys: (pattern) => cmd("KEYS", pattern || "*"),

// Lists
lpush: (key, ...vals) => cmd("LPUSH", key, ...vals),
rpush: (key, ...vals) => cmd("RPUSH", key, ...vals),
lpop: (key) => cmd("LPOP", key),
rpop: (key) => cmd("RPOP", key),
lrange: (key, start, stop) => cmd("LRANGE", key, start, stop),
llen: (key) => cmd("LLEN", key),

// Hashes
hset: (key, field, value) => cmd("HSET", key, field, value),
hget: (key, field) => cmd("HGET", key, field),
hgetall: (key) => cmd("HGETALL", key),
hdel: (key, ...fields) => cmd("HDEL", key, ...fields),

// Sets
sadd: (key, ...members) => cmd("SADD", key, ...members),
smembers: (key) => cmd("SMEMBERS", key),
srem: (key, ...members) => cmd("SREM", key, ...members),

// Pub/Sub
publish: (channel, message) => cmd("PUBLISH", channel, message),

// Pipeline: batch multiple commands
pipeline: (commands) => ops.op_redis_pipeline(id, commands),

// Raw command
command: cmd,

// Info
ping: () => cmd("PING"),
info: (section) => section ? cmd("INFO", section) : cmd("INFO"),
dbsize: () => cmd("DBSIZE"),
flushdb: () => cmd("FLUSHDB"),
};
}

globalThis.jse = globalThis.jse || {};
globalThis.jse.redis = connect;
globalThis.malia = globalThis.malia || globalThis.jse;
})(globalThis);
Loading
Loading