UNPKG

@nodescript/core

Version:

Visual programming language for Browser and Node

99 lines 3.33 kB
import { ModuleSpecSchema } from '../schema/ModuleSpec.js'; import * as systemModules from '../system/index.js'; export class GenericModuleLoader { constructor() { this.modules = new Map(); this.addModule('@system/AI', systemModules.AI); this.addModule('@system/Comment', systemModules.Comment); this.addModule('@system/EvalAsync', systemModules.EvalAsync); this.addModule('@system/EvalJson', systemModules.EvalJson); this.addModule('@system/EvalSync', systemModules.EvalSync); this.addModule('@system/EvalTemplate', systemModules.EvalTemplate); this.addModule('@system/Frame', systemModules.Frame); this.addModule('@system/Input', systemModules.Input); this.addModule('@system/Output', systemModules.Output); this.addModule('@system/Param', systemModules.Param); this.addModule('@system/Result', systemModules.Output); this.addModule('@system/Scope', systemModules.Scope); } resolveModule(ref) { return this.getModule(ref) ?? this.createUnresolved(ref); } getModule(ref) { return this.modules.get(ref) ?? null; } async loadModule(ref) { const existing = this.getModule(ref); if (existing) { // Do not import twice return existing; } const module = await this.fetchModule(ref); this.modules.set(ref, module); return module; } addModule(moduleRef, moduleSpec) { this.modules.set(moduleRef, moduleSpec); return moduleSpec; } removeModule(ref) { this.modules.delete(ref); } createUnresolved(ref) { return { moduleName: 'Unresolved', version: '0.0.0', labelParam: '', keywords: [], description: `Module ${ref} not found`, deprecated: '', params: {}, result: { schema: { type: 'any' }, }, newScope: false, cacheMode: 'auto', evalMode: 'auto', attributes: { ref, }, }; } } export class StandardModuleLoader extends GenericModuleLoader { constructor() { super(...arguments); this.registryUrl = 'https://registry.nodescript.dev'; } resolveModuleUrl(ref) { return new URL(ref + '.json', this.registryUrl).toString(); } resolveComputeUrl(ref) { return new URL(ref + '.mjs', this.registryUrl).toString(); } async fetchModule(ref) { const url = this.resolveModuleUrl(ref); const res = await fetch(url); if (!res.ok) { throw new ModuleLoadFailedError(`Failed to load module ${ref}: HTTP ${res.status}`, res.status); } const json = await res.json(); return ModuleSpecSchema.decode(json); } } export class UnresolvedNodeError extends Error { constructor() { super(...arguments); this.name = this.constructor.name; this.status = 500; } } export class ModuleLoadFailedError extends Error { constructor(message, status = 500) { super(message); this.message = message; this.status = status; this.name = this.constructor.name; } } //# sourceMappingURL=ModuleLoader.js.map