Skip to content

Commit 17abb58

Browse files
authored
Merge pull request tree-sitter#896 from hvithrafn/load-bytes
Modify Language.load to accept bytes directly
2 parents da5dcba + c994adb commit 17abb58

File tree

2 files changed

+24
-19
lines changed

2 files changed

+24
-19
lines changed

lib/binding_web/binding.js

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -856,26 +856,31 @@ class Language {
856856
);
857857
}
858858

859-
static load(url) {
859+
static load(input) {
860860
let bytes;
861-
if (
862-
typeof process !== 'undefined' &&
863-
process.versions &&
864-
process.versions.node
865-
) {
866-
const fs = require('fs');
867-
bytes = Promise.resolve(fs.readFileSync(url));
861+
if (input instanceof Uint8Array) {
862+
bytes = Promise.resolve(input);
868863
} else {
869-
bytes = fetch(url)
870-
.then(response => response.arrayBuffer()
871-
.then(buffer => {
872-
if (response.ok) {
873-
return new Uint8Array(buffer);
874-
} else {
875-
const body = new TextDecoder('utf-8').decode(buffer);
876-
throw new Error(`Language.load failed with status ${response.status}.\n\n${body}`)
877-
}
878-
}));
864+
const url = input;
865+
if (
866+
typeof process !== 'undefined' &&
867+
process.versions &&
868+
process.versions.node
869+
) {
870+
const fs = require('fs');
871+
bytes = Promise.resolve(fs.readFileSync(url));
872+
} else {
873+
bytes = fetch(url)
874+
.then(response => response.arrayBuffer()
875+
.then(buffer => {
876+
if (response.ok) {
877+
return new Uint8Array(buffer);
878+
} else {
879+
const body = new TextDecoder('utf-8').decode(buffer);
880+
throw new Error(`Language.load failed with status ${response.status}.\n\n${body}`)
881+
}
882+
}));
883+
}
879884
}
880885

881886
// emscripten-core/emscripten#12969

lib/binding_web/tree-sitter-web.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ declare module 'web-tree-sitter' {
127127
}
128128

129129
class Language {
130-
static load(path: string): Promise<Language>;
130+
static load(input: string | Uint8Array): Promise<Language>;
131131

132132
readonly version: number;
133133
readonly fieldCount: number;

0 commit comments

Comments
 (0)