Skip to content

Commit

Permalink
resolver helper (#1867)
Browse files Browse the repository at this point in the history
  • Loading branch information
lolopinto authored Dec 29, 2024
1 parent c13154f commit 85a80c5
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
4 changes: 2 additions & 2 deletions ts/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions ts/src/graphql/node_resolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,11 @@ class SearchResult {
class SearchResultResolver implements NodeResolver {
encode(result: SearchResult) {
const str = `searchResult:${result.data.context}:${result.id}`;
return Buffer.from(str, "ascii").toString("base64");
return btoa(str);
}

async decodeObj(viewer: Viewer, id: string) {
const decoded = Buffer.from(id, "base64").toString("ascii");
const decoded = atob(id);
let parts = decoded.split(":");
if (parts.length != 3) {
return null;
Expand Down
16 changes: 9 additions & 7 deletions ts/src/graphql/node_resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,20 @@ interface loadEnt {
(v: Viewer, nodeType: string, id: ID): Promise<Ent | null>;
}

function encodeHelper(nodeType: string, id: ID): string {
return btoa(`node:${nodeType}:${id}`);
}

export class EntNodeResolver implements NodeResolver {
constructor(private loader: loadEnt) {}

encode(node: Ent): string {
// let's do 3 parts. we take the "node" prefix
const str = `node:${node.nodeType}:${node.id}`;
return Buffer.from(str, "ascii").toString("base64");
return encodeHelper(node.nodeType, node.id);
}

static decode(id: string): ID | null {
const decoded = Buffer.from(id, "base64").toString("ascii");
const decoded = atob(id);
let parts = decoded.split(":");
if (parts.length != 3) {
return null;
Expand All @@ -34,7 +37,7 @@ export class EntNodeResolver implements NodeResolver {
}

mustDecode(id: string): [ID, string] {
const decoded = Buffer.from(id, "base64").toString("ascii");
const decoded = atob(id);
let parts = decoded.split(":");
if (parts.length != 3) {
throw new Error(`invalid id ${id} passed to EntNodeResolver`);
Expand All @@ -43,7 +46,7 @@ export class EntNodeResolver implements NodeResolver {
}

async decodeObj(viewer: Viewer, id: string): Promise<Node | null> {
const decoded = Buffer.from(id, "base64").toString("ascii");
const decoded = atob(id);
let parts = decoded.split(":");
if (parts.length != 3 || parts[0] != "node") {
return null;
Expand Down Expand Up @@ -117,6 +120,5 @@ export function mustDecodeNullableIDFromGQLID(
// This takes an ent and returns the graphql id
export function encodeGQLID(node: Ent): string {
// let's do 3 parts. we take the "node" prefix
const str = `node:${node.nodeType}:${node.id}`;
return Buffer.from(str, "ascii").toString("base64");
return btoa(`node:${node.nodeType}:${node.id}`);
}

3 comments on commit 85a80c5

@Swahvay
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you got atob and btoa backwards. atob is ascii-to-base64.

@lolopinto
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I confirmed that it did what i wanted

> Buffer.from("node:user:4", "ascii").toString("base64");
'bm9kZTp1c2VyOjQ='
> btoa("node:user:4")
'bm9kZTp1c2VyOjQ='
> atob("node:user:4")
Uncaught:
DOMException [InvalidCharacterError]: Invalid character
    at new DOMException (node:internal/per_context/domexception:53:5)
    at __node_internal_ (node:internal/util:670:10)
    at atob (node:buffer:1335:13)
>

@Swahvay
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow you're right. I had that totally backwards. I'm so confused about the naming convention of the function now though... 🤔

Please sign in to comment.