Skip to content

Commit

Permalink
chore: drop process (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
P0lip authored Mar 18, 2024
1 parent ab6ad5a commit 6f6d49e
Show file tree
Hide file tree
Showing 27 changed files with 114 additions and 60 deletions.
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
18
5 changes: 5 additions & 0 deletions lib/environment/browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* eslint-env browser */
"use strict";

exports.environment = "browser";
exports.getCwd = () => location.origin + location.pathname;
23 changes: 23 additions & 0 deletions lib/environment/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"use strict";

exports.env = {
platform: "unknown",
environment: "unknown",
getEnvironment () {
return this.environment;
},
setEnvironment (newEnvironment) {
this.environment = newEnvironment;
},
getCwd () {
return "/";
},
isBrowser () {
return this.environment === "browser";
},
TextDecoder: globalThis.TextDecoder || class {
constructor () {
throw new Error("TextDecoder not available");
}
}
};
10 changes: 10 additions & 0 deletions lib/environment/node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"use strict";

const { cwd: processCwd, platform } = require("process");

exports.environment = "node";
exports.platform = platform;

exports.getCwd = () => processCwd();

exports.TextDecoder = globalThis.TextDecoder || require("util").TextDecoder;
8 changes: 8 additions & 0 deletions lib/index.browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"use strict";

const browser = require("./environment/browser.js");
const { env } = require("./environment/index.js");

Object.assign(env, browser);

module.exports = require("./index.js");
8 changes: 8 additions & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -501,4 +501,12 @@ declare namespace $RefParser {
public readonly name = "InvalidPointerError";
public readonly code ="EINVALIDPOINTER";
}

export const env: {
readonly platform: string;
readonly environment: string;
getCwd(): string;
setEnvironment(newEnv: string): void;
getEnvironment(): string;
};
}
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const maybe = require("call-me-maybe");
const { ono } = require("@jsdevtools/ono");

module.exports = $RefParser;
module.exports.env = require("./environment/index.js").env;
module.exports.default = $RefParser;
module.exports.JSONParserError = JSONParserError;
module.exports.JSONParserErrorGroup = JSONParserErrorGroup;
Expand Down
8 changes: 8 additions & 0 deletions lib/index.node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"use strict";

const node = require("./environment/node.js");
const { env } = require("./environment/index.js");

Object.assign(env, node);

module.exports = require("./index.js");
7 changes: 3 additions & 4 deletions lib/parsers/json.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
"use strict";

const { ParserError } = require("../util/errors");
const TextDecoder = require("../util/text-decoder");

const decoder = new TextDecoder();
const { ParserError } = require("../util/errors.js");
const { env } = require("../environment/index.js");

module.exports = {
/**
Expand Down Expand Up @@ -42,6 +40,7 @@ module.exports = {
async parse (file) { // eslint-disable-line require-await
let data = file.data;
if (ArrayBuffer.isView(data)) {
let decoder = new env.TextDecoder();
data = decoder.decode(data);
}

Expand Down
6 changes: 3 additions & 3 deletions lib/parsers/text.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";

const { ParserError } = require("../util/errors");
const TextDecoder = require("../util/text-decoder");
const { ParserError } = require("../util/errors.js");
const { env } = require("../environment/index.js");

let TEXT_REGEXP = /\.(txt|htm|html|md|xml|js|min|map|css|scss|less|svg)$/i;

Expand Down Expand Up @@ -58,7 +58,7 @@ module.exports = {
return file.data;
}
else if (ArrayBuffer.isView(file.data)) {
let decoder = new TextDecoder(this.encoding);
let decoder = new env.TextDecoder(this.encoding);
return decoder.decode(file.data);
}
else {
Expand Down
7 changes: 3 additions & 4 deletions lib/parsers/yaml.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
"use strict";

const { ParserError } = require("../util/errors");
const TextDecoder = require("../util/text-decoder");
const yaml = require("@stoplight/yaml");

const decoder = new TextDecoder();
const { ParserError } = require("../util/errors.js");
const { env } = require("../environment/index.js");

module.exports = {
/**
Expand Down Expand Up @@ -43,6 +41,7 @@ module.exports = {
async parse (file) { // eslint-disable-line require-await
let data = file.data;
if (ArrayBuffer.isView(data)) {
let decoder = new env.TextDecoder();
data = decoder.decode(data);
}

Expand Down
5 changes: 3 additions & 2 deletions lib/resolvers/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const { ono } = require("@jsdevtools/ono");
const url = require("../util/url");
const { ResolverError } = require("../util/errors");
const { env } = require("../environment/index.js");

module.exports = {
/**
Expand Down Expand Up @@ -73,7 +74,7 @@ module.exports = {
read (file) {
let u = url.parse(file.url);

if (process.browser && !u.protocol) {
if (env.isBrowser() && !u.protocol) {
// Use the protocol of the current page
u.protocol = url.parse(location.href).protocol;
}
Expand Down Expand Up @@ -115,7 +116,7 @@ async function download (u, httpOptions, redirects) {
credentials: httpOptions.withCredentials ? "include" : "omit",
signal: controller.signal,
// browser fetch API does not support redirects https://fetch.spec.whatwg.org/#atomic-http-redirect-handling
redirect: process.browser
redirect: env.isBrowser()
? "follow"
: httpOptions.redirects === 0
? "error"
Expand Down
5 changes: 0 additions & 5 deletions lib/util/text-decoder.js

This file was deleted.

25 changes: 15 additions & 10 deletions lib/util/url.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"use strict";

let isWindows = /^win/.test(process.platform),
forwardSlashPattern = /\//g,
const { env } = require("../environment/index.js");

let forwardSlashPattern = /\//g,
protocolPattern = /^(\w{2,}):\/\//i,
url = module.exports,
slash = /\//g,
Expand Down Expand Up @@ -36,11 +37,11 @@ exports.resolve = require("url").resolve;
* @returns {string}
*/
exports.cwd = function cwd () {
if (process.browser) {
return location.href;
if (env.isBrowser()) {
return env.getCwd();
}

let path = process.cwd();
let path = env.getCwd();

let lastChar = path.slice(-1);
if (lastChar === "/" || lastChar === "\\") {
Expand Down Expand Up @@ -121,7 +122,7 @@ exports.isHttp = function isHttp (path) {
}
else if (protocol === undefined) {
// There is no protocol. If we're running in a browser, then assume it's HTTP.
return process.browser;
return env.isBrowser();
}
else {
// It's some other protocol, such as "ftp://", "mongodb://", etc.
Expand All @@ -137,7 +138,7 @@ exports.isHttp = function isHttp (path) {
* @returns {boolean}
*/
exports.isFileSystemPath = function isFileSystemPath (path) {
if (process.browser) {
if (env.isBrowser()) {
// We're running in a browser, so assume that all paths are URLs.
// This way, even relative paths will be treated as URLs rather than as filesystem paths
return false;
Expand Down Expand Up @@ -166,7 +167,7 @@ exports.isFileSystemPath = function isFileSystemPath (path) {
exports.fromFileSystemPath = function fromFileSystemPath (path) {
// Step 1: On Windows, replace backslashes with forward slashes,
// rather than encoding them as "%5C"
if (isWindows) {
if (isWindows()) {
path = path.replace(/\\/g, "/");
}

Expand Down Expand Up @@ -222,12 +223,12 @@ exports.toFileSystemPath = function toFileSystemPath (path, keepFileProtocol) {
// On Windows, it will start with something like "C:/".
// On Posix, it will start with "/"
isFileUrl = false;
path = isWindows ? path : "/" + path;
path = isWindows() ? path : "/" + path;
}
}

// Step 4: Normalize Windows paths (unless it's a "file://" URL)
if (isWindows && !isFileUrl) {
if (isWindows() && !isFileUrl) {
// Replace forward slashes with backslashes
path = path.replace(forwardSlashPattern, "\\");

Expand Down Expand Up @@ -280,3 +281,7 @@ exports.safePathToPointer = function safePointerToPath (path) {
})
.join("/")}`;
};

function isWindows () {
return env.platform.startsWith("win");
}
11 changes: 0 additions & 11 deletions package-lock.json

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

16 changes: 14 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,24 @@
"url": "https://github.com/stoplightio/json-schema-ref-parser.git"
},
"license": "MIT",
"main": "lib/index.js",
"main": "lib/index.node.js",
"typings": "lib/index.d.ts",
"browser": {
"./lib/index.node.js": "./lib/index.browser.js",
"./lib/environment/node.js": "./lib/environment/browser.js",
"fs": false
},
"exports": {
".": {
"types": "./lib/index.d.ts",
"browser": "./lib/index.browser.js",
"node": "./lib/index.node.js"
},
"./lib/util/url": "./lib/util/url.js",
"./lib/parsers/json": "./lib/parsers/json.js",
"./lib/parsers/yaml": "./lib/parsers/yaml.js",
"./lib/bundle/stoplight/defaults": "./lib/bundle/stoplight/defaults.js"
},
"files": [
"lib"
],
Expand Down Expand Up @@ -81,7 +94,6 @@
"@stoplight/path": "^1.3.2",
"@stoplight/yaml": "^4.0.2",
"call-me-maybe": "^1.0.1",
"fastestsmallesttextencoderdecoder": "^1.0.22",
"url": "^0.11.3"
}
}
10 changes: 0 additions & 10 deletions test/fixtures/polyfill.js

This file was deleted.

2 changes: 1 addition & 1 deletion test/specs/callbacks.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";

const { expect } = require("chai");
const $RefParser = require("../../lib");
const $RefParser = require("../..");
const helper = require("../utils/helper");
const path = require("../utils/path");
const { ParserError } = require("../../lib/util/errors");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const chai = require("chai");
const chaiSubset = require("chai-subset");
chai.use(chaiSubset);
const { expect } = chai;
const $RefParser = require("../../../lib");
const $RefParser = require("../../..");
const path = require("../../utils/path");
const parsedSchema = require("./parsed");
const dereferencedSchema = require("./dereferenced");
Expand Down
2 changes: 1 addition & 1 deletion test/specs/empty/empty.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";

const { expect } = require("chai");
const $RefParser = require("../../../lib");
const $RefParser = require("../../..");
const helper = require("../../utils/helper");
const path = require("../../utils/path");

Expand Down
1 change: 1 addition & 0 deletions test/specs/exports.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ describe("json-schema-ref-parser package exports", () => {
it("should not export anything else", async () => {
expect(commonJSExport).to.have.same.keys(
"default",
"env",
"parse",
"resolve",
"dereference",
Expand Down
2 changes: 1 addition & 1 deletion test/specs/http.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { host } = require("@jsdevtools/host-environment");
const { expect, use } = require("chai");
const chai = require("chai");
const spies = require("chai-spies");
const $RefParser = require("../../lib");
const $RefParser = require("../..");

use(spies);

Expand Down
2 changes: 1 addition & 1 deletion test/specs/invalid-pointers/invalid-pointers.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const chai = require("chai");
const chaiSubset = require("chai-subset");
chai.use(chaiSubset);
const { expect } = chai;
const $RefParser = require("../../../lib");
const $RefParser = require("../../..");
const helper = require("../../utils/helper");
const path = require("../../utils/path");
const { JSONParserErrorGroup, InvalidPointerError } = require("../../../lib/util/errors");
Expand Down
2 changes: 1 addition & 1 deletion test/specs/invalid/invalid.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const chai = require("chai");
const chaiSubset = require("chai-subset");
chai.use(chaiSubset);
const { expect } = chai;
const $RefParser = require("../../../lib");
const $RefParser = require("../../..");
const helper = require("../../utils/helper");
const path = require("../../utils/path");
const { JSONParserErrorGroup, ParserError, ResolverError } = require("../../../lib/util/errors");
Expand Down
2 changes: 1 addition & 1 deletion test/specs/missing-pointers/missing-pointers.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const chai = require("chai");
const chaiSubset = require("chai-subset");
chai.use(chaiSubset);
const { expect } = chai;
const $RefParser = require("../../../lib");
const $RefParser = require("../../..");
const { JSONParserErrorGroup, MissingPointerError } = require("../../../lib/util/errors");
const helper = require("../../utils/helper");

Expand Down
Loading

0 comments on commit 6f6d49e

Please sign in to comment.