switched to wrangler 2.0, fixed NaN check on track id, updated compat date

This commit is contained in:
uh wot 2022-05-13 00:25:00 +02:00
parent 0dcc61196f
commit 58884fb608
Signed by: uhwot
GPG Key ID: CB2454984587B781
12 changed files with 475 additions and 2684 deletions

14
.gitignore vendored
View File

@ -1,10 +1,8 @@
/target
/dist
**/*.rs.bk
Cargo.lock
bin/
pkg/
wasm-pack.log
worker/
node_modules/
.cargo-ok
wasm/pkg/
wasm/target
wasm/Cargo.lock
wasm/wasm-pack.log
wasm/.cargo-ok

View File

@ -1,6 +0,0 @@
{
"trailingComma": "es5",
"tabWidth": 4,
"semi": false,
"singleQuote": true
}

32
.vscode/tasks.json vendored
View File

@ -1,32 +0,0 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "wrangler build",
"problemMatcher": [],
"group": {
"kind": "build",
"isDefault": true
}
},
{
"label": "dev",
"type": "shell",
"command": "wrangler dev",
"problemMatcher": [],
"group": {
"kind": "test",
"isDefault": true
}
},
{
"label": "publish",
"type": "shell",
"command": "wrangler publish"
}
]
}

10
dist/worker.js vendored

File diff suppressed because one or more lines are too long

View File

@ -1,5 +1,8 @@
import html from './index.html'
import { Cipher, legacy_stream_url } from './wasm/pkg'
import { Router } from 'itty-router'
const ID3Writer = require('browser-id3-writer');
import ID3Writer from 'browser-id3-writer'
const router = Router()
@ -55,8 +58,7 @@ async function gw_api_call(method, params) {
checkForm = json.results.checkForm
await KV.put('checkForm', checkForm)
sid = response.headers.get('set-cookie').split(',').map(v => v.trimStart())[0]
sid = sid.match(/^sid=(fr[\da-f]+)/)[1]
sid = json.results.SESSION_ID
await KV.put('sid', sid)
}
@ -72,7 +74,7 @@ router.get('/:type/:id', async request => {
}
id = parseInt(id)
if (id === NaN || (type !== 'track' && id < 0)) {
if (isNaN(id) || (type !== 'track' && id < 0)) {
return new Response("Invalid ID", { status: 400, headers: { 'content-type': 'text/plain' } })
}
@ -202,8 +204,6 @@ async function track(id, format, tagging, range_header) {
}
}
const wasm = await import('./pkg')
let track_url
let use_legacy_url = !['320', 'flac'].includes(format)
if (!use_legacy_url) { // server-side stream url
@ -249,7 +249,7 @@ async function track(id, format, tagging, range_header) {
return new Response("Couldn't get stream URL", { status: 403, headers: { 'content-type': 'text/plain' } })
}
} else { // legacy stream url
track_url = await legacy_track_url(json, format, wasm.legacy_stream_url)
track_url = legacy_track_url(json, format)
}
let title = json.SNG_TITLE
@ -268,7 +268,7 @@ async function track(id, format, tagging, range_header) {
let id3_buffer
let id3_len = 0
if (tagging) {
let id3 = new ID3Writer(Buffer.alloc(0));
let id3 = new ID3Writer(new Uint8Array());
id3.padding = 0
id3.setFrame('TIT2', title)
@ -426,7 +426,7 @@ async function track(id, format, tagging, range_header) {
id = json.FALLBACK.SNG_ID
}
const cipher = new wasm.Cipher(String(id))
const cipher = new Cipher(String(id))
pipeDecryptedStream(writer, track.body, trk_range_end, cipher, trk_range_start, bytes_remove_start, bytes_remove_end)
} else {
@ -466,7 +466,7 @@ async function pipeDecryptedStream(writer, body, length, cipher, byte_count, byt
writer.close()
}
function legacy_track_url(json, format, url_func) {
function legacy_track_url(json, format) {
// needed if track has fallback, like https://www.deezer.com/track/11835714
if (json.FALLBACK) {
json = json.FALLBACK
@ -478,7 +478,7 @@ function legacy_track_url(json, format, url_func) {
format = formats[format].num
return url_func(md5_origin, format, id, media_version)
return legacy_stream_url(md5_origin, format, id, media_version)
}
async function m3u8(type, id, format, tagging, host) {
@ -495,7 +495,7 @@ async function m3u8(type, id, format, tagging, host) {
if (track.id < 0) { // user-uploaded track
format_tmp = 'misc'
}
let result = `https://${host}/track/${track.id}?f=${format_tmp}&t=${+ tagging}`
const result = `https://${host}/track/${track.id}?f=${format_tmp}&t=${+ tagging}`
list += `#EXTINF:${track.duration},${track.title}\n${result}\n`
}
@ -503,7 +503,7 @@ async function m3u8(type, id, format, tagging, host) {
}
router.get('/', () => {
return new Response(require('./index.html'), { status: 200, headers: { 'content-type': 'text/html' } })
return new Response(html, { status: 200, headers: { 'content-type': 'text/html' } })
})
router.all("*", () => new Response("not found", { status: 404, headers: { 'content-type': 'text/plain' } }))

View File

@ -4,19 +4,18 @@
"description": "worker for getting deezer track urls",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"format": "prettier --write '**/*.{js,css,json,md}'"
"dev": "wrangler dev",
"dev_local": "wrangler dev --local",
"publish": "wrangler publish",
"wasm_build": "node wasm_build.mjs"
},
"author": "uh_wot <uhwot@protonmail.com>",
"license": "MIT",
"devDependencies": {
"@wasm-tool/wasm-pack-plugin": "^1.6.0",
"html-loader": "^1.3.2",
"prettier": "^2.6.2"
"wrangler": "^2.0.3"
},
"dependencies": {
"browser-id3-writer": "^4.4.0",
"itty-router": "^2.6.1",
"serverless-cloudflare-workers": "^1.2.0"
"itty-router": "^2.6.1"
}
}

20
wasm_build.mjs Normal file
View File

@ -0,0 +1,20 @@
import * as fs from 'fs'
import { execSync } from 'child_process'
import { chdir } from 'process'
chdir('wasm')
execSync('wasm-pack build -t nodejs')
const pkg_name = JSON.parse(fs.readFileSync('pkg/package.json', 'utf8'))['name']
const js_path = `pkg/${pkg_name}.js`
let loader_js = fs.readFileSync(js_path, 'utf8')
loader_js = `import wasmModule from './${pkg_name}_bg.wasm'\n\n` + loader_js
loader_js = loader_js.replace('const { TextDecoder, TextEncoder } = require(String.raw`util`);\n', '')
loader_js = loader_js.replace(`const path = require('path').join(__dirname, '${pkg_name}_bg.wasm');\n`, '')
loader_js = loader_js.replace("const bytes = require('fs').readFileSync(path);\n\n", '')
loader_js = loader_js.replace('const wasmModule = new WebAssembly.Module(bytes);\n', '')
fs.writeFileSync(js_path, loader_js)

View File

@ -1,21 +0,0 @@
const path = require("path");
const WasmPackPlugin = require("@wasm-tool/wasm-pack-plugin");
module.exports = {
entry: {
index: "./index.js"
},
plugins: [
new WasmPackPlugin({
crateDirectory: __dirname,
}),
],
module: {
rules: [
{
test: /\.html$/i,
loader: 'html-loader'
}
]
}
};

View File

@ -1,12 +1,14 @@
name = "dz"
type = "webpack"
webpack_config = "webpack.config.js"
account_id = "03479b0523a52b140e0dabac40cb0fc8"
workers_dev = true
kv_namespaces = [
{ binding = "KV", id = "974c0967a84e415daa054bbbcc7f80c6", preview_id = "cfcc6491f3484cbca664913836635113" }
]
compatibility_date = "2022-04-30"
main = "./main.js"
compatibility_date = "2022-05-12"
[build]
command = "yarn run wasm_build"
watch_dir = "./wasm/src"
# [secrets]
# ARL

3007
yarn.lock

File diff suppressed because it is too large Load Diff