fix(packaging): expose Ajv from CJS validator subpaths (#2431)
This commit is contained in:
parent
ddba550fa9
commit
1b90c96d11
4 changed files with 37 additions and 3 deletions
6
.changeset/cjs-ajv-validator-subpath.md
Normal file
6
.changeset/cjs-ajv-validator-subpath.md
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
---
|
||||
'@modelcontextprotocol/server': patch
|
||||
'@modelcontextprotocol/client': patch
|
||||
---
|
||||
|
||||
Fix the CommonJS `validators/ajv` subpath so reading the exported `Ajv` class no longer throws `ReferenceError: import_ajv is not defined`. The subpath now re-exports the bundled provider's concrete `Ajv` value in CJS output, matching the existing ESM behavior.
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
import { execFileSync } from 'node:child_process';
|
||||
import { existsSync, readFileSync } from 'node:fs';
|
||||
import { createRequire } from 'node:module';
|
||||
import { dirname, join } from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
|
||||
|
|
@ -7,6 +8,7 @@ import { beforeAll, describe, expect, test } from 'vitest';
|
|||
|
||||
const pkgDir = join(dirname(fileURLToPath(import.meta.url)), '../..');
|
||||
const distDir = join(pkgDir, 'dist');
|
||||
const requireDist = createRequire(join(pkgDir, 'package.json'));
|
||||
const NODE_ONLY = /\b(child_process|cross-spawn|node:stream|node:child_process)\b/;
|
||||
// Anchored at start-of-line so JSDoc-example `from 'ajv'` strings in vendored chunks don't match.
|
||||
const VALIDATOR_BACKEND_IMPORT = /^import[^\n]*?from\s+["'](?:ajv|ajv-formats|@cfworker\/json-schema)["']/m;
|
||||
|
|
@ -67,4 +69,15 @@ describe('@modelcontextprotocol/client root entry is browser-safe', () => {
|
|||
}
|
||||
}
|
||||
});
|
||||
|
||||
test('CJS AJV validator subpath exposes Ajv at runtime', () => {
|
||||
const { Ajv, AjvJsonSchemaValidator, addFormats } = requireDist(
|
||||
'./dist/validators/ajv.cjs'
|
||||
) as typeof import('../../src/validators/ajv');
|
||||
|
||||
const ajv = new Ajv({ strict: false, allErrors: true });
|
||||
addFormats(ajv);
|
||||
|
||||
expect(new AjvJsonSchemaValidator(ajv)).toBeInstanceOf(AjvJsonSchemaValidator);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
* AJV-based JSON Schema validator provider
|
||||
*/
|
||||
|
||||
import { Ajv as Draft7Ajv } from 'ajv';
|
||||
import { Ajv2020 } from 'ajv/dist/2020.js';
|
||||
import _addFormats from 'ajv-formats';
|
||||
|
||||
|
|
@ -31,6 +32,7 @@ interface AjvValidateFunction {
|
|||
errors?: any;
|
||||
}
|
||||
|
||||
/** `ajv-formats` default export, normalised through the CJS/ESM interop wrapper. */
|
||||
const addFormats = _addFormats as unknown as typeof _addFormats.default;
|
||||
|
||||
function createDefaultAjvInstance(): AjvLike {
|
||||
|
|
@ -152,6 +154,6 @@ export class AjvJsonSchemaValidator implements jsonSchemaValidator {
|
|||
* declaration bundling — see #2339). To construct a custom 2020-12 instance, add `ajv` to your own
|
||||
* dependencies (matching the SDK's pinned version) and `import { Ajv2020 } from 'ajv/dist/2020.js'`.
|
||||
*/
|
||||
export { Ajv } from 'ajv';
|
||||
/** `ajv-formats` default export, normalised through the CJS/ESM interop wrapper. */
|
||||
export { addFormats };
|
||||
const Ajv = Draft7Ajv;
|
||||
|
||||
export { addFormats, Ajv };
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
import { execFileSync } from 'node:child_process';
|
||||
import { existsSync, readFileSync } from 'node:fs';
|
||||
import { createRequire } from 'node:module';
|
||||
import { dirname, join } from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
|
||||
|
|
@ -7,6 +8,7 @@ import { beforeAll, describe, expect, test } from 'vitest';
|
|||
|
||||
const pkgDir = join(dirname(fileURLToPath(import.meta.url)), '../..');
|
||||
const distDir = join(pkgDir, 'dist');
|
||||
const requireDist = createRequire(join(pkgDir, 'package.json'));
|
||||
const NODE_ONLY = /\b(child_process|cross-spawn|node:stream|node:child_process)\b/;
|
||||
// Anchored at start-of-line so JSDoc-example `from 'ajv'` strings in vendored chunks don't match.
|
||||
const VALIDATOR_BACKEND_IMPORT = /^import[^\n]*?from\s+["'](?:ajv|ajv-formats|@cfworker\/json-schema)["']/m;
|
||||
|
|
@ -68,4 +70,15 @@ describe('@modelcontextprotocol/server root entry is browser-safe', () => {
|
|||
}
|
||||
}
|
||||
});
|
||||
|
||||
test('CJS AJV validator subpath exposes Ajv at runtime', () => {
|
||||
const { Ajv, AjvJsonSchemaValidator, addFormats } = requireDist(
|
||||
'./dist/validators/ajv.cjs'
|
||||
) as typeof import('../../src/validators/ajv');
|
||||
|
||||
const ajv = new Ajv({ strict: false, allErrors: true });
|
||||
addFormats(ajv);
|
||||
|
||||
expect(new AjvJsonSchemaValidator(ajv)).toBeInstanceOf(AjvJsonSchemaValidator);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in a new issue