@difizen/libro-common 0.0.2-alpha.0

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.
Files changed (71) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +0 -0
  3. package/es/array.d.ts +368 -0
  4. package/es/array.d.ts.map +1 -0
  5. package/es/array.js +577 -0
  6. package/es/display-wrapper.d.ts +6 -0
  7. package/es/display-wrapper.d.ts.map +1 -0
  8. package/es/display-wrapper.js +12 -0
  9. package/es/index.d.ts +11 -0
  10. package/es/index.d.ts.map +1 -0
  11. package/es/index.js +10 -0
  12. package/es/iter.d.ts +147 -0
  13. package/es/iter.d.ts.map +1 -0
  14. package/es/iter.js +162 -0
  15. package/es/json.d.ts +126 -0
  16. package/es/json.d.ts.map +1 -0
  17. package/es/json.js +274 -0
  18. package/es/path.d.ts +97 -0
  19. package/es/path.d.ts.map +1 -0
  20. package/es/path.js +60 -0
  21. package/es/polling/index.d.ts +3 -0
  22. package/es/polling/index.d.ts.map +1 -0
  23. package/es/polling/index.js +2 -0
  24. package/es/polling/poll.d.ts +193 -0
  25. package/es/polling/poll.d.ts.map +1 -0
  26. package/es/polling/poll.js +501 -0
  27. package/es/polling/protocol.d.ts +120 -0
  28. package/es/polling/protocol.d.ts.map +1 -0
  29. package/es/polling/protocol.js +13 -0
  30. package/es/posix.d.ts +2 -0
  31. package/es/posix.d.ts.map +1 -0
  32. package/es/posix.js +71 -0
  33. package/es/protocol/cell-protocol.d.ts +181 -0
  34. package/es/protocol/cell-protocol.d.ts.map +1 -0
  35. package/es/protocol/cell-protocol.js +1 -0
  36. package/es/protocol/index.d.ts +4 -0
  37. package/es/protocol/index.d.ts.map +1 -0
  38. package/es/protocol/index.js +3 -0
  39. package/es/protocol/notebook-protocol.d.ts +63 -0
  40. package/es/protocol/notebook-protocol.d.ts.map +1 -0
  41. package/es/protocol/notebook-protocol.js +41 -0
  42. package/es/protocol/output-protocol.d.ts +125 -0
  43. package/es/protocol/output-protocol.d.ts.map +1 -0
  44. package/es/protocol/output-protocol.js +1 -0
  45. package/es/sanitizer.d.ts +44 -0
  46. package/es/sanitizer.d.ts.map +1 -0
  47. package/es/sanitizer.js +659 -0
  48. package/es/url.d.ts +98 -0
  49. package/es/url.d.ts.map +1 -0
  50. package/es/url.js +134 -0
  51. package/es/utils.d.ts +57 -0
  52. package/es/utils.d.ts.map +1 -0
  53. package/es/utils.js +124 -0
  54. package/package.json +62 -0
  55. package/src/array.ts +608 -0
  56. package/src/display-wrapper.tsx +11 -0
  57. package/src/index.ts +10 -0
  58. package/src/iter.ts +199 -0
  59. package/src/json.ts +321 -0
  60. package/src/path.ts +138 -0
  61. package/src/polling/index.ts +2 -0
  62. package/src/polling/poll.ts +508 -0
  63. package/src/polling/protocol.ts +145 -0
  64. package/src/posix.ts +75 -0
  65. package/src/protocol/cell-protocol.ts +215 -0
  66. package/src/protocol/index.ts +3 -0
  67. package/src/protocol/notebook-protocol.ts +73 -0
  68. package/src/protocol/output-protocol.ts +162 -0
  69. package/src/sanitizer.ts +944 -0
  70. package/src/url.ts +157 -0
  71. package/src/utils.ts +145 -0
package/src/url.ts ADDED
@@ -0,0 +1,157 @@
1
+ import { isWeb } from '@difizen/mana-common';
2
+ import { posix } from 'path-browserify';
3
+ import urlparse from 'url-parse';
4
+
5
+ /**
6
+ * The namespace for URL-related functions.
7
+ */
8
+ // eslint-disable-next-line @typescript-eslint/no-namespace
9
+ export class URL {
10
+ /**
11
+ * Parse URL and retrieve hostname
12
+ *
13
+ * @param url - The URL string to parse
14
+ *
15
+ * @returns a hostname string value
16
+ */
17
+ static getHostName(url: string): string {
18
+ return urlparse(url).hostname;
19
+ }
20
+ /**
21
+ * Normalize a url.
22
+ */
23
+ static normalize(url: string): string;
24
+ static normalize(url: undefined): undefined;
25
+ static normalize(url: string | undefined): string | undefined;
26
+ static normalize(url: string | undefined): string | undefined {
27
+ if (!url) {
28
+ return undefined;
29
+ }
30
+ if (isWeb) {
31
+ const a = document.createElement('a');
32
+ a.href = url;
33
+ return a.toString();
34
+ }
35
+ return urlparse(url).toString();
36
+ }
37
+
38
+ /**
39
+ * Join a sequence of url components and normalizes as in node `path.join`.
40
+ *
41
+ * @param parts - The url components.
42
+ *
43
+ * @returns the joined url.
44
+ */
45
+ static join(...parts: string[]): string {
46
+ let u = urlparse(parts[0], {});
47
+ // Schema-less URL can be only parsed as relative to a base URL
48
+ // see https://github.com/unshiftio/url-parse/issues/219#issuecomment-1002219326
49
+ const isSchemaLess = u.protocol === '' && u.slashes;
50
+ if (isSchemaLess) {
51
+ u = urlparse(parts[0], 'https:' + parts[0]);
52
+ }
53
+ const prefix = `${isSchemaLess ? '' : u.protocol}${u.slashes ? '//' : ''}${u.auth}${
54
+ u.auth ? '@' : ''
55
+ }${u.host}`;
56
+ // If there was a prefix, then the first path must start at the root.
57
+ const path = posix.join(
58
+ `${!!prefix && u.pathname[0] !== '/' ? '/' : ''}${u.pathname}`,
59
+ ...parts.slice(1),
60
+ );
61
+ return `${prefix}${path === '.' ? '' : path}`;
62
+ }
63
+
64
+ /**
65
+ * Encode the components of a multi-segment url.
66
+ *
67
+ * @param url - The url to encode.
68
+ *
69
+ * @returns the encoded url.
70
+ *
71
+ * #### Notes
72
+ * Preserves the `'/'` separators.
73
+ * Should not include the base url, since all parts are escaped.
74
+ */
75
+ static encodeParts(url: string): string {
76
+ return URL.join(...url.split('/').map(encodeURIComponent));
77
+ }
78
+ /**
79
+ * Parse a url into a URL object.
80
+ *
81
+ * @param urlString - The URL string to parse.
82
+ *
83
+ * @returns A URL object.
84
+ */
85
+ static parse(url: string): IUrl {
86
+ if (typeof document !== 'undefined' && document) {
87
+ const a = document.createElement('a');
88
+ a.href = url;
89
+ return a;
90
+ }
91
+ return urlparse(url);
92
+ }
93
+ /**
94
+ * Test whether the url is a local url.
95
+ *
96
+ * #### Notes
97
+ * This function returns `false` for any fully qualified url, including
98
+ * `data:`, `file:`, and `//` protocol URLs.
99
+ */
100
+ static isLocal(url: string): boolean {
101
+ const { protocol } = URL.parse(url);
102
+
103
+ return (
104
+ (!protocol || url.toLowerCase().indexOf(protocol) !== 0) && url.indexOf('/') !== 0
105
+ );
106
+ }
107
+ }
108
+
109
+ /**
110
+ * The interface for a URL object
111
+ */
112
+ export interface IUrl {
113
+ /**
114
+ * The full URL string that was parsed with both the protocol and host
115
+ * components converted to lower-case.
116
+ */
117
+ href: string;
118
+
119
+ /**
120
+ * Identifies the URL's lower-cased protocol scheme.
121
+ */
122
+ protocol: string;
123
+
124
+ /**
125
+ * The full lower-cased host portion of the URL, including the port if
126
+ * specified.
127
+ */
128
+ host: string;
129
+
130
+ /**
131
+ * The lower-cased host name portion of the host component without the
132
+ * port included.
133
+ */
134
+ hostname: string;
135
+
136
+ /**
137
+ * The numeric port portion of the host component.
138
+ */
139
+ port: string;
140
+
141
+ /**
142
+ * The entire path section of the URL.
143
+ */
144
+ pathname: string;
145
+
146
+ /**
147
+ * The "fragment" portion of the URL including the leading ASCII hash
148
+ * `(#)` character
149
+ */
150
+ hash: string;
151
+
152
+ /**
153
+ * The search element, including leading question mark (`'?'`), if any,
154
+ * of the URL.
155
+ */
156
+ search?: string;
157
+ }
package/src/utils.ts ADDED
@@ -0,0 +1,145 @@
1
+ import type { PartialJSONObject, PartialJSONValue } from './json.js';
2
+ import { isObject } from './json.js';
3
+ import type {
4
+ ICell,
5
+ ICodeCell,
6
+ IMarkdownCell,
7
+ IRawCell,
8
+ } from './protocol/cell-protocol.js';
9
+ import type { MultilineString } from './protocol/notebook-protocol.js';
10
+ import type {
11
+ IDisplayData,
12
+ IDisplayUpdate,
13
+ IError,
14
+ IExecuteResult,
15
+ IOutput,
16
+ IStream,
17
+ } from './protocol/output-protocol.js';
18
+
19
+ /**
20
+ * Validate a mime type/value pair.
21
+ *
22
+ * @param type - The mimetype name.
23
+ *
24
+ * @param value - The value associated with the type.
25
+ *
26
+ * @returns Whether the type/value pair are valid.
27
+ */
28
+ export function validateMimeValue(
29
+ type: string,
30
+ value: MultilineString | PartialJSONObject,
31
+ ): boolean {
32
+ // Check if "application/json" or "application/foo+json"
33
+ const jsonTest = /^application\/.+\+json$/;
34
+ const isJSONType = type === 'application/json' || jsonTest.test(type);
35
+
36
+ const isString = (x: any) => {
37
+ return Object.prototype.toString.call(x) === '[object String]';
38
+ };
39
+
40
+ // If it is an array, make sure if is not a JSON type and it is an
41
+ // array of strings.
42
+ if (Array.isArray(value)) {
43
+ if (isJSONType) {
44
+ return false;
45
+ }
46
+ let valid = true;
47
+ value.forEach((v) => {
48
+ if (!isString(v)) {
49
+ valid = false;
50
+ }
51
+ });
52
+ return valid;
53
+ }
54
+
55
+ // If it is a string, make sure we are not a JSON type.
56
+ if (isString(value)) {
57
+ return !isJSONType;
58
+ }
59
+
60
+ // It is not a string, make sure it is a JSON type.
61
+ if (!isJSONType) {
62
+ return false;
63
+ }
64
+
65
+ // It is a JSON type, make sure it is a valid JSON object.
66
+ return isObject(value);
67
+ }
68
+
69
+ /**
70
+ * join multiline string, normalizing line endings to \n
71
+ * @param value
72
+ * @returns
73
+ */
74
+ export function concatMultilineString(value: MultilineString): string {
75
+ if (Array.isArray(value)) {
76
+ return value.map((s) => s.replace(/\r\n/g, '\n').replace(/\r/g, '\n')).join('');
77
+ } else {
78
+ return value.replace(/\r\n/g, '\n').replace(/\r/g, '\n');
79
+ }
80
+ }
81
+
82
+ /**
83
+ * Test whether a cell is a raw cell.
84
+ */
85
+ export function isRaw(cell: ICell): cell is IRawCell {
86
+ return cell.cell_type === 'raw';
87
+ }
88
+
89
+ /**
90
+ * Test whether a cell is a markdown cell.
91
+ */
92
+ export function isMarkdown(cell: ICell): cell is IMarkdownCell {
93
+ return cell.cell_type === 'markdown';
94
+ }
95
+
96
+ /**
97
+ * Test whether a cell is a code cell.
98
+ */
99
+ export function isCode(cell: ICell): cell is ICodeCell {
100
+ return cell.cell_type === 'code';
101
+ }
102
+
103
+ /**
104
+ * Test whether a cell is a code cell.
105
+ */
106
+ export function isOutput(
107
+ output: PartialJSONValue | IOutput[] | undefined,
108
+ ): output is IOutput[] {
109
+ return !!(output && output instanceof Array);
110
+ }
111
+
112
+ /**
113
+ * Test whether an output is an execute result.
114
+ */
115
+ export function isExecuteResult(output: IOutput): output is IExecuteResult {
116
+ return output.output_type === 'execute_result';
117
+ }
118
+
119
+ /**
120
+ * Test whether an output is from display data.
121
+ */
122
+ export function isDisplayData(output: IOutput): output is IDisplayData {
123
+ return output.output_type === 'display_data';
124
+ }
125
+
126
+ /**
127
+ * Test whether an output is from updated display data.
128
+ */
129
+ export function isDisplayUpdate(output: IOutput): output is IDisplayUpdate {
130
+ return output.output_type === 'update_display_data';
131
+ }
132
+
133
+ /**
134
+ * Test whether an output is from a stream.
135
+ */
136
+ export function isStream(output: IOutput): output is IStream {
137
+ return output.output_type === 'stream';
138
+ }
139
+
140
+ /**
141
+ * Test whether an output is an error.
142
+ */
143
+ export function isError(output: IOutput): output is IError {
144
+ return output.output_type === 'error';
145
+ }