Skip to content

Commit

Permalink
make memory file system element operations async
Browse files Browse the repository at this point in the history
  • Loading branch information
KurtThiemann committed Feb 6, 2025
1 parent 30ac068 commit 2d8bd00
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 32 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cm-spyglass",
"version": "1.0.0",
"version": "1.1.0",
"exports": {
".": "./index.js",
"./src/Dependency/VanillaMcDocDependency": "./src/Dependency/VanillaMcDocDependency.js"
Expand Down
20 changes: 10 additions & 10 deletions src/FileSystem/Memory/MemoryFileSystemDirectory.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,41 @@ export default class MemoryFileSystemDirectory extends MemoryFileSystemEntry {
/**
* @param {string} name
* @param {MemoryFileSystemEntry} entry
* @return {this}
* @return {Promise<this>}
*/
addEntry(name, entry) {
async addEntry(name, entry) {
this.entries.set(name, entry);
return this;
}

/**
* @param {string} name
* @return {?MemoryFileSystemEntry}
* @return {Promise<?MemoryFileSystemEntry>}
*/
getEntry(name) {
async getEntry(name) {
return this.entries.get(name) ?? null;
}

/**
* @return {IterableIterator<[string, MemoryFileSystemEntry]>}
* @return {Promise<Iterable<[string, MemoryFileSystemEntry]>>}
*/
getEntries() {
async getEntries() {
return this.entries.entries();
}

/**
* @param {string} name
* @return {boolean}
* @return {Promise<boolean>}
*/
hasEntry(name) {
async hasEntry(name) {
return this.entries.has(name);
}

/**
* @param {string} name
* @return {this}
* @return {Promise<this>}
*/
removeEntry(name) {
async removeEntry(name) {
this.entries.delete(name);
return this;
}
Expand Down
4 changes: 2 additions & 2 deletions src/FileSystem/Memory/MemoryFileSystemFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ export default class MemoryFileSystemFile extends MemoryFileSystemEntry {
}

/**
* @return {Uint8Array}
* @return {Promise<Uint8Array>}
*/
getContent() {
async getContent() {
return this.content;
}
}
38 changes: 19 additions & 19 deletions src/FileSystem/MemoryFileSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ export default class MemoryFileSystem {
/**
* @param location
* @param {boolean} parent If true, return the parent directory of the entry
* @return {MemoryFileSystemEntry}
* @return {Promise<MemoryFileSystemEntry>}
*/
findEntry(location, parent = false) {
async findEntry(location, parent = false) {
let parts = this.getPathParts(location);

if (parent) {
Expand All @@ -48,7 +48,7 @@ export default class MemoryFileSystem {
throw new Error(`ENOTDIR: ${location}`);
}

current = current.getEntry(part);
current = await current.getEntry(part);
if (current === null) {
throw new Error(`ENOENT: ${location}`);
}
Expand All @@ -75,10 +75,10 @@ export default class MemoryFileSystem {
continue;
}

let next = current.getEntry(part);
let next = await current.getEntry(part);
if (next === null) {
next = new MemoryFileSystemDirectory();
current.addEntry(part, next);
await current.addEntry(part, next);
} else if (!(next instanceof MemoryFileSystemDirectory)) {
throw new Error(`EEXIST: ${location}`);
}
Expand All @@ -87,30 +87,30 @@ export default class MemoryFileSystem {
}
}

let parent = this.findEntry(location, true);
let parent = await this.findEntry(location, true);
let basename = parts.pop();
if (!(parent instanceof MemoryFileSystemDirectory)) {
throw new Error(`ENOTDIR: ${location}`);
}
if (parent.hasEntry(basename)) {
if (await parent.hasEntry(basename)) {
throw new Error(`EEXIST: ${location}`);
}

parent.addEntry(basename, new MemoryFileSystemDirectory());
await parent.addEntry(basename, new MemoryFileSystemDirectory());
}

/**
* @inheritDoc
*/
async readdir(location) {
let directory = this.findEntry(location);
let directory = await this.findEntry(location);

if (!(directory instanceof MemoryFileSystemDirectory)) {
throw new Error(`ENOTDIR: ${location}`);
}

let result = [];
for (let [name, entry] of directory.getEntries()) {
for (let [name, entry] of await directory.getEntries()) {
let isDirectory = entry instanceof MemoryFileSystemDirectory;
result.push({
name: name,
Expand All @@ -127,12 +127,12 @@ export default class MemoryFileSystem {
* @inheritDoc
*/
async readFile(location) {
let entry = this.findEntry(location);
let entry = await this.findEntry(location);
if (!(entry instanceof MemoryFileSystemFile)) {
throw new Error(`EISDIR: ${location}`);
}

return entry.getContent();
return await entry.getContent();
}

/**
Expand All @@ -146,7 +146,7 @@ export default class MemoryFileSystem {
* @inheritDoc
*/
async stat(location) {
let entry = this.findEntry(location);
let entry = await this.findEntry(location);
let isDirectory = entry instanceof MemoryFileSystemDirectory;

return {
Expand All @@ -160,13 +160,13 @@ export default class MemoryFileSystem {
*/
async unlink(location) {
let parts = this.getPathParts(location);
let parent = this.findEntry(location, true);
let parent = await this.findEntry(location, true);
let basename = parts.pop();
if (!(parent instanceof MemoryFileSystemDirectory) || !parent.hasEntry(basename)) {
if (!(parent instanceof MemoryFileSystemDirectory) || !await parent.hasEntry(basename)) {
throw new Error(`ENOENT: ${location}`);
}

parent.removeEntry(basename);
await parent.removeEntry(basename);
}

/**
Expand All @@ -181,17 +181,17 @@ export default class MemoryFileSystem {
*/
async writeFile(location, data, _options) {
let parts = this.getPathParts(location);
let parent = this.findEntry(location, true);
let parent = await this.findEntry(location, true);
let basename = parts.pop();

if (!(parent instanceof MemoryFileSystemDirectory)) {
throw new Error(`ENOENT: ${location}`);
}

if (parent.hasEntry(basename) && !(parent.getEntry(basename) instanceof MemoryFileSystemFile)) {
if (await parent.hasEntry(basename) && !(await parent.getEntry(basename) instanceof MemoryFileSystemFile)) {
throw new Error(`EISDIR: ${location}`);
}

parent.addEntry(basename, new MemoryFileSystemFile(data));
await parent.addEntry(basename, new MemoryFileSystemFile(data));
}
}

0 comments on commit 2d8bd00

Please sign in to comment.