ag-common 0.0.738 → 0.0.740

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.
@@ -14,10 +14,11 @@ export type StorageConfig = {
14
14
  accountId: string;
15
15
  accessKeyId?: string;
16
16
  secretAccessKey?: string;
17
+ /** region to use. default auto */
18
+ region?: string;
17
19
  };
18
20
  export declare const createStorageClient: (config: StorageConfig) => S3Client;
19
- export declare const setS3: (config: StorageConfig) => S3Client;
20
- export declare const s3: S3Client;
21
+ export declare const setS3: (config: StorageConfig) => void;
21
22
  export declare const getS3Object: ({ fileurl: { Bucket, Key }, }: {
22
23
  fileurl: {
23
24
  Bucket: string;
@@ -22,7 +22,7 @@ var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _ar
22
22
  function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
23
23
  };
24
24
  Object.defineProperty(exports, "__esModule", { value: true });
25
- exports.copyFile = exports.deleteFiles = exports.deleteFile = exports.putS3Object = exports.getS3Object = exports.s3 = exports.setS3 = exports.createStorageClient = void 0;
25
+ exports.copyFile = exports.deleteFiles = exports.deleteFile = exports.putS3Object = exports.getS3Object = exports.setS3 = exports.createStorageClient = void 0;
26
26
  exports.getS3Objects = getS3Objects;
27
27
  exports.listFiles = listFiles;
28
28
  exports.getPresignedPostURL = getPresignedPostURL;
@@ -48,6 +48,7 @@ const getCacheKey = (config) => {
48
48
  });
49
49
  };
50
50
  const createStorageClient = (config) => {
51
+ var _a;
51
52
  const cacheKey = getCacheKey(config);
52
53
  const cachedClient = clientCache.get(cacheKey);
53
54
  if (cachedClient) {
@@ -59,14 +60,14 @@ const createStorageClient = (config) => {
59
60
  if (!accountId) {
60
61
  throw new Error('Account ID is required for R2. Set CLOUDFLARE_ACCOUNT_ID env var or provide accountId in config');
61
62
  }
62
- client = new client_s3_1.S3Client(Object.assign({ endpoint: `https://${accountId}.r2.cloudflarestorage.com`, forcePathStyle: true }, (config.accessKeyId && config.secretAccessKey
63
+ client = new client_s3_1.S3Client(Object.assign(Object.assign({ endpoint: `https://${accountId}.r2.cloudflarestorage.com`, forcePathStyle: true }, (config.accessKeyId && config.secretAccessKey
63
64
  ? {
64
65
  credentials: {
65
66
  accessKeyId: config.accessKeyId,
66
67
  secretAccessKey: config.secretAccessKey,
67
68
  },
68
69
  }
69
- : {})));
70
+ : {})), { region: (_a = config.region) !== null && _a !== void 0 ? _a : 'auto' }));
70
71
  }
71
72
  else {
72
73
  client = new client_s3_1.S3Client({
@@ -77,15 +78,18 @@ const createStorageClient = (config) => {
77
78
  return client;
78
79
  };
79
80
  exports.createStorageClient = createStorageClient;
80
- const setS3 = (config) => (0, exports.createStorageClient)(config);
81
+ let s3;
82
+ const setS3 = (config) => {
83
+ s3 = (0, exports.createStorageClient)(config);
84
+ };
81
85
  exports.setS3 = setS3;
82
- exports.s3 = (0, exports.setS3)({
86
+ (0, exports.setS3)({
83
87
  provider: 's3',
84
88
  region: 'ap-southeast-2',
85
89
  });
86
90
  const getS3Object = (_a) => __awaiter(void 0, [_a], void 0, function* ({ fileurl: { Bucket, Key }, }) {
87
91
  try {
88
- const r = yield exports.s3.send(new client_s3_1.GetObjectCommand({ Bucket, Key }));
92
+ const r = yield s3.send(new client_s3_1.GetObjectCommand({ Bucket, Key }));
89
93
  if (!r.Body) {
90
94
  throw new Error('no body returned');
91
95
  }
@@ -124,7 +128,7 @@ function getS3Objects(_a) {
124
128
  }
125
129
  const putS3Object = (_a) => __awaiter(void 0, [_a], void 0, function* ({ Body, Bucket, Key, ContentType, CacheControl = 'public, max-age=300', }) {
126
130
  try {
127
- yield exports.s3.send(new client_s3_1.PutObjectCommand({ Body, Bucket, Key, ContentType, CacheControl }));
131
+ yield s3.send(new client_s3_1.PutObjectCommand({ Body, Bucket, Key, ContentType, CacheControl }));
128
132
  return {};
129
133
  }
130
134
  catch (e) {
@@ -134,7 +138,7 @@ const putS3Object = (_a) => __awaiter(void 0, [_a], void 0, function* ({ Body, B
134
138
  exports.putS3Object = putS3Object;
135
139
  const deleteFile = (_a) => __awaiter(void 0, [_a], void 0, function* ({ Bucket, Key, }) {
136
140
  try {
137
- yield exports.s3.send(new client_s3_1.DeleteObjectCommand({ Bucket, Key }));
141
+ yield s3.send(new client_s3_1.DeleteObjectCommand({ Bucket, Key }));
138
142
  return {};
139
143
  }
140
144
  catch (e) {
@@ -150,7 +154,7 @@ const deleteFiles = (_a) => __awaiter(void 0, [_a], void 0, function* ({ Bucket,
150
154
  while (toDelete.length > 0) {
151
155
  const { part, rest } = (0, array_1.take)(toDelete, 900);
152
156
  toDelete = rest;
153
- const res = yield exports.s3.send(new client_s3_1.DeleteObjectsCommand({
157
+ const res = yield s3.send(new client_s3_1.DeleteObjectsCommand({
154
158
  Bucket,
155
159
  Delete: { Objects: part },
156
160
  }));
@@ -170,7 +174,7 @@ exports.deleteFiles = deleteFiles;
170
174
  const copyFile = (_a) => __awaiter(void 0, [_a], void 0, function* ({ Bucket, fromKey, toKey, deleteSource = false, }) {
171
175
  try {
172
176
  (0, log_1.debug)(`copying s3 file from ${Bucket}- ${fromKey} to ${toKey}`);
173
- yield exports.s3.send(new client_s3_1.CopyObjectCommand({
177
+ yield s3.send(new client_s3_1.CopyObjectCommand({
174
178
  //incl bucket
175
179
  CopySource: Bucket + '/' + fromKey,
176
180
  //dest
@@ -197,7 +201,7 @@ function listFiles(bucketName, opt) {
197
201
  const ret = [];
198
202
  let response;
199
203
  do {
200
- response = yield exports.s3.send(new client_s3_1.ListObjectsV2Command({
204
+ response = yield s3.send(new client_s3_1.ListObjectsV2Command({
201
205
  Bucket: bucketName,
202
206
  ContinuationToken: response === null || response === void 0 ? void 0 : response.NextContinuationToken,
203
207
  Prefix: opt === null || opt === void 0 ? void 0 : opt.prefix,
@@ -230,7 +234,7 @@ function listFiles(bucketName, opt) {
230
234
  function getPresignedPostURL(_a) {
231
235
  return __awaiter(this, arguments, void 0, function* ({ bucket, key, maxMb = 5, }) {
232
236
  try {
233
- const ps = yield (0, s3_presigned_post_1.createPresignedPost)(exports.s3, {
237
+ const ps = yield (0, s3_presigned_post_1.createPresignedPost)(s3, {
234
238
  Bucket: bucket,
235
239
  Key: key,
236
240
  Expires: 600,
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.0.738",
2
+ "version": "0.0.740",
3
3
  "name": "ag-common",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",