Skip to content

Commit

Permalink
feat: move to pure esm
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack-Works committed Oct 9, 2022
1 parent f3cdc50 commit ef5dfa0
Show file tree
Hide file tree
Showing 27 changed files with 168 additions and 138 deletions.
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@
"name": "@dimensiondev/stego-js",
"version": "0.13.0",
"packageManager": "[email protected]",
"type": "module",
"description": "Steganography, based on frequency domain implemented in JavaScript.",
"repository": "https://github.com/DimensionDev/Stego-JS",
"license": "MIT",
"author": "DimensionDev",
"main": "./cjs/node.js",
"main": "./esm/node.js",
"module": "./esm/node.js",
"browser": "./cjs/dom.js",
"bin": "./cjs/cli/cli.js",
"browser": "./esm/dom.js",
"bin": "./esm/cli/cli.js",
"scripts": {
"prebuild": "pnpm run clean",
"build": "pnpm run build:umd && pnpm run build:dist",
"build:dist": "tsc && tsc -p tsconfig.esm.json",
"build:dist": "tsc",
"build:umd": "rollup -c",
"clean": "rimraf umd cjs esm",
"clean": "rimraf umd esm",
"lint": "eslint . --ext .ts,.tsx",
"prepublishOnly": "pnpm run build",
"test": "vitest"
Expand Down
11 changes: 6 additions & 5 deletions src/cli/cli.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/env node
import meow from 'meow'
import { createReadStream } from 'fs'
import { rs2Buf } from '../utils/helper'
import { encode, decode, AlgorithmVersion } from '../node'
import { rs2Buf } from '../utils/helper.js'
import { encode, decode, AlgorithmVersion } from '../node.js'
import {
CLI_NAME,
DEFAULT_COPIES,
Expand All @@ -13,8 +13,8 @@ import {
DEFAULT_EXHAUST_PIXELS,
DEFAULT_FAKE_MASK_PIXELS,
DEFAULT_ALGORITHM_VERSION,
} from '../constant'
import { normalizeFlags, validateFlags, flags2Options, flags } from './flag'
} from '../constant.js'
import { normalizeFlags, validateFlags, flags2Options, flags } from './flag.js'

const cli = meow(
`Usage
Expand All @@ -23,7 +23,7 @@ const cli = meow(
Options
-h, --help Print help message.
-v, --version Print version message.
-e, --encode Encode message into given image.
-d, --decode Decode message from given image.
Expand Down Expand Up @@ -57,6 +57,7 @@ Examples
{
flags,
inferType: true,
importMeta: import.meta,
},
)

Expand Down
8 changes: 4 additions & 4 deletions src/cli/flag.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { resolve as resolvePath } from 'path'
import { EncodeOptions, DecodeOptions, AlgorithmVersion } from '../utils/stego-params'
import { GrayscaleAlgorithm } from '../utils/grayscale'
import { TransformAlgorithm } from '../utils/transform'
import { EncodeOptions, DecodeOptions, AlgorithmVersion } from '../utils/stego-params.js'
import { GrayscaleAlgorithm } from '../utils/grayscale.js'
import { TransformAlgorithm } from '../utils/transform.js'
import {
DEFAULT_ALGORITHM_VERSION,
DEFAULT_COPIES,
Expand All @@ -13,7 +13,7 @@ import {
TOLERANCE_NOT_SET,
DEFAULT_TOLERANCE,
MAX_TOLERANCE,
} from '../constant'
} from '../constant.js'
import { TypedFlags } from 'meow'

export interface Flags {
Expand Down
14 changes: 6 additions & 8 deletions src/constant.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TransformAlgorithm } from './utils/transform'
import { AlgorithmVersion } from './utils/stego-params'
import { TransformAlgorithm } from './utils/transform.js'
import { AlgorithmVersion } from './utils/stego-params.js'

export const CLI_NAME = 'stego-js'

Expand Down Expand Up @@ -36,12 +36,10 @@ export const DEFAULT_CROP_EDGE_PIXELS = true
export const DEFAULT_ALGORITHM_VERSION = AlgorithmVersion.V2

export const DEFAULT_MASK = [
0x89, 0x50, 0x4e, 0x47, 0xd, 0xa, 0x1a, 0xa, 0x0, 0x0, 0x0, 0xd, 0x49, 0x48, 0x44, 0x52, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0,
0x0, 0x1, 0x1, 0x3, 0x0, 0x0, 0x0, 0x25, 0xdb, 0x56, 0xca, 0x0, 0x0, 0x0, 0x1, 0x73, 0x52, 0x47, 0x42, 0x1, 0xd9,
0xc9, 0x2c, 0x7f, 0x0, 0x0, 0x0, 0x9, 0x70, 0x48, 0x59, 0x73, 0x0, 0x0, 0xb, 0x13, 0x0, 0x0, 0xb, 0x13, 0x1, 0x0,
0x9a, 0x9c, 0x18, 0x0, 0x0, 0x0, 0x3, 0x50, 0x4c, 0x54, 0x45, 0xff, 0xff, 0xff, 0xa7, 0xc4, 0x1b, 0xc8, 0x0, 0x0, 0x0,
0xa, 0x49, 0x44, 0x41, 0x54, 0x78, 0x9c, 0x63, 0x60, 0x0, 0x0, 0x0, 0x2, 0x0, 0x1, 0x48, 0xaf, 0xa4, 0x71, 0x0, 0x0,
0x0, 0x0, 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82,
137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 0, 1, 0, 0, 0, 1, 1, 3, 0, 0, 0, 37, 219, 86, 202,
0, 0, 0, 1, 115, 82, 71, 66, 1, 217, 201, 44, 127, 0, 0, 0, 9, 112, 72, 89, 115, 0, 0, 11, 19, 0, 0, 11, 19, 1, 0,
154, 156, 24, 0, 0, 0, 3, 80, 76, 84, 69, 255, 255, 255, 167, 196, 27, 200, 0, 0, 0, 10, 73, 68, 65, 84, 120, 156, 99,
96, 0, 0, 0, 2, 0, 1, 72, 175, 164, 113, 0, 0, 0, 0, 73, 69, 78, 68, 174, 66, 96, 130,
]

export const SEED = [
Expand Down
16 changes: 8 additions & 8 deletions src/dom.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { proxy } from './utils/expose'
import { imgType } from './utils/helper'
import { preprocessImage } from './utils/image'
import { AlgorithmVersion } from './utils/stego-params'
import * as v1 from './v1'
import * as v2 from './v2'
import { proxy } from './utils/expose.js'
import { imgType } from './utils/helper.js'
import { preprocessImage } from './utils/image.js'
import { AlgorithmVersion } from './utils/stego-params.js'
import * as v1 from './v1/index.js'
import * as v2 from './v2/index.js'

export { imgType as getImageType }
export * from './utils/types'
export * from './constant'
export * from './utils/types.js'
export * from './constant.js'

const { encode, decode } = proxy({
algoithms: { [AlgorithmVersion.V1]: v1, [AlgorithmVersion.V2]: v2 },
Expand Down
16 changes: 8 additions & 8 deletions src/node.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { JsColorType, Transformer } from '@napi-rs/image'
import { proxy } from './utils/expose'
import { imgType } from './utils/helper'
import { preprocessImage } from './utils/image'
import { AlgorithmVersion } from './utils/stego-params'
import * as v1 from './v1'
import * as v2 from './v2'
import { proxy } from './utils/expose.js'
import { imgType } from './utils/helper.js'
import { preprocessImage } from './utils/image.js'
import { AlgorithmVersion } from './utils/stego-params.js'
import * as v1 from './v1/index.js'
import * as v2 from './v2/index.js'

export { imgType as getImageType }
export * from './utils/types'
export * from './constant'
export * from './utils/types.js'
export * from './constant.js'

const { encode, decode } = proxy({
algoithms: { [AlgorithmVersion.V1]: v1, [AlgorithmVersion.V2]: v2 },
Expand Down
10 changes: 8 additions & 2 deletions src/utils/expose.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AlgorithmVersion, DecodeOptions, EncodeOptions } from './stego-params'
import { AlgorithmVersion, DecodeOptions, EncodeOptions } from './stego-params.js'

export interface EncodedImageData {
data: ImageData
Expand All @@ -16,7 +16,13 @@ export interface Methods {
}

interface ProxyOptions {
algoithms: Record<AlgorithmVersion, { encode: Encoder; decode: Decoder }>
algoithms: Record<
AlgorithmVersion,
{
encode: Encoder
decode: Decoder
}
>
methods: Methods
}

Expand Down
2 changes: 1 addition & 1 deletion src/utils/grayscale.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { clamp } from './helper'
import { clamp } from './helper.js'

// more grayscale algorithm:
// http://www.tannerhelland.com/3643/grayscale-image-algorithm-vb6/
Expand Down
11 changes: 5 additions & 6 deletions src/utils/image.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { clamp } from './helper'
import { Options } from './stego-params'
import { Locator, loc2idx, loc2coord } from './locator'
import { clamp } from './helper.js'
import { Options } from './stego-params.js'
import { Locator, loc2idx, loc2coord } from './locator.js'
import { lanczos } from '@rgba-image/lanczos'
import { MAX_WIDTH } from '../constant'
import { MAX_WIDTH } from '../constant.js'

import { transform } from '../utils/transform'
import { transform } from '../utils/transform.js'

export type Pixel = [number, number, number, number]

Expand Down Expand Up @@ -121,7 +121,6 @@ export function updateImgByPixelChannelAt(imgData: ImageData, loc: number, chann

export function updateImgByPixelAt(imgData: ImageData, options: Options, pixel: Pixel, loc: number) {
const { data } = imgData

;[data[loc], data[loc + 1], data[loc + 2], data[loc + 3]] = pixel
}

Expand Down
2 changes: 1 addition & 1 deletion src/utils/locator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Options } from './stego-params'
import { Options } from './stego-params.js'

export interface Locator {
/**
Expand Down
4 changes: 2 additions & 2 deletions src/utils/mask.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Options } from './stego-params'
import { Locator, loc2idx, loc2coord } from './locator'
import { Options } from './stego-params.js'
import { Locator, loc2idx, loc2coord } from './locator.js'

export function isBlockVisibleAt({ data }: ImageData, loc: Locator, options: Options) {
const { size } = options
Expand Down
4 changes: 2 additions & 2 deletions src/utils/stego-params.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GrayscaleAlgorithm } from './grayscale'
import { TransformAlgorithm } from './transform'
import { GrayscaleAlgorithm } from './grayscale.js'
import { TransformAlgorithm } from './transform.js'

export enum AlgorithmVersion {
V1 = 'V1',
Expand Down
6 changes: 3 additions & 3 deletions src/utils/transform.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import FFT from '../fft/index.js'
import * as DCT from '../dct'
import { fastDctLee } from '../dct/fastdct'
import { Options } from './stego-params'
import * as DCT from '../dct/index.js'
import { fastDctLee } from '../dct/fastdct.js'
import { Options } from './stego-params.js'

export enum TransformAlgorithm {
FFT1D = 'FFT1D',
Expand Down
8 changes: 4 additions & 4 deletions src/utils/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type { Decoder, Encoder, EncodedImageData } from './expose'
export { type EncodeOptions, type DecodeOptions, AlgorithmVersion } from './stego-params'
export { GrayscaleAlgorithm } from './grayscale'
export { TransformAlgorithm } from './transform'
export type { Decoder, Encoder, EncodedImageData } from './expose.js'
export { type EncodeOptions, type DecodeOptions, AlgorithmVersion } from './stego-params.js'
export { GrayscaleAlgorithm } from './grayscale.js'
export { TransformAlgorithm } from './transform.js'
6 changes: 3 additions & 3 deletions src/v1/bit.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Options } from '../utils/stego-params'
import { getPos, Accumulator } from './position'
import { Locator } from '../utils/locator'
import { Options } from '../utils/stego-params.js'
import { getPos, Accumulator } from './position.js'
import { Locator } from '../utils/locator.js'

export type Bit = 0 | 1

Expand Down
8 changes: 4 additions & 4 deletions src/v1/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { EncodeOptions } from '../utils/stego-params'
import { encodeImg } from './stego'
import { cropImg } from '../utils/image'
import { EncodeOptions } from '../utils/stego-params.js'
import { encodeImg } from './stego.js'
import { cropImg } from '../utils/image.js'

export { decodeImg as decode } from './stego'
export { decodeImg as decode } from './stego.js'

export async function encode(imgData: ImageData, maskData: ImageData, options: EncodeOptions) {
const { width, height } = imgData
Expand Down
8 changes: 4 additions & 4 deletions src/v1/position.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Options } from '../utils/stego-params'
import { TransformAlgorithm } from '../utils/transform'
import { hashCode, squareCircleIntersect } from '../utils/helper'
import { Locator } from '../utils/locator'
import { Options } from '../utils/stego-params.js'
import { TransformAlgorithm } from '../utils/transform.js'
import { hashCode, squareCircleIntersect } from '../utils/helper.js'
import { Locator } from '../utils/locator.js'

export interface Accumulator {
/**
Expand Down
20 changes: 10 additions & 10 deletions src/v1/stego.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { GrayscaleAlgorithm, grayscale, narrow } from '../utils/grayscale'
import { transform, inverseTransform } from '../utils/transform'
import { cropImg, updateImgByBlock, updateImgByPixel, visitImgByBlock, updateImgByPixelAt } from '../utils/image'
import { mergeBits, createBits, str2bits, setBit, getBit, bits2str, Bit } from './bit'
import { createAcc } from './position'
import { isPixelVisibleAt, isBlockVisibleAt } from '../utils/mask'
import { rand } from '../utils/helper'
import { loc2idx, loc2coord } from '../utils/locator'
import { EncodeOptions, DecodeOptions } from '../utils/stego-params'
import { GrayscaleAlgorithm, grayscale, narrow } from '../utils/grayscale.js'
import { transform, inverseTransform } from '../utils/transform.js'
import { cropImg, updateImgByBlock, updateImgByPixel, visitImgByBlock, updateImgByPixelAt } from '../utils/image.js'
import { mergeBits, createBits, str2bits, setBit, getBit, bits2str, Bit } from './bit.js'
import { createAcc } from './position.js'
import { isPixelVisibleAt, isBlockVisibleAt } from '../utils/mask.js'
import { rand } from '../utils/helper.js'
import { loc2idx, loc2coord } from '../utils/locator.js'
import { EncodeOptions, DecodeOptions } from '../utils/stego-params.js'

export async function encodeImg(imgData: ImageData, maskData: ImageData, options: EncodeOptions) {
const { text, size, narrow: narrowSize, copies, grayscaleAlgorithm, transformAlgorithm, exhaustPixels } = options
Expand All @@ -16,7 +16,7 @@ export async function encodeImg(imgData: ImageData, maskData: ImageData, options
const bits = mergeBits(
createBits(exhaustPixels ? sizeOfBlocks : textBits.length + 8 * copies),
textBits,
createBits(8 * copies).fill(1), // the end of message
createBits(8 * copies).fill(1),
)

if (textBits.length + 8 * copies > sizeOfBlocks) {
Expand Down
Loading

0 comments on commit ef5dfa0

Please sign in to comment.