Skip to content

Commit

Permalink
fix: make it node compatible (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
chronark authored Oct 17, 2023
1 parent 816e865 commit 118f3cf
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 22 deletions.
6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
{
"name": "@upstash/query",
"version": "0.0.1-canary.0",
"version": "0.0.2-canary.2",
"description": "Secondary indexing for Redis",
"repository": {
"type": "git",
"url": "git+https://github.com/upstash/query.git"
},
"module": "./dist/index.js",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"keywords": [
Expand Down Expand Up @@ -42,6 +41,5 @@
},
"dependencies": {
"crypto-js": "^4.1.1"
},
"type": "module"
}
}
8 changes: 8 additions & 0 deletions src/collection_index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,14 @@ describe("delete", () => {
});
const i = c.createIndex({ name: crypto.randomUUID(), terms: ["hello"] });



type X = Parameters<typeof c.createIndex>[0]["terms"]
const x: X = [""]




await c.set("1", { hello: "1" });
await c.set("2", { hello: "2" });
await c.set("3", { hello: "3" });
Expand Down
20 changes: 7 additions & 13 deletions src/collection_index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import type { DotNotation } from "./dot-notation";
import type { EncoderDecoder } from "./encoding";
import { Event, Interceptor } from "./interceptor";
import { ArrToKeys, Data, Document } from "./types";
import sha256 from 'crypto-js/sha256';
import Hex from 'crypto-js/enc-hex';


export type IndexConfig<TData extends Data, TTerms extends DotNotation<TData>[]> = {
name: string;
Expand Down Expand Up @@ -119,22 +122,13 @@ export class Index<TData extends Data, TTerms extends DotNotation<TData>[]> {

private hashTerms = async (terms: Record<ArrToKeys<TTerms>, unknown>): Promise<string> => {
const keys = Object.keys(terms).sort() as TTerms;
const bufs: Uint8Array[] = [];
let buf = ""
for (const key of keys) {
bufs.push(new TextEncoder().encode(key as string));
bufs.push(new TextEncoder().encode(this.enc.encode(terms[key]!)));
}
const buf = new Uint8Array(bufs.reduce((acc, b) => acc + b.length, 0));
let offset = 0;
for (const b of bufs) {
buf.set(b, offset);
offset += b.length;
buf += key as string;
buf += this.enc.encode(terms[key]!)
}
return Hex.stringify(sha256(buf))

const hash = await crypto.subtle.digest("SHA-256", buf);
return Array.from(new Uint8Array(hash))
.map((b) => b.toString(16).padStart(2, "0"))
.join("");
};

public match = async (
Expand Down
2 changes: 1 addition & 1 deletion src/interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class Interceptor<TData extends Data> {
* Returns a function that can be used to unregister the callback.
*/
public listen(event: Event, callback: Callback<TData>): () => void {
const id = crypto.randomUUID();
const id = Math.random().toString()

this.callbacks[event][id] = callback;

Expand Down
7 changes: 3 additions & 4 deletions tsup.config.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { defineConfig } from "tsup";

export default defineConfig({
entry: ["./src/index.ts"],
entry: ["src/index.ts"],
format: ["cjs", "esm"],
splitting: true,
splitting: false,
sourcemap: true,
clean: true,
bundle: true,
dts: true,
external: ["next"],
});
});

0 comments on commit 118f3cf

Please sign in to comment.