From 7920e8c4e53d6196602c75e32b564395fc7084c8 Mon Sep 17 00:00:00 2001 From: Vincent Lannurien Date: Sat, 17 Jan 2026 08:33:44 +0100 Subject: [PATCH] initial commit --- LICENSE | 18 ++++++++++++++ README.md | 3 +++ exercice1.js | 7 ++++++ exercice1.ts | 19 +++++++++++++++ exercice2.js | 6 +++++ exercice2.ts | 15 ++++++++++++ exercice3.ts | 52 +++++++++++++++++++++++++++++++++++++++ exercice4.js | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++ exercice5.ts | 62 ++++++++++++++++++++++++++++++++++++++++++++++ 9 files changed, 251 insertions(+) create mode 100644 LICENSE create mode 100644 README.md create mode 100644 exercice1.js create mode 100644 exercice1.ts create mode 100644 exercice2.js create mode 100644 exercice2.ts create mode 100644 exercice3.ts create mode 100644 exercice4.js create mode 100644 exercice5.ts diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..4b549e2 --- /dev/null +++ b/LICENSE @@ -0,0 +1,18 @@ +MIT License + +Copyright (c) 2026 courses + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and +associated documentation files (the "Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the +following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial +portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT +LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO +EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..cf4d770 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# m1-sor-exercises + +quick exercises we do during classes: bits of typescript, http, and more \ No newline at end of file diff --git a/exercice1.js b/exercice1.js new file mode 100644 index 0000000..7fa719f --- /dev/null +++ b/exercice1.js @@ -0,0 +1,7 @@ +function addition(a, b) { + return a + b; +} + +console.log(addition(2, 3)); // attendu : 5 +console.log(addition("2", 3)); // attendu : ? +console.log(addition(true, 3)); // attendu : ? diff --git a/exercice1.ts b/exercice1.ts new file mode 100644 index 0000000..459be02 --- /dev/null +++ b/exercice1.ts @@ -0,0 +1,19 @@ +function addition(a, b) { + return a + b; +} + +console.log(addition(2, 3)); // attendu : 5 +console.log(addition("2", 3)); // attendu : ? +console.log(addition(true, 3)); // attendu : ? + +namespace Typed { + function addition(a: number, b: number): number { + return a + b; + } + + console.log(addition(2, 3)); + // L'argument de type 'string' n'est pas attribuable au paramètre de type 'number'.ts(2345) + console.log(addition("2", 3)); + // L'argument de type 'boolean' n'est pas attribuable au paramètre de type 'number'.ts(2345) + console.log(addition(true, 3)); +} \ No newline at end of file diff --git a/exercice2.js b/exercice2.js new file mode 100644 index 0000000..c6a9f3a --- /dev/null +++ b/exercice2.js @@ -0,0 +1,6 @@ +function afficherUtilisateur(user) { + console.log(`${user.nom} a ${user.age} ans`); +} + +afficherUtilisateur({ nom: "Alice", age: 25 }); +afficherUtilisateur({ nom: "Bob" }); // manque age diff --git a/exercice2.ts b/exercice2.ts new file mode 100644 index 0000000..68314ba --- /dev/null +++ b/exercice2.ts @@ -0,0 +1,15 @@ +interface Utilisateur { + nom: string; + age: number; +} + +function afficherUtilisateur(u: Utilisateur): void { + console.log(`${u.nom} a ${u.age} ans`); +} + +afficherUtilisateur({ nom: "Alice", age: 25 }); +// L'argument de type '{ nom: string; }' n'est pas attribuable +// au paramètre de type 'Utilisateur'. +// La propriété 'age' est absente du type '{ nom: string; }' +// mais obligatoire dans le type 'Utilisateur'.ts(2345) +afficherUtilisateur({ nom: "Bob" }); diff --git a/exercice3.ts b/exercice3.ts new file mode 100644 index 0000000..5804b85 --- /dev/null +++ b/exercice3.ts @@ -0,0 +1,52 @@ +interface Animal { + name: string; +} +interface Dog extends Animal { + bark: () => void; +} +interface Cat extends Animal { + meow: () => void; +} +const makeNoise = (animal: Animal) => { + if (animal instanceof Dog) { + // 'Dog' fait uniquement référence à un type mais s'utilise en tant que valeur ici.ts(2693) + animal.bark(); + } +}; + +namespace TaggedUnion { + interface Dog { + kind: "dog"; // Tagged union + bark: () => void; + } + interface Cat { + kind: "cat"; // Tagged union + meow: () => void; + } + type Animal = Dog | Cat; + const makeNoise = (animal: Animal) => { + if (animal.kind === "dog") animal.bark(); + else animal.meow(); + }; + const dog: Dog = { + kind: "dog", + bark: () => console.log("bark"), + }; + makeNoise(dog); +} + +namespace Classes { + class Dog { + constructor(public name: string, public bark: () => void) {} + } + class Cat { + constructor(public name: string, public meow: () => void) {} + } + type Animal = Dog | Cat; + const makeNoise = (animal: Animal) => { + if (animal instanceof Dog) animal.bark(); + else animal.meow(); + }; + const dog = new Dog("Fido", () => console.log("bark")); + makeNoise(dog); +} \ No newline at end of file diff --git a/exercice4.js b/exercice4.js new file mode 100644 index 0000000..ee35c07 --- /dev/null +++ b/exercice4.js @@ -0,0 +1,69 @@ +function readFile(path, charset) { + throw new Error("Function not implemented."); +} + +function readTextFileIfExists(path) { + return new Promise((resolve, reject) => { + if (!path) { + reject(new Error("No file path provided")); + return; + } + + readFile(path, "utf8") + .then(data => { + if (data.length === 0) { + reject(new Error("File is empty")); // explicit failure + } else { + resolve(data); // successful read + } + }) + .catch(err => reject(err)); // I/O error + }); +} + +readTextFileIfExists("data.txt") + .then(text => console.log("File contents:", text)) + .catch(err => console.error("I/O error:", err.message)); + +try { + const text = await readTextFileIfExists("data.txt"); + console.log("File contents:", text); +} catch (err) { + console.error("I/O error:", err.message); +} + +const files = ["file1.txt", "file2.txt", "file3.txt"]; + +for (const file of files) { + try { + const text = await readTextFileIfExists(file); + console.log("File contents:", text); + } catch (err) { + console.error("I/O error:", err.message); + continue; + } +} + +const otherFiles = ["does_not_exist.txt", "file2.txt", "file3.txt"]; + +const failContents = await Promise.all( + otherFiles.map(readTextFileIfExists) +); +try { + failContents.forEach((text, i) => { + console.log("File contents:", text); + }); +} catch (err) { + console.error("I/O error:", err.message); +} + +const successContents = await Promise.allSettled( + files.map(readTextFileIfExists) +); +successContents.forEach((result, i) => { + if (result.status === "fulfilled") { + console.log("File contents:", result.value); + } else if (result.status === "rejected") { + console.error("I/O error:", result.reason); + } +}); \ No newline at end of file diff --git a/exercice5.ts b/exercice5.ts new file mode 100644 index 0000000..6575e7c --- /dev/null +++ b/exercice5.ts @@ -0,0 +1,62 @@ +const listener = Deno.listen({ port: 8080 }); + +const decoder = new TextDecoder(); +const encoder = new TextEncoder(); + +for await (const conn of listener) { + handleConn(conn); +} + +async function handleConn(conn: Deno.Conn) { + const buf = new Uint8Array(1024); + const bytes = await conn.read(buf); + if (bytes === null) { + conn.close(); + return; + } + + const rawRequest = decoder.decode(buf.subarray(0, bytes)); + + console.log(rawRequest); + + const [ + requestMethod, + requestHost, + requestUserAgent, + requestAccept, + requestContentType, + requestContentLength, + _, + requestBody, + ] = rawRequest.split("\r\n"); + + const [method, path, protocol] = requestMethod.split(" "); + const [, host] = requestHost.split(" "); + const [, userAgent] = requestUserAgent.split(" "); + const [, accept] = requestAccept.split(" "); + const [, contentType] = requestContentType.split(" "); + const [, contentLength] = requestContentLength.split(" "); + + const responseBody = `{` + + `"method": "${method}",` + + `"path": "${path}",` + + `"protocol": "${protocol}",` + + `"host": "${host}",` + + `"user-agent": "${userAgent}",` + + `"accept": "${accept}",` + + `"content-type": "${contentType}",` + + `"content-length": "${contentLength}",` + + `"body": "${requestBody}"` + + `}\n`; + + const response = + "HTTP/1.1 200 OK\r\n" + + "Content-Type: text/json\r\n" + + `Content-Length: ${responseBody.length}\r\n` + + "Connection: close\r\n" + + "\r\n" + + responseBody; + + await conn.write(encoder.encode(response)); + conn.close(); +}