Skip to content

Commit a2d05af

Browse files
committed
Add support for Uint8Array in puter.fs.write
1 parent b0a2590 commit a2d05af

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

src/puter-js/src/modules/FileSystem/operations/write.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ const write = async function (targetPath, data, options = {}) {
4242
else if(data instanceof Blob){
4343
data = new File([data ?? ''], filename ?? 'Untitled', { type: data.type });
4444
}
45+
// typed arrays (Uint8Array, Int8Array, etc.) and ArrayBuffer
46+
else if(data instanceof ArrayBuffer || ArrayBuffer.isView(data)){
47+
data = new File([data], filename ?? 'Untitled', { type: "application/octet-stream" });
48+
}
4549

4650
if(!data)
4751
data = new File([data ?? ''], filename);

src/puter-js/test/fs.test.js

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ naughtyStrings = [
3434
"file^name^with^carats^.txt",
3535
"file&name&with&amps&.txt",
3636
"file*name*with*asterisks*.txt",
37-
"file_name_with_long_name_exceeding_255_characters_abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz.txt",
37+
"file_name_with_long_name_exceeding_255_characters_abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz.txt",
3838
"file👍name👍with👍thumbs👍up.txt",
3939
"invisible\u200Bname.txt", // Invisible Unicode character (Zero Width Space)
4040
"invisible\u200Cname.txt", // Invisible Unicode character (Zero Width Non-Joiner)
@@ -693,4 +693,45 @@ window.fsTests = [
693693
}
694694
}
695695
},
696+
{
697+
name: "testFSWriteReadBinaryFile",
698+
description: "Test writing and reading binary file data and verify it remains intact",
699+
test: async function() {
700+
try {
701+
let randName = puter.randName() + '.webp';
702+
703+
// Create some binary data - a simple byte array representing a small binary file
704+
const binaryData = new Uint8Array([
705+
0x52, 0x49, 0x46, 0x46, 0x24, 0x00, 0x00, 0x00, 0x57, 0x45, 0x42, 0x50, 0x56, 0x50, 0x38, 0x20,
706+
0x18, 0x00, 0x00, 0x00, 0x30, 0x01, 0x00, 0x9D, 0x01, 0x2A, 0x01, 0x00, 0x01, 0x00, 0x02, 0x00,
707+
0x34, 0x25, 0xA4, 0x00, 0x03, 0x70, 0x00, 0xFE, 0xFB, 0xFD, 0x50, 0x00
708+
]);
709+
710+
// Write the binary data to a file
711+
const writeResult = await puter.fs.write(randName, binaryData);
712+
assert(writeResult.uid, "Failed to write binary file");
713+
714+
// Read the binary data back
715+
const readResult = await puter.fs.read(randName);
716+
const readBinaryData = new Uint8Array(await readResult.arrayBuffer());
717+
718+
// Verify the binary data is identical
719+
assert(readBinaryData.length === binaryData.length, "Binary data length mismatch");
720+
for (let i = 0; i < binaryData.length; i++) {
721+
assert(readBinaryData[i] === binaryData[i], `Binary data mismatch at byte ${i}: expected ${binaryData[i]}, got ${readBinaryData[i]}`);
722+
}
723+
724+
pass("testFSWriteReadBinaryFile passed");
725+
726+
// Clean up - delete the test file
727+
try {
728+
await puter.fs.delete(randName);
729+
} catch (error) {
730+
fail("testFSWriteReadBinaryFile failed to delete file:", error);
731+
}
732+
} catch (error) {
733+
fail("testFSWriteReadBinaryFile failed:", error);
734+
}
735+
}
736+
},
696737
];

src/puter-js/test/run.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<html>
22
<head>
33
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
4-
<script src="https://js.puter.com/v2/"></script>
4+
<script src="http://puter.localhost:4100/sdk/puter.dev.js"></script>
55
<script src="./kv.test.js"></script>
66
<script src="./fs.test.js"></script>
77
<script src="./ai.test.js"></script>

0 commit comments

Comments
 (0)