Skip to content

Commit

Permalink
Don't register the message handler with null (#160)
Browse files Browse the repository at this point in the history
* Don't register the message handler with null

* Fully functioning bugfix

But jest throws a type error. TSC is fine and browsers run it fine, but
jsdom+jest blows up

* Split worker from its functions so tests compile
  • Loading branch information
skeet70 authored Jun 12, 2024
1 parent 56f2a7b commit 8cff90d
Show file tree
Hide file tree
Showing 10 changed files with 330 additions and 340 deletions.
1 change: 0 additions & 1 deletion globals.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ interface Window {
msCrypto: Crypto;
}


/*
* Declare types for modules without build in types
*/
Expand Down
4 changes: 2 additions & 2 deletions integration/components/InitializeApi.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export default class InitializeApi extends React.Component<InitializeApiProps, I
};

handleEnter = (event: React.KeyboardEvent<HTMLInputElement>) => {
if (event.charCode === 13) {
if (event.key === "Enter") {
this.setPasscode();
}
};
Expand All @@ -60,7 +60,7 @@ export default class InitializeApi extends React.Component<InitializeApiProps, I
}
};

generateJWT(): Promise<string> {
async generateJWT(): Promise<string> {
return fetch(`/generateJWT/${window.User.id}`)
.then((response) => response.json())
.then((jwt: string) => {
Expand Down
4 changes: 0 additions & 4 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ module.exports = {
diagnostics: {
ignoreCodes: [151001],
},
//This can be removed once https://github.com/kulshekhar/ts-jest/issues/1471 is released, probably in ts-jest 25.3.0
tsconfig: {
outDir: "$$ts-jest$$",
},
},
},
testPathIgnorePatterns: ["/node_modules/", "/nightwatch/", "/protobuf/"],
Expand Down
146 changes: 73 additions & 73 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,75 +1,75 @@
{
"license": "AGPL-3.0-only",
"version": "4.2.22",
"scripts": {
"cleanTest": "find dist -type d -name tests -prune -exec rm -rf {} \\;",
"lint": "eslint . --ext .ts,.tsx",
"test": "tsc --noEmit && yarn run lint && yarn run unit --coverage",
"unit": "jest",
"watch": "tsc --noEmit --watch",
"start": "webpack serve --color --config integration/clientHost.webpack.js &! webpack serve --color --config integration/iclHost.webpack.js",
"protocompile": "pbjs -t static-module -w es6 --no-create --no-decode --no-verify --no-convert -o src/frame/protobuf/EncryptedDeks.js src/frame/protobuf/edeks.proto",
"protots": "pbts -o src/frame/protobuf/EncryptedDeks.d.ts src/frame/protobuf/EncryptedDeks.js",
"protobuild": "yarn run protocompile && yarn run protots",
"integrate": "yarn start",
"nightwatch": "nightwatch"
},
"dependencies": {
"@ironcorelabs/recrypt-wasm-binding": "0.6.0",
"@stablelib/ed25519": "1.0.2",
"@stablelib/utf8": "1.0.1",
"base64-js": "1.5.1",
"fast-text-encoding": "1.0.4",
"futurejs": "2.2.0",
"sjcl": "1.0.8"
},
"peerDependencies": {
"@stablelib/utf8": "^0.10",
"base64-js": "^1.2",
"futurejs": "^2"
},
"devDependencies": {
"@types/base64-js": "^1.2.5",
"@types/jest": "^28.1.6",
"@types/material-ui": "^0.21.7",
"@types/react": "^18.0.0",
"@types/react-dom": "^18.0.0",
"@types/react-tap-event-plugin": "^0.0.30",
"@types/sjcl": "^1.0.28",
"@types/webpack-env": "^1.16.0",
"@typescript-eslint/eslint-plugin": "^5.33.0",
"@typescript-eslint/parser": "^5.33.0",
"animal-id": "^0.0.1",
"chromedriver": "^119.0.1",
"cookie-parser": "^1.4.4",
"eslint": "^8.21.0",
"eslint-plugin-react": "^7.19.0",
"express": "^4.19.2",
"jest": "^28.1.3",
"jest-environment-jsdom": "^28.1.3",
"jest-extended": "^3.0.2",
"jsonwebtoken": "^9.0.0",
"material-ui": "^0.20.2",
"nightwatch": "^2.3.0",
"protobufjs": "^7.2.5",
"protobufjs-cli": "^1.0.2",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"shelljs": "^0.8.5",
"ts-jest": "^28.0.0",
"ts-loader": "^9.3.1",
"typescript": "4.7.4",
"webpack": "^5.76.0",
"webpack-cli": "^4.7.0",
"webpack-dev-server": "^4.10.0",
"worker-loader": "^3.0.8"
},
"prettier": {
"printWidth": 160,
"tabWidth": 4,
"trailingComma": "es5",
"bracketSpacing": false,
"jsxBracketSameLine": true,
"arrowParens": "always"
}
"license": "AGPL-3.0-only",
"version": "4.2.22",
"scripts": {
"cleanTest": "find dist -type d -name tests -prune -exec rm -rf {} \\;",
"lint": "eslint . --ext .ts,.tsx",
"test": "tsc --noEmit && yarn run lint && yarn run unit --coverage",
"unit": "jest",
"watch": "tsc --noEmit --watch",
"start": "webpack serve --color --config integration/clientHost.webpack.js &! webpack serve --color --config integration/iclHost.webpack.js",
"protocompile": "pbjs -t static-module -w es6 --no-create --no-decode --no-verify --no-convert -o src/frame/protobuf/EncryptedDeks.js src/frame/protobuf/edeks.proto",
"protots": "pbts -o src/frame/protobuf/EncryptedDeks.d.ts src/frame/protobuf/EncryptedDeks.js",
"protobuild": "yarn run protocompile && yarn run protots",
"integrate": "yarn start",
"nightwatch": "nightwatch"
},
"dependencies": {
"@ironcorelabs/recrypt-wasm-binding": "0.6.0",
"@stablelib/ed25519": "1.0.2",
"@stablelib/utf8": "1.0.1",
"base64-js": "1.5.1",
"fast-text-encoding": "1.0.4",
"futurejs": "2.2.0",
"sjcl": "1.0.8"
},
"peerDependencies": {
"@stablelib/utf8": "^0.10",
"base64-js": "^1.2",
"futurejs": "^2"
},
"devDependencies": {
"@types/base64-js": "^1.2.5",
"@types/jest": "^28.1.6",
"@types/material-ui": "^0.21.7",
"@types/react": "^18.0.0",
"@types/react-dom": "^18.0.0",
"@types/react-tap-event-plugin": "^0.0.30",
"@types/sjcl": "^1.0.28",
"@types/webpack-env": "^1.16.0",
"@typescript-eslint/eslint-plugin": "^5.33.0",
"@typescript-eslint/parser": "^5.33.0",
"animal-id": "^0.0.1",
"chromedriver": "^119.0.1",
"cookie-parser": "^1.4.4",
"eslint": "^8.21.0",
"eslint-plugin-react": "^7.19.0",
"express": "^4.19.2",
"jest": "^28.1.3",
"jest-environment-jsdom": "^28.1.3",
"jest-extended": "^3.0.2",
"jsonwebtoken": "^9.0.0",
"material-ui": "^0.20.2",
"nightwatch": "^2.3.0",
"protobufjs": "^7.2.5",
"protobufjs-cli": "^1.0.2",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"shelljs": "^0.8.5",
"ts-jest": "^28.0.0",
"ts-loader": "^9.3.1",
"typescript": "4.7.4",
"webpack": "^5.76.0",
"webpack-cli": "^4.7.0",
"webpack-dev-server": "^4.10.0",
"worker-loader": "^3.0.8"
},
"prettier": {
"printWidth": 160,
"tabWidth": 4,
"trailingComma": "es5",
"bracketSpacing": false,
"jsxBracketSameLine": true,
"arrowParens": "always"
}
}
5 changes: 0 additions & 5 deletions src/frame/WorkerLoader.ts

This file was deleted.

42 changes: 33 additions & 9 deletions src/frame/WorkerMediator.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,32 @@
import Future from "futurejs";
import {ErrorCodes} from "../Constants";
import SDKError from "../lib/SDKError";
import {ErrorResponse, RequestMessage, ResponseMessage} from "../WorkerMessageTypes";
import worker from "./WorkerLoader";

class WorkerMessenger {
readonly worker: Worker;
callbackCount = 0;
callbacks: {[key: string]: (data: ResponseMessage) => void} = {};
constructor(workerInstance: Worker) {
this.worker = workerInstance;
worker.addEventListener("message", this.processMessage, false);
workerReady: Promise<void>;
constructor() {
this.worker = new Worker(_WORKER_PATH_LOCATION_);

// create a promise that will resolve when we receive a "ready" message from our worker. Messages to the
// worker from this messenger will wait for this Promise to be resolved (which once done will stay resolved)
// before sending.
this.workerReady = new Promise((resolve) => {
this.worker.addEventListener(
"message",
(event: MessageEvent) => {
if (event.data == "ready") {
resolve();
} else {
this.processMessage(event);
}
},
false
);
});
}

/**
Expand All @@ -22,10 +39,17 @@ class WorkerMessenger {
replyID: this.callbackCount++,
data,
};
this.worker.postMessage(message, transferList.map((intByteArray) => intByteArray.buffer));
return new Future<SDKError, ResponseMessage>((_, resolve) => {
this.callbacks[message.replyID] = resolve;
});
return Future.tryP(() => this.workerReady)
.errorMap((e) => new SDKError(e, ErrorCodes.FRAME_LOAD_FAILURE))
.flatMap(() => {
this.worker.postMessage(
message,
transferList.map((intByteArray) => intByteArray.buffer)
);
return new Future<SDKError, ResponseMessage>((_, resolve) => {
this.callbacks[message.replyID] = resolve;
});
});
}

/**
Expand All @@ -42,7 +66,7 @@ class WorkerMessenger {
};
}

export const messenger = new WorkerMessenger(worker);
export const messenger = new WorkerMessenger();

/**
* Type guard to check if returned message is an error message type
Expand Down
Loading

0 comments on commit 8cff90d

Please sign in to comment.