From 281fca04cc753fc3e602205447d13fc1137069d5 Mon Sep 17 00:00:00 2001 From: Billie Hilton Date: Wed, 1 Feb 2023 15:34:35 -0500 Subject: [PATCH] feat: recognize Stoplight protocol (#44) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit BREAKING CHANGE: dropped support for Node <12 --------- Co-authored-by: Jakub Rożek --- src/__tests__/isAbsolute.spec.ts | 1 + src/__tests__/isStoplightURI.spec.ts | 31 ++++++++++++++++++++++++++++ src/__tests__/isURL.spec.ts | 27 ++++++++++++++++++++++++ src/grammar.pegjs | 8 ++++++- src/index.ts | 1 + src/isStoplightURI.ts | 6 ++++++ src/types.ts | 2 +- 7 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 src/__tests__/isStoplightURI.spec.ts create mode 100644 src/__tests__/isURL.spec.ts create mode 100644 src/isStoplightURI.ts diff --git a/src/__tests__/isAbsolute.spec.ts b/src/__tests__/isAbsolute.spec.ts index 56b5921..d0f0686 100644 --- a/src/__tests__/isAbsolute.spec.ts +++ b/src/__tests__/isAbsolute.spec.ts @@ -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 => { diff --git a/src/__tests__/isStoplightURI.spec.ts b/src/__tests__/isStoplightURI.spec.ts new file mode 100644 index 0000000..4d7a610 --- /dev/null +++ b/src/__tests__/isStoplightURI.spec.ts @@ -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); + }); +}); diff --git a/src/__tests__/isURL.spec.ts b/src/__tests__/isURL.spec.ts new file mode 100644 index 0000000..940efc1 --- /dev/null +++ b/src/__tests__/isURL.spec.ts @@ -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); + }); +}); diff --git a/src/grammar.pegjs b/src/grammar.pegjs index c093859..4ecacfc 100644 --- a/src/grammar.pegjs +++ b/src/grammar.pegjs @@ -32,6 +32,7 @@ RemotePath RemoteProtocol = HttpProtocol / HttpsProtocol + / StoplightProtocol HttpProtocol = raw:"http://"i { @@ -41,7 +42,12 @@ RemotePath HttpsProtocol = raw:"https://"i { return 'https' - } + } + + StoplightProtocol + = raw:"stoplight://"i { + return 'stoplight' + } Origin = $ NotSep+ diff --git a/src/index.ts b/src/index.ts index 42810d8..87b635a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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'; diff --git a/src/isStoplightURI.ts b/src/isStoplightURI.ts new file mode 100644 index 0000000..c25eb5d --- /dev/null +++ b/src/isStoplightURI.ts @@ -0,0 +1,6 @@ +import { parse } from './parse.js'; + +export function isStoplightURI(filepath: string) { + const parsed = parse(filepath); + return parsed.protocol === 'stoplight'; +} diff --git a/src/types.ts b/src/types.ts index a1c5247..68860a6 100644 --- a/src/types.ts +++ b/src/types.ts @@ -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;