fs/lib/ResourceFacade.js

  1. import posixPath from "node:path/posix";
  2. /**
  3. * A {@link @ui5/fs/Resource Resource} with a different path than it's original
  4. *
  5. * @public
  6. * @class
  7. * @alias @ui5/fs/ResourceFacade
  8. */
  9. class ResourceFacade {
  10. #path;
  11. #name;
  12. #resource;
  13. /**
  14. *
  15. * @public
  16. * @param {object} parameters Parameters
  17. * @param {string} parameters.path Virtual path of the facade resource
  18. * @param {@ui5/fs/Resource} parameters.resource Resource to conceal
  19. */
  20. constructor({path, resource}) {
  21. if (!path) {
  22. throw new Error("Unable to create ResourceFacade: Missing parameter 'path'");
  23. }
  24. if (!resource) {
  25. throw new Error("Unable to create ResourceFacade: Missing parameter 'resource'");
  26. }
  27. path = posixPath.normalize(path);
  28. if (!posixPath.isAbsolute(path)) {
  29. throw new Error(`Unable to create ResourceFacade: Parameter 'path' must be absolute: ${path}`);
  30. }
  31. this.#path = path;
  32. this.#name = posixPath.basename(path);
  33. this.#resource = resource;
  34. }
  35. /**
  36. * Gets the resources path
  37. *
  38. * @public
  39. * @returns {string} (Virtual) path of the resource
  40. */
  41. getPath() {
  42. return this.#path;
  43. }
  44. /**
  45. * Gets the resource name
  46. *
  47. * @public
  48. * @returns {string} Name of the resource
  49. */
  50. getName() {
  51. return this.#name;
  52. }
  53. /**
  54. * Sets the resources path
  55. *
  56. * @public
  57. * @param {string} path (Virtual) path of the resource
  58. */
  59. setPath(path) {
  60. throw new Error(`The path of a ResourceFacade can't be changed`);
  61. }
  62. /**
  63. * Returns a clone of the resource. The clones content is independent from that of the original resource.
  64. * A ResourceFacade becomes a Resource
  65. *
  66. * @public
  67. * @returns {Promise<@ui5/fs/Resource>} Promise resolving with the clone
  68. */
  69. async clone() {
  70. // Cloning resolves the facade
  71. const resourceClone = await this.#resource.clone();
  72. resourceClone.setPath(this.getPath());
  73. return resourceClone;
  74. }
  75. /**
  76. * ======================================================================
  77. * Call through functions to original resource
  78. * ======================================================================
  79. */
  80. /**
  81. * Gets a buffer with the resource content.
  82. *
  83. * @public
  84. * @returns {Promise<Buffer>} Promise resolving with a buffer of the resource content.
  85. */
  86. async getBuffer() {
  87. return this.#resource.getBuffer();
  88. }
  89. /**
  90. * Sets a Buffer as content.
  91. *
  92. * @public
  93. * @param {Buffer} buffer Buffer instance
  94. */
  95. setBuffer(buffer) {
  96. return this.#resource.setBuffer(buffer);
  97. }
  98. /**
  99. * Gets a string with the resource content.
  100. *
  101. * @public
  102. * @returns {Promise<string>} Promise resolving with the resource content.
  103. */
  104. getString() {
  105. return this.#resource.getString();
  106. }
  107. /**
  108. * Sets a String as content
  109. *
  110. * @public
  111. * @param {string} string Resource content
  112. */
  113. setString(string) {
  114. return this.#resource.setString(string);
  115. }
  116. /**
  117. * Gets a readable stream for the resource content.
  118. *
  119. * Repetitive calls of this function are only possible if new content has been set in the meantime (through
  120. * [setStream]{@link @ui5/fs/Resource#setStream}, [setBuffer]{@link @ui5/fs/Resource#setBuffer}
  121. * or [setString]{@link @ui5/fs/Resource#setString}). This
  122. * is to prevent consumers from accessing drained streams.
  123. *
  124. * @public
  125. * @returns {stream.Readable} Readable stream for the resource content.
  126. */
  127. getStream() {
  128. return this.#resource.getStream();
  129. }
  130. /**
  131. * Sets a readable stream as content.
  132. *
  133. * @public
  134. * @param {stream.Readable|@ui5/fs/Resource~createStream} stream Readable stream of the resource content or
  135. callback for dynamic creation of a readable stream
  136. */
  137. setStream(stream) {
  138. return this.#resource.setStream(stream);
  139. }
  140. /**
  141. * Gets the resources stat info.
  142. * Note that a resources stat information is not updated when the resource is being modified.
  143. * Also, depending on the used adapter, some fields might be missing which would be present for a
  144. * [fs.Stats]{@link https://nodejs.org/api/fs.html#fs_class_fs_stats} instance.
  145. *
  146. * @public
  147. * @returns {fs.Stats|object} Instance of [fs.Stats]{@link https://nodejs.org/api/fs.html#fs_class_fs_stats}
  148. * or similar object
  149. */
  150. getStatInfo() {
  151. return this.#resource.getStatInfo();
  152. }
  153. /**
  154. * Size in bytes allocated by the underlying buffer.
  155. *
  156. * @see {TypedArray#byteLength}
  157. * @returns {Promise<number>} size in bytes, <code>0</code> if there is no content yet
  158. */
  159. async getSize() {
  160. return this.#resource.getSize();
  161. }
  162. /**
  163. * Adds a resource collection name that was involved in locating this resource.
  164. *
  165. * @param {string} name Resource collection name
  166. */
  167. pushCollection(name) {
  168. return this.#resource.pushCollection(name);
  169. }
  170. /**
  171. * Tracing: Get tree for printing out trace
  172. *
  173. * @returns {object} Trace tree
  174. */
  175. getPathTree() {
  176. return this.#resource.getPathTree();
  177. }
  178. /**
  179. * Retrieve the project assigned to the resource
  180. * <br/>
  181. * <b>Note for UI5 Tooling extensions (i.e. custom tasks, custom middleware):</b>
  182. * In order to ensure compatibility across UI5 Tooling versions, consider using the
  183. * <code>getProject(resource)</code> method provided by
  184. * [TaskUtil]{@link module:@ui5/project/build/helpers/TaskUtil} and
  185. * [MiddlewareUtil]{@link module:@ui5/server.middleware.MiddlewareUtil}, which will
  186. * return a Specification Version-compatible Project interface.
  187. *
  188. * @public
  189. * @returns {@ui5/project/specifications/Project} Project this resource is associated with
  190. */
  191. getProject() {
  192. return this.#resource.getProject();
  193. }
  194. /**
  195. * Assign a project to the resource
  196. *
  197. * @public
  198. * @param {@ui5/project/specifications/Project} project Project this resource is associated with
  199. */
  200. setProject(project) {
  201. return this.#resource.setProject(project);
  202. }
  203. /**
  204. * Check whether a project has been assigned to the resource
  205. *
  206. * @public
  207. * @returns {boolean} True if the resource is associated with a project
  208. */
  209. hasProject() {
  210. return this.#resource.hasProject();
  211. }
  212. /**
  213. * Check whether the content of this resource has been changed during its life cycle
  214. *
  215. * @public
  216. * @returns {boolean} True if the resource's content has been changed
  217. */
  218. isModified() {
  219. return this.#resource.isModified();
  220. }
  221. /**
  222. * Returns source metadata if any where provided during the creation of this resource.
  223. * Typically set by an adapter to store information for later retrieval.
  224. *
  225. * @returns {object|null}
  226. */
  227. getSourceMetadata() {
  228. return this.#resource.getSourceMetadata();
  229. }
  230. /**
  231. * Returns the resource concealed by this facade
  232. *
  233. * @returns {@ui5/fs/Resource}
  234. */
  235. getConcealedResource() {
  236. return this.#resource;
  237. }
  238. }
  239. export default ResourceFacade;