@drawbridge/drawbridge-utils 0.0.7 → 0.0.8

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.
@@ -0,0 +1,53 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // nanoid.js
20
+ var nanoid_exports = {};
21
+ __export(nanoid_exports, {
22
+ nanoid: () => nanoid,
23
+ retry: () => retry
24
+ });
25
+ module.exports = __toCommonJS(nanoid_exports);
26
+ var import_nanoid = require("nanoid");
27
+ var nanoid = (0, import_nanoid.customAlphabet)("0123456789abcdefghijklmnopqrstuvwxyz", 8);
28
+ var DEFAULT_MAX_RETRIES = 5;
29
+ var retry = async ({
30
+ field,
31
+ operation,
32
+ maxRetries = DEFAULT_MAX_RETRIES
33
+ }) => {
34
+ var _a;
35
+ for (let attempt = 0; attempt < maxRetries; attempt++) {
36
+ try {
37
+ return await operation(nanoid());
38
+ } catch (error) {
39
+ const isCollision = (error == null ? void 0 : error.code) === 11e3 && ((_a = error == null ? void 0 : error.keyPattern) == null ? void 0 : _a[field]);
40
+ if (!isCollision || attempt === maxRetries - 1) {
41
+ throw error;
42
+ }
43
+ ;
44
+ }
45
+ ;
46
+ }
47
+ ;
48
+ };
49
+ // Annotate the CommonJS export names for ESM import in node:
50
+ 0 && (module.exports = {
51
+ nanoid,
52
+ retry
53
+ });
@@ -0,0 +1,63 @@
1
+ import { customAlphabet } from 'nanoid';
2
+
3
+ // 8-char base36 nanoid (36⁸ ≈ 2.8T entropy). Used as the short-id / slug
4
+ // generator across drawbridge collections with unique indexes (page.shortId,
5
+ // organization.shortId, affiliate.shortId, link.slug, prize.slug,
6
+ // advertisement.slug, etc.).
7
+ //
8
+ // Also exported from the main entrypoint for back-compat:
9
+ // const { nanoid } = require( '@drawbridge/drawbridge-utils' );
10
+ //
11
+ // New code should prefer this subpath, which also exposes `retry`:
12
+ // const { nanoid, retry } = require( '@drawbridge/drawbridge-utils/nanoid' );
13
+
14
+ const nanoid = customAlphabet( '0123456789abcdefghijklmnopqrstuvwxyz', 8 );
15
+
16
+ // Retry an async operation when Mongo rejects an insert because of a
17
+ // duplicate-key error on a specific field. The operation receives a fresh
18
+ // nanoid() on each attempt — typically used for shortId / slug fields where
19
+ // a unique index is in place and the value is randomly generated.
20
+ //
21
+ // const result = await retry({
22
+ // field : 'shortId',
23
+ // operation : ( shortId ) => controller.create({
24
+ // collection : 'page',
25
+ // data : { ..., shortId }
26
+ // })
27
+ // });
28
+ //
29
+ // Throws immediately if the error is anything other than E11000 on the named
30
+ // field. Throws the last error if retries are exhausted (extremely unlikely
31
+ // at 36⁸ entropy — 5 consecutive collisions are astronomically improbable).
32
+
33
+ const DEFAULT_MAX_RETRIES = 5;
34
+
35
+ const retry = async ({
36
+ field,
37
+ operation,
38
+ maxRetries = DEFAULT_MAX_RETRIES
39
+ }) => {
40
+
41
+ for( let attempt = 0; attempt < maxRetries; attempt++ ){
42
+
43
+ try {
44
+
45
+ return await operation( nanoid() );
46
+
47
+ } catch ( error ){
48
+
49
+ const isCollision = (
50
+ error?.code === 11000 &&
51
+ error?.keyPattern?.[ field ]
52
+ );
53
+
54
+ if( ! isCollision || attempt === maxRetries - 1 ){
55
+
56
+ throw error;
57
+
58
+ }
59
+ }
60
+ }
61
+ };
62
+
63
+ export { nanoid, retry };
@@ -0,0 +1,63 @@
1
+ import { customAlphabet } from 'nanoid';
2
+
3
+ // 8-char base36 nanoid (36⁸ ≈ 2.8T entropy). Used as the short-id / slug
4
+ // generator across drawbridge collections with unique indexes (page.shortId,
5
+ // organization.shortId, affiliate.shortId, link.slug, prize.slug,
6
+ // advertisement.slug, etc.).
7
+ //
8
+ // Also exported from the main entrypoint for back-compat:
9
+ // const { nanoid } = require( '@drawbridge/drawbridge-utils' );
10
+ //
11
+ // New code should prefer this subpath, which also exposes `retry`:
12
+ // const { nanoid, retry } = require( '@drawbridge/drawbridge-utils/nanoid' );
13
+
14
+ const nanoid = customAlphabet( '0123456789abcdefghijklmnopqrstuvwxyz', 8 );
15
+
16
+ // Retry an async operation when Mongo rejects an insert because of a
17
+ // duplicate-key error on a specific field. The operation receives a fresh
18
+ // nanoid() on each attempt — typically used for shortId / slug fields where
19
+ // a unique index is in place and the value is randomly generated.
20
+ //
21
+ // const result = await retry({
22
+ // field : 'shortId',
23
+ // operation : ( shortId ) => controller.create({
24
+ // collection : 'page',
25
+ // data : { ..., shortId }
26
+ // })
27
+ // });
28
+ //
29
+ // Throws immediately if the error is anything other than E11000 on the named
30
+ // field. Throws the last error if retries are exhausted (extremely unlikely
31
+ // at 36⁸ entropy — 5 consecutive collisions are astronomically improbable).
32
+
33
+ const DEFAULT_MAX_RETRIES = 5;
34
+
35
+ const retry = async ({
36
+ field,
37
+ operation,
38
+ maxRetries = DEFAULT_MAX_RETRIES
39
+ }) => {
40
+
41
+ for( let attempt = 0; attempt < maxRetries; attempt++ ){
42
+
43
+ try {
44
+
45
+ return await operation( nanoid() );
46
+
47
+ } catch ( error ){
48
+
49
+ const isCollision = (
50
+ error?.code === 11000 &&
51
+ error?.keyPattern?.[ field ]
52
+ );
53
+
54
+ if( ! isCollision || attempt === maxRetries - 1 ){
55
+
56
+ throw error;
57
+
58
+ }
59
+ }
60
+ }
61
+ };
62
+
63
+ export { nanoid, retry };
package/dist/nanoid.js ADDED
@@ -0,0 +1,28 @@
1
+ // nanoid.js
2
+ import { customAlphabet } from "nanoid";
3
+ var nanoid = customAlphabet("0123456789abcdefghijklmnopqrstuvwxyz", 8);
4
+ var DEFAULT_MAX_RETRIES = 5;
5
+ var retry = async ({
6
+ field,
7
+ operation,
8
+ maxRetries = DEFAULT_MAX_RETRIES
9
+ }) => {
10
+ var _a;
11
+ for (let attempt = 0; attempt < maxRetries; attempt++) {
12
+ try {
13
+ return await operation(nanoid());
14
+ } catch (error) {
15
+ const isCollision = (error == null ? void 0 : error.code) === 11e3 && ((_a = error == null ? void 0 : error.keyPattern) == null ? void 0 : _a[field]);
16
+ if (!isCollision || attempt === maxRetries - 1) {
17
+ throw error;
18
+ }
19
+ ;
20
+ }
21
+ ;
22
+ }
23
+ ;
24
+ };
25
+ export {
26
+ nanoid,
27
+ retry
28
+ };
package/package.json CHANGED
@@ -39,6 +39,11 @@
39
39
  "types": "./dist/upload.d.ts",
40
40
  "import": "./dist/upload.js",
41
41
  "require": "./dist/upload.cjs"
42
+ },
43
+ "./nanoid": {
44
+ "types": "./dist/nanoid.d.ts",
45
+ "import": "./dist/nanoid.js",
46
+ "require": "./dist/nanoid.cjs"
42
47
  }
43
48
  },
44
49
  "files": [
@@ -55,5 +60,5 @@
55
60
  "build": "tsup && npm publish"
56
61
  },
57
62
  "types": "dist/index.d.ts",
58
- "version": "0.0.7"
63
+ "version": "0.0.8"
59
64
  }