@awsesh/core 1.0.2 → 1.0.4

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.
@@ -0,0 +1,6 @@
1
+ export declare function readJsonFile<T>(filePath: string): Promise<T>;
2
+ export declare function writeJsonFile(filePath: string, value: unknown): Promise<void>;
3
+ export declare function fileExists(filePath: string): Promise<boolean>;
4
+ export declare function listJsonFiles(dir: string, options?: {
5
+ recursive?: boolean;
6
+ }): Promise<string[]>;
package/index.js CHANGED
@@ -20058,52 +20058,89 @@ var Credentials;
20058
20058
  Credentials.listProfiles = listProfiles;
20059
20059
  })(Credentials ||= {});
20060
20060
  // src/sessions.ts
20061
+ import path3 from "node:path";
20062
+ import fs3 from "node:fs/promises";
20063
+
20064
+ // src/filesystem.ts
20061
20065
  import path2 from "node:path";
20062
20066
  import fs2 from "node:fs/promises";
20067
+ async function readJsonFile(filePath) {
20068
+ const content = await fs2.readFile(filePath, "utf-8");
20069
+ return JSON.parse(content);
20070
+ }
20071
+ async function writeJsonFile(filePath, value) {
20072
+ await fs2.writeFile(filePath, JSON.stringify(value, null, 2));
20073
+ }
20074
+ async function fileExists(filePath) {
20075
+ try {
20076
+ await fs2.access(filePath);
20077
+ return true;
20078
+ } catch {
20079
+ return false;
20080
+ }
20081
+ }
20082
+ async function listJsonFiles(dir, options = {}) {
20083
+ const recursive = options.recursive ?? false;
20084
+ const entries = await fs2.readdir(dir, { withFileTypes: true });
20085
+ const files = [];
20086
+ for (const entry of entries) {
20087
+ const entryPath = path2.join(dir, entry.name);
20088
+ if (entry.isFile() && entry.name.endsWith(".json")) {
20089
+ files.push(entry.name);
20090
+ continue;
20091
+ }
20092
+ if (recursive && entry.isDirectory()) {
20093
+ const nestedFiles = await listJsonFiles(entryPath, { recursive: true });
20094
+ for (const nestedFile of nestedFiles) {
20095
+ files.push(path2.posix.join(entry.name, nestedFile));
20096
+ }
20097
+ }
20098
+ }
20099
+ return files;
20100
+ }
20101
+
20102
+ // src/sessions.ts
20063
20103
  var Sessions;
20064
20104
  ((Sessions) => {
20065
20105
  function create(options) {
20066
20106
  const { dir } = options;
20067
20107
  async function ensureDir() {
20068
- await fs2.mkdir(dir, { recursive: true }).catch(() => {});
20108
+ await fs3.mkdir(dir, { recursive: true }).catch(() => {});
20069
20109
  }
20070
20110
  return {
20071
20111
  async list() {
20072
20112
  try {
20073
20113
  await ensureDir();
20074
- const glob = new Bun.Glob("*.json");
20075
- const files = await Array.fromAsync(glob.scan({ cwd: dir, onlyFiles: true }));
20076
- const sessions = await Promise.all(files.map((file) => Bun.file(path2.join(dir, file)).json()));
20114
+ const files = await listJsonFiles(dir);
20115
+ const sessions = await Promise.all(files.map((file) => readJsonFile(path3.join(dir, file))));
20077
20116
  return sessions;
20078
20117
  } catch {
20079
20118
  return [];
20080
20119
  }
20081
20120
  },
20082
20121
  async load(name) {
20083
- const target = path2.join(dir, `${name}.json`);
20084
- const file = Bun.file(target);
20085
- if (!await file.exists())
20122
+ const target = path3.join(dir, `${name}.json`);
20123
+ if (!await fileExists(target))
20086
20124
  return;
20087
- return file.json();
20125
+ return readJsonFile(target);
20088
20126
  },
20089
20127
  async save(session) {
20090
20128
  await ensureDir();
20091
- const target = path2.join(dir, `${session.name}.json`);
20092
- await Bun.write(target, JSON.stringify(session, null, 2));
20129
+ const target = path3.join(dir, `${session.name}.json`);
20130
+ await writeJsonFile(target, session);
20093
20131
  },
20094
20132
  async remove(name) {
20095
- const target = path2.join(dir, `${name}.json`);
20096
- await fs2.unlink(target).catch(() => {});
20133
+ const target = path3.join(dir, `${name}.json`);
20134
+ await fs3.unlink(target).catch(() => {});
20097
20135
  },
20098
20136
  async exists(name) {
20099
- const target = path2.join(dir, `${name}.json`);
20100
- return Bun.file(target).exists();
20137
+ const target = path3.join(dir, `${name}.json`);
20138
+ return fileExists(target);
20101
20139
  },
20102
20140
  async count() {
20103
20141
  try {
20104
20142
  await ensureDir();
20105
- const glob = new Bun.Glob("*.json");
20106
- const files = await Array.fromAsync(glob.scan({ cwd: dir, onlyFiles: true }));
20143
+ const files = await listJsonFiles(dir);
20107
20144
  return files.length;
20108
20145
  } catch {
20109
20146
  return 0;
@@ -20114,50 +20151,46 @@ var Sessions;
20114
20151
  Sessions.create = create;
20115
20152
  })(Sessions ||= {});
20116
20153
  // src/storage.ts
20117
- import path3 from "node:path";
20118
- import fs3 from "node:fs/promises";
20154
+ import path4 from "node:path";
20155
+ import fs4 from "node:fs/promises";
20119
20156
  var Storage;
20120
20157
  ((Storage) => {
20121
20158
  function create(options) {
20122
20159
  const { dir } = options;
20123
20160
  async function ensureDir(dirPath) {
20124
- await fs3.mkdir(dirPath, { recursive: true }).catch(() => {});
20161
+ await fs4.mkdir(dirPath, { recursive: true }).catch(() => {});
20125
20162
  }
20126
20163
  return {
20127
20164
  async read(key) {
20128
- const target = path3.join(dir, `${key}.json`);
20165
+ const target = path4.join(dir, `${key}.json`);
20129
20166
  try {
20130
- const file = Bun.file(target);
20131
- if (!await file.exists())
20132
- return;
20133
- return file.json();
20167
+ return await readJsonFile(target);
20134
20168
  } catch {
20135
20169
  return;
20136
20170
  }
20137
20171
  },
20138
20172
  async write(key, value) {
20139
- const target = path3.join(dir, `${key}.json`);
20140
- await ensureDir(path3.dirname(target));
20141
- await Bun.write(target, JSON.stringify(value, null, 2));
20173
+ const target = path4.join(dir, `${key}.json`);
20174
+ await ensureDir(path4.dirname(target));
20175
+ await writeJsonFile(target, value);
20142
20176
  },
20143
20177
  async update(key, fn) {
20144
- const target = path3.join(dir, `${key}.json`);
20178
+ const target = path4.join(dir, `${key}.json`);
20145
20179
  let content;
20146
20180
  try {
20147
- const file = Bun.file(target);
20148
- content = await file.json();
20181
+ content = await readJsonFile(target);
20149
20182
  } catch {
20150
20183
  content = {};
20151
20184
  }
20152
20185
  const updated = fn(content);
20153
- await ensureDir(path3.dirname(target));
20154
- await Bun.write(target, JSON.stringify(updated, null, 2));
20186
+ await ensureDir(path4.dirname(target));
20187
+ await writeJsonFile(target, updated);
20155
20188
  return updated;
20156
20189
  },
20157
20190
  async remove(key) {
20158
- const target = path3.join(dir, `${key}.json`);
20191
+ const target = path4.join(dir, `${key}.json`);
20159
20192
  try {
20160
- await fs3.unlink(target);
20193
+ await fs4.unlink(target);
20161
20194
  } catch (error) {
20162
20195
  const err = error;
20163
20196
  if (err.code !== "ENOENT")
@@ -20165,10 +20198,9 @@ var Storage;
20165
20198
  }
20166
20199
  },
20167
20200
  async list(prefix) {
20168
- const targetDir = path3.join(dir, prefix);
20201
+ const targetDir = path4.join(dir, prefix);
20169
20202
  try {
20170
- const glob = new Bun.Glob("**/*.json");
20171
- const results = await Array.fromAsync(glob.scan({ cwd: targetDir, onlyFiles: true }));
20203
+ const results = await listJsonFiles(targetDir, { recursive: true });
20172
20204
  return results.map((x) => x.slice(0, -5)).sort();
20173
20205
  } catch (error) {
20174
20206
  const err = error;
@@ -20178,9 +20210,9 @@ var Storage;
20178
20210
  }
20179
20211
  },
20180
20212
  async exists(key) {
20181
- const target = path3.join(dir, `${key}.json`);
20213
+ const target = path4.join(dir, `${key}.json`);
20182
20214
  try {
20183
- await fs3.access(target);
20215
+ await fs4.access(target);
20184
20216
  return true;
20185
20217
  } catch {
20186
20218
  return false;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@awsesh/core",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "description": "AWS SSO session management SDK",
5
5
  "type": "module",
6
6
  "main": "./index.js",