Skip to content

Commit

Permalink
feat: recognize Stoplight protocol (#44)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: dropped support for Node <12

---------

Co-authored-by: Jakub Rożek <[email protected]>
  • Loading branch information
billiegoose and P0lip authored Feb 1, 2023
1 parent 4f1fa64 commit 281fca0
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/__tests__/isAbsolute.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ describe('isAbsolute', () => {
'/var/bin.d',
'http://example.com/is/absolute',
'https://stoplight.io',
'stoplight://resources/overrides/abc',
'file:///this/is/also/absolute',
'file://c:/and/this/is/../absolute',
])('treats %s path as absolute', filepath => {
Expand Down
31 changes: 31 additions & 0 deletions src/__tests__/isStoplightURI.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { isStoplightURI } from '../isStoplightURI.js';

describe('isStoplightURI', () => {
it.each(['stoplight://resources/overrides/abc', 'stoplight://some-resource'])(
'treats %s path as a Stoplight URI',
filepath => {
expect(isStoplightURI(filepath)).toBe(true);
},
);

it.each([
'http://example.com/is/absolute',
'https://stoplight.io',
'\\foo\\bar.json',
'c:\\foo\\bar.json',
'c:\\',
'c:/',
'c:/foo/bar.json',
'/home/test',
'/',
'//',
'/var/lib/test/',
'/var/bin.d',
'foo/bar',
'test',
'',
'file:///this/is/also/absolute',
])('does treat %s path as a Stoplight URI', filepath => {
expect(isStoplightURI(filepath)).toBe(false);
});
});
27 changes: 27 additions & 0 deletions src/__tests__/isURL.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { isURL } from '../isURL.js';

describe('isURL', () => {
it.each(['http://example.com/is/absolute', 'https://stoplight.io'])('treats %s path as an URL', filepath => {
expect(isURL(filepath)).toBe(true);
});

it.each([
'stoplight://resources/overrides/abc',
'\\foo\\bar.json',
'c:\\foo\\bar.json',
'c:\\',
'c:/',
'c:/foo/bar.json',
'/home/test',
'/',
'//',
'/var/lib/test/',
'/var/bin.d',
'foo/bar',
'test',
'',
'file:///this/is/also/absolute',
])('does treat %s path as an URL', filepath => {
expect(isURL(filepath)).toBe(false);
});
});
8 changes: 7 additions & 1 deletion src/grammar.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ RemotePath
RemoteProtocol
= HttpProtocol
/ HttpsProtocol
/ StoplightProtocol

HttpProtocol
= raw:"http://"i {
Expand All @@ -41,7 +42,12 @@ RemotePath
HttpsProtocol
= raw:"https://"i {
return 'https'
}
}

StoplightProtocol
= raw:"stoplight://"i {
return 'stoplight'
}

Origin
= $ NotSep+
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export * from './dirname.js';
export * from './extname.js';
export * from './format.js';
export * from './isAbsolute.js';
export * from './isStoplightURI.js';
export * from './isURL.js';
export * from './join.js';
export { normalize } from './normalize.js';
Expand Down
6 changes: 6 additions & 0 deletions src/isStoplightURI.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { parse } from './parse.js';

export function isStoplightURI(filepath: string) {
const parsed = parse(filepath);
return parsed.protocol === 'stoplight';
}
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export interface IPath {
protocol: 'file' | 'http' | 'https' | null;
protocol: 'file' | 'http' | 'https' | 'stoplight' | null;
origin: string | null;
absolute: boolean;
drive: string | null;
Expand Down

0 comments on commit 281fca0

Please sign in to comment.