@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.
Files changed (60) hide show
  1. package/dist/builder/edit.d.ts +5 -13
  2. package/dist/builder/edit.d.ts.map +1 -1
  3. package/dist/builder/edit.js +10 -31
  4. package/dist/builder/edit.js.map +1 -1
  5. package/dist/builder/entity.d.ts +13 -5
  6. package/dist/builder/entity.d.ts.map +1 -1
  7. package/dist/builder/entity.js +27 -7
  8. package/dist/builder/entity.js.map +1 -1
  9. package/dist/builder/index.d.ts +1 -0
  10. package/dist/builder/index.d.ts.map +1 -1
  11. package/dist/builder/index.js +1 -0
  12. package/dist/builder/index.js.map +1 -1
  13. package/dist/builder/relation.d.ts +14 -8
  14. package/dist/builder/relation.d.ts.map +1 -1
  15. package/dist/builder/relation.js +28 -14
  16. package/dist/builder/relation.js.map +1 -1
  17. package/dist/builder/update-relation.d.ts +64 -0
  18. package/dist/builder/update-relation.d.ts.map +1 -0
  19. package/dist/builder/update-relation.js +107 -0
  20. package/dist/builder/update-relation.js.map +1 -0
  21. package/dist/builder/update.d.ts +22 -14
  22. package/dist/builder/update.d.ts.map +1 -1
  23. package/dist/builder/update.js +52 -32
  24. package/dist/builder/update.js.map +1 -1
  25. package/dist/codec/compression.d.ts +132 -0
  26. package/dist/codec/compression.d.ts.map +1 -0
  27. package/dist/codec/compression.js +244 -0
  28. package/dist/codec/compression.js.map +1 -0
  29. package/dist/codec/edit.js +5 -8
  30. package/dist/codec/edit.js.map +1 -1
  31. package/dist/codec/index.d.ts +1 -0
  32. package/dist/codec/index.d.ts.map +1 -1
  33. package/dist/codec/index.js +9 -0
  34. package/dist/codec/index.js.map +1 -1
  35. package/dist/codec/op.d.ts.map +1 -1
  36. package/dist/codec/op.js +192 -81
  37. package/dist/codec/op.js.map +1 -1
  38. package/dist/codec/value.d.ts.map +1 -1
  39. package/dist/codec/value.js +92 -16
  40. package/dist/codec/value.js.map +1 -1
  41. package/dist/index.d.ts +1 -1
  42. package/dist/index.d.ts.map +1 -1
  43. package/dist/index.js +3 -1
  44. package/dist/index.js.map +1 -1
  45. package/dist/test/basic.test.js +154 -31
  46. package/dist/test/basic.test.js.map +1 -1
  47. package/dist/types/index.d.ts +2 -2
  48. package/dist/types/index.d.ts.map +1 -1
  49. package/dist/types/index.js +1 -1
  50. package/dist/types/index.js.map +1 -1
  51. package/dist/types/op.d.ts +46 -27
  52. package/dist/types/op.d.ts.map +1 -1
  53. package/dist/types/op.js +3 -3
  54. package/dist/types/op.js.map +1 -1
  55. package/dist/types/value.d.ts +17 -8
  56. package/dist/types/value.d.ts.map +1 -1
  57. package/dist/types/value.js +19 -10
  58. package/dist/types/value.js.map +1 -1
  59. package/package.json +5 -1
  60. package/readme.md +76 -12
@@ -1,5 +1,5 @@
1
1
  import type { Id } from "../types/id.js";
2
- import type { UnsetLanguage, UnsetProperty } from "../types/op.js";
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 setProperties;
11
- private unsetProperties;
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, lat: number, lon: number): this;
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 TIMESTAMP value.
50
+ * Sets a TIME value (ISO 8601 time string with timezone).
51
51
  */
52
- setTimestamp(property: Id, micros: bigint): this;
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 non-linguistic value for a property.
82
+ * Unsets the English value for a property.
75
83
  */
76
- unsetNonLinguistic(property: Id): this;
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 properties array.
90
+ * Returns the built set values array.
83
91
  */
84
- getSetProperties(): PropertyValue[];
92
+ getSet(): PropertyValue[];
85
93
  /**
86
- * Returns the built unset properties array.
94
+ * Returns the built unset array.
87
95
  */
88
- getUnsetProperties(): UnsetProperty[];
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,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AACnE,OAAO,KAAK,EAAE,eAAe,EAAE,aAAa,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAC;AAC/E,OAAO,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AAErD;;GAEG;AACH,qBAAa,mBAAmB;IAC9B,OAAO,CAAC,QAAQ,CAAC,GAA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AC,QAAQ,EAAE,EAAE,EAAE,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,EAAE,GAAG,IAAI;IAIhF;;OAEG;IACH,YAAY,CACV,QAAQ,EAAE,EAAE,EACZ,OAAO,EAAE,gBAAgB,EACzB,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,UAAU,GACf,IAAI;IAQP;;OAEG;IACH,KAAK,CAAC,QAAQ,EAAE,EAAE,EAAE,QAAQ,EAAE,aAAa,GAAG,IAAI;IAKlD;;OAEG;IACH,QAAQ,CAAC,QAAQ,EAAE,EAAE,GAAG,IAAI;IAK5B;;OAEG;IACH,YAAY,CAAC,QAAQ,EAAE,EAAE,GAAG,IAAI;IAKhC;;OAEG;IACH,aAAa,CAAC,QAAQ,EAAE,EAAE,EAAE,QAAQ,EAAE,EAAE,GAAG,IAAI;IAQ/C;;OAEG;IACH,MAAM,IAAI,aAAa,EAAE;IAIzB;;OAEG;IACH,QAAQ,IAAI,UAAU,EAAE;CAGzB"}
@@ -3,8 +3,8 @@
3
3
  */
4
4
  export class UpdateEntityBuilder {
5
5
  _id;
6
- setProperties = [];
7
- unsetProperties = [];
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.setProperties.push({ property, value });
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.setProperties.push({
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.setProperties.push({
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.setProperties.push({
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.setProperties.push({
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.setProperties.push({
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, lon) {
78
- this.setProperties.push({
77
+ setPoint(property, lon, lat, alt) {
78
+ this._set.push({
79
79
  property,
80
- value: { type: "point", lat, lon },
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.setProperties.push({
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 TIMESTAMP value.
95
+ * Sets a TIME value (ISO 8601 time string with timezone).
96
96
  */
97
- setTimestamp(property, micros) {
98
- this.setProperties.push({
97
+ setTime(property, value) {
98
+ this._set.push({
99
99
  property,
100
- value: { type: "timestamp", value: micros },
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.setProperties.push({
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.setProperties.push({
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.unsetProperties.push({ property, language });
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.unsetProperties.push({ property, language: { type: "all" } });
161
+ this._unset.push({ property, language: { type: "all" } });
142
162
  return this;
143
163
  }
144
164
  /**
145
- * Unsets the non-linguistic value for a property.
165
+ * Unsets the English value for a property.
146
166
  */
147
- unsetNonLinguistic(property) {
148
- this.unsetProperties.push({ property, language: { type: "nonLinguistic" } });
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.unsetProperties.push({
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 properties array.
182
+ * Returns the built set values array.
163
183
  */
164
- getSetProperties() {
165
- return this.setProperties;
184
+ getSet() {
185
+ return this._set;
166
186
  }
167
187
  /**
168
- * Returns the built unset properties array.
188
+ * Returns the built unset array.
169
189
  */
170
- getUnsetProperties() {
171
- return this.unsetProperties;
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,aAAa,GAAoB,EAAE,CAAC;IACpC,eAAe,GAAoB,EAAE,CAAC;IAE9C,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,aAAa,CAAC,IAAI,CAAC,EAAE,QAAQ,EAAE,KAAK,EAAE,CAAC,CAAC;QAC7C,OAAO,IAAI,CAAC;IACd,CAAC;IAED;;OAEG;IACH,OAAO,CAAC,QAAY,EAAE,KAAa,EAAE,QAAa;QAChD,IAAI,CAAC,aAAa,CAAC,IAAI,CAAC;YACtB,QAAQ;YACR,KAAK,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,QAAQ,EAAE;SACzC,CAAC,CAAC;QACH,OAAO,IAAI,CAAC;IACd,CAAC;IAED;;OAEG;IACH,QAAQ,CAAC,QAAY,EAAE,KAAa,EAAE,IAAS;QAC7C,IAAI,CAAC,aAAa,CAAC,IAAI,CAAC;YACtB,QAAQ;YACR,KAAK,EAAE,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE;SACtC,CAAC,CAAC;QACH,OAAO,IAAI,CAAC;IACd,CAAC;IAED;;OAEG;IACH,UAAU,CAAC,QAAY,EAAE,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AC,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