diff options
author | Florian Dold <florian@dold.me> | 2021-04-07 16:12:40 +0200 |
---|---|---|
committer | Florian Dold <florian@dold.me> | 2021-04-07 16:13:16 +0200 |
commit | 29d710c392c2b28e8c8c2a177c8de40061a58e77 (patch) | |
tree | d259fe5571de9c3a10eaa9548423c4387fd9ec2e | |
parent | 46056c416b51b783d1b9c88385aba0d293021524 (diff) |
fix issue in JSON canonicalization (and move stuff to taler-util)
-rw-r--r-- | packages/taler-util/src/helpers.test.d.ts | 1 | ||||
-rw-r--r-- | packages/taler-util/src/helpers.test.js | 29 | ||||
-rw-r--r-- | packages/taler-util/src/helpers.test.js.map | 1 | ||||
-rw-r--r-- | packages/taler-util/src/helpers.test.ts | 46 | ||||
-rw-r--r-- | packages/taler-util/src/helpers.ts | 54 | ||||
-rw-r--r-- | packages/taler-util/src/index.ts | 21 |
6 files changed, 91 insertions, 61 deletions
diff --git a/packages/taler-util/src/helpers.test.d.ts b/packages/taler-util/src/helpers.test.d.ts new file mode 100644 index 000000000..cb0ff5c3b --- /dev/null +++ b/packages/taler-util/src/helpers.test.d.ts @@ -0,0 +1 @@ +export {}; diff --git a/packages/taler-util/src/helpers.test.js b/packages/taler-util/src/helpers.test.js new file mode 100644 index 000000000..ed3198c85 --- /dev/null +++ b/packages/taler-util/src/helpers.test.js @@ -0,0 +1,29 @@ +/* + This file is part of TALER + (C) 2017 Inria and GNUnet e.V. + + TALER is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + TALER is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ +import test from "ava"; +import * as helpers from "./helpers"; +test("URL canonicalization", (t) => { + // converts to relative, adds https + t.is("https://alice.example.com/exchange/", helpers.canonicalizeBaseUrl("alice.example.com/exchange")); + // keeps http, adds trailing slash + t.is("http://alice.example.com/exchange/", helpers.canonicalizeBaseUrl("http://alice.example.com/exchange")); + // keeps http, adds trailing slash + t.is("http://alice.example.com/exchange/", helpers.canonicalizeBaseUrl("http://alice.example.com/exchange#foobar")); + // Remove search component + t.is("http://alice.example.com/exchange/", helpers.canonicalizeBaseUrl("http://alice.example.com/exchange?foo=bar")); + t.pass(); +}); +//# sourceMappingURL=helpers.test.js.map
\ No newline at end of file diff --git a/packages/taler-util/src/helpers.test.js.map b/packages/taler-util/src/helpers.test.js.map new file mode 100644 index 000000000..1289a5d9d --- /dev/null +++ b/packages/taler-util/src/helpers.test.js.map @@ -0,0 +1 @@ +{"version":3,"file":"helpers.test.js","sourceRoot":"","sources":["helpers.test.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,IAAI,MAAM,KAAK,CAAC;AACvB,OAAO,KAAK,OAAO,MAAM,WAAW,CAAC;AAErC,IAAI,CAAC,sBAAsB,EAAE,CAAC,CAAC,EAAE,EAAE;IACjC,mCAAmC;IACnC,CAAC,CAAC,EAAE,CACF,qCAAqC,EACrC,OAAO,CAAC,mBAAmB,CAAC,4BAA4B,CAAC,CAC1D,CAAC;IAEF,kCAAkC;IAClC,CAAC,CAAC,EAAE,CACF,oCAAoC,EACpC,OAAO,CAAC,mBAAmB,CAAC,mCAAmC,CAAC,CACjE,CAAC;IAEF,kCAAkC;IAClC,CAAC,CAAC,EAAE,CACF,oCAAoC,EACpC,OAAO,CAAC,mBAAmB,CAAC,0CAA0C,CAAC,CACxE,CAAC;IAEF,0BAA0B;IAC1B,CAAC,CAAC,EAAE,CACF,oCAAoC,EACpC,OAAO,CAAC,mBAAmB,CAAC,2CAA2C,CAAC,CACzE,CAAC;IAEF,CAAC,CAAC,IAAI,EAAE,CAAC;AACX,CAAC,CAAC,CAAC"}
\ No newline at end of file diff --git a/packages/taler-util/src/helpers.test.ts b/packages/taler-util/src/helpers.test.ts new file mode 100644 index 000000000..dbecf14b8 --- /dev/null +++ b/packages/taler-util/src/helpers.test.ts @@ -0,0 +1,46 @@ +/* + This file is part of TALER + (C) 2017 Inria and GNUnet e.V. + + TALER is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + TALER is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +import test from "ava"; +import * as helpers from "./helpers"; + +test("URL canonicalization", (t) => { + // converts to relative, adds https + t.is( + "https://alice.example.com/exchange/", + helpers.canonicalizeBaseUrl("alice.example.com/exchange"), + ); + + // keeps http, adds trailing slash + t.is( + "http://alice.example.com/exchange/", + helpers.canonicalizeBaseUrl("http://alice.example.com/exchange"), + ); + + // keeps http, adds trailing slash + t.is( + "http://alice.example.com/exchange/", + helpers.canonicalizeBaseUrl("http://alice.example.com/exchange#foobar"), + ); + + // Remove search component + t.is( + "http://alice.example.com/exchange/", + helpers.canonicalizeBaseUrl("http://alice.example.com/exchange?foo=bar"), + ); + + t.pass(); +}); diff --git a/packages/taler-util/src/helpers.ts b/packages/taler-util/src/helpers.ts index d3093d1b1..282724464 100644 --- a/packages/taler-util/src/helpers.ts +++ b/packages/taler-util/src/helpers.ts @@ -84,57 +84,6 @@ export function canonicalJson(obj: any): string { } /** - * Check for deep equality of two objects. - * Only arrays, objects and primitives are supported. - */ -export function deepEquals(x: any, y: any): boolean { - if (x === y) { - return true; - } - - if (Array.isArray(x) && x.length !== y.length) { - return false; - } - - const p = Object.keys(x); - return ( - Object.keys(y).every((i) => p.indexOf(i) !== -1) && - p.every((i) => deepEquals(x[i], y[i])) - ); -} - -export function deepCopy(x: any): any { - // FIXME: this has many issues ... - return JSON.parse(JSON.stringify(x)); -} - -/** - * Map from a collection to a list or results and then - * concatenate the results. - */ -export function flatMap<T, U>(xs: T[], f: (x: T) => U[]): U[] { - return xs.reduce((acc: U[], next: T) => [...f(next), ...acc], []); -} - -/** - * Compute the hash function of a JSON object. - */ -export function hash(val: any): number { - const str = canonicalJson(val); - // https://github.com/darkskyapp/string-hash - let h = 5381; - let i = str.length; - while (i) { - h = (h * 33) ^ str.charCodeAt(--i); - } - - /* JavaScript does bitwise operations (like XOR, above) on 32-bit signed - * integers. Since we want the results to be always positive, convert the - * signed int to an unsigned by doing an unsigned bitshift. */ - return h >>> 0; -} - -/** * Lexically compare two strings. */ export function strcmp(s1: string, s2: string): number { @@ -147,6 +96,9 @@ export function strcmp(s1: string, s2: string): number { return 0; } +/** + * Shorthand function for formatted JSON stringification. + */ export function j2s(x: any): string { return JSON.stringify(x, undefined, 2); } diff --git a/packages/taler-util/src/index.ts b/packages/taler-util/src/index.ts index 255d6cbc7..3416c7d12 100644 --- a/packages/taler-util/src/index.ts +++ b/packages/taler-util/src/index.ts @@ -2,17 +2,18 @@ import { TalerErrorCode } from "./taler-error-codes.js"; export { TalerErrorCode }; -export * from "./codec.js"; export * from "./amounts.js"; -export * from "./talerconfig.js"; -export * from "./time.js"; -export * from "./walletTypes.js"; -export * from "./transactionsTypes.js"; +export * from "./backupTypes.js"; +export * from "./codec.js"; +export * from "./helpers.js"; +export * from "./libtool-version.js"; export * from "./notifications.js"; -export * from "./talerTypes.js"; -export * from "./taleruri.js"; +export * from "./payto.js"; export * from "./ReserveStatus.js"; export * from "./ReserveTransaction.js"; -export * from "./backupTypes.js"; -export * from "./payto.js"; -export * from "./libtool-version.js";
\ No newline at end of file +export * from "./talerconfig.js"; +export * from "./talerTypes.js"; +export * from "./taleruri.js"; +export * from "./time.js"; +export * from "./transactionsTypes.js"; +export * from "./walletTypes.js";
\ No newline at end of file |