naystack 1.2.16 → 1.2.19
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/email/client.cjs.js +35 -7
- package/dist/auth/email/client.d.mts +13 -1
- package/dist/auth/email/client.d.ts +13 -1
- package/dist/auth/email/client.esm.js +34 -4
- package/dist/auth/email/index.cjs.js +23 -18
- package/dist/auth/email/index.d.mts +4 -12
- package/dist/auth/email/index.d.ts +4 -12
- package/dist/auth/email/index.esm.js +23 -18
- package/dist/auth/email/routes/delete.cjs.js +45 -1
- package/dist/auth/email/routes/delete.esm.js +45 -1
- package/dist/auth/email/routes/get.cjs.js +6 -2
- package/dist/auth/email/routes/get.d.mts +2 -2
- package/dist/auth/email/routes/get.d.ts +2 -2
- package/dist/auth/email/routes/get.esm.js +6 -2
- package/dist/auth/email/routes/post.cjs.js +3 -1
- package/dist/auth/email/routes/post.esm.js +3 -1
- package/dist/auth/email/routes/put.cjs.js +3 -0
- package/dist/auth/email/routes/put.esm.js +3 -0
- package/dist/auth/email/types.d.mts +5 -3
- package/dist/auth/email/types.d.ts +5 -3
- package/dist/auth/email/utils.cjs.js +8 -8
- package/dist/auth/email/utils.d.mts +3 -5
- package/dist/auth/email/utils.d.ts +3 -5
- package/dist/auth/email/utils.esm.js +7 -7
- package/dist/auth/index.cjs.js +23 -18
- package/dist/auth/index.d.mts +1 -0
- package/dist/auth/index.d.ts +1 -0
- package/dist/auth/index.esm.js +23 -18
- package/dist/client/hooks.d.mts +2 -2
- package/dist/client/hooks.d.ts +2 -2
- package/dist/file/client.cjs.js +20 -23
- package/dist/file/client.d.mts +4 -10
- package/dist/file/client.d.ts +4 -10
- package/dist/file/client.esm.js +19 -22
- package/dist/file/index.cjs.js +47 -55
- package/dist/file/index.d.mts +0 -1
- package/dist/file/index.d.ts +0 -1
- package/dist/file/index.esm.js +47 -55
- package/dist/file/put.cjs.js +29 -58
- package/dist/file/put.d.mts +2 -1
- package/dist/file/put.d.ts +2 -1
- package/dist/file/put.esm.js +29 -58
- package/dist/file/setup.cjs.js +47 -55
- package/dist/file/setup.d.mts +16 -12
- package/dist/file/setup.d.ts +16 -12
- package/dist/file/setup.esm.js +47 -55
- package/dist/file/utils.cjs.js +29 -27
- package/dist/file/utils.d.mts +9 -6
- package/dist/file/utils.d.ts +9 -6
- package/dist/file/utils.esm.js +24 -22
- package/dist/graphql/client.cjs.js +11 -34
- package/dist/graphql/client.d.mts +3 -9
- package/dist/graphql/client.d.ts +3 -9
- package/dist/graphql/client.esm.js +8 -30
- package/dist/graphql/server.d.mts +2 -2
- package/dist/graphql/server.d.ts +2 -2
- package/dist/graphql/types.d.mts +2 -0
- package/dist/graphql/types.d.ts +2 -0
- package/package.json +1 -1
package/dist/file/setup.cjs.js
CHANGED
|
@@ -25,7 +25,6 @@ __export(setup_exports, {
|
|
|
25
25
|
module.exports = __toCommonJS(setup_exports);
|
|
26
26
|
|
|
27
27
|
// src/file/put.ts
|
|
28
|
-
var import_functions2 = require("@vercel/functions");
|
|
29
28
|
var import_server3 = require("next/server");
|
|
30
29
|
var import_uuid = require("uuid");
|
|
31
30
|
|
|
@@ -53,33 +52,31 @@ function getUserIdFromRefreshToken(refreshKey, refreshToken) {
|
|
|
53
52
|
var import_server2 = require("next/server");
|
|
54
53
|
|
|
55
54
|
// src/auth/email/utils.ts
|
|
56
|
-
var
|
|
55
|
+
var getContext = (refreshKey, signingKey, req) => {
|
|
57
56
|
const bearer = req.headers.get("authorization");
|
|
58
57
|
if (!bearer) {
|
|
59
58
|
const refresh = req.cookies.get("refresh")?.value;
|
|
60
59
|
const userId = getUserIdFromRefreshToken(refreshKey, refresh);
|
|
61
|
-
if (userId) return {
|
|
62
|
-
return null;
|
|
60
|
+
if (userId) return { userId, isRefreshID: true };
|
|
61
|
+
return { userId: null };
|
|
63
62
|
}
|
|
64
63
|
const token = bearer.slice(7);
|
|
65
64
|
try {
|
|
66
65
|
const res = (0, import_jsonwebtoken2.verify)(token, signingKey);
|
|
67
66
|
if (typeof res === "string") {
|
|
68
|
-
return null;
|
|
67
|
+
return { userId: null };
|
|
69
68
|
}
|
|
70
69
|
return {
|
|
71
|
-
|
|
70
|
+
userId: res.id
|
|
72
71
|
};
|
|
73
72
|
} catch {
|
|
74
73
|
}
|
|
75
|
-
return null;
|
|
74
|
+
return { userId: null };
|
|
76
75
|
};
|
|
77
76
|
|
|
78
77
|
// src/file/utils.ts
|
|
79
|
-
var import_node_crypto = require("crypto");
|
|
80
78
|
var import_client_s3 = require("@aws-sdk/client-s3");
|
|
81
79
|
var import_s3_request_presigner = require("@aws-sdk/s3-request-presigner");
|
|
82
|
-
var import_functions = require("@vercel/functions");
|
|
83
80
|
var getS3Client = (options) => new import_client_s3.S3Client({
|
|
84
81
|
region: options.region,
|
|
85
82
|
credentials: {
|
|
@@ -88,30 +85,34 @@ var getS3Client = (options) => new import_client_s3.S3Client({
|
|
|
88
85
|
}
|
|
89
86
|
});
|
|
90
87
|
var getURLPrefix = (options) => `https://${options.bucket}.s3.${options.region}.amazonaws.com/`;
|
|
91
|
-
function
|
|
92
|
-
return
|
|
88
|
+
function getKey(keys) {
|
|
89
|
+
return typeof keys === "string" ? keys : keys.join("/");
|
|
93
90
|
}
|
|
94
|
-
var
|
|
91
|
+
var getUploadURL = (client, Bucket) => (keys) => {
|
|
95
92
|
const command = new import_client_s3.PutObjectCommand({
|
|
96
93
|
Bucket,
|
|
97
|
-
Key:
|
|
98
|
-
ACL:
|
|
94
|
+
Key: getKey(keys),
|
|
95
|
+
ACL: "public-read"
|
|
99
96
|
});
|
|
100
97
|
return (0, import_s3_request_presigner.getSignedUrl)(client, command, { expiresIn: 300 });
|
|
101
98
|
};
|
|
102
|
-
var
|
|
103
|
-
|
|
104
|
-
return `${getURLPrefix(options)}${getHash(keys)}`;
|
|
99
|
+
var getDownloadURL = (options) => (keys) => {
|
|
100
|
+
return `${getURLPrefix(options)}${getKey(keys)}`;
|
|
105
101
|
};
|
|
106
|
-
var
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
102
|
+
var uploadFile = (client, options) => async (keys, {
|
|
103
|
+
url,
|
|
104
|
+
blob
|
|
105
|
+
}) => {
|
|
106
|
+
if (!blob && !url) return null;
|
|
107
|
+
const fileBlob = blob || await fetch(url).then((file) => file.blob());
|
|
108
|
+
if (fileBlob) {
|
|
109
|
+
const key = getKey(keys);
|
|
110
|
+
await uploadBlob(client, options.bucket)(fileBlob, key);
|
|
111
|
+
return getDownloadURL(options)(key);
|
|
111
112
|
}
|
|
112
113
|
return null;
|
|
113
114
|
};
|
|
114
|
-
var
|
|
115
|
+
var deleteFile = (client, options) => async (url) => {
|
|
115
116
|
const key = url.split(getURLPrefix(options))[1];
|
|
116
117
|
if (key) {
|
|
117
118
|
try {
|
|
@@ -128,7 +129,7 @@ var deleteImage = (client, options) => async (url) => {
|
|
|
128
129
|
}
|
|
129
130
|
return false;
|
|
130
131
|
};
|
|
131
|
-
var
|
|
132
|
+
var uploadBlob = (client, Bucket) => async (file, key) => {
|
|
132
133
|
const fileBuffer = await file.arrayBuffer();
|
|
133
134
|
return client.send(
|
|
134
135
|
new import_client_s3.PutObjectCommand({
|
|
@@ -144,37 +145,29 @@ var uploadFile = (client, Bucket) => async (file, key) => {
|
|
|
144
145
|
|
|
145
146
|
// src/file/put.ts
|
|
146
147
|
var getFileUploadPutRoute = (options, client) => async (req) => {
|
|
147
|
-
const ctx =
|
|
148
|
-
if (!ctx?.
|
|
148
|
+
const ctx = getContext(options.refreshKey, options.signingKey, req);
|
|
149
|
+
if (!ctx?.userId || ctx.isRefreshID)
|
|
149
150
|
return import_server3.NextResponse.json({ error: "unauthorized" }, { status: 401 });
|
|
150
151
|
const formData = await req.formData();
|
|
151
|
-
const type = formData.get("type");
|
|
152
|
-
const sync = Boolean(formData.get("sync"));
|
|
153
152
|
const file = formData.get("file");
|
|
153
|
+
if (!file) return import_server3.NextResponse.json({ error: "no file" }, { status: 400 });
|
|
154
154
|
const data = formData.get("data");
|
|
155
|
-
const
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
if (!type || !ctx.accessUserId) return;
|
|
160
|
-
const { deleteURL, response } = await options.processFile({
|
|
161
|
-
url,
|
|
162
|
-
type,
|
|
163
|
-
userId: ctx.accessUserId,
|
|
164
|
-
data: typeof data === "string" ? JSON.parse(data) : void 0
|
|
165
|
-
});
|
|
166
|
-
if (deleteURL) await deleteImage(client, options)(deleteURL);
|
|
167
|
-
return response;
|
|
155
|
+
const inputData = {
|
|
156
|
+
type: formData.get("type") + "",
|
|
157
|
+
userId: ctx.userId,
|
|
158
|
+
data: typeof data === "string" ? JSON.parse(data) : void 0
|
|
168
159
|
};
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
160
|
+
const fileKey = options.getKey ? await options.getKey(inputData) : (0, import_uuid.v4)();
|
|
161
|
+
const url = getDownloadURL(options)(fileKey);
|
|
162
|
+
await uploadBlob(client, options.bucket)(file, fileKey);
|
|
163
|
+
const onUploadResponse = await options.onUpload({
|
|
164
|
+
...inputData,
|
|
165
|
+
url
|
|
166
|
+
});
|
|
167
|
+
return import_server3.NextResponse.json({
|
|
168
|
+
url,
|
|
169
|
+
onUploadResponse
|
|
170
|
+
});
|
|
178
171
|
};
|
|
179
172
|
|
|
180
173
|
// src/file/setup.ts
|
|
@@ -182,11 +175,10 @@ function setupFileUpload(options) {
|
|
|
182
175
|
const client = getS3Client(options);
|
|
183
176
|
return {
|
|
184
177
|
PUT: getFileUploadPutRoute(options, client),
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
uploadFile: uploadFile(client, options.bucket)
|
|
178
|
+
uploadFile: uploadFile(client, options),
|
|
179
|
+
deleteFile: deleteFile(client, options),
|
|
180
|
+
getUploadURL: getUploadURL(client, options.bucket),
|
|
181
|
+
getDownloadURL: getDownloadURL(options)
|
|
190
182
|
};
|
|
191
183
|
}
|
|
192
184
|
// Annotate the CommonJS export names for ESM import in node:
|
package/dist/file/setup.d.mts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import * as _aws_sdk_client_s3 from '@aws-sdk/client-s3';
|
|
2
1
|
import * as next_server from 'next/server';
|
|
3
2
|
|
|
4
3
|
interface SetupFileUploadOptions {
|
|
@@ -8,27 +7,32 @@ interface SetupFileUploadOptions {
|
|
|
8
7
|
bucket: string;
|
|
9
8
|
awsSecret: string;
|
|
10
9
|
awsKey: string;
|
|
11
|
-
|
|
10
|
+
getKey?: (data: {
|
|
11
|
+
type: string;
|
|
12
|
+
userId: number;
|
|
13
|
+
data: object;
|
|
14
|
+
}) => Promise<string>;
|
|
15
|
+
onUpload: (data: {
|
|
12
16
|
url: string | null;
|
|
13
17
|
type: string;
|
|
14
18
|
userId: number;
|
|
15
19
|
data: object;
|
|
16
|
-
}) => Promise<
|
|
17
|
-
deleteURL?: string;
|
|
18
|
-
response?: object;
|
|
19
|
-
}>;
|
|
20
|
+
}) => Promise<object>;
|
|
20
21
|
}
|
|
21
22
|
declare function setupFileUpload(options: SetupFileUploadOptions): {
|
|
22
23
|
PUT: (req: next_server.NextRequest) => Promise<next_server.NextResponse<{
|
|
23
24
|
error: string;
|
|
24
25
|
}> | next_server.NextResponse<{
|
|
25
|
-
url: string
|
|
26
|
+
url: string;
|
|
27
|
+
onUploadResponse: object;
|
|
26
28
|
}>>;
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
29
|
+
uploadFile: (keys: string | string[], { url, blob, }: {
|
|
30
|
+
blob?: Blob;
|
|
31
|
+
url?: string;
|
|
32
|
+
}) => Promise<string | null>;
|
|
33
|
+
deleteFile: (url: string) => Promise<boolean>;
|
|
34
|
+
getUploadURL: (keys: string | string[]) => Promise<string>;
|
|
35
|
+
getDownloadURL: (keys: string | string[]) => string;
|
|
32
36
|
};
|
|
33
37
|
|
|
34
38
|
export { type SetupFileUploadOptions, setupFileUpload };
|
package/dist/file/setup.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import * as _aws_sdk_client_s3 from '@aws-sdk/client-s3';
|
|
2
1
|
import * as next_server from 'next/server';
|
|
3
2
|
|
|
4
3
|
interface SetupFileUploadOptions {
|
|
@@ -8,27 +7,32 @@ interface SetupFileUploadOptions {
|
|
|
8
7
|
bucket: string;
|
|
9
8
|
awsSecret: string;
|
|
10
9
|
awsKey: string;
|
|
11
|
-
|
|
10
|
+
getKey?: (data: {
|
|
11
|
+
type: string;
|
|
12
|
+
userId: number;
|
|
13
|
+
data: object;
|
|
14
|
+
}) => Promise<string>;
|
|
15
|
+
onUpload: (data: {
|
|
12
16
|
url: string | null;
|
|
13
17
|
type: string;
|
|
14
18
|
userId: number;
|
|
15
19
|
data: object;
|
|
16
|
-
}) => Promise<
|
|
17
|
-
deleteURL?: string;
|
|
18
|
-
response?: object;
|
|
19
|
-
}>;
|
|
20
|
+
}) => Promise<object>;
|
|
20
21
|
}
|
|
21
22
|
declare function setupFileUpload(options: SetupFileUploadOptions): {
|
|
22
23
|
PUT: (req: next_server.NextRequest) => Promise<next_server.NextResponse<{
|
|
23
24
|
error: string;
|
|
24
25
|
}> | next_server.NextResponse<{
|
|
25
|
-
url: string
|
|
26
|
+
url: string;
|
|
27
|
+
onUploadResponse: object;
|
|
26
28
|
}>>;
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
29
|
+
uploadFile: (keys: string | string[], { url, blob, }: {
|
|
30
|
+
blob?: Blob;
|
|
31
|
+
url?: string;
|
|
32
|
+
}) => Promise<string | null>;
|
|
33
|
+
deleteFile: (url: string) => Promise<boolean>;
|
|
34
|
+
getUploadURL: (keys: string | string[]) => Promise<string>;
|
|
35
|
+
getDownloadURL: (keys: string | string[]) => string;
|
|
32
36
|
};
|
|
33
37
|
|
|
34
38
|
export { type SetupFileUploadOptions, setupFileUpload };
|
package/dist/file/setup.esm.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
// src/file/put.ts
|
|
2
|
-
import { waitUntil as waitUntil2 } from "@vercel/functions";
|
|
3
2
|
import { NextResponse as NextResponse3 } from "next/server";
|
|
4
3
|
import { v4 } from "uuid";
|
|
5
4
|
|
|
@@ -27,37 +26,35 @@ function getUserIdFromRefreshToken(refreshKey, refreshToken) {
|
|
|
27
26
|
import { NextResponse as NextResponse2 } from "next/server";
|
|
28
27
|
|
|
29
28
|
// src/auth/email/utils.ts
|
|
30
|
-
var
|
|
29
|
+
var getContext = (refreshKey, signingKey, req) => {
|
|
31
30
|
const bearer = req.headers.get("authorization");
|
|
32
31
|
if (!bearer) {
|
|
33
32
|
const refresh = req.cookies.get("refresh")?.value;
|
|
34
33
|
const userId = getUserIdFromRefreshToken(refreshKey, refresh);
|
|
35
|
-
if (userId) return {
|
|
36
|
-
return null;
|
|
34
|
+
if (userId) return { userId, isRefreshID: true };
|
|
35
|
+
return { userId: null };
|
|
37
36
|
}
|
|
38
37
|
const token = bearer.slice(7);
|
|
39
38
|
try {
|
|
40
39
|
const res = verify2(token, signingKey);
|
|
41
40
|
if (typeof res === "string") {
|
|
42
|
-
return null;
|
|
41
|
+
return { userId: null };
|
|
43
42
|
}
|
|
44
43
|
return {
|
|
45
|
-
|
|
44
|
+
userId: res.id
|
|
46
45
|
};
|
|
47
46
|
} catch {
|
|
48
47
|
}
|
|
49
|
-
return null;
|
|
48
|
+
return { userId: null };
|
|
50
49
|
};
|
|
51
50
|
|
|
52
51
|
// src/file/utils.ts
|
|
53
|
-
import { createHash } from "crypto";
|
|
54
52
|
import {
|
|
55
53
|
DeleteObjectCommand,
|
|
56
54
|
PutObjectCommand,
|
|
57
55
|
S3Client
|
|
58
56
|
} from "@aws-sdk/client-s3";
|
|
59
57
|
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
|
|
60
|
-
import { waitUntil } from "@vercel/functions";
|
|
61
58
|
var getS3Client = (options) => new S3Client({
|
|
62
59
|
region: options.region,
|
|
63
60
|
credentials: {
|
|
@@ -66,30 +63,34 @@ var getS3Client = (options) => new S3Client({
|
|
|
66
63
|
}
|
|
67
64
|
});
|
|
68
65
|
var getURLPrefix = (options) => `https://${options.bucket}.s3.${options.region}.amazonaws.com/`;
|
|
69
|
-
function
|
|
70
|
-
return
|
|
66
|
+
function getKey(keys) {
|
|
67
|
+
return typeof keys === "string" ? keys : keys.join("/");
|
|
71
68
|
}
|
|
72
|
-
var
|
|
69
|
+
var getUploadURL = (client, Bucket) => (keys) => {
|
|
73
70
|
const command = new PutObjectCommand({
|
|
74
71
|
Bucket,
|
|
75
|
-
Key:
|
|
76
|
-
ACL:
|
|
72
|
+
Key: getKey(keys),
|
|
73
|
+
ACL: "public-read"
|
|
77
74
|
});
|
|
78
75
|
return getSignedUrl(client, command, { expiresIn: 300 });
|
|
79
76
|
};
|
|
80
|
-
var
|
|
81
|
-
|
|
82
|
-
return `${getURLPrefix(options)}${getHash(keys)}`;
|
|
77
|
+
var getDownloadURL = (options) => (keys) => {
|
|
78
|
+
return `${getURLPrefix(options)}${getKey(keys)}`;
|
|
83
79
|
};
|
|
84
|
-
var
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
80
|
+
var uploadFile = (client, options) => async (keys, {
|
|
81
|
+
url,
|
|
82
|
+
blob
|
|
83
|
+
}) => {
|
|
84
|
+
if (!blob && !url) return null;
|
|
85
|
+
const fileBlob = blob || await fetch(url).then((file) => file.blob());
|
|
86
|
+
if (fileBlob) {
|
|
87
|
+
const key = getKey(keys);
|
|
88
|
+
await uploadBlob(client, options.bucket)(fileBlob, key);
|
|
89
|
+
return getDownloadURL(options)(key);
|
|
89
90
|
}
|
|
90
91
|
return null;
|
|
91
92
|
};
|
|
92
|
-
var
|
|
93
|
+
var deleteFile = (client, options) => async (url) => {
|
|
93
94
|
const key = url.split(getURLPrefix(options))[1];
|
|
94
95
|
if (key) {
|
|
95
96
|
try {
|
|
@@ -106,7 +107,7 @@ var deleteImage = (client, options) => async (url) => {
|
|
|
106
107
|
}
|
|
107
108
|
return false;
|
|
108
109
|
};
|
|
109
|
-
var
|
|
110
|
+
var uploadBlob = (client, Bucket) => async (file, key) => {
|
|
110
111
|
const fileBuffer = await file.arrayBuffer();
|
|
111
112
|
return client.send(
|
|
112
113
|
new PutObjectCommand({
|
|
@@ -122,37 +123,29 @@ var uploadFile = (client, Bucket) => async (file, key) => {
|
|
|
122
123
|
|
|
123
124
|
// src/file/put.ts
|
|
124
125
|
var getFileUploadPutRoute = (options, client) => async (req) => {
|
|
125
|
-
const ctx =
|
|
126
|
-
if (!ctx?.
|
|
126
|
+
const ctx = getContext(options.refreshKey, options.signingKey, req);
|
|
127
|
+
if (!ctx?.userId || ctx.isRefreshID)
|
|
127
128
|
return NextResponse3.json({ error: "unauthorized" }, { status: 401 });
|
|
128
129
|
const formData = await req.formData();
|
|
129
|
-
const type = formData.get("type");
|
|
130
|
-
const sync = Boolean(formData.get("sync"));
|
|
131
130
|
const file = formData.get("file");
|
|
131
|
+
if (!file) return NextResponse3.json({ error: "no file" }, { status: 400 });
|
|
132
132
|
const data = formData.get("data");
|
|
133
|
-
const
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
if (!type || !ctx.accessUserId) return;
|
|
138
|
-
const { deleteURL, response } = await options.processFile({
|
|
139
|
-
url,
|
|
140
|
-
type,
|
|
141
|
-
userId: ctx.accessUserId,
|
|
142
|
-
data: typeof data === "string" ? JSON.parse(data) : void 0
|
|
143
|
-
});
|
|
144
|
-
if (deleteURL) await deleteImage(client, options)(deleteURL);
|
|
145
|
-
return response;
|
|
133
|
+
const inputData = {
|
|
134
|
+
type: formData.get("type") + "",
|
|
135
|
+
userId: ctx.userId,
|
|
136
|
+
data: typeof data === "string" ? JSON.parse(data) : void 0
|
|
146
137
|
};
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
138
|
+
const fileKey = options.getKey ? await options.getKey(inputData) : v4();
|
|
139
|
+
const url = getDownloadURL(options)(fileKey);
|
|
140
|
+
await uploadBlob(client, options.bucket)(file, fileKey);
|
|
141
|
+
const onUploadResponse = await options.onUpload({
|
|
142
|
+
...inputData,
|
|
143
|
+
url
|
|
144
|
+
});
|
|
145
|
+
return NextResponse3.json({
|
|
146
|
+
url,
|
|
147
|
+
onUploadResponse
|
|
148
|
+
});
|
|
156
149
|
};
|
|
157
150
|
|
|
158
151
|
// src/file/setup.ts
|
|
@@ -160,11 +153,10 @@ function setupFileUpload(options) {
|
|
|
160
153
|
const client = getS3Client(options);
|
|
161
154
|
return {
|
|
162
155
|
PUT: getFileUploadPutRoute(options, client),
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
uploadFile: uploadFile(client, options.bucket)
|
|
156
|
+
uploadFile: uploadFile(client, options),
|
|
157
|
+
deleteFile: deleteFile(client, options),
|
|
158
|
+
getUploadURL: getUploadURL(client, options.bucket),
|
|
159
|
+
getDownloadURL: getDownloadURL(options)
|
|
168
160
|
};
|
|
169
161
|
}
|
|
170
162
|
export {
|
package/dist/file/utils.cjs.js
CHANGED
|
@@ -20,18 +20,16 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/file/utils.ts
|
|
21
21
|
var utils_exports = {};
|
|
22
22
|
__export(utils_exports, {
|
|
23
|
-
|
|
24
|
-
|
|
23
|
+
deleteFile: () => deleteFile,
|
|
24
|
+
getDownloadURL: () => getDownloadURL,
|
|
25
25
|
getS3Client: () => getS3Client,
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
26
|
+
getUploadURL: () => getUploadURL,
|
|
27
|
+
uploadBlob: () => uploadBlob,
|
|
28
|
+
uploadFile: () => uploadFile
|
|
29
29
|
});
|
|
30
30
|
module.exports = __toCommonJS(utils_exports);
|
|
31
|
-
var import_node_crypto = require("crypto");
|
|
32
31
|
var import_client_s3 = require("@aws-sdk/client-s3");
|
|
33
32
|
var import_s3_request_presigner = require("@aws-sdk/s3-request-presigner");
|
|
34
|
-
var import_functions = require("@vercel/functions");
|
|
35
33
|
var getS3Client = (options) => new import_client_s3.S3Client({
|
|
36
34
|
region: options.region,
|
|
37
35
|
credentials: {
|
|
@@ -40,30 +38,34 @@ var getS3Client = (options) => new import_client_s3.S3Client({
|
|
|
40
38
|
}
|
|
41
39
|
});
|
|
42
40
|
var getURLPrefix = (options) => `https://${options.bucket}.s3.${options.region}.amazonaws.com/`;
|
|
43
|
-
function
|
|
44
|
-
return
|
|
41
|
+
function getKey(keys) {
|
|
42
|
+
return typeof keys === "string" ? keys : keys.join("/");
|
|
45
43
|
}
|
|
46
|
-
var
|
|
44
|
+
var getUploadURL = (client, Bucket) => (keys) => {
|
|
47
45
|
const command = new import_client_s3.PutObjectCommand({
|
|
48
46
|
Bucket,
|
|
49
|
-
Key:
|
|
50
|
-
ACL:
|
|
47
|
+
Key: getKey(keys),
|
|
48
|
+
ACL: "public-read"
|
|
51
49
|
});
|
|
52
50
|
return (0, import_s3_request_presigner.getSignedUrl)(client, command, { expiresIn: 300 });
|
|
53
51
|
};
|
|
54
|
-
var
|
|
55
|
-
|
|
56
|
-
return `${getURLPrefix(options)}${getHash(keys)}`;
|
|
52
|
+
var getDownloadURL = (options) => (keys) => {
|
|
53
|
+
return `${getURLPrefix(options)}${getKey(keys)}`;
|
|
57
54
|
};
|
|
58
|
-
var
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
55
|
+
var uploadFile = (client, options) => async (keys, {
|
|
56
|
+
url,
|
|
57
|
+
blob
|
|
58
|
+
}) => {
|
|
59
|
+
if (!blob && !url) return null;
|
|
60
|
+
const fileBlob = blob || await fetch(url).then((file) => file.blob());
|
|
61
|
+
if (fileBlob) {
|
|
62
|
+
const key = getKey(keys);
|
|
63
|
+
await uploadBlob(client, options.bucket)(fileBlob, key);
|
|
64
|
+
return getDownloadURL(options)(key);
|
|
63
65
|
}
|
|
64
66
|
return null;
|
|
65
67
|
};
|
|
66
|
-
var
|
|
68
|
+
var deleteFile = (client, options) => async (url) => {
|
|
67
69
|
const key = url.split(getURLPrefix(options))[1];
|
|
68
70
|
if (key) {
|
|
69
71
|
try {
|
|
@@ -80,7 +82,7 @@ var deleteImage = (client, options) => async (url) => {
|
|
|
80
82
|
}
|
|
81
83
|
return false;
|
|
82
84
|
};
|
|
83
|
-
var
|
|
85
|
+
var uploadBlob = (client, Bucket) => async (file, key) => {
|
|
84
86
|
const fileBuffer = await file.arrayBuffer();
|
|
85
87
|
return client.send(
|
|
86
88
|
new import_client_s3.PutObjectCommand({
|
|
@@ -95,10 +97,10 @@ var uploadFile = (client, Bucket) => async (file, key) => {
|
|
|
95
97
|
};
|
|
96
98
|
// Annotate the CommonJS export names for ESM import in node:
|
|
97
99
|
0 && (module.exports = {
|
|
98
|
-
|
|
99
|
-
|
|
100
|
+
deleteFile,
|
|
101
|
+
getDownloadURL,
|
|
100
102
|
getS3Client,
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
103
|
+
getUploadURL,
|
|
104
|
+
uploadBlob,
|
|
105
|
+
uploadFile
|
|
104
106
|
});
|
package/dist/file/utils.d.mts
CHANGED
|
@@ -4,10 +4,13 @@ import { SetupFileUploadOptions } from './setup.mjs';
|
|
|
4
4
|
import 'next/server';
|
|
5
5
|
|
|
6
6
|
declare const getS3Client: (options: SetupFileUploadOptions) => S3Client;
|
|
7
|
-
declare const
|
|
8
|
-
declare const
|
|
9
|
-
declare const
|
|
10
|
-
|
|
11
|
-
|
|
7
|
+
declare const getUploadURL: (client: S3Client, Bucket: string) => (keys: string | string[]) => Promise<string>;
|
|
8
|
+
declare const getDownloadURL: (options: SetupFileUploadOptions) => (keys: string | string[]) => string;
|
|
9
|
+
declare const uploadFile: (client: S3Client, options: SetupFileUploadOptions) => (keys: string | string[], { url, blob, }: {
|
|
10
|
+
blob?: Blob;
|
|
11
|
+
url?: string;
|
|
12
|
+
}) => Promise<string | null>;
|
|
13
|
+
declare const deleteFile: (client: S3Client, options: SetupFileUploadOptions) => (url: string) => Promise<boolean>;
|
|
14
|
+
declare const uploadBlob: (client: S3Client, Bucket: string) => (file: File | Blob, key: string) => Promise<_aws_sdk_client_s3.PutObjectCommandOutput>;
|
|
12
15
|
|
|
13
|
-
export {
|
|
16
|
+
export { deleteFile, getDownloadURL, getS3Client, getUploadURL, uploadBlob, uploadFile };
|
package/dist/file/utils.d.ts
CHANGED
|
@@ -4,10 +4,13 @@ import { SetupFileUploadOptions } from './setup.js';
|
|
|
4
4
|
import 'next/server';
|
|
5
5
|
|
|
6
6
|
declare const getS3Client: (options: SetupFileUploadOptions) => S3Client;
|
|
7
|
-
declare const
|
|
8
|
-
declare const
|
|
9
|
-
declare const
|
|
10
|
-
|
|
11
|
-
|
|
7
|
+
declare const getUploadURL: (client: S3Client, Bucket: string) => (keys: string | string[]) => Promise<string>;
|
|
8
|
+
declare const getDownloadURL: (options: SetupFileUploadOptions) => (keys: string | string[]) => string;
|
|
9
|
+
declare const uploadFile: (client: S3Client, options: SetupFileUploadOptions) => (keys: string | string[], { url, blob, }: {
|
|
10
|
+
blob?: Blob;
|
|
11
|
+
url?: string;
|
|
12
|
+
}) => Promise<string | null>;
|
|
13
|
+
declare const deleteFile: (client: S3Client, options: SetupFileUploadOptions) => (url: string) => Promise<boolean>;
|
|
14
|
+
declare const uploadBlob: (client: S3Client, Bucket: string) => (file: File | Blob, key: string) => Promise<_aws_sdk_client_s3.PutObjectCommandOutput>;
|
|
12
15
|
|
|
13
|
-
export {
|
|
16
|
+
export { deleteFile, getDownloadURL, getS3Client, getUploadURL, uploadBlob, uploadFile };
|