Structured Field Values for HTTP parser and serializer
npm install @httpland/sfv-parser






Structured Field Values for HTTP parser and serializer.
Compliant with
RFC 8941, Structured Field Values for HTTP.
Specify field value and field type(List, Dictionary, Item) for parser.
``ts
import { parseSfv } from "https://deno.land/x/sfv_parser@$VERSION/parse.ts";
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
const result = parseSfv("sugar, tea, rum", "List");
assertEquals(result, {
"type": "List",
"value": [
{
"type": "Item",
"value": [
{ "type": "Token", "value": "sugar" },
{ "type": "Parameters", "value": [] },
],
},
{
"type": "Item",
"value": [
{ "type": "Token", "value": "tea" },
{ "type": "Parameters", "value": [] },
],
},
{
"type": "Item",
"value": [
{ "type": "Token", "value": "rum" },
{ "type": "Parameters", "value": [] },
],
},
],
});
`
If field value has an invalid syntax, it may throw a SyntaxError.
`ts
import { parseSfv } from "https://deno.land/x/sfv_parser@$VERSION/parse.ts";
import { assertThrows } from "https://deno.land/std/testing/asserts.ts";
assertThrows(() => parseSfv("this, is, list", "Dictionary"));
`
Serialize Sfv into string.
`ts
import { stringifySfv } from "https://deno.land/x/sfv_parser@$VERSION/mod.ts";
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
const sfv = {
"type": "List",
"value": [
{
"type": "Item",
"value": [
{ "type": "Token", "value": "sugar" },
{ "type": "Parameters", "value": [] },
],
},
{
"type": "Item",
"value": [
{ "type": "Token", "value": "tea" },
{ "type": "Parameters", "value": [] },
],
},
{
"type": "Item",
"value": [
{ "type": "Token", "value": "rum" },
{ "type": "Parameters", "value": [] },
],
},
],
} as const;
assertEquals(stringifySfv(sfv), "sugar, tea, rum");
`
The range of possible values for Integer and Decimal is specified. If thisRangeError
is violated, may be thrown.
For example, the possible values of type Integer range from-999,999,999,999,999 to 999,999,999,999,999.
`ts
import {
Dictionary,
Integer,
Item,
Parameters,
stringifySfv,
} from "https://deno.land/x/sfv_parser@$VERSION/mod.ts";
import { assertThrows } from "https://deno.land/std/testing/asserts.ts";
const sfv = new Dictionary({
foo: new Item(
[new Integer(10e14), new Parameters()],
),
});
assertThrows(() => stringifySfv(sfv));
`
If Data type contains invalid characters, it may throw TypeError.
For example, Token must be compliant with.
`ts
import {
Item,
List,
Parameters,
stringifySfv,
Token,
} from "https://deno.land/x/sfv_parser@$VERSION/mod.ts";
import { assertThrows } from "https://deno.land/std/testing/asserts.ts";
const sfv = new List([
new Item(
[new Token("
),
]);
assertThrows(() => stringifySfv(sfv));
`
Provides a utility of type constructor for
structured data types.
This saves you the trouble of typing the type field.
Representation of
Rfc 8941, 3.1. Lists.
`ts
import {
type InnerList,
type Item,
List,
} from "https://deno.land/x/sfv_parser@$VERSION/types.ts";
declare const input: (Item | InnerList)[];
const list = new List(input);
`
yield:
`json`
{
"type": "List",
"value": "
}
Representation of
Rfc 8941, 3.1.1. Inner Lists.
`ts
import {
InnerList,
type Item,
type Parameters,
} from "https://deno.land/x/sfv_parser@$VERSION/types.ts";
declare const items: Item[];
declare const parameters: Parameters;
const innerList = new InnerList([items, parameters]);
`
or,
`ts
import {
InnerList,
type Item,
} from "https://deno.land/x/sfv_parser@$VERSION/types.ts";
declare const items: Item[];
const innerList = new InnerList(items);
`
yield:
`json`
{
"type": "InnerList",
"value": ["
}
Representation of
Rfc 8941, 3.1.2. Parameters.
`ts
import {
type BareItem,
Parameters,
} from "https://deno.land/x/sfv_parser@$VERSION/types.ts";
declare const bareItem: BareItem;
const parameters = new Parameters({
"
});
`
or,
`ts
import {
type BareItem,
Parameters,
} from "https://deno.land/x/sfv_parser@$VERSION/types.ts";
declare const bareItem: BareItem;
const parameters = new Parameters([
["
]);
`
yield:
`json`
{
"type": "Boolean",
"value": "[string,
}
Representation of
Rfc 8941, 3.2. Dictionaries.
`ts
import {
Dictionary,
type InnerList,
type Item,
} from "https://deno.land/x/sfv_parser@$VERSION/types.ts";
declare const input: Item | InnerList;
const dictionary = new Dictionary({ "
`
or,
`ts
import {
Dictionary,
type InnerList,
type Item,
} from "https://deno.land/x/sfv_parser@$VERSION/types.ts";
declare const input: Item | InnerList;
const dictionary = new Dictionary([
["
]);
`
yield:
`json`
{
"type": "Dictionary",
"value": "[string,
}
Representation of
Rfc 8941, 3.3. Items.
`ts
import {
type BareItem,
Item,
type Parameters,
} from "https://deno.land/x/sfv_parser@$VERSION/types.ts";
declare const bareItem: BareItem;
declare const parameters: Parameters;
const item = new Item([bareItem, parameters]);
`
or,
`ts
import {
type BareItem,
Item,
} from "https://deno.land/x/sfv_parser@$VERSION/types.ts";
declare const bareItem: BareItem;
const item = new Item(bareItem);
`
yield:
`json`
{
"type": "Item",
"value": ["
}
Representation of
RFC 8941, 3.3.1. Integers.
`ts
import { Integer } from "https://deno.land/x/sfv_parser@$VERSION/types.ts";
declare const input: number;
const integer = new Integer(input);
`
yield:
`json`
{
"type": "Integer",
"value": "
}
Representation of
Rfc 8941, 3.3.2. Decimals.
`ts
import { Decimal } from "https://deno.land/x/sfv_parser@$VERSION/types.ts";
declare const input: number;
const decimal = new Decimal(input);
`
yield:
`json`
{
"type": "Decimal",
"value": "
}
Representation of
Rfc 8941, 3.3.3. Strings.
`ts
import { String } from "https://deno.land/x/sfv_parser@$VERSION/types.ts";
declare const input: string;
const string = new String(input);
`
yield:
`json`
{
"type": "String",
"value": "
}
Representation of
RFC 8941, 3.3.4. Tokens.
`ts
import { Token } from "https://deno.land/x/sfv_parser@$VERSION/types.ts";
declare const input: string;
const token = new Token(input);
`
yield:
`json`
{
"type": "Token",
"value": "
}
Representation of
Rfc 8941,3.3.5. Byte Sequences.
`ts
import { Binary } from "https://deno.land/x/sfv_parser@$VERSION/types.ts";
declare const input: Uint8Array;
const binary = new Binary(input);
`
yield:
`json`
{
"type": "Binary",
"value": "
}
Representation of
Rfc 8941, 3.3.6. Booleans.
`ts
import { Boolean } from "https://deno.land/x/sfv_parser@$VERSION/types.ts";
declare const input: boolean;
const boolean = new Boolean(input);
`
yield:
`json``
{
"type": "Boolean",
"value": "
}
All test cases in
structured-field-tests have
been passed.
All APIs can be found in the
deno doc.
Copyright © 2023-present httpland.
Released under the MIT license