JavaScriptObfuscatorInternal.spec.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { IObfuscationResult } from '../../src/interfaces/IObfuscationResult';
  2. import { JavaScriptObfuscatorInternal } from '../../src/JavaScriptObfuscatorInternal';
  3. import { NO_CUSTOM_NODES_PRESET } from '../../src/preset-options/NoCustomNodesPreset';
  4. import { Options } from '../../src/options/Options';
  5. const assert: Chai.AssertStatic = require('chai').assert;
  6. describe('JavaScriptObfuscatorInternal', () => {
  7. describe(`setSourceMapUrl (url: string)`, () => {
  8. let javaScriptObfuscator: JavaScriptObfuscatorInternal,
  9. obfuscationResult: IObfuscationResult,
  10. sourceMapUrl: string = 'test.js.map';
  11. it('should link obfuscated code with source map', () => {
  12. javaScriptObfuscator = new JavaScriptObfuscatorInternal(
  13. new Options(
  14. Object.assign({}, NO_CUSTOM_NODES_PRESET, {
  15. sourceMap: true,
  16. sourceMapFileName: sourceMapUrl
  17. })
  18. )
  19. );
  20. obfuscationResult = javaScriptObfuscator.obfuscate('var test = 1;');
  21. assert.match(
  22. obfuscationResult.getObfuscatedCode(),
  23. new RegExp(`sourceMappingURL=${sourceMapUrl}`))
  24. ;
  25. assert.isOk(JSON.parse(obfuscationResult.getSourceMap()).mappings);
  26. });
  27. it('should properly add base url to source map import inside obfuscated code if `sourceMapBaseUrl` is set', () => {
  28. let sourceMapBaseUrl: string = 'http://localhost:9000';
  29. javaScriptObfuscator = new JavaScriptObfuscatorInternal(
  30. new Options(
  31. Object.assign({}, NO_CUSTOM_NODES_PRESET, {
  32. sourceMap: true,
  33. sourceMapBaseUrl: sourceMapBaseUrl,
  34. sourceMapFileName: sourceMapUrl
  35. })
  36. )
  37. );
  38. obfuscationResult = javaScriptObfuscator.obfuscate('var test = 1;');
  39. assert.match(
  40. obfuscationResult.getObfuscatedCode(),
  41. new RegExp(`sourceMappingURL=${sourceMapBaseUrl}/${sourceMapUrl}$`))
  42. ;
  43. assert.isOk(JSON.parse(obfuscationResult.getSourceMap()).mappings);
  44. });
  45. });
  46. });