Skip to content
This repository has been archived by the owner on Jun 6, 2024. It is now read-only.

Commit

Permalink
Merge branch 'main' of github.com:schoenbergerb/noscrape
Browse files Browse the repository at this point in the history
  • Loading branch information
schoenbergerb committed Jan 3, 2022
2 parents 34f4bad + 2616b0c commit 835af33
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/font.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Font } from "opentype.js";

export default function generateObfuscatedFont(font, glyphs, translation) {
export default function generateObfuscatedFont(font, glyphs) {
const newFont = new Font({
familyName: "noscrape",
styleName: "obfuscated",
Expand Down
6 changes: 3 additions & 3 deletions src/obfuscate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ObfuscationOptions } from "./obfuscation-options";
import obfuscateGlyphs from "./glyph";
import generateObfuscatedFont from "./font";
import value2glyphs from "./value2glyphs";
import obfuscateObject from "./obfuscate-object";
import obfuscateValue from "./obfuscate/value";

/**
* @param value object which will be translated
Expand All @@ -21,9 +21,9 @@ export default async function obfuscate<T>(

const { translation, glyphs } = obfuscateGlyphs(originalGlyphs, options);

const buffer = generateObfuscatedFont(font, glyphs, translation);
const buffer = generateObfuscatedFont(font, glyphs);

const obj = obfuscateObject(value, translation);
const obj = obfuscateValue(value, translation);

return {
value: obj,
Expand Down
5 changes: 5 additions & 0 deletions src/obfuscate/number.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import obfuscateString from "./string";

export default function obfuscateNumber(n: number, translation) {
return obfuscateString(`${n}`, translation)
}
11 changes: 7 additions & 4 deletions src/obfuscate-object.ts → src/obfuscate/object.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
import _ from "lodash";
import obfuscateString from "./obfuscate-string";
import obfuscateNumber from "./number";
import obfuscateString from "./string";

export default function obfuscateObject<T>(
value: T,
translation: Map<number, number>
): T {
): T {
const obj = _.clone(value);

Object.keys(obj).map((key) => {
switch (typeof obj[key]) {
case "number":
obj[key] = obfuscateNumber(value[key], translation);
break;
case "string":
obj[key] = obfuscateString(`${obj[key]}`, translation);
obj[key] = obfuscateString(value[key], translation);
break;
case "object":
obj[key] = obfuscateObject(obj[key], translation);
obj[key] = obfuscateObject(value[key], translation);
break;
default:
break;
Expand Down
File renamed without changes.
23 changes: 23 additions & 0 deletions src/obfuscate/value.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import _ from "lodash";
import obfuscateNumber from "./number";
import obfuscateObject from "./object";
import obfuscateString from "./string";

export default function obfuscateValue<T>(
value: T | string | number,
translation: Map<number, number>
): T | string | number {

switch (typeof value) {
case "number":
return obfuscateNumber(value, translation);
case "string":
return obfuscateString(value, translation);
case "object":
return obfuscateObject(value, translation);
default:
throw new Error(typeof value + ' could not be obfuscated');
}
}


7 changes: 7 additions & 0 deletions src/value2glyphs.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import _ from "lodash";
import { Font, Glyph } from "opentype.js";



const values = (object) => {

if (typeof object === 'number') {
object = `${object}`
}

return _.flatten(
_.values(object).map((v) => {
switch (typeof v) {
Expand Down
32 changes: 29 additions & 3 deletions test/noscrape.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,29 @@ const demoObject = {

describe("font obfuscation", () => {

it('should render example', async () => {

it("should obfuscate simple number", async () => {

const number = 1234567890

const { font, value } = await obfuscate(number, 'example/example.ttf')

expect(`${value}`).not.toBeNull()
expect(`${value}`).not.toBe(number)
})

it("should obfuscate simple string", async () => {

const simpleString = "noscrape"

const { value } = await obfuscate(simpleString, 'example/example.ttf')

expect(value).not.toBeNull()
expect(value).not.toBe(simpleString)

})

it('should obfuscate object', async () => {

const object = {
string: "noscrape",
Expand All @@ -22,7 +44,7 @@ describe("font obfuscation", () => {
}
}

const { font, value } = await obfuscate(object, 'example/example.ttf', {
const { font, value } = await obfuscate<any>(object, 'example/example.ttf', {
strength: 5
})

Expand All @@ -47,7 +69,7 @@ describe("font obfuscation", () => {
continue
}

const { font, value } = await obfuscate(demoObject, 'example/example.ttf', {
const { value } = await obfuscate<any>(demoObject, 'example/example.ttf', {
characterRange
})

Expand All @@ -57,5 +79,9 @@ describe("font obfuscation", () => {
}
})

it ("should match font size", async () => {
const { font } = await obfuscate(0, 'example/example.ttf')
expect(font.byteLength).toBe(1996)
})

});

0 comments on commit 835af33

Please sign in to comment.