@drawbridge/drawbridge-utils 0.0.6 → 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
+ };
@@ -0,0 +1,65 @@
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
+ // upload.js
20
+ var upload_exports = {};
21
+ __export(upload_exports, {
22
+ allowedMimes: () => allowedMimes,
23
+ allowedUploadTypes: () => allowedUploadTypes,
24
+ isAllowedMime: () => isAllowedMime,
25
+ resolveUploadType: () => resolveUploadType
26
+ });
27
+ module.exports = __toCommonJS(upload_exports);
28
+ var allowedUploadTypes = {
29
+ application: {
30
+ pdf: "application/pdf"
31
+ },
32
+ image: {
33
+ jpeg: "image/jpeg",
34
+ jpg: "image/jpeg",
35
+ png: "image/png",
36
+ webp: "image/webp"
37
+ },
38
+ video: {
39
+ mp4: "video/mp4"
40
+ }
41
+ };
42
+ var allowedMimes = new Set(
43
+ Object.values(allowedUploadTypes).flatMap(
44
+ (extensions) => Object.values(extensions)
45
+ )
46
+ );
47
+ var resolveUploadType = (type, extension) => {
48
+ var _a;
49
+ const mime = (_a = allowedUploadTypes == null ? void 0 : allowedUploadTypes[type]) == null ? void 0 : _a[extension];
50
+ if (!mime) {
51
+ const error = new Error("Unsupported upload type or extension");
52
+ error.status = 400;
53
+ throw error;
54
+ }
55
+ ;
56
+ return mime;
57
+ };
58
+ var isAllowedMime = (mime) => allowedMimes.has(mime);
59
+ // Annotate the CommonJS export names for ESM import in node:
60
+ 0 && (module.exports = {
61
+ allowedMimes,
62
+ allowedUploadTypes,
63
+ isAllowedMime,
64
+ resolveUploadType
65
+ });
@@ -0,0 +1,58 @@
1
+ // Source-of-truth allowlist for file upload types accepted across drawbridge.
2
+ //
3
+ // Two consumer shapes:
4
+ //
5
+ // 1. The API needs `(type, extension) → mime` when serving an upload route
6
+ // where the URL carries those params. Use `resolveUploadType( type, extension )`.
7
+ // Throws { status: 400 } on anything not in the allowlist.
8
+ //
9
+ // 2. The sync worker downstream needs to validate that a `mimetype` flowing
10
+ // from the DB / job payload is one of the canonical values before passing
11
+ // it to S3 as a ContentType header. Use `isAllowedMime( mime )`.
12
+ //
13
+ // Adding a new accepted upload format means updating `allowedUploadTypes` here
14
+ // AND publishing a new utils version. Both api and sync inherit the change.
15
+ //
16
+ // Why both helpers and not just one: api routes know (type, extension) but not
17
+ // the canonical mime; sync workers have the mime but not the original type tuple.
18
+
19
+ const allowedUploadTypes = {
20
+ application : {
21
+ pdf : 'application/pdf'
22
+ },
23
+ image : {
24
+ jpeg : 'image/jpeg',
25
+ jpg : 'image/jpeg',
26
+ png : 'image/png',
27
+ webp : 'image/webp'
28
+ },
29
+ video : {
30
+ mp4 : 'video/mp4'
31
+ }
32
+ };
33
+
34
+ const allowedMimes = new Set(
35
+ Object.values( allowedUploadTypes ).flatMap(
36
+ ( extensions ) => Object.values( extensions )
37
+ )
38
+ );
39
+
40
+ const resolveUploadType = ( type, extension ) => {
41
+
42
+ const mime = allowedUploadTypes?.[ type ]?.[ extension ];
43
+
44
+ if( ! mime ){
45
+
46
+ const error = new Error( 'Unsupported upload type or extension' );
47
+ error.status = 400;
48
+
49
+ throw error;
50
+
51
+ }
52
+ return mime;
53
+
54
+ };
55
+
56
+ const isAllowedMime = ( mime ) => allowedMimes.has( mime );
57
+
58
+ export { allowedMimes, allowedUploadTypes, isAllowedMime, resolveUploadType };
@@ -0,0 +1,58 @@
1
+ // Source-of-truth allowlist for file upload types accepted across drawbridge.
2
+ //
3
+ // Two consumer shapes:
4
+ //
5
+ // 1. The API needs `(type, extension) → mime` when serving an upload route
6
+ // where the URL carries those params. Use `resolveUploadType( type, extension )`.
7
+ // Throws { status: 400 } on anything not in the allowlist.
8
+ //
9
+ // 2. The sync worker downstream needs to validate that a `mimetype` flowing
10
+ // from the DB / job payload is one of the canonical values before passing
11
+ // it to S3 as a ContentType header. Use `isAllowedMime( mime )`.
12
+ //
13
+ // Adding a new accepted upload format means updating `allowedUploadTypes` here
14
+ // AND publishing a new utils version. Both api and sync inherit the change.
15
+ //
16
+ // Why both helpers and not just one: api routes know (type, extension) but not
17
+ // the canonical mime; sync workers have the mime but not the original type tuple.
18
+
19
+ const allowedUploadTypes = {
20
+ application : {
21
+ pdf : 'application/pdf'
22
+ },
23
+ image : {
24
+ jpeg : 'image/jpeg',
25
+ jpg : 'image/jpeg',
26
+ png : 'image/png',
27
+ webp : 'image/webp'
28
+ },
29
+ video : {
30
+ mp4 : 'video/mp4'
31
+ }
32
+ };
33
+
34
+ const allowedMimes = new Set(
35
+ Object.values( allowedUploadTypes ).flatMap(
36
+ ( extensions ) => Object.values( extensions )
37
+ )
38
+ );
39
+
40
+ const resolveUploadType = ( type, extension ) => {
41
+
42
+ const mime = allowedUploadTypes?.[ type ]?.[ extension ];
43
+
44
+ if( ! mime ){
45
+
46
+ const error = new Error( 'Unsupported upload type or extension' );
47
+ error.status = 400;
48
+
49
+ throw error;
50
+
51
+ }
52
+ return mime;
53
+
54
+ };
55
+
56
+ const isAllowedMime = ( mime ) => allowedMimes.has( mime );
57
+
58
+ export { allowedMimes, allowedUploadTypes, isAllowedMime, resolveUploadType };
package/dist/upload.js ADDED
@@ -0,0 +1,38 @@
1
+ // upload.js
2
+ var allowedUploadTypes = {
3
+ application: {
4
+ pdf: "application/pdf"
5
+ },
6
+ image: {
7
+ jpeg: "image/jpeg",
8
+ jpg: "image/jpeg",
9
+ png: "image/png",
10
+ webp: "image/webp"
11
+ },
12
+ video: {
13
+ mp4: "video/mp4"
14
+ }
15
+ };
16
+ var allowedMimes = new Set(
17
+ Object.values(allowedUploadTypes).flatMap(
18
+ (extensions) => Object.values(extensions)
19
+ )
20
+ );
21
+ var resolveUploadType = (type, extension) => {
22
+ var _a;
23
+ const mime = (_a = allowedUploadTypes == null ? void 0 : allowedUploadTypes[type]) == null ? void 0 : _a[extension];
24
+ if (!mime) {
25
+ const error = new Error("Unsupported upload type or extension");
26
+ error.status = 400;
27
+ throw error;
28
+ }
29
+ ;
30
+ return mime;
31
+ };
32
+ var isAllowedMime = (mime) => allowedMimes.has(mime);
33
+ export {
34
+ allowedMimes,
35
+ allowedUploadTypes,
36
+ isAllowedMime,
37
+ resolveUploadType
38
+ };
package/package.json CHANGED
@@ -34,6 +34,16 @@
34
34
  "types": "./dist/circuit.d.ts",
35
35
  "import": "./dist/circuit.js",
36
36
  "require": "./dist/circuit.cjs"
37
+ },
38
+ "./upload": {
39
+ "types": "./dist/upload.d.ts",
40
+ "import": "./dist/upload.js",
41
+ "require": "./dist/upload.cjs"
42
+ },
43
+ "./nanoid": {
44
+ "types": "./dist/nanoid.d.ts",
45
+ "import": "./dist/nanoid.js",
46
+ "require": "./dist/nanoid.cjs"
37
47
  }
38
48
  },
39
49
  "files": [
@@ -50,5 +60,5 @@
50
60
  "build": "tsup && npm publish"
51
61
  },
52
62
  "types": "dist/index.d.ts",
53
- "version": "0.0.6"
63
+ "version": "0.0.8"
54
64
  }