Skip to content

Commit

Permalink
use better solution
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenjoezhang committed Oct 22, 2023
1 parent 0af0983 commit 919132a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
3 changes: 2 additions & 1 deletion lib/permalink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ class Permalink {

stringify(data) {
return this.rule.replace(rParam, (match, name) => {
if (['permalink', 'path'].includes(name)) {
const descriptor = Object.getOwnPropertyDescriptor(data, name);
if (descriptor && typeof descriptor.get === 'function') {
throw new Error('Invalid permalink setting!');
}
return data[name];
Expand Down
17 changes: 13 additions & 4 deletions test/permalink.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,18 @@ describe('Permalink', () => {
});

it('stringify() - avoid infinite loops', () => {
permalink = new Permalink('/:permalink');
(() => permalink.stringify({})).should.throw('Invalid permalink setting!');
permalink = new Permalink('/:path');
(() => permalink.stringify({})).should.throw('Invalid permalink setting!');
const post = {
get path() {
return this.permalink;
},

get permalink() {
const permalink = new Permalink('/:permalink');
return permalink.stringify(post);
}
};

(() => post.path).should.throw('Invalid permalink setting!');
(() => post.permalink).should.throw('Invalid permalink setting!');
});
});

0 comments on commit 919132a

Please sign in to comment.