write
Write files to the device filesystem.
If the parent directory does not exist, it will be created.
If the file already exists, it will be overwritten.
interface AdbSyncWriteOptions {
filename: string;
file: ReadableStream<MaybeConsumable<Uint8Array>>;
type?: LinuxFileType;
permission?: number;
mtime?: number;
dryRun?: boolean;
}
declare class AdbSync {
write(options: AdbSyncWriteOptions): Promise<void>;
}
Options
filename
: Path to the file on the device filesystemfile
: File content. It uses the Consumable pattern.type
: File type, defaults toLinuxFileType.File
. Can't beLinuxFileType.Directory
.permission
: File permission, defaults to0o644
mtime
: File modification time, defaults to current time.dryRun
: Iftrue
, the file will not be written to the device filesystem. This was added for debugging and benchmarking purposes.
Example
Write a file
import { encodeUtf8 } from "@yume-chan/adb";
const file = new ReadableStream({
start(controller) {
controller.enqueue(encodeUtf8("Hello, world!"));
controller.close();
},
});
await sync.write({
filename: "/sdcard/Download/hello.txt",
file,
});
Equivalent ADB Command
echo "Hello, world!" > hello.txt
adb push hello.txt /sdcard/Download/hello.txt
Write a symbolic link
To write a symbolic link, write the target path as content and set type to LinuxFileType.SymbolicLink
.
import { encodeUtf8 } from "@yume-chan/adb";
await sync.write({
filename: "/sdcard/Download/hello.txt",
file: new ReadableStream({
start(controller) {
controller.enqueue(encodeUtf8("/sdcard/Download/target.txt"));
controller.close();
},
}),
type: LinuxFileType.SymbolicLink,
});
Equivalent ADB Command
adb shell ln -s /sdcard/Download/target.txt /sdcard/Download/hello.txt
(Web) upload a user-selected file
- JavaScript
- TypeScript
const input = document.createElement("input");
input.type = "file";
input.accept = "*/*";
input.onchange = async () => {
const file = input.files[0];
if (!file) {
return;
}
await sync.write({
filename: `/sdcard/Download/${file.name}`,
// The `ReadableStream` type in TypeScript `lib.dom.d.ts` is not compatible with Tango's Web Streams API typings
file: file.stream(),
});
};
input.click();
import { type ReadableStream } from "@yume-chan/stream-extra";
const input = document.createElement("input");
input.type = "file";
input.accept = "*/*";
input.onchange = async () => {
const file = input.files[0];
if (!file) {
return;
}
await sync.write({
filename: `/sdcard/Download/${file.name}`,
// The `ReadableStream` type in TypeScript `lib.dom.d.ts` is not compatible with Tango's Web Streams API typings
file: file.stream() as never as ReadableStream<Uint8Array>,
});
};
input.click();