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/index.cjs.js
CHANGED
|
@@ -25,7 +25,6 @@ __export(file_exports, {
|
|
|
25
25
|
module.exports = __toCommonJS(file_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/index.d.mts
CHANGED
package/dist/file/index.d.ts
CHANGED
package/dist/file/index.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/put.cjs.js
CHANGED
|
@@ -23,7 +23,6 @@ __export(put_exports, {
|
|
|
23
23
|
getFileUploadPutRoute: () => getFileUploadPutRoute
|
|
24
24
|
});
|
|
25
25
|
module.exports = __toCommonJS(put_exports);
|
|
26
|
-
var import_functions2 = require("@vercel/functions");
|
|
27
26
|
var import_server3 = require("next/server");
|
|
28
27
|
var import_uuid = require("uuid");
|
|
29
28
|
|
|
@@ -51,59 +50,39 @@ function getUserIdFromRefreshToken(refreshKey, refreshToken) {
|
|
|
51
50
|
var import_server2 = require("next/server");
|
|
52
51
|
|
|
53
52
|
// src/auth/email/utils.ts
|
|
54
|
-
var
|
|
53
|
+
var getContext = (refreshKey, signingKey, req) => {
|
|
55
54
|
const bearer = req.headers.get("authorization");
|
|
56
55
|
if (!bearer) {
|
|
57
56
|
const refresh = req.cookies.get("refresh")?.value;
|
|
58
57
|
const userId = getUserIdFromRefreshToken(refreshKey, refresh);
|
|
59
|
-
if (userId) return {
|
|
60
|
-
return null;
|
|
58
|
+
if (userId) return { userId, isRefreshID: true };
|
|
59
|
+
return { userId: null };
|
|
61
60
|
}
|
|
62
61
|
const token = bearer.slice(7);
|
|
63
62
|
try {
|
|
64
63
|
const res = (0, import_jsonwebtoken2.verify)(token, signingKey);
|
|
65
64
|
if (typeof res === "string") {
|
|
66
|
-
return null;
|
|
65
|
+
return { userId: null };
|
|
67
66
|
}
|
|
68
67
|
return {
|
|
69
|
-
|
|
68
|
+
userId: res.id
|
|
70
69
|
};
|
|
71
70
|
} catch {
|
|
72
71
|
}
|
|
73
|
-
return null;
|
|
72
|
+
return { userId: null };
|
|
74
73
|
};
|
|
75
74
|
|
|
76
75
|
// src/file/utils.ts
|
|
77
|
-
var import_node_crypto = require("crypto");
|
|
78
76
|
var import_client_s3 = require("@aws-sdk/client-s3");
|
|
79
77
|
var import_s3_request_presigner = require("@aws-sdk/s3-request-presigner");
|
|
80
|
-
var import_functions = require("@vercel/functions");
|
|
81
78
|
var getURLPrefix = (options) => `https://${options.bucket}.s3.${options.region}.amazonaws.com/`;
|
|
82
|
-
function
|
|
83
|
-
return
|
|
79
|
+
function getKey(keys) {
|
|
80
|
+
return typeof keys === "string" ? keys : keys.join("/");
|
|
84
81
|
}
|
|
85
|
-
var
|
|
86
|
-
|
|
87
|
-
return `${getURLPrefix(options)}${getHash(keys)}`;
|
|
88
|
-
};
|
|
89
|
-
var deleteImage = (client, options) => async (url) => {
|
|
90
|
-
const key = url.split(getURLPrefix(options))[1];
|
|
91
|
-
if (key) {
|
|
92
|
-
try {
|
|
93
|
-
await client.send(
|
|
94
|
-
new import_client_s3.DeleteObjectCommand({
|
|
95
|
-
Bucket: options.bucket,
|
|
96
|
-
Key: key
|
|
97
|
-
})
|
|
98
|
-
);
|
|
99
|
-
return true;
|
|
100
|
-
} catch (e) {
|
|
101
|
-
console.error("ERROR", url, e);
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
return false;
|
|
82
|
+
var getDownloadURL = (options) => (keys) => {
|
|
83
|
+
return `${getURLPrefix(options)}${getKey(keys)}`;
|
|
105
84
|
};
|
|
106
|
-
var
|
|
85
|
+
var uploadBlob = (client, Bucket) => async (file, key) => {
|
|
107
86
|
const fileBuffer = await file.arrayBuffer();
|
|
108
87
|
return client.send(
|
|
109
88
|
new import_client_s3.PutObjectCommand({
|
|
@@ -119,37 +98,29 @@ var uploadFile = (client, Bucket) => async (file, key) => {
|
|
|
119
98
|
|
|
120
99
|
// src/file/put.ts
|
|
121
100
|
var getFileUploadPutRoute = (options, client) => async (req) => {
|
|
122
|
-
const ctx =
|
|
123
|
-
if (!ctx?.
|
|
101
|
+
const ctx = getContext(options.refreshKey, options.signingKey, req);
|
|
102
|
+
if (!ctx?.userId || ctx.isRefreshID)
|
|
124
103
|
return import_server3.NextResponse.json({ error: "unauthorized" }, { status: 401 });
|
|
125
104
|
const formData = await req.formData();
|
|
126
|
-
const type = formData.get("type");
|
|
127
|
-
const sync = Boolean(formData.get("sync"));
|
|
128
105
|
const file = formData.get("file");
|
|
106
|
+
if (!file) return import_server3.NextResponse.json({ error: "no file" }, { status: 400 });
|
|
129
107
|
const data = formData.get("data");
|
|
130
|
-
const
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
if (!type || !ctx.accessUserId) return;
|
|
135
|
-
const { deleteURL, response } = await options.processFile({
|
|
136
|
-
url,
|
|
137
|
-
type,
|
|
138
|
-
userId: ctx.accessUserId,
|
|
139
|
-
data: typeof data === "string" ? JSON.parse(data) : void 0
|
|
140
|
-
});
|
|
141
|
-
if (deleteURL) await deleteImage(client, options)(deleteURL);
|
|
142
|
-
return response;
|
|
108
|
+
const inputData = {
|
|
109
|
+
type: formData.get("type") + "",
|
|
110
|
+
userId: ctx.userId,
|
|
111
|
+
data: typeof data === "string" ? JSON.parse(data) : void 0
|
|
143
112
|
};
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
113
|
+
const fileKey = options.getKey ? await options.getKey(inputData) : (0, import_uuid.v4)();
|
|
114
|
+
const url = getDownloadURL(options)(fileKey);
|
|
115
|
+
await uploadBlob(client, options.bucket)(file, fileKey);
|
|
116
|
+
const onUploadResponse = await options.onUpload({
|
|
117
|
+
...inputData,
|
|
118
|
+
url
|
|
119
|
+
});
|
|
120
|
+
return import_server3.NextResponse.json({
|
|
121
|
+
url,
|
|
122
|
+
onUploadResponse
|
|
123
|
+
});
|
|
153
124
|
};
|
|
154
125
|
// Annotate the CommonJS export names for ESM import in node:
|
|
155
126
|
0 && (module.exports = {
|
package/dist/file/put.d.mts
CHANGED
|
@@ -5,7 +5,8 @@ import { SetupFileUploadOptions } from './setup.mjs';
|
|
|
5
5
|
declare const getFileUploadPutRoute: (options: SetupFileUploadOptions, client: S3Client) => (req: NextRequest) => Promise<NextResponse<{
|
|
6
6
|
error: string;
|
|
7
7
|
}> | NextResponse<{
|
|
8
|
-
url: string
|
|
8
|
+
url: string;
|
|
9
|
+
onUploadResponse: object;
|
|
9
10
|
}>>;
|
|
10
11
|
|
|
11
12
|
export { getFileUploadPutRoute };
|
package/dist/file/put.d.ts
CHANGED
|
@@ -5,7 +5,8 @@ import { SetupFileUploadOptions } from './setup.js';
|
|
|
5
5
|
declare const getFileUploadPutRoute: (options: SetupFileUploadOptions, client: S3Client) => (req: NextRequest) => Promise<NextResponse<{
|
|
6
6
|
error: string;
|
|
7
7
|
}> | NextResponse<{
|
|
8
|
-
url: string
|
|
8
|
+
url: string;
|
|
9
|
+
onUploadResponse: object;
|
|
9
10
|
}>>;
|
|
10
11
|
|
|
11
12
|
export { getFileUploadPutRoute };
|
package/dist/file/put.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,63 +26,43 @@ 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 getURLPrefix = (options) => `https://${options.bucket}.s3.${options.region}.amazonaws.com/`;
|
|
62
|
-
function
|
|
63
|
-
return
|
|
59
|
+
function getKey(keys) {
|
|
60
|
+
return typeof keys === "string" ? keys : keys.join("/");
|
|
64
61
|
}
|
|
65
|
-
var
|
|
66
|
-
|
|
67
|
-
return `${getURLPrefix(options)}${getHash(keys)}`;
|
|
68
|
-
};
|
|
69
|
-
var deleteImage = (client, options) => async (url) => {
|
|
70
|
-
const key = url.split(getURLPrefix(options))[1];
|
|
71
|
-
if (key) {
|
|
72
|
-
try {
|
|
73
|
-
await client.send(
|
|
74
|
-
new DeleteObjectCommand({
|
|
75
|
-
Bucket: options.bucket,
|
|
76
|
-
Key: key
|
|
77
|
-
})
|
|
78
|
-
);
|
|
79
|
-
return true;
|
|
80
|
-
} catch (e) {
|
|
81
|
-
console.error("ERROR", url, e);
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
return false;
|
|
62
|
+
var getDownloadURL = (options) => (keys) => {
|
|
63
|
+
return `${getURLPrefix(options)}${getKey(keys)}`;
|
|
85
64
|
};
|
|
86
|
-
var
|
|
65
|
+
var uploadBlob = (client, Bucket) => async (file, key) => {
|
|
87
66
|
const fileBuffer = await file.arrayBuffer();
|
|
88
67
|
return client.send(
|
|
89
68
|
new PutObjectCommand({
|
|
@@ -99,37 +78,29 @@ var uploadFile = (client, Bucket) => async (file, key) => {
|
|
|
99
78
|
|
|
100
79
|
// src/file/put.ts
|
|
101
80
|
var getFileUploadPutRoute = (options, client) => async (req) => {
|
|
102
|
-
const ctx =
|
|
103
|
-
if (!ctx?.
|
|
81
|
+
const ctx = getContext(options.refreshKey, options.signingKey, req);
|
|
82
|
+
if (!ctx?.userId || ctx.isRefreshID)
|
|
104
83
|
return NextResponse3.json({ error: "unauthorized" }, { status: 401 });
|
|
105
84
|
const formData = await req.formData();
|
|
106
|
-
const type = formData.get("type");
|
|
107
|
-
const sync = Boolean(formData.get("sync"));
|
|
108
85
|
const file = formData.get("file");
|
|
86
|
+
if (!file) return NextResponse3.json({ error: "no file" }, { status: 400 });
|
|
109
87
|
const data = formData.get("data");
|
|
110
|
-
const
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
if (!type || !ctx.accessUserId) return;
|
|
115
|
-
const { deleteURL, response } = await options.processFile({
|
|
116
|
-
url,
|
|
117
|
-
type,
|
|
118
|
-
userId: ctx.accessUserId,
|
|
119
|
-
data: typeof data === "string" ? JSON.parse(data) : void 0
|
|
120
|
-
});
|
|
121
|
-
if (deleteURL) await deleteImage(client, options)(deleteURL);
|
|
122
|
-
return response;
|
|
88
|
+
const inputData = {
|
|
89
|
+
type: formData.get("type") + "",
|
|
90
|
+
userId: ctx.userId,
|
|
91
|
+
data: typeof data === "string" ? JSON.parse(data) : void 0
|
|
123
92
|
};
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
93
|
+
const fileKey = options.getKey ? await options.getKey(inputData) : v4();
|
|
94
|
+
const url = getDownloadURL(options)(fileKey);
|
|
95
|
+
await uploadBlob(client, options.bucket)(file, fileKey);
|
|
96
|
+
const onUploadResponse = await options.onUpload({
|
|
97
|
+
...inputData,
|
|
98
|
+
url
|
|
99
|
+
});
|
|
100
|
+
return NextResponse3.json({
|
|
101
|
+
url,
|
|
102
|
+
onUploadResponse
|
|
103
|
+
});
|
|
133
104
|
};
|
|
134
105
|
export {
|
|
135
106
|
getFileUploadPutRoute
|