jsfilter.js 565 B

12345678910111213141516171819202122232425262728293031
  1. import * as util from './util.js'
  2. /**
  3. * @param {string} code
  4. */
  5. export function parseSync(code) {
  6. // TODO: parse js ast
  7. let match
  8. code = code.replace(/(\b)location(\b)/g, (s, $1, $2) => {
  9. match = true
  10. return $1 + '__location' + $2
  11. })
  12. if (match) {
  13. return code
  14. }
  15. }
  16. /**
  17. * @param {Uint8Array} buf
  18. */
  19. export async function parseBin(buf, charset) {
  20. const str = util.bytesToStr(buf, charset)
  21. const ret = parseSync(str)
  22. if (ret) {
  23. return util.strToBytes(ret)
  24. }
  25. if (!util.isUtf8(charset)) {
  26. return util.strToBytes(str)
  27. }
  28. }