Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support for wildcards #46

Closed
wants to merge 9 commits into from
Closed
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions src/lib/encoding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ export function parseManifestStream(stream: Readable): EventEmitter {
let currentKey: string | undefined;
const keyPath: Array<string | number> = [];
let indexPath: string | undefined;
let wildcardpath: string | undefined;
angrymouse marked this conversation as resolved.
Show resolved Hide resolved
let paths: { [k: string]: string } = {};
let hasValidManifestKey = false; // { "manifest": "arweave/paths" }
let hasValidManifestVersion = false; // { "version": "0.1.0" }
Expand Down Expand Up @@ -295,6 +296,20 @@ export function parseManifestStream(stream: Readable): EventEmitter {
paths = {};
}
}

// Wildcard - { "*": { "path": "404.html" } }
if (
arielmelendez marked this conversation as resolved.
Show resolved Hide resolved
keyPath.length === 1 &&
keyPath[0] === '*' &&
currentKey === 'path'
) {
wildcardPath = data;
// Resolve if the path id is already known
if (wildcardPath !== undefined && paths[wildcardPath] !== undefined) {
emitter.emit('wildcard', { path: wildcardPath, id: paths[wildcardPath] });
paths = {};
}
}

// Paths - { "paths": { "some/path/file.html": { "id": "<data-id>" } }
if (
Expand All @@ -311,6 +326,9 @@ export function parseManifestStream(stream: Readable): EventEmitter {
} else if (p === indexPath) {
emitter.emit('index', { path: p, id: data });
paths = {};
} else if (p === wildcardPath){
emitter.emit('wildcard', { path: p, id: data });
paths = {};
}
}
});
Expand All @@ -324,29 +342,41 @@ export function resolveManifestStreamPath(
): Promise<string | undefined> {
return new Promise((resolve, reject) => {
const emitter = parseManifestStream(stream);

let resolved=false
// Remove trailing slashes from path - treat /path and /path/ the same
let wildcard;
const sanitizedPath = path !== undefined ? path.replace(/\/+$/g, '') : '';

emitter.on('error', (err) => {
resolved=true
reject(err);
});

emitter.on('end', () => {
resolve(undefined);
if(!resolved){
resolved=true
resolve(wildcard);
}
});

emitter.on('index', (data) => {
if (sanitizedPath === '') {
resolved=true
resolve(data.id);
}
});

emitter.on('path', (data) => {
const trimmedDataPath = data.path.replace(/\/+$/g, '');
if (sanitizedPath !== '' && trimmedDataPath === sanitizedPath) {
resolved=true
resolve(data.id);
}
});

emitter.on('wildcard',(data) => {
wildcard=data
})

});
}