Skip to content

Commit

Permalink
feat: Support reading dictionary files over http. (#3150)
Browse files Browse the repository at this point in the history
* Support reading files over http.
* Update fileReader.ts
* Support detecting dictionary changes.
  • Loading branch information
Jason3S committed Jun 28, 2022
1 parent f13da03 commit 7ba81e6
Show file tree
Hide file tree
Showing 23 changed files with 571 additions and 91 deletions.
190 changes: 186 additions & 4 deletions packages/cspell-io/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions packages/cspell-io/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,9 @@
"jest": "^28.1.1",
"lorem-ipsum": "^2.0.8",
"rimraf": "^3.0.2"
},
"dependencies": {
"@types/node-fetch": "^2.6.2",
"node-fetch": "^2.6.7"
}
}
19 changes: 19 additions & 0 deletions packages/cspell-io/src/file/FetchError.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { FetchUrlError } from './FetchError';
import { toURL } from './util';

const oc = expect.objectContaining;

describe('FetchError', () => {
test.each`
url | status | message | expected
${'http://google.com'} | ${404} | ${undefined} | ${oc({ code: 'ENOENT', message: 'URL not found.' })}
${'http://google.com'} | ${403} | ${undefined} | ${oc({ code: 'EACCES', message: 'Permission denied.' })}
${'http://google.com'} | ${403} | ${'Not good'} | ${oc({ code: 'EACCES', message: 'Not good' })}
${'http://google.com'} | ${500} | ${''} | ${oc({ code: 'ECONNREFUSED', message: 'Fatal Error' })}
`('create', ({ url, status, message, expected }) => {
url = toURL(url);
const e = FetchUrlError.create(url, status, message);
expect(e).toEqual(expected);
expect(e.url).toBe(url);
});
});
18 changes: 18 additions & 0 deletions packages/cspell-io/src/file/FetchError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export class FetchUrlError extends Error implements NodeJS.ErrnoException {
constructor(
message: string,
readonly code: string | undefined,
readonly status: number | undefined,
readonly url: URL
) {
super(message);
this.name = 'FetchUrlError';
}

static create(url: URL, status: number, message?: string): FetchUrlError {
if (status === 404) return new FetchUrlError(message || 'URL not found.', 'ENOENT', status, url);
if (status >= 400 && status < 500)
return new FetchUrlError(message || 'Permission denied.', 'EACCES', status, url);
return new FetchUrlError(message || 'Fatal Error', 'ECONNREFUSED', status, url);
}
}
Loading

0 comments on commit 7ba81e6

Please sign in to comment.