From 751e64cf367854aa1cc2c16101891d5f75f24116 Mon Sep 17 00:00:00 2001 From: Gunnar Omander Date: Mon, 25 Nov 2024 19:13:47 +0100 Subject: [PATCH] Fix mjs export --- README.md | 4 +- index.mjs | 6 ++- sample/mjs/index.js | 81 +++++++++++++++++++++++++++++++++++++++++ sample/mjs/package.json | 15 ++++++++ sample/mjs/yarn.lock | 12 ++++++ sample/package.json | 2 +- 6 files changed, 116 insertions(+), 4 deletions(-) create mode 100644 sample/mjs/index.js create mode 100644 sample/mjs/package.json create mode 100644 sample/mjs/yarn.lock diff --git a/README.md b/README.md index ed30cda..4284667 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ or submit a PR, read below. ## Developing - Install latest `Rust`. Suggest using [rustup](https://rustup.rs/). If on -Windows, use WSL for an easier time. + Windows, use WSL for an easier time. - Install `NodeJS@18+`. LTS versions suggested. - Install `yarn@1.x`. - Install dependencies with `yarn`. @@ -48,7 +48,7 @@ Windows, use WSL for an easier time. You can then compile the rust code with `yarn build`. After running `yarn build`, you will see a -`napi-gif-encoder.[win32|linux].node` file in the project root. +`napi-gif-encoder..node` file in the project root. This is the native addon built from [lib.rs](./src/lib.rs). ## Try out using sample project diff --git a/index.mjs b/index.mjs index 6e793c8..2e74884 100644 --- a/index.mjs +++ b/index.mjs @@ -1,3 +1,7 @@ import { loadBinding } from '@node-rs/helper' -export default loadBinding(__dirname, 'napi-gif-encoder', '@gomander/napi-gif-encoder') \ No newline at end of file +export const { GIFEncoder } = loadBinding( + import.meta.dirname, + 'napi-gif-encoder', + '@gomander/napi-gif-encoder' +) diff --git a/sample/mjs/index.js b/sample/mjs/index.js new file mode 100644 index 0000000..ce570eb --- /dev/null +++ b/sample/mjs/index.js @@ -0,0 +1,81 @@ +/* eslint-disable no-console */ +import { createReadStream } from 'node:fs' +import { join } from 'node:path' +import { argv } from 'node:process' +import JsGifEncoder from 'gif-encoder-2' +import { PNG } from 'pngjs' +import { GIFEncoder } from '../../index.mjs' + +const imagePaths = [...Array(46).keys()].map((i) => `../BBB${i + 1580}.png`) + +async function loadImage(path) { + return new Promise((resolve, reject) => { + createReadStream(path) + .pipe(new PNG()) + .on('parsed', function () { + resolve({ + buffer: this.data, + width: this.width, + height: this.height, + }) + }) + .on('error', reject) + }) +} + +async function loadImages() { + const promises = imagePaths.map(loadImage) + return await Promise.all(promises) +} + +async function main() { + const images = await loadImages() + try { + const encoder = new GIFEncoder(images[0].width, images[0].height, join(import.meta.dirname, 'output.gif')) + encoder.setFrameRate(30) + encoder.setSampleFactor(2) + // encoder.setRepeat(0) + for (const image of images) { + encoder.addFrame(image.buffer) + } + console.log('Encoding with Rust GIF encoder') + const start = new Date().getTime() + await encoder.finish() + const end = new Date().getTime() + console.log(`Encode time: ${end - start}ms`) + } catch (error) { + console.error(`Unexpected error: ${JSON.stringify(error)}`) + } +} + +class ContextLike { + constructor(buffer) { + this.buffer = buffer + } + + getImageData(sx, sy, sw, sh) { + return { data: this.buffer } + } +} + +async function mainJs() { + const images = await loadImages() + const gif = new JsGifEncoder(images[0].width, images[1].height, 'neuquant', true, 46) + gif.setFrameRate(30) + gif.setRepeat(1) + console.log('Encoding with JavaScript GIF encoder') + const start = new Date().getTime() + gif.start() + for (const image of images) { + gif.addFrame(new ContextLike(image)) + } + gif.finish() + const end = new Date().getTime() + console.log(`Encode time: ${end - start}ms`) +} + +if (argv.includes('--js')) { + mainJs() +} else { + main() +} diff --git a/sample/mjs/package.json b/sample/mjs/package.json new file mode 100644 index 0000000..34b21af --- /dev/null +++ b/sample/mjs/package.json @@ -0,0 +1,15 @@ +{ + "name": "napi-gif-encoder-sample-mjs", + "version": "0.0.7", + "description": "Mjs sample for napi-gif-encoder", + "type": "module", + "module": "index.js", + "repository": "git@github.com:gomander/napi-gif-encoder.git", + "author": "Tyrone Trevorrow ", + "license": "MIT", + "private": false, + "dependencies": { + "gif-encoder-2": "tyrone-sudeium/gif-encoder-2#feature/type_definitions", + "pngjs": "^6.0.0" + } +} diff --git a/sample/mjs/yarn.lock b/sample/mjs/yarn.lock new file mode 100644 index 0000000..b22ac3f --- /dev/null +++ b/sample/mjs/yarn.lock @@ -0,0 +1,12 @@ +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. +# yarn lockfile v1 + + +gif-encoder-2@tyrone-sudeium/gif-encoder-2#feature/type_definitions: + version "1.0.5" + resolved "https://codeload.github.com/tyrone-sudeium/gif-encoder-2/tar.gz/a7aa7760d2af8a7c4dfbce788f4767233f8fe99e" + +pngjs@^6.0.0: + version "6.0.0" + resolved "https://registry.yarnpkg.com/pngjs/-/pngjs-6.0.0.tgz#ca9e5d2aa48db0228a52c419c3308e87720da821" + integrity sha512-TRzzuFRRmEoSW/p1KVAmiOgPco2Irlah+bGFCeNfJXxxYGwSw7YwAOAcd7X28K/m5bjBWKsC29KyoMfHbypayg== diff --git a/sample/package.json b/sample/package.json index e6d83a5..6ba1230 100644 --- a/sample/package.json +++ b/sample/package.json @@ -1,6 +1,6 @@ { "name": "napi-gif-encoder-sample", - "version": "0.0.4", + "version": "0.0.7", "description": "Sample for napi-gif-encoder", "main": "index.js", "repository": "git@github.com:gomander/napi-gif-encoder.git",