wasmify-rails 0.1.0 → 0.1.1

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: 442a1c699d7e906546d63b1d5c6c451652d65ecd651bd16fa72d98841a845acf
4
- data.tar.gz: d8dac2abf684945cc8c9fbd10d3ed6c6236c9535c3f7c3974501638b7be6f607
3
+ metadata.gz: d1fa2c7f453042f39b655707ff49c7ee8d79f3ec381d9627221fa3937d9ac1b8
4
+ data.tar.gz: 1c18b79d5c1932f812bc1dcc93ab5c642614ef2bf484d9b11a3e0e2fedea851b
5
5
  SHA512:
6
- metadata.gz: 14b9d7c161e577b80d5d9b92d6cb6a7294203f535f938423b194e4adf204541f0d93c4efa466847d2546bf8a919aa96c82037613fb31c7114f3fe1c4debaa5ee
7
- data.tar.gz: 61c8ee0c9f26376f53e22bff246bba40c9d038094d846c17cc7b022f600344c70142777901d28d60eb41a5c8471c4dacbf6c188166a0aa76e1b88554c52900e9
6
+ metadata.gz: 9f44be4213456165fe64810bb81b97e08eaea66a1af60970065c0e7ddb4ba790082062d9cd43d773c60422cd5c18f97044c940504eab8f2c62e4fab1c6bf1dc7
7
+ data.tar.gz: e2fb22c5c0466e33a2245ea5dc6ed09c4f68e60cfe58a26ba010fb21b9f499614067bb6ff27eca909a257960c32e04dcbfaad47df7543c3b5130e8a94617d54a
data/CHANGELOG.md CHANGED
@@ -2,6 +2,13 @@
2
2
 
3
3
  ## master
4
4
 
5
+ ## 0.1.1
6
+
7
+ - Support multipart file uploads by converting files to data URIs.
8
+
9
+ At the Rack side, we use a `Rack::DataUriUploads` middleware to automatically convert
10
+ data-URI-encoded files to files uploads, so the application can handle them as usual.
11
+
5
12
  ## 0.1.0
6
13
 
7
14
  - Initial release
data/README.md CHANGED
@@ -147,11 +147,14 @@ bin/rails wasmify:pack
147
147
 
148
148
  And go to the `pwa` for the instructions on how to launch the application.
149
149
 
150
+ Here is an example app:
151
+
152
+ <video src="https://github.com/user-attachments/assets/34e54379-5f3e-42eb-a4fa-96c9aaa91869"></video>
153
+
150
154
  ## Roadmap
151
155
 
152
156
  - PGLite support (see [this example](https://github.com/kateinoigakukun/mastodon/blob/fff2e4a626a20a616c546ddf4f91766abaf1133a/pwa/dist/pglite.rb#L1))
153
157
  - Active Storage OPFS service
154
- - File uploads support (multipart/form-data)
155
158
  - Background jobs support
156
159
  - WASI Preview 2 support (also [this](https://github.com/kateinoigakukun/mastodon/tree/katei/wasmify))
157
160
 
@@ -11,5 +11,5 @@ module ActionMailer
11
11
  end
12
12
 
13
13
  ActiveSupport.on_load(:action_mailer) do
14
- ActionMailer::Base.add_delivery_method :null
14
+ ActionMailer::Base.add_delivery_method :null, ActionMailer::NullDeliveryMethod
15
15
  end
@@ -12,9 +12,9 @@ let db = null;
12
12
  const initDB = async (progress) => {
13
13
  if (db) return db;
14
14
 
15
- progress.updateStep("Initializing SQLite database...");
15
+ progress?.updateStep("Initializing SQLite database...");
16
16
  db = await setupSQLiteDatabase();
17
- progress.updateStep("SQLite database created.");
17
+ progress?.updateStep("SQLite database created.");
18
18
  };
19
19
 
20
20
  let vm = null;
@@ -14,6 +14,7 @@ module ImageProcessing
14
14
  fail ArgumentError, "File not found: #{source}" unless File.file?(source)
15
15
 
16
16
  if destination
17
+ File.delete(destination) if File.identical?(source, destination)
17
18
  FileUtils.cp(source, destination)
18
19
  end
19
20
  end
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rack"
4
+ require "base64"
5
+
6
+ module Rack
7
+ class DataUriUploads
8
+ # A specific prefix we use to identify data URIs that must be transformed
9
+ # into file uploads.
10
+ PREFIX = "BbC14y"
11
+ DATAURI_REGEX = %r{^#{PREFIX}data:(.*?);(.*?),(.*)$}
12
+
13
+ def initialize(app)
14
+ @app = app
15
+ end
16
+
17
+ def call(env)
18
+ return @app.call(env) unless env[RACK_INPUT]
19
+
20
+ request = Rack::Request.new(env)
21
+
22
+ if request.post? || request.put? || request.patch?
23
+ transform_params(request.params)
24
+ end
25
+
26
+ @app.call(env)
27
+ end
28
+
29
+ private
30
+
31
+ def transform_params(params)
32
+ params.each do |key, value|
33
+ if value.is_a?(String) && value.match?(DATAURI_REGEX)
34
+ params[key] = from_data_uri(value)
35
+ elsif value.is_a?(Hash)
36
+ transform_params(value)
37
+ elsif value.is_a?(Array)
38
+ value.each { transform_params(_1) }
39
+ end
40
+ end
41
+ end
42
+
43
+ def from_data_uri(data_uri)
44
+ matches = data_uri.match(DATAURI_REGEX)
45
+
46
+ content_type = matches[1]
47
+ encoding = matches[2]
48
+ data = matches[3]
49
+
50
+ file_data = Base64.decode64(data)
51
+
52
+ file = Tempfile.new(["upload", mime_to_extension(content_type)])
53
+ file.binmode
54
+ file.write(file_data)
55
+ file.rewind
56
+
57
+ # Create a Rack::Test::UploadedFile, so it works with strong parameters
58
+ Rack::Test::UploadedFile.new(file.path, content_type)
59
+ end
60
+
61
+ def mime_to_extension(mime_type)
62
+ mime_type.split("/").then do |parts|
63
+ next "" unless parts.length == 2
64
+ ".#{parts.last}"
65
+ end
66
+ end
67
+ end
68
+ end
@@ -6,6 +6,12 @@ module Wasmify
6
6
  rake_tasks do
7
7
  load "wasmify/rails/tasks.rake"
8
8
  end
9
+
10
+ initializer "wasmify.rack_data_uri" do |app|
11
+ require "rack/data_uri_uploads"
12
+
13
+ app.middleware.use Rack::DataUriUploads
14
+ end
9
15
  end
10
16
  end
11
17
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Wasmify
4
4
  module Rails # :nodoc:
5
- VERSION = "0.1.0"
5
+ VERSION = "0.1.1"
6
6
  end
7
7
  end
data/lib/wasmify-rails.rb CHANGED
@@ -18,6 +18,8 @@ module ImageProcessing
18
18
  autoload :Null, "image_processing/null"
19
19
  end
20
20
 
21
+ require "action_mailer/null_delivery"
22
+
21
23
  # NullDB for Active Record
22
24
  ActiveRecord::ConnectionAdapters.register("nulldb", "ActiveRecord::ConnectionAdapters::NullDBAdapter", "active_record/connection_adapters/nulldb_adapter")
23
25
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wasmify-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vladimir Dementyev
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-09-21 00:00:00.000000000 Z
11
+ date: 2024-09-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties
@@ -122,6 +122,7 @@ files:
122
122
  - CHANGELOG.md
123
123
  - LICENSE.txt
124
124
  - README.md
125
+ - lib/action_mailer/null_delivery.rb
125
126
  - lib/active_record/connection_adapters/nulldb_adapter.rb
126
127
  - lib/active_record/connection_adapters/nulldb_adapter/checkpoint.rb
127
128
  - lib/active_record/connection_adapters/nulldb_adapter/column.rb
@@ -134,7 +135,6 @@ files:
134
135
  - lib/active_record/connection_adapters/nulldb_adapter/statement.rb
135
136
  - lib/active_record/connection_adapters/nulldb_adapter/table_definition.rb
136
137
  - lib/active_record/connection_adapters/sqlite3_wasm_adapter.rb
137
- - lib/active_storage/null_delivery.rb
138
138
  - lib/generators/wasmify/install/USAGE
139
139
  - lib/generators/wasmify/install/install_generator.rb
140
140
  - lib/generators/wasmify/install/templates/config/environments/wasm.rb
@@ -150,6 +150,7 @@ files:
150
150
  - lib/generators/wasmify/pwa/templates/pwa/rails.sw.js
151
151
  - lib/generators/wasmify/pwa/templates/pwa/vite.config.js
152
152
  - lib/image_processing/null.rb
153
+ - lib/rack/data_uri_uploads.rb
153
154
  - lib/wasmify-rails.rb
154
155
  - lib/wasmify/rails/builder.rb
155
156
  - lib/wasmify/rails/configuration.rb
@@ -178,7 +179,7 @@ metadata:
178
179
  documentation_uri: https://github.com/palkan/wasmify-rails
179
180
  homepage_uri: https://github.com/palkan/wasmify-rails
180
181
  source_code_uri: https://github.com/palkan/wasmify-rails
181
- post_install_message:
182
+ post_install_message:
182
183
  rdoc_options: []
183
184
  require_paths:
184
185
  - lib
@@ -193,8 +194,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
193
194
  - !ruby/object:Gem::Version
194
195
  version: '0'
195
196
  requirements: []
196
- rubygems_version: 3.5.18
197
- signing_key:
197
+ rubygems_version: 3.5.16
198
+ signing_key:
198
199
  specification_version: 4
199
200
  summary: Tools and extensions to package Rails apps as Wasm modules
200
201
  test_files: []