สภาพแวดล้อมรันไทม์แบบกำหนดเอง
ทำความเข้าใจความเป็นไปได้ของสภาพแวดล้อมรันไทม์แบบกำหนดเองหรือการนำ Deno แบบเฉพาะทางไปใช้งานใกล้ผู้ใช้
สภาพแวดล้อมรันไทม์แบบกำหนดเอง เป็นบทเรียน Edge Computing with Cloudflare Workers & Deno ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Edge Computing with Cloudflare Workers & Deno และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Edge Computing with Cloudflare Workers & Deno มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Intro to Custom Runtimes
Welcome! Today we'll explore custom runtime environments and specialized Deno deployments at the edge.
A custom runtime lets you tailor the execution environment for your code. Instead of using a standard, pre-configured serverless runtime, you can define specific versions, dependencies, or even entirely different languages.
At the edge, this means more control over how your code runs close to users, potentially unlocking new performance and functionality.
Why Customize at the Edge?
Why would you need a custom runtime?
- Specialized Libraries: Use native libraries or specific versions not available in standard runtimes.
- Performance: Optimize for specific workloads, like heavy computation or data processing, by using highly efficient languages or custom binaries.
- Resource Control: Fine-tune memory, CPU, or network usage for highly specific edge devices.
- Unique Languages: Run code written in languages other than JavaScript/TypeScript, often compiled to WebAssembly.
Deno's Role in Customization
Deno's design makes it particularly suitable for custom edge environments:
- Single Executable: Deno itself is a single, self-contained executable, easy to deploy or embed.
- No
node_modules: Its URL-based module system avoids complex dependency trees, simplifying custom builds. - Explicit Permissions: Granular control over file system, network, and environment access allows for secure, minimal deployments.
- Built-in Tooling: Tools like
deno compile(which we'll see) help create self-contained binaries.
WebAssembly (Wasm) Overview
WebAssembly (Wasm) is key to custom runtimes. It's a low-level binary instruction format that runs in modern web browsers and increasingly, on serverless platforms like Cloudflare Workers.
Wasm acts as a compilation target for many languages (Rust, C++, Go, AssemblyScript), allowing you to write high-performance code and run it securely and portably across different environments, including the edge.
Deno Loading Wasm Modules
Deno has built-in support for WebAssembly, allowing you to load and execute Wasm modules directly within your Deno applications. This extends Deno's capabilities far beyond JavaScript/TypeScript.
Here's a simple Deno script that loads and runs a small Wasm module designed to add two numbers:
const wasmCode = new Uint8Array([
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00,
0x01, 0x07, 0x01, 0x60, 0x02, 0x7f, 0x7f, 0x01,
0x7f, 0x03, 0x02, 0x01, 0x00, 0x07, 0x07, 0x01,
0x03, 0x61, 0x64, 0x64, 0x00, 0x00, 0x0a, 0x09,
0x01, 0x07, 0x00, 0x20, 0x00, 0x20, 0x01, 0x6a,
0x0b
]); // This Wasm adds two i32 numbers
async function runWasm() {
const { instance } = await WebAssembly.instantiate(wasmCode);
const addResult = instance.exports.add(5, 7);
console.log(`Wasm add(5, 7) = ${addResult}`);
}
runWasm();Wasm on Edge Platforms
Edge platforms like Cloudflare Workers embrace WebAssembly. You can compile code written in Rust, C++, or Go into Wasm and deploy it as a Worker.
This allows developers to leverage existing codebases, achieve near-native performance, and utilize specific language features that might not be available or efficient in JavaScript. It's a powerful way to create highly optimized edge functions.
Specialized Deno Deployments
While Deno Deploy offers a fantastic managed service, there are scenarios requiring specialized Deno deployments:
- Self-Hosting Deno: Running Deno on your own infrastructure for full control over the environment.
- Embedded Systems: Deploying Deno on resource-constrained edge devices with specific hardware requirements.
- Custom Deno Builds: Compiling Deno from source with specific features enabled or disabled to meet unique needs, such as integrating custom native plugins or reducing binary size.
Building Custom Deno Runtimes
For truly custom Deno environments, you might compile Deno from its source code. This advanced technique allows you to:
- Include specific Rust crates (libraries) directly within the Deno binary.
- Remove unnecessary features to create a smaller, more focused runtime.
- Target unusual architectures or operating systems not officially supported by standard Deno releases.
This approach is for highly specialized use cases where absolute control over the runtime is paramount.
Deno Compile for Edge Executables
Deno's deno compile command allows you to create a standalone executable from your Deno script. This executable includes the Deno runtime and your application code, making it portable.
While not a 'runtime environment' itself, a compiled Deno app is a highly specialized Deno deployment, perfect for edge devices that need self-contained, single-file applications without a separate Deno installation.
console.log("Hello from a Deno compiled app!");
if (Deno.args.length > 0) {
console.log(`Arguments received: ${Deno.args.join(", ")}`);
}
// To compile this:
// deno compile --allow-read --allow-env my_app.tsSummary of Benefits
Custom runtime environments and specialized Deno deployments offer significant benefits for advanced edge applications:
- Enhanced Performance: Leverage Wasm or optimized native code.
- Greater Flexibility: Use a wider range of languages and libraries.
- Resource Efficiency: Tailor the environment to specific hardware or memory constraints.
- Security: Fine-tune permissions and isolation.
These strategies empower developers to push the boundaries of what's possible at the edge.
Check Your Understanding
Which of the following are key reasons or capabilities enabled by using custom runtime environments or specialized Deno deployments at the edge?
Recap: Custom Edge Runtimes
In this lesson, we explored the concept of custom runtime environments and specialized Deno deployments for edge applications.
- We saw how custom runtimes offer unparalleled control, enabling specific libraries, performance optimizations, and unique language support.
- We learned about WebAssembly (Wasm) as a crucial technology for running high-performance, non-JS code at the edge, and how Deno can interact with it.
- Finally, we discussed scenarios for specialized Deno deployments, including self-hosting and creating standalone executables with
deno compile.
These advanced strategies empower you to build highly efficient and flexible edge solutions!
คำถามที่พบบ่อย
บทเรียน “สภาพแวดล้อมรันไทม์แบบกำหนดเอง” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “สภาพแวดล้อมรันไทม์แบบกำหนดเอง” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Edge Computing with Cloudflare Workers & Deno ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Edge Computing with Cloudflare Workers & Deno มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “สภาพแวดล้อมรันไทม์แบบกำหนดเอง”
ทำความเข้าใจความเป็นไปได้ของสภาพแวดล้อมรันไทม์แบบกำหนดเองหรือการนำ Deno แบบเฉพาะทางไปใช้งานใกล้ผู้ใช้ คุณปฏิบัติ Edge Computing with Cloudflare Workers & Deno ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Edge Computing with Cloudflare Workers & Deno หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Edge Computing with Cloudflare Workers & Deno บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “สภาพแวดล้อมรันไทม์แบบกำหนดเอง” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Edge Computing with Cloudflare Workers & Deno นี้ได้ไหม
ได้ บทเรียน Edge Computing with Cloudflare Workers & Deno ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- Deno Deploy และ CLI
- สภาพแวดล้อมรันไทม์แบบกำหนดเอง
- การทดสอบและ CI/CD สำหรับระบบใกล้ผู้ใช้
- การปรับใช้แบบน้ำเงิน-เขียวและทยอยเปิดใช้งาน