klenod-runtime 0.0.5 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 739914ba526e13992821c30f81881def0ffd3381ad655a26b6004b5a7fb61c9c
4
- data.tar.gz: 7f32c13692b4ae7bf103ac27fb82752c85a89cf300c99f5a7fcc49acffa65a7c
3
+ metadata.gz: c27ef979d9823153396b640d84a1d4e892f4e66c5c88f8afad0f6fc02a8ae17e
4
+ data.tar.gz: 6b4be228b1052f59104eff4ac30750661c689422978b5e0484ec95c95da9febc
5
5
  SHA512:
6
- metadata.gz: 9e06c336456aa5b4d5f9f75b0385a91f1b28b5c58598d160a28bd50eeb929d7d83b97b5eda50ece404a6ddcfddf6dfa65f64e1446bdcf556ec31e28e4e393128
7
- data.tar.gz: d99f919df953bb5b3c818971839d6117d2b99c17713999551da67fd3222c47c8b866afb0ad52c4c1302911dfbb50540836e9216314c67ce1130e7694c41ab13d
6
+ metadata.gz: daab02d1ecd9845a8e6faba1bddca6db086226ba200e4420349a9fd35ca65283b0c7408ec32b8b66ef016901dd45679a14c4f9aece51dff406b573d0275113ca
7
+ data.tar.gz: 92f1cf562e098530ec0a7689b32e86ab70f4c0d58af380a97d6fa2a1e965b3ac7851e23b0fdebe29de6a25224b586d711d7cccd0cbbbe8d70e857ce15c5900d8
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "uri"
4
+
5
+ module Klenod
6
+ module Runtime
7
+ module AssetUrl
8
+ DEFAULT_BASE = "/assets/"
9
+ LEGACY_OUTPUT_PREFIX = "/assets/"
10
+
11
+ module_function
12
+
13
+ def normalize(base)
14
+ value = base.to_s
15
+ value = DEFAULT_BASE if value.empty?
16
+ uri = URI.parse(value)
17
+
18
+ if uri.scheme
19
+ unless %w[http https].include?(uri.scheme) && uri.host && !uri.query && !uri.fragment
20
+ raise ArgumentError, "asset base must be an absolute HTTP(S) URL without a query or fragment: #{base.inspect}"
21
+ end
22
+ elsif !value.start_with?("/") || uri.query || uri.fragment
23
+ raise ArgumentError, "asset base must be an absolute path or HTTP(S) URL: #{base.inspect}"
24
+ end
25
+
26
+ value.end_with?("/") ? value : "#{value}/"
27
+ rescue URI::InvalidURIError
28
+ raise ArgumentError, "asset base must be an absolute path or HTTP(S) URL: #{base.inspect}"
29
+ end
30
+
31
+ def join(base, output_path)
32
+ path = output_path.to_s
33
+ unless path.start_with?("/") && !path.start_with?("//")
34
+ raise ArgumentError, "asset output path must be root-relative: #{output_path.inspect}"
35
+ end
36
+
37
+ "#{normalize(base)}#{path.delete_prefix("/")}"
38
+ end
39
+
40
+ def legacy_join(base, output_path)
41
+ join(base, output_path.delete_prefix(LEGACY_OUTPUT_PREFIX).prepend("/"))
42
+ end
43
+
44
+ def path_prefix(base)
45
+ normalized = normalize(base)
46
+ normalized if normalized.start_with?("/")
47
+ end
48
+ end
49
+ end
50
+ end
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "asset_url"
4
+
3
5
  module Klenod
4
6
  module Runtime
5
7
  ModuleSpec =
@@ -9,11 +11,22 @@ module Klenod
9
11
  DefaultImport = Data.define(:name)
10
12
 
11
13
  AssetSpec =
12
- Data.define(:logical_name, :content_hash, :output_path, :content_type, :metadata)
14
+ Data.define(:logical_name, :content_hash, :output_path, :content_type, :metadata, :url) do
15
+ def initialize(logical_name = nil, content_hash = nil, output_path = nil, content_type = nil, metadata = nil, url = nil, **keywords)
16
+ logical_name = keywords.fetch(:logical_name, logical_name)
17
+ content_hash = keywords.fetch(:content_hash, content_hash)
18
+ output_path = keywords.fetch(:output_path, output_path)
19
+ content_type = keywords.fetch(:content_type, content_type)
20
+ metadata = keywords.fetch(:metadata, metadata)
21
+ url = keywords.fetch(:url, url)
22
+ url ||= output_path
23
+ super(logical_name:, content_hash:, output_path:, content_type:, metadata:, url:)
24
+ end
25
+ end
13
26
  AssetReference = Data.define(:index, :asset)
14
27
 
15
28
  class Bundle
16
- attr_reader :entrypoints, :modules, :assets, :source_root
29
+ attr_reader :entrypoints, :modules, :assets, :source_root, :base
17
30
 
18
31
  def self.load(source, source_root: nil)
19
32
  BundleFormat.load(source, source_root: source_root)
@@ -23,11 +36,12 @@ module Klenod
23
36
  load(path, source_root: source_root)
24
37
  end
25
38
 
26
- def initialize(entrypoints, modules, assets, source_root: nil)
39
+ def initialize(entrypoints, modules, assets, source_root: nil, base: AssetUrl::DEFAULT_BASE)
27
40
  @entrypoints = entrypoints
28
41
  @modules = modules
29
- @assets = assets
30
42
  @source_root = source_root
43
+ @base = AssetUrl.normalize(base)
44
+ @assets = bind_asset_urls(assets)
31
45
  @mods = {}
32
46
  end
33
47
 
@@ -85,6 +99,17 @@ module Klenod
85
99
  @assets.fetch(output_path)
86
100
  end
87
101
 
102
+ def asset_url(asset_or_output_path)
103
+ if asset_or_output_path.respond_to?(:output_path)
104
+ asset = @assets.fetch(asset_or_output_path.output_path, asset_or_output_path)
105
+ return asset.url || AssetUrl.join(base, asset.output_path)
106
+ end
107
+
108
+ @assets.fetch(asset_or_output_path).url
109
+ rescue KeyError
110
+ AssetUrl.join(base, asset_or_output_path)
111
+ end
112
+
88
113
  def assets_for(logical_name)
89
114
  @assets.values.select { |asset| asset.logical_name == logical_name.to_s }
90
115
  end
@@ -121,11 +146,13 @@ module Klenod
121
146
  end
122
147
 
123
148
  def marshal_dump
124
- [@entrypoints, @modules, @assets, @source_root]
149
+ [@entrypoints, @modules, @assets, @source_root, @base]
125
150
  end
126
151
 
127
152
  def marshal_load(data)
128
- @entrypoints, @modules, @assets, @source_root = data
153
+ @entrypoints, @modules, assets, @source_root, base = data
154
+ @base = AssetUrl.normalize(base)
155
+ @assets = bind_asset_urls(assets)
129
156
  @mods = {}
130
157
  end
131
158
 
@@ -145,6 +172,18 @@ module Klenod
145
172
 
146
173
  private
147
174
 
175
+ def bind_asset_urls(assets)
176
+ assets.to_h do |output_path, asset|
177
+ legacy_url = asset.url.nil? || (asset.url == asset.output_path && base != AssetUrl::DEFAULT_BASE)
178
+ url = legacy_output_path?(asset.output_path) ? AssetUrl.legacy_join(base, asset.output_path) : AssetUrl.join(base, asset.output_path)
179
+ [output_path, legacy_url ? asset.with(url:) : asset]
180
+ end
181
+ end
182
+
183
+ def legacy_output_path?(output_path)
184
+ output_path.start_with?(AssetUrl::LEGACY_OUTPUT_PREFIX)
185
+ end
186
+
148
187
  def assets_for_runtime_module(module_id)
149
188
  module_spec = @modules.fetch(module_id)
150
189
  logical_names = [module_spec.id, module_spec.source_path].uniq
@@ -2,6 +2,7 @@
2
2
 
3
3
  require "json"
4
4
 
5
+ require_relative "asset_url"
5
6
  require_relative "version"
6
7
  require_relative "source_map"
7
8
 
@@ -12,7 +13,7 @@ module Klenod
12
13
 
13
14
  module BundleFormat
14
15
  MAGIC = "MODPACK_BUNDLE_V1\n"
15
- FORMAT_VERSION = 1
16
+ FORMAT_VERSION = 3
16
17
 
17
18
  module_function
18
19
 
@@ -39,6 +40,7 @@ module Klenod
39
40
  "format_version" => FORMAT_VERSION,
40
41
  "runtime_version" => Runtime::VERSION,
41
42
  "source_root" => encode_value(bundle.source_root),
43
+ "base" => bundle.base,
42
44
  "entrypoints" => encode_value(bundle.entrypoints),
43
45
  "modules" => encode_modules(bundle.modules),
44
46
  "assets" => encode_assets(bundle.assets)
@@ -53,7 +55,8 @@ module Klenod
53
55
  decode_value(payload.fetch("entrypoints")),
54
56
  decode_modules(payload.fetch("modules")),
55
57
  decode_assets(payload.fetch("assets")),
56
- source_root: decode_value(payload["source_root"])
58
+ source_root: decode_value(payload["source_root"]),
59
+ base: payload.key?("base") ? payload.fetch("base") : AssetUrl::DEFAULT_BASE
57
60
  )
58
61
  bundle.source_root = source_root if source_root
59
62
  bundle
@@ -67,7 +70,7 @@ module Klenod
67
70
  raise BundleFormatError, "Malformed Klenod bundle payload" unless payload.is_a?(Hash)
68
71
 
69
72
  version = payload["format_version"]
70
- unless version == FORMAT_VERSION
73
+ unless [1, 2, FORMAT_VERSION].include?(version)
71
74
  raise BundleFormatError, "Unsupported Klenod bundle format version: #{version.inspect}"
72
75
  end
73
76
 
@@ -157,7 +160,8 @@ module Klenod
157
160
  "content_hash" => asset.content_hash,
158
161
  "output_path" => asset.output_path,
159
162
  "content_type" => asset.content_type,
160
- "metadata" => encode_value(asset.metadata)
163
+ "metadata" => encode_value(asset.metadata),
164
+ "url" => asset.url
161
165
  }
162
166
  ]
163
167
  end
@@ -173,7 +177,8 @@ module Klenod
173
177
  asset_payload.fetch("content_hash"),
174
178
  asset_payload.fetch("output_path"),
175
179
  asset_payload.fetch("content_type"),
176
- decode_value(asset_payload.fetch("metadata"))
180
+ decode_value(asset_payload.fetch("metadata")),
181
+ asset_payload["url"]
177
182
  )
178
183
  ]
179
184
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Klenod
4
4
  module Runtime
5
- VERSION = "0.0.5"
5
+ VERSION = "0.0.6"
6
6
  end
7
7
  end
@@ -4,6 +4,7 @@ require_relative "runtime/version"
4
4
  require_relative "runtime/source_map"
5
5
  require_relative "runtime/backtrace_rewriter"
6
6
  require_relative "runtime/mod"
7
+ require_relative "runtime/asset_url"
7
8
  require_relative "runtime/bundle"
8
9
  require_relative "runtime/bundle_format"
9
10
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: klenod-runtime
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrés Alin
@@ -18,6 +18,7 @@ extra_rdoc_files: []
18
18
  files:
19
19
  - README.md
20
20
  - lib/klenod/runtime.rb
21
+ - lib/klenod/runtime/asset_url.rb
21
22
  - lib/klenod/runtime/backtrace_rewriter.rb
22
23
  - lib/klenod/runtime/bundle.rb
23
24
  - lib/klenod/runtime/bundle_format.rb
@@ -37,7 +38,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
37
38
  requirements:
38
39
  - - ">="
39
40
  - !ruby/object:Gem::Version
40
- version: 4.0.6
41
+ version: '4.0'
41
42
  required_rubygems_version: !ruby/object:Gem::Requirement
42
43
  requirements:
43
44
  - - ">="