quidproquo-core 0.1.28 → 0.1.29

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.
@@ -1,12 +1,16 @@
1
1
  /**
2
2
  * Generates a new random (version 4) UUID.
3
3
  *
4
- * This is the single place in quidproquo to mint a v4 UUID. It uses the native
5
- * `crypto.randomUUID()` which is available in Node 20+ and all modern browsers
6
- * (secure contexts), so it works across both backend and frontend packages with
7
- * no external dependency. Centralising it here means the underlying source can be
8
- * swapped in one spot if that ever changes.
4
+ * This is the single place in quidproquo to mint a v4 UUID, so it works across
5
+ * both backend and frontend packages with no external dependency.
6
+ *
7
+ * It prefers the native `crypto.randomUUID()` (Node 20+, and browsers in a
8
+ * SECURE context: https or localhost). Browsers deliberately hide that method on
9
+ * plain-http origins, which is exactly where a self-hosted app on a LAN ip lives
10
+ * (e.g. http://192.168.x.x), so it falls back to assembling the same v4 layout
11
+ * from `crypto.getRandomValues()`, which is available everywhere.
9
12
  *
10
13
  * @returns {string} A randomly generated v4 UUID, e.g. `109156be-c4fb-41ea-b1b4-efe1671c5836`.
11
14
  */
12
15
  export declare function generateUuid(): string;
16
+ export declare function generateUuidFromRandomValues(cryptoApi: Pick<Crypto, 'getRandomValues'>): string;
@@ -1,18 +1,40 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.generateUuid = generateUuid;
4
+ exports.generateUuidFromRandomValues = generateUuidFromRandomValues;
4
5
  /**
5
6
  * Generates a new random (version 4) UUID.
6
7
  *
7
- * This is the single place in quidproquo to mint a v4 UUID. It uses the native
8
- * `crypto.randomUUID()` which is available in Node 20+ and all modern browsers
9
- * (secure contexts), so it works across both backend and frontend packages with
10
- * no external dependency. Centralising it here means the underlying source can be
11
- * swapped in one spot if that ever changes.
8
+ * This is the single place in quidproquo to mint a v4 UUID, so it works across
9
+ * both backend and frontend packages with no external dependency.
10
+ *
11
+ * It prefers the native `crypto.randomUUID()` (Node 20+, and browsers in a
12
+ * SECURE context: https or localhost). Browsers deliberately hide that method on
13
+ * plain-http origins, which is exactly where a self-hosted app on a LAN ip lives
14
+ * (e.g. http://192.168.x.x), so it falls back to assembling the same v4 layout
15
+ * from `crypto.getRandomValues()`, which is available everywhere.
12
16
  *
13
17
  * @returns {string} A randomly generated v4 UUID, e.g. `109156be-c4fb-41ea-b1b4-efe1671c5836`.
14
18
  */
15
19
  function generateUuid() {
16
- return globalThis.crypto.randomUUID();
20
+ // lib.dom types the global as always present; older runtimes (Node before 19,
21
+ // some workers) don't expose it, so treat it as optional and fail loudly. No
22
+ // Math.random() last resort: a guessable id is worse than no id.
23
+ const cryptoApi = globalThis.crypto;
24
+ if (typeof (cryptoApi === null || cryptoApi === void 0 ? void 0 : cryptoApi.randomUUID) === 'function') {
25
+ return cryptoApi.randomUUID();
26
+ }
27
+ if (typeof (cryptoApi === null || cryptoApi === void 0 ? void 0 : cryptoApi.getRandomValues) !== 'function') {
28
+ throw new Error('generateUuid: no Web Crypto API available in this environment');
29
+ }
30
+ return generateUuidFromRandomValues(cryptoApi);
31
+ }
32
+ // RFC 4122 v4 from 16 random bytes: version nibble 4 in byte 6, variant 10xx in byte 8.
33
+ function generateUuidFromRandomValues(cryptoApi) {
34
+ const bytes = cryptoApi.getRandomValues(new Uint8Array(16));
35
+ bytes[6] = (bytes[6] & 0x0f) | 0x40;
36
+ bytes[8] = (bytes[8] & 0x3f) | 0x80;
37
+ const hex = Array.from(bytes, (byte) => byte.toString(16).padStart(2, '0')).join('');
38
+ return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
17
39
  }
18
40
  //# sourceMappingURL=generateUuid.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"generateUuid.js","sourceRoot":"","sources":["../../../../src/utils/uuid/generateUuid.ts"],"names":[],"mappings":";;AAWA,oCAEC;AAbD;;;;;;;;;;GAUG;AACH,SAAgB,YAAY;IAC1B,OAAO,UAAU,CAAC,MAAM,CAAC,UAAU,EAAE,CAAC;AACxC,CAAC","sourcesContent":["/**\n * Generates a new random (version 4) UUID.\n *\n * This is the single place in quidproquo to mint a v4 UUID. It uses the native\n * `crypto.randomUUID()` which is available in Node 20+ and all modern browsers\n * (secure contexts), so it works across both backend and frontend packages with\n * no external dependency. Centralising it here means the underlying source can be\n * swapped in one spot if that ever changes.\n *\n * @returns {string} A randomly generated v4 UUID, e.g. `109156be-c4fb-41ea-b1b4-efe1671c5836`.\n */\nexport function generateUuid(): string {\n return globalThis.crypto.randomUUID();\n}\n"]}
1
+ {"version":3,"file":"generateUuid.js","sourceRoot":"","sources":["../../../../src/utils/uuid/generateUuid.ts"],"names":[],"mappings":";;AAcA,oCAeC;AAGD,oEASC;AAzCD;;;;;;;;;;;;;GAaG;AACH,SAAgB,YAAY;IAC1B,8EAA8E;IAC9E,6EAA6E;IAC7E,iEAAiE;IACjE,MAAM,SAAS,GAAG,UAAU,CAAC,MAA4B,CAAC;IAE1D,IAAI,OAAO,CAAA,SAAS,aAAT,SAAS,uBAAT,SAAS,CAAE,UAAU,CAAA,KAAK,UAAU,EAAE,CAAC;QAChD,OAAO,SAAS,CAAC,UAAU,EAAE,CAAC;IAChC,CAAC;IAED,IAAI,OAAO,CAAA,SAAS,aAAT,SAAS,uBAAT,SAAS,CAAE,eAAe,CAAA,KAAK,UAAU,EAAE,CAAC;QACrD,MAAM,IAAI,KAAK,CAAC,+DAA+D,CAAC,CAAC;IACnF,CAAC;IAED,OAAO,4BAA4B,CAAC,SAAS,CAAC,CAAC;AACjD,CAAC;AAED,wFAAwF;AACxF,SAAgB,4BAA4B,CAAC,SAA0C;IACrF,MAAM,KAAK,GAAG,SAAS,CAAC,eAAe,CAAC,IAAI,UAAU,CAAC,EAAE,CAAC,CAAC,CAAC;IAE5D,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,GAAG,IAAI,CAAC;IACpC,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,GAAG,IAAI,CAAC;IAEpC,MAAM,GAAG,GAAG,KAAK,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IAErF,OAAO,GAAG,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,IAAI,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,GAAG,CAAC,KAAK,CAAC,EAAE,EAAE,EAAE,CAAC,IAAI,GAAG,CAAC,KAAK,CAAC,EAAE,EAAE,EAAE,CAAC,IAAI,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC,EAAE,CAAC;AAC7G,CAAC","sourcesContent":["/**\n * Generates a new random (version 4) UUID.\n *\n * This is the single place in quidproquo to mint a v4 UUID, so it works across\n * both backend and frontend packages with no external dependency.\n *\n * It prefers the native `crypto.randomUUID()` (Node 20+, and browsers in a\n * SECURE context: https or localhost). Browsers deliberately hide that method on\n * plain-http origins, which is exactly where a self-hosted app on a LAN ip lives\n * (e.g. http://192.168.x.x), so it falls back to assembling the same v4 layout\n * from `crypto.getRandomValues()`, which is available everywhere.\n *\n * @returns {string} A randomly generated v4 UUID, e.g. `109156be-c4fb-41ea-b1b4-efe1671c5836`.\n */\nexport function generateUuid(): string {\n // lib.dom types the global as always present; older runtimes (Node before 19,\n // some workers) don't expose it, so treat it as optional and fail loudly. No\n // Math.random() last resort: a guessable id is worse than no id.\n const cryptoApi = globalThis.crypto as Crypto | undefined;\n\n if (typeof cryptoApi?.randomUUID === 'function') {\n return cryptoApi.randomUUID();\n }\n\n if (typeof cryptoApi?.getRandomValues !== 'function') {\n throw new Error('generateUuid: no Web Crypto API available in this environment');\n }\n\n return generateUuidFromRandomValues(cryptoApi);\n}\n\n// RFC 4122 v4 from 16 random bytes: version nibble 4 in byte 6, variant 10xx in byte 8.\nexport function generateUuidFromRandomValues(cryptoApi: Pick<Crypto, 'getRandomValues'>): string {\n const bytes = cryptoApi.getRandomValues(new Uint8Array(16));\n\n bytes[6] = (bytes[6] & 0x0f) | 0x40;\n bytes[8] = (bytes[8] & 0x3f) | 0x80;\n\n const hex = Array.from(bytes, (byte) => byte.toString(16).padStart(2, '0')).join('');\n\n return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;\n}\n"]}
@@ -1,12 +1,16 @@
1
1
  /**
2
2
  * Generates a new random (version 4) UUID.
3
3
  *
4
- * This is the single place in quidproquo to mint a v4 UUID. It uses the native
5
- * `crypto.randomUUID()` which is available in Node 20+ and all modern browsers
6
- * (secure contexts), so it works across both backend and frontend packages with
7
- * no external dependency. Centralising it here means the underlying source can be
8
- * swapped in one spot if that ever changes.
4
+ * This is the single place in quidproquo to mint a v4 UUID, so it works across
5
+ * both backend and frontend packages with no external dependency.
6
+ *
7
+ * It prefers the native `crypto.randomUUID()` (Node 20+, and browsers in a
8
+ * SECURE context: https or localhost). Browsers deliberately hide that method on
9
+ * plain-http origins, which is exactly where a self-hosted app on a LAN ip lives
10
+ * (e.g. http://192.168.x.x), so it falls back to assembling the same v4 layout
11
+ * from `crypto.getRandomValues()`, which is available everywhere.
9
12
  *
10
13
  * @returns {string} A randomly generated v4 UUID, e.g. `109156be-c4fb-41ea-b1b4-efe1671c5836`.
11
14
  */
12
15
  export declare function generateUuid(): string;
16
+ export declare function generateUuidFromRandomValues(cryptoApi: Pick<Crypto, 'getRandomValues'>): string;
@@ -1,15 +1,36 @@
1
1
  /**
2
2
  * Generates a new random (version 4) UUID.
3
3
  *
4
- * This is the single place in quidproquo to mint a v4 UUID. It uses the native
5
- * `crypto.randomUUID()` which is available in Node 20+ and all modern browsers
6
- * (secure contexts), so it works across both backend and frontend packages with
7
- * no external dependency. Centralising it here means the underlying source can be
8
- * swapped in one spot if that ever changes.
4
+ * This is the single place in quidproquo to mint a v4 UUID, so it works across
5
+ * both backend and frontend packages with no external dependency.
6
+ *
7
+ * It prefers the native `crypto.randomUUID()` (Node 20+, and browsers in a
8
+ * SECURE context: https or localhost). Browsers deliberately hide that method on
9
+ * plain-http origins, which is exactly where a self-hosted app on a LAN ip lives
10
+ * (e.g. http://192.168.x.x), so it falls back to assembling the same v4 layout
11
+ * from `crypto.getRandomValues()`, which is available everywhere.
9
12
  *
10
13
  * @returns {string} A randomly generated v4 UUID, e.g. `109156be-c4fb-41ea-b1b4-efe1671c5836`.
11
14
  */
12
15
  export function generateUuid() {
13
- return globalThis.crypto.randomUUID();
16
+ // lib.dom types the global as always present; older runtimes (Node before 19,
17
+ // some workers) don't expose it, so treat it as optional and fail loudly. No
18
+ // Math.random() last resort: a guessable id is worse than no id.
19
+ const cryptoApi = globalThis.crypto;
20
+ if (typeof cryptoApi?.randomUUID === 'function') {
21
+ return cryptoApi.randomUUID();
22
+ }
23
+ if (typeof cryptoApi?.getRandomValues !== 'function') {
24
+ throw new Error('generateUuid: no Web Crypto API available in this environment');
25
+ }
26
+ return generateUuidFromRandomValues(cryptoApi);
27
+ }
28
+ // RFC 4122 v4 from 16 random bytes: version nibble 4 in byte 6, variant 10xx in byte 8.
29
+ export function generateUuidFromRandomValues(cryptoApi) {
30
+ const bytes = cryptoApi.getRandomValues(new Uint8Array(16));
31
+ bytes[6] = (bytes[6] & 0x0f) | 0x40;
32
+ bytes[8] = (bytes[8] & 0x3f) | 0x80;
33
+ const hex = Array.from(bytes, (byte) => byte.toString(16).padStart(2, '0')).join('');
34
+ return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
14
35
  }
15
36
  //# sourceMappingURL=generateUuid.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"generateUuid.js","sourceRoot":"","sources":["../../../../src/utils/uuid/generateUuid.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,MAAM,UAAU,YAAY;IAC1B,OAAO,UAAU,CAAC,MAAM,CAAC,UAAU,EAAE,CAAC;AACxC,CAAC","sourcesContent":["/**\n * Generates a new random (version 4) UUID.\n *\n * This is the single place in quidproquo to mint a v4 UUID. It uses the native\n * `crypto.randomUUID()` which is available in Node 20+ and all modern browsers\n * (secure contexts), so it works across both backend and frontend packages with\n * no external dependency. Centralising it here means the underlying source can be\n * swapped in one spot if that ever changes.\n *\n * @returns {string} A randomly generated v4 UUID, e.g. `109156be-c4fb-41ea-b1b4-efe1671c5836`.\n */\nexport function generateUuid(): string {\n return globalThis.crypto.randomUUID();\n}\n"]}
1
+ {"version":3,"file":"generateUuid.js","sourceRoot":"","sources":["../../../../src/utils/uuid/generateUuid.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,YAAY;IAC1B,8EAA8E;IAC9E,6EAA6E;IAC7E,iEAAiE;IACjE,MAAM,SAAS,GAAG,UAAU,CAAC,MAA4B,CAAC;IAE1D,IAAI,OAAO,SAAS,EAAE,UAAU,KAAK,UAAU,EAAE,CAAC;QAChD,OAAO,SAAS,CAAC,UAAU,EAAE,CAAC;IAChC,CAAC;IAED,IAAI,OAAO,SAAS,EAAE,eAAe,KAAK,UAAU,EAAE,CAAC;QACrD,MAAM,IAAI,KAAK,CAAC,+DAA+D,CAAC,CAAC;IACnF,CAAC;IAED,OAAO,4BAA4B,CAAC,SAAS,CAAC,CAAC;AACjD,CAAC;AAED,wFAAwF;AACxF,MAAM,UAAU,4BAA4B,CAAC,SAA0C;IACrF,MAAM,KAAK,GAAG,SAAS,CAAC,eAAe,CAAC,IAAI,UAAU,CAAC,EAAE,CAAC,CAAC,CAAC;IAE5D,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,GAAG,IAAI,CAAC;IACpC,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,GAAG,IAAI,CAAC;IAEpC,MAAM,GAAG,GAAG,KAAK,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IAErF,OAAO,GAAG,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,IAAI,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,GAAG,CAAC,KAAK,CAAC,EAAE,EAAE,EAAE,CAAC,IAAI,GAAG,CAAC,KAAK,CAAC,EAAE,EAAE,EAAE,CAAC,IAAI,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC,EAAE,CAAC;AAC7G,CAAC","sourcesContent":["/**\n * Generates a new random (version 4) UUID.\n *\n * This is the single place in quidproquo to mint a v4 UUID, so it works across\n * both backend and frontend packages with no external dependency.\n *\n * It prefers the native `crypto.randomUUID()` (Node 20+, and browsers in a\n * SECURE context: https or localhost). Browsers deliberately hide that method on\n * plain-http origins, which is exactly where a self-hosted app on a LAN ip lives\n * (e.g. http://192.168.x.x), so it falls back to assembling the same v4 layout\n * from `crypto.getRandomValues()`, which is available everywhere.\n *\n * @returns {string} A randomly generated v4 UUID, e.g. `109156be-c4fb-41ea-b1b4-efe1671c5836`.\n */\nexport function generateUuid(): string {\n // lib.dom types the global as always present; older runtimes (Node before 19,\n // some workers) don't expose it, so treat it as optional and fail loudly. No\n // Math.random() last resort: a guessable id is worse than no id.\n const cryptoApi = globalThis.crypto as Crypto | undefined;\n\n if (typeof cryptoApi?.randomUUID === 'function') {\n return cryptoApi.randomUUID();\n }\n\n if (typeof cryptoApi?.getRandomValues !== 'function') {\n throw new Error('generateUuid: no Web Crypto API available in this environment');\n }\n\n return generateUuidFromRandomValues(cryptoApi);\n}\n\n// RFC 4122 v4 from 16 random bytes: version nibble 4 in byte 6, variant 10xx in byte 8.\nexport function generateUuidFromRandomValues(cryptoApi: Pick<Crypto, 'getRandomValues'>): string {\n const bytes = cryptoApi.getRandomValues(new Uint8Array(16));\n\n bytes[6] = (bytes[6] & 0x0f) | 0x40;\n bytes[8] = (bytes[8] & 0x3f) | 0x80;\n\n const hex = Array.from(bytes, (byte) => byte.toString(16).padStart(2, '0')).join('');\n\n return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "quidproquo-core",
3
- "version": "0.1.28",
3
+ "version": "0.1.29",
4
4
  "description": "",
5
5
  "main": "./lib/commonjs/index.js",
6
6
  "module": "./lib/esm/index.js",
@@ -36,7 +36,7 @@
36
36
  "homepage": "https://github.com/qpqjs/quidproquo#readme",
37
37
  "devDependencies": {
38
38
  "@types/node": "^24.2.1",
39
- "quidproquo-tsconfig": "0.1.28",
39
+ "quidproquo-tsconfig": "0.1.29",
40
40
  "typescript": "^5.8.2"
41
41
  },
42
42
  "dependencies": {