@drawbridge/drawbridge-utils 0.0.6 → 0.0.7
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/dist/upload.cjs +65 -0
- package/dist/upload.d.cts +58 -0
- package/dist/upload.d.ts +58 -0
- package/dist/upload.js +38 -0
- package/package.json +6 -1
package/dist/upload.cjs
ADDED
|
@@ -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 };
|
package/dist/upload.d.ts
ADDED
|
@@ -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,11 @@
|
|
|
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"
|
|
37
42
|
}
|
|
38
43
|
},
|
|
39
44
|
"files": [
|
|
@@ -50,5 +55,5 @@
|
|
|
50
55
|
"build": "tsup && npm publish"
|
|
51
56
|
},
|
|
52
57
|
"types": "dist/index.d.ts",
|
|
53
|
-
"version": "0.0.
|
|
58
|
+
"version": "0.0.7"
|
|
54
59
|
}
|