@dxos/codec-protobuf
Advanced tools
Comparing version 2.10.8 to 2.10.9
@@ -1,2 +0,2 @@ | ||
import { Schema } from './codec'; | ||
import type { Schema } from './schema'; | ||
export declare const anySubstitutions: { | ||
@@ -3,0 +3,0 @@ 'google.protobuf.Any': { |
import protobufjs from 'protobufjs'; | ||
import { Substitutions } from './common'; | ||
import { BidirectionalMapingDescriptors } from './mapping'; | ||
export declare class Schema<T> { | ||
private _typesRoot; | ||
static fromJson<T extends Record<string, any>>(schema: any, substitutions?: Substitutions): Schema<T>; | ||
private readonly _mapping; | ||
constructor(_typesRoot: protobufjs.Root, substitutions: Substitutions); | ||
getCodecForType<K extends keyof T & string>(typeName: K): Codec<T[K]>; | ||
tryGetCodecForType(typeName: string): Codec; | ||
/** | ||
* Dynamically add new definitions to this schema. | ||
*/ | ||
addJson(schema: any): void; | ||
} | ||
import type { Schema } from './schema'; | ||
export declare class Codec<T = any> { | ||
@@ -17,0 +5,0 @@ private readonly _type; |
@@ -5,63 +5,5 @@ "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 __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Codec = exports.Schema = void 0; | ||
const lodash_merge_1 = __importDefault(require("lodash.merge")); | ||
const protobufjs_1 = __importStar(require("protobufjs")); | ||
exports.Codec = void 0; | ||
const mapping_1 = require("./mapping"); | ||
class Schema { | ||
constructor(_typesRoot, substitutions) { | ||
this._typesRoot = _typesRoot; | ||
this._mapping = mapping_1.createMappingDescriptors(substitutions); | ||
} | ||
static fromJson(schema, substitutions = {}) { | ||
const root = protobufjs_1.default.Root.fromJSON(schema); | ||
return new Schema(root, substitutions); | ||
} | ||
getCodecForType(typeName) { | ||
if (typeof typeName !== 'string') { | ||
throw new TypeError('Expected `typeName` argument to be a string'); | ||
} | ||
const type = this._typesRoot.lookupType(typeName); | ||
return new Codec(type, this._mapping, this); | ||
} | ||
tryGetCodecForType(typeName) { | ||
if (typeof typeName !== 'string') { | ||
throw new TypeError('Expected `typeName` argument to be a string'); | ||
} | ||
const type = this._typesRoot.lookupType(typeName); | ||
return new Codec(type, this._mapping, this); | ||
} | ||
/** | ||
* Dynamically add new definitions to this schema. | ||
*/ | ||
addJson(schema) { | ||
if (!schema.nested) { | ||
throw new Error('Invalid schema: missing nested object'); | ||
} | ||
this._typesRoot = protobufjs_1.Root.fromJSON(lodash_merge_1.default(this._typesRoot.toJSON(), schema)); | ||
} | ||
} | ||
exports.Schema = Schema; | ||
class Codec { | ||
@@ -68,0 +10,0 @@ constructor(_type, _mapping, _schema) { |
@@ -1,2 +0,2 @@ | ||
import { Schema } from './codec'; | ||
import type { Schema } from './schema'; | ||
export interface SubstitutionDescriptor<T> { | ||
@@ -3,0 +3,0 @@ encode: (value: T, schema: Schema<any>) => any; |
export * from './common'; | ||
export * from './codec'; | ||
export * from './any'; | ||
export * from './schema'; | ||
export * from './service'; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -19,2 +19,4 @@ "use strict"; | ||
__exportStar(require("./any"), exports); | ||
__exportStar(require("./schema"), exports); | ||
__exportStar(require("./service"), exports); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@dxos/codec-protobuf", | ||
"version": "2.10.8", | ||
"version": "2.10.9", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "main": "dist/src/index.js", |
@@ -5,3 +5,3 @@ // | ||
import { Schema } from './codec'; | ||
import type { Schema } from './schema'; | ||
@@ -8,0 +8,0 @@ export const anySubstitutions = { |
@@ -5,50 +5,7 @@ // | ||
import merge from 'lodash.merge'; | ||
import protobufjs, { Root } from 'protobufjs'; | ||
import protobufjs from 'protobufjs'; | ||
import { Substitutions } from './common'; | ||
import { BidirectionalMapingDescriptors, createMappingDescriptors, mapMessage } from './mapping'; | ||
import { BidirectionalMapingDescriptors, mapMessage } from './mapping'; | ||
import type { Schema } from './schema'; | ||
export class Schema<T> { | ||
static fromJson<T extends Record<string, any>> (schema: any, substitutions: Substitutions = {}) { | ||
const root = protobufjs.Root.fromJSON(schema); | ||
return new Schema<T>(root, substitutions); | ||
} | ||
private readonly _mapping: BidirectionalMapingDescriptors; | ||
constructor ( | ||
private _typesRoot: protobufjs.Root, | ||
substitutions: Substitutions | ||
) { | ||
this._mapping = createMappingDescriptors(substitutions); | ||
} | ||
getCodecForType<K extends keyof T & string> (typeName: K): Codec<T[K]> { | ||
if (typeof typeName !== 'string') { | ||
throw new TypeError('Expected `typeName` argument to be a string'); | ||
} | ||
const type = this._typesRoot.lookupType(typeName); | ||
return new Codec(type, this._mapping, this); | ||
} | ||
tryGetCodecForType (typeName: string): Codec { | ||
if (typeof typeName !== 'string') { | ||
throw new TypeError('Expected `typeName` argument to be a string'); | ||
} | ||
const type = this._typesRoot.lookupType(typeName); | ||
return new Codec(type, this._mapping, this); | ||
} | ||
/** | ||
* Dynamically add new definitions to this schema. | ||
*/ | ||
addJson (schema: any) { | ||
if (!schema.nested) { | ||
throw new Error('Invalid schema: missing nested object'); | ||
} | ||
this._typesRoot = Root.fromJSON(merge(this._typesRoot.toJSON(), schema)); | ||
} | ||
} | ||
export class Codec<T = any> { | ||
@@ -55,0 +12,0 @@ constructor ( |
@@ -5,3 +5,3 @@ // | ||
import { Schema } from './codec'; | ||
import type { Schema } from './schema'; | ||
@@ -8,0 +8,0 @@ export interface SubstitutionDescriptor<T> { |
@@ -8,1 +8,3 @@ // | ||
export * from './any'; | ||
export * from './schema'; | ||
export * from './service'; |
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
169480
38
629