pojoc 0.8.0 → 0.8.2
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/README.md +96 -13
- package/dist/compiled.js +23 -3
- package/dist/wire.d.ts +2 -1
- package/dist/wire.js +55 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,30 +1,113 @@
|
|
|
1
|
-
# Pojoc TypeScript
|
|
1
|
+
# Pojoc for TypeScript
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
The Pojoc CLI compiles a `.pojoc` schema into a TypeScript module with types
|
|
4
|
+
and binary codecs. The `pojoc` npm package supplies shared wire primitives.
|
|
5
|
+
Use the same release of the CLI and runtime.
|
|
6
|
+
|
|
7
|
+
## Generate and use a codec
|
|
8
|
+
|
|
9
|
+
Save this schema as `schemas/player.pojoc`.
|
|
10
|
+
|
|
11
|
+
```pojoc
|
|
12
|
+
schema Player {
|
|
13
|
+
version 1 {
|
|
14
|
+
fields {
|
|
15
|
+
id: u32 = 0
|
|
16
|
+
name: string = ""
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
version 2 {
|
|
21
|
+
diff { + level: u32 = 1 }
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
From the directory containing `package.json`, install the runtime and
|
|
27
|
+
generate `src/generated/player.ts`.
|
|
5
28
|
|
|
6
29
|
```sh
|
|
7
30
|
npm install pojoc
|
|
8
|
-
|
|
31
|
+
npm install --save-dev @pojoc/cli
|
|
32
|
+
npm exec -- pojoc build schemas/player.pojoc --target typescript -o src/generated
|
|
9
33
|
```
|
|
10
34
|
|
|
11
|
-
|
|
35
|
+
From `src/app.ts`, import the codec and its `Player` type.
|
|
12
36
|
|
|
13
37
|
```ts
|
|
14
|
-
import { create, decode, encode } from "./generated/player.ts";
|
|
38
|
+
import { create, decode, encode, encodeForVersion } from "./generated/player.ts";
|
|
39
|
+
import type { Player } from "./generated/player.ts";
|
|
15
40
|
|
|
16
|
-
const player = create();
|
|
17
|
-
const bytes = encode(player); // Uint8Array
|
|
18
|
-
const roundTrip = decode(bytes); //
|
|
41
|
+
const player: Player = create({ id: 42, name: "Mira" });
|
|
42
|
+
const bytes = encode(player); // Uint8Array containing a version 2 envelope
|
|
43
|
+
const roundTrip = decode(bytes); // { id: 42, name: "Mira", level: 1 }
|
|
44
|
+
const legacy = encodeForVersion(player, 1);
|
|
45
|
+
const upgraded = decode(legacy); // level is filled in from the version 2 default
|
|
19
46
|
```
|
|
20
47
|
|
|
21
48
|
`create()` fills in omitted fields from the schema defaults. Optional fields
|
|
22
49
|
have type `T | null`. Pass `null` to encode an absent value.
|
|
23
50
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
51
|
+
Include the generated file in your regular TypeScript build. Regenerate it
|
|
52
|
+
when the schema or any of its imports change. For a frontend-only project,
|
|
53
|
+
run the generation command from an npm script before type checking or bundling.
|
|
54
|
+
|
|
55
|
+
## What the compiler emits
|
|
56
|
+
|
|
57
|
+
1. Pojoc resolves schema imports, applies each version's `diff`, and computes
|
|
58
|
+
the wire layouts shared with the Rust target.
|
|
59
|
+
2. It emits a single `.ts` module per entry schema. The module includes
|
|
60
|
+
public TypeScript types, schema-specific readers and writers, version
|
|
61
|
+
dispatch, and field access functions. Imported schema layouts are included
|
|
62
|
+
in that module.
|
|
63
|
+
3. `encode` writes the latest version. `decode` reads the version in the
|
|
64
|
+
envelope, runs that version's reader, and returns the current type. The
|
|
65
|
+
compiler emits those functions ahead of time. At runtime they call shared
|
|
66
|
+
operations such as varint and string decoding from `pojoc/v1/compiler`.
|
|
67
|
+
|
|
68
|
+
The [schema reference](../../docs/schema.md) covers TypeScript type mappings,
|
|
69
|
+
defaults, and field access.
|
|
70
|
+
|
|
71
|
+
## Generation options
|
|
72
|
+
|
|
73
|
+
| Option | Effect |
|
|
74
|
+
| --- | --- |
|
|
75
|
+
| `--standalone` | Embed the shared runtime in the generated module so it can run without the npm package. |
|
|
76
|
+
| `--runtime portable` | Use the portable profile, which is the default for browser and server builds. |
|
|
77
|
+
| `--runtime bun` or `--runtime node` | Enable the specialized writer for supported variable-length layouts. Both profiles currently use the same writer specialization. `--bun` is shorthand for `--runtime bun`. |
|
|
78
|
+
| `--compact` | Reduce output size by omitting dedicated seeks for historical layouts. Those seeks fall back to decoding. |
|
|
79
|
+
|
|
80
|
+
## Generate from Cargo
|
|
81
|
+
|
|
82
|
+
For a Rust backend and TypeScript frontend in the same project, add
|
|
83
|
+
`pojoc-build` as a build dependency.
|
|
84
|
+
|
|
85
|
+
```sh
|
|
86
|
+
cargo add --build pojoc-build --no-default-features
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
In `build.rs`, write the TypeScript module to a directory the frontend can
|
|
90
|
+
import.
|
|
91
|
+
|
|
92
|
+
```rust
|
|
93
|
+
use pojoc_build::{TypeScriptOptions, compile_typescript};
|
|
94
|
+
|
|
95
|
+
fn main() {
|
|
96
|
+
compile_typescript(
|
|
97
|
+
"schemas/player.pojoc",
|
|
98
|
+
"web/src/generated",
|
|
99
|
+
TypeScriptOptions::default(),
|
|
100
|
+
)
|
|
101
|
+
.unwrap_or_else(|error| panic!("{}", error.render()));
|
|
102
|
+
}
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
The paths are relative to the Rust package. Cargo tracks the schema and its
|
|
106
|
+
transitive imports, and generation leaves an identical output file untouched.
|
|
107
|
+
The `standalone`, `runtime`, and `compact` fields on `TypeScriptOptions`
|
|
108
|
+
correspond to the CLI options above.
|
|
109
|
+
Run `cargo build` before building the frontend so the file is available.
|
|
110
|
+
The Rust side can use `pojoc_build::schema!` with the same schema file.
|
|
28
111
|
|
|
29
112
|
## Publishing
|
|
30
113
|
|
package/dist/compiled.js
CHANGED
|
@@ -123,10 +123,16 @@ function readItems(r, count, table, read) {
|
|
|
123
123
|
function readPairs(r, size, table, key, item) {
|
|
124
124
|
const count = size ?? readLength(r);
|
|
125
125
|
limitCollection(count);
|
|
126
|
+
if (size === null) {
|
|
127
|
+
const result = new Map();
|
|
128
|
+
for (let i = 0; i < count; i++)
|
|
129
|
+
result.set(key(r, table), item(r, table));
|
|
130
|
+
return result;
|
|
131
|
+
}
|
|
126
132
|
const pairs = [];
|
|
127
133
|
for (let i = 0; i < count; i++)
|
|
128
134
|
pairs.push([key(r, table), item(r, table)]);
|
|
129
|
-
return
|
|
135
|
+
return pairs;
|
|
130
136
|
}
|
|
131
137
|
function readVariant(r, size, table, kind, read) {
|
|
132
138
|
if (size > r.end - r.pos)
|
|
@@ -221,8 +227,22 @@ function writePairs(w, value, table, size, key, item) {
|
|
|
221
227
|
}
|
|
222
228
|
function skipTable(r) {
|
|
223
229
|
const count = tableLength(r);
|
|
224
|
-
|
|
225
|
-
|
|
230
|
+
const { data, end } = r;
|
|
231
|
+
let pos = r.pos;
|
|
232
|
+
for (let i = 0; i < count; i++) {
|
|
233
|
+
const length = pos < end ? data[pos] : undefined;
|
|
234
|
+
if (length !== undefined && length < 0x80) {
|
|
235
|
+
pos += 1 + length;
|
|
236
|
+
if (pos > end)
|
|
237
|
+
invalid("Unexpected end of Pojoc payload");
|
|
238
|
+
}
|
|
239
|
+
else {
|
|
240
|
+
r.pos = pos;
|
|
241
|
+
takeBytes(r, readLength(r));
|
|
242
|
+
pos = r.pos;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
r.pos = pos;
|
|
226
246
|
}
|
|
227
247
|
function skipDelta(r, count) {
|
|
228
248
|
limitCollection(count);
|
package/dist/wire.d.ts
CHANGED
|
@@ -14,6 +14,7 @@ declare function readVarint32(r: Cursor, extendedLength?: boolean): number;
|
|
|
14
14
|
declare function readLength(r: Cursor): number;
|
|
15
15
|
declare function readVarint32Array(r: Cursor, count: number): number[];
|
|
16
16
|
declare function readString(r: Cursor): string;
|
|
17
|
+
declare function readSeekString(r: Cursor): string;
|
|
17
18
|
declare function readIntern(r: Cursor, table: ReadTable): string;
|
|
18
19
|
declare const cursorReads: CursorOps;
|
|
19
20
|
declare function limitCollection(count: number): void;
|
|
@@ -27,4 +28,4 @@ type Envelope = Cursor & {
|
|
|
27
28
|
declare function openEnvelope(input: Input, offset: number, current: bigint, shortVersion: number): Envelope;
|
|
28
29
|
declare function openSchemaEnvelope(input: Input, offset: number, current: bigint, shortVersion: number, currentSize: number | undefined, fixedSize: (version: bigint) => number | undefined): Envelope;
|
|
29
30
|
export type { Envelope };
|
|
30
|
-
export { textEncoder, invalid, checkedVarint32, varint32Size, writeVarint32, cursorView, takeBytes, readBytes, readByte, readUint, readVarint, readVarint32, readLength, readVarint32Array, readString, readIntern, cursorReads, limitCollection, tableLength, readTable, readTableOffsets, emptyTable, openEnvelope, openSchemaEnvelope, };
|
|
31
|
+
export { textEncoder, invalid, checkedVarint32, varint32Size, writeVarint32, cursorView, takeBytes, readBytes, readByte, readUint, readVarint, readVarint32, readLength, readVarint32Array, readString, readSeekString, readIntern, cursorReads, limitCollection, tableLength, readTable, readTableOffsets, emptyTable, openEnvelope, openSchemaEnvelope, };
|
package/dist/wire.js
CHANGED
|
@@ -31,7 +31,17 @@ function writeVarint32(data, pos, value) {
|
|
|
31
31
|
function decodeText(data, start, end) {
|
|
32
32
|
if (end - start <= 32) {
|
|
33
33
|
let value = "";
|
|
34
|
-
|
|
34
|
+
let i = start;
|
|
35
|
+
for (; i <= end - 16; i += 16) {
|
|
36
|
+
const first = data[i];
|
|
37
|
+
if (first >= 0x80)
|
|
38
|
+
return textDecoder.decode(data.subarray(start, end));
|
|
39
|
+
const block = decodeAscii16(data, i, first);
|
|
40
|
+
if (block === undefined)
|
|
41
|
+
return textDecoder.decode(data.subarray(start, end));
|
|
42
|
+
value += block;
|
|
43
|
+
}
|
|
44
|
+
for (; i < end; i++) {
|
|
35
45
|
const byte = data[i];
|
|
36
46
|
if (byte >= 0x80)
|
|
37
47
|
return textDecoder.decode(data.subarray(start, end));
|
|
@@ -41,6 +51,42 @@ function decodeText(data, start, end) {
|
|
|
41
51
|
}
|
|
42
52
|
return textDecoder.decode(data.subarray(start, end));
|
|
43
53
|
}
|
|
54
|
+
function decodeAscii16(data, start, b0) {
|
|
55
|
+
const b1 = data[start + 1];
|
|
56
|
+
const b2 = data[start + 2];
|
|
57
|
+
const b3 = data[start + 3];
|
|
58
|
+
const b4 = data[start + 4];
|
|
59
|
+
const b5 = data[start + 5];
|
|
60
|
+
const b6 = data[start + 6];
|
|
61
|
+
const b7 = data[start + 7];
|
|
62
|
+
const b8 = data[start + 8];
|
|
63
|
+
const b9 = data[start + 9];
|
|
64
|
+
const b10 = data[start + 10];
|
|
65
|
+
const b11 = data[start + 11];
|
|
66
|
+
const b12 = data[start + 12];
|
|
67
|
+
const b13 = data[start + 13];
|
|
68
|
+
const b14 = data[start + 14];
|
|
69
|
+
const b15 = data[start + 15];
|
|
70
|
+
if ((b0 |
|
|
71
|
+
b1 |
|
|
72
|
+
b2 |
|
|
73
|
+
b3 |
|
|
74
|
+
b4 |
|
|
75
|
+
b5 |
|
|
76
|
+
b6 |
|
|
77
|
+
b7 |
|
|
78
|
+
b8 |
|
|
79
|
+
b9 |
|
|
80
|
+
b10 |
|
|
81
|
+
b11 |
|
|
82
|
+
b12 |
|
|
83
|
+
b13 |
|
|
84
|
+
b14 |
|
|
85
|
+
b15) >=
|
|
86
|
+
0x80)
|
|
87
|
+
return undefined;
|
|
88
|
+
return String.fromCharCode(b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15);
|
|
89
|
+
}
|
|
44
90
|
function cursorView(r) {
|
|
45
91
|
const data = r.data;
|
|
46
92
|
return (r.view ??= new DataView(data.buffer, data.byteOffset, data.byteLength));
|
|
@@ -164,6 +210,13 @@ function readString(r) {
|
|
|
164
210
|
r.pos = end;
|
|
165
211
|
return decodeText(r.data, start, end);
|
|
166
212
|
}
|
|
213
|
+
function readSeekString(r) {
|
|
214
|
+
const count = readLength(r);
|
|
215
|
+
const start = takeBytes(r, count);
|
|
216
|
+
if (count <= 8)
|
|
217
|
+
return decodeText(r.data, start, r.pos);
|
|
218
|
+
return textDecoder.decode(r.data.subarray(start, r.pos));
|
|
219
|
+
}
|
|
167
220
|
function readIntern(r, table) {
|
|
168
221
|
const index = readLength(r);
|
|
169
222
|
if (index >= table.length)
|
|
@@ -273,4 +326,4 @@ function openSchemaEnvelope(input, offset, current, shortVersion, currentSize, f
|
|
|
273
326
|
r.end = r.pos + size;
|
|
274
327
|
return r;
|
|
275
328
|
}
|
|
276
|
-
export { textEncoder, invalid, checkedVarint32, varint32Size, writeVarint32, cursorView, takeBytes, readBytes, readByte, readUint, readVarint, readVarint32, readLength, readVarint32Array, readString, readIntern, cursorReads, limitCollection, tableLength, readTable, readTableOffsets, emptyTable, openEnvelope, openSchemaEnvelope, };
|
|
329
|
+
export { textEncoder, invalid, checkedVarint32, varint32Size, writeVarint32, cursorView, takeBytes, readBytes, readByte, readUint, readVarint, readVarint32, readLength, readVarint32Array, readString, readSeekString, readIntern, cursorReads, limitCollection, tableLength, readTable, readTableOffsets, emptyTable, openEnvelope, openSchemaEnvelope, };
|