diff --git a/node/lib/binding.ts b/node/lib/binding.ts index 275bf12ff..4327bb4bc 100644 --- a/node/lib/binding.ts +++ b/node/lib/binding.ts @@ -1,9 +1,13 @@ import { join } from 'path' +import * as url from 'url' /** * bindings are not typed yet. * if the available function names are required they can be found inside of `../src/module.c` */ -export const bindings: any = require('node-gyp-build')(join(__dirname, '../')) +import build from 'node-gyp-build' +export const bindings: any = build( + join(url.fileURLToPath(new URL('.', import.meta.url)), '../') +) export default bindings diff --git a/node/lib/chat.ts b/node/lib/chat.ts index 49a6b8f52..8559ffc64 100644 --- a/node/lib/chat.ts +++ b/node/lib/chat.ts @@ -1,11 +1,11 @@ /* eslint-disable camelcase */ -import binding from './binding' +import binding from './binding.js' import rawDebug from 'debug' const debug = rawDebug('deltachat:node:chat') -import { C } from './constants' -import { integerToHexColor } from './util' -import { ChatJSON } from './types' +import { C } from './constants.js' +import { integerToHexColor } from './util.js' +import { ChatJSON } from './types.js' interface NativeChat {} /** diff --git a/node/lib/chatlist.ts b/node/lib/chatlist.ts index 73233e006..c5574d532 100644 --- a/node/lib/chatlist.ts +++ b/node/lib/chatlist.ts @@ -1,9 +1,10 @@ /* eslint-disable camelcase */ -import binding from './binding' -import { Lot } from './lot' -import { Chat } from './chat' -const debug = require('debug')('deltachat:node:chatlist') +import binding from './binding.js' +import { Lot } from './lot.js' +import { Chat } from './chat.js' +import rawDebug from 'debug' +const debug = rawDebug('deltachat:node:chatlist') interface NativeChatList {} /** diff --git a/node/lib/contact.ts b/node/lib/contact.ts index 41e3e270c..f670562bb 100644 --- a/node/lib/contact.ts +++ b/node/lib/contact.ts @@ -1,9 +1,10 @@ -import { integerToHexColor } from './util' +import { integerToHexColor } from './util.js' /* eslint-disable camelcase */ -import binding from './binding' -const debug = require('debug')('deltachat:node:contact') +import binding from './binding.js' +import rawDebug from 'debug' +const debug = rawDebug('deltachat:node:contact') interface NativeContact {} /** diff --git a/node/lib/context.ts b/node/lib/context.ts index b292e9e35..80c073fe8 100644 --- a/node/lib/context.ts +++ b/node/lib/context.ts @@ -1,17 +1,17 @@ /* eslint-disable camelcase */ -import binding from './binding' -import { C, EventId2EventName } from './constants' -import { Chat } from './chat' -import { ChatList } from './chatlist' -import { Contact } from './contact' -import { Message } from './message' -import { Lot } from './lot' -import { Locations } from './locations' +import binding from './binding.js' +import { C, EventId2EventName } from './constants.js' +import { Chat } from './chat.js' +import { ChatList } from './chatlist.js' +import { Contact } from './contact.js' +import { Message } from './message.js' +import { Lot } from './lot.js' +import { Locations } from './locations.js' import rawDebug from 'debug' -import { AccountManager } from './deltachat' +import { AccountManager } from './deltachat.js' import { join } from 'path' -import { EventEmitter } from 'stream' +import { EventEmitter } from 'events' const debug = rawDebug('deltachat:node:index') const noop = function () {} diff --git a/node/lib/deltachat.ts b/node/lib/deltachat.ts index 6080aacda..28b332973 100644 --- a/node/lib/deltachat.ts +++ b/node/lib/deltachat.ts @@ -1,13 +1,13 @@ /* eslint-disable camelcase */ -import binding from './binding' -import { EventId2EventName } from './constants' +import binding from './binding.js' +import { EventId2EventName } from './constants.js' import { EventEmitter } from 'events' import { existsSync } from 'fs' import rawDebug from 'debug' import { tmpdir } from 'os' import { join } from 'path' -import { Context } from './context' +import { Context } from './context.js' const debug = rawDebug('deltachat:node:index') const noop = function () {} diff --git a/node/lib/index.ts b/node/lib/index.ts index 6b179dbeb..660261172 100644 --- a/node/lib/index.ts +++ b/node/lib/index.ts @@ -1,20 +1,20 @@ -import { AccountManager } from './deltachat' +import { AccountManager } from './deltachat.js' export default AccountManager -export { Context } from './context' -export { Chat } from './chat' -export { ChatList } from './chatlist' -export { C } from './constants' -export { Contact } from './contact' +export { Context } from './context.js' +export { Chat } from './chat.js' +export { ChatList } from './chatlist.js' +export { C } from './constants.js' +export { Contact } from './contact.js' export { AccountManager as DeltaChat } -export { Locations } from './locations' -export { Lot } from './lot' +export { Locations } from './locations.js' +export { Lot } from './lot.js' export { Message, MessageState, MessageViewType, MessageDownloadState, -} from './message' +} from './message.js' -export * from './types' +export * from './types.js' diff --git a/node/lib/locations.ts b/node/lib/locations.ts index 6e197a133..d593fe8bc 100644 --- a/node/lib/locations.ts +++ b/node/lib/locations.ts @@ -1,7 +1,8 @@ /* eslint-disable camelcase */ -const binding = require('../binding') -const debug = require('debug')('deltachat:node:locations') +import binding from './binding.js' +import rawDebug from 'debug' +const debug = rawDebug('deltachat:node:locations') interface NativeLocations {} /** diff --git a/node/lib/lot.ts b/node/lib/lot.ts index 4da0bccab..a7f880464 100644 --- a/node/lib/lot.ts +++ b/node/lib/lot.ts @@ -1,7 +1,8 @@ /* eslint-disable camelcase */ -const binding = require('../binding') -const debug = require('debug')('deltachat:node:lot') +import binding from './binding.js' +import rawDebug from 'debug' +const debug = rawDebug('deltachat:node:lot') interface NativeLot {} /** diff --git a/node/lib/message.ts b/node/lib/message.ts index 5b2ad67d9..65e2e61d1 100644 --- a/node/lib/message.ts +++ b/node/lib/message.ts @@ -1,11 +1,12 @@ /* eslint-disable camelcase */ -import binding from './binding' -import { C } from './constants' -import { Lot } from './lot' -import { Chat } from './chat' -import { WebxdcInfo } from './context' -const debug = require('debug')('deltachat:node:message') +import binding from './binding.js' +import { C } from './constants.js' +import { Lot } from './lot.js' +import { Chat } from './chat.js' +import { WebxdcInfo } from './context.js' +import rawDebug from 'debug' +const debug = rawDebug('deltachat:node:message') export enum MessageDownloadState { Available = C.DC_DOWNLOAD_AVAILABLE, diff --git a/node/lib/node-gyp-build.d.ts b/node/lib/node-gyp-build.d.ts new file mode 100644 index 000000000..901f97cd5 --- /dev/null +++ b/node/lib/node-gyp-build.d.ts @@ -0,0 +1 @@ +declare module 'node-gyp-build' diff --git a/node/lib/types.ts b/node/lib/types.ts index 6bb54d9f8..f09bdba21 100644 --- a/node/lib/types.ts +++ b/node/lib/types.ts @@ -1,4 +1,4 @@ -import { C } from './constants' +import { C } from './constants.js' export type ChatTypes = | C.DC_CHAT_TYPE_GROUP diff --git a/node/scripts/common.js b/node/scripts/common.js index af085cb59..12876d5b9 100644 --- a/node/scripts/common.js +++ b/node/scripts/common.js @@ -1,4 +1,4 @@ -const spawnSync = require('child_process').spawnSync +import { spawnSync } from 'child_process' const verbose = isVerbose() @@ -23,4 +23,4 @@ function isVerbose () { return loglevel === 'verbose' || process.env.CI === 'true' } -module.exports = { spawn, log, isVerbose, verbose } +export { spawn, log, isVerbose, verbose } diff --git a/node/scripts/generate-constants.js b/node/scripts/generate-constants.js index 7e821dc0a..260c60bd2 100755 --- a/node/scripts/generate-constants.js +++ b/node/scripts/generate-constants.js @@ -1,14 +1,16 @@ #!/usr/bin/env node -const fs = require('fs') -const path = require('path') +import fs from 'fs' +import path from 'path' +import * as url from 'url' const data = [] -const header = path.resolve(__dirname, '../../deltachat-ffi/deltachat.h') +const header = path.resolve(url.fileURLToPath(new URL('.', import.meta.url)), '../../deltachat-ffi/deltachat.h') console.log('Generating constants...') const header_data = fs.readFileSync(header, 'UTF-8') const regex = /^#define\s+(\w+)\s+(\w+)/gm +var match while (null != (match = regex.exec(header_data))) { const key = match[1] const value = parseInt(match[2]) @@ -17,8 +19,6 @@ while (null != (match = regex.exec(header_data))) { } } -delete header_data - const constants = data .filter( ({ key }) => key.toUpperCase()[0] === key[0] // check if define name is uppercase @@ -49,17 +49,17 @@ const events = data // backwards compat fs.writeFileSync( - path.resolve(__dirname, '../constants.js'), + path.resolve(url.fileURLToPath(new URL('.', import.meta.url)), '../constants.js'), `// Generated!\n\nmodule.exports = {\n${constants}\n}\n` ) // backwards compat fs.writeFileSync( - path.resolve(__dirname, '../events.js'), + path.resolve(url.fileURLToPath(new URL('.', import.meta.url)), '../events.js'), `/* eslint-disable quotes */\n// Generated!\n\nmodule.exports = {\n${events}\n}\n` ) fs.writeFileSync( - path.resolve(__dirname, '../lib/constants.ts'), + path.resolve(url.fileURLToPath(new URL('.', import.meta.url)), '../lib/constants.js'), `// Generated!\n\nexport enum C {\n${constants.replace(/:/g, ' =')},\n}\n // Generated!\n\nexport const EventId2EventName: { [key: number]: string } = {\n${events},\n}\n` ) diff --git a/node/scripts/install.js b/node/scripts/install.js index 3f320be4a..06400fa25 100644 --- a/node/scripts/install.js +++ b/node/scripts/install.js @@ -1,6 +1,7 @@ -const {execSync} = require('child_process') -const {existsSync} = require('fs') -const {join} = require('path') +import {execSync} from 'child_process' +import {existsSync} from 'fs' +import {join} from 'path' +import * as url from 'url' const run = (cmd) => { console.log('[i] running `' + cmd + '`') @@ -16,7 +17,7 @@ if (process.env.USE_SYSTEM_LIBDELTACHAT === 'true') { run('npm run install:prebuilds') } -if (!existsSync(join(__dirname, '..', 'dist'))) { +if (!existsSync(join(url.fileURLToPath(new URL('.', import.meta.url)), '..', 'dist'))) { console.log('[i] Didn\'t find already built typescript bindings. Trying to transpile them. If this fail, make sure typescript is installed ;)') run('npm run build:bindings:ts') } diff --git a/node/scripts/postLinksToDetails.js b/node/scripts/postLinksToDetails.js index 83b70037e..93f351573 100644 --- a/node/scripts/postLinksToDetails.js +++ b/node/scripts/postLinksToDetails.js @@ -1,4 +1,5 @@ -const { readFileSync } = require('fs') +import { readFileSync } from 'fs' +import { request } from 'https' const sha = JSON.parse( readFileSync(process.env['GITHUB_EVENT_PATH'], 'utf8') @@ -21,8 +22,6 @@ const STATUS_DATA = { target_url: base_url + file_url, } -const http = require('https') - const options = { method: 'POST', headers: { @@ -32,7 +31,7 @@ const options = { }, } -const req = http.request(GITHUB_API_URL, options, function(res) { +const req = request(GITHUB_API_URL, options, function (res) { var chunks = [] res.on('data', function(chunk) { chunks.push(chunk) diff --git a/node/scripts/postinstall.js b/node/scripts/postinstall.js index 15eb241ad..17511e60c 100644 --- a/node/scripts/postinstall.js +++ b/node/scripts/postinstall.js @@ -1,5 +1,6 @@ -const fs = require('fs') -const path = require('path') +import fs from 'fs' +import path from 'path' +import * as url from 'url' if (process.platform !== 'win32') { console.log('postinstall: not windows, so skipping!') @@ -7,7 +8,7 @@ if (process.platform !== 'win32') { } const from = path.resolve( - __dirname, + url.fileURLToPath(new URL('.', import.meta.url)), '..', '..', 'target', @@ -19,7 +20,7 @@ const getDestination = () => { const argv = process.argv if (argv.length === 3 && argv[2] === '--prebuild') { return path.resolve( - __dirname, + url.fileURLToPath(new URL('.', import.meta.url)), '..', 'prebuilds', 'win32-x64', @@ -27,7 +28,7 @@ const getDestination = () => { ) } else { return path.resolve( - __dirname, + url.fileURLToPath(new URL('.', import.meta.url)), '..', 'build', 'Release', diff --git a/node/scripts/rebuild-core.js b/node/scripts/rebuild-core.js index 9ff794773..3c8ac71cd 100644 --- a/node/scripts/rebuild-core.js +++ b/node/scripts/rebuild-core.js @@ -1,7 +1,8 @@ -const path = require('path') -const { spawn } = require('./common') +import path from 'path' +import { spawn } from './common.js' +import * as url from 'url' const opts = { - cwd: path.resolve(__dirname, '../..'), + cwd: path.resolve(url.fileURLToPath(new URL('.', import.meta.url)), '../..'), stdio: 'inherit' } diff --git a/node/tsconfig.json b/node/tsconfig.json index 25ce300f2..2f9db602f 100644 --- a/node/tsconfig.json +++ b/node/tsconfig.json @@ -3,7 +3,8 @@ "outDir": "dist", "rootDir": "./lib", "sourceMap": true, - "module": "commonjs", + "module": "nodenext", + "moduleResolution": "nodenext", "target": "es5", "esModuleInterop": true, "declaration": true, diff --git a/package.json b/package.json index f4310052e..a7ab728d2 100644 --- a/package.json +++ b/package.json @@ -1,4 +1,5 @@ { + "type": "module", "dependencies": { "debug": "^4.1.1", "napi-macros": "^2.0.0", @@ -10,7 +11,7 @@ "@types/node": "^20.8.10", "chai": "~4.3.10", "chai-as-promised": "^7.1.1", - "mocha": "^8.2.1", + "mocha": "^10.2.0", "node-gyp": "^10.0.0", "prebuildify": "^5.0.1", "prebuildify-ci": "^1.0.5", @@ -27,7 +28,7 @@ ], "homepage": "https://github.com/deltachat/deltachat-core-rust/tree/master/node", "license": "GPL-3.0-or-later", - "main": "node/dist/index.js", + "exports": "node/dist/index.js", "name": "deltachat-node", "repository": { "type": "git",