@metrc/retailid 0.10.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.
- package/README.md +226 -0
- package/dist/retailid.cjs +341 -0
- package/dist/retailid.cjs.map +1 -0
- package/dist/retailid.esm.js +331 -0
- package/dist/retailid.esm.js.map +1 -0
- package/dist/retailid.min.js +36 -0
- package/dist/retailid.min.js.map +1 -0
- package/dist/src/index.d.ts +41 -0
- package/dist/test/browser.test.d.ts +1 -0
- package/package.json +60 -0
package/README.md
ADDED
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
# RetailID JavaScript SDK
|
|
2
|
+
|
|
3
|
+
Official JavaScript/TypeScript SDK for integrating with [Metrc RetailID](https://www.metrc.com/retailid/).
|
|
4
|
+
|
|
5
|
+
Encode and decode RetailID QR labels to enable product traceability and supply chain visibility in your web and Node.js applications.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
### npm / yarn / pnpm
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install @metrc/retailid
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
### CDN (Browser)
|
|
16
|
+
|
|
17
|
+
```html
|
|
18
|
+
<!-- Latest version -->
|
|
19
|
+
<script src="https://cdn.1a4.com/qr/js/retailid.min.js"></script>
|
|
20
|
+
|
|
21
|
+
<!-- Or pinned version -->
|
|
22
|
+
<script src="https://cdn.1a4.com/qr/js/v0.10.0/retailid.min.js"></script>
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
The CDN script exposes a global `RetailId` object.
|
|
26
|
+
|
|
27
|
+
---
|
|
28
|
+
|
|
29
|
+
## Quick Start
|
|
30
|
+
|
|
31
|
+
```javascript
|
|
32
|
+
import { ObjectId, RetailIdPair, getShortUrl } from '@metrc/retailid';
|
|
33
|
+
|
|
34
|
+
// Encode a batch ID and index to a short URL
|
|
35
|
+
const batchId = new ObjectId('1a4060300020081000006609');
|
|
36
|
+
const url = getShortUrl(batchId, 1);
|
|
37
|
+
// => "HTTPS://1A4.COM/5LN8CBN1UB33DON9CHKX"
|
|
38
|
+
|
|
39
|
+
// Decode a short URL back to its components
|
|
40
|
+
const pair = new RetailIdPair('HTTPS://1A4.COM/5LN8CBN1UB33DON9CHKX');
|
|
41
|
+
console.log(pair.batchId.toHexString()); // "1a4060300020081000006609"
|
|
42
|
+
console.log(pair.index); // 1
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
---
|
|
46
|
+
|
|
47
|
+
## Decoding a QR URL
|
|
48
|
+
|
|
49
|
+
Parse a scanned QR code URL to extract the tag (batch ID) and index:
|
|
50
|
+
|
|
51
|
+
```javascript
|
|
52
|
+
const pair = new RetailIdPair("HTTPS://1A4.COM/5LN8CBN1UB33DON9CHKX");
|
|
53
|
+
|
|
54
|
+
console.log(pair.batchId.toHexString()); // "1a4060300020081000006609"
|
|
55
|
+
console.log(pair.index); // 1
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
Both URL formats are supported:
|
|
59
|
+
- **Base36 (current):** `HTTPS://1A4.COM/5LN8CBN1UB33DON9CHKX`
|
|
60
|
+
- **Base64 (legacy):** `https://1a4.com/GkBgMAAEG2AAAJKXEQ`
|
|
61
|
+
|
|
62
|
+
The SDK auto-detects the format based on URL case:
|
|
63
|
+
- Uppercase `HTTPS://` → Base36 decoding
|
|
64
|
+
- Lowercase `https://` → Base64 decoding
|
|
65
|
+
|
|
66
|
+
---
|
|
67
|
+
|
|
68
|
+
## Creating a URL from Tag + Index
|
|
69
|
+
|
|
70
|
+
Generate a URL directly from a tag (hex string) and index:
|
|
71
|
+
|
|
72
|
+
```javascript
|
|
73
|
+
const tag = "1a4060300020081000006609";
|
|
74
|
+
const index = 1;
|
|
75
|
+
|
|
76
|
+
// Base36 (default)
|
|
77
|
+
const url = getShortUrl(new ObjectId(tag), index);
|
|
78
|
+
// "HTTPS://1A4.COM/5LN8CBN1UB33DON9CHKX"
|
|
79
|
+
|
|
80
|
+
// Base64
|
|
81
|
+
const legacyUrl = getShortUrl(new ObjectId(tag), index, { base64: true });
|
|
82
|
+
// "https://1a4.com/GkBgAwACAIEAAAZmCQE"
|
|
83
|
+
|
|
84
|
+
// Custom domain
|
|
85
|
+
const customUrl = getShortUrl(new ObjectId(tag), index, { domain: 'example.com' });
|
|
86
|
+
// "HTTPS://EXAMPLE.COM/5LN8CBN1UB33DON9CHKX"
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
---
|
|
90
|
+
|
|
91
|
+
## Full Round-Trip Example
|
|
92
|
+
|
|
93
|
+
```javascript
|
|
94
|
+
// 1. Scan a QR code
|
|
95
|
+
const scannedUrl = "HTTPS://1A4.COM/5LN8CBN1UB33DON9CHKX";
|
|
96
|
+
|
|
97
|
+
// 2. Decode to get tag and index
|
|
98
|
+
const pair = new RetailIdPair(scannedUrl);
|
|
99
|
+
const tag = pair.batchId.toHexString(); // "1a4060300020081000006609"
|
|
100
|
+
const index = pair.index; // 1
|
|
101
|
+
|
|
102
|
+
// 3. Use tag and index in your application
|
|
103
|
+
console.log(`Tag: ${tag}, Index: ${index}`);
|
|
104
|
+
|
|
105
|
+
// 4. Re-encode if needed
|
|
106
|
+
const newUrl = pair.encode();
|
|
107
|
+
console.log(newUrl); // "HTTPS://1A4.COM/5LN8CBN1UB33DON9CHKX"
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
---
|
|
111
|
+
|
|
112
|
+
## API Reference
|
|
113
|
+
|
|
114
|
+
### ObjectId
|
|
115
|
+
|
|
116
|
+
A 12-byte MongoDB-compatible identifier.
|
|
117
|
+
|
|
118
|
+
```typescript
|
|
119
|
+
// Create from hex string
|
|
120
|
+
const oid = new ObjectId('1a40603000041b6000009297');
|
|
121
|
+
|
|
122
|
+
// Create from Uint8Array
|
|
123
|
+
const oid = new ObjectId(bytes);
|
|
124
|
+
|
|
125
|
+
// Generate new random ObjectId
|
|
126
|
+
const oid = new ObjectId();
|
|
127
|
+
|
|
128
|
+
// Properties and methods
|
|
129
|
+
oid.id; // Uint8Array (12 bytes)
|
|
130
|
+
oid.toHexString(); // "1a40603000041b6000009297"
|
|
131
|
+
oid.equals(other); // boolean
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
### VarInt
|
|
135
|
+
|
|
136
|
+
Variable-length integer encoding (LEB128).
|
|
137
|
+
|
|
138
|
+
```typescript
|
|
139
|
+
// Create from number
|
|
140
|
+
const v = new VarInt(128);
|
|
141
|
+
|
|
142
|
+
// Create from bytes
|
|
143
|
+
const v = new VarInt(bytes);
|
|
144
|
+
|
|
145
|
+
// Static methods
|
|
146
|
+
VarInt.encode(128); // Uint8Array
|
|
147
|
+
VarInt.decode(bytes, offset); // { value: number, length: number }
|
|
148
|
+
|
|
149
|
+
// Instance methods
|
|
150
|
+
v.toHexString(); // hex representation
|
|
151
|
+
v.toString(); // decimal string
|
|
152
|
+
v.length(); // byte length
|
|
153
|
+
v.data(); // Uint8Array
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
### RetailIdPair
|
|
157
|
+
|
|
158
|
+
Decoded RetailID representation.
|
|
159
|
+
|
|
160
|
+
| Method | Description |
|
|
161
|
+
|--------|-------------|
|
|
162
|
+
| `new RetailIdPair(url)` | Decode a QR URL into tag + index |
|
|
163
|
+
| `pair.batchId` | ObjectId - the batch identifier |
|
|
164
|
+
| `pair.index` | number - the index within the batch |
|
|
165
|
+
| `pair.encode()` | Re-encode to Base36 URL (default) |
|
|
166
|
+
| `pair.encode({ base64: true })` | Re-encode to Base64 URL (legacy) |
|
|
167
|
+
| `pair.encode({ domain: 'example.com' })` | Re-encode with custom domain |
|
|
168
|
+
|
|
169
|
+
### getShortUrl
|
|
170
|
+
|
|
171
|
+
| Method | Description |
|
|
172
|
+
|--------|-------------|
|
|
173
|
+
| `getShortUrl(objectId, index)` | Create Base36 URL from ObjectId + index |
|
|
174
|
+
| `getShortUrl(objectId, index, { base64: true })` | Create Base64 URL (legacy) |
|
|
175
|
+
| `getShortUrl(objectId, index, { domain: 'example.com' })` | Create URL with custom domain |
|
|
176
|
+
|
|
177
|
+
### Utility Functions
|
|
178
|
+
|
|
179
|
+
| Function | Description |
|
|
180
|
+
|----------|-------------|
|
|
181
|
+
| `encodeBase36(bytes)` | Encode Uint8Array to uppercase Base36 string |
|
|
182
|
+
| `decodeBase36(str)` | Decode Base36 string to Uint8Array |
|
|
183
|
+
| `testBase36(str)` | Check if string is valid Base36 |
|
|
184
|
+
| `encodeUrl64(base64)` | Convert standard Base64 to URL-safe |
|
|
185
|
+
| `decodeUrl64(urlSafe)` | Convert URL-safe Base64 to standard |
|
|
186
|
+
|
|
187
|
+
---
|
|
188
|
+
|
|
189
|
+
## Bundle Formats
|
|
190
|
+
|
|
191
|
+
| Format | File | Use |
|
|
192
|
+
|--------|------|-----|
|
|
193
|
+
| ESM | `dist/retailid.esm.js` | Modern bundlers, Node.js ESM |
|
|
194
|
+
| CommonJS | `dist/retailid.cjs` | Node.js require() |
|
|
195
|
+
| Browser | `dist/retailid.min.js` | Script tag (IIFE) |
|
|
196
|
+
| Types | `dist/index.d.ts` | TypeScript definitions |
|
|
197
|
+
|
|
198
|
+
---
|
|
199
|
+
|
|
200
|
+
## Test Vectors
|
|
201
|
+
|
|
202
|
+
| URL | Tag | Index |
|
|
203
|
+
|-----|-----|-------|
|
|
204
|
+
| `HTTPS://1A4.COM/5LN8CBN1UB33DON9CHKX` | `1a4060300020081000006609` | 1 |
|
|
205
|
+
| `HTTPS://1A4.COM/13TX7BMRX3IU01B9EGTPTT` | `1a4060300020081000006609` | 128 |
|
|
206
|
+
| `HTTPS://1A4.COM/7V8S42PYJD1XC9C2UVNCD1D` | `1a4060300020081000006609` | 16384 |
|
|
207
|
+
| `https://1a4.com/GkBgMAAEG2AAAJKXEQ` | `1a40603000041b6000009297` | 17 |
|
|
208
|
+
|
|
209
|
+
---
|
|
210
|
+
|
|
211
|
+
## Development
|
|
212
|
+
|
|
213
|
+
```bash
|
|
214
|
+
# Install dependencies
|
|
215
|
+
npm install
|
|
216
|
+
|
|
217
|
+
# Run tests
|
|
218
|
+
npm test
|
|
219
|
+
|
|
220
|
+
# Build
|
|
221
|
+
npm run build
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
## License
|
|
225
|
+
|
|
226
|
+
ISC
|
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// Minimal ObjectId implementation for browser
|
|
4
|
+
class ObjectId {
|
|
5
|
+
constructor(input) {
|
|
6
|
+
if (input === undefined) {
|
|
7
|
+
// Generate new ObjectId (12 random bytes)
|
|
8
|
+
this.bytes = new Uint8Array(12);
|
|
9
|
+
crypto.getRandomValues(this.bytes);
|
|
10
|
+
}
|
|
11
|
+
else if (typeof input === 'string') {
|
|
12
|
+
if (input.length !== 24 || !/^[0-9a-fA-F]+$/.test(input)) {
|
|
13
|
+
throw new Error(`Invalid ObjectId hex string: ${input}`);
|
|
14
|
+
}
|
|
15
|
+
this.bytes = hexToBytes(input);
|
|
16
|
+
}
|
|
17
|
+
else if (input instanceof Uint8Array) {
|
|
18
|
+
if (input.length !== 12) {
|
|
19
|
+
throw new Error(`Invalid ObjectId bytes length: ${input.length}`);
|
|
20
|
+
}
|
|
21
|
+
this.bytes = input;
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
throw new Error('ObjectId must be a 24-char hex string or 12-byte Uint8Array');
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
get id() {
|
|
28
|
+
return this.bytes;
|
|
29
|
+
}
|
|
30
|
+
toHexString() {
|
|
31
|
+
return bytesToHex(this.bytes);
|
|
32
|
+
}
|
|
33
|
+
toString() {
|
|
34
|
+
return this.toHexString();
|
|
35
|
+
}
|
|
36
|
+
equals(other) {
|
|
37
|
+
const otherBytes = other instanceof ObjectId ? other.bytes : other;
|
|
38
|
+
if (this.bytes.length !== otherBytes.length)
|
|
39
|
+
return false;
|
|
40
|
+
for (let i = 0; i < this.bytes.length; i++) {
|
|
41
|
+
if (this.bytes[i] !== otherBytes[i])
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
return true;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
// Hex conversion utilities
|
|
48
|
+
function hexToBytes(hex) {
|
|
49
|
+
const bytes = new Uint8Array(hex.length / 2);
|
|
50
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
51
|
+
bytes[i] = parseInt(hex.slice(i * 2, i * 2 + 2), 16);
|
|
52
|
+
}
|
|
53
|
+
return bytes;
|
|
54
|
+
}
|
|
55
|
+
function bytesToHex(bytes) {
|
|
56
|
+
let hex = '';
|
|
57
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
58
|
+
hex += bytes[i].toString(16).padStart(2, '0');
|
|
59
|
+
}
|
|
60
|
+
return hex;
|
|
61
|
+
}
|
|
62
|
+
// Varint encoding/decoding (inlined, no dependency)
|
|
63
|
+
function varintEncode(num) {
|
|
64
|
+
const bytes = [];
|
|
65
|
+
while (num >= 0x80) {
|
|
66
|
+
bytes.push((num & 0x7f) | 0x80);
|
|
67
|
+
num >>>= 7;
|
|
68
|
+
}
|
|
69
|
+
bytes.push(num);
|
|
70
|
+
return new Uint8Array(bytes);
|
|
71
|
+
}
|
|
72
|
+
function varintDecode(bytes, offset = 0) {
|
|
73
|
+
let value = 0;
|
|
74
|
+
let shift = 0;
|
|
75
|
+
let length = 0;
|
|
76
|
+
while (true) {
|
|
77
|
+
if (offset + length >= bytes.length) {
|
|
78
|
+
throw new Error('Varint decode: buffer underflow');
|
|
79
|
+
}
|
|
80
|
+
const byte = bytes[offset + length];
|
|
81
|
+
value |= (byte & 0x7f) << shift;
|
|
82
|
+
length++;
|
|
83
|
+
if ((byte & 0x80) === 0)
|
|
84
|
+
break;
|
|
85
|
+
shift += 7;
|
|
86
|
+
}
|
|
87
|
+
return { value, length };
|
|
88
|
+
}
|
|
89
|
+
// Base36 encoding/decoding
|
|
90
|
+
function bigIntToBase36(num) {
|
|
91
|
+
return num.toString(36).toUpperCase();
|
|
92
|
+
}
|
|
93
|
+
function base36ToBigInt(str) {
|
|
94
|
+
return [...str.toLowerCase()].reduce((acc, curr) => BigInt(parseInt(curr, 36)) + 36n * acc, 0n);
|
|
95
|
+
}
|
|
96
|
+
function bigIntToBytes(bn) {
|
|
97
|
+
let hex = bn.toString(16);
|
|
98
|
+
if (hex.length % 2)
|
|
99
|
+
hex = '0' + hex;
|
|
100
|
+
const len = hex.length / 2;
|
|
101
|
+
const u8 = new Uint8Array(len);
|
|
102
|
+
for (let i = 0; i < len; i++) {
|
|
103
|
+
u8[i] = parseInt(hex.slice(i * 2, i * 2 + 2), 16);
|
|
104
|
+
}
|
|
105
|
+
return u8;
|
|
106
|
+
}
|
|
107
|
+
function bytesToBigInt(buf) {
|
|
108
|
+
let hex = '';
|
|
109
|
+
for (let i = 0; i < buf.length; i++) {
|
|
110
|
+
hex += buf[i].toString(16).padStart(2, '0');
|
|
111
|
+
}
|
|
112
|
+
return BigInt('0x' + hex);
|
|
113
|
+
}
|
|
114
|
+
const encodeBase36 = (buf) => bigIntToBase36(bytesToBigInt(buf));
|
|
115
|
+
const decodeBase36 = (base36) => bigIntToBytes(base36ToBigInt(base36));
|
|
116
|
+
const testBase36 = (base36) => /^[A-Za-z0-9]+$/.test(base36);
|
|
117
|
+
// URL64 encoding/decoding
|
|
118
|
+
const decodeUrl64 = (encoded) => {
|
|
119
|
+
encoded = encoded.replace(/-/g, '+').replace(/_/g, '/');
|
|
120
|
+
while (encoded.length % 4)
|
|
121
|
+
encoded += '=';
|
|
122
|
+
return encoded;
|
|
123
|
+
};
|
|
124
|
+
const encodeUrl64 = (base64) => {
|
|
125
|
+
return base64.replace(/=/g, '').replace(/\+/g, '-').replace(/\//g, '_');
|
|
126
|
+
};
|
|
127
|
+
// VarInt class
|
|
128
|
+
class VarInt {
|
|
129
|
+
static decode(source) {
|
|
130
|
+
const { value, length } = varintDecode(source);
|
|
131
|
+
const bytes = source.slice(0, length);
|
|
132
|
+
return { value, length, bytes };
|
|
133
|
+
}
|
|
134
|
+
static encode(value) {
|
|
135
|
+
return varintEncode(value);
|
|
136
|
+
}
|
|
137
|
+
toHexString() {
|
|
138
|
+
return bytesToHex(this.bytes);
|
|
139
|
+
}
|
|
140
|
+
toString() {
|
|
141
|
+
return '0x' + this.value.toString(16);
|
|
142
|
+
}
|
|
143
|
+
length() {
|
|
144
|
+
return this.bytes.length;
|
|
145
|
+
}
|
|
146
|
+
data() {
|
|
147
|
+
return this.bytes;
|
|
148
|
+
}
|
|
149
|
+
constructor(source) {
|
|
150
|
+
if (typeof source === 'number') {
|
|
151
|
+
this.value = source;
|
|
152
|
+
this.bytes = VarInt.encode(this.value);
|
|
153
|
+
}
|
|
154
|
+
else {
|
|
155
|
+
try {
|
|
156
|
+
const { value, bytes } = VarInt.decode(source);
|
|
157
|
+
this.value = value;
|
|
158
|
+
this.bytes = bytes;
|
|
159
|
+
}
|
|
160
|
+
catch (e) {
|
|
161
|
+
console.error(source, e);
|
|
162
|
+
throw new Error('failed to construct VarInt');
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
// RetailIdPair class
|
|
168
|
+
class RetailIdPair {
|
|
169
|
+
constructor(stringUrl, strict) {
|
|
170
|
+
const { batchId, index } = getBatchIdAndIndexFromUrl(stringUrl, strict);
|
|
171
|
+
this.batchId = batchId;
|
|
172
|
+
this.index = index;
|
|
173
|
+
}
|
|
174
|
+
encode(options) {
|
|
175
|
+
return getShortUrl(this.batchId, this.index, options);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
// Internal helpers
|
|
179
|
+
function concatBytes(...arrays) {
|
|
180
|
+
const totalLength = arrays.reduce((sum, arr) => sum + arr.length, 0);
|
|
181
|
+
const result = new Uint8Array(totalLength);
|
|
182
|
+
let offset = 0;
|
|
183
|
+
for (const arr of arrays) {
|
|
184
|
+
result.set(arr, offset);
|
|
185
|
+
offset += arr.length;
|
|
186
|
+
}
|
|
187
|
+
return result;
|
|
188
|
+
}
|
|
189
|
+
function base64ToBytes(base64) {
|
|
190
|
+
let binary;
|
|
191
|
+
try {
|
|
192
|
+
binary = atob(base64);
|
|
193
|
+
}
|
|
194
|
+
catch {
|
|
195
|
+
throw new Error(`Invalid base64 encoding: "${base64}"`);
|
|
196
|
+
}
|
|
197
|
+
const bytes = new Uint8Array(binary.length);
|
|
198
|
+
for (let i = 0; i < binary.length; i++) {
|
|
199
|
+
bytes[i] = binary.charCodeAt(i);
|
|
200
|
+
}
|
|
201
|
+
return bytes;
|
|
202
|
+
}
|
|
203
|
+
function bytesToBase64(bytes) {
|
|
204
|
+
let binary = '';
|
|
205
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
206
|
+
binary += String.fromCharCode(bytes[i]);
|
|
207
|
+
}
|
|
208
|
+
return btoa(binary);
|
|
209
|
+
}
|
|
210
|
+
// Known RetailID prefixes
|
|
211
|
+
const RETAILID_PREFIXES = ["1a4", "abc"];
|
|
212
|
+
// Valid ObjectId timestamp range: 2012-01-01 to 2052-01-01
|
|
213
|
+
const MIN_OBJECTID_TIMESTAMP = Math.floor(new Date("2012-01-01").getTime() / 1000);
|
|
214
|
+
const MAX_OBJECTID_TIMESTAMP = Math.floor(new Date("2052-01-01").getTime() / 1000);
|
|
215
|
+
const isValidObjectIdTimestamp = (bytes) => {
|
|
216
|
+
if (bytes.length < 4)
|
|
217
|
+
return false;
|
|
218
|
+
// First 4 bytes are big-endian Unix timestamp
|
|
219
|
+
const timestamp = (bytes[0] << 24) | (bytes[1] << 16) | (bytes[2] << 8) | bytes[3];
|
|
220
|
+
return timestamp >= MIN_OBJECTID_TIMESTAMP && timestamp <= MAX_OBJECTID_TIMESTAMP;
|
|
221
|
+
};
|
|
222
|
+
const getBatchIdAndIndex = (buffer, mode) => {
|
|
223
|
+
if (buffer.length < 13) {
|
|
224
|
+
throw new Error(`Buffer too short: expected at least 13 bytes, got ${buffer.length}`);
|
|
225
|
+
}
|
|
226
|
+
const idBytes = buffer.subarray(0, 12);
|
|
227
|
+
const hexId = bytesToHex(idBytes);
|
|
228
|
+
// Check for known RetailID prefixes
|
|
229
|
+
const hasKnownPrefix = RETAILID_PREFIXES.some(prefix => hexId.startsWith(prefix));
|
|
230
|
+
if (!hasKnownPrefix) {
|
|
231
|
+
if (mode === 'strict') {
|
|
232
|
+
throw new Error(`unknown prefix ${hexId}`);
|
|
233
|
+
}
|
|
234
|
+
else if (mode === 'mongo') {
|
|
235
|
+
if (!isValidObjectIdTimestamp(idBytes)) {
|
|
236
|
+
throw new Error(`invalid mongo timestamp ${hexId}`);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
let batchId;
|
|
241
|
+
try {
|
|
242
|
+
batchId = new ObjectId(hexId);
|
|
243
|
+
}
|
|
244
|
+
catch (e) {
|
|
245
|
+
throw new Error(`Invalid ObjectId: ${hexId}`);
|
|
246
|
+
}
|
|
247
|
+
const indexBuf = buffer.subarray(12);
|
|
248
|
+
const res = VarInt.decode(indexBuf);
|
|
249
|
+
return { batchId, index: res.value };
|
|
250
|
+
};
|
|
251
|
+
const getBatchIdAndIndexFromUrl = (url, strict = false) => {
|
|
252
|
+
let shortCode = url;
|
|
253
|
+
if (url.toLowerCase().indexOf('http') !== -1) {
|
|
254
|
+
const [, uri] = url.split('//');
|
|
255
|
+
const [domain, ...paths] = uri.split('/');
|
|
256
|
+
if (!paths.length) {
|
|
257
|
+
throw new Error(`no paths for domain: ${domain}, full: ${url}`);
|
|
258
|
+
}
|
|
259
|
+
shortCode = paths[paths.length - 1];
|
|
260
|
+
if (!shortCode) {
|
|
261
|
+
throw new Error(`no id in url ${url}, domain: ${domain}`);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
// Mixed case shortcode = definitely base64, all uppercase = could be either
|
|
265
|
+
const shortCodeIsAllUppercase = shortCode === shortCode.toUpperCase();
|
|
266
|
+
// Decode helpers
|
|
267
|
+
const decodeBase36Buf = () => decodeBase36(shortCode);
|
|
268
|
+
const decodeBase64Buf = () => base64ToBytes(decodeUrl64(shortCode));
|
|
269
|
+
// Try with a specific encoding and mode, return null on failure
|
|
270
|
+
const tryParse = (buf, mode) => {
|
|
271
|
+
try {
|
|
272
|
+
return getBatchIdAndIndex(buf, mode);
|
|
273
|
+
}
|
|
274
|
+
catch {
|
|
275
|
+
return null;
|
|
276
|
+
}
|
|
277
|
+
};
|
|
278
|
+
// 1. Try strict mode (known prefixes only) with both encodings
|
|
279
|
+
// Could be base36 - try it first
|
|
280
|
+
const base36Buf = tryParse(decodeBase36Buf(), 'strict');
|
|
281
|
+
if (base36Buf)
|
|
282
|
+
return base36Buf;
|
|
283
|
+
// Try base64
|
|
284
|
+
const base64Strict = tryParse(decodeBase64Buf(), 'strict');
|
|
285
|
+
if (base64Strict)
|
|
286
|
+
return base64Strict;
|
|
287
|
+
// 2. If allowMongo, try mongo mode (valid timestamp) with both encodings
|
|
288
|
+
if (shortCodeIsAllUppercase) {
|
|
289
|
+
const base36Mongo = tryParse(decodeBase36Buf(), strict ? 'mongo' : 'any');
|
|
290
|
+
if (base36Mongo)
|
|
291
|
+
return base36Mongo;
|
|
292
|
+
}
|
|
293
|
+
const base64Mongo = tryParse(decodeBase64Buf(), strict ? 'mongo' : 'any');
|
|
294
|
+
if (base64Mongo)
|
|
295
|
+
return base64Mongo;
|
|
296
|
+
throw new Error(`Unrecognized URL: ${url}`);
|
|
297
|
+
};
|
|
298
|
+
const getBuffer = (batchId, index) => {
|
|
299
|
+
const varintBuf = VarInt.encode(index);
|
|
300
|
+
let batchBuf = batchId.id;
|
|
301
|
+
if (!batchBuf) {
|
|
302
|
+
if (typeof batchId === 'string') {
|
|
303
|
+
batchBuf = hexToBytes(batchId);
|
|
304
|
+
}
|
|
305
|
+
else {
|
|
306
|
+
throw new Error(`${batchId ? 'bad' : 'missing'} buffer or string: ${batchId}`);
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
const buf = concatBytes(batchBuf, varintBuf);
|
|
310
|
+
// this inline decode unit test is unnecessary overhead if we have good unit tests
|
|
311
|
+
// const result = getBatchIdAndIndex(buf, 'mongo');
|
|
312
|
+
// if (!result.batchId.equals(batchBuf)) throw new Error('bad batchId in encoding');
|
|
313
|
+
// if (result.index !== index) {
|
|
314
|
+
// throw new Error(`bad index in encoding ${result.index} != ${index}`);
|
|
315
|
+
// }
|
|
316
|
+
return buf;
|
|
317
|
+
};
|
|
318
|
+
const getShortUrl = (batchId, index, options) => {
|
|
319
|
+
const domain = options?.domain || '1a4.com';
|
|
320
|
+
if (options?.base64) {
|
|
321
|
+
const base64 = bytesToBase64(getBuffer(batchId, index));
|
|
322
|
+
const dataPart = encodeUrl64(base64);
|
|
323
|
+
return `https://${domain}/${dataPart}`;
|
|
324
|
+
}
|
|
325
|
+
else {
|
|
326
|
+
const buf = getBuffer(batchId, index);
|
|
327
|
+
const dataPart = encodeBase36(buf);
|
|
328
|
+
return `HTTPS://${domain}/${dataPart}`.toUpperCase();
|
|
329
|
+
}
|
|
330
|
+
};
|
|
331
|
+
|
|
332
|
+
exports.ObjectId = ObjectId;
|
|
333
|
+
exports.RetailIdPair = RetailIdPair;
|
|
334
|
+
exports.VarInt = VarInt;
|
|
335
|
+
exports.decodeBase36 = decodeBase36;
|
|
336
|
+
exports.decodeUrl64 = decodeUrl64;
|
|
337
|
+
exports.encodeBase36 = encodeBase36;
|
|
338
|
+
exports.encodeUrl64 = encodeUrl64;
|
|
339
|
+
exports.getShortUrl = getShortUrl;
|
|
340
|
+
exports.testBase36 = testBase36;
|
|
341
|
+
//# sourceMappingURL=retailid.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"retailid.cjs","sources":["../../src/index.ts"],"sourcesContent":[null],"names":[],"mappings":";;AAEA;MACa,QAAQ,CAAA;AAGnB,IAAA,WAAA,CAAY,KAA2B,EAAA;AACrC,QAAA,IAAI,KAAK,KAAK,SAAS,EAAE;;YAEvB,IAAI,CAAC,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AAC;AACvD,QAAA,MAAM,QAAQ,GAAG,WAAW,CAAC,MAAM,CAAC;AACpC,QAAA,OAAO,CAAA,QAAA,EAAW,MAAM,CAAA,CAAA,EAAI,QAAQ,EAAE;IACxC;SAAO;QACL,MAAM,GAAG,GAAG,SAAS,CAAC,OAAO,EAAE,KAAK,CAAC;AACrC,QAAA,MAAM,QAAQ,GAAG,YAAY,CAAC,GAAG,CAAC;QAClC,OAAO,CAAA,QAAA,EAAW,MAAM,CAAA,CAAA,EAAI,QAAQ,EAAE,CAAC,WAAW,EAAE;IACtD;AACF;;;;;;;;;;;;"}
|
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
// Minimal ObjectId implementation for browser
|
|
2
|
+
class ObjectId {
|
|
3
|
+
constructor(input) {
|
|
4
|
+
if (input === undefined) {
|
|
5
|
+
// Generate new ObjectId (12 random bytes)
|
|
6
|
+
this.bytes = new Uint8Array(12);
|
|
7
|
+
crypto.getRandomValues(this.bytes);
|
|
8
|
+
}
|
|
9
|
+
else if (typeof input === 'string') {
|
|
10
|
+
if (input.length !== 24 || !/^[0-9a-fA-F]+$/.test(input)) {
|
|
11
|
+
throw new Error(`Invalid ObjectId hex string: ${input}`);
|
|
12
|
+
}
|
|
13
|
+
this.bytes = hexToBytes(input);
|
|
14
|
+
}
|
|
15
|
+
else if (input instanceof Uint8Array) {
|
|
16
|
+
if (input.length !== 12) {
|
|
17
|
+
throw new Error(`Invalid ObjectId bytes length: ${input.length}`);
|
|
18
|
+
}
|
|
19
|
+
this.bytes = input;
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
throw new Error('ObjectId must be a 24-char hex string or 12-byte Uint8Array');
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
get id() {
|
|
26
|
+
return this.bytes;
|
|
27
|
+
}
|
|
28
|
+
toHexString() {
|
|
29
|
+
return bytesToHex(this.bytes);
|
|
30
|
+
}
|
|
31
|
+
toString() {
|
|
32
|
+
return this.toHexString();
|
|
33
|
+
}
|
|
34
|
+
equals(other) {
|
|
35
|
+
const otherBytes = other instanceof ObjectId ? other.bytes : other;
|
|
36
|
+
if (this.bytes.length !== otherBytes.length)
|
|
37
|
+
return false;
|
|
38
|
+
for (let i = 0; i < this.bytes.length; i++) {
|
|
39
|
+
if (this.bytes[i] !== otherBytes[i])
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
return true;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
// Hex conversion utilities
|
|
46
|
+
function hexToBytes(hex) {
|
|
47
|
+
const bytes = new Uint8Array(hex.length / 2);
|
|
48
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
49
|
+
bytes[i] = parseInt(hex.slice(i * 2, i * 2 + 2), 16);
|
|
50
|
+
}
|
|
51
|
+
return bytes;
|
|
52
|
+
}
|
|
53
|
+
function bytesToHex(bytes) {
|
|
54
|
+
let hex = '';
|
|
55
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
56
|
+
hex += bytes[i].toString(16).padStart(2, '0');
|
|
57
|
+
}
|
|
58
|
+
return hex;
|
|
59
|
+
}
|
|
60
|
+
// Varint encoding/decoding (inlined, no dependency)
|
|
61
|
+
function varintEncode(num) {
|
|
62
|
+
const bytes = [];
|
|
63
|
+
while (num >= 0x80) {
|
|
64
|
+
bytes.push((num & 0x7f) | 0x80);
|
|
65
|
+
num >>>= 7;
|
|
66
|
+
}
|
|
67
|
+
bytes.push(num);
|
|
68
|
+
return new Uint8Array(bytes);
|
|
69
|
+
}
|
|
70
|
+
function varintDecode(bytes, offset = 0) {
|
|
71
|
+
let value = 0;
|
|
72
|
+
let shift = 0;
|
|
73
|
+
let length = 0;
|
|
74
|
+
while (true) {
|
|
75
|
+
if (offset + length >= bytes.length) {
|
|
76
|
+
throw new Error('Varint decode: buffer underflow');
|
|
77
|
+
}
|
|
78
|
+
const byte = bytes[offset + length];
|
|
79
|
+
value |= (byte & 0x7f) << shift;
|
|
80
|
+
length++;
|
|
81
|
+
if ((byte & 0x80) === 0)
|
|
82
|
+
break;
|
|
83
|
+
shift += 7;
|
|
84
|
+
}
|
|
85
|
+
return { value, length };
|
|
86
|
+
}
|
|
87
|
+
// Base36 encoding/decoding
|
|
88
|
+
function bigIntToBase36(num) {
|
|
89
|
+
return num.toString(36).toUpperCase();
|
|
90
|
+
}
|
|
91
|
+
function base36ToBigInt(str) {
|
|
92
|
+
return [...str.toLowerCase()].reduce((acc, curr) => BigInt(parseInt(curr, 36)) + 36n * acc, 0n);
|
|
93
|
+
}
|
|
94
|
+
function bigIntToBytes(bn) {
|
|
95
|
+
let hex = bn.toString(16);
|
|
96
|
+
if (hex.length % 2)
|
|
97
|
+
hex = '0' + hex;
|
|
98
|
+
const len = hex.length / 2;
|
|
99
|
+
const u8 = new Uint8Array(len);
|
|
100
|
+
for (let i = 0; i < len; i++) {
|
|
101
|
+
u8[i] = parseInt(hex.slice(i * 2, i * 2 + 2), 16);
|
|
102
|
+
}
|
|
103
|
+
return u8;
|
|
104
|
+
}
|
|
105
|
+
function bytesToBigInt(buf) {
|
|
106
|
+
let hex = '';
|
|
107
|
+
for (let i = 0; i < buf.length; i++) {
|
|
108
|
+
hex += buf[i].toString(16).padStart(2, '0');
|
|
109
|
+
}
|
|
110
|
+
return BigInt('0x' + hex);
|
|
111
|
+
}
|
|
112
|
+
const encodeBase36 = (buf) => bigIntToBase36(bytesToBigInt(buf));
|
|
113
|
+
const decodeBase36 = (base36) => bigIntToBytes(base36ToBigInt(base36));
|
|
114
|
+
const testBase36 = (base36) => /^[A-Za-z0-9]+$/.test(base36);
|
|
115
|
+
// URL64 encoding/decoding
|
|
116
|
+
const decodeUrl64 = (encoded) => {
|
|
117
|
+
encoded = encoded.replace(/-/g, '+').replace(/_/g, '/');
|
|
118
|
+
while (encoded.length % 4)
|
|
119
|
+
encoded += '=';
|
|
120
|
+
return encoded;
|
|
121
|
+
};
|
|
122
|
+
const encodeUrl64 = (base64) => {
|
|
123
|
+
return base64.replace(/=/g, '').replace(/\+/g, '-').replace(/\//g, '_');
|
|
124
|
+
};
|
|
125
|
+
// VarInt class
|
|
126
|
+
class VarInt {
|
|
127
|
+
static decode(source) {
|
|
128
|
+
const { value, length } = varintDecode(source);
|
|
129
|
+
const bytes = source.slice(0, length);
|
|
130
|
+
return { value, length, bytes };
|
|
131
|
+
}
|
|
132
|
+
static encode(value) {
|
|
133
|
+
return varintEncode(value);
|
|
134
|
+
}
|
|
135
|
+
toHexString() {
|
|
136
|
+
return bytesToHex(this.bytes);
|
|
137
|
+
}
|
|
138
|
+
toString() {
|
|
139
|
+
return '0x' + this.value.toString(16);
|
|
140
|
+
}
|
|
141
|
+
length() {
|
|
142
|
+
return this.bytes.length;
|
|
143
|
+
}
|
|
144
|
+
data() {
|
|
145
|
+
return this.bytes;
|
|
146
|
+
}
|
|
147
|
+
constructor(source) {
|
|
148
|
+
if (typeof source === 'number') {
|
|
149
|
+
this.value = source;
|
|
150
|
+
this.bytes = VarInt.encode(this.value);
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
try {
|
|
154
|
+
const { value, bytes } = VarInt.decode(source);
|
|
155
|
+
this.value = value;
|
|
156
|
+
this.bytes = bytes;
|
|
157
|
+
}
|
|
158
|
+
catch (e) {
|
|
159
|
+
console.error(source, e);
|
|
160
|
+
throw new Error('failed to construct VarInt');
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
// RetailIdPair class
|
|
166
|
+
class RetailIdPair {
|
|
167
|
+
constructor(stringUrl, strict) {
|
|
168
|
+
const { batchId, index } = getBatchIdAndIndexFromUrl(stringUrl, strict);
|
|
169
|
+
this.batchId = batchId;
|
|
170
|
+
this.index = index;
|
|
171
|
+
}
|
|
172
|
+
encode(options) {
|
|
173
|
+
return getShortUrl(this.batchId, this.index, options);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
// Internal helpers
|
|
177
|
+
function concatBytes(...arrays) {
|
|
178
|
+
const totalLength = arrays.reduce((sum, arr) => sum + arr.length, 0);
|
|
179
|
+
const result = new Uint8Array(totalLength);
|
|
180
|
+
let offset = 0;
|
|
181
|
+
for (const arr of arrays) {
|
|
182
|
+
result.set(arr, offset);
|
|
183
|
+
offset += arr.length;
|
|
184
|
+
}
|
|
185
|
+
return result;
|
|
186
|
+
}
|
|
187
|
+
function base64ToBytes(base64) {
|
|
188
|
+
let binary;
|
|
189
|
+
try {
|
|
190
|
+
binary = atob(base64);
|
|
191
|
+
}
|
|
192
|
+
catch {
|
|
193
|
+
throw new Error(`Invalid base64 encoding: "${base64}"`);
|
|
194
|
+
}
|
|
195
|
+
const bytes = new Uint8Array(binary.length);
|
|
196
|
+
for (let i = 0; i < binary.length; i++) {
|
|
197
|
+
bytes[i] = binary.charCodeAt(i);
|
|
198
|
+
}
|
|
199
|
+
return bytes;
|
|
200
|
+
}
|
|
201
|
+
function bytesToBase64(bytes) {
|
|
202
|
+
let binary = '';
|
|
203
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
204
|
+
binary += String.fromCharCode(bytes[i]);
|
|
205
|
+
}
|
|
206
|
+
return btoa(binary);
|
|
207
|
+
}
|
|
208
|
+
// Known RetailID prefixes
|
|
209
|
+
const RETAILID_PREFIXES = ["1a4", "abc"];
|
|
210
|
+
// Valid ObjectId timestamp range: 2012-01-01 to 2052-01-01
|
|
211
|
+
const MIN_OBJECTID_TIMESTAMP = Math.floor(new Date("2012-01-01").getTime() / 1000);
|
|
212
|
+
const MAX_OBJECTID_TIMESTAMP = Math.floor(new Date("2052-01-01").getTime() / 1000);
|
|
213
|
+
const isValidObjectIdTimestamp = (bytes) => {
|
|
214
|
+
if (bytes.length < 4)
|
|
215
|
+
return false;
|
|
216
|
+
// First 4 bytes are big-endian Unix timestamp
|
|
217
|
+
const timestamp = (bytes[0] << 24) | (bytes[1] << 16) | (bytes[2] << 8) | bytes[3];
|
|
218
|
+
return timestamp >= MIN_OBJECTID_TIMESTAMP && timestamp <= MAX_OBJECTID_TIMESTAMP;
|
|
219
|
+
};
|
|
220
|
+
const getBatchIdAndIndex = (buffer, mode) => {
|
|
221
|
+
if (buffer.length < 13) {
|
|
222
|
+
throw new Error(`Buffer too short: expected at least 13 bytes, got ${buffer.length}`);
|
|
223
|
+
}
|
|
224
|
+
const idBytes = buffer.subarray(0, 12);
|
|
225
|
+
const hexId = bytesToHex(idBytes);
|
|
226
|
+
// Check for known RetailID prefixes
|
|
227
|
+
const hasKnownPrefix = RETAILID_PREFIXES.some(prefix => hexId.startsWith(prefix));
|
|
228
|
+
if (!hasKnownPrefix) {
|
|
229
|
+
if (mode === 'strict') {
|
|
230
|
+
throw new Error(`unknown prefix ${hexId}`);
|
|
231
|
+
}
|
|
232
|
+
else if (mode === 'mongo') {
|
|
233
|
+
if (!isValidObjectIdTimestamp(idBytes)) {
|
|
234
|
+
throw new Error(`invalid mongo timestamp ${hexId}`);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
let batchId;
|
|
239
|
+
try {
|
|
240
|
+
batchId = new ObjectId(hexId);
|
|
241
|
+
}
|
|
242
|
+
catch (e) {
|
|
243
|
+
throw new Error(`Invalid ObjectId: ${hexId}`);
|
|
244
|
+
}
|
|
245
|
+
const indexBuf = buffer.subarray(12);
|
|
246
|
+
const res = VarInt.decode(indexBuf);
|
|
247
|
+
return { batchId, index: res.value };
|
|
248
|
+
};
|
|
249
|
+
const getBatchIdAndIndexFromUrl = (url, strict = false) => {
|
|
250
|
+
let shortCode = url;
|
|
251
|
+
if (url.toLowerCase().indexOf('http') !== -1) {
|
|
252
|
+
const [, uri] = url.split('//');
|
|
253
|
+
const [domain, ...paths] = uri.split('/');
|
|
254
|
+
if (!paths.length) {
|
|
255
|
+
throw new Error(`no paths for domain: ${domain}, full: ${url}`);
|
|
256
|
+
}
|
|
257
|
+
shortCode = paths[paths.length - 1];
|
|
258
|
+
if (!shortCode) {
|
|
259
|
+
throw new Error(`no id in url ${url}, domain: ${domain}`);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
// Mixed case shortcode = definitely base64, all uppercase = could be either
|
|
263
|
+
const shortCodeIsAllUppercase = shortCode === shortCode.toUpperCase();
|
|
264
|
+
// Decode helpers
|
|
265
|
+
const decodeBase36Buf = () => decodeBase36(shortCode);
|
|
266
|
+
const decodeBase64Buf = () => base64ToBytes(decodeUrl64(shortCode));
|
|
267
|
+
// Try with a specific encoding and mode, return null on failure
|
|
268
|
+
const tryParse = (buf, mode) => {
|
|
269
|
+
try {
|
|
270
|
+
return getBatchIdAndIndex(buf, mode);
|
|
271
|
+
}
|
|
272
|
+
catch {
|
|
273
|
+
return null;
|
|
274
|
+
}
|
|
275
|
+
};
|
|
276
|
+
// 1. Try strict mode (known prefixes only) with both encodings
|
|
277
|
+
// Could be base36 - try it first
|
|
278
|
+
const base36Buf = tryParse(decodeBase36Buf(), 'strict');
|
|
279
|
+
if (base36Buf)
|
|
280
|
+
return base36Buf;
|
|
281
|
+
// Try base64
|
|
282
|
+
const base64Strict = tryParse(decodeBase64Buf(), 'strict');
|
|
283
|
+
if (base64Strict)
|
|
284
|
+
return base64Strict;
|
|
285
|
+
// 2. If allowMongo, try mongo mode (valid timestamp) with both encodings
|
|
286
|
+
if (shortCodeIsAllUppercase) {
|
|
287
|
+
const base36Mongo = tryParse(decodeBase36Buf(), strict ? 'mongo' : 'any');
|
|
288
|
+
if (base36Mongo)
|
|
289
|
+
return base36Mongo;
|
|
290
|
+
}
|
|
291
|
+
const base64Mongo = tryParse(decodeBase64Buf(), strict ? 'mongo' : 'any');
|
|
292
|
+
if (base64Mongo)
|
|
293
|
+
return base64Mongo;
|
|
294
|
+
throw new Error(`Unrecognized URL: ${url}`);
|
|
295
|
+
};
|
|
296
|
+
const getBuffer = (batchId, index) => {
|
|
297
|
+
const varintBuf = VarInt.encode(index);
|
|
298
|
+
let batchBuf = batchId.id;
|
|
299
|
+
if (!batchBuf) {
|
|
300
|
+
if (typeof batchId === 'string') {
|
|
301
|
+
batchBuf = hexToBytes(batchId);
|
|
302
|
+
}
|
|
303
|
+
else {
|
|
304
|
+
throw new Error(`${batchId ? 'bad' : 'missing'} buffer or string: ${batchId}`);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
const buf = concatBytes(batchBuf, varintBuf);
|
|
308
|
+
// this inline decode unit test is unnecessary overhead if we have good unit tests
|
|
309
|
+
// const result = getBatchIdAndIndex(buf, 'mongo');
|
|
310
|
+
// if (!result.batchId.equals(batchBuf)) throw new Error('bad batchId in encoding');
|
|
311
|
+
// if (result.index !== index) {
|
|
312
|
+
// throw new Error(`bad index in encoding ${result.index} != ${index}`);
|
|
313
|
+
// }
|
|
314
|
+
return buf;
|
|
315
|
+
};
|
|
316
|
+
const getShortUrl = (batchId, index, options) => {
|
|
317
|
+
const domain = options?.domain || '1a4.com';
|
|
318
|
+
if (options?.base64) {
|
|
319
|
+
const base64 = bytesToBase64(getBuffer(batchId, index));
|
|
320
|
+
const dataPart = encodeUrl64(base64);
|
|
321
|
+
return `https://${domain}/${dataPart}`;
|
|
322
|
+
}
|
|
323
|
+
else {
|
|
324
|
+
const buf = getBuffer(batchId, index);
|
|
325
|
+
const dataPart = encodeBase36(buf);
|
|
326
|
+
return `HTTPS://${domain}/${dataPart}`.toUpperCase();
|
|
327
|
+
}
|
|
328
|
+
};
|
|
329
|
+
|
|
330
|
+
export { ObjectId, RetailIdPair, VarInt, decodeBase36, decodeUrl64, encodeBase36, encodeUrl64, getShortUrl, testBase36 };
|
|
331
|
+
//# sourceMappingURL=retailid.esm.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"retailid.esm.js","sources":["../../src/index.ts"],"sourcesContent":[null],"names":[],"mappings":"AAEA;MACa,QAAQ,CAAA;AAGnB,IAAA,WAAA,CAAY,KAA2B,EAAA;AACrC,QAAA,IAAI,KAAK,KAAK,SAAS,EAAE;;YAEvB,IAAI,CAAC,KAAK,GAAG,IAAI,UAAU,CAAC,EAAE,CAAC;AAC/B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MAAM,CAAC;AACpC,QAAA,OAAO,CAAA,QAAA,EAAW,MAAM,CAAA,CAAA,EAAI,QAAQ,EAAE;IACxC;SAAO;QACL,MAAM,GAAG,GAAG,SAAS,CAAC,OAAO,EAAE,KAAK,CAAC;AACrC,QAAA,MAAM,QAAQ,GAAG,YAAY,CAAC,GAAG,CAAC;QAClC,OAAO,CAAA,QAAA,EAAW,MAAM,CAAA,CAAA,EAAI,QAAQ,EAAE,CAAC,WAAW,EAAE;IACtD;AACF;;;;"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
var RetailId=function(t){"use strict";class e{constructor(t){if(void 0===t)this.bytes=new Uint8Array(12),
|
|
2
|
+
crypto.getRandomValues(this.bytes);else if("string"==typeof t){
|
|
3
|
+
if(24!==t.length||!/^[0-9a-fA-F]+$/.test(t))throw new Error(`Invalid ObjectId hex string: ${t}`);this.bytes=r(t)}else{
|
|
4
|
+
if(!(t instanceof Uint8Array))throw new Error("ObjectId must be a 24-char hex string or 12-byte Uint8Array")
|
|
5
|
+
;if(12!==t.length)throw new Error(`Invalid ObjectId bytes length: ${t.length}`);this.bytes=t}}get id(){return this.bytes}toHexString(){
|
|
6
|
+
return n(this.bytes)}toString(){return this.toHexString()}equals(t){const r=t instanceof e?t.bytes:t;if(this.bytes.length!==r.length)return!1
|
|
7
|
+
;for(let t=0;t<this.bytes.length;t++)if(this.bytes[t]!==r[t])return!1;return!0}}function r(t){const e=new Uint8Array(t.length/2)
|
|
8
|
+
;for(let r=0;r<e.length;r++)e[r]=parseInt(t.slice(2*r,2*r+2),16);return e}function n(t){let e=""
|
|
9
|
+
;for(let r=0;r<t.length;r++)e+=t[r].toString(16).padStart(2,"0");return e}const o=t=>{return e=function(t){let e=""
|
|
10
|
+
;for(let r=0;r<t.length;r++)e+=t[r].toString(16).padStart(2,"0");return BigInt("0x"+e)}(t),e.toString(36).toUpperCase();var e},s=t=>function(t){
|
|
11
|
+
let e=t.toString(16);e.length%2&&(e="0"+e);const r=e.length/2,n=new Uint8Array(r);for(let t=0;t<r;t++)n[t]=parseInt(e.slice(2*t,2*t+2),16);return n
|
|
12
|
+
}([...t.toLowerCase()].reduce((t,e)=>BigInt(parseInt(e,36))+36n*t,0n)),i=t=>{for(t=t.replace(/-/g,"+").replace(/_/g,"/");t.length%4;)t+="=";return t
|
|
13
|
+
},a=t=>t.replace(/=/g,"").replace(/\+/g,"-").replace(/\//g,"_");class c{static decode(t){const{value:e,length:r}=function(t,e=0){let r=0,n=0,o=0
|
|
14
|
+
;for(;;){if(e+o>=t.length)throw new Error("Varint decode: buffer underflow");const s=t[e+o];if(r|=(127&s)<<n,o++,!(128&s))break;n+=7}return{value:r,
|
|
15
|
+
length:o}}(t);return{value:e,length:r,bytes:t.slice(0,r)}}static encode(t){return function(t){const e=[];for(;t>=128;)e.push(127&t|128),t>>>=7
|
|
16
|
+
;return e.push(t),new Uint8Array(e)}(t)}toHexString(){return n(this.bytes)}toString(){return"0x"+this.value.toString(16)}length(){
|
|
17
|
+
return this.bytes.length}data(){return this.bytes}constructor(t){if("number"==typeof t)this.value=t,this.bytes=c.encode(this.value);else try{
|
|
18
|
+
const{value:e,bytes:r}=c.decode(t);this.value=e,this.bytes=r}catch(e){throw console.error(t,e),new Error("failed to construct VarInt")}}}
|
|
19
|
+
const l=["1a4","abc"],h=Math.floor(new Date("2012-01-01").getTime()/1e3),u=Math.floor(new Date("2052-01-01").getTime()/1e3),f=(t,r)=>{
|
|
20
|
+
if(t.length<13)throw new Error(`Buffer too short: expected at least 13 bytes, got ${t.length}`);const o=t.subarray(0,12),s=n(o)
|
|
21
|
+
;if(!l.some(t=>s.startsWith(t))){if("strict"===r)throw new Error(`unknown prefix ${s}`);if("mongo"===r&&!(t=>{if(t.length<4)return!1
|
|
22
|
+
;const e=t[0]<<24|t[1]<<16|t[2]<<8|t[3];return e>=h&&e<=u})(o))throw new Error(`invalid mongo timestamp ${s}`)}let i;try{i=new e(s)}catch(t){
|
|
23
|
+
throw new Error(`Invalid ObjectId: ${s}`)}const a=t.subarray(12);return{batchId:i,index:c.decode(a).value}},g=(t,e=!1)=>{let r=t
|
|
24
|
+
;if(-1!==t.toLowerCase().indexOf("http")){const[,e]=t.split("//"),[n,...o]=e.split("/")
|
|
25
|
+
;if(!o.length)throw new Error(`no paths for domain: ${n}, full: ${t}`);if(r=o[o.length-1],!r)throw new Error(`no id in url ${t}, domain: ${n}`)}
|
|
26
|
+
const n=r===r.toUpperCase(),o=()=>s(r),a=()=>function(t){let e;try{e=atob(t)}catch{throw new Error(`Invalid base64 encoding: "${t}"`)}
|
|
27
|
+
const r=new Uint8Array(e.length);for(let t=0;t<e.length;t++)r[t]=e.charCodeAt(t);return r}(i(r)),c=(t,e)=>{try{return f(t,e)}catch{return null}
|
|
28
|
+
},l=c(o(),"strict");if(l)return l;const h=c(a(),"strict");if(h)return h;if(n){const t=c(o(),e?"mongo":"any");if(t)return t}
|
|
29
|
+
const u=c(a(),e?"mongo":"any");if(u)return u;throw new Error(`Unrecognized URL: ${t}`)},d=(t,e)=>{const n=c.encode(e);let o=t.id;if(!o){
|
|
30
|
+
if("string"!=typeof t)throw new Error(`${t?"bad":"missing"} buffer or string: ${t}`);o=r(t)}return function(...t){
|
|
31
|
+
const e=t.reduce((t,e)=>t+e.length,0),r=new Uint8Array(e);let n=0;for(const e of t)r.set(e,n),n+=e.length;return r}(o,n)},b=(t,e,r)=>{
|
|
32
|
+
const n=r?.domain||"1a4.com";if(r?.base64){const r=function(t){let e="";for(let r=0;r<t.length;r++)e+=String.fromCharCode(t[r]);return btoa(e)
|
|
33
|
+
}(d(t,e));return`https://${n}/${a(r)}`}{const r=d(t,e);return`HTTPS://${n}/${o(r)}`.toUpperCase()}};return t.ObjectId=e,t.RetailIdPair=class{
|
|
34
|
+
constructor(t,e){const{batchId:r,index:n}=g(t,e);this.batchId=r,this.index=n}encode(t){return b(this.batchId,this.index,t)}},t.VarInt=c,
|
|
35
|
+
t.decodeBase36=s,t.decodeUrl64=i,t.encodeBase36=o,t.encodeUrl64=a,t.getShortUrl=b,t.testBase36=t=>/^[A-Za-z0-9]+$/.test(t),t}({});
|
|
36
|
+
//# sourceMappingURL=retailid.min.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"retailid.min.js","sources":["../../src/index.ts"],"sourcesContent":[null],"names":["ObjectId","constructor","input","undefined","this","bytes","Uint8Array","crypto","getRandomValues","length","test","Error","hexToBytes","id","toHexString","bytesToHex","toString","equals","other","otherBytes","i","hex","parseInt","slice","padStart","encodeBase36","buf","bigIntToBase36","num","BigInt","bytesToBigInt","toUpperCase","decodeBase36","base36","bn","len","u8","bigIntToBytes","toLowerCase","reduce","acc","curr","decodeUrl64","encoded","replace","encodeUrl64","base64","VarInt","decode","source","value","offset","shift","byte","varintDecode","encode","push","varintEncode","data","e","console","error","RETAILID_PREFIXES","MIN_OBJECTID_TIMESTAMP","Math","floor","Date","getTime","MAX_OBJECTID_TIMESTAMP","getBatchIdAndIndex","buffer","mode","idBytes","subarray","hexId","some","prefix","startsWith","timestamp","isValidObjectIdTimestamp","batchId","indexBuf","index","getBatchIdAndIndexFromUrl","url","strict","shortCode","indexOf","uri","split","domain","paths","shortCodeIsAllUppercase","decodeBase36Buf","decodeBase64Buf","binary","atob","charCodeAt","base64ToBytes","tryParse","base36Buf","base64Strict","base36Mongo","base64Mongo","getBuffer","varintBuf","batchBuf","arrays","totalLength","sum","arr","result","set","concatBytes","getShortUrl","options","String","fromCharCode","btoa","bytesToBase64","stringUrl"],"mappings":"4CAGaA,EAGX,WAAAC,CAAYC,GACV,QAAcC,IAAVD,EAEFE,KAAKC,MAAQ,IAAIC,WAAW;AAC5BC,OAAOC,gBAAgBJ,KAAKC,YACvB,GAAqB,iBAAVH,EAAoB;AACpC,GAAqB,KAAjBA,EAAMO,SAAkB,iBAAiBC,KAAKR,GAChD,MAAM,IAAIS,MAAM,gCAAgCT,KAElDE,KAAKC,MAAQO,EAAWV,EAC1B,KAAO;AAAA,KAAIA,aAAi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f5E,KAAK8E,MAAQA,CACf,CAEA,MAAA3B,CAAO4D,GACL,OAAOD,EAAY9G,KAAK4E,QAAS5E,KAAK8E,MAAOiC,EAC/C;+FA1EyBlF,GAA4B,iBAAiBvB,KAAKuB"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
export declare class ObjectId {
|
|
2
|
+
private bytes;
|
|
3
|
+
constructor(input?: string | Uint8Array);
|
|
4
|
+
get id(): Uint8Array;
|
|
5
|
+
toHexString(): string;
|
|
6
|
+
toString(): string;
|
|
7
|
+
equals(other: ObjectId | Uint8Array): boolean;
|
|
8
|
+
}
|
|
9
|
+
export declare const encodeBase36: (buf: Uint8Array) => string;
|
|
10
|
+
export declare const decodeBase36: (base36: string) => Uint8Array;
|
|
11
|
+
export declare const testBase36: (base36: string) => boolean;
|
|
12
|
+
export declare const decodeUrl64: (encoded: string) => string;
|
|
13
|
+
export declare const encodeUrl64: (base64: string) => string;
|
|
14
|
+
export declare class VarInt {
|
|
15
|
+
private bytes;
|
|
16
|
+
value: number;
|
|
17
|
+
static decode(source: Uint8Array): {
|
|
18
|
+
value: number;
|
|
19
|
+
length: number;
|
|
20
|
+
bytes: Uint8Array;
|
|
21
|
+
};
|
|
22
|
+
static encode(value: number): Uint8Array;
|
|
23
|
+
toHexString(): string;
|
|
24
|
+
toString(): string;
|
|
25
|
+
length(): number;
|
|
26
|
+
data(): Uint8Array;
|
|
27
|
+
constructor(source: Uint8Array | number);
|
|
28
|
+
}
|
|
29
|
+
export declare class RetailIdPair {
|
|
30
|
+
batchId: ObjectId;
|
|
31
|
+
index: number;
|
|
32
|
+
constructor(stringUrl: string, strict?: boolean);
|
|
33
|
+
encode(options?: {
|
|
34
|
+
domain?: string;
|
|
35
|
+
base64?: boolean;
|
|
36
|
+
}): string;
|
|
37
|
+
}
|
|
38
|
+
export declare const getShortUrl: (batchId: ObjectId, index: number, options?: {
|
|
39
|
+
domain?: string;
|
|
40
|
+
base64?: boolean;
|
|
41
|
+
}) => string;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@metrc/retailid",
|
|
3
|
+
"version": "0.10.0",
|
|
4
|
+
"description": "Official SDK for encoding and decoding Metrc RetailID QR labels",
|
|
5
|
+
"main": "dist/retailid.cjs",
|
|
6
|
+
"module": "dist/retailid.esm.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/retailid.esm.js",
|
|
12
|
+
"require": "./dist/retailid.cjs"
|
|
13
|
+
},
|
|
14
|
+
"./browser": "./dist/retailid.min.js"
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
19
|
+
"scripts": {
|
|
20
|
+
"build": "rollup -c",
|
|
21
|
+
"build:watch": "rollup -c -w",
|
|
22
|
+
"test": "vitest run",
|
|
23
|
+
"test:watch": "vitest",
|
|
24
|
+
"prepublishOnly": "npm run build && npm test",
|
|
25
|
+
"publish:cdn": "./publish.sh"
|
|
26
|
+
},
|
|
27
|
+
"keywords": [
|
|
28
|
+
"retailid",
|
|
29
|
+
"metrc",
|
|
30
|
+
"qr",
|
|
31
|
+
"qr-code",
|
|
32
|
+
"supply-chain",
|
|
33
|
+
"traceability"
|
|
34
|
+
],
|
|
35
|
+
"author": "Metrc LLC <dev@metrc.com>",
|
|
36
|
+
"license": "MIT",
|
|
37
|
+
"repository": {
|
|
38
|
+
"type": "git",
|
|
39
|
+
"url": "https://github.com/user/retailid-sdk.git",
|
|
40
|
+
"directory": "javascript"
|
|
41
|
+
},
|
|
42
|
+
"homepage": "https://www.metrc.com/retailid/",
|
|
43
|
+
"bugs": {
|
|
44
|
+
"url": "https://github.com/user/retailid-sdk/issues"
|
|
45
|
+
},
|
|
46
|
+
"publishConfig": {
|
|
47
|
+
"access": "public"
|
|
48
|
+
},
|
|
49
|
+
"type": "module",
|
|
50
|
+
"devDependencies": {
|
|
51
|
+
"@rollup/plugin-commonjs": "^29.0.0",
|
|
52
|
+
"@rollup/plugin-node-resolve": "^16.0.3",
|
|
53
|
+
"@rollup/plugin-terser": "^0.4.4",
|
|
54
|
+
"@rollup/plugin-typescript": "^12.3.0",
|
|
55
|
+
"jsdom": "^27.0.1",
|
|
56
|
+
"rollup": "^4.53.3",
|
|
57
|
+
"tslib": "^2.8.1",
|
|
58
|
+
"vitest": "^3.2.4"
|
|
59
|
+
}
|
|
60
|
+
}
|