Skip to content

Commit

Permalink
add: deno support
Browse files Browse the repository at this point in the history
  • Loading branch information
butthx committed Apr 10, 2023
1 parent e4d4bde commit 49a3a7d
Show file tree
Hide file tree
Showing 9 changed files with 285 additions and 144 deletions.
12 changes: 5 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
{
"name": "@tgsnake/fileid",
"version": "2.0.2",
"version": "2.1.0",
"description": "core framework for tgsnake for generating file id",
"main": "./lib/index.js",
"devDependencies": {
"@types/node": "18.15.8",
"prettier": "2.8.7",
"ts-node": "10.9.1",
"typescript": "5.0.2"
"deno2node": "1.8.1",
"prettier": "2.8.7"
},
"scripts": {
"start": "node tes/index.js",
"build": "tsc",
"build": "deno2node tsconfig.json",
"build:license": "node ./generator/license",
"dev": "ts-node tes/index.ts",
"prepare": "tsc",
"prepare": "deno2node tsconfig.json",
"prettier": "prettier -w ."
},
"homepage": "https://tgsnake.js.org",
Expand Down
4 changes: 2 additions & 2 deletions src/decode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import {
Reader,
PHOTO_TYPES,
Options,
} from './';

} from './index.ts';
import { Buffer } from 'node:buffer';
export class Decode implements Options {
/**
* The major version of bot api file id. Usually is 4.
Expand Down
5 changes: 3 additions & 2 deletions src/encode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ import {
PHOTO_TYPES,
DOCUMENT_TYPES,
Options,
} from './';
import type { Decode } from './decode';
} from './index.ts';
import { Buffer } from 'node:buffer';
import type { Decode } from './decode.ts';

export class Encode {
/**
Expand Down
10 changes: 5 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ export {
Reader,
PHOTO_TYPES,
DOCUMENT_TYPES,
Options,
} from './utils';
export { FileId } from './parser';
export { Encode } from './encode';
export { Decode } from './decode';
type Options,
} from './utils.ts';
export { FileId } from './parser.ts';
export { Encode } from './encode.ts';
export { Decode } from './decode.ts';
7 changes: 4 additions & 3 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
* tgsnake is a free software : you can redistribute it and/or modify
* it under the terms of the MIT License as published.
*/
import { Decode } from './decode';
import { Encode } from './encode';
import { Options } from './utils';
import { Decode } from './decode.ts';
import { Encode } from './encode.ts';
import { Options } from './utils.ts';
import { Buffer } from 'node:buffer';

export namespace FileId {
/**
Expand Down
12 changes: 11 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* tgsnake is a free software : you can redistribute it and/or modify
* it under the terms of the MIT License as published.
*/
import { Buffer } from 'node:buffer';

export enum FileType {
THUMBNAIL = 0,
Expand Down Expand Up @@ -178,7 +179,16 @@ export function base64_url_encode(base: string | Buffer): string {
: base.toString('base64url');
}
export function base64_url_decode(base: string | Buffer): Buffer {
return typeof base === 'string' ? Buffer.from(base, 'base64url') : base;
return typeof base === 'string' ? Buffer.from(base64urlTobase64(base), 'base64') : base;
}
export function base64urlTobase64(text: string): string {
const pad = text.length % 4;
if (pad === 1) {
throw new Error('Invalid base64url');
}
return (pad === 2 || pad === 3 ? text.padEnd(4 - pad, '=') : text)
.replace(/\-/g, '+')
.replace(/_/g, '/');
}
export function rle_encode(base: string | Buffer): Buffer {
let buffer: Buffer = typeof base === 'string' ? Buffer.from(base) : base;
Expand Down
38 changes: 38 additions & 0 deletions tes/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* tgsnake - Telegram MTProto framework for nodejs.
* Copyright (C) 2022 butthx <https://github.com/butthx>
*
* THIS FILE IS PART OF TGSNAKE
*
* tgsnake is a free software : you can redistribute it and/or modify
* it under the terms of the MIT License as published.
*/

const { FileId } = require('../lib');
const file = FileId.decodeFileId('AQADBQADZq8xG6uF-FQAEAIAAyGEcyoBAANi-pbYnH388wAEIAQ');
console.log(
'AQADBQADZq8xG6uF-FQAEAIAAyGEcyoBAANi-pbYnH388wAEIAQ',
file,
FileId.encodeFileId(file),
FileId.encodeFileId({
version: 4,
subVersion: 32,
dcId: 5,
fileType: 1,
id: BigInt('6122790663352332134'),
accessHash: BigInt(0),
fileReference: undefined,
url: undefined,
volumeId: BigInt(0),
localId: 0,
secret: undefined,
chatId: BigInt('5007180833'),
chatAccessHash: BigInt('-865678915759834526'),
stickerSetId: undefined,
stickerSetAccessHash: undefined,
thumbnailSource: 2,
thumbnailFileType: undefined,
thumbnailSize: undefined,
fileTypeUniqueId: undefined,
})
);
3 changes: 2 additions & 1 deletion tes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
* it under the terms of the MIT License as published.
*/

import { FileId } from '../src';
import { FileId } from '../src/index.ts';
const file = FileId.decodeFileId('AQADBQADZq8xG6uF-FQAEAIAAyGEcyoBAANi-pbYnH388wAEIAQ');
console.log(
'AQADBQADZq8xG6uF-FQAEAIAAyGEcyoBAANi-pbYnH388wAEIAQ',
file,
FileId.encodeFileId(file),
FileId.encodeFileId({
Expand Down
Loading

0 comments on commit 49a3a7d

Please sign in to comment.