@flametrench/ids 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 +201 -0
- package/README.md +81 -0
- package/dist/index.d.ts +101 -0
- package/dist/index.js +107 -0
- package/dist/index.js.map +1 -0
- package/package.json +59 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for describing the origin of the Work and
|
|
141
|
+
reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may accept and charge a
|
|
167
|
+
fee for, acceptance of support, warranty, indemnity, or other
|
|
168
|
+
liability obligations and/or rights consistent with this License.
|
|
169
|
+
However, in accepting such obligations, You may act only on Your
|
|
170
|
+
own behalf and on Your sole responsibility, not on behalf of any
|
|
171
|
+
other Contributor, and only if You agree to indemnify, defend,
|
|
172
|
+
and hold each Contributor harmless for any liability incurred by,
|
|
173
|
+
or claims asserted against, such Contributor by reason of your
|
|
174
|
+
accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 NDC Digital, LLC
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# @flametrench/ids
|
|
2
|
+
|
|
3
|
+
Prefixed wire-format identifiers for [Flametrench](https://github.com/flametrench/spec).
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import { generate, encode, decode, isValid } from "@flametrench/ids";
|
|
7
|
+
|
|
8
|
+
generate("usr");
|
|
9
|
+
// → "usr_0190f2a81b3c7abc8123456789abcdef"
|
|
10
|
+
|
|
11
|
+
encode("org", "0190f2a8-1b3c-7abc-8123-456789abcdef");
|
|
12
|
+
// → "org_0190f2a81b3c7abc8123456789abcdef"
|
|
13
|
+
|
|
14
|
+
decode("usr_0190f2a81b3c7abc8123456789abcdef");
|
|
15
|
+
// → { type: "usr", uuid: "0190f2a8-1b3c-7abc-8123-456789abcdef" }
|
|
16
|
+
|
|
17
|
+
isValid("usr_0190f2a81b3c7abc8123456789abcdef"); // true
|
|
18
|
+
isValid("usr_0190f2a81b3c7abc8123456789abcdef", "org"); // false
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Why prefixed IDs
|
|
22
|
+
|
|
23
|
+
Flametrench uses UUIDv7 in storage and prefixed strings on the wire. The wire format is self-describing: `usr_...` is a user, `org_...` is an organization, `ses_...` is a session. This is the Stripe playbook, and it pays off in every log line, support ticket, and debugger session.
|
|
24
|
+
|
|
25
|
+
The specification details live at [flametrench/spec/docs/ids.md](https://github.com/flametrench/spec/blob/main/docs/ids.md). The Laravel SDK exposes the same API shape in PHP: [flametrench/ids](https://packagist.org/packages/flametrench/ids).
|
|
26
|
+
|
|
27
|
+
## Install
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
pnpm add @flametrench/ids
|
|
31
|
+
# or
|
|
32
|
+
npm install @flametrench/ids
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Requires Node 20 or newer. ESM-only (no CommonJS build).
|
|
36
|
+
|
|
37
|
+
## Registered type prefixes
|
|
38
|
+
|
|
39
|
+
The v0.1 specification reserves the following prefixes:
|
|
40
|
+
|
|
41
|
+
| Prefix | Resource |
|
|
42
|
+
| ------ | ------------------- |
|
|
43
|
+
| `usr` | User |
|
|
44
|
+
| `org` | Organization |
|
|
45
|
+
| `mem` | Membership |
|
|
46
|
+
| `inv` | Invitation |
|
|
47
|
+
| `ses` | Session |
|
|
48
|
+
| `cred` | Credential |
|
|
49
|
+
| `tup` | Authorization tuple |
|
|
50
|
+
|
|
51
|
+
Implementations must not invent prefixes outside the specification. New prefixes are added through the specification's RFC process.
|
|
52
|
+
|
|
53
|
+
## Type narrowing at API boundaries
|
|
54
|
+
|
|
55
|
+
The `isId` type guard narrows `unknown` to `string` when a value is a valid Flametrench identifier. Useful in route handlers, webhook processors, and any boundary where incoming values need validation before use.
|
|
56
|
+
|
|
57
|
+
```ts
|
|
58
|
+
import { isId } from "@flametrench/ids";
|
|
59
|
+
|
|
60
|
+
export async function loadUser(raw: unknown) {
|
|
61
|
+
if (!isId(raw, "usr")) {
|
|
62
|
+
throw new Response("Invalid user id", { status: 400 });
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// `raw` is narrowed to `string` here, and is guaranteed to be a user id.
|
|
66
|
+
return fetchUserById(raw);
|
|
67
|
+
}
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## Testing
|
|
71
|
+
|
|
72
|
+
```bash
|
|
73
|
+
pnpm install
|
|
74
|
+
pnpm test
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
The test suite uses Vitest and covers encoding, decoding, validation, generation, round-trip properties, sortability, and cross-language parity with the PHP SDK.
|
|
78
|
+
|
|
79
|
+
## License
|
|
80
|
+
|
|
81
|
+
Apache License 2.0. Copyright 2026 NDC Digital, LLC.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Thrown when an ID string is malformed or cannot be parsed as a
|
|
3
|
+
* Flametrench wire-format identifier.
|
|
4
|
+
*/
|
|
5
|
+
declare class InvalidIdError extends Error {
|
|
6
|
+
constructor(message: string);
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Thrown when an ID uses a type prefix that is not in the registered
|
|
10
|
+
* type prefix set.
|
|
11
|
+
*/
|
|
12
|
+
declare class InvalidTypeError extends Error {
|
|
13
|
+
constructor(message: string);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Registered type prefixes for Flametrench v0.1.
|
|
18
|
+
*
|
|
19
|
+
* Keep this map synchronized with the Flametrench specification's
|
|
20
|
+
* reserved prefix registry at:
|
|
21
|
+
* https://github.com/flametrench/spec/blob/main/docs/ids.md
|
|
22
|
+
*
|
|
23
|
+
* Parallel implementations (Laravel, future SDKs) must use the same
|
|
24
|
+
* prefixes and semantics.
|
|
25
|
+
*/
|
|
26
|
+
declare const TYPES: {
|
|
27
|
+
readonly usr: "user";
|
|
28
|
+
readonly org: "organization";
|
|
29
|
+
readonly mem: "membership";
|
|
30
|
+
readonly inv: "invitation";
|
|
31
|
+
readonly ses: "session";
|
|
32
|
+
readonly cred: "credential";
|
|
33
|
+
readonly tup: "authorization_tuple";
|
|
34
|
+
};
|
|
35
|
+
type IdType = keyof typeof TYPES;
|
|
36
|
+
/**
|
|
37
|
+
* The shape returned by {@link decode}.
|
|
38
|
+
*/
|
|
39
|
+
interface DecodedId {
|
|
40
|
+
type: IdType;
|
|
41
|
+
uuid: string;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Encode a type and UUID into Flametrench wire format.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* encode("usr", "0190f2a8-1b3c-7abc-8123-456789abcdef")
|
|
48
|
+
* // → "usr_0190f2a81b3c7abc8123456789abcdef"
|
|
49
|
+
*
|
|
50
|
+
* @throws {InvalidTypeError} If the type prefix is not registered.
|
|
51
|
+
* @throws {InvalidIdError} If the UUID is not a valid UUID.
|
|
52
|
+
*/
|
|
53
|
+
declare function encode(type: string, uuid: string): string;
|
|
54
|
+
/**
|
|
55
|
+
* Decode a Flametrench wire-format ID into its type and canonical UUID.
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* decode("usr_0190f2a81b3c7abc8123456789abcdef")
|
|
59
|
+
* // → { type: "usr", uuid: "0190f2a8-1b3c-7abc-8123-456789abcdef" }
|
|
60
|
+
*
|
|
61
|
+
* @throws {InvalidIdError} If the ID is malformed.
|
|
62
|
+
* @throws {InvalidTypeError} If the type prefix is not registered.
|
|
63
|
+
*/
|
|
64
|
+
declare function decode(id: string): DecodedId;
|
|
65
|
+
/**
|
|
66
|
+
* Check whether a string is a valid Flametrench wire-format ID.
|
|
67
|
+
*
|
|
68
|
+
* Optionally asserts that the ID is of a specific type.
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* isValid("usr_0190f2a81b3c7abc8123456789abcdef") // true
|
|
72
|
+
* isValid("usr_0190f2a81b3c7abc8123456789abcdef", "org") // false
|
|
73
|
+
*/
|
|
74
|
+
declare function isValid(id: string, expectedType?: IdType): boolean;
|
|
75
|
+
/**
|
|
76
|
+
* Extract the type prefix from a wire-format ID.
|
|
77
|
+
*
|
|
78
|
+
* @throws {InvalidIdError} If the ID is malformed.
|
|
79
|
+
* @throws {InvalidTypeError} If the type prefix is not registered.
|
|
80
|
+
*/
|
|
81
|
+
declare function typeOf(id: string): IdType;
|
|
82
|
+
/**
|
|
83
|
+
* Generate a fresh wire-format ID of the given type.
|
|
84
|
+
*
|
|
85
|
+
* Uses UUIDv7 so generated IDs are sortable by creation time.
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* generate("usr")
|
|
89
|
+
* // → "usr_0190f2a81b3c7abc8123456789abcdef"
|
|
90
|
+
*
|
|
91
|
+
* @throws {InvalidTypeError} If the type prefix is not registered.
|
|
92
|
+
*/
|
|
93
|
+
declare function generate(type: IdType): string;
|
|
94
|
+
/**
|
|
95
|
+
* Type guard narrowing `unknown` to a valid Flametrench ID string.
|
|
96
|
+
*
|
|
97
|
+
* Useful at API boundaries where incoming values need validation.
|
|
98
|
+
*/
|
|
99
|
+
declare function isId(value: unknown, expectedType?: IdType): value is string;
|
|
100
|
+
|
|
101
|
+
export { type DecodedId, type IdType, InvalidIdError, InvalidTypeError, TYPES, decode, encode, generate, isId, isValid, typeOf };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { v7 as uuidv7, validate as uuidValidate } from "uuid";
|
|
3
|
+
|
|
4
|
+
// src/errors.ts
|
|
5
|
+
var InvalidIdError = class extends Error {
|
|
6
|
+
constructor(message) {
|
|
7
|
+
super(message);
|
|
8
|
+
this.name = "InvalidIdError";
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
var InvalidTypeError = class extends Error {
|
|
12
|
+
constructor(message) {
|
|
13
|
+
super(message);
|
|
14
|
+
this.name = "InvalidTypeError";
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
// src/index.ts
|
|
19
|
+
var TYPES = {
|
|
20
|
+
usr: "user",
|
|
21
|
+
org: "organization",
|
|
22
|
+
mem: "membership",
|
|
23
|
+
inv: "invitation",
|
|
24
|
+
ses: "session",
|
|
25
|
+
cred: "credential",
|
|
26
|
+
tup: "authorization_tuple"
|
|
27
|
+
};
|
|
28
|
+
var HEX_PAYLOAD_LENGTH = 32;
|
|
29
|
+
var HEX_PATTERN = /^[0-9a-f]{32}$/;
|
|
30
|
+
var VERSION_NIBBLE_PATTERN = /^[1-8]$/;
|
|
31
|
+
function encode(type, uuid) {
|
|
32
|
+
assertType(type);
|
|
33
|
+
if (!uuidValidate(uuid)) {
|
|
34
|
+
throw new InvalidIdError(`Value is not a valid UUID: ${uuid}`);
|
|
35
|
+
}
|
|
36
|
+
const hex = uuid.replaceAll("-", "").toLowerCase();
|
|
37
|
+
return `${type}_${hex}`;
|
|
38
|
+
}
|
|
39
|
+
function decode(id) {
|
|
40
|
+
const separator = id.indexOf("_");
|
|
41
|
+
if (separator === -1) {
|
|
42
|
+
throw new InvalidIdError(`ID missing type separator: ${id}`);
|
|
43
|
+
}
|
|
44
|
+
const type = id.slice(0, separator);
|
|
45
|
+
const hex = id.slice(separator + 1);
|
|
46
|
+
assertType(type);
|
|
47
|
+
if (hex.length !== HEX_PAYLOAD_LENGTH || !HEX_PATTERN.test(hex)) {
|
|
48
|
+
throw new InvalidIdError(
|
|
49
|
+
`ID payload is not 32 lowercase hex characters: ${id}`
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
if (!VERSION_NIBBLE_PATTERN.test(hex[12])) {
|
|
53
|
+
throw new InvalidIdError(`ID payload is not a valid UUID: ${id}`);
|
|
54
|
+
}
|
|
55
|
+
const canonical = [
|
|
56
|
+
hex.slice(0, 8),
|
|
57
|
+
hex.slice(8, 12),
|
|
58
|
+
hex.slice(12, 16),
|
|
59
|
+
hex.slice(16, 20),
|
|
60
|
+
hex.slice(20, 32)
|
|
61
|
+
].join("-");
|
|
62
|
+
return { type, uuid: canonical };
|
|
63
|
+
}
|
|
64
|
+
function isValid(id, expectedType) {
|
|
65
|
+
try {
|
|
66
|
+
const decoded = decode(id);
|
|
67
|
+
if (expectedType !== void 0 && decoded.type !== expectedType) {
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
return true;
|
|
71
|
+
} catch (error) {
|
|
72
|
+
if (error instanceof InvalidIdError || error instanceof InvalidTypeError) {
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
throw error;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
function typeOf(id) {
|
|
79
|
+
return decode(id).type;
|
|
80
|
+
}
|
|
81
|
+
function generate(type) {
|
|
82
|
+
assertType(type);
|
|
83
|
+
return encode(type, uuidv7());
|
|
84
|
+
}
|
|
85
|
+
function isId(value, expectedType) {
|
|
86
|
+
return typeof value === "string" && isValid(value, expectedType);
|
|
87
|
+
}
|
|
88
|
+
function assertType(type) {
|
|
89
|
+
if (!Object.hasOwn(TYPES, type)) {
|
|
90
|
+
const registered = Object.keys(TYPES).join(", ");
|
|
91
|
+
throw new InvalidTypeError(
|
|
92
|
+
`Unregistered type prefix: '${type}'. Registered prefixes: ${registered}.`
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
export {
|
|
97
|
+
InvalidIdError,
|
|
98
|
+
InvalidTypeError,
|
|
99
|
+
TYPES,
|
|
100
|
+
decode,
|
|
101
|
+
encode,
|
|
102
|
+
generate,
|
|
103
|
+
isId,
|
|
104
|
+
isValid,
|
|
105
|
+
typeOf
|
|
106
|
+
};
|
|
107
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/errors.ts"],"sourcesContent":["// Copyright 2026 NDC Digital, LLC\n// SPDX-License-Identifier: Apache-2.0\n\nimport { v7 as uuidv7, validate as uuidValidate } from \"uuid\";\nimport { InvalidIdError, InvalidTypeError } from \"./errors.js\";\n\nexport { InvalidIdError, InvalidTypeError };\n\n/**\n * Registered type prefixes for Flametrench v0.1.\n *\n * Keep this map synchronized with the Flametrench specification's\n * reserved prefix registry at:\n * https://github.com/flametrench/spec/blob/main/docs/ids.md\n *\n * Parallel implementations (Laravel, future SDKs) must use the same\n * prefixes and semantics.\n */\nexport const TYPES = {\n usr: \"user\",\n org: \"organization\",\n mem: \"membership\",\n inv: \"invitation\",\n ses: \"session\",\n cred: \"credential\",\n tup: \"authorization_tuple\",\n} as const satisfies Record<string, string>;\n\nexport type IdType = keyof typeof TYPES;\n\n/**\n * The shape returned by {@link decode}.\n */\nexport interface DecodedId {\n type: IdType;\n uuid: string;\n}\n\nconst HEX_PAYLOAD_LENGTH = 32;\nconst HEX_PATTERN = /^[0-9a-f]{32}$/;\nconst VERSION_NIBBLE_PATTERN = /^[1-8]$/;\n\n/**\n * Encode a type and UUID into Flametrench wire format.\n *\n * @example\n * encode(\"usr\", \"0190f2a8-1b3c-7abc-8123-456789abcdef\")\n * // → \"usr_0190f2a81b3c7abc8123456789abcdef\"\n *\n * @throws {InvalidTypeError} If the type prefix is not registered.\n * @throws {InvalidIdError} If the UUID is not a valid UUID.\n */\nexport function encode(type: string, uuid: string): string {\n assertType(type);\n\n if (!uuidValidate(uuid)) {\n throw new InvalidIdError(`Value is not a valid UUID: ${uuid}`);\n }\n\n const hex = uuid.replaceAll(\"-\", \"\").toLowerCase();\n return `${type}_${hex}`;\n}\n\n/**\n * Decode a Flametrench wire-format ID into its type and canonical UUID.\n *\n * @example\n * decode(\"usr_0190f2a81b3c7abc8123456789abcdef\")\n * // → { type: \"usr\", uuid: \"0190f2a8-1b3c-7abc-8123-456789abcdef\" }\n *\n * @throws {InvalidIdError} If the ID is malformed.\n * @throws {InvalidTypeError} If the type prefix is not registered.\n */\nexport function decode(id: string): DecodedId {\n const separator = id.indexOf(\"_\");\n if (separator === -1) {\n throw new InvalidIdError(`ID missing type separator: ${id}`);\n }\n\n const type = id.slice(0, separator);\n const hex = id.slice(separator + 1);\n\n assertType(type);\n\n if (hex.length !== HEX_PAYLOAD_LENGTH || !HEX_PATTERN.test(hex)) {\n throw new InvalidIdError(\n `ID payload is not 32 lowercase hex characters: ${id}`,\n );\n }\n\n // Version nibble (13th hex char) must be 1-8. This rejects the Nil UUID\n // (v0) and Max UUID (v15/f), which the `uuid` package's validate() accepts\n // but are not meaningful identifiers.\n if (!VERSION_NIBBLE_PATTERN.test(hex[12]!)) {\n throw new InvalidIdError(`ID payload is not a valid UUID: ${id}`);\n }\n\n const canonical = [\n hex.slice(0, 8),\n hex.slice(8, 12),\n hex.slice(12, 16),\n hex.slice(16, 20),\n hex.slice(20, 32),\n ].join(\"-\");\n\n return { type, uuid: canonical };\n}\n\n/**\n * Check whether a string is a valid Flametrench wire-format ID.\n *\n * Optionally asserts that the ID is of a specific type.\n *\n * @example\n * isValid(\"usr_0190f2a81b3c7abc8123456789abcdef\") // true\n * isValid(\"usr_0190f2a81b3c7abc8123456789abcdef\", \"org\") // false\n */\nexport function isValid(id: string, expectedType?: IdType): boolean {\n try {\n const decoded = decode(id);\n if (expectedType !== undefined && decoded.type !== expectedType) {\n return false;\n }\n return true;\n } catch (error) {\n if (error instanceof InvalidIdError || error instanceof InvalidTypeError) {\n return false;\n }\n throw error;\n }\n}\n\n/**\n * Extract the type prefix from a wire-format ID.\n *\n * @throws {InvalidIdError} If the ID is malformed.\n * @throws {InvalidTypeError} If the type prefix is not registered.\n */\nexport function typeOf(id: string): IdType {\n return decode(id).type;\n}\n\n/**\n * Generate a fresh wire-format ID of the given type.\n *\n * Uses UUIDv7 so generated IDs are sortable by creation time.\n *\n * @example\n * generate(\"usr\")\n * // → \"usr_0190f2a81b3c7abc8123456789abcdef\"\n *\n * @throws {InvalidTypeError} If the type prefix is not registered.\n */\nexport function generate(type: IdType): string {\n assertType(type);\n return encode(type, uuidv7());\n}\n\n/**\n * Type guard narrowing `unknown` to a valid Flametrench ID string.\n *\n * Useful at API boundaries where incoming values need validation.\n */\nexport function isId(value: unknown, expectedType?: IdType): value is string {\n return typeof value === \"string\" && isValid(value, expectedType);\n}\n\nfunction assertType(type: string): asserts type is IdType {\n if (!Object.hasOwn(TYPES, type)) {\n const registered = Object.keys(TYPES).join(\", \");\n throw new InvalidTypeError(\n `Unregistered type prefix: '${type}'. Registered prefixes: ${registered}.`,\n );\n }\n}\n","// Copyright 2026 NDC Digital, LLC\n// SPDX-License-Identifier: Apache-2.0\n\n/**\n * Thrown when an ID string is malformed or cannot be parsed as a\n * Flametrench wire-format identifier.\n */\nexport class InvalidIdError extends Error {\n constructor(message: string) {\n super(message);\n this.name = \"InvalidIdError\";\n }\n}\n\n/**\n * Thrown when an ID uses a type prefix that is not in the registered\n * type prefix set.\n */\nexport class InvalidTypeError extends Error {\n constructor(message: string) {\n super(message);\n this.name = \"InvalidTypeError\";\n }\n}\n"],"mappings":";AAGA,SAAS,MAAM,QAAQ,YAAY,oBAAoB;;;ACIhD,IAAM,iBAAN,cAA6B,MAAM;AAAA,EACxC,YAAY,SAAiB;AAC3B,UAAM,OAAO;AACb,SAAK,OAAO;AAAA,EACd;AACF;AAMO,IAAM,mBAAN,cAA+B,MAAM;AAAA,EAC1C,YAAY,SAAiB;AAC3B,UAAM,OAAO;AACb,SAAK,OAAO;AAAA,EACd;AACF;;;ADLO,IAAM,QAAQ;AAAA,EACnB,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,MAAM;AAAA,EACN,KAAK;AACP;AAYA,IAAM,qBAAqB;AAC3B,IAAM,cAAc;AACpB,IAAM,yBAAyB;AAYxB,SAAS,OAAO,MAAc,MAAsB;AACzD,aAAW,IAAI;AAEf,MAAI,CAAC,aAAa,IAAI,GAAG;AACvB,UAAM,IAAI,eAAe,8BAA8B,IAAI,EAAE;AAAA,EAC/D;AAEA,QAAM,MAAM,KAAK,WAAW,KAAK,EAAE,EAAE,YAAY;AACjD,SAAO,GAAG,IAAI,IAAI,GAAG;AACvB;AAYO,SAAS,OAAO,IAAuB;AAC5C,QAAM,YAAY,GAAG,QAAQ,GAAG;AAChC,MAAI,cAAc,IAAI;AACpB,UAAM,IAAI,eAAe,8BAA8B,EAAE,EAAE;AAAA,EAC7D;AAEA,QAAM,OAAO,GAAG,MAAM,GAAG,SAAS;AAClC,QAAM,MAAM,GAAG,MAAM,YAAY,CAAC;AAElC,aAAW,IAAI;AAEf,MAAI,IAAI,WAAW,sBAAsB,CAAC,YAAY,KAAK,GAAG,GAAG;AAC/D,UAAM,IAAI;AAAA,MACR,kDAAkD,EAAE;AAAA,IACtD;AAAA,EACF;AAKA,MAAI,CAAC,uBAAuB,KAAK,IAAI,EAAE,CAAE,GAAG;AAC1C,UAAM,IAAI,eAAe,mCAAmC,EAAE,EAAE;AAAA,EAClE;AAEA,QAAM,YAAY;AAAA,IAChB,IAAI,MAAM,GAAG,CAAC;AAAA,IACd,IAAI,MAAM,GAAG,EAAE;AAAA,IACf,IAAI,MAAM,IAAI,EAAE;AAAA,IAChB,IAAI,MAAM,IAAI,EAAE;AAAA,IAChB,IAAI,MAAM,IAAI,EAAE;AAAA,EAClB,EAAE,KAAK,GAAG;AAEV,SAAO,EAAE,MAAM,MAAM,UAAU;AACjC;AAWO,SAAS,QAAQ,IAAY,cAAgC;AAClE,MAAI;AACF,UAAM,UAAU,OAAO,EAAE;AACzB,QAAI,iBAAiB,UAAa,QAAQ,SAAS,cAAc;AAC/D,aAAO;AAAA,IACT;AACA,WAAO;AAAA,EACT,SAAS,OAAO;AACd,QAAI,iBAAiB,kBAAkB,iBAAiB,kBAAkB;AACxE,aAAO;AAAA,IACT;AACA,UAAM;AAAA,EACR;AACF;AAQO,SAAS,OAAO,IAAoB;AACzC,SAAO,OAAO,EAAE,EAAE;AACpB;AAaO,SAAS,SAAS,MAAsB;AAC7C,aAAW,IAAI;AACf,SAAO,OAAO,MAAM,OAAO,CAAC;AAC9B;AAOO,SAAS,KAAK,OAAgB,cAAwC;AAC3E,SAAO,OAAO,UAAU,YAAY,QAAQ,OAAO,YAAY;AACjE;AAEA,SAAS,WAAW,MAAsC;AACxD,MAAI,CAAC,OAAO,OAAO,OAAO,IAAI,GAAG;AAC/B,UAAM,aAAa,OAAO,KAAK,KAAK,EAAE,KAAK,IAAI;AAC/C,UAAM,IAAI;AAAA,MACR,8BAA8B,IAAI,2BAA2B,UAAU;AAAA,IACzE;AAAA,EACF;AACF;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@flametrench/ids",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Prefixed wire-format IDs for Flametrench. UUIDv7 storage, self-describing public identifiers.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"author": {
|
|
7
|
+
"name": "NDC Digital, LLC",
|
|
8
|
+
"url": "https://flametrench.dev"
|
|
9
|
+
},
|
|
10
|
+
"homepage": "https://github.com/flametrench/node",
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "https://github.com/flametrench/node.git",
|
|
14
|
+
"directory": "packages/ids"
|
|
15
|
+
},
|
|
16
|
+
"bugs": {
|
|
17
|
+
"url": "https://github.com/flametrench/node/issues"
|
|
18
|
+
},
|
|
19
|
+
"keywords": [
|
|
20
|
+
"flametrench",
|
|
21
|
+
"uuid",
|
|
22
|
+
"uuidv7",
|
|
23
|
+
"identifier",
|
|
24
|
+
"id"
|
|
25
|
+
],
|
|
26
|
+
"type": "module",
|
|
27
|
+
"main": "./dist/index.js",
|
|
28
|
+
"module": "./dist/index.js",
|
|
29
|
+
"types": "./dist/index.d.ts",
|
|
30
|
+
"exports": {
|
|
31
|
+
".": {
|
|
32
|
+
"types": "./dist/index.d.ts",
|
|
33
|
+
"import": "./dist/index.js"
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
"files": [
|
|
37
|
+
"dist",
|
|
38
|
+
"README.md",
|
|
39
|
+
"LICENSE"
|
|
40
|
+
],
|
|
41
|
+
"engines": {
|
|
42
|
+
"node": ">=20"
|
|
43
|
+
},
|
|
44
|
+
"dependencies": {
|
|
45
|
+
"uuid": "^14.0.0"
|
|
46
|
+
},
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"@types/node": "^20.0.0",
|
|
49
|
+
"tsup": "^8.5.0",
|
|
50
|
+
"typescript": "^5.4.0",
|
|
51
|
+
"vitest": "^3.0.0"
|
|
52
|
+
},
|
|
53
|
+
"scripts": {
|
|
54
|
+
"build": "tsup",
|
|
55
|
+
"test": "vitest run",
|
|
56
|
+
"test:watch": "vitest",
|
|
57
|
+
"typecheck": "tsc --noEmit"
|
|
58
|
+
}
|
|
59
|
+
}
|