@pi-r/chrome 0.6.3 → 0.6.4

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.
package/index.js CHANGED
@@ -1302,6 +1302,7 @@ class ChromeDocument extends Document {
1302
1302
  forDb(item) {
1303
1303
  switch (item.source) {
1304
1304
  case 'cloud':
1305
+ case 'json':
1305
1306
  case 'uri':
1306
1307
  case 'local':
1307
1308
  case 'export':
@@ -2293,43 +2294,46 @@ class ChromeDocument extends Document {
2293
2294
  const detectWidth = domElement.getAttribute('width') === 'detect';
2294
2295
  const detectHeight = domElement.getAttribute('height') === 'detect';
2295
2296
  if (detectWidth || detectHeight) {
2296
- let width = 0, height = 0, buffer = null;
2297
+ let width = 0, height = 0, buffer = null, packageName;
2298
+ const addLog = (dimension) => this.addLog(types_1.STATUS_TYPE.WARN, 'Could not detect image ' + dimension, packageName || this.moduleName, item.localUri ? path.basename(item.localUri) : item.filename);
2297
2299
  if (isImage(item.mimeType) && (buffer = host.getBuffer(item))) {
2298
- let dimensions;
2299
2300
  try {
2300
- dimensions = require('probe-image-size').sync(buffer);
2301
+ ({ width, height } = require(packageName = 'image-size').imageSize(buffer));
2301
2302
  }
2302
2303
  catch {
2303
- }
2304
- if (dimensions) {
2305
- ({ width, height } = dimensions);
2306
- }
2307
- else {
2308
2304
  try {
2309
- ({ width, height } = (await require('jimp').read(buffer)).bitmap);
2305
+ const dimension = require(packageName = 'probe-image-size').sync(buffer);
2306
+ if (!dimension) {
2307
+ throw new Error("MIME not found" /* ERR_MESSAGE.NOTFOUND_MIME */);
2308
+ }
2309
+ ({ width, height } = dimension);
2310
2310
  }
2311
2311
  catch {
2312
- const filename = item.localUri || item.filename;
2313
- if (filename) {
2314
- this.addLog(types_1.STATUS_TYPE.WARN, `Could not detect image size (${path.basename(filename)})`);
2312
+ try {
2313
+ ({ width, height } = (await require('jimp').read(buffer)).bitmap);
2314
+ }
2315
+ catch {
2315
2316
  }
2317
+ packageName = '';
2316
2318
  }
2317
2319
  }
2318
2320
  }
2319
2321
  if (detectWidth) {
2320
- if (width > 0) {
2322
+ if (width) {
2321
2323
  domElement.setAttribute('width', width.toString());
2322
2324
  }
2323
2325
  else {
2324
2326
  domElement.removeAttribute('width');
2327
+ addLog('width');
2325
2328
  }
2326
2329
  }
2327
2330
  if (detectHeight) {
2328
- if (height > 0) {
2331
+ if (height) {
2329
2332
  domElement.setAttribute('height', height.toString());
2330
2333
  }
2331
2334
  else {
2332
2335
  domElement.removeAttribute('height');
2336
+ addLog('height');
2333
2337
  }
2334
2338
  }
2335
2339
  }
@@ -2424,6 +2428,7 @@ class ChromeDocument extends Document {
2424
2428
  }
2425
2429
  break;
2426
2430
  }
2431
+ case 'json':
2427
2432
  case 'uri':
2428
2433
  case 'local':
2429
2434
  case 'export':
@@ -2488,11 +2493,20 @@ class ChromeDocument extends Document {
2488
2493
  return [];
2489
2494
  });
2490
2495
  break;
2496
+ case 'json':
2497
+ if ((result[0] = checkObject(current, current.items)) === null) {
2498
+ this.abort(type);
2499
+ errorRemove(current, errorDataSource("Invalid parameters" /* ERR_MESSAGE.PARAMETERS */ + ' (items)', type));
2500
+ return;
2501
+ }
2502
+ if (this.settingsOf(type, 'coerce') === true) {
2503
+ (0, types_1.coerceObject)(result[0]);
2504
+ }
2505
+ break;
2491
2506
  case 'uri':
2492
2507
  case 'local': {
2493
2508
  const { ignoreCache, encoding = 'utf-8', options } = current;
2494
2509
  let content, target, data, cacheKey;
2495
- const addDownload = () => typeof content === 'string' && host.addDownload(Buffer.byteLength(content, encoding));
2496
2510
  if (type === 'local') {
2497
2511
  let location;
2498
2512
  ({ pathname: target } = current);
@@ -2508,7 +2522,6 @@ class ChromeDocument extends Document {
2508
2522
  try {
2509
2523
  content = fs.readFileSync(location, encoding);
2510
2524
  cacheKey = location;
2511
- addDownload();
2512
2525
  }
2513
2526
  catch (err) {
2514
2527
  this.abort(type);
@@ -2531,7 +2544,22 @@ class ChromeDocument extends Document {
2531
2544
  return;
2532
2545
  }
2533
2546
  else if (Document.isURL(target, 'file')) {
2534
- if (!ignoreCache && CACHE_DBRESULT.has(target)) {
2547
+ let { format, contentType, body } = current;
2548
+ if (body) {
2549
+ if (!format && !contentType && (0, types_1.isPlainObject)(body)) {
2550
+ format = 'json';
2551
+ }
2552
+ try {
2553
+ content = await host.Request.post(target, body, { format, encoding, contentType });
2554
+ }
2555
+ catch (err) {
2556
+ this.abort(type);
2557
+ this.writeFail(["Unable to download file" /* ERR_MESSAGE.DOWNLOAD_FILE */, target], err, { type: 1024 /* LOG_TYPE.HTTP */, startTime });
2558
+ errorRemove(current);
2559
+ return;
2560
+ }
2561
+ }
2562
+ else if (!ignoreCache && CACHE_DBRESULT.has(target)) {
2535
2563
  data = getCacheItem(CACHE_DBRESULT, target);
2536
2564
  }
2537
2565
  else {
@@ -2540,10 +2568,7 @@ class ChromeDocument extends Document {
2540
2568
  }
2541
2569
  if (!(data = cacheData[target])) {
2542
2570
  try {
2543
- content = await host.fetchBuffer(target, { encoding });
2544
- if (Buffer.isBuffer(content)) {
2545
- content = content.toString('utf-8');
2546
- }
2571
+ content = await host.Request.get(target, { format, encoding });
2547
2572
  cacheKey = target;
2548
2573
  }
2549
2574
  catch (err) {
@@ -2573,7 +2598,6 @@ class ChromeDocument extends Document {
2573
2598
  try {
2574
2599
  content = fs.readFileSync(pathname, encoding);
2575
2600
  cacheKey = pathname;
2576
- addDownload();
2577
2601
  }
2578
2602
  catch (err) {
2579
2603
  this.abort(type);
@@ -2584,6 +2608,9 @@ class ChromeDocument extends Document {
2584
2608
  }
2585
2609
  }
2586
2610
  }
2611
+ if (Buffer.isBuffer(content)) {
2612
+ content = content.toString('utf-8');
2613
+ }
2587
2614
  if ((0, types_1.isString)(content)) {
2588
2615
  const { format = path.extname(target).substring(1) } = current;
2589
2616
  try {
@@ -2595,15 +2622,19 @@ class ChromeDocument extends Document {
2595
2622
  errorRemove(current);
2596
2623
  return;
2597
2624
  }
2598
- if (data && cacheKey) {
2599
- setCacheData.call(this, current, data, cacheKey, cacheData);
2600
- }
2625
+ host.addDownload(Buffer.byteLength(content, encoding));
2626
+ }
2627
+ else if (content) {
2628
+ data = content;
2601
2629
  }
2602
2630
  if (!data) {
2603
2631
  this.abort(type);
2604
2632
  errorRemove(current, errorDataSource(content !== null ? 'Invalid response' : 'Empty response', target));
2605
2633
  return;
2606
2634
  }
2635
+ if (cacheKey) {
2636
+ setCacheData.call(this, current, data, cacheKey, cacheData);
2637
+ }
2607
2638
  if ((result[0] = checkObject(current, data)) === null) {
2608
2639
  this.abort(type);
2609
2640
  errorRemove(current, errorDataSource('Invalid ' + (current.query ? 'query' : 'URI'), target));
@@ -2974,6 +3005,13 @@ class ChromeDocument extends Document {
2974
3005
  if (preRender) {
2975
3006
  await checkValue(preRender);
2976
3007
  }
3008
+ const { leadingText, trailingText } = item;
3009
+ if (leadingText) {
3010
+ value = leadingText + value;
3011
+ }
3012
+ if (trailingText) {
3013
+ value += trailingText;
3014
+ }
2977
3015
  domElement.innerXml = domBase.initOpts.normalize ? dom_1.DomWriter.normalize(value, { newline: domBase.newline, escapeEntities: domBase.initOpts.escapeEntities }) : value;
2978
3016
  }
2979
3017
  else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pi-r/chrome",
3
- "version": "0.6.3",
3
+ "version": "0.6.4",
4
4
  "description": "Chrome document constructor for E-mc.",
5
5
  "main": "index.js",
6
6
  "publishConfig": {
@@ -20,9 +20,9 @@
20
20
  "license": "MIT",
21
21
  "homepage": "https://github.com/anpham6/pi-r#readme",
22
22
  "dependencies": {
23
- "@e-mc/cloud": "^0.8.3",
24
- "@e-mc/core": "^0.8.3",
25
- "@e-mc/document": "^0.8.3",
26
- "@e-mc/types": "^0.8.3"
23
+ "@e-mc/cloud": "^0.8.4",
24
+ "@e-mc/core": "^0.8.4",
25
+ "@e-mc/document": "^0.8.4",
26
+ "@e-mc/types": "^0.8.4"
27
27
  }
28
28
  }
package/types/index.d.ts CHANGED
@@ -26,6 +26,7 @@ interface DocumentDirectory extends IDocumentDirectory {
26
26
  }
27
27
 
28
28
  interface DocumentComponentExtended<T> extends DbSourceDataType<T> {
29
+ json?: T;
29
30
  uri?: T;
30
31
  local?: T;
31
32
  export?: T;
@@ -3,6 +3,7 @@ import type { AttributeAction, ConditionProperty, CssConditionData, ElementActio
3
3
  import type { UploadAction } from '@e-mc/types/lib/cloud';
4
4
  import type { CascadeAction, DbSource } from '@e-mc/types/lib/db';
5
5
  import type { RequestData as IRequestData } from '@e-mc/types/lib/node';
6
+ import type { BufferFormat } from '@e-mc/types/lib/request';
6
7
 
7
8
  interface ImportFrom {
8
9
  placeholder: string;
@@ -13,7 +14,7 @@ interface ImportFrom {
13
14
  }
14
15
 
15
16
  interface DataObject extends CascadeAction {
16
- format?: string;
17
+ format?: BufferFormat;
17
18
  options?: PlainObject;
18
19
  }
19
20
 
@@ -86,7 +87,7 @@ export interface UnusedAtRule {
86
87
  }
87
88
 
88
89
  export interface DataSource<T = string> extends IDataSource<T>, ElementAction {
89
- source: "cloud" | "uri" | "local" | "export" | DbSource;
90
+ source: "cloud" | "json" | "uri" | "local" | "export" | DbSource;
90
91
  element?: XmlTagNode;
91
92
  type?: "text" | "attribute" | "display";
92
93
  dynamic?: boolean;
@@ -96,8 +97,16 @@ export interface DataSource<T = string> extends IDataSource<T>, ElementAction {
96
97
  ignoreEmpty?: boolean;
97
98
  }
98
99
 
100
+ export interface TextDataSource extends DataSource {
101
+ type: "text";
102
+ leadingText?: string;
103
+ trailingText?: string;
104
+ }
105
+
99
106
  export interface UriDataSource extends DataSource, DataObject {
100
107
  source: "uri";
108
+ body?: unknown;
109
+ contentType?: string;
101
110
  }
102
111
 
103
112
  export interface LocalDataSource extends DataSource, DataObject {
@@ -105,6 +114,11 @@ export interface LocalDataSource extends DataSource, DataObject {
105
114
  pathname?: string;
106
115
  }
107
116
 
117
+ export interface JSONDataSource extends DataSource, CascadeAction {
118
+ source: "json";
119
+ items?: unknown;
120
+ }
121
+
108
122
  export interface ExportDataSource extends DataSource, CascadeAction {
109
123
  source: "export";
110
124
  execute?: FunctionType;