@e-mc/document 0.13.9 → 0.14.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.
@@ -1,9 +1,9 @@
1
1
  "use strict";
2
- exports.SourceMap = exports.TransformSeries = void 0;
3
- const fs = require("node:fs");
4
- const path = require("node:path");
5
- const types_1 = require("@e-mc/types");
6
- const core_1 = require("@e-mc/core");
2
+
3
+ const fs = require('node:fs');
4
+ const path = require('node:path');
5
+ const { importESM, isErrorCode, isObject, isPlainObject, isString } = require('@e-mc/types');
6
+ const { AbortComponent, Module, Permission } = require('@e-mc/core');
7
7
  const kTransformSeries = Symbol.for('document:transform:series:constructor');
8
8
  function parseMap(data, value) {
9
9
  let [mimeType, encoding] = data.split(';');
@@ -23,7 +23,7 @@ function toJSON(data) {
23
23
  }
24
24
  }
25
25
  function parseModuleArgs(startDir, packageName, version) {
26
- if ((0, types_1.isObject)(startDir)) {
26
+ if (isObject(startDir)) {
27
27
  ({ startDir, packageName, version } = startDir);
28
28
  }
29
29
  if (!startDir && packageName && version && version !== 'latest') {
@@ -49,7 +49,7 @@ class Out {
49
49
  this.logQueued = undefined;
50
50
  }
51
51
  }
52
- class TransformSeries extends core_1.Module {
52
+ class TransformSeries extends Module {
53
53
  type;
54
54
  static [kTransformSeries] = true;
55
55
  baseConfig = {};
@@ -80,7 +80,7 @@ class TransformSeries extends core_1.Module {
80
80
  this.#sourceMap = options.sourceMap instanceof SourceMap ? options.sourceMap : this.createSourceMap(code);
81
81
  }
82
82
  if (options.metadata) {
83
- this.#metadata = (0, types_1.isPlainObject)(options.metadata) ? { ...options.metadata } : options.metadata;
83
+ this.#metadata = isPlainObject(options.metadata) ? { ...options.metadata } : options.metadata;
84
84
  }
85
85
  }
86
86
  reset() {
@@ -93,7 +93,7 @@ class TransformSeries extends core_1.Module {
93
93
  }
94
94
  init(instance, dirname) {
95
95
  if (instance.hasPermission('fs')) {
96
- this.permission = core_1.Permission.clone(instance.permission, true);
96
+ this.permission = Permission.clone(instance.permission, true);
97
97
  }
98
98
  this.abortable = instance.abortable;
99
99
  this._moduleName = instance.moduleName + ':transform:' + this.type;
@@ -105,7 +105,7 @@ class TransformSeries extends core_1.Module {
105
105
  host.retain(this);
106
106
  this.#username = host.username;
107
107
  }
108
- core_1.AbortComponent.attach(instance, this.signal);
108
+ AbortComponent.attach(instance, this.signal);
109
109
  return this;
110
110
  }
111
111
  createSourceMap(code) {
@@ -157,10 +157,10 @@ class TransformSeries extends core_1.Module {
157
157
  const pathname = startDir && this.findModule(startDir, packageName, version || this.version);
158
158
  let result;
159
159
  if (pathname) {
160
- result = await (0, types_1.importESM)(pathname, false, true);
160
+ result = await importESM(pathname, false, true);
161
161
  }
162
162
  else if (packageName) {
163
- result = await (0, types_1.importESM)(packageName);
163
+ result = await importESM(packageName);
164
164
  }
165
165
  if (result) {
166
166
  return result;
@@ -181,7 +181,7 @@ class TransformSeries extends core_1.Module {
181
181
  return pathname;
182
182
  }
183
183
  const data = require(path.join(pathname, 'package.json'));
184
- if ((0, types_1.isString)(data?.version)) {
184
+ if (isString(data?.version)) {
185
185
  if (version === data.version) {
186
186
  return pathname;
187
187
  }
@@ -197,7 +197,7 @@ class TransformSeries extends core_1.Module {
197
197
  }
198
198
  }
199
199
  catch (err) {
200
- if ((0, types_1.isErrorCode)(err, 'EACCES')) {
200
+ if (isErrorCode(err, 'EACCES')) {
201
201
  return '';
202
202
  }
203
203
  }
@@ -207,7 +207,7 @@ class TransformSeries extends core_1.Module {
207
207
  return '';
208
208
  }
209
209
  close(instance) {
210
- core_1.AbortComponent.detach(instance, this.signal);
210
+ AbortComponent.detach(instance, this.signal);
211
211
  this.host?.release(this);
212
212
  }
213
213
  set code(value) {
@@ -272,7 +272,7 @@ class TransformSeries extends core_1.Module {
272
272
  }
273
273
  get packageVersion() {
274
274
  const packageName = this.packageName;
275
- return packageName ? core_1.Module.getPackageVersion(packageName, { startDir: this.#modulesDir }) : '0.0.0';
275
+ return packageName ? Module.getPackageVersion(packageName, { startDir: this.#modulesDir }) : '0.0.0';
276
276
  }
277
277
  set host(value) {
278
278
  this._host = value;
@@ -284,13 +284,12 @@ class TransformSeries extends core_1.Module {
284
284
  return this.#username;
285
285
  }
286
286
  }
287
- exports.TransformSeries = TransformSeries;
288
287
  class SourceMap {
289
288
  static get SOURCE_MAPPING_URL() {
290
289
  return /[\r\n]*(?:(\/\/)|(\/\*))\s*[#@][ ]+sourceMappingURL=(data:[^,]+,)?(\S+)\s*(\*\/)?\r?\n?/g;
291
290
  }
292
291
  static findSourceMap(code, uri) {
293
- if (code ||= uri && core_1.Module.readText(uri)) {
292
+ if (code ||= uri && Module.readText(uri)) {
294
293
  const pattern = this.SOURCE_MAPPING_URL;
295
294
  pattern.lastIndex = 0;
296
295
  let match;
@@ -302,7 +301,7 @@ class SourceMap {
302
301
  }
303
302
  else if (uri) {
304
303
  const url = decodeURIComponent(match[4]);
305
- map = toJSON(core_1.Module.readText(path.isAbsolute(url) ? url : path.join(path.dirname(uri), url)));
304
+ map = toJSON(Module.readText(path.isAbsolute(url) ? url : path.join(path.dirname(uri), url)));
306
305
  }
307
306
  if (map) {
308
307
  return map;
@@ -314,10 +313,10 @@ class SourceMap {
314
313
  const pattern = this.SOURCE_MAPPING_URL;
315
314
  pattern.lastIndex = 0;
316
315
  const match = pattern.exec(value);
317
- return match ? [value.substring(0, match.index) + value.substring(match.index + match[0].length), (match[3] || '') + match[4], match[3] && toJSON(parseMap(match[3], match[4])) || null] : [value];
316
+ return match ? [value.slice(0, match.index) + value.slice(match.index + match[0].length), (match[3] || '') + match[4], match[3] && toJSON(parseMap(match[3], match[4])) || null] : [value];
318
317
  }
319
318
  static isRaw(map) {
320
- return (0, types_1.isObject)(map) && (0, types_1.isString)(map.mappings);
319
+ return isObject(map) && isString(map.mappings);
321
320
  }
322
321
  code;
323
322
  output = new Map();
@@ -379,7 +378,7 @@ class SourceMap {
379
378
  return map;
380
379
  }
381
380
  try {
382
- return JSON.parse((0, types_1.isString)(map) || Buffer.isBuffer(map) ? map.toString('utf8') : map.toString());
381
+ return JSON.parse(isString(map) || Buffer.isBuffer(map) ? map.toString('utf8') : map.toString());
383
382
  }
384
383
  catch {
385
384
  return null;
@@ -397,4 +396,6 @@ class SourceMap {
397
396
  return this.#map;
398
397
  }
399
398
  }
399
+
400
400
  exports.SourceMap = SourceMap;
401
+ exports.TransformSeries = TransformSeries;
package/util.d.ts CHANGED
@@ -15,7 +15,8 @@ declare namespace util {
15
15
  function hasValue(target: string | string[] | undefined, ...values: string[]): boolean;
16
16
  function getModuleName(err: unknown): string | undefined;
17
17
  function removeInternalProperties<T extends object>(value: T, retaining?: string[]): T;
18
- function isObject<T = object>(value: unknown): value is T;
18
+ function isObject<T extends object>(value: unknown): value is T;
19
+ function isPlainObject<T = PlainObject>(value: unknown): value is T;
19
20
  }
20
21
 
21
22
  export = util;
package/util.js CHANGED
@@ -1,33 +1,17 @@
1
1
  "use strict";
2
- exports.isObject = exports.PATTERN_PARENTHESIS = void 0;
3
- exports.loadPlugins = loadPlugins;
4
- exports.replaceAll = replaceAll;
5
- exports.concatString = concatString;
6
- exports.spliceString = spliceString;
7
- exports.spliceMatch = spliceMatch;
8
- exports.splitEnclosing = splitEnclosing;
9
- exports.trimQuote = trimQuote;
10
- exports.wrapQuote = wrapQuote;
11
- exports.appendSuffix = appendSuffix;
12
- exports.getIndent = getIndent;
13
- exports.getNewline = getNewline;
14
- exports.getHashData = getHashData;
15
- exports.hasValue = hasValue;
16
- exports.getModuleName = getModuleName;
17
- exports.removeInternalProperties = removeInternalProperties;
18
- const path = require("node:path");
19
- const types_1 = require("@e-mc/types");
20
- Object.defineProperty(exports, "isObject", { enumerable: true, get: function () { return types_1.isObject; } });
2
+
3
+ const path = require('node:path');
4
+ const { isArray, isError, isObject, isPlainObject, isString, requireESM } = require('@e-mc/types');
21
5
  function loadPlugins(plugins, map) {
22
6
  const result = [];
23
7
  const items = (Array.isArray(plugins) ? plugins : [plugins]).map(value => {
24
8
  let name, options;
25
- if ((0, types_1.isString)(value)) {
9
+ if (isString(value)) {
26
10
  name = value;
27
11
  }
28
12
  else if (Array.isArray(value)) {
29
13
  [name, options] = value;
30
- if (!(0, types_1.isString)(name)) {
14
+ if (!isString(name)) {
31
15
  return null;
32
16
  }
33
17
  }
@@ -42,10 +26,10 @@ function loadPlugins(plugins, map) {
42
26
  });
43
27
  for (const plugin of items) {
44
28
  if (plugin) {
45
- let method = typeof plugin[0] === 'function' ? plugin[0] : require(plugin[0]), options = plugin[1];
46
- if ((0, types_1.isPlainObject)(method)) {
29
+ let method = typeof plugin[0] === 'function' ? plugin[0] : requireESM(plugin[0]), options = plugin[1];
30
+ if (isPlainObject(method)) {
47
31
  const attr = options?.__default__;
48
- if ((0, types_1.isString)(attr)) {
32
+ if (isString(attr)) {
49
33
  method = method[attr];
50
34
  options = { ...options };
51
35
  delete options.__default__;
@@ -61,14 +45,14 @@ function replaceAll(source, valueOf, opening = '{{', closing = '}}') {
61
45
  if (i > 0) {
62
46
  const j = b.indexOf(closing);
63
47
  if (j !== -1) {
64
- b = valueOf(b.substring(0, j)) + b.substring(j + closing.length);
48
+ b = valueOf(b.slice(0, j)) + b.slice(j + closing.length);
65
49
  }
66
50
  }
67
51
  return a + b;
68
52
  }, '');
69
53
  }
70
54
  function concatString(values, newline) {
71
- if ((0, types_1.isArray)(values)) {
55
+ if (isArray(values)) {
72
56
  newline ||= values.find(item => item.indexOf('\r') !== -1) ? '\r\n' : '\n';
73
57
  return values.reduce((a, b) => a + newline + b, '');
74
58
  }
@@ -78,7 +62,7 @@ function spliceString(source, startIndex, endIndex, content, pattern) {
78
62
  if (pattern) {
79
63
  pattern.lastIndex = startIndex + content.length;
80
64
  }
81
- return source.substring(0, startIndex) + content + source.substring(endIndex);
65
+ return source.slice(0, startIndex) + content + source.slice(endIndex);
82
66
  }
83
67
  function spliceMatch(source, match, content, pattern) {
84
68
  const index = match.index;
@@ -129,7 +113,8 @@ function splitEnclosing(value, pattern, options, opening = '(', closing = ')') {
129
113
  let start, startWith;
130
114
  ({ trim, start, startWith, count = 0 } = options);
131
115
  if (startWith !== undefined) {
132
- result.push(value.substring(0, position = startWith));
116
+ position = startWith;
117
+ result.push(value.slice(0, position));
133
118
  }
134
119
  else if (start !== undefined) {
135
120
  position = start;
@@ -140,7 +125,7 @@ function splitEnclosing(value, pattern, options, opening = '(', closing = ')') {
140
125
  }
141
126
  while ((index = nextIndex()) !== -1) {
142
127
  if (index !== position) {
143
- result.push(value.substring(position, index));
128
+ result.push(value.slice(position, index));
144
129
  }
145
130
  found: {
146
131
  for (let i = index + end + 1, open = 1, close = 0; i < length; ++i) {
@@ -157,7 +142,7 @@ function splitEnclosing(value, pattern, options, opening = '(', closing = ')') {
157
142
  index += opening.length;
158
143
  }
159
144
  position = i + 1;
160
- result.push(value.substring(index, position - (trim ? closing.length : 0)));
145
+ result.push(value.slice(index, position - (trim ? closing.length : 0)));
161
146
  if (position === length) {
162
147
  return result;
163
148
  }
@@ -179,7 +164,7 @@ function splitEnclosing(value, pattern, options, opening = '(', closing = ')') {
179
164
  return [value];
180
165
  }
181
166
  if (position < length) {
182
- result.push(value.substring(position));
167
+ result.push(value.slice(position));
183
168
  }
184
169
  return result;
185
170
  }
@@ -194,7 +179,7 @@ function wrapQuote(value, quote = '"') {
194
179
  }
195
180
  function appendSuffix(filename, value, separator = '-') {
196
181
  const ext = path.extname(filename);
197
- return ext && value ? filename.substring(0, filename.length - ext.length) + separator + value + ext : filename;
182
+ return ext && value ? filename.slice(0, filename.length - ext.length) + separator + value + ext : filename;
198
183
  }
199
184
  function getIndent(value, spaces = 4) {
200
185
  const match = /^(\t|[ ]{2,}[^\s])/m.exec(value);
@@ -219,20 +204,20 @@ function hasValue(target, ...values) {
219
204
  if (typeof target === 'string') {
220
205
  return values.includes(target);
221
206
  }
222
- if ((0, types_1.isArray)(target)) {
207
+ if (isArray(target)) {
223
208
  return target.some(item => values.includes(item));
224
209
  }
225
210
  }
226
211
  return false;
227
212
  }
228
213
  function getModuleName(err) {
229
- const match = err instanceof Error && /Cannot find (?:module|package) '([^']+)'/.exec(err.message);
214
+ const match = isError(err) && /Cannot find (?:module|package) '([^']+)'/.exec(err.message);
230
215
  if (match) {
231
216
  return match[1];
232
217
  }
233
218
  }
234
219
  function removeInternalProperties(value, retaining) {
235
- if ((0, types_1.isObject)(value)) {
220
+ if (isObject(value)) {
236
221
  for (const prop in value) {
237
222
  if (Object.hasOwn(value, prop)) {
238
223
  if (retaining) {
@@ -248,4 +233,23 @@ function removeInternalProperties(value, retaining) {
248
233
  }
249
234
  return value;
250
235
  }
251
- exports.PATTERN_PARENTHESIS = '(?:[^()]+(?=\\))|[^()]|\\([^()]*\\(+|\\([^()]*\\)|(?:\\)[^()]+)?\\)*?)+';
236
+ const PATTERN_PARENTHESIS = '(?:[^()]+(?=\\))|[^()]|\\([^()]*\\(+|\\([^()]*\\)|(?:\\)[^()]+)?\\)*?)+';
237
+
238
+ exports.PATTERN_PARENTHESIS = PATTERN_PARENTHESIS;
239
+ exports.appendSuffix = appendSuffix;
240
+ exports.concatString = concatString;
241
+ exports.getHashData = getHashData;
242
+ exports.getIndent = getIndent;
243
+ exports.getModuleName = getModuleName;
244
+ exports.getNewline = getNewline;
245
+ exports.hasValue = hasValue;
246
+ exports.isObject = isObject;
247
+ exports.isPlainObject = isPlainObject;
248
+ exports.loadPlugins = loadPlugins;
249
+ exports.removeInternalProperties = removeInternalProperties;
250
+ exports.replaceAll = replaceAll;
251
+ exports.spliceMatch = spliceMatch;
252
+ exports.spliceString = spliceString;
253
+ exports.splitEnclosing = splitEnclosing;
254
+ exports.trimQuote = trimQuote;
255
+ exports.wrapQuote = wrapQuote;