@geoprotocol/grc-20 0.1.1 → 0.1.5
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/dist/builder/edit.d.ts +5 -13
- package/dist/builder/edit.d.ts.map +1 -1
- package/dist/builder/edit.js +10 -31
- package/dist/builder/edit.js.map +1 -1
- package/dist/builder/entity.d.ts +13 -5
- package/dist/builder/entity.d.ts.map +1 -1
- package/dist/builder/entity.js +27 -7
- package/dist/builder/entity.js.map +1 -1
- package/dist/builder/index.d.ts +1 -0
- package/dist/builder/index.d.ts.map +1 -1
- package/dist/builder/index.js +1 -0
- package/dist/builder/index.js.map +1 -1
- package/dist/builder/relation.d.ts +14 -8
- package/dist/builder/relation.d.ts.map +1 -1
- package/dist/builder/relation.js +28 -14
- package/dist/builder/relation.js.map +1 -1
- package/dist/builder/update-relation.d.ts +64 -0
- package/dist/builder/update-relation.d.ts.map +1 -0
- package/dist/builder/update-relation.js +107 -0
- package/dist/builder/update-relation.js.map +1 -0
- package/dist/builder/update.d.ts +22 -14
- package/dist/builder/update.d.ts.map +1 -1
- package/dist/builder/update.js +52 -32
- package/dist/builder/update.js.map +1 -1
- package/dist/codec/compression.d.ts +132 -0
- package/dist/codec/compression.d.ts.map +1 -0
- package/dist/codec/compression.js +244 -0
- package/dist/codec/compression.js.map +1 -0
- package/dist/codec/edit.js +5 -8
- package/dist/codec/edit.js.map +1 -1
- package/dist/codec/index.d.ts +1 -0
- package/dist/codec/index.d.ts.map +1 -1
- package/dist/codec/index.js +9 -0
- package/dist/codec/index.js.map +1 -1
- package/dist/codec/op.d.ts.map +1 -1
- package/dist/codec/op.js +192 -81
- package/dist/codec/op.js.map +1 -1
- package/dist/codec/value.d.ts.map +1 -1
- package/dist/codec/value.js +92 -16
- package/dist/codec/value.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/test/basic.test.js +154 -31
- package/dist/test/basic.test.js.map +1 -1
- package/dist/types/index.d.ts +2 -2
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/index.js +1 -1
- package/dist/types/index.js.map +1 -1
- package/dist/types/op.d.ts +46 -27
- package/dist/types/op.d.ts.map +1 -1
- package/dist/types/op.js +3 -3
- package/dist/types/op.js.map +1 -1
- package/dist/types/value.d.ts +17 -8
- package/dist/types/value.d.ts.map +1 -1
- package/dist/types/value.js +19 -10
- package/dist/types/value.js.map +1 -1
- package/package.json +5 -1
- package/readme.md +76 -12
package/dist/builder/update.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Id } from "../types/id.js";
|
|
2
|
-
import type { UnsetLanguage,
|
|
2
|
+
import type { UnsetLanguage, UnsetValue } from "../types/op.js";
|
|
3
3
|
import type { DecimalMantissa, PropertyValue, Value } from "../types/value.js";
|
|
4
4
|
import { EmbeddingSubType } from "../types/value.js";
|
|
5
5
|
/**
|
|
@@ -7,8 +7,8 @@ import { EmbeddingSubType } from "../types/value.js";
|
|
|
7
7
|
*/
|
|
8
8
|
export declare class UpdateEntityBuilder {
|
|
9
9
|
private readonly _id;
|
|
10
|
-
private
|
|
11
|
-
private
|
|
10
|
+
private _set;
|
|
11
|
+
private _unset;
|
|
12
12
|
constructor(id: Id);
|
|
13
13
|
/**
|
|
14
14
|
* Returns the entity ID.
|
|
@@ -39,17 +39,25 @@ export declare class UpdateEntityBuilder {
|
|
|
39
39
|
*/
|
|
40
40
|
setBytes(property: Id, value: Uint8Array): this;
|
|
41
41
|
/**
|
|
42
|
-
* Sets a POINT value.
|
|
42
|
+
* Sets a POINT value (longitude, latitude, optional altitude).
|
|
43
43
|
*/
|
|
44
|
-
setPoint(property: Id,
|
|
44
|
+
setPoint(property: Id, lon: number, lat: number, alt?: number): this;
|
|
45
45
|
/**
|
|
46
|
-
* Sets a DATE value.
|
|
46
|
+
* Sets a DATE value (ISO 8601 date string).
|
|
47
47
|
*/
|
|
48
48
|
setDate(property: Id, value: string): this;
|
|
49
49
|
/**
|
|
50
|
-
* Sets a
|
|
50
|
+
* Sets a TIME value (ISO 8601 time string with timezone).
|
|
51
51
|
*/
|
|
52
|
-
|
|
52
|
+
setTime(property: Id, value: string): this;
|
|
53
|
+
/**
|
|
54
|
+
* Sets a DATETIME value (ISO 8601 datetime string).
|
|
55
|
+
*/
|
|
56
|
+
setDatetime(property: Id, value: string): this;
|
|
57
|
+
/**
|
|
58
|
+
* Sets a SCHEDULE value (RFC 5545 iCalendar format).
|
|
59
|
+
*/
|
|
60
|
+
setSchedule(property: Id, value: string): this;
|
|
53
61
|
/**
|
|
54
62
|
* Sets a DECIMAL value.
|
|
55
63
|
*/
|
|
@@ -71,20 +79,20 @@ export declare class UpdateEntityBuilder {
|
|
|
71
79
|
*/
|
|
72
80
|
unsetAll(property: Id): this;
|
|
73
81
|
/**
|
|
74
|
-
* Unsets the
|
|
82
|
+
* Unsets the English value for a property.
|
|
75
83
|
*/
|
|
76
|
-
|
|
84
|
+
unsetEnglish(property: Id): this;
|
|
77
85
|
/**
|
|
78
86
|
* Unsets a specific language for a property.
|
|
79
87
|
*/
|
|
80
88
|
unsetLanguage(property: Id, language: Id): this;
|
|
81
89
|
/**
|
|
82
|
-
* Returns the built set
|
|
90
|
+
* Returns the built set values array.
|
|
83
91
|
*/
|
|
84
|
-
|
|
92
|
+
getSet(): PropertyValue[];
|
|
85
93
|
/**
|
|
86
|
-
* Returns the built unset
|
|
94
|
+
* Returns the built unset array.
|
|
87
95
|
*/
|
|
88
|
-
|
|
96
|
+
getUnset(): UnsetValue[];
|
|
89
97
|
}
|
|
90
98
|
//# sourceMappingURL=update.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"update.d.ts","sourceRoot":"","sources":["../../src/builder/update.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,EAAE,EAAE,MAAM,gBAAgB,CAAC;AACzC,OAAO,KAAK,EAAE,aAAa,EA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UAAU,EAAE;CAGzB"}
|
package/dist/builder/update.js
CHANGED
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
*/
|
|
4
4
|
export class UpdateEntityBuilder {
|
|
5
5
|
_id;
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
_set = [];
|
|
7
|
+
_unset = [];
|
|
8
8
|
constructor(id) {
|
|
9
9
|
this._id = id;
|
|
10
10
|
}
|
|
@@ -18,14 +18,14 @@ export class UpdateEntityBuilder {
|
|
|
18
18
|
* Sets a property value.
|
|
19
19
|
*/
|
|
20
20
|
set(property, value) {
|
|
21
|
-
this.
|
|
21
|
+
this._set.push({ property, value });
|
|
22
22
|
return this;
|
|
23
23
|
}
|
|
24
24
|
/**
|
|
25
25
|
* Sets a TEXT value.
|
|
26
26
|
*/
|
|
27
27
|
setText(property, value, language) {
|
|
28
|
-
this.
|
|
28
|
+
this._set.push({
|
|
29
29
|
property,
|
|
30
30
|
value: { type: "text", value, language },
|
|
31
31
|
});
|
|
@@ -35,7 +35,7 @@ export class UpdateEntityBuilder {
|
|
|
35
35
|
* Sets an INT64 value.
|
|
36
36
|
*/
|
|
37
37
|
setInt64(property, value, unit) {
|
|
38
|
-
this.
|
|
38
|
+
this._set.push({
|
|
39
39
|
property,
|
|
40
40
|
value: { type: "int64", value, unit },
|
|
41
41
|
});
|
|
@@ -45,7 +45,7 @@ export class UpdateEntityBuilder {
|
|
|
45
45
|
* Sets a FLOAT64 value.
|
|
46
46
|
*/
|
|
47
47
|
setFloat64(property, value, unit) {
|
|
48
|
-
this.
|
|
48
|
+
this._set.push({
|
|
49
49
|
property,
|
|
50
50
|
value: { type: "float64", value, unit },
|
|
51
51
|
});
|
|
@@ -55,7 +55,7 @@ export class UpdateEntityBuilder {
|
|
|
55
55
|
* Sets a BOOL value.
|
|
56
56
|
*/
|
|
57
57
|
setBool(property, value) {
|
|
58
|
-
this.
|
|
58
|
+
this._set.push({
|
|
59
59
|
property,
|
|
60
60
|
value: { type: "bool", value },
|
|
61
61
|
});
|
|
@@ -65,39 +65,59 @@ export class UpdateEntityBuilder {
|
|
|
65
65
|
* Sets a BYTES value.
|
|
66
66
|
*/
|
|
67
67
|
setBytes(property, value) {
|
|
68
|
-
this.
|
|
68
|
+
this._set.push({
|
|
69
69
|
property,
|
|
70
70
|
value: { type: "bytes", value },
|
|
71
71
|
});
|
|
72
72
|
return this;
|
|
73
73
|
}
|
|
74
74
|
/**
|
|
75
|
-
* Sets a POINT value.
|
|
75
|
+
* Sets a POINT value (longitude, latitude, optional altitude).
|
|
76
76
|
*/
|
|
77
|
-
setPoint(property, lat,
|
|
78
|
-
this.
|
|
77
|
+
setPoint(property, lon, lat, alt) {
|
|
78
|
+
this._set.push({
|
|
79
79
|
property,
|
|
80
|
-
value: { type: "point", lat,
|
|
80
|
+
value: { type: "point", lon, lat, alt },
|
|
81
81
|
});
|
|
82
82
|
return this;
|
|
83
83
|
}
|
|
84
84
|
/**
|
|
85
|
-
* Sets a DATE value.
|
|
85
|
+
* Sets a DATE value (ISO 8601 date string).
|
|
86
86
|
*/
|
|
87
87
|
setDate(property, value) {
|
|
88
|
-
this.
|
|
88
|
+
this._set.push({
|
|
89
89
|
property,
|
|
90
90
|
value: { type: "date", value },
|
|
91
91
|
});
|
|
92
92
|
return this;
|
|
93
93
|
}
|
|
94
94
|
/**
|
|
95
|
-
* Sets a
|
|
95
|
+
* Sets a TIME value (ISO 8601 time string with timezone).
|
|
96
96
|
*/
|
|
97
|
-
|
|
98
|
-
this.
|
|
97
|
+
setTime(property, value) {
|
|
98
|
+
this._set.push({
|
|
99
99
|
property,
|
|
100
|
-
value: { type: "
|
|
100
|
+
value: { type: "time", value },
|
|
101
|
+
});
|
|
102
|
+
return this;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Sets a DATETIME value (ISO 8601 datetime string).
|
|
106
|
+
*/
|
|
107
|
+
setDatetime(property, value) {
|
|
108
|
+
this._set.push({
|
|
109
|
+
property,
|
|
110
|
+
value: { type: "datetime", value },
|
|
111
|
+
});
|
|
112
|
+
return this;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Sets a SCHEDULE value (RFC 5545 iCalendar format).
|
|
116
|
+
*/
|
|
117
|
+
setSchedule(property, value) {
|
|
118
|
+
this._set.push({
|
|
119
|
+
property,
|
|
120
|
+
value: { type: "schedule", value },
|
|
101
121
|
});
|
|
102
122
|
return this;
|
|
103
123
|
}
|
|
@@ -105,7 +125,7 @@ export class UpdateEntityBuilder {
|
|
|
105
125
|
* Sets a DECIMAL value.
|
|
106
126
|
*/
|
|
107
127
|
setDecimal(property, exponent, mantissa, unit) {
|
|
108
|
-
this.
|
|
128
|
+
this._set.push({
|
|
109
129
|
property,
|
|
110
130
|
value: { type: "decimal", exponent, mantissa, unit },
|
|
111
131
|
});
|
|
@@ -121,7 +141,7 @@ export class UpdateEntityBuilder {
|
|
|
121
141
|
* Sets an EMBEDDING value.
|
|
122
142
|
*/
|
|
123
143
|
setEmbedding(property, subType, dims, data) {
|
|
124
|
-
this.
|
|
144
|
+
this._set.push({
|
|
125
145
|
property,
|
|
126
146
|
value: { type: "embedding", subType, dims, data },
|
|
127
147
|
});
|
|
@@ -131,44 +151,44 @@ export class UpdateEntityBuilder {
|
|
|
131
151
|
* Unsets a specific property+language combination.
|
|
132
152
|
*/
|
|
133
153
|
unset(property, language) {
|
|
134
|
-
this.
|
|
154
|
+
this._unset.push({ property, language });
|
|
135
155
|
return this;
|
|
136
156
|
}
|
|
137
157
|
/**
|
|
138
158
|
* Unsets all values for a property (all languages).
|
|
139
159
|
*/
|
|
140
160
|
unsetAll(property) {
|
|
141
|
-
this.
|
|
161
|
+
this._unset.push({ property, language: { type: "all" } });
|
|
142
162
|
return this;
|
|
143
163
|
}
|
|
144
164
|
/**
|
|
145
|
-
* Unsets the
|
|
165
|
+
* Unsets the English value for a property.
|
|
146
166
|
*/
|
|
147
|
-
|
|
148
|
-
this.
|
|
167
|
+
unsetEnglish(property) {
|
|
168
|
+
this._unset.push({ property, language: { type: "english" } });
|
|
149
169
|
return this;
|
|
150
170
|
}
|
|
151
171
|
/**
|
|
152
172
|
* Unsets a specific language for a property.
|
|
153
173
|
*/
|
|
154
174
|
unsetLanguage(property, language) {
|
|
155
|
-
this.
|
|
175
|
+
this._unset.push({
|
|
156
176
|
property,
|
|
157
177
|
language: { type: "specific", language },
|
|
158
178
|
});
|
|
159
179
|
return this;
|
|
160
180
|
}
|
|
161
181
|
/**
|
|
162
|
-
* Returns the built set
|
|
182
|
+
* Returns the built set values array.
|
|
163
183
|
*/
|
|
164
|
-
|
|
165
|
-
return this.
|
|
184
|
+
getSet() {
|
|
185
|
+
return this._set;
|
|
166
186
|
}
|
|
167
187
|
/**
|
|
168
|
-
* Returns the built unset
|
|
188
|
+
* Returns the built unset array.
|
|
169
189
|
*/
|
|
170
|
-
|
|
171
|
-
return this.
|
|
190
|
+
getUnset() {
|
|
191
|
+
return this._unset;
|
|
172
192
|
}
|
|
173
193
|
}
|
|
174
194
|
//# sourceMappingURL=update.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"update.js","sourceRoot":"","sources":["../../src/builder/update.ts"],"names":[],"mappings":"AAKA;;GAEG;AACH,MAAM,OAAO,mBAAmB;IACb,GAAG,CAAK;IACjB,
|
|
1
|
+
{"version":3,"file":"update.js","sourceRoot":"","sources":["../../src/builder/update.ts"],"names":[],"mappings":"AAKA;;GAEG;AACH,MAAM,OAAO,mBAAmB;IACb,GAAG,CAAK;IACjB,IAAI,GAAoB,EAAE,CAAC;IAC3B,MAAM,GAAiB,EAAE,CAAC;IAElC,YAAY,EAAM;QAChB,IAAI,CAAC,GAAG,GAAG,EAAE,CAAC;IAChB,CAAC;IAED;;OAEG;IACH,IAAI,EAAE;QACJ,OAAO,IAAI,CAAC,GAAG,CAAC;IAClB,CAAC;IAED;;OAEG;IACH,GAAG,CAAC,QAAY,EAAE,KAAY;QAC5B,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,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CAAC,MAAM,CAAC,IAAI,CAAC;YACf,QAAQ;YACR,QAAQ,EAAE,EAAE,IAAI,EAAE,UAAU,EAAE,QAAQ,EAAE;SACzC,CAAC,CAAC;QACH,OAAO,IAAI,CAAC;IACd,CAAC;IAED;;OAEG;IACH,MAAM;QACJ,OAAO,IAAI,CAAC,IAAI,CAAC;IACnB,CAAC;IAED;;OAEG;IACH,QAAQ;QACN,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;CACF"}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Zstd compression support for GRC-20 edits.
|
|
3
|
+
*
|
|
4
|
+
* This module uses lazy loading - the @bokuweb/zstd-wasm library is only
|
|
5
|
+
* imported when compression/decompression is actually used.
|
|
6
|
+
*/
|
|
7
|
+
import type { Edit } from "../types/edit.js";
|
|
8
|
+
import { type EncodeOptions } from "./edit.js";
|
|
9
|
+
/**
|
|
10
|
+
* Checks if the compression module is loaded and ready.
|
|
11
|
+
*
|
|
12
|
+
* Use this to check if compression functions can be called synchronously
|
|
13
|
+
* (after preloading) or if they will need to load the WASM first.
|
|
14
|
+
*
|
|
15
|
+
* @returns true if WASM is loaded and compression is ready
|
|
16
|
+
*/
|
|
17
|
+
export declare function isCompressionReady(): boolean;
|
|
18
|
+
/**
|
|
19
|
+
* Preloads the compression WASM module.
|
|
20
|
+
*
|
|
21
|
+
* Call this on app startup to ensure compression is ready when needed.
|
|
22
|
+
* The promise resolves when WASM is fully loaded and initialized.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```typescript
|
|
26
|
+
* // On app startup
|
|
27
|
+
* import { preloadCompression } from '@geoprotocol/grc-20';
|
|
28
|
+
*
|
|
29
|
+
* preloadCompression().then(() => {
|
|
30
|
+
* console.log('Compression ready!');
|
|
31
|
+
* });
|
|
32
|
+
* ```
|
|
33
|
+
*
|
|
34
|
+
* @returns Promise that resolves when compression is ready
|
|
35
|
+
*/
|
|
36
|
+
export declare function preloadCompression(): Promise<void>;
|
|
37
|
+
/**
|
|
38
|
+
* Checks if data appears to be compressed (starts with GRC2Z magic).
|
|
39
|
+
*/
|
|
40
|
+
export declare function isCompressed(data: Uint8Array): boolean;
|
|
41
|
+
/**
|
|
42
|
+
* Compresses raw bytes using Zstd.
|
|
43
|
+
*
|
|
44
|
+
* @param data - The data to compress
|
|
45
|
+
* @param level - Compression level (default: 3)
|
|
46
|
+
* @returns The compressed data (without GRC2Z header)
|
|
47
|
+
*/
|
|
48
|
+
export declare function compress(data: Uint8Array, level?: number): Promise<Uint8Array>;
|
|
49
|
+
/**
|
|
50
|
+
* Decompresses raw Zstd-compressed bytes.
|
|
51
|
+
*
|
|
52
|
+
* @param data - The compressed data (without GRC2Z header)
|
|
53
|
+
* @returns The decompressed data
|
|
54
|
+
*/
|
|
55
|
+
export declare function decompress(data: Uint8Array): Promise<Uint8Array>;
|
|
56
|
+
/**
|
|
57
|
+
* Encodes an Edit to compressed binary format (GRC2Z).
|
|
58
|
+
*
|
|
59
|
+
* Format:
|
|
60
|
+
* - 5 bytes: "GRC2Z" magic
|
|
61
|
+
* - varint: uncompressed size
|
|
62
|
+
* - bytes: zstd-compressed GRC2 data
|
|
63
|
+
*
|
|
64
|
+
* @param edit - The edit to encode
|
|
65
|
+
* @param options - Encoding options (e.g., canonical mode)
|
|
66
|
+
* @returns The compressed binary data
|
|
67
|
+
*/
|
|
68
|
+
export declare function encodeEditCompressed(edit: Edit, options?: EncodeOptions): Promise<Uint8Array>;
|
|
69
|
+
/**
|
|
70
|
+
* Options for auto-encoding.
|
|
71
|
+
*/
|
|
72
|
+
export interface EncodeAutoOptions extends EncodeOptions {
|
|
73
|
+
/**
|
|
74
|
+
* Minimum uncompressed size (in bytes) before compression is applied.
|
|
75
|
+
* If the uncompressed data is smaller than this threshold, it will
|
|
76
|
+
* be returned as-is (uncompressed GRC2 format).
|
|
77
|
+
*
|
|
78
|
+
* Default: 256 bytes
|
|
79
|
+
*
|
|
80
|
+
* Set to 0 to always compress.
|
|
81
|
+
* Set to Infinity to never compress.
|
|
82
|
+
*/
|
|
83
|
+
threshold?: number;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Encodes an Edit to binary format, automatically choosing compression.
|
|
87
|
+
*
|
|
88
|
+
* This is the recommended encoding function for most use cases. It:
|
|
89
|
+
* - Encodes small edits without compression (faster, no WASM needed)
|
|
90
|
+
* - Compresses larger edits for better size efficiency
|
|
91
|
+
*
|
|
92
|
+
* The output can be decoded with `decodeEditAuto()`.
|
|
93
|
+
*
|
|
94
|
+
* @param edit - The edit to encode
|
|
95
|
+
* @param options - Encoding options including compression threshold
|
|
96
|
+
* @returns The binary data (GRC2 or GRC2Z format depending on size)
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```typescript
|
|
100
|
+
* // Auto-compress (default threshold: 256 bytes)
|
|
101
|
+
* const data = await encodeEditAuto(edit);
|
|
102
|
+
*
|
|
103
|
+
* // Always compress
|
|
104
|
+
* const compressed = await encodeEditAuto(edit, { threshold: 0 });
|
|
105
|
+
*
|
|
106
|
+
* // Never compress
|
|
107
|
+
* const uncompressed = await encodeEditAuto(edit, { threshold: Infinity });
|
|
108
|
+
*
|
|
109
|
+
* // Custom threshold (1KB)
|
|
110
|
+
* const data = await encodeEditAuto(edit, { threshold: 1024 });
|
|
111
|
+
* ```
|
|
112
|
+
*/
|
|
113
|
+
export declare function encodeEditAuto(edit: Edit, options?: EncodeAutoOptions): Promise<Uint8Array>;
|
|
114
|
+
/**
|
|
115
|
+
* Decodes a compressed Edit from binary data (GRC2Z format).
|
|
116
|
+
*
|
|
117
|
+
* @param data - The compressed binary data
|
|
118
|
+
* @returns The decoded Edit
|
|
119
|
+
* @throws DecodeError if the data is invalid or not compressed
|
|
120
|
+
*/
|
|
121
|
+
export declare function decodeEditCompressed(data: Uint8Array): Promise<Edit>;
|
|
122
|
+
/**
|
|
123
|
+
* Decodes an Edit from binary data, automatically detecting compression.
|
|
124
|
+
*
|
|
125
|
+
* This is a convenience function that handles both compressed (GRC2Z)
|
|
126
|
+
* and uncompressed (GRC2) formats.
|
|
127
|
+
*
|
|
128
|
+
* @param data - The binary data (compressed or uncompressed)
|
|
129
|
+
* @returns The decoded Edit
|
|
130
|
+
*/
|
|
131
|
+
export declare function decodeEditAuto(data: Uint8Array): Promise<Edit>;
|
|
132
|
+
//# sourceMappingURL=compression.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compression.d.ts","sourceRoot":"","sources":["../../src/codec/compression.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AAE7C,OAAO,EAA0B,KAAK,aAAa,EAAE,MAAM,WAAW,CAAC;AAmEvE;;;;;;;GAOG;AACH,wBAAgB,kBAAkB,IAAI,OAAO,CAE5C;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAsB,kBAAkB,IAAI,OAAO,CAAC,IAAI,CAAC,CAExD;AAED;;GAEG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,UAAU,GAAG,OAAO,CAMtD;AAED;;;;;;GAMG;AACH,wBAAsB,QAAQ,CAAC,IAAI,EAAE,UAAU,EAAE,KAAK,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC,CAGpF;AAED;;;;;GAKG;AACH,wBAAsB,UAAU,CAAC,IAAI,EAAE,UAAU,GAAG,OAAO,CAAC,UAAU,CAAC,CAGtE;AAED;;;;;;;;;;;GAWG;AACH,wBAAsB,oBAAoB,CACxC,IAAI,EAAE,IAAI,EACV,OAAO,CAAC,EAAE,aAAa,GACtB,OAAO,CAAC,UAAU,CAAC,CAcrB;AAED;;GAEG;AACH,MAAM,WAAW,iBAAkB,SAAQ,aAAa;IACtD;;;;;;;;;OASG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAKD;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,wBAAsB,cAAc,CAClC,IAAI,EAAE,IAAI,EACV,OAAO,CAAC,EAAE,iBAAiB,GAC1B,OAAO,CAAC,UAAU,CAAC,CAqBrB;AAED;;;;;;GAMG;AACH,wBAAsB,oBAAoB,CAAC,IAAI,EAAE,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC,CAiC1E;AAED;;;;;;;;GAQG;AACH,wBAAsB,cAAc,CAAC,IAAI,EAAE,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC,CAKpE"}
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Zstd compression support for GRC-20 edits.
|
|
3
|
+
*
|
|
4
|
+
* This module uses lazy loading - the @bokuweb/zstd-wasm library is only
|
|
5
|
+
* imported when compression/decompression is actually used.
|
|
6
|
+
*/
|
|
7
|
+
import { DecodeError, Reader, Writer } from "./primitives.js";
|
|
8
|
+
import { encodeEdit, decodeEdit } from "./edit.js";
|
|
9
|
+
// Magic bytes for compressed format
|
|
10
|
+
const MAGIC_COMPRESSED = new TextEncoder().encode("GRC2Z");
|
|
11
|
+
// Default compression level (matches Rust implementation)
|
|
12
|
+
const DEFAULT_COMPRESSION_LEVEL = 3;
|
|
13
|
+
// Cached zstd functions
|
|
14
|
+
let zstdCompress = null;
|
|
15
|
+
let zstdDecompress = null;
|
|
16
|
+
let zstdLoadPromise = null;
|
|
17
|
+
/**
|
|
18
|
+
* Loads the zstd-wasm library.
|
|
19
|
+
*/
|
|
20
|
+
async function loadZstd() {
|
|
21
|
+
const zstd = await import("@bokuweb/zstd-wasm");
|
|
22
|
+
await zstd.init();
|
|
23
|
+
zstdCompress = zstd.compress;
|
|
24
|
+
zstdDecompress = zstd.decompress;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Starts preloading zstd in the background (non-blocking).
|
|
28
|
+
* Fails silently if the module can't be loaded (e.g., in browsers without bundler).
|
|
29
|
+
*/
|
|
30
|
+
function preloadZstd() {
|
|
31
|
+
if (zstdLoadPromise !== null)
|
|
32
|
+
return;
|
|
33
|
+
zstdLoadPromise = loadZstd().catch(() => {
|
|
34
|
+
// Preload failed (e.g., bare specifier in browser without bundler)
|
|
35
|
+
// Will retry on-demand when compression is actually used
|
|
36
|
+
zstdLoadPromise = null;
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
// Start preloading in the background (non-blocking, fails silently)
|
|
40
|
+
preloadZstd();
|
|
41
|
+
/**
|
|
42
|
+
* Ensures zstd is loaded and ready.
|
|
43
|
+
* If preload failed, retries loading on-demand.
|
|
44
|
+
*/
|
|
45
|
+
async function ensureZstdLoaded() {
|
|
46
|
+
if (zstdCompress !== null && zstdDecompress !== null) {
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
if (zstdLoadPromise !== null) {
|
|
50
|
+
await zstdLoadPromise;
|
|
51
|
+
if (zstdCompress !== null)
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
// Preload failed or not started, try loading now
|
|
55
|
+
try {
|
|
56
|
+
zstdLoadPromise = loadZstd();
|
|
57
|
+
await zstdLoadPromise;
|
|
58
|
+
}
|
|
59
|
+
catch (error) {
|
|
60
|
+
throw new Error("Failed to load zstd-wasm. Compression requires a bundler (Vite, webpack, etc.) " +
|
|
61
|
+
"or an import map to resolve '@bokuweb/zstd-wasm'. " +
|
|
62
|
+
"Original error: " + (error instanceof Error ? error.message : String(error)));
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Checks if the compression module is loaded and ready.
|
|
67
|
+
*
|
|
68
|
+
* Use this to check if compression functions can be called synchronously
|
|
69
|
+
* (after preloading) or if they will need to load the WASM first.
|
|
70
|
+
*
|
|
71
|
+
* @returns true if WASM is loaded and compression is ready
|
|
72
|
+
*/
|
|
73
|
+
export function isCompressionReady() {
|
|
74
|
+
return zstdCompress !== null && zstdDecompress !== null;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Preloads the compression WASM module.
|
|
78
|
+
*
|
|
79
|
+
* Call this on app startup to ensure compression is ready when needed.
|
|
80
|
+
* The promise resolves when WASM is fully loaded and initialized.
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* ```typescript
|
|
84
|
+
* // On app startup
|
|
85
|
+
* import { preloadCompression } from '@geoprotocol/grc-20';
|
|
86
|
+
*
|
|
87
|
+
* preloadCompression().then(() => {
|
|
88
|
+
* console.log('Compression ready!');
|
|
89
|
+
* });
|
|
90
|
+
* ```
|
|
91
|
+
*
|
|
92
|
+
* @returns Promise that resolves when compression is ready
|
|
93
|
+
*/
|
|
94
|
+
export async function preloadCompression() {
|
|
95
|
+
await ensureZstdLoaded();
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Checks if data appears to be compressed (starts with GRC2Z magic).
|
|
99
|
+
*/
|
|
100
|
+
export function isCompressed(data) {
|
|
101
|
+
if (data.length < 5)
|
|
102
|
+
return false;
|
|
103
|
+
for (let i = 0; i < 5; i++) {
|
|
104
|
+
if (data[i] !== MAGIC_COMPRESSED[i])
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
return true;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Compresses raw bytes using Zstd.
|
|
111
|
+
*
|
|
112
|
+
* @param data - The data to compress
|
|
113
|
+
* @param level - Compression level (default: 3)
|
|
114
|
+
* @returns The compressed data (without GRC2Z header)
|
|
115
|
+
*/
|
|
116
|
+
export async function compress(data, level) {
|
|
117
|
+
await ensureZstdLoaded();
|
|
118
|
+
return zstdCompress(data, level ?? DEFAULT_COMPRESSION_LEVEL);
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Decompresses raw Zstd-compressed bytes.
|
|
122
|
+
*
|
|
123
|
+
* @param data - The compressed data (without GRC2Z header)
|
|
124
|
+
* @returns The decompressed data
|
|
125
|
+
*/
|
|
126
|
+
export async function decompress(data) {
|
|
127
|
+
await ensureZstdLoaded();
|
|
128
|
+
return zstdDecompress(data);
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Encodes an Edit to compressed binary format (GRC2Z).
|
|
132
|
+
*
|
|
133
|
+
* Format:
|
|
134
|
+
* - 5 bytes: "GRC2Z" magic
|
|
135
|
+
* - varint: uncompressed size
|
|
136
|
+
* - bytes: zstd-compressed GRC2 data
|
|
137
|
+
*
|
|
138
|
+
* @param edit - The edit to encode
|
|
139
|
+
* @param options - Encoding options (e.g., canonical mode)
|
|
140
|
+
* @returns The compressed binary data
|
|
141
|
+
*/
|
|
142
|
+
export async function encodeEditCompressed(edit, options) {
|
|
143
|
+
// First encode to uncompressed format
|
|
144
|
+
const uncompressed = encodeEdit(edit, options);
|
|
145
|
+
// Compress the data
|
|
146
|
+
const compressed = await compress(uncompressed);
|
|
147
|
+
// Build the final output: magic + uncompressed size + compressed data
|
|
148
|
+
const writer = new Writer(5 + 10 + compressed.length);
|
|
149
|
+
writer.writeBytes(MAGIC_COMPRESSED);
|
|
150
|
+
writer.writeVarintNumber(uncompressed.length);
|
|
151
|
+
writer.writeBytes(compressed);
|
|
152
|
+
return writer.finish();
|
|
153
|
+
}
|
|
154
|
+
// Default threshold for auto-compression (256 bytes)
|
|
155
|
+
const DEFAULT_COMPRESSION_THRESHOLD = 256;
|
|
156
|
+
/**
|
|
157
|
+
* Encodes an Edit to binary format, automatically choosing compression.
|
|
158
|
+
*
|
|
159
|
+
* This is the recommended encoding function for most use cases. It:
|
|
160
|
+
* - Encodes small edits without compression (faster, no WASM needed)
|
|
161
|
+
* - Compresses larger edits for better size efficiency
|
|
162
|
+
*
|
|
163
|
+
* The output can be decoded with `decodeEditAuto()`.
|
|
164
|
+
*
|
|
165
|
+
* @param edit - The edit to encode
|
|
166
|
+
* @param options - Encoding options including compression threshold
|
|
167
|
+
* @returns The binary data (GRC2 or GRC2Z format depending on size)
|
|
168
|
+
*
|
|
169
|
+
* @example
|
|
170
|
+
* ```typescript
|
|
171
|
+
* // Auto-compress (default threshold: 256 bytes)
|
|
172
|
+
* const data = await encodeEditAuto(edit);
|
|
173
|
+
*
|
|
174
|
+
* // Always compress
|
|
175
|
+
* const compressed = await encodeEditAuto(edit, { threshold: 0 });
|
|
176
|
+
*
|
|
177
|
+
* // Never compress
|
|
178
|
+
* const uncompressed = await encodeEditAuto(edit, { threshold: Infinity });
|
|
179
|
+
*
|
|
180
|
+
* // Custom threshold (1KB)
|
|
181
|
+
* const data = await encodeEditAuto(edit, { threshold: 1024 });
|
|
182
|
+
* ```
|
|
183
|
+
*/
|
|
184
|
+
export async function encodeEditAuto(edit, options) {
|
|
185
|
+
const threshold = options?.threshold ?? DEFAULT_COMPRESSION_THRESHOLD;
|
|
186
|
+
// First encode to uncompressed format
|
|
187
|
+
const uncompressed = encodeEdit(edit, options);
|
|
188
|
+
// If below threshold, return uncompressed
|
|
189
|
+
if (uncompressed.length < threshold) {
|
|
190
|
+
return uncompressed;
|
|
191
|
+
}
|
|
192
|
+
// Compress the data
|
|
193
|
+
const compressed = await compress(uncompressed);
|
|
194
|
+
// Build the final output: magic + uncompressed size + compressed data
|
|
195
|
+
const writer = new Writer(5 + 10 + compressed.length);
|
|
196
|
+
writer.writeBytes(MAGIC_COMPRESSED);
|
|
197
|
+
writer.writeVarintNumber(uncompressed.length);
|
|
198
|
+
writer.writeBytes(compressed);
|
|
199
|
+
return writer.finish();
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Decodes a compressed Edit from binary data (GRC2Z format).
|
|
203
|
+
*
|
|
204
|
+
* @param data - The compressed binary data
|
|
205
|
+
* @returns The decoded Edit
|
|
206
|
+
* @throws DecodeError if the data is invalid or not compressed
|
|
207
|
+
*/
|
|
208
|
+
export async function decodeEditCompressed(data) {
|
|
209
|
+
// Check magic
|
|
210
|
+
if (!isCompressed(data)) {
|
|
211
|
+
throw new DecodeError("E001", "invalid magic bytes: expected GRC2Z for compressed data");
|
|
212
|
+
}
|
|
213
|
+
const reader = new Reader(data);
|
|
214
|
+
// Skip magic
|
|
215
|
+
reader.readBytes(5);
|
|
216
|
+
// Read declared uncompressed size
|
|
217
|
+
const declaredSize = reader.readVarintNumber();
|
|
218
|
+
// Read compressed data
|
|
219
|
+
const compressedData = reader.readBytes(reader.remaining());
|
|
220
|
+
// Decompress
|
|
221
|
+
const decompressed = await decompress(compressedData);
|
|
222
|
+
// Verify size
|
|
223
|
+
if (decompressed.length !== declaredSize) {
|
|
224
|
+
throw new DecodeError("E001", `uncompressed size mismatch: declared ${declaredSize}, actual ${decompressed.length}`);
|
|
225
|
+
}
|
|
226
|
+
// Decode the uncompressed edit
|
|
227
|
+
return decodeEdit(decompressed);
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* Decodes an Edit from binary data, automatically detecting compression.
|
|
231
|
+
*
|
|
232
|
+
* This is a convenience function that handles both compressed (GRC2Z)
|
|
233
|
+
* and uncompressed (GRC2) formats.
|
|
234
|
+
*
|
|
235
|
+
* @param data - The binary data (compressed or uncompressed)
|
|
236
|
+
* @returns The decoded Edit
|
|
237
|
+
*/
|
|
238
|
+
export async function decodeEditAuto(data) {
|
|
239
|
+
if (isCompressed(data)) {
|
|
240
|
+
return decodeEditCompressed(data);
|
|
241
|
+
}
|
|
242
|
+
return decodeEdit(data);
|
|
243
|
+
}
|
|
244
|
+
//# sourceMappingURL=compression.js.map
|