Merge pull request #896 from hvithrafn/load-bytes

Modify Language.load to accept bytes directly
This commit is contained in:
Max Brunsfeld 2021-01-25 09:23:01 -08:00 committed by GitHub
commit 17abb583b2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 19 deletions

View file

@ -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

View file

@ -127,7 +127,7 @@ declare module 'web-tree-sitter' {
}
class Language {
static load(path: string): Promise<Language>;
static load(input: string | Uint8Array): Promise<Language>;
readonly version: number;
readonly fieldCount: number;