mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-23 08:23:12 +09:00
7 files changed
+63
-9
No files matched your search
@@ -10,6 +10,8 @@ const yargs = require("yargs");
|
|||||||
const version = require("./package").version;
|
const version = require("./package").version;
|
||||||
const setBlocking = require("set-blocking");
|
const setBlocking = require("set-blocking");
|
||||||
|
|
||||||
|
// Reduce startup time by lazy compiling schemas.
|
||||||
|
global.lazySchemaCompilation = true;
|
||||||
const lib = require("@clusterio/lib");
|
const lib = require("@clusterio/lib");
|
||||||
const { ConsoleTransport, levels, logger } = lib;
|
const { ConsoleTransport, levels, logger } = lib;
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
src/data/message_validate.js
|
||||||
Whitespace-only changes.
@@ -4,6 +4,10 @@
|
|||||||
"version": "2.0.0-alpha.13",
|
"version": "2.0.0-alpha.13",
|
||||||
"repository": "https://github.com/clusterio/clusterio",
|
"repository": "https://github.com/clusterio/clusterio",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
"scripts": {
|
||||||
|
"prepare": "pnpm run compile_validator",
|
||||||
|
"compile_validator": "node scripts/compile_validator.js"
|
||||||
|
},
|
||||||
"browser": "browser.js",
|
"browser": "browser.js",
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"factorio"
|
"factorio"
|
||||||
|
|||||||
@@ -0,0 +1,28 @@
|
|||||||
|
"use strict";
|
||||||
|
const fs = require("fs-extra");
|
||||||
|
const path = require("path");
|
||||||
|
const Ajv = require("ajv");
|
||||||
|
const standaloneCode = require("ajv/dist/standalone").default;
|
||||||
|
|
||||||
|
/* eslint-disable node/no-sync */
|
||||||
|
|
||||||
|
// Provide an empty file for message_core to load
|
||||||
|
fs.outputFileSync(path.join(__dirname, "../src/data/message_validate.js"), "");
|
||||||
|
|
||||||
|
const { Message } = require("../src/data/messages_core");
|
||||||
|
|
||||||
|
const ajv = new Ajv({
|
||||||
|
allowUnionTypes: true,
|
||||||
|
strict: "log",
|
||||||
|
strictTuples: false,
|
||||||
|
verbose: true,
|
||||||
|
code: {
|
||||||
|
source: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
const validate = ajv.compile(Message.jsonSchema);
|
||||||
|
let moduleCode = standaloneCode(ajv, validate);
|
||||||
|
fs.outputFileSync(
|
||||||
|
path.join(__dirname, "../src/data/message_validate.js"),
|
||||||
|
`// generated by scripts/compile_validator.js\n/* eslint-disable */\n${moduleCode}`
|
||||||
|
);
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
|
// Note: Run compile_validator script after making changes to this file.
|
||||||
"use strict";
|
"use strict";
|
||||||
const libSchema = require("../schema");
|
const message_validate = require("./message_validate.js");
|
||||||
|
|
||||||
|
|
||||||
/** @memberof module:lib */
|
/** @memberof module:lib */
|
||||||
@@ -851,7 +852,7 @@ Message.jsonSchema = {
|
|||||||
MessageDisconnect.jsonSchema,
|
MessageDisconnect.jsonSchema,
|
||||||
],
|
],
|
||||||
};
|
};
|
||||||
Message.validate = libSchema.compile(Message.jsonSchema);
|
Message.validate = message_validate;
|
||||||
|
|
||||||
/** @memberof module:lib */
|
/** @memberof module:lib */
|
||||||
class PingRequest {
|
class PingRequest {
|
||||||
|
|||||||
@@ -5,18 +5,36 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
const Ajv = require("ajv");
|
const Ajv = require("ajv");
|
||||||
const ajv = new Ajv({
|
const ajv = new Ajv({
|
||||||
|
// These are duplicated in scripts/compile_validator
|
||||||
allowUnionTypes: true,
|
allowUnionTypes: true,
|
||||||
strict: "log",
|
strict: "log",
|
||||||
strictTuples: false,
|
strictTuples: false,
|
||||||
verbose: true,
|
verbose: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Compile JSON schema into validator
|
||||||
|
* @function
|
||||||
|
* @param {Object} schema - JSON schema to create validator for.
|
||||||
|
* @returns {Function} Validator for the schema.
|
||||||
|
*/
|
||||||
|
function compile(schema) {
|
||||||
|
if (typeof global !== "object" || !global.lazySchemaCompilation) {
|
||||||
|
return ajv.compile(schema);
|
||||||
|
}
|
||||||
|
|
||||||
|
let doValidate;
|
||||||
|
function validate(data) {
|
||||||
|
if (!doValidate) {
|
||||||
|
doValidate = ajv.compile(schema);
|
||||||
|
}
|
||||||
|
let result = doValidate(data);
|
||||||
|
validate.errors = doValidate.errors;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
return validate;
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
/**
|
compile,
|
||||||
* Compile JSON schema into validator
|
|
||||||
* @function
|
|
||||||
* @param {Object} schema - JSON schema to create validator for.
|
|
||||||
* @returns {Function} Validator for the schema.
|
|
||||||
*/
|
|
||||||
compile: ajv.compile.bind(ajv),
|
|
||||||
};
|
};
|
||||||
Reference in new issue
Block a user