Skip to content

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

feat(harper.js): use fs.readFile at Node.js runtime to avoid fetch issues

Asuka109a0740076fc8a
diff --git a/packages/harper.js/examples/commonjs-simple/index.js b/packages/harper.js/examples/commonjs-simple/index.js
index d21606e33..718a2ed7d 100644
--- a/packages/harper.js/examples/commonjs-simple/index.js
+++ b/packages/harper.js/examples/commonjs-simple/index.js
@@ -1,9 +1,9 @@
async function main() {
const harper = await import('harper.js');
// We cannot use `WorkerLinter` on Node.js since it relies on web-specific APIs.
- let linter = new harper.LocalLinter();
+ const linter = new harper.LocalLinter({ binary: harper.binary });
- let lints = await linter.lint('This is a example of how to use `harper.js`.');
+ const lints = await linter.lint('This is a example of how to use `harper.js`.');
console.log('Here are the results of linting the above text:');
diff --git a/packages/harper.js/examples/commonjs-simple/package.json b/packages/harper.js/examples/commonjs-simple/package.json
index ad222da08..55c0927fc 100644
--- a/packages/harper.js/examples/commonjs-simple/package.json
+++ b/packages/harper.js/examples/commonjs-simple/package.json
@@ -6,6 +6,6 @@
"start": "node index.js"
},
"dependencies": {
- "harper.js": "^0.14.0"
+ "harper.js": "*"
}
}
diff --git a/packages/harper.js/examples/commonjs-simple/yarn.lock b/packages/harper.js/examples/commonjs-simple/yarn.lock
deleted file mode 100644
index f54ca7e98..000000000
--- a/packages/harper.js/examples/commonjs-simple/yarn.lock
+++ /dev/null
@@ -1,8 +0,0 @@
-# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
-# yarn lockfile v1
-
-
-harper.js@^0.14.0:
- version "0.14.0"
- resolved "https://registry.yarnpkg.com/harper.js/-/harper.js-0.14.0.tgz#16d511070d4474dca501d4de1c76f7849ec0accb"
- integrity sha512-WRzop2PmFK2AZiqNMHHWDFIqFmS7DeoNVhyibyu7T6wuSe24E1Tunolz8ZzMPfcaKuBB2SY+yGbyTOyj+/QD8g==
diff --git a/packages/harper.js/src/Linter.test.ts b/packages/harper.js/src/Linter.test.ts
index 6bec3a0fd..25d85281a 100644
--- a/packages/harper.js/src/Linter.test.ts
+++ b/packages/harper.js/src/Linter.test.ts
@@ -170,7 +170,7 @@ for (const [linterName, Linter] of Object.entries(linters)) {
test(`${linterName} can add words to the dictionary`, async () => {
const source = 'asdf is not a word';
- const linter = new Linter();
+ const linter = new Linter({ binary });
let lints = await linter.lint(source);
expect(lints).toHaveLength(1);
@@ -182,7 +182,7 @@ for (const [linterName, Linter] of Object.entries(linters)) {
});
test(`${linterName} allows correct capitalization of "United States"`, async () => {
- const linter = new Linter();
+ const linter = new Linter({ binary });
const lints = await linter.lint('The United States is a big country.');
expect(lints).toHaveLength(0);
diff --git a/packages/harper.js/src/LocalLinter.ts b/packages/harper.js/src/LocalLinter.ts
index 41050bd74..4f4ec1c1a 100644
--- a/packages/harper.js/src/LocalLinter.ts
+++ b/packages/harper.js/src/LocalLinter.ts
@@ -106,14 +106,14 @@ export default class LocalLinter implements Linter {
}
async importWords(words: string[]): Promise<void> {
- await this.initialize();
+ const inner = await this.inner;
- return this.inner!.import_words(words);
+ return inner.import_words(words);
}
async exportWords(): Promise<string[]> {
- await this.initialize();
+ const inner = await this.inner;
- return this.inner!.export_words();
+ return inner.export_words();
}
}
diff --git a/packages/harper.js/src/binary.ts b/packages/harper.js/src/binary.ts
index a4d9d6b8b..c3cb5daaf 100644
--- a/packages/harper.js/src/binary.ts
+++ b/packages/harper.js/src/binary.ts
@@ -1,6 +1,6 @@
import { default as binaryUrl } from 'harper-wasm/harper_wasm_bg.wasm?no-inline';
import { default as binaryInlinedUrl } from 'harper-wasm/harper_wasm_bg.wasm?inline';
-import type { Span, Suggestion, Linter as WasmLinter } from 'harper-wasm';
+import type { InitInput, Span, Suggestion, Linter as WasmLinter } from 'harper-wasm';
import { assert } from './utils';
import { LintConfig } from './main';
@@ -11,7 +11,14 @@ export async function loadBinary(binary: string): Promise<typeof import('harper-
return _loadedBinaryMap.get(binary)!;
} else {
const exports = await import('harper-wasm');
- await exports.default({ module_or_path: binary });
+ let input: InitInput;
+ if (typeof window === 'undefined' && binary.startsWith('file://')) {
+ const fs = await import('node:fs/promises');
+ input = fs.readFile(new URL(binary).pathname);
+ } else {
+ input = binary;
+ }
+ await exports.default({ module_or_path: input });
_loadedBinaryMap.set(binary, exports);
return exports;
}
@@ -56,47 +63,48 @@ export function isSerializedRequest(v: unknown): v is SerializedRequest {
/** This class aims to define the communication protocol between the main thread and the worker.
* Note that much of the complication here comes from the fact that we can't serialize function calls or referenced WebAssembly memory.*/
export class BinaryModule {
- private exported: Promise<typeof import('harper-wasm')>;
+ public url: string | URL;
+
+ private get inner() {
+ return loadBinary(typeof this.url === 'string' ? this.url : this.url.href);
+ }
- constructor(public url: string | URL) {
- this.exported = import('harper-wasm').then(async (exports) => {
- await exports.default({ module_or_path: url });
- return exports;
- });
+ constructor(url: string | URL) {
+ this.url = url;
}
async applySuggestion(text: string, suggestion: Suggestion, span: Span): Promise<string> {
- const exported = await this.exported;
+ const exported = await this.inner;
return exported.apply_suggestion(text, span, suggestion);
}
async getDefaultLintConfigAsJSON(): Promise<string> {
- const exported = await this.exported;
+ const exported = await this.inner;
return exported.get_default_lint_config_as_json();
}
async getDefaultLintConfig(): Promise<LintConfig> {
- const exported = await this.exported;
+ const exported = await this.inner;
return exported.get_default_lint_config();
}
async toTitleCase(text: string): Promise<string> {
- const exported = await this.exported;
+ const exported = await this.inner;
return exported.to_title_case(text);
}
async setup(): Promise<void> {
- const exported = await this.exported;
+ const exported = await this.inner;
exported.setup();
}
async createLinter(): Promise<WasmLinter> {
- const exported = await this.exported;
+ const exported = await this.inner;
return exported.Linter.new();
}
async serializeArg(arg: any): Promise<RequestArg> {
- const { Lint, Span, Suggestion } = await this.exported;
+ const { Lint, Span, Suggestion } = await this.inner;
if (Array.isArray(arg)) {
return {
@@ -144,7 +152,7 @@ export class BinaryModule {
}
async deserializeArg(requestArg: RequestArg): Promise<any> {
- const { Lint, Span, Suggestion } = await this.exported;
+ const { Lint, Span, Suggestion } = await this.inner;
switch (requestArg.type) {
case 'undefined':
diff --git a/packages/harper.js/vite.config.ts b/packages/harper.js/vite.config.ts
index 8671ddced..5a970a016 100644
--- a/packages/harper.js/vite.config.ts
+++ b/packages/harper.js/vite.config.ts
@@ -28,6 +28,7 @@ export default defineConfig({
minify: false,
assetsInlineLimit: 0,
rollupOptions: {
+ external: [/^node:/],
output: {
minifyInternalExports: false,
inlineDynamicImports: true
diff --git a/packages/package.json b/packages/package.json
index 86cbe7f0f..adaf7765d 100644
--- a/packages/package.json
+++ b/packages/package.json
@@ -5,7 +5,8 @@
"web",
"vscode-plugin",
"harper.js",
- "wordpress-plugin"
+ "wordpress-plugin",
+ "harper.js/examples/commonjs-simple"
],
"devDependencies": {
"eslint": "^8.28.0",