Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(common): pass manifest json to sandbox #273

Merged
merged 17 commits into from
Sep 22, 2023
Merged
Changes from 1 commit
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
33 changes: 32 additions & 1 deletion packages/field-plugin/helpers/vite/src/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,39 @@ export const load = (): Manifest => {
try {
const content: string = readFileSync(MANIFEST_FULL_PATH, 'utf8')
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here I kept informing the encoding since otherwise it would return me a Buffer instead of a string.

So, this way is the simplest one, I guess. If there is any other better way, let me know, please.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We've had some other code like readFileSync(...).toString(). I don't know what's better, though.


return JSON.parse(content) as Manifest
const manifest: Manifest = JSON.parse(content)

validateSchema(manifest)

return manifest
} catch (err: any) {
throw new Error('Error while loading the manifest file: ' + err.message)
}
}

const validateSchema = (manifest: Manifest): void => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added this simple validation, without using any external packages, here since we decided not to go too far on this.

const allowedProperties = ['options']

if (typeof manifest !== 'object') {
throw new Error(`The manifest should be an object`)
}

Object.keys(manifest).forEach((prop) => {
if (!allowedProperties.includes(prop)) {
throw new Error(`The property ${prop} is not allowed`)
}
})

if (manifest.options !== undefined && !Array.isArray(manifest.options)) {
throw new Error(`When declared, the 'options' property should be an array`)
}

manifest.options?.forEach((o) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's rename this o to option to be more informative. Other than that, it looks good!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion here, @eunjae-lee. 🙌

I've just renamed it.

If there is something else, let me know, please.

if (!('name' in o && 'value' in o)) {
throw new Error(
`Some of the defined 'options' are invalid. ` +
`Please, make sure they contain a 'name' and 'value' properties`,
)
}
})
}