From 322dbe7c6882466a55b2e179c8dc8686c81adc64 Mon Sep 17 00:00:00 2001 From: Junfeng Li Date: Sun, 10 Jun 2018 15:29:04 -0700 Subject: [PATCH 1/2] fix(serve): encode glob returned path. Close #422. --- src/fs.ts | 6 +++++- src/test/memfs.test.ts | 8 ++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/fs.ts b/src/fs.ts index 924900488..92f7a4a0d 100644 --- a/src/fs.ts +++ b/src/fs.ts @@ -78,7 +78,11 @@ export class LocalFileSystem implements FileSystem { follow: true, }) globber.on('match', (file: string) => { - subscriber.next(normalizeUri(base + file)) + const encodedPath = file + .split('/') + .map(encodeURIComponent) + .join('/') + subscriber.next(normalizeUri(base + encodedPath)) }) globber.on('error', (err: any) => { subscriber.error(err) diff --git a/src/test/memfs.test.ts b/src/test/memfs.test.ts index c5ed629bb..4e64881a0 100644 --- a/src/test/memfs.test.ts +++ b/src/test/memfs.test.ts @@ -18,6 +18,14 @@ describe('memfs.ts', () => { sinon.assert.calledOnce(listener) sinon.assert.calledWithExactly(listener, 'file:///foo/bar.txt', undefined) }) + it('should add just a URI and emit an event when URI has encoded char', () => { + const listener = sinon.spy() + const fs = new InMemoryFileSystem('/') + fs.on('add', listener) + fs.add('file:///foo/%25bar.txt') + sinon.assert.calledOnce(listener) + sinon.assert.calledWithExactly(listener, 'file:///foo/%25bar.txt', undefined) + }) it('should add content for a URI and emit an event', () => { const listener = sinon.spy() const fs = new InMemoryFileSystem('/') From 9ced6fa4e4e0c04a79c8919b03fd93ee3ff36a99 Mon Sep 17 00:00:00 2001 From: Felix Becker Date: Sun, 10 Jun 2018 23:00:58 -0700 Subject: [PATCH 2/2] fix: use map to better handle errors otherwise an exception during encoding would crash the process --- src/fs.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/fs.ts b/src/fs.ts index 92f7a4a0d..ad00f2e4c 100644 --- a/src/fs.ts +++ b/src/fs.ts @@ -78,11 +78,7 @@ export class LocalFileSystem implements FileSystem { follow: true, }) globber.on('match', (file: string) => { - const encodedPath = file - .split('/') - .map(encodeURIComponent) - .join('/') - subscriber.next(normalizeUri(base + encodedPath)) + subscriber.next(file) }) globber.on('error', (err: any) => { subscriber.error(err) @@ -93,6 +89,12 @@ export class LocalFileSystem implements FileSystem { return () => { globber.abort() } + }).map(file => { + const encodedPath = file + .split('/') + .map(encodeURIComponent) + .join('/') + return normalizeUri(base + encodedPath) }) }