Skip to content

Commit

Permalink
feat: esm (#187)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: ESM only

* feat: esm

* fix: regex artifacts
  • Loading branch information
ThaUnknown authored Nov 25, 2022
1 parent d9bf302 commit 0a3cb58
Show file tree
Hide file tree
Showing 15 changed files with 96 additions and 95 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ npm install create-torrent
The simplest way to use `create-torrent` is like this:

```js
const createTorrent = require('create-torrent')
const fs = require('fs')
import createTorrent from 'create-torrent'
import fs from 'fs'

createTorrent('/path/to/folder', (err, torrent) => {
if (!err) {
Expand Down
6 changes: 3 additions & 3 deletions bin/cmd.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node
const fs = require('fs')
const minimist = require('minimist')
const createTorrent = require('../')
import fs from 'fs'
import minimist from 'minimist'
import createTorrent from '../index.js'

const argv = minimist(process.argv.slice(2), {
alias: {
Expand Down
14 changes: 6 additions & 8 deletions get-files.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const corePath = require('path')
const fs = require('fs')
const junk = require('junk')
const once = require('once')
const parallel = require('run-parallel')
import corePath from 'path'
import fs from 'fs'
import junk from 'junk'
import once from 'once'
import parallel from 'run-parallel'

function notHidden (file) {
return file[0] !== '.'
Expand Down Expand Up @@ -34,7 +34,7 @@ function getFilePathStream (path) {
return () => fs.createReadStream(path)
}

function getFiles (path, keepRoot, cb) {
export default function getFiles (path, keepRoot, cb) {
traversePath(path, getFileInfo, (err, files) => {
if (err) return cb(err)

Expand Down Expand Up @@ -67,5 +67,3 @@ function getFileInfo (path, cb) {
cb(null, info)
})
}

module.exports = getFiles
54 changes: 26 additions & 28 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/*! create-torrent. MIT License. WebTorrent LLC <https://webtorrent.io/opensource> */
const bencode = require('bencode')
const blockIterator = require('block-iterator')
const calcPieceLength = require('piece-length')
const corePath = require('path')
const isFile = require('is-file')
const junk = require('junk')
const joinIterator = require('join-async-iterator')
const parallel = require('run-parallel')
const queueMicrotask = require('queue-microtask')
const sha1 = require('simple-sha1')
require('fast-readable-async-iterator')

const getFiles = require('./get-files') // browser exclude
import bencode from 'bencode'
import blockIterator from 'block-iterator'
import calcPieceLength from 'piece-length'
import corePath from 'path'
import isFile from 'is-file'
import junk from 'junk'
import joinIterator from 'join-async-iterator'
import parallel from 'run-parallel'
import queueMicrotask from 'queue-microtask'
import sha1 from 'simple-sha1'
import 'fast-readable-async-iterator'

import getFiles from './get-files.js' // browser exclude

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

function onFiles (files, opts, cb) {
let announceList = opts.announceList
let _announceList = opts.announceList

if (!announceList) {
if (typeof opts.announce === 'string') announceList = [[opts.announce]]
if (!_announceList) {
if (typeof opts.announce === 'string') _announceList = [[opts.announce]]
else if (Array.isArray(opts.announce)) {
announceList = opts.announce.map(u => [u])
_announceList = opts.announce.map(u => [u])
}
}

if (!announceList) announceList = []
if (!_announceList) _announceList = []

if (globalThis.WEBTORRENT_ANNOUNCE) {
if (typeof globalThis.WEBTORRENT_ANNOUNCE === 'string') {
announceList.push([[globalThis.WEBTORRENT_ANNOUNCE]])
_announceList.push([[globalThis.WEBTORRENT_ANNOUNCE]])
} else if (Array.isArray(globalThis.WEBTORRENT_ANNOUNCE)) {
announceList = announceList.concat(globalThis.WEBTORRENT_ANNOUNCE.map(u => [u]))
_announceList = _announceList.concat(globalThis.WEBTORRENT_ANNOUNCE.map(u => [u]))
}
}

// When no trackers specified, use some reasonable defaults
if (opts.announce === undefined && opts.announceList === undefined) {
announceList = announceList.concat(module.exports.announceList)
_announceList = _announceList.concat(announceList)
}

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

if (announceList.length !== 0) {
torrent.announce = announceList[0][0]
torrent['announce-list'] = announceList
if (_announceList.length !== 0) {
torrent.announce = _announceList[0][0]
torrent['announce-list'] = _announceList
}

if (opts.comment !== undefined) torrent.comment = opts.comment
Expand Down Expand Up @@ -391,7 +391,5 @@ async function * getStreamStream (readable, file) {
}
}

module.exports = createTorrent
module.exports.parseInput = parseInput
module.exports.announceList = announceList
module.exports.isJunkPath = isJunkPath
export default createTorrent
export { parseInput, announceList, isJunkPath }
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"email": "feross@webtorrent.io",
"url": "https://webtorrent.io"
},
"type": "module",
"bin": {
"create-torrent": "./bin/cmd.js"
},
Expand Down Expand Up @@ -42,6 +43,9 @@
"engines": {
"node": ">=12"
},
"exports": {
"import": "./index.js"
},
"keywords": [
".torrent",
"bittorrent",
Expand All @@ -56,7 +60,6 @@
"webtorrent"
],
"license": "MIT",
"main": "index.js",
"repository": {
"type": "git",
"url": "git://github.com/webtorrent/create-torrent.git"
Expand Down
8 changes: 4 additions & 4 deletions test/basic.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const parseTorrent = require('parse-torrent')
const path = require('path')
const test = require('tape')
const createTorrent = require('../')
import parseTorrent from 'parse-torrent'
import path from 'path'
import test from 'tape'
import createTorrent from '../index.js'

test('implicit torrent name and file name', t => {
t.plan(5)
Expand Down
14 changes: 7 additions & 7 deletions test/browser/basic.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* global Blob */

const fixtures = require('webtorrent-fixtures')
const fs = require('fs')
const parseTorrent = require('parse-torrent')
const path = require('path')
const sha1 = require('simple-sha1')
const test = require('tape')
const createTorrent = require('../../')
import fixtures from 'webtorrent-fixtures'
import fs from 'fs'
import parseTorrent from 'parse-torrent'
import path from 'path'
import sha1 from 'simple-sha1'
import test from 'tape'
import createTorrent from '../../index.js'

function makeFileShim (buf, name) {
const file = new Blob([buf])
Expand Down
12 changes: 6 additions & 6 deletions test/browser/directory.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/* global Blob */

const fs = require('fs')
const parseTorrent = require('parse-torrent')
const path = require('path')
const sha1 = require('simple-sha1')
const test = require('tape')
const createTorrent = require('../../')
import fs from 'fs'
import parseTorrent from 'parse-torrent'
import path from 'path'
import sha1 from 'simple-sha1'
import test from 'tape'
import createTorrent from '../../index.js'

function makeFileShim (buf, name, fullPath) {
const file = new Blob([buf])
Expand Down
10 changes: 5 additions & 5 deletions test/buffer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const createTorrent = require('../')
const parseTorrent = require('parse-torrent')
const path = require('path')
const sha1 = require('simple-sha1')
const test = require('tape')
import createTorrent from '../index.js'
import parseTorrent from 'parse-torrent'
import path from 'path'
import sha1 from 'simple-sha1'
import test from 'tape'

test('create nested torrent with array of buffers', t => {
t.plan(14)
Expand Down
4 changes: 2 additions & 2 deletions test/error.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const test = require('tape')
const createTorrent = require('../')
import test from 'tape'
import createTorrent from '../index.js'

test('error handling', t => {
t.plan(5)
Expand Down
12 changes: 6 additions & 6 deletions test/fs.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const fixtures = require('webtorrent-fixtures')
const parseTorrent = require('parse-torrent')
const path = require('path')
const sha1 = require('simple-sha1')
const test = require('tape')
const createTorrent = require('../')
import fixtures from 'webtorrent-fixtures'
import parseTorrent from 'parse-torrent'
import path from 'path'
import sha1 from 'simple-sha1'
import test from 'tape'
import createTorrent from '../index.js'

test('create single file torrent', t => {
t.plan(11)
Expand Down
14 changes: 7 additions & 7 deletions test/mixed.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const fixtures = require('webtorrent-fixtures')
const fs = require('fs')
const parseTorrent = require('parse-torrent')
const path = require('path')
const sha1 = require('simple-sha1')
const test = require('tape')
const createTorrent = require('../')
import fixtures from 'webtorrent-fixtures'
import fs from 'fs'
import parseTorrent from 'parse-torrent'
import path from 'path'
import sha1 from 'simple-sha1'
import test from 'tape'
import createTorrent from '../index.js'

test('create multi file torrent with array of mixed types', t => {
t.plan(20)
Expand Down
14 changes: 8 additions & 6 deletions test/source.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
const { join } = require('path')
const folderPath = require('webtorrent-fixtures').folder.contentPath
const parseTorrent = require('parse-torrent')
const sha1 = require('simple-sha1')
const test = require('tape')
import { join } from 'path'
import fixtures from 'webtorrent-fixtures'
import parseTorrent from 'parse-torrent'
import sha1 from 'simple-sha1'
import test from 'tape'

const createTorrent = require('../')
import createTorrent from '../index.js'

const { contentPath: folderPath } = fixtures.folder

test('verify info-hash without no source set (default)', t => {
t.plan(12)
Expand Down
6 changes: 3 additions & 3 deletions test/ssl.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const parseTorrent = require('parse-torrent')
const test = require('tape')
const createTorrent = require('../')
import parseTorrent from 'parse-torrent'
import test from 'tape'
import createTorrent from '../index.js'

test('create ssl cert torrent', t => {
t.plan(2)
Expand Down
14 changes: 7 additions & 7 deletions test/stream.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const fixtures = require('webtorrent-fixtures')
const fs = require('fs')
const parseTorrent = require('parse-torrent')
const path = require('path')
const sha1 = require('simple-sha1')
const test = require('tape')
const createTorrent = require('../')
import fixtures from 'webtorrent-fixtures'
import fs from 'fs'
import parseTorrent from 'parse-torrent'
import path from 'path'
import sha1 from 'simple-sha1'
import test from 'tape'
import createTorrent from '../index.js'

test('create single file torrent from a stream', t => {
t.plan(11)
Expand Down

0 comments on commit 0a3cb58

Please sign in to comment.