@yongdall/fs-s3 0.1.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/hooks.yongdall.mjs +10 -0
- package/hooks.yongdall.mjs.map +1 -0
- package/index.d.mts +11 -0
- package/index.mjs +6 -0
- package/index.mjs.map +1 -0
- package/package.json +24 -0
- package/s3-f2L38N0p.mjs +66 -0
- package/s3-f2L38N0p.mjs.map +1 -0
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import s3 from "#index";
|
|
2
|
+
|
|
3
|
+
//#region providers/fs-s3/hooks.yongdall.mjs
|
|
4
|
+
/** @import { Hooks } from '@yongdall/connection' */
|
|
5
|
+
/** @type {Hooks.providers} */
|
|
6
|
+
const providers = { fs: { s3 } };
|
|
7
|
+
|
|
8
|
+
//#endregion
|
|
9
|
+
export { providers };
|
|
10
|
+
//# sourceMappingURL=hooks.yongdall.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hooks.yongdall.mjs","names":[],"sources":["../../providers/fs-s3/hooks.yongdall.mjs"],"sourcesContent":["import s3 from '#index';\n/** @import { Hooks } from '@yongdall/connection' */\n/** @type {Hooks.providers} */\nexport const providers = {\n\tfs: { s3 },\n};\n"],"mappings":";;;;;AAGA,MAAa,YAAY,EACxB,IAAI,EAAE,IAAI,EACV"}
|
package/index.d.mts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import * as stream0 from "stream";
|
|
2
|
+
|
|
3
|
+
//#region providers/fs-s3/index.d.mts
|
|
4
|
+
declare function _default(url: string): Promise<{
|
|
5
|
+
urlRead(path: string, expiresIn?: number): Promise<string>;
|
|
6
|
+
urlWrite(path: string, expiresIn?: number): Promise<string>;
|
|
7
|
+
read(path: string): Promise<ReadableStream>;
|
|
8
|
+
write(path: string, data: ReadableStream | Blob | ArrayBufferView<ArrayBuffer> | ArrayBuffer | string | Buffer<ArrayBuffer> | stream0.Readable): Promise<void>;
|
|
9
|
+
}>;
|
|
10
|
+
//#endregion
|
|
11
|
+
export { _default as default };
|
package/index.mjs
ADDED
package/index.mjs.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../../providers/fs-s3/index.mjs"],"sourcesContent":["export default (/** @type {string} */url) => import('./s3.mjs').then(v => v.default(url));\n"],"mappings":";AAAA,qBAAqC,QAAQ,OAAO,qBAAY,MAAK,MAAK,EAAE,QAAQ,IAAI,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@yongdall/fs-s3",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"main": "./index.mjs",
|
|
5
|
+
"exports": {
|
|
6
|
+
".": "./index.mjs"
|
|
7
|
+
},
|
|
8
|
+
"imports": {
|
|
9
|
+
"#index": "./index.mjs"
|
|
10
|
+
},
|
|
11
|
+
"version": "0.1.0",
|
|
12
|
+
"description": "",
|
|
13
|
+
"keywords": [],
|
|
14
|
+
"author": "",
|
|
15
|
+
"license": "ISC",
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"@aws-sdk/client-s3": "^3.958.0",
|
|
18
|
+
"@aws-sdk/s3-request-presigner": "^3.958.0",
|
|
19
|
+
"@smithy/node-http-handler": "^4.4.7"
|
|
20
|
+
},
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"@yongdall/connection": "^0.1.0"
|
|
23
|
+
}
|
|
24
|
+
}
|
package/s3-f2L38N0p.mjs
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import "node:stream";
|
|
2
|
+
import { GetObjectCommand, PutObjectCommand, S3Client } from "@aws-sdk/client-s3";
|
|
3
|
+
import { NodeHttpHandler } from "@smithy/node-http-handler";
|
|
4
|
+
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
|
|
5
|
+
|
|
6
|
+
//#region providers/fs-s3/s3.mjs
|
|
7
|
+
/**
|
|
8
|
+
*
|
|
9
|
+
* @param {string[]} base
|
|
10
|
+
* @param {string} root
|
|
11
|
+
* @returns {{Bucket: string, Key: string}}
|
|
12
|
+
*/
|
|
13
|
+
function tPath(base, root) {
|
|
14
|
+
const [bucket, ...path] = [...base, ...root.split(/#|\?/, 1)[0].split("/").filter(Boolean)];
|
|
15
|
+
return {
|
|
16
|
+
Bucket: bucket,
|
|
17
|
+
Key: path.join("/")
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
*
|
|
22
|
+
* @param {string} uri
|
|
23
|
+
* @returns
|
|
24
|
+
*/
|
|
25
|
+
function s3_default(uri) {
|
|
26
|
+
const url = new URL(uri);
|
|
27
|
+
const endPoint = new URL(url.protocol.includes("https") ? `https://a` : `http://a`);
|
|
28
|
+
endPoint.hostname = url.hostname;
|
|
29
|
+
endPoint.port = url.port;
|
|
30
|
+
const s3 = new S3Client({
|
|
31
|
+
endpoint: endPoint.href,
|
|
32
|
+
region: "us-east-1",
|
|
33
|
+
credentials: {
|
|
34
|
+
accessKeyId: url.username,
|
|
35
|
+
secretAccessKey: url.password
|
|
36
|
+
},
|
|
37
|
+
forcePathStyle: true,
|
|
38
|
+
requestHandler: new NodeHttpHandler({
|
|
39
|
+
connectionTimeout: 3e3,
|
|
40
|
+
socketTimeout: 5e3
|
|
41
|
+
})
|
|
42
|
+
});
|
|
43
|
+
const root = url.pathname.split("/").filter(Boolean);
|
|
44
|
+
return {
|
|
45
|
+
async urlRead(path, expiresIn = 3600 * 24) {
|
|
46
|
+
return await getSignedUrl(s3, new GetObjectCommand(tPath(root, path)), { expiresIn });
|
|
47
|
+
},
|
|
48
|
+
async urlWrite(path, expiresIn = 3600 * 24) {
|
|
49
|
+
return await getSignedUrl(s3, new PutObjectCommand(tPath(root, path)), { expiresIn });
|
|
50
|
+
},
|
|
51
|
+
async read(path) {
|
|
52
|
+
return (await s3.send(new GetObjectCommand(tPath(root, path)))).Body?.transformToWebStream();
|
|
53
|
+
},
|
|
54
|
+
async write(path, data) {
|
|
55
|
+
const Body = ArrayBuffer.isView(data) || data instanceof ArrayBuffer ? new Blob([data]) : data;
|
|
56
|
+
await s3.send(new PutObjectCommand({
|
|
57
|
+
...tPath(root, path),
|
|
58
|
+
Body
|
|
59
|
+
}));
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
//#endregion
|
|
65
|
+
export { s3_default as default };
|
|
66
|
+
//# sourceMappingURL=s3-f2L38N0p.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"s3-f2L38N0p.mjs","names":[],"sources":["../../providers/fs-s3/s3.mjs"],"sourcesContent":["import { Readable } from 'node:stream';\n\nimport { PutObjectCommand, S3Client } from \"@aws-sdk/client-s3\";\nimport { NodeHttpHandler } from \"@smithy/node-http-handler\";\nimport { GetObjectCommand } from \"@aws-sdk/client-s3\";\nimport { getSignedUrl } from \"@aws-sdk/s3-request-presigner\";\n\n/**\n * \n * @param {string[]} base \n * @param {string} root \n * @returns {{Bucket: string, Key: string}}\n */\nfunction tPath(base, root) {\n\tconst [bucket, ...path] = [...base, ...root.split(/#|\\?/, 1)[0].split('/').filter(Boolean)];\n\treturn { Bucket: bucket, Key: path.join('/') };\n}\n/**\n * \n * @param {string} uri\n * @returns \n */\nexport default function (uri) {\n\tconst url = new URL(uri);\n\tconst endPoint = new URL(url.protocol.includes('https') ? `https://a` : `http://a`);\n\tendPoint.hostname = url.hostname;\n\tendPoint.port = url.port;\n\tconst s3 = new S3Client({\n\t\tendpoint: endPoint.href,\n\t\tregion: \"us-east-1\", // 可随意填写\n\t\tcredentials: {\n\t\t\taccessKeyId: url.username,\n\t\t\tsecretAccessKey: url.password,\n\t\t},\n\t\tforcePathStyle: true,\n\t\trequestHandler: new NodeHttpHandler({\n\t\t\tconnectionTimeout: 3000,\n\t\t\tsocketTimeout: 5000,\n\t\t}),\n\t});\n\n\tconst root = url.pathname.split('/').filter(Boolean);\n\treturn {\n\t\t/**\n\t\t * \n\t\t * @param {string} path \n\t\t * @param {number} [expiresIn] \n\t\t * @returns {Promise<string>}\n\t\t */\n\t\tasync urlRead(path, expiresIn = 60 * 60 * 24) {\n\t\t\tconst url = await getSignedUrl(\n\t\t\t\ts3,\n\t\t\t\tnew GetObjectCommand(tPath(root, path)),\n\t\t\t\t{ expiresIn }\n\t\t\t);\n\n\t\t\treturn url;\n\t\t},\n\t\t/**\n\t\t * \n\t\t * @param {string} path \n\t\t * @param {number} [expiresIn] \n\t\t * @returns {Promise<string>}\n\t\t */\n\t\tasync urlWrite(path, expiresIn = 60 * 60 * 24) {\n\t\t\treturn await getSignedUrl(\n\t\t\t\ts3,\n\t\t\t\tnew PutObjectCommand(tPath(root, path)),\n\t\t\t\t{ expiresIn }\n\t\t\t);\n\t\t},\n\t\t/**\n\t\t * \n\t\t * @param {string} path \n\t\t * @returns {Promise<ReadableStream>}\n\t\t */\n\t\tasync read(path) {\n\t\t\tconst response = await s3.send(\n\t\t\t\tnew GetObjectCommand(tPath(root, path))\n\t\t\t);\n\t\t\t// @ts-ignore\n\t\t\treturn response.Body?.transformToWebStream();\n\t\t},\n\t\t/**\n\t\t * \n\t\t * @param {string} path \n\t\t * @param {ReadableStream | Blob | ArrayBufferView<ArrayBuffer> | ArrayBuffer | string | Buffer<ArrayBuffer> | Readable} data \n\t\t * @returns \n\t\t */\n\t\tasync write(path, data) {\n\t\t\tconst Body = ArrayBuffer.isView(data) || data instanceof ArrayBuffer\n\t\t\t\t? new Blob([data]) : data;\n\t\t\tawait s3.send(new PutObjectCommand({ ...tPath(root, path), Body }));\n\t\t},\n\t};\n}\n"],"mappings":";;;;;;;;;;;;AAaA,SAAS,MAAM,MAAM,MAAM;CAC1B,MAAM,CAAC,QAAQ,GAAG,QAAQ,CAAC,GAAG,MAAM,GAAG,KAAK,MAAM,QAAQ,EAAE,CAAC,GAAG,MAAM,IAAI,CAAC,OAAO,QAAQ,CAAC;AAC3F,QAAO;EAAE,QAAQ;EA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