@rasmx/uid 1.0.0 → 1.0.1

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.
Files changed (3) hide show
  1. package/index.d.ts +16 -22
  2. package/index.js +28 -30
  3. package/package.json +1 -1
package/index.d.ts CHANGED
@@ -1,31 +1,25 @@
1
1
  export interface UuidNamespace {
2
- v1(): Promise<string>;
3
- v3(namespace: string, name: string): Promise<string>;
4
- v4(): Promise<string>;
5
- v5(namespace: string, name: string): Promise<string>;
6
- v6(): Promise<string>;
7
- v7(): Promise<string>;
8
- bulk(amount?: number): Promise<string[]>;
9
- isValid(str: string): Promise<boolean>;
10
- toShort(str: string): Promise<string>;
11
- fromShort(short: string): Promise<string>;
12
- getTimestamp(str: string): Promise<Date | null>;
2
+ v1(): string;
3
+ v3(namespace: string, name: string): string;
4
+ v4(): string;
5
+ v5(namespace: string, name: string): string;
6
+ v6(): string;
7
+ v7(): string;
8
+ bulk(amount?: number): string[];
9
+ isValid(str: string): boolean;
10
+ toShort(str: string): string;
11
+ fromShort(short: string): string;
12
+ getTimestamp(str: string): Date | null;
13
13
  }
14
14
 
15
15
  export interface UlidNamespace {
16
- generate(): Promise<string>;
17
- fromTime(ms: number | bigint): Promise<string>;
18
- getTimestamp(str: string): Promise<Date | null>;
16
+ generate(): string;
17
+ fromTime(ms: number | bigint): string;
18
+ getTimestamp(str: string): Date | null;
19
19
  }
20
20
 
21
21
  export const uuid: UuidNamespace;
22
22
  export const ulid: UlidNamespace;
23
- export const nanoid: (size?: number, alphabet?: string) => Promise<string>;
23
+ export const nanoid: (size?: number, alphabet?: string) => string;
24
24
 
25
- const init: () => Promise<{
26
- uuid: UuidNamespace;
27
- ulid: UlidNamespace;
28
- nanoid: typeof nanoid;
29
- }>;
30
-
31
- export default init;
25
+ export default function init(input?: string | URL | Request): Promise<void>;
package/index.js CHANGED
@@ -1,48 +1,46 @@
1
- import init, * as wasm from './pkg/rasmx_uuid.js';
1
+ import initWasm, * as wasm from './pkg/rasmx_uuid.js';
2
2
 
3
- let initialized = null;
3
+ let ready = false;
4
4
 
5
- async function ensure() {
6
- if (!initialized) {
7
- initialized = init();
8
- }
9
- return initialized;
5
+ export default async function init(input) {
6
+ if (ready) return;
7
+ await initWasm(input);
8
+ ready = true;
10
9
  }
11
10
 
11
+ const check = () => {
12
+ if (!ready) throw new Error("@rasmx/uuid: WASM module not initialized. Call 'await init()' once before use.");
13
+ };
14
+
12
15
  export const uuid = {
13
- v1: async () => { await ensure(); return wasm.v1(); },
14
- v3: async (ns, name) => { await ensure(); return wasm.v3(ns, name); },
15
- v4: async () => { await ensure(); return wasm.v4(); },
16
- v5: async (ns, name) => { await ensure(); return wasm.v5(ns, name); },
17
- v6: async () => { await ensure(); return wasm.v6(); },
18
- v7: async () => { await ensure(); return wasm.v7(); },
19
- bulk: async (amount = 10) => { await ensure(); return wasm.bulk_v7(amount); },
20
- isValid: async (str) => { await ensure(); return wasm.is_valid_uuid(str); },
21
- toShort: async (str) => { await ensure(); return wasm.to_short_id(str); },
22
- fromShort: async (short) => { await ensure(); return wasm.from_short_id(short); },
23
- getTimestamp: async (str) => {
24
- await ensure();
16
+ v1: () => { check(); return wasm.v1(); },
17
+ v3: (ns, name) => { check(); return wasm.v3(ns, name); },
18
+ v4: () => { check(); return wasm.v4(); },
19
+ v5: (ns, name) => { check(); return wasm.v5(ns, name); },
20
+ v6: () => { check(); return wasm.v6(); },
21
+ v7: () => { check(); return wasm.v7(); },
22
+ bulk: (amount = 10) => { check(); return wasm.bulk_v7(amount); },
23
+ isValid: (str) => { check(); return wasm.is_valid_uuid(str); },
24
+ toShort: (str) => { check(); return wasm.to_short_id(str); },
25
+ fromShort: (short) => { check(); return wasm.from_short_id(short); },
26
+ getTimestamp: (str) => {
27
+ check();
25
28
  const ts = wasm.get_timestamp(str);
26
29
  return ts ? new Date(Number(ts)) : null;
27
30
  }
28
31
  };
29
32
 
30
- export const nanoid = async (size = 21, alphabet) => {
31
- await ensure();
33
+ export const nanoid = (size = 21, alphabet) => {
34
+ check();
32
35
  return wasm.nanoid(size, alphabet);
33
36
  };
34
37
 
35
38
  export const ulid = {
36
- generate: async () => { await ensure(); return wasm.ulid(); },
37
- fromTime: async (ms) => { await ensure(); return wasm.ulid_from_time(BigInt(ms)); },
38
- getTimestamp: async (str) => {
39
- await ensure();
39
+ generate: () => { check(); return wasm.ulid(); },
40
+ fromTime: (ms) => { check(); return wasm.ulid_from_time(BigInt(ms)); },
41
+ getTimestamp: (str) => {
42
+ check();
40
43
  const ts = wasm.get_timestamp(str);
41
44
  return ts ? new Date(Number(ts)) : null;
42
45
  }
43
- };
44
-
45
- export default async () => {
46
- await ensure();
47
- return { uuid, nanoid, ulid };
48
46
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rasmx/uid",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "Blazing fast WASM ID generation library powered by Rust. Support for UUID (v1-v7), ULID, NanoID, and Base62 shortening.",
5
5
  "publishConfig": {
6
6
  "access": "public"