From 791a3a58b5c64635f7bffd69babcdf6f3be1d5c7 Mon Sep 17 00:00:00 2001 From: Alina Sireneva Date: Fri, 1 Mar 2024 18:16:07 +0300 Subject: [PATCH] fix: wasm build and e2e fixes --- e2e/cjs/tests/tl-runtime.js | 33 +++++++++----------------- e2e/cjs/tests/tl-schema.js | 13 ++++++----- e2e/cjs/tests/wasm.js | 5 +++- e2e/cjs/utils.js | 6 +++++ e2e/esm/tests/tl-runtime.js | 33 +++++++++----------------- e2e/esm/tests/tl-schema.js | 11 ++++++--- e2e/esm/tests/wasm.js | 7 ++++-- e2e/esm/utils.js | 6 +++++ e2e/ts/tests/01.auth.ts | 3 ++- e2e/ts/tests/02.methods.ts | 2 +- e2e/ts/tests/03.files.ts | 3 ++- e2e/ts/tests/04.updates.ts | 3 ++- e2e/ts/tests/packaging/tl-runtime.ts | 35 +++++++++++----------------- e2e/ts/tests/packaging/tl-schema.ts | 11 ++++++--- e2e/ts/tests/packaging/wasm.ts | 7 ++++-- e2e/ts/utils.ts | 6 +++++ packages/wasm/build.config.cjs | 5 +++- 17 files changed, 101 insertions(+), 88 deletions(-) diff --git a/e2e/cjs/tests/tl-runtime.js b/e2e/cjs/tests/tl-runtime.js index 0db623e2..4e38b6ef 100644 --- a/e2e/cjs/tests/tl-runtime.js +++ b/e2e/cjs/tests/tl-runtime.js @@ -1,35 +1,24 @@ -const { - TlBinaryReader, - TlBinaryWriter, - TlSerializationCounter, - hexEncode, - hexDecode, - hexDecodeToBuffer, -} = require('@mtcute/tl-runtime') +const { TlBinaryReader, TlBinaryWriter, TlSerializationCounter } = require('@mtcute/tl-runtime') const Long = require('long') const { describe, it } = require('mocha') const { expect } = require('chai') +const { NodePlatform } = require('@mtcute/node') // here we primarily want to check that everything imports properly, // and that the code is actually executable. The actual correctness // of the implementation is covered tested by unit tests +const p = new NodePlatform() + describe('@mtcute/tl-runtime', () => { describe('encodings', () => { it('works with Buffers', () => { - const buf = Buffer.alloc(5) - hexDecode(buf, '0102030405') - - expect(hexEncode(Buffer.from('hello'))).to.equal('68656c6c6f') - expect(buf).eql(Buffer.from([1, 2, 3, 4, 5])) + expect(p.hexEncode(Buffer.from('hello'))).to.equal('68656c6c6f') + expect(p.hexDecode('0102030405')).eql(Buffer.from([1, 2, 3, 4, 5])) }) it('works with Uint8Arrays', () => { - const buf = new Uint8Array(5) - hexDecode(buf, '0102030405') - - expect(hexEncode(new Uint8Array([1, 2, 3, 4, 5]))).to.equal('0102030405') - expect(buf).eql(new Uint8Array([1, 2, 3, 4, 5])) + expect(p.hexEncode(new Uint8Array([1, 2, 3, 4, 5]))).to.equal('0102030405') }) }) @@ -61,7 +50,7 @@ describe('@mtcute/tl-runtime', () => { }) it('should work with Uint8Arrays', () => { - const buf = hexDecodeToBuffer(data) + const buf = p.hexDecode(data) const r = new TlBinaryReader(map, buf, 8) @@ -96,7 +85,7 @@ describe('@mtcute/tl-runtime', () => { w.long(Long.fromString('51E57AC91E83C801', true, 16)) // messageId w.object(obj) - expect(hexEncode(w.result())).eq( + expect(p.hexEncode(w.result())).eq( '000000000000000001c8831ec97ae551632416050817ed48941a08f98100000015c4b51c01000000216be86c022bb4c3', ) }) @@ -104,7 +93,7 @@ describe('@mtcute/tl-runtime', () => { it('should work with Uint8Arrays', () => { const obj = { _: 'mt_resPQ', - pq: hexDecodeToBuffer('17ED48941A08F981'), + pq: p.hexDecode('17ED48941A08F981'), serverPublicKeyFingerprints: [Long.fromString('c3b42b026ce86b21', 16)], } @@ -115,7 +104,7 @@ describe('@mtcute/tl-runtime', () => { w.long(Long.fromString('51E57AC91E83C801', true, 16)) // messageId w.object(obj) - expect(hexEncode(w.result())).eq( + expect(p.hexEncode(w.result())).eq( '000000000000000001c8831ec97ae551632416050817ed48941a08f98100000015c4b51c01000000216be86c022bb4c3', ) }) diff --git a/e2e/cjs/tests/tl-schema.js b/e2e/cjs/tests/tl-schema.js index 7bab1853..43508e29 100644 --- a/e2e/cjs/tests/tl-schema.js +++ b/e2e/cjs/tests/tl-schema.js @@ -1,18 +1,17 @@ const Long = require('long') -const { - TlBinaryReader, - TlBinaryWriter, - hexEncode, -} = require('@mtcute/tl-runtime') +const { TlBinaryReader, TlBinaryWriter } = require('@mtcute/tl-runtime') const { tl } = require('@mtcute/tl') const { __tlReaderMap } = require('@mtcute/tl/binary/reader') const { __tlWriterMap } = require('@mtcute/tl/binary/writer') const { describe, it } = require('mocha') const { expect } = require('chai') +const { NodePlatform } = require('@mtcute/node') // here we primarily want to check that @mtcute/tl correctly works with @mtcute/tl-runtime +const p = new NodePlatform() + describe('@mtcute/tl', () => { it('writers map works with TlBinaryWriter', () => { const obj = { @@ -21,7 +20,9 @@ describe('@mtcute/tl', () => { accessHash: Long.fromNumber(456), } - expect(hexEncode(TlBinaryWriter.serializeObject(__tlWriterMap, obj))).to.equal('4ca5e8dd7b00000000000000c801000000000000') + expect(p.hexEncode(TlBinaryWriter.serializeObject(__tlWriterMap, obj))).to.equal( + '4ca5e8dd7b00000000000000c801000000000000', + ) }) it('readers map works with TlBinaryReader', () => { diff --git a/e2e/cjs/tests/wasm.js b/e2e/cjs/tests/wasm.js index 8171f182..3047e690 100644 --- a/e2e/cjs/tests/wasm.js +++ b/e2e/cjs/tests/wasm.js @@ -1,8 +1,11 @@ const wasm = require('@mtcute/wasm') const { describe, it, before } = require('mocha') const { expect } = require('chai') +const { NodeCryptoProvider } = require('@mtcute/node') -before(() => wasm.initAsync()) +before(async () => { + await new NodeCryptoProvider().initialize() +}) describe('@mtcute/wasm', () => { const key = Buffer.from('5468697320697320616E20696D706C655468697320697320616E20696D706C65', 'hex') diff --git a/e2e/cjs/utils.js b/e2e/cjs/utils.js index 402a563b..c1888ac1 100644 --- a/e2e/cjs/utils.js +++ b/e2e/cjs/utils.js @@ -1,16 +1,22 @@ const { MemoryStorage } = require('@mtcute/core') +const { setPlatform } = require('@mtcute/core/platform.js') const { LogManager } = require('@mtcute/core/utils.js') +const { NodeCryptoProvider, NodePlatform, TcpTransport } = require('@mtcute/node') exports.getApiParams = () => { if (!process.env.API_ID || !process.env.API_HASH) { throw new Error('API_ID and API_HASH env variables must be set') } + setPlatform(new NodePlatform()) + return { apiId: parseInt(process.env.API_ID), apiHash: process.env.API_HASH, testMode: true, storage: new MemoryStorage(), logLevel: LogManager.DEBUG, + transport: () => new TcpTransport(), + crypto: new NodeCryptoProvider(), } } diff --git a/e2e/esm/tests/tl-runtime.js b/e2e/esm/tests/tl-runtime.js index 7e6c0407..d5a4d3d8 100644 --- a/e2e/esm/tests/tl-runtime.js +++ b/e2e/esm/tests/tl-runtime.js @@ -2,34 +2,23 @@ import { expect } from 'chai' import Long from 'long' import { describe, it } from 'mocha' -import { - hexDecode, - hexDecodeToBuffer, - hexEncode, - TlBinaryReader, - TlBinaryWriter, - TlSerializationCounter, -} from '@mtcute/tl-runtime' +import { NodePlatform } from '@mtcute/node' +import { TlBinaryReader, TlBinaryWriter, TlSerializationCounter } from '@mtcute/tl-runtime' // here we primarily want to check that everything imports properly, // and that the code is actually executable. The actual correctness // of the implementation is covered tested by unit tests +const p = new NodePlatform() + describe('encodings', () => { it('works with Buffers', () => { - const buf = Buffer.alloc(5) - hexDecode(buf, '0102030405') - - expect(hexEncode(Buffer.from('hello'))).to.equal('68656c6c6f') - expect(buf).eql(Buffer.from([1, 2, 3, 4, 5])) + expect(p.hexEncode(Buffer.from('hello'))).to.equal('68656c6c6f') + expect(p.hexDecode('0102030405')).eql(Buffer.from([1, 2, 3, 4, 5])) }) it('works with Uint8Arrays', () => { - const buf = new Uint8Array(5) - hexDecode(buf, '0102030405') - - expect(hexEncode(new Uint8Array([1, 2, 3, 4, 5]))).to.equal('0102030405') - expect(buf).eql(new Uint8Array([1, 2, 3, 4, 5])) + expect(p.hexEncode(new Uint8Array([1, 2, 3, 4, 5]))).to.equal('0102030405') }) }) @@ -61,7 +50,7 @@ describe('TlBinaryReader', () => { }) it('should work with Uint8Arrays', () => { - const buf = hexDecodeToBuffer(data) + const buf = p.hexDecode(data) const r = new TlBinaryReader(map, buf, 8) @@ -96,7 +85,7 @@ describe('TlBinaryWriter', () => { w.long(Long.fromString('51E57AC91E83C801', true, 16)) // messageId w.object(obj) - expect(hexEncode(w.result())).eq( + expect(p.hexEncode(w.result())).eq( '000000000000000001c8831ec97ae551632416050817ed48941a08f98100000015c4b51c01000000216be86c022bb4c3', ) }) @@ -104,7 +93,7 @@ describe('TlBinaryWriter', () => { it('should work with Uint8Arrays', () => { const obj = { _: 'mt_resPQ', - pq: hexDecodeToBuffer('17ED48941A08F981'), + pq: p.hexDecode('17ED48941A08F981'), serverPublicKeyFingerprints: [Long.fromString('c3b42b026ce86b21', 16)], } @@ -115,7 +104,7 @@ describe('TlBinaryWriter', () => { w.long(Long.fromString('51E57AC91E83C801', true, 16)) // messageId w.object(obj) - expect(hexEncode(w.result())).eq( + expect(p.hexEncode(w.result())).eq( '000000000000000001c8831ec97ae551632416050817ed48941a08f98100000015c4b51c01000000216be86c022bb4c3', ) }) diff --git a/e2e/esm/tests/tl-schema.js b/e2e/esm/tests/tl-schema.js index 7c37cd27..ba4aab19 100644 --- a/e2e/esm/tests/tl-schema.js +++ b/e2e/esm/tests/tl-schema.js @@ -2,13 +2,16 @@ import { expect } from 'chai' import Long from 'long' import { describe, it } from 'mocha' +import { NodePlatform } from '@mtcute/node' import { tl } from '@mtcute/tl' import { __tlReaderMap } from '@mtcute/tl/binary/reader.js' import { __tlWriterMap } from '@mtcute/tl/binary/writer.js' -import { hexDecodeToBuffer, hexEncode, TlBinaryReader, TlBinaryWriter } from '@mtcute/tl-runtime' +import { TlBinaryReader, TlBinaryWriter } from '@mtcute/tl-runtime' // here we primarily want to check that @mtcute/tl correctly works with @mtcute/tl-runtime +const p = new NodePlatform() + describe('@mtcute/tl', () => { it('writers map works with TlBinaryWriter', () => { const obj = { @@ -17,11 +20,13 @@ describe('@mtcute/tl', () => { accessHash: Long.fromNumber(456), } - expect(hexEncode(TlBinaryWriter.serializeObject(__tlWriterMap, obj))).to.equal('4ca5e8dd7b00000000000000c801000000000000') + expect(p.hexEncode(TlBinaryWriter.serializeObject(__tlWriterMap, obj))).to.equal( + '4ca5e8dd7b00000000000000c801000000000000', + ) }) it('readers map works with TlBinaryReader', () => { - const buf = hexDecodeToBuffer('4ca5e8dd7b00000000000000c801000000000000') + const buf = p.hexDecode('4ca5e8dd7b00000000000000c801000000000000') const obj = TlBinaryReader.deserializeObject(__tlReaderMap, buf) expect(obj._).equal('inputPeerUser') diff --git a/e2e/esm/tests/wasm.js b/e2e/esm/tests/wasm.js index acd40735..02067d0c 100644 --- a/e2e/esm/tests/wasm.js +++ b/e2e/esm/tests/wasm.js @@ -1,9 +1,12 @@ import { expect } from 'chai' import { before, describe, it } from 'mocha' -import { ige256Decrypt, ige256Encrypt, initAsync } from '@mtcute/wasm' +import { NodeCryptoProvider } from '@mtcute/node' +import { ige256Decrypt, ige256Encrypt } from '@mtcute/wasm' -before(() => initAsync()) +before(async () => { + await new NodeCryptoProvider().initialize() +}) describe('@mtcute/wasm', () => { const key = Buffer.from('5468697320697320616E20696D706C655468697320697320616E20696D706C65', 'hex') diff --git a/e2e/esm/utils.js b/e2e/esm/utils.js index e5817cd7..64910652 100644 --- a/e2e/esm/utils.js +++ b/e2e/esm/utils.js @@ -1,16 +1,22 @@ import { MemoryStorage } from '@mtcute/core' +import { setPlatform } from '@mtcute/core/platform.js' import { LogManager } from '@mtcute/core/utils.js' +import { NodeCryptoProvider, NodePlatform, TcpTransport } from '@mtcute/node' export const getApiParams = () => { if (!process.env.API_ID || !process.env.API_HASH) { throw new Error('API_ID and API_HASH env variables must be set') } + setPlatform(new NodePlatform()) + return { apiId: parseInt(process.env.API_ID), apiHash: process.env.API_HASH, testMode: true, storage: new MemoryStorage(), logLevel: LogManager.DEBUG, + transport: () => new TcpTransport(), + crypto: new NodeCryptoProvider(), } } diff --git a/e2e/ts/tests/01.auth.ts b/e2e/ts/tests/01.auth.ts index 834aed82..9fb3c0b0 100644 --- a/e2e/ts/tests/01.auth.ts +++ b/e2e/ts/tests/01.auth.ts @@ -1,7 +1,8 @@ import { expect } from 'chai' import { describe, it } from 'mocha' -import { BaseTelegramClient, MtUnsupportedError, TelegramClient } from '@mtcute/core' +import { BaseTelegramClient, MtUnsupportedError } from '@mtcute/core' +import { TelegramClient } from '@mtcute/core/client.js' import { getApiParams } from '../utils.js' diff --git a/e2e/ts/tests/02.methods.ts b/e2e/ts/tests/02.methods.ts index 21bc5da1..32335739 100644 --- a/e2e/ts/tests/02.methods.ts +++ b/e2e/ts/tests/02.methods.ts @@ -1,7 +1,7 @@ import { expect } from 'chai' import { describe, it } from 'mocha' -import { TelegramClient } from '@mtcute/core' +import { TelegramClient } from '@mtcute/core/client.js' import { getApiParams } from '../utils.js' diff --git a/e2e/ts/tests/03.files.ts b/e2e/ts/tests/03.files.ts index e1f39e3e..bbfc00b5 100644 --- a/e2e/ts/tests/03.files.ts +++ b/e2e/ts/tests/03.files.ts @@ -3,7 +3,8 @@ import { expect } from 'chai' import { createHash } from 'crypto' import { describe, it } from 'mocha' -import { FileDownloadLocation, TelegramClient, Thumbnail } from '@mtcute/core' +import { FileDownloadLocation, Thumbnail } from '@mtcute/core' +import { TelegramClient } from '@mtcute/core/client.js' import { sleep } from '@mtcute/core/utils.js' import { getApiParams } from '../utils.js' diff --git a/e2e/ts/tests/04.updates.ts b/e2e/ts/tests/04.updates.ts index 7e55c4b7..a6349b34 100644 --- a/e2e/ts/tests/04.updates.ts +++ b/e2e/ts/tests/04.updates.ts @@ -1,7 +1,8 @@ import { expect } from 'chai' import { describe, it } from 'mocha' -import { Message, TelegramClient } from '@mtcute/core' +import { Message } from '@mtcute/core' +import { TelegramClient } from '@mtcute/core/client.js' import { getApiParams, waitFor } from '../utils.js' diff --git a/e2e/ts/tests/packaging/tl-runtime.ts b/e2e/ts/tests/packaging/tl-runtime.ts index 8db7253a..16390583 100644 --- a/e2e/ts/tests/packaging/tl-runtime.ts +++ b/e2e/ts/tests/packaging/tl-runtime.ts @@ -3,34 +3,25 @@ import { expect } from 'chai' import Long from 'long' import { describe, it } from 'mocha' -import { - hexDecode, - hexDecodeToBuffer, - hexEncode, - TlBinaryReader, - TlBinaryWriter, - TlSerializationCounter, -} from '@mtcute/tl-runtime' +import { TlBinaryReader, TlBinaryWriter, TlSerializationCounter } from '@mtcute/tl-runtime' +import { NodePlatform } from '@mtcute/node' +import { setPlatform } from '@mtcute/core/platform.js' // here we primarily want to check that everything imports properly, // and that the code is actually executable. The actual correctness // of the implementation is covered tested by unit tests +const p = new NodePlatform() +setPlatform(p) + describe('encodings', () => { it('works with Buffers', () => { - const buf = Buffer.alloc(5) - hexDecode(buf, '0102030405') - - expect(hexEncode(Buffer.from('hello'))).to.equal('68656c6c6f') - expect(buf).eql(Buffer.from([1, 2, 3, 4, 5])) + expect(p.hexEncode(Buffer.from('hello'))).to.equal('68656c6c6f') + expect(p.hexDecode('0102030405')).eql(Buffer.from([1, 2, 3, 4, 5])) }) it('works with Uint8Arrays', () => { - const buf = new Uint8Array(5) - hexDecode(buf, '0102030405') - - expect(hexEncode(new Uint8Array([1, 2, 3, 4, 5]))).to.equal('0102030405') - expect(buf).eql(new Uint8Array([1, 2, 3, 4, 5])) + expect(p.hexEncode(new Uint8Array([1, 2, 3, 4, 5]))).to.equal('0102030405') }) }) @@ -62,7 +53,7 @@ describe('TlBinaryReader', () => { }) it('should work with Uint8Arrays', () => { - const buf = hexDecodeToBuffer(data) + const buf = p.hexDecode(data) const r = new TlBinaryReader(map, buf, 8) @@ -98,7 +89,7 @@ describe('TlBinaryWriter', () => { w.long(Long.fromString('51E57AC91E83C801', true, 16)) // messageId w.object(obj) - expect(hexEncode(w.result())).eq( + expect(p.hexEncode(w.result())).eq( '000000000000000001c8831ec97ae551632416050817ed48941a08f98100000015c4b51c01000000216be86c022bb4c3', ) }) @@ -106,7 +97,7 @@ describe('TlBinaryWriter', () => { it('should work with Uint8Arrays', () => { const obj = { _: 'mt_resPQ', - pq: hexDecodeToBuffer('17ED48941A08F981'), + pq: p.hexDecode('17ED48941A08F981'), serverPublicKeyFingerprints: [Long.fromString('c3b42b026ce86b21', 16)], } @@ -117,7 +108,7 @@ describe('TlBinaryWriter', () => { w.long(Long.fromString('51E57AC91E83C801', true, 16)) // messageId w.object(obj) - expect(hexEncode(w.result())).eq( + expect(p.hexEncode(w.result())).eq( '000000000000000001c8831ec97ae551632416050817ed48941a08f98100000015c4b51c01000000216be86c022bb4c3', ) }) diff --git a/e2e/ts/tests/packaging/tl-schema.ts b/e2e/ts/tests/packaging/tl-schema.ts index dd62f287..241fe134 100644 --- a/e2e/ts/tests/packaging/tl-schema.ts +++ b/e2e/ts/tests/packaging/tl-schema.ts @@ -2,13 +2,18 @@ import { expect } from 'chai' import Long from 'long' import { describe, it } from 'mocha' +import { setPlatform } from '@mtcute/core/platform.js' +import { NodePlatform } from '@mtcute/node' import { tl } from '@mtcute/tl' import { __tlReaderMap } from '@mtcute/tl/binary/reader.js' import { __tlWriterMap } from '@mtcute/tl/binary/writer.js' -import { hexDecodeToBuffer, hexEncode, TlBinaryReader, TlBinaryWriter } from '@mtcute/tl-runtime' +import { TlBinaryReader, TlBinaryWriter } from '@mtcute/tl-runtime' // here we primarily want to check that @mtcute/tl correctly works with @mtcute/tl-runtime +const p = new NodePlatform() +setPlatform(p) + describe('@mtcute/tl', () => { it('writers map works with TlBinaryWriter', () => { const obj = { @@ -17,13 +22,13 @@ describe('@mtcute/tl', () => { accessHash: Long.fromNumber(456), } - expect(hexEncode(TlBinaryWriter.serializeObject(__tlWriterMap, obj))).to.equal( + expect(p.hexEncode(TlBinaryWriter.serializeObject(__tlWriterMap, obj))).to.equal( '4ca5e8dd7b00000000000000c801000000000000', ) }) it('readers map works with TlBinaryReader', () => { - const buf = hexDecodeToBuffer('4ca5e8dd7b00000000000000c801000000000000') + const buf = p.hexDecode('4ca5e8dd7b00000000000000c801000000000000') // eslint-disable-next-line const obj = TlBinaryReader.deserializeObject(__tlReaderMap, buf) diff --git a/e2e/ts/tests/packaging/wasm.ts b/e2e/ts/tests/packaging/wasm.ts index acd40735..02067d0c 100644 --- a/e2e/ts/tests/packaging/wasm.ts +++ b/e2e/ts/tests/packaging/wasm.ts @@ -1,9 +1,12 @@ import { expect } from 'chai' import { before, describe, it } from 'mocha' -import { ige256Decrypt, ige256Encrypt, initAsync } from '@mtcute/wasm' +import { NodeCryptoProvider } from '@mtcute/node' +import { ige256Decrypt, ige256Encrypt } from '@mtcute/wasm' -before(() => initAsync()) +before(async () => { + await new NodeCryptoProvider().initialize() +}) describe('@mtcute/wasm', () => { const key = Buffer.from('5468697320697320616E20696D706C655468697320697320616E20696D706C65', 'hex') diff --git a/e2e/ts/utils.ts b/e2e/ts/utils.ts index 90f869f1..7a392e2c 100644 --- a/e2e/ts/utils.ts +++ b/e2e/ts/utils.ts @@ -2,7 +2,9 @@ import { join } from 'path' import { MaybePromise, MemoryStorage } from '@mtcute/core' +import { setPlatform } from '@mtcute/core/platform.js' import { LogManager, sleep } from '@mtcute/core/utils.js' +import { NodeCryptoProvider, NodePlatform, TcpTransport } from '@mtcute/node' import { SqliteStorage } from '@mtcute/sqlite' export const getApiParams = (storage?: string) => { @@ -10,12 +12,16 @@ export const getApiParams = (storage?: string) => { throw new Error('API_ID and API_HASH env variables must be set') } + setPlatform(new NodePlatform()) + return { apiId: parseInt(process.env.API_ID), apiHash: process.env.API_HASH, testMode: true, storage: storage ? new SqliteStorage(join(__dirname, storage)) : new MemoryStorage(), logLevel: LogManager.VERBOSE, + transport: () => new TcpTransport(), + crypto: new NodeCryptoProvider(), } } diff --git a/packages/wasm/build.config.cjs b/packages/wasm/build.config.cjs index 14c5233f..2d9238c2 100644 --- a/packages/wasm/build.config.cjs +++ b/packages/wasm/build.config.cjs @@ -1,3 +1,6 @@ -module.exports = () => ({ +module.exports = ({ path: { join }, fs, outDir, packageDir }) => ({ esmOnlyDirectives: true, + final() { + fs.cpSync(join(packageDir, 'lib/mtcute.wasm'), join(outDir, 'mtcute.wasm')) + }, })