javascript-ampache 1.1.8 → 1.1.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/utils.d.ts CHANGED
@@ -1,2 +1,4 @@
1
+ import { Base } from "./base";
1
2
  export declare function applyMixins(derivedCtor: any, baseCtors: any[]): void;
2
3
  export declare function encryptPassword(password: string, time: number): string;
4
+ export declare function outputDebugURL(url: string, config: Base): void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "javascript-ampache",
3
- "version": "1.1.8",
3
+ "version": "1.1.10",
4
4
  "description": "A JS library for the Ampache API",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.m.js",
package/src/auth/index.ts CHANGED
@@ -3,6 +3,7 @@ import qs from "querystringify";
3
3
  import fetch from "isomorphic-unfetch";
4
4
  import { Base, Success } from "../base";
5
5
  import { AuthResponse } from "./types";
6
+ import { outputDebugURL } from "../utils";
6
7
 
7
8
  export class Auth extends Base {
8
9
  /**
@@ -20,6 +21,8 @@ export class Auth extends Base {
20
21
  timestamp?: number;
21
22
  version?: string;
22
23
  }) {
24
+ let token = params.auth;
25
+
23
26
  // generate a timestamp if one wasn't provided
24
27
  if (!params.timestamp) {
25
28
  params.timestamp = Math.floor(new Date().getTime() / 1000);
@@ -35,10 +38,22 @@ export class Auth extends Base {
35
38
  delete params.timestamp;
36
39
  }
37
40
 
41
+ // not needed if using Bearer token
42
+ if (this.useBearerToken) {
43
+ delete params.auth;
44
+ }
45
+
38
46
  let query = this.url + "/server/json.server.php?action=handshake";
39
47
  query += qs.stringify(params, "&");
40
48
 
41
- return fetch(query)
49
+ if (this.debug) {
50
+ outputDebugURL(query, this);
51
+ }
52
+
53
+ return fetch(query, {
54
+ method: "GET",
55
+ headers: this.useBearerToken ? { Authorization: "Bearer " + token } : {},
56
+ })
42
57
  .then((response) => response.json())
43
58
  .then((data) => {
44
59
  if (data.auth) {
package/src/base.ts CHANGED
@@ -1,10 +1,11 @@
1
1
  import fetch from "isomorphic-unfetch";
2
2
  import qs from "querystringify";
3
+ import { outputDebugURL } from "./utils";
3
4
 
4
5
  type Config = {
5
6
  url: string;
6
7
  sessionKey?: string;
7
- useBearerToken: false;
8
+ useBearerToken?: boolean;
8
9
  debug?: boolean;
9
10
  };
10
11
 
@@ -51,7 +52,6 @@ export abstract class Base {
51
52
  }
52
53
 
53
54
  protected request<T>(endpoint: string): Promise<T> {
54
- let authString = "&auth=" + this.sessionKey;
55
55
  let url =
56
56
  this.url +
57
57
  "/server/json.server.php?action=" +
@@ -60,21 +60,16 @@ export abstract class Base {
60
60
  this.version;
61
61
 
62
62
  if (!this.useBearerToken) {
63
- url += authString;
63
+ url += "&auth=" + this.sessionKey;
64
64
  }
65
65
 
66
66
  if (this.debug) {
67
- console.debug(
68
- "javascript-ampache query URL %c" + url + authString,
69
- "color: black; font-style: italic; background-color: orange;padding: 2px",
70
- );
67
+ outputDebugURL(url, this);
71
68
  }
72
69
 
73
70
  return fetch(url, {
74
71
  method: "GET",
75
- headers: {
76
- Authorization: this.useBearerToken ? "Bearer " + this.sessionKey : undefined,
77
- },
72
+ headers: this.useBearerToken ? { Authorization: "Bearer " + this.sessionKey } : {},
78
73
  }).then((r) => {
79
74
  if (r.ok) {
80
75
  return r.json();
@@ -84,28 +79,22 @@ export abstract class Base {
84
79
  }
85
80
 
86
81
  protected binary<T>(endpoint: string): Promise<Blob> {
87
- let authString = "&auth=" + this.sessionKey;
88
82
  let url =
89
83
  this.url +
90
84
  "/server/json.server.php?action=" + endpoint +
91
85
  "&version=" + this.version;
92
86
 
93
87
  if (!this.useBearerToken) {
94
- url += authString;
88
+ url += "&auth=" + this.sessionKey;
95
89
  }
96
90
 
97
91
  if (this.debug) {
98
- console.debug(
99
- "javascript-ampache query URL %c" + url + this.useBearerToken ? "&auth=" + this.sessionKey : "",
100
- "color: black; font-style: italic; background-color: orange;padding: 2px",
101
- );
92
+ outputDebugURL(url, this);
102
93
  }
103
94
 
104
95
  return fetch(url, {
105
96
  method: "GET",
106
- headers: {
107
- Authorization: this.useBearerToken ? "Bearer " + this.sessionKey : undefined,
108
- },
97
+ headers: this.useBearerToken ? { Authorization: "Bearer " + this.sessionKey } : {},
109
98
  })
110
99
  .then((response) => response.blob())
111
100
  .then((r) => {
@@ -131,11 +120,12 @@ export abstract class Base {
131
120
  "/server/json.server.php?action=" + query +
132
121
  "&version=" + this.version;
133
122
 
123
+ if (!this.useBearerToken) {
124
+ url += "&auth=" + this.sessionKey;
125
+ }
126
+
134
127
  if (this.debug) {
135
- console.debug(
136
- "javascript-ampache query URL %c" + url + this.useBearerToken ? "&auth=" + this.sessionKey : "",
137
- "color: black; font-style: italic; background-color: orange;padding: 2px",
138
- );
128
+ outputDebugURL(url, this);
139
129
  }
140
130
 
141
131
  return url;
package/src/utils.ts CHANGED
@@ -1,25 +1,40 @@
1
- import JsSHA from "jssha/dist/sha256";
2
-
3
- export function applyMixins(derivedCtor: any, baseCtors: any[]) {
4
- baseCtors.forEach((baseCtor) => {
5
- Object.getOwnPropertyNames(baseCtor.prototype).forEach((name) => {
6
- Object.defineProperty(
7
- derivedCtor.prototype,
8
- name,
9
- Object.getOwnPropertyDescriptor(baseCtor.prototype, name),
10
- );
11
- });
12
- });
13
- }
14
-
15
- export function encryptPassword(password: string, time: number) {
16
- let key = getSHA256(password);
17
- return getSHA256(time + key);
18
-
19
- function getSHA256(text) {
20
- let shaObj = new JsSHA("SHA-256", "TEXT", { encoding: "UTF8" });
21
- shaObj.update(text);
22
-
23
- return shaObj.getHash("HEX");
24
- }
25
- }
1
+ import JsSHA from "jssha/dist/sha256";
2
+ import { Base } from "./base";
3
+
4
+ export function applyMixins(derivedCtor: any, baseCtors: any[]) {
5
+ baseCtors.forEach((baseCtor) => {
6
+ Object.getOwnPropertyNames(baseCtor.prototype).forEach((name) => {
7
+ Object.defineProperty(
8
+ derivedCtor.prototype,
9
+ name,
10
+ Object.getOwnPropertyDescriptor(baseCtor.prototype, name),
11
+ );
12
+ });
13
+ });
14
+ }
15
+
16
+ export function encryptPassword(password: string, time: number) {
17
+ let key = getSHA256(password);
18
+ return getSHA256(time + key);
19
+
20
+ function getSHA256(text) {
21
+ let shaObj = new JsSHA("SHA-256", "TEXT", { encoding: "UTF8" });
22
+ shaObj.update(text);
23
+
24
+ return shaObj.getHash("HEX");
25
+ }
26
+ }
27
+
28
+ export function outputDebugURL(url: string, config: Base) {
29
+ let label = "javascript-ampache query URL";
30
+
31
+ if (config.useBearerToken) {
32
+ label = "(Using Bearer token, auth added for debugging) - " + label;
33
+ url += "&auth=" + config.sessionKey;
34
+ }
35
+
36
+ console.debug(
37
+ label + " %c" + url,
38
+ "color: black; font-style: italic; background-color: orange;padding: 2px",
39
+ );
40
+ }