wasmify-rails 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d1fa2c7f453042f39b655707ff49c7ee8d79f3ec381d9627221fa3937d9ac1b8
4
- data.tar.gz: 1c18b79d5c1932f812bc1dcc93ab5c642614ef2bf484d9b11a3e0e2fedea851b
3
+ metadata.gz: 67904120ba3d147d07ad14d5e15c6f9faf06eb43f3c93d132326407581be4ad7
4
+ data.tar.gz: c88da10a277060fcda66faa34f1334dc246870d4ab0d32655772d0b05ed534a2
5
5
  SHA512:
6
- metadata.gz: 9f44be4213456165fe64810bb81b97e08eaea66a1af60970065c0e7ddb4ba790082062d9cd43d773c60422cd5c18f97044c940504eab8f2c62e4fab1c6bf1dc7
7
- data.tar.gz: e2fb22c5c0466e33a2245ea5dc6ed09c4f68e60cfe58a26ba010fb21b9f499614067bb6ff27eca909a257960c32e04dcbfaad47df7543c3b5130e8a94617d54a
6
+ metadata.gz: 78a1328a1ee3339968a6715b1482012f77dff692b69a55062a7c0eb1c460a7c631c617149a863eac7771c5dcccbd3673fa4fb18ca916005e215615db50bea210
7
+ data.tar.gz: f128859e3c1f7e5ab2eee405afe71e7a5bd9fd593cfe88dae69cf6dd91f70b837c2bfe42364b95da2a1d60507776e28123dc7389a48231f720a5b9acbd599f80
data/CHANGELOG.md CHANGED
@@ -2,6 +2,14 @@
2
2
 
3
3
  ## master
4
4
 
5
+ ## 0.1.2
6
+
7
+ - Add cache support to Rack handler.
8
+
9
+ Now we use `caches` for files with `Cache-Control`, so we don't perform a Wasm request.
10
+
11
+ - Minor fixes and improvements.
12
+
5
13
  ## 0.1.1
6
14
 
7
15
  - Support multipart file uploads by converting files to data URIs.
data/README.md CHANGED
@@ -13,7 +13,7 @@ Adding to your Gemfile:
13
13
  gem "wasmify-rails", group: [:development, :wasm]
14
14
  ```
15
15
 
16
- ## Usage
16
+ ## Usage: generators and tasks
17
17
 
18
18
  This gem comes with a few commands (Rake tasks) to help you get started with packing your Rails app into a Wasm module.
19
19
 
@@ -151,6 +151,27 @@ Here is an example app:
151
151
 
152
152
  <video src="https://github.com/user-attachments/assets/34e54379-5f3e-42eb-a4fa-96c9aaa91869"></video>
153
153
 
154
+ ## Rails/Ruby extensions
155
+
156
+ This gem provides a variety of _adapters_ and plugins to make your Rails application Wasm-compatible:
157
+
158
+ - Active Record
159
+
160
+ - `sqlite3_wasm` adapter: work with `sqlite3` Wasm just like with a regular SQLite database.
161
+ - `nulldb` adapter for testing purposes.
162
+
163
+ - Active Storage
164
+
165
+ - `null` variant processor (just leaves files as is)
166
+
167
+ - Action Mailer
168
+
169
+ - `null` delivery method (to disable emails in Wasm)
170
+
171
+ - Rack
172
+
173
+ - `Rack::DataUriUploads` middleware to transparently transform Data URI uploads into files.
174
+
154
175
  ## Roadmap
155
176
 
156
177
  - PGLite support (see [this example](https://github.com/kateinoigakukun/mastodon/blob/fff2e4a626a20a616c546ddf4f91766abaf1133a/pwa/dist/pglite.rb#L1))
@@ -164,7 +185,7 @@ Bug reports and pull requests are welcome on GitHub at [https://github.com/](htt
164
185
 
165
186
  ## Credits
166
187
 
167
- The `nulldb` adapter for Active Record (used for tests) is adopted from [this project](https://github.com/nulldb/nulldb).
188
+ The `nulldb` adapter for Active Record (used for tests) is ported from [this project](https://github.com/nulldb/nulldb).
168
189
 
169
190
  ## License
170
191
 
@@ -118,6 +118,7 @@ module ActiveRecord
118
118
  str_val = val.to_s
119
119
  next str_val if val.typeof == "string"
120
120
  next str_val == "true" if val.typeof == "boolean"
121
+ next nil if str_val == "null"
121
122
 
122
123
  # handle integers and floats
123
124
  next str_val.include?(".") ? val.to_f : val.to_i if val.typeof == "number"
@@ -136,14 +137,37 @@ module ActiveRecord
136
137
  end
137
138
  end
138
139
 
140
+ # This type converts byte arrays represented as strings from JS to binaries in Ruby
141
+ class JSBinary < ActiveModel::Type::Binary
142
+ def deserialize(value)
143
+ bvalue = value
144
+ if value.is_a?(String)
145
+ bvalue = value.split(",").map(&:to_i).pack("c*")
146
+ end
147
+
148
+ super(bvalue)
149
+ end
150
+ end
151
+
152
+ ActiveRecord::Type.register(:binary, JSBinary, adapter: :sqlite3_wasm)
153
+
139
154
  class << self
140
155
  def database_exists?(config)
141
156
  true
142
157
  end
143
158
 
144
159
  def new_client(config) = ExternalInterface.new(config)
160
+
161
+ private
162
+ def initialize_type_map(m)
163
+ super
164
+ register_class_with_limit m, %r(binary)i, JSBinary
165
+ end
145
166
  end
146
167
 
168
+ # Re-initialize type map to include JSBinary
169
+ TYPE_MAP = Type::TypeMap.new.tap { |m| initialize_type_map(m) }
170
+
147
171
  attr_reader :external_interface
148
172
 
149
173
  def initialize(...)
@@ -47,10 +47,15 @@ class Wasmify::InstallGenerator < Rails::Generators::Base
47
47
  end
48
48
  end
49
49
 
50
- def skip_bundler_setup_in_boot
50
+ def configure_boot_file
51
51
  inject_into_file "config/boot.rb", after: /require ['"]bundler\/setup['"]/ do
52
52
  " unless RUBY_PLATFORM =~ /wasm/"
53
53
  end
54
+
55
+ # Disable bootsnap if any
56
+ inject_into_file "config/boot.rb", after: /require ['"]bootsnap\/setup['"]/ do
57
+ " unless RUBY_PLATFORM =~ /wasm/"
58
+ end
54
59
  end
55
60
 
56
61
  def add_tzinfo_data_to_gemfile
@@ -26,5 +26,8 @@ Rails.application.configure do
26
26
  config.active_storage.variant_processor = :null
27
27
  end
28
28
 
29
- config.secret_key_base = "<change-me>"
29
+ # Do not use the same secret key base in a local app (for security reasons)
30
+ config.secret_key_base = "wasm-secret"
31
+ # Use a different session cookie name to avoid conflicts
32
+ config.session_store :cookie_store, key: "_local_session"
30
33
  end
@@ -15,3 +15,4 @@ exclude_gems:
15
15
  - nio4r
16
16
  - io-console
17
17
  - psych
18
+ - date
@@ -4,15 +4,7 @@ export const setupSQLiteDatabase = async () => {
4
4
  const sqlite3 = await sqlite3InitModule();
5
5
 
6
6
  console.log("Running SQLite3 version", sqlite3.version.libVersion);
7
- const db =
8
- "opfs" in sqlite3
9
- ? new sqlite3.oo1.OpfsDb("/railsdb.sqlite3")
10
- : new sqlite3.oo1.DB("/railsdb.sqlite3", "ct");
11
- console.log(
12
- "opfs" in sqlite3
13
- ? `OPFS is available, created persisted database at ${db.filename}`
14
- : `OPFS is not available, created transient database ${db.filename}`,
15
- );
16
-
7
+ // NOTE: This database is transient and will be lost if you uninstall the service worker (aka hard reset)
8
+ const db = new sqlite3.oo1.DB("/railsdb.sqlite3", "ct");
17
9
  return db;
18
10
  };
@@ -15,6 +15,8 @@ const initDB = async (progress) => {
15
15
  progress?.updateStep("Initializing SQLite database...");
16
16
  db = await setupSQLiteDatabase();
17
17
  progress?.updateStep("SQLite database created.");
18
+
19
+ return db;
18
20
  };
19
21
 
20
22
  let vm = null;
@@ -47,6 +49,8 @@ const initVM = async (progress, opts = {}) => {
47
49
  vm.eval("ActiveRecord::Tasks::DatabaseTasks.prepare_all");
48
50
 
49
51
  redirectConsole = false;
52
+
53
+ return vm;
50
54
  };
51
55
 
52
56
  const resetVM = () => {
@@ -29,6 +29,7 @@ module Rack
29
29
  private
30
30
 
31
31
  def transform_params(params)
32
+ return params unless params.is_a?(Hash)
32
33
  params.each do |key, value|
33
34
  if value.is_a?(String) && value.match?(DATAURI_REGEX)
34
35
  params[key] = from_data_uri(value)
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Wasmify
4
4
  module Rails # :nodoc:
5
- VERSION = "0.1.1"
5
+ VERSION = "0.1.2"
6
6
  end
7
7
  end
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.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vladimir Dementyev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-09-23 00:00:00.000000000 Z
11
+ date: 2024-09-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties