Skip to content

Commit 6d5ee29

Browse files
authored
feat(runtime): basic streams implementation (#96)
* feat(runtime): basic streams implementation * fix: async stream implementation
1 parent e07c71e commit 6d5ee29

22 files changed

+1895
-252
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ authors = ["the Andromeda team"]
77
edition = "2024"
88
license = "Mozilla Public License 2.0"
99
repository = "https://github.com/tryandromeda/andromeda"
10-
version = "0.1.0-draft7"
10+
version = "0.1.0-draft8"
1111

1212
[workspace.dependencies]
1313
andromeda-core = { path = "core" }

examples/blob.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ const sliced = blob.slice(0, 5);
55
console.log(" Sliced size:", sliced.size);
66

77
const file = new File(["File content here"], "document.txt", {
8-
type: "text/plain",
9-
lastModified: Date.now() - 10000,
8+
type: "text/plain",
9+
lastModified: Date.now() - 10000,
1010
});
1111
console.log(" File name:", file.name);
1212
console.log(" File size:", file.size);
@@ -28,5 +28,5 @@ console.log(" FormData has blob:", formData.has("blob"));
2828

2929
console.log(" FormData keys:");
3030
for (const key of formData.keys()) {
31-
console.log(" -", key);
31+
console.log(" -", key);
3232
}

examples/lint_empty_statement.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
let a = 1;;
1+
let a = 1;

examples/modules/config.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
2-
"name": "test-package",
3-
"version": "1.0.0",
4-
"description": "A test JSON module",
5-
"features": ["json-import", "module-loading"],
6-
"config": {
7-
"debug": true,
8-
"timeout": 5000
9-
}
2+
"name": "test-package",
3+
"version": "1.0.0",
4+
"description": "A test JSON module",
5+
"features": ["json-import", "module-loading"],
6+
"config": {
7+
"debug": true,
8+
"timeout": 5000
9+
}
1010
}

examples/modules/default-export.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export default function greet(name) {
2-
return `Hello, ${name}!`;
2+
return `Hello, ${name}!`;
33
}
44

55
export const version = "1.0.0";

examples/modules/greeting.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
21
export default function greet(name: string): string {
3-
return `Hello, ${name}!`;
2+
return `Hello, ${name}!`;
43
}
54

65
export function farewell(name: string): string {
7-
return `Goodbye, ${name}!`;
6+
return `Goodbye, ${name}!`;
87
}

examples/modules/math.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
21
export function add(a, b) {
3-
return a + b;
2+
return a + b;
43
}
54

65
export function multiply(a, b) {
7-
return a * b;
6+
return a * b;
87
}
98

109
export const PI = 3.14159;
1110

1211
export default function square(x) {
13-
return x * x;
12+
return x * x;
1413
}

examples/modules/mixed-exports.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,26 @@ export const API_URL = "https://api.example.com";
33
export const MAX_RETRIES = 3;
44

55
export function fetchData(endpoint: string): string {
6-
return `Fetching from ${API_URL}/${endpoint}`;
6+
return `Fetching from ${API_URL}/${endpoint}`;
77
}
88

99
export class DataProcessor {
10-
name: string;
11-
12-
constructor(name: string) {
13-
this.name = name;
14-
}
15-
16-
process(data: string): string {
17-
return `${this.name} processed: ${data}`;
18-
}
10+
name: string;
11+
12+
constructor(name: string) {
13+
this.name = name;
14+
}
15+
16+
process(data: string): string {
17+
return `${this.name} processed: ${data}`;
18+
}
1919
}
2020

2121
// Default export
2222
export default {
23-
name: "MixedExportsModule",
24-
version: "2.0.0",
25-
init() {
26-
return "Module initialized";
27-
}
23+
name: "MixedExportsModule",
24+
version: "2.0.0",
25+
init() {
26+
return "Module initialized";
27+
},
2828
};

examples/modules/sequential-test.js

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,37 @@
11
// Simple sequential test to avoid module loading conflicts
2-
console.log('🚀 Testing ES6 modules sequentially...\n');
2+
console.log("🚀 Testing ES6 modules sequentially...\n");
33

44
async function runTests() {
5-
try {
6-
// Test 1: Basic math module
7-
console.log('📦 Test 1: Math module');
8-
const mathModule = await import('./math.js');
9-
console.log(' ✅ math.add(2, 3) =', mathModule.add(2, 3));
10-
console.log(' ✅ math.PI =', mathModule.PI);
11-
console.log(' ✅ Default export square(4) =', mathModule.default(4));
5+
try {
6+
// Test 1: Basic math module
7+
console.log("📦 Test 1: Math module");
8+
const mathModule = await import("./math.js");
9+
console.log(" ✅ math.add(2, 3) =", mathModule.add(2, 3));
10+
console.log(" ✅ math.PI =", mathModule.PI);
11+
console.log(" ✅ Default export square(4) =", mathModule.default(4));
1212

13-
// Test 2: Default export module
14-
console.log('\n🎯 Test 2: Default export');
15-
const defaultModule = await import('./default-export.js');
16-
console.log(' ✅ Default greeting:', defaultModule.default('Andromeda'));
17-
console.log(' ✅ Version:', defaultModule.version);
13+
// Test 2: Default export module
14+
console.log("\n🎯 Test 2: Default export");
15+
const defaultModule = await import("./default-export.js");
16+
console.log(" ✅ Default greeting:", defaultModule.default("Andromeda"));
17+
console.log(" ✅ Version:", defaultModule.version);
1818

19-
// Test 3: TypeScript module
20-
console.log('\n🔀 Test 3: TypeScript module');
21-
const tsModule = await import('./mixed-exports.ts');
22-
console.log(' ✅ API_URL:', tsModule.API_URL);
23-
console.log(' ✅ fetchData:', tsModule.fetchData('users'));
19+
// Test 3: TypeScript module
20+
console.log("\n🔀 Test 3: TypeScript module");
21+
const tsModule = await import("./mixed-exports.ts");
22+
console.log(" ✅ API_URL:", tsModule.API_URL);
23+
console.log(" ✅ fetchData:", tsModule.fetchData("users"));
2424

25-
// Test 4: JSON module
26-
console.log('\n📄 Test 4: JSON module');
27-
const jsonModule = await import('./config.json');
28-
console.log(' ✅ Package name:', jsonModule.default.name);
29-
console.log(' ✅ Features:', jsonModule.default.features);
25+
// Test 4: JSON module
26+
console.log("\n📄 Test 4: JSON module");
27+
const jsonModule = await import("./config.json");
28+
console.log(" ✅ Package name:", jsonModule.default.name);
29+
console.log(" ✅ Features:", jsonModule.default.features);
3030

31-
console.log('\n🎉 All tests completed successfully!');
32-
} catch (error) {
33-
console.error('❌ Test failed:', error.message);
34-
}
31+
console.log("\n🎉 All tests completed successfully!");
32+
} catch (error) {
33+
console.error("❌ Test failed:", error.message);
34+
}
3535
}
3636

3737
// Use async/await pattern instead of multiple concurrent imports

examples/streams.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
const stream1 = new ReadableStream({
2+
start(controller) {
3+
console.log(" ReadableStream started");
4+
controller.enqueue("Hello");
5+
controller.enqueue(" ");
6+
controller.enqueue("World!");
7+
controller.close();
8+
console.log(" ReadableStream data enqueued and closed");
9+
},
10+
});
11+
12+
const _reader = stream1.getReader();
13+
14+
const chunks: string[] = [];
15+
const stream2 = new WritableStream({
16+
start(_controller) {
17+
console.log(" WritableStream started");
18+
},
19+
write(chunk, _controller) {
20+
console.log(" WritableStream received chunk:", chunk);
21+
if (typeof chunk === "string") {
22+
chunks.push(chunk);
23+
} else if (chunk instanceof Uint8Array) {
24+
const text = new TextDecoder().decode(chunk);
25+
chunks.push(text);
26+
}
27+
},
28+
close() {
29+
console.log(" WritableStream closed");
30+
},
31+
});
32+
33+
const _writer = stream2.getWriter();
34+
35+
const strategy1 = new CountQueuingStrategy({ highWaterMark: 5 });
36+
console.log(" Strategy highWaterMark:", strategy1.highWaterMark);
37+
console.log(" Size of 'test':", strategy1.size("test"));
38+
console.log(" Size of [1,2,3]:", strategy1.size([1, 2, 3]));
39+
40+
const strategy2 = new ByteLengthQueuingStrategy({ highWaterMark: 1024 });
41+
console.log(" Strategy highWaterMark:", strategy2.highWaterMark);
42+
43+
const testBuffer = new Uint8Array([1, 2, 3, 4, 5]);
44+
console.log(" Size of Uint8Array(5):", strategy2.size(testBuffer));
45+
46+
const upperCaseTransform = new TransformStream({
47+
transform(chunk, controller) {
48+
console.log(" Transform received chunk:", chunk);
49+
if (typeof chunk === "string") {
50+
controller.enqueue(chunk.toUpperCase());
51+
} else if (chunk instanceof Uint8Array) {
52+
const text = new TextDecoder().decode(chunk);
53+
const upperText = text.toUpperCase();
54+
const encoder = new TextEncoder();
55+
controller.enqueue(encoder.encode(upperText));
56+
}
57+
},
58+
});
59+
60+
console.log(" TransformStream created successfully");
61+
console.log(" Readable side:", upperCaseTransform.readable);
62+
console.log(" Writable side:", upperCaseTransform.writable);

0 commit comments

Comments
 (0)