Skip to content

Commit

Permalink
Merge pull request #46 from badgateway/sf-date
Browse files Browse the repository at this point in the history
Implement the 'date' type
  • Loading branch information
evert committed Jun 13, 2023
2 parents 6fbfe32 + 2708c62 commit 823f522
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 15 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"env": {
"browser": true,
"node": true,
"es6": true
"es6": true,
"mocha": true
},
"extends": [
"eslint:recommended",
Expand Down
19 changes: 13 additions & 6 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
ChangeLog
=========

2.0.0 (????-??-??)
------------------

* Support for a new 'Date' type, from draft [draft-ietf-httpbis-sfbis-02][7].


1.0.0 (2023-06-13)
------------------

Expand Down Expand Up @@ -84,9 +90,10 @@ ChangeLog
* First version!
* Parses all of the [04 draft of the specification][1].

[1]: https://tools.ietf.org/html/draft-ietf-httpbis-header-structure-04 [2]:
https://tools.ietf.org/html/draft-ietf-httpbis-header-structure-09 [3]:
https://tools.ietf.org/html/draft-ietf-httpbis-header-structure-10 [4]:
https://tools.ietf.org/html/draft-ietf-httpbis-header-structure-13 [5]:
https://datatracker.ietf.org/doc/html/rfc8941 [6]:
https://github.com/httpwg/structured-field-tests
[1]: https://tools.ietf.org/html/draft-ietf-httpbis-header-structure-04
[2]: https://tools.ietf.org/html/draft-ietf-httpbis-header-structure-09
[3]: https://tools.ietf.org/html/draft-ietf-httpbis-header-structure-10
[4]: https://tools.ietf.org/html/draft-ietf-httpbis-header-structure-13
[5]: https://datatracker.ietf.org/doc/html/rfc8941
[6]: https://github.com/httpwg/structured-field-tests
[7]: https://www.ietf.org/archive/id/draft-ietf-httpbis-sfbis-02.html
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ too, but plain Javascript is also fully supported.
Compatibility
-------------

This package has 2725 unittests, the vast majority are supplied from the
This package has 2740 unittests, the vast majority are supplied from the
official [HTTP WG test suite][2].

However, there are 2 differences in the serializer:
Expand Down
32 changes: 31 additions & 1 deletion src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,9 @@ export default class Parser {
if (char === '?') {
return this.parseBoolean();
}

if (char === '@') {
return this.parseDate();
}
throw new ParseError(this.pos, 'Unexpected input');

}
Expand Down Expand Up @@ -350,6 +352,34 @@ export default class Parser {

}

private parseDate(): Date {

this.expectChar('@');
this.pos++;
let sign = 1;
let inputNumber = '';
if (this.lookChar()==='-') {
sign = -1;
this.pos++;
}

if (!isDigit(this.lookChar())) {
throw new ParseError(this.pos, 'Expected a digit (0-9)');
}

while(!this.eof()) {
const char = this.getChar();
if (isDigit(char)) {
inputNumber+=char;
} else {
throw new ParseError(this.pos, 'Expected a digit (0-9), whitespace or EOL');
}
}

return new Date(parseInt(inputNumber,10)*sign*1000);

}

private parseKey(): string {

if (!this.lookChar().match(/^[a-z*]/)) {
Expand Down
7 changes: 7 additions & 0 deletions src/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ export function serializeBareItem(input: BareItem): string {
if (input instanceof ByteSequence) {
return serializeByteSequence(input);
}
if (input instanceof Date) {
return serializeDate(input);
}
if (typeof input === 'boolean') {
return serializeBoolean(input);
}
Expand Down Expand Up @@ -123,6 +126,10 @@ export function serializeToken(input: Token): string {
return input.toString();
}

export function serializeDate(input: Date): string {
return '@' + Math.floor(input.getTime()/1000);
}

export function serializeParameters(input: Parameters): string {

return Array.from(input).map(([key, value]) => {
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@ export class ByteSequence {

}

export type BareItem = number | string | Token | ByteSequence | boolean;
export type BareItem = number | string | Token | ByteSequence | Date | boolean;

export type Item = [BareItem, Parameters];
23 changes: 18 additions & 5 deletions test/httpwg-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ describe('HTTP-WG tests', () => {
'number',
'string',
'token',
'date',

'item',

Expand Down Expand Up @@ -270,6 +271,12 @@ function packTestValue(input) {
value: base32Encode(Buffer.from(input.toBase64(), 'base64'), 'RFC4648')
}
}
if (input instanceof Date) {
return {
__type: 'date',
value: Math.floor(input.getTime()/1000)
};
}
if (input instanceof Map) {
return Array.from(input.entries()).map( ([key, value]) => {
return [key, packTestValue(value)];
Expand Down Expand Up @@ -306,11 +313,17 @@ function packTestValue(input) {
*/
function unpackTestValue(input) {

if (input.__type === 'token') {
return new Token(input.value);
}
if (input.__type === 'binary') {
return new ByteSequence(Buffer.from(base32Decode(input.value, 'RFC4648')).toString('base64'));
if (input.__type) {
switch(input.__type) {
case 'token' :
return new Token(input.value);
case 'binary':
return new ByteSequence(Buffer.from(base32Decode(input.value, 'RFC4648')).toString('base64'));
case 'date' :
return new Date(input.value * 1000);
default:
throw new Error('Unknown input __type: ' + input.__type);
}
}
if (Array.isArray(input)) {
return input.map(unpackTestValue);
Expand Down

0 comments on commit 823f522

Please sign in to comment.