Skip to content

Commit

Permalink
Merge pull request #63 from maslick/browser-module
Browse files Browse the repository at this point in the history
publish browser, CJS and ESM versions in a single npm package
  • Loading branch information
maslick authored Feb 6, 2022
2 parents edee14d + 10144b9 commit aa36d4f
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ npm install @maslick/koder
```

```javascript
// CommonJS format
// CommonJS
const Koder = require('@maslick/koder');

// ES6 modules
Expand Down
35 changes: 35 additions & 0 deletions dist/browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class Koder {
initialize(config) {
return (async () => {
// Load WASM file
console.log("Browser");
config ||= {};
const directory = config.wasmDirectory || "./wasm";
this.mod = await CreateKoder({locateFile: file => `${directory}/${file}`});

// Initialize a glue API object (between JavaScript and C++ code)
this.api = {
createBuffer: this.mod.cwrap('createBuffer', 'number', ['number']),
deleteBuffer: this.mod.cwrap('deleteBuffer', '', ['number']),
triggerDecode: this.mod.cwrap('triggerDecode', 'number', ['number', 'number', 'number']),
getScanResults: this.mod.cwrap('getScanResults', 'number', [])
};

// return the class
return this;
})();
}

decode(imgData, width, height) {
const buffer = this.api.createBuffer(width * height * 4);
this.mod.HEAPU8.set(imgData, buffer);
const results = [];
if (this.api.triggerDecode(buffer, width, height) > 0) {
const resultAddress = this.api.getScanResults();
results.push(this.mod.UTF8ToString(resultAddress));
this.api.deleteBuffer(resultAddress);
}
if (results.length > 0) return results[0];
else return null;
}
}
1 change: 1 addition & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class Koder {
get initialized() {
return (async () => {
// Load WASM file
console.log("CJS");
this.mod = await CreateKoder();

// Initialize a glue API object (between JavaScript and C++ code)
Expand Down
37 changes: 37 additions & 0 deletions dist/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import CreateKoder from './zbar.js';

class Koder {
get initialized() {
return (async () => {
// Load WASM file
console.log("ESM");
this.mod = await CreateKoder();

// Initialize a glue API object (between JavaScript and C++ code)
this.api = {
createBuffer: this.mod.cwrap('createBuffer', 'number', ['number']),
deleteBuffer: this.mod.cwrap('deleteBuffer', '', ['number']),
triggerDecode: this.mod.cwrap('triggerDecode', 'number', ['number', 'number', 'number']),
getScanResults: this.mod.cwrap('getScanResults', 'number', [])
};

// return the class
return this;
})();
}

decode(imgData, width, height) {
const buffer = this.api.createBuffer(width * height * 4);
this.mod.HEAPU8.set(imgData, buffer);
const results = [];
if (this.api.triggerDecode(buffer, width, height) > 0) {
const resultAddress = this.api.getScanResults();
results.push(this.mod.UTF8ToString(resultAddress));
this.api.deleteBuffer(resultAddress);
}
if (results.length > 0) return results[0];
else return null;
}
}

export default Koder;
9 changes: 7 additions & 2 deletions dist/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@maslick/koder",
"version": "1.2.1",
"version": "1.3.0",
"description": "QR/bar code scanner for the Browser",
"author": "Pavel Maslov",
"license": "MIT",
Expand Down Expand Up @@ -29,5 +29,10 @@
"upca",
"upce"
],
"main": "index.js"
"exports": {
".": {
"require": "./index.cjs",
"import": "./index.mjs"
}
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "koder-react",
"version": "1.7.0",
"version": "1.8.0",
"homepage": "./",
"private": true,
"description": "QR/bar code scanner for the Browser",
Expand Down
9 changes: 5 additions & 4 deletions public/wasm/koder.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
class Koder {
get initialized() {
initialize(config) {
return (async () => {
// Load WASM file
this.mod = await CreateKoder({
locateFile: file => `wasm/${file}`,
});
console.log("Browser");
config ||= {};
const directory = config.wasmDirectory || "./wasm";
this.mod = await CreateKoder({locateFile: file => `${directory}/${file}`});

// Initialize a glue API object (between JavaScript and C++ code)
this.api = {
Expand Down
2 changes: 1 addition & 1 deletion public/wasmWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ importScripts("wasm/koder.js");

(async () => {
// Initialize Koder
const koder = await new Koder().initialized;
const koder = await new Koder().initialize({wasmDirectory: "./wasm"});

// Listen for messages from JS main thread containing raw image data
self.addEventListener('message', event => {
Expand Down

0 comments on commit aa36d4f

Please sign in to comment.