Skip to content

Commit

Permalink
export ES module instead of UMD
Browse files Browse the repository at this point in the history
  • Loading branch information
egoroof committed May 31, 2023
1 parent 87e2824 commit 1ff8abb
Show file tree
Hide file tree
Showing 15 changed files with 58 additions and 38 deletions.
56 changes: 40 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Find the changelog in [CHANGELOG.md](https://github.com/egoroof/browser-id3-writ
## Table of Contents

- [Installation](#installation)
- [JS modules](#js-modules)
- [Usage](#usage)
- [Browser](#browser)
1. [Get ArrayBuffer of song](#get-arraybuffer-of-song)
Expand All @@ -26,6 +27,8 @@ Find the changelog in [CHANGELOG.md](https://github.com/egoroof/browser-id3-writ
- [Node.js](#nodejs)
- [Supported frames](#supported-frames)
- [APIC picture types](#apic-picture-types)
- [SYLT content types](#sylt-content-types)
- [SYLT timestamp formats](#sylt-timestamp-formats)

## Installation

Expand All @@ -35,6 +38,25 @@ Take latest version [here](https://unpkg.com/browser-id3-writer) or with npm:
npm install browser-id3-writer --save
```

### JS modules

The library is only deployed in [native JS modules](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules), so in browsers you have to use `script` with type `module`:

```html
<script type="module">
import { ID3Writer } from 'https://your-host/browser-id3-writer.mjs';
// your code here..
</script>
```

Or bundle the library to your code.

In Nodejs it imports easily:

```js
import { ID3Writer } from 'browser-id3-writer';
```

## Usage

### Browser
Expand All @@ -52,7 +74,9 @@ For example you can create file input and use

```html
<input type="file" id="file" accept="audio/mpeg" />
<script>
<script type="module">
import { ID3Writer } from 'https://your-host/browser-id3-writer.mjs';
document.getElementById('file').addEventListener('change', function () {
if (this.files.length === 0) {
return;
Expand Down Expand Up @@ -164,11 +188,11 @@ writer.revokeURL(); // if you have access to writer
Simple example with blocking IO:

```js
const ID3Writer = require('browser-id3-writer');
const fs = require('fs');
import { ID3Writer } from 'browser-id3-writer';
import { readFileSync, writeFileSync } from 'fs';

const songBuffer = fs.readFileSync('path_to_song.mp3');
const coverBuffer = fs.readFileSync('path_to_cover.jpg');
const songBuffer = readFileSync('path_to_song.mp3');
const coverBuffer = readFileSync('path_to_cover.jpg');

const writer = new ID3Writer(songBuffer);
writer
Expand All @@ -184,7 +208,7 @@ writer
writer.addTag();

const taggedSongBuffer = Buffer.from(writer.arrayBuffer);
fs.writeFileSync('song_with_tags.mp3', taggedSongBuffer);
writeFileSync('song_with_tags.mp3', taggedSongBuffer);
```

You can also create only ID3 tag without song and use it as you want:
Expand Down Expand Up @@ -267,23 +291,23 @@ writer.setFrame('USLT', {
writer.setFrame('IPLS', [
['role', 'name'],
['role', 'name'],
...
// ...
]);
```

- SYLT (synchronised lyrics):

```js
writer.setFrame('SYLT', {
type: 1,
text: [
['lyrics here', 0],
['lyrics here', 3500],
...
],
timestampFormat: 2,
language: 'eng',
description: 'description',
type: 1,
text: [
['lyrics here', 0],
['lyrics here', 3500],
// ...
],
timestampFormat: 2,
language: 'eng',
description: 'description',
});
```

Expand Down
14 changes: 5 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
"name": "browser-id3-writer",
"version": "5.0.0",
"description": "JavaScript library for writing ID3 tag to MP3 files in browsers and Node.js",
"main": "dist/browser-id3-writer.js",
"main": "dist/browser-id3-writer.mjs",
"scripts": {
"lint": "eslint src tools test --ext js,mjs",
"build": "npm run build:wrap && npm run build:compress && node tools/distSize.mjs",
"build:wrap": "rollup -f umd -n ID3Writer -i src/ID3Writer.mjs -o dist/browser-id3-writer.js",
"build:compress": "terser dist/browser-id3-writer.js -o dist/browser-id3-writer.js -m -c",
"build": "npm run build:bundle && npm run build:compress && node tools/distSize.mjs",
"build:bundle": "rollup -i src/ID3Writer.mjs -o dist/browser-id3-writer.mjs",
"build:compress": "terser dist/browser-id3-writer.mjs -o dist/browser-id3-writer.mjs -m -c --module",
"test": "npm run prettier:check && npm run lint && npm run build && npm run mocha",
"mocha": "node --test --test-reporter spec test/",
"preversion": "npm test",
Expand All @@ -34,13 +34,9 @@
"files": [
"LICENSE.md",
"README.md",
"dist/browser-id3-writer.js"
"dist/browser-id3-writer.mjs"
],
"license": "MIT",
"bugs": {
"url": "https://github.com/egoroof/browser-id3-writer/issues"
},
"homepage": "https://github.com/egoroof/browser-id3-writer#readme",
"devDependencies": {
"eslint": "^8.41.0",
"prettier": "^2.8.8",
Expand Down
2 changes: 1 addition & 1 deletion src/ID3Writer.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
getSynchronisedLyricsFrameSize,
} from './sizes.mjs';

export default class ID3Writer {
export class ID3Writer {
_setIntegerFrame(name, value) {
const integer = parseInt(value, 10);

Expand Down
2 changes: 1 addition & 1 deletion test/arrayOfStrings.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { deepStrictEqual } from 'assert';
import { getEmptyBuffer, id3Header } from './utils.mjs';
import { encodeUtf16le, encodeWindows1252 } from '../src/encoder.mjs';
import { uint28ToUint7Array, uint32ToUint8Array } from '../src/transform.mjs';
import ID3Writer from '../dist/browser-id3-writer.js';
import { ID3Writer } from '../dist/browser-id3-writer.mjs';

const frames = ['TPE1', 'TCOM', 'TCON'];

Expand Down
2 changes: 1 addition & 1 deletion test/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { deepStrictEqual, throws } from 'assert';
import { getEmptyBuffer, id3Header } from './utils.mjs';
import { encodeUtf16le, encodeWindows1252 } from '../src/encoder.mjs';
import { uint28ToUint7Array, uint32ToUint8Array } from '../src/transform.mjs';
import ID3Writer from '../dist/browser-id3-writer.js';
import { ID3Writer } from '../dist/browser-id3-writer.mjs';

describe('Commom usage', () => {
it('Copy data from buffer, add padding and tags', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/integer.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { deepStrictEqual } from 'assert';
import { getEmptyBuffer, id3Header } from './utils.mjs';
import { encodeWindows1252 } from '../src/encoder.mjs';
import { uint28ToUint7Array, uint32ToUint8Array } from '../src/transform.mjs';
import ID3Writer from '../dist/browser-id3-writer.js';
import { ID3Writer } from '../dist/browser-id3-writer.mjs';

const frames = ['TLEN', 'TYER', 'TBPM'];

Expand Down
2 changes: 1 addition & 1 deletion test/object/APIC.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
uint28ToUint7Array,
uint32ToUint8Array,
} from '../../src/transform.mjs';
import ID3Writer from '../../dist/browser-id3-writer.js';
import { ID3Writer } from '../../dist/browser-id3-writer.mjs';

const imageContent = [4, 8, 15, 16, 23, 42];

Expand Down
2 changes: 1 addition & 1 deletion test/object/COMM.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
uint28ToUint7Array,
uint32ToUint8Array,
} from '../../src/transform.mjs';
import ID3Writer from '../../dist/browser-id3-writer.js';
import { ID3Writer } from '../../dist/browser-id3-writer.mjs';

describe('COMM', () => {
it('COMM', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/object/IPLS.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
uint28ToUint7Array,
uint32ToUint8Array,
} from '../../src/transform.mjs';
import ID3Writer from '../../dist/browser-id3-writer.js';
import { ID3Writer } from '../../dist/browser-id3-writer.mjs';

describe('IPLS', () => {
it('IPLS', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/object/PRIV.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
uint28ToUint7Array,
uint32ToUint8Array,
} from '../../src/transform.mjs';
import ID3Writer from '../../dist/browser-id3-writer.js';
import { ID3Writer } from '../../dist/browser-id3-writer.mjs';

describe('PRIV', () => {
it('PRIV', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/object/SYLT.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
uint28ToUint7Array,
uint32ToUint8Array,
} from '../../src/transform.mjs';
import ID3Writer from '../../dist/browser-id3-writer.js';
import { ID3Writer } from '../../dist/browser-id3-writer.mjs';

describe('SYLT', () => {
it('SYLT', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/object/TXXX.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
uint28ToUint7Array,
uint32ToUint8Array,
} from '../../src/transform.mjs';
import ID3Writer from '../../dist/browser-id3-writer.js';
import { ID3Writer } from '../../dist/browser-id3-writer.mjs';

describe('TXXX', () => {
it('TXXX', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/object/USLT.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
uint28ToUint7Array,
uint32ToUint8Array,
} from '../../src/transform.mjs';
import ID3Writer from '../../dist/browser-id3-writer.js';
import { ID3Writer } from '../../dist/browser-id3-writer.mjs';

describe('USLT', () => {
it('USLT', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/string.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { deepStrictEqual } from 'assert';
import { getEmptyBuffer, id3Header } from './utils.mjs';
import { encodeUtf16le, encodeWindows1252 } from '../src/encoder.mjs';
import { uint28ToUint7Array, uint32ToUint8Array } from '../src/transform.mjs';
import ID3Writer from '../dist/browser-id3-writer.js';
import { ID3Writer } from '../dist/browser-id3-writer.mjs';

const oneByteEncodedFrames = ['TDAT'];
const twoByteEncodedFrames = [
Expand Down
2 changes: 1 addition & 1 deletion tools/distSize.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { gzipSync } from 'zlib';

const bytesToKiB = (bytes) => (bytes / 1024).toFixed(2);

const filePath = 'dist/browser-id3-writer.js';
const filePath = 'dist/browser-id3-writer.mjs';
const distFile = readFileSync(filePath);
const size = bytesToKiB(distFile.byteLength);
const gzipSize = bytesToKiB(gzipSync(distFile).byteLength);
Expand Down

0 comments on commit 1ff8abb

Please sign in to comment.