Skip to content

Commit

Permalink
pulling in AjvUtilities from Docs.json.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
SignpostMarv committed Jun 30, 2024
1 parent d540ae3 commit 4d16604
Show file tree
Hide file tree
Showing 8 changed files with 329 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "nodejs template",
"name": "Ajv utilities",
"build": {
"dockerfile": "Dockerfile"
},
Expand Down
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
[![Coverage Status](https://coveralls.io/repos/github/satisfactory-dev/ajv-utilities/badge.svg?branch=main)](https://coveralls.io/github/satisfactory-dev/ajv-utilities?branch=main)
[![Workflow Status](https://github.com/satisfactory-dev/ajv-utilities/actions/workflows/node.js.yml/badge.svg?branch=main)](https://github.com/satisfactory-dev/ajv-utilities/actions/workflows/node.js.yml?query=branch%3Amain)

# Installation

`npm install --save-dev @satisfactory-dev/ajv-utlities`

# Usage

## `compile`

```ts
import Ajv from 'ajv/dist/2020';
import {compile, FailedToCompileSchema} from '@satisfactory-dev/ajv-utlities';

const schema = {type: 'string'};

try {
const check = compile(new Ajv(), schema);
} catch (err) {
if (err instanceof FailedToCompileSchema) {
console.error('failed to compile', err);
} else {
console.error('unknown error', err);
}
}
```

## `esmify`

```ts
import Ajv from 'ajv/dist/2020';
import standalone from 'ajv/dist/standalone';
import {compile, esmify} from '@satisfactory-dev/ajv-utlities';

const ajv = new Ajv();

await writeFile(
'./standalone-validator.mjs',
esmify(standalone(ajv, compile(ajv, schema)))
);
```
1 change: 1 addition & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './src/AjvUtilities';
88 changes: 69 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"type": "module",
"name": "@signpostmarv/nodejs-template",
"description": "nodejs template",
"name": "@satisfactory-dev/ajv-utlities",
"description": "Ajv Utlities",
"license": "Apache-2.0",
"repository": {
"type": "git",
"url": "git+https://github.com/SignpostMarv/nodejs-template.git"
"url": "git+https://github.com/satisfactory-dev/ajv-utlities.git"
},
"funding": "https://github.com/SignpostMarv/nodejs-template?sponsor=1",
"funding": "https://github.com/satisfactory-dev/ajv-utlities?sponsor=1",
"exports": "./index.js",
"types": "./index.d.ts",
"devDependencies": {
Expand All @@ -19,5 +19,8 @@
"ts-node": "^10.9.2",
"typescript": "~5.5.2",
"typescript-eslint": "^7.14.1"
},
"dependencies": {
"ajv": "^8.16.0"
}
}
92 changes: 92 additions & 0 deletions src/AjvUtilities.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import Ajv, {
SchemaObject,
ValidateFunction,
} from 'ajv/dist/2020';

const cache = new WeakMap<SchemaObject, ValidateFunction>();

const duplicate_compile_check: {[key: string]: ValidateFunction} = {};

export class FailedToCompileSchema extends Error
{
readonly err:unknown;
readonly schema:SchemaObject;

constructor(
schema:SchemaObject,
err:unknown,
message = 'Failed to compile schema'
) {
super(message);
this.schema = schema;
this.err = err;
}
}

export function compile<T>(
ajv:Ajv,
schema:SchemaObject,
) : ValidateFunction<T> {
try {
performance.mark('ajv compile start');

if (cache.has(schema)) {
const result = cache.get(schema) as ValidateFunction<T>;

performance.measure('ajv compile cached', 'ajv compile start');

return result;
}

const key = JSON.stringify(schema);
if (key in duplicate_compile_check) {
performance.measure('ajv compile duplicate', 'ajv compile start');

return duplicate_compile_check[key] as ValidateFunction<T>;
}

const result = ajv.compile<T>(schema);
cache.set(schema, result);
duplicate_compile_check[key] = result;

performance.measure('ajv compile', 'ajv compile start');

return result;
} catch (err) {
throw new FailedToCompileSchema(schema, err);
}
}

export function esmify(code:string): string
{
const replacements:{[key: string]: [string, string]} = {
'require("ajv/dist/runtime/equal").default': [
'fast_deep_equal',
`import fast_deep_equal from 'fast-deep-equal';`,
],
'require("ajv/dist/runtime/ucs2length").default': [
'ucs2length.default',
`import ucs2length from 'ajv/dist/runtime/ucs2length';`,
],
};

const import_these:string[] = [];

for (const entry of Object.entries(replacements)) {
const [look_for, [replace_with, add_to_import]] = entry;

if (code.includes(look_for)) {
code = code.replace(look_for, replace_with);
import_these.push(add_to_import);
}
}

if (import_these.length) {
code = code.replace(/^"use strict";/, [
'"use strict";',
...import_these,
].join(''));
}

return code;
}
1 change: 1 addition & 0 deletions tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ let already_stopped = false;

run({
files: [
`${__dirname}/tests/src/AjvUtilities.spec.ts`,
],
concurrency: true,
signal: ac.signal,
Expand Down
Loading

0 comments on commit 4d16604

Please sign in to comment.