use base64 for HTTP headers (#1795)

* use base64 for HTTP headers

* fe for base64 headers

Co-authored-by: Matt Hill <matthewonthemoon@gmail.com>
This commit is contained in:
Aiden McClelland
2022-09-12 13:20:49 -06:00
committed by GitHub
parent 5564154da2
commit 36911d7ed6
5 changed files with 22 additions and 15 deletions

View File

@@ -47,10 +47,10 @@ pub fn db<M: Metadata>(ctx: RpcContext) -> DynMiddleware<M> {
Err(dump) => serde_json::to_vec(&[dump]),
}
.with_kind(crate::ErrorKind::Serialization)?;
Ok::<_, Error>(
url::form_urlencoded::byte_serialize(&json)
.collect::<String>(),
)
Ok::<_, Error>(base64::encode_config(
&json,
base64::URL_SAFE,
))
}
.await
{
@@ -60,10 +60,10 @@ pub fn db<M: Metadata>(ctx: RpcContext) -> DynMiddleware<M> {
Err(e) => res.headers.append(
"X-Patch-Error",
HeaderValue::from_str(
&url::form_urlencoded::byte_serialize(
e.to_string().as_bytes(),
)
.collect::<String>(),
&base64::encode_config(
&e.to_string(),
base64::URL_SAFE,
),
)?,
),
};

View File

@@ -23,6 +23,7 @@
"@start9labs/emver": "^0.1.5",
"aes-js": "^3.1.2",
"ansi-to-html": "^0.7.2",
"base64-js": "^1.5.1",
"cbor": "npm:@jprochazk/cbor@^0.4.9",
"cbor-web": "^8.1.0",
"core-js": "^3.21.1",

View File

@@ -37,6 +37,7 @@
"@start9labs/emver": "^0.1.5",
"aes-js": "^3.1.2",
"ansi-to-html": "^0.7.2",
"base64-js": "^1.5.1",
"cbor": "npm:@jprochazk/cbor@^0.4.9",
"cbor-web": "^8.1.0",
"core-js": "^3.21.1",

View File

@@ -18,6 +18,7 @@ import { AuthService } from '../auth.service'
import { DOCUMENT } from '@angular/common'
import { DataModel } from '../patch-db/data-model'
import { PatchDB, Update } from 'patch-db-client'
import * as Base64 from 'base64-js'
@Injectable()
export class LiveApiService extends ApiService {
@@ -415,15 +416,22 @@ export class LiveApiService extends ApiService {
}
const res = await this.http.rpcRequest<T>(options)
const encoded = res.headers.get('x-patch-updates')
const encodedUpdates = res.headers.get('x-patch-updates')
const encodedError = res.headers.get('x-patch-error')
if (encoded) {
const updates: Update<DataModel>[] = JSON.parse(
decodeURIComponent(encoded),
if (encodedUpdates) {
const decoded = new TextDecoder().decode(
Base64.toByteArray(encodedUpdates),
)
const updates: Update<DataModel>[] = JSON.parse(decoded)
this.patchStream$.next(updates)
}
if (encodedError) {
const error = new TextDecoder().decode(Base64.toByteArray(encodedError))
console.error(error)
}
const rpcRes = res.body
if (isRpcError(rpcRes)) {

View File

@@ -20,9 +20,6 @@
"module": "es2020",
"lib": ["es2020", "dom"],
"paths": {
"stream": ["./node_modules/stream-browserify"],
"crypto": ["./node_modules/crypto-browserify"],
"vm": ["./node_modules/vm-browserify"],
/* These paths are relative to each app base folder */
"@start9labs/marketplace": ["../marketplace/src/public-api"],
"@start9labs/shared": ["../shared/src/public-api"]