@systemfsoftware/hex-schema 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/dist/hex-schema.d.ts +32 -0
- package/dist/index.d.mts +33 -0
- package/dist/index.d.ts +33 -0
- package/dist/index.mjs +70 -0
- package/package.json +64 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Ryan Lee
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { Schema } from 'effect';
|
|
2
|
+
|
|
3
|
+
export declare const ColonHex: Schema.brand<Schema.transform<Schema.transform<Schema.refine<string, typeof Schema.String>, Schema.SchemaClass<string, string, never>>, Schema.brand<Schema.transform<Schema.refine<string, typeof Schema.String>, Schema.refine<string, typeof Schema.String>>, "HexString">>, "ColonHex">;
|
|
4
|
+
|
|
5
|
+
export declare type ColonHex = Schema.Schema.Type<typeof ColonHex>;
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Composable hex-to-bytes schema. Compose any branded hex schema with
|
|
9
|
+
* this to produce a `Uint8Array` from that format:
|
|
10
|
+
*
|
|
11
|
+
* ```ts
|
|
12
|
+
* S.compose(PrefixedHex, HexBytes) // Schema<Uint8Array, string>
|
|
13
|
+
* S.compose(ColonHex, HexBytes) // Schema<Uint8Array, string>
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
export declare const HexBytes: Schema.Schema<Uint8Array<ArrayBufferLike>, string, never>;
|
|
17
|
+
|
|
18
|
+
export declare const HexString: Schema.brand<Schema.transform<Schema.refine<string, typeof Schema.String>, Schema.refine<string, typeof Schema.String>>, "HexString">;
|
|
19
|
+
|
|
20
|
+
export declare type HexString = Schema.Schema.Type<typeof HexString>;
|
|
21
|
+
|
|
22
|
+
export declare const PrefixedHex: Schema.brand<Schema.transform<Schema.refine<string, typeof Schema.String>, Schema.refine<string, typeof Schema.String>>, "PrefixedHex">;
|
|
23
|
+
|
|
24
|
+
export declare type PrefixedHex = Schema.Schema.Type<typeof PrefixedHex>;
|
|
25
|
+
|
|
26
|
+
export declare const StrictHex: Schema.refine<string, typeof Schema.String>;
|
|
27
|
+
|
|
28
|
+
export declare const Uint8ArrayFromPrefixedHex: Schema.transform<Schema.refine<string, typeof Schema.String>, Schema.Schema<Uint8Array<ArrayBufferLike>, string, never>>;
|
|
29
|
+
|
|
30
|
+
export declare type Uint8ArrayFromPrefixedHex = Schema.Schema.Type<typeof Uint8ArrayFromPrefixedHex>;
|
|
31
|
+
|
|
32
|
+
export { }
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
//#region src/colon-hex.schema.d.ts
|
|
3
|
+
declare const ColonHex: Schema.brand<Schema.transform<Schema.transform<Schema.refine<string, typeof Schema.String>, Schema.SchemaClass<string, string, never>>, Schema.brand<Schema.transform<Schema.refine<string, typeof Schema.String>, Schema.refine<string, typeof Schema.String>>, "HexString">>, "ColonHex">;
|
|
4
|
+
type ColonHex = Schema.Schema.Type<typeof ColonHex>;
|
|
5
|
+
//#endregion
|
|
6
|
+
//#region src/hex-bytes.schema.d.ts
|
|
7
|
+
/**
|
|
8
|
+
* Composable hex-to-bytes schema. Compose any branded hex schema with
|
|
9
|
+
* this to produce a `Uint8Array` from that format:
|
|
10
|
+
*
|
|
11
|
+
* ```ts
|
|
12
|
+
* S.compose(PrefixedHex, HexBytes) // Schema<Uint8Array, string>
|
|
13
|
+
* S.compose(ColonHex, HexBytes) // Schema<Uint8Array, string>
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
declare const HexBytes: Schema.Schema<Uint8Array<ArrayBufferLike>, string, never>;
|
|
17
|
+
//#endregion
|
|
18
|
+
//#region src/hex-string.schema.d.ts
|
|
19
|
+
declare const HexString: Schema.brand<Schema.transform<Schema.refine<string, typeof Schema.String>, Schema.refine<string, typeof Schema.String>>, "HexString">;
|
|
20
|
+
type HexString = Schema.Schema.Type<typeof HexString>;
|
|
21
|
+
//#endregion
|
|
22
|
+
//#region src/prefixed-hex.schema.d.ts
|
|
23
|
+
declare const PrefixedHex: Schema.brand<Schema.transform<Schema.brand<Schema.transform<Schema.refine<string, typeof Schema.String>, Schema.refine<string, typeof Schema.String>>, "HexString">, Schema.refine<string, typeof Schema.String>>, "PrefixedHex">;
|
|
24
|
+
type PrefixedHex = Schema.Schema.Type<typeof PrefixedHex>;
|
|
25
|
+
//#endregion
|
|
26
|
+
//#region src/strict-hex.schema.d.ts
|
|
27
|
+
declare const StrictHex: Schema.refine<string, typeof Schema.String>;
|
|
28
|
+
//#endregion
|
|
29
|
+
//#region src/uint8array-from-prefixed-hex.schema.d.ts
|
|
30
|
+
declare const Uint8ArrayFromPrefixedHex: Schema.transform<Schema.brand<Schema.transform<Schema.brand<Schema.transform<Schema.refine<string, typeof Schema.String>, Schema.refine<string, typeof Schema.String>>, "HexString">, Schema.refine<string, typeof Schema.String>>, "PrefixedHex">, Schema.Schema<Uint8Array<ArrayBufferLike>, string, never>>;
|
|
31
|
+
type Uint8ArrayFromPrefixedHex = Schema.Schema.Type<typeof Uint8ArrayFromPrefixedHex>;
|
|
32
|
+
//#endregion
|
|
33
|
+
export { ColonHex, HexBytes, HexString, PrefixedHex, StrictHex, Uint8ArrayFromPrefixedHex };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
//#region src/colon-hex.schema.d.ts
|
|
3
|
+
declare const ColonHex: Schema.brand<Schema.transform<Schema.transform<Schema.refine<string, typeof Schema.String>, Schema.SchemaClass<string, string, never>>, Schema.brand<Schema.transform<Schema.refine<string, typeof Schema.String>, Schema.refine<string, typeof Schema.String>>, "HexString">>, "ColonHex">;
|
|
4
|
+
type ColonHex = Schema.Schema.Type<typeof ColonHex>;
|
|
5
|
+
//#endregion
|
|
6
|
+
//#region src/hex-bytes.schema.d.ts
|
|
7
|
+
/**
|
|
8
|
+
* Composable hex-to-bytes schema. Compose any branded hex schema with
|
|
9
|
+
* this to produce a `Uint8Array` from that format:
|
|
10
|
+
*
|
|
11
|
+
* ```ts
|
|
12
|
+
* S.compose(PrefixedHex, HexBytes) // Schema<Uint8Array, string>
|
|
13
|
+
* S.compose(ColonHex, HexBytes) // Schema<Uint8Array, string>
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
declare const HexBytes: Schema.Schema<Uint8Array<ArrayBufferLike>, string, never>;
|
|
17
|
+
//#endregion
|
|
18
|
+
//#region src/hex-string.schema.d.ts
|
|
19
|
+
declare const HexString: Schema.brand<Schema.transform<Schema.refine<string, typeof Schema.String>, Schema.refine<string, typeof Schema.String>>, "HexString">;
|
|
20
|
+
type HexString = Schema.Schema.Type<typeof HexString>;
|
|
21
|
+
//#endregion
|
|
22
|
+
//#region src/prefixed-hex.schema.d.ts
|
|
23
|
+
declare const PrefixedHex: Schema.brand<Schema.transform<Schema.refine<string, typeof Schema.String>, Schema.refine<string, typeof Schema.String>>, "PrefixedHex">;
|
|
24
|
+
type PrefixedHex = Schema.Schema.Type<typeof PrefixedHex>;
|
|
25
|
+
//#endregion
|
|
26
|
+
//#region src/strict-hex.schema.d.ts
|
|
27
|
+
declare const StrictHex: Schema.refine<string, typeof Schema.String>;
|
|
28
|
+
//#endregion
|
|
29
|
+
//#region src/uint8array-from-prefixed-hex.schema.d.ts
|
|
30
|
+
declare const Uint8ArrayFromPrefixedHex: Schema.transform<Schema.refine<string, typeof Schema.String>, Schema.Schema<Uint8Array<ArrayBufferLike>, string, never>>;
|
|
31
|
+
type Uint8ArrayFromPrefixedHex = Schema.Schema.Type<typeof Uint8ArrayFromPrefixedHex>;
|
|
32
|
+
//#endregion
|
|
33
|
+
export { ColonHex, HexBytes, HexString, PrefixedHex, StrictHex, Uint8ArrayFromPrefixedHex };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { Encoding, Schema, pipe } from "effect";
|
|
2
|
+
//#region src/strict-hex.schema.ts
|
|
3
|
+
const StrictHex = Schema.String.pipe(Schema.pattern(/^[0-9a-f]*$/), Schema.annotations({
|
|
4
|
+
arbitrary: () => (fc) => fc.hexaString(),
|
|
5
|
+
identifier: "StrictHex",
|
|
6
|
+
description: "A lowercase hexadecimal string with no prefix",
|
|
7
|
+
title: "Strict Hex String"
|
|
8
|
+
}));
|
|
9
|
+
//#endregion
|
|
10
|
+
//#region src/hex-string.schema.ts
|
|
11
|
+
const HexString = pipe(Schema.transform(Schema.String.pipe(Schema.pattern(/^(0x)?[0-9a-fA-F]*$/), Schema.annotations({ arbitrary: () => (fc) => fc.stringMatching(/^(0x)?[0-9a-fA-F]*$/) })), StrictHex, {
|
|
12
|
+
decode: (hex) => (hex.startsWith("0x") ? hex.slice(2) : hex).toLowerCase(),
|
|
13
|
+
encode: (s) => s
|
|
14
|
+
}), Schema.annotations({
|
|
15
|
+
identifier: "HexString",
|
|
16
|
+
description: "A string representing hexadecimal data, with or without the 0x prefix",
|
|
17
|
+
title: "Hex String"
|
|
18
|
+
}), Schema.brand("HexString"));
|
|
19
|
+
//#endregion
|
|
20
|
+
//#region src/colon-hex.schema.ts
|
|
21
|
+
const hexToColon = (hex) => (hex.match(/.{1,2}/g) ?? []).map((byte) => byte.toUpperCase()).join(":");
|
|
22
|
+
const ColonHex = pipe(Schema.compose(Schema.transform(Schema.String.pipe(Schema.pattern(/^([0-9A-Fa-f]{1,2}(:[0-9A-Fa-f]{1,2})*)?$/), Schema.annotations({ arbitrary: () => (fc) => fc.hexaString().map(hexToColon) })), Schema.encodedSchema(HexString), {
|
|
23
|
+
strict: true,
|
|
24
|
+
decode: (colon) => colon.replaceAll(":", ""),
|
|
25
|
+
encode: hexToColon
|
|
26
|
+
}), HexString), Schema.annotations({
|
|
27
|
+
identifier: "ColonHex",
|
|
28
|
+
description: "Colon-separated uppercase hex bytes — the fingerprint format",
|
|
29
|
+
title: "Colon-Separated Hex String"
|
|
30
|
+
}), Schema.brand("ColonHex"));
|
|
31
|
+
//#endregion
|
|
32
|
+
//#region src/hex-bytes.schema.ts
|
|
33
|
+
/**
|
|
34
|
+
* Composable hex-to-bytes schema. Compose any branded hex schema with
|
|
35
|
+
* this to produce a `Uint8Array` from that format:
|
|
36
|
+
*
|
|
37
|
+
* ```ts
|
|
38
|
+
* S.compose(PrefixedHex, HexBytes) // Schema<Uint8Array, string>
|
|
39
|
+
* S.compose(ColonHex, HexBytes) // Schema<Uint8Array, string>
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
const HexBytes = Schema.Uint8ArrayFromHex.pipe(Schema.annotations({
|
|
43
|
+
identifier: "HexBytes",
|
|
44
|
+
description: "Uint8Array encoded as a lowercase hex string — compose with any hex schema",
|
|
45
|
+
title: "Hex Bytes"
|
|
46
|
+
}));
|
|
47
|
+
//#endregion
|
|
48
|
+
//#region src/prefixed-hex.schema.ts
|
|
49
|
+
const PrefixedHex = pipe(Schema.transform(Schema.String.pipe(Schema.pattern(/^0x[0-9a-f]*$/), Schema.annotations({ arbitrary: () => (fc) => fc.hexaString().map((hex) => `0x${hex}`) })), StrictHex, {
|
|
50
|
+
strict: true,
|
|
51
|
+
decode: (fromA) => fromA.slice(2),
|
|
52
|
+
encode: (toI) => `0x${toI}`
|
|
53
|
+
}), Schema.annotations({
|
|
54
|
+
identifier: "PrefixedHex",
|
|
55
|
+
description: "A 0x-prefixed hex string on the wire — decodes to a plain lowercase hex string",
|
|
56
|
+
title: "Prefixed Hex String"
|
|
57
|
+
}), Schema.brand("PrefixedHex"));
|
|
58
|
+
//#endregion
|
|
59
|
+
//#region src/uint8array-from-prefixed-hex.schema.ts
|
|
60
|
+
const Uint8ArrayFromPrefixedHex = pipe(Schema.transform(Schema.String.pipe(Schema.pattern(/^0x(?:[0-9a-f]{2})*$/), Schema.annotations({ arbitrary: () => (fc) => fc.uint8Array().map((bytes) => `0x${Encoding.encodeHex(bytes)}`) })), HexBytes, {
|
|
61
|
+
strict: true,
|
|
62
|
+
decode: (prefixed) => prefixed.slice(2),
|
|
63
|
+
encode: (hex) => `0x${hex}`
|
|
64
|
+
}), Schema.annotations({
|
|
65
|
+
identifier: "Uint8ArrayFromPrefixedHex",
|
|
66
|
+
description: "Uint8Array encoded as a 0x-prefixed hex string",
|
|
67
|
+
title: "Uint8Array from Prefixed Hex"
|
|
68
|
+
}));
|
|
69
|
+
//#endregion
|
|
70
|
+
export { ColonHex, HexBytes, HexString, PrefixedHex, StrictHex, Uint8ArrayFromPrefixedHex };
|
package/package.json
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@systemfsoftware/hex-schema",
|
|
3
|
+
"license": "MIT",
|
|
4
|
+
"version": "0.1.0",
|
|
5
|
+
"author": "Ryan Lee <drdgvhbh@gmail.com>",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/systemfsoftware/systemfsoftware.git",
|
|
9
|
+
"directory": "packages/hex-schema"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/systemfsoftware/systemfsoftware/tree/main/packages/hex-schema#readme",
|
|
12
|
+
"bugs": "https://github.com/systemfsoftware/systemfsoftware/issues",
|
|
13
|
+
"description": "Effect Schema definitions for hexadecimal wire formats — strict, colon-delimited, prefixed, and byte-level schemas with branded types.",
|
|
14
|
+
"keywords": [
|
|
15
|
+
"effect",
|
|
16
|
+
"effect-ts",
|
|
17
|
+
"schema",
|
|
18
|
+
"codec",
|
|
19
|
+
"hex",
|
|
20
|
+
"hexstring",
|
|
21
|
+
"branded-types",
|
|
22
|
+
"typescript"
|
|
23
|
+
],
|
|
24
|
+
"type": "module",
|
|
25
|
+
"exports": {
|
|
26
|
+
".": {
|
|
27
|
+
"types": "./dist/hex-schema.d.ts",
|
|
28
|
+
"default": "./dist/index.mjs"
|
|
29
|
+
},
|
|
30
|
+
"./package.json": "./package.json"
|
|
31
|
+
},
|
|
32
|
+
"files": [
|
|
33
|
+
"dist"
|
|
34
|
+
],
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"effect": "^3.22.0"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@effect/vitest": "0.30.0",
|
|
40
|
+
"@microsoft/api-extractor": "^7.58.7",
|
|
41
|
+
"@types/node": "^24",
|
|
42
|
+
"fast-check": "^3",
|
|
43
|
+
"rimraf": "^6.1.3",
|
|
44
|
+
"tsdown": "^0.22.9",
|
|
45
|
+
"vitest": "^4",
|
|
46
|
+
"@systemfsoftware/effect-schema-law": "^0.1.0",
|
|
47
|
+
"@systemfsoftware/effect-schema-vite": "^0.1.0",
|
|
48
|
+
"@systemfsoftware/tsconfig": "^1.0.0",
|
|
49
|
+
"@systemfsoftware/oxlint-config": "^0.1.0",
|
|
50
|
+
"@systemfsoftware/vitest-config": "^0.1.0"
|
|
51
|
+
},
|
|
52
|
+
"peerDependencies": {
|
|
53
|
+
"effect": "*"
|
|
54
|
+
},
|
|
55
|
+
"scripts": {
|
|
56
|
+
"clean": "rimraf dist",
|
|
57
|
+
"build": "tsdown",
|
|
58
|
+
"typecheck": "tsc --noEmit --incremental",
|
|
59
|
+
"test": "vitest run",
|
|
60
|
+
"api:check": "api-extractor run",
|
|
61
|
+
"api:update": "api-extractor run --local",
|
|
62
|
+
"lint": "oxlint . ${AGENT:+--format=unix --quiet}"
|
|
63
|
+
}
|
|
64
|
+
}
|