Skip to content

Offline, privacy-first grammar checker. Fast, open-source, Rust-powered

refactor(harper.js): we do not need named types for Weirpacks (#2657)

Elijah Pottereef2380fbc77
diff --git a/harper-core/src/weirpack/mod.rs b/harper-core/src/weirpack/mod.rs
index 2876e9246..f034c97e3 100644
--- a/harper-core/src/weirpack/mod.rs
+++ b/harper-core/src/weirpack/mod.rs
@@ -1,3 +1,5 @@
+//! See [our main documentation](https://writewithharper.com/docs/weir#Weirpacks) on Weir and the Weirpack format.
+
use std::io::{Read, Write};
use std::path::Path;
diff --git a/packages/harper.js/src/main.ts b/packages/harper.js/src/main.ts
index 12e45f5a6..ca1c82788 100644
--- a/packages/harper.js/src/main.ts
+++ b/packages/harper.js/src/main.ts
@@ -14,7 +14,7 @@ export type {
export { default as LocalLinter } from './LocalLinter';
export type { default as Summary } from './Summary';
export { default as WorkerLinter } from './WorkerLinter';
-export type { WeirpackArchive, WeirpackFileMap, WeirpackManifest } from './weirpack';
+export type { WeirpackArchive } from './weirpack';
export { packWeirpackFiles, unpackWeirpackBytes } from './weirpack';
/** A linting rule configuration dependent on upstream Harper's available rules.
* This is a record, since you shouldn't hard-code the existence of any particular rules and should generalize based on this struct. */
diff --git a/packages/harper.js/src/weirpack.ts b/packages/harper.js/src/weirpack.ts
index c82fa949d..6c12cbec7 100644
--- a/packages/harper.js/src/weirpack.ts
+++ b/packages/harper.js/src/weirpack.ts
@@ -1,16 +1,16 @@
import { strFromU8, strToU8, unzipSync, zipSync } from 'fflate';
-export type WeirpackManifest = Record<string, unknown>;
-export type WeirpackFileMap = Map<string, string>;
-
export type WeirpackArchive = {
- manifest: WeirpackManifest;
- files: WeirpackFileMap;
+ manifest: Record<string, unknown>;
+ files: Map<string, string>;
};
const manifestFilename = 'manifest.json';
-export function packWeirpackFiles(files: WeirpackFileMap): Uint8Array {
+/** Convert a Weirpack file system into a real Weirpack binary by compressing and serializing them into a byte array.
+ *
+ * For clarity on what a Weirpack is, read [the Weir documentation.](https://writewithharper.com/docs/weir#Weirpacks) */
+export function packWeirpackFiles(files: Map<string, string>): Uint8Array {
if (!files.has(manifestFilename)) {
throw new Error('Weirpack is missing manifest.json');
}
@@ -23,6 +23,7 @@ export function packWeirpackFiles(files: WeirpackFileMap): Uint8Array {
return zipSync(entries, { level: 6 });
}
+/** Decompress and deserialize a Weirpack from a byte array. */
export function unpackWeirpackBytes(bytes: Uint8Array): WeirpackArchive {
const archive = unzipSync(bytes);
const manifestBytes = archive[manifestFilename];
@@ -31,8 +32,8 @@ export function unpackWeirpackBytes(bytes: Uint8Array): WeirpackArchive {
}
const manifestText = strFromU8(manifestBytes);
- const manifest = JSON.parse(manifestText) as WeirpackManifest;
- const files: WeirpackFileMap = new Map();
+ const manifest = JSON.parse(manifestText);
+ const files = new Map();
files.set(manifestFilename, manifestText);
const fileNames = Object.keys(archive);