public async Task <string> GetStartupCodeAsync()
            {
                var bundleFile = await FileSystem.Current.GetFileFromPathAsync(_sourcePath);

                _stream = await bundleFile.OpenAsync(PCLStorage.FileAccess.Read);

                var header = new byte[HeaderSize];

                if (await _stream.ReadAsync(header, 0, HeaderSize) < HeaderSize)
                {
                    throw new InvalidOperationException("Reached end of file before end of indexed unbundle header.");
                }

                var numberOfTableEntries = InetHelpers.LittleEndianToHost(BitConverter.ToUInt32(header, 4));
                var startupCodeSize      = InetHelpers.LittleEndianToHost(BitConverter.ToUInt32(header, 8));
                var moduleTableSize      = numberOfTableEntries * 8 /* bytes per entry */;

                _baseOffset  = HeaderSize + (int)moduleTableSize;
                _moduleTable = new byte[moduleTableSize];
                if (await _stream.ReadAsync(_moduleTable, 0, (int)moduleTableSize) < moduleTableSize)
                {
                    throw new InvalidOperationException("Reached end of file before end of indexed unbundle module table.");
                }

                var startupCodeBuffer = new byte[startupCodeSize];

                if (await _stream.ReadAsync(startupCodeBuffer, 0, (int)startupCodeSize) < startupCodeSize)
                {
                    throw new InvalidOperationException("Reached end of file before end of startup code.");
                }

                return(Encoding.UTF8.GetString(startupCodeBuffer));
            }
            public string GetStartupCode()
            {
                _stream = File.OpenRead(_sourcePath);
                var header = new byte[HeaderSize];

                if (_stream.Read(header, 0, HeaderSize) < HeaderSize)
                {
                    throw new InvalidOperationException("Reached end of file before end of indexed unbundle header.");
                }

                var numberOfTableEntries = InetHelpers.LittleEndianToHost(BitConverter.ToUInt32(header, 4));
                var startupCodeSize      = InetHelpers.LittleEndianToHost(BitConverter.ToUInt32(header, 8));
                var moduleTableSize      = numberOfTableEntries * 8 /* bytes per entry */;

                _baseOffset  = HeaderSize + (int)moduleTableSize;
                _moduleTable = new byte[moduleTableSize];
                if (_stream.Read(_moduleTable, 0, (int)moduleTableSize) < moduleTableSize)
                {
                    throw new InvalidOperationException("Reached end of file before end of indexed unbundle module table.");
                }

                var startupCodeBuffer = new byte[startupCodeSize];

                if (_stream.Read(startupCodeBuffer, 0, (int)startupCodeSize) < startupCodeSize)
                {
                    throw new InvalidOperationException("Reached end of file before end of startup code.");
                }

                return(Encoding.UTF8.GetString(startupCodeBuffer));
            }
        private static bool IsIndexedUnbundle(string sourcePath)
        {
            using (var stream = File.OpenRead(sourcePath))
            {
                var header = new byte[4];
                var read   = stream.Read(header, 0, 4);
                if (read < 4)
                {
                    return(false);
                }

                var magic = InetHelpers.LittleEndianToHost(BitConverter.ToUInt32(header, 0));
                return(magic == MagicFileHeader);
            }
        }
        private static async Task <bool> IsIndexedUnbundleAsync(string sourcePath)
        {
            var bundleFile = await FileSystem.Current.GetFileFromPathAsync(sourcePath);

            using (var stream = await bundleFile.OpenAsync(PCLStorage.FileAccess.Read))
            {
                var header = new byte[4];
                var read   = await stream.ReadAsync(header, 0, 4);

                if (read < 4)
                {
                    return(false);
                }

                var magic = InetHelpers.LittleEndianToHost(BitConverter.ToUInt32(header, 0));
                return(magic == MagicFileHeader);
            }
        }
            public JavaScriptUnbundleModule GetModule(int index)
            {
                var offset = InetHelpers.LittleEndianToHost(BitConverter.ToUInt32(_moduleTable, index * 8));
                var length = InetHelpers.LittleEndianToHost(BitConverter.ToUInt32(_moduleTable, index * 8 + 4));

                _stream.Seek(_baseOffset + offset, SeekOrigin.Begin);
                var moduleData = new byte[length];

                if (_stream.Read(moduleData, 0, (int)length) < length)
                {
                    throw new InvalidOperationException("Reached end of file before end of unbundle module.");
                }

                var source    = Encoding.UTF8.GetString(moduleData);
                var sourceUrl = index + ".js";

                return(new JavaScriptUnbundleModule(source, sourceUrl));
            }
        private static bool IsUnbundle(string sourcePath)
        {
            var magicFilePath = Path.Combine(GetUnbundleModulesDirectory(sourcePath), MagicFileName);

            if (!File.Exists(magicFilePath))
            {
                return(false);
            }

            using (var stream = File.OpenRead(magicFilePath))
            {
                var header = new byte[4];
                var read   = stream.Read(header, 0, 4);
                if (read < 4)
                {
                    return(false);
                }

                var magicHeader = BitConverter.ToUInt32(header, 0);
                return(InetHelpers.LittleEndianToHost(magicHeader) == MagicFileHeader);
            }
        }
        private static async Task <bool> IsUnbundleAsync(string sourcePath)
        {
            var magicFilePath = Path.Combine(GetUnbundleModulesDirectory(sourcePath), MagicFileName);
            var magicFile     = await FileSystem.Current.GetFileFromPathAsync(magicFilePath);

            if (magicFile == null)
            {
                return(false);
            }

            using (var stream = await magicFile.OpenAsync(PCLStorage.FileAccess.Read))
            {
                var header = new byte[4];
                var read   = await stream.ReadAsync(header, 0, 4);

                if (read < 4)
                {
                    return(false);
                }

                var magicHeader = BitConverter.ToUInt32(header, 0);
                return(InetHelpers.LittleEndianToHost(magicHeader) == MagicFileHeader);
            }
        }