Skip to content

Commit

Permalink
removing slow types
Browse files Browse the repository at this point in the history
  • Loading branch information
mimiMonads committed Jun 20, 2024
1 parent d90c21f commit 7bbddcc
Show file tree
Hide file tree
Showing 19 changed files with 201 additions and 195 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/deno.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ jobs:
with:
deno-version: v1.x # Uses latest deno version 1
- run: deno add @cross/test @std/assert # Installs dependencies from jsr.io
- run: deno test -A # Runs tests
- run: deno test -A

- name: Verify formatting
run: deno fmt --check

- name: Verify JSR
run: deno publish --dry-run
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@vixeny/core",
"version": "0.1.37",
"version": "0.1.40",
"exports": "./main.ts",
"imports": {
"@cross/test": "jsr:@cross/test@^0.0.9",
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
{
"name": "vixeny",
"version": "0.1.37",
"version": "0.1.40",
"description": "A functional router for Bun and Deno",
"main": "main.ts",
"type": "module",
"directories": {
"test": "test"
},
"scripts": {
"test": "deno test -A ./test/*"
"test": "deno test -A ./test/*",
"release": "deno test -A ./test/* && deno fmt --check && deno publish --dry-run"
},
"repository": {
"type": "git",
Expand Down
5 changes: 2 additions & 3 deletions src/components/cookieToToken/cookieToTokenGets.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import verifySha256 from "../jwt/verifySha256.mjs";
import type { SupportedKeys } from "../../morphism.ts";
import verifySha256 from "../jwt/verifySha256.ts";

export default (secret: SupportedKeys) => (name: string) =>
export default (secret: Uint8Array) => (name: string) =>
(
(sha256) => (c: string | null) =>
(
Expand Down
59 changes: 32 additions & 27 deletions src/components/cookieToToken/mainCookieToToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,39 @@ import type { Petition, SupportedKeys } from "../../morphism.ts";
import cookieToTokenBodyParser from "./cookieToTokenBodyParser.ts";
import cookieToTokenGets from "./cookieToTokenGets.ts";
import plugins from "../../exportable/plugin.ts";
import composerTools from "../../composer/composerTools.ts";

export const f = (o?: FunRouterOptions<any>) => (p: Petition) =>
p.crypto && "globalKey" in p.crypto
? (
(getCookies) =>
"token" in p.crypto
? ((s: SupportedKeys) => (arr: string[]) =>
arr.reduce(
(acc, x) => acc(cookieToTokenGets(s)(x)),
cookieToTokenBodyParser(arr),
))(p.crypto.globalKey)(
//@ts-ignore
Object.keys(p.crypto.token),
)
: getCookies && getCookies.length > 0
? ((s: SupportedKeys) => (arr: string[]) =>
arr.reduce(
(acc, x) => acc(cookieToTokenGets(s)(x)),
cookieToTokenBodyParser(arr),
))(p.crypto.globalKey)(
getCookies,
)
: void console.error("No token found, please use 'token' ") ??
(() => null)
)(
plugins.pluginIsUsing(p)("token"),
)
: () => null;
export const f =
(o?: FunRouterOptions<any>) =>
(p: Petition): (s: string) => Record<string, string | undefined> =>
p.crypto && "globalKey" in p.crypto
? (
(getCookies) =>
"token" in p.crypto
? ((s: SupportedKeys) => (arr: string[]) =>
arr.reduce(
(acc, x) =>
acc(cookieToTokenGets(composerTools.parsingToHexa(s))(x)),
cookieToTokenBodyParser(arr),
))(p.crypto.globalKey)(
//@ts-ignore
Object.keys(p.crypto.token),
)
: getCookies && getCookies.length > 0
? ((s: SupportedKeys) => (arr: string[]) =>
arr.reduce(
(acc, x) =>
acc(cookieToTokenGets(composerTools.parsingToHexa(s))(x)),
cookieToTokenBodyParser(arr),
))(p.crypto.globalKey)(
getCookies,
)
: void console.error("No token found, please use 'token' ") ??
((_: string) => null)
)(
plugins.pluginIsUsing(p)("token"),
)
: () => null;

export const isUsing = (o?: FunRouterOptions<any>) => (p: Petition): string =>
(
Expand Down
2 changes: 1 addition & 1 deletion src/components/encode/b64toUrl.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import bun from "./src/base64toUrl/convertBase64ToBase64urlBun.mjs";
import deno from "./src/base64toUrl/convertBase64ToBase64urlDeno.mjs";
import node from "./src/base64toUrl/convertBase64ToBase64urlNode.mjs";
import name from "../runtime/name.mjs";
import name from "../runtime/name.js";

/**
* Selects and returns a function to convert Base64 strings to Base64Url strings based on the runtime environment.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
import signer from "./src/sign/sha256.mjs";
import signer from "./src/sign/sha256.ts";
import nodeCrypto from "node:crypto";
import BufferProto from "node:buffer";
import name from "../runtime/name.mjs";
import args from "../runtime/name.ts";

type HashFunction = (data: Uint8Array) => nodeCrypto.Hash;

export default () =>
(
(rt) =>
signer(
//@ts-ignore
rt === "Bun" ? Buffer : BufferProto.Buffer,
)(
rt === "Bun"
//@ts-ignore
? (d) => new Bun.CryptoHasher("sha256").update(d)
: (d) => nodeCrypto.createHash("sha256").update(d),
)
)(
name(),
args(),
)();
79 changes: 0 additions & 79 deletions src/components/jwt/src/sign/sha256.mjs

This file was deleted.

44 changes: 44 additions & 0 deletions src/components/jwt/src/sign/sha256.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import type BufferProto from "node:buffer";
import type nodeCrypto from "node:crypto";

type HashFunction = (data: Uint8Array) => nodeCrypto.Hash;

export default (Buffer: typeof BufferProto.Buffer) =>
(sha256: HashFunction) =>
(
header = Buffer.from(JSON.stringify({
alg: "HS256",
typ: "JWT",
})).toString("base64url") + ".",
) =>
(key: Uint8Array) =>
(
(hmac) =>
((lf) => (rg: Uint8Array) => (message: Object) =>
(
(json) =>
header + json + "." +
sha256(
Buffer.concat([
lf,
sha256(
Buffer.concat([
rg,
Buffer.from(header + json),
]),
).digest(),
]),
)
.digest().toString("base64url")
)(
Buffer.from(JSON.stringify(message)).toString("base64url"),
))(new Uint8Array(64).map((_x, i) => hmac[i] ^ 0x5c))(
new Uint8Array(64).map((_x, i) => hmac[i] ^ 0x36),
)
)(
key.length > 64
? sha256(key).digest()
: key.length < 64
? Buffer.concat([key, Buffer.alloc(64 - key.length)])
: key,
);
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
export default (Buffer) => (hash) => (key) =>
import type BufferProto from "node:buffer";
import type nodeCrypto from "node:crypto";

type HashFunction = (data: Uint8Array) => nodeCrypto.Hash;

export default (Buffer: typeof BufferProto.Buffer) =>
(hash: HashFunction) =>
(key: Uint8Array) =>
(
(hmac) =>
(
(lf) => (rg) => (message) =>
(lf) => (rg: Uint8Array) => (message: string) =>
message.substring(message.lastIndexOf(".") + 1) ===
hash(
Buffer.concat([
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
import verify from "./src/verify/sha256.mjs";
import verify from "./src/verify/sha256.ts";
import args from "../runtime/name.ts";
import nodeCrypto from "node:crypto";
import BufferProto from "node:buffer";

args();

export default () =>
(
(rt) =>
verify(
//@ts-ignore
rt === "Bun" ? Buffer : BufferProto.Buffer,
)(
rt === "Bun"
//@ts-ignore
? (d) => new Bun.CryptoHasher("sha256").update(d)
: (d) => nodeCrypto.createHash("sha256").update(d),
: (d: nodeCrypto.BinaryLike) =>
nodeCrypto.createHash("sha256").update(d),
)
)(
typeof Bun !== "undefined"
? "Bun"
: typeof Bun !== "undefined"
? "Deno"
: "Node",
args(),
);
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
*/

export default () =>
//@ts-ignore
typeof Bun !== "undefined"
? "Bun"
//@ts-ignore
: typeof Bun !== "undefined"
? "Deno"
: "Node";
18 changes: 9 additions & 9 deletions src/composer/composerTools.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Petition } from "../morphism.ts";
import type { Petition, SupportedKeys } from "../morphism.ts";
import type { FunRouterOptions } from "../options.ts";
import mainCheck from "./checkPetition/mainCheck.ts";
type RecFunc = (f: Petition) => boolean;
Expand Down Expand Up @@ -34,7 +34,7 @@ const recursiveCheckAsync =
))(
(solver: RecFunc) => (p: Petition): boolean =>
p.f.constructor.name === "AsyncFunction" ||
p.isAsync
p.isAsync
? true
: p.f.constructor.name === "Function" &&
typeof p.resolve === "undefined"
Expand All @@ -56,12 +56,12 @@ const recursiveCheckAsync =
false,
) as unknown as (f: Petition) => boolean;

const parsingToHexa = (crypto: { globalKey: string }): Uint8Array =>
typeof crypto.globalKey === "string"
? /^[0-9a-fA-F]+$/g.test(crypto.globalKey)
? new Uint8Array([...crypto.globalKey].map((x) => x.charCodeAt(0)))
: new Uint8Array([...crypto.globalKey].map((x) => x.charCodeAt(0)))
: crypto.globalKey;
const parsingToHexa = (globalKey: SupportedKeys): Uint8Array =>
typeof globalKey === "string"
? /^[0-9a-fA-F]+$/g.test(globalKey)
? new Uint8Array([...globalKey].map((x) => x.charCodeAt(0)))
: new Uint8Array([...globalKey].map((x) => x.charCodeAt(0)))
: globalKey;

const isUsing = (o?: FunRouterOptions<any>) => (f: Petition): string[] =>
mainCheck(o)(f);
Expand Down Expand Up @@ -109,7 +109,7 @@ const localAsync =
(p: Petition) =>
(elementsUsed: string[]): boolean =>
(
(p.f.constructor.name === "AsyncFunction" ) ||
(p.f.constructor.name === "AsyncFunction") ||
o && o.cyclePlugin && Object.keys(o.cyclePlugin || {})
.some((x) =>
elementsUsed.includes(x)
Expand Down
Loading

0 comments on commit 7bbddcc

Please sign in to comment.