-
-
Notifications
You must be signed in to change notification settings - Fork 333
Fix MV3 service worker crash by shimming ajv/ajv-formats #173
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
Open
insane66613
wants to merge
25
commits into
srbhptl39:main
Choose a base branch
from
insane66613:fix/mv3-ajv-shims
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
3e94295
Fix MV3 service worker crash by shimming ajv/ajv-formats
insane66613 f2770e4
Update chrome-extension/src/shims/ajv.ts
insane66613 13c6962
Update package.json
insane66613 988a0d3
Update chrome-extension/src/shims/ajv-codegen.ts
insane66613 e0e6658
Enable manual triggering of build-zip workflow
insane66613 fc018c8
Initial plan
Copilot 0b51943
chore: bump version to v0.6.1
Copilot 9152de1
Merge pull request #1 from insane66613/copilot/update-version-to-v061
insane66613 1d3cfa4
Add GitHub Actions workflow for releasing extension
insane66613 f806c7b
Update pnpm version to 9.15.0 in release workflow
insane66613 44d5e24
Update pnpm version to 9.15.1
insane66613 3d0cef2
Initial plan
Copilot 05d1e85
Merge pull request #2 from insane66613/copilot/fix-node-version-mismatch
insane66613 d9c5842
Initial plan
Copilot 994c299
Update release workflow to use Node.js 22.12.0
Copilot 765c8bc
Merge pull request #3 from insane66613/copilot/update-node-version-re…
insane66613 c81ba75
Update upload-artifact and action-gh-release paths in release.yml
insane66613 55b6a52
Update release workflow for version tagging and pnpm
insane66613 a51be3c
Initial plan
Copilot 143178e
Add icon-16.png and remove from .gitignore
Copilot 4a1cc25
Initial plan
Copilot 1a95de7
Fix: unload policy violation, SSE error logging/categorisation, Resiz…
Copilot d27fb4b
Merge pull request #5 from insane66613/copilot/fix-resizeobserver-issues
insane66613 3ba6962
Merge pull request #4 from insane66613/copilot/fix-icon-loading-error
insane66613 e812b9a
Merge tag 'v0.6.2' into fix/mv3-ajv-shims
insane66613 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ on: | |
| push: | ||
| branches: [ main, dev ] | ||
| pull_request: | ||
| workflow_dispatch: | ||
|
|
||
| jobs: | ||
| build: | ||
|
|
||
This file contains hidden or 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,55 @@ | ||
| name: Release Extension | ||
|
|
||
| on: | ||
| push: | ||
| tags: | ||
| - "v*" | ||
| workflow_dispatch: | ||
| inputs: | ||
| tag: | ||
| description: "Tag to release (e.g. v0.6.1)" | ||
| required: true | ||
| type: string | ||
|
|
||
| jobs: | ||
| release: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - name: Checkout repository | ||
| uses: actions/checkout@v4 | ||
| with: | ||
| fetch-depth: 0 | ||
|
|
||
| - name: Install pnpm | ||
| uses: pnpm/action-setup@v4 | ||
| with: | ||
| version: 9.15.1 | ||
|
|
||
| - name: Setup Node | ||
| uses: actions/setup-node@v4 | ||
| with: | ||
| node-version: "22.12.0" | ||
| cache: "pnpm" | ||
|
|
||
| - name: Install dependencies | ||
| run: pnpm install | ||
|
|
||
| - name: Build extension | ||
| run: pnpm build | ||
|
|
||
| - name: Create ZIP package | ||
| run: pnpm zip | ||
|
|
||
| - name: Upload Release Artifact | ||
| uses: actions/upload-artifact@v4 | ||
| with: | ||
| name: extension-zip | ||
| path: ./dist-zip/*.zip | ||
|
|
||
| - name: Create GitHub Release | ||
| uses: softprops/action-gh-release@v2 | ||
| with: | ||
| tag_name: ${{ github.event.inputs.tag || github.ref_name }} | ||
| files: ./dist-zip/*.zip | ||
| env: | ||
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or 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 hidden or 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 hidden or 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,74 @@ | ||
| // CSP-safe stub for Ajv internal codegen helpers. | ||
| // | ||
| // Some packages (notably ajv-formats) import `ajv/dist/compile/codegen` and expect | ||
| // helpers like `operators`, `str`, `_`, `or`, and `KeywordCxt` to exist. | ||
| // | ||
| // In MV3 service workers we must avoid Ajv's real codegen (it can use `new Function()`). | ||
| // This module provides just enough surface area to prevent crashes during module init. | ||
|
|
||
| export const operators = { | ||
| LT: '<', | ||
| LTE: '<=', | ||
| GT: '>', | ||
| GTE: '>=', | ||
| EQ: '===', | ||
| NEQ: '!==', | ||
| } as const; | ||
|
|
||
| function interpolate(strings: TemplateStringsArray, exprs: unknown[]): string { | ||
| let out = ''; | ||
| for (let i = 0; i < strings.length; i++) { | ||
| out += strings[i] ?? ''; | ||
| if (i < exprs.length) out += String(exprs[i]); | ||
| } | ||
| return out; | ||
| } | ||
|
|
||
| // Ajv's codegen exports `_` and `str` as tagged-template helpers. We return plain strings. | ||
| export function _(strings: TemplateStringsArray, ...exprs: unknown[]): string { | ||
| return interpolate(strings, exprs); | ||
| } | ||
|
|
||
| export function str(strings: TemplateStringsArray, ...exprs: unknown[]): string { | ||
| return interpolate(strings, exprs); | ||
| } | ||
|
|
||
| export function or(...parts: unknown[]): string { | ||
| return parts.map((p) => String(p)).join(' || '); | ||
| } | ||
|
|
||
| export function and(...parts: unknown[]): string { | ||
| return parts.map((p) => String(p)).join(' && '); | ||
| } | ||
|
|
||
| export function getProperty(prop: unknown): string { | ||
| if (typeof prop === 'string' && /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(prop)) return `.${prop}`; | ||
| const safe = String(prop).replace(/[\\'\n\r\u2028\u2029]/g, (ch) => { | ||
| switch (ch) { | ||
| case '\\': | ||
| return '\\\\'; | ||
| case "'": | ||
| return "\\'"; | ||
| case '\n': | ||
| return '\\n'; | ||
| case '\r': | ||
| return '\\r'; | ||
| case '\u2028': | ||
| return '\\u2028'; | ||
| case '\u2029': | ||
| return '\\u2029'; | ||
| default: | ||
| return ch; | ||
| } | ||
| }); | ||
| return `['${safe}']`; | ||
| } | ||
|
|
||
| // Minimal KeywordCxt shape used by ajv-formats at runtime. | ||
| export class KeywordCxt { | ||
| public $data = false; | ||
| public schemaCode: string = '""'; | ||
| public schema: unknown = undefined; | ||
|
|
||
| constructor(_it: unknown, _def: unknown, _keyword: string) {} | ||
| } |
This file contains hidden or 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,19 @@ | ||
| // CSP-safe stub for `ajv-formats`. | ||
| // | ||
| // Some dependencies import `ajv-formats` (often via `ajv-formats/dist/formats`). | ||
| // The real package expects full Ajv internals/codegen to exist and can crash in MV3. | ||
| // This shim ensures those imports are no-ops. | ||
|
|
||
| export type AjvLike = { | ||
| addFormat?: (name: string, format: unknown) => unknown; | ||
| addKeyword?: (keyword: string, definition?: unknown) => unknown; | ||
| }; | ||
|
|
||
| export default function addFormats(ajv: AjvLike, _opts?: unknown): AjvLike { | ||
| return ajv; | ||
| } | ||
|
|
||
| // Common ajv-formats named exports (keep harmless). | ||
| export const formats: Record<string, unknown> = {}; | ||
| export const fullFormats: Record<string, unknown> = {}; | ||
| export const fastFormats: Record<string, unknown> = {}; |
This file contains hidden or 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,60 @@ | ||
| // src/shims/ajv.ts | ||
|
|
||
| export class Name { | ||
| public str: string; | ||
|
|
||
| constructor(s: unknown) { | ||
| this.str = String(s); | ||
| } | ||
|
|
||
| toString() { | ||
| return this.str; | ||
| } | ||
| } | ||
|
|
||
| export default class Ajv { | ||
| constructor(options?: any) {} | ||
|
|
||
| // CRITICAL: Return 'this' to allow method chaining (e.g. ajv.addSchema().compile()) | ||
| addSchema(schema: any, key?: string) { | ||
| return this; | ||
| } | ||
| addKeyword(keyword: string, definition?: any) { | ||
| return this; | ||
| } | ||
| addFormat(name: string, format: any) { | ||
| return this; | ||
| } | ||
|
|
||
| // The actual validator | ||
| compile(schema: any) { | ||
|
insane66613 marked this conversation as resolved.
|
||
| // In development, warn that this CSP-safe shim bypasses real validation. | ||
| if ( | ||
| typeof process !== 'undefined' && | ||
| process.env && | ||
| process.env.NODE_ENV === 'development' | ||
| ) { | ||
| // eslint-disable-next-line no-console | ||
| console.warn( | ||
| '[Ajv shim] Using CSP-compatible Ajv shim: all validations will always pass. ' + | ||
| 'This is intended for the Chrome extension environment and may mask schema errors.', | ||
| ); | ||
| } | ||
| // Return a function that always says "Valid" (true) | ||
| return (data: any) => true; | ||
| } | ||
|
|
||
| // Direct validation | ||
| validate(schema: any, data: any) { | ||
| return true; | ||
| } | ||
|
|
||
| // Properties | ||
| errors = null; | ||
| } | ||
|
|
||
| // Export dummy codegen helpers just in case something imports them. | ||
| export const _ = () => {}; | ||
| export const str = () => {}; | ||
|
insane66613 marked this conversation as resolved.
|
||
| export const nil = {}; | ||
| export const CodeGen = {}; | ||
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.