refactor: combine sub-pkgs into one pkg

This commit is contained in:
2025-07-12 14:31:14 +02:00
parent da474d7523
commit 8db56a62e4
20 changed files with 40 additions and 106 deletions

View File

@@ -1,11 +1,12 @@
{
"name": "@pow-captcha/shared",
"name": "@pow-captcha/pow-captcha",
"version": "0.0.0",
"main": "dist/lib.js",
"type": "module",
"types": "dist/lib.d.ts",
"scripts": {
"build": "tsc -p tsconfig.build.json"
"build": "tsc -p tsconfig.build.json",
"test": "jest"
},
"dependencies": {
"base64-js": "^1.5.1",

View File

@@ -0,0 +1,5 @@
import * as server from "./server";
import * as solver from "./solver";
import * as wire from "./wire";
export { server, solver, wire };

View File

@@ -1,6 +1,6 @@
import { describe, it, expect } from "@jest/globals";
import { wire } from "@pow-captcha/shared";
import * as server from "./lib";
import * as wire from "./wire";
import * as server from "./server";
const SECRET = "e2c0b4ab-a215-4b36-bba8-19fae1601045";

View File

@@ -1,5 +1,6 @@
import { utils, wire } from "@pow-captcha/shared";
import * as solver from "@pow-captcha/solver";
import * as wire from "./wire";
import * as solver from "./solver";
import { createArray } from "./utils";
export type CreateChallengesOptions = {
/** @default 50 */
@@ -15,21 +16,15 @@ export async function createChallengesRaw({
challengeLength = 16,
difficulty = 2,
}: CreateChallengesOptions): Promise<wire.Challenge> {
const challenges = utils.createArray(
challengeCount,
(): wire.ChallengeEntry => {
const challenge = new Uint8Array(challengeLength);
crypto.getRandomValues(challenge);
const challenges = createArray(challengeCount, (): wire.ChallengeEntry => {
const challenge = new Uint8Array(challengeLength);
crypto.getRandomValues(challenge);
const target = new Uint8Array(difficulty);
crypto.getRandomValues(target);
const target = new Uint8Array(difficulty);
crypto.getRandomValues(target);
return [
wire.serializeArray(challenge),
wire.serializeArray(target),
];
},
);
return [wire.serializeArray(challenge), wire.serializeArray(target)];
});
return {
magic: wire.CHALLENGE_MAGIC,
@@ -41,10 +36,8 @@ export async function createChallenges(
options: CreateChallengesOptions,
secret: string,
): Promise<wire.SignedData> {
return await wire.serializeAndSignData(
createChallengesRaw(options),
secret,
);
const data = await createChallengesRaw(options);
return await wire.serializeAndSignData(data, secret);
}
export async function redeemChallengeSolutionRaw(

View File

@@ -1,5 +1,5 @@
import { describe, it, expect } from "@jest/globals";
import * as solver from "./lib";
import * as solver from "./solver";
describe("solver", () => {
it("solveJs", async () => {

View File

@@ -1,3 +1,5 @@
import { arrayStartsWith } from "./utils";
export async function solveJs(
nonce: Uint8Array,
target: Uint8Array,
@@ -42,16 +44,3 @@ export async function verify(
return arrayStartsWith(hash, target);
}
function arrayStartsWith(array: Uint8Array, search: Uint8Array): boolean {
const searchLen = search.length;
if (searchLen > array.length) {
return false;
}
for (let i = 0; i < searchLen; i += 1) {
if (array[i] !== search[i]) {
return false;
}
}
return true;
}

View File

@@ -17,3 +17,16 @@ export function byteArraysEqual(arr1: Uint8Array, arr2: Uint8Array): boolean {
}
return true;
}
export function arrayStartsWith(array: Uint8Array, search: Uint8Array): boolean {
const searchLen = search.length;
if (searchLen > array.length) {
return false;
}
for (let i = 0; i < searchLen; i += 1) {
if (array[i] !== search[i]) {
return false;
}
}
return true;
}

View File

@@ -1,15 +0,0 @@
{
"name": "@pow-captcha/server",
"version": "0.0.0",
"main": "dist/lib.js",
"type": "module",
"types": "dist/lib.d.ts",
"scripts": {
"build": "tsc -p tsconfig.build.json",
"test": "jest"
},
"dependencies": {
"@pow-captcha/solver": "workspace:*",
"@pow-captcha/shared": "workspace:*"
}
}

View File

@@ -1,2 +0,0 @@
export * as wire from "./wire";
export * as utils from "./utils";

View File

@@ -1,8 +0,0 @@
{
"extends": "../../tsconfig.build.json",
"compilerOptions": {
"outDir": "dist"
},
"include": ["src/**/*"],
"exclude": ["**/*.spec.ts"]
}

View File

@@ -1,4 +0,0 @@
{
"extends": "../../tsconfig.base.json",
"include": ["src/**/*"]
}

View File

@@ -1,6 +0,0 @@
/** @type {import('jest').Config} */
export default {
transform: {
"^.+\\.(t|j)sx?$": "@swc/jest",
},
};

View File

@@ -1,11 +0,0 @@
{
"name": "@pow-captcha/solver",
"version": "0.0.0",
"main": "dist/lib.js",
"type": "module",
"types": "dist/lib.d.ts",
"scripts": {
"build": "tsc -p tsconfig.build.json",
"test": "jest"
}
}

View File

@@ -1,8 +0,0 @@
{
"extends": "../../tsconfig.build.json",
"compilerOptions": {
"outDir": "dist"
},
"include": ["src/**/*"],
"exclude": ["**/*.spec.ts"]
}

View File

@@ -1,4 +0,0 @@
{
"extends": "../../tsconfig.base.json",
"include": ["src/**/*"]
}

13
pnpm-lock.yaml generated
View File

@@ -42,16 +42,7 @@ importers:
specifier: ^8.35.0
version: 8.35.0(eslint@9.29.0)(typescript@5.8.3)
pkgs/server:
dependencies:
'@pow-captcha/shared':
specifier: workspace:*
version: link:../shared
'@pow-captcha/solver':
specifier: workspace:*
version: link:../solver
pkgs/shared:
pkgs/pow-captcha:
dependencies:
base64-js:
specifier: ^1.5.1
@@ -60,7 +51,7 @@ importers:
specifier: ^3.25.67
version: 3.25.67
pkgs/solver: {}
pkgs/solver-wasm: {}
packages: