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
Show file tree
Hide file tree
Changes from all 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
8 changes: 8 additions & 0 deletions src/lib/encoding.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,14 @@ describe('Manifest parsing', () => {
expect(id).to.equal('cG7Hdi_iTQPoEYgQJFqJ8NMpN4KoZ-vH_j7pG4iP7NI');
});

it('should return wildcard file for files not present in manifest', async ()=>{
const id=await resolveManifestStreamPath(
exampleManifestStream(),
'path/that/does/not/exist.txt'
);
expect(id).to.equal('vNIcG7HdiMpFqJ8NZN4KopG4_iTQPoH_j7iP7EYgQJ-')
})

it('should return the ID for non-index paths', async () => {
// TODO use an array here
const id1 = await resolveManifestStreamPath(
Expand Down
48 changes: 44 additions & 4 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;
let paths: { [k: string]: string } = {};
let hasValidManifestKey = false; // { "manifest": "arweave/paths" }
let hasValidManifestVersion = false; // { "version": "0.1.0" }
Expand Down Expand Up @@ -277,8 +278,8 @@ export function parseManifestStream(stream: Readable): EventEmitter {
hasValidManifestKey = true;
}

// Manifest version - { "version": "0.1.0" }
if (keyPath.length === 0 && currentKey === 'version' && data === '0.1.0') {
// Manifest version - { "version": "0.2.0" } or { "version": "0.1.0" }
if (keyPath.length === 0 && currentKey === 'version' && ["0.1.0", "0.2.0"].includes(data)) {
hasValidManifestVersion = true;
}

Expand All @@ -295,6 +296,30 @@ 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 = {};
}
}

// Wildcard - { "*": { "id": "wildcard-id" } }
if (
keyPath.length === 1 &&
keyPath[0] === '*' &&
currentKey === 'id'
) {
emitter.emit('wildcard', { path: null, id: data });
paths = {};
}

// Paths - { "paths": { "some/path/file.html": { "id": "<data-id>" } }
if (
Expand All @@ -311,6 +336,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 +352,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
})

});
}
7 changes: 5 additions & 2 deletions test/mock_files/manifests/example_manifest.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
{
"manifest": "arweave/paths",
"version": "0.1.0",
"version": "0.2.0",
"*":{
"id":"vNIcG7HdiMpFqJ8NZN4KopG4_iTQPoH_j7iP7EYgQJ-"
},
"index": {
"path": "index.html"
"path": "index.html",
},
"paths": {
"index.html": {
Expand Down
Loading