123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- {
- async function speedtest(getPromise, count, parallel = 1) {
- const start = performance.now()
- let promiseArr = []
- for (let i = 0; i < count; i++) {
- for (let j = 0; j < parallel; j++) {
- promiseArr.push(getPromise())
- }
- await Promise.all(promiseArr)
- }
- const end = performance.now()
- const duration = +(end - start).toFixed(5)
- const querySpeed = +(count / duration).toFixed(5)
- const queryDuration = +(duration / count).toFixed(5)
- const paralledSpeed = +((count * parallel) / duration).toFixed(5)
- const parallelDuration = +(duration / (count * parallel)).toFixed(5)
- return {
- duration,
- querySpeed,
- queryDuration,
- paralledSpeed,
- parallelDuration
- }
- }
- const delay = ms => new Promise(ok => setTimeout(() => ok(ms), ms))
-
- speedtest(() => delay(1000), 10, 10).then(result => console.log('delay: ', result))
-
-
-
-
-
- speedtest(() => fetch('http://swapi.dev/api/people/1').then(res => res.json()), 10, 5).then(result => console.log('swapi: ', result))
- }
- {
- const gql = function (url, query, variables) {
- let gqlParams = {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json',
- Accept: 'application/json'
- },
- body: JSON.stringify(
- {
- query,
- variables
- }
- )
- }
- return fetch(url, gqlParams).then(res => res.json())
- };
-
- (async () => {
- const catQuery = `query cats($q: String){
- CategoryFind(query: $q){
- _id name
- }
- }`
- const cats = await gql("http://shop-roles.node.ed.asmer.org.ua/graphql", catQuery, { q: "[{}]" })
- console.log(1, cats)
- const loginQuery = `query login($login:String, $password:String){
- login(login:$login, password:$password)
- }`
- const token = await gql("http://shop-roles.node.ed.asmer.org.ua/graphql", loginQuery, { login: "test457", password: "123123" })
- console.log(2, token)
- })()
- }
- {
- const token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOnsiaWQiOiI2MzIyMDVhZWI3NGUxZjVmMmVjMWEzMjAiLCJsb2dpbiI6InRlc3Q0NTciLCJhY2wiOlsiNjMyMjA1YWViNzRlMWY1ZjJlYzFhMzIwIiwidXNlciJdfSwiaWF0IjoxNjY4MjcyMTYzfQ.rxV1ki9G6LjT2IPWcqkMeTi_1K9sb3Si8vLB6UDAGdw"
- const jwtDecode = function (token) {
- try {
- let parseData = token.split('.')[1]
- return JSON.parse(atob(parseData))
- }
- catch (e) {
- return undefined
- }
- }
- console.log(jwtDecode(token))
-
-
-
-
-
-
-
-
-
-
-
- try {
- console.log(jwtDecode())
- console.log(jwtDecode("дичь"))
- console.log(jwtDecode("ey.ey.ey"))
- console.log('до сюда доработало, а значит jwtDecode не матерился в консоль красным цветом')
- }
- finally {
- console.log('ДЗ, видимо, окончено')
- }
- }
|