Module: Dhall::Resolvers

Defined in:
lib/dhall/resolve.rb

Defined Under Namespace

Modules: NoCache Classes: Default, LocalOnly, None, RamCache, ReadPathAndIPFSSources, ResolutionSet, SourceWithDeadline, Standard, StandardFileCache

Constant Summary collapse

ReadPathSources =
lambda do |sources|
	sources.map do |source|
		Promise.resolve(nil).then { source.pathname.binread }
	end
end
ReadEnvironmentSources =
lambda do |sources|
	sources.map do |source|
		Promise.resolve(nil).then do
			ENV.fetch(source.var) do
				raise ImportFailedException, "No #{source}"
			end
		end
	end
end
PreflightCORS =
lambda do |source, parent_origin|
	timeout = source.deadline.timeout
	uri = source.uri
	if parent_origin != "localhost" && parent_origin != source.origin
		req = Net::HTTP::Options.new(uri)
		req["Origin"] = parent_origin
		req["Access-Control-Request-Method"] = "GET"
		req["Access-Control-Request-Headers"] =
			source.headers.to_a.map { |h|
				(h.fetch("header") { h.fetch("mapKey") }).to_s
			}.join(",")
		r = Util.net_http_req_with_timeout(uri, req, timeout: timeout)

		raise ImportFailedException, source if r.code != "200"
		unless r["Access-Control-Allow-Origin"] == parent_origin ||
		       r["Access-Control-Allow-Origin"] == "*"
			raise ImportBannedException, source
		end
	end
end
ReadHttpSources =
lambda do |sources, parent_origin|
	sources.map do |source|
		Promise.resolve(nil).then do
			PreflightCORS.call(source, parent_origin)
			timeout = source.deadline.timeout
			uri = source.uri
			r = loop do
				req = Net::HTTP::Get.new(uri)
				source.headers.each do |header|
					req[(header.fetch("header") { header.fetch("mapKey") }).to_s] =
						(header.fetch("value") { header.fetch("mapValue") }).to_s
				end
				r = Util.net_http_req_with_timeout(uri, req, timeout: timeout)

				break r unless ["301", "302", "303", "307", "308"].include?(r.code)
				uri = URI(r["Location"])
			end

			raise ImportFailedException, source if r.code != "200"
			r.body
		end
	end
end
StandardReadHttpSources =
lambda do |sources, parent_origin|
	ReadHttpSources.call(sources, parent_origin).map do |source_promise|
		source_promise.then do |s|
			s = s.dup.force_encoding("UTF-8")
			unless s.valid_encoding?
				raise ImportFailedException, "#{s.inspect} is not valid UTF-8"
			end
			s
		end
	end
end
RejectSources =
lambda do |sources|
	sources.map do |source|
		Promise.new.reject(ImportBannedException.new(source))
	end
end