@rtorcato/api-upload 0.2.0
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/LICENSE +21 -0
- package/README.md +53 -0
- package/dist/index.d.ts +57 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +64 -0
- package/dist/index.js.map +1 -0
- package/package.json +71 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Richard Torcato
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# @rtorcato/api-upload
|
|
2
|
+
|
|
3
|
+
Promise-based S3 file upload for Express via [multer-s3](https://github.com/anacronw/multer-s3) — public/private ACL, cache-control, deterministic keys, and errors normalized to [`@rtorcato/api-errors`](https://github.com/rtorcato/api-common/tree/main/packages/api-errors).
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
pnpm add @rtorcato/api-upload @aws-sdk/client-s3 multer multer-s3 express
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
`@aws-sdk/client-s3`, `multer`, `multer-s3`, and `express` are peer dependencies — you bring your own versions and S3 client.
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { S3Client } from '@aws-sdk/client-s3'
|
|
17
|
+
import { uploader } from '@rtorcato/api-upload'
|
|
18
|
+
|
|
19
|
+
const s3 = new S3Client({ region: 'us-east-1' })
|
|
20
|
+
|
|
21
|
+
app.post('/avatar', async (req, res, next) => {
|
|
22
|
+
try {
|
|
23
|
+
const file = await uploader(req, res, {
|
|
24
|
+
s3,
|
|
25
|
+
bucket: 'avatars',
|
|
26
|
+
field: 'avatar', // multipart form field
|
|
27
|
+
key: `users/${req.user.id}.png`,
|
|
28
|
+
isPublic: true, // public-read ACL (default: private)
|
|
29
|
+
maxSizeBytes: 5 * 1024 * 1024,
|
|
30
|
+
})
|
|
31
|
+
res.json({ url: file.location })
|
|
32
|
+
} catch (err) {
|
|
33
|
+
next(err) // 413 / 400 HttpError → your error handler
|
|
34
|
+
}
|
|
35
|
+
})
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
`key` can also be a function `(req, file) => string` for deterministic keys derived from the request.
|
|
39
|
+
|
|
40
|
+
## Errors
|
|
41
|
+
|
|
42
|
+
`uploader` rejects with an `HttpError` (from `@rtorcato/api-errors`) that drops straight into the `api-errors-express` handler:
|
|
43
|
+
|
|
44
|
+
- **`413 file_too_large`** — the request `Content-Length` exceeds `maxSizeBytes`. Checked **before** anything streams to S3.
|
|
45
|
+
- **`400 no_file`** — the field was empty.
|
|
46
|
+
- **`400`** — any other multer error (e.g. unexpected field).
|
|
47
|
+
|
|
48
|
+
## Notes
|
|
49
|
+
|
|
50
|
+
- **ACL:** `isPublic` sets `public-read` / `private`. Buckets with Object Ownership set to *bucket-owner-enforced* reject ACLs — leave `isPublic` unset and control access with a bucket policy instead.
|
|
51
|
+
- **Size limit:** enforced via `Content-Length` up front (which includes small multipart overhead) rather than multer's mid-stream limit — this avoids buffering oversized bodies and a multer-s3 hang when aborting an in-flight upload.
|
|
52
|
+
|
|
53
|
+
Source: https://github.com/rtorcato/api-common/tree/main/packages/api-upload
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { S3Client } from '@aws-sdk/client-s3';
|
|
2
|
+
import type { Request, Response } from 'express';
|
|
3
|
+
/** A file uploaded to S3 — the multer file plus the fields multer-s3 adds. */
|
|
4
|
+
export interface UploadedFile {
|
|
5
|
+
fieldname: string;
|
|
6
|
+
originalname: string;
|
|
7
|
+
mimetype: string;
|
|
8
|
+
size: number;
|
|
9
|
+
/** Bucket the object was written to. */
|
|
10
|
+
bucket: string;
|
|
11
|
+
/** Object key. */
|
|
12
|
+
key: string;
|
|
13
|
+
/** Public URL / S3 location of the object. */
|
|
14
|
+
location: string;
|
|
15
|
+
etag: string;
|
|
16
|
+
contentType: string;
|
|
17
|
+
}
|
|
18
|
+
export interface UploadOptions {
|
|
19
|
+
/** S3 client the object is written with (you own its config/credentials). */
|
|
20
|
+
s3: S3Client;
|
|
21
|
+
/** Destination bucket. */
|
|
22
|
+
bucket: string;
|
|
23
|
+
/** Multipart form field holding the file. */
|
|
24
|
+
field: string;
|
|
25
|
+
/** Object key — a string, or a function of the request + file for deterministic keys. */
|
|
26
|
+
key: string | ((req: Request, file: Express.Multer.File) => string);
|
|
27
|
+
/** `public-read` ACL when true, otherwise `private`. Default: `false`. */
|
|
28
|
+
isPublic?: boolean;
|
|
29
|
+
/** `Cache-Control` stored on the object. Default: `max-age=31536000` (1 year). */
|
|
30
|
+
cacheControl?: string;
|
|
31
|
+
/** Extra object metadata. */
|
|
32
|
+
metadata?: Record<string, string>;
|
|
33
|
+
/** Max request size in bytes (checked against `Content-Length`); exceeding it rejects with a `413` before anything streams to S3. */
|
|
34
|
+
maxSizeBytes?: number;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Upload a single file from a multipart request straight to S3, resolving with the
|
|
38
|
+
* stored object's details.
|
|
39
|
+
*
|
|
40
|
+
* Rejects with an `HttpError` (from `@rtorcato/api-errors`): `413 file_too_large`
|
|
41
|
+
* when `maxSizeBytes` is exceeded, `400 no_file` when the field is empty, and a
|
|
42
|
+
* `400` for any other multer error — so it slots into the error-handler middleware.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```ts
|
|
46
|
+
* app.post('/avatar', async (req, res, next) => {
|
|
47
|
+
* try {
|
|
48
|
+
* const file = await uploader(req, res, {
|
|
49
|
+
* s3, bucket: 'avatars', field: 'avatar', key: `users/${req.user.id}.png`, isPublic: true,
|
|
50
|
+
* })
|
|
51
|
+
* res.json({ url: file.location })
|
|
52
|
+
* } catch (err) { next(err) }
|
|
53
|
+
* })
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
export declare function uploader(req: Request, res: Response, options: UploadOptions): Promise<UploadedFile>;
|
|
57
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAA;AAElD,OAAO,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAA;AAIhD,8EAA8E;AAC9E,MAAM,WAAW,YAAY;IAC5B,SAAS,EAAE,MAAM,CAAA;IACjB,YAAY,EAAE,MAAM,CAAA;IACpB,QAAQ,EAAE,MAAM,CAAA;IAChB,IAAI,EAAE,MAAM,CAAA;IACZ,wCAAwC;IACxC,MAAM,EAAE,MAAM,CAAA;IACd,kBAAkB;IAClB,GAAG,EAAE,MAAM,CAAA;IACX,8CAA8C;IAC9C,QAAQ,EAAE,MAAM,CAAA;IAChB,IAAI,EAAE,MAAM,CAAA;IACZ,WAAW,EAAE,MAAM,CAAA;CACnB;AAED,MAAM,WAAW,aAAa;IAC7B,6EAA6E;IAC7E,EAAE,EAAE,QAAQ,CAAA;IACZ,0BAA0B;IAC1B,MAAM,EAAE,MAAM,CAAA;IACd,6CAA6C;IAC7C,KAAK,EAAE,MAAM,CAAA;IACb,yFAAyF;IACzF,GAAG,EAAE,MAAM,GAAG,CAAC,CAAC,GAAG,EAAE,OAAO,EAAE,IAAI,EAAE,OAAO,CAAC,MAAM,CAAC,IAAI,KAAK,MAAM,CAAC,CAAA;IACnE,0EAA0E;IAC1E,QAAQ,CAAC,EAAE,OAAO,CAAA;IAClB,kFAAkF;IAClF,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB,6BAA6B;IAC7B,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IACjC,qIAAqI;IACrI,YAAY,CAAC,EAAE,MAAM,CAAA;CACrB;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAsB,QAAQ,CAC7B,GAAG,EAAE,OAAO,EACZ,GAAG,EAAE,QAAQ,EACb,OAAO,EAAE,aAAa,GACpB,OAAO,CAAC,YAAY,CAAC,CA0CvB"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { BadRequestError, HttpError } from '@rtorcato/api-errors';
|
|
2
|
+
import multer from 'multer';
|
|
3
|
+
import multerS3 from 'multer-s3';
|
|
4
|
+
/**
|
|
5
|
+
* Upload a single file from a multipart request straight to S3, resolving with the
|
|
6
|
+
* stored object's details.
|
|
7
|
+
*
|
|
8
|
+
* Rejects with an `HttpError` (from `@rtorcato/api-errors`): `413 file_too_large`
|
|
9
|
+
* when `maxSizeBytes` is exceeded, `400 no_file` when the field is empty, and a
|
|
10
|
+
* `400` for any other multer error — so it slots into the error-handler middleware.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* app.post('/avatar', async (req, res, next) => {
|
|
15
|
+
* try {
|
|
16
|
+
* const file = await uploader(req, res, {
|
|
17
|
+
* s3, bucket: 'avatars', field: 'avatar', key: `users/${req.user.id}.png`, isPublic: true,
|
|
18
|
+
* })
|
|
19
|
+
* res.json({ url: file.location })
|
|
20
|
+
* } catch (err) { next(err) }
|
|
21
|
+
* })
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export async function uploader(req, res, options) {
|
|
25
|
+
// Reject oversized uploads up front via Content-Length — cheaper than
|
|
26
|
+
// buffering the body, and avoids a multer-s3 hang where aborting an in-flight
|
|
27
|
+
// upload on multer's own fileSize limit never settles the callback.
|
|
28
|
+
if (options.maxSizeBytes !== undefined) {
|
|
29
|
+
const contentLength = Number(req.headers['content-length']);
|
|
30
|
+
if (Number.isFinite(contentLength) && contentLength > options.maxSizeBytes) {
|
|
31
|
+
throw new HttpError(413, 'File too large', 'file_too_large');
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
const { key } = options;
|
|
35
|
+
const storage = multerS3({
|
|
36
|
+
s3: options.s3,
|
|
37
|
+
bucket: options.bucket,
|
|
38
|
+
acl: options.isPublic ? 'public-read' : 'private',
|
|
39
|
+
contentType: multerS3.AUTO_CONTENT_TYPE,
|
|
40
|
+
cacheControl: options.cacheControl ?? 'max-age=31536000',
|
|
41
|
+
metadata: (_req, _file, cb) => cb(null, options.metadata ?? {}),
|
|
42
|
+
key: typeof key === 'function'
|
|
43
|
+
? (r, file, cb) => cb(null, key(r, file))
|
|
44
|
+
: (_r, _file, cb) => cb(null, key),
|
|
45
|
+
});
|
|
46
|
+
const middleware = multer({ storage }).single(options.field);
|
|
47
|
+
try {
|
|
48
|
+
await new Promise((resolve, reject) => {
|
|
49
|
+
middleware(req, res, (err) => (err ? reject(err) : resolve()));
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
catch (err) {
|
|
53
|
+
// e.g. LIMIT_UNEXPECTED_FILE when the field name doesn't match.
|
|
54
|
+
if (err instanceof multer.MulterError) {
|
|
55
|
+
throw new BadRequestError(err.message, err.code.toLowerCase());
|
|
56
|
+
}
|
|
57
|
+
throw err;
|
|
58
|
+
}
|
|
59
|
+
const file = req.file;
|
|
60
|
+
if (!file)
|
|
61
|
+
throw new BadRequestError('No file uploaded', 'no_file');
|
|
62
|
+
return file;
|
|
63
|
+
}
|
|
64
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAA;AAEjE,OAAO,MAAM,MAAM,QAAQ,CAAA;AAC3B,OAAO,QAAQ,MAAM,WAAW,CAAA;AAqChC;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,CAAC,KAAK,UAAU,QAAQ,CAC7B,GAAY,EACZ,GAAa,EACb,OAAsB;IAEtB,sEAAsE;IACtE,8EAA8E;IAC9E,oEAAoE;IACpE,IAAI,OAAO,CAAC,YAAY,KAAK,SAAS,EAAE,CAAC;QACxC,MAAM,aAAa,GAAG,MAAM,CAAC,GAAG,CAAC,OAAO,CAAC,gBAAgB,CAAC,CAAC,CAAA;QAC3D,IAAI,MAAM,CAAC,QAAQ,CAAC,aAAa,CAAC,IAAI,aAAa,GAAG,OAAO,CAAC,YAAY,EAAE,CAAC;YAC5E,MAAM,IAAI,SAAS,CAAC,GAAG,EAAE,gBAAgB,EAAE,gBAAgB,CAAC,CAAA;QAC7D,CAAC;IACF,CAAC;IAED,MAAM,EAAE,GAAG,EAAE,GAAG,OAAO,CAAA;IACvB,M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IAAI,eAAe,CAAC,GAAG,CAAC,OAAO,EAAE,GAAG,CAAC,IAAI,CAAC,WAAW,EAAE,CAAC,CAAA;QAC/D,CAAC;QACD,MAAM,GAAG,CAAA;IACV,CAAC;IAED,MAAM,IAAI,GAAG,GAAG,CAAC,IAAgC,CAAA;IACjD,IAAI,CAAC,IAAI;QAAE,MAAM,IAAI,eAAe,CAAC,kBAAkB,EAAE,SAAS,CAAC,CAAA;IACnE,OAAO,IAAI,CAAA;AACZ,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@rtorcato/api-upload",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Promise-based S3 file upload for Express via multer-s3 — public/private ACL, cache-control, deterministic keys, typed errors.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": "Richard Torcato",
|
|
8
|
+
"main": "./dist/index.js",
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"import": "./dist/index.js"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
19
|
+
"engines": {
|
|
20
|
+
"node": ">=22"
|
|
21
|
+
},
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"@rtorcato/api-errors": "0.2.0"
|
|
24
|
+
},
|
|
25
|
+
"peerDependencies": {
|
|
26
|
+
"@aws-sdk/client-s3": "^3.0.0",
|
|
27
|
+
"express": "^4.18.0 || ^5.0.0",
|
|
28
|
+
"multer": "^2.0.0",
|
|
29
|
+
"multer-s3": "^3.0.0"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@aws-sdk/client-s3": "^3.700.0",
|
|
33
|
+
"aws-sdk-client-mock": "^4.1.0",
|
|
34
|
+
"@types/express": "^5.0.0",
|
|
35
|
+
"@types/multer": "^2.0.0",
|
|
36
|
+
"@types/supertest": "^7.2.0",
|
|
37
|
+
"express": "^5.1.0",
|
|
38
|
+
"multer": "^2.2.0",
|
|
39
|
+
"multer-s3": "^3.0.1",
|
|
40
|
+
"supertest": "^7.1.0",
|
|
41
|
+
"typescript": "~6.0.3"
|
|
42
|
+
},
|
|
43
|
+
"publishConfig": {
|
|
44
|
+
"access": "public"
|
|
45
|
+
},
|
|
46
|
+
"keywords": [
|
|
47
|
+
"api",
|
|
48
|
+
"nodejs",
|
|
49
|
+
"typescript",
|
|
50
|
+
"express",
|
|
51
|
+
"s3",
|
|
52
|
+
"upload",
|
|
53
|
+
"multer",
|
|
54
|
+
"multer-s3",
|
|
55
|
+
"aws"
|
|
56
|
+
],
|
|
57
|
+
"repository": {
|
|
58
|
+
"type": "git",
|
|
59
|
+
"url": "git+https://github.com/rtorcato/api-common.git",
|
|
60
|
+
"directory": "packages/api-upload"
|
|
61
|
+
},
|
|
62
|
+
"homepage": "https://github.com/rtorcato/api-common/tree/main/packages/api-upload#readme",
|
|
63
|
+
"bugs": {
|
|
64
|
+
"url": "https://github.com/rtorcato/api-common/issues"
|
|
65
|
+
},
|
|
66
|
+
"scripts": {
|
|
67
|
+
"build": "tsc -p tsconfig.build.json",
|
|
68
|
+
"typecheck": "tsc --noEmit",
|
|
69
|
+
"test": "vitest run"
|
|
70
|
+
}
|
|
71
|
+
}
|