diff --git a/package.json b/package.json index 5ba07c6cf37..a733f95a5aa 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "build:dev": "yarn clean && git rev-parse HEAD > git-revision.txt && yarn build:compile && yarn build:types", "build:types": "tsc -p tsconfig-build.json --emitDeclarationOnly", "build:compile": "babel -d lib --verbose --extensions \".ts,.js\" src", - "build:compile-browser": "mkdirp dist && browserify -d src/browser-index.js -p [ tsify -p ./tsconfig-build.json ] -t [ babelify --sourceMaps=inline --presets [ @babel/preset-env @babel/preset-typescript ] ] | exorcist dist/browser-matrix.js.map > dist/browser-matrix.js", + "build:compile-browser": "mkdirp dist && browserify -d src/browser-index.ts -p [ tsify -p ./tsconfig-build.json ] -t [ babelify --sourceMaps=inline --presets [ @babel/preset-env @babel/preset-typescript ] ] | exorcist dist/browser-matrix.js.map > dist/browser-matrix.js", "build:minify-browser": "terser dist/browser-matrix.js --compress --mangle --source-map --output dist/browser-matrix.min.js", "gendoc": "jsdoc -c jsdoc.json -P package.json", "lint": "yarn lint:types && yarn lint:js", @@ -32,7 +32,7 @@ "main": "./src/index.ts", "browser": "./lib/browser-index.js", "matrix_src_main": "./src/index.ts", - "matrix_src_browser": "./src/browser-index.js", + "matrix_src_browser": "./src/browser-index.ts", "matrix_lib_main": "./lib/index.js", "matrix_lib_typings": "./lib/index.d.ts", "author": "matrix.org", @@ -78,6 +78,7 @@ "@types/bs58": "^4.0.1", "@types/jest": "^26.0.20", "@types/node": "12", + "@types/qs": "^6.9.7", "@types/request": "^2.48.5", "@typescript-eslint/eslint-plugin": "^4.17.0", "@typescript-eslint/parser": "^4.17.0", diff --git a/src/browser-index.js b/src/browser-index.ts similarity index 69% rename from src/browser-index.js rename to src/browser-index.ts index dfa3f4d68b2..625de982531 100644 --- a/src/browser-index.js +++ b/src/browser-index.ts @@ -16,9 +16,23 @@ limitations under the License. import * as matrixcs from "./matrix"; import request from "browser-request"; -import queryString from "qs"; +import queryString, { IStringifyOptions } from "qs"; -matrixcs.request(function(opts, fn) { +interface RequestOptions { + qs: string | Record; + qsStringifyOptions: IStringifyOptions; +} + +enum IndexedDBStoreName { + crypto = "matrix-js-sdk:crypto" +} + +type GlobalObject = NodeJS.Global & typeof globalThis & Record<"matrixcs", typeof matrixcs>; + +matrixcs.request(function( + opts: RequestOptions, + fn: (er: Error, response: any, body: any) => any, +) { // We manually fix the query string for browser-request because // it doesn't correctly handle cases like ?via=one&via=two. Instead // we mimic `request`'s query string interface to make it all work @@ -31,24 +45,23 @@ matrixcs.request(function(opts, fn) { // just *accessing* indexedDB throws an exception in firefox with // indexeddb disabled. -let indexedDB; +let indexedDB: IDBFactory; try { indexedDB = global.indexedDB; } catch (e) {} // if our browser (appears to) support indexeddb, use an indexeddb crypto store. if (indexedDB) { - matrixcs.setCryptoStoreFactory( - function() { - return new matrixcs.IndexedDBCryptoStore( - indexedDB, "matrix-js-sdk:crypto", - ); - }, - ); + matrixcs.setCryptoStoreFactory(function() { + return new matrixcs.IndexedDBCryptoStore( + indexedDB, + IndexedDBStoreName.crypto, + ); + }); } // We export 3 things to make browserify happy as well as downstream projects. // It's awkward, but required. export * from "./matrix"; export default matrixcs; // keep export for browserify package deps -global.matrixcs = matrixcs; +(global as GlobalObject).matrixcs = matrixcs; diff --git a/yarn.lock b/yarn.lock index b57bc4f6ac9..e5502df6909 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1483,6 +1483,11 @@ resolved "https://registry.yarnpkg.com/@types/prettier/-/prettier-2.4.1.tgz#e1303048d5389563e130f5bdd89d37a99acb75eb" integrity sha512-Fo79ojj3vdEZOHg3wR9ksAMRz4P3S5fDB5e/YWZiFnyFQI1WY2Vftu9XoXVVtJfxB7Bpce/QTqWSSntkz2Znrw== +"@types/qs@^6.9.7": + version "6.9.7" + resolved "https://registry.yarnpkg.com/@types/qs/-/qs-6.9.7.tgz#63bb7d067db107cc1e457c303bc25d511febf6cb" + integrity sha512-FGa1F62FT09qcrueBA6qYTrJPVDzah9a+493+o2PCXsesWHIn27G98TsSMs3WPNbZIEj4+VJf6saSFpvD+3Zsw== + "@types/request@^2.48.5": version "2.48.7" resolved "https://registry.yarnpkg.com/@types/request/-/request-2.48.7.tgz#a962d11a26e0d71d9a9913d96bb806dc4d4c2f19"