Add support for processors over http

This commit is contained in:
Endeavorance 2025-04-03 09:47:19 -04:00
parent b5136c5d02
commit 3682a7a763
7 changed files with 52 additions and 12 deletions

View file

@ -7,10 +7,7 @@
},
"files": {
"ignoreUnknown": false,
"ignore": [
"*.d.ts",
"*.json"
],
"ignore": ["*.d.ts", "*.json"],
"include": [
"./src/**/*.ts",
"./src/**/*.tsx",

1
bunfig.toml Normal file
View file

@ -0,0 +1 @@
preload = ["./src/preload/http-plugin.js"]

View file

@ -1,6 +1,6 @@
import path, { dirname } from "node:path";
import { Glob } from "bun";
import { MuseFileNotFoundError, MuseError } from "./errors";
import { MuseError, MuseFileNotFoundError } from "./errors";
import { parseMuseFile } from "./parse";
import {
type Binding,
@ -14,7 +14,9 @@ async function loadProcessor(
bindingDirname: string,
processorPath: string,
): Promise<MuseProcessor> {
const resolvedProcessorPath = path.resolve(bindingDirname, processorPath);
const resolvedProcessorPath = processorPath.startsWith("http")
? processorPath
: path.resolve(bindingDirname, processorPath);
const { process, name, description } = await import(resolvedProcessorPath);
if (!process || typeof process !== "function") {

View file

@ -1,8 +1,8 @@
import { parseArgs } from "node:util";
import chalk from "chalk";
import { MuseError } from "./errors";
import { loadBinding } from "./binding";
import { version } from "../package.json";
import { loadBinding } from "./binding";
import { MuseError } from "./errors";
import type { Binding, CLIArguments } from "./types";
interface Loggers {

View file

@ -1,7 +1,7 @@
import { normalize, extname } from "node:path";
import YAML from "yaml";
import TOML from "smol-toml";
import { extname, normalize } from "node:path";
import EMDY from "@endeavorance/emdy";
import TOML from "smol-toml";
import YAML from "yaml";
import type { MuseEntry, UnknownRecord } from "./types";
function parseYAMLEntries(text: string): UnknownRecord[] {

View file

@ -0,0 +1,40 @@
const rx_any = /./;
const rx_http = /^https?:\/\//;
const rx_relative_path = /^\.\.?\//;
const rx_absolute_path = /^\//;
async function load_http_module(href) {
return fetch(href).then((response) =>
response
.text()
.then((text) =>
response.ok
? { contents: text, loader: "js" }
: Promise.reject(
new Error(`Failed to load module '${href}'': ${text}`),
),
),
);
}
Bun.plugin({
name: "http_imports",
setup(build) {
build.onResolve({ filter: rx_relative_path }, (args) => {
if (rx_http.test(args.importer)) {
return { path: new URL(args.path, args.importer).href };
}
});
build.onResolve({ filter: rx_absolute_path }, (args) => {
if (rx_http.test(args.importer)) {
return { path: new URL(args.path, args.importer).href };
}
});
build.onLoad({ filter: rx_any, namespace: "http" }, (args) =>
load_http_module(`http:${args.path}`),
);
build.onLoad({ filter: rx_any, namespace: "https" }, (args) =>
load_http_module(`https:${args.path}`),
);
},
});