@owlmeans/basic-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 +21 -0
- package/README.md +146 -0
- package/build/consts.d.ts +5 -0
- package/build/consts.d.ts.map +1 -0
- package/build/consts.js +6 -0
- package/build/consts.js.map +1 -0
- package/build/helper.d.ts +5 -0
- package/build/helper.d.ts.map +1 -0
- package/build/helper.js +19 -0
- package/build/helper.js.map +1 -0
- package/build/index.d.ts +3 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +3 -0
- package/build/index.js.map +1 -0
- package/package.json +36 -0
- package/src/consts.ts +5 -0
- package/src/helper.ts +22 -0
- package/src/index.ts +3 -0
- package/tsconfig.json +14 -0
- package/tsconfig.tsbuildinfo +1 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 OwlMeans Common — Fullstack typescript framework
|
|
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.
|
package/README.md
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
# OwlMeans Basic IDs — Basic ID Generating Libraries
|
|
2
|
+
|
|
3
|
+
A lightweight TypeScript library for generating various types of random and semi-random identifiers. This package is primarily used by the OwlMeans Authentication Subsystem and provides utilities for creating secure, random prefixes and IDs in different encoding formats.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @owlmeans/basic-ids
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { createRandomPrefix, createIdOfLength, uuid, IdStyle } from '@owlmeans/basic-ids';
|
|
15
|
+
|
|
16
|
+
// Generate a random prefix (default: 6 bytes, Base58 encoded)
|
|
17
|
+
const prefix = createRandomPrefix();
|
|
18
|
+
|
|
19
|
+
// Generate a specific length ID
|
|
20
|
+
const shortId = createIdOfLength(8, IdStyle.Base58);
|
|
21
|
+
|
|
22
|
+
// Generate a UUID v4
|
|
23
|
+
const id = uuid();
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## API Reference
|
|
27
|
+
|
|
28
|
+
### IdStyle Enum
|
|
29
|
+
|
|
30
|
+
Defines the available encoding formats for generated IDs.
|
|
31
|
+
|
|
32
|
+
```typescript
|
|
33
|
+
enum IdStyle {
|
|
34
|
+
Base58 = 'base58',
|
|
35
|
+
Base64 = 'base64'
|
|
36
|
+
}
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
**Values:**
|
|
40
|
+
- `Base58`: Uses Base58 encoding (default) - more compact, URL-safe, and avoids ambiguous characters
|
|
41
|
+
- `Base64`: Uses Base64 URL-safe encoding without padding - standard web-safe encoding
|
|
42
|
+
|
|
43
|
+
### createRandomPrefix(length?, format?)
|
|
44
|
+
|
|
45
|
+
Creates a random prefix using cryptographically secure random bytes.
|
|
46
|
+
|
|
47
|
+
**Parameters:**
|
|
48
|
+
- `length` (optional): `number` - Number of random bytes to generate (default: 6)
|
|
49
|
+
- `format` (optional): `IdStyle` - Encoding format (default: `IdStyle.Base58`)
|
|
50
|
+
|
|
51
|
+
**Returns:** `string` - The encoded random prefix
|
|
52
|
+
|
|
53
|
+
**Example:**
|
|
54
|
+
```typescript
|
|
55
|
+
// Generate 6-byte Base58 prefix (default)
|
|
56
|
+
const prefix1 = createRandomPrefix();
|
|
57
|
+
|
|
58
|
+
// Generate 10-byte Base64 prefix
|
|
59
|
+
const prefix2 = createRandomPrefix(10, IdStyle.Base64);
|
|
60
|
+
|
|
61
|
+
// Generate 4-byte Base58 prefix
|
|
62
|
+
const prefix3 = createRandomPrefix(4, IdStyle.Base58);
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### createIdOfLength(length?, format?)
|
|
66
|
+
|
|
67
|
+
Creates an ID of a specific character length by generating random bytes and truncating the encoded result.
|
|
68
|
+
|
|
69
|
+
**Parameters:**
|
|
70
|
+
- `length` (optional): `number` - Desired character length of the final ID (default: 6)
|
|
71
|
+
- `format` (optional): `IdStyle` - Encoding format (default: `IdStyle.Base58`)
|
|
72
|
+
|
|
73
|
+
**Returns:** `string` - The encoded ID trimmed to the specified length
|
|
74
|
+
|
|
75
|
+
**Example:**
|
|
76
|
+
```typescript
|
|
77
|
+
// Generate 6-character Base58 ID (default)
|
|
78
|
+
const id1 = createIdOfLength();
|
|
79
|
+
|
|
80
|
+
// Generate 12-character Base64 ID
|
|
81
|
+
const id2 = createIdOfLength(12, IdStyle.Base64);
|
|
82
|
+
|
|
83
|
+
// Generate 8-character Base58 ID
|
|
84
|
+
const id3 = createIdOfLength(8, IdStyle.Base58);
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
**Note:** This function generates `length * 2` random bytes and then truncates the encoded result to the desired length to ensure sufficient entropy.
|
|
88
|
+
|
|
89
|
+
### uuid()
|
|
90
|
+
|
|
91
|
+
Generates a standard UUID version 4 (random UUID).
|
|
92
|
+
|
|
93
|
+
**Returns:** `string` - A UUID v4 string in the format `xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx`
|
|
94
|
+
|
|
95
|
+
**Example:**
|
|
96
|
+
```typescript
|
|
97
|
+
const id = uuid();
|
|
98
|
+
// Example output: "f47ac10b-58cc-4372-a567-0e02b2c3d479"
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## Use Cases
|
|
102
|
+
|
|
103
|
+
### Authentication Tokens
|
|
104
|
+
```typescript
|
|
105
|
+
// Generate a session prefix
|
|
106
|
+
const sessionPrefix = createRandomPrefix(8, IdStyle.Base58);
|
|
107
|
+
|
|
108
|
+
// Generate a short API key
|
|
109
|
+
const apiKey = createIdOfLength(32, IdStyle.Base64);
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
### Resource Identifiers
|
|
113
|
+
```typescript
|
|
114
|
+
// Generate short resource IDs
|
|
115
|
+
const resourceId = createIdOfLength(10);
|
|
116
|
+
|
|
117
|
+
// Generate unique request IDs
|
|
118
|
+
const requestId = uuid();
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
### Database Keys
|
|
122
|
+
```typescript
|
|
123
|
+
// Generate compact database keys
|
|
124
|
+
const dbKey = createRandomPrefix(12, IdStyle.Base58);
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
## Security Considerations
|
|
128
|
+
|
|
129
|
+
- All random functions use cryptographically secure random number generation via `@noble/hashes`
|
|
130
|
+
- Base58 encoding avoids ambiguous characters (0, O, l, I) making IDs more user-friendly
|
|
131
|
+
- Base64 encoding provides standard web-safe encoding
|
|
132
|
+
- The `createIdOfLength` function generates extra entropy to compensate for truncation
|
|
133
|
+
|
|
134
|
+
## Dependencies
|
|
135
|
+
|
|
136
|
+
- `@noble/hashes` - Cryptographic hashing and random number generation
|
|
137
|
+
- `@scure/base` - Base encoding utilities (Base58, Base64)
|
|
138
|
+
- `uuid` - UUID generation
|
|
139
|
+
|
|
140
|
+
## Part of OwlMeans Common
|
|
141
|
+
|
|
142
|
+
This package is part of the OwlMeans Common library ecosystem. For more information about the overall architecture and concepts, see the [main README](../../README.md).
|
|
143
|
+
|
|
144
|
+
## License
|
|
145
|
+
|
|
146
|
+
See the [LICENSE](../../LICENSE) file in the root directory.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"consts.d.ts","sourceRoot":"","sources":["../src/consts.ts"],"names":[],"mappings":"AACA,oBAAY,OAAO;IACjB,MAAM,WAAW;IACjB,MAAM,WAAW;CAClB"}
|
package/build/consts.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"consts.js","sourceRoot":"","sources":["../src/consts.ts"],"names":[],"mappings":"AACA,MAAM,CAAN,IAAY,OAGX;AAHD,WAAY,OAAO;IACjB,4BAAiB,CAAA;IACjB,4BAAiB,CAAA;AACnB,CAAC,EAHW,OAAO,KAAP,OAAO,QAGlB"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { IdStyle } from './consts.js';
|
|
2
|
+
export declare const createRandomPrefix: (length?: number, format?: IdStyle) => string;
|
|
3
|
+
export declare const createIdOfLength: (length?: number, format?: IdStyle) => string;
|
|
4
|
+
export declare const uuid: () => string;
|
|
5
|
+
//# sourceMappingURL=helper.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"helper.d.ts","sourceRoot":"","sources":["../src/helper.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,OAAO,EAAE,MAAM,aAAa,CAAA;AAGrC,eAAO,MAAM,kBAAkB,YAAY,MAAM,WAAc,OAAO,KAAoB,MASzF,CAAA;AAED,eAAO,MAAM,gBAAgB,YAAY,MAAM,WAAc,OAAO,KAAoB,MAEvF,CAAA;AAED,eAAO,MAAM,IAAI,QAAO,MAAc,CAAA"}
|
package/build/helper.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { randomBytes } from '@noble/hashes/utils';
|
|
2
|
+
import { base58, base64urlnopad } from '@scure/base';
|
|
3
|
+
import { IdStyle } from './consts.js';
|
|
4
|
+
import { v4 } from 'uuid';
|
|
5
|
+
export const createRandomPrefix = (length = 6, format = IdStyle.Base58) => {
|
|
6
|
+
const rand = randomBytes(length);
|
|
7
|
+
switch (format) {
|
|
8
|
+
case IdStyle.Base64:
|
|
9
|
+
return base64urlnopad.encode(rand);
|
|
10
|
+
case IdStyle.Base58:
|
|
11
|
+
default:
|
|
12
|
+
return base58.encode(rand);
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
export const createIdOfLength = (length = 6, format = IdStyle.Base58) => {
|
|
16
|
+
return createRandomPrefix(length * 2, format).slice(0, length);
|
|
17
|
+
};
|
|
18
|
+
export const uuid = () => v4();
|
|
19
|
+
//# sourceMappingURL=helper.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"helper.js","sourceRoot":"","sources":["../src/helper.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAA;AACjD,OAAO,EAAE,MAAM,EAAE,cAAc,EAAE,MAAM,aAAa,CAAA;AACpD,OAAO,EAAE,OAAO,EAAE,MAAM,aAAa,CAAA;AACrC,OAAO,EAAE,EAAE,EAAE,MAAM,MAAM,CAAA;AAEzB,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAAC,SAAiB,CAAC,EAAE,SAAkB,OAAO,CAAC,MAAM,EAAU,EAAE;IACjG,MAAM,IAAI,GAAG,WAAW,CAAC,MAAM,CAAC,CAAA;IAChC,QAAQ,MAAM,EAAE,CAAC;QACf,KAAK,OAAO,CAAC,MAAM;YACjB,OAAO,cAAc,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;QACpC,KAAK,OAAO,CAAC,MAAM,CAAC;QACpB;YACE,OAAO,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;IAC9B,CAAC;AACH,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC,SAAiB,CAAC,EAAE,SAAkB,OAAO,CAAC,MAAM,EAAU,EAAE;IAC/F,OAAO,kBAAkB,CAAC,MAAM,GAAG,CAAC,EAAE,MAAM,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,MAAM,CAAC,CAAA;AAChE,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,IAAI,GAAG,GAAW,EAAE,CAAC,EAAE,EAAE,CAAA"}
|
package/build/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA"}
|
package/build/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA"}
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@owlmeans/basic-ids",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"build": "tsc -b",
|
|
7
|
+
"dev": "sleep 42 && nodemon -e ts,tsx,json --watch src --exec \"tsc -p ./tsconfig.json\"",
|
|
8
|
+
"watch": "tsc -b -w --preserveWatchOutput --pretty"
|
|
9
|
+
},
|
|
10
|
+
"main": "build/index.js",
|
|
11
|
+
"module": "build/index.js",
|
|
12
|
+
"types": "build/index.d.ts",
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"import": "./build/index.js",
|
|
16
|
+
"require": "./build/index.js",
|
|
17
|
+
"default": "./build/index.js",
|
|
18
|
+
"module": "./build/index.js",
|
|
19
|
+
"types": "./build/index.d.ts"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"@types/uuid": "^10.0.0",
|
|
24
|
+
"nodemon": "^3.1.7",
|
|
25
|
+
"typescript": "^5.6.3"
|
|
26
|
+
},
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"@noble/hashes": "^1.5.0",
|
|
29
|
+
"@scure/base": "^1.1.9",
|
|
30
|
+
"uuid": "^10.0.0"
|
|
31
|
+
},
|
|
32
|
+
"private": false,
|
|
33
|
+
"publishConfig": {
|
|
34
|
+
"access": "public"
|
|
35
|
+
}
|
|
36
|
+
}
|
package/src/consts.ts
ADDED
package/src/helper.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
|
|
2
|
+
import { randomBytes } from '@noble/hashes/utils'
|
|
3
|
+
import { base58, base64urlnopad } from '@scure/base'
|
|
4
|
+
import { IdStyle } from './consts.js'
|
|
5
|
+
import { v4 } from 'uuid'
|
|
6
|
+
|
|
7
|
+
export const createRandomPrefix = (length: number = 6, format: IdStyle = IdStyle.Base58): string => {
|
|
8
|
+
const rand = randomBytes(length)
|
|
9
|
+
switch (format) {
|
|
10
|
+
case IdStyle.Base64:
|
|
11
|
+
return base64urlnopad.encode(rand)
|
|
12
|
+
case IdStyle.Base58:
|
|
13
|
+
default:
|
|
14
|
+
return base58.encode(rand)
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export const createIdOfLength = (length: number = 6, format: IdStyle = IdStyle.Base58): string => {
|
|
19
|
+
return createRandomPrefix(length * 2, format).slice(0, length)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export const uuid = (): string => v4()
|
package/src/index.ts
ADDED
package/tsconfig.json
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": [
|
|
3
|
+
"../tsconfig.default.json",
|
|
4
|
+
],
|
|
5
|
+
"compilerOptions": {
|
|
6
|
+
"rootDir": "./src/", /* Specify the root folder within your source files. */
|
|
7
|
+
"outDir": "./build/", /* Specify an output folder for all emitted files. */
|
|
8
|
+
},
|
|
9
|
+
"exclude": [
|
|
10
|
+
"./dist/**/*",
|
|
11
|
+
"./build/**/*",
|
|
12
|
+
"./*.ts"
|
|
13
|
+
]
|
|
14
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"root":["./src/consts.ts","./src/helper.ts","./src/index.ts"],"version":"5.6.3"}
|