-
-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: CSpell-IO to use Service Bus (#3303)
* refactor: CSpell-IO to use Service Bus The goal is to have CSpell-IO support both node and web. * Update index.test.ts.snap * Add some test helpers * Have tests use the test helper * Use URLs in the handlers instead of string. * wire up readFile * Support reading http files.
- Loading branch information
Showing
47 changed files
with
700 additions
and
219 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import type { Stats } from './models'; | ||
|
||
export interface CSpellIO { | ||
readFile(uriOrFilename: string): Promise<string>; | ||
readFileSync(uriOrFilename: string): string; | ||
writeFile(uriOrFilename: string, content: string): Promise<void>; | ||
getStat(uriOrFilename: string): Promise<Stats>; | ||
getStatSync(uriOrFilename: string): Stats; | ||
compareStats(left: Stats, right: Stats): number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { CSpellIONode } from './CSpellIONode'; | ||
import { pathToSample as ps } from './test/helper'; | ||
|
||
const sc = expect.stringContaining; | ||
const oc = expect.objectContaining; | ||
|
||
describe('CSpellIONode', () => { | ||
test('constructor', () => { | ||
const cspellIo = new CSpellIONode(); | ||
expect(cspellIo).toBeDefined(); | ||
}); | ||
|
||
test.each` | ||
filename | expected | ||
${__filename} | ${sc('This bit of text')} | ||
${ps('cities.txt')} | ${sc('San Francisco\n')} | ||
${ps('cities.txt.gz')} | ${sc('San Francisco\n')} | ||
${'https://raw.githubusercontent.com/streetsidesoftware/cspell/main/packages/cspell-io/samples/cities.txt'} | ${sc('San Francisco\n')} | ||
${'https://raw.githubusercontent.com/streetsidesoftware/cspell/main/packages/cspell-io/samples/cities.txt.gz'} | ${sc('San Francisco\n')} | ||
`('readFile $filename', async ({ filename, expected }) => { | ||
const cspellIo = new CSpellIONode(); | ||
await expect(cspellIo.readFile(filename)).resolves.toEqual(expected); | ||
}); | ||
|
||
test.each` | ||
filename | expected | ||
${ps('cities.not_found.txt')} | ${oc({ code: 'ENOENT' })} | ||
${ps('cities.not_found.txt.gz')} | ${oc({ code: 'ENOENT' })} | ||
${'https://raw.githubusercontent.com/streetsidesoftware/cspell/main/packages/cspell-io/samples/cities.not_found.txt'} | ${oc({ code: 'ENOENT' })} | ||
${'https://raw.githubusercontent.com/streetsidesoftware/cspell/main/packages/cspell-io/not_found/cities.txt.gz'} | ${oc({ code: 'ENOENT' })} | ||
`('readFile not found $filename', async ({ filename, expected }) => { | ||
const cspellIo = new CSpellIONode(); | ||
await expect(cspellIo.readFile(filename)).rejects.toEqual(expected); | ||
}); | ||
|
||
test.each` | ||
filename | expected | ||
${__filename} | ${sc('This bit of text')} | ||
${ps('cities.txt')} | ${sc('San Francisco\n')} | ||
${ps('cities.txt.gz')} | ${sc('San Francisco\n')} | ||
`('readFileSync $filename', ({ filename, expected }) => { | ||
const cspellIo = new CSpellIONode(); | ||
expect(cspellIo.readFileSync(filename)).toEqual(expected); | ||
}); | ||
|
||
const stats = { | ||
urlA: { eTag: 'W/"10c5e3c7c73159515d4334813d6ba0255230270d92ebfdbd37151db7a0db5918"', mtimeMs: 0, size: -1 }, | ||
urlB: { eTag: 'W/"10c5e3c7c73159515d4334813d6ba0255230270d92ebfdbd37151db7a0dbffff"', mtimeMs: 0, size: -1 }, | ||
file1: { mtimeMs: 1658757408444.0342, size: 1886 }, | ||
file2: { mtimeMs: 1658757408444.0342, size: 2886 }, | ||
file3: { mtimeMs: 1758757408444.0342, size: 1886 }, | ||
}; | ||
|
||
test.each` | ||
left | right | expected | ||
${stats.urlA} | ${stats.urlA} | ${0} | ||
${stats.urlA} | ${stats.file1} | ${1} | ||
${stats.file1} | ${stats.file3} | ${-1} | ||
${stats.file2} | ${stats.file3} | ${1} | ||
`('getStat $left <> $right', async ({ left, right, expected }) => { | ||
const cspellIo = new CSpellIONode(); | ||
const r = cspellIo.compareStats(left, right); | ||
expect(r).toEqual(expected); | ||
}); | ||
|
||
// writeFile(_uriOrFilename: string, _content: string): Promise<void> { | ||
// throw new ErrorNotImplemented('writeFile'); | ||
// } | ||
// getStat(_uriOrFilename: string): Promise<Stats> { | ||
// throw new ErrorNotImplemented('getStat'); | ||
// } | ||
// getStatSync(_uriOrFilename: string): Stats { | ||
// throw new ErrorNotImplemented('getStatSync'); | ||
// } | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { isServiceResponseSuccess, ServiceBus } from '@cspell/cspell-service-bus'; | ||
import { compareStats } from './common/stat'; | ||
import { CSpellIO } from './CSpellIO'; | ||
import { ErrorNotImplemented } from './errors/ErrorNotImplemented'; | ||
import { registerHandlers } from './handlers/node/file'; | ||
import { Stats } from './models/Stats'; | ||
import { toURL } from './node/file/util'; | ||
import { | ||
RequestFsReadFile, | ||
RequestFsReadFileSync, | ||
RequestFsStat, | ||
RequestFsStatSync, | ||
RequestFsWriteFile, | ||
} from './requests'; | ||
|
||
export class CSpellIONode implements CSpellIO { | ||
constructor(readonly serviceBus = new ServiceBus()) { | ||
registerHandlers(serviceBus); | ||
} | ||
|
||
readFile(uriOrFilename: string): Promise<string> { | ||
const url = toURL(uriOrFilename); | ||
const res = this.serviceBus.dispatch(RequestFsReadFile.create({ url })); | ||
if (!isServiceResponseSuccess(res)) { | ||
throw genError(res.error, 'readFile'); | ||
} | ||
return res.value; | ||
} | ||
readFileSync(uriOrFilename: string): string { | ||
const url = toURL(uriOrFilename); | ||
const res = this.serviceBus.dispatch(RequestFsReadFileSync.create({ url })); | ||
if (!isServiceResponseSuccess(res)) { | ||
throw genError(res.error, 'readFileSync'); | ||
} | ||
return res.value; | ||
} | ||
writeFile(uriOrFilename: string, _content: string): Promise<void> { | ||
const url = toURL(uriOrFilename); | ||
const res = this.serviceBus.dispatch(RequestFsWriteFile.create({ url })); | ||
if (!isServiceResponseSuccess(res)) { | ||
throw genError(res.error, 'writeFile'); | ||
} | ||
return res.value; | ||
} | ||
getStat(uriOrFilename: string): Promise<Stats> { | ||
const url = toURL(uriOrFilename); | ||
const res = this.serviceBus.dispatch(RequestFsStat.create({ url })); | ||
if (!isServiceResponseSuccess(res)) { | ||
throw genError(res.error, 'getStat'); | ||
} | ||
return res.value; | ||
} | ||
getStatSync(uriOrFilename: string): Stats { | ||
const url = toURL(uriOrFilename); | ||
const res = this.serviceBus.dispatch(RequestFsStatSync.create({ url })); | ||
if (!isServiceResponseSuccess(res)) { | ||
throw genError(res.error, 'getStatSync'); | ||
} | ||
return res.value; | ||
} | ||
compareStats(left: Stats, right: Stats): number { | ||
return compareStats(left, right); | ||
} | ||
} | ||
|
||
function genError(err: Error | undefined, alt: string): Error { | ||
return err || new ErrorNotImplemented(alt); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { CSpellIO } from './CSpellIO'; | ||
import { ErrorNotImplemented } from './errors/ErrorNotImplemented'; | ||
import { compareStats } from './common/stat'; | ||
import { Stats } from './models/Stats'; | ||
|
||
export class CSpellIOWeb implements CSpellIO { | ||
readFile(_uriOrFilename: string): Promise<string> { | ||
throw new ErrorNotImplemented('readFile'); | ||
} | ||
readFileSync(_uriOrFilename: string): string { | ||
throw new ErrorNotImplemented('readFileSync'); | ||
} | ||
writeFile(_uriOrFilename: string, _content: string): Promise<void> { | ||
throw new ErrorNotImplemented('writeFile'); | ||
} | ||
getStat(_uriOrFilename: string): Promise<Stats> { | ||
throw new ErrorNotImplemented('getStat'); | ||
} | ||
getStatSync(_uriOrFilename: string): Stats { | ||
throw new ErrorNotImplemented('getStatSync'); | ||
} | ||
compareStats(left: Stats, right: Stats): number { | ||
return compareStats(left, right); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { compareStats } from './stat'; | ||
|
||
describe('stat', () => { | ||
const stats = { | ||
urlA: { eTag: 'W/"10c5e3c7c73159515d4334813d6ba0255230270d92ebfdbd37151db7a0db5918"', mtimeMs: 0, size: -1 }, | ||
urlB: { eTag: 'W/"10c5e3c7c73159515d4334813d6ba0255230270d92ebfdbd37151db7a0dbffff"', mtimeMs: 0, size: -1 }, | ||
file1: { mtimeMs: 1658757408444.0342, size: 1886 }, | ||
file2: { mtimeMs: 1658757408444.0342, size: 2886 }, | ||
file3: { mtimeMs: 1758757408444.0342, size: 1886 }, | ||
}; | ||
|
||
test.each` | ||
left | right | expected | ||
${stats.urlA} | ${stats.urlA} | ${0} | ||
${stats.urlA} | ${stats.urlB} | ${-1} | ||
${stats.urlA} | ${stats.file1} | ${1} | ||
${stats.file1} | ${stats.file1} | ${0} | ||
${stats.file1} | ${stats.file2} | ${-1} | ||
${stats.file1} | ${stats.file3} | ${-1} | ||
${stats.file2} | ${stats.file1} | ${1} | ||
${stats.file3} | ${stats.file1} | ${1} | ||
${stats.file2} | ${stats.file3} | ${1} | ||
`('getStat $left <> $right', async ({ left, right, expected }) => { | ||
const r = compareStats(left, right); | ||
expect(r).toEqual(expected); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Stats } from '../models/Stats'; | ||
|
||
/** | ||
* Compare two Stats to see if they have the same value. | ||
* @param left - Stats | ||
* @param right - Stats | ||
* @returns 0 - equal; 1 - left > right; -1 left < right | ||
*/ | ||
export function compareStats(left: Stats, right: Stats): number { | ||
if (left === right) return 0; | ||
if (left.eTag || right.eTag) return left.eTag === right.eTag ? 0 : (left.eTag || '') < (right.eTag || '') ? -1 : 1; | ||
const diff = left.size - right.size || left.mtimeMs - right.mtimeMs; | ||
return diff < 0 ? -1 : diff > 0 ? 1 : 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export class ErrorNotImplemented extends Error { | ||
constructor(readonly method: string) { | ||
super(`Method ${method} is not supported.`); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
export { readFile, readFileSync } from './fileReader'; | ||
export { writeToFile, writeToFileIterable, writeToFileIterableP } from './fileWriter'; | ||
export { getStat, getStatSync } from './stat'; | ||
export type { Stats } from './stat'; | ||
export { readFile, readFileSync } from './../node/file'; | ||
export { writeToFile, writeToFileIterable, writeToFileIterableP } from './../node/file'; | ||
export { getStat, getStatSync } from './../node/file'; |
Oops, something went wrong.