Skip to content

Commit

Permalink
fix: do not regenerate api object that does not change (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
paololim committed Aug 24, 2022
1 parent ab66f54 commit f9bb59a
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions packages/siwe-parser/lib/abnf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,22 @@ DIGIT = %x30-39
HEXDIG = DIGIT / "A" / "B" / "C" / "D" / "E" / "F"
`;

class GrammarApi {
static grammarObj = this.generateApi();

static generateApi() {
const api = new apgApi(GRAMMAR);
api.generate();
if (api.errors.length) {
console.error(api.errorsToAscii());
console.error(api.linesToAscii());
console.log(api.displayAttributeErrors());
throw new Error(`ABNF grammar has errors`);
}
return api.toObject();
}
}

export class ParsedMessage {
domain: string;
address: string;
Expand All @@ -159,16 +175,6 @@ export class ParsedMessage {
resources: Array<string> | null;

constructor(msg: string) {
const api = new apgApi(GRAMMAR);
api.generate();
if (api.errors.length) {
console.error(api.errorsToAscii());
console.error(api.linesToAscii());
console.log(api.displayAttributeErrors());
throw new Error(`ABNF grammar has errors`);
}

const grammarObj = api.toObject();
const parser = new apgLib.parser();
parser.ast = new apgLib.ast();
const id = apgLib.ids;
Expand Down Expand Up @@ -324,7 +330,7 @@ export class ParsedMessage {
};
parser.ast.callbacks.resources = resources;

const result = parser.parse(grammarObj, "sign-in-with-ethereum", msg);
const result = parser.parse(GrammarApi.grammarObj, "sign-in-with-ethereum", msg);
if (!result.success) {
throw new Error(`Invalid message: ${JSON.stringify(result)}`);
}
Expand Down

0 comments on commit f9bb59a

Please sign in to comment.