Skip to content

Commit 0a3cb58

Browse files
authored
feat: esm (#187)
BREAKING CHANGE: ESM only * feat: esm * fix: regex artifacts
1 parent d9bf302 commit 0a3cb58

15 files changed

Lines changed: 96 additions & 95 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ npm install create-torrent
2626
The simplest way to use `create-torrent` is like this:
2727

2828
```js
29-
const createTorrent = require('create-torrent')
30-
const fs = require('fs')
29+
import createTorrent from 'create-torrent'
30+
import fs from 'fs'
3131

3232
createTorrent('/path/to/folder', (err, torrent) => {
3333
if (!err) {

bin/cmd.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env node
2-
const fs = require('fs')
3-
const minimist = require('minimist')
4-
const createTorrent = require('../')
2+
import fs from 'fs'
3+
import minimist from 'minimist'
4+
import createTorrent from '../index.js'
55

66
const argv = minimist(process.argv.slice(2), {
77
alias: {

get-files.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
const corePath = require('path')
2-
const fs = require('fs')
3-
const junk = require('junk')
4-
const once = require('once')
5-
const parallel = require('run-parallel')
1+
import corePath from 'path'
2+
import fs from 'fs'
3+
import junk from 'junk'
4+
import once from 'once'
5+
import parallel from 'run-parallel'
66

77
function notHidden (file) {
88
return file[0] !== '.'
@@ -34,7 +34,7 @@ function getFilePathStream (path) {
3434
return () => fs.createReadStream(path)
3535
}
3636

37-
function getFiles (path, keepRoot, cb) {
37+
export default function getFiles (path, keepRoot, cb) {
3838
traversePath(path, getFileInfo, (err, files) => {
3939
if (err) return cb(err)
4040

@@ -67,5 +67,3 @@ function getFileInfo (path, cb) {
6767
cb(null, info)
6868
})
6969
}
70-
71-
module.exports = getFiles

index.js

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
/*! create-torrent. MIT License. WebTorrent LLC <https://webtorrent.io/opensource> */
2-
const bencode = require('bencode')
3-
const blockIterator = require('block-iterator')
4-
const calcPieceLength = require('piece-length')
5-
const corePath = require('path')
6-
const isFile = require('is-file')
7-
const junk = require('junk')
8-
const joinIterator = require('join-async-iterator')
9-
const parallel = require('run-parallel')
10-
const queueMicrotask = require('queue-microtask')
11-
const sha1 = require('simple-sha1')
12-
require('fast-readable-async-iterator')
13-
14-
const getFiles = require('./get-files') // browser exclude
2+
import bencode from 'bencode'
3+
import blockIterator from 'block-iterator'
4+
import calcPieceLength from 'piece-length'
5+
import corePath from 'path'
6+
import isFile from 'is-file'
7+
import junk from 'junk'
8+
import joinIterator from 'join-async-iterator'
9+
import parallel from 'run-parallel'
10+
import queueMicrotask from 'queue-microtask'
11+
import sha1 from 'simple-sha1'
12+
import 'fast-readable-async-iterator'
13+
14+
import getFiles from './get-files.js' // browser exclude
1515

1616
const announceList = [
1717
['udp://tracker.leechers-paradise.org:6969'],
@@ -243,28 +243,28 @@ async function getPieceList (files, pieceLength, estimatedTorrentLength, opts, c
243243
}
244244

245245
function onFiles (files, opts, cb) {
246-
let announceList = opts.announceList
246+
let _announceList = opts.announceList
247247

248-
if (!announceList) {
249-
if (typeof opts.announce === 'string') announceList = [[opts.announce]]
248+
if (!_announceList) {
249+
if (typeof opts.announce === 'string') _announceList = [[opts.announce]]
250250
else if (Array.isArray(opts.announce)) {
251-
announceList = opts.announce.map(u => [u])
251+
_announceList = opts.announce.map(u => [u])
252252
}
253253
}
254254

255-
if (!announceList) announceList = []
255+
if (!_announceList) _announceList = []
256256

257257
if (globalThis.WEBTORRENT_ANNOUNCE) {
258258
if (typeof globalThis.WEBTORRENT_ANNOUNCE === 'string') {
259-
announceList.push([[globalThis.WEBTORRENT_ANNOUNCE]])
259+
_announceList.push([[globalThis.WEBTORRENT_ANNOUNCE]])
260260
} else if (Array.isArray(globalThis.WEBTORRENT_ANNOUNCE)) {
261-
announceList = announceList.concat(globalThis.WEBTORRENT_ANNOUNCE.map(u => [u]))
261+
_announceList = _announceList.concat(globalThis.WEBTORRENT_ANNOUNCE.map(u => [u]))
262262
}
263263
}
264264

265265
// When no trackers specified, use some reasonable defaults
266266
if (opts.announce === undefined && opts.announceList === undefined) {
267-
announceList = announceList.concat(module.exports.announceList)
267+
_announceList = _announceList.concat(announceList)
268268
}
269269

270270
if (typeof opts.urlList === 'string') opts.urlList = [opts.urlList]
@@ -277,9 +277,9 @@ function onFiles (files, opts, cb) {
277277
encoding: 'UTF-8'
278278
}
279279

280-
if (announceList.length !== 0) {
281-
torrent.announce = announceList[0][0]
282-
torrent['announce-list'] = announceList
280+
if (_announceList.length !== 0) {
281+
torrent.announce = _announceList[0][0]
282+
torrent['announce-list'] = _announceList
283283
}
284284

285285
if (opts.comment !== undefined) torrent.comment = opts.comment
@@ -391,7 +391,5 @@ async function * getStreamStream (readable, file) {
391391
}
392392
}
393393

394-
module.exports = createTorrent
395-
module.exports.parseInput = parseInput
396-
module.exports.announceList = announceList
397-
module.exports.isJunkPath = isJunkPath
394+
export default createTorrent
395+
export { parseInput, announceList, isJunkPath }

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"email": "feross@webtorrent.io",
88
"url": "https://webtorrent.io"
99
},
10+
"type": "module",
1011
"bin": {
1112
"create-torrent": "./bin/cmd.js"
1213
},
@@ -42,6 +43,9 @@
4243
"engines": {
4344
"node": ">=12"
4445
},
46+
"exports": {
47+
"import": "./index.js"
48+
},
4549
"keywords": [
4650
".torrent",
4751
"bittorrent",
@@ -56,7 +60,6 @@
5660
"webtorrent"
5761
],
5862
"license": "MIT",
59-
"main": "index.js",
6063
"repository": {
6164
"type": "git",
6265
"url": "git://github.com/webtorrent/create-torrent.git"

test/basic.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
const parseTorrent = require('parse-torrent')
2-
const path = require('path')
3-
const test = require('tape')
4-
const createTorrent = require('../')
1+
import parseTorrent from 'parse-torrent'
2+
import path from 'path'
3+
import test from 'tape'
4+
import createTorrent from '../index.js'
55

66
test('implicit torrent name and file name', t => {
77
t.plan(5)

test/browser/basic.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/* global Blob */
22

3-
const fixtures = require('webtorrent-fixtures')
4-
const fs = require('fs')
5-
const parseTorrent = require('parse-torrent')
6-
const path = require('path')
7-
const sha1 = require('simple-sha1')
8-
const test = require('tape')
9-
const createTorrent = require('../../')
3+
import fixtures from 'webtorrent-fixtures'
4+
import fs from 'fs'
5+
import parseTorrent from 'parse-torrent'
6+
import path from 'path'
7+
import sha1 from 'simple-sha1'
8+
import test from 'tape'
9+
import createTorrent from '../../index.js'
1010

1111
function makeFileShim (buf, name) {
1212
const file = new Blob([buf])

test/browser/directory.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/* global Blob */
22

3-
const fs = require('fs')
4-
const parseTorrent = require('parse-torrent')
5-
const path = require('path')
6-
const sha1 = require('simple-sha1')
7-
const test = require('tape')
8-
const createTorrent = require('../../')
3+
import fs from 'fs'
4+
import parseTorrent from 'parse-torrent'
5+
import path from 'path'
6+
import sha1 from 'simple-sha1'
7+
import test from 'tape'
8+
import createTorrent from '../../index.js'
99

1010
function makeFileShim (buf, name, fullPath) {
1111
const file = new Blob([buf])

test/buffer.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
const createTorrent = require('../')
2-
const parseTorrent = require('parse-torrent')
3-
const path = require('path')
4-
const sha1 = require('simple-sha1')
5-
const test = require('tape')
1+
import createTorrent from '../index.js'
2+
import parseTorrent from 'parse-torrent'
3+
import path from 'path'
4+
import sha1 from 'simple-sha1'
5+
import test from 'tape'
66

77
test('create nested torrent with array of buffers', t => {
88
t.plan(14)

test/error.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const test = require('tape')
2-
const createTorrent = require('../')
1+
import test from 'tape'
2+
import createTorrent from '../index.js'
33

44
test('error handling', t => {
55
t.plan(5)

0 commit comments

Comments
 (0)