fix issue in JSON canonicalization (and move stuff to taler-util)
This commit is contained in:
parent
46056c416b
commit
29d710c392
1
packages/taler-util/src/helpers.test.d.ts
vendored
Normal file
1
packages/taler-util/src/helpers.test.d.ts
vendored
Normal file
@ -0,0 +1 @@
|
||||
export {};
|
29
packages/taler-util/src/helpers.test.js
Normal file
29
packages/taler-util/src/helpers.test.js
Normal file
@ -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
|
1
packages/taler-util/src/helpers.test.js.map
Normal file
1
packages/taler-util/src/helpers.test.js.map
Normal file
@ -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"}
|
46
packages/taler-util/src/helpers.test.ts
Normal file
46
packages/taler-util/src/helpers.test.ts
Normal file
@ -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();
|
||||
});
|
@ -83,57 +83,6 @@ export function canonicalJson(obj: any): string {
|
||||
return s + "}";
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
@ -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);
|
||||
}
|
||||
|
@ -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";
|
||||
export * from "./talerconfig.js";
|
||||
export * from "./talerTypes.js";
|
||||
export * from "./taleruri.js";
|
||||
export * from "./time.js";
|
||||
export * from "./transactionsTypes.js";
|
||||
export * from "./walletTypes.js";
|
Loading…
Reference in New Issue
Block a user