@monokle/validation
Advanced tools
Comparing version 0.12.1 to 0.13.0
@@ -0,1 +1,7 @@ | ||
import fs from "fs"; | ||
import path from "path"; | ||
import fetch from 'node-fetch'; | ||
import requireFromString from 'require-from-string'; | ||
import virtual from '@rollup/plugin-virtual'; | ||
import { rollup } from 'rollup'; | ||
import { ResourceParser } from "./common/resourceParser.js"; | ||
@@ -10,4 +16,2 @@ import { MonokleValidator } from "./MonokleValidator.js"; | ||
import { YamlValidator } from "./validators/yaml-syntax/validator.js"; | ||
import fs from "fs"; | ||
import * as path from "path"; | ||
export function createExtensibleNodeMonokleValidator(parser = new ResourceParser(), schemaLoader = new SchemaLoader()) { | ||
@@ -30,12 +34,4 @@ return new MonokleValidator(async (pluginName) => { | ||
try { | ||
const filePath = path.join(process.cwd(), ".monokle-plugins", `${pluginName}-plugin.js`); | ||
if (fs.existsSync(filePath)) { | ||
const customPlugin = await import(/* @vite-ignore */ filePath); | ||
return new SimpleCustomValidator(customPlugin.default, parser); | ||
} | ||
else { | ||
const url = `https://plugins.monokle.com/validation/${pluginName}/latest.js`; | ||
const customPlugin = await importWithDataUrl(url); | ||
return new SimpleCustomValidator(customPlugin.default, parser); | ||
} | ||
const customPlugin = await loadCustomPlugin(pluginName); | ||
return new SimpleCustomValidator(customPlugin, parser); | ||
} | ||
@@ -48,12 +44,30 @@ catch (err) { | ||
} | ||
async function importWithDataUrl(url) { | ||
const response = await fetch(url); | ||
if (!response.ok) { | ||
throw new Error(`Error fetching ${url}: ${response.statusText}`); | ||
} | ||
const source = await response.text(); | ||
const buff = Buffer.from(source); | ||
const encodedSource = buff.toString("base64"); | ||
const dataUrl = `data:text/javascript;base64,${encodedSource}`; | ||
return await import(/* @vite-ignore */ dataUrl); | ||
async function bundlePluginCode(code) { | ||
const bundle = await rollup({ | ||
input: 'pluginCode', | ||
plugins: [ | ||
virtual({ | ||
pluginCode: code, | ||
}), | ||
], | ||
}); | ||
const { output } = await bundle.generate({ format: 'commonjs' }); | ||
await bundle.close(); | ||
return output[0].code; | ||
} | ||
async function loadCustomPlugin(pluginName) { | ||
const filePath = path.join(process.cwd(), '.monokle-plugins', `${pluginName}-plugin.js`); | ||
const pluginCode = fs.existsSync(filePath) | ||
? fs.readFileSync(filePath, { encoding: 'utf-8' }) | ||
: await (async () => { | ||
const url = `https://plugins.monokle.com/validation/${pluginName}/latest.js`; | ||
const response = await fetch(url); | ||
if (!response.ok) { | ||
throw new Error(`Error fetching ${url}: ${response.statusText}`); | ||
} | ||
return response.text(); | ||
})(); | ||
const code = await bundlePluginCode(pluginCode); | ||
const customPlugin = requireFromString(code, filePath); | ||
return customPlugin; | ||
} |
{ | ||
"name": "@monokle/validation", | ||
"version": "0.12.1", | ||
"version": "0.13.0", | ||
"description": "Kubernetes resource validation", | ||
@@ -51,12 +51,14 @@ "author": "Kubeshop", | ||
"@types/lodash": "4.14.185", | ||
"@types/require-from-string": "1.2.1", | ||
"rimraf": "3.0.2", | ||
"tiny-glob": "0.2.9", | ||
"type-fest": "3.0.0", | ||
"typescript": "4.8.3", | ||
"uuid": "9.0.0", | ||
"vite": "3.1.2", | ||
"vitest": "0.23.4", | ||
"tiny-glob": "0.2.9", | ||
"uuid": "9.0.0" | ||
"vitest": "0.23.4" | ||
}, | ||
"dependencies": { | ||
"@open-policy-agent/opa-wasm": "1.8.0", | ||
"@rollup/plugin-virtual": "3.0.1", | ||
"ajv": "6.12.6", | ||
@@ -66,2 +68,5 @@ "change-case": "4.1.2", | ||
"lodash": "4.17.21", | ||
"node-fetch": "3.3.0", | ||
"require-from-string": "2.0.2", | ||
"rollup": "3.18.0", | ||
"tiny-invariant": "1.2.0", | ||
@@ -68,0 +73,0 @@ "yaml": "2.1.1", |
286838
7081
12
10
+ Added@rollup/plugin-virtual@3.0.1
+ Addednode-fetch@3.3.0
+ Addedrequire-from-string@2.0.2
+ Addedrollup@3.18.0
+ Added@rollup/plugin-virtual@3.0.1(transitive)
+ Addeddata-uri-to-buffer@4.0.1(transitive)
+ Addedfetch-blob@3.2.0(transitive)
+ Addedformdata-polyfill@4.0.10(transitive)
+ Addedfsevents@2.3.3(transitive)
+ Addednode-domexception@1.0.0(transitive)
+ Addednode-fetch@3.3.0(transitive)
+ Addedrequire-from-string@2.0.2(transitive)
+ Addedrollup@3.18.0(transitive)
+ Addedweb-streams-polyfill@3.3.3(transitive)