From c994adbf61790f834381b3dae051a445513a05ce Mon Sep 17 00:00:00 2001 From: hvithrafn Date: Mon, 25 Jan 2021 00:12:35 -0700 Subject: [PATCH] Modify Language.load to accept bytes directly --- lib/binding_web/binding.js | 41 ++++++++++++++++------------ lib/binding_web/tree-sitter-web.d.ts | 2 +- 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/lib/binding_web/binding.js b/lib/binding_web/binding.js index 20a29905..47d72742 100644 --- a/lib/binding_web/binding.js +++ b/lib/binding_web/binding.js @@ -856,26 +856,31 @@ class Language { ); } - static load(url) { + static load(input) { let bytes; - if ( - typeof process !== 'undefined' && - process.versions && - process.versions.node - ) { - const fs = require('fs'); - bytes = Promise.resolve(fs.readFileSync(url)); + if (input instanceof Uint8Array) { + bytes = Promise.resolve(input); } else { - bytes = fetch(url) - .then(response => response.arrayBuffer() - .then(buffer => { - if (response.ok) { - return new Uint8Array(buffer); - } else { - const body = new TextDecoder('utf-8').decode(buffer); - throw new Error(`Language.load failed with status ${response.status}.\n\n${body}`) - } - })); + const url = input; + if ( + typeof process !== 'undefined' && + process.versions && + process.versions.node + ) { + const fs = require('fs'); + bytes = Promise.resolve(fs.readFileSync(url)); + } else { + bytes = fetch(url) + .then(response => response.arrayBuffer() + .then(buffer => { + if (response.ok) { + return new Uint8Array(buffer); + } else { + const body = new TextDecoder('utf-8').decode(buffer); + throw new Error(`Language.load failed with status ${response.status}.\n\n${body}`) + } + })); + } } // emscripten-core/emscripten#12969 diff --git a/lib/binding_web/tree-sitter-web.d.ts b/lib/binding_web/tree-sitter-web.d.ts index 092c9353..172d2cd6 100644 --- a/lib/binding_web/tree-sitter-web.d.ts +++ b/lib/binding_web/tree-sitter-web.d.ts @@ -127,7 +127,7 @@ declare module 'web-tree-sitter' { } class Language { - static load(path: string): Promise; + static load(input: string | Uint8Array): Promise; readonly version: number; readonly fieldCount: number;