Merge pull request #896 from hvithrafn/load-bytes
Modify Language.load to accept bytes directly
This commit is contained in:
commit
17abb583b2
2 changed files with 24 additions and 19 deletions
|
|
@ -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
|
||||
|
|
|
|||
2
lib/binding_web/tree-sitter-web.d.ts
vendored
2
lib/binding_web/tree-sitter-web.d.ts
vendored
|
|
@ -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;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue