From c8403f39aa0d9f93ab11a45abe6c8a0976db1afc Mon Sep 17 00:00:00 2001 From: Johannes Marbach Date: Fri, 13 Sep 2024 17:35:45 +0200 Subject: [PATCH] Fix build failure in node.js example (#4394) * Fix node.js example Relates to: element-hq/element-web#26922 Signed-off-by: Johannes Marbach * Update examples/node/app.js Co-authored-by: Richard van der Hoff <1389908+richvdh@users.noreply.github.com> * Update examples/node/package.json Co-authored-by: Richard van der Hoff <1389908+richvdh@users.noreply.github.com> * Move imports to the top of the file --------- Co-authored-by: Richard van der Hoff <1389908+richvdh@users.noreply.github.com> --- examples/node/app.js | 27 ++++++++++++++++----------- examples/node/package.json | 3 ++- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/examples/node/app.js b/examples/node/app.js index 5e48e26c404..8f38c34cdbe 100644 --- a/examples/node/app.js +++ b/examples/node/app.js @@ -1,9 +1,15 @@ +import clc from "cli-color"; +import fs from "fs"; +import readline from "readline"; +import sdk, { ClientEvent, EventType, MsgType, RoomEvent } from "matrix-js-sdk"; +import { KnownMembership } from "matrix-js-sdk/lib/@types/membership.js"; + +var myHomeServer = "http://localhost:8008"; var myUserId = "@example:localhost"; var myAccessToken = "QGV4YW1wbGU6bG9jYWxob3N0.qPEvLuYfNBjxikiCjP"; -var sdk = require("matrix-js-sdk"); -var clc = require("cli-color"); + var matrixClient = sdk.createClient({ - baseUrl: "http://localhost:8008", + baseUrl: myHomeServer, accessToken: myAccessToken, userId: myUserId, }); @@ -15,7 +21,6 @@ var numMessagesToShow = 20; // Reading from stdin var CLEAR_CONSOLE = "\x1B[2J"; -var readline = require("readline"); var rl = readline.createInterface({ input: process.stdin, output: process.stdout, @@ -97,7 +102,7 @@ rl.on("line", function (line) { }) .then(function (url) { var content = { - msgtype: "m.file", + msgtype: MsgType.File, body: filename, url: JSON.parse(url).content_uri, }; @@ -138,7 +143,7 @@ rl.on("line", function (line) { // ==== END User input // show the room list after syncing. -matrixClient.on("sync", function (state, prevState, data) { +matrixClient.on(ClientEvent.Sync, function (state, prevState, data) { switch (state) { case "PREPARED": setRoomList(); @@ -149,7 +154,7 @@ matrixClient.on("sync", function (state, prevState, data) { } }); -matrixClient.on("Room", function () { +matrixClient.on(ClientEvent.Room, function () { setRoomList(); if (!viewingRoom) { printRoomList(); @@ -158,7 +163,7 @@ matrixClient.on("Room", function () { }); // print incoming messages. -matrixClient.on("Room.timeline", function (event, room, toStartOfTimeline) { +matrixClient.on(RoomEvent.Timeline, function (event, room, toStartOfTimeline) { if (toStartOfTimeline) { return; // don't print paginated results } @@ -305,7 +310,7 @@ function printRoomInfo(room) { print(eTypeHeader + sendHeader + contentHeader); print(new Array(100).join("-")); eventMap.keys().forEach(function (eventType) { - if (eventType === "m.room.member") { + if (eventType === EventType.RoomMember) { return; } // use /members instead. var eventEventMap = eventMap.get(eventType); @@ -343,7 +348,7 @@ function printLine(event) { name = name.slice(0, maxNameWidth - 1) + "\u2026"; } - if (event.getType() === "m.room.message") { + if (event.getType() === EventType.RoomMessage) { body = event.getContent().body; } else if (event.isState()) { var stateName = event.getType(); @@ -394,4 +399,4 @@ function fixWidth(str, len) { return str; } -matrixClient.startClient(numMessagesToShow); // messages for each room. +matrixClient.startClient({ initialSyncLimit: numMessagesToShow }); diff --git a/examples/node/package.json b/examples/node/package.json index 130fad9214e..df42d05121b 100644 --- a/examples/node/package.json +++ b/examples/node/package.json @@ -5,8 +5,9 @@ "main": "app.js", "author": "", "license": "Apache 2.0", + "type": "module", "dependencies": { "cli-color": "^1.0.0", - "matrix-js-sdk": "^32.0.0" + "matrix-js-sdk": "^34.5.0" } }