generated from EVerest/everest-template
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
build(fetch-schema-plugin): added vite plugin to fetch schemas from e…
…verest framework Closes #131 Signed-off-by: Lukas Mertens <[email protected]> commit-id:bb068b2d
- Loading branch information
1 parent
c7fdebf
commit 920b06a
Showing
5 changed files
with
65 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import {remoteSchemas} from "../schemas.config"; | ||
import yaml from 'js-yaml'; | ||
import crypto from 'crypto'; | ||
import fs from "fs"; | ||
|
||
|
||
export function vitePluginFetchSchemas() { | ||
return { | ||
name: 'vite-plugin-fetch-schema', | ||
buildStart() { | ||
fetchRemoteSchemas(); | ||
} | ||
}; | ||
} | ||
|
||
function sha256(data: string): string { | ||
return crypto.createHash('sha256').update(data, 'utf8').digest('hex'); | ||
} | ||
|
||
export function fetchRemoteSchemas() { | ||
remoteSchemas.forEach(async (schema) => { | ||
if (!fs.existsSync(`./public/schemas/${schema.name}.json`)) { | ||
const fetchedSchemaYAML = await (await (await fetch(schema.url)).blob()).text(); | ||
const fetchedSchemaHash = sha256(fetchedSchemaYAML); | ||
if (fetchedSchemaHash !== schema.hash) { | ||
throw new Error(`Schema hash mismatch for ${schema.url}. Expected: ${schema.hash}, got: ${fetchedSchemaHash}`); | ||
} | ||
const schemaJSON = yaml.load(fetchedSchemaYAML); | ||
|
||
schema.hash = fetchedSchemaHash; | ||
fs.writeFileSync(`./public/schemas/${schema.name}.json`, JSON.stringify(schemaJSON)); | ||
|
||
console.log(`${schema.url} fetched and cached successfully.`); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// configure the release branch here | ||
const releaseBranch = 'release/2024.3.0-rc1'; | ||
|
||
export type RemoteSchema = { | ||
url: string; | ||
name: string; | ||
hash: string; | ||
} | ||
|
||
export const remoteSchemas: RemoteSchema[] = [ | ||
{ | ||
url: `https://raw.githubusercontent.com/EVerest/everest-framework/${releaseBranch}/schemas/config.yaml`, | ||
name: 'config', | ||
hash: 'cd2296cdf0bd29bfffa58e920e5052d1bd5d2f1d2fd63d92fd5ac7e8af64d80b' | ||
}, | ||
{ | ||
url: `https://raw.githubusercontent.com/EVerest/everest-framework/release/${releaseBranch}/schemas/interface.yaml`, | ||
name: 'interface', | ||
hash: '9b1059249c4073737bec94753a6735f664d77b97321d4379b9de3179ea0a9cd5' | ||
} | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
920b06a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Codacy detected an issue:
Message:
User controlled data in 'yaml.load()' function can result in Remote Code Injection.
Occurred on:
const schemaJSON = yaml.load(fetchedSchemaYAML);
Currently on:
Since js-yaml v4 yaml.load is safe by default. yaml.safeLoad is deprecated
920b06a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since js-yaml v4 yaml.load is safe by default. yaml.safeLoad is deprecated => we can ignore this