@jonsoc/util 1.1.46
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/package.json +26 -0
- package/src/binary.ts +41 -0
- package/src/encode.ts +30 -0
- package/src/error.ts +54 -0
- package/src/fn.ts +11 -0
- package/src/identifier.ts +48 -0
- package/src/iife.ts +3 -0
- package/src/lazy.ts +11 -0
- package/src/path.ts +19 -0
- package/src/retry.ts +41 -0
- package/src/slug.ts +74 -0
- package/sst-env.d.ts +9 -0
- package/tsconfig.json +14 -0
package/package.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@jonsoc/util",
|
|
3
|
+
"version": "1.1.46",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/Noisemaker111/Jonsoc"
|
|
9
|
+
},
|
|
10
|
+
"exports": {
|
|
11
|
+
"./*": "./src/*.ts"
|
|
12
|
+
},
|
|
13
|
+
"scripts": {
|
|
14
|
+
"typecheck": "tsc --noEmit"
|
|
15
|
+
},
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"zod": "catalog:"
|
|
18
|
+
},
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"typescript": "catalog:",
|
|
21
|
+
"@types/bun": "catalog:"
|
|
22
|
+
},
|
|
23
|
+
"publishConfig": {
|
|
24
|
+
"access": "public"
|
|
25
|
+
}
|
|
26
|
+
}
|
package/src/binary.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
export namespace Binary {
|
|
2
|
+
export function search<T>(array: T[], id: string, compare: (item: T) => string): { found: boolean; index: number } {
|
|
3
|
+
let left = 0
|
|
4
|
+
let right = array.length - 1
|
|
5
|
+
|
|
6
|
+
while (left <= right) {
|
|
7
|
+
const mid = Math.floor((left + right) / 2)
|
|
8
|
+
const midId = compare(array[mid])
|
|
9
|
+
|
|
10
|
+
if (midId === id) {
|
|
11
|
+
return { found: true, index: mid }
|
|
12
|
+
} else if (midId < id) {
|
|
13
|
+
left = mid + 1
|
|
14
|
+
} else {
|
|
15
|
+
right = mid - 1
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
return { found: false, index: left }
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function insert<T>(array: T[], item: T, compare: (item: T) => string): T[] {
|
|
23
|
+
const id = compare(item)
|
|
24
|
+
let left = 0
|
|
25
|
+
let right = array.length
|
|
26
|
+
|
|
27
|
+
while (left < right) {
|
|
28
|
+
const mid = Math.floor((left + right) / 2)
|
|
29
|
+
const midId = compare(array[mid])
|
|
30
|
+
|
|
31
|
+
if (midId < id) {
|
|
32
|
+
left = mid + 1
|
|
33
|
+
} else {
|
|
34
|
+
right = mid
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
array.splice(left, 0, item)
|
|
39
|
+
return array
|
|
40
|
+
}
|
|
41
|
+
}
|
package/src/encode.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export function base64Encode(value: string) {
|
|
2
|
+
const bytes = new TextEncoder().encode(value)
|
|
3
|
+
const binary = Array.from(bytes, (b) => String.fromCharCode(b)).join("")
|
|
4
|
+
return btoa(binary).replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "")
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export function base64Decode(value: string) {
|
|
8
|
+
const binary = atob(value.replace(/-/g, "+").replace(/_/g, "/"))
|
|
9
|
+
const bytes = Uint8Array.from(binary, (c) => c.charCodeAt(0))
|
|
10
|
+
return new TextDecoder().decode(bytes)
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export async function hash(content: string, algorithm = "SHA-256"): Promise<string> {
|
|
14
|
+
const encoder = new TextEncoder()
|
|
15
|
+
const data = encoder.encode(content)
|
|
16
|
+
const hashBuffer = await crypto.subtle.digest(algorithm, data)
|
|
17
|
+
const hashArray = Array.from(new Uint8Array(hashBuffer))
|
|
18
|
+
const hashHex = hashArray.map((b) => b.toString(16).padStart(2, "0")).join("")
|
|
19
|
+
return hashHex
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function checksum(content: string): string | undefined {
|
|
23
|
+
if (!content) return undefined
|
|
24
|
+
let hash = 0x811c9dc5
|
|
25
|
+
for (let i = 0; i < content.length; i++) {
|
|
26
|
+
hash ^= content.charCodeAt(i)
|
|
27
|
+
hash = Math.imul(hash, 0x01000193)
|
|
28
|
+
}
|
|
29
|
+
return (hash >>> 0).toString(36)
|
|
30
|
+
}
|
package/src/error.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import z from "zod"
|
|
2
|
+
|
|
3
|
+
export abstract class NamedError extends Error {
|
|
4
|
+
abstract schema(): z.core.$ZodType
|
|
5
|
+
abstract toObject(): { name: string; data: any }
|
|
6
|
+
|
|
7
|
+
static create<Name extends string, Data extends z.core.$ZodType>(name: Name, data: Data) {
|
|
8
|
+
const schema = z
|
|
9
|
+
.object({
|
|
10
|
+
name: z.literal(name),
|
|
11
|
+
data,
|
|
12
|
+
})
|
|
13
|
+
.meta({
|
|
14
|
+
ref: name,
|
|
15
|
+
})
|
|
16
|
+
const result = class extends NamedError {
|
|
17
|
+
public static readonly Schema = schema
|
|
18
|
+
|
|
19
|
+
public override readonly name = name as Name
|
|
20
|
+
|
|
21
|
+
constructor(
|
|
22
|
+
public readonly data: z.input<Data>,
|
|
23
|
+
options?: ErrorOptions,
|
|
24
|
+
) {
|
|
25
|
+
super(name, options)
|
|
26
|
+
this.name = name
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
static isInstance(input: any): input is InstanceType<typeof result> {
|
|
30
|
+
return typeof input === "object" && "name" in input && input.name === name
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
schema() {
|
|
34
|
+
return schema
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
toObject() {
|
|
38
|
+
return {
|
|
39
|
+
name: name,
|
|
40
|
+
data: this.data,
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
Object.defineProperty(result, "name", { value: name })
|
|
45
|
+
return result
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
public static readonly Unknown = NamedError.create(
|
|
49
|
+
"UnknownError",
|
|
50
|
+
z.object({
|
|
51
|
+
message: z.string(),
|
|
52
|
+
}),
|
|
53
|
+
)
|
|
54
|
+
}
|
package/src/fn.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { z } from "zod"
|
|
2
|
+
|
|
3
|
+
export function fn<T extends z.ZodType, Result>(schema: T, cb: (input: z.infer<T>) => Result) {
|
|
4
|
+
const result = (input: z.infer<T>) => {
|
|
5
|
+
const parsed = schema.parse(input)
|
|
6
|
+
return cb(parsed)
|
|
7
|
+
}
|
|
8
|
+
result.force = (input: z.infer<T>) => cb(input)
|
|
9
|
+
result.schema = schema
|
|
10
|
+
return result
|
|
11
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { randomBytes } from "crypto"
|
|
2
|
+
|
|
3
|
+
export namespace Identifier {
|
|
4
|
+
const LENGTH = 26
|
|
5
|
+
|
|
6
|
+
// State for monotonic ID generation
|
|
7
|
+
let lastTimestamp = 0
|
|
8
|
+
let counter = 0
|
|
9
|
+
|
|
10
|
+
export function ascending() {
|
|
11
|
+
return create(false)
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function descending() {
|
|
15
|
+
return create(true)
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function randomBase62(length: number): string {
|
|
19
|
+
const chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
|
20
|
+
let result = ""
|
|
21
|
+
const bytes = randomBytes(length)
|
|
22
|
+
for (let i = 0; i < length; i++) {
|
|
23
|
+
result += chars[bytes[i] % 62]
|
|
24
|
+
}
|
|
25
|
+
return result
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function create(descending: boolean, timestamp?: number): string {
|
|
29
|
+
const currentTimestamp = timestamp ?? Date.now()
|
|
30
|
+
|
|
31
|
+
if (currentTimestamp !== lastTimestamp) {
|
|
32
|
+
lastTimestamp = currentTimestamp
|
|
33
|
+
counter = 0
|
|
34
|
+
}
|
|
35
|
+
counter++
|
|
36
|
+
|
|
37
|
+
let now = BigInt(currentTimestamp) * BigInt(0x1000) + BigInt(counter)
|
|
38
|
+
|
|
39
|
+
now = descending ? ~now : now
|
|
40
|
+
|
|
41
|
+
const timeBytes = Buffer.alloc(6)
|
|
42
|
+
for (let i = 0; i < 6; i++) {
|
|
43
|
+
timeBytes[i] = Number((now >> BigInt(40 - 8 * i)) & BigInt(0xff))
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return timeBytes.toString("hex") + randomBase62(LENGTH - 12)
|
|
47
|
+
}
|
|
48
|
+
}
|
package/src/iife.ts
ADDED
package/src/lazy.ts
ADDED
package/src/path.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export function getFilename(path: string | undefined) {
|
|
2
|
+
if (!path) return ""
|
|
3
|
+
const trimmed = path.replace(/[\/\\]+$/, "")
|
|
4
|
+
const parts = trimmed.split(/[\/\\]/)
|
|
5
|
+
return parts[parts.length - 1] ?? ""
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export function getDirectory(path: string | undefined) {
|
|
9
|
+
if (!path) return ""
|
|
10
|
+
const trimmed = path.replace(/[\/\\]+$/, "")
|
|
11
|
+
const parts = trimmed.split(/[\/\\]/)
|
|
12
|
+
return parts.slice(0, parts.length - 1).join("/") + "/"
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function getFileExtension(path: string | undefined) {
|
|
16
|
+
if (!path) return ""
|
|
17
|
+
const parts = path.split(".")
|
|
18
|
+
return parts[parts.length - 1]
|
|
19
|
+
}
|
package/src/retry.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
export interface RetryOptions {
|
|
2
|
+
attempts?: number
|
|
3
|
+
delay?: number
|
|
4
|
+
factor?: number
|
|
5
|
+
maxDelay?: number
|
|
6
|
+
retryIf?: (error: unknown) => boolean
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const TRANSIENT_MESSAGES = [
|
|
10
|
+
"load failed",
|
|
11
|
+
"network connection was lost",
|
|
12
|
+
"network request failed",
|
|
13
|
+
"failed to fetch",
|
|
14
|
+
"econnreset",
|
|
15
|
+
"econnrefused",
|
|
16
|
+
"etimedout",
|
|
17
|
+
"socket hang up",
|
|
18
|
+
]
|
|
19
|
+
|
|
20
|
+
function isTransientError(error: unknown): boolean {
|
|
21
|
+
if (!error) return false
|
|
22
|
+
const message = String(error instanceof Error ? error.message : error).toLowerCase()
|
|
23
|
+
return TRANSIENT_MESSAGES.some((m) => message.includes(m))
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export async function retry<T>(fn: () => Promise<T>, options: RetryOptions = {}): Promise<T> {
|
|
27
|
+
const { attempts = 3, delay = 500, factor = 2, maxDelay = 10000, retryIf = isTransientError } = options
|
|
28
|
+
|
|
29
|
+
let lastError: unknown
|
|
30
|
+
for (let attempt = 0; attempt < attempts; attempt++) {
|
|
31
|
+
try {
|
|
32
|
+
return await fn()
|
|
33
|
+
} catch (error) {
|
|
34
|
+
lastError = error
|
|
35
|
+
if (attempt === attempts - 1 || !retryIf(error)) throw error
|
|
36
|
+
const wait = Math.min(delay * Math.pow(factor, attempt), maxDelay)
|
|
37
|
+
await new Promise((resolve) => setTimeout(resolve, wait))
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
throw lastError
|
|
41
|
+
}
|
package/src/slug.ts
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
export namespace Slug {
|
|
2
|
+
const ADJECTIVES = [
|
|
3
|
+
"brave",
|
|
4
|
+
"calm",
|
|
5
|
+
"clever",
|
|
6
|
+
"cosmic",
|
|
7
|
+
"crisp",
|
|
8
|
+
"curious",
|
|
9
|
+
"eager",
|
|
10
|
+
"gentle",
|
|
11
|
+
"glowing",
|
|
12
|
+
"happy",
|
|
13
|
+
"hidden",
|
|
14
|
+
"jolly",
|
|
15
|
+
"kind",
|
|
16
|
+
"lucky",
|
|
17
|
+
"mighty",
|
|
18
|
+
"misty",
|
|
19
|
+
"neon",
|
|
20
|
+
"nimble",
|
|
21
|
+
"playful",
|
|
22
|
+
"proud",
|
|
23
|
+
"quick",
|
|
24
|
+
"quiet",
|
|
25
|
+
"shiny",
|
|
26
|
+
"silent",
|
|
27
|
+
"stellar",
|
|
28
|
+
"sunny",
|
|
29
|
+
"swift",
|
|
30
|
+
"tidy",
|
|
31
|
+
"witty",
|
|
32
|
+
] as const
|
|
33
|
+
|
|
34
|
+
const NOUNS = [
|
|
35
|
+
"cabin",
|
|
36
|
+
"cactus",
|
|
37
|
+
"canyon",
|
|
38
|
+
"circuit",
|
|
39
|
+
"comet",
|
|
40
|
+
"eagle",
|
|
41
|
+
"engine",
|
|
42
|
+
"falcon",
|
|
43
|
+
"forest",
|
|
44
|
+
"garden",
|
|
45
|
+
"harbor",
|
|
46
|
+
"island",
|
|
47
|
+
"knight",
|
|
48
|
+
"lagoon",
|
|
49
|
+
"meadow",
|
|
50
|
+
"moon",
|
|
51
|
+
"mountain",
|
|
52
|
+
"nebula",
|
|
53
|
+
"orchid",
|
|
54
|
+
"otter",
|
|
55
|
+
"panda",
|
|
56
|
+
"pixel",
|
|
57
|
+
"planet",
|
|
58
|
+
"river",
|
|
59
|
+
"rocket",
|
|
60
|
+
"sailor",
|
|
61
|
+
"squid",
|
|
62
|
+
"star",
|
|
63
|
+
"tiger",
|
|
64
|
+
"wizard",
|
|
65
|
+
"wolf",
|
|
66
|
+
] as const
|
|
67
|
+
|
|
68
|
+
export function create() {
|
|
69
|
+
return [
|
|
70
|
+
ADJECTIVES[Math.floor(Math.random() * ADJECTIVES.length)],
|
|
71
|
+
NOUNS[Math.floor(Math.random() * NOUNS.length)],
|
|
72
|
+
].join("-")
|
|
73
|
+
}
|
|
74
|
+
}
|
package/sst-env.d.ts
ADDED
package/tsconfig.json
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ESNext",
|
|
4
|
+
"module": "ESNext",
|
|
5
|
+
"moduleResolution": "bundler",
|
|
6
|
+
"skipLibCheck": true,
|
|
7
|
+
"allowSyntheticDefaultImports": true,
|
|
8
|
+
"esModuleInterop": true,
|
|
9
|
+
"allowJs": true,
|
|
10
|
+
"noEmit": true,
|
|
11
|
+
"strict": true,
|
|
12
|
+
"isolatedModules": true
|
|
13
|
+
}
|
|
14
|
+
}
|