Skip to content

Commit

Permalink
add import example
Browse files Browse the repository at this point in the history
  • Loading branch information
taichunmin committed Apr 1, 2023
1 parent 17b2ac8 commit b3c97e0
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 90 deletions.
51 changes: 51 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,42 @@ See Mtools Tec's [How to make PN532 work on Bluetooth](https://shop.mtoolstec.co

## Installing

### Package manager

Using npm:

```bash
$ npm install pn532.js
```

Using yarn:

```bash
$ yarn add pn532.js
```

Once the package is installed, you can import the library using `import` or `require`:

```js
// import
import { Pn532, Packet, utils: Pn532utils } from 'pn532.js'
import Crypto1 from 'pn532.js/Crypto1.js'
import LoggerRxTx from 'pn532.js/plugin/LoggerRxTx.js'
import Pn532Hf14a from 'pn532.js/plugin/Hf14a.js'
import Pn532WebbleAdapter from 'pn532.js/plugin/WebbleAdapter.js'
import Pn532WebserialAdapter from 'pn532.js/plugin/WebserialAdapter.js'

// require
const { Pn532, Packet, utils: Pn532utils } = require('pn532.js')
const Crypto1 = require('pn532.js/Crypto1.js')
const LoggerRxTx = require('pn532.js/plugin/LoggerRxTx.js')
const Pn532Hf14a = require('pn532.js/plugin/Hf14a.js')
const Pn532WebbleAdapter = require('pn532.js/plugin/WebbleAdapter.js')
const Pn532WebserialAdapter = require('pn532.js/plugin/WebserialAdapter.js')
```

### CDN

Using jsDelivr CDN:

```html
Expand All @@ -62,6 +98,21 @@ Using jsDelivr CDN:
<script src="https://cdn.jsdelivr.net/npm/pn532.js@0/dist/plugin/WebserialAdapter.min.js"></script>
```

After the `script` tag, you can use the `PN532.js` as following:

```js
// setup
const {
_, // lodash: https://cdn.jsdelivr.net/npm/lodash@4/lodash.min.js
Pn532: { Pn532, Packet, utils: Pn532utils },
Crypto1,
Pn532Hf14a,
Pn532LoggerRxTx,
Pn532WebbleAdapter,
Pn532WebserialAdapter,
} = window
```

## Getting Started

### use Adapter
Expand Down
143 changes: 85 additions & 58 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,84 +1,111 @@
{
"name": "pn532.js",
"version": "0.1.14",
"author": "taichunmin <taichunmin@gmail.com>",
"browser": "dist/pn532.min.js",
"description": "pn532.js is a JavaScript library for PN532 base on Web Bluetooth and Web Serial.",
"homepage": "https://taichunmin.idv.tw/pn532.js/docs/",
"jsdelivr": "dist/pn532.min.js",
"license": "MIT",
"main": "src/main.js",
"main": "dist/main.js",
"module": "src/main.js",
"name": "pn532.js",
"type": "module",
"unpkg": "dist/pn532.min.js",
"version": "0.1.14",
"bugs": {
"url": "https://github.com/taichunmin/pn532.js/issues"
"url": "https://github.com/taichunmin/pn532.js/issues"
},
"contributors": [
{
"email": "taichunmin@gmail.com",
"name": "Chunmin Tai",
"url": "https://taichunmin.idv.tw"
}
{
"email": "taichunmin@gmail.com",
"name": "Chunmin Tai",
"url": "https://taichunmin.idv.tw"
}
],
"dependencies": {
"lodash": "^4.17.21",
"web-serial-polyfill": "^1.0.14"
"lodash": "^4.17.21",
"web-serial-polyfill": "^1.0.14"
},
"devDependencies": {
"@rollup/plugin-commonjs": "^24.0.1",
"@rollup/plugin-json": "^6.0.0",
"@rollup/plugin-node-resolve": "^15.0.1",
"@rollup/plugin-terser": "^0.4.0",
"cross-env": "^7.0.3",
"dayjs": "^1.11.7",
"documentation": "^14.0.1",
"dotenv": "^16.0.3",
"eslint": "^8.33.0",
"eslint-config-standard": "^17.0.0",
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-n": "^15.6.1",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^6.1.1",
"eslint-plugin-pug": "^1.2.5",
"fast-glob": "^3.2.12",
"finalhandler": "^1.2.0",
"html-minifier": "^4.0.0",
"jest": "^29.4.1",
"jstransformer-sass": "^1.0.0",
"livereload": "^0.9.3",
"node-watch": "^0.7.3",
"pug": "^3.0.2",
"rollup": "^3.14.0",
"rollup-plugin-version-injector": "^1.3.3",
"serve-static": "^1.15.0",
"web-streams-polyfill": "^3.2.1"
"@rollup/plugin-commonjs": "^24.0.1",
"@rollup/plugin-json": "^6.0.0",
"@rollup/plugin-node-resolve": "^15.0.1",
"@rollup/plugin-terser": "^0.4.0",
"cross-env": "^7.0.3",
"dayjs": "^1.11.7",
"documentation": "^14.0.1",
"dotenv": "^16.0.3",
"eslint": "^8.33.0",
"eslint-config-standard": "^17.0.0",
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-n": "^15.6.1",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^6.1.1",
"eslint-plugin-pug": "^1.2.5",
"fast-glob": "^3.2.12",
"finalhandler": "^1.2.0",
"html-minifier": "^4.0.0",
"jest": "^29.4.1",
"jstransformer-sass": "^1.0.0",
"livereload": "^0.9.3",
"node-watch": "^0.7.3",
"pug": "^3.0.2",
"rollup": "^3.14.0",
"serve-static": "^1.15.0",
"web-streams-polyfill": "^3.2.1"
},
"exports": {
"./package.json": "./package.json",
".": {
"default": "./dist/main.js",
"import": "./src/main.js"
},
"./Crypto1.js": {
"default": "./dist/Crypto1.js",
"import": "./src/Crypto1.js"
},
"./plugin/Hf14a.js": {
"default": "./dist/plugin/Hf14a.js",
"import": "./src/plugin/Hf14a.js"
},
"./plugin/LoggerRxTx.js": {
"default": "./dist/plugin/LoggerRxTx.js",
"import": "./src/plugin/LoggerRxTx.js"
},
"./plugin/WebbleAdapter.js": {
"default": "./dist/plugin/WebbleAdapter.js",
"import": "./src/plugin/WebbleAdapter.js"
},
"./plugin/WebserialAdapter.js": {
"default": "./dist/plugin/WebserialAdapter.js",
"import": "./src/plugin/WebserialAdapter.js"
}
},
"keywords": [
"13.56MHz",
"browser",
"chrome",
"libnfc",
"mifare classic",
"NFC",
"PN532",
"RFID",
"web bluetooth",
"web serial"
"13.56MHz",
"browser",
"chrome",
"libnfc",
"mifare classic",
"NFC",
"PN532",
"RFID",
"web bluetooth",
"web serial"
],
"repository": {
"type": "git",
"url": "https://github.com/taichunmin/pn532.js.git"
"type": "git",
"url": "https://github.com/taichunmin/pn532.js.git"
},
"resolutions": {
"**/jstransformer-scss": "taichunmin/jstransformer-scss"
"**/jstransformer-scss": "taichunmin/jstransformer-scss"
},
"scripts": {
"build": "cross-env DEBUG=app:* node ./index.js && yarn docjs",
"dev": "cross-env DEBUG=app:* node ./dev.js",
"docjs": "documentation build --config documentation.yml src/** -f html --github -o dist/docs",
"lint": "eslint --ext .js,.pug --fix .",
"mkcert": "mkcert -key-file ./mkcert/key.pem -cert-file ./mkcert/cert.pem -ecdsa localhost",
"repl": "node --experimental-repl-await repl.js",
"test": "node --experimental-vm-modules --no-warnings node_modules/jest/bin/jest.js"
"build": "cross-env DEBUG=app:* node ./index.js && yarn docjs",
"dev": "cross-env DEBUG=app:* node ./dev.js",
"docjs": "documentation build --config documentation.yml src/** -f html --github -o dist/docs",
"lint": "eslint --ext .js,.pug --fix .",
"mkcert": "mkcert -key-file ./mkcert/key.pem -cert-file ./mkcert/cert.pem -ecdsa localhost",
"repl": "node --experimental-repl-await repl.js",
"test": "node --experimental-vm-modules --no-warnings node_modules/jest/bin/jest.js"
}
}
25 changes: 13 additions & 12 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import _ from 'lodash'
import { createRequire } from 'node:module'
import { fileURLToPath } from 'node:url'
import commonjs from '@rollup/plugin-commonjs'
import json from '@rollup/plugin-json'
import resolve from '@rollup/plugin-node-resolve'
import terser from '@rollup/plugin-terser'
import versionInjector from 'rollup-plugin-version-injector'

const require = createRequire(import.meta.url)
const pkg = require('./package.json')
const externalDependencies = _.keys(_.omit(pkg.dependencies, ['web-serial-polyfill']))

// good rollup example: https://github.com/MattiasBuelens/web-streams-polyfill/blob/master/rollup.config.js
const configs = [
// src/main.js
{
input: 'src/main.js',
plugins: [json(), resolve(), commonjs(), versionInjector({
injectInComments: false,
logLevel: 'error',
})],
external: ['lodash'],
plugins: [json(), resolve({ browser: true }), commonjs()],
external: externalDependencies,
output: _.times(2, isMin => ({
file: 'dist/pn532.js',
format: 'umd',
Expand All @@ -33,18 +34,18 @@ const configs = [
// src/Crypto1.js
{
input: 'src/Crypto1.js',
plugins: [json(), resolve(), commonjs()],
plugins: [json(), resolve({ browser: true }), commonjs()],
external: [
'lodash',
fileURLToPath(new URL('src/main.js', import.meta.url)),
...externalDependencies,
fileURLToPath(new URL('src/Packet.js', import.meta.url)),
],
output: _.times(2, isMin => ({
file: 'dist/Crypto1.js',
format: 'umd',
name: 'Crypto1',
globals: {
lodash: '_',
[fileURLToPath(new URL('src/main', import.meta.url))]: 'Pn532',
[fileURLToPath(new URL('src/Packet', import.meta.url))]: 'Pn532.Packet',
},
...(!isMin ? {} : { // for minify
file: 'dist/Crypto1.min.js',
Expand All @@ -56,8 +57,8 @@ const configs = [
// plugins
..._.map(['Hf14a', 'LoggerRxTx', 'WebbleAdapter', 'WebserialAdapter'], plugin => ({
input: `src/plugin/${plugin}.js`,
plugins: [json(), resolve(), commonjs()],
external: ['lodash'],
plugins: [json(), resolve({ browser: true }), commonjs()],
external: externalDependencies,
output: _.times(2, isMin => ({
file: `dist/plugin/${plugin}.js`,
format: 'umd',
Expand Down
2 changes: 1 addition & 1 deletion src/Crypto1.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* import Crypto1 from 'pn532.js/Crypto1'
*/
import _ from 'lodash'
import { Packet } from './main'
import Packet from './Packet'

const LF_POLY_ODD = 0x29CE5C
const LF_POLY_EVEN = 0x870804
Expand Down
2 changes: 1 addition & 1 deletion src/main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { default as Packet } from './Packet.js'
export { default as Pn532 } from './Pn532.js'
export { version } from '../package.json'
export * as utils from './utils.js'
export const version = '[VI]{version}[/VI]'
4 changes: 2 additions & 2 deletions web/mfkey.pug
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ block script
methods: {
async btnMfkey32v2 () {
try {
for (let k of ['uid', 'nt0', 'nr0', 'ar0', 'nt1', 'nr1', 'ar1']) {
for (const k of ['uid', 'nt0', 'nr0', 'ar0', 'nt1', 'nr1', 'ar1']) {
if (!/^[0-9a-fA-F]{8}$/.test(this.mfkey32v2[k])) throw new Error(`欄位 ${k} 格式錯誤`)
}
this.showLoading('正在破解...', '正在計算金鑰')
Expand All @@ -75,7 +75,7 @@ block script
},
async btnMfkey64 () {
try {
for (let k of ['uid', 'nt', 'nr', 'ar', 'at']) {
for (const k of ['uid', 'nt', 'nr', 'ar', 'at']) {
if (!/^[0-9a-fA-F]{8}$/.test(this.mfkey64[k])) throw new Error(`欄位 ${k} 格式錯誤`)
}
this.showLoading('正在破解...', '正在計算金鑰')
Expand Down
17 changes: 1 addition & 16 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1207,7 +1207,7 @@ chalk@^2.0.0:
escape-string-regexp "^1.0.5"
supports-color "^5.3.0"

chalk@^4.0.0, chalk@^4.1.0:
chalk@^4.0.0:
version "4.1.2"
resolved "https://registry.yarnpkg.com/chalk/-/chalk-4.1.2.tgz#aac4e2b7734a740867aeb16bf02aad556a1e7a01"
integrity sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==
Expand Down Expand Up @@ -1376,11 +1376,6 @@ cross-spawn@^7.0.1, cross-spawn@^7.0.2, cross-spawn@^7.0.3:
shebang-command "^2.0.0"
which "^2.0.1"

dateformat@^4.2.1:
version "4.6.3"
resolved "https://registry.yarnpkg.com/dateformat/-/dateformat-4.6.3.tgz#556fa6497e5217fedb78821424f8a1c22fa3f4b5"
integrity sha512-2P0p0pFGzHS5EMnhdxQi7aJN+iMheud0UhG4dlE1DLAlvL8JHjJJTX/CSm4JXwV0Ka5nGk3zC5mcb5bUQUxxMA==

dayjs@^1.11.7:
version "1.11.7"
resolved "https://registry.yarnpkg.com/dayjs/-/dayjs-1.11.7.tgz#4b296922642f70999544d1144a2c25730fce63e2"
Expand Down Expand Up @@ -3219,7 +3214,6 @@ jstransformer-sass@^1.0.0:

jstransformer-scss@^0.1.3, jstransformer-scss@taichunmin/jstransformer-scss:
version "1.0.1-beta.1"
uid e0563c2813d7f8294a27f74f2d10c6c096fc9c0c
resolved "https://codeload.github.com/taichunmin/jstransformer-scss/tar.gz/e0563c2813d7f8294a27f74f2d10c6c096fc9c0c"
dependencies:
extend-shallow "^3.0.2"
Expand Down Expand Up @@ -4514,15 +4508,6 @@ rimraf@^3.0.2:
dependencies:
glob "^7.1.3"

rollup-plugin-version-injector@^1.3.3:
version "1.3.3"
resolved "https://registry.yarnpkg.com/rollup-plugin-version-injector/-/rollup-plugin-version-injector-1.3.3.tgz#df1f8d371bc127592c29aeda6914745db4013a48"
integrity sha512-+Rrf0xIFHkwFGuMfphVlAOtd9FlhHFh3vrDwamJ6+YR3IxebRHGVT879qwWzZ1CpWMCLlngb2MmHW5wC5EJqvg==
dependencies:
chalk "^4.1.0"
dateformat "^4.2.1"
lodash "^4.17.20"

rollup@^3.14.0:
version "3.14.0"
resolved "https://registry.yarnpkg.com/rollup/-/rollup-3.14.0.tgz#f5925255f3b6e8de1dba3916d7619c7da5708d95"
Expand Down

0 comments on commit b3c97e0

Please sign in to comment.