@kubun/protocol 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +57 -0
- package/README.md +11 -0
- package/lib/index.d.ts +14 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +6 -0
- package/lib/models/cluster.d.ts +17 -0
- package/lib/models/cluster.d.ts.map +1 -0
- package/lib/models/cluster.js +102 -0
- package/lib/models/document.d.ts +3788 -0
- package/lib/models/document.d.ts.map +1 -0
- package/lib/models/document.js +229 -0
- package/lib/models/graph.d.ts +23 -0
- package/lib/models/graph.d.ts.map +1 -0
- package/lib/models/graph.js +39 -0
- package/lib/models/json-schema.d.ts +845 -0
- package/lib/models/json-schema.d.ts.map +1 -0
- package/lib/models/json-schema.js +245 -0
- package/lib/models/value.d.ts +16 -0
- package/lib/models/value.d.ts.map +1 -0
- package/lib/models/value.js +15 -0
- package/lib/services/graph.d.ts +2539 -0
- package/lib/services/graph.d.ts.map +1 -0
- package/lib/services/graph.js +164 -0
- package/package.json +35 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# The Prosperity Public License 3.0.0
|
|
2
|
+
|
|
3
|
+
Contributor: Paul Le Cam
|
|
4
|
+
|
|
5
|
+
Source Code: https://github.com/PaulLeCam/kubun
|
|
6
|
+
|
|
7
|
+
## Purpose
|
|
8
|
+
|
|
9
|
+
This license allows you to use and share this software for noncommercial purposes for free and to try this software for commercial purposes for thirty days.
|
|
10
|
+
|
|
11
|
+
## Agreement
|
|
12
|
+
|
|
13
|
+
In order to receive this license, you have to agree to its rules. Those rules are both obligations under that agreement and conditions to your license. Don't do anything with this software that triggers a rule you can't or won't follow.
|
|
14
|
+
|
|
15
|
+
## Notices
|
|
16
|
+
|
|
17
|
+
Make sure everyone who gets a copy of any part of this software from you, with or without changes, also gets the text of this license and the contributor and source code lines above.
|
|
18
|
+
|
|
19
|
+
## Commercial Trial
|
|
20
|
+
|
|
21
|
+
Limit your use of this software for commercial purposes to a thirty-day trial period. If you use this software for work, your company gets one trial period for all personnel, not one trial per person.
|
|
22
|
+
|
|
23
|
+
## Contributions Back
|
|
24
|
+
|
|
25
|
+
Developing feedback, changes, or additions that you contribute back to the contributor on the terms of a standardized public software license such as [the Blue Oak Model License 1.0.0](https://blueoakcouncil.org/license/1.0.0), [the Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0.html), [the MIT license](https://spdx.org/licenses/MIT.html), or [the two-clause BSD license](https://spdx.org/licenses/BSD-2-Clause.html) doesn't count as use for a commercial purpose.
|
|
26
|
+
|
|
27
|
+
## Personal Uses
|
|
28
|
+
|
|
29
|
+
Personal use for research, experiment, and testing for the benefit of public knowledge, personal study, private entertainment, hobby projects, amateur pursuits, or religious observance, without any anticipated commercial application, doesn't count as use for a commercial purpose.
|
|
30
|
+
|
|
31
|
+
## Noncommercial Organizations
|
|
32
|
+
|
|
33
|
+
Use by any charitable organization, educational institution, public research organization, public safety or health organization, environmental protection organization, or government institution doesn't count as use for a commercial purpose regardless of the source of funding or obligations resulting from the funding.
|
|
34
|
+
|
|
35
|
+
## Defense
|
|
36
|
+
|
|
37
|
+
Don't make any legal claim against anyone accusing this software, with or without changes, alone or with other technology, of infringing any patent.
|
|
38
|
+
|
|
39
|
+
## Copyright
|
|
40
|
+
|
|
41
|
+
The contributor licenses you to do everything with this software that would otherwise infringe their copyright in it.
|
|
42
|
+
|
|
43
|
+
## Patent
|
|
44
|
+
|
|
45
|
+
The contributor licenses you to do everything with this software that would otherwise infringe any patents they can license or become able to license.
|
|
46
|
+
|
|
47
|
+
## Reliability
|
|
48
|
+
|
|
49
|
+
The contributor can't revoke this license.
|
|
50
|
+
|
|
51
|
+
## Excuse
|
|
52
|
+
|
|
53
|
+
You're excused for unknowingly breaking [Notices](#notices) if you take all practical steps to comply within thirty days of learning you broke the rule.
|
|
54
|
+
|
|
55
|
+
## No Liability
|
|
56
|
+
|
|
57
|
+
***As far as the law allows, this software comes as is, without any warranty or condition, and the contributor won't be liable to anyone for any damages related to this software or this license, under any kind of legal claim.***
|
package/README.md
ADDED
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { AnyClientMessageOf, AnyServerMessageOf, ClientTransportOf, ServerTransportOf } from '@enkaku/protocol';
|
|
2
|
+
import type { GraphCommands } from './services/graph.js';
|
|
3
|
+
export * from './models/cluster.js';
|
|
4
|
+
export * from './models/document.js';
|
|
5
|
+
export * from './models/graph.js';
|
|
6
|
+
export * from './models/json-schema.js';
|
|
7
|
+
export * from './models/value.js';
|
|
8
|
+
export * from './services/graph.js';
|
|
9
|
+
export type Definitions = GraphCommands;
|
|
10
|
+
export type ClientMessage = AnyClientMessageOf<Definitions>;
|
|
11
|
+
export type ServerMessage = AnyServerMessageOf<Definitions>;
|
|
12
|
+
export type ClientTransport = ClientTransportOf<Definitions>;
|
|
13
|
+
export type ServerTransport = ServerTransportOf<Definitions>;
|
|
14
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,kBAAkB,EAClB,kBAAkB,EAClB,iBAAiB,EACjB,iBAAiB,EAClB,MAAM,kBAAkB,CAAA;AAEzB,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAA;AAExD,cAAc,qBAAqB,CAAA;AACnC,cAAc,sBAAsB,CAAA;AACpC,cAAc,mBAAmB,CAAA;AACjC,cAAc,yBAAyB,CAAA;AACvC,cAAc,mBAAmB,CAAA;AACjC,cAAc,qBAAqB,CAAA;AAEnC,MAAM,MAAM,WAAW,GAAG,aAAa,CAAA;AAEvC,MAAM,MAAM,aAAa,GAAG,kBAAkB,CAAC,WAAW,CAAC,CAAA;AAC3D,MAAM,MAAM,aAAa,GAAG,kBAAkB,CAAC,WAAW,CAAC,CAAA;AAE3D,MAAM,MAAM,eAAe,GAAG,iBAAiB,CAAC,WAAW,CAAC,CAAA;AAC5D,MAAM,MAAM,eAAe,GAAG,iBAAiB,CAAC,WAAW,CAAC,CAAA"}
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { JSONSchema } from 'json-schema-typed';
|
|
2
|
+
import type { DocumentModel, DocumentModelsCluster } from './document.js';
|
|
3
|
+
type SetDocumentModel = Omit<DocumentModel, 'schema'> & {
|
|
4
|
+
schema: JSONSchema.Object;
|
|
5
|
+
};
|
|
6
|
+
type JSONSchemaScalar = JSONSchema.Boolean | JSONSchema.Integer | JSONSchema.Number | JSONSchema.String;
|
|
7
|
+
type JSONSchemaShape = JSONSchema.Array | JSONSchema.Object;
|
|
8
|
+
type JSONSchemaType = JSONSchemaScalar | JSONSchemaShape;
|
|
9
|
+
export type JSONSchemaReferencesRecord = Record<string, JSONSchemaType>;
|
|
10
|
+
export declare const REFERENCE_PREFIX = "#/$defs/";
|
|
11
|
+
export declare class ClusterBuilder {
|
|
12
|
+
#private;
|
|
13
|
+
get cluster(): DocumentModelsCluster;
|
|
14
|
+
set(index: number, model: SetDocumentModel): void;
|
|
15
|
+
}
|
|
16
|
+
export {};
|
|
17
|
+
//# sourceMappingURL=cluster.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cluster.d.ts","sourceRoot":"","sources":["../../src/models/cluster.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAA;AAEnD,OAAO,KAAK,EAAE,aAAa,EAAuB,qBAAqB,EAAE,MAAM,eAAe,CAAA;AAG9F,KAAK,gBAAgB,GAAG,IAAI,CAAC,aAAa,EAAE,QAAQ,CAAC,GAAG;IACtD,MAAM,EAAE,UAAU,CAAC,MAAM,CAAA;CAC1B,CAAA;AAED,KAAK,gBAAgB,GACjB,UAAU,CAAC,OAAO,GAClB,UAAU,CAAC,OAAO,GAClB,UAAU,CAAC,MAAM,GACjB,UAAU,CAAC,MAAM,CAAA;AACrB,KAAK,eAAe,GAAG,UAAU,CAAC,KAAK,GAAG,UAAU,CAAC,MAAM,CAAA;AAC3D,KAAK,cAAc,GAAG,gBAAgB,GAAG,eAAe,CAAA;AAExD,MAAM,MAAM,0BAA0B,GAAG,MAAM,CAAC,MAAM,EAAE,cAAc,CAAC,CAAA;AAEvE,eAAO,MAAM,gBAAgB,aAAa,CAAA;AAuF1C,qBAAa,cAAc;;IAGzB,IAAI,OAAO,IAAI,qBAAqB,CAEnC;IAED,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,gBAAgB;CAO3C"}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { ScalarModelID, ShapeModelID } from '@kubun/id';
|
|
2
|
+
export const REFERENCE_PREFIX = '#/$defs/';
|
|
3
|
+
function toReference(id) {
|
|
4
|
+
return `${REFERENCE_PREFIX}${id}`;
|
|
5
|
+
}
|
|
6
|
+
function addScalar(refs, schema) {
|
|
7
|
+
const id = ScalarModelID.create(schema);
|
|
8
|
+
refs[id.toString()] = schema;
|
|
9
|
+
return toReference(id);
|
|
10
|
+
}
|
|
11
|
+
function addShape(refs, schema) {
|
|
12
|
+
const id = ShapeModelID.create(schema);
|
|
13
|
+
refs[id.toString()] = schema;
|
|
14
|
+
return toReference(id);
|
|
15
|
+
}
|
|
16
|
+
function encodeSchema(refs, schema) {
|
|
17
|
+
if (schema.$ref != null) {
|
|
18
|
+
return schema.$ref;
|
|
19
|
+
}
|
|
20
|
+
const type = schema.type;
|
|
21
|
+
if (type == null) {
|
|
22
|
+
throw new Error('Invalid schema: missing type');
|
|
23
|
+
}
|
|
24
|
+
switch(type){
|
|
25
|
+
case 'array':
|
|
26
|
+
{
|
|
27
|
+
if (schema.title == null) {
|
|
28
|
+
throw new Error('Invalid array schema: missing title');
|
|
29
|
+
}
|
|
30
|
+
if (schema.items == null) {
|
|
31
|
+
throw new Error('Invalid array schema: missing items');
|
|
32
|
+
}
|
|
33
|
+
return addShape(refs, {
|
|
34
|
+
...schema,
|
|
35
|
+
items: {
|
|
36
|
+
$ref: encodeSchema(refs, schema.items)
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
case 'object':
|
|
41
|
+
{
|
|
42
|
+
if (schema.title == null) {
|
|
43
|
+
throw new Error('Invalid object schema: missing title');
|
|
44
|
+
}
|
|
45
|
+
const entries = Object.entries(schema.properties ?? {});
|
|
46
|
+
if (entries.length === 0) {
|
|
47
|
+
throw new Error('Invalid object schema: must contain at least one property');
|
|
48
|
+
}
|
|
49
|
+
const properties = {};
|
|
50
|
+
for (const [key, value] of entries){
|
|
51
|
+
properties[key] = {
|
|
52
|
+
$ref: encodeSchema(refs, value)
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
return addShape(refs, {
|
|
56
|
+
type: 'object',
|
|
57
|
+
title: schema.title,
|
|
58
|
+
properties,
|
|
59
|
+
required: schema.required ?? [],
|
|
60
|
+
additionalProperties: false
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
default:
|
|
64
|
+
return addScalar(refs, schema);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
function normalizeDocumentSchema(schema) {
|
|
68
|
+
const entries = Object.entries(schema.properties ?? {});
|
|
69
|
+
if (entries.length === 0) {
|
|
70
|
+
throw new Error('Document schema must contain at least one property');
|
|
71
|
+
}
|
|
72
|
+
const properties = {};
|
|
73
|
+
const refs = {};
|
|
74
|
+
for (const [key, value] of entries){
|
|
75
|
+
properties[key] = {
|
|
76
|
+
$ref: encodeSchema(refs, value)
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
return {
|
|
80
|
+
type: 'object',
|
|
81
|
+
properties,
|
|
82
|
+
additionalProperties: false,
|
|
83
|
+
required: schema.required ?? [],
|
|
84
|
+
$defs: refs
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
export class ClusterBuilder {
|
|
88
|
+
#cluster = [];
|
|
89
|
+
get cluster() {
|
|
90
|
+
return this.#cluster;
|
|
91
|
+
}
|
|
92
|
+
set(index, model) {
|
|
93
|
+
if (index !== this.#cluster.length) {
|
|
94
|
+
throw new Error(`Invalid document index: ${index}, expected: ${this.#cluster.length}`);
|
|
95
|
+
}
|
|
96
|
+
const schema = normalizeDocumentSchema(model.schema);
|
|
97
|
+
this.#cluster.push({
|
|
98
|
+
...model,
|
|
99
|
+
schema
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
}
|