@based/client
Advanced tools
Comparing version 5.0.0-alpha.1 to 5.0.0-alpha.2
@@ -1,5 +0,5 @@ | ||
import { createEncoder } from '@saulx/utils'; | ||
import { createEncoder, encodeBase64, decodeBase64 } from '@saulx/utils'; | ||
export const decodeAuthState = (authState) => { | ||
try { | ||
const str = global.atob(decodeURI(authState)); | ||
const str = new TextDecoder().decode(decodeBase64(decode(authState))); | ||
return JSON.parse(str); | ||
@@ -21,3 +21,3 @@ } | ||
// | HT (what is this?) | ||
const { encode } = createEncoder([ | ||
const { encode, decode } = createEncoder([ | ||
'(', | ||
@@ -43,4 +43,4 @@ ')', | ||
export const encodeAuthState = (authState) => { | ||
return encodeURI(encode(global.btoa(String.fromCodePoint(...new TextEncoder().encode(JSON.stringify(authState)))))); | ||
return encode(encodeBase64(new TextEncoder().encode(JSON.stringify(authState)))); | ||
}; | ||
//# sourceMappingURL=parseAuthState.js.map |
import { setStorage, removeStorage } from '../persistentStorage/index.js'; | ||
import { CACHE_AUTH } from '../persistentStorage/constants.js'; | ||
export const updateAuthState = (client, authState) => { | ||
if (authState.persistent) { | ||
setStorage(client, '@based-authState', authState); | ||
setStorage(client, CACHE_AUTH, authState); | ||
} | ||
else { | ||
removeStorage(client, '@based-authState'); | ||
removeStorage(client, CACHE_AUTH); | ||
} | ||
@@ -9,0 +10,0 @@ client.authState = authState; |
import { inflateSync } from 'fflate'; | ||
// add diff to the bundle as well | ||
import { applyPatch } from '@saulx/diff'; | ||
@@ -4,0 +3,0 @@ import { deepEqual } from '@saulx/utils'; |
import { inflateSync, deflateSync } from 'fflate'; | ||
import { CACHE_PREFIX, CACHE_SIZE, CACHE_NAME, CACHE_AUTH, } from './constants.js'; | ||
const decoder = new TextDecoder(); | ||
import { encodeBase64, decodeBase64 } from '@saulx/utils'; | ||
const decoder = new TextDecoder('utf-8'); | ||
const encoder = new TextEncoder(); | ||
const decode = (dataURI) => { | ||
const data = global.atob(dataURI); | ||
const uncompressed = decoder.decode(inflateSync(encoder.encode(data))); | ||
const uncompressed = decoder.decode(inflateSync(decodeBase64(dataURI))); | ||
const parsed = JSON.parse(uncompressed); | ||
@@ -45,4 +45,4 @@ return parsed; | ||
const stringifiedJson = JSON.stringify(value); | ||
const encoded = stringifiedJson.length > 70 || key === CACHE_AUTH + env | ||
? global.btoa(decoder.decode(deflateSync(encoder.encode(stringifiedJson)))) | ||
const encoded = stringifiedJson.length > 70 || key === CACHE_AUTH + '-' + env | ||
? encodeBase64(deflateSync(encoder.encode(stringifiedJson))) | ||
: stringifiedJson; | ||
@@ -60,3 +60,3 @@ const blob = new Blob([encoded]); | ||
if (client.authState.persistent === true) { | ||
setStorageBrowser(client, CACHE_AUTH + env, client.authState); | ||
setStorageBrowser(client, CACHE_AUTH + '-' + env, client.authState); | ||
} | ||
@@ -69,3 +69,3 @@ client.storageSize += size; | ||
catch (err) { | ||
// console.error(`Based - Error writing ${key} to localStorage`, err) | ||
console.error(`Based - Error writing ${key} to localStorage`, err); | ||
} | ||
@@ -81,3 +81,3 @@ }; | ||
if (value !== undefined) { | ||
if (value.length < 70 && key !== CACHE_AUTH + env) { | ||
if (value.length < 70 && key !== CACHE_AUTH + '-' + env) { | ||
try { | ||
@@ -131,3 +131,3 @@ return JSON.parse(value); | ||
} | ||
if (key === CACHE_AUTH + env) { | ||
if (key === CACHE_AUTH + '-' + env) { | ||
const authState = getStorageBrowser(client, key); | ||
@@ -134,0 +134,0 @@ if (authState) { |
export const CACHE_NAME = '@based'; | ||
export const CACHE_PREFIX = CACHE_NAME + '-cache-'; | ||
export const CACHE_PREFIX = CACHE_NAME + '-cache'; | ||
export const CACHE_SIZE = CACHE_NAME + '-size'; | ||
export const CACHE_AUTH = CACHE_NAME + '-authState-'; | ||
export const CACHE_AUTH = CACHE_NAME + '-authState'; | ||
//# sourceMappingURL=constants.js.map |
@@ -1,6 +0,1 @@ | ||
import { BasedClient } from '../index.js'; | ||
export declare const store: (client: BasedClient) => Promise<void>; | ||
export declare const initStorageNode: (client: BasedClient) => Promise<void>; | ||
export declare const clearStorageNode: (client: BasedClient) => Promise<void>; | ||
export declare const removeStorageNode: (client: BasedClient, _key: string) => void; | ||
export declare const setStorageNode: (client: BasedClient, _key: string, _value: any) => void; | ||
export {}; |
@@ -1,86 +0,109 @@ | ||
import { join } from 'path'; | ||
import gzip from 'zlib'; | ||
import fs from 'fs'; | ||
import { promisify } from 'util'; | ||
const writeFile = promisify(fs.writeFile); | ||
const rm = promisify(fs.rm); | ||
const compress = promisify(gzip.gzip); | ||
export const store = async (client) => { | ||
try { | ||
clearTimeout(client.storageBeingWritten); | ||
client.storageBeingWritten = null; | ||
const file = join(client.storagePath, 'based-' + client.storageEnvKey + '.storage'); | ||
const cache = []; | ||
client.cache.forEach((c, id) => { | ||
if (c.p) { | ||
cache.push(id, c.c, c.v); | ||
} | ||
}); | ||
const f = { cache }; | ||
if (client.authState.persistent) { | ||
f.authState = client.authState; | ||
} | ||
client.storageBeingWritten = null; | ||
await writeFile(file, await compress(JSON.stringify(f))); | ||
} | ||
catch (err) { | ||
console.error(' [Based-client] Cannot update persistent storage', client.storagePath); | ||
} | ||
}; | ||
const writeToStorage = (client) => { | ||
if (!client.storageBeingWritten) { | ||
client.storageBeingWritten = setTimeout(() => store(client), 5e3); | ||
} | ||
}; | ||
export const initStorageNode = async (client) => { | ||
const file = join(client.storagePath, 'based-' + client.storageEnvKey + '.storage'); | ||
try { | ||
const s = fs.statSync(file); | ||
if (s) { | ||
try { | ||
const r = fs.readFileSync(file); | ||
const unpacked = gzip.gunzipSync(r); | ||
const c = JSON.parse(unpacked.toString()); | ||
for (let i = 0; i < c.cache.length - 2; i += 3) { | ||
client.cache.set(c.cache[i], { | ||
c: c.cache[i + 1], | ||
v: c.cache[i + 2], | ||
p: true, | ||
}); | ||
} | ||
if (c.authState) { | ||
client.setAuthState(c.authState).catch(() => { | ||
console.error(' [Based-client] Invalid authState'); | ||
clearStorageNode(client); | ||
}); | ||
} | ||
} | ||
catch (err) { | ||
console.error(' [Based-client] Corrupt persistent storage - clear'); | ||
await clearStorageNode(client); | ||
} | ||
} | ||
else { | ||
const x = await compress(JSON.stringify({ cache: [] })); | ||
await writeFile(file, x).catch((err) => { | ||
if (err) { | ||
console.error(' [Based-client] Failed creating persistent storage, cannot write file'); | ||
client.storagePath = null; | ||
} | ||
}); | ||
} | ||
} | ||
catch (err) { } | ||
}; | ||
export const clearStorageNode = async (client) => { | ||
const file = join(client.storagePath, 'based-' + client.storageEnvKey + '.storage'); | ||
clearTimeout(client.storageBeingWritten); | ||
return rm(file); | ||
}; | ||
export const removeStorageNode = (client, _key) => { | ||
writeToStorage(client); | ||
}; | ||
export const setStorageNode = (client, _key, _value) => { | ||
writeToStorage(client); | ||
}; | ||
// import { AuthState, BasedClient } from '..' | ||
// import { join } from 'path' | ||
// import gzip from 'zlib' | ||
// import fs from 'fs' | ||
// import { promisify } from 'util' | ||
export {}; | ||
// const writeFile = promisify(fs.writeFile) | ||
// const rm = promisify(fs.rm) | ||
// const compress = promisify(gzip.gzip) | ||
// export const store = async (client: BasedClient) => { | ||
// try { | ||
// clearTimeout(client.storageBeingWritten) | ||
// client.storageBeingWritten = null | ||
// const file = join( | ||
// client.storagePath, | ||
// 'based-' + client.storageEnvKey + '.storage' | ||
// ) | ||
// // [id, checksum, value] | ||
// const cache: any[] = [] | ||
// client.cache.forEach((c, id) => { | ||
// if (c.persistent) { | ||
// cache.push(id, c.checksum, c.value) | ||
// } | ||
// }) | ||
// const f: { cache: any[]; authState?: AuthState } = { cache } | ||
// if (client.authState.persistent) { | ||
// f.authState = client.authState | ||
// } | ||
// client.storageBeingWritten = null | ||
// await writeFile(file, await compress(JSON.stringify(f))) | ||
// } catch (err) { | ||
// console.error( | ||
// ' [Based-client] Cannot update persistent storage', | ||
// client.storagePath | ||
// ) | ||
// } | ||
// } | ||
// const writeToStorage = (client: BasedClient) => { | ||
// if (!client.storageBeingWritten) { | ||
// client.storageBeingWritten = setTimeout(() => store(client), 5e3) | ||
// } | ||
// } | ||
// export const initStorageNode = async (client: BasedClient) => { | ||
// const file = join( | ||
// client.storagePath, | ||
// 'based-' + client.storageEnvKey + '.storage' | ||
// ) | ||
// try { | ||
// const s = fs.statSync(file) | ||
// if (s) { | ||
// try { | ||
// const r = fs.readFileSync(file) | ||
// const unpacked = gzip.gunzipSync(r) | ||
// const c = JSON.parse(unpacked.toString()) | ||
// for (let i = 0; i < c.cache.length - 2; i += 3) { | ||
// client.cache.set(c.cache[i], { | ||
// checksum: c.cache[i + 1], | ||
// value: c.cache[i + 2], | ||
// persistent: true, | ||
// }) | ||
// } | ||
// if (c.authState) { | ||
// client.setAuthState(c.authState).catch(() => { | ||
// console.error(' [Based-client] Invalid authState') | ||
// clearStorageNode(client) | ||
// }) | ||
// } | ||
// } catch (err) { | ||
// console.error(' [Based-client] Corrupt persistent storage - clear') | ||
// await clearStorageNode(client) | ||
// } | ||
// } else { | ||
// const x = await compress(JSON.stringify({ cache: [] })) | ||
// await writeFile(file, x).catch((err) => { | ||
// if (err) { | ||
// console.error( | ||
// ' [Based-client] Failed creating persistent storage, cannot write file' | ||
// ) | ||
// client.storagePath = null | ||
// } | ||
// }) | ||
// } | ||
// } catch (err) {} | ||
// } | ||
// export const clearStorageNode = async (client: BasedClient) => { | ||
// const file = join( | ||
// client.storagePath, | ||
// 'based-' + client.storageEnvKey + '.storage' | ||
// ) | ||
// clearTimeout(client.storageBeingWritten) | ||
// return rm(file) | ||
// } | ||
// export const removeStorageNode = ( | ||
// client: BasedClient, | ||
// // eslint-disable-next-line | ||
// key: string | ||
// ) => { | ||
// writeToStorage(client) | ||
// } | ||
// export const setStorageNode = ( | ||
// client: BasedClient, | ||
// // eslint-disable-next-line | ||
// key: string, | ||
// // eslint-disable-next-line | ||
// value: any | ||
// ) => { | ||
// writeToStorage(client) | ||
// } | ||
//# sourceMappingURL=node.js.map |
{ | ||
"name": "@based/client", | ||
"version": "5.0.0-alpha.1", | ||
"version": "5.0.0-alpha.2", | ||
"license": "MIT", | ||
@@ -12,2 +12,3 @@ "scripts": { | ||
"microBuild": "microbundle -i dist/src/index.js -o test/browser/bundle.js --no-pkg-main -f modern --visualize", | ||
"browserBuildConsole": "esbuild ./test/browser/index.ts --bundle --outfile=./test/browser/out.js --minify --metafile=./test/browser/meta.json --define:global=window", | ||
"browserBuild": "esbuild ./test/browser/index.ts --bundle --outfile=./test/browser/out.js --minify --metafile=./test/browser/meta.json --define:global=window --drop:console", | ||
@@ -42,3 +43,3 @@ "browser": "node ./test/browser/server.js" | ||
"@saulx/hash": "^3.0.0", | ||
"@saulx/utils": "^4.0.4", | ||
"@saulx/utils": "^4.1.0", | ||
"@based/fetch": "^2.0.1", | ||
@@ -45,0 +46,0 @@ "@based/opts": "^1.0.0", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
3114
2
8
189696
Updated@saulx/utils@^4.1.0