@lwrjs/fs-asset-provider 0.9.0-alpha.3 → 0.9.0-alpha.31

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.
@@ -29,22 +29,30 @@ __export(exports, {
29
29
  var import_fs = __toModule(require("fs"));
30
30
  var import_path = __toModule(require("path"));
31
31
  var import_shared_utils = __toModule(require("@lwrjs/shared-utils"));
32
- var import_shared_utils2 = __toModule(require("@lwrjs/shared-utils"));
33
32
  function hash(filePath) {
34
33
  const result = import_fs.default.statSync(filePath);
35
34
  return (result.mtimeMs * 1e4).toString();
36
35
  }
37
36
  var FsAssetProvider = class {
38
- constructor(pluginConfig, providerConfig) {
37
+ constructor(_pluginConfig, providerConfig) {
39
38
  this.name = "fs-asset-provider";
40
39
  this.cachedAssets = new Map();
41
40
  const {
42
41
  appEmitter,
43
- runtimeEnvironment: {watchFiles}
42
+ runtimeEnvironment: {watchFiles},
43
+ watcherFactory
44
44
  } = providerConfig;
45
+ const {assets, rootDir, layoutsDir, contentDir, basePath} = providerConfig.config;
46
+ this.basePath = basePath;
47
+ this.resourcePaths = {assets, rootDir, layoutsDir, contentDir};
48
+ this.assetPathMap = assets.reduce((map, asset) => {
49
+ const urlPath = `${basePath}${asset.urlPath}`;
50
+ map.set(urlPath, asset.dir || asset.file);
51
+ return map;
52
+ }, new Map());
45
53
  this.emitter = appEmitter;
46
- if (watchFiles) {
47
- this.watcher = (0, import_shared_utils2.setupWatcher)(this.onModuleChange.bind(this));
54
+ if (watchFiles && watcherFactory) {
55
+ this.watcher = watcherFactory.setupWatcher(this.onModuleChange.bind(this));
48
56
  }
49
57
  }
50
58
  async onModuleChange(fileChanged) {
@@ -53,34 +61,40 @@ var FsAssetProvider = class {
53
61
  throw new Error("We are observing a file we have not yet processed, this shouldn't happen...");
54
62
  }
55
63
  this.cachedAssets.delete(fileChanged);
56
- cachedAsset = await this.getAsset(fileChanged);
64
+ cachedAsset = await this.getAssetFromFSPath(fileChanged);
57
65
  this.emitter.notifyAssetSourceChanged(cachedAsset);
58
66
  }
59
67
  async initialize() {
60
68
  return;
61
69
  }
62
- async getAsset(fullAssetPath) {
63
- fullAssetPath = decodeURI(fullAssetPath);
70
+ async getAsset(assetId) {
71
+ const fullAssetPath = (0, import_shared_utils.normalizeAssetSpecifier)(assetId, this.assetPathMap, this.resourcePaths, this.basePath);
72
+ return this.getAssetFromFSPath(fullAssetPath);
73
+ }
74
+ async getAssetFromFSPath(assetPath) {
75
+ const fullAssetPath = decodeURI(assetPath);
64
76
  if (!this.cachedAssets.has(fullAssetPath)) {
65
- const [assetPath] = fullAssetPath.split("?");
66
- const ext = (0, import_path.extname)(`x.${assetPath}`).toLowerCase().substr(1);
67
- if (ext && import_fs.default.existsSync(assetPath)) {
68
- const ownHash = hash(assetPath);
69
- const mimeType = (0, import_shared_utils.mimeLookup)(assetPath);
77
+ const [assetPath2] = fullAssetPath.split("?");
78
+ const ext = (0, import_path.extname)(`x.${assetPath2}`).toLowerCase().substring(1);
79
+ if (ext && import_fs.default.existsSync(assetPath2)) {
80
+ const ownHash = hash(assetPath2);
81
+ const mimeType = (0, import_shared_utils.mimeLookup)(assetPath2);
70
82
  const assetDef = {
71
- entry: assetPath,
83
+ entry: assetPath2,
72
84
  ext,
73
85
  mime: mimeType,
74
86
  ownHash,
75
87
  content: (passedEncoding) => {
76
88
  const encoding = passedEncoding || (mimeType && mimeType.startsWith("text/") ? "utf-8" : null);
77
- return import_fs.default.readFileSync(assetPath, {encoding});
89
+ return import_fs.default.readFileSync(assetPath2, {encoding});
78
90
  }
79
91
  };
80
92
  this.cachedAssets.set(fullAssetPath, assetDef);
81
93
  if (this.watcher) {
82
- this.watcher.add(assetPath);
94
+ this.watcher.add(assetPath2);
83
95
  }
96
+ } else {
97
+ return void 0;
84
98
  }
85
99
  }
86
100
  return this.cachedAssets.get(fullAssetPath);
@@ -1,12 +1,22 @@
1
- import { ProviderContext, AssetProvider, AssetSource, Watcher, LwrAppEmitter } from '@lwrjs/types';
1
+ import type { ProviderContext, AssetProvider, AssetSource, Watcher, LwrAppEmitter, AssetIdentifier, ResourcePaths } from '@lwrjs/types';
2
2
  export default class FsAssetProvider implements AssetProvider {
3
3
  name: string;
4
4
  emitter: LwrAppEmitter;
5
5
  watcher?: Watcher;
6
6
  cachedAssets: Map<string, AssetSource>;
7
- constructor(pluginConfig: unknown, providerConfig: ProviderContext);
7
+ resourcePaths: ResourcePaths;
8
+ assetPathMap: Map<string, string>;
9
+ basePath: string;
10
+ constructor(_pluginConfig: unknown, providerConfig: ProviderContext);
8
11
  onModuleChange(fileChanged: string): Promise<void>;
9
12
  initialize(): Promise<void>;
10
- getAsset(fullAssetPath: string): Promise<AssetSource | undefined>;
13
+ /**
14
+ * Public API to get an AssetSource object from an asset identifier
15
+ */
16
+ getAsset(assetId: AssetIdentifier): Promise<AssetSource | undefined>;
17
+ /**
18
+ * Get and AssetSource object from a resolved filesystem file path.
19
+ */
20
+ private getAssetFromFSPath;
11
21
  }
12
22
  //# sourceMappingURL=index.d.ts.map
package/build/es/index.js CHANGED
@@ -1,19 +1,26 @@
1
1
  import fs from 'fs';
2
2
  import { extname } from 'path';
3
- import { mimeLookup } from '@lwrjs/shared-utils';
4
- import { setupWatcher } from '@lwrjs/shared-utils';
3
+ import { mimeLookup, normalizeAssetSpecifier } from '@lwrjs/shared-utils';
5
4
  function hash(filePath) {
6
5
  const result = fs.statSync(filePath);
7
6
  return (result.mtimeMs * 10000).toString();
8
7
  }
9
8
  export default class FsAssetProvider {
10
- constructor(pluginConfig, providerConfig) {
9
+ constructor(_pluginConfig, providerConfig) {
11
10
  this.name = 'fs-asset-provider';
12
11
  this.cachedAssets = new Map();
13
- const { appEmitter, runtimeEnvironment: { watchFiles }, } = providerConfig;
12
+ const { appEmitter, runtimeEnvironment: { watchFiles }, watcherFactory, } = providerConfig;
13
+ const { assets, rootDir, layoutsDir, contentDir, basePath } = providerConfig.config;
14
+ this.basePath = basePath;
15
+ this.resourcePaths = { assets, rootDir, layoutsDir, contentDir };
16
+ this.assetPathMap = assets.reduce((map, asset) => {
17
+ const urlPath = `${basePath}${asset.urlPath}`;
18
+ map.set(urlPath, asset.dir || asset.file);
19
+ return map;
20
+ }, new Map());
14
21
  this.emitter = appEmitter;
15
- if (watchFiles) {
16
- this.watcher = setupWatcher(this.onModuleChange.bind(this));
22
+ if (watchFiles && watcherFactory) {
23
+ this.watcher = watcherFactory.setupWatcher(this.onModuleChange.bind(this));
17
24
  }
18
25
  }
19
26
  async onModuleChange(fileChanged) {
@@ -23,19 +30,29 @@ export default class FsAssetProvider {
23
30
  }
24
31
  // Update the cache entry
25
32
  this.cachedAssets.delete(fileChanged);
26
- cachedAsset = await this.getAsset(fileChanged);
33
+ cachedAsset = await this.getAssetFromFSPath(fileChanged);
27
34
  this.emitter.notifyAssetSourceChanged(cachedAsset);
28
35
  }
29
36
  // -- Public API --------------------------------------------------------------------
30
37
  async initialize() {
31
38
  return;
32
39
  }
33
- async getAsset(fullAssetPath) {
34
- fullAssetPath = decodeURI(fullAssetPath); // decodeURIComponent decodes too many things (eg: /)
40
+ /**
41
+ * Public API to get an AssetSource object from an asset identifier
42
+ */
43
+ async getAsset(assetId) {
44
+ const fullAssetPath = normalizeAssetSpecifier(assetId, this.assetPathMap, this.resourcePaths, this.basePath);
45
+ return this.getAssetFromFSPath(fullAssetPath);
46
+ }
47
+ /**
48
+ * Get and AssetSource object from a resolved filesystem file path.
49
+ */
50
+ async getAssetFromFSPath(assetPath) {
51
+ const fullAssetPath = decodeURI(assetPath); // decodeURIComponent decodes too many things (eg: /)
35
52
  if (!this.cachedAssets.has(fullAssetPath)) {
36
53
  const [assetPath] = fullAssetPath.split('?'); // remove query params
37
54
  // Clever normalization to avoid corner cases
38
- const ext = extname(`x.${assetPath}`).toLowerCase().substr(1);
55
+ const ext = extname(`x.${assetPath}`).toLowerCase().substring(1);
39
56
  if (ext && fs.existsSync(assetPath)) {
40
57
  const ownHash = hash(assetPath);
41
58
  const mimeType = mimeLookup(assetPath);
@@ -55,6 +72,10 @@ export default class FsAssetProvider {
55
72
  this.watcher.add(assetPath);
56
73
  }
57
74
  }
75
+ else {
76
+ // returned undefined move on to next provider
77
+ return undefined;
78
+ }
58
79
  }
59
80
  return this.cachedAssets.get(fullAssetPath);
60
81
  }
package/package.json CHANGED
@@ -4,15 +4,15 @@
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
7
- "version": "0.9.0-alpha.3",
7
+ "version": "0.9.0-alpha.31",
8
8
  "homepage": "https://developer.salesforce.com/docs/platform/lwr/overview",
9
9
  "repository": {
10
10
  "type": "git",
11
- "url": "https://github.com/salesforce/lwr.git",
11
+ "url": "https://github.com/salesforce-experience-platform-emu/lwr.git",
12
12
  "directory": "packages/@lwrjs/fs-asset-provider"
13
13
  },
14
14
  "bugs": {
15
- "url": "https://github.com/salesforce/lwr/issues"
15
+ "url": "https://github.com/salesforce-experience-platform-emu/lwr/issues"
16
16
  },
17
17
  "type": "module",
18
18
  "types": "build/es/index.d.ts",
@@ -30,13 +30,13 @@
30
30
  "build/**/*.d.ts"
31
31
  ],
32
32
  "dependencies": {
33
- "@lwrjs/shared-utils": "0.9.0-alpha.3"
33
+ "@lwrjs/shared-utils": "0.9.0-alpha.31"
34
34
  },
35
35
  "devDependencies": {
36
- "@lwrjs/types": "0.9.0-alpha.3"
36
+ "@lwrjs/types": "0.9.0-alpha.31"
37
37
  },
38
38
  "engines": {
39
- "node": ">=14.15.4 <19"
39
+ "node": ">=16.0.0 <20"
40
40
  },
41
- "gitHead": "3e96a852db94afbbfbc6558098303a85acd6604d"
41
+ "gitHead": "be668a80891cd00d1fb689aa99037b0f05cc4061"
42
42
  }