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

client [NET-992]: pre-compiled JSON schema validator #1642

Merged
merged 3 commits into from
Jul 6, 2023

Conversation

harbu
Copy link
Contributor

@harbu harbu commented Jul 4, 2023

Summary

Compile JSON schema validator functions at build time instead of run time as described here https://ajv.js.org/standalone.html to avoid having to rely on eval during run time. Another added benefit is that we don't need to include ajv in our webpack build, reducing the browser bundle size slightly.

Changes

  • Mark packages ajv and ajv-format as dev dependencies.
  • Add script bin/generate-config-validator.js that generates the pre-built JSON schema validator function.
  • Call above script in prebuild phase, ensuring it gets called every time the client package is built.
  • Add folder src/generated for generated code that gets ignored by eslint.

Checklist before requesting a review

  • Is this a breaking change? If it is, be clear in summary.
  • Read through code myself one more time.
  • Make sure any and all TODO comments left behind are meant to be left in.
  • Has reasonable passing test coverage?
  • Updated changelog if applicable.
  • Updated documentation if applicable.

@linear
Copy link

linear bot commented Jul 4, 2023

NET-992 Compile schemas at build time to avoid eval with ajv

This ajv dependency is giving trouble to Swash, because it uses eval which is now completely forbidden in Chrome webstore. Apparently this library is related to schema validation. On the ajv website there's this note:

In order to use Ajv without relaxing CSP, you can compile the schemas using CLI (opens new window) or programmatically in your build code - see Standalone validation code. Compiled JavaScript file can export one or several validation functions that have the same code as the schemas compiled at runtime.

If I understand correctly, instead of using the library at runtime (which involves the evil eval), it's possible to compile the schemas at build-time and thus completely fix the issue.

@github-actions github-actions bot added the client Related to Client Package label Jul 4, 2023
@harbu harbu force-pushed the NET-992-compile-schemas-at-build-time branch from cb38e9b to 8b73001 Compare July 4, 2023 12:14
@harbu harbu force-pushed the NET-992-compile-schemas-at-build-time branch from 8b73001 to 029b6da Compare July 4, 2023 12:37
@harbu harbu changed the title NET-992: Compile JSON schemas at build time to avoid eval client [NET-992]: pre-compiled JSON schema validator Jul 4, 2023
@harbu harbu marked this pull request as ready for review July 4, 2023 13:19
@harbu harbu merged commit 7c07067 into main Jul 6, 2023
@harbu harbu deleted the NET-992-compile-schemas-at-build-time branch July 6, 2023 12:12
samt1803 added a commit that referenced this pull request Jul 10, 2023
…FlagService-2

* origin/streamr-1.0:
  make eslint happy again
  release(client, cli-tools): v8.5.5
  client [NET-992]: pre-compiled JSON schema validator (#1642)

# Conflicts:
#	package-lock.json
#	packages/broker/package.json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
client Related to Client Package
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant