@versatiles/versatiles-rs 4.7.0 → 4.9.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.
Files changed (7) hide show
  1. package/README.md +149 -4
  2. package/index.cjs +60 -54
  3. package/index.d.ts +270 -18
  4. package/index.js +61 -55
  5. package/package.json +9 -9
  6. package/vpl.d.ts +173 -5
  7. package/vpl.js +104 -31
package/index.d.ts CHANGED
@@ -499,6 +499,8 @@ export declare class TileSource {
499
499
  * # Arguments
500
500
  *
501
501
  * * `path` - File path or URL to the tile container
502
+ * * `options` - Optional source options; `sshIdentity` names a private key
503
+ * file for an `sftp://` path
502
504
  *
503
505
  * # Returns
504
506
  *
@@ -519,9 +521,14 @@ export declare class TileSource {
519
521
  *
520
522
  * // Open remote file
521
523
  * const reader = await ContainerReader.from_path('https://example.com/tiles.pmtiles');
524
+ *
525
+ * // Open an SFTP file with a specific key
526
+ * const reader = await TileSource.fromPath('sftp://host/tiles.pmtiles', {
527
+ * sshIdentity: '/home/deploy/.ssh/id_ed25519',
528
+ * });
522
529
  * ```
523
530
  */
524
- static fromPath(path: string): Promise<TileSource>
531
+ static fromPath(path: string, options?: SourceOptions | undefined | null): Promise<TileSource>
525
532
  /**
526
533
  * Create a TileSource from a VPL (VersaTiles Pipeline Language) string
527
534
  *
@@ -586,7 +593,7 @@ export declare class TileSource {
586
593
  * VPL sources can be converted using `convertTo()` just like any other tile source.
587
594
  * The conversion will process the pipeline and write the output tiles to the target format.
588
595
  */
589
- static fromVpl(vpl: string, dir?: string | undefined | null): Promise<TileSource>
596
+ static fromVpl(vpl: string, dir?: string | undefined | null, options?: SourceOptions | undefined | null): Promise<TileSource>
590
597
  /**
591
598
  * Create a TileSource from a pipeline definition (JSON)
592
599
  *
@@ -598,8 +605,10 @@ export declare class TileSource {
598
605
  * * `steps_json` - JSON string describing the pipeline steps
599
606
  * * `dir` - Optional base directory for resolving relative file paths.
600
607
  * Defaults to the current working directory if not specified.
608
+ * * `options` - Optional source options; `sshIdentity` names a private key
609
+ * file for `sftp://` sources inside the pipeline
601
610
  */
602
- static fromPipeline(stepsJson: string, dir?: string | undefined | null): Promise<TileSource>
611
+ static fromPipeline(stepsJson: string, dir?: string | undefined | null, options?: SourceOptions | undefined | null): Promise<TileSource>
603
612
  /**
604
613
  * Convert this tile source to another format
605
614
  *
@@ -609,7 +618,7 @@ export declare class TileSource {
609
618
  *
610
619
  * # Arguments
611
620
  *
612
- * * `output` - Path to the output tile container
621
+ * * `output` - Path to the output tile container, or an `sftp://` URL
613
622
  * * `options` - Optional conversion options (zoom range, bbox, compression, etc.)
614
623
  * * `on_progress` - Optional callback for progress updates
615
624
  * * `on_message` - Optional callback for step/warning/error messages
@@ -621,7 +630,11 @@ export declare class TileSource {
621
630
  * - `bboxBorder`: Add border tiles around bbox (in tile units)
622
631
  * - `compress`: Output compression ("gzip", "brotli", "uncompressed")
623
632
  * - `flipY`: Flip tiles vertically (TMS ↔ XYZ coordinate systems)
624
- * - `swapXY`: Swap X and Y tile coordinates
633
+ * - `swapXy`: Swap X and Y tile coordinates
634
+ * - `writerOptions`: Format-specific settings for the output writer, keyed by the
635
+ * writer's own `snake_case` names, e.g. `{ allow_unclustered: 'true' }` for PMTiles
636
+ * - `sshIdentity`: Private key file for an `sftp://` input, defaulting to
637
+ * `VERSATILES_SSH_IDENTITY`
625
638
  *
626
639
  * # Progress Callbacks
627
640
  *
@@ -780,6 +793,33 @@ export declare class TileSource {
780
793
  sourceType(): SourceType
781
794
  }
782
795
 
796
+ /**
797
+ * Checks VPL text against the known operations, without building the pipeline.
798
+ *
799
+ * Building opens files and fetches URLs, so it cannot run on a keystroke — and it conflates two
800
+ * failures. `from_container filename=missing.mbtiles` is the *world* being wrong; `from_debug
801
+ * format=png nonsense=1` is the *pipeline* being wrong. This reports only the second kind, and
802
+ * performs no I/O.
803
+ *
804
+ * Returns a JSON string, with the same result shape as [`parse_vpl`]:
805
+ *
806
+ * - on success, `{"ok": true, "problems": [{"message": "...", "path": [0], "property": "nonsense"}]}`
807
+ * — an empty `problems` array means nothing is wrong with the pipeline
808
+ * - on failure, `{"ok": false, "error": {...}}` — the text did not parse at all
809
+ *
810
+ * `path` addresses the offending node by index: `[2]` is the third node of the pipeline, and
811
+ * descending into a node's bracketed sources appends the source index and then the node index.
812
+ * The same walk applies to the tree from `parseVplCst`, which is how this becomes a span.
813
+ *
814
+ * Two things are deliberately *not* reported, because doing so would reject VPL that runs:
815
+ * parameter values are never validated (`color=red` passes here and fails at build time), and
816
+ * neither are enum values, since the accepted set includes aliases the metadata does not list.
817
+ *
818
+ * @param vpl - the VPL text to check
819
+ * @returns a JSON string describing either the problems found or a parse error
820
+ */
821
+ export declare function checkVpl(vpl: string): string
822
+
783
823
  /**
784
824
  * Convert tiles from one container format to another
785
825
  *
@@ -790,7 +830,7 @@ export declare class TileSource {
790
830
  * # Arguments
791
831
  *
792
832
  * * `input` - Path or URL to the input tile container
793
- * * `output` - Path to the output tile container
833
+ * * `output` - Path to the output tile container, or an `sftp://` URL
794
834
  * * `options` - Optional conversion options (zoom range, bbox, compression, etc.)
795
835
  * * `on_progress` - Optional callback for progress updates
796
836
  * * `on_message` - Optional callback for step/warning/error messages
@@ -802,7 +842,11 @@ export declare class TileSource {
802
842
  * - `bboxBorder`: Add border tiles around bbox (in tile units)
803
843
  * - `compress`: Output compression ("gzip", "brotli", "uncompressed")
804
844
  * - `flipY`: Flip tiles vertically (TMS ↔ XYZ coordinate systems)
805
- * - `swapXY`: Swap X and Y tile coordinates
845
+ * - `swapXy`: Swap X and Y tile coordinates
846
+ * - `writerOptions`: Format-specific settings for the output writer, keyed by the
847
+ * writer's own `snake_case` names, e.g. `{ allow_unclustered: 'true' }` for PMTiles
848
+ * - `sshIdentity`: Private key file for an `sftp://` input, defaulting to
849
+ * `VERSATILES_SSH_IDENTITY`
806
850
  *
807
851
  * # Progress Callbacks
808
852
  *
@@ -951,6 +995,51 @@ export interface ConvertOptions {
951
995
  * **Default:** `false` (no swapping)
952
996
  */
953
997
  swapXy?: boolean
998
+ /**
999
+ * Format-specific options for the output writer
1000
+ *
1001
+ * Which options exist depends on the output format. An option the chosen
1002
+ * writer does not accept is an error, not a no-op, and the message lists
1003
+ * what that format does accept.
1004
+ *
1005
+ * Keys reach the writer verbatim, so they keep the writer's own spelling —
1006
+ * `snake_case`, unlike the camelCase fields around them here.
1007
+ *
1008
+ * PMTiles accepts three; every other format accepts none:
1009
+ * - `allow_unclustered` — write an archive that is not physically clustered,
1010
+ * in a single pass, at the cost of more range requests when serving it
1011
+ * - `reorder` — stay clustered by writing the tile data twice, at the cost of
1012
+ * a second pass and temporary disk the size of the output
1013
+ * - `temp_dir` — where `reorder` puts that temporary file (default: the
1014
+ * output file's own directory)
1015
+ *
1016
+ * They matter only for a source that cannot supply Hilbert order, such as a
1017
+ * pipeline containing `raster_overview`; writing one to `.pmtiles` without
1018
+ * either opt-in is an error. Asking for both is also an error, since they buy
1019
+ * different things. Booleans are strings — `'true'`, `'1'` or `'yes'`,
1020
+ * case-insensitively, and likewise for false.
1021
+ *
1022
+ * **Example:** `{ allow_unclustered: 'true' }` when writing PMTiles
1023
+ *
1024
+ * **Default:** No writer options
1025
+ */
1026
+ writerOptions?: Record<string, string>
1027
+ /**
1028
+ * SSH identity (private key) file used for `sftp://` sources
1029
+ *
1030
+ * A path to a private key file on disk — the same thing the CLI's
1031
+ * `--ssh-identity` names. It is read when the input is an `sftp://` URL, and
1032
+ * ignored otherwise.
1033
+ *
1034
+ * Without it, the `VERSATILES_SSH_IDENTITY` environment variable is used;
1035
+ * without that, a password in the URL, the SSH agent and the usual `~/.ssh`
1036
+ * defaults still apply.
1037
+ *
1038
+ * **Example:** `'/home/deploy/.ssh/id_ed25519'`
1039
+ *
1040
+ * **Default:** `VERSATILES_SSH_IDENTITY`, if set
1041
+ */
1042
+ sshIdentity?: string
954
1043
  }
955
1044
 
956
1045
  /**
@@ -961,6 +1050,77 @@ export interface ConvertOptions {
961
1050
  */
962
1051
  export declare function generateVplTypescript(): string
963
1052
 
1053
+ /**
1054
+ * Compute the per-layer byte breakdown of an uncompressed vector tile.
1055
+ *
1056
+ * # Arguments
1057
+ *
1058
+ * * `tile` - Uncompressed MVT bytes, as returned by `TileSource.getTile()`.
1059
+ *
1060
+ * # Returns
1061
+ *
1062
+ * One entry per layer, in the order the layers appear in the tile. A tile with
1063
+ * no layers — including a zero-byte buffer, which is what an empty tile encodes
1064
+ * to — yields an empty array. `getTile()` signals a missing tile with `null`,
1065
+ * so an empty buffer means an empty tile rather than a mistake.
1066
+ *
1067
+ * # Errors
1068
+ *
1069
+ * Returns an error if the buffer is not an uncompressed vector tile. Compressed
1070
+ * input and raster input are named as such rather than surfacing as a protobuf
1071
+ * parse failure.
1072
+ *
1073
+ * # Examples
1074
+ *
1075
+ * ```javascript
1076
+ * const { TileSource, layerStats } = require('@versatiles/versatiles-rs');
1077
+ *
1078
+ * const source = await TileSource.fromPath('berlin.mbtiles');
1079
+ * const tile = await source.getTile(14, 8802, 5373);
1080
+ * if (tile) {
1081
+ * for (const layer of layerStats(tile)) {
1082
+ * console.log(layer.name, layer.geometryBytes, layer.propertyBytes, layer.encodedBytes);
1083
+ * }
1084
+ * }
1085
+ * ```
1086
+ */
1087
+ export declare function layerStats(tile: Buffer): Array<LayerStats>
1088
+
1089
+ /**
1090
+ * Byte breakdown of a single layer within a vector tile.
1091
+ *
1092
+ * All figures are uncompressed MVT content — what can actually be shrunk.
1093
+ * The named categories plus `otherBytes` sum exactly to `encodedBytes`, so a
1094
+ * stacked bar built from them adds up without a fudge factor.
1095
+ */
1096
+ export interface LayerStats {
1097
+ /** Layer name. */
1098
+ name: string
1099
+ /** Number of features in the layer. */
1100
+ featureCount: number
1101
+ /** Total number of geometry vertices across all features. */
1102
+ vertexCount: number
1103
+ /** Geometry command streams. */
1104
+ geometryBytes: number
1105
+ /** Per-feature property references (the packed `tag_ids` varints). */
1106
+ tagBytes: number
1107
+ /** Key strings in the property table. */
1108
+ keyBytes: number
1109
+ /** Encoded value messages in the property table. */
1110
+ valueBytes: number
1111
+ /** `keyBytes + valueBytes` — the whole property table. */
1112
+ propertyBytes: number
1113
+ /** Feature ids. Zero for sources that carry none. */
1114
+ idBytes: number
1115
+ /**
1116
+ * Framing, geometry-type fields and the layer name: whatever the named
1117
+ * categories do not account for.
1118
+ */
1119
+ otherBytes: number
1120
+ /** Exact serialized size of the layer. */
1121
+ encodedBytes: number
1122
+ }
1123
+
964
1124
  /**
965
1125
  * Status or diagnostic message from an operation
966
1126
  *
@@ -984,17 +1144,43 @@ export interface MessageData {
984
1144
  message: string
985
1145
  }
986
1146
 
987
- /** Probe result with container information */
988
- export interface ProbeResult {
989
- /** Source name or path */
990
- sourceName: string
991
- /** Container type (e.g., "mbtiles", "versatiles") */
992
- containerName: string
993
- /** TileJSON metadata as JSON string */
994
- tileJsonRaw: string
995
- /** Reader parameters */
996
- parameters: SourceMetadata
997
- }
1147
+ /**
1148
+ * Parses VPL text into the JSON pipeline representation.
1149
+ *
1150
+ * Returns a JSON string rather than throwing, because an editor parses on every keystroke and a
1151
+ * syntax error there is an expected state rather than an exception:
1152
+ *
1153
+ * - on success, `{"ok": true, "pipeline": [...]}` — the same shape `VPL.toJSON()` produces
1154
+ * - on failure, `{"ok": false, "error": {"span": {...}, "message": "...", "context": [...],
1155
+ * "trace": "..."}}`
1156
+ *
1157
+ * `span` holds **byte** offsets into the input, so a caller can convert to whatever unit it
1158
+ * counts in. `trace` is the caret-annotated rendering the CLI prints.
1159
+ *
1160
+ * @param vpl - the VPL text to parse
1161
+ * @returns a JSON string describing either the pipeline or the error
1162
+ */
1163
+ export declare function parseVpl(vpl: string): string
1164
+
1165
+ /**
1166
+ * Parses VPL text into the lossless syntax tree.
1167
+ *
1168
+ * Unlike [`parse_vpl`], which returns the semantic pipeline the engine runs, this keeps
1169
+ * everything the file contains — comments, whitespace, the order the parameters were written in,
1170
+ * and which quotes the author chose. Hold this tree if you mean to edit a file somebody wrote:
1171
+ * [`stringify_vpl_cst`] prints it back byte for byte, so the parts nobody touched stay untouched.
1172
+ *
1173
+ * Returns a JSON string, with the same result shape as [`parse_vpl`]:
1174
+ *
1175
+ * - on success, `{"ok": true, "cst": {...}}`
1176
+ * - on failure, `{"ok": false, "error": {...}}`
1177
+ *
1178
+ * Every token carries a `span` of **byte** offsets into the input.
1179
+ *
1180
+ * @param vpl - the VPL text to parse
1181
+ * @returns a JSON string describing either the syntax tree or the error
1182
+ */
1183
+ export declare function parseVplCst(vpl: string): string
998
1184
 
999
1185
  /**
1000
1186
  * Progress information for long-running operations
@@ -1157,6 +1343,20 @@ export interface ServerOptions {
1157
1343
  * **Default:** `false` (optimal recompression)
1158
1344
  */
1159
1345
  minimalRecompression?: boolean
1346
+ /**
1347
+ * `Cache-Control` header sent with every tile
1348
+ *
1349
+ * Tile URLs are stable — built from a mount name and a coordinate — so
1350
+ * when the tiles behind a mount change, the URL does not, and the browser
1351
+ * answers from its own cache. A dev server or an interactive preview that
1352
+ * re-mounts under the same name will otherwise keep showing the previous
1353
+ * edit.
1354
+ *
1355
+ * Example: `"no-cache"`.
1356
+ *
1357
+ * **Default:** `"public, max-age=2419200, no-transform"` (four weeks)
1358
+ */
1359
+ cacheControl?: string
1160
1360
  }
1161
1361
 
1162
1362
  /** Tile source metadata describing output characteristics */
@@ -1171,6 +1371,58 @@ export interface SourceMetadata {
1171
1371
  maxZoom: number
1172
1372
  }
1173
1373
 
1374
+ /**
1375
+ * Options for opening a tile source
1376
+ *
1377
+ * Everything here is optional; an omitted field keeps the default behaviour.
1378
+ */
1379
+ export interface SourceOptions {
1380
+ /**
1381
+ * SSH identity (private key) file used for `sftp://` sources
1382
+ *
1383
+ * A path to a private key file on disk — the same thing the CLI's
1384
+ * `--ssh-identity` names. It is read when the source is an `sftp://` URL,
1385
+ * including one reached through a pipeline, and ignored otherwise.
1386
+ *
1387
+ * Without it, the `VERSATILES_SSH_IDENTITY` environment variable is used;
1388
+ * without that, a password in the URL, the SSH agent and the usual `~/.ssh`
1389
+ * defaults still apply. Set this to give one source a different key than the
1390
+ * rest of the process.
1391
+ *
1392
+ * **Example:** `'/home/deploy/.ssh/id_ed25519'`
1393
+ *
1394
+ * **Default:** `VERSATILES_SSH_IDENTITY`, if set
1395
+ */
1396
+ sshIdentity?: string
1397
+ }
1398
+
1399
+ /**
1400
+ * Writes a pipeline as VPL text.
1401
+ *
1402
+ * Takes the JSON produced by `VPL.toJSON()` and returns text that `TileSource.fromVpl` accepts.
1403
+ * Quoting is decided by the grammar, so values containing spaces, quotes or newlines are handled
1404
+ * without the caller knowing the rules.
1405
+ *
1406
+ * @param stepsJson - JSON string describing the pipeline steps
1407
+ * @returns the pipeline as VPL text
1408
+ */
1409
+ export declare function stringifyVpl(stepsJson: string): string
1410
+
1411
+ /**
1412
+ * Writes a lossless syntax tree back out as VPL text.
1413
+ *
1414
+ * The inverse of [`parse_vpl_cst`]: an unedited tree prints back to exactly the text it was
1415
+ * parsed from. Fields that describe formatting — `leading`, `trailing`, `span` — may be omitted
1416
+ * when building a tree by hand, so a minimal node is `{"name": {"text": "from_debug"}}`.
1417
+ *
1418
+ * Note that `span` values are stale after an edit; they describe where a token *was*. Parse the
1419
+ * printed text again to get fresh ones.
1420
+ *
1421
+ * @param cstJson - JSON string describing the syntax tree
1422
+ * @returns the tree as VPL text
1423
+ */
1424
+ export declare function stringifyVplCst(cstJson: string): string
1425
+
1174
1426
  export interface TileJSON {
1175
1427
  version: string
1176
1428
  minzoom: number