naystack 1.5.11 → 1.5.13
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/auth/index.cjs.js +6 -5
- package/dist/auth/index.d.mts +1 -1
- package/dist/auth/index.d.ts +1 -1
- package/dist/auth/index.esm.js +5 -5
- package/dist/auth/instagram/client.cjs.js +4 -4
- package/dist/auth/instagram/client.d.mts +11 -9
- package/dist/auth/instagram/client.d.ts +11 -9
- package/dist/auth/instagram/client.esm.js +3 -3
- package/dist/auth/instagram/index.cjs.js +6 -5
- package/dist/auth/instagram/index.d.mts +3 -6
- package/dist/auth/instagram/index.d.ts +3 -6
- package/dist/auth/instagram/index.esm.js +5 -5
- package/dist/auth/instagram/route.cjs.js +2 -2
- package/dist/auth/instagram/route.d.mts +1 -1
- package/dist/auth/instagram/route.d.ts +1 -1
- package/dist/auth/instagram/route.esm.js +2 -2
- package/dist/auth/instagram/utils.cjs.js +3 -3
- package/dist/auth/instagram/utils.d.mts +2 -2
- package/dist/auth/instagram/utils.d.ts +2 -2
- package/dist/auth/instagram/utils.esm.js +2 -2
- package/dist/file/index.cjs.js +30 -16
- package/dist/file/index.d.mts +2 -0
- package/dist/file/index.d.ts +2 -0
- package/dist/file/index.esm.js +25 -15
- package/dist/file/put.cjs.js +18 -3
- package/dist/file/put.d.mts +1 -3
- package/dist/file/put.d.ts +1 -3
- package/dist/file/put.esm.js +18 -3
- package/dist/file/setup.cjs.js +13 -48
- package/dist/file/setup.d.mts +0 -7
- package/dist/file/setup.d.ts +0 -7
- package/dist/file/setup.esm.js +13 -48
- package/dist/file/utils.cjs.js +14 -8
- package/dist/file/utils.d.mts +22 -26
- package/dist/file/utils.d.ts +22 -26
- package/dist/file/utils.esm.js +14 -7
- package/package.json +1 -1
package/dist/file/index.esm.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
// src/file/put.ts
|
|
2
|
+
import { waitUntil } from "@vercel/functions";
|
|
2
3
|
import { NextResponse as NextResponse3 } from "next/server";
|
|
3
4
|
import { v4 } from "uuid";
|
|
4
5
|
|
|
@@ -111,7 +112,7 @@ import {
|
|
|
111
112
|
S3Client
|
|
112
113
|
} from "@aws-sdk/client-s3";
|
|
113
114
|
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
|
|
114
|
-
var
|
|
115
|
+
var client = new S3Client({
|
|
115
116
|
region: getEnv("AWS_REGION" /* AWS_REGION */),
|
|
116
117
|
credentials: {
|
|
117
118
|
accessKeyId: getEnv("AWS_ACCESS_KEY_ID" /* AWS_ACCESS_KEY_ID */),
|
|
@@ -124,7 +125,8 @@ var URL_PREFIX = `https://${getEnv("AWS_BUCKET" /* AWS_BUCKET */)}.s3.${getEnv(
|
|
|
124
125
|
function getKey(keys) {
|
|
125
126
|
return typeof keys === "string" ? keys : keys.join("/");
|
|
126
127
|
}
|
|
127
|
-
var getUploadURL = (
|
|
128
|
+
var getUploadURL = (keys) => {
|
|
129
|
+
if (!checkClient(client)) return;
|
|
128
130
|
const command = new PutObjectCommand({
|
|
129
131
|
Bucket: getEnv("AWS_BUCKET" /* AWS_BUCKET */),
|
|
130
132
|
Key: getKey(keys),
|
|
@@ -135,21 +137,23 @@ var getUploadURL = (client) => (keys) => {
|
|
|
135
137
|
var getDownloadURL = (keys) => {
|
|
136
138
|
return `${URL_PREFIX}${getKey(keys)}`;
|
|
137
139
|
};
|
|
138
|
-
var uploadFile =
|
|
140
|
+
var uploadFile = async (keys, {
|
|
139
141
|
url,
|
|
140
142
|
blob
|
|
141
143
|
}) => {
|
|
144
|
+
if (!checkClient(client)) return;
|
|
142
145
|
if (!blob && !url) return null;
|
|
143
146
|
const fileBlob = blob || await fetch(url).then((file) => file.blob());
|
|
144
147
|
if (fileBlob) {
|
|
145
148
|
const key = getKey(keys);
|
|
146
|
-
await uploadBlob(
|
|
149
|
+
await uploadBlob(fileBlob, key);
|
|
147
150
|
return getDownloadURL(key);
|
|
148
151
|
}
|
|
149
152
|
return null;
|
|
150
153
|
};
|
|
151
|
-
var deleteFile =
|
|
154
|
+
var deleteFile = async (url) => {
|
|
152
155
|
const key = url.split(URL_PREFIX)[1];
|
|
156
|
+
if (!checkClient(client)) return;
|
|
153
157
|
if (key) {
|
|
154
158
|
try {
|
|
155
159
|
await client.send(
|
|
@@ -165,7 +169,12 @@ var deleteFile = (client) => async (url) => {
|
|
|
165
169
|
}
|
|
166
170
|
return false;
|
|
167
171
|
};
|
|
168
|
-
|
|
172
|
+
function checkClient(client2) {
|
|
173
|
+
if (!client2) throw new Error("Client does not exist");
|
|
174
|
+
return true;
|
|
175
|
+
}
|
|
176
|
+
var uploadBlob = async (file, key) => {
|
|
177
|
+
if (!checkClient(client)) return;
|
|
169
178
|
const fileBuffer = await file.arrayBuffer();
|
|
170
179
|
return client.send(
|
|
171
180
|
new PutObjectCommand({
|
|
@@ -180,7 +189,7 @@ var uploadBlob = (client) => async (file, key) => {
|
|
|
180
189
|
};
|
|
181
190
|
|
|
182
191
|
// src/file/put.ts
|
|
183
|
-
var getFileUploadPutRoute = (options
|
|
192
|
+
var getFileUploadPutRoute = (options) => async (req) => {
|
|
184
193
|
const ctx = getContext(req);
|
|
185
194
|
if (!ctx?.userId || ctx.isRefreshID)
|
|
186
195
|
return NextResponse3.json({ error: "unauthorized" }, { status: 401 });
|
|
@@ -188,6 +197,7 @@ var getFileUploadPutRoute = (options, client) => async (req) => {
|
|
|
188
197
|
const file = formData.get("file");
|
|
189
198
|
if (!file) return NextResponse3.json({ error: "no file" }, { status: 400 });
|
|
190
199
|
const data = formData.get("data");
|
|
200
|
+
const async = formData.get("async");
|
|
191
201
|
const inputData = {
|
|
192
202
|
type: formData.get("type") + "",
|
|
193
203
|
userId: ctx.userId,
|
|
@@ -195,7 +205,8 @@ var getFileUploadPutRoute = (options, client) => async (req) => {
|
|
|
195
205
|
};
|
|
196
206
|
const fileKey = options.getKey ? await options.getKey(inputData) : v4();
|
|
197
207
|
const url = getDownloadURL(fileKey);
|
|
198
|
-
|
|
208
|
+
if (async) waitUntil(uploadBlob(file, fileKey));
|
|
209
|
+
else await uploadBlob(file, fileKey);
|
|
199
210
|
const onUploadResponse = await options.onUpload({
|
|
200
211
|
...inputData,
|
|
201
212
|
url
|
|
@@ -208,15 +219,14 @@ var getFileUploadPutRoute = (options, client) => async (req) => {
|
|
|
208
219
|
|
|
209
220
|
// src/file/setup.ts
|
|
210
221
|
function setupFileUpload(options) {
|
|
211
|
-
const client = getS3Client();
|
|
212
222
|
return {
|
|
213
|
-
PUT: getFileUploadPutRoute(options
|
|
214
|
-
uploadFile: uploadFile(client),
|
|
215
|
-
deleteFile: deleteFile(client),
|
|
216
|
-
getUploadURL: getUploadURL(client),
|
|
217
|
-
getDownloadURL
|
|
223
|
+
PUT: getFileUploadPutRoute(options)
|
|
218
224
|
};
|
|
219
225
|
}
|
|
220
226
|
export {
|
|
221
|
-
|
|
227
|
+
deleteFile,
|
|
228
|
+
getDownloadURL,
|
|
229
|
+
getUploadURL,
|
|
230
|
+
setupFileUpload,
|
|
231
|
+
uploadFile
|
|
222
232
|
};
|
package/dist/file/put.cjs.js
CHANGED
|
@@ -23,6 +23,7 @@ __export(put_exports, {
|
|
|
23
23
|
getFileUploadPutRoute: () => getFileUploadPutRoute
|
|
24
24
|
});
|
|
25
25
|
module.exports = __toCommonJS(put_exports);
|
|
26
|
+
var import_functions = require("@vercel/functions");
|
|
26
27
|
var import_server3 = require("next/server");
|
|
27
28
|
var import_uuid = require("uuid");
|
|
28
29
|
|
|
@@ -131,6 +132,13 @@ var getContext = (req) => {
|
|
|
131
132
|
// src/file/utils.ts
|
|
132
133
|
var import_client_s3 = require("@aws-sdk/client-s3");
|
|
133
134
|
var import_s3_request_presigner = require("@aws-sdk/s3-request-presigner");
|
|
135
|
+
var client = new import_client_s3.S3Client({
|
|
136
|
+
region: getEnv("AWS_REGION" /* AWS_REGION */),
|
|
137
|
+
credentials: {
|
|
138
|
+
accessKeyId: getEnv("AWS_ACCESS_KEY_ID" /* AWS_ACCESS_KEY_ID */),
|
|
139
|
+
secretAccessKey: getEnv("AWS_ACCESS_KEY_SECRET" /* AWS_ACCESS_KEY_SECRET */)
|
|
140
|
+
}
|
|
141
|
+
});
|
|
134
142
|
var URL_PREFIX = `https://${getEnv("AWS_BUCKET" /* AWS_BUCKET */)}.s3.${getEnv(
|
|
135
143
|
"AWS_REGION" /* AWS_REGION */
|
|
136
144
|
)}.amazonaws.com/`;
|
|
@@ -140,7 +148,12 @@ function getKey(keys) {
|
|
|
140
148
|
var getDownloadURL = (keys) => {
|
|
141
149
|
return `${URL_PREFIX}${getKey(keys)}`;
|
|
142
150
|
};
|
|
143
|
-
|
|
151
|
+
function checkClient(client2) {
|
|
152
|
+
if (!client2) throw new Error("Client does not exist");
|
|
153
|
+
return true;
|
|
154
|
+
}
|
|
155
|
+
var uploadBlob = async (file, key) => {
|
|
156
|
+
if (!checkClient(client)) return;
|
|
144
157
|
const fileBuffer = await file.arrayBuffer();
|
|
145
158
|
return client.send(
|
|
146
159
|
new import_client_s3.PutObjectCommand({
|
|
@@ -155,7 +168,7 @@ var uploadBlob = (client) => async (file, key) => {
|
|
|
155
168
|
};
|
|
156
169
|
|
|
157
170
|
// src/file/put.ts
|
|
158
|
-
var getFileUploadPutRoute = (options
|
|
171
|
+
var getFileUploadPutRoute = (options) => async (req) => {
|
|
159
172
|
const ctx = getContext(req);
|
|
160
173
|
if (!ctx?.userId || ctx.isRefreshID)
|
|
161
174
|
return import_server3.NextResponse.json({ error: "unauthorized" }, { status: 401 });
|
|
@@ -163,6 +176,7 @@ var getFileUploadPutRoute = (options, client) => async (req) => {
|
|
|
163
176
|
const file = formData.get("file");
|
|
164
177
|
if (!file) return import_server3.NextResponse.json({ error: "no file" }, { status: 400 });
|
|
165
178
|
const data = formData.get("data");
|
|
179
|
+
const async = formData.get("async");
|
|
166
180
|
const inputData = {
|
|
167
181
|
type: formData.get("type") + "",
|
|
168
182
|
userId: ctx.userId,
|
|
@@ -170,7 +184,8 @@ var getFileUploadPutRoute = (options, client) => async (req) => {
|
|
|
170
184
|
};
|
|
171
185
|
const fileKey = options.getKey ? await options.getKey(inputData) : (0, import_uuid.v4)();
|
|
172
186
|
const url = getDownloadURL(fileKey);
|
|
173
|
-
|
|
187
|
+
if (async) (0, import_functions.waitUntil)(uploadBlob(file, fileKey));
|
|
188
|
+
else await uploadBlob(file, fileKey);
|
|
174
189
|
const onUploadResponse = await options.onUpload({
|
|
175
190
|
...inputData,
|
|
176
191
|
url
|
package/dist/file/put.d.mts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { S3Client } from '@aws-sdk/client-s3';
|
|
2
1
|
import { NextRequest, NextResponse } from 'next/server';
|
|
3
2
|
import { SetupFileUploadOptions } from './setup.mjs';
|
|
4
3
|
|
|
@@ -9,11 +8,10 @@ import { SetupFileUploadOptions } from './setup.mjs';
|
|
|
9
8
|
* Expects multipart form data with fields: `file` (File), `type` (string), and optional `data` (JSON string).
|
|
10
9
|
*
|
|
11
10
|
* @param options - `SetupFileUploadOptions` (getKey, onUpload).
|
|
12
|
-
* @param client - S3 client instance.
|
|
13
11
|
* @returns Async Next.js route handler for PUT requests.
|
|
14
12
|
* @category File
|
|
15
13
|
*/
|
|
16
|
-
declare const getFileUploadPutRoute: (options: SetupFileUploadOptions
|
|
14
|
+
declare const getFileUploadPutRoute: (options: SetupFileUploadOptions) => (req: NextRequest) => Promise<NextResponse<{
|
|
17
15
|
error: string;
|
|
18
16
|
}> | NextResponse<{
|
|
19
17
|
url: string;
|
package/dist/file/put.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { S3Client } from '@aws-sdk/client-s3';
|
|
2
1
|
import { NextRequest, NextResponse } from 'next/server';
|
|
3
2
|
import { SetupFileUploadOptions } from './setup.js';
|
|
4
3
|
|
|
@@ -9,11 +8,10 @@ import { SetupFileUploadOptions } from './setup.js';
|
|
|
9
8
|
* Expects multipart form data with fields: `file` (File), `type` (string), and optional `data` (JSON string).
|
|
10
9
|
*
|
|
11
10
|
* @param options - `SetupFileUploadOptions` (getKey, onUpload).
|
|
12
|
-
* @param client - S3 client instance.
|
|
13
11
|
* @returns Async Next.js route handler for PUT requests.
|
|
14
12
|
* @category File
|
|
15
13
|
*/
|
|
16
|
-
declare const getFileUploadPutRoute: (options: SetupFileUploadOptions
|
|
14
|
+
declare const getFileUploadPutRoute: (options: SetupFileUploadOptions) => (req: NextRequest) => Promise<NextResponse<{
|
|
17
15
|
error: string;
|
|
18
16
|
}> | NextResponse<{
|
|
19
17
|
url: string;
|
package/dist/file/put.esm.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
// src/file/put.ts
|
|
2
|
+
import { waitUntil } from "@vercel/functions";
|
|
2
3
|
import { NextResponse as NextResponse3 } from "next/server";
|
|
3
4
|
import { v4 } from "uuid";
|
|
4
5
|
|
|
@@ -111,6 +112,13 @@ import {
|
|
|
111
112
|
S3Client
|
|
112
113
|
} from "@aws-sdk/client-s3";
|
|
113
114
|
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
|
|
115
|
+
var client = new S3Client({
|
|
116
|
+
region: getEnv("AWS_REGION" /* AWS_REGION */),
|
|
117
|
+
credentials: {
|
|
118
|
+
accessKeyId: getEnv("AWS_ACCESS_KEY_ID" /* AWS_ACCESS_KEY_ID */),
|
|
119
|
+
secretAccessKey: getEnv("AWS_ACCESS_KEY_SECRET" /* AWS_ACCESS_KEY_SECRET */)
|
|
120
|
+
}
|
|
121
|
+
});
|
|
114
122
|
var URL_PREFIX = `https://${getEnv("AWS_BUCKET" /* AWS_BUCKET */)}.s3.${getEnv(
|
|
115
123
|
"AWS_REGION" /* AWS_REGION */
|
|
116
124
|
)}.amazonaws.com/`;
|
|
@@ -120,7 +128,12 @@ function getKey(keys) {
|
|
|
120
128
|
var getDownloadURL = (keys) => {
|
|
121
129
|
return `${URL_PREFIX}${getKey(keys)}`;
|
|
122
130
|
};
|
|
123
|
-
|
|
131
|
+
function checkClient(client2) {
|
|
132
|
+
if (!client2) throw new Error("Client does not exist");
|
|
133
|
+
return true;
|
|
134
|
+
}
|
|
135
|
+
var uploadBlob = async (file, key) => {
|
|
136
|
+
if (!checkClient(client)) return;
|
|
124
137
|
const fileBuffer = await file.arrayBuffer();
|
|
125
138
|
return client.send(
|
|
126
139
|
new PutObjectCommand({
|
|
@@ -135,7 +148,7 @@ var uploadBlob = (client) => async (file, key) => {
|
|
|
135
148
|
};
|
|
136
149
|
|
|
137
150
|
// src/file/put.ts
|
|
138
|
-
var getFileUploadPutRoute = (options
|
|
151
|
+
var getFileUploadPutRoute = (options) => async (req) => {
|
|
139
152
|
const ctx = getContext(req);
|
|
140
153
|
if (!ctx?.userId || ctx.isRefreshID)
|
|
141
154
|
return NextResponse3.json({ error: "unauthorized" }, { status: 401 });
|
|
@@ -143,6 +156,7 @@ var getFileUploadPutRoute = (options, client) => async (req) => {
|
|
|
143
156
|
const file = formData.get("file");
|
|
144
157
|
if (!file) return NextResponse3.json({ error: "no file" }, { status: 400 });
|
|
145
158
|
const data = formData.get("data");
|
|
159
|
+
const async = formData.get("async");
|
|
146
160
|
const inputData = {
|
|
147
161
|
type: formData.get("type") + "",
|
|
148
162
|
userId: ctx.userId,
|
|
@@ -150,7 +164,8 @@ var getFileUploadPutRoute = (options, client) => async (req) => {
|
|
|
150
164
|
};
|
|
151
165
|
const fileKey = options.getKey ? await options.getKey(inputData) : v4();
|
|
152
166
|
const url = getDownloadURL(fileKey);
|
|
153
|
-
|
|
167
|
+
if (async) waitUntil(uploadBlob(file, fileKey));
|
|
168
|
+
else await uploadBlob(file, fileKey);
|
|
154
169
|
const onUploadResponse = await options.onUpload({
|
|
155
170
|
...inputData,
|
|
156
171
|
url
|
package/dist/file/setup.cjs.js
CHANGED
|
@@ -25,6 +25,7 @@ __export(setup_exports, {
|
|
|
25
25
|
module.exports = __toCommonJS(setup_exports);
|
|
26
26
|
|
|
27
27
|
// src/file/put.ts
|
|
28
|
+
var import_functions = require("@vercel/functions");
|
|
28
29
|
var import_server3 = require("next/server");
|
|
29
30
|
var import_uuid = require("uuid");
|
|
30
31
|
|
|
@@ -133,7 +134,7 @@ var getContext = (req) => {
|
|
|
133
134
|
// src/file/utils.ts
|
|
134
135
|
var import_client_s3 = require("@aws-sdk/client-s3");
|
|
135
136
|
var import_s3_request_presigner = require("@aws-sdk/s3-request-presigner");
|
|
136
|
-
var
|
|
137
|
+
var client = new import_client_s3.S3Client({
|
|
137
138
|
region: getEnv("AWS_REGION" /* AWS_REGION */),
|
|
138
139
|
credentials: {
|
|
139
140
|
accessKeyId: getEnv("AWS_ACCESS_KEY_ID" /* AWS_ACCESS_KEY_ID */),
|
|
@@ -146,48 +147,15 @@ var URL_PREFIX = `https://${getEnv("AWS_BUCKET" /* AWS_BUCKET */)}.s3.${getEnv(
|
|
|
146
147
|
function getKey(keys) {
|
|
147
148
|
return typeof keys === "string" ? keys : keys.join("/");
|
|
148
149
|
}
|
|
149
|
-
var getUploadURL = (client) => (keys) => {
|
|
150
|
-
const command = new import_client_s3.PutObjectCommand({
|
|
151
|
-
Bucket: getEnv("AWS_BUCKET" /* AWS_BUCKET */),
|
|
152
|
-
Key: getKey(keys),
|
|
153
|
-
ACL: "public-read"
|
|
154
|
-
});
|
|
155
|
-
return (0, import_s3_request_presigner.getSignedUrl)(client, command, { expiresIn: 300 });
|
|
156
|
-
};
|
|
157
150
|
var getDownloadURL = (keys) => {
|
|
158
151
|
return `${URL_PREFIX}${getKey(keys)}`;
|
|
159
152
|
};
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
if (fileBlob) {
|
|
167
|
-
const key = getKey(keys);
|
|
168
|
-
await uploadBlob(client)(fileBlob, key);
|
|
169
|
-
return getDownloadURL(key);
|
|
170
|
-
}
|
|
171
|
-
return null;
|
|
172
|
-
};
|
|
173
|
-
var deleteFile = (client) => async (url) => {
|
|
174
|
-
const key = url.split(URL_PREFIX)[1];
|
|
175
|
-
if (key) {
|
|
176
|
-
try {
|
|
177
|
-
await client.send(
|
|
178
|
-
new import_client_s3.DeleteObjectCommand({
|
|
179
|
-
Bucket: getEnv("AWS_BUCKET" /* AWS_BUCKET */),
|
|
180
|
-
Key: key
|
|
181
|
-
})
|
|
182
|
-
);
|
|
183
|
-
return true;
|
|
184
|
-
} catch (e) {
|
|
185
|
-
console.error("ERROR", url, e);
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
return false;
|
|
189
|
-
};
|
|
190
|
-
var uploadBlob = (client) => async (file, key) => {
|
|
153
|
+
function checkClient(client2) {
|
|
154
|
+
if (!client2) throw new Error("Client does not exist");
|
|
155
|
+
return true;
|
|
156
|
+
}
|
|
157
|
+
var uploadBlob = async (file, key) => {
|
|
158
|
+
if (!checkClient(client)) return;
|
|
191
159
|
const fileBuffer = await file.arrayBuffer();
|
|
192
160
|
return client.send(
|
|
193
161
|
new import_client_s3.PutObjectCommand({
|
|
@@ -202,7 +170,7 @@ var uploadBlob = (client) => async (file, key) => {
|
|
|
202
170
|
};
|
|
203
171
|
|
|
204
172
|
// src/file/put.ts
|
|
205
|
-
var getFileUploadPutRoute = (options
|
|
173
|
+
var getFileUploadPutRoute = (options) => async (req) => {
|
|
206
174
|
const ctx = getContext(req);
|
|
207
175
|
if (!ctx?.userId || ctx.isRefreshID)
|
|
208
176
|
return import_server3.NextResponse.json({ error: "unauthorized" }, { status: 401 });
|
|
@@ -210,6 +178,7 @@ var getFileUploadPutRoute = (options, client) => async (req) => {
|
|
|
210
178
|
const file = formData.get("file");
|
|
211
179
|
if (!file) return import_server3.NextResponse.json({ error: "no file" }, { status: 400 });
|
|
212
180
|
const data = formData.get("data");
|
|
181
|
+
const async = formData.get("async");
|
|
213
182
|
const inputData = {
|
|
214
183
|
type: formData.get("type") + "",
|
|
215
184
|
userId: ctx.userId,
|
|
@@ -217,7 +186,8 @@ var getFileUploadPutRoute = (options, client) => async (req) => {
|
|
|
217
186
|
};
|
|
218
187
|
const fileKey = options.getKey ? await options.getKey(inputData) : (0, import_uuid.v4)();
|
|
219
188
|
const url = getDownloadURL(fileKey);
|
|
220
|
-
|
|
189
|
+
if (async) (0, import_functions.waitUntil)(uploadBlob(file, fileKey));
|
|
190
|
+
else await uploadBlob(file, fileKey);
|
|
221
191
|
const onUploadResponse = await options.onUpload({
|
|
222
192
|
...inputData,
|
|
223
193
|
url
|
|
@@ -230,13 +200,8 @@ var getFileUploadPutRoute = (options, client) => async (req) => {
|
|
|
230
200
|
|
|
231
201
|
// src/file/setup.ts
|
|
232
202
|
function setupFileUpload(options) {
|
|
233
|
-
const client = getS3Client();
|
|
234
203
|
return {
|
|
235
|
-
PUT: getFileUploadPutRoute(options
|
|
236
|
-
uploadFile: uploadFile(client),
|
|
237
|
-
deleteFile: deleteFile(client),
|
|
238
|
-
getUploadURL: getUploadURL(client),
|
|
239
|
-
getDownloadURL
|
|
204
|
+
PUT: getFileUploadPutRoute(options)
|
|
240
205
|
};
|
|
241
206
|
}
|
|
242
207
|
// Annotate the CommonJS export names for ESM import in node:
|
package/dist/file/setup.d.mts
CHANGED
|
@@ -68,13 +68,6 @@ declare function setupFileUpload(options: SetupFileUploadOptions): {
|
|
|
68
68
|
url: string;
|
|
69
69
|
onUploadResponse: object;
|
|
70
70
|
}>>;
|
|
71
|
-
uploadFile: (keys: string | string[], { url, blob, }: {
|
|
72
|
-
blob?: Blob;
|
|
73
|
-
url?: string;
|
|
74
|
-
}) => Promise<string | null>;
|
|
75
|
-
deleteFile: (url: string) => Promise<boolean>;
|
|
76
|
-
getUploadURL: (keys: string | string[]) => Promise<string>;
|
|
77
|
-
getDownloadURL: (keys: string | string[]) => string;
|
|
78
71
|
};
|
|
79
72
|
|
|
80
73
|
export { type SetupFileUploadOptions, setupFileUpload };
|
package/dist/file/setup.d.ts
CHANGED
|
@@ -68,13 +68,6 @@ declare function setupFileUpload(options: SetupFileUploadOptions): {
|
|
|
68
68
|
url: string;
|
|
69
69
|
onUploadResponse: object;
|
|
70
70
|
}>>;
|
|
71
|
-
uploadFile: (keys: string | string[], { url, blob, }: {
|
|
72
|
-
blob?: Blob;
|
|
73
|
-
url?: string;
|
|
74
|
-
}) => Promise<string | null>;
|
|
75
|
-
deleteFile: (url: string) => Promise<boolean>;
|
|
76
|
-
getUploadURL: (keys: string | string[]) => Promise<string>;
|
|
77
|
-
getDownloadURL: (keys: string | string[]) => string;
|
|
78
71
|
};
|
|
79
72
|
|
|
80
73
|
export { type SetupFileUploadOptions, setupFileUpload };
|
package/dist/file/setup.esm.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
// src/file/put.ts
|
|
2
|
+
import { waitUntil } from "@vercel/functions";
|
|
2
3
|
import { NextResponse as NextResponse3 } from "next/server";
|
|
3
4
|
import { v4 } from "uuid";
|
|
4
5
|
|
|
@@ -111,7 +112,7 @@ import {
|
|
|
111
112
|
S3Client
|
|
112
113
|
} from "@aws-sdk/client-s3";
|
|
113
114
|
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
|
|
114
|
-
var
|
|
115
|
+
var client = new S3Client({
|
|
115
116
|
region: getEnv("AWS_REGION" /* AWS_REGION */),
|
|
116
117
|
credentials: {
|
|
117
118
|
accessKeyId: getEnv("AWS_ACCESS_KEY_ID" /* AWS_ACCESS_KEY_ID */),
|
|
@@ -124,48 +125,15 @@ var URL_PREFIX = `https://${getEnv("AWS_BUCKET" /* AWS_BUCKET */)}.s3.${getEnv(
|
|
|
124
125
|
function getKey(keys) {
|
|
125
126
|
return typeof keys === "string" ? keys : keys.join("/");
|
|
126
127
|
}
|
|
127
|
-
var getUploadURL = (client) => (keys) => {
|
|
128
|
-
const command = new PutObjectCommand({
|
|
129
|
-
Bucket: getEnv("AWS_BUCKET" /* AWS_BUCKET */),
|
|
130
|
-
Key: getKey(keys),
|
|
131
|
-
ACL: "public-read"
|
|
132
|
-
});
|
|
133
|
-
return getSignedUrl(client, command, { expiresIn: 300 });
|
|
134
|
-
};
|
|
135
128
|
var getDownloadURL = (keys) => {
|
|
136
129
|
return `${URL_PREFIX}${getKey(keys)}`;
|
|
137
130
|
};
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
if (fileBlob) {
|
|
145
|
-
const key = getKey(keys);
|
|
146
|
-
await uploadBlob(client)(fileBlob, key);
|
|
147
|
-
return getDownloadURL(key);
|
|
148
|
-
}
|
|
149
|
-
return null;
|
|
150
|
-
};
|
|
151
|
-
var deleteFile = (client) => async (url) => {
|
|
152
|
-
const key = url.split(URL_PREFIX)[1];
|
|
153
|
-
if (key) {
|
|
154
|
-
try {
|
|
155
|
-
await client.send(
|
|
156
|
-
new DeleteObjectCommand({
|
|
157
|
-
Bucket: getEnv("AWS_BUCKET" /* AWS_BUCKET */),
|
|
158
|
-
Key: key
|
|
159
|
-
})
|
|
160
|
-
);
|
|
161
|
-
return true;
|
|
162
|
-
} catch (e) {
|
|
163
|
-
console.error("ERROR", url, e);
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
return false;
|
|
167
|
-
};
|
|
168
|
-
var uploadBlob = (client) => async (file, key) => {
|
|
131
|
+
function checkClient(client2) {
|
|
132
|
+
if (!client2) throw new Error("Client does not exist");
|
|
133
|
+
return true;
|
|
134
|
+
}
|
|
135
|
+
var uploadBlob = async (file, key) => {
|
|
136
|
+
if (!checkClient(client)) return;
|
|
169
137
|
const fileBuffer = await file.arrayBuffer();
|
|
170
138
|
return client.send(
|
|
171
139
|
new PutObjectCommand({
|
|
@@ -180,7 +148,7 @@ var uploadBlob = (client) => async (file, key) => {
|
|
|
180
148
|
};
|
|
181
149
|
|
|
182
150
|
// src/file/put.ts
|
|
183
|
-
var getFileUploadPutRoute = (options
|
|
151
|
+
var getFileUploadPutRoute = (options) => async (req) => {
|
|
184
152
|
const ctx = getContext(req);
|
|
185
153
|
if (!ctx?.userId || ctx.isRefreshID)
|
|
186
154
|
return NextResponse3.json({ error: "unauthorized" }, { status: 401 });
|
|
@@ -188,6 +156,7 @@ var getFileUploadPutRoute = (options, client) => async (req) => {
|
|
|
188
156
|
const file = formData.get("file");
|
|
189
157
|
if (!file) return NextResponse3.json({ error: "no file" }, { status: 400 });
|
|
190
158
|
const data = formData.get("data");
|
|
159
|
+
const async = formData.get("async");
|
|
191
160
|
const inputData = {
|
|
192
161
|
type: formData.get("type") + "",
|
|
193
162
|
userId: ctx.userId,
|
|
@@ -195,7 +164,8 @@ var getFileUploadPutRoute = (options, client) => async (req) => {
|
|
|
195
164
|
};
|
|
196
165
|
const fileKey = options.getKey ? await options.getKey(inputData) : v4();
|
|
197
166
|
const url = getDownloadURL(fileKey);
|
|
198
|
-
|
|
167
|
+
if (async) waitUntil(uploadBlob(file, fileKey));
|
|
168
|
+
else await uploadBlob(file, fileKey);
|
|
199
169
|
const onUploadResponse = await options.onUpload({
|
|
200
170
|
...inputData,
|
|
201
171
|
url
|
|
@@ -208,13 +178,8 @@ var getFileUploadPutRoute = (options, client) => async (req) => {
|
|
|
208
178
|
|
|
209
179
|
// src/file/setup.ts
|
|
210
180
|
function setupFileUpload(options) {
|
|
211
|
-
const client = getS3Client();
|
|
212
181
|
return {
|
|
213
|
-
PUT: getFileUploadPutRoute(options
|
|
214
|
-
uploadFile: uploadFile(client),
|
|
215
|
-
deleteFile: deleteFile(client),
|
|
216
|
-
getUploadURL: getUploadURL(client),
|
|
217
|
-
getDownloadURL
|
|
182
|
+
PUT: getFileUploadPutRoute(options)
|
|
218
183
|
};
|
|
219
184
|
}
|
|
220
185
|
export {
|
package/dist/file/utils.cjs.js
CHANGED
|
@@ -22,7 +22,6 @@ var utils_exports = {};
|
|
|
22
22
|
__export(utils_exports, {
|
|
23
23
|
deleteFile: () => deleteFile,
|
|
24
24
|
getDownloadURL: () => getDownloadURL,
|
|
25
|
-
getS3Client: () => getS3Client,
|
|
26
25
|
getUploadURL: () => getUploadURL,
|
|
27
26
|
uploadBlob: () => uploadBlob,
|
|
28
27
|
uploadFile: () => uploadFile
|
|
@@ -81,7 +80,7 @@ function getEnv(key, skipCheck) {
|
|
|
81
80
|
}
|
|
82
81
|
|
|
83
82
|
// src/file/utils.ts
|
|
84
|
-
var
|
|
83
|
+
var client = new import_client_s3.S3Client({
|
|
85
84
|
region: getEnv("AWS_REGION" /* AWS_REGION */),
|
|
86
85
|
credentials: {
|
|
87
86
|
accessKeyId: getEnv("AWS_ACCESS_KEY_ID" /* AWS_ACCESS_KEY_ID */),
|
|
@@ -94,7 +93,8 @@ var URL_PREFIX = `https://${getEnv("AWS_BUCKET" /* AWS_BUCKET */)}.s3.${getEnv(
|
|
|
94
93
|
function getKey(keys) {
|
|
95
94
|
return typeof keys === "string" ? keys : keys.join("/");
|
|
96
95
|
}
|
|
97
|
-
var getUploadURL = (
|
|
96
|
+
var getUploadURL = (keys) => {
|
|
97
|
+
if (!checkClient(client)) return;
|
|
98
98
|
const command = new import_client_s3.PutObjectCommand({
|
|
99
99
|
Bucket: getEnv("AWS_BUCKET" /* AWS_BUCKET */),
|
|
100
100
|
Key: getKey(keys),
|
|
@@ -105,21 +105,23 @@ var getUploadURL = (client) => (keys) => {
|
|
|
105
105
|
var getDownloadURL = (keys) => {
|
|
106
106
|
return `${URL_PREFIX}${getKey(keys)}`;
|
|
107
107
|
};
|
|
108
|
-
var uploadFile =
|
|
108
|
+
var uploadFile = async (keys, {
|
|
109
109
|
url,
|
|
110
110
|
blob
|
|
111
111
|
}) => {
|
|
112
|
+
if (!checkClient(client)) return;
|
|
112
113
|
if (!blob && !url) return null;
|
|
113
114
|
const fileBlob = blob || await fetch(url).then((file) => file.blob());
|
|
114
115
|
if (fileBlob) {
|
|
115
116
|
const key = getKey(keys);
|
|
116
|
-
await uploadBlob(
|
|
117
|
+
await uploadBlob(fileBlob, key);
|
|
117
118
|
return getDownloadURL(key);
|
|
118
119
|
}
|
|
119
120
|
return null;
|
|
120
121
|
};
|
|
121
|
-
var deleteFile =
|
|
122
|
+
var deleteFile = async (url) => {
|
|
122
123
|
const key = url.split(URL_PREFIX)[1];
|
|
124
|
+
if (!checkClient(client)) return;
|
|
123
125
|
if (key) {
|
|
124
126
|
try {
|
|
125
127
|
await client.send(
|
|
@@ -135,7 +137,12 @@ var deleteFile = (client) => async (url) => {
|
|
|
135
137
|
}
|
|
136
138
|
return false;
|
|
137
139
|
};
|
|
138
|
-
|
|
140
|
+
function checkClient(client2) {
|
|
141
|
+
if (!client2) throw new Error("Client does not exist");
|
|
142
|
+
return true;
|
|
143
|
+
}
|
|
144
|
+
var uploadBlob = async (file, key) => {
|
|
145
|
+
if (!checkClient(client)) return;
|
|
139
146
|
const fileBuffer = await file.arrayBuffer();
|
|
140
147
|
return client.send(
|
|
141
148
|
new import_client_s3.PutObjectCommand({
|
|
@@ -152,7 +159,6 @@ var uploadBlob = (client) => async (file, key) => {
|
|
|
152
159
|
0 && (module.exports = {
|
|
153
160
|
deleteFile,
|
|
154
161
|
getDownloadURL,
|
|
155
|
-
getS3Client,
|
|
156
162
|
getUploadURL,
|
|
157
163
|
uploadBlob,
|
|
158
164
|
uploadFile
|
package/dist/file/utils.d.mts
CHANGED
|
@@ -1,21 +1,14 @@
|
|
|
1
1
|
import * as _aws_sdk_client_s3 from '@aws-sdk/client-s3';
|
|
2
|
-
import { S3Client } from '@aws-sdk/client-s3';
|
|
3
2
|
|
|
4
3
|
/**
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
* @category File
|
|
8
|
-
*/
|
|
9
|
-
declare const getS3Client: () => S3Client;
|
|
10
|
-
/**
|
|
11
|
-
* Returns a function that generates a presigned PUT URL for uploading to the given S3 key(s).
|
|
12
|
-
* The presigned URL expires after 5 minutes.
|
|
4
|
+
* Generates a presigned PUT URL for uploading a file to the given S3 key(s).
|
|
5
|
+
* The URL expires after 5 minutes.
|
|
13
6
|
*
|
|
14
|
-
* @param
|
|
15
|
-
* @returns
|
|
7
|
+
* @param keys - S3 key or key path segments (array joined by `/`).
|
|
8
|
+
* @returns A presigned upload URL.
|
|
16
9
|
* @category File
|
|
17
10
|
*/
|
|
18
|
-
declare const getUploadURL: (
|
|
11
|
+
declare const getUploadURL: (keys: string | string[]) => Promise<string> | undefined;
|
|
19
12
|
/**
|
|
20
13
|
* Builds the public download URL for one or more keys in the configured S3 bucket.
|
|
21
14
|
*
|
|
@@ -25,31 +18,34 @@ declare const getUploadURL: (client: S3Client) => (keys: string | string[]) => P
|
|
|
25
18
|
*/
|
|
26
19
|
declare const getDownloadURL: (keys: string | string[]) => string;
|
|
27
20
|
/**
|
|
28
|
-
*
|
|
21
|
+
* Uploads a file to S3 at the given key(s), either from a Blob or a remote URL.
|
|
29
22
|
*
|
|
30
|
-
* @param
|
|
31
|
-
* @
|
|
23
|
+
* @param keys - S3 key or key path segments (array joined by `/`).
|
|
24
|
+
* @param options.blob - A Blob/File to upload directly.
|
|
25
|
+
* @param options.url - A remote URL to fetch and upload. Ignored if `blob` is provided.
|
|
26
|
+
* @returns The public download URL of the uploaded file, or `null` if neither `blob` nor `url` was provided.
|
|
32
27
|
* @category File
|
|
33
28
|
*/
|
|
34
|
-
declare const uploadFile: (
|
|
29
|
+
declare const uploadFile: (keys: string | string[], { url, blob, }: {
|
|
35
30
|
blob?: Blob;
|
|
36
31
|
url?: string;
|
|
37
|
-
}) => Promise<string | null>;
|
|
32
|
+
}) => Promise<string | null | undefined>;
|
|
38
33
|
/**
|
|
39
|
-
*
|
|
34
|
+
* Deletes an S3 object identified by its full public URL.
|
|
40
35
|
*
|
|
41
|
-
* @param
|
|
42
|
-
* @returns `
|
|
36
|
+
* @param url - The full public URL of the S3 object to delete.
|
|
37
|
+
* @returns `true` if the object was deleted successfully, `false` otherwise.
|
|
43
38
|
* @category File
|
|
44
39
|
*/
|
|
45
|
-
declare const deleteFile: (
|
|
40
|
+
declare const deleteFile: (url: string) => Promise<boolean | undefined>;
|
|
46
41
|
/**
|
|
47
|
-
*
|
|
42
|
+
* Uploads a Blob or File to S3 at the given key with public-read ACL.
|
|
48
43
|
*
|
|
49
|
-
* @param
|
|
50
|
-
* @
|
|
44
|
+
* @param file - The Blob or File to upload.
|
|
45
|
+
* @param key - The S3 object key.
|
|
46
|
+
* @returns The `PutObjectCommandOutput` from S3.
|
|
51
47
|
* @category File
|
|
52
48
|
*/
|
|
53
|
-
declare const uploadBlob: (
|
|
49
|
+
declare const uploadBlob: (file: File | Blob, key: string) => Promise<_aws_sdk_client_s3.PutObjectCommandOutput | undefined>;
|
|
54
50
|
|
|
55
|
-
export { deleteFile, getDownloadURL,
|
|
51
|
+
export { deleteFile, getDownloadURL, getUploadURL, uploadBlob, uploadFile };
|