clientHelper.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. "use strict";
  2. var __defProp = Object.defineProperty;
  3. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  4. var __getOwnPropNames = Object.getOwnPropertyNames;
  5. var __hasOwnProp = Object.prototype.hasOwnProperty;
  6. var __export = (target, all) => {
  7. for (var name in all)
  8. __defProp(target, name, { get: all[name], enumerable: true });
  9. };
  10. var __copyProps = (to, from, except, desc) => {
  11. if (from && typeof from === "object" || typeof from === "function") {
  12. for (let key of __getOwnPropNames(from))
  13. if (!__hasOwnProp.call(to, key) && key !== except)
  14. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  15. }
  16. return to;
  17. };
  18. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  19. var clientHelper_exports = {};
  20. __export(clientHelper_exports, {
  21. addSourceUrlToScript: () => addSourceUrlToScript,
  22. envObjectToArray: () => envObjectToArray,
  23. evaluationScript: () => evaluationScript
  24. });
  25. module.exports = __toCommonJS(clientHelper_exports);
  26. var import_rtti = require("../utils/isomorphic/rtti");
  27. function envObjectToArray(env) {
  28. const result = [];
  29. for (const name in env) {
  30. if (!Object.is(env[name], void 0))
  31. result.push({ name, value: String(env[name]) });
  32. }
  33. return result;
  34. }
  35. async function evaluationScript(platform, fun, arg, addSourceUrl = true) {
  36. if (typeof fun === "function") {
  37. const source = fun.toString();
  38. const argString = Object.is(arg, void 0) ? "undefined" : JSON.stringify(arg);
  39. return `(${source})(${argString})`;
  40. }
  41. if (arg !== void 0)
  42. throw new Error("Cannot evaluate a string with arguments");
  43. if ((0, import_rtti.isString)(fun))
  44. return fun;
  45. if (fun.content !== void 0)
  46. return fun.content;
  47. if (fun.path !== void 0) {
  48. let source = await platform.fs().promises.readFile(fun.path, "utf8");
  49. if (addSourceUrl)
  50. source = addSourceUrlToScript(source, fun.path);
  51. return source;
  52. }
  53. throw new Error("Either path or content property must be present");
  54. }
  55. function addSourceUrlToScript(source, path) {
  56. return `${source}
  57. //# sourceURL=${path.replace(/\n/g, "")}`;
  58. }
  59. // Annotate the CommonJS export names for ESM import in node:
  60. 0 && (module.exports = {
  61. addSourceUrlToScript,
  62. envObjectToArray,
  63. evaluationScript
  64. });