UNPKG

@jsonjoy.com/json-type

Version:

High-performance JSON Pointer implementation

123 lines (122 loc) 4.17 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BinaryType = void 0; const tslib_1 = require("tslib"); const printTree_1 = require("tree-dump/lib/printTree"); const schema = tslib_1.__importStar(require("../../schema")); const json_random_1 = require("@jsonjoy.com/util/lib/json-random"); const json_binary_1 = require("@jsonjoy.com/json-pack/lib/json-binary"); const validate_1 = require("../../schema/validate"); const constants_1 = require("../../constants"); const AbstractType_1 = require("./AbstractType"); const formats = new Set([ 'bencode', 'bson', 'cbor', 'ion', 'json', 'msgpack', 'resp3', 'ubjson', ]); class BinaryType extends AbstractType_1.AbstractType { constructor(type, options) { super(); this.type = type; this.schema = schema.s.Binary(schema.s.any, options); } getSchema() { return { ...this.schema, type: this.type.getSchema(), }; } toJsonSchema(ctx) { return { type: 'binary', ...super.toJsonSchema(ctx), }; } getOptions() { const { kind, type, ...options } = this.schema; return options; } validateSchema() { const schema = this.getSchema(); (0, validate_1.validateTType)(schema, 'bin'); const { min, max, format } = schema; (0, validate_1.validateMinMax)(min, max); if (format !== undefined) { if (!formats.has(format)) throw new Error('FORMAT'); } this.type.validateSchema(); } codegenValidator(ctx, path, r) { const hasBuffer = typeof Buffer === 'function'; const err = ctx.err(constants_1.ValidationError.BIN, path); ctx.js( // prettier-ignore `if(!(${r} instanceof Uint8Array)${hasBuffer ? ` && !Buffer.isBuffer(${r})` : ''}) return ${err};`); const { min, max } = this.schema; if (typeof min === 'number' && min === max) { const err = ctx.err(constants_1.ValidationError.BIN_LEN, path); ctx.js(`if(${r}.length !== ${min}) return ${err};`); } else { if (typeof min === 'number') { const err = ctx.err(constants_1.ValidationError.BIN_LEN, path); ctx.js(`if(${r}.length < ${min}) return ${err};`); } if (typeof max === 'number') { const err = ctx.err(constants_1.ValidationError.BIN_LEN, path); ctx.js(`if(${r}.length > ${max}) return ${err};`); } } ctx.emitCustomValidators(this, path, r); } codegenJsonTextEncoder(ctx, value) { ctx.linkBase64(); ctx.writeText('"data:application/octet-stream;base64,'); ctx.js(/* js */ `s += toBase64(${value.use()});`); ctx.writeText('"'); } codegenBinaryEncoder(ctx, value) { ctx.js(/* js */ `encoder.writeBin(${value.use()});`); } codegenCborEncoder(ctx, value) { this.codegenBinaryEncoder(ctx, value); } codegenMessagePackEncoder(ctx, value) { this.codegenBinaryEncoder(ctx, value); } codegenJsonEncoder(ctx, value) { this.codegenBinaryEncoder(ctx, value); } codegenCapacityEstimator(ctx, value) { ctx.inc(41 /* MaxEncodingOverhead.Binary */); ctx.codegen.js(`size += ${2 /* MaxEncodingOverhead.BinaryLengthMultiplier */} * ${value.use()}.length;`); } random() { const octets = json_random_1.RandomJson.genString() .split('') .map((c) => c.charCodeAt(0)); return new Uint8Array(octets); } toTypeScriptAst() { return { node: 'GenericTypeAnnotation', id: { node: 'Identifier', name: 'Uint8Array', }, }; } toJson(value, system = this.system) { return ('"' + (0, json_binary_1.stringifyBinary)(value) + '"'); } toString(tab = '') { return super.toString(tab) + (0, printTree_1.printTree)(tab, [(tab) => this.type.toString(tab)]); } } exports.BinaryType = BinaryType;