@ysk-kit/storage 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/dist/index.d.ts +33 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +38 -0
- package/dist/index.js.map +1 -0
- package/dist/s3.d.ts +9 -0
- package/dist/s3.d.ts.map +1 -0
- package/dist/s3.js +26 -0
- package/dist/s3.js.map +1 -0
- package/package.json +34 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 YSK Limited
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export type PresignPutInput = {
|
|
2
|
+
key: string;
|
|
3
|
+
mime: string;
|
|
4
|
+
byteSize: number;
|
|
5
|
+
};
|
|
6
|
+
export type PresignResult = {
|
|
7
|
+
url: string;
|
|
8
|
+
headers: Record<string, string>;
|
|
9
|
+
};
|
|
10
|
+
export interface IStoragePort {
|
|
11
|
+
presignPut(input: PresignPutInput): Promise<PresignResult>;
|
|
12
|
+
presignGet(key: string): Promise<PresignResult>;
|
|
13
|
+
putLocal?(key: string, body: Buffer, mime: string): Promise<void>;
|
|
14
|
+
getLocal?(key: string): Promise<{
|
|
15
|
+
body: Buffer;
|
|
16
|
+
mime: string;
|
|
17
|
+
} | null>;
|
|
18
|
+
}
|
|
19
|
+
import { createS3Storage } from './s3';
|
|
20
|
+
export { createS3Storage };
|
|
21
|
+
export type StorageEnv = {
|
|
22
|
+
API_PUBLIC_URL: string;
|
|
23
|
+
S3_ENDPOINT?: string | undefined;
|
|
24
|
+
S3_BUCKET?: string | undefined;
|
|
25
|
+
S3_ACCESS_KEY?: string | undefined;
|
|
26
|
+
S3_SECRET_KEY?: string | undefined;
|
|
27
|
+
S3_REGION?: string | undefined;
|
|
28
|
+
};
|
|
29
|
+
export declare const createStorageFromEnv: (env: StorageEnv) => IStoragePort;
|
|
30
|
+
export declare const createLocalStorage: (opts: {
|
|
31
|
+
publicBaseUrl: string;
|
|
32
|
+
}) => IStoragePort;
|
|
33
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,eAAe,GAAG;IAAE,GAAG,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAA;CAAE,CAAC;AAC9E,MAAM,MAAM,aAAa,GAAG;IAAE,GAAG,EAAE,MAAM,CAAC;IAA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env.S3_ENDPOINT && env.S3_BUCKET && env.S3_ACCESS_KEY && env.S3_SECRET_KEY) {
|
|
5
|
+
return createS3Storage({
|
|
6
|
+
endpoint: env.S3_ENDPOINT,
|
|
7
|
+
bucket: env.S3_BUCKET,
|
|
8
|
+
accessKey: env.S3_ACCESS_KEY,
|
|
9
|
+
secretKey: env.S3_SECRET_KEY,
|
|
10
|
+
region: env.S3_REGION && env.S3_REGION.length > 0 ? env.S3_REGION : 'auto',
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
return createLocalStorage({ publicBaseUrl: env.API_PUBLIC_URL });
|
|
14
|
+
};
|
|
15
|
+
export const createLocalStorage = (opts) => {
|
|
16
|
+
const blobs = new Map();
|
|
17
|
+
return {
|
|
18
|
+
async presignPut(input) {
|
|
19
|
+
return {
|
|
20
|
+
url: `${opts.publicBaseUrl}/v1/files/local/${encodeURIComponent(input.key)}`,
|
|
21
|
+
headers: { 'content-type': input.mime },
|
|
22
|
+
};
|
|
23
|
+
},
|
|
24
|
+
async presignGet(key) {
|
|
25
|
+
return {
|
|
26
|
+
url: `${opts.publicBaseUrl}/v1/files/local/${encodeURIComponent(key)}`,
|
|
27
|
+
headers: {},
|
|
28
|
+
};
|
|
29
|
+
},
|
|
30
|
+
async putLocal(key, body, mime) {
|
|
31
|
+
blobs.set(key, { body, mime });
|
|
32
|
+
},
|
|
33
|
+
async getLocal(key) {
|
|
34
|
+
return blobs.get(key) ?? null;
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAUA,OAAO,EAAE,eAAe,EAAE,MAAM,MAAM,CAAC;AAEvC,OAAO,EAAE,eAAe,EAAE,CAAC;AAW3B,MAAM,CAAC,MAAM,oBAAoB,GAAG,CAAC,GAAe,EAAgB,EAAE;IACpE,IAAI,GA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AAC;QACD,KAAK,CAAC,QAAQ,CAAC,GAAG;YAChB,OAAO,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,IAAI,CAAC;QAChC,CAAC;KACF,CAAC;AACJ,CAAC,CAAC"}
|
package/dist/s3.d.ts
ADDED
package/dist/s3.d.ts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"s3.d.ts","sourceRoot":"","sources":["../src/s3.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAE5C,eAAO,MAAM,eAAe,GAAI,MAAM;IACpC,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC;IAClB,MAAM,EAAE,MAAM,CAAC;CAChB,KAAG,YA8BH,CAAC"}
|
package/dist/s3.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { GetObjectCommand, PutObjectCommand, S3Client } from '@aws-sdk/client-s3';
|
|
2
|
+
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
|
|
3
|
+
export const createS3Storage = (opts) => {
|
|
4
|
+
const client = new S3Client({
|
|
5
|
+
endpoint: opts.endpoint,
|
|
6
|
+
region: opts.region,
|
|
7
|
+
credentials: { accessKeyId: opts.accessKey, secretAccessKey: opts.secretKey },
|
|
8
|
+
forcePathStyle: true,
|
|
9
|
+
});
|
|
10
|
+
return {
|
|
11
|
+
async presignPut(input) {
|
|
12
|
+
const url = await getSignedUrl(client, new PutObjectCommand({
|
|
13
|
+
Bucket: opts.bucket,
|
|
14
|
+
Key: input.key,
|
|
15
|
+
ContentType: input.mime,
|
|
16
|
+
ContentLength: input.byteSize,
|
|
17
|
+
}), { expiresIn: 600 });
|
|
18
|
+
return { url, headers: { 'content-type': input.mime } };
|
|
19
|
+
},
|
|
20
|
+
async presignGet(key) {
|
|
21
|
+
const url = await getSignedUrl(client, new GetObjectCommand({ Bucket: opts.bucket, Key: key }), { expiresIn: 600 });
|
|
22
|
+
return { url, headers: {} };
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
//# sourceMappingURL=s3.js.map
|
package/dist/s3.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"s3.js","sourceRoot":"","sources":["../src/s3.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAClF,OAAO,EAAE,YAAY,EAAE,MAAM,+BAA+B,CAAC;AAG7D,MAAM,CAAC,MAAM,eAAe,GAAG,CAAC,IAM/B,EAAgB,EAAE;IACjB,MAAM,MAAM,GAAG,IAAI,QAAQ,CAAC;QAC1B,QAAQ,EAAE,IAAI,CAAC,QAAQ;QACvB,MAAM,EAAE,IAAI,CAAC,MAAM;QACnB,WAAW,EAAE,EAAE,WAAW,EAAE,IAAI,CAAC,SAAS,EAAE,eAAe,EAAE,IAAI,CAAC,SAAS,EAAE;QAC7E,cAAc,EAAE,IAAI;KACrB,CAAC,CAAC;IACH,OAAO;QACL,KAAK,CAAC,UAAU,CAAC,KAAK;YACpB,MAAM,GAAG,GAAG,MAAM,YAAY,CAC5B,MAAM,EACN,IAAI,gBAAgB,CAAC;gBACnB,MAAM,EAAE,IAAI,CAAC,MAAM;gBACnB,GAAG,EAAE,KAAK,CAAC,GAAG;gBACd,WAAW,EAAE,KAAK,CAAC,IAAI;gBACvB,aAAa,EAAE,KAAK,CAAC,QAAQ;aAC9B,CAAC,EACF,EAAE,SAAS,EAAE,GAAG,EAAE,CACnB,CAAC;YACF,OAAO,EAAE,GAAG,EAAE,OAAO,EAAE,EAAE,cAAc,EAAE,KAAK,CAAC,IAAI,EAAE,EAAE,CAAC;QAC1D,CAAC;QACD,KAAK,CAAC,UAAU,CAAC,GAAG;YAClB,MAAM,GAAG,GAAG,MAAM,YAAY,CAC5B,MAAM,EACN,IAAI,gBAAgB,CAAC,EAAE,MAAM,EAAE,IAAI,CAAC,MAAM,EAAE,GAAG,EAAE,GAAG,EAAE,CAAC,EACvD,EAAE,SAAS,EAAE,GAAG,EAAE,CACnB,CAAC;YACF,OAAO,EAAE,GAAG,EAAE,OAAO,EAAE,EAAE,EAAE,CAAC;QAC9B,CAAC;KACF,CAAC;AACJ,CAAC,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ysk-kit/storage",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"import": "./dist/index.js"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"dependencies": {
|
|
14
|
+
"@aws-sdk/client-s3": "^3.1142.0",
|
|
15
|
+
"@aws-sdk/s3-request-presigner": "^3.1142.0"
|
|
16
|
+
},
|
|
17
|
+
"devDependencies": {
|
|
18
|
+
"@types/node": "^24.5.2",
|
|
19
|
+
"typescript": "^6.0.3",
|
|
20
|
+
"vitest": "^5.0.2"
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"dist"
|
|
24
|
+
],
|
|
25
|
+
"publishConfig": {
|
|
26
|
+
"registry": "https://registry.npmjs.org",
|
|
27
|
+
"access": "public"
|
|
28
|
+
},
|
|
29
|
+
"scripts": {
|
|
30
|
+
"typecheck": "tsc --noEmit -p tsconfig.json",
|
|
31
|
+
"test": "vitest run --passWithNoTests",
|
|
32
|
+
"build": "tsc -p tsconfig.build.json"
|
|
33
|
+
}
|
|
34
|
+
}
|