Skip to content

Commit 75afe92

Browse files
committed
Fixed #455
Make namespace max depth configurable in idGenerator
1 parent 12687dc commit 75afe92

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed

src/core/format/IdGenerator.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
import {sha3_256} from 'js-sha3';
1717
import * as utilities from './Utilities';
18+
import { idGeneratorConst } from './Utilities';
1819

1920
export class IdGenerator {
2021
/**
@@ -34,20 +35,21 @@ export class IdGenerator {
3435
/**
3536
* Parses a unified namespace name into a path.
3637
* @param {string} name The unified namespace name.
38+
* @param {number} maxDepth The max namespace depth (network configuration, default: 3)
3739
* @returns {array<module:coders/uint64~uint64>} The namespace path.
3840
*/
39-
public static generateNamespacePath = (name: string) => {
41+
public static generateNamespacePath = (name: string, maxDepth: number = idGeneratorConst.default_namespace_max_depth) => {
4042
if (0 >= name.length) {
4143
utilities.throwInvalidFqn('having zero length', name);
4244
}
4345
let namespaceId = utilities.idGeneratorConst.namespace_base_id;
4446
const path = [];
4547
const start = utilities.split(name, (substringStart, size) => {
4648
namespaceId = utilities.generateNamespaceId(namespaceId, utilities.extractPartName(name, substringStart, size));
47-
utilities.append(path, namespaceId, name);
49+
utilities.append(path, namespaceId, name, maxDepth);
4850
});
4951
namespaceId = utilities.generateNamespaceId(namespaceId, utilities.extractPartName(name, start, name.length - start));
50-
utilities.append(path, namespaceId, name);
52+
utilities.append(path, namespaceId, name, maxDepth);
5153
return path;
5254
}
5355
}

src/core/format/Utilities.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ export const tryParseUint = (str) => {
9494

9595
export const idGeneratorConst = {
9696
namespace_base_id: [0, 0],
97-
namespace_max_depth: 3,
97+
default_namespace_max_depth: 3,
9898
name_pattern: /^[a-z0-9][a-z0-9-_]*$/,
9999
};
100100

@@ -113,8 +113,8 @@ export const extractPartName = (name, start, size) => {
113113
return partName;
114114
};
115115

116-
export const append = (path, id, name) => {
117-
if (idGeneratorConst.namespace_max_depth === path.length) {
116+
export const append = (path, id, name, maxDepth) => {
117+
if (maxDepth === path.length) {
118118
this.throwInvalidFqn('too many parts', name);
119119
}
120120
path.push(id);

test/core/format/IdGenerator.spec.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,9 @@ describe('id generator', () => {
246246
expect(() => idGenerator.generateNamespacePath(name), `name ${name}`).to.throw('invalid part name'));
247247
});
248248

249+
it('rejects names depth exceeds maxDepth', () => {
250+
expect(() => idGenerator.generateNamespacePath('nem.xem', 1)).to.throw(Error, 'too many parts');
251+
});
249252
addBasicTests(idGenerator.generateNamespacePath);
250253
});
251254
});

0 commit comments

Comments
 (0)