@ndn/naming-convention2
Advanced tools
Comparing version 0.0.20191223-beta.1 to 0.0.20200606
@@ -1,24 +0,2 @@ | ||
import { NamingConvention } from "@ndn/packet"; | ||
/** KeywordNameComponent, interpreted as string. */ | ||
export declare const Keyword: NamingConvention<string, string>; | ||
/** SegmentNameComponent, interpreted as number. */ | ||
export declare const Segment: NamingConvention<number, number>; | ||
/** ByteOffsetNameComponent, interpreted as number. */ | ||
export declare const ByteOffset: NamingConvention<number, number>; | ||
/** VersionNameComponent, interpreted as number. */ | ||
export declare const Version: NamingConvention<number, number>; | ||
/** TimestampNameComponent, interpreted as number. */ | ||
export declare const Timestamp: NamingConvention<number | Date, number> & { | ||
/** | ||
* TimestampNameComponent, interpreted as Date. | ||
* Reject during parsing if value is not a multiple of milliseconds. | ||
*/ | ||
Date: NamingConvention<number | Date, Date>; | ||
/** | ||
* TimestampNameComponent, interpreted as Date. | ||
* Round to nearest milliseconds during parsing. | ||
*/ | ||
DateInexact: NamingConvention<number | Date, Date>; | ||
}; | ||
/** SequenceNumNameComponent, interpreted as number. */ | ||
export declare const SequenceNum: NamingConvention<number, number>; | ||
export * from "./conventions"; | ||
export * from "./alt-uri"; |
@@ -1,84 +0,2 @@ | ||
import { Component } from "@ndn/packet"; | ||
import { Encoder, NNI } from "@ndn/tlv"; | ||
class Typed { | ||
constructor(tt) { | ||
this.tt = tt; | ||
} | ||
match(comp) { | ||
return comp.type === this.tt; | ||
} | ||
} | ||
class TypedString extends Typed { | ||
constructor(tt) { | ||
super(tt); | ||
} | ||
create(v) { | ||
return new Component(this.tt, v); | ||
} | ||
parse(comp) { | ||
return comp.text; | ||
} | ||
} | ||
class TypedNumber extends Typed { | ||
constructor(tt) { | ||
super(tt); | ||
} | ||
create(v) { | ||
return new Component(this.tt, Encoder.encode(NNI(v), 8)); | ||
} | ||
parse(comp) { | ||
return NNI.decode(comp.value); | ||
} | ||
} | ||
class TimestampBase extends Typed { | ||
constructor() { | ||
super(0x24); | ||
} | ||
create(v) { | ||
if (typeof v !== "number") { | ||
v = v.getTime() * 1000; | ||
} | ||
return TypedNumber.prototype.create.call(this, v); | ||
} | ||
} | ||
class TimestampNumber extends TimestampBase { | ||
parse(comp) { | ||
return TypedNumber.prototype.parse.call(this, comp); | ||
} | ||
} | ||
class TimestampDate extends TimestampBase { | ||
constructor(strict) { | ||
super(); | ||
this.strict = strict; | ||
} | ||
parse(comp) { | ||
const n = TypedNumber.prototype.parse.call(this, comp); | ||
if (this.strict && n % 1000 !== 0) { | ||
throw new Error("Timestamp is not multiple of milliseconds"); | ||
} | ||
return new Date(n / 1000); | ||
} | ||
} | ||
/** KeywordNameComponent, interpreted as string. */ | ||
export const Keyword = new TypedString(0x20); | ||
/** SegmentNameComponent, interpreted as number. */ | ||
export const Segment = new TypedNumber(0x21); | ||
/** ByteOffsetNameComponent, interpreted as number. */ | ||
export const ByteOffset = new TypedNumber(0x22); | ||
/** VersionNameComponent, interpreted as number. */ | ||
export const Version = new TypedNumber(0x23); | ||
/** TimestampNameComponent, interpreted as number. */ | ||
export const Timestamp = Object.assign(new TimestampNumber(), { | ||
/** | ||
* TimestampNameComponent, interpreted as Date. | ||
* Reject during parsing if value is not a multiple of milliseconds. | ||
*/ | ||
Date: new TimestampDate(true), | ||
/** | ||
* TimestampNameComponent, interpreted as Date. | ||
* Round to nearest milliseconds during parsing. | ||
*/ | ||
DateInexact: new TimestampDate(false), | ||
}); | ||
/** SequenceNumNameComponent, interpreted as number. */ | ||
export const SequenceNum = new TypedNumber(0x25); | ||
export * from "./conventions.js"; | ||
export * from "./alt-uri.js"; |
{ | ||
"name": "@ndn/naming-convention2", | ||
"version": "0.0.20191223-beta.1", | ||
"description": "NDNts: Naming Convention v0.3", | ||
"version": "0.0.20200606", | ||
"description": "NDNts: Naming Convention rev2", | ||
"keywords": [ | ||
@@ -24,6 +24,6 @@ "NDN", | ||
"dependencies": { | ||
"@ndn/packet": "0.0.20191223-beta.1", | ||
"@ndn/tlv": "0.0.20191223-beta.1", | ||
"@ndn/packet": "0.0.20200606", | ||
"@ndn/tlv": "0.0.20200606", | ||
"tslib": "*" | ||
} | ||
} |
@@ -9,3 +9,3 @@ # @ndn/naming-convention2 | ||
```ts | ||
import { Keyword, Version, Segment } from "@ndn/naming-convention2"; | ||
import { Keyword, Version, Segment, AltUri } from "@ndn/naming-convention2"; | ||
// We also have ByteOffset, Timestamp, and SequenceNum. | ||
@@ -19,9 +19,9 @@ | ||
let name = new Name(["A", Keyword.create("metadata")]); | ||
assert.equal(name.toString(), "/A/32=metadata"); | ||
assert.equal(name.toString(), "/8=A/32=metadata"); | ||
// name.append() has an overload for convention component. | ||
name = name.append(Version, 3); | ||
assert.equal(name.toString(), "/A/32=metadata/35=%03"); | ||
assert.equal(name.toString(), "/8=A/32=metadata/35=%03"); | ||
name = name.append(Segment, 0); | ||
assert.equal(name.toString(), "/A/32=metadata/35=%03/33=%00"); | ||
assert.equal(name.toString(), "/8=A/32=metadata/35=%03/33=%00"); | ||
@@ -45,2 +45,12 @@ // convention.match() checks whether a Component follows the convention. | ||
assert.equal(name.at(-1).as(Segment), 0); | ||
// If you need alternate URI syntax, use AltUri.ofName() or AltUri.ofComponent(). | ||
// Make sure you are importing AltUri from this package, not from @ndn/packet package. | ||
assert.equal(AltUri.ofName(name), "/A/32=metadata/v=3/seg=0"); | ||
assert.equal(AltUri.ofComponent(name.at(2)), "v=3"); | ||
// This feature is not in the regular component.toString() and name.toString() methods, | ||
// because not every application would adopt this particular set of naming conventions. | ||
// It is incorrect to interpret "35=%03" as a "version 3" everywhere, because in some application | ||
// it could mean something completely different. | ||
// Using AltUri from this package indicates you have adopted these naming conventions. | ||
``` |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
7524
8
123
54
1
+ Added@ndn/packet@0.0.20200606(transitive)
+ Added@ndn/tlv@0.0.20200606(transitive)
- Removed@ndn/packet@0.0.20191223-beta.1(transitive)
- Removed@ndn/tlv@0.0.20191223-beta.1(transitive)
Updated@ndn/packet@0.0.20200606
Updated@ndn/tlv@0.0.20200606