@loaders.gl/loader-utils 3.4.11 → 3.4.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. package/dist/es5/json-loader.js +1 -1
  2. package/dist/es5/lib/node/fs.browser.js +33 -0
  3. package/dist/es5/lib/node/fs.browser.js.map +1 -0
  4. package/dist/es5/lib/node/fs.js +17 -31
  5. package/dist/es5/lib/node/fs.js.map +1 -1
  6. package/dist/es5/lib/node/stream.browser.js +11 -0
  7. package/dist/es5/lib/node/stream.browser.js.map +1 -0
  8. package/dist/es5/lib/node/stream.js +1 -4
  9. package/dist/es5/lib/node/stream.js.map +1 -1
  10. package/dist/esm/json-loader.js +1 -1
  11. package/dist/esm/lib/node/fs.browser.js +14 -0
  12. package/dist/esm/lib/node/fs.browser.js.map +1 -0
  13. package/dist/esm/lib/node/fs.js +12 -26
  14. package/dist/esm/lib/node/fs.js.map +1 -1
  15. package/dist/esm/lib/node/stream.browser.js +3 -0
  16. package/dist/esm/lib/node/stream.browser.js.map +1 -0
  17. package/dist/esm/lib/node/stream.js +1 -4
  18. package/dist/esm/lib/node/stream.js.map +1 -1
  19. package/dist/lib/node/fs.browser.d.ts +14 -0
  20. package/dist/lib/node/fs.browser.d.ts.map +1 -0
  21. package/dist/lib/node/fs.d.ts +14 -12
  22. package/dist/lib/node/fs.d.ts.map +1 -1
  23. package/dist/lib/node/stream.browser.d.ts +3 -0
  24. package/dist/lib/node/stream.browser.d.ts.map +1 -0
  25. package/dist/lib/node/stream.d.ts +4 -1
  26. package/dist/lib/node/stream.d.ts.map +1 -1
  27. package/package.json +9 -5
  28. package/src/lib/node/fs.browser.ts +14 -0
  29. package/src/lib/node/fs.ts +14 -48
  30. package/src/lib/node/stream.browser.ts +3 -0
  31. package/src/lib/node/stream.ts +2 -10
  32. package/dist/index.js +0 -122
  33. package/dist/json-loader.js +0 -28
  34. package/dist/lib/binary-utils/array-buffer-utils.js +0 -84
  35. package/dist/lib/binary-utils/dataview-copy-utils.js +0 -97
  36. package/dist/lib/binary-utils/get-first-characters.js +0 -45
  37. package/dist/lib/binary-utils/memory-conversion-utils.js +0 -73
  38. package/dist/lib/binary-utils/memory-copy-utils.js +0 -61
  39. package/dist/lib/env-utils/assert.js +0 -13
  40. package/dist/lib/env-utils/globals.js +0 -30
  41. package/dist/lib/filesystems/node-filesystem.js +0 -73
  42. package/dist/lib/filesystems/readable-file.js +0 -25
  43. package/dist/lib/filesystems/writable-file.js +0 -48
  44. package/dist/lib/iterators/async-iteration.js +0 -53
  45. package/dist/lib/iterators/text-iterators.js +0 -61
  46. package/dist/lib/node/buffer.browser.js +0 -22
  47. package/dist/lib/node/buffer.js +0 -36
  48. package/dist/lib/node/fs.js +0 -49
  49. package/dist/lib/node/promisify.js +0 -22
  50. package/dist/lib/node/stream.js +0 -17
  51. package/dist/lib/option-utils/merge-loader-options.js +0 -24
  52. package/dist/lib/parser-utils/parse-json.js +0 -16
  53. package/dist/lib/path-utils/file-aliases.js +0 -47
  54. package/dist/lib/path-utils/get-cwd.js +0 -12
  55. package/dist/lib/path-utils/path.js +0 -178
  56. package/dist/lib/request-utils/request-scheduler.js +0 -142
  57. package/dist/lib/worker-loader-utils/create-loader-worker.js +0 -98
  58. package/dist/lib/worker-loader-utils/encode-with-worker.js +0 -21
  59. package/dist/lib/worker-loader-utils/parse-with-worker.js +0 -81
  60. package/dist/types.js +0 -3
  61. package/dist/workers/json-worker.js +0 -5
@@ -1,48 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
- Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.makeWritableFile = void 0;
27
- // Forked from https://github.com/kbajalc/parquets under MIT license (Copyright (c) 2017 ironSource Ltd.)
28
- const globals_1 = require("../env-utils/globals");
29
- const fs = __importStar(require("../node/fs"));
30
- /** Helper function to create an envelope reader for a binary memory input */
31
- function makeWritableFile(pathOrStream, options) {
32
- if (globals_1.isBrowser) {
33
- return {
34
- write: async () => { },
35
- close: async () => { }
36
- };
37
- }
38
- const outputStream = typeof pathOrStream === 'string' ? fs.createWriteStream(pathOrStream, options) : pathOrStream;
39
- return {
40
- write: async (buffer) => new Promise((resolve, reject) => {
41
- outputStream.write(buffer, (err) => (err ? reject(err) : resolve()));
42
- }),
43
- close: () => new Promise((resolve, reject) => {
44
- outputStream.close((err) => (err ? reject(err) : resolve()));
45
- })
46
- };
47
- }
48
- exports.makeWritableFile = makeWritableFile;
@@ -1,53 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.concatenateStringsAsync = exports.concatenateArrayBuffersAsync = exports.forEach = void 0;
4
- const array_buffer_utils_1 = require("../binary-utils/array-buffer-utils");
5
- // GENERAL UTILITIES
6
- /**
7
- * Iterate over async iterator, without resetting iterator if end is not reached
8
- * - forEach intentionally does not reset iterator if exiting loop prematurely
9
- * so that iteration can continue in a second loop
10
- * - It is recommended to use a standard for-await as last loop to ensure
11
- * iterator gets properly reset
12
- *
13
- * TODO - optimize using sync iteration if argument is an Iterable?
14
- *
15
- * @param iterator
16
- * @param visitor
17
- */
18
- async function forEach(iterator, visitor) {
19
- // eslint-disable-next-line
20
- while (true) {
21
- const { done, value } = await iterator.next();
22
- if (done) {
23
- iterator.return();
24
- return;
25
- }
26
- const cancel = visitor(value);
27
- if (cancel) {
28
- return;
29
- }
30
- }
31
- }
32
- exports.forEach = forEach;
33
- // Breaking big data into iterable chunks, concatenating iterable chunks into big data objects
34
- /**
35
- * Concatenates all data chunks yielded by an (async) iterator
36
- * This function can e.g. be used to enable atomic parsers to work on (async) iterator inputs
37
- */
38
- async function concatenateArrayBuffersAsync(asyncIterator) {
39
- const arrayBuffers = [];
40
- for await (const chunk of asyncIterator) {
41
- arrayBuffers.push(chunk);
42
- }
43
- return (0, array_buffer_utils_1.concatenateArrayBuffers)(...arrayBuffers);
44
- }
45
- exports.concatenateArrayBuffersAsync = concatenateArrayBuffersAsync;
46
- async function concatenateStringsAsync(asyncIterator) {
47
- const strings = [];
48
- for await (const chunk of asyncIterator) {
49
- strings.push(chunk);
50
- }
51
- return strings.join('');
52
- }
53
- exports.concatenateStringsAsync = concatenateStringsAsync;
@@ -1,61 +0,0 @@
1
- "use strict";
2
- // TextDecoder iterators
3
- // TextDecoder will keep any partial undecoded bytes between calls to `decode`
4
- Object.defineProperty(exports, "__esModule", { value: true });
5
- exports.makeNumberedLineIterator = exports.makeLineIterator = exports.makeTextEncoderIterator = exports.makeTextDecoderIterator = void 0;
6
- async function* makeTextDecoderIterator(arrayBufferIterator, options = {}) {
7
- const textDecoder = new TextDecoder(undefined, options);
8
- for await (const arrayBuffer of arrayBufferIterator) {
9
- yield typeof arrayBuffer === 'string'
10
- ? arrayBuffer
11
- : textDecoder.decode(arrayBuffer, { stream: true });
12
- }
13
- }
14
- exports.makeTextDecoderIterator = makeTextDecoderIterator;
15
- // TextEncoder iterator
16
- // TODO - this is not useful unless min chunk size is given
17
- // TextEncoder will keep any partial undecoded bytes between calls to `encode`
18
- // If iterator does not yield strings, assume arrayBuffer and return unencoded
19
- async function* makeTextEncoderIterator(textIterator) {
20
- const textEncoder = new TextEncoder();
21
- for await (const text of textIterator) {
22
- yield typeof text === 'string' ? textEncoder.encode(text) : text;
23
- }
24
- }
25
- exports.makeTextEncoderIterator = makeTextEncoderIterator;
26
- /**
27
- * @param textIterator async iterable yielding strings
28
- * @returns an async iterable over lines
29
- * See http://2ality.com/2018/04/async-iter-nodejs.html
30
- */
31
- async function* makeLineIterator(textIterator) {
32
- let previous = '';
33
- for await (const textChunk of textIterator) {
34
- previous += textChunk;
35
- let eolIndex;
36
- while ((eolIndex = previous.indexOf('\n')) >= 0) {
37
- // line includes the EOL
38
- const line = previous.slice(0, eolIndex + 1);
39
- previous = previous.slice(eolIndex + 1);
40
- yield line;
41
- }
42
- }
43
- if (previous.length > 0) {
44
- yield previous;
45
- }
46
- }
47
- exports.makeLineIterator = makeLineIterator;
48
- /**
49
- * @param lineIterator async iterable yielding lines
50
- * @returns async iterable yielding numbered lines
51
- *
52
- * See http://2ality.com/2018/04/async-iter-nodejs.html
53
- */
54
- async function* makeNumberedLineIterator(lineIterator) {
55
- let counter = 1;
56
- for await (const line of lineIterator) {
57
- yield { counter, line };
58
- counter++;
59
- }
60
- }
61
- exports.makeNumberedLineIterator = makeNumberedLineIterator;
@@ -1,22 +0,0 @@
1
- "use strict";
2
- // loaders.gl, MIT license
3
- Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.toBuffer = exports.toArrayBuffer = void 0;
5
- // Isolates Buffer references to ensure they are only bundled under Node.js (avoids big webpack polyfill)
6
- // this file is selected by the package.json "browser" field).
7
- /**
8
- * Convert Buffer to ArrayBuffer
9
- * Converts Node.js `Buffer` to `ArrayBuffer` (without triggering bundler to include Buffer polyfill on browser)
10
- * @todo better data type
11
- */
12
- function toArrayBuffer(buffer) {
13
- return buffer;
14
- }
15
- exports.toArrayBuffer = toArrayBuffer;
16
- /**
17
- * Convert (copy) ArrayBuffer to Buffer
18
- */
19
- function toBuffer(binaryData) {
20
- throw new Error('Buffer not supported in browser');
21
- }
22
- exports.toBuffer = toBuffer;
@@ -1,36 +0,0 @@
1
- "use strict";
2
- // loaders.gl, MIT license
3
- Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.toBuffer = exports.toArrayBuffer = void 0;
5
- // Isolates Buffer references to ensure they are only bundled under Node.js (avoids big webpack polyfill)
6
- // this file is selected by the package.json "browser" field).
7
- /**
8
- * Convert Buffer to ArrayBuffer
9
- * Converts Node.js `Buffer` to `ArrayBuffer` (without triggering bundler to include Buffer polyfill on browser)
10
- * @todo better data type
11
- */
12
- function toArrayBuffer(buffer) {
13
- // TODO - per docs we should just be able to call buffer.buffer, but there are issues
14
- if (Buffer.isBuffer(buffer)) {
15
- const typedArray = new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.length);
16
- return typedArray.slice().buffer;
17
- }
18
- return buffer;
19
- }
20
- exports.toArrayBuffer = toArrayBuffer;
21
- /**
22
- * Convert (copy) ArrayBuffer to Buffer
23
- */
24
- function toBuffer(binaryData) {
25
- if (Buffer.isBuffer(binaryData)) {
26
- return binaryData;
27
- }
28
- if (ArrayBuffer.isView(binaryData)) {
29
- binaryData = binaryData.buffer;
30
- }
31
- if (typeof Buffer !== 'undefined' && binaryData instanceof ArrayBuffer) {
32
- return Buffer.from(binaryData);
33
- }
34
- throw new Error('toBuffer');
35
- }
36
- exports.toBuffer = toBuffer;
@@ -1,49 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports._readToArrayBuffer = exports.isSupported = exports.createWriteStream = exports.fstat = exports.read = exports.close = exports.open = exports.writeFileSync = exports.writeFile = exports.readFileSync = exports.readFile = exports.stat = exports.readdir = void 0;
7
- // fs wrapper (promisified fs + avoids bundling fs in browsers)
8
- const fs_1 = __importDefault(require("fs"));
9
- const buffer_1 = require("./buffer");
10
- const promisify_1 = require("./promisify");
11
- exports.isSupported = Boolean(fs_1.default);
12
- // paths
13
- try {
14
- /** Wrapper for Node.js fs method */
15
- exports.readdir = (0, promisify_1.promisify2)(fs_1.default.readdir);
16
- /** Wrapper for Node.js fs method */
17
- exports.stat = (0, promisify_1.promisify2)(fs_1.default.stat);
18
- /** Wrapper for Node.js fs method */
19
- exports.readFile = fs_1.default.readFile;
20
- /** Wrapper for Node.js fs method */
21
- exports.readFileSync = fs_1.default.readFileSync;
22
- /** Wrapper for Node.js fs method */
23
- exports.writeFile = (0, promisify_1.promisify3)(fs_1.default.writeFile);
24
- /** Wrapper for Node.js fs method */
25
- exports.writeFileSync = fs_1.default.writeFileSync;
26
- // file descriptors
27
- /** Wrapper for Node.js fs method */
28
- exports.open = fs_1.default.open;
29
- /** Wrapper for Node.js fs method */
30
- exports.close = (fd) => new Promise((resolve, reject) => fs_1.default.close(fd, (err) => (err ? reject(err) : resolve())));
31
- /** Wrapper for Node.js fs method */
32
- exports.read = fs_1.default.read;
33
- /** Wrapper for Node.js fs method */
34
- exports.fstat = fs_1.default.fstat;
35
- exports.createWriteStream = fs_1.default.createWriteStream;
36
- exports.isSupported = Boolean(fs_1.default);
37
- }
38
- catch {
39
- // ignore
40
- }
41
- async function _readToArrayBuffer(fd, start, length) {
42
- const buffer = Buffer.alloc(length);
43
- const { bytesRead } = await (0, exports.read)(fd, buffer, 0, length, start);
44
- if (bytesRead !== length) {
45
- throw new Error('fs.read failed');
46
- }
47
- return (0, buffer_1.toArrayBuffer)(buffer);
48
- }
49
- exports._readToArrayBuffer = _readToArrayBuffer;
@@ -1,22 +0,0 @@
1
- "use strict";
2
- // @loaders.gl, MIT license
3
- Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.promisify3 = exports.promisify2 = exports.promisify1 = void 0;
5
- /**
6
- * Typesafe promisify implementation
7
- * @link https://dev.to/_gdelgado/implement-a-type-safe-version-of-node-s-promisify-in-7-lines-of-code-in-typescript-2j34
8
- * @param fn
9
- * @returns
10
- */
11
- function promisify1(fn) {
12
- return (args) => new Promise((resolve, reject) => fn(args, (error, callbackArgs) => (error ? reject(error) : resolve(callbackArgs))));
13
- }
14
- exports.promisify1 = promisify1;
15
- function promisify2(fn) {
16
- return (arg1, arg2) => new Promise((resolve, reject) => fn(arg1, arg2, (error, callbackArgs) => (error ? reject(error) : resolve(callbackArgs))));
17
- }
18
- exports.promisify2 = promisify2;
19
- function promisify3(fn) {
20
- return (arg1, arg2, arg3) => new Promise((resolve, reject) => fn(arg1, arg2, arg3, (error, callbackArgs) => (error ? reject(error) : resolve(callbackArgs))));
21
- }
22
- exports.promisify3 = promisify3;
@@ -1,17 +0,0 @@
1
- "use strict";
2
- // loaders.gl, MIT license
3
- var __importDefault = (this && this.__importDefault) || function (mod) {
4
- return (mod && mod.__esModule) ? mod : { "default": mod };
5
- };
6
- Object.defineProperty(exports, "__esModule", { value: true });
7
- exports.isSupported = exports.Transform = void 0;
8
- const stream_1 = __importDefault(require("stream"));
9
- exports.isSupported = Boolean(stream_1.default);
10
- // paths
11
- try {
12
- /** Wrapper for Node.js fs method */
13
- exports.Transform = stream_1.default.Transform;
14
- }
15
- catch {
16
- // ignore
17
- }
@@ -1,24 +0,0 @@
1
- "use strict";
2
- // loaders.gl, MIT license
3
- Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.mergeLoaderOptions = void 0;
5
- /**
6
- *
7
- * @param baseOptions Can be undefined, in which case a fresh options object will be minted
8
- * @param newOptions
9
- * @returns
10
- */
11
- function mergeLoaderOptions(baseOptions, newOptions) {
12
- const options = { ...baseOptions };
13
- for (const [key, newValue] of Object.entries(newOptions)) {
14
- if (newValue && typeof newValue === 'object') {
15
- options[key] = options[key] || {};
16
- Object.assign(options[key], newOptions[key]);
17
- }
18
- else {
19
- options[key] = newOptions[key];
20
- }
21
- }
22
- return options;
23
- }
24
- exports.mergeLoaderOptions = mergeLoaderOptions;
@@ -1,16 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.parseJSON = void 0;
4
- const get_first_characters_1 = require("../binary-utils/get-first-characters");
5
- /**
6
- * Minimal JSON parser that throws more meaningful error messages
7
- */
8
- function parseJSON(string) {
9
- try {
10
- return JSON.parse(string);
11
- }
12
- catch (_) {
13
- throw new Error(`Failed to parse JSON from data starting with "${(0, get_first_characters_1.getFirstCharacters)(string)}"`);
14
- }
15
- }
16
- exports.parseJSON = parseJSON;
@@ -1,47 +0,0 @@
1
- "use strict";
2
- // Simple file alias mechanisms for tests.
3
- Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.resolvePath = exports.addAliases = exports.getPathPrefix = exports.setPathPrefix = void 0;
5
- let pathPrefix = '';
6
- const fileAliases = {};
7
- /*
8
- * Set a relative path prefix
9
- */
10
- function setPathPrefix(prefix) {
11
- pathPrefix = prefix;
12
- }
13
- exports.setPathPrefix = setPathPrefix;
14
- /*
15
- * Get the relative path prefix
16
- */
17
- function getPathPrefix() {
18
- return pathPrefix;
19
- }
20
- exports.getPathPrefix = getPathPrefix;
21
- /**
22
- *
23
- * @param aliases
24
- *
25
- * Note: addAliases are an experimental export, they are only for testing of loaders.gl loaders
26
- * not intended as a generic aliasing mechanism
27
- */
28
- function addAliases(aliases) {
29
- Object.assign(fileAliases, aliases);
30
- }
31
- exports.addAliases = addAliases;
32
- /**
33
- * Resolves aliases and adds path-prefix to paths
34
- */
35
- function resolvePath(filename) {
36
- for (const alias in fileAliases) {
37
- if (filename.startsWith(alias)) {
38
- const replacement = fileAliases[alias];
39
- filename = filename.replace(alias, replacement);
40
- }
41
- }
42
- if (!filename.startsWith('http://') && !filename.startsWith('https://')) {
43
- filename = `${pathPrefix}${filename}`;
44
- }
45
- return filename;
46
- }
47
- exports.resolvePath = resolvePath;
@@ -1,12 +0,0 @@
1
- "use strict";
2
- // loaders.gl MIT license
3
- Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.getCWD = void 0;
5
- function getCWD() {
6
- if (typeof process !== 'undefined' && typeof process.cwd !== 'undefined') {
7
- return process.cwd();
8
- }
9
- const pathname = window.location?.pathname;
10
- return pathname?.slice(0, pathname.lastIndexOf('/') + 1) || '';
11
- }
12
- exports.getCWD = getCWD;
@@ -1,178 +0,0 @@
1
- "use strict";
2
- // Beginning of a minimal implementation of the Node.js path API, that doesn't pull in big polyfills.
3
- Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.resolve = exports.join = exports.dirname = exports.filename = void 0;
5
- const get_cwd_1 = require("./get-cwd");
6
- /**
7
- * Replacement for Node.js path.filename
8
- * @param url
9
- */
10
- function filename(url) {
11
- const slashIndex = url ? url.lastIndexOf('/') : -1;
12
- return slashIndex >= 0 ? url.substr(slashIndex + 1) : '';
13
- }
14
- exports.filename = filename;
15
- /**
16
- * Replacement for Node.js path.dirname
17
- * @param url
18
- */
19
- function dirname(url) {
20
- const slashIndex = url ? url.lastIndexOf('/') : -1;
21
- return slashIndex >= 0 ? url.substr(0, slashIndex) : '';
22
- }
23
- exports.dirname = dirname;
24
- /**
25
- * Replacement for Node.js path.join
26
- * @param parts
27
- */
28
- function join(...parts) {
29
- const separator = '/';
30
- parts = parts.map((part, index) => {
31
- if (index) {
32
- part = part.replace(new RegExp(`^${separator}`), '');
33
- }
34
- if (index !== parts.length - 1) {
35
- part = part.replace(new RegExp(`${separator}$`), '');
36
- }
37
- return part;
38
- });
39
- return parts.join(separator);
40
- }
41
- exports.join = join;
42
- /* eslint-disable no-continue */
43
- /**
44
- * https://nodejs.org/api/path.html#path_path_resolve_paths
45
- * @param paths A sequence of paths or path segments.
46
- * @return resolved path
47
- * Forked from BTOdell/path-resolve under MIT license
48
- * @see https://github.com/BTOdell/path-resolve/blob/master/LICENSE
49
- */
50
- function resolve(...components) {
51
- const paths = [];
52
- for (let _i = 0; _i < components.length; _i++) {
53
- paths[_i] = components[_i];
54
- }
55
- let resolvedPath = '';
56
- let resolvedAbsolute = false;
57
- let cwd;
58
- for (let i = paths.length - 1; i >= -1 && !resolvedAbsolute; i--) {
59
- let path;
60
- if (i >= 0) {
61
- path = paths[i];
62
- }
63
- else {
64
- if (cwd === undefined) {
65
- cwd = (0, get_cwd_1.getCWD)();
66
- }
67
- path = cwd;
68
- }
69
- // Skip empty entries
70
- if (path.length === 0) {
71
- continue;
72
- }
73
- resolvedPath = `${path}/${resolvedPath}`;
74
- resolvedAbsolute = path.charCodeAt(0) === SLASH;
75
- }
76
- // At this point the path should be resolved to a full absolute path, but
77
- // handle relative paths to be safe (might happen when process.cwd() fails)
78
- // Normalize the path (removes leading slash)
79
- resolvedPath = normalizeStringPosix(resolvedPath, !resolvedAbsolute);
80
- if (resolvedAbsolute) {
81
- return `/${resolvedPath}`;
82
- }
83
- else if (resolvedPath.length > 0) {
84
- return resolvedPath;
85
- }
86
- return '.';
87
- }
88
- exports.resolve = resolve;
89
- const SLASH = 47;
90
- const DOT = 46;
91
- /**
92
- * Resolves . and .. elements in a path with directory names
93
- * Forked from BTOdell/path-resolve under MIT license
94
- * @see https://github.com/BTOdell/path-resolve/blob/master/LICENSE
95
- */
96
- /* eslint-disable max-depth */
97
- // eslint-disable-next-line complexity, max-statements
98
- function normalizeStringPosix(path, allowAboveRoot) {
99
- let res = '';
100
- let lastSlash = -1;
101
- let dots = 0;
102
- let code;
103
- let isAboveRoot = false;
104
- for (let i = 0; i <= path.length; ++i) {
105
- if (i < path.length) {
106
- code = path.charCodeAt(i);
107
- }
108
- else if (code === SLASH) {
109
- break;
110
- }
111
- else {
112
- code = SLASH;
113
- }
114
- if (code === SLASH) {
115
- if (lastSlash === i - 1 || dots === 1) {
116
- // NOOP
117
- }
118
- else if (lastSlash !== i - 1 && dots === 2) {
119
- if (res.length < 2 ||
120
- !isAboveRoot ||
121
- res.charCodeAt(res.length - 1) !== DOT ||
122
- res.charCodeAt(res.length - 2) !== DOT) {
123
- if (res.length > 2) {
124
- const start = res.length - 1;
125
- let j = start;
126
- for (; j >= 0; --j) {
127
- if (res.charCodeAt(j) === SLASH) {
128
- break;
129
- }
130
- }
131
- if (j !== start) {
132
- res = j === -1 ? '' : res.slice(0, j);
133
- lastSlash = i;
134
- dots = 0;
135
- isAboveRoot = false;
136
- continue;
137
- }
138
- }
139
- else if (res.length === 2 || res.length === 1) {
140
- res = '';
141
- lastSlash = i;
142
- dots = 0;
143
- isAboveRoot = false;
144
- continue;
145
- }
146
- }
147
- if (allowAboveRoot) {
148
- if (res.length > 0) {
149
- res += '/..';
150
- }
151
- else {
152
- res = '..';
153
- }
154
- isAboveRoot = true;
155
- }
156
- }
157
- else {
158
- const slice = path.slice(lastSlash + 1, i);
159
- if (res.length > 0) {
160
- res += `/${slice}`;
161
- }
162
- else {
163
- res = slice;
164
- }
165
- isAboveRoot = false;
166
- }
167
- lastSlash = i;
168
- dots = 0;
169
- }
170
- else if (code === DOT && dots !== -1) {
171
- ++dots;
172
- }
173
- else {
174
- dots = -1;
175
- }
176
- }
177
- return res;
178
- }