s2-tools
Version:
A collection of geospatial tools primarily designed for WGS84, Web Mercator, and S2.
68 lines • 2.1 kB
JavaScript
import { createWriteStream } from 'fs';
import { open } from 'fs/promises';
/** The File writer is to be used by bun/node/deno on the local filesystem. */
export class FileWriter {
file;
#stream;
#textEncoder = new TextEncoder();
/** @param file - the location of the PMTiles data in the FS */
constructor(file) {
this.file = file;
this.#stream = createWriteStream(file, { flags: 'a+' }); // Open with append mode and create stream
}
/**
* Write data to the buffer
* @param data - the data to write
* @param offset - where in the buffer to start
*/
async write(data, offset) {
const fd = await open(this.file, 'r+'); // Open file for reading and writing
try {
await fd.write(data, 0, data.length, offset); // Write at the specified offset
}
finally {
await fd.close(); // Close the file after writing
}
}
/**
* Append data to the buffer
* @param data - the data to append
* @returns - a promise that resolves when the data is appended
*/
async append(data) {
return await new Promise((resolve, reject) => {
this.#stream.write(data, (err) => {
if (err instanceof Error)
reject(err);
else
resolve();
});
});
}
/**
* Append string to the buffer synchronously
* @param string - the string to append
*/
async appendString(string) {
await this.append(this.#textEncoder.encode(string));
}
/**
* Append data to the buffer synchronously
* @param data - the data to append
*/
appendSync(data) {
this.#stream.write(data); // Write data synchronously
}
/**
* Append string to the buffer synchronously
* @param string - the string to append
*/
appendStringSync(string) {
this.appendSync(this.#textEncoder.encode(string));
}
/** Close the file */
close() {
this.#stream.end();
}
}
//# sourceMappingURL=file.js.map