Skip to content

Commit

Permalink
Properly transpile to ESM and CJS
Browse files Browse the repository at this point in the history
  • Loading branch information
Acconut committed May 17, 2024
1 parent 3cd76fd commit 4620b9c
Show file tree
Hide file tree
Showing 34 changed files with 76 additions and 72 deletions.
2 changes: 1 addition & 1 deletion lib/browser/httpStack.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable max-classes-per-file */

import { HttpProgressHandler, HttpRequest, HttpResponse, HttpStack } from '../upload'
import { HttpProgressHandler, HttpRequest, HttpResponse, HttpStack } from '../upload.js'

export default class XHRHttpStack implements HttpStack<Blob> {
createRequest(method, url) {
Expand Down
6 changes: 3 additions & 3 deletions lib/browser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import BaseUpload, {
defaultOptions as baseDefaultOptions,
terminate,
UploadOptions,
} from '../upload'
} from '../upload.js'
import NoopUrlStorage from '../noopUrlStorage.js'
import { enableDebugLog } from '../logger'
import DetailedError from '../error'
import { enableDebugLog } from '../logger.js'
import DetailedError from '../error.js'

import { canStoreURLs, WebStorageUrlStorage } from './urlStorage.js'
import DefaultHttpStack from './httpStack.js'
Expand Down
4 changes: 2 additions & 2 deletions lib/browser/sources/StreamSource.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FileSource } from '../../upload'
import { FileSliceTypes } from '../index'
import { FileSource } from '../../upload.js'
import { FileSliceTypes } from '../index.js'

function len(blobOrArray): number {
if (blobOrArray === undefined) return 0
Expand Down
2 changes: 1 addition & 1 deletion lib/browser/urlStorage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PreviousUpload, UrlStorage } from '../upload'
import { PreviousUpload, UrlStorage } from '../upload.js'

let hasStorage = false
try {
Expand Down
2 changes: 1 addition & 1 deletion lib/error.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HttpRequest, HttpResponse } from './upload'
import { HttpRequest, HttpResponse } from './upload.js'

class DetailedError extends Error {
originalRequest?: HttpRequest<unknown>
Expand Down
10 changes: 5 additions & 5 deletions lib/node/fileReader.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { ReadStream } from 'fs'
import isStream from 'is-stream'

import { FileReader as IFileReader } from '../upload'
import BufferSource from './sources/BufferSource'
import getFileSource from './sources/FileSource'
import StreamSource from './sources/StreamSource'
import { FileSliceTypes, FileTypes } from './index'
import { FileReader as IFileReader } from '../upload.js'
import BufferSource from './sources/BufferSource.js'
import getFileSource from './sources/FileSource.js'
import StreamSource from './sources/StreamSource.js'
import { FileSliceTypes, FileTypes } from './index.js'

// TODO: Consider renaming this NodeFileReader
export default class FileReader implements IFileReader<FileTypes, FileSliceTypes> {
Expand Down
4 changes: 2 additions & 2 deletions lib/node/fileSignature.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as fs from 'fs'
import * as path from 'path'
import { createHash } from 'crypto'
import { UploadOptions } from '../upload'
import { FileTypes, FileSliceTypes } from './index'
import { UploadOptions } from '../upload.js'
import { FileTypes, FileSliceTypes } from './index.js'

/**
* Generate a fingerprint for a file which will be used the store the endpoint
Expand Down
4 changes: 2 additions & 2 deletions lib/node/httpStack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import * as https from 'https'
import { parse } from 'url'
import { Readable, Transform } from 'stream'
import throttle from 'lodash.throttle'
import { HttpProgressHandler, HttpRequest, HttpResponse, HttpStack } from '../upload'
import { FileSliceTypes } from './index'
import { HttpProgressHandler, HttpRequest, HttpResponse, HttpStack } from '../upload.js'
import { FileSliceTypes } from './index.js'

export default class NodeHttpStack implements HttpStack<FileSliceTypes> {
private _requestOptions: http.RequestOptions
Expand Down
18 changes: 9 additions & 9 deletions lib/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ import BaseUpload, {
UploadOptions,
defaultOptions as baseDefaultOptions,
terminate,
} from '../upload'
import NoopUrlStorage from '../noopUrlStorage'
import { enableDebugLog } from '../logger'
import DetailedError from '../error'
} from '../upload.js'
import NoopUrlStorage from '../noopUrlStorage.js'
import { enableDebugLog } from '../logger.js'
import DetailedError from '../error.js'

import { FileUrlStorage, canStoreURLs } from './urlStorage'
import DefaultHttpStack from './httpStack'
import FileReader from './fileReader'
import fingerprint from './fileSignature'
import StreamSource from './sources/StreamSource'
import { FileUrlStorage, canStoreURLs } from './urlStorage.js'
import DefaultHttpStack from './httpStack.js'
import FileReader from './fileReader.js'
import fingerprint from './fileSignature.js'
import StreamSource from './sources/StreamSource.js'

const defaultOptions = {
...baseDefaultOptions,
Expand Down
2 changes: 1 addition & 1 deletion lib/node/sources/BufferSource.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FileSource } from '../../upload'
import { FileSource } from '../../upload.js'

export default class BufferSource implements FileSource<Buffer> {
size: number
Expand Down
2 changes: 1 addition & 1 deletion lib/node/sources/FileSource.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createReadStream, promises as fsPromises, ReadStream } from 'fs'
import { FileSource as IFileSource } from '../../upload'
import { FileSource as IFileSource } from '../../upload.js'

export default async function getFileSource(stream: ReadStream): Promise<FileSource> {
const path = stream.path.toString()
Expand Down
2 changes: 1 addition & 1 deletion lib/node/sources/StreamSource.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Readable } from 'stream'
import { FileSource } from '../../upload'
import { FileSource } from '../../upload.js'

/**
* readChunk reads a chunk with the given size from the given
Expand Down
2 changes: 1 addition & 1 deletion lib/node/urlStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { readFile, writeFile } from 'fs'
import * as lockfile from 'proper-lockfile'
import combineErrors from 'combine-errors'
import { PreviousUpload, UrlStorage } from '../upload'
import { PreviousUpload, UrlStorage } from '../upload.js'

export const canStoreURLs = true

Expand Down
2 changes: 1 addition & 1 deletion lib/noopUrlStorage.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint no-unused-vars: "off" */

import { UrlStorage } from './upload'
import { UrlStorage } from './upload.js'

export default class NoopUrlStorage implements UrlStorage {
findAllUploads() {
Expand Down
6 changes: 3 additions & 3 deletions lib/upload.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Base64 } from 'js-base64'
import URL from 'url-parse'
import DetailedError from './error'
import { log } from './logger'
import uuid from './uuid'
import DetailedError from './error.js'
import { log } from './logger.js'
import uuid from './uuid.js'

const PROTOCOL_TUS_V1 = 'tus-v1'
const PROTOCOL_IETF_DRAFT_03 = 'ietf-draft-03'
Expand Down
13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "tus-js-client",
"version": "4.1.0",
"description": "A pure JavaScript client for the tus resumable upload protocol",
"type": "module",
"main": "lib.es5/node/index.js",
"module": "lib.esm/node/index.js",
"files": [
Expand Down Expand Up @@ -85,20 +86,20 @@
"url-parse": "^1.5.7"
},
"scripts": {
"build-test-bundle": "mkdir -p dist && browserify test/spec/browser-index.js -d -o dist/browser-test-bundle.js",
"build-test-bundle": "mkdir -p dist && browserify test/spec/browser-index.cjs -d -o dist/browser-test-bundle.js",
"build-bundle": "mkdir -p dist && browserify lib.es5/browser/index.js -s tus -d | exorcist ./dist/tus.js.map > dist/tus.js",
"build-minify": "uglifyjs ./dist/tus.js -o ./dist/tus.min.js --compress --mangle --source-map \"content='./dist/tus.js.map',url='tus.min.js.map'\"",
"build-transpile-esm": "tsc --project tsconfig-esm.json",
"build-transpile-cjs": "tsc --project tsconfig-cjs.json",
"build-transpile-esm": "tsc --project tsconfig-esm.json && echo '{\"type\":\"commonjs\"}' > lib.es5/package.json",
"build-transpile-cjs": "tsc --project tsconfig-cjs.json && echo '{\"type\":\"module\"}' > lib.esm/package.json",
"build-transpile": "npm-run-all build-transpile-esm build-transpile-cjs",
"build": "npm-run-all build-transpile build-bundle build-minify build-test-bundle",
"watch-bundle": "chokidar --initial \"lib/**/*\" -c \"npm run build-bundle\"",
"watch-test-bundle": "chokidar --initial \"test/spec/**/*\" -c \"npm run build-test-bundle\"",
"watch-transpile": "chokidar --initial \"lib/**/*\" -c \"npm run build-transpile\"",
"watch": "npm-run-all --parallel watch-*",
"test-puppeteer": "karma start test/karma/puppeteer.conf.js",
"test-browserstack": "karma start test/karma/browserstack.conf.js",
"test-node": "jasmine test/spec/node-index.js",
"test-puppeteer": "karma start test/karma/puppeteer.conf.cjs",
"test-browserstack": "karma start test/karma/browserstack.conf.cjs",
"test-node": "jasmine test/spec/node-index.cjs",
"test-types": "tsd",
"lint": "eslint . --cache && yarn lint:formatting",
"fix:formatting": "prettier --write .",
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

// Karma configuration for testing using BrowserStack
const baseConfig = require('./base.conf')
const baseConfig = require('./base.conf.cjs')

// Pass ID from Github Actions to BrowserStack, so that individual
// runs are grouped together.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

// Karma configuration for testing using Puppeteer
const baseConfig = require('./base.conf')
const baseConfig = require('./base.conf.cjs')

// Configure to use Puppeteer. See https://github.com/karma-runner/karma-chrome-launcher#available-browsers
process.env.CHROME_BIN = require('puppeteer').executablePath()
Expand Down
13 changes: 13 additions & 0 deletions test/spec/browser-index.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict'

beforeEach(() => {
// Clear localStorage before every test to prevent stored URLs to
// interfere with our setup.
localStorage.clear()
})

require('./test-common.cjs')
require('./test-browser-specific.cjs')
require('./test-parallel-uploads.cjs')
require('./test-terminate.cjs')
require('./test-end-to-end.cjs')
13 changes: 0 additions & 13 deletions test/spec/browser-index.js

This file was deleted.

File renamed without changes.
File renamed without changes.
9 changes: 9 additions & 0 deletions test/spec/node-index.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict'

// TODO: Move all tests to ESM as well

require('./test-common.cjs')
require('./test-node-specific.cjs')
require('./test-parallel-uploads.cjs')
require('./test-terminate.cjs')
require('./test-end-to-end.cjs')
7 changes: 0 additions & 7 deletions test/spec/node-index.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const assertUrlStorage = require('./helpers/assertUrlStorage')
const { TestHttpStack, waitableFunction, wait } = require('./helpers/utils')
const assertUrlStorage = require('./helpers/assertUrlStorage.cjs')
const { TestHttpStack, waitableFunction, wait } = require('./helpers/utils.cjs')
const tus = require('../..')

describe('tus', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/spec/test-common.js → test/spec/test-common.cjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const { TestHttpStack, waitableFunction, wait, getBlob } = require('./helpers/utils')
const { TestHttpStack, waitableFunction, wait, getBlob } = require('./helpers/utils.cjs')
const tus = require('../..')

// Uncomment to enable debug log from tus-js-client
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const axios = require('axios')
const { getBlob } = require('./helpers/utils')
const { getBlob } = require('./helpers/utils.cjs')
const tus = require('../..')

// Test timeout for end-to-end tests when uploading to real server.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ const crypto = require('crypto')
const intoStream = require('into-stream')
const { once } = require('events')
const tus = require('../..')
const assertUrlStorage = require('./helpers/assertUrlStorage')
const { TestHttpStack, waitableFunction } = require('./helpers/utils')
const assertUrlStorage = require('./helpers/assertUrlStorage.cjs')
const { TestHttpStack, waitableFunction } = require('./helpers/utils.cjs')

describe('tus', () => {
describe('#canStoreURLs', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const { TestHttpStack, waitableFunction, wait, getBlob } = require('./helpers/utils')
const { TestHttpStack, waitableFunction, wait, getBlob } = require('./helpers/utils.cjs')
const tus = require('../..')

describe('tus', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const { TestHttpStack, getBlob } = require('./helpers/utils')
const { TestHttpStack, getBlob } = require('./helpers/utils.cjs')
const tus = require('../..')

describe('tus', () => {
Expand Down
1 change: 1 addition & 0 deletions tsconfig-cjs.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "commonjs",
"moduleResolution": null,
// TODO: Rename to lib.cjs
"outDir": "lib.es5"
}
Expand Down
2 changes: 0 additions & 2 deletions tsconfig-esm.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"moduleResolution": "NodeNext",
"module": "NodeNext",
"outDir": "lib.esm/"
}
}
2 changes: 2 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
"include": ["lib/**/*"],
"compilerOptions": {
"target": "ES2018",
"module": "Node16",
"moduleResolution": "Node16",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
Expand Down

0 comments on commit 4620b9c

Please sign in to comment.