@polywrap/wrap-manifest-types-js
Advanced tools
Comparing version
@@ -8,2 +8,2 @@ /** | ||
import { DeserializeManifestOptions } from "../../"; | ||
export declare function deserializeWrapManifest(manifest: Uint8Array, options?: DeserializeManifestOptions): WrapManifest; | ||
export declare function deserializeWrapManifest(manifest: Uint8Array, options?: DeserializeManifestOptions): Promise<WrapManifest>; |
@@ -8,2 +8,57 @@ "use strict"; | ||
*/ | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
}); | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
}; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
var __generator = (this && this.__generator) || function (thisArg, body) { | ||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; | ||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; | ||
function verb(n) { return function (v) { return step([n, v]); }; } | ||
function step(op) { | ||
if (f) throw new TypeError("Generator is already executing."); | ||
while (_) try { | ||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [op[0] & 2, t.value]; | ||
switch (op[0]) { | ||
case 0: case 1: t = op; break; | ||
case 4: _.label++; return { value: op[1], done: false }; | ||
case 5: _.label++; y = op[1]; op = [0]; continue; | ||
case 7: op = _.ops.pop(); _.trys.pop(); continue; | ||
default: | ||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } | ||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } | ||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } | ||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } | ||
if (t[2]) _.ops.pop(); | ||
_.trys.pop(); continue; | ||
} | ||
op = body.call(thisArg, _); | ||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } | ||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; | ||
} | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -13,29 +68,40 @@ exports.deserializeWrapManifest = void 0; | ||
var msgpack_js_1 = require("@polywrap/msgpack-js"); | ||
var semver_1 = require("semver"); | ||
var Semver = __importStar(require("semver")); | ||
function deserializeWrapManifest(manifest, options) { | ||
var anyWrapManifest; | ||
try { | ||
anyWrapManifest = msgpack_js_1.msgpackDecode(manifest); | ||
} | ||
catch (e) { | ||
throw Error("Unable to parse WrapManifest: " + ("[" + new Uint8Array(manifest).toString() + "]")); | ||
} | ||
if (!options || !options.noValidate) { | ||
_1.validateWrapManifest(anyWrapManifest); | ||
} | ||
var versionCompare = semver_1.compare(anyWrapManifest.version, _1.latestWrapManifestVersion); | ||
if (versionCompare === -1) { | ||
// Upgrade | ||
return _1.migrateWrapManifest(anyWrapManifest, _1.latestWrapManifestVersion); | ||
} | ||
else if (versionCompare === 1) { | ||
// Downgrade | ||
throw Error("Cannot downgrade WrapManifest version " + anyWrapManifest.version + ", please upgrade your WRAP package."); | ||
} | ||
else { | ||
// Latest | ||
return anyWrapManifest; | ||
} | ||
return __awaiter(this, void 0, void 0, function () { | ||
var anyWrapManifest, versionCompare; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
try { | ||
anyWrapManifest = msgpack_js_1.msgpackDecode(manifest); | ||
} | ||
catch (e) { | ||
throw Error("Unable to parse WrapManifest: " + ("[" + new Uint8Array(manifest).toString() + "]")); | ||
} | ||
if (!(!options || !options.noValidate)) return [3 /*break*/, 2]; | ||
return [4 /*yield*/, _1.validateWrapManifest(anyWrapManifest)]; | ||
case 1: | ||
_a.sent(); | ||
_a.label = 2; | ||
case 2: | ||
versionCompare = Semver.compare(Semver.coerce(anyWrapManifest.version) || anyWrapManifest.version, Semver.coerce(_1.latestWrapManifestVersion) || _1.latestWrapManifestVersion); | ||
if (versionCompare === -1) { | ||
// Upgrade | ||
return [2 /*return*/, _1.migrateWrapManifest(anyWrapManifest, _1.latestWrapManifestVersion)]; | ||
} | ||
else if (versionCompare === 1) { | ||
// Downgrade | ||
throw Error("Cannot downgrade WrapManifest version " + anyWrapManifest.version + ", please upgrade your WRAP package."); | ||
} | ||
else { | ||
// Latest | ||
return [2 /*return*/, anyWrapManifest]; | ||
} | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
} | ||
exports.deserializeWrapManifest = deserializeWrapManifest; | ||
//# sourceMappingURL=deserialize.js.map |
@@ -6,12 +6,22 @@ /** | ||
*/ | ||
import { WrapManifest as WrapManifest0_1_0 } from "./0.1.0"; | ||
export { WrapManifest0_1_0, }; | ||
import { WrapManifest as WrapManifest_0_1, Abi as WrapAbi_0_1_0_1 } from "./0.1"; | ||
export { WrapManifest_0_1, WrapAbi_0_1_0_1 }; | ||
export declare enum WrapManifestVersions { | ||
"0.1.0" = "0.1.0" | ||
"v0.1.0" = "0.1.0", | ||
"v0.1" = "0.1" | ||
} | ||
export declare type AnyWrapManifest = WrapManifest0_1_0; | ||
export declare type WrapManifest = WrapManifest0_1_0; | ||
export declare const latestWrapManifestVersion = WrapManifestVersions["0.1.0"]; | ||
export declare enum WrapManifestAbiVersions { | ||
"v0.1" = "0.1" | ||
} | ||
export declare enum WrapAbiVersions { | ||
"v0.1" = "0.1" | ||
} | ||
export declare type AnyWrapManifest = WrapManifest_0_1; | ||
export declare type AnyWrapAbi = WrapAbi_0_1_0_1; | ||
export declare type WrapManifest = WrapManifest_0_1; | ||
export declare type WrapAbi = WrapAbi_0_1_0_1; | ||
export declare const latestWrapManifestVersion = WrapManifestVersions["v0.1"]; | ||
export declare const latestWrapAbiVersion = "0.1"; | ||
export { migrateWrapManifest } from "./migrate"; | ||
export { deserializeWrapManifest } from "./deserialize"; | ||
export { validateWrapManifest } from "./validate"; |
@@ -9,8 +9,22 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.validateWrapManifest = exports.deserializeWrapManifest = exports.migrateWrapManifest = exports.latestWrapManifestVersion = exports.WrapManifestVersions = void 0; | ||
exports.validateWrapManifest = exports.deserializeWrapManifest = exports.migrateWrapManifest = exports.latestWrapAbiVersion = exports.latestWrapManifestVersion = exports.WrapAbiVersions = exports.WrapManifestAbiVersions = exports.WrapManifestVersions = void 0; | ||
var WrapManifestVersions; | ||
(function (WrapManifestVersions) { | ||
WrapManifestVersions["0.1.0"] = "0.1.0"; | ||
// NOTE: Patch fix for backwards compatability | ||
WrapManifestVersions["v0.1.0"] = "0.1.0"; | ||
WrapManifestVersions["v0.1"] = "0.1"; | ||
})(WrapManifestVersions = exports.WrapManifestVersions || (exports.WrapManifestVersions = {})); | ||
exports.latestWrapManifestVersion = WrapManifestVersions["0.1.0"]; | ||
; | ||
var WrapManifestAbiVersions; | ||
(function (WrapManifestAbiVersions) { | ||
WrapManifestAbiVersions["v0.1"] = "0.1"; | ||
})(WrapManifestAbiVersions = exports.WrapManifestAbiVersions || (exports.WrapManifestAbiVersions = {})); | ||
; | ||
var WrapAbiVersions; | ||
(function (WrapAbiVersions) { | ||
WrapAbiVersions["v0.1"] = "0.1"; | ||
})(WrapAbiVersions = exports.WrapAbiVersions || (exports.WrapAbiVersions = {})); | ||
; | ||
exports.latestWrapManifestVersion = WrapManifestVersions["v0.1"]; | ||
exports.latestWrapAbiVersion = "0.1"; | ||
var migrate_1 = require("./migrate"); | ||
@@ -17,0 +31,0 @@ Object.defineProperty(exports, "migrateWrapManifest", { enumerable: true, get: function () { return migrate_1.migrateWrapManifest; } }); |
@@ -8,2 +8,2 @@ /** | ||
import { Schema } from "jsonschema"; | ||
export declare function validateWrapManifest(manifest: AnyWrapManifest, extSchema?: Schema | undefined): void; | ||
export declare function validateWrapManifest(manifest: AnyWrapManifest, extSchema?: Schema | undefined): Promise<void>; |
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
var __generator = (this && this.__generator) || function (thisArg, body) { | ||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; | ||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; | ||
function verb(n) { return function (v) { return step([n, v]); }; } | ||
function step(op) { | ||
if (f) throw new TypeError("Generator is already executing."); | ||
while (_) try { | ||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [op[0] & 2, t.value]; | ||
switch (op[0]) { | ||
case 0: case 1: t = op; break; | ||
case 4: _.label++; return { value: op[1], done: false }; | ||
case 5: _.label++; y = op[1]; op = [0]; continue; | ||
case 7: op = _.ops.pop(); _.trys.pop(); continue; | ||
default: | ||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } | ||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } | ||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } | ||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } | ||
if (t[2]) _.ops.pop(); | ||
_.trys.pop(); continue; | ||
} | ||
op = body.call(thisArg, _); | ||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } | ||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; | ||
} | ||
}; | ||
var __read = (this && this.__read) || function (o, n) { | ||
@@ -27,27 +63,84 @@ var m = typeof Symbol === "function" && o[Symbol.iterator]; | ||
exports.validateWrapManifest = void 0; | ||
var _0_1_0_json_1 = __importDefault(require("@polywrap/wrap-manifest-schemas/formats/wrap.info/0.1.0.json")); | ||
var _0_1_json_1 = __importDefault(require("@polywrap/wrap-manifest-schemas/formats/wrap.info/0.1.json")); | ||
var _0_1_json_2 = __importDefault(require("@polywrap/wrap-manifest-schemas/formats/abi/0.1.json")); | ||
var jsonschema_1 = require("jsonschema"); | ||
var json_schema_ref_parser_1 = require("json-schema-ref-parser"); | ||
var path_1 = __importDefault(require("path")); | ||
var schemas = { | ||
"0.1.0": _0_1_0_json_1.default, | ||
// NOTE: Patch fix for backwards compatability | ||
"0.1.0": _0_1_json_1.default, | ||
"0.1": _0_1_json_1.default, | ||
}; | ||
var validator = new jsonschema_1.Validator(); | ||
var abiSchemas = { | ||
// NOTE: Patch fix for backwards compatability | ||
"0.1.0": _0_1_json_2.default, | ||
"0.1": _0_1_json_2.default, | ||
}; | ||
function throwIfErrors(result, version) { | ||
if (result.errors.length) { | ||
throw new Error(__spread([ | ||
"Validation errors encountered while sanitizing WrapManifest version " + version | ||
], result.errors.map(function (error) { return error.toString(); })).join("\n")); | ||
} | ||
} | ||
function validateWrapManifest(manifest, extSchema) { | ||
if (extSchema === void 0) { extSchema = undefined; } | ||
var schema = schemas[manifest.version]; | ||
if (!schema) { | ||
throw Error("Unrecognized WrapManifest schema version \"" + manifest.version + "\"\nmanifest: " + JSON.stringify(manifest, null, 2)); | ||
} | ||
var throwIfErrors = function (result) { | ||
if (result.errors.length) { | ||
throw new Error(__spread([ | ||
"Validation errors encountered while sanitizing WrapManifest version " + manifest.version | ||
], result.errors.map(function (error) { return error.toString(); })).join("\n")); | ||
} | ||
}; | ||
throwIfErrors(validator.validate(manifest, schema)); | ||
if (extSchema) { | ||
throwIfErrors(validator.validate(manifest, extSchema)); | ||
} | ||
return __awaiter(this, void 0, void 0, function () { | ||
var schema, abiSchema, abiJsonSchemaRelPath, finalSchema, refs, validator, resolveRefs; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
schema = schemas[manifest.version]; | ||
abiSchema = abiSchemas[manifest.version]; | ||
if (!schema || !abiSchema) { | ||
throw new Error("Unrecognized WrapManifest schema version \"" + manifest.version + "\"\nmanifest: " + JSON.stringify(manifest, null, 2)); | ||
} | ||
if (!schema.properties) { | ||
// This should never happen | ||
throw new Error("WrapManifest schema doesn't contain any properties"); | ||
} | ||
abiJsonSchemaRelPath = schema.properties.abi.$ref; | ||
return [4 /*yield*/, json_schema_ref_parser_1.bundle(schema, { | ||
resolve: { | ||
file: { | ||
read: function (file) { | ||
// If both url is same | ||
if (!path_1.default.relative(abiJsonSchemaRelPath, file.url)) { | ||
return abiSchema; | ||
} | ||
return file.data; | ||
}, | ||
}, | ||
}, | ||
})]; | ||
case 1: | ||
finalSchema = _a.sent(); | ||
return [4 /*yield*/, json_schema_ref_parser_1.resolve(finalSchema)]; | ||
case 2: | ||
refs = _a.sent(); | ||
validator = new jsonschema_1.Validator(); | ||
validator.addSchema(finalSchema); | ||
resolveRefs = function () { | ||
var unresolvedRef = validator.unresolvedRefs.shift(); | ||
if (!unresolvedRef) | ||
return; | ||
var relRefIdx = unresolvedRef.indexOf("#"); | ||
var relRef = unresolvedRef.slice(relRefIdx); | ||
var resolvedSchema = refs.get(relRef); | ||
if (!resolvedSchema) | ||
throw new Error("Failed to resolve the ref: " + relRef); | ||
validator.addSchema(resolvedSchema, unresolvedRef); | ||
resolveRefs(); | ||
}; | ||
resolveRefs(); | ||
throwIfErrors(validator.validate(manifest, finalSchema), manifest.version); | ||
if (extSchema) { | ||
throwIfErrors(validator.validate(manifest, extSchema), manifest.version); | ||
} | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
} | ||
exports.validateWrapManifest = validateWrapManifest; | ||
//# sourceMappingURL=validate.js.map |
{ | ||
"name": "@polywrap/wrap-manifest-types-js", | ||
"description": "WRAP Manifest TypeScript Typings", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"license": "MIT", | ||
@@ -19,4 +19,5 @@ "repository": { | ||
"dependencies": { | ||
"@polywrap/msgpack-js": "0.2.0", | ||
"@polywrap/wrap-manifest-schemas": "0.2.0", | ||
"@polywrap/msgpack-js": "0.3.0", | ||
"@polywrap/wrap-manifest-schemas": "0.3.0", | ||
"json-schema-ref-parser": "9.0.9", | ||
"jsonschema": "1.4.0", | ||
@@ -26,7 +27,9 @@ "semver": "7.3.5" | ||
"devDependencies": { | ||
"@polywrap/os-js": "0.2.0", | ||
"@polywrap/os-js": "0.3.0", | ||
"@types/jest": "26.0.8", | ||
"@types/mustache": "4.0.1", | ||
"@types/prettier": "2.6.0", | ||
"@types/semver": "7.3.8", | ||
"jest": "26.6.3", | ||
"json-schema-to-typescript": "10.1.3", | ||
"json-schema-to-typescript": "11.0.1", | ||
"mustache": "4.0.1", | ||
@@ -33,0 +36,0 @@ "rimraf": "3.0.2", |
@@ -1,39 +0,79 @@ | ||
import path from "path" | ||
import path from "path"; | ||
import fs from "fs"; | ||
import * as os from "@polywrap/os-js"; | ||
import Mustache from "mustache"; | ||
import { compile } from "json-schema-to-typescript" | ||
import { compile } from "json-schema-to-typescript"; | ||
import { FileInfo, bundle, JSONSchema } from "json-schema-ref-parser"; | ||
async function generateFormatTypes() { | ||
// Fetch all schemas within the @polywrap/wrap-manifest-schemas/formats directory | ||
const schemasPackageDir = path.dirname(require.resolve("@polywrap/wrap-manifest-schemas")); | ||
const formatsDir = path.join(schemasPackageDir, "formats/wrap.info"); | ||
const formatOutputDir = path.join(__dirname, `../src/formats/wrap.info`); | ||
// Fetch all schemas within the @polywrap/wrap-manifest-schemas/schemas/formats directory | ||
const schemasPackageDir = path.dirname( | ||
require.resolve("@polywrap/wrap-manifest-schemas") | ||
); | ||
const formatsDir = path.join(schemasPackageDir, "formats"); | ||
// Get all JSON schemas for this format type (0.1.0, 0.2.0, etc) | ||
const formatSchemaFiles = fs.readdirSync(formatsDir); | ||
const formatSchemas: any[] = []; | ||
const formatModules: any[] = []; | ||
// Resolve json-schema to typescript for wrap format type | ||
const formatTypeName = "wrap.info"; | ||
const wrapDir = path.join(formatsDir, formatTypeName); | ||
const wrapOutputDir = path.join( | ||
__dirname, | ||
`../src/formats/${formatTypeName}` | ||
); | ||
const wrapModules: { | ||
interface: string; | ||
version: string; | ||
abiVersion: string; | ||
}[] = []; | ||
for (let k = 0; k < formatSchemaFiles.length; ++k) { | ||
const formatSchemaName = formatSchemaFiles[k]; | ||
const formatVersion = formatSchemaName.replace(".json", ""); | ||
const formatSchemaPath = path.join(formatsDir, formatSchemaName); | ||
// Get all JSON schemas for this format type (v1, v2, etc) | ||
const wrapSchemaFiles = fs.readdirSync(wrapDir); | ||
const wrapSchemas: JSONSchema[] = []; | ||
for (let k = 0; k < wrapSchemaFiles.length; ++k) { | ||
const wrapSchemaName = wrapSchemaFiles[k]; | ||
const wrapVersion = wrapSchemaName.replace(".json", ""); | ||
const wrapSchemaPath = path.join(wrapDir, wrapSchemaName); | ||
try { | ||
// Parse the JSON schema | ||
const formatSchema = JSON.parse( | ||
fs.readFileSync(formatSchemaPath, { encoding: "utf-8" }) | ||
const wrapSchema = JSON.parse( | ||
fs.readFileSync(wrapSchemaPath, { encoding: "utf-8" }) | ||
); | ||
formatSchemas.push(formatSchema); | ||
const abiJsonSchemaRelPath = wrapSchema.properties.abi.$ref; | ||
const abiJsonSchemaPath = path.join(wrapDir, abiJsonSchemaRelPath); | ||
const abiJsonSchema = JSON.parse( | ||
fs.readFileSync(abiJsonSchemaPath, { encoding: "utf-8" }) | ||
); | ||
const abiVersion = path | ||
.parse(abiJsonSchemaPath) | ||
.base.replace(".json", ""); | ||
// Convert it to a TypeScript interface | ||
const tsFile = await compile( | ||
formatSchema, | ||
formatSchema.id | ||
const bundledSchema = await bundle(wrapSchema, { | ||
resolve: { | ||
file: { | ||
read: (file: FileInfo) => { | ||
// If both url is same | ||
if (!path.relative(abiJsonSchemaRelPath, file.url)) { | ||
return abiJsonSchema; | ||
} | ||
return file.data; | ||
}, | ||
}, | ||
}, | ||
}); | ||
const finalWrapSchema = JSON.parse( | ||
JSON.stringify(bundledSchema).replace( | ||
/unevaluatedProperties/g, | ||
"additionalProperties" | ||
) | ||
); | ||
wrapSchemas.push(finalWrapSchema); | ||
// Convert it to a TypeScript interface | ||
const tsFile = await compile(finalWrapSchema, wrapSchema.id); | ||
// Emit the result | ||
const tsOutputPath = path.join(formatOutputDir, `${formatVersion}.ts`); | ||
const tsOutputPath = path.join(wrapOutputDir, `${wrapVersion}.ts`); | ||
fs.mkdirSync(path.dirname(tsOutputPath), { recursive: true }); | ||
@@ -46,8 +86,12 @@ os.writeFileSync( | ||
// Add metadata for the root index.ts file to use | ||
formatModules.push({ | ||
interface: formatSchema.id, | ||
version: formatVersion | ||
wrapModules.push({ | ||
interface: wrapSchema.id, | ||
version: wrapVersion, | ||
abiVersion: abiVersion, | ||
}); | ||
} catch (error) { | ||
console.error(`Error generating the Manifest file ${formatSchemaPath}: `, error); | ||
console.error( | ||
`Error generating the Manifest file ${wrapSchemaPath}: `, | ||
error | ||
); | ||
throw error; | ||
@@ -59,3 +103,3 @@ } | ||
const tsTemplate = fs.readFileSync( | ||
path.join(__dirname, `./templates/${name}-ts.mustache`), | ||
path.join(__dirname, `/templates/${name}-ts.mustache`), | ||
{ encoding: "utf-8" } | ||
@@ -68,17 +112,19 @@ ); | ||
// Emit the source | ||
const tsOutputPath = path.join(formatOutputDir, `${name}.ts`); | ||
const tsOutputPath = path.join(wrapOutputDir, `${name}.ts`); | ||
fs.mkdirSync(path.dirname(tsOutputPath), { recursive: true }); | ||
os.writeFileSync(tsOutputPath, tsSrc); | ||
} | ||
}; | ||
const lastItem = (arr: any): unknown => arr[arr.length - 1]; | ||
const versionToTs = (version: any) => | ||
const lastItem = <T>(arr: Array<T>) => arr[arr.length - 1]; | ||
const versionToTs = (version: string) => | ||
version.replace(/\./g, "_").replace(/\-/g, "_"); | ||
const formats = formatModules.map((module) => { | ||
const formats = wrapModules.map((module) => { | ||
return { | ||
type: module.interface, | ||
version: module.version, | ||
tsVersion: versionToTs(module.version) | ||
} | ||
tsVersion: versionToTs(module.version), | ||
abiVersion: module.abiVersion, | ||
abiTsVersion: versionToTs(module.abiVersion), | ||
}; | ||
}); | ||
@@ -90,3 +136,3 @@ const latest = lastItem(formats); | ||
formats, | ||
latest | ||
latest, | ||
}; | ||
@@ -99,3 +145,3 @@ | ||
prevFormats: [...formats], | ||
latest: latest | ||
latest: latest, | ||
}; | ||
@@ -107,6 +153,10 @@ migrateContext.prevFormats.pop(); | ||
// Generate a deserialize.ts file that exports a deserialization function for the latest format version | ||
renderTemplate("deserialize", {}); | ||
const deserializeContext = { | ||
type: migrateContext.latest.type, | ||
}; | ||
renderTemplate("deserialize", deserializeContext); | ||
// Generate a validate.ts file that validates the manifest against the JSON schema | ||
const validateFormats = formatModules.map((module) => { | ||
const validateFormats = wrapModules.map((module) => { | ||
return { | ||
@@ -116,2 +166,5 @@ type: module.interface, | ||
tsVersion: versionToTs(module.version), | ||
abiVersion: module.abiVersion, | ||
abiTsVersion: versionToTs(module.abiVersion), | ||
dir: formatTypeName, | ||
}; | ||
@@ -123,9 +176,10 @@ }); | ||
latest: lastItem(validateFormats), | ||
validators: [] as string[] | ||
validators: [] as string[], | ||
}; | ||
for (let k = 0; k < formatSchemas.length; ++k) { | ||
const formatSchema = formatSchemas[k]; | ||
// Extract all validators | ||
for (let k = 0; k < wrapSchemas.length; ++k) { | ||
const formatSchema = wrapSchemas[k]; | ||
const getValidator = (obj: any) => { | ||
const getValidator = (obj: Record<string, unknown>) => { | ||
if (typeof obj !== "object") { | ||
@@ -143,7 +197,7 @@ return; | ||
for (let j = 0; j < keys.length; ++j) { | ||
getValidator(obj[keys[j]]); | ||
getValidator(obj[keys[j]] as Record<string, unknown>); | ||
} | ||
} | ||
}; | ||
getValidator(formatSchema); | ||
getValidator(formatSchema as Record<string, unknown>); | ||
} | ||
@@ -154,3 +208,3 @@ | ||
return Promise.resolve(); | ||
}; | ||
} | ||
@@ -161,5 +215,5 @@ generateFormatTypes() | ||
}) | ||
.catch(err => { | ||
.catch((err) => { | ||
console.error(err); | ||
process.abort(); | ||
}); |
@@ -1,66 +0,139 @@ | ||
import { | ||
WrapManifest, | ||
deserializeWrapManifest | ||
} from ".."; | ||
import { | ||
msgpackEncode | ||
} from "@polywrap/msgpack-js"; | ||
import { WrapManifest, deserializeWrapManifest } from ".."; | ||
import { msgpackEncode } from "@polywrap/msgpack-js"; | ||
const testManifest: WrapManifest = { | ||
abi: { | ||
objectTypes: [ | ||
{ | ||
type: "SampleResult", | ||
kind: 1, | ||
properties: [ | ||
{ | ||
type: "String", | ||
name: "value", | ||
required: true, | ||
kind: 34, | ||
scalar: { | ||
type: "String", | ||
name: "value", | ||
required: true, | ||
kind: 4, | ||
}, | ||
}, | ||
], | ||
interfaces: [], | ||
}, | ||
], | ||
enumTypes: [], | ||
interfaceTypes: [], | ||
importedObjectTypes: [], | ||
importedModuleTypes: [], | ||
importedEnumTypes: [], | ||
importedEnvTypes: [], | ||
moduleType: { | ||
type: "Module", | ||
kind: 128, | ||
methods: [ | ||
{ | ||
type: "Method", | ||
name: "sampleMethod", | ||
required: true, | ||
kind: 64, | ||
arguments: [ | ||
{ | ||
type: "String", | ||
name: "arg", | ||
required: true, | ||
kind: 34, | ||
scalar: { | ||
type: "String", | ||
name: "arg", | ||
required: true, | ||
kind: 4, | ||
}, | ||
}, | ||
], | ||
return: { | ||
type: "SampleResult", | ||
name: "sampleMethod", | ||
required: true, | ||
kind: 34, | ||
object: { | ||
type: "SampleResult", | ||
name: "sampleMethod", | ||
required: true, | ||
kind: 8192, | ||
}, | ||
}, | ||
}, | ||
], | ||
imports: [], | ||
interfaces: [], | ||
}, | ||
}, | ||
name: "template-wasm-as", | ||
type: "wasm", | ||
version: "0.1.0", | ||
type: "wasm", | ||
name: "dog-cat", | ||
abi: {} | ||
}; | ||
describe("Polywrap Manifest Validation", () => { | ||
it("Should succeed", () => { | ||
it("Should succeed", async () => { | ||
const manifest = msgpackEncode(testManifest); | ||
expect(deserializeWrapManifest(manifest)).toMatchObject(testManifest); | ||
expect(await deserializeWrapManifest(manifest)).toMatchObject(testManifest); | ||
}); | ||
it("Should throw incorrect version format error", () => { | ||
it("Should throw incorrect version format error", async () => { | ||
const manifest = msgpackEncode({ | ||
...testManifest, | ||
version: "bad-str" | ||
version: "bad-str", | ||
}); | ||
expect(() => deserializeWrapManifest(manifest)).toThrowError(/Unrecognized WrapManifest schema version/); | ||
await expect(() => deserializeWrapManifest(manifest)).rejects.toThrow( | ||
/Unrecognized WrapManifest schema version/ | ||
); | ||
}); | ||
it("Should throw not accepted field error", () => { | ||
it("Should throw not accepted field error", async () => { | ||
const manifest = msgpackEncode({ | ||
...testManifest, | ||
not_accepted_field: "not_accepted_field" | ||
not_accepted_field: "not_accepted_field", | ||
}); | ||
expect(() => deserializeWrapManifest(manifest)).toThrowError(/not allowed to have the additional property "not_accepted_field"/); | ||
await expect(() => deserializeWrapManifest(manifest)).rejects.toThrow( | ||
/not allowed to have the additional property "not_accepted_field"/ | ||
); | ||
}); | ||
it("Should throw required field missing error", () => { | ||
it("Should throw required field missing error", async () => { | ||
const manifest = msgpackEncode({ | ||
...testManifest, | ||
name: undefined | ||
name: undefined, | ||
}); | ||
expect(() => deserializeWrapManifest(manifest)).toThrowError(/instance requires property "name"/); | ||
await expect(() => deserializeWrapManifest(manifest)).rejects.toThrow( | ||
/instance requires property "name"/ | ||
); | ||
}); | ||
it("Should throw if name field incorrect patterh", () => { | ||
it("Should throw if name field incorrect patterh", async () => { | ||
const manifest = msgpackEncode({ | ||
...testManifest, | ||
name: "foo bar baz $%##$@#$@#$@#$#$" | ||
name: "foo bar baz $%##$@#$@#$@#$#$", | ||
}); | ||
expect(() => deserializeWrapManifest(manifest)).toThrowError(/instance.name does not match pattern/); | ||
await expect(() => deserializeWrapManifest(manifest)).rejects.toThrow( | ||
/instance.name does not match pattern/ | ||
); | ||
}); | ||
it("Should throw wrong type error", () => { | ||
it("Should throw wrong type error", async () => { | ||
const manifest = msgpackEncode({ | ||
...testManifest, | ||
abi: true | ||
abi: true, | ||
}); | ||
expect(() => deserializeWrapManifest(manifest)).toThrowError(/instance.abi is not of a type\(s\) object/); | ||
await expect(() => deserializeWrapManifest(manifest)).rejects.toThrow( | ||
/instance.abi is not of a type\(s\) object/ | ||
); | ||
}); | ||
}); |
@@ -18,8 +18,8 @@ /* eslint-disable */ | ||
import { msgpackDecode } from "@polywrap/msgpack-js"; | ||
import { compare } from "semver"; | ||
import * as Semver from "semver"; | ||
export function deserializeWrapManifest( | ||
export async function deserializeWrapManifest( | ||
manifest: Uint8Array, | ||
options?: DeserializeManifestOptions | ||
): WrapManifest { | ||
): Promise<WrapManifest> { | ||
let anyWrapManifest: AnyWrapManifest | undefined; | ||
@@ -33,8 +33,8 @@ try { | ||
if (!options || !options.noValidate) { | ||
validateWrapManifest(anyWrapManifest); | ||
await validateWrapManifest(anyWrapManifest); | ||
} | ||
const versionCompare = compare( | ||
anyWrapManifest.version, | ||
latestWrapManifestVersion | ||
const versionCompare = Semver.compare( | ||
Semver.coerce(anyWrapManifest.version) || anyWrapManifest.version, | ||
Semver.coerce(latestWrapManifestVersion) || latestWrapManifestVersion | ||
); | ||
@@ -41,0 +41,0 @@ |
@@ -9,24 +9,41 @@ /* eslint-disable */ | ||
import { | ||
WrapManifest as WrapManifest0_1_0 | ||
} from "./0.1.0"; | ||
WrapManifest as WrapManifest_0_1, | ||
Abi as WrapAbi_0_1_0_1 | ||
} from "./0.1"; | ||
export { | ||
WrapManifest0_1_0, | ||
WrapManifest_0_1, | ||
WrapAbi_0_1_0_1 | ||
}; | ||
export enum WrapManifestVersions { | ||
"0.1.0" = "0.1.0", | ||
} | ||
// NOTE: Patch fix for backwards compatability | ||
"v0.1.0" = "0.1.0", | ||
"v0.1" = "0.1", | ||
}; | ||
export enum WrapManifestAbiVersions { | ||
"v0.1" = "0.1", | ||
}; | ||
export enum WrapAbiVersions { | ||
"v0.1" = "0.1", | ||
}; | ||
export type AnyWrapManifest = | ||
| WrapManifest0_1_0 | ||
| WrapManifest_0_1 | ||
; | ||
export type WrapManifest = WrapManifest0_1_0; | ||
export type AnyWrapAbi = | ||
| WrapAbi_0_1_0_1 | ||
; | ||
export const latestWrapManifestVersion = WrapManifestVersions["0.1.0"] | ||
export type WrapManifest = WrapManifest_0_1; | ||
export type WrapAbi = WrapAbi_0_1_0_1; | ||
export const latestWrapManifestVersion = WrapManifestVersions["v0.1"]; | ||
export const latestWrapAbiVersion = "0.1"; | ||
export { migrateWrapManifest } from "./migrate"; | ||
export { deserializeWrapManifest } from "./deserialize"; | ||
export { validateWrapManifest } from "./validate"; |
@@ -12,3 +12,4 @@ /* eslint-disable */ | ||
import schema_0_1_0 from "@polywrap/wrap-manifest-schemas/formats/wrap.info/0.1.0.json"; | ||
import schema_0_1 from "@polywrap/wrap-manifest-schemas/formats/wrap.info/0.1.json"; | ||
import abi_schema_0_1 from "@polywrap/wrap-manifest-schemas/formats/abi/0.1.json"; | ||
@@ -21,2 +22,4 @@ import { | ||
} from "jsonschema"; | ||
import { resolve, bundle, FileInfo, $Refs } from "json-schema-ref-parser"; | ||
import path from "path"; | ||
@@ -27,33 +30,86 @@ type WrapManifestSchemas = { | ||
type WrapAbiSchemas = { | ||
[key in WrapManifestVersions]: Schema | undefined | ||
} | ||
const schemas: WrapManifestSchemas = { | ||
"0.1.0": schema_0_1_0, | ||
// NOTE: Patch fix for backwards compatability | ||
"0.1.0": schema_0_1, | ||
"0.1": schema_0_1, | ||
}; | ||
const validator = new Validator(); | ||
const abiSchemas: WrapAbiSchemas = { | ||
// NOTE: Patch fix for backwards compatability | ||
"0.1.0": abi_schema_0_1, | ||
"0.1": abi_schema_0_1, | ||
} | ||
export function validateWrapManifest( | ||
function throwIfErrors(result: ValidatorResult, version: string) { | ||
if (result.errors.length) { | ||
throw new Error([ | ||
`Validation errors encountered while sanitizing WrapManifest version ${version}`, | ||
...result.errors.map((error: ValidationError) => error.toString()) | ||
].join("\n")); | ||
} | ||
} | ||
export async function validateWrapManifest( | ||
manifest: AnyWrapManifest, | ||
extSchema: Schema | undefined = undefined | ||
): void { | ||
): Promise<void> { | ||
const schema = schemas[manifest.version as WrapManifestVersions]; | ||
const abiSchema = abiSchemas[manifest.version as WrapManifestVersions]; | ||
if (!schema) { | ||
throw Error(`Unrecognized WrapManifest schema version "${manifest.version}"\nmanifest: ${JSON.stringify(manifest, null, 2)}`); | ||
if (!schema || !abiSchema) { | ||
throw new Error(`Unrecognized WrapManifest schema version "${manifest.version}"\nmanifest: ${JSON.stringify(manifest, null, 2)}`); | ||
} | ||
const throwIfErrors = (result: ValidatorResult) => { | ||
if (result.errors.length) { | ||
throw new Error([ | ||
`Validation errors encountered while sanitizing WrapManifest version ${manifest.version}`, | ||
...result.errors.map((error: ValidationError) => error.toString()) | ||
].join("\n")); | ||
} | ||
}; | ||
if (!schema.properties) { | ||
// This should never happen | ||
throw new Error(`WrapManifest schema doesn't contain any properties`) | ||
} | ||
throwIfErrors(validator.validate(manifest, schema)); | ||
const abiJsonSchemaRelPath = schema.properties.abi.$ref as string; | ||
const finalSchema = await bundle(schema as any, { | ||
resolve: { | ||
file: { | ||
read: (file: FileInfo) => { | ||
// If both url is same | ||
if (!path.relative(abiJsonSchemaRelPath, file.url)) { | ||
return abiSchema as any; | ||
} | ||
return file.data; | ||
}, | ||
}, | ||
}, | ||
}); | ||
const refs: $Refs = await resolve(finalSchema); | ||
const validator = new Validator(); | ||
validator.addSchema(finalSchema as Schema); | ||
const resolveRefs = () => { | ||
const unresolvedRef = validator.unresolvedRefs.shift(); | ||
if (!unresolvedRef) return; | ||
const relRefIdx = unresolvedRef.indexOf("#"); | ||
const relRef = unresolvedRef.slice(relRefIdx); | ||
const resolvedSchema = refs.get(relRef); | ||
if (!resolvedSchema) throw new Error(`Failed to resolve the ref: ${relRef}`); | ||
validator.addSchema(resolvedSchema as Schema, unresolvedRef); | ||
resolveRefs(); | ||
} | ||
resolveRefs(); | ||
throwIfErrors(validator.validate(manifest, finalSchema as Schema), manifest.version); | ||
if (extSchema) { | ||
throwIfErrors(validator.validate(manifest, extSchema)); | ||
throwIfErrors(validator.validate(manifest, extSchema), manifest.version); | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
60782
76.92%1191
89.35%5
25%12
20%1
Infinity%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
- Removed
- Removed
Updated