wasmify-rails 0.1.1 → 0.1.3

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: 683d693ae789d6a5681cbab9e6f66f8a8fbd52a23d4dc440ff78d14e13c3085d
4
+ data.tar.gz: e8c18abb0382cbec70d562debbf4f16d82eeece71c9dcdb0a82f916216c3f7a5
5
5
  SHA512:
6
- metadata.gz: 9f44be4213456165fe64810bb81b97e08eaea66a1af60970065c0e7ddb4ba790082062d9cd43d773c60422cd5c18f97044c940504eab8f2c62e4fab1c6bf1dc7
7
- data.tar.gz: e2fb22c5c0466e33a2245ea5dc6ed09c4f68e60cfe58a26ba010fb21b9f499614067bb6ff27eca909a257960c32e04dcbfaad47df7543c3b5130e8a94617d54a
6
+ metadata.gz: d7aafd09c3e716080f96efa987aeb1825ab5307be9cc1dae32e657f30a4f40314fd65e4ec610b8f271c40b3cd7ad0c951eff9be5cc9e3cd80456edaedec19a6c
7
+ data.tar.gz: 44619a0600d773ecd96e9f012b0245d6567e5d140b04397577560665ae5b49a9f7983755431f644274ebdb6dc76a3e33c91bf37eee5da56d8e4bfdc8f70a9e9b
data/CHANGELOG.md CHANGED
@@ -2,6 +2,20 @@
2
2
 
3
3
  ## master
4
4
 
5
+ ## 0.1.3
6
+
7
+ - Check if `cookieStore` is available and only manipulate cookies if it is.
8
+
9
+ - Add `skipWaiting()` to the server worker to force the new version to take over.
10
+
11
+ ## 0.1.2
12
+
13
+ - Add cache support to Rack handler.
14
+
15
+ Now we use `caches` for files with `Cache-Control`, so we don't perform a Wasm request.
16
+
17
+ - Minor fixes and improvements.
18
+
5
19
  ## 0.1.1
6
20
 
7
21
  - 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,29 @@ 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
+ - `Kernel#on_wasm?`: a convenient predicate method to check if the code is running in the Wasm environment.
159
+
160
+ - Active Record
161
+
162
+ - `sqlite3_wasm` adapter: work with `sqlite3` Wasm just like with a regular SQLite database.
163
+ - `nulldb` adapter for testing purposes.
164
+
165
+ - Active Storage
166
+
167
+ - `null` variant processor (just leaves files as is)
168
+
169
+ - Action Mailer
170
+
171
+ - `null` delivery method (to disable emails in Wasm)
172
+
173
+ - Rack
174
+
175
+ - `Rack::DataUriUploads` middleware to transparently transform Data URI uploads into files.
176
+
154
177
  ## Roadmap
155
178
 
156
179
  - PGLite support (see [this example](https://github.com/kateinoigakukun/mastodon/blob/fff2e4a626a20a616c546ddf4f91766abaf1133a/pwa/dist/pglite.rb#L1))
@@ -164,7 +187,7 @@ Bug reports and pull requests are welcome on GitHub at [https://github.com/](htt
164
187
 
165
188
  ## Credits
166
189
 
167
- The `nulldb` adapter for Active Record (used for tests) is adopted from [this project](https://github.com/nulldb/nulldb).
190
+ The `nulldb` adapter for Active Record (used for tests) is ported from [this project](https://github.com/nulldb/nulldb).
168
191
 
169
192
  ## License
170
193
 
@@ -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 = () => {
@@ -67,7 +71,7 @@ self.addEventListener("activate", (event) => {
67
71
 
68
72
  self.addEventListener("install", (event) => {
69
73
  console.log("[rails-web] Install Service Worker");
70
- event.waitUntil(installApp());
74
+ event.waitUntil(installApp().then(() => self.skipWaiting()));
71
75
  });
72
76
 
73
77
  const rackHandler = new RackHandler(initVM, { assumeSSL: true });
@@ -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.3"
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.3
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-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties