Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion crates/xmtp_legacy_migration/src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,5 +144,8 @@ pub(crate) async fn read(path: &str) -> Result<Vec<u8>, MigrationError> {
let bytes = read_output(path)
.await
.map_err(|value| MigrationError::Output(OutputError::Browser { value }))?;
Ok(js_sys::Uint8Array::new(&bytes).to_vec())
Ok(bytes
.dyn_into::<js_sys::Uint8Array>()
.map_err(output)?
.to_vec())
}
12 changes: 11 additions & 1 deletion sdks/browser/test/platform/migration/archive-read-limit.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { createMigrationServer, withMigrationPage } from "./server.mjs";

const limit = 100_000_000;
const allocations = [];
const copies = [];
const server = await createMigrationServer(
"crates/xmtp_legacy_migration/fixtures",
["stable"],
Expand All @@ -17,6 +18,10 @@ try {
allocations.push(
Number(event.text().slice("MIGRATION_ARCHIVE_READ ".length)),
);
if (event.text().startsWith("MIGRATION_ARCHIVE_COPY "))
copies.push(
Number(event.text().slice("MIGRATION_ARCHIVE_COPY ".length)),
);
});
// verifies: MIG-008
const result = await page.evaluate(async (limit) => {
Expand Down Expand Up @@ -127,14 +132,19 @@ try {
assert.equal(result.retry, true);
assert.equal(result.sourceUnchanged, true);
assert.ok(allocations.includes(limit), "boundary read was not observed");
assert.equal(
copies.includes(limit),
false,
"archive read made a duplicate 100 MB byte-array copy",
);
assert.equal(
allocations.includes(limit + 1),
false,
"oversized archive bytes were allocated before rejection",
);
});
console.log(
"Browser archive read: inclusive 100 MB limit, rejection before allocation, preservation, and retry passed",
"Browser archive read: inclusive 100 MB limit, no duplicate copy, rejection before allocation, preservation, and retry passed",
);
} finally {
await browser.close();
Expand Down
8 changes: 8 additions & 0 deletions sdks/browser/test/platform/migration/read-limit-worker.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
// Observe the real OPFS archive read without replacing it.
const pending = [];
self.onmessage = (event) => pending.push(event);
const NativeUint8Array = Uint8Array;
globalThis.Uint8Array = new Proxy(NativeUint8Array, {
construct(target, args, newTarget) {
if (args[0] instanceof NativeUint8Array)
console.log("MIGRATION_ARCHIVE_COPY " + args[0].byteLength);
return Reflect.construct(target, args, newTarget);
},
});
const read = Blob.prototype.arrayBuffer;
Blob.prototype.arrayBuffer = function (...args) {
if (this instanceof File && this.name.startsWith("archive-"))
Expand Down
Loading