Skip to content

Commit

Permalink
feat(hash): include hash in FileInfo #256
Browse files Browse the repository at this point in the history
  • Loading branch information
jonluca committed Mar 6, 2024
1 parent 8a42204 commit 48445fb
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 16 deletions.
11 changes: 8 additions & 3 deletions lib/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ export default parse;
*/
async function parse(path: string, $refs: $Refs, options: Options) {
// Remove the URL fragment, if any
path = url.stripHash(path);
const hashIndex = path.indexOf("#");
let hash = "";
if (hashIndex >= 0) {
hash = path.substring(hashIndex);
// Remove the URL fragment, if any
path = path.substring(0, hashIndex);
}

// Add a new $Ref for this file, even though we don't have the value yet.
// This ensures that we don't simultaneously read & parse the same file multiple times
Expand All @@ -28,6 +34,7 @@ async function parse(path: string, $refs: $Refs, options: Options) {
// This "file object" will be passed to all resolvers and parsers.
const file = {
url: path,
hash,
extension: url.getExtension(path),
} as FileInfo;

Expand Down Expand Up @@ -103,8 +110,6 @@ async function readFile(file: FileInfo, options: Options, $refs: $Refs): Promise
* The promise resolves with the parsed file contents and the parser that was used.
*/
async function parseFile(file: FileInfo, options: Options, $refs: $Refs) {
// console.log('Parsing %s', file.url);

// Find the parsers that can read this file type.
// If none of the parsers are an exact match for this file, then we'll try ALL of them.
// This handles situations where the file IS a supported type, just with an unknown extension.
Expand Down
16 changes: 8 additions & 8 deletions lib/pointer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,9 @@ class Pointer {
* @param [originalPath]
* @returns
*/
static parse(path: string, originalPath?: string) {
static parse(path: string, originalPath?: string): string[] {
// Get the JSON pointer from the path's hash
let pointer = url.getHash(path).substr(1);
const pointer = url.getHash(path).substring(1);

// If there's no pointer, then there are no tokens,
// so return an empty array
Expand All @@ -201,18 +201,18 @@ class Pointer {
}

// Split into an array
pointer = pointer.split("/");
const split = pointer.split("/");

// Decode each part, according to RFC 6901
for (let i = 0; i < pointer.length; i++) {
pointer[i] = safeDecodeURIComponent(pointer[i].replace(escapedSlash, "/").replace(escapedTilde, "~"));
for (let i = 0; i < split.length; i++) {
split[i] = safeDecodeURIComponent(split[i].replace(escapedSlash, "/").replace(escapedTilde, "~"));
}

if (pointer[0] !== "") {
throw new InvalidPointerError(pointer, originalPath === undefined ? path : originalPath);
if (split[0] !== "") {
throw new InvalidPointerError(split, originalPath === undefined ? path : originalPath);
}

return pointer.slice(1);
return split.slice(1);
}

/**
Expand Down
5 changes: 5 additions & 0 deletions lib/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ export interface FileInfo {
*/
url: string;

/**
* The hash (URL fragment) of the file URL, including the # symbol. If the URL doesn't have a hash, then this will be an empty string.
*/
hash: string;

/**
* The lowercase file extension, such as ".json", ".yaml", ".txt", etc.
*/
Expand Down
10 changes: 5 additions & 5 deletions lib/util/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ export function stripQuery(path: any) {
* @param path
* @returns
*/
export function getHash(path: any) {
export function getHash(path: string) {
const hashIndex = path.indexOf("#");
if (hashIndex >= 0) {
return path.substr(hashIndex);
return path.substring(hashIndex);
}
return "#";
}
Expand All @@ -119,10 +119,10 @@ export function getHash(path: any) {
* @param path
* @returns
*/
export function stripHash(path: any) {
export function stripHash(path: string) {
const hashIndex = path.indexOf("#");
if (hashIndex >= 0) {
path = path.substr(0, hashIndex);
path = path.substring(0, hashIndex);
}
return path;
}
Expand All @@ -133,7 +133,7 @@ export function stripHash(path: any) {
* @param path
* @returns
*/
export function isHttp(path: any) {
export function isHttp(path: string) {
const protocol = getProtocol(path);
if (protocol === "http" || protocol === "https") {
return true;
Expand Down

0 comments on commit 48445fb

Please sign in to comment.