cspell-io 6.26.2 → 6.27.0

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.
@@ -0,0 +1,4 @@
1
+ import type { BufferEncoding } from '../models/BufferEncoding';
2
+ export type { BufferEncoding } from '../models/BufferEncoding';
3
+ export type BufferEncodingExt = BufferEncoding | 'utf16be';
4
+ //# sourceMappingURL=BufferEncoding.d.ts.map
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=BufferEncoding.js.map
@@ -0,0 +1,12 @@
1
+ /// <reference types="node" />
2
+ import type { BufferEncodingExt } from './BufferEncoding';
3
+ export declare function decodeUtf16LE(buf: Buffer): string;
4
+ export declare function decodeUtf16BE(buf: Buffer): string;
5
+ export declare function decode(buf: Buffer, encoding?: BufferEncodingExt): string;
6
+ export declare function swapBytesInPlace(buf: Buffer): Buffer;
7
+ export declare function swapBytes(buf: Buffer): Buffer;
8
+ export declare function encodeString(str: string, encoding?: BufferEncodingExt, bom?: boolean): Buffer;
9
+ export declare function encodeUtf16LE(str: string, bom?: boolean): Buffer;
10
+ export declare function encodeUtf16BE(str: string, bom?: boolean): Buffer;
11
+ export declare function calcEncodingFromBom(buf: Buffer): 'utf16be' | 'utf16le' | undefined;
12
+ //# sourceMappingURL=encode-decode.d.ts.map
@@ -0,0 +1,84 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.calcEncodingFromBom = exports.encodeUtf16BE = exports.encodeUtf16LE = exports.encodeString = exports.swapBytes = exports.swapBytesInPlace = exports.decode = exports.decodeUtf16BE = exports.decodeUtf16LE = void 0;
4
+ const BOM_BE = 0xfeff;
5
+ const BOM_LE = 0xfffe;
6
+ function decodeUtf16LE(buf) {
7
+ const bom = (buf[0] << 8) | buf[1];
8
+ buf = bom === BOM_LE ? buf.subarray(2) : buf;
9
+ return buf.toString('utf16le');
10
+ }
11
+ exports.decodeUtf16LE = decodeUtf16LE;
12
+ function decodeUtf16BE(buf) {
13
+ return decodeUtf16LE(swapBytes(buf));
14
+ }
15
+ exports.decodeUtf16BE = decodeUtf16BE;
16
+ function decode(buf, encoding) {
17
+ switch (encoding) {
18
+ case 'utf16be':
19
+ return decodeUtf16BE(buf);
20
+ case 'utf16le':
21
+ return decodeUtf16LE(buf);
22
+ }
23
+ if (buf.length < 2 || (encoding && !encoding.startsWith('utf')))
24
+ return buf.toString(encoding);
25
+ const bom = (buf[0] << 8) | buf[1];
26
+ if (bom === BOM_BE || (buf[0] === 0 && buf[1] !== 0))
27
+ return decodeUtf16BE(buf);
28
+ if (bom === BOM_LE || (buf[0] !== 0 && buf[1] === 0))
29
+ return decodeUtf16LE(buf);
30
+ return buf.toString(encoding);
31
+ }
32
+ exports.decode = decode;
33
+ function swapBytesInPlace(buf) {
34
+ for (let i = 0; i < buf.length - 1; i += 2) {
35
+ const v = buf[i];
36
+ buf[i] = buf[i + 1];
37
+ buf[i + 1] = v;
38
+ }
39
+ return buf;
40
+ }
41
+ exports.swapBytesInPlace = swapBytesInPlace;
42
+ function swapBytes(buf) {
43
+ const tBuf = Buffer.from(buf);
44
+ return swapBytesInPlace(tBuf);
45
+ }
46
+ exports.swapBytes = swapBytes;
47
+ function encodeString(str, encoding, bom) {
48
+ switch (encoding) {
49
+ case 'utf16be':
50
+ return encodeUtf16BE(str, bom);
51
+ case 'utf16le':
52
+ return encodeUtf16LE(str, bom);
53
+ }
54
+ return Buffer.from(str, encoding);
55
+ }
56
+ exports.encodeString = encodeString;
57
+ function encodeUtf16LE(str, bom = true) {
58
+ const buf = Buffer.from(str, 'utf16le');
59
+ if (bom) {
60
+ const target = Buffer.alloc(buf.length + 2);
61
+ target.writeUint16LE(BOM_BE);
62
+ buf.copy(target, 2);
63
+ return target;
64
+ }
65
+ return buf;
66
+ }
67
+ exports.encodeUtf16LE = encodeUtf16LE;
68
+ function encodeUtf16BE(str, bom = true) {
69
+ return swapBytesInPlace(encodeUtf16LE(str, bom));
70
+ }
71
+ exports.encodeUtf16BE = encodeUtf16BE;
72
+ function calcEncodingFromBom(buf) {
73
+ if (buf.length < 2)
74
+ return undefined;
75
+ switch ((buf[0] << 8) | buf[1]) {
76
+ case BOM_BE:
77
+ return 'utf16be';
78
+ case BOM_LE:
79
+ return 'utf16le';
80
+ }
81
+ return undefined;
82
+ }
83
+ exports.calcEncodingFromBom = calcEncodingFromBom;
84
+ //# sourceMappingURL=encode-decode.js.map
@@ -0,0 +1,7 @@
1
+ /// <reference types="node" />
2
+ import type { BufferEncodingExt } from './BufferEncoding';
3
+ export declare function createDecoderTransformer(encoding?: BufferEncodingExt): (iterable: AsyncIterable<string | Buffer> | Iterable<string | Buffer>) => AsyncIterable<string>;
4
+ export declare function encoderTransformer(iterable: Iterable<string>, encoding?: BufferEncodingExt): Iterable<Buffer>;
5
+ export declare function encoderTransformer(iterable: AsyncIterable<string>, encoding?: BufferEncodingExt): AsyncIterable<Buffer>;
6
+ export declare function encoderTransformer(iterable: Iterable<string> | AsyncIterable<string>, encoding?: BufferEncodingExt): Iterable<Buffer> | AsyncIterable<Buffer>;
7
+ //# sourceMappingURL=transformers.d.ts.map
@@ -0,0 +1,70 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.encoderTransformer = exports.createDecoderTransformer = void 0;
4
+ const encode_decode_1 = require("./encode-decode");
5
+ function createDecoderTransformer(encoding) {
6
+ function standardDecoder(buf) {
7
+ return (0, encode_decode_1.decode)(buf, encoding);
8
+ }
9
+ let decoder = undefined;
10
+ if (encoding && !encoding.startsWith('utf'))
11
+ return decoderNonUtf;
12
+ return decoderUtf;
13
+ async function* decoderNonUtf(iterable) {
14
+ for await (const buf of iterable) {
15
+ yield typeof buf === 'string' ? buf : (0, encode_decode_1.decode)(buf, encoding);
16
+ }
17
+ }
18
+ async function* decoderUtf(iterable) {
19
+ for await (const sb of iterable) {
20
+ if (typeof sb === 'string') {
21
+ yield sb;
22
+ continue;
23
+ }
24
+ if (sb.length < 2) {
25
+ yield standardDecoder(sb);
26
+ continue;
27
+ }
28
+ if (decoder) {
29
+ yield decoder(sb);
30
+ continue;
31
+ }
32
+ decoder = standardDecoder;
33
+ const _encoding = (0, encode_decode_1.calcEncodingFromBom)(sb);
34
+ if (_encoding === 'utf16le') {
35
+ decoder = encode_decode_1.decodeUtf16LE;
36
+ yield decoder(sb.subarray(2));
37
+ continue;
38
+ }
39
+ if (_encoding === 'utf16be') {
40
+ decoder = encode_decode_1.decodeUtf16BE;
41
+ yield decoder(sb.subarray(2));
42
+ continue;
43
+ }
44
+ yield decoder(sb);
45
+ }
46
+ }
47
+ }
48
+ exports.createDecoderTransformer = createDecoderTransformer;
49
+ function encoderTransformer(iterable, encoding) {
50
+ return isAsyncIterable(iterable) ? encoderAsyncIterable(iterable, encoding) : encoderIterable(iterable, encoding);
51
+ }
52
+ exports.encoderTransformer = encoderTransformer;
53
+ function* encoderIterable(iterable, encoding) {
54
+ let useBom = true;
55
+ for (const chunk of iterable) {
56
+ yield (0, encode_decode_1.encodeString)(chunk, encoding, useBom);
57
+ useBom = false;
58
+ }
59
+ }
60
+ async function* encoderAsyncIterable(iterable, encoding) {
61
+ let useBom = true;
62
+ for await (const chunk of iterable) {
63
+ yield (0, encode_decode_1.encodeString)(chunk, encoding, useBom);
64
+ useBom = false;
65
+ }
66
+ }
67
+ function isAsyncIterable(v) {
68
+ return v && typeof v === 'object' && !!v[Symbol.asyncIterator];
69
+ }
70
+ //# sourceMappingURL=transformers.js.map
@@ -1,3 +1,3 @@
1
- export { writeToFile, writeToFileIterable, writeToFileIterableP } from './../node/file';
1
+ export { writeToFile, writeToFileIterable, writeToFileIterable as writeToFileIterableP } from './../node/file';
2
2
  export { getStat, getStatSync, readFile, readFileSync } from './file';
3
3
  //# sourceMappingURL=index.d.ts.map
@@ -4,7 +4,7 @@ exports.readFileSync = exports.readFile = exports.getStatSync = exports.getStat
4
4
  var file_1 = require("./../node/file");
5
5
  Object.defineProperty(exports, "writeToFile", { enumerable: true, get: function () { return file_1.writeToFile; } });
6
6
  Object.defineProperty(exports, "writeToFileIterable", { enumerable: true, get: function () { return file_1.writeToFileIterable; } });
7
- Object.defineProperty(exports, "writeToFileIterableP", { enumerable: true, get: function () { return file_1.writeToFileIterableP; } });
7
+ Object.defineProperty(exports, "writeToFileIterableP", { enumerable: true, get: function () { return file_1.writeToFileIterable; } });
8
8
  var file_2 = require("./file");
9
9
  Object.defineProperty(exports, "getStat", { enumerable: true, get: function () { return file_2.getStat; } });
10
10
  Object.defineProperty(exports, "getStatSync", { enumerable: true, get: function () { return file_2.getStatSync; } });
@@ -9,6 +9,7 @@ const assert_1 = __importDefault(require("assert"));
9
9
  const fs_1 = require("fs");
10
10
  const url_1 = require("url");
11
11
  const zlib_1 = require("zlib");
12
+ const encode_decode_1 = require("../../common/encode-decode");
12
13
  const errors_1 = require("../../errors");
13
14
  const dataUrl_1 = require("../../node/dataUrl");
14
15
  const fetch_1 = require("../../node/file/fetch");
@@ -97,7 +98,7 @@ const handleRequestFsReadBinaryFileData = requests_1.RequestFsReadBinaryFile.cre
97
98
  return (0, cspell_service_bus_1.createResponse)(Promise.resolve(res.value));
98
99
  }, undefined, 'Node: Read data: urls.');
99
100
  function bufferToText(buf, encoding) {
100
- return buf[0] === 0x1f && buf[1] === 0x8b ? bufferToText((0, zlib_1.gunzipSync)(buf), encoding) : buf.toString(encoding);
101
+ return buf[0] === 0x1f && buf[1] === 0x8b ? (0, encode_decode_1.decode)((0, zlib_1.gunzipSync)(buf), encoding) : (0, encode_decode_1.decode)(buf, encoding);
101
102
  }
102
103
  /**
103
104
  * Handle fs:stat
@@ -27,62 +27,57 @@ exports.readFileSync = exports.readFile = void 0;
27
27
  // cSpell:ignore curr
28
28
  // cSpell:words zlib iconv
29
29
  const fs = __importStar(require("fs"));
30
+ const Stream = __importStar(require("stream"));
30
31
  const url_1 = require("url");
32
+ const util_1 = require("util");
31
33
  const zlib = __importStar(require("zlib"));
34
+ const encode_decode_1 = require("../../common/encode-decode");
35
+ const transformers_1 = require("../../common/transformers");
32
36
  const fetch_1 = require("./fetch");
33
37
  const FetchError_1 = require("./FetchError");
34
- const util_1 = require("./util");
38
+ const util_2 = require("./util");
35
39
  const defaultEncoding = 'utf8';
40
+ const pipeline = (0, util_1.promisify)(Stream.pipeline);
36
41
  async function readFile(filename, encoding) {
37
- const url = (0, util_1.toURL)(filename);
38
- if (!(0, util_1.isSupportedURL)(url)) {
42
+ const url = (0, util_2.toURL)(filename);
43
+ if (!(0, util_2.isSupportedURL)(url)) {
39
44
  throw new Error('Unsupported network protocol');
40
45
  }
41
- return (0, util_1.isFileURL)(url) ? _readFile(url, encoding) : _fetchURL(url, encoding);
46
+ return (0, util_2.isFileURL)(url) ? _readFile(url, encoding) : _fetchURL(url, encoding);
42
47
  }
43
48
  exports.readFile = readFile;
44
49
  function _readFile(url, encoding) {
45
50
  // Convert it to a string because Yarn2 cannot handle URLs.
46
51
  const filename = (0, url_1.fileURLToPath)(url);
47
- return _read(() => fs.createReadStream(filename), (0, util_1.isZipped)(filename), encoding);
52
+ return _read(() => fs.createReadStream(filename), (0, util_2.isZipped)(filename), encoding);
48
53
  }
49
54
  async function _fetchURL(url, encoding) {
50
55
  const response = await (0, fetch_1.fetch)(url);
51
56
  if (!response.ok) {
52
57
  throw FetchError_1.FetchUrlError.create(url, response.status);
53
58
  }
54
- return _read(() => response.body, (0, util_1.isZipped)(url), encoding);
59
+ return _read(() => response.body, (0, util_2.isZipped)(url), encoding);
55
60
  }
56
- function _read(getStream, isZipped, encoding = defaultEncoding) {
57
- return new Promise((resolve, reject) => {
58
- const data = [];
59
- const stream = prepareFileStream(getStream, isZipped, encoding, reject);
60
- let resolved = false;
61
- function complete() {
62
- resolve(data.join(''));
63
- resolved = resolved || (resolve(data.join('')), true);
64
- }
65
- stream.on('error', reject);
66
- stream.on('data', (d) => data.push(d));
67
- stream.on('close', complete);
68
- stream.on('end', complete);
69
- });
70
- }
71
- function prepareFileStream(getStream, isZipped, encoding, fnError) {
72
- const pipes = [];
73
- if (isZipped) {
74
- pipes.push(zlib.createGunzip());
75
- }
76
- const fileStream = getStream();
77
- fileStream.on('error', fnError);
78
- const stream = pipes.reduce((s, p) => s.pipe(p).on('error', fnError), fileStream);
79
- stream.setEncoding(encoding);
80
- return stream;
61
+ async function _read(getStream, isZipped, encoding) {
62
+ const stream = getStream();
63
+ const decoder = (0, transformers_1.createDecoderTransformer)(encoding);
64
+ const collector = createCollector(encoding || defaultEncoding);
65
+ return isZipped ? pipeline(stream, zlib.createGunzip(), decoder, collector) : pipeline(stream, decoder, collector);
81
66
  }
82
- function readFileSync(filename, encoding = defaultEncoding) {
67
+ function readFileSync(filename, encoding) {
83
68
  const rawData = fs.readFileSync(filename);
84
- const data = (0, util_1.isZipped)(filename) ? zlib.gunzipSync(rawData) : rawData;
85
- return data.toString(encoding);
69
+ const data = (0, util_2.isZipped)(filename) ? zlib.gunzipSync(rawData) : rawData;
70
+ return !encoding || encoding.startsWith('utf') ? (0, encode_decode_1.decode)(data) : data.toString(encoding);
86
71
  }
87
72
  exports.readFileSync = readFileSync;
73
+ function createCollector(encoding) {
74
+ async function collect(iterable) {
75
+ const buf = [];
76
+ for await (const sb of iterable) {
77
+ buf.push(typeof sb === 'string' ? sb : sb.toString(encoding));
78
+ }
79
+ return buf.join('');
80
+ }
81
+ return collect;
82
+ }
88
83
  //# sourceMappingURL=fileReader.js.map
@@ -1,5 +1,4 @@
1
- /// <reference types="node" />
2
- export declare function writeToFile(filename: string, data: string): NodeJS.WritableStream;
3
- export declare function writeToFileIterable(filename: string, data: Iterable<string>): NodeJS.WritableStream;
4
- export declare function writeToFileIterableP(filename: string, data: Iterable<string>): Promise<void>;
1
+ import type { BufferEncoding, BufferEncodingExt } from '../../common/BufferEncoding';
2
+ export declare function writeToFile(filename: string, data: string | Iterable<string> | AsyncIterable<string>, encoding?: BufferEncoding): Promise<void>;
3
+ export declare function writeToFileIterable(filename: string, data: Iterable<string> | AsyncIterable<string>, encoding?: BufferEncodingExt): Promise<void>;
5
4
  //# sourceMappingURL=fileWriter.d.ts.map
@@ -23,27 +23,21 @@ var __importStar = (this && this.__importStar) || function (mod) {
23
23
  return result;
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.writeToFileIterableP = exports.writeToFileIterable = exports.writeToFile = void 0;
26
+ exports.writeToFileIterable = exports.writeToFile = void 0;
27
27
  const fs = __importStar(require("fs"));
28
- const stream = __importStar(require("stream"));
28
+ const Stream = __importStar(require("stream"));
29
+ const util_1 = require("util");
29
30
  const zlib = __importStar(require("zlib"));
30
- function writeToFile(filename, data) {
31
- return writeToFileIterable(filename, [data]);
31
+ const transformers_1 = require("../../common/transformers");
32
+ const pipeline = (0, util_1.promisify)(Stream.pipeline);
33
+ function writeToFile(filename, data, encoding) {
34
+ return writeToFileIterable(filename, typeof data === 'string' ? [data] : data, encoding);
32
35
  }
33
36
  exports.writeToFile = writeToFile;
34
- function writeToFileIterable(filename, data) {
35
- const sourceStream = stream.Readable.from(data);
36
- const writeStream = fs.createWriteStream(filename);
37
- const zip = filename.match(/\.gz$/) ? zlib.createGzip() : new stream.PassThrough();
38
- return sourceStream.pipe(zip).pipe(writeStream);
37
+ function writeToFileIterable(filename, data, encoding) {
38
+ const stream = Stream.Readable.from((0, transformers_1.encoderTransformer)(data, encoding));
39
+ const zip = filename.match(/\.gz$/) ? zlib.createGzip() : new Stream.PassThrough();
40
+ return pipeline(stream, zip, fs.createWriteStream(filename));
39
41
  }
40
42
  exports.writeToFileIterable = writeToFileIterable;
41
- function writeToFileIterableP(filename, data) {
42
- const stream = writeToFileIterable(filename, data);
43
- return new Promise((resolve, reject) => {
44
- stream.on('finish', () => resolve());
45
- stream.on('error', (e) => reject(e));
46
- });
47
- }
48
- exports.writeToFileIterableP = writeToFileIterableP;
49
43
  //# sourceMappingURL=fileWriter.js.map
@@ -1,4 +1,4 @@
1
1
  export { readFile, readFileSync } from './fileReader';
2
- export { writeToFile, writeToFileIterable, writeToFileIterableP } from './fileWriter';
2
+ export { writeToFile, writeToFileIterable } from './fileWriter';
3
3
  export { getStat, getStatSync } from './stat';
4
4
  //# sourceMappingURL=index.d.ts.map
@@ -1,13 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getStatSync = exports.getStat = exports.writeToFileIterableP = exports.writeToFileIterable = exports.writeToFile = exports.readFileSync = exports.readFile = void 0;
3
+ exports.getStatSync = exports.getStat = exports.writeToFileIterable = exports.writeToFile = exports.readFileSync = exports.readFile = void 0;
4
4
  var fileReader_1 = require("./fileReader");
5
5
  Object.defineProperty(exports, "readFile", { enumerable: true, get: function () { return fileReader_1.readFile; } });
6
6
  Object.defineProperty(exports, "readFileSync", { enumerable: true, get: function () { return fileReader_1.readFileSync; } });
7
7
  var fileWriter_1 = require("./fileWriter");
8
8
  Object.defineProperty(exports, "writeToFile", { enumerable: true, get: function () { return fileWriter_1.writeToFile; } });
9
9
  Object.defineProperty(exports, "writeToFileIterable", { enumerable: true, get: function () { return fileWriter_1.writeToFileIterable; } });
10
- Object.defineProperty(exports, "writeToFileIterableP", { enumerable: true, get: function () { return fileWriter_1.writeToFileIterableP; } });
11
10
  var stat_1 = require("./stat");
12
11
  Object.defineProperty(exports, "getStat", { enumerable: true, get: function () { return stat_1.getStat; } });
13
12
  Object.defineProperty(exports, "getStatSync", { enumerable: true, get: function () { return stat_1.getStatSync; } });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cspell-io",
3
- "version": "6.26.2",
3
+ "version": "6.27.0",
4
4
  "description": "A library of useful I/O functions used across various cspell tools.",
5
5
  "main": "dist/index.js",
6
6
  "typings": "dist/index.d.ts",
@@ -38,14 +38,14 @@
38
38
  "node": ">=14"
39
39
  },
40
40
  "devDependencies": {
41
- "@types/node": "^18.13.0",
41
+ "@types/node": "^18.14.1",
42
42
  "@types/node-fetch": "^2.6.2",
43
43
  "jest": "^29.4.3",
44
44
  "lorem-ipsum": "^2.0.8"
45
45
  },
46
46
  "dependencies": {
47
- "@cspell/cspell-service-bus": "6.26.2",
47
+ "@cspell/cspell-service-bus": "6.27.0",
48
48
  "node-fetch": "^2.6.9"
49
49
  },
50
- "gitHead": "9c983b5f96c3d7b089e49178ab0b437b5eada256"
50
+ "gitHead": "b0e31c7ba91ba467d5fd9c66f238bb5d899f4833"
51
51
  }